Cortex M3 PNJ
Cortex M3 PNJ
PNJ
The Cortex-M3 Embedded Systems:
The Cortex-M3 Processor Basics
Refer to Chapter 1, 2, and 3 in the reference book
“The Definitive Guide the ARM Cortex-M3”
History about ARM Processers
ARM company is known as the Advanced
RISC Machine, born in 1990
ARM does not manufacture processors or sell chips directly.
They license their process design to semiconductor
companies, known as intellectual property (IP) licensing.
-M
C
o
x
e
3
rt
m
P
S
ro
o
o
y
ce
re
e
ss
st
r
Trace Interface
Register
Interrupt Controller
Instruction
Fetch unit
Bank
Decoder
Debug
(NVIC)
Interrupts System Trace
ALU
m
M
In
o
a
ry
e
te
ce
rf
Memory
D
B
B
In
n
io
ru
u
a
ta
s
s
st
ct
Protection
Unit
Debug
Debug
B
In
n
n
o
u
te
e
rc
s
ct
Interface
Any cost?
1) overhead of state
switches
2) complex software
development
Instruction Set of Cortex-M3
Cortex-M3 supports only the Thumb-2 (including the
traditional Thumb) instruction set
Advantages:
1) No state switching overhead, saving
both execution time and instruction
space
2) No need to separate ARM code and
Thumb code source files, making
software development and maintenance
easier
Operation Modes in Cortex-M3
Two modes and two privilege levels
Cortex-M3 Basics: Registers
Name Functions (and Banked Registers) Name Functions
R5 General-Purpose Register
R6 General-Purpose Register
R7 General-Purpose Register
R8 General-Purpose Register
R9 General-Purpose Register
Register Register
contents contents restored
PUSH Data Processing POP
(Original register contents
destroyed)
SP
Memory Memory
Stack in Cortex-M3
Cortex-M3 uses a full-descending stack arrangement
The stack pointer decrements when new data is pushed in the stack
The assembly language syntax is as follows:
PUSH {R0} ; R13=R13-4, then Memory[R13]=R0
POP {R0} ; R0=Memory[R13], then R13=R13+4
Either R13 or SP can be used in your program codes (referring to the current
stack pointer you are using, either MSP or PSP)
A particular stack pointer (MSP/PSP) can be accessed using special register
access instructions (MRS/MSR)
Since PUSH and POP operations are always word aligned, the stack pointer
R13 bit 0 and bit 1 are hardwired to zero and always read as zero (RAZ)
Cortex-M3 Basics: Registers
Link Register R14
R14 is the link register (LR), used to store the return program counter
when a subroutine or function is called.
e.g., when using the BL (Branch with Link) instruction:
Cortex-M3 Basics: Registers
Program Counter R15
When you read this register you will find that the value is different than the
location of the executing instruction by 4, due to the pipelining of the
processor
Example:
0x1000 : MOV R0, PC ; R0 = 0x1004
When reading the PC, the LSB (bit 0) is always 0.
Why?
When writing to the PC, it will cause a branch. The LSB must be set to 1 to
indicate the Thumb state operations (setting to 0 implies to switch to the
ARM state, which will result in a fault exception in Cortex-M3)
Can you write to the PC in 8086? Why to set LSB of R15 (and R14) since
all instructions are half-word or word aligned?
Cortex-M3 Basics: Registers
Special Registers
The special registers in the Cortex-M3 processor
include:
Program Status Registers (PSRs)
Interrupt Mask Registers (PRIMASK, FAULTMASK, and BASEPRI)
Control Register (CONTROL)
When they are accessed as a collective item, the name xPSR is used (PSR used
in program codes).
EPSR and IPSR are read-only:
MRS R0, APSR ; Read Flag state into R0
MRS R0, IPSR ; Read Exception/Interrupt state
MRS R0, EPSR ; Read Execution state
MSR APSR, R0 ; Write Flag state
Accessing xPSR:
MRS R0, PSR ; Read the combined program status word
MSR PSR, R0 ; Write combined program state word
Bit Fields in Cortex-M3 Program Status Registers
Bit Description
N Negative
Z Zero
C Carry/borrow
V Overflow
Q Sticky saturation flag
ICI/IT Interrupt-Continuable Instruction (ICI) bits, IF-THEN
instruction status bit
T Thumb state, always 1; trying to clear this bit will cause a
fault exception
Exception Number Indicates which exception the processor is handling
Cortex-M3 Interrupt Mask Registers
CONTROL[0]
The CONTRL[0] bit is writable only in privileged level.
To access the Control register, the MRS and MSR instructions are used:
MRS R0, CONTROL ; Read CONTROL register into R0
MSR CONTROL, R0 ; Write R0 into CONTROL register
Cortex-M3 Basics: Operation Mode
Two modes and two privilege levels.
Interrupt
Exit
Interrupt Service
Routine(ISR)
Interrupt
Event
Main
Program
Stacking Unstacking Time
Interrupt
Exit
Interrupt Service
Routine(ISR)
Interrupt
Event
Main
Program
Stacking Unstacking Time
Example:
The reset is exception type 1. The address of the reset vector is 1 times 4,
which equals 0x00000004; and NMI vector (type 2) is located in 2 * 4 =
0x00000008
Vector Table Definition After Reset
Cortex-M3: Stack Memory Operations
Multiple registers can be pushed and popped in one
instruction, using comma to separate
PUSH {reglist} ;push the largest numbered register first
POP {reglist} ; pop the lowest numbered registers first
Example:
PUSH {R0-R7, LR} ; Save registers
…
POP {R0-R7, PC} ; Restore registers
Cortex-M3: Stack Implementation
The stack pointer (SP) points to the last data pushed to the stack memory,
and the SP decrements first in a new PUSH operation.
R0 0x12345678 PUSH {R0}
Occupied Occupied
Memory
Last pushed data SP Occupied
address Stack
-- 0x12345678 SP growth
-- --
low
For POP operations, the data is read from the memory location pointer by
SP, then the stack pointer is incremented. The contents in the memory
location are unchanged.
Occupied Occupied
Memory
Occupied Occupied SP
address
0x12345678 SP 0x12345678
-- --
R0 -- R0 0x12345678
Cortex-M3: The Two-Stack Model
The Cortex-M3 has two stack pointers: the Main Stack Pointer (MSP) and
the Process Stack Pointer (PSP).
The SP register to be used is controlled by the CONTROL register bit 1.
Control [1] = 0, both Thread mode and Handler mode use MSP (main
program and handlers share the same stack memory region)
Interrupt
Exit
Interrupt Service
Interrupt Routine(ISR)
Event
Main
Program Unstacking
Stacking Time
Which stack will the automatic stacking and unstacking mechanism use?
Cortex-M3: Reset Sequence
After the processor exits reset, it will read two words
from memory:
Address 0x00000000: default value of R13 (MSP)
Address 0x00000004: Reset vector (the starting address of startup
program)
Time
Initial Stack Pointer Value and Initial Program Counter (PC) Value Example
Flash
Boot Code
0x00000100
Other Exception Reset
Vectors Vector
0x00000004 0x00000101
0x00000000 0x20008000