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 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์์๋ง ์ง์ ์ฌ์ฉ

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 ์ํ

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 ํจ์จ โ.

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์ ์ ๋ชป ์์ธกํ ์ ์์

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 Endianness
- Big Endian / Little Endian ์ ํ ๊ฐ๋ฅ
- Cortex-M3์ code fetch๋ ํญ์ little-endian

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

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. |

์ ๋ฆฌ
| ์์ | ์ค๋ช |
|---|---|
| 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!
Please to write a comment.