Unit 1
Unit 1
Introduction
IN THIS CHAPTER
What Is the ARM Cortex-M3 Processor? ..................................................................................................... 1
Background of ARM and ARM Architecture ................................................................................................ 2
Instruction Set Development ...................................................................................................................... 7
The Thumb-2 Technology and Instruction Set Architecture.......................................................................... 8
Cortex-M3 Processor Applications ............................................................................................................. 9
Organization of This Book ....................................................................................................................... 1 0
Further Reading ...................................................................................................................................... 1 0
1
2 CHAPTER 1 Introduction
Enhanced determinism
FIGURE 1.1
The Cortex-M3 Processor versus the Cortex-M3-Based MCU.
T Thumb®
4 CHAPTER 1 Introduction
A profile
R profile
M profile
A Profile (ARMv7-A)
R Profile (ARMv7-R)
M Profile (ARMv7-M)
1.2 Background of ARM and ARM Architecture 5
v7-R (real-time)
e.g., Cortex-R4
Architecture v6-M
ARM926, 946,
966,
Intel XScale
ARM7TDMI,
Examples 920T,
Intel StrongARM Cortex-M0,
Cortex-M1 (FPGA)
FIGURE 1.2
The Evolution of ARM Processor Architecture.
Cortex-
M3 Technical Reference Manual (TRM)
T D M
I
6 CHAPTER 1 Introduction
2
(S J
2
1.3 Instruction Set Development 7
v4 v4T v5 v5E v6 v7
SIMD, v6
Enhanced memory
DSP support
instructions added
ARM added
Thumb-2
technology
Thumb introduced
Thumb instructions
introduced
Architecture development
FIGURE 1.3
Instruction Set Enhancement.
Thumb-2 technology
32-bit and 16-bit
Thumb instruction set ARMv7-M
architecture
Cortex-M3
Thumb
instructions
(16 bits)
FIGURE 1.4
The Relationship between the Thumb Instruction Set in Thumb-2 Technology and the Traditional Thumb.
3
1.5 Cortex-M3 Processor Applications 9
Low-cost microcontrollers
Automotive
Data communications
Industrial control
Consumer products
CHAPTER
Cortex-M3 Basics
IN THIS CHAPTER
Registers ................................................................................................................................................ 2 5
Special Registers ................................................................................................................................... 2 9
Operation Mode ...................................................................................................................................... 3 2
Exceptions and Interrupts ........................................................................................................................ 3 5
Vector Tables ......................................................................................................................................... 3 6
Stack Memory Operations ....................................................................................................................... 3 6
Reset Sequence ...................................................................................................................................... 4 0
3.1 REGISTERS
®
25
26 CHAPTER 3 Cortex-M3 Basics
FIGURE 3.1
Registers in the Cortex-M3.
Memory Memory
FIGURE 3.2
Basic Concept of Stack Memory.
When doing PUSH and POP operations, the pointer register, commonly called stack pointer, is adjusted
automatically to prevent next stack operations from corrupting previous stacked data. More details on stack
operations are provided on later part of this chapter.
subroutine_1
PUSH {R0-R7, R12, R14} ; Save registers
... ; Do your processing
POP {R0-R7, R12, R14} ; Restore registers
BX R14 ; Return to calling function
28 CHAPTER 3 Cortex-M3 Basics
R13, SP
R13/SP
SP_main
SP_process
xPSR
APSR N Z C V Q
FIGURE 3.3
Program Status Registers (PSRs) in the Cortex-M3.
FIGURE 3.4
Combined Program Status Registers (xPSR) in the Cortex-M3.
30 CHAPTER 3 Cortex-M3 Basics
ARM
N Z C V Q IT J Reserved GE[3:0] IT E A I F T M[4:0]
(general)
FIGURE 3.5
Current Program Status Registers in Traditional ARM Processors.
PSR
MRS r0, PSR ; Read the combined program status word
MSR PSR, r0 ; Write combined program state word
CONTROL[1]
32 CHAPTER 3 Cortex-M3 Basics
CONTROL[0]
Privileged User
Handler mode
When running an exception handler (not allowed)
(CONTROL[1] 0)
Reprogram
Switch to user CONTROL
Privileged mode by writing register
handler to CONTROL
Exception Exception
register handler handler
FIGURE 3.7
Switching of Operation Mode by Programming the Control Register or by Exceptions.
34 CHAPTER 3 Cortex-M3 Basics
User thread
FIGURE 3.8
Simple Applications Do Not Require User Access Level in Thread Mode.
Interrupt
exit
Interrupt service
routine (ISR)
Interrupt
event
Main
program Stacking Unstacking
Time
Thread mode Handler mode Thread mode
(privileged) (privileged) (privileged)
FIGURE 3.9
Switching Processor Mode at Interrupt.
Interrupt
exit
Interrupt service
routine (ISR)
Interrupt
event
Main
program Stacking Unstacking
Time
Thread mode Handler mode Thread mode
(user) (privileged) (user)
FIGURE 3.10
Switching Processor Mode and Privilege Level at Interrupt.
3.4 Exceptions and Interrupts 35
IRQ
vector table
Main program
...
; R0 = X, R1 = Y, R2 = Z
Subroutine
BL function1
function1
PUSH {R0} ; store R0 to stack & adjust SP
PUSH {R1} ; store R1 to stack & adjust SP
PUSH {R2} ; store R2 to stack & adjust SP
... ; Executing task (R0, R1 and R2
; could be changed)
POP {R2} ; restore R2 and SP re-adjusted
POP {R1} ; restore R1 and SP re-adjusted
POP {R0} ; restore R0 and SP re-adjusted
BX LR ; Return
; Back to main program
; R0 = X, R1 = Y, R2 = Z
... ; next instructions
FIGURE 3.11
Stack Operation Basics: One Register in Each Stack Operation.
38 CHAPTER 3 Cortex-M3 Basics
Main program
...
; R0 = X, R1 = Y, R2 = Z Subroutine
BL function 1
function 1
PUSH {R0-R2} ; Store R0, R1, R2 to stack
... ; Executing task (R0, R1 and R2
; could be changed)
POP {R0-R2} ; restore R0, R1, R2
BX LR ; Return
FIGURE 3.12
Stack Operation Basics: Multiple Register Stack Operation.
Main program
...
; R0 = X, R1 = Y, R2 = Z Subroutine
BL function 1
function 1
PUSH {R0-R2, LR} ; Save registers
; including link register
... ; Executing task (R0, R1 and R2
; could be changed)
POP {R0-R2, PC} ; Restore registers and
; return
; Back to main program
; R0 = X, R1 = Y, R2 = Z
... ; next instructions
FIGURE 3.13
Stack Operation Basics: Combining Stack POP and RETURN.
R0 0x12345678
PUSH {R0}
Occupied Occupied
Memory Occupied Occupied
address Last pushed data SP Occupied
- 0x12345678 SP
- - Stack
grow
FIGURE 3.14
Cortex-M3 Stack PUSH Implementation.
3.6 Stack Memory Operations 39
R0 - R0 0x12345678
FIGURE 3.15
Cortex-M3 Stack POP Implementation.
Interrupt
exit
Interrupt service
Interrupt routine (ISR)
event
Main
program Stacking Unstacking
Time
Thread mode Handler mode Thread mode
(use MSP) (use MSP) (use MSP)
FIGURE 3.16
CONTROL[1] 0: Both Thread Level and Handler Use Main Stack.
40 CHAPTER 3 Cortex-M3 Basics
Interrupt
exit
Interrupt service
routine (ISR)
Interrupt
event
Main
program Stacking Unstacking
Time
Thread mode Handler mode Thread mode
(use PSP) (use MSP) (use PSP)
FIGURE 3.17
CONTROL[1]=1: Thread Level Uses Process Stack and Handler Uses Main Stack.
Interrupt
exit
Interrupt service
routine (ISR)
Interrupt
event
Main
program Stacking Unstacking
Time
Thread mode Handler mode Thread mode
(use PSP) (use MSP) (use PSP)
FIGURE 3.17
CONTROL[1]=1: Thread Level Uses Process Stack and Handler Uses Main Stack.
Time
FIGURE 3.18
Reset Sequence.
Other memory
Initial SP value
0x20008000 0x20008000
0x20007 FFC 1st stacked item
0x20007 FF8 2nd stacked item
Stack grows
Stack
downwards
memory
0x20007 C00
Other memory
Flash
FIGURE 3.19
Initial Stack Pointer Value and Initial Program Counter Value Example.
42 CHAPTER 3 Cortex-M3 Basics
CHAPTER
Instruction Sets
IN THIS CHAPTER
Assembly Basics ..................................................................................................................................... 43
Instruction List ....................................................................................................................................... 46
Instruction Descriptions .......................................................................................................................... 52
Several Useful Instructions in the Cortex-M3 ........................................................................................... 70
label
opcode operand1, operand2, ...; Comments
label
43
44 CHAPTER 4 Instruction Sets
#number
S
S
AND R0, R1 ; Traditional Thumb syntax
ANDS R0, R0, R1 ; Equivalent UAL syntax (S suffix is added)
Continued
48 CHAPTER 4 Instruction Sets
Continued
50 CHAPTER 4 Instruction Sets
usage fault
MOV R8, R3
Rd
PUSH {R0, R4-R7, R9} ; Push R0, R4, R5, R6, R7, R9 into
; stack memory
POP {R2,R3} ; Pop R2 and R3 from stack
address1
LDR R0, =address1 ; R0 set to 0x4000
...
address1 ; address here is 0x4000
DCD 0x0 ; address1 contains data
...
C Register 0
0 Register C
Register C
Register C
Register C
FIGURE 4.1
Shift and Rotate Instructions.
C
60 CHAPTER 4 Instruction Sets
REV16.W
(Reverse bytes in half word)
REVSH.W
(Reverse bytes in bottom
half word and sign extend results)
sign extend
FIGURE 4.2
Operation of Reverse instructions.
In addition, if the subroutine you call is a C function, you might also need to save the contents in R0–R3
and R12 if these values will be needed at a later stage. According to AAPCS [Ref. 5], the contents in these
registers could be changed by a C function.
Q flag
Table 4.25 Flag Bits in APSR that Can Be Used for Conditional Branches
Flag PSR Bit Description
4.3 Instruction Descriptions 63
These flags can also have special results when used with shift and rotate instructions. Refer to the ARM v7-M
Architecture Application Level Reference Manual [Ref. 2] for details.
N Z C V
N Z C V
i = 5;
while (i != 0 ){
func1(); ; call a function
i−−;
}
...
BL strchr
CBNZ R0, email_looks_okay ; Branch if result is not zero
BL show_error_message
BL exit
email_looks_okay
...
ITxyz T E
cond
if (R1<R2) then
R2=R2−R1
R2=R2/2
else
R1=R1−R2
R1=R1/2
T E
4.3 Instruction Descriptions 67
Without
saturation
Dynamic
range 0 Amplify
With
signed
0
saturation
FIGURE 4.3
Signed Saturation Operation.
Dynamic With
Amplify unsigned
range
saturation
0 0 0
FIGURE 4.4
Unsigned Saturation Operation.
70 CHAPTER 4 Instruction Sets
SReg
IT block
x
y
z
cond
cond
4.4 Several Useful Instructions in the Cortex-M3 71
Table 4.31 Special Register Names for MRS and MSR Instructions
Symbol Description
IT <cond> IT EQ
instr1<cond> ADDEQ R0, R0, R1
IT<x> <cond> ITE GE
instr1<cond> ADDGE R0, R0, R1
instr2<cond or ~(cond)> ADDLT R0, R0, R3
IT<x><y> <cond> ITET GT
instr1<cond> ADDGT R0, R0, R1
instr2<cond or ~(cond)> ADDLE R0, R0, R3
instr3<cond or ~(cond)> ADDGT R2, R4, #1
IT<x><y><z> <cond> ITETT NE
instr1<cond> ADDNE R0, R0, R1
instr2<cond or ~(cond)> ADDEQ R0, R0, R3
instr3<cond or ~(cond)> ADDNE R2, R4, #1
instr4<cond or ~(cond)> MOVNE R5, R3
cond cond
E
x y z T E
cond cond
72 CHAPTER 4 Instruction Sets
Rd
REV R1, R0
REVH R2, R0
REVSH R1, R0
RBIT.W R0, R1
LDR R0,=0x1234FFFF
BFC.W R0, #4, #8
LDR R0,=0x12345678
LDR R1,=0x3355AACC
BFI.W R1, R0, #8, #16 ; Insert R0[15:0] to R1[23:8]
LDR R0,=0x5678ABCD
UBFX.W R1, R0, #4, #8
LDR R0,=0x5678ABCD
SBFX.W R1, R0, #4, #8
<Rxf> Rxf2>
Rn Rxf
LDR R2,=0x1000
LDRD.W R0, R1, [R2] ; This will gives R0 = memory[0x1000],
; R1 = memory[0x1004]
Rn Rm
Rn Rn
armasm
Cortex-M3 Implementation
Overview
IN THIS CHAPTER
The Pipeline ........................................................................................................................................... 99
A Detailed Block Diagram ..................................................................................................................... 101
Bus Interfaces on the Cortex-M3 ........................................................................................................... 104
Other Interfaces on the Cortex-M3 ......................................................................................................... 105
The External PPB .................................................................................................................................. 105
Typical Connections .............................................................................................................................. 106
Reset Types and Reset Signals .............................................................................................................. 107
99
100 CHAPTER 6 Cortex-M3 Implementation Overview
FIGURE 6.1
The Three-Stage Pipeline in the Cortex-M3.
Byte
3 2 1 0
Instruction Unaligned 32-bit Thumb-2
memory instruction in memory
N A1
N 4 B1 A2 Executing
N 8 C1 B2 Decoding
N 0xC D C2 Fetching
Instruction
buffer
(Inst C1) Pipeline stage
Instruction
Decode Execute
Instruction fetch
(Inst B) (Inst A)
(Inst C2 & D)
FIGURE 6.2
Use of a Buffer in the Instruction Fetch Unit to Improve 32-Bit Instruction Handling.
®
6.2 A Detailed Block Diagram 101
CM3Core
NVIC
WIC
Trace
ETM TPIU output
NMI NVIC CM3Core
Trigger
Interrupts SYSTICK
Instruction bus Data bus
AHB interconnect
(Internal BusMatrix)
FIGURE 6.3
The Cortex-M3 Processor System Block Diagram.
102 CHAPTER 6 Cortex-M3 Implementation Overview
SYSTICK Timer
WIC
MPU
BusMatrix
6.2 A Detailed Block Diagram 103
AHB to APB
SW-DP/SWJ-DP
AHB-AP
ETM
DWT
ITM
TPIU
FPB
ROM table
104 CHAPTER 6 Cortex-M3 Implementation Overview
CoreSight
Technology System Design Guide
2
106 CHAPTER 6 Cortex-M3 Implementation Overview
BusMatrix3
3
6.7 Reset Types and Reset Signals 107
Additional
debug
components
Cortex-M3
External private
peripheral bus
Instruction Data System
bus bus bus
External RAM
FIGURE 6.4
The Cortex-M3 Processor System Block Diagram.