STM32F10xxx Cortex-M3 programming manual.pdf

(1222 KB) Pobierz
STM32F10xxx Cortex-M3 programming manual
PM0056
Programming manual
STM32F10xxx Cortex-M3 programming manual
This programming manual provides information for application and system-level software
developers. It gives a full description of the STM32F10xxx Cortex™-M3 processor
programming model, instruction set and core peripherals.
The STM32F10xxx Cortex -M3 processor is a high performance 32-bit processor designed
for the microcontroller market. It offers significant benefits to developers, including:
Outstanding processing performance combined with fast interrupt handling
Enhanced system debug with extensive breakpoint and trace capabilities
Efficient processor core, system and memories
Ultra-low power consumption with integrated sleep modes
Platform security
April 2009
Doc ID 15491 Rev 1
1/137
219741068.002.png
Contents
PM0056
Contents
1
About this document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.1
Typographical conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.2
List of abbreviations for registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.3
About the STM32 Cortex-M3 processor and core peripherals . . . . . . . . . . 8
1.3.1
System level interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.3.2
Integrated configurable debug . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.3.3
Cortex-M3 processor features and benefits summary . . . . . . . . . . . . . . 10
1.3.4
Cortex-M3 core peripherals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2
The Cortex-M3 processor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.1
Programmers model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.1.1
Processor mode and privilege levels for software execution . . . . . . . . . 12
2.1.2
Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.1.3
Core registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.1.4
Exceptions and interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.1.5
Data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.1.6
The Cortex microcontroller software interface standard (CMSIS) . . . . . 22
2.2
Memory model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.2.1
Memory regions, types and attributes . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.2.2
Memory system ordering of memory accesses . . . . . . . . . . . . . . . . . . . 25
2.2.3
Behavior of memory accesses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.2.4
Software ordering of memory accesses . . . . . . . . . . . . . . . . . . . . . . . . 26
2.2.5
Bit-banding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.2.6
Memory endianness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.2.7
Synchronization primitives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2.2.8
Programming hints for the synchronization primitives . . . . . . . . . . . . . . 31
2.3
Exception model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.3.1
Exception states . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.3.2
Exception types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.3.3
Exception handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
2.3.4
Vector table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.3.5
Exception priorities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.3.6
Interrupt priority grouping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
2.3.7
Exception entry and return . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
2/137
Doc ID 15491 Rev 1
219741068.003.png
PM0056
Contents
2.4
Fault handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
2.4.1
Fault types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
2.4.2
Fault escalation and hard faults . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
2.4.3
Fault status registers and fault address registers . . . . . . . . . . . . . . . . . 41
2.4.4
Lockup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
2.5
Power management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
2.5.1
Entering sleep mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
2.5.2
Wakeup from sleep mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
2.5.3
The external event input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
2.5.4
Power management programming hints . . . . . . . . . . . . . . . . . . . . . . . . 43
3
The Cortex-M3 instruction set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
3.1
Instruction set summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
3.2
Intrinsic functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
3.3
About the instruction descriptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
3.3.1
Operands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
3.3.2
Restrictions when using PC or SP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
3.3.3
Flexible second operand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
3.3.4
Shift operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.3.5
Address alignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
3.3.6
PC-relative expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
3.3.7
Conditional execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
3.3.8
Instruction width selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
3.4
Memory access instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
3.4.1
ADR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
3.4.2
LDR and STR, immediate offset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
3.4.3
LDR and STR, register offset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
3.4.4
LDR and STR, unprivileged . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
3.4.5
LDR, PC-relative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
3.4.6
LDM and STM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
3.4.7
PUSH and POP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
3.4.8
LDREX and STREX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
3.4.9
CLREX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
3.5
General data processing instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
3.5.1
ADD, ADC, SUB, SBC, and RSB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
3.5.2
AND, ORR, EOR, BIC, and ORN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Doc ID 15491 Rev 1
3/137
219741068.004.png
Contents
PM0056
3.5.3
ASR, LSL, LSR, ROR, and RRX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
3.5.4
CLZ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
3.5.5
CMP and CMN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
3.5.6
MOV and MVN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
3.5.7
MOVT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
3.5.8
REV, REV16, REVSH, and RBIT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
3.5.9
TST and TEQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
3.6
Multiply and divide instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
3.6.1
MUL, MLA, and MLS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
3.6.2
UMULL, UMLAL, SMULL, and SMLAL . . . . . . . . . . . . . . . . . . . . . . . . . 84
3.6.3
SDIV and UDIV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
3.7
Saturating instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
3.7.1
SSAT and USAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
3.8
Bitfield instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
3.8.1
BFC and BFI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
3.8.2
SBFX and UBFX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
3.8.3
SXT and UXT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
3.8.4
Branch and control instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
3.8.5
B, BL, BX, and BLX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
3.8.6
CBZ and CBNZ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
3.8.7
IT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
3.8.8
TBB and TBH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
3.9
Miscellaneous instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
3.9.1 BKPT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
3.9.2 CPS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
3.9.3 DMB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
3.9.4 DSB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
3.9.5 ISB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
3.9.6 MRS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
3.9.7 MSR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
3.9.8 NOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
3.9.9 SEV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
3.9.10 SVC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
3.9.11 WFE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
3.9.12 WFI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
4
Core peripherals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
4/137
Doc ID 15491 Rev 1
219741068.005.png
PM0056
Contents
4.1
About the STM32 core peripherals . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
4.2
Nested vectored interrupt controller (NVIC) . . . . . . . . . . . . . . . . . . . . . . 104
4.2.1 The CMSIS mapping of the Cortex-M3 NVIC registers . . . . . . . . . . . . 105
4.2.2 Interrupt set-enable registers (NVIC_ISERx) . . . . . . . . . . . . . . . . . . . . 106
4.2.3 Interrupt clear-enable registers (NVIC_ICERx) . . . . . . . . . . . . . . . . . . 107
4.2.4 Interrupt set-pending registers (NVIC_ISPRx) . . . . . . . . . . . . . . . . . . . 108
4.2.5 Interrupt clear-pending registers (NVIC_ICPRx) . . . . . . . . . . . . . . . . . 109
4.2.6 Interrupt active bit registers (NVIC_IABRx) . . . . . . . . . . . . . . . . . . . . . 110
4.2.7 Interrupt priority registers (NVIC_IPRx) . . . . . . . . . . . . . . . . . . . . . . . . 111
4.2.8 Software trigger interrupt register (NVIC_STIR) . . . . . . . . . . . . . . . . . 112
4.2.9 Level-sensitive and pulse interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
4.2.10 NVIC design hints and tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
4.2.11 NVIC register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
4.3
System control block (SCB) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
4.3.1 CPUID base register (SCB_CPUID) . . . . . . . . . . . . . . . . . . . . . . . . . . 115
4.3.2 Interrupt control and state register (SCB_ICSR) . . . . . . . . . . . . . . . . . 116
4.3.3 Vector table offset register (SCB_VTOR) . . . . . . . . . . . . . . . . . . . . . . . 118
4.3.4 Application interrupt and reset control register (SCB_AIRCR) . . . . . . 119
4.3.5 System control register (SCB_SCR) . . . . . . . . . . . . . . . . . . . . . . . . . . 120
4.3.6 Configuration and control register (SCB_CCR) . . . . . . . . . . . . . . . . . . 121
4.3.7 System handler priority registers (SHPRx) . . . . . . . . . . . . . . . . . . . . . 122
4.3.8 System handler control and state register (SCB_SHCSR) . . . . . . . . . 124
4.3.9 Configurable fault status register (SCB_CFSR) . . . . . . . . . . . . . . . . . . 126
4.3.10 Hard fault status register (SCB_HFSR) . . . . . . . . . . . . . . . . . . . . . . . . 129
4.3.11 Memory management fault address register (SCB_MMFAR) . . . . . . . 130
4.3.12 Bus fault address register (SCB_BFAR) . . . . . . . . . . . . . . . . . . . . . . . 130
4.3.13 System control block design hints and tips . . . . . . . . . . . . . . . . . . . . . 131
4.3.14 SCB register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
4.4
SysTick timer (STK) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
4.4.1
SysTick control and status register (STK_CTRL) . . . . . . . . . . . . . . . . 132
4.4.2
SysTick reload value register (STK_LOAD) . . . . . . . . . . . . . . . . . . . . . 133
4.4.3
SysTick current value register (STK_VAL) . . . . . . . . . . . . . . . . . . . . . . 134
4.4.4
SysTick calibration value register (STK_CALIB) . . . . . . . . . . . . . . . . . 134
4.4.5
SysTick design hints and tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
4.4.6
SysTick register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
5
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Doc ID 15491 Rev 1
5/137
219741068.001.png
Zgłoś jeśli naruszono regulamin