5. Cortex M3

5์žฅ Cortex-M3 ๊ฐœ์š”

ARM Cortex-M3๋Š” ์ž„๋ฒ ๋””๋“œ ๋งˆ์ดํฌ๋กœ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์œ„ํ•œ ARMv7-M ์•„ํ‚คํ…์ฒ˜ ๊ธฐ๋ฐ˜ ํ”„๋กœ์„ธ์„œ๋‹ค. Thumb-2 ๋ช…๋ น์–ด ์ง€์›, 3-stage pipeline, NVIC(Nested Vectored Interrupt Controller), Memory-mapped bit-band ๋“ฑ์„ ํŠน์ง•์œผ๋กœ ํ•œ๋‹ค.

Specification

ํ•ญ๋ชฉ ๊ฐ’
ISA Support ARMv7-M (Thumb-2)
Pipeline 3 stages (Fetch-Decode-Execute)
Performance Efficiency 1.25 DMIPS/MHz
Memory Protection Optional MPU
Interrupt Priority Levels 3~256
Instruction/Data Bus Harvard architecture (separate I-bus, D-bus)
Sleep Modes Integrated WFI, WFE, sleep-on-exit
Debug Serial Wire / JTAG

Harvard Modified Memory Architecture

Harvard: Instruction๊ณผ data๋ฅผ ๋ณ„๋„ bus๋กœ ์ ‘๊ทผ โ€” parallel access ๊ฐ€๋Šฅ, bandwidth ์ฆ๊ฐ€.

Pipeline (3-Stage)

Cortex-M3๋Š” 3-stage pipeline์„ ์‚ฌ์šฉ:

  • Fetch: instruction์„ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ๊ฐ€์ ธ์˜ด
  • Decode: instruction ํ•ด์„, register ์ฝ๊ธฐ + branch prediction ์ˆ˜ํ–‰
  • Execute: ALU ์—ฐ์‚ฐ, memory access, writeback

Branch ์˜ˆ์ธก ์‹คํŒจ ์‹œ pipeline flush โ†’ stall ๋ฐœ์ƒ. Late arrival: ๋‹ค์Œ I๊ฐ€ branch ๊ฒฐ๊ณผ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๊ฒฝ์šฐ.

Cortex-M3 Pipeline ๋ฐ Branch Prediction

Cortex-M3 Architecture

  • Operating Mode: Thread mode, Handler mode
  • Processor Register Set: R0~R12 (general), R13 (SP), R14 (LR), R15 (PC), xPSR
  • Bank stacks: MSP (Main Stack Pointer) โ€” OS/exception, PSP (Process Stack Pointer) โ€” user thread

General Purpose Register (R0~R12)

  • Low registers (R0~R7): 16-bit Thumb I์—์„œ ์ง์ ‘ ์‚ฌ์šฉ
  • High registers (R8~R12): ๋Œ€๋ถ€๋ถ„ 32-bit Thumb-2 I์—์„œ๋งŒ ์ง์ ‘ ์‚ฌ์šฉ

Operating Mode ๋ฐ Register Set

Program Status Register (xPSR)

ARMv7-M์€ xPSR์„ ์„ธ ๋ถ€๋ถ„์œผ๋กœ ์ ‘๊ทผ:

  • APSR: N, Z, C, V, Q flags
  • IPSR: ํ˜„์žฌ ์‹คํ–‰ ์ค‘์ธ exception ๋ฒˆํ˜ธ
  • EPSR: Thumb state bit, IT block state, ICI bits

APSR Flags

  • N(bit 31): Negative, Z(bit 30): Zero, C(bit 29): Carry, V(bit 28): Overflow, Q(bit 27): Sticky saturation

IPSR

  • bits[8:0]: ํ˜„์žฌ exception number. Thread mode์—์„œ 0.

EPSR

  • Thumb bit: Cortex-M์€ Thumb mode๋งŒ ์ง€์› โ†’ ํ•ญ์ƒ 1
  • IT bit: IT (If-Then) block์˜ conditional execution ์ƒํƒœ

xPSR ๊ตฌ์กฐ

Special Purpose Registers

PRIMASK

1-bit flag. 1์ด๋ฉด configurable exception mask (NMI์™€ HardFault๋Š” ์˜ˆ์™ธ).

FAULTMASK

1-bit flag. 1์ด๋ฉด NMI ์ œ์™ธํ•œ ๋ชจ๋“  exception masking.

BASEPRI

8-bit priority. ์ด ๊ฐ’๋ณด๋‹ค ๋‚ฎ์€ ์šฐ์„ ์ˆœ์œ„์˜ interrupt๋ฅผ mask.

CONTROL

  • SPSEL (bit 0): active stack pointer (0=MSP, 1=PSP)
  • nPRIV (bit 1): privileged level (0=privileged, 1=unprivileged)

IT (If-Then) Block

Thumb-2์˜ Conditional Execution. IT ๋ช…๋ น์–ด ๋’ค์— ์ตœ๋Œ€ 4๊ฐœ์˜ conditional I๋ฅผ ๋ฐฐ์น˜:

CMP r0, r1
ITTE  EQ
MOVEQ r2, r3
ADDEQ r4, r4, #1
MOVNE r5, r6

โ†’ Branch ์—†์ด ์กฐ๊ฑด๋ถ€ ์‹คํ–‰ โ†’ pipeline ํšจ์œจ โ†‘.

Special Registers ๋ฐ IT Block

CONTROL Register ์ƒ์„ธ

Bits Name Function
[31:2] Reserved -
[1] SPSEL Active stack pointer (0=MSP, 1=PSP). Handler mode: ํ•ญ์ƒ 0
[0] nPRIV Thread mode privilege (0=Privileged, 1=Unprivileged)
  • SPSEL: ํ˜„์žฌ ํ™œ์„ฑํ™”๋œ stack pointer. Handler mode์—์„œ๋Š” ํ•ญ์ƒ 0์œผ๋กœ ์ฝํžˆ๊ณ  ์ˆ˜์ • ๋ถˆ๊ฐ€
  • OS ํ™˜๊ฒฝ์—์„œ ARM์€ Thread mode์˜ thread๋Š” ํ•ญ์ƒ process stack, kernel/exception handler๋Š” main stack ์ด์šฉ์„ ์ถ”์ฒœ

Exceptions and Interrupts

  • Interrupt/system exception ์ง€์›
  • Exception์€ SW control์˜ normal flow๋ฅผ ๋ณ€ํ™”์‹œํ‚ด
  • Reset์„ ์ œ์™ธํ•œ ๋ชจ๋“  exception์€ Handler mode์—์„œ ์ˆ˜ํ–‰
  • NVIC reg๋ฅผ ํ†ตํ•ด interrupt handling ๊ฐ€๋Šฅ (์ค‘๋ณต๋œ interrupt ์ฒ˜๋ฆฌ)

Data Type

  • 32-bit word, 16-bit half-word, 8-bit byte
  • Data memory access: little-endian/big-endian ๋ชจ๋‘ ์ง€์›
  • Instruction memory, PPB access: little-endian๋งŒ ์ง€์›

Memory Model

$2^{32}$ B = 4 GB์˜ memory.

0xFFFFFFFF  Vendor-specific (511 MB)
0xE0100000  
0xE00FFFFF  Private peripheral bus (1.0 MB)
0xE0000000  
0xDFFFFFFF  External device (1.0 GB)
0xA0000000  
0x9FFFFFFF  External RAM (1.0 GB)
0x60000000  
0x5FFFFFFF  Peripheral (0.5 GB)  โ€” bit-banding region
0x40000000  
0x3FFFFFFF  SRAM (0.5 GB)  โ€” bit-banding region
0x20000000  
0x1FFFFFFF  Code (0.5 GB)
0x00000000  

Bit-Banding

Cortex-M3์˜ ํŠน์ง•: Atomic operation์„ bit data๋กœ ์ œ๊ณต (ARM์—๋งŒ ์กด์žฌ).

  • Peripheral 0.5GB / SRAM 0.5GB region์—์„œ 1MB Bit band region โ†’ 32MB Bit band alias
  • Alias์˜ ํ•œ word(32-bit)๋Š” bit-band region์˜ ํ•œ bit์— ๋Œ€์‘
  • Alias ์ฃผ์†Œ์— read/writeํ•˜๋ฉด ํ•ด๋‹น bit๋งŒ atomicํ•˜๊ฒŒ read/modify ๊ฐ€๋Šฅ

Instruction Prefetch and Branch Prediction

Instruction Prefetch: ์ˆ˜ํ–‰ ์ „ I์„ ์˜ˆ์ธกํ•ด์„œ ๋ฏธ๋ฆฌ ์ฝ์–ด๋‘ .

  • ์˜ˆ์ธก์ด ํ‹€๋ฆฐ I์€ cache pollution์„ ์ผ์œผ์ผœ ์„ฑ๋Šฅ ์ €ํ•˜
  • Branch I ๋‹ค์Œ์˜ I์„ ์ž˜ ๋ชป ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ์Œ

Control/Memory Model/Bit-Banding

Software Ordering of Memory Access

  • DMB (Data Memory Barrier): ์ด์ „ memory access ์™„๋ฃŒ ํ›„ ์ดํ›„ access
  • DSB (Data Synchronization Barrier): DMB + instruction ์™„๋ฃŒ๊นŒ์ง€ ๋ณด์žฅ
  • ISB (Instruction Synchronization Barrier): Pipeline flush

Bit-Banding ์ฃผ์†Œ ๋ณ€ํ™˜

bit_word_offset = (byte_offset x 32) + (bit_number x 4)
bit_word_addr = bit_band_base + bit_word_offset

์˜ˆ: SRAM bit-band region 0x20000000 โ†’ alias 0x22000000

  • 0x20000000 bit 0 โ†’ 0x22000000
  • 0x20000000 bit 1 โ†’ 0x22000004
  • 0x20000000 bit 2 โ†’ 0x22000008

Memory Barrier ๋ฐ Bit-Banding

Memory Endianness

  • Big Endian / Little Endian ์„ ํƒ ๊ฐ€๋Šฅ
  • Cortex-M3์˜ code fetch๋Š” ํ•ญ์ƒ little-endian

Bit-banding ๊ณ„์‚ฐ ์˜ˆ์ œ

Exception Model

Cortex-M3์˜ Exception ์ฒด๊ณ„. Reset, NMI, HardFault, MemManage, BusFault, UsageFault, SVCall, DebugMonitor, PendSV, SysTick, IRQ[0..239].

Exception Type (์šฐ์„ ์ˆœ์œ„ ์ˆœ)

  • Reset: ๊ฐ€์žฅ ๋†’์Œ (fixed)
  • NMI: 2๋ฒˆ์งธ (fixed)
  • HardFault: fault escalation
  • Configurable: MemManage, BusFault, UsageFault, SVCall, DebugMonitor, PendSV, SysTick, IRQ
  • IRQ: ์™ธ๋ถ€ peripheral

Exception Handlers

  • ISR (Interrupt service routine): IRQ handler
  • Fault handler: HardFault, MemManage, UsageFault, BusFault
  • System handler: NMI, PendSV, SVCall, SysTick

Exception Model ๋ฐ Properties

Vector Table

Stack pointer์˜ reset๊ฐ’์„ ํฌํ•จ. ๋ชจ๋“  exception handler์˜ start address๊ฐ€ ๋“ค์–ด ์žˆ์Œ.

Exception number IRQ number Offset Vector
16+n n 0x0040+4n IRQn
15 -1 0x003C SysTick
14 -2 0x0038 PendSV
11 -5 0x002C SVCall
6 -10 0x0018 Usage fault
5 -11 0x0014 Bus fault
4 -12 0x0010 Memory management fault
3 -13 0x000C Hard fault
2 -14 0x0008 NMI
1 - 0x0004 Reset
0 - 0x0000 Initial SP value

Exception Priorities

๋‚ฎ์€ value๊ฐ€ ๋†’์€ ์šฐ์„ ์ˆœ์œ„.

  • Reset, HardFault, NMI๋ฅผ ์ œ์™ธํ•˜๊ณ  ์šฐ์„  ์ˆœ์œ„๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Œ
  • ์„ค์ •ํ•˜์ง€ ์•Š์œผ๋ฉด 0 โ†’ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Œ

Exception Entry and Return

Preemption

ํ”„๋กœ์„ธ์„œ๊ฐ€ Exception Handler๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ์ค‘, ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ exception์ด ๋ฐœ์ƒํ•˜๋ฉด ์„ ์  โ†’ Nested exception.

Return

์™„๋ฃŒ๋œ exception handler๋Š” late-arriving exception์„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š์Œ. Pop ๋„์ค‘์€ ๊ฐ€๋Šฅํ•˜๋‚˜ ์™„๋ฃŒ ๋˜๋ฉด ์ˆ˜ํ–‰ X.

โ†’ Tail-chaining, Late-arriving ์ตœ์ ํ™”.

Exception Entry โ€” Stacking

  • Stacking: Interrupt ๋ฐœ์ƒ ์‹œ stack์— ๋ฐฑ์—…, tail-chained๋‚˜ late-arriving์€ ์ˆ˜ํ–‰ X
  • Stack frame: 8๊ฐœ์˜ word ์ž๋™ ๋ฐฑ์—… (HW)
SP + 0x1C    <previous>
SP + 0x18    xPSR
SP + 0x14    PC
SP + 0x10    LR
SP + 0x0C    R12
SP + 0x08    R3
SP + 0x04    R2
SP + 0x00    R1, R0

Full descending stack: Stack pointer๊ฐ€ stack frame์˜ ๋‚ฎ์€ ๊ณณ.

Exception Return

EXC_RETURN[3:0] ๊ฐ’์— ๋”ฐ๋ผ return stack๊ณผ processor mode ๊ฒฐ์ •.

EXC_RETURN Description
0xFFFFFFF1 Return to Handler mode. State from MSP. Execution uses MSP.
0xFFFFFFF9 Return to Thread mode. State from MSP. Execution uses MSP.
0xFFFFFFFD Return to Thread mode. State from PSP. Execution uses PSP.

Vector Table, Priorities, Exception Entry/Return

์ •๋ฆฌ

์š”์†Œ ์„ค๋ช…
ARMv7-M, Thumb-2 16/32-bit ํ˜ผ์šฉ instruction
3-stage pipeline Fetch-Decode-Execute + branch prediction
Harvard arch I-bus, D-bus ๋ถ„๋ฆฌ
MSP / PSP OS/thread์šฉ stack pointer ๋ถ„๋ฆฌ
xPSR = APSR+IPSR+EPSR Flag, exception ๋ฒˆํ˜ธ, Thumb/IT ์ƒํƒœ
Bit-banding Atomic bit access via alias address
NVIC + Tail-chaining ์ €์ง€์—ฐ nested interrupt
Memory barrier DMB/DSB/ISB๋กœ multi-master ๋™๊ธฐํ™”

Cortex-M3๋Š” ์ €์ „๋ ฅ ์ž„๋ฒ ๋””๋“œ MCU ์‹œ์žฅ(STM32, LPC17xx ๋“ฑ)์˜ ํ‘œ์ค€์ด ๋˜์—ˆ๊ณ , ์ดํ›„ Cortex-M4, M7๋กœ ํ™•์žฅ๋œ๋‹ค.

๋น„์Šทํ•œ ๊ธ€ ์ถ”์ฒœ

Comments (0)

No comments yet. Be the first to comment!