STM32F0xxx Cortex M0 Programming Manual
Programming%20Manual
stm32f0%20Programming%20Manual
User Manual:
Open the PDF directly: View PDF
.
Page Count: 91
PM0215
Programming manual
STM32F0xxx Cortex-M0 programming manual
Introduction
This programming manual provides information for application and system-level software
developers. It gives a full description of the STM32 Cortex™-M0 processor programming
model, instruction set and core peripherals.
The STM32 Cortex™-M0 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
Table 1.
Applicable products
Type
Microcontroller
April 2012
Part numbers
STM32F0xxx
Doc ID 022979 Rev 1
1/91
www.st.com
Contents
PM0215
Contents
1
2
About this document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.1
Typographical conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.2
List of abbreviations for registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.3
About the STM32 Cortex-M0 processor and core peripherals . . . . . . . . . . 9
1.3.1
System level interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.3.2
Integrated configurable debug . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.3.3
Cortex-M0 processor features and benefits summary . . . . . . . . . . . . . . 10
1.3.4
Cortex-M0 core peripherals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
The STM32 Cortex-M0 processor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.1
2.2
2.3
Programmers model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.1.1
Processor modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.1.2
Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.1.3
Core registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.1.4
Exceptions and interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.1.5
Data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.1.6
The Cortex microcontroller software interface standard (CMSIS) . . . . . 17
Memory model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.2.1
Memory regions, types and attributes . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.2.2
Memory system ordering of memory accesses . . . . . . . . . . . . . . . . . . . 19
2.2.3
Behavior of memory accesses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.2.4
Software ordering of memory accesses . . . . . . . . . . . . . . . . . . . . . . . . 20
2.2.5
Memory endianness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Exception model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.3.1
Exception states . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.3.2
Exception types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.3.3
Exception handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.3.4
Vector table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.3.5
Exception priorities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.3.6
Exception entry and return . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.4
Fault handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.5
Power management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.5.1
2/91
Entering sleep mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Doc ID 022979 Rev 1
PM0215
3
Contents
2.5.2
Wakeup from sleep mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.5.3
The external event input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2.5.4
Power management programming hints . . . . . . . . . . . . . . . . . . . . . . . . 30
The STM32 Cortex-M0 instruction set . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.1
Instruction set summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.2
CMSIS intrinsic functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.3
About the instruction descriptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.4
3.5
3.6
3.3.1
Operands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.3.2
Restrictions when using PC or SP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.3.3
Shift operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.3.4
Address alignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3.3.5
PC-relative expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3.3.6
Conditional execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Memory access instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
3.4.1
ADR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
3.4.2
LDR and STR, immediate offset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
3.4.3
LDR and STR, register offset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
3.4.4
LDR, PC-relative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
3.4.5
LDM and STM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
3.4.6
PUSH and POP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
General data processing instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
3.5.1
ADD{S}, ADCS, SUB{S}, SBCS, and RSBS . . . . . . . . . . . . . . . . . . . . . 49
3.5.2
ANDS, ORRS, EORS and BICS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
3.5.3
ASRS, LSLS, LSRS and RORS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.5.4
CMP and CMN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
3.5.5
MOV, MOVS and MVNS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
3.5.6
MULS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
3.5.7
REV, REV16, and REVSH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
3.5.8
SXTB, SXTH, UXTB and UXTH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
3.5.9
TST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Branch and control instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
3.6.1
3.7
B, BL, BX, and BLX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Miscellaneous instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
3.7.1
BKPT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
3.7.2
CPSID CPSIE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Doc ID 022979 Rev 1
3/91
Contents
4
PM0215
DMB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
3.7.4
DSB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
3.7.5
ISB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
3.7.6
MRS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
3.7.7
MSR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
3.7.8
NOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
3.7.9
SEV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
3.7.10
SVC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
3.7.11
WFE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
3.7.12
WFI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Core peripherals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
4.1
About the STM32 Cortex-M0 core peripherals . . . . . . . . . . . . . . . . . . . . . 69
4.2
Nested vectored interrupt controller (NVIC) . . . . . . . . . . . . . . . . . . . . . . . 70
4.3
4.4
4/91
3.7.3
4.2.1
Accessing the Cortex-M0 NVIC registers using CMSIS . . . . . . . . . . . . 70
4.2.2
Interrupt set-enable register (ISER) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
4.2.3
Interrupt clear-enable register (ICER) . . . . . . . . . . . . . . . . . . . . . . . . . . 71
4.2.4
Interrupt set-pending register (ISPR) . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
4.2.5
Interrupt clear-pending register (ICPR) . . . . . . . . . . . . . . . . . . . . . . . . . 72
4.2.6
Interrupt priority register (IPR0-IPR7) . . . . . . . . . . . . . . . . . . . . . . . . . . 73
4.2.7
Level-sensitive and pulse interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
4.2.8
NVIC design hints and tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
4.2.9
NVIC register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
System control block (SCB) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
4.3.1
CPUID base register (CPUID) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
4.3.2
Interrupt control and state register (ICSR) . . . . . . . . . . . . . . . . . . . . . . . 78
4.3.3
Application interrupt and reset control register (AIRCR) . . . . . . . . . . . . 80
4.3.4
System control register (SCR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
4.3.5
Configuration and control register (CCR) . . . . . . . . . . . . . . . . . . . . . . . 82
4.3.6
System handler priority registers (SHPRx) . . . . . . . . . . . . . . . . . . . . . . 83
4.3.7
SCB usage hints and tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
4.3.8
SCB register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
SysTick timer (STK) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
4.4.1
SysTick control and status register (STK_CSR) . . . . . . . . . . . . . . . . . . 86
4.4.2
SysTick reload value register (STK_RVR) . . . . . . . . . . . . . . . . . . . . . . . 87
4.4.3
SysTick current value register (STK_CVR) . . . . . . . . . . . . . . . . . . . . . . 87
4.4.4
SysTick calibration value register (STK_CALIB) . . . . . . . . . . . . . . . . . . 88
Doc ID 022979 Rev 1
PM0215
5
Contents
4.4.5
SysTick design hints and tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
4.4.6
SysTick register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Doc ID 022979 Rev 1
5/91
List of tables
PM0215
List of tables
Table 1.
Table 2.
Table 3.
Table 4.
Table 5.
Table 6.
Table 7.
Table 8.
Table 9.
Table 10.
Table 11.
Table 12.
Table 13.
Table 14.
Table 15.
Table 16.
Table 17.
Table 18.
Table 19.
Table 20.
Table 21.
Table 22.
Table 23.
Table 24.
Table 25.
Table 26.
Table 27.
Table 28.
Table 29.
Table 30.
Table 31.
Table 32.
Table 33.
Table 34.
Table 35.
6/91
Applicable products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Summary of processor mode and stack usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Core register set summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
PSR register combinations and attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
APSR bit definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
IPSR bit definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
EPSR bit definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
PRIMASK register bit definitions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
CONTROL register bit definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Ordering of memory accesses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Memory access behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Properties of the different exception types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Exception return behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Cortex-M0 instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
CMSIS intrinsic functions to generate some Cortex-M0 instructions . . . . . . . . . . . . . . . . . 35
CMSIS intrinsic functions to access the special registers. . . . . . . . . . . . . . . . . . . . . . . . . . 35
Condition code suffixes and their relationship with the flags . . . . . . . . . . . . . . . . . . . . . . . 40
Memory access instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Data processing instructions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
ADCS, ADD, RSBS, SBCS and SUB operand restrictions. . . . . . . . . . . . . . . . . . . . . . . . . 50
Branch and control instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Branch ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Miscellaneous instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
STM32 core peripheral register regions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
NVIC register summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
CMSIS access NVIC functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
IPR bit assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
CMSIS functions for NVIC control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
NVIC register map and reset values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Summary of the system control block registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
System fault handler priority fields and registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
SCB register map and reset values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
System timer registers summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
SysTick register map and reset values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Doc ID 022979 Rev 1
PM0215
List of figures
List of figures
Figure 1.
Figure 2.
Figure 3.
Figure 4.
Figure 5.
Figure 6.
Figure 7.
Figure 8.
Figure 9.
Figure 10.
Figure 11.
Figure 12.
Figure 13.
Figure 14.
STM32 Cortex-M0 implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Processor core registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
APSR, IPSR and EPSR bit assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
PRIMASK register bit assignments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
CONTROL register bit assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Memory map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Little-endian example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Vector table. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Cortex-M0 stack frame layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
ASR#3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
LSR#3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
LSL#3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
ROR #3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
IPR register mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Doc ID 022979 Rev 1
7/91
About this document
1
PM0215
About this document
This document provides the information required for application and system-level software
development. It does not provide information on debug components, features, or operation.
This material is for microcontroller software and hardware engineers, including those who
have no experience of ARM products.
1.1
Typographical conventions
The typographical conventions used in this document are:
italic
Highlights important notes, introduces special terminology, denotes
internal cross-references, and citations.
< and >
Enclose replaceable terms for assembler syntax where they appear in
code or code fragments. For example:
LDRSB
Source Exif Data:
File Type : PDF
File Type Extension : pdf
MIME Type : application/pdf
PDF Version : 1.3
Linearized : No
Page Layout : SinglePage
Page Mode : UseNone
Page Count : 91
Creator : C2 v4.2.0220 build 670 - c2_rendition_config : Techlit_Active
Producer : Acrobat Distiller 9.0.0 (Windows); modified using iText 2.1.7 by 1T3XT
Title : STM32F0xxx Cortex-M0 programming manual
Keywords : Technical Literature, 022979, Product Development, Specification, Programming manual, STM32F0 Series, STM32F051C4, STM32F051C6, STM32F051C8, STM32F051K4, STM32F051K6, STM32F051K8, STM32F051R4, STM32F051R6, STM32F051R8, STM32F0DISCOVERY, STM32F061K8, STM32F061R8, STM32F050C6, STM32F050C4, STM32F050K6, STM32F050G6, STM32F050K4, STM32F050G4, STM32F051VC
Modify Date : 2018:03:12 11:37:16+01:00
Subject : -
Author : STMICROELECTRONICS
Create Date : 2012:04:27 16:40:40Z
EXIF Metadata provided by EXIF.tools