Programming manual.pdf

(1181 KB) Pobierz
STM32F10xxx/20xxx/21xxx/L1xxxx Cortex-M3 programming manual
PM0056
Programming manual
STM32F10xxx/20xxx/21xxx/L1xxxx
Cortex-M3 programming manual
This programming manual provides information for application and system-level software
developers. It gives a full description of the STM32F10xxx/20xxx/21xxx/L1xxxx Cortex™-
M3 processor programming model, instruction set and core peripherals.
The STM32F10xxx/20xxx/21xxx/L1xxxx 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
March 2011
Doc ID 15491 Rev 4
1/155
648950078.002.png
Contents
PM0056
Contents
1
About this document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.1
Typographical conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.2
List of abbreviations for registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.3
About the STM32 Cortex-M3 processor and core peripherals . . . . . . . . . 10
1.3.1
System level interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.3.2
Integrated configurable debug . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.3.3
Cortex-M3 processor features and benefits summary . . . . . . . . . . . . . . 12
1.3.4
Cortex-M3 core peripherals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2
The Cortex-M3 processor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.1
Programmers model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.1.1
Processor mode and privilege levels for software execution . . . . . . . . . 14
2.1.2
Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.1.3
Core registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.1.4
Exceptions and interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.1.5
Data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.1.6
The Cortex microcontroller software interface standard (CMSIS) . . . . . 24
2.2
Memory model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.2.1
Memory regions, types and attributes . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.2.2
Memory system ordering of memory accesses . . . . . . . . . . . . . . . . . . . 27
2.2.3
Behavior of memory accesses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.2.4
Software ordering of memory accesses . . . . . . . . . . . . . . . . . . . . . . . . 28
2.2.5
Bit-banding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.2.6
Memory endianness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
2.2.7
Synchronization primitives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.2.8
Programming hints for the synchronization primitives . . . . . . . . . . . . . . 33
2.3
Exception model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
2.3.1
Exception states . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
2.3.2
Exception types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
2.3.3
Exception handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
2.3.4
Vector table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
2.3.5
Exception priorities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
2.3.6
Interrupt priority grouping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
2.3.7
Exception entry and return . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
2/155
Doc ID 15491 Rev 4
648950078.003.png
PM0056
Contents
2.4
Fault handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
2.4.1
Fault types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
2.4.2
Fault escalation and hard faults . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
2.4.3
Fault status registers and fault address registers . . . . . . . . . . . . . . . . . 43
2.4.4
Lockup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
2.5
Power management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
2.5.1
Entering sleep mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
2.5.2
Wakeup from sleep mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
2.5.3
The external event input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
2.5.4
Power management programming hints . . . . . . . . . . . . . . . . . . . . . . . . 45
3
The Cortex-M3 instruction set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
3.1
Instruction set summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
3.2
Intrinsic functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
3.3
About the instruction descriptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.3.1
Operands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.3.2
Restrictions when using PC or SP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
3.3.3
Flexible second operand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
3.3.4
Shift operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
3.3.5
Address alignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
3.3.6
PC-relative expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
3.3.7
Conditional execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
3.3.8
Instruction width selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
3.4
Memory access instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
3.4.1
ADR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
3.4.2
LDR and STR, immediate offset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
3.4.3
LDR and STR, register offset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
3.4.4
LDR and STR, unprivileged . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
3.4.5
LDR, PC-relative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
3.4.6
LDM and STM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
3.4.7
PUSH and POP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
3.4.8
LDREX and STREX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
3.4.9
CLREX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
3.5
General data processing instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
3.5.1
ADD, ADC, SUB, SBC, and RSB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
3.5.2
AND, ORR, EOR, BIC, and ORN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Doc ID 15491 Rev 4
3/155
648950078.004.png
Contents
PM0056
3.5.3
ASR, LSL, LSR, ROR, and RRX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
3.5.4
CLZ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
3.5.5
CMP and CMN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
3.5.6
MOV and MVN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
3.5.7
MOVT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
3.5.8
REV, REV16, REVSH, and RBIT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
3.5.9
TST and TEQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
3.6
Multiply and divide instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
3.6.1
MUL, MLA, and MLS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
3.6.2
UMULL, UMLAL, SMULL, and SMLAL . . . . . . . . . . . . . . . . . . . . . . . . . 86
3.6.3
SDIV and UDIV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
3.7
Saturating instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
3.7.1
SSAT and USAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
3.8
Bitfield instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
3.8.1
BFC and BFI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
3.8.2
SBFX and UBFX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
3.8.3
SXT and UXT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
3.8.4
Branch and control instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
3.8.5
B, BL, BX, and BLX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
3.8.6
CBZ and CBNZ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
3.8.7
IT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
3.8.8
TBB and TBH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
3.9
Miscellaneous instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
3.9.1 BKPT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
3.9.2 CPS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
3.9.3 DMB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
3.9.4 DSB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
3.9.5 ISB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
3.9.6 MRS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
3.9.7 MSR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
3.9.8 NOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
3.9.9 SEV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
3.9.10 SVC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
3.9.11 WFE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
3.9.12 WFI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
4
Core peripherals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
4/155
Doc ID 15491 Rev 4
648950078.005.png
PM0056
Contents
4.1
About the STM32 core peripherals . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
4.2
Memory protection unit (MPU) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
4.2.1
MPU access permission attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
4.2.2
MPU mismatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
4.2.3
Updating an MPU region . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
4.2.4
MPU design hints and tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
4.2.5
MPU type register (MPU_TYPER) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
4.2.6
MPU control register (MPU_CR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
4.2.7
MPU region number register (MPU_RNR) . . . . . . . . . . . . . . . . . . . . . 114
4.2.8
MPU region base address register (MPU_RBAR) . . . . . . . . . . . . . . . . 115
4.2.9
MPU region attribute and size register (MPU_RASR) . . . . . . . . . . . . . 117
4.3
Nested vectored interrupt controller (NVIC) . . . . . . . . . . . . . . . . . . . . . . 119
4.3.1 The CMSIS mapping of the Cortex-M3 NVIC registers . . . . . . . . . . . . 120
4.3.2 Interrupt set-enable registers (NVIC_ISERx) . . . . . . . . . . . . . . . . . . . . 121
4.3.3 Interrupt clear-enable registers (NVIC_ICERx) . . . . . . . . . . . . . . . . . . 122
4.3.4 Interrupt set-pending registers (NVIC_ISPRx) . . . . . . . . . . . . . . . . . . . 123
4.3.5 Interrupt clear-pending registers (NVIC_ICPRx) . . . . . . . . . . . . . . . . . 124
4.3.6 Interrupt active bit registers (NVIC_IABRx) . . . . . . . . . . . . . . . . . . . . . 125
4.3.7 Interrupt priority registers (NVIC_IPRx) . . . . . . . . . . . . . . . . . . . . . . . . 126
4.3.8 Software trigger interrupt register (NVIC_STIR) . . . . . . . . . . . . . . . . . 127
4.3.9 Level-sensitive and pulse interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
4.3.10 NVIC design hints and tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
4.3.11 NVIC register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
4.4
System control block (SCB) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
4.4.1 Auxiliary control register (SCB_ACTLR) . . . . . . . . . . . . . . . . . . . . . . . 130
4.4.2 CPUID base register (SCB_CPUID) . . . . . . . . . . . . . . . . . . . . . . . . . . 131
4.4.3 Interrupt control and state register (SCB_ICSR) . . . . . . . . . . . . . . . . . 132
4.4.4 Vector table offset register (SCB_VTOR) . . . . . . . . . . . . . . . . . . . . . . . 134
4.4.5 Application interrupt and reset control register (SCB_AIRCR) . . . . . . 135
4.4.6 System control register (SCB_SCR) . . . . . . . . . . . . . . . . . . . . . . . . . . 136
4.4.7 Configuration and control register (SCB_CCR) . . . . . . . . . . . . . . . . . . 137
4.4.8 System handler priority registers (SHPRx) . . . . . . . . . . . . . . . . . . . . . 138
4.4.9 System handler control and state register (SCB_SHCSR) . . . . . . . . . 140
4.4.10 Configurable fault status register (SCB_CFSR) . . . . . . . . . . . . . . . . . . 142
4.4.11 Hard fault status register (SCB_HFSR) . . . . . . . . . . . . . . . . . . . . . . . . 145
4.4.12 Memory management fault address register (SCB_MMFAR) . . . . . . . 147
Doc ID 15491 Rev 4
5/155
648950078.001.png
Zgłoś jeśli naruszono regulamin