STM32 Reference Manual
STM32 Reference Manual
RM0008
Reference manual
STM32F101xx, STM32F102xx, STM32F103xx, STM32F105xx
and STM32F107xx advanced ARM-based 32-bit MCUs
Introduction
This reference manual targets application developers. It provides complete information on
how to use the STM32F101xx, STM32F102xx, STM32F103xx and
STM32F105xx/STM32F107xx microcontroller memory and peripherals. The STM32F101xx,
STM32F102xx, STM32F103xx and STM32F105xx/STM32F107xx will be referred to as
STM32F10xxx throughout the document, unless otherwise specified.
The STM32F10xxx is a family of microcontrollers with different memory sizes, packages and
peripherals.
For ordering information, mechanical and electrical device characteristics please refer to the
low-, medium-, high- and XL-density STM32F101xx and STM32F103xx datasheets, to the
low- and medium-density STM32F102xx datasheets and to the
STM32F105xx/STM32F107xx connectivity line datasheet.
For information on programming, erasing and protection of the internal Flash memory
please refer to:
RM0042, the Flash programming manual for low-, medium- high-density and
connectivity line STM32F10xxx devices
PM0068, the Flash programming manual for XL-density STM32F10xxx devices.
For information on the ARM Cortex-M3 core, please refer to the STM32F10xxx Cortex-
M3 programming manual (PM0056).
Related documents
Available from www.st.com:
STM32F101xx, STM32F102xx, STM32F103xx, STM32F105xx/STM32F107xx and
datasheets
STM32F10xxx Cortex-M3 programming manual (PM0056)
STM32F10xxx Flash programming manual (PM0042)
STM32F10xxx XL-density Flash programming manual (PM0068)
www.st.com
Contents RM0008
2/1093 Doc ID 13902 Rev 13
Contents
1 Overview of the manual . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
2 Documentation conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
2.1 List of abbreviations for registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
2.2 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
2.3 Peripheral availability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
3 Memory and bus architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
3.1 System architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
3.2 Memory organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
3.3 Memory map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
3.3.1 Embedded SRAM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
3.3.2 Bit banding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
3.3.3 Embedded Flash memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
3.4 Boot configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
4 CRC calculation unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
4.1 CRC introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
4.2 CRC main features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
4.3 CRC functional description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
4.4 CRC registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
4.4.1 Data register (CRC_DR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
4.4.2 Independent data register (CRC_IDR) . . . . . . . . . . . . . . . . . . . . . . . . . 63
4.4.3 Control register (CRC_CR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
4.4.4 CRC register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
5 Power control (PWR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
5.1 Power supplies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
5.1.1 Independent A/D and D/A converter supply and reference voltage . . . . 66
5.1.2 Battery backup domain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
5.1.3 Voltage regulator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
5.2 Power supply supervisor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
5.2.1 Power on reset (POR)/power down reset (PDR) . . . . . . . . . . . . . . . . . . 68
RM0008 Contents
Doc ID 13902 Rev 13 3/1093
5.2.2 Programmable voltage detector (PVD) . . . . . . . . . . . . . . . . . . . . . . . . . 68
5.3 Low-power modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
5.3.1 Slowing down system clocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
5.3.2 Peripheral clock gating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
5.3.3 Sleep mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
5.3.4 Stop mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
5.3.5 Standby mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
5.3.6 Auto-wakeup (AWU) from low-power mode . . . . . . . . . . . . . . . . . . . . . . 75
5.4 Power control registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
5.4.1 Power control register (PWR_CR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
5.4.2 Power control/status register (PWR_CSR) . . . . . . . . . . . . . . . . . . . . . . 77
5.4.3 PWR register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
6 Backup registers (BKP) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
6.1 BKP introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
6.2 BKP main features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
6.3 BKP functional description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
6.3.1 Tamper detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
6.3.2 RTC calibration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
6.4 BKP registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
6.4.1 Backup data register x (BKP_DRx) (x = 1 ..42) . . . . . . . . . . . . . . . . . . . 81
6.4.2 RTC clock calibration register (BKP_RTCCR) . . . . . . . . . . . . . . . . . . . . 81
6.4.3 Backup control register (BKP_CR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
6.4.4 Backup control/status register (BKP_CSR) . . . . . . . . . . . . . . . . . . . . . . 82
6.4.5 BKP register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
7 Low-, medium-, high- and XL-density reset and clock
control (RCC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
7.1 Reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
7.1.1 System reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
7.1.2 Power reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
7.1.3 Backup domain reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
7.2 Clocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
7.2.1 HSE clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
7.2.2 HSI clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
7.2.3 PLL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Contents RM0008
4/1093 Doc ID 13902 Rev 13
7.2.4 LSE clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
7.2.5 LSI clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
7.2.6 System clock (SYSCLK) selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
7.2.7 Clock security system (CSS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
7.2.8 RTC clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
7.2.9 Watchdog clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
7.2.10 Clock-out capability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
7.3 RCC registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
7.3.1 Clock control register (RCC_CR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
7.3.2 Clock configuration register (RCC_CFGR) . . . . . . . . . . . . . . . . . . . . . . 98
7.3.3 Clock interrupt register (RCC_CIR) . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
7.3.4 APB2 peripheral reset register (RCC_APB2RSTR) . . . . . . . . . . . . . . 103
7.3.5 APB1 peripheral reset register (RCC_APB1RSTR) . . . . . . . . . . . . . . 106
7.3.6 AHB peripheral clock enable register (RCC_AHBENR) . . . . . . . . . . . 108
7.3.7 APB2 peripheral clock enable register (RCC_APB2ENR) . . . . . . . . . . 109
7.3.8 APB1 peripheral clock enable register (RCC_APB1ENR) . . . . . . . . . . 111
7.3.9 Backup domain control register (RCC_BDCR) . . . . . . . . . . . . . . . . . . 115
7.3.10 Control/status register (RCC_CSR) . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
7.3.11 RCC register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
8 Connectivity line devices: reset and clock control (RCC) . . . . . . . . . 120
8.1 Reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
8.1.1 System reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
8.1.2 Power reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
8.1.3 Backup domain reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
8.2 Clocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
8.2.1 HSE clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
8.2.2 HSI clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
8.2.3 PLLs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
8.2.4 LSE clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
8.2.5 LSI clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
8.2.6 System clock (SYSCLK) selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
8.2.7 Clock security system (CSS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
8.2.8 RTC clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
8.2.9 Watchdog clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
8.2.10 Clock-out capability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
8.3 RCC registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
RM0008 Contents
Doc ID 13902 Rev 13 5/1093
8.3.1 Clock control register (RCC_CR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
8.3.2 Clock configuration register (RCC_CFGR) . . . . . . . . . . . . . . . . . . . . . 131
8.3.3 Clock interrupt register (RCC_CIR) . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
8.3.4 APB2 peripheral reset register (RCC_APB2RSTR) . . . . . . . . . . . . . . 137
8.3.5 APB1 peripheral reset register (RCC_APB1RSTR) . . . . . . . . . . . . . . 138
8.3.6 AHB Peripheral Clock enable register (RCC_AHBENR) . . . . . . . . . . . 141
8.3.7 APB2 peripheral clock enable register (RCC_APB2ENR) . . . . . . . . . . 142
8.3.8 APB1 peripheral clock enable register (RCC_APB1ENR) . . . . . . . . . . 144
8.3.9 Backup domain control register (RCC_BDCR) . . . . . . . . . . . . . . . . . . 146
8.3.10 Control/status register (RCC_CSR) . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
8.3.11 AHB peripheral clock reset register (RCC_AHBRSTR) . . . . . . . . . . . . 149
8.3.12 Clock configuration register2 (RCC_CFGR2) . . . . . . . . . . . . . . . . . . . 150
8.3.13 RCC register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
9 General-purpose and alternate-function I/Os (GPIOs and AFIOs) . . 154
9.1 GPIO functional description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
9.1.1 General-purpose I/O (GPIO) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
9.1.2 Atomic bit set or reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
9.1.3 External interrupt/wakeup lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
9.1.4 Alternate functions (AF) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
9.1.5 Software remapping of I/O alternate functions . . . . . . . . . . . . . . . . . . 157
9.1.6 GPIO locking mechanism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
9.1.7 Input configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
9.1.8 Output configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
9.1.9 Alternate function configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
9.1.10 Analog configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
9.1.11 GPIO configurations for device peripherals . . . . . . . . . . . . . . . . . . . . . 161
9.2 GPIO registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
9.2.1 Port configuration register low (GPIOx_CRL) (x=A..G) . . . . . . . . . . . . 166
9.2.2 Port configuration register high (GPIOx_CRH) (x=A..G) . . . . . . . . . . . 167
9.2.3 Port input data register (GPIOx_IDR) (x=A..G) . . . . . . . . . . . . . . . . . . 167
9.2.4 Port output data register (GPIOx_ODR) (x=A..G) . . . . . . . . . . . . . . . . 168
9.2.5 Port bit set/reset register (GPIOx_BSRR) (x=A..G) . . . . . . . . . . . . . . . 168
9.2.6 Port bit reset register (GPIOx_BRR) (x=A..G) . . . . . . . . . . . . . . . . . . . 169
9.2.7 Port configuration lock register (GPIOx_LCKR) (x=A..G) . . . . . . . . . . 169
9.3 Alternate function I/O and debug configuration (AFIO) . . . . . . . . . . . . . 170
9.3.1 Using OSC32_IN/OSC32_OUT pins as GPIO ports PC14/PC15 . . . . 170
Contents RM0008
6/1093 Doc ID 13902 Rev 13
9.3.2 Using OSC_IN/OSC_OUT pins as GPIO ports PD0/PD1 . . . . . . . . . . 170
9.3.3 CAN1 alternate function remapping . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
9.3.4 CAN2 alternate function remapping . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
9.3.5 JTAG/SWD alternate function remapping . . . . . . . . . . . . . . . . . . . . . . 171
9.3.6 ADC alternate function remapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
9.3.7 Timer alternate function remapping . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
9.3.8 USART alternate function remapping . . . . . . . . . . . . . . . . . . . . . . . . . 175
9.3.9 I2C1 alternate function remapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
9.3.10 SPI1 alternate function remapping . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
9.3.11 SPI3/I2S3 alternate function remapping . . . . . . . . . . . . . . . . . . . . . . . 176
9.3.12 Ethernet alternate function remapping . . . . . . . . . . . . . . . . . . . . . . . . 176
9.4 AFIO registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
9.4.1 Event control register (AFIO_EVCR) . . . . . . . . . . . . . . . . . . . . . . . . . . 178
9.4.2 AF remap and debug I/O configuration register (AFIO_MAPR) . . . . . . 179
9.4.3 External interrupt configuration register 1 (AFIO_EXTICR1) . . . . . . . . 185
9.4.4 External interrupt configuration register 2 (AFIO_EXTICR2) . . . . . . . . 185
9.4.5 External interrupt configuration register 3 (AFIO_EXTICR3) . . . . . . . . 186
9.4.6 External interrupt configuration register 4 (AFIO_EXTICR4) . . . . . . . . 186
9.4.7 AF remap and debug I/O configuration register2 (AFIO_MAPR2) . . . . 187
9.5 GPIO and AFIO register maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
10 Interrupts and events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
10.1 Nested vectored interrupt controller (NVIC) . . . . . . . . . . . . . . . . . . . . . . 190
10.1.1 SysTick calibration value register . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
10.1.2 Interrupt and exception vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
10.2 External interrupt/event controller (EXTI) . . . . . . . . . . . . . . . . . . . . . . . . 198
10.2.1 Main features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
10.2.2 Block diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
10.2.3 Wakeup event management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
10.2.4 Functional description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
10.2.5 External interrupt/event line mapping . . . . . . . . . . . . . . . . . . . . . . . . . 200
10.3 EXTI registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
10.3.1 Interrupt mask register (EXTI_IMR) . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
10.3.2 Event mask register (EXTI_EMR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
10.3.3 Rising trigger selection register (EXTI_RTSR) . . . . . . . . . . . . . . . . . . 203
10.3.4 Falling trigger selection register (EXTI_FTSR) . . . . . . . . . . . . . . . . . . 203
10.3.5 Software interrupt event register (EXTI_SWIER) . . . . . . . . . . . . . . . . . 204
RM0008 Contents
Doc ID 13902 Rev 13 7/1093
10.3.6 Pending register (EXTI_PR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
10.3.7 EXTI register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
11 Analog-to-digital converter (ADC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
11.1 ADC introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
11.2 ADC main features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
11.3 ADC functional description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
11.3.1 ADC on-off control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
11.3.2 ADC clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
11.3.3 Channel selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
11.3.4 Single conversion mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
11.3.5 Continuous conversion mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
11.3.6 Timing diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
11.3.7 Analog watchdog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
11.3.8 Scan mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
11.3.9 Injected channel management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
11.3.10 Discontinuous mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
11.4 Calibration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
11.5 Data alignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
11.6 Channel-by-channel programmable sample time . . . . . . . . . . . . . . . . . . 216
11.7 Conversion on external trigger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
11.8 DMA request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
11.9 Dual ADC mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
11.9.1 Injected simultaneous mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
11.9.2 Regular simultaneous mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
11.9.3 Fast interleaved mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
11.9.4 Slow interleaved mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
11.9.5 Alternate trigger mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
11.9.6 Independent mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
11.9.7 Combined regular/injected simultaneous mode . . . . . . . . . . . . . . . . . . 224
11.9.8 Combined regular simultaneous + alternate trigger mode . . . . . . . . . . 224
11.9.9 Combined injected simultaneous + interleaved . . . . . . . . . . . . . . . . . . 225
11.10 Temperature sensor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
11.11 ADC interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
11.12 ADC registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
11.12.1 ADC status register (ADC_SR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Contents RM0008
8/1093 Doc ID 13902 Rev 13
11.12.2 ADC control register 1 (ADC_CR1) . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
11.12.3 ADC control register 2 (ADC_CR2) . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
11.12.4 ADC sample time register 1 (ADC_SMPR1) . . . . . . . . . . . . . . . . . . . . 235
11.12.5 ADC sample time register 2 (ADC_SMPR2) . . . . . . . . . . . . . . . . . . . . 236
11.12.6 ADC injected channel data offset register x (ADC_JOFRx)(x=1..4) . . 236
11.12.7 ADC watchdog high threshold register (ADC_HTR) . . . . . . . . . . . . . . 237
11.12.8 ADC watchdog low threshold register (ADC_LTR) . . . . . . . . . . . . . . . 237
11.12.9 ADC regular sequence register 1 (ADC_SQR1) . . . . . . . . . . . . . . . . . 238
11.12.10 ADC regular sequence register 2 (ADC_SQR2) . . . . . . . . . . . . . . . . . 239
11.12.11 ADC regular sequence register 3 (ADC_SQR3) . . . . . . . . . . . . . . . . . 240
11.12.12 ADC injected sequence register (ADC_JSQR) . . . . . . . . . . . . . . . . . . 241
11.12.13 ADC injected data register x (ADC_JDRx) (x= 1..4) . . . . . . . . . . . . . . 242
11.12.14 ADC regular data register (ADC_DR) . . . . . . . . . . . . . . . . . . . . . . . . . 242
11.12.15 ADC register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
12 Digital-to-analog converter (DAC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
12.1 DAC introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
12.2 DAC main features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
12.3 DAC functional description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
12.3.1 DAC channel enable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
12.3.2 DAC output buffer enable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
12.3.3 DAC data format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
12.3.4 DAC conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
12.3.5 DAC output voltage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
12.3.6 DAC trigger selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
12.3.7 DMA request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
12.3.8 Noise generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
12.3.9 Triangle-wave generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
12.4 Dual DAC channel conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
12.4.1 Independent trigger without wave generation . . . . . . . . . . . . . . . . . . . 252
12.4.2 Independent trigger with same LFSR generation . . . . . . . . . . . . . . . . 253
12.4.3 Independent trigger with different LFSR generation . . . . . . . . . . . . . . 253
12.4.4 Independent trigger with same triangle generation . . . . . . . . . . . . . . . 253
12.4.5 Independent trigger with different triangle generation . . . . . . . . . . . . . 254
12.4.6 Simultaneous software start . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
12.4.7 Simultaneous trigger without wave generation . . . . . . . . . . . . . . . . . . 254
12.4.8 Simultaneous trigger with same LFSR generation . . . . . . . . . . . . . . . 255
RM0008 Contents
Doc ID 13902 Rev 13 9/1093
12.4.9 Simultaneous trigger with different LFSR generation . . . . . . . . . . . . . 255
12.4.10 Simultaneous trigger with same triangle generation . . . . . . . . . . . . . . 255
12.4.11 Simultaneous trigger with different triangle generation . . . . . . . . . . . . 256
12.5 DAC registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
12.5.1 DAC control register (DAC_CR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
12.5.2 DAC software trigger register (DAC_SWTRIGR) . . . . . . . . . . . . . . . . . 259
12.5.3 DAC channel1 12-bit right-aligned data holding register
(DAC_DHR12R1) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
12.5.4 DAC channel1 12-bit left aligned data holding register
(DAC_DHR12L1) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
12.5.5 DAC channel1 8-bit right aligned data holding register
(DAC_DHR8R1) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
12.5.6 DAC channel2 12-bit right aligned data holding register
(DAC_DHR12R2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
12.5.7 DAC channel2 12-bit left aligned data holding register
(DAC_DHR12L2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
12.5.8 DAC channel2 8-bit right-aligned data holding register
(DAC_DHR8R2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
12.5.9 Dual DAC 12-bit right-aligned data holding register
(DAC_DHR12RD) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
12.5.10 DUAL DAC 12-bit left aligned data holding register
(DAC_DHR12LD) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
12.5.11 DUAL DAC 8-bit right aligned data holding register
(DAC_DHR8RD) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
12.5.12 DAC channel1 data output register (DAC_DOR1) . . . . . . . . . . . . . . . . 263
12.5.13 DAC channel2 data output register (DAC_DOR2) . . . . . . . . . . . . . . . . 263
12.5.14 DAC register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
13 Direct memory access controller (DMA) . . . . . . . . . . . . . . . . . . . . . . . 265
13.1 DMA introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
13.2 DMA main features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
13.3 DMA functional description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
13.3.1 DMA transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
13.3.2 Arbiter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
13.3.3 DMA channels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
13.3.4 Programmable data width, data alignment and endians . . . . . . . . . . . 270
13.3.5 Error management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
13.3.6 Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
13.3.7 DMA request mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
Contents RM0008
10/1093 Doc ID 13902 Rev 13
13.4 DMA registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
13.4.1 DMA interrupt status register (DMA_ISR) . . . . . . . . . . . . . . . . . . . . . . 275
13.4.2 DMA interrupt flag clear register (DMA_IFCR) . . . . . . . . . . . . . . . . . . 276
13.4.3 DMA channel x configuration register (DMA_CCRx) (x = 1..7,
where x = channel number) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
13.4.4 DMA channel x number of data register (DMA_CNDTRx) (x = 1..7),
where x = channel number) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
13.4.5 DMA channel x peripheral address register (DMA_CPARx) (x = 1..7),
where x = channel number) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
13.4.6 DMA channel x memory address register (DMA_CMARx) (x = 1..7),
where x = channel number) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
13.4.7 DMA register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
14 Advanced-control timers (TIM1&TIM8) . . . . . . . . . . . . . . . . . . . . . . . . 282
14.1 TIM1&TIM8 introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
14.2 TIM1&TIM8 main features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
14.3 TIM1&TIM8 functional description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
14.3.1 Time-base unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
14.3.2 Counter modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
14.3.3 Repetition counter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
14.3.4 Clock selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
14.3.5 Capture/compare channels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
14.3.6 Input capture mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
14.3.7 PWM input mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
14.3.8 Forced output mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
14.3.9 Output compare mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
14.3.10 PWM mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
14.3.11 Complementary outputs and dead-time insertion . . . . . . . . . . . . . . . . 307
14.3.12 Using the break function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
14.3.13 Clearing the OCxREF signal on an external event . . . . . . . . . . . . . . . 311
14.3.14 6-step PWM generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
14.3.15 One-pulse mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
14.3.16 Encoder interface mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
14.3.17 Timer input XOR function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
14.3.18 Interfacing with Hall sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
14.3.19 TIMx and external trigger synchronization . . . . . . . . . . . . . . . . . . . . . . 319
14.3.20 Timer synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
14.3.21 Debug mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
RM0008 Contents
Doc ID 13902 Rev 13 11/1093
14.4 TIM1&TIM8 registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
14.4.1 TIM1&TIM8 control register 1 (TIMx_CR1) . . . . . . . . . . . . . . . . . . . . . 323
14.4.2 TIM1&TIM8 control register 2 (TIMx_CR2) . . . . . . . . . . . . . . . . . . . . . 324
14.4.3 TIM1&TIM8 slave mode control register (TIMx_SMCR) . . . . . . . . . . . 326
14.4.4 TIM1&TIM8 DMA/interrupt enable register (TIMx_DIER) . . . . . . . . . . 328
14.4.5 TIM1&TIM8 status register (TIMx_SR) . . . . . . . . . . . . . . . . . . . . . . . . 330
14.4.6 TIM1&TIM8 event generation register (TIMx_EGR) . . . . . . . . . . . . . . 331
14.4.7 TIM1&TIM8 capture/compare mode register 1 (TIMx_CCMR1) . . . . . 333
14.4.8 TIM1&TIM8 capture/compare mode register 2 (TIMx_CCMR2) . . . . . 336
14.4.9 TIM1&TIM8 capture/compare enable register (TIMx_CCER) . . . . . . . 337
14.4.10 TIM1&TIM8 counter (TIMx_CNT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
14.4.11 TIM1&TIM8 prescaler (TIMx_PSC) . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
14.4.12 TIM1&TIM8 auto-reload register (TIMx_ARR) . . . . . . . . . . . . . . . . . . . 340
14.4.13 TIM1&TIM8 repetition counter register (TIMx_RCR) . . . . . . . . . . . . . . 341
14.4.14 TIM1&TIM8 capture/compare register 1 (TIMx_CCR1) . . . . . . . . . . . . 341
14.4.15 TIM1&TIM8 capture/compare register 2 (TIMx_CCR2) . . . . . . . . . . . . 342
14.4.16 TIM1&TIM8 capture/compare register 3 (TIMx_CCR3) . . . . . . . . . . . . 342
14.4.17 TIM1&TIM8 capture/compare register 4 (TIMx_CCR4) . . . . . . . . . . . . 343
14.4.18 TIM1&TIM8 break and dead-time register (TIMx_BDTR) . . . . . . . . . . 343
14.4.19 TIM1&TIM8 DMA control register (TIMx_DCR) . . . . . . . . . . . . . . . . . . 345
14.4.20 TIM1&TIM8 DMA address for full transfer (TIMx_DMAR) . . . . . . . . . . 346
14.4.21 TIM1&TIM8 register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
15 General-purpose timers (TIM2 to TIM5) . . . . . . . . . . . . . . . . . . . . . . . . 349
15.1 TIM2 to TIM5 introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
15.2 TIMx main features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
15.3 TIMx functional description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
15.3.1 Time-base unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
15.3.2 Counter modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
15.3.3 Clock selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
15.3.4 Capture/compare channels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
15.3.5 Input capture mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
15.3.6 PWM input mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
15.3.7 Forced output mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
15.3.8 Output compare mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
15.3.9 PWM mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
15.3.10 One-pulse mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Contents RM0008
12/1093 Doc ID 13902 Rev 13
15.3.11 Clearing the OCxREF signal on an external event . . . . . . . . . . . . . . . 374
15.3.12 Encoder interface mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
15.3.13 Timer input XOR function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
15.3.14 Timers and external trigger synchronization . . . . . . . . . . . . . . . . . . . . 377
15.3.15 Timer synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
15.3.16 Debug mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
15.4 TIMx2 to TIM5 registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
15.4.1 TIMx control register 1 (TIMx_CR1) . . . . . . . . . . . . . . . . . . . . . . . . . . 386
15.4.2 TIMx control register 2 (TIMx_CR2) . . . . . . . . . . . . . . . . . . . . . . . . . . 388
15.4.3 TIMx slave mode control register (TIMx_SMCR) . . . . . . . . . . . . . . . . . 389
15.4.4 TIMx DMA/Interrupt enable register (TIMx_DIER) . . . . . . . . . . . . . . . . 391
15.4.5 TIMx status register (TIMx_SR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
15.4.6 TIMx event generation register (TIMx_EGR) . . . . . . . . . . . . . . . . . . . . 394
15.4.7 TIMx capture/compare mode register 1 (TIMx_CCMR1) . . . . . . . . . . . 395
15.4.8 TIMx capture/compare mode register 2 (TIMx_CCMR2) . . . . . . . . . . . 398
15.4.9 TIMx capture/compare enable register (TIMx_CCER) . . . . . . . . . . . . . 399
15.4.10 TIMx counter (TIMx_CNT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
15.4.11 TIMx prescaler (TIMx_PSC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
15.4.12 TIMx auto-reload register (TIMx_ARR) . . . . . . . . . . . . . . . . . . . . . . . . 401
15.4.13 TIMx capture/compare register 1 (TIMx_CCR1) . . . . . . . . . . . . . . . . . 402
15.4.14 TIMx capture/compare register 2 (TIMx_CCR2) . . . . . . . . . . . . . . . . . 402
15.4.15 TIMx capture/compare register 3 (TIMx_CCR3) . . . . . . . . . . . . . . . . . 403
15.4.16 TIMx capture/compare register 4 (TIMx_CCR4) . . . . . . . . . . . . . . . . . 403
15.4.17 TIMx DMA control register (TIMx_DCR) . . . . . . . . . . . . . . . . . . . . . . . 404
15.4.18 TIMx DMA address for full transfer (TIMx_DMAR) . . . . . . . . . . . . . . . 404
15.4.19 TIMx register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
16 General-purpose timers (TIM9 to TIM14) . . . . . . . . . . . . . . . . . . . . . . . 408
16.1 TIM9 to TIM14 introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
16.2 TIM9 to TIM14 main features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
16.2.1 TIM9/TIM12 main features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
16.3 TIM10/TIM11 and TIM13/TIM14 main features . . . . . . . . . . . . . . . . . . . 410
16.4 TIM9 to TIM14 functional description . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
16.4.1 Time-base unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
16.4.2 Counter modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
16.4.3 Clock selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
16.4.4 Capture/compare channels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
RM0008 Contents
Doc ID 13902 Rev 13 13/1093
16.4.5 Input capture mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
16.4.6 PWM input mode (only for TIM9/12) . . . . . . . . . . . . . . . . . . . . . . . . . . 419
16.4.7 Forced output mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
16.4.8 Output compare mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
16.4.9 PWM mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
16.4.10 One-pulse mode (only for TIM9/12) . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
16.4.11 TIM9/12 external trigger synchronization . . . . . . . . . . . . . . . . . . . . . . . 424
16.4.12 Timer synchronization (TIM9/12) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
16.4.13 Debug mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
16.5 TIM9 and TIM12 registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
16.5.1 TIM9/12 control register 1 (TIMx_CR1) . . . . . . . . . . . . . . . . . . . . . . . . 428
16.5.2 TIM9/12 slave mode control register (TIMx_SMCR) . . . . . . . . . . . . . . 429
16.5.3 TIM9/12 Interrupt enable register (TIMx_DIER) . . . . . . . . . . . . . . . . . 430
16.5.4 TIM9/12 status register (TIMx_SR) . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
16.5.5 TIM9/12 event generation register (TIMx_EGR) . . . . . . . . . . . . . . . . . 432
16.5.6 TIM9/12 capture/compare mode register 1 (TIMx_CCMR1) . . . . . . . . 433
16.5.7 TIM9/12 capture/compare enable register (TIMx_CCER) . . . . . . . . . . 436
16.5.8 TIM9/12 counter (TIMx_CNT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
16.5.9 TIM9/12 prescaler (TIMx_PSC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
16.5.10 TIM9/12 auto-reload register (TIMx_ARR) . . . . . . . . . . . . . . . . . . . . . 437
16.5.11 TIM9/12 capture/compare register 1 (TIMx_CCR1) . . . . . . . . . . . . . . 438
16.5.12 TIM9/12 capture/compare register 2 (TIMx_CCR2) . . . . . . . . . . . . . . 438
16.5.13 TIM9/12 register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
16.6 TIM10/11/13/14 registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
16.6.1 TIM10/11/13/14 control register 1 (TIMx_CR1) . . . . . . . . . . . . . . . . . . 440
16.6.2 TIM10/11/13/14 Interrupt enable register (TIMx_DIER) . . . . . . . . . . . 441
16.6.3 TIM10/11/13/14 status register (TIMx_SR) . . . . . . . . . . . . . . . . . . . . . 441
16.6.4 TIM10/11/13/14 event generation register (TIMx_EGR) . . . . . . . . . . . 442
16.6.5 TIM10/11/13/14 capture/compare mode register 1
(TIMx_CCMR1) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
16.6.6 TIM10/11/13/14 capture/compare enable register
(TIMx_CCER) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
16.6.7 TIM10/11/13/14 counter (TIMx_CNT) . . . . . . . . . . . . . . . . . . . . . . . . . 446
16.6.8 TIM10/11/13/14 prescaler (TIMx_PSC) . . . . . . . . . . . . . . . . . . . . . . . . 446
16.6.9 TIM10/11/13/14 auto-reload register (TIMx_ARR) . . . . . . . . . . . . . . . 446
16.6.10 TIM10/11/13/14 capture/compare register 1 (TIMx_CCR1) . . . . . . . . 447
16.6.11 TIM10/11/13/14 register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
Contents RM0008
14/1093 Doc ID 13902 Rev 13
17 Basic timers (TIM6&TIM7) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
17.1 TIM6&TIM7 introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
17.2 TIM6&TIM7 main features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
17.3 TIM6&TIM7 functional description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
17.3.1 Time-base unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
17.3.2 Counting mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
17.3.3 Clock source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
17.3.4 Debug mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
17.4 TIM6&TIM7 registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
17.4.1 TIM6&TIM7 control register 1 (TIMx_CR1) . . . . . . . . . . . . . . . . . . . . . 455
17.4.2 TIM6&TIM7 control register 2 (TIMx_CR2) . . . . . . . . . . . . . . . . . . . . . 457
17.4.3 TIM6&TIM7 DMA/Interrupt enable register (TIMx_DIER) . . . . . . . . . . 457
17.4.4 TIM6&TIM7 status register (TIMx_SR) . . . . . . . . . . . . . . . . . . . . . . . . 458
17.4.5 TIM6&TIM7 event generation register (TIMx_EGR) . . . . . . . . . . . . . . 458
17.4.6 TIM6&TIM7 counter (TIMx_CNT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
17.4.7 TIM6&TIM7 prescaler (TIMx_PSC) . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
17.4.8 TIM6&TIM7 auto-reload register (TIMx_ARR) . . . . . . . . . . . . . . . . . . . 459
17.4.9 TIM6&TIM7 register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
18 Real-time clock (RTC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
18.1 RTC introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
18.2 RTC main features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
18.3 RTC functional description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
18.3.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
18.3.2 Resetting RTC registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
18.3.3 Reading RTC registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
18.3.4 Configuring RTC registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
18.3.5 RTC flag assertion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
18.4 RTC registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
18.4.1 RTC control register high (RTC_CRH) . . . . . . . . . . . . . . . . . . . . . . . . 466
18.4.2 RTC control register low (RTC_CRL) . . . . . . . . . . . . . . . . . . . . . . . . . . 467
18.4.3 RTC prescaler load register (RTC_PRLH / RTC_PRLL) . . . . . . . . . . . 468
18.4.4 RTC prescaler divider register (RTC_DIVH / RTC_DIVL) . . . . . . . . . . 469
18.4.5 RTC counter register (RTC_CNTH / RTC_CNTL) . . . . . . . . . . . . . . . . 470
18.4.6 RTC alarm register high (RTC_ALRH / RTC_ALRL) . . . . . . . . . . . . . . 471
18.4.7 RTC register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
RM0008 Contents
Doc ID 13902 Rev 13 15/1093
19 Independent watchdog (IWDG) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
19.1 IWDG introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
19.2 IWDG main features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
19.3 IWDG functional description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
19.3.1 Hardware watchdog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
19.3.2 Register access protection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
19.3.3 Debug mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
19.4 IWDG registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
19.4.1 Key register (IWDG_KR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
19.4.2 Prescaler register (IWDG_PR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
19.4.3 Reload register (IWDG_RLR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
19.4.4 Status register (IWDG_SR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
19.4.5 IWDG register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
20 Window watchdog (WWDG) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478
20.1 WWDG introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478
20.2 WWDG main features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478
20.3 WWDG functional description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478
20.4 How to program the watchdog timeout . . . . . . . . . . . . . . . . . . . . . . . . . . 480
20.5 Debug mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
20.6 WWDG registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
20.6.1 Control register (WWDG_CR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
20.6.2 Configuration register (WWDG_CFR) . . . . . . . . . . . . . . . . . . . . . . . . . 483
20.6.3 Status register (WWDG_SR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
20.6.4 WWDG register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484
21 Flexible static memory controller (FSMC) . . . . . . . . . . . . . . . . . . . . . 485
21.1 FSMC main features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
21.2 Block diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
21.3 AHB interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
21.3.1 Supported memories and transactions . . . . . . . . . . . . . . . . . . . . . . . . 488
21.4 External device address mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
21.4.1 NOR/PSRAM address mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
21.4.2 NAND/PC Card address mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
21.5 NOR Flash/PSRAM controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
Contents RM0008
16/1093 Doc ID 13902 Rev 13
21.5.1 External memory interface signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
21.5.2 Supported memories and transactions . . . . . . . . . . . . . . . . . . . . . . . . 494
21.5.3 General timing rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
21.5.4 NOR Flash/PSRAM controller asynchronous transactions . . . . . . . . . 495
21.5.5 Synchronous burst transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
21.5.6 NOR/PSRAM controller registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
21.6 NAND Flash/PC Card controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522
21.6.1 External memory interface signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
21.6.2 NAND Flash / PC Card supported memories and transactions . . . . . . 525
21.6.3 Timing diagrams for NAND and PC Card . . . . . . . . . . . . . . . . . . . . . . 525
21.6.4 NAND Flash operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526
21.6.5 NAND Flash pre-wait functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
21.6.6 Error correction code computation ECC (NAND Flash) . . . . . . . . . . . . 528
21.6.7 PC Card/CompactFlash operations . . . . . . . . . . . . . . . . . . . . . . . . . . . 528
21.6.8 NAND Flash/PC Card controller registers . . . . . . . . . . . . . . . . . . . . . . 531
21.6.9 FSMC register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537
22 Secure digital input/output interface (SDIO) . . . . . . . . . . . . . . . . . . . . 539
22.1 SDIO main features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539
22.2 SDIO bus topology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540
22.3 SDIO functional description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
22.3.1 SDIO adapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
22.3.2 SDIO AHB interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553
22.4 Card functional description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554
22.4.1 Card identification mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554
22.4.2 Card reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554
22.4.3 Operating voltage range validation . . . . . . . . . . . . . . . . . . . . . . . . . . . 554
22.4.4 Card identification process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555
22.4.5 Block write . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556
22.4.6 Block read . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556
22.4.7 Stream access, stream write and stream read (MultiMediaCard only) 557
22.4.8 Erase: group erase and sector erase . . . . . . . . . . . . . . . . . . . . . . . . . . 558
22.4.9 Wide bus selection or deselection . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
22.4.10 Protection management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
22.4.11 Card status register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562
22.4.12 SD status register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565
22.4.13 SD I/O mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569
RM0008 Contents
Doc ID 13902 Rev 13 17/1093
22.4.14 Commands and responses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 570
22.5 Response formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
22.5.1 R1 (normal response command) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574
22.5.2 R1b . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574
22.5.3 R2 (CID, CSD register) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574
22.5.4 R3 (OCR register) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574
22.5.5 R4 (Fast I/O) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
22.5.6 R4b . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
22.5.7 R5 (interrupt request) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
22.5.8 R6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
22.6 SDIO I/O card-specific operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577
22.6.1 SDIO I/O read wait operation by SDIO_D2 signalling . . . . . . . . . . . . . 577
22.6.2 SDIO read wait operation by stopping SDIO_CK . . . . . . . . . . . . . . . . 577
22.6.3 SDIO suspend/resume operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578
22.6.4 SDIO interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578
22.7 CE-ATA specific operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578
22.7.1 Command completion signal disable . . . . . . . . . . . . . . . . . . . . . . . . . . 578
22.7.2 Command completion signal enable . . . . . . . . . . . . . . . . . . . . . . . . . . 578
22.7.3 CE-ATA interrupt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579
22.7.4 Aborting CMD61 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579
22.8 HW flow control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579
22.9 SDIO registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579
22.9.1 SDIO power control register (SDIO_POWER) . . . . . . . . . . . . . . . . . . . 580
22.9.2 SDI clock control register (SDIO_CLKCR) . . . . . . . . . . . . . . . . . . . . . . 580
22.9.3 SDIO argument register (SDIO_ARG) . . . . . . . . . . . . . . . . . . . . . . . . . 581
22.9.4 SDIO command register (SDIO_CMD) . . . . . . . . . . . . . . . . . . . . . . . . 582
22.9.5 SDIO command response register (SDIO_RESPCMD) . . . . . . . . . . . 583
22.9.6 SDIO response 1..4 register (SDIO_RESPx) . . . . . . . . . . . . . . . . . . . 583
22.9.7 SDIO data timer register (SDIO_DTIMER) . . . . . . . . . . . . . . . . . . . . . 584
22.9.8 SDIO data length register (SDIO_DLEN) . . . . . . . . . . . . . . . . . . . . . . 584
22.9.9 SDIO data control register (SDIO_DCTRL) . . . . . . . . . . . . . . . . . . . . . 585
22.9.10 SDIO data counter register (SDIO_DCOUNT) . . . . . . . . . . . . . . . . . . 586
22.9.11 SDIO status register (SDIO_STA) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
22.9.12 SDIO interrupt clear register (SDIO_ICR) . . . . . . . . . . . . . . . . . . . . . . 588
22.9.13 SDIO mask register (SDIO_MASK) . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
22.9.14 SDIO FIFO counter register (SDIO_FIFOCNT) . . . . . . . . . . . . . . . . . . 592
Contents RM0008
18/1093 Doc ID 13902 Rev 13
22.9.15 SDIO data FIFO register (SDIO_FIFO) . . . . . . . . . . . . . . . . . . . . . . . . 593
22.9.16 SDIO register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
23 Universal serial bus full-speed device interface (USB) . . . . . . . . . . . 595
23.1 USB introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595
23.2 USB main features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595
23.3 USB functional description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595
23.3.1 Description of USB blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597
23.4 Programming considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598
23.4.1 Generic USB device programming . . . . . . . . . . . . . . . . . . . . . . . . . . . 598
23.4.2 System and power-on reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
23.4.3 Double-buffered endpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603
23.4.4 Isochronous transfers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
23.4.5 Suspend/Resume events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 608
23.5 USB registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610
23.5.1 Common registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610
23.5.2 Endpoint-specific registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617
23.5.3 Buffer descriptor table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621
23.5.4 USB register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624
24 Controller area network (bxCAN) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626
24.1 bxCAN introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626
24.2 bxCAN main features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626
24.3 bxCAN general description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627
24.3.1 CAN 2.0B active core . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628
24.3.2 Control, status and configuration registers . . . . . . . . . . . . . . . . . . . . . 628
24.3.3 Tx mailboxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628
24.3.4 Acceptance filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628
24.4 bxCAN operating modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629
24.4.1 Initialization mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630
24.4.2 Normal mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630
24.4.3 Sleep mode (low power) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630
24.5 Test mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631
24.5.1 Silent mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631
24.5.2 Loop back mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632
24.5.3 Loop back combined with silent mode . . . . . . . . . . . . . . . . . . . . . . . . . 632
RM0008 Contents
Doc ID 13902 Rev 13 19/1093
24.6 STM32F10xxx in Debug mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633
24.7 bxCAN functional description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633
24.7.1 Transmission handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633
24.7.2 Time triggered communication mode . . . . . . . . . . . . . . . . . . . . . . . . . 635
24.7.3 Reception handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635
24.7.4 Identifier filtering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 636
24.7.5 Message storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 640
24.7.6 Error management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 642
24.7.7 Bit timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 642
24.8 bxCAN interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644
24.9 CAN registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 646
24.9.1 Register access protection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 646
24.9.2 CAN control and status registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 646
24.9.3 CAN mailbox registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 656
24.9.4 CAN filter registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663
24.9.5 bxCAN register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667
25 Serial peripheral interface (SPI) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 670
25.1 SPI introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 670
25.2 SPI and I
2
S main features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671
25.2.1 SPI features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671
25.2.2 I
2
S features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672
25.3 SPI functional description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673
25.3.1 General description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673
25.3.2 Configuring the SPI in slave mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 676
25.3.3 Configuring the SPI in master mode . . . . . . . . . . . . . . . . . . . . . . . . . . 678
25.3.4 Configuring the SPI for Simplex communication . . . . . . . . . . . . . . . . . 679
25.3.5 Data transmission and reception procedures . . . . . . . . . . . . . . . . . . . 679
25.3.6 CRC calculation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 686
25.3.7 Status flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 688
25.3.8 Disabling the SPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689
25.3.9 SPI communication using DMA (direct memory addressing) . . . . . . . 690
25.3.10 Error flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691
25.3.11 SPI interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 692
25.4 I
2
S functional description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693
Contents RM0008
20/1093 Doc ID 13902 Rev 13
25.4.1 The I
2
S audio protocol is not available in low- and medium-density
devices. This section concerns only high-density, XL-density and
connectivity line devices. I
2
S general description 693
25.4.2 Supported audio protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 694
25.4.3 Clock generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701
25.4.4 I
2
S master mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706
25.4.5 I
2
S slave mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707
25.4.6 Status flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709
25.4.7 Error flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 710
25.4.8 I
2
S interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711
25.4.9 DMA features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711
25.5 SPI and I
2
S registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 712
25.5.1 SPI control register 1 (SPI_CR1) (not used in I
2
S mode) . . . . . . . . . . 712
25.5.2 SPI control register 2 (SPI_CR2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714
25.5.3 SPI status register (SPI_SR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715
25.5.4 SPI data register (SPI_DR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 716
25.5.5 SPI CRC polynomial register (SPI_CRCPR) (not used in I
2
S
mode) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 716
25.5.6 SPI RX CRC register (SPI_RXCRCR) (not used in I
2
S mode) . . . . . . 717
25.5.7 SPI TX CRC register (SPI_TXCRCR) (not used in I
2
S mode) . . . . . . 717
25.5.8 SPI_I
2
S configuration register (SPI_I2SCFGR) . . . . . . . . . . . . . . . . . . 718
25.5.9 SPI_I
2
S prescaler register (SPI_I2SPR) . . . . . . . . . . . . . . . . . . . . . . . 719
25.5.10 SPI register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720
26 Inter-integrated circuit (I
2
C) interface . . . . . . . . . . . . . . . . . . . . . . . . . 721
26.1 I
2
C introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721
26.2 I
2
C main features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721
26.3 I
2
C functional description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 722
26.3.1 Mode selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 722
26.3.2 I2C slave mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724
26.3.3 I2C master mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 726
26.3.4 Error conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734
26.3.5 SDA/SCL line control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735
26.3.6 SMBus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 736
26.3.7 DMA requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738
26.3.8 Packet error checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740
26.4 I
2
C interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740
RM0008 Contents
Doc ID 13902 Rev 13 21/1093
26.5 I
2
C debug mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742
26.6 I
2
C registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742
26.6.1 I
2
C Control register 1 (I2C_CR1) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742
26.6.2 I
2
C Control register 2 (I2C_CR2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744
26.6.3 I
2
C Own address register 1 (I2C_OAR1) . . . . . . . . . . . . . . . . . . . . . . . 746
26.6.4 I
2
C Own address register 2 (I2C_OAR2) . . . . . . . . . . . . . . . . . . . . . . . 746
26.6.5 I
2
C Data register (I2C_DR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 747
26.6.6 I
2
C Status register 1 (I2C_SR1) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 747
26.6.7 I
2
C Status register 2 (I2C_SR2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751
26.6.8 I
2
C Clock control register (I2C_CCR) . . . . . . . . . . . . . . . . . . . . . . . . . 752
26.6.9 I
2
C TRISE register (I2C_TRISE) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 753
26.6.10 I2C register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754
27 Universal synchronous asynchronous receiver
transmitter (USART) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755
27.1 USART introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755
27.2 USART main features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 756
27.3 USART functional description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 757
27.3.1 USART character description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 760
27.3.2 Transmitter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 761
27.3.3 Receiver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764
27.3.4 Fractional baud rate generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768
27.3.5 USART receivers tolerance to clock deviation . . . . . . . . . . . . . . . . . . 770
27.3.6 Multiprocessor communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770
27.3.7 Parity control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 772
27.3.8 LIN (local interconnection network) mode . . . . . . . . . . . . . . . . . . . . . . 773
27.3.9 USART synchronous mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775
27.3.10 Single-wire half-duplex communication . . . . . . . . . . . . . . . . . . . . . . . . 777
27.3.11 Smartcard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 778
27.3.12 IrDA SIR ENDEC block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 780
27.3.13 Continuous communication using DMA . . . . . . . . . . . . . . . . . . . . . . . . 782
27.3.14 Hardware flow control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 785
27.4 USART interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 786
27.5 USART mode configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787
27.6 USART registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787
27.6.1 Status register (USART_SR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 788
Contents RM0008
22/1093 Doc ID 13902 Rev 13
27.6.2 Data register (USART_DR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 790
27.6.3 Baud rate register (USART_BRR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 790
27.6.4 Control register 1 (USART_CR1) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 791
27.6.5 Control register 2 (USART_CR2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793
27.6.6 Control register 3 (USART_CR3) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794
27.6.7 Guard time and prescaler register (USART_GTPR) . . . . . . . . . . . . . . 796
27.6.8 USART register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 797
28 USB on-the-go full-speed (OTG_FS) . . . . . . . . . . . . . . . . . . . . . . . . . . 798
28.1 OTG_FS introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 798
28.2 OTG_FS main features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 799
28.2.1 General features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 799
28.2.2 Host-mode features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 800
28.2.3 Peripheral-mode features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 800
28.3 OTG_FS functional description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 801
28.3.1 OTG full-speed core . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 801
28.3.2 Full-speed OTG PHY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 801
28.4 OTG dual role device (DRD) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802
28.4.1 ID line detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 803
28.4.2 HNP dual role device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 803
28.4.3 SRP dual role device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 803
28.5 USB peripheral . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 804
28.5.1 SRP-capable peripheral . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 804
28.5.2 Peripheral states . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805
28.5.3 Peripheral endpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 806
28.6 USB host . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 808
28.6.1 SRP-capable host . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 809
28.6.2 USB host states . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 809
28.6.3 Host channels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 810
28.6.4 Host scheduler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 812
28.7 SOF trigger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 813
28.7.1 Host SOFs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 813
28.7.2 Peripheral SOFs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 813
28.8 Power options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 814
28.9 Dynamic update of the OTG_FS_HFIR register . . . . . . . . . . . . . . . . . . . 815
28.10 USB data FIFOs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 815
RM0008 Contents
Doc ID 13902 Rev 13 23/1093
28.11 Peripheral FIFO architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816
28.11.1 Peripheral Rx FIFO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816
28.11.2 Peripheral Tx FIFOs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 817
28.12 Host FIFO architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 817
28.12.1 Host Rx FIFO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 817
28.12.2 Host Tx FIFOs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 818
28.13 FIFO RAM allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 818
28.13.1 Device mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 818
28.13.2 Host mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 819
28.14 USB system performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 819
28.15 OTG_FS interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 820
28.16 OTG_FS control and status registers . . . . . . . . . . . . . . . . . . . . . . . . . . . 822
28.16.1 CSR memory map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 823
28.16.2 OTG_FS global registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 827
28.16.3 Host-mode registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 848
28.16.4 Device-mode registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 859
28.16.5 OTG_FS power and clock gating control register
(OTG_FS_PCGCCTL) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 881
28.16.6 OTG_FS register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 882
28.17 OTG_FS programming model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 888
28.17.1 Core initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 888
28.17.2 Host initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 890
28.17.3 Device initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 890
28.17.4 Host programming model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 891
28.17.5 Device programming model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 908
28.17.6 Operational model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 910
28.17.7 Worst case response time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 927
28.17.8 OTG programming model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 928
29 Ethernet (ETH): media access control (MAC) with
DMA controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 935
29.1 Ethernet introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 935
29.2 Ethernet main features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 935
29.2.1 MAC core features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 936
29.2.2 DMA features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 937
29.2.3 PTP features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 937
Contents RM0008
24/1093 Doc ID 13902 Rev 13
29.3 Ethernet pins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 938
29.4 Ethernet functional description: SMI, MII and RMII . . . . . . . . . . . . . . . . 939
29.4.1 Station management interface: SMI . . . . . . . . . . . . . . . . . . . . . . . . . . . 939
29.4.2 Media-independent interface: MII . . . . . . . . . . . . . . . . . . . . . . . . . . . . 942
29.4.3 Reduced media-independent interface: RMII . . . . . . . . . . . . . . . . . . . 945
29.4.4 MII/RMII selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 946
29.5 Ethernet functional description: MAC 802.3 . . . . . . . . . . . . . . . . . . . . . . 947
29.5.1 MAC 802.3 frame format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 947
29.5.2 MAC frame transmission . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 951
29.5.3 MAC frame reception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 958
29.5.4 MAC interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 963
29.5.5 MAC filtering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 963
29.5.6 MAC loopback mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 966
29.5.7 MAC management counters: MMC . . . . . . . . . . . . . . . . . . . . . . . . . . . 966
29.5.8 Power management: PMT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 967
29.5.9 Precision time protocol (IEEE1588 PTP) . . . . . . . . . . . . . . . . . . . . . . . 970
29.6 Ethernet functional description: DMA controller operation . . . . . . . . . . . 976
29.6.1 Initialization of a transfer using DMA . . . . . . . . . . . . . . . . . . . . . . . . . . 977
29.6.2 Host bus burst access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 977
29.6.3 Host data buffer alignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 978
29.6.4 Buffer size calculations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 978
29.6.5 DMA arbiter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 979
29.6.6 Error response to DMA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 979
29.6.7 Tx DMA configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 979
29.6.8 Rx DMA configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 988
29.6.9 DMA interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 997
29.7 Ethernet interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 998
29.8 Ethernet register descriptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 999
29.8.1 MAC register description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 999
29.8.2 MMC register description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1016
29.8.3 IEEE 1588 time stamp registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1021
29.8.4 DMA register description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1025
29.8.5 Ethernet register maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1039
30 Device electronic signature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1042
30.1 Memory size registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1042
RM0008 Contents
Doc ID 13902 Rev 13 25/1093
30.1.1 Flash size register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1042
30.2 Unique device ID register (96 bits) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1043
31 Debug support (DBG) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1045
31.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1045
31.2 Reference ARM documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1047
31.3 SWJ debug port (serial wire and JTAG) . . . . . . . . . . . . . . . . . . . . . . . . 1047
31.3.1 Mechanism to select the JTAG-DP or the SW-DP . . . . . . . . . . . . . . . 1047
31.4 Pinout and debug port pins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1048
31.4.1 SWJ debug port pins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1049
31.4.2 Flexible SWJ-DP pin assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1049
31.4.3 Internal pull-up and pull-down on JTAG pins . . . . . . . . . . . . . . . . . . . 1050
31.4.4 Using serial wire and releasing the unused debug pins as GPIOs . . 1051
31.5 STM32F10xxx JTAG TAP connection . . . . . . . . . . . . . . . . . . . . . . . . . 1051
31.6 ID codes and locking mechanism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1052
31.6.1 MCU device ID code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1052
31.6.2 Boundary scan TAP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1053
31.6.3 Cortex-M3 TAP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1054
31.6.4 Cortex-M3 JEDEC-106 ID code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1054
31.7 JTAG debug port . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1054
31.8 SW debug port . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1056
31.8.1 SW protocol introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1056
31.8.2 SW protocol sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1056
31.8.3 SW-DP state machine (reset, idle states, ID code) . . . . . . . . . . . . . . 1057
31.8.4 DP and AP read/write accesses . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1057
31.8.5 SW-DP registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1058
31.8.6 SW-AP registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1058
31.9 AHB-AP (AHB access port) - valid for both JTAG-DP
and SW-DP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1059
31.10 Core debug . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1060
31.11 Capability of the debugger host to connect under system reset . . . . . 1060
31.12 FPB (Flash patch breakpoint) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1061
31.13 DWT (data watchpoint trigger) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1062
31.14 ITM (instrumentation trace macrocell) . . . . . . . . . . . . . . . . . . . . . . . . . 1062
31.14.1 General description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1062
31.14.2 Time stamp packets, synchronization and overflow packets . . . . . . . 1062
Contents RM0008
26/1093 Doc ID 13902 Rev 13
31.15 ETM (Embedded trace macrocell) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1064
31.15.1 General description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1064
31.15.2 Signal protocol, packet types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1064
31.15.3 Main ETM registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1065
31.15.4 Configuration example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1065
31.16 MCU debug component (DBGMCU) . . . . . . . . . . . . . . . . . . . . . . . . . . 1065
31.16.1 Debug support for low-power modes . . . . . . . . . . . . . . . . . . . . . . . . . 1065
31.16.2 Debug support for timers, watchdog, bxCAN and I
2
C . . . . . . . . . . . . 1066
31.16.3 Debug MCU configuration register . . . . . . . . . . . . . . . . . . . . . . . . . . . 1066
31.17 TPIU (trace port interface unit) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1068
31.17.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1068
31.17.2 TRACE pin assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1069
31.17.3 TPUI formatter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1071
31.17.4 TPUI frame synchronization packets . . . . . . . . . . . . . . . . . . . . . . . . . 1071
31.17.5 Transmission of the synchronization frame packet . . . . . . . . . . . . . . 1071
31.17.6 Synchronous mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1072
31.17.7 Asynchronous mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1072
31.17.8 TRACECLKIN connection inside the STM32F10xxx . . . . . . . . . . . . . 1072
31.17.9 TPIU registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1073
31.17.10 Example of configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1074
31.18 DBG register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1074
32 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1075
RM0008 List of tables
Doc ID 13902 Rev 13 27/1093
List of tables
Table 1. Sections related to each STM32F10xxx product . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Table 2. Sections related to each peripheral . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Table 3. Register boundary addresses. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Table 4. Flash module organization (low-density devices) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Table 5. Flash module organization (medium-density devices) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Table 6. Flash module organization (high-density devices) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Table 7. Flash module organization (connectivity line devices) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Table 8. XL-density Flash module organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Table 9. Boot modes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Table 10. CRC calculation unit register map and reset values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Table 11. Low-power mode summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Table 12. Sleep-now. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Table 13. Sleep-on-exit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Table 14. Stop mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Table 15. Standby mode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Table 16. PWR register map and reset values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Table 17. BKP register map and reset values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Table 18. RCC register map and reset values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Table 19. RCC register map and reset values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Table 20. Port bit configuration table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Table 21. Output MODE bits. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Table 22. Advanced timers TIM1/TIM8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Table 23. General-purpose timers TIM2/3/4/5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Table 24. USARTs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Table 25. SPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Table 26. I2S . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Table 27. I2C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Table 28. BxCAN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Table 29. USB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Table 30. OTG_FS pin configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Table 31. SDIO. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Table 32. FSMC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Table 33. Other IOs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Table 34. CAN1 alternate function remapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Table 35. CAN2 alternate function remapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Table 36. Debug interface signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Table 37. Debug port mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Table 38. ADC1 external trigger injected conversion alternate function remapping. . . . . . . . . . . . . 172
Table 39. ADC1 external trigger regular conversion alternate function remapping . . . . . . . . . . . . . 172
Table 40. ADC2 external trigger injected conversion alternate function remapping. . . . . . . . . . . . . 172
Table 41. ADC2 external trigger regular conversion alternate function remapping . . . . . . . . . . . . . 173
Table 42. TIM5 alternate function remapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Table 43. TIM4 alternate function remapping. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Table 44. TIM3 alternate function remapping. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Table 45. TIM2 alternate function remapping. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Table 46. TIM1 alternate function remapping. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Table 47. TIM9 remapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Table 48. TIM10 remapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
List of tables RM0008
28/1093 Doc ID 13902 Rev 13
Table 49. TIM11 remapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Table 50. TIM13 remapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Table 51. TIM14 remapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Table 52. USART3 remapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Table 53. USART2 remapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Table 54. USART1 remapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Table 55. I2C1 remapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Table 56. SPI1 remapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Table 57. SPI3/I2S3 remapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Table 58. ETH remapping. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Table 59. GPIO register map and reset values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Table 60. AFIO register map and reset values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Table 61. Vector table for connectivity line devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Table 62. Vector table for XL-density devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Table 63. Vector table for other STM32F10xxx devices. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Table 64. External interrupt/event controller register map and reset values. . . . . . . . . . . . . . . . . . . 205
Table 65. ADC pins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Table 66. Analog watchdog channel selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Table 67. External trigger for regular channels for ADC1 and ADC2 . . . . . . . . . . . . . . . . . . . . . . . . 216
Table 68. External trigger for injected channels for ADC1 and ADC2 . . . . . . . . . . . . . . . . . . . . . . . 217
Table 69. External trigger for regular channels for ADC3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Table 70. External trigger for injected channels for ADC3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Table 71. ADC interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Table 72. ADC register map and reset values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Table 73. DAC pins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Table 74. External triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Table 75. DAC register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Table 76. Programmable data width & endian behavior (when bits PINC = MINC = 1) . . . . . . . . . . 270
Table 77. DMA interrupt requests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Table 78. Summary of DMA1 requests for each channel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Table 79. Summary of DMA2 requests for each channel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Table 80. DMA register map and reset values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Table 81. Counting direction versus encoder signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
Table 82. TIMx Internal trigger connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Table 83. Output control bits for complementary OCx and OCxN channels with
break feature. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Table 84. TIM1&TIM8 register map and reset values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Table 85. Counting direction versus encoder signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
Table 86. TIMx Internal trigger connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
Table 87. Output control bit for standard OCx channels. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
Table 88. TIMx register map and reset values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
Table 89. TIMx internal trigger connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
Table 90. Output control bit for standard OCx channels. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
Table 91. TIM9/12 register map and reset values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
Table 92. Output control bit for standard OCx channels. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
Table 93. TIM10/11/13/14 register map and reset values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
Table 94. TIM6&TIM7 register map and reset values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
Table 95. RTC register map and reset values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
Table 96. Min/max IWDG timeout period at 40 kHz (LSI) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Table 97. IWDG register map and reset values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
Table 98. Min-max timeout value @36 MHz (PCLK1) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
Table 99. WWDG register map and reset values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484
RM0008 List of tables
Doc ID 13902 Rev 13 29/1093
Table 100. NOR/PSRAM bank selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
Table 101. External memory address. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
Table 102. Memory mapping and timing registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
Table 103. NAND bank selections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
Table 104. Programmable NOR/PSRAM access parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
Table 105. Nonmultipled I/O NOR Flash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
Table 106. Multiplexed I/O NOR Flash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
Table 107. Nonmultiplexed I/Os PSRAM/SRAM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
Table 108. NOR Flash/PSRAM supported memories and transactions . . . . . . . . . . . . . . . . . . . . . . . 494
Table 109. FSMC_BCRx bit fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
Table 110. FSMC_BTRx bit fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
Table 111. FSMC_BCRx bit fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
Table 112. FSMC_BTRx bit fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
Table 113. FSMC_BWTRx bit fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
Table 114. FSMC_BCRx bit fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
Table 115. FSMC_BTRx bit fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
Table 116. FSMC_BWTRx bit fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
Table 117. FSMC_BCRx bit fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504
Table 118. FSMC_BTRx bit fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504
Table 119. FSMC_BWTRx bit fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504
Table 120. FSMC_BCRx bit fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506
Table 121. FSMC_BTRx bit fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506
Table 122. FSMC_BWTRx bit fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506
Table 123. FSMC_BCRx bit fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
Table 124. FSMC_BTRx bit fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
Table 125. FSMC_BCRx bit fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
Table 126. FSMC_BTRx bit fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514
Table 127. FSMC_BCRx bit fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516
Table 128. FSMC_BTRx bit fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516
Table 129. Programmable NAND/PC Card access parameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
Table 130. 8-bit NAND Flash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
Table 131. 16-bit NAND Flash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524
Table 132. 16-bit PC Card . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524
Table 133. Supported memories and transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525
Table 134. 16-bit PC-Card signals and access type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529
Table 135. ECC result relevant bits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536
Table 136. FSMC register map. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537
Table 137. SDIO I/O definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
Table 138. Command format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547
Table 139. Short response format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
Table 140. Long response format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
Table 141. Command path status flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
Table 142. Data token format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551
Table 143. Transmit FIFO status flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552
Table 144. Receive FIFO status flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553
Table 145. Card status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
Table 146. SD status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565
Table 147. Speed class code field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567
Table 148. Performance move field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567
Table 149. AU_SIZE field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567
Table 150. Maximum AU size. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
Table 151. Erase size field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
List of tables RM0008
30/1093 Doc ID 13902 Rev 13
Table 152. Erase timeout field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
Table 153. Erase offset field. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569
Table 154. Block-oriented write commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 571
Table 155. Block-oriented write protection commands. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572
Table 156. Erase commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572
Table 157. I/O mode commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572
Table 158. Lock card . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
Table 159. Application-specific commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
Table 160. R1 response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574
Table 161. R2 response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574
Table 162. R3 response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
Table 163. R4 response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
Table 164. R4b response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
Table 165. R5 response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
Table 166. R6 response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
Table 167. Response type and SDIO_RESPx registers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583
Table 168. SDIO register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
Table 169. Double-buffering buffer flag definition. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 605
Table 170. Bulk double-buffering memory buffers usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 606
Table 171. Isochronous memory buffers usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
Table 172. Resume event detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609
Table 173. Reception status encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620
Table 174. Endpoint type encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620
Table 175. Endpoint kind meaning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620
Table 176. Transmission status encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620
Table 177. Definition of allocated buffer memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624
Table 178. USB register map and reset values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624
Table 179. Transmit mailbox mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
Table 180. Receive mailbox mapping. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
Table 181. bxCAN register map and reset values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667
Table 182. SPI interrupt requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 692
Table 183. Audio-frequency precision using standard 8 MHz HSE (high-density and XL-density
devices only) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 703
Table 184. Audio-frequency precision using standard 25 MHz and PLL3
(connectivity line devices only) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 704
Table 185. Audio-frequency precision using standard 14.7456 MHz and PLL3
(connectivity line devices only) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705
Table 186. I
2
S interrupt requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711
Table 187. SPI register map and reset values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720
Table 188. SMBus vs. I2C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 736
Table 189. I2C Interrupt requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740
Table 190. I2C register map and reset values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754
Table 191. Noise detection from sampled data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767
Table 192. Error calculation for programmed baud rates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 769
Table 193. USART receivers tolerance when DIV_Fraction is 0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 770
Table 194. USART receivers tolerance when DIV_Fraction is different from 0 . . . . . . . . . . . . . . . . . 770
Table 195. Frame formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 772
Table 196. USART interrupt requests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 786
Table 197. USART mode configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787
Table 198. USART register map and reset values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 797
Table 199. Core global control and status registers (CSRs). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 823
Table 200. Host-mode control and status registers (CSRs) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 824
RM0008 List of tables
Doc ID 13902 Rev 13 31/1093
Table 201. Device-mode control and status registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825
Table 202. Data FIFO (DFIFO) access register map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 827
Table 203. Power and clock gating control and status registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 827
Table 204. Minimum duration for soft disconnect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 861
Table 205. OTG_FS register map and reset values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 882
Table 206. Ethernet pin configuration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 938
Table 207. Management frame format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 940
Table 208. Clock range. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 942
Table 209. TX interface signal encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 944
Table 210. RX interface signal encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 944
Table 211. Frame statuses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 960
Table 212. Destination address filtering table. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 965
Table 213. Source address filtering table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 966
Table 214. Receive descriptor 0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 994
Table 215. Ethernet register map and reset values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1039
Table 216. SWJ debug port pins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1049
Table 217. Flexible SWJ-DP pin assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1050
Table 218. JTAG debug port data registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1054
Table 219. 32-bit debug port registers addressed through the shifted value A[3:2] . . . . . . . . . . . . . 1055
Table 220. Packet request (8-bits) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1056
Table 221. ACK response (3 bits). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1057
Table 222. DATA transfer (33 bits) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1057
Table 223. SW-DP registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1058
Table 224. Cortex-M3 AHB-AP registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1059
Table 225. Core debug registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1060
Table 226. Main ITM registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1063
Table 227. Main ETM registers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1065
Table 228. Asynchronous TRACE pin assignment. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1069
Table 229. Synchronous TRACE pin assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1069
Table 230. Flexible TRACE pin assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1070
Table 231. Important TPIU registers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1073
Table 232. DBG register map and reset values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1074
Table 233. Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1075
List of figures RM0008
32/1093 Doc ID 13902 Rev 13
List of figures
Figure 1. System architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Figure 2. System architecture in connectivity line devices. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Figure 3. CRC calculation unit block diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Figure 4. Power supply overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Figure 5. Power on reset/power down reset waveform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Figure 6. PVD thresholds. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Figure 7. Simplified diagram of the reset circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Figure 8. Clock tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Figure 9. HSE/ LSE clock sources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Figure 10. Simplified diagram of the reset circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Figure 11. Clock tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Figure 12. HSE/ LSE clock sources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Figure 13. Basic structure of a standard I/O port bit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Figure 14. Basic structure of a five-volt tolerant I/O port bit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Figure 15. Input floating/pull up/pull down configurations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Figure 16. Output configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Figure 17. Alternate function configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Figure 18. High impedance-analog configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Figure 19. ADC / DAC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Figure 20. External interrupt/event controller block diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Figure 21. External interrupt/event GPIO mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Figure 22. Single ADC block diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Figure 23. Timing diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Figure 24. Analog watchdog guarded area . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Figure 25. Injected conversion latency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Figure 26. Calibration timing diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Figure 27. Right alignment of data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Figure 28. Left alignment of data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Figure 29. Dual ADC block diagram
(1)
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Figure 30. Injected simultaneous mode on 4 channels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Figure 31. Regular simultaneous mode on 16 channels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Figure 32. Fast interleaved mode on 1 channel in continuous conversion mode . . . . . . . . . . . . . . . 222
Figure 33. Slow interleaved mode on 1 channel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Figure 34. Alternate trigger: injected channel group of each ADC. . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Figure 35. Alternate trigger: 4 injected channels (each ADC) in discontinuous model . . . . . . . . . . . 224
Figure 36. Alternate + Regular simultaneous. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Figure 37. Case of trigger occurring during injected conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Figure 38. Interleaved single channel with injected sequence CH11, CH12 . . . . . . . . . . . . . . . . . . . 225
Figure 39. Temperature sensor and VREFINT channel block diagram . . . . . . . . . . . . . . . . . . . . . . 226
Figure 40. DAC channel block diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Figure 41. Data registers in single DAC channel mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Figure 42. Data registers in dual DAC channel mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Figure 43. Timing diagram for conversion with trigger disabled TEN = 0 . . . . . . . . . . . . . . . . . . . . . 249
Figure 44. DAC LFSR register calculation algorithm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Figure 45. DAC conversion (SW trigger enabled) with LFSR wave generation. . . . . . . . . . . . . . . . . 251
Figure 46. DAC triangle wave generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Figure 47. DAC conversion (SW trigger enabled) with triangle wave generation . . . . . . . . . . . . . . . 252
Figure 48. DMA block diagram in connectivity line devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
RM0008 List of figures
Doc ID 13902 Rev 13 33/1093
Figure 49. DMA block diagram in low-, medium- high- and XL-density devices . . . . . . . . . . . . . . . . 267
Figure 50. DMA1 request mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
Figure 51. DMA2 request mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Figure 52. Advanced-control timer block diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Figure 53. Counter timing diagram with prescaler division change from 1 to 2 . . . . . . . . . . . . . . . . . 286
Figure 54. Counter timing diagram with prescaler division change from 1 to 4 . . . . . . . . . . . . . . . . . 286
Figure 55. Counter timing diagram, internal clock divided by 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Figure 56. Counter timing diagram, internal clock divided by 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Figure 57. Counter timing diagram, internal clock divided by 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Figure 58. Counter timing diagram, internal clock divided by N. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Figure 59. Counter timing diagram, update event when ARPE=0 (TIMx_ARR not preloaded) . . . . . 288
Figure 60. Counter timing diagram, update event when ARPE=1
(TIMx_ARR preloaded) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Figure 61. Counter timing diagram, internal clock divided by 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Figure 62. Counter timing diagram, internal clock divided by 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Figure 63. Counter timing diagram, internal clock divided by 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Figure 64. Counter timing diagram, internal clock divided by N. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Figure 65. Counter timing diagram, update event when repetition counter
is not used . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Figure 66. Counter timing diagram, internal clock divided by 1, TIMx_ARR = 0x6 . . . . . . . . . . . . . . 292
Figure 67. Counter timing diagram, internal clock divided by 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Figure 68. Counter timing diagram, internal clock divided by 4, TIMx_ARR=0x36 . . . . . . . . . . . . . . 293
Figure 69. Counter timing diagram, internal clock divided by N. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Figure 70. Counter timing diagram, update event with ARPE=1 (counter underflow) . . . . . . . . . . . . 294
Figure 71. Counter timing diagram, Update event with ARPE=1 (counter overflow) . . . . . . . . . . . . . 294
Figure 72. Update rate examples depending on mode and TIMx_RCR register settings . . . . . . . . . 295
Figure 73. Control circuit in normal mode, internal clock divided by 1. . . . . . . . . . . . . . . . . . . . . . . . 296
Figure 74. TI2 external clock connection example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Figure 75. Control circuit in external clock mode 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Figure 76. External trigger input block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Figure 77. Control circuit in external clock mode 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Figure 78. Capture/compare channel (example: channel 1 input stage) . . . . . . . . . . . . . . . . . . . . . . 299
Figure 79. Capture/compare channel 1 main circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Figure 80. Output stage of capture/compare channel (channel 1 to 3) . . . . . . . . . . . . . . . . . . . . . . . 300
Figure 81. Output stage of capture/compare channel (channel 4). . . . . . . . . . . . . . . . . . . . . . . . . . . 300
Figure 82. PWM input mode timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
Figure 83. Output compare mode, toggle on OC1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
Figure 84. Edge-aligned PWM waveforms (ARR=8) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Figure 85. Center-aligned PWM waveforms (ARR=8) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Figure 86. Complementary output with dead-time insertion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Figure 87. Dead-time waveforms with delay greater than the negative pulse. . . . . . . . . . . . . . . . . . 307
Figure 88. Dead-time waveforms with delay greater than the positive pulse. . . . . . . . . . . . . . . . . . . 308
Figure 89. Output behavior in response to a break.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Figure 90. Clearing TIMx OCxREF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Figure 91. 6-step generation, COM example (OSSR=1) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Figure 92. Example of one pulse mode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Figure 93. Example of counter operation in encoder interface mode. . . . . . . . . . . . . . . . . . . . . . . . . 316
Figure 94. Example of encoder interface mode with TI1FP1 polarity inverted. . . . . . . . . . . . . . . . . . 316
Figure 95. Example of hall sensor interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Figure 96. Control circuit in reset mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Figure 97. Control circuit in gated mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Figure 98. Control circuit in trigger mode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
List of figures RM0008
34/1093 Doc ID 13902 Rev 13
Figure 99. Control circuit in external clock mode 2 + trigger mode . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Figure 100. General-purpose timer block diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
Figure 101. Counter timing diagram with prescaler division change from 1 to 2 . . . . . . . . . . . . . . . . . 352
Figure 102. Counter timing diagram with prescaler division change from 1 to 4 . . . . . . . . . . . . . . . . . 353
Figure 103. Counter timing diagram, internal clock divided by 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
Figure 104. Counter timing diagram, internal clock divided by 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
Figure 105. Counter timing diagram, internal clock divided by 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
Figure 106. Counter timing diagram, internal clock divided by N. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
Figure 107. Counter timing diagram, Update event when ARPE=0 (TIMx_ARR not preloaded). . . . . 355
Figure 108. Counter timing diagram, Update event when ARPE=1 (TIMx_ARR preloaded). . . . . . . . 356
Figure 109. Counter timing diagram, internal clock divided by 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Figure 110. Counter timing diagram, internal clock divided by 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Figure 111. Counter timing diagram, internal clock divided by 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Figure 112. Counter timing diagram, internal clock divided by N. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Figure 113. Counter timing diagram, Update event when repetition counter is not used . . . . . . . . . . 358
Figure 114. Counter timing diagram, internal clock divided by 1, TIMx_ARR=0x6 . . . . . . . . . . . . . . . 359
Figure 115. Counter timing diagram, internal clock divided by 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
Figure 116. Counter timing diagram, internal clock divided by 4, TIMx_ARR=0x36 . . . . . . . . . . . . . . 360
Figure 117. Counter timing diagram, internal clock divided by N. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
Figure 118. Counter timing diagram, Update event with ARPE=1 (counter underflow). . . . . . . . . . . . 361
Figure 119. Counter timing diagram, Update event with ARPE=1 (counter overflow) . . . . . . . . . . . . . 361
Figure 120. Control circuit in normal mode, internal clock divided by 1. . . . . . . . . . . . . . . . . . . . . . . . 362
Figure 121. TI2 external clock connection example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
Figure 122. Control circuit in external clock mode 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
Figure 123. External trigger input block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
Figure 124. Control circuit in external clock mode 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
Figure 125. Capture/compare channel (example: channel 1 input stage) . . . . . . . . . . . . . . . . . . . . . . 365
Figure 126. Capture/compare channel 1 main circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
Figure 127. Output stage of capture/compare channel (channel 1). . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Figure 128. PWM input mode timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Figure 129. Output compare mode, toggle on OC1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Figure 130. Edge-aligned PWM waveforms (ARR=8) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Figure 131. Center-aligned PWM waveforms (ARR=8) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Figure 132. Example of one-pulse mode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Figure 133. Clearing TIMx OCxREF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
Figure 134. Example of counter operation in encoder interface mode . . . . . . . . . . . . . . . . . . . . . . . . 376
Figure 135. Example of encoder interface mode with TI1FP1 polarity inverted . . . . . . . . . . . . . . . . . 377
Figure 136. Control circuit in reset mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
Figure 137. Control circuit in gated mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Figure 138. Control circuit in trigger mode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Figure 139. Control circuit in external clock mode 2 + trigger mode . . . . . . . . . . . . . . . . . . . . . . . . . . 380
Figure 140. Master/Slave timer example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Figure 141. Gating timer 2 with OC1REF of timer 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
Figure 142. Gating timer 2 with Enable of timer 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
Figure 143. Triggering timer 2 with update of timer 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
Figure 144. Triggering timer 2 with Enable of timer 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
Figure 145. Triggering timer 1 and 2 with timer 1 TI1 input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Figure 146. General-purpose timer block diagram (TIM9 and TIM12) . . . . . . . . . . . . . . . . . . . . . . . . 409
Figure 147. General-purpose timer block diagram (TIM10/11/13/14) . . . . . . . . . . . . . . . . . . . . . . . . 410
Figure 148. Counter timing diagram with prescaler division change from 1 to 2 . . . . . . . . . . . . . . . . . 412
Figure 149. Counter timing diagram with prescaler division change from 1 to 4 . . . . . . . . . . . . . . . . . 412
Figure 150. Counter timing diagram, internal clock divided by 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
RM0008 List of figures
Doc ID 13902 Rev 13 35/1093
Figure 151. Counter timing diagram, internal clock divided by 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
Figure 152. Counter timing diagram, internal clock divided by 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
Figure 153. Counter timing diagram, internal clock divided by N. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
Figure 154. Counter timing diagram, update event when ARPE=0 (TIMx_ARR not
preloaded). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
Figure 155. Counter timing diagram, update event when ARPE=1 (TIMx_ARR
preloaded). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
Figure 156. Control circuit in normal mode, internal clock divided by 1. . . . . . . . . . . . . . . . . . . . . . . . 416
Figure 157. TI2 external clock connection example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
Figure 158. Control circuit in external clock mode 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Figure 159. Capture/compare channel (example: channel 1 input stage) . . . . . . . . . . . . . . . . . . . . . . 417
Figure 160. Capture/compare channel 1 main circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
Figure 161. Output stage of capture/compare channel (channel 1). . . . . . . . . . . . . . . . . . . . . . . . . . . 418
Figure 162. PWM input mode timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
Figure 163. Output compare mode, toggle on OC1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
Figure 164. Edge-aligned PWM waveforms (ARR=8) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
Figure 165. Example of one pulse mode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
Figure 166. Control circuit in reset mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
Figure 167. Control circuit in gated mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
Figure 168. Control circuit in trigger mode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
Figure 169. Basic timer block diagram. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
Figure 170. Counter timing diagram with prescaler division change from 1 to 2 . . . . . . . . . . . . . . . . . 451
Figure 171. Counter timing diagram with prescaler division change from 1 to 4 . . . . . . . . . . . . . . . . . 451
Figure 172. Counter timing diagram, internal clock divided by 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
Figure 173. Counter timing diagram, internal clock divided by 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
Figure 174. Counter timing diagram, internal clock divided by 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
Figure 175. Counter timing diagram, internal clock divided by N. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
Figure 176. Counter timing diagram, update event when ARPE = 0 (TIMx_ARR not
preloaded). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
Figure 177. Counter timing diagram, update event when ARPE=1 (TIMx_ARR
preloaded). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
Figure 178. Control circuit in normal mode, internal clock divided by 1. . . . . . . . . . . . . . . . . . . . . . . . 455
Figure 179. RTC simplified block diagram. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
Figure 180. RTC second and alarm waveform example with PR=0003, ALARM=00004 . . . . . . . . . . 465
Figure 181. RTC Overflow waveform example with PR=0003. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
Figure 182. Independent watchdog block diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Figure 183. Watchdog block diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
Figure 184. Window watchdog timing diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
Figure 185. FSMC block diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
Figure 186. FSMC memory banks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
Figure 187. Mode1 read accesses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
Figure 188. Mode1 write accesses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
Figure 189. ModeA read accesses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
Figure 190. ModeA write accesses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
Figure 191. Mode2/B read accesses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500
Figure 192. Mode2 write accesses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500
Figure 193. ModeB write accesses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
Figure 194. ModeC read accesses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
Figure 195. ModeC write accesses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
Figure 196. ModeD read accesses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
Figure 197. Multiplexed read accesses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507
Figure 198. Multiplexed write accesses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507
List of figures RM0008
36/1093 Doc ID 13902 Rev 13
Figure 199. Asynchronous wait during a read access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509
Figure 200. Asynchronous wait during a write access. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510
Figure 201. Wait configurations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
Figure 202. Synchronous multiplexed read mode - NOR, PSRAM (CRAM) . . . . . . . . . . . . . . . . . . . . 513
Figure 203. Synchronous multiplexed write mode - PSRAM (CRAM) . . . . . . . . . . . . . . . . . . . . . . . . . 515
Figure 204. NAND/PC Card controller timing for common memory access . . . . . . . . . . . . . . . . . . . 526
Figure 205. Access to non CE dont care NAND-Flash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
Figure 206. SDIO no response and no data operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540
Figure 207. SDIO (multiple) block read operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540
Figure 208. SDIO (multiple) block write operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
Figure 209. SDIO sequential read operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
Figure 210. SDIO sequential write operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
Figure 211. SDIO block diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
Figure 212. SDIO adapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
Figure 213. Control unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
Figure 214. SDIO adapter command path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
Figure 215. Command path state machine (CPSM) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546
Figure 216. SDIO command transfer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547
Figure 217. Data path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549
Figure 218. Data path state machine (DPSM) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550
Figure 219. USB peripheral block diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596
Figure 220. Packet buffer areas with examples of buffer description table locations . . . . . . . . . . . . . 600
Figure 221. CAN network topology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627
Figure 222. Dual CAN block diagram (connectivity devices) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629
Figure 223. bxCAN operating modes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631
Figure 224. bxCAN in silent mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632
Figure 225. bxCAN in loop back mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632
Figure 226. bxCAN in combined mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633
Figure 227. Transmit mailbox states . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634
Figure 228. Receive FIFO states . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635
Figure 229. Filter bank scale configuration - register organization . . . . . . . . . . . . . . . . . . . . . . . . . . . 638
Figure 230. Example of filter numbering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639
Figure 231. Filtering mechanism - example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 640
Figure 232. CAN error state diagram. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
Figure 233. Bit timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643
Figure 234. CAN frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644
Figure 235. Event flags and interrupt generation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645
Figure 236. SPI block diagram. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673
Figure 237. Single master/ single slave application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 674
Figure 238. Data clock timing diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675
Figure 239. TXE/RXNE/BSY behavior in Master / full-duplex mode (BIDIMODE=0 and RXONLY=0)
in the case of continuous transfers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 682
Figure 240. TXE/RXNE/BSY behavior in Slave / full-duplex mode (BIDIMODE=0, RXONLY=0) in the
case of continuous transfers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 682
Figure 241. TXE/BSY behavior in Master transmit-only mode (BIDIMODE=0 and RXONLY=0) in the
case of continuous transfers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683
Figure 242. TXE/BSY in Slave transmit-only mode (BIDIMODE=0 and RXONLY=0) in the case of
continuous transfers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 684
Figure 243. RXNE behavior in receive-only mode (BIDIRMODE=0 and RXONLY=1) in the case of
continuous transfers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 685
Figure 244. TXE/BSY behavior when transmitting (BIDIRMODE=0 and RXONLY=0) in the case of
discontinuous transfers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 686
RM0008 List of figures
Doc ID 13902 Rev 13 37/1093
Figure 245. Transmission using DMA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 690
Figure 246. Reception using DMA. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691
Figure 247. I
2
S block diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693
Figure 248. I
2
S Phillips protocol waveforms (16/32-bit full accuracy, CPOL = 0) . . . . . . . . . . . . . . . . 695
Figure 249. I
2
S Phillips standard waveforms (24-bit frame with CPOL = 0) . . . . . . . . . . . . . . . . . . . . 695
Figure 250. Transmitting 0x8EAA33 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 696
Figure 251. Receiving 0x8EAA33 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 696
Figure 252. I
2
S Phillips standard (16-bit extended to 32-bit packet frame with CPOL = 0) . . . . . . . . . 696
Figure 253. Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697
Figure 254. MSB Justified 16-bit or 32-bit full-accuracy length with CPOL = 0 . . . . . . . . . . . . . . . . . . 697
Figure 255. MSB Justified 24-bit frame length with CPOL = 0. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 698
Figure 256. MSB Justified 16-bit extended to 32-bit packet frame with CPOL = 0 . . . . . . . . . . . . . . . 698
Figure 257. LSB justified 16-bit or 32-bit full-accuracy with CPOL = 0 . . . . . . . . . . . . . . . . . . . . . . . . 698
Figure 258. LSB Justified 24-bit frame length with CPOL = 0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699
Figure 259. Operations required to transmit 0x3478AE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699
Figure 260. Operations required to receive 0x3478AE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699
Figure 261. LSB Justified 16-bit extended to 32-bit packet frame with CPOL = 0. . . . . . . . . . . . . . . . 700
Figure 262. Example of LSB justified 16-bit extended to 32-bit packet frame . . . . . . . . . . . . . . . . . . . 700
Figure 263. PCM standard waveforms (16-bit) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701
Figure 264. PCM standard waveforms (16-bit extended to 32-bit packet frame). . . . . . . . . . . . . . . . . 701
Figure 265. Audio sampling frequency definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702
Figure 266. I
2
S clock generator architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 702
Figure 267. I2C bus protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723
Figure 268. I2C block diagram. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724
Figure 269. Transfer sequence diagram for slave transmitter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 725
Figure 270. Transfer sequence diagram for slave receiver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 726
Figure 271. Transfer sequence diagram for master transmitter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 729
Figure 272. Method 1: transfer sequence diagram for master receiver . . . . . . . . . . . . . . . . . . . . . . . . 731
Figure 273. Method 2: transfer sequence diagram for master receiver when N>2 . . . . . . . . . . . . . . . 732
Figure 274. Method 2: transfer sequence diagram for master receiver when N=2 . . . . . . . . . . . . . . . 733
Figure 275. Method 2: transfer sequence diagram for master receiver when N=1 . . . . . . . . . . . . . . . 734
Figure 276. I2C interrupt mapping diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741
Figure 277. USART block diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759
Figure 278. Word length programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 760
Figure 279. Configurable stop bits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 762
Figure 280. TC/TXE behavior when transmitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763
Figure 281. Start bit detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764
Figure 282. Data sampling for noise detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766
Figure 283. Mute mode using Idle line detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 771
Figure 284. Mute mode using address mark detection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 772
Figure 285. Break detection in LIN mode (11-bit break length - LBDL bit is set) . . . . . . . . . . . . . . . . . 774
Figure 286. Break detection in LIN mode vs. Framing error detection. . . . . . . . . . . . . . . . . . . . . . . . . 775
Figure 287. USART example of synchronous transmission. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 776
Figure 288. USART data clock timing diagram (M=0) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 776
Figure 289. USART data clock timing diagram (M=1) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777
Figure 290. RX data setup/hold time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777
Figure 291. ISO 7816-3 asynchronous protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 778
Figure 292. Parity error detection using the 1.5 stop bits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 780
Figure 293. IrDA SIR ENDEC- block diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 782
Figure 294. IrDA data modulation (3/16) -normal mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 782
Figure 295. Transmission using DMA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 783
Figure 296. Reception using DMA. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 784
List of figures RM0008
38/1093 Doc ID 13902 Rev 13
Figure 297. Hardware flow control between two USARTs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 785
Figure 298. RTS flow control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 785
Figure 299. CTS flow control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 786
Figure 300. USART interrupt mapping diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 787
Figure 301. Block diagram. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 801
Figure 302. OTG A-B device connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802
Figure 303. USB peripheral-only connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 804
Figure 304. USB host-only connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 809
Figure 305. SOF connectivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 813
Figure 306. Updating OTG_FS_HFIR dynamically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 815
Figure 307. Device-mode FIFO address mapping and AHB FIFO access mapping . . . . . . . . . . . . . . 816
Figure 308. Host-mode FIFO address mapping and AHB FIFO access mapping. . . . . . . . . . . . . . . . 817
Figure 309. Interrupt hierarchy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 821
Figure 310. CSR memory map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 823
Figure 311. Transmit FIFO write task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 892
Figure 312. Receive FIFO read task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 893
Figure 313. Normal bulk/control OUT/SETUP and bulk/control IN transactions . . . . . . . . . . . . . . . . . 895
Figure 314. Bulk/control IN transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 898
Figure 315. Normal interrupt OUT/IN transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 900
Figure 316. Normal isochronous OUT/IN transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 905
Figure 317. Receive FIFO packet read . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 911
Figure 318. Processing a SETUP packet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 913
Figure 319. Bulk OUT transaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 919
Figure 320. TRDT max timing case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 928
Figure 321. A-device SRP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 929
Figure 322. B-device SRP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 930
Figure 323. A-device HNP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 931
Figure 324. B-device HNP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 933
Figure 325. ETH block diagram. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 939
Figure 326. SMI interface signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 940
Figure 327. MDIO timing and frame structure - Write cycle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 941
Figure 328. MDIO timing and frame structure - Read cycle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 942
Figure 329. Media independent interface signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 943
Figure 330. MII clock sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 945
Figure 331. Reduced media-independent interface signals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 945
Figure 332. RMII clock sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 946
Figure 333. Clock scheme . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 946
Figure 334. Address field format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 948
Figure 335. MAC frame format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 950
Figure 336. Tagged MAC frame format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 950
Figure 337. Transmission bit order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 956
Figure 338. Transmission with no collision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 957
Figure 339. Transmission with collision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 957
Figure 340. Frame transmission in MMI and RMII modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 958
Figure 341. Receive bit order. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 962
Figure 342. Reception with no error. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 962
Figure 343. Reception with errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 962
Figure 344. Reception with false carrier indication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 963
Figure 345. MAC core interrupt masking scheme . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 963
Figure 346. Wakeup frame filter register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 968
Figure 347. Networked time synchronization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 971
Figure 348. System time update using the Fine correction method. . . . . . . . . . . . . . . . . . . . . . . . . . . 973
RM0008 List of figures
Doc ID 13902 Rev 13 39/1093
Figure 349. PTP trigger output to TIM2 ITR1 connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 975
Figure 350. PPS output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 976
Figure 351. Descriptor ring and chain structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 977
Figure 352. TxDMA operation in Default mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 981
Figure 353. TxDMA operation in OSF mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 983
Figure 354. ransmit descriptor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 984
Figure 355. Receive DMA operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 990
Figure 356. Rx DMA descriptor structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 992
Figure 357. Interrupt scheme. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 998
Figure 358. Ethernet MAC remote wakeup frame filter register (ETH_MACRWUFFR). . . . . . . . . . . 1008
Figure 359. Block diagram of STM32 MCU and Cortex-M3-level debug support . . . . . . . . . . . . . . . 1046
Figure 360. SWJ debug port . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1047
Figure 361. JTAG TAP connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1052
Figure 362. TPIU block diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1069
Overview of the manual RM0008
40/1093 Doc ID 13902 Rev 13
1 Overview of the manual
Legend for Table 1:
The section in each row applies to products in columns marked with "
Table 1. Sections related to each STM32F10xxx product
L
o
w
-
d
e
n
s
i
t
y
S
T
M
3
2
F
1
0
1
x
x
M
e
d
i
u
m
-
d
e
n
s
i
t
y
S
T
M
3
2
F
1
0
1
x
x
H
i
g
h
a
n
d
X
L
-
d
e
n
s
i
t
y
S
T
M
3
2
F
1
0
1
x
L
o
w
-
d
e
n
s
i
t
y
S
T
M
3
2
F
1
0
2
x
x
M
e
d
i
u
m
-
d
e
n
s
i
t
y
S
T
M
3
2
F
1
0
2
x
x
L
o
w
-
d
e
n
s
i
t
y
S
T
M
3
2
F
1
0
3
x
x
M
e
d
i
u
m
-
d
e
n
s
i
t
y
S
T
M
3
2
F
1
0
3
x
x
H
i
g
h
a
n
d
X
L
-
d
e
n
s
i
t
y
S
T
M
3
2
F
1
0
3
x
x
S
T
M
3
2
F
1
0
5
x
x
S
T
M
3
2
F
1
0
7
x
x
Section 2:
Documentation
conventions
Section 3: Memory and
bus architecture
Section 4: CRC
calculation unit
Section 5: Power
control (PWR)
Section 6: Backup
registers (BKP)
Section 7: Low-,
medium-, high- and XL-
density reset and clock
control (RCC)
Section 8: Connectivity
line devices: reset and
clock control (RCC)
Section 9: General-
purpose and alternate-
function I/Os (GPIOs
and AFIOs)
Section 10: Interrupts
and events
Section 13: Direct
memory access
controller (DMA)
RM0008 Overview of the manual
Doc ID 13902 Rev 13 41/1093
Section 11: Analog-to-
digital converter (ADC)
Section 12: Digital-to-
analog converter (DAC)
Section 14: Advanced-
control timers
(TIM1&TIM8)
Section 15: General-
purpose timers (TIM2 to
TIM5)
Section 16: General-
purpose timers (TIM9 to
TIM14)
(1)
(1)
Section 17: Basic
timers (TIM6&TIM7)
Section 18: Real-time
clock (RTC)
Section 19:
Independent watchdog
(IWDG)
Section 20: Window
watchdog (WWDG)
Section 21: Flexible
static memory
controller (FSMC)
Section 28: Secure
digital input/output
interface (SDIO)
Section 23: Universal
serial bus full-speed
device interface (USB)
Section 24: Controller
area network (bxCAN)
Table 1. Sections related to each STM32F10xxx product (continued)
L
o
w
-
d
e
n
s
i
t
y
S
T
M
3
2
F
1
0
1
x
x
M
e
d
i
u
m
-
d
e
n
s
i
t
y
S
T
M
3
2
F
1
0
1
x
x
H
i
g
h
a
n
d
X
L
-
d
e
n
s
i
t
y
S
T
M
3
2
F
1
0
1
x
L
o
w
-
d
e
n
s
i
t
y
S
T
M
3
2
F
1
0
2
x
x
M
e
d
i
u
m
-
d
e
n
s
i
t
y
S
T
M
3
2
F
1
0
2
x
x
L
o
w
-
d
e
n
s
i
t
y
S
T
M
3
2
F
1
0
3
x
x
M
e
d
i
u
m
-
d
e
n
s
i
t
y
S
T
M
3
2
F
1
0
3
x
x
H
i
g
h
a
n
d
X
L
-
d
e
n
s
i
t
y
S
T
M
3
2
F
1
0
3
x
x
S
T
M
3
2
F
1
0
5
x
x
S
T
M
3
2
F
1
0
7
x
x
Overview of the manual RM0008
42/1093 Doc ID 13902 Rev 13
Note: 1) Available only on XL-density devices
Section 25: Serial
peripheral interface
(SPI)
Section 26: Inter-
integrated circuit (I2C)
interface
Section 27: Universal
synchronous
asynchronous receiver
transmitter (USART)
Section 28: USB on-
the-go full-speed
(OTG_FS)
Section 29: Ethernet
(ETH): media access
control (MAC) with
DMA controller
The section in this row must be read when using the peripherals in columns
marked with "
The section in this row can optionally be read when using the peripherals in
columns marked with "
Table 2. Sections related to each peripheral
B
a
c
k
u
p
r
e
g
i
s
t
e
r
s
(
B
K
P
)
G
e
n
e
r
a
l
-
p
u
r
p
o
s
e
I
/
O
s
(
G
P
I
O
s
)
A
n
a
l
o
g
-
t
o
-
d
i
g
i
t
a
l
c
o
n
v
e
r
t
e
r
(
A
D
C
)
D
i
g
i
t
a
l
-
t
o
-
a
n
a
l
o
g
c
o
n
v
e
r
t
e
r
(
D
A
C
)
A
d
v
a
n
c
e
d
-
c
o
n
t
r
o
l
t
i
m
e
r
s
(
T
I
M
1
&
T
I
M
8
)
G
e
n
e
r
a
l
-
p
u
r
p
o
s
e
t
i
m
e
r
s
(
T
I
M
2
t
o
T
I
M
5
)
G
e
n
e
r
a
l
-
p
u
r
p
o
s
e
t
i
m
e
r
s
(
T
I
M
9
t
o
T
I
M
1
4
)
B
a
s
i
c
t
i
m
e
r
s
(
T
I
M
6
&
T
I
M
7
)
R
e
a
l
-
t
i
m
e
c
l
o
c
k
(
R
T
C
)
I
n
d
e
p
e
n
d
e
n
t
w
a
t
c
h
d
o
g
(
I
W
D
G
)
W
i
n
d
o
w
w
a
t
c
h
d
o
g
(
W
W
D
G
)
F
l
e
x
i
b
l
e
s
t
a
t
i
c
m
e
m
o
r
y
c
o
n
t
r
o
l
l
e
r
(
F
S
M
C
)
S
e
c
u
r
e
d
i
g
i
t
a
l
i
n
p
u
t
/
o
u
t
p
u
t
i
n
t
e
r
f
a
c
e
(
S
D
I
O
)
U
S
B
f
u
l
l
-
s
p
e
e
d
d
e
v
i
c
e
(
U
S
B
)
C
o
n
t
r
o
l
l
e
r
a
r
e
a
n
e
t
w
o
r
k
(
b
x
C
A
N
)
S
e
r
i
a
l
p
e
r
i
p
h
e
r
a
l
i
n
t
e
r
f
a
c
e
(
S
P
I
)
I
n
t
e
r
-
i
n
t
e
g
r
a
t
e
d
c
i
r
c
u
i
t
(
I
2
C
)
i
n
t
e
r
f
a
c
e
U
S
A
R
T
U
S
B
o
n
-
t
h
e
-
g
o
f
u
l
l
-
s
p
e
e
d
(
O
T
G
_
F
S
)
E
t
h
e
r
n
e
t
(
E
T
H
)
Section 2:
Documentation
conventions
Section 3: Memory and
bus architecture
Section 4: CRC
calculation unit
Section 5: Power
control (PWR)
Section 6: Backup
registers (BKP)
Section 7: Low-,
medium-, high- and XL-
density reset and clock
control (RCC)
Section 8: Connectivity
line devices: reset and
clock control (RCC)
Section 9: General-
purpose and alternate-
function I/Os (GPIOs
and AFIOs)
Section 10: Interrupts
and events
Overview of the manual RM0008
44/1093 Doc ID 13902 Rev 13
Section 13: Direct
memory access
controller (DMA)
Section 11: Analog-to-
digital converter (ADC)
Section 12: Digital-to-
analog converter (DAC)
n
c
r
e
m
e
n
t
a
t
i
o
n
ai14715c
D
e
c
r
e
m
e
n
t
a
t
i
o
n
0
Digital-to-analog converter (DAC) RM0008
252/1093 Doc ID 13902 Rev 13
Figure 47. DAC conversion (SW trigger enabled) with triangle wave generation
Note: 1 DAC trigger must be enabled for noise generation, by setting the TENx bit in the DAC_CR
register.
2 MAMPx[3:0] bits must be configured before enabling the DAC, otherwise they cannot be
changed.
12.4 Dual DAC channel conversion
To efficiently use the bus bandwidth in applications that require the two DAC channels at the
same time, three dual registers are implemented: DHR8RD, DHR12RD and DHR12LD. A
unique register access is then required to drive both DAC channels at the same time.
Eleven possible conversion modes are possible using the two DAC channels and these dual
registers. All the conversion modes can nevertheless be obtained using separate DHRx
registers if needed.
All modes are described in the paragraphs below.
12.4.1 Independent trigger without wave generation
To configure the DAC in this conversion mode, the following sequence is required:
Set the two DAC channel trigger enable bits TEN1 and TEN2
Configure different trigger sources by setting different values in the TSEL1[2:0] and
TSEL2[2:0] bits
Load the dual DAC channel data into the desired DHR register (DAC_DHR12RD,
DAC_DHR12LD or DAC_DHR8RD)
When a DAC channel1 trigger arrives, the DHR1 register is transferred into DAC_DOR1
(three APB1 clock cycles later).
When a DAC channel2 trigger arrives, the DHR2 register is transferred into DAC_DOR2
(three APB1 clock cycles later).
APB1_CLK
0xABE
0xABE
DHR
DOR
ai14714
0xABF
SWTRIG
0xAC0
RM0008 Digital-to-analog converter (DAC)
Doc ID 13902 Rev 13 253/1093
12.4.2 Independent trigger with same LFSR generation
To configure the DAC in this conversion mode, the following sequence is required:
Set the two DAC channel trigger enable bits TEN1 and TEN2
Configure different trigger sources by setting different values in the TSEL1[2:0] and
TSEL2[2:0] bits
Configure the two DAC channel WAVEx[1:0] bits as 01 and the same LFSR mask
value in the MAMPx[3:0] bits
Load the dual DAC channel data into the desired DHR register (DHR12RD, DHR12LD
or DHR8RD)
When a DAC channel1 trigger arrives, the LFSR1 counter, with the same mask, is added to
the DHR1 register and the sum is transferred into DAC_DOR1 (three APB1 clock cycles
later). Then the LFSR1 counter is updated.
When a DAC channel2 trigger arrives, the LFSR2 counter, with the same mask, is added to
the DHR2 register and the sum is transferred into DAC_DOR2 (three APB1 clock cycles
later). Then the LFSR2 counter is updated.
12.4.3 Independent trigger with different LFSR generation
To configure the DAC in this conversion mode, the following sequence is required:
Set the two DAC channel trigger enable bits TEN1 and TEN2
Configure different trigger sources by setting different values in the TSEL1[2:0] and
TSEL2[2:0] bits
Configure the two DAC channel WAVEx[1:0] bits as 01 and set different LFSR masks
values in the MAMP1[3:0] and MAMP2[3:0] bits
Load the dual DAC channel data into the desired DHR register (DAC_DHR12RD,
DAC_DHR12LD or DAC_DHR8RD)
When a DAC channel1 trigger arrives, the LFSR1 counter, with the mask configured by
MAMP1[3:0], is added to the DHR1 register and the sum is transferred into DAC_DOR1
(three APB1 clock cycles later). Then the LFSR1 counter is updated.
When a DAC channel2 trigger arrives, the LFSR2 counter, with the mask configured by
MAMP2[3:0], is added to the DHR2 register and the sum is transferred into DAC_DOR2
(three APB1 clock cycles later). Then the LFSR2 counter is updated.
12.4.4 Independent trigger with same triangle generation
To configure the DAC in this conversion mode, the following sequence is required:
Set the two DAC channel trigger enable bits TEN1 and TEN2
Configure different trigger sources by setting different values in the TSEL1[2:0] and
TSEL2[2:0] bits
Configure the two DAC channel WAVEx[1:0] bits as 1x and the same maximum
amplitude value in the MAMPx[3:0] bits
Load the dual DAC channel data into the desired DHR register (DAC_DHR12RD,
DAC_DHR12LD or DAC_DHR8RD)
When a DAC channel1 trigger arrives, the DAC channel1 triangle counter, with the same
triangle amplitude, is added to the DHR1 register and the sum is transferred into
Digital-to-analog converter (DAC) RM0008
254/1093 Doc ID 13902 Rev 13
DAC_DOR1 (three APB1 clock cycles later). The DAC channel1 triangle counter is then
updated.
When a DAC channel2 trigger arrives, the DAC channel2 triangle counter, with the same
triangle amplitude, is added to the DHR2 register and the sum is transferred into
DAC_DOR2 (three APB1 clock cycles later). The DAC channel2 triangle counter is then
updated.
12.4.5 Independent trigger with different triangle generation
To configure the DAC in this conversion mode, the following sequence is required:
Set the two DAC channel trigger enable bits TEN1 and TEN2
Configure different trigger sources by setting different values in the TSEL1[2:0] and
TSEL2[2:0] bits
Configure the two DAC channel WAVEx[1:0] bits as 1x and set different maximum
amplitude values in the MAMP1[3:0] and MAMP2[3:0] bits
Load the dual DAC channel data into the desired DHR register (DAC_DHR12RD,
DAC_DHR12LD or DAC_DHR8RD)
When a DAC channel1 trigger arrives, the DAC channel1 triangle counter, with a triangle
amplitude configured by MAMP1[3:0], is added to the DHR1 register and the sum is
transferred into DAC_DOR1 (three APB1 clock cycles later). The DAC channel1 triangle
counter is then updated.
When a DAC channel2 trigger arrives, the DAC channel2 triangle counter, with a triangle
amplitude configured by MAMP2[3:0], is added to the DHR2 register part and the sum is
transferred into DAC_DOR2 (three APB1 clock cycles later). The DAC channel2 triangle
counter is then updated.
12.4.6 Simultaneous software start
To configure the DAC in this conversion mode, the following sequence is required:
Load the dual DAC channel data to the desired DHR register (DAC_DHR12RD,
DAC_DHR12LD or DAC_DHR8RD)
In this configuration, one APB1 clock cycle later, the DHR1 and DHR2 registers are
transferred into DAC_DOR1 and DAC_DOR2, respectively.
12.4.7 Simultaneous trigger without wave generation
To configure the DAC in this conversion mode, the following sequence is required:
Set the two DAC channel trigger enable bits TEN1 and TEN2
Configure the same trigger source for both DAC channels by setting the same value in
the TSEL1[2:0] and TSEL2[2:0] bits
Load the dual DAC channel data to the desired DHR register (DAC_DHR12RD,
DAC_DHR12LD or DAC_DHR8RD)
When a trigger arrives, the DHR1 and DHR2 registers are transferred into DAC_DOR1 and
DAC_DOR2, respectively (after three APB1 clock cycles).
RM0008 Digital-to-analog converter (DAC)
Doc ID 13902 Rev 13 255/1093
12.4.8 Simultaneous trigger with same LFSR generation
To configure the DAC in this conversion mode, the following sequence is required:
Set the two DAC channel trigger enable bits TEN1 and TEN2
Configure the same trigger source for both DAC channels by setting the same value in
the TSEL1[2:0] and TSEL2[2:0] bits
Configure the two DAC channel WAVEx[1:0] bits as 01 and the same LFSR mask
value in the MAMPx[3:0] bits
Load the dual DAC channel data to the desired DHR register (DHR12RD, DHR12LD or
DHR8RD)
When a trigger arrives, the LFSR1 counter, with the same mask, is added to the DHR1
register and the sum is transferred into DAC_DOR1 (three APB1 clock cycles later). The
LFSR1 counter is then updated. At the same time, the LFSR2 counter, with the same mask,
is added to the DHR2 register and the sum is transferred into DAC_DOR2 (three APB1 clock
cycles later). The LFSR2 counter is then updated.
12.4.9 Simultaneous trigger with different LFSR generation
To configure the DAC in this conversion mode, the following sequence is required:
Set the two DAC channel trigger enable bits TEN1 and TEN2
Configure the same trigger source for both DAC channels by setting the same value in
the TSEL1[2:0] and TSEL2[2:0] bits
Configure the two DAC channel WAVEx[1:0] bits as 01 and set different LFSR masks
values using the MAMP1[3:0] and MAMP2[3:0] bits
Load the dual DAC channel data into the desired DHR register (DAC_DHR12RD,
DAC_DHR12LD or DAC_DHR8RD)
When a trigger arrives, the LFSR1 counter, with the mask configured by MAMP1[3:0], is
added to the DHR1 register and the sum is transferred into DAC_DOR1 (three APB1 clock
cycles later). The LFSR1 counter is then updated.
At the same time, the LFSR2 counter, with the mask configured by MAMP2[3:0], is added to
the DHR2 register and the sum is transferred into DAC_DOR2 (three APB1 clock cycles
later). The LFSR2 counter is then updated.
12.4.10 Simultaneous trigger with same triangle generation
To configure the DAC in this conversion mode, the following sequence is required:
Set the two DAC channel trigger enable bits TEN1 and TEN2
Configure the same trigger source for both DAC channels by setting the same value in
the TSEL1[2:0] and TSEL2[2:0] bits
Configure the two DAC channel WAVEx[1:0] bits as 1x and the same maximum
amplitude value using the MAMPx[3:0] bits
Load the dual DAC channel data into the desired DHR register (DAC_DHR12RD,
DAC_DHR12LD or DAC_DHR8RD)
When a trigger arrives, the DAC channel1 triangle counter, with the same triangle amplitude,
is added to the DHR1 register and the sum is transferred into DAC_DOR1 (three APB1 clock
cycles later). The DAC channel1 triangle counter is then updated.
At the same time, the DAC channel2 triangle counter, with the same triangle amplitude, is
Digital-to-analog converter (DAC) RM0008
256/1093 Doc ID 13902 Rev 13
added to the DHR2 register and the sum is transferred into DAC_DOR2 (three APB1 clock
cycles later). The DAC channel2 triangle counter is then updated.
12.4.11 Simultaneous trigger with different triangle generation
To configure the DAC in this conversion mode, the following sequence is required:
Set the two DAC channel trigger enable bits TEN1 and TEN2
Configure the same trigger source for both DAC channels by setting the same value in
the TSEL1[2:0] and TSEL2[2:0] bits
Configure the two DAC channel WAVEx[1:0] bits as 1x and set different maximum
amplitude values in the MAMP1[3:0] and MAMP2[3:0] bits
Load the dual DAC channel data into the desired DHR register (DAC_DHR12RD,
DAC_DHR12LD or DAC_DHR8RD)
When a trigger arrives, the DAC channel1 triangle counter, with a triangle amplitude
configured by MAMP1[3:0], is added to the DHR1 register and the sum is transferred into
DAC_DOR1 (three APB1 clock cycles later). Then the DAC channel1 triangle counter is
updated.
At the same time, the DAC channel2 triangle counter, with a triangle amplitude configured
by MAMP2[3:0], is added to the DHR2 register and the sum is transferred into DAC_DOR2
(three APB1 clock cycles later). Then the DAC channel2 triangle counter is updated.
12.5 DAC registers
The peripheral registers have to be accessed by words (32-bit).
12.5.1 DAC control register (DAC_CR)
Address offset: 0x00
Reset value: 0x0000 0000
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
DMA
EN2
MAMP2[3:0] WAVE2[1:0] TSEL2[2:0] TEN2 BOFF2 EN2
rw rw rw rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
DMA
EN1
MAMP1[3:0] WAVE1[1:0] TSEL1[2:0] TEN1 BOFF1 EN1
rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:29 Reserved.
Bit 28 DMAEN2: DAC channel2 DMA enable
This bit is set and cleared by software.
0: DAC channel2 DMA mode disabled
1: DAC channel2 DMA mode enabled
RM0008 Digital-to-analog converter (DAC)
Doc ID 13902 Rev 13 257/1093
Bit 27:24 MAMP2[3:0]: DAC channel2 mask/amplitude selector
These bits are written by software to select mask in wave generation mode or amplitude in
triangle generation mode.
0000: Unmask bit0 of LFSR/ Triangle Amplitude equal to 1
0001: Unmask bits[1:0] of LFSR/ Triangle Amplitude equal to 3
0010: Unmask bits[2:0] of LFSR/ Triangle Amplitude equal to 7
0011: Unmask bits[3:0] of LFSR/ Triangle Amplitude equal to 15
0100: Unmask bits[4:0] of LFSR/ Triangle Amplitude equal to 31
0101: Unmask bits[5:0] of LFSR/ Triangle Amplitude equal to 63
0110: Unmask bits[6:0] of LFSR/ Triangle Amplitude equal to 127
0111: Unmask bits[7:0] of LFSR/ Triangle Amplitude equal to 255
1000: Unmask bits[8:0] of LFSR/ Triangle Amplitude equal to 511
1001: Unmask bits[9:0] of LFSR/ Triangle Amplitude equal to 1023
1010: Unmask bits[10:0] of LFSR/ Triangle Amplitude equal to 2047
1011: Unmask bits[11:0] of LFSR/ Triangle Amplitude equal to 4095
Bit 23:22 WAVE2[1:0]: DAC channel2 noise/triangle wave generation enable
These bits are set/reset by software.
00: wave generation disabled
01: Noise wave generation enabled
1x: Triangle wave generation enabled
Note: only used if bit TEN2 = 1 (DAC channel2 trigger enabled)
Bits 21:19 TSEL2[2:0]: DAC channel2 trigger selection
These bits select the external event used to trigger DAC channel2
000: Timer 6 TRGO event
001: Timer 3 TRGO event in connectivity line devices, Timer 8 TRGO in high-density and
XL-density devices
010: Timer 7 TRGO event
011: Timer 5 TRGO event
100: Timer 2 TRGO event
101: Timer 4 TRGO event
110: External line9
111: Software trigger
Note: only used if bit TEN2 = 1 (DAC channel2 trigger enabled)
Bit 18 TEN2: DAC channel2 trigger enable
This bit set and cleared by software to enable/disable DAC channel2 trigger
0: DAC channel2 trigger disabled and data written into DAC_DHRx register is transferred
one APB1 clock cycle later to the DAC_DOR2 register.
1: DAC channel2 trigger enabled and data transfer from DAC_DHRx register is transferred
three APB1 clock cycles later to the DAC_DOR2 register.
Note: When software trigger is selected, it takes only one APB1 clock cycle for DAC_DHRx to
DAC_DOR2 register transfer.
Bit 17 BOFF2: DAC channel2 output buffer disable
This bit set and cleared by software to enable/disable DAC channel2 output buffer.
0: DAC channel2 output buffer enabled
1: DAC channel2 output buffer disabled
Bit 16 EN2: DAC channel2 enable
This bit set and cleared by software to enable/disable DAC channel2.
0: DAC channel2 disabled
1: DAC channel2 enabled
Digital-to-analog converter (DAC) RM0008
258/1093 Doc ID 13902 Rev 13
Bits 15:13 Reserved.
Bit 12 DMAEN1: DAC channel1 DMA enable
This bit is set and cleared by software.
0: DAC channel1 DMA mode disabled
1: DAC channel1 DMA mode enabled
Bits 11:8 MAMP1[3:0]: DAC channel1 mask/amplitude selector
These bits are written by software to select mask in wave generation mode or amplitude in
triangle generation mode.
0000: Unmask bit0 of LFSR/ Triangle Amplitude equal to 1
0001: Unmask bits[1:0] of LFSR/ Triangle Amplitude equal to 3
0010: Unmask bits[2:0] of LFSR/ Triangle Amplitude equal to 7
0011: Unmask bits[3:0] of LFSR/ Triangle Amplitude equal to 15
0100: Unmask bits[4:0] of LFSR/ Triangle Amplitude equal to 31
0101: Unmask bits[5:0] of LFSR/ Triangle Amplitude equal to 63
0110: Unmask bits[6:0] of LFSR/ Triangle Amplitude equal to 127
0111: Unmask bits[7:0] of LFSR/ Triangle Amplitude equal to 255
1000: Unmask bits[8:0] of LFSR/ Triangle Amplitude equal to 511
1001: Unmask bits[9:0] of LFSR/ Triangle Amplitude equal to 1023
1010: Unmask bits[10:0] of LFSR/ Triangle Amplitude equal to 2047
1011: Unmask bits[11:0] of LFSR/ Triangle Amplitude equal to 4095
Bits 7:6 WAVE1[1:0]: DAC channel1 noise/triangle wave generation enable
These bits are set/reset by software.
00: wave generation disabled
01: Noise wave generation enabled
1x: Triangle wave generation enabled
Note: only used if bit TEN1 = 1 (DAC channel1 trigger enabled)
Bits 5:3 TSEL1[2:0]: DAC channel1 trigger selection
These bits select the external event used to trigger DAC channel1
000: Timer 6 TRGO event
001: Timer 3 TRGO event in connectivity line devices, Timer 8 TRGO in high-density and
XL-density devices
010: Timer 7 TRGO event
011: Timer 5 TRGO event
100: Timer 2 TRGO event
101: Timer 4 TRGO event
110: External line9
111: Software trigger
Note: only used if bit TEN1 = 1 (DAC channel1 trigger enabled)
Bit 2 TEN1: DAC channel1 trigger enable
This bit set and cleared by software to enable/disable DAC channel1 trigger
0: DAC channel1 trigger disabled and data written into DAC_DHRx register is transferred
one APB1 clock cycle later to the DAC_DOR1 register.
1: DAC channel1 trigger enabled and data transfer from DAC_DHRx register is transferred
three APB1 clock cycles later to the DAC_DOR1 register.
Note: When software trigger is selected, it takes only one APB1 clock cycle for DAC_DHRx to
DAC_DOR1 register transfer.
RM0008 Digital-to-analog converter (DAC)
Doc ID 13902 Rev 13 259/1093
12.5.2 DAC software trigger register (DAC_SWTRIGR)
Address offset: 0x04
Reset value: 0x0000 0000
Bit 1 BOFF1: DAC channel1 output buffer disable
This bit set and cleared by software to enable/disable DAC channel1 output buffer.
0: DAC channel1 output buffer enabled
1: DAC channel1 output buffer disabled
Bit 0 EN1: DAC channel1 enable
This bit set and cleared by software to enable/disable DAC channel1.
0: DAC channel1 disabled
1: DAC channel1 enabled
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
SWTRI
G2
SWTRI
G1
w w
Bits 31:2 Reserved.
Bit 1 SWTRIG2: DAC channel2 software trigger
This bit is set and cleared by software to enable/disable the software trigger.
0: Software trigger disabled
1: Software trigger enabled
Note: This bit is reset by hardware (one APB1 clock cycle later) once the DAC_DHR2 register
value is loaded to the DAC_DOR2 register.
Bit 0 SWTRIG1: DAC channel1 software trigger
This bit is set and cleared by software to enable/disable the software trigger.
0: Software trigger disabled
1: Software trigger enabled
Note: This bit is reset by hardware (one APB1 clock cycle later) once the DAC_DHR1 register
value is loaded to the DAC_DOR1 register.
Digital-to-analog converter (DAC) RM0008
260/1093 Doc ID 13902 Rev 13
12.5.3 DAC channel1 12-bit right-aligned data holding register
(DAC_DHR12R1)
Address offset: 0x08
Reset value: 0x0000 0000
12.5.4 DAC channel1 12-bit left aligned data holding register
(DAC_DHR12L1)
Address offset: 0x0C
Reset value: 0x0000 0000
12.5.5 DAC channel1 8-bit right aligned data holding register
(DAC_DHR8R1)
Address offset: 0x10
Reset value: 0x0000 0000
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
DACC1DHR[11:0]
rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:12 Reserved.
Bit 11:0 DACC1DHR[11:0]: DAC channel1 12-bit right-aligned data
These bits are written by software which specify 12-bit data for DAC channel1.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
DACC1DHR[11:0]
Reserved
rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:16 Reserved.
Bit 15:4 DACC1DHR[11:0]: DAC channel1 12-bit left-aligned data
These bits are written by software which specify 12-bit data for DAC channel1.
Bits 3:0 Reserved.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
DACC1DHR[7:0]
rw rw rw rw rw rw rw rw
Bits 31:8 Reserved.
Bits 7:0 DACC1DHR[7:0]: DAC channel1 8-bit right-aligned data
These bits are written by software which specify 8-bit data for DAC channel1.
RM0008 Digital-to-analog converter (DAC)
Doc ID 13902 Rev 13 261/1093
12.5.6 DAC channel2 12-bit right aligned data holding register
(DAC_DHR12R2)
Address offset: 0x14
Reset value: 0x0000 0000
12.5.7 DAC channel2 12-bit left aligned data holding register
(DAC_DHR12L2)
Address offset: 0x18
Reset value: 0x0000 0000
12.5.8 DAC channel2 8-bit right-aligned data holding register
(DAC_DHR8R2)
Address offset: 0x1C
Reset value: 0x0000 0000
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
DACC2DHR[11:0]
rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:12 Reserved.
Bits 11:0 DACC2DHR[11:0]: DAC channel2 12-bit right-aligned data
These bits are written by software which specify 12-bit data for DAC channel2.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
DACC2DHR[11:0]
Reserved
rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:16 Reserved.
Bits 15:4 DACC2DHR[11:0]: DAC channel2 12-bit left-aligned data
These bits are written by software which specify 12-bit data for DAC channel2.
Bits 3:0 Reserved.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
DACC2DHR[7:0]
rw rw rw rw rw rw rw rw
Bits 31:8 Reserved.
Bits 7:0 DACC2DHR[7:0]: DAC channel2 8-bit right-aligned data
These bits are written by software which specify 8-bit data for DAC channel2.
Digital-to-analog converter (DAC) RM0008
262/1093 Doc ID 13902 Rev 13
12.5.9 Dual DAC 12-bit right-aligned data holding register
(DAC_DHR12RD)
Address offset: 0x20
Reset value: 0x0000 0000
12.5.10 DUAL DAC 12-bit left aligned data holding register
(DAC_DHR12LD)
Address offset: 0x24
Reset value: 0x0000 0000
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
DACC2DHR[11:0]
rw rw rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
DACC1DHR[11:0]
rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:28 Reserved.
Bits 27:16 DACC2DHR[11:0]: DAC channel2 12-bit right-aligned data
These bits are written by software which specify 12-bit data for DAC channel2.
Bits 15:12 Reserved.
Bits 11:0 DACC1DHR[11:0]: DAC channel1 12-bit right-aligned data
These bits are written by software which specify 12-bit data for DAC channel1.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
DACC2DHR[11:0]
Reserved
rw rw rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
DACC1DHR[11:0]
Reserved
rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:20 DACC2DHR[11:0]: DAC channel2 12-bit left-aligned data
These bits are written by software which specify 12-bit data for DAC channel2.
Bits 19:16 Reserved.
Bits 15:4 DACC1DHR[11:0]: DAC channel1 12-bit left-aligned data
These bits are written by software which specify 12-bit data for DAC channel1.
Bits 3:0 Reserved.
RM0008 Digital-to-analog converter (DAC)
Doc ID 13902 Rev 13 263/1093
12.5.11 DUAL DAC 8-bit right aligned data holding register
(DAC_DHR8RD)
Address offset: 0x28
Reset value: 0x0000 0000
12.5.12 DAC channel1 data output register (DAC_DOR1)
Address offset: 0x2C
Reset value: 0x0000 0000
12.5.13 DAC channel2 data output register (DAC_DOR2)
Address offset: 0x30
Reset value: 0x0000 0000
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
DACC2DHR[7:0] DACC1DHR[7:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:16 Reserved.
Bits 15:8 DACC2DHR[7:0]: DAC channel2 8-bit right-aligned data
These bits are written by software which specify 8-bit data for DAC channel2.
Bits 7:0 DACC1DHR[7:0]: DAC channel1 8-bit right-aligned data
These bits are written by software which specify 8-bit data for DAC channel1.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
DACC1DOR[11:0]
r r r r r r r r r r r r
Bits 31:12 Reserved.
Bit 11:0 DACC1DOR[11:0]: DAC channel1 data output
These bits are read only, they contain data output for DAC channel1.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
DACC2DOR[11:0]
r r r r r r r r r r r r
Bits 31:12 Reserved.
Bit 11:0 DACC2DOR[11:0]: DAC channel2 data output
These bits are read only, they contain data output for DAC channel2.
Digital-to-analog converter (DAC) RM0008
264/1093 Doc ID 13902 Rev 13
12.5.14 DAC register map
The following table summarizes the DAC registers.
Note: Refer to Table 3 on page 50 for the register boundary addresses.
Table 75. DAC register map
Offset Register
3
1
3
0
2
9
2
8
2
7
2
6
2
5
2
4
2
3
2
2
2
1
2
0
1
9
1
8
1
7
1
6
1
5
1
4
1
3
1
2
1
1
1
0
9 8 7 6 5 4 3 2 1 0
0x00
DAC_CR
Reserved
D
M
A
E
N
2
MAMP2[3:0]
WAVE
2[2:0]
TSEL2[2:0]
T
E
N
2
B
O
F
F
2
E
N
2
Reserved
D
M
A
E
N
1
MAMP1[3:0]
WAVE
1[2:0]
TSEL1
[2:0]
T
E
N
1
B
O
F
F
1
E
N
1
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x04
DAC_SWTRIG
R
Reserved
S
W
T
R
I
G
2
S
W
T
R
I
G
1
Reset value 0 0
0x08
DAC_DHR12R
1
Reserved
DACC1DHR[11:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0
0x0C
DAC_DHR12L
1
Reserved
DACC1DHR[11:0]
Reserved
Reset value 0 0 0 0 0 0 0 0 0 0 0 0
0x10
DAC_DHR8R1
Reserved
DACC1DHR[7:0]
Reset value 0 0 0 0 0 0 0 0
0x14
DAC_DHR12R
2
Reserved
DACC2DHR[11:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0
0x18
DAC_DHR12L
2
Reserved
DACC2DHR[11:0]
Reserved
Reset value 0 0 0 0 0 0 0 0 0 0 0 0
0x1C
DAC_DHR8R2
Reserved
DACC2DHR[7:0]
0x20
DAC_DHR12R
D
Reserved
DACC2DHR[11:0]
Reserved
DACC1DHR[11:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x24
DAC_DHR12L
D
DACC2DHR[11:0]
Reserved
DACC1DHR[11:0]
Reserved
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x28
DAC_DHR8RD
Reserved
DACC2DHR[7:0] DACC1DHR[7:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x2C
DAC_DOR1
Reserved
DACC1DOR[11:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0
0x30
DAC_DOR2
Reserved
DACC2DOR[11:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0
RM0008 Direct memory access controller (DMA)
Doc ID 13902 Rev 13 265/1093
13 Direct memory access controller (DMA)
Low-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 16 and 32 Kbytes.
Medium-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 64 and 128 Kbytes.
High-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 256 and 512 Kbytes.
XL-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 768 Kbytes and 1 Mbyte.
Connectivity line devices are STM32F105xx and STM32F107xx microcontrollers.
This section applies to the whole STM32F10xxx family, unless otherwise specified.
13.1 DMA introduction
Direct memory access (DMA) is used in order to provide high-speed data transfer between
peripherals and memory as well as memory to memory. Data can be quickly moved by DMA
without any CPU actions. This keeps CPU resources free for other operations.
The two DMA controllers have 12 channels in total (7 for DMA1 and 5 for DMA2), each
dedicated to managing memory access requests from one or more peripherals. It has an
arbiter for handling the priority between DMA requests.
13.2 DMA main features
12 independently configurable channels (requests): 7 for DMA1 and 5 for DMA2
Each of the 12 channels is connected to dedicated hardware DMA requests, software
trigger is also supported on each channel. This configuration is done by software.
Priorities between requests from channels of one DMA are software programmable (4
levels consisting of very high, high, medium, low) or hardware in case of equality
(request 1 has priority over request 2, etc.)
Independent source and destination transfer size (byte, half word, word), emulating
packing and unpacking. Source/destination addresses must be aligned on the data
size.
Support for circular buffer management
3 event flags (DMA Half Transfer, DMA Transfer complete and DMA Transfer Error)
logically ORed together in a single interrupt request for each channel
Memory-to-memory transfer
Peripheral-to-memory and memory-to-peripheral, and peripheral-to-peripheral
transfers
Access to Flash, SRAM, APB1, APB2 and AHB peripherals as source and destination
Programmable number of data to be transferred: up to 65536
Direct memory access controller (DMA) RM0008
266/1093 Doc ID 13902 Rev 13
The block diagram is shown in Figure 48.
Figure 48. DMA block diagram in connectivity line devices
FLTF
Ch.1
Ch.2
Ch.7
Arbiter
Cortex-M3
SRAM
AHB Slave
DMA1
Code
DCode
System
DMA request
APB1
Flash
Bridge 1
Bridge 2
Ch.1
Ch.2
Ch.5
Arbiter
AHB Slave
DMA2
APB2
ai15811b
DMA request
B
u
s
m
a
t
r
i
x
DMA
D
M
A
Reset & clock
control (RCC)
GPOC
USART1
SP1
TM1
ADC2
ADC1
GPOE
GPOD
GPOB
GPOA
EXT
AFO
DAC SP3/2S
TM2
PWR
BKP
CAN1
CAN2
2C2
2C1
UART5
UART4
USART3
USART2
SP2/2S
WDG
WWDG
RTC
TM7
TM6
TM5
TM4
TM3
USB OTG FS
Ethernet MAC
D
M
A
RM0008 Direct memory access controller (DMA)
Doc ID 13902 Rev 13 267/1093
Figure 49. DMA block diagram in low-, medium- high- and XL-density devices
1. The DMA2 controller is available only in high-density and XL-density devices.
1. ADC3, SPI/I2S3, UART4, SDIO, TIM5, TIM6, DAC, TIM7, TIM8 DMA requests are available only in high-
density devices
13.3 DMA functional description
The DMA controller performs direct memory transfer by sharing the system bus with the
Cortex-M3 core. The DMA request may stop the CPU access to the system bus for some
bus cycles, when the CPU and DMA are targeting the same destination (memory or
peripheral). The bus matrix implements round-robin scheduling, thus ensuring at least half
of the system bus bandwidth (both to memory and peripheral) for the CPU.
13.3.1 DMA transactions
After an event, the peripheral sends a request signal to the DMA Controller. The DMA
controller serves the request depending on the channel priorities. As soon as the DMA
Controller accesses the peripheral, an Acknowledge is sent to the peripheral by the DMA
Controller. The peripheral releases its request as soon as it gets the Acknowledge from the
DMA Controller. Once the request is deasserted by the peripheral, the DMA Controller
release the Acknowledge. If there are more requests, the peripheral can initiate the next
transaction.
FLITF
Ch.1
Ch.2
Ch.7
Arbiter
Cortex-M3
SRAM
AHB Slave
DMA1
ICode
DCode
System
DMA request
APB2
Flash
Bridge 2
Bridge 1
USART1
SPI1
ADC1
ADC3
USART2
USART3
UART4
I2C2
I2C1
TIM2
TIM3
TIM4
Ch.1
Ch.2
Ch.5
Arbiter
AHB Slave
DMA2
FSMC
SDIO
APB1
DMA request
TIM1 SPI/I2S3
SPI/I2S2
TIM8
TIM5
TIM6
TIM7
ai14801b
DMA request
B
u
s
m
a
t
r
i
x
DMA
D
M
A
Reset & clock control
(RCC)
AHB System
Direct memory access controller (DMA) RM0008
268/1093 Doc ID 13902 Rev 13
In summary, each DMA transfer consists of three operations:
The loading of data from the peripheral data register or a location in memory
addressed through an internal current peripheral/memory address register. The start
address used for the first transfer is the base peripheral/memory address programmed
in the DMA_CPARx or DMA_CMARx register
The storage of the data loaded to the peripheral data register or a location in memory
addressed through an internal current peripheral/memory address register. The start
address used for the first transfer is the base peripheral/memory address programmed
in the DMA_CPARx or DMA_CMARx register
The post-decrementing of the DMA_CNDTRx register, which contains the number of
transactions that have still to be performed.
13.3.2 Arbiter
The arbiter manages the channel requests based on their priority and launches the
peripheral/memory access sequences.
The priorities are managed in two stages:
Software: each channel priority can be configured in the DMA_CCRx register. There
are four levels:
Very high priority
High priority
Medium priority
Low priority
Hardware: if 2 requests have the same software priority level, the channel with the
lowest number will get priority versus the channel with the highest number. For
example, channel 2 gets priority over channel 4.
Note: In high-density, XL-density and connectivity line devices, the DMA1 controller has priority
over the DMA2 controller.
13.3.3 DMA channels
Each channel can handle DMA transfer between a peripheral register located at a fixed
address and a memory address. The amount of data to be transferred (up to 65535) is
programmable. The register which contains the amount of data items to be transferred is
decremented after each transaction.
Programmable data sizes
Transfer data sizes of the peripheral and memory are fully programmable through the PSIZE
and MSIZE bits in the DMA_CCRx register.
Pointer incrementation
Peripheral and memory pointers can optionally be automatically post-incremented after
each transaction depending on the PINC and MINC bits in the DMA_CCRx register. If
incremented mode is enabled, the address of the next transfer will be the address of the
previous one incremented by 1, 2 or 4 depending on the chosen data size. The first transfer
address is the one programmed in the DMA_CPARx/DMA_CMARx registers. During
transfer operations, these registers keep the initially programmed value. The current transfer
RM0008 Direct memory access controller (DMA)
Doc ID 13902 Rev 13 269/1093
addresses (in the current internal peripheral/memory address register) are not accessible by
software.
If the channel is configured in noncircular mode, no DMA request is served after the last
transfer (that is once the number of data items to be transferred has reached zero). In order
to reload a new number of data items to be transferred into the DMA_CNDTRx register, the
DMA channel must be disabled.
Note: If a DMA channel is disabled, the DMA registers are not reset. The DMA channel registers
(DMA_CCRx, DMA_CPARx and DMA_CMARx) retain the initial values programmed during
the channel configuration phase.
In circular mode, after the last transfer, the DMA_CNDTRx register is automatically reloaded
with the initially programmed value. The current internal address registers are reloaded with
the base address values from the DMA_CPARx/DMA_CMARx registers.
Channel configuration procedure
The following sequence should be followed to configure a DMA channelx (where x is the
channel number).
1. Set the peripheral register address in the DMA_CPARx register. The data will be
moved from/ to this address to/ from the memory after the peripheral event.
2. Set the memory address in the DMA_CMARx register. The data will be written to or
read from this memory after the peripheral event.
3. Configure the total number of data to be transferred in the DMA_CNDTRx register.
After each peripheral event, this value will be decremented.
4. Configure the channel priority using the PL[1:0] bits in the DMA_CCRx register
5. Configure data transfer direction, circular mode, peripheral & memory incremented
mode, peripheral & memory data size, and interrupt after half and/or full transfer in the
DMA_CCRx register
6. Activate the channel by setting the ENABLE bit in the DMA_CCRx register.
As soon as the channel is enabled, it can serve any DMA request from the peripheral
connected on the channel.
Once half of the bytes are transferred, the half-transfer flag (HTIF) is set and an interrupt is
generated if the Half-Transfer Interrupt Enable bit (HTIE) is set. At the end of the transfer,
the Transfer Complete Flag (TCIF) is set and an interrupt is generated if the Transfer
Complete Interrupt Enable bit (TCIE) is set.
Circular mode
Circular mode is available to handle circular buffers and continuous data flows (e.g. ADC
scan mode). This feature can be enabled using the CIRC bit in the DMA_CCRx register.
When circular mode is activated, the number of data to be transferred is automatically
reloaded with the initial value programmed during the channel configuration phase, and the
DMA requests continue to be served.
Memory-to-memory mode
The DMA channels can also work without being triggered by a request from a peripheral.
This mode is called Memory to Memory mode.
If the MEM2MEM bit in the DMA_CCRx register is set, then the channel initiates transfers as
soon as it is enabled by software by setting the Enable bit (EN) in the DMA_CCRx register.
Direct memory access controller (DMA) RM0008
270/1093 Doc ID 13902 Rev 13
The transfer stops once the DMA_CNDTRx register reaches zero. Memory to Memory
mode may not be used at the same time as Circular mode.
13.3.4 Programmable data width, data alignment and endians
When PSIZE and MSIZE are not equal, the DMA performs some data alignments as
described in Table 76: Programmable data width & endian behavior (when bits PINC =
MINC = 1).
Addressing an AHB peripheral that does not support byte or halfword write
operations
When the DMA initiates an AHB byte or halfword write operation, the data are duplicated on
the unused lanes of the HWDATA[31:0] bus. So when the used AHB slave peripheral does
not support byte or halfword write operations (when HSIZE is not used by the peripheral)
Table 76. Programmable data width & endian behavior (when bits PINC = MINC = 1)
Source
port
width
Destination
port width
Number
of data
items to
transfer
(NDT)
Source content:
address / data
Transfer operations
Destination
content:
address / data
8 8 4
@0x0 / B0
@0x1 / B1
@0x2 / B2
@0x3 / B3
1: READ B0[7:0] @0x0 then WRITE B0[7:0] @0x0
2: READ B1[7:0] @0x1 then WRITE B1[7:0] @0x1
3: READ B2[7:0] @0x2 then WRITE B2[7:0] @0x2
4: READ B3[7:0] @0x3 then WRITE B3[7:0] @0x3
@0x0 / B0
@0x1 / B1
@0x2 / B2
@0x3 / B3
8 16 4
@0x0 / B0
@0x1 / B1
@0x2 / B2
@0x3 / B3
1: READ B0[7:0] @0x0 then WRITE 00B0[15:0] @0x0
2: READ B1[7:0] @0x1 then WRITE 00B1[15:0] @0x2
3: READ B3[7:0] @0x2 then WRITE 00B2[15:0] @0x4
4: READ B4[7:0] @0x3 then WRITE 00B3[15:0] @0x6
@0x0 / 00B0
@0x2 / 00B1
@0x4 / 00B2
@0x6 / 00B3
8 32 4
@0x0 / B0
@0x1 / B1
@0x2 / B2
@0x3 / B3
1: READ B0[7:0] @0x0 then WRITE 000000B0[31:0] @0x0
2: READ B1[7:0] @0x1 then WRITE 000000B1[31:0] @0x4
3: READ B3[7:0] @0x2 then WRITE 000000B2[31:0] @0x8
4: READ B4[7:0] @0x3 then WRITE 000000B3[31:0] @0xC
@0x0 / 000000B0
@0x4 / 000000B1
@0x8 / 000000B2
@0xC / 000000B3
16 8 4
@0x0 / B1B0
@0x2 / B3B2
@0x4 / B5B4
@0x6 / B7B6
1: READ B1B0[15:0] @0x0 then WRITE B0[7:0] @0x0
2: READ B3B2[15:0] @0x2 then WRITE B2[7:0] @0x1
3: READ B5B4[15:0] @0x4 then WRITE B4[7:0] @0x2
4: READ B7B6[15:0] @0x6 then WRITE B6[7:0] @0x3
@0x0 / B0
@0x1 / B2
@0x2 / B4
@0x3 / B6
16 16 4
@0x0 / B1B0
@0x2 / B3B2
@0x4 / B5B4
@0x6 / B7B6
1: READ B1B0[15:0] @0x0 then WRITE B1B0[15:0] @0x0
2: READ B3B2[15:0] @0x2 then WRITE B3B2[15:0] @0x2
3: READ B5B4[15:0] @0x4 then WRITE B5B4[15:0] @0x4
4: READ B7B6[15:0] @0x6 then WRITE B7B6[15:0] @0x6
@0x0 / B1B0
@0x2 / B3B2
@0x4 / B5B4
@0x6 / B7B6
16 32 4
@0x0 / B1B0
@0x2 / B3B2
@0x4 / B5B4
@0x6 / B7B6
1: READ B1B0[15:0] @0x0 then WRITE 0000B1B0[31:0] @0x0
2: READ B3B2[15:0] @0x2 then WRITE 0000B3B2[31:0] @0x4
3: READ B5B4[15:0] @0x4 then WRITE 0000B5B4[31:0] @0x8
4: READ B7B6[15:0] @0x6 then WRITE 0000B7B6[31:0] @0xC
@0x0 / 0000B1B0
@0x4 / 0000B3B2
@0x8 / 0000B5B4
@0xC / 0000B7B6
32 8 4
@0x0 / B3B2B1B0
@0x4 / B7B6B5B4
@0x8 / BBBAB9B8
@0xC / BFBEBDBC
1: READ B3B2B1B0[31:0] @0x0 then WRITE B0[7:0] @0x0
2: READ B7B6B5B4[31:0] @0x4 then WRITE B4[7:0] @0x1
3: READ BBBAB9B8[31:0] @0x8 then WRITE B8[7:0] @0x2
4: READ BFBEBDBC[31:0] @0xC then WRITE BC[7:0] @0x3
@0x0 / B0
@0x1 / B4
@0x2 / B8
@0x3 / BC
32 16 4
@0x0 / B3B2B1B0
@0x4 / B7B6B5B4
@0x8 / BBBAB9B8
@0xC / BFBEBDBC
1: READ B3B2B1B0[31:0] @0x0 then WRITE B1B0[7:0] @0x0
2: READ B7B6B5B4[31:0] @0x4 then WRITE B5B4[7:0] @0x1
3: READ BBBAB9B8[31:0] @0x8 then WRITE B9B8[7:0] @0x2
4: READ BFBEBDBC[31:0] @0xC then WRITE BDBC[7:0] @0x3
@0x0 / B1B0
@0x2 / B5B4
@0x4 / B9B8
@0x6 / BDBC
32 32 4
@0x0 / B3B2B1B0
@0x4 / B7B6B5B4
@0x8 / BBBAB9B8
@0xC / BFBEBDBC
1: READ B3B2B1B0[31:0] @0x0 then WRITE B3B2B1B0[31:0] @0x0
2: READ B7B6B5B4[31:0] @0x4 then WRITE B7B6B5B4[31:0] @0x4
3: READ BBBAB9B8[31:0] @0x8 then WRITE BBBAB9B8[31:0] @0x8
4: READ BFBEBDBC[31:0] @0xC then WRITE BFBEBDBC[31:0] @0xC
@0x0 / B3B2B1B0
@0x4 / B7B6B5B4
@0x8 / BBBAB9B8
@0xC / BFBEBDBC
RM0008 Direct memory access controller (DMA)
Doc ID 13902 Rev 13 271/1093
and does not generate any error, the DMA writes the 32 HWDATA bits as shown in the two
examples below:
To write the halfword 0xABCD, the DMA sets the HWDATA bus to 0xABCDABCD
with HSIZE = HalfWord
To write the byte 0xAB, the DMA sets the HWDATA bus to 0xABABABAB with
HSIZE = Byte
Assuming that the AHB/APB bridge is an AHB 32-bit slave peripheral that does not take the
HSIZE data into account, it will transform any AHB byte or halfword operation into a 32-bit
APB operation in the following manner:
an AHB byte write operation of the data 0xB0 to 0x0 (or to 0x1, 0x2 or 0x3) will be
converted to an APB word write operation of the data 0xB0B0B0B0 to 0x0
an AHB halfword write operation of the data 0xB1B0 to 0x0 (or to 0x2) will be
converted to an APB word write operation of the data 0xB1B0B1B0 to 0x0
For instance, if you want to write the APB backup registers (16-bit registers aligned to a 32-
bit address boundary), you must configure the memory source size (MSIZE) to 16-bit and
the peripheral destination size (PSIZE) to 32-bit.
13.3.5 Error management
A DMA transfer error can be generated by reading from or writing to a reserved address
space. When a DMA transfer error occurs during a DMA read or a write access, the faulty
channel is automatically disabled through a hardware clear of its EN bit in the corresponding
Channel configuration register (DMA_CCRx). The channel's transfer error interrupt flag
(TEIF) in the DMA_IFR register is set and an interrupt is generated if the transfer error
interrupt enable bit (TEIE) in the DMA_CCRx register is set.
13.3.6 Interrupts
An interrupt can be produced on a Half-transfer, Transfer complete or Transfer error for each
DMA channel. Separate interrupt enable bits are available for flexibility.
Note: In high-density and XL-density devices, DMA2 Channel4 and DMA2 Channel5 interrupts are
mapped onto the same interrupt vector. In connectivity line devices, DMA2 Channel4 and
DMA2 Channel5 interrupts have separate interrupt vectors. All other DMA1 and DMA2
Channel interrupts have their own interrupt vector.
Table 77. DMA interrupt requests
Interrupt event Event flag Enable Control bit
Half-transfer HTIF HTIE
Transfer complete TCIF TCIE
Transfer error TEIF TEIE
Direct memory access controller (DMA) RM0008
272/1093 Doc ID 13902 Rev 13
13.3.7 DMA request mapping
DMA1 controller
The 7 requests from the peripherals (TIMx[1,2,3,4], ADC1, SPI1, SPI/I2S2, I2Cx[1,2] and
USARTx[1,2,3]) are simply logically ORed before entering the DMA1, this means that only
one request must be enabled at a time. Refer to Figure 50: DMA1 request mapping.
The peripheral DMA requests can be independently activated/de-activated by programming
the DMA control bit in the registers of the corresponding peripheral.
Figure 50. DMA1 request mapping
Fixed hardware priority
Channel 3
internal
HW request 3
High priority
Low priority
Peripheral
Channel 2
HW request 2
Channel 1
SW trigger (MEM2MEM bit)
Channel 1 EN bit
HW request 1
Channel 4
HW request 4
DMA1
Channel 5
HW request 5
Channel 6
HW REQUEST 6
Channel 7
HW request 7
request
ADC1
USART1_TX
TIM1_CH4
SPI1_TX
USART3_TX
USART1_RX
TIM1_UP
I2C1_TX
TIM3_CH1
I2C1_RX
TIM2_CH2
SPI1_RX
TIM1_CH2
TIM4_CH3
TIM2_CH1
SPI/I2S2_TX
I2C2_RX
USART2_RX
TIM3_TRIG
TIM1_CH3
USART2_TX
TIM2_CH4
TIM4_UP
SPI/I2S2_RX
I2C2_TX
TIM1_TRIG
TIM4_CH2
TIM3_CH4
TIM3_UP
USART3_RX
TIM3_CH3
TIM1_CH1
TIM2_UP
TIM2_CH3
TIM4_CH1
Channel 2 EN bit
Channel 3 EN bit
Channel 4 EN bit
Channel 5 EN bit
Channel 6 EN bit
Channel 7 EN bit
SW trigger (MEM2MEM bit)
SW trigger (MEM2MEM bit)
SW trigger (MEM2MEM bit)
SW trigger (MEM2MEM bit)
SW TRIGGER (MEM2MEM bit)
SW trigger (MEM2MEM bit)
request signals
TIM1_COM
RM0008 Direct memory access controller (DMA)
Doc ID 13902 Rev 13 273/1093
Table 78 lists the DMA requests for each channel.
DMA2 controller
The 5 requests from the peripherals (TIMx[5,6,7,8], ADC3, SPI/I2S3, UART4,
DAC_Channel[1,2] and SDIO) are simply logically ORed before entering the DMA2, this
means that only one request must be enabled at a time. Refer to Figure 51: DMA2 request
mapping.
The peripheral DMA requests can be independently activated/de-activated by programming
the DMA control bit in the registers of the corresponding peripheral.
Note: The DMA2 controller and its relative requests are available only in high-density, XL-density
and connectivity line devices.
Table 78. Summary of DMA1 requests for each channel
Peripherals Channel 1 Channel 2 Channel 3 Channel 4 Channel 5 Channel 6 Channel 7
ADC1 ADC1
SPI/I
2
S SPI1_RX SPI1_TX
SPI2/I2S2_R
X
SPI2/I2S2_T
X
USART USART3_TX USART3_RX USART1_TX USART1_RX USART2_RX USART2_TX
I
2
C I2C2_TX I2C2_RX I2C1_TX I2C1_RX
TIM1 TIM1_CH1 TIM1_CH2
TIM1_CH4
TIM1_TRIG
TIM1_COM
TIM1_UP TIM1_CH3
TIM2 TIM2_CH3 TIM2_UP TIM2_CH1
TIM2_CH2
TIM2_CH4
TIM3 TIM3_CH3
TIM3_CH4
TIM3_UP
TIM3_CH1
TIM3_TRIG
TIM4 TIM4_CH1 TIM4_CH2 TIM4_CH3 TIM4_UP
Direct memory access controller (DMA) RM0008
274/1093 Doc ID 13902 Rev 13
Figure 51. DMA2 request mapping
Table 79 lists the DMA2 requests for each channel.
13.4 DMA registers
Refer to Section 2.1 on page 46 for a list of abbreviations used in register descriptions.
Table 79. Summary of DMA2 requests for each channel
Peripherals Channel 1 Channel 2 Channel 3 Channel 4 Channel 5
ADC3
(1)
1. ADC3, SDIO and TIM8 DMA requests are available only in high-density and XL-density devices.
ADC3
SPI/I2S3 SPI/I2S3_RX SPI/I2S3_TX
UART4 UART4_RX UART4_TX
SDIO
(1)
SDIO
TIM5
TIM5_CH4
TIM5_TRIG
TIM5_CH3
TIM5_UP
TIM5_CH2 TIM5_CH1
TIM6/
DAC_Channel1
TIM6_UP/
DAC_Channel1
AES AES_OUT AES_IN
Fixed hardware priority
Channel 3
internal
HW request 3
HIGH PRIORITY
LOW PRIORITY
Peripheral request signals
Channel 2
HW request 2
Channel 1
SW trigger (MEM2MEM bit)
Channel 1 EN bit
HW request 1
Channel 4
HW request 4
DMA2
Channel 5
HW request 5
request
TIM5_CH2
SDIO
TIM5_CH4
TIM8_UP
TIM7_UP/DAC_Channel2
TIM8_CH3
TIM5_TRIG
Channel 2 EN bit
Channel 3 EN bit
Channel 4 EN bit
Channel 5 EN bit
SW trigger (MEM2MEM bit)
SW trigger (MEM2MEM bit)
SW trigger (MEM2MEM bit)
SW trigger (MEM2MEM bit)
SPI/I2S3_RX
TIM8_CH4
TIM5_UP
TIM5_CH3
TIM8_TRIG
TIM8_COM
SPI/I2S3_TX
TIM8_CH1
UART4_RX
TIM6_UP/DAC_Channel1
ADC3
UART4_TX
TIM8_CH2
TIM5_CH1
RM0008 Direct memory access controller (DMA)
Doc ID 13902 Rev 13 275/1093
Note: In the following registers, all bits related to channel6 and channel7 are not relevant for DMA2
since it has only 5 channels.
The peripheral registers can be accessed by bytes (8-bit), half-words (16-bit) or words (32-
bit).
13.4.1 DMA interrupt status register (DMA_ISR)
Address offset: 0x00
Reset value: 0x0000 0000
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
TEIF7 HTIF7 TCIF7 GIF7 TEIF6 HTIF6 TCIF6 GIF6 TEIF5 HTIF5 TCIF5 GIF5
r r r r r r r r r r r r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
TEIF4 HTIF4 TCIF4 GIF4 TEIF3 HTIF3 TCIF3 GIF3 TEIF2 HTIF2 TCIF2 GIF2 TEIF1 HTIF1 TCIF1 GIF1
r r r r r r r r r r r r r r r r
Bits 31:28 Reserved, must be kept at reset value.
Bits 27, 23, 19, 15,
11, 7, 3
TEIFx: Channel x transfer error flag (x = 1 ..7)
This bit is set by hardware. It is cleared by software writing 1 to the corresponding bit in the
DMA_IFCR register.
0: No transfer error (TE) on channel x
1: A transfer error (TE) occurred on channel x
Bits 26, 22, 18, 14,
10, 6, 2
HTIFx: Channel x half transfer flag (x = 1 ..7)
This bit is set by hardware. It is cleared by software writing 1 to the corresponding bit in the
DMA_IFCR register.
0: No half transfer (HT) event on channel x
1: A half transfer (HT) event occurred on channel x
Bits 25, 21, 17, 13,
9, 5, 1
TCIFx: Channel x transfer complete flag (x = 1 ..7)
This bit is set by hardware. It is cleared by software writing 1 to the corresponding bit in the
DMA_IFCR register.
0: No transfer complete (TC) event on channel x
1: A transfer complete (TC) event occurred on channel x
Bits 24, 20, 16, 12,
8, 4, 0
GIFx: Channel x global interrupt flag (x = 1 ..7)
This bit is set by hardware. It is cleared by software writing 1 to the corresponding bit in the
DMA_IFCR register.
0: No TE, HT or TC event on channel x
1: A TE, HT or TC event occurred on channel x
Direct memory access controller (DMA) RM0008
276/1093 Doc ID 13902 Rev 13
13.4.2 DMA interrupt flag clear register (DMA_IFCR)
Address offset: 0x04
Reset value: 0x0000 0000
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
CTEIF7 CHTIF7 CTCIF7 CGIF7 CTEIF6 CHTIF6 CTCIF6 CGIF6 CTEIF5 CHTIF5 CTCIF5 CGIF5
w w w w w w w w w w w w
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CTEIF4 CHTIF4 CTCIF4 CGIF4 CTEIF3 CHTIF3 CTCIF3 CGIF3 CTEIF2 CHTIF2 CTCIF2 CGIF2 CTEIF1 CHTIF1 CTCIF1 CGIF1
w w w w w w w w w w w w w w w w
Bits 31:28 Reserved, must be kept at reset value.
Bits 27, 23, 19, 15,
11, 7, 3
CTEIFx: Channel x transfer error clear (x = 1 ..7)
This bit is set and cleared by software.
0: No effect
1: Clears the corresponding TEIF flag in the DMA_ISR register
Bits 26, 22, 18, 14,
10, 6, 2
CHTIFx: Channel x half transfer clear (x = 1 ..7)
This bit is set and cleared by software.
0: No effect
1: Clears the corresponding HTIF flag in the DMA_ISR register
Bits 25, 21, 17, 13,
9, 5, 1
CTCIFx: Channel x transfer complete clear (x = 1 ..7)
This bit is set and cleared by software.
0: No effect
1: Clears the corresponding TCIF flag in the DMA_ISR register
Bits 24, 20, 16, 12,
8, 4, 0
CGIFx: Channel x global interrupt clear (x = 1 ..7)
This bit is set and cleared by software.
0: No effect
1: Clears the GIF, TEIF, HTIF and TCIF flags in the DMA_ISR register
RM0008 Direct memory access controller (DMA)
Doc ID 13902 Rev 13 277/1093
13.4.3 DMA channel x configuration register (DMA_CCRx) (x = 1..7,
where x = channel number)
Address offset: 0x08 + 0d20 (channel number 1)
Reset value: 0x0000 0000
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res.
MEM2
MEM
PL[1:0] MSIZE[1:0] PSIZE[1:0] MINC PINC CIRC DIR TEIE HTIE TCIE EN
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:15 Reserved, must be kept at reset value.
Bit 14 MEM2MEM: Memory to memory mode
This bit is set and cleared by software.
0: Memory to memory mode disabled
1: Memory to memory mode enabled
Bits 13:12 PL[1:0]: Channel priority level
These bits are set and cleared by software.
00: Low
01: Medium
10: High
11: Very high
Bits 11:10 MSIZE[1:0]: Memory size
These bits are set and cleared by software.
00: 8-bits
01: 16-bits
10: 32-bits
11: Reserved
Bits 9:8 PSIZE[1:0]: Peripheral size
These bits are set and cleared by software.
00: 8-bits
01: 16-bits
10: 32-bits
11: Reserved
Bit 7 MINC: Memory increment mode
This bit is set and cleared by software.
0: Memory increment mode disabled
1: Memory increment mode enabled
Bit 6 PINC: Peripheral increment mode
This bit is set and cleared by software.
0: Peripheral increment mode disabled
1: Peripheral increment mode enabled
Bit 5 CIRC: Circular mode
This bit is set and cleared by software.
0: Circular mode disabled
1: Circular mode enabled
Direct memory access controller (DMA) RM0008
278/1093 Doc ID 13902 Rev 13
13.4.4 DMA channel x number of data register (DMA_CNDTRx) (x = 1..7),
where x = channel number)
Address offset: 0x0C + 0d20 (channel number 1)
Reset value: 0x0000 0000
Bit 4 DIR: Data transfer direction
This bit is set and cleared by software.
0: Read from peripheral
1: Read from memory
Bit 3 TEIE: Transfer error interrupt enable
This bit is set and cleared by software.
0: TE interrupt disabled
1: TE interrupt enabled
Bit 2 HTIE: Half transfer interrupt enable
This bit is set and cleared by software.
0: HT interrupt disabled
1: HT interrupt enabled
Bit 1 TCIE: Transfer complete interrupt enable
This bit is set and cleared by software.
0: TC interrupt disabled
1: TC interrupt enabled
Bit 0 EN: Channel enable
This bit is set and cleared by software.
0: Channel disabled
1: Channel enabled
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
NDT
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:16 Reserved, must be kept at reset value.
Bits 15:0 NDT[15:0]: Number of data to transfer
Number of data to be transferred (0 up to 65535). This register can only be written when the
channel is disabled. Once the channel is enabled, this register is read-only, indicating the
remaining bytes to be transmitted. This register decrements after each DMA transfer.
Once the transfer is completed, this register can either stay at zero or be reloaded
automatically by the value previously programmed if the channel is configured in auto-reload
mode.
If this register is zero, no transaction can be served whether the channel is enabled or not.
RM0008 Direct memory access controller (DMA)
Doc ID 13902 Rev 13 279/1093
13.4.5 DMA channel x peripheral address register (DMA_CPARx) (x = 1..7),
where x = channel number)
Address offset: 0x10 + 0d20 (channel number 1)
Reset value: 0x0000 0000
This register must not be written when the channel is enabled.
13.4.6 DMA channel x memory address register (DMA_CMARx) (x = 1..7),
where x = channel number)
Address offset: 0x14 + 0d20 (channel number 1)
Reset value: 0x0000 0000
This register must not be written when the channel is enabled.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
PA
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:0 PA[31:0]: Peripheral address
Base address of the peripheral data register from/to which the data will be read/written.
When PSIZE is 01 (16-bit), the PA[0] bit is ignored. Access is automatically aligned to a half-
word address.
When PSIZE is 10 (32-bit), PA[1:0] are ignored. Access is automatically aligned to a word
address.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
MA
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:0 MA[31:0]: Memory address
Base address of the memory area from/to which the data will be read/written.
When MSIZE is 01 (16-bit), the MA[0] bit is ignored. Access is automatically aligned to a
half-word address.
When MSIZE is 10 (32-bit), MA[1:0] are ignored. Access is automatically aligned to a word
address.
Direct memory access controller (DMA) RM0008
280/1093 Doc ID 13902 Rev 13
13.4.7 DMA register map
The following table gives the DMA register map and the reset values.
Table 80. DMA register map and reset values
Offset Register
3
1
3
0
2
9
2
8
2
7
2
6
2
5
2
4
2
3
2
2
2
1
2
0
1
9
1
8
1
7
1
6
1
5
1
4
1
3
1
2
1
1
1
0
9 8 7 6 5 4 3 2 1 0
0x000
DMA_ISR
Reserved
T
E
I
F
7
H
T
I
F
7
T
C
I
F
7
G
I
F
7
T
E
I
F
6
H
T
I
F
6
T
C
I
F
6
G
I
F
6
T
E
I
F
5
H
T
I
F
5
T
C
I
F
5
G
I
F
5
T
E
I
F
4
H
T
I
F
4
T
C
I
F
4
G
I
F
4
T
E
I
F
3
H
T
I
F
3
T
C
I
F
3
G
I
F
3
T
E
I
F
2
H
T
I
F
2
T
C
I
F
2
G
I
F
2
T
E
I
F
1
H
T
I
F
1
T
C
I
F
1
G
I
F
1
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x004
DMA_IFCR
Reserved
C
T
E
I
F
7
C
H
T
I
F
7
C
T
C
I
F
7
C
G
I
F
7
C
T
E
I
F
6
C
H
T
I
F
6
C
T
C
I
F
6
C
G
I
F
6
C
T
E
I
F
5
C
H
T
I
F
5
C
T
C
I
F
5
C
G
I
F
5
C
T
E
I
F
4
C
H
T
I
F
4
C
T
C
I
F
4
C
G
I
F
4
C
T
E
I
F
3
C
H
T
I
F
3
C
T
C
I
F
3
C
G
I
F
3
C
T
E
I
F
2
C
H
T
I
F
2
C
T
C
I
F
2
C
G
I
F
2
C
T
E
I
F
1
C
H
T
I
F
1
C
T
C
I
F
1
C
G
I
F
1
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x008
DMA_CCR1
Reserved
M
E
M
2
M
E
M
PL
[1:0]
M
S
I
Z
E
[
1
:
0
]
P
S
I
Z
E
[
1
:
0
]
M
I
N
C
P
I
N
C
C
I
R
C
D
I
R
T
E
I
E
H
T
I
E
T
C
I
E
E
N
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x00C
DMA_CNDTR1
Reserved
NDT[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x010
DMA_CPAR1 PA[31:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x014
DMA_CMAR1 MA[31:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x018 Reserved
0x01C
DMA_CCR2
Reserved
M
E
M
2
M
E
M
PL
[1:0]
M
S
I
Z
E
[
1
:
0
]
P
S
I
Z
E
[
1
:
0
]
M
I
N
C
P
I
N
C
C
I
R
C
D
I
R
T
E
I
E
H
T
I
E
T
C
I
E
E
N
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x020
DMA_CNDTR2
Reserved
NDT[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x024
DMA_CPAR2 PA[31:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x028
DMA_CMAR2 MA[31:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x02C Reserved
0x030
DMA_CCR3
Reserved
M
E
M
2
M
E
M
PL
[1:0]
M
S
I
Z
E
[
1
:
0
]
P
S
I
Z
E
[
1
:
0
]
M
I
N
C
P
I
N
C
C
I
R
C
D
I
R
T
E
I
E
H
T
I
E
T
C
I
E
E
N
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x034
DMA_CNDTR3
Reserved
NDT[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x038
DMA_CPAR3 PA[31:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x03C
DMA_CMAR3 MA[31:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x040 Reserved
0x044
DMA_CCR4
Reserved
M
E
M
2
M
E
M
PL
[1:0]
M
S
I
Z
E
[
1
:
0
]
P
S
I
Z
E
[
1
:
0
]
M
I
N
C
P
I
N
C
C
I
R
C
D
I
R
T
E
I
E
H
T
I
E
T
C
I
E
E
N
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x048
DMA_CNDTR4
Reserved
NDT[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
RM0008 Direct memory access controller (DMA)
Doc ID 13902 Rev 13 281/1093
Refer to for the register boundary addresses.
0x04C
DMA_CPAR4 PA[31:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x050
DMA_CMAR4 MA[31:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x054 Reserved
0x058
DMA_CCR5
Reserved
M
E
M
2
M
E
M
PL
[1:0]
M
S
I
Z
E
[
1
:
0
]
P
S
I
Z
E
[
1
:
0
]
M
I
N
C
P
I
N
C
C
I
R
C
D
I
R
T
E
I
E
H
T
I
E
T
C
I
E
E
N
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x05C
DMA_CNDTR5
Reserved
NDT[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x060
DMA_CPAR5 PA[31:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x064
DMA_CMAR5 MA[31:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x068 Reserved
0x06C
DMA_CCR6
Reserved
M
E
M
2
M
E
M
PL
[1:0]
M
S
I
Z
E
[
1
:
0
]
P
S
I
Z
E
[
1
:
0
]
M
I
N
C
P
I
N
C
C
I
R
C
D
I
R
T
E
I
E
H
T
I
E
T
C
I
E
E
N
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x070
DMA_CNDTR6
Reserved
NDT[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x074
DMA_CPAR6 PA[31:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x078
DMA_CMAR6 MA[31:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x07C Reserved
0x080
DMA_CCR7
Reserved
M
E
M
2
M
E
M
PL
[1:0]
M
S
I
Z
E
[
1
:
0
]
P
S
I
Z
E
[
1
:
0
]
M
I
N
C
P
I
N
C
C
I
R
C
D
I
R
T
E
I
E
H
T
I
E
T
C
I
E
E
N
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x084
DMA_CNDTR7
Reserved
NDT[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x088
DMA_CPAR7 PA[31:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x08C
DMA_CMAR7 MA[31:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x090 Reserved
Table 80. DMA register map and reset values (continued)
Offset Register
3
1
3
0
2
9
2
8
2
7
2
6
2
5
2
4
2
3
2
2
2
1
2
0
1
9
1
8
1
7
1
6
1
5
1
4
1
3
1
2
1
1
1
0
9 8 7 6 5 4 3 2 1 0
Advanced-control timers (TIM1&TIM8) RM0008
282/1093 Doc ID 13902 Rev 13
14 Advanced-control timers (TIM1&TIM8)
Low-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 16 and 32 Kbytes.
Medium-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 64 and 128 Kbytes.
High-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 256 and 512 Kbytes.
XL-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 768 Kbytes and 1 Mbyte.
Connectivity line devices are STM32F105xx and STM32F107xx microcontrollers.
Low- and medium-density STM32F103xx devices, and the STM32F105xx/STM32F107xx
connectivity line devices, contain one advanced-control timer (TIM1) whereas high-density
and XL-density STM32F103xx devices feature two advance-control timers (TIM1 and TIM8).
14.1 TIM1&TIM8 introduction
The advanced-control timers (TIM1&TIM8) consist of a 16-bit auto-reload counter driven by
a programmable prescaler.
It may be used for a variety of purposes, including measuring the pulse lengths of input
signals (input capture) or generating output waveforms (output compare, PWM,
complementary PWM with dead-time insertion).
Pulse lengths and waveform periods can be modulated from a few microseconds to several
milliseconds using the timer prescaler and the RCC clock controller prescalers.
The advanced-control (TIM1&TIM8) and general-purpose (TIMx) timers are completely
independent, and do not share any resources. They can be synchronized together as
described in Section 14.3.20.
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 283/1093
14.2 TIM1&TIM8 main features
TIM1&TIM8 timer features include:
16-bit up, down, up/down auto-reload counter.
16-bit programmable prescaler allowing dividing (also on the fly) the counter clock
frequency either by any factor between 1 and 65535.
Up to 4 independent channels for:
Input Capture
Output Compare
PWM generation (Edge and Center-aligned Mode)
One-pulse mode output
Complementary outputs with programmable dead-time
Synchronization circuit to control the timer with external signals and to interconnect
several timers together.
Repetition counter to update the timer registers only after a given number of cycles of
the counter.
Break input to put the timers output signals in reset state or in a known state.
Interrupt/DMA generation on the following events:
Update: counter overflow/underflow, counter initialization (by software or
internal/external trigger)
Trigger event (counter start, stop, initialization or count by internal/external trigger)
Input capture
Output compare
Break input
Supports incremental (quadrature) encoder and hall-sensor circuitry for positioning
purposes
Trigger input for external clock or cycle-by-cycle current management
Advanced-control timers (TIM1&TIM8) RM0008
284/1093 Doc ID 13902 Rev 13
Figure 52. Advanced-control timer block diagram
Prescaler
AutoReload Register
COUNTER
Capture/Compare 1 Register
Capture/Compare 2 Register
U
U
U
CC1I
CC2I
ETR
Trigger
Controller
+/-
Stop, Clear or Up/Down
TI1FP1
TI2FP2
ITR0
ITR1
ITR2
TRGI
Controller
Encoder
Interface
Capture/Compare 3 Register
U
CC3I
output
control
DTG
DTG registers
TRGO
OC1REF
OC2REF
OC3REF
REP Register
U
Repetition
counter
UI
Reset, Enable, Up/Down, Count
Capture/Compare 4 Register
U
CC4I
OC4REF
CK_PSC
TI4
Prescaler
Prescaler
IC4PS
IC3PS
IC1
IC2
Prescaler
Prescaler
Input Filter &
Edge detector
IC2PS
IC1PS
TI1FP1
output
control
DTG
output
control
DTG
output
control
Reg
event
Notes:
Preload registers transferred
to active registers on U event
according to control bit
interrupt & DMA output
Input Filter
Polarity Selection & Edge
Detector & Prescaler
ETRP
TGI
TRC
TRC
IC3
IC4
ITR
ETRF
TRC
TI1F_ED
Input Filter &
Edge detector
Input Filter &
Edge detector
Input Filter &
Edge detector
CC1I
CC2I
CC3I
CC4I
TI1FP2
TI2FP1
TI2FP2
TI3FP3
TRC
TRC
TI3FP4
TI4FP3
TI4FP4
BI
TI3
TI1
TI2
XOR
TIMx_CH1
TIMx_CH2
TIMx_CH3
TIMx_CH4
BRK
TIMx_BKIN
OC1
OC2
OC3
TIMx_CH1
TIMx_CH2
TIMx_CH3
TIMx_CH3N
OC3N
TIMx_CH2N
OC2N
TIMx_CH1N
OC1N
OC4
TIMx_CH4
TIMx_ETR
to other timers
Mode
Slave
PSC CNT
Internal Clock (CK_INT)
CK_CNT
ETRF
Clock failure event from clock controller
Polarity Selection
CSS (Clock Security system
CK_TIM18 from RCC
to DAC/ADC
ITR3
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 285/1093
14.3 TIM1&TIM8 functional description
14.3.1 Time-base unit
The main block of the programmable advanced-control timer is a 16-bit counter with its
related auto-reload register. The counter can count up, down or both up and down. The
counter clock can be divided by a prescaler.
The counter, the auto-reload register and the prescaler register can be written or read by
software. This is true even when the counter is running.
The time-base unit includes:
Counter register (TIMx_CNT)
Prescaler register (TIMx_PSC)
Auto-reload register (TIMx_ARR)
Repetition counter register (TIMx_RCR)
The auto-reload register is preloaded. Writing to or reading from the auto-reload register
accesses the preload register. The content of the preload register are transferred into the
shadow register permanently or at each update event (UEV), depending on the auto-reload
preload enable bit (ARPE) in TIMx_CR1 register. The update event is sent when the counter
reaches the overflow (or underflow when downcounting) and if the UDIS bit equals 0 in the
TIMx_CR1 register. It can also be generated by software. The generation of the update
event is described in detailed for each configuration.
The counter is clocked by the prescaler output CK_CNT, which is enabled only when the
counter enable bit (CEN) in TIMx_CR1 register is set (refer also to the slave mode controller
description to get more details on counter enabling).
Note that the counter starts counting 1 clock cycle after setting the CEN bit in the TIMx_CR1
register.
Prescaler description
The prescaler can divide the counter clock frequency by any factor between 1 and 65536. It
is based on a 16-bit counter controlled through a 16-bit register (in the TIMx_PSC register).
It can be changed on the fly as this control register is buffered. The new prescaler ratio is
taken into account at the next update event.
Figure 54 and Figure 55 give some examples of the counter behavior when the prescaler
ratio is changed on the fly:
Advanced-control timers (TIM1&TIM8) RM0008
286/1093 Doc ID 13902 Rev 13
Figure 53. Counter timing diagram with prescaler division change from 1 to 2
Figure 54. Counter timing diagram with prescaler division change from 1 to 4
14.3.2 Counter modes
Upcounting mode
In upcounting mode, the counter counts from 0 to the auto-reload value (content of the
TIMx_ARR register), then restarts from 0 and generates a counter overflow event.
If the repetition counter is used, the update event (UEV) is generated after upcounting is
repeated for the number of times programmed in the repetition counter register
(TIMx_RCR). Else the update event is generated at each counter overflow.
Setting the UG bit in the TIMx_EGR register (by software or by using the slave mode
controller) also generates an update event.
The UEV event can be disabled by software by setting the UDIS bit in the TIMx_CR1
register. This is to avoid updating the shadow registers while writing new values in the
CK_PSC
00
CEN
Timer clock = CK_CNT
Counter register
Update event (UEV)
0
F9 FA FB FC F7
Prescaler control register 0 1
Write a new value in TIMx_PSC
01 02 03
Prescaler buffer 0 1
Prescaler counter 0 1 0 1 0 1 0 1
F8
CK_PSC
00
CEN
Timer clock = CK_CNT
Counter register
Update event (UEV)
0
F9 FA FB FC F7
Prescaler control register 0 3
Write a new value in TIMx_PSC
Prescaler buffer 0 3
Prescaler counter 0 1 2 3 0 1 2 3
F8 01
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 287/1093
preload registers. Then no update event occurs until the UDIS bit has been written to 0.
However, the counter restarts from 0, as well as the counter of the prescaler (but the
prescale rate does not change). In addition, if the URS bit (update request selection) in
TIMx_CR1 register is set, setting the UG bit generates an update event UEV but without
setting the UIF flag (thus no interrupt or DMA request is sent). This is to avoid generating
both update and capture interrupts when clearing the counter on the capture event.
When an update event occurs, all the registers are updated and the update flag (UIF bit in
TIMx_SR register) is set (depending on the URS bit):
The repetition counter is reloaded with the content of TIMx_RCR register,
The auto-reload shadow register is updated with the preload value (TIMx_ARR),
The buffer of the prescaler is reloaded with the preload value (content of the TIMx_PSC
register).
The following figures show some examples of the counter behavior for different clock
frequencies when TIMx_ARR=0x36.
Figure 55. Counter timing diagram, internal clock divided by 1
Figure 56. Counter timing diagram, internal clock divided by 2
CK_PSC
00
CNT_EN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
Counter overflow
Update event (UEV)
01 02 03 04 05 06 07 32 33 34 35 36 31
CK_PSC
0035 0000 0001 0002 0003
CNT_EN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
0034 0036
Counter overflow
Update event (UEV)
Advanced-control timers (TIM1&TIM8) RM0008
288/1093 Doc ID 13902 Rev 13
Figure 57. Counter timing diagram, internal clock divided by 4
Figure 58. Counter timing diagram, internal clock divided by N
Figure 59. Counter timing diagram, update event when ARPE=0 (TIMx_ARR not
preloaded)
CK_PSC
0000 0001
CNT_EN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
0035 0036
Counter overflow
Update event (UEV)
Timer clock = CK_CNT
Counter register 00 1F 20
Update interrupt flag (UIF)
Counter overflow
Update event (UEV)
CK_PSC
CK_PSC
00
CEN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
Counter overflow
Update event (UEV)
01 02 03 04 05 06 07 32 33 34 35 36 31
Auto-reload register FF 36
Write a new value in TIMx_ARR
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 289/1093
Figure 60. Counter timing diagram, update event when ARPE=1
(TIMx_ARR preloaded)
Downcounting mode
In downcounting mode, the counter counts from the auto-reload value (content of the
TIMx_ARR register) down to 0, then restarts from the auto-reload value and generates a
counter underflow event.
If the repetition counter is used, the update event (UEV) is generated after downcounting is
repeated for the number of times programmed in the repetition counter register
(TIMx_RCR). Else the update event is generated at each counter underflow.
Setting the UG bit in the TIMx_EGR register (by software or by using the slave mode
controller) also generates an update event.
The UEV update event can be disabled by software by setting the UDIS bit in TIMx_CR1
register. This is to avoid updating the shadow registers while writing new values in the
preload registers. Then no update event occurs until UDIS bit has been written to 0.
However, the counter restarts from the current auto-reload value, whereas the counter of the
prescaler restarts from 0 (but the prescale rate doesnt change).
In addition, if the URS bit (update request selection) in TIMx_CR1 register is set, setting the
UG bit generates an update event UEV but without setting the UIF flag (thus no interrupt or
DMA request is sent). This is to avoid generating both update and capture interrupts when
clearing the counter on the capture event.
When an update event occurs, all the registers are updated and the update flag (UIF bit in
TIMx_SR register) is set (depending on the URS bit):
The repetition counter is reloaded with the content of TIMx_RCR register
The buffer of the prescaler is reloaded with the preload value (content of the TIMx_PSC
register)
The auto-reload active register is updated with the preload value (content of the
TIMx_ARR register). Note that the auto-reload is updated before the counter is
reloaded, so that the next period is the expected one
CK_PSC
00
CEN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
Counter overflow
Update event (UEV)
01 02 03 04 05 06 07 F1 F2 F3 F4 F5 F0
Auto-reload preload register F5 36
Auto-reload shadow register F5 36
Write a new value in TIMx_ARR
Advanced-control timers (TIM1&TIM8) RM0008
290/1093 Doc ID 13902 Rev 13
The following figures show some examples of the counter behavior for different clock
frequencies when TIMx_ARR=0x36.
Figure 61. Counter timing diagram, internal clock divided by 1
Figure 62. Counter timing diagram, internal clock divided by 2
Figure 63. Counter timing diagram, internal clock divided by 4
CK_PSC
36
CNT_EN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
Counter underflow (cnt_udf)
Update event (UEV)
35 34 33 32 31 30 2F 04 03 02 01 00 05
CK_PSC
0001 0036 0035 0034 0033
CNT_EN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
0002 0000
Counter underflow
Update event (UEV)
CK_PSC
0036 0035
CNT_EN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
0001 0000
Counter underflow
Update event (UEV)
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 291/1093
Figure 64. Counter timing diagram, internal clock divided by N
Figure 65. Counter timing diagram, update event when repetition counter
is not used
Center-aligned mode (up/down counting)
In center-aligned mode, the counter counts from 0 to the auto-reload value (content of the
TIMx_ARR register) 1, generates a counter overflow event, then counts from the auto-
reload value down to 1 and generates a counter underflow event. Then it restarts counting
from 0.
Center-aligned mode is active when the CMS bits in TIMx_CR1 register are not equal to
'00'. The Output compare interrupt flag of channels configured in output is set when: the
counter counts down (Center aligned mode 1, CMS = "01"), the counter counts up (Center
aligned mode 2, CMS = "10") the counter counts up and down (Center aligned mode 3,
CMS = "11").
In this mode, the DIR direction bit in the TIMx_CR1 register cannot be written. It is updated
by hardware and gives the current direction of the counter.
The update event can be generated at each counter overflow and at each counter underflow
or by setting the UG bit in the TIMx_EGR register (by software or by using the slave mode
controller) also generates an update event. In this case, the counter restarts counting from
0, as well as the counter of the prescaler.
Timer clock = CK_CNT
Counter register 36 20 1F
Update interrupt flag (UIF)
Counter underflow
Update event (UEV)
CK_PSC
00
CK_PSC
36
CEN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
Counter underflow
Update event (UEV)
35 34 33 32 31 30 2F 04 03 02 01 00 05
Auto-reload register FF 36
Write a new value in TIMx_ARR
Advanced-control timers (TIM1&TIM8) RM0008
292/1093 Doc ID 13902 Rev 13
The UEV update event can be disabled by software by setting the UDIS bit in the TIMx_CR1
register. This is to avoid updating the shadow registers while writing new values in the
preload registers. Then no update event occurs until UDIS bit has been written to 0.
However, the counter continues counting up and down, based on the current auto-reload
value.
In addition, if the URS bit (update request selection) in TIMx_CR1 register is set, setting the
UG bit generates an UEV update event but without setting the UIF flag (thus no interrupt or
DMA request is sent). This is to avoid generating both update and capture interrupts when
clearing the counter on the capture event.
When an update event occurs, all the registers are updated and the update flag (UIF bit in
TIMx_SR register) is set (depending on the URS bit):
The repetition counter is reloaded with the content of TIMx_RCR register
The buffer of the prescaler is reloaded with the preload value (content of the TIMx_PSC
register)
The auto-reload active register is updated with the preload value (content of the
TIMx_ARR register). Note that if the update source is a counter overflow, the auto-
reload is updated before the counter is reloaded, so that the next period is the expected
one (the counter is loaded with the new value).
The following figures show some examples of the counter behavior for different clock
frequencies.
Figure 66. Counter timing diagram, internal clock divided by 1, TIMx_ARR = 0x6
1. Here, center-aligned mode 1 is used (for more details refer to Section 14.4: TIM1&TIM8 registers on page 323).
CK_PSC
02
CNT_EN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
Counter underflow
Update event (UEV)
03 04 05 06 05 04 03 03 02 01 00 01 04
Counter overflow
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 293/1093
Figure 67. Counter timing diagram, internal clock divided by 2
Figure 68. Counter timing diagram, internal clock divided by 4, TIMx_ARR=0x36
1. Center-aligned mode 2 or 3 is used with an UIF on overflow.
Figure 69. Counter timing diagram, internal clock divided by N
CK_PSC
0002 0000 0001 0002 0003
CNT_EN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
0003 0001
Counter underflow
Update event (UEV)
CK_PSC
0036 0035
CNT_EN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
0034 0035
Counter overflow
Update event (UEV)
Timer clock = CK_CNT
Counter register 00 20 1F
Update interrupt flag (UIF)
Counter underflow
Update event (UEV)
CK_PSC
01
Advanced-control timers (TIM1&TIM8) RM0008
294/1093 Doc ID 13902 Rev 13
Figure 70. Counter timing diagram, update event with ARPE=1 (counter underflow)
Figure 71. Counter timing diagram, Update event with ARPE=1 (counter overflow)
14.3.3 Repetition counter
Section 14.3.1: Time-base unit describes how the update event (UEV) is generated with
respect to the counter overflows/underflows. It is actually generated only when the repetition
counter has reached zero. This can be useful when generating PWM signals.
This means that data are transferred from the preload registers to the shadow registers
(TIMx_ARR auto-reload register, TIMx_PSC prescaler register, but also TIMx_CCRx
capture/compare registers in compare mode) every N counter overflows or underflows,
where N is the value in the TIMx_RCR repetition counter register.
CK_PSC
00
CEN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
Counter underflow
Update event (UEV)
01 02 03 04 05 06 07 05 04 03 02 01 06
Auto-reload preload register FD 36
Write a new value in TIMx_ARR
Auto-reload active register FD 36
CK_PSC
36
CEN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
Counter overflow
Update event (UEV)
35 34 33 32 31 30 2F F8 F9 FA FB FC F7
Auto-reload preload register FD 36
Write a new value in TIMx_ARR
Auto-reload active register FD 36
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 295/1093
The repetition counter is decremented:
At each counter overflow in upcounting mode,
At each counter underflow in downcounting mode,
At each counter overflow and at each counter underflow in center-aligned mode.
Although this limits the maximum number of repetition to 128 PWM cycles, it makes it
possible to update the duty cycle twice per PWM period. When refreshing compare
registers only once per PWM period in center-aligned mode, maximum resolution is
2xT
ck
, due to the symmetry of the pattern.
The repetition counter is an auto-reload type; the repetition rate is maintained as defined by
the TIMx_RCR register value (refer to Figure 72). When the update event is generated by
software (by setting the UG bit in TIMx_EGR register) or by hardware through the slave
mode controller, it occurs immediately whatever the value of the repetition counter is and the
repetition counter is reloaded with the content of the TIMx_RCR register.
Figure 72. Update rate examples depending on mode and TIMx_RCR register
settings
Center-aligned mode Edge-aligned mode
UEV Update Event: Preload registers transferred to active registers and update interrupt generated
Counter
TIMx_RCR = 0
TIMx_RCR = 1
TIMx_RCR = 2
TIMx_RCR = 3
Update Event if the repetition counter underflow occurs when the counter is equal to
to the auto-reload value.
UEV
TIMx_RCR = 3
and
re-synchronization
(by SW)
(by SW)
TIMx_CNT
(by SW)
Upcounting Downcounting
UEV
UEV
UEV
UEV
Advanced-control timers (TIM1&TIM8) RM0008
296/1093 Doc ID 13902 Rev 13
14.3.4 Clock selection
The counter clock can be provided by the following clock sources:
Internal clock (CK_INT)
External clock mode1: external input pin
External clock mode2: external trigger input ETR
Internal trigger inputs (ITRx): using one timer as prescaler for another timer, for
example, you can configure Timer 1 to act as a prescaler for Timer 2. Refer to Using
one timer as prescaler for another for more details.
Internal clock source (CK_INT)
If the slave mode controller is disabled (SMS=000), then the CEN, DIR (in the TIMx_CR1
register) and UG bits (in the TIMx_EGR register) are actual control bits and can be changed
only by software (except UG which remains cleared automatically). As soon as the CEN bit
is written to 1, the prescaler is clocked by the internal clock CK_INT.
Figure 73 shows the behavior of the control circuit and the upcounter in normal mode,
without prescaler.
Figure 73. Control circuit in normal mode, internal clock divided by 1
External clock source mode 1
This mode is selected when SMS=111 in the TIMx_SMCR register. The counter can count
at each rising or falling edge on a selected input.
Figure 74. TI2 external clock connection example
Internal clock
00
Counter clock = CK_CNT = CK_PSC
Counter register 01 02 03 04 05 06 07 32 33 34 35 36 31
CEN=CNT_EN
UG
CNT_INIT
CK_INT
encoder
mode
external clock
mode 1
external clock
mode 2
internal clock
mode
ETRF
TRGI
TI1F
TI2F or
or
or
(internal clock)
CK_PSC
ECE
TIMx_SMCR
SMS[2:0]
ITRx
TI1_ED
TI1FP1
TI2FP2
ETRF
TIMx_SMCR
TS[2:0]
TI2
0
1
TIMx_CCER
CC2P
Filter
ICF[3:0]
TIMx_CCMR1
Edge
Detector
TI2F_Rising
TI2F_Falling
110
0xx
100
101
111
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 297/1093
For example, to configure the upcounter to count in response to a rising edge on the TI2
input, use the following procedure:
1. Configure channel 2 to detect rising edges on the TI2 input by writing CC2S = 01 in
the TIMx_CCMR1 register.
2. Configure the input filter duration by writing the IC2F[3:0] bits in the TIMx_CCMR1
register (if no filter is needed, keep IC2F=0000).
3. Select rising edge polarity by writing CC2P=0 in the TIMx_CCER register.
4. Configure the timer in external clock mode 1 by writing SMS=111 in the TIMx_SMCR
register.
5. Select TI2 as the trigger input source by writing TS=110 in the TIMx_SMCR register.
6. Enable the counter by writing CEN=1 in the TIMx_CR1 register.
Note: The capture prescaler is not used for triggering, so you dont need to configure it.
When a rising edge occurs on TI2, the counter counts once and the TIF flag is set.
The delay between the rising edge on TI2 and the actual clock of the counter is due to the
resynchronization circuit on TI2 input.
Figure 75. Control circuit in external clock mode 1
External clock source mode 2
This mode is selected by writing ECE=1 in the TIMx_SMCR register.
The counter can count at each rising or falling edge on the external trigger input ETR.
The Figure 76 gives an overview of the external trigger input block.
Figure 76. External trigger input block
Counter clock = CK_CNT = CK_PSC
Counter register 35 36 34
TI2
CNT_EN
TIF
Write TIF=0
ETR
0
1
TIMx_SMCR
ETP
divider
/1, /2, /4, /8
ETPS[1:0]
ETRP
filter
ETF[3:0]
downcounter
f
DTS
TIMx_SMCR TIMx_SMCR
ETR pin
CK_INT
encoder
mode
external clock
mode 1
external clock
mode 2
internal clock
mode
ETRF
TRGI
TI1F
TI2F or
or
or
(internal clock)
CK_PSC
ECE
TIMx_SMCR
SMS[2:0]
Advanced-control timers (TIM1&TIM8) RM0008
298/1093 Doc ID 13902 Rev 13
For example, to configure the upcounter to count each 2 rising edges on ETR, use the
following procedure:
1. As no filter is needed in this example, write ETF[3:0]=0000 in the TIMx_SMCR register.
2. Set the prescaler by writing ETPS[1:0]=01 in the TIMx_SMCR register
3. Select rising edge detection on the ETR pin by writing ETP=0 in the TIMx_SMCR
register
4. Enable external clock mode 2 by writing ECE=1 in the TIMx_SMCR register.
5. Enable the counter by writing CEN=1 in the TIMx_CR1 register.
The counter counts once each 2 ETR rising edges.
The delay between the rising edge on ETR and the actual clock of the counter is due to the
resynchronization circuit on the ETRP signal.
Figure 77. Control circuit in external clock mode 2
14.3.5 Capture/compare channels
Each Capture/Compare channel is built around a capture/compare register (including a
shadow register), a input stage for capture (with digital filter, multiplexing and prescaler) and
an output stage (with comparator and output control).
Figure 78 to Figure 81 give an overview of one Capture/Compare channel.
The input stage samples the corresponding TIx input to generate a filtered signal TIxF.
Then, an edge detector with polarity selection generates a signal (TIxFPx) which can be
used as trigger input by the slave mode controller or as the capture command. It is
prescaled before the capture register (ICxPS).
Counter clock = CK_CNT = CK_PSC
Counter register 35 36 34
ETR
CNT_EN
f
CK_INT
ETRP
ETRF
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 299/1093
Figure 78. Capture/compare channel (example: channel 1 input stage)
The output stage generates an intermediate waveform which is then used for reference:
OCxRef (active high). The polarity acts at the end of the chain.
Figure 79. Capture/compare channel 1 main circuit
TI1
0
1
TIMx_CCER
CC1P
divider
/1, /2, /4, /8
ICPS[1:0]
TI1F_ED
filter
ICF[3:0]
downcounter
TIMx_CCMR1
Edge
Detector
TI1F_Rising
TI1F_Falling
to the slave mode controller
TI1FP1
11
01
TIMx_CCMR1
CC1S[1:0]
IC1
TI2FP1
TRC
(from channel 2)
(from slave mode
controller)
10
f
DTS
TIMx_CCER
CC1E
IC1PS
TI1F
0
1
TI2F_rising
TI2F_falling
(from channel 2)
CC1E
Capture/compare shadow register
comparator
Capture/compare preload register
Counter
IC1PS
CC1S[0]
CC1S[1]
capture
input
mode
S
R
read CCR1H
read CCR1L
read_in_progress
capture_transfer
CC1S[0]
CC1S[1]
S
R
write CCR1H
write CCR1L
write_in_progress
output
mode
UEV
OC1PE
(from time
compare_transfer
APB Bus
8 8
h
i
g
h
l
o
w
(
i
f
1
6
-
b
i
t
)
MCU-peripheral interface
TIM1_CCMR1
OC1PE
base unit)
CNT>CCR1
CNT=CCR1
TIM1_EGR
CC1G
Advanced-control timers (TIM1&TIM8) RM0008
300/1093 Doc ID 13902 Rev 13
Figure 80. Output stage of capture/compare channel (channel 1 to 3)
Figure 81. Output stage of capture/compare channel (channel 4)
The capture/compare block is made of one preload register and one shadow register. Write
and read always access the preload register.
In capture mode, captures are actually done in the shadow register, which is copied into the
preload register.
In compare mode, the content of the preload register is copied into the shadow register
which is compared to the counter.
14.3.6 Input capture mode
In Input capture mode, the Capture/Compare Registers (TIMx_CCRx) are used to latch the
value of the counter after a transition detected by the corresponding ICx signal. When a
capture occurs, the corresponding CCXIF flag (TIMx_SR register) is set and an interrupt or
a DMA request can be sent if they are enabled. If a capture occurs while the CCxIF flag was
already high, then the over-capture flag CCxOF (TIMx_SR register) is set. CCxIF can be
cleared by software by writing it to 0 or by reading the captured data stored in the
TIMx_CCRx register. CCxOF is cleared when you write it to 0.
Output mode
CNT>CCR1
CNT=CCR1 controller
TIM1_CCMR1
OC1M[2:0]
OC1REF
OC1CE
Dead-time
generator
OC1_DT
OC1N_DT
DTG[7:0]
TIM1_BDTR
0
0
CC1E
TIM1_CCER
CC1NE
0
1
CC1P
TIM1_CCER
0
1
CC1NP
TIM1_CCER
Output
enable
circuit
OC1
Output
enable
circuit
OC1N
CC1E TIM1_CCER CC1NE
OSSI TIM1_BDTR MOE OSSR
0x
10
11
11
10
x0
ETR
Output mode
CNT > CCR4
CNT = CCR4 controller
TIM1_CCMR2
OC2M[2:0]
OC4 REF
0
1
CC4P
TIM1_CCER
Output
enable
circuit
OC4
CC4E TIM1_CCER
OSSI TIM1_BDTR MOE
To the master mode
controller
TIM1_CR2 OIS4
ETR
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 301/1093
The following example shows how to capture the counter value in TIMx_CCR1 when TI1
input rises. To do this, use the following procedure:
Select the active input: TIMx_CCR1 must be linked to the TI1 input, so write the CC1S
bits to 01 in the TIMx_CCMR1 register. As soon as CC1S becomes different from 00,
the channel is configured in input and the TIMx_CCR1 register becomes read-only.
Program the input filter duration you need with respect to the signal you connect to the
timer (when the input is one of the TIx (ICxF bits in the TIMx_CCMRx register). Lets
imagine that, when toggling, the input signal is not stable during at must 5 internal clock
cycles. We must program a filter duration longer than these 5 clock cycles. We can
validate a transition on TI1 when 8 consecutive samples with the new level have been
detected (sampled at f
DTS
frequency). Then write IC1F bits to 0011 in the
TIMx_CCMR1 register.
Select the edge of the active transition on the TI1 channel by writing CC1P bit to 0 in
the TIMx_CCER register (rising edge in this case).
Program the input prescaler. In our example, we wish the capture to be performed at
each valid transition, so the prescaler is disabled (write IC1PS bits to 00 in the
TIMx_CCMR1 register).
Enable capture from the counter into the capture register by setting the CC1E bit in the
TIMx_CCER register.
If needed, enable the related interrupt request by setting the CC1IE bit in the
TIMx_DIER register, and/or the DMA request by setting the CC1DE bit in the
TIMx_DIER register.
When an input capture occurs:
The TIMx_CCR1 register gets the value of the counter on the active transition.
CC1IF flag is set (interrupt flag). CC1OF is also set if at least two consecutive captures
occurred whereas the flag was not cleared.
An interrupt is generated depending on the CC1IE bit.
A DMA request is generated depending on the CC1DE bit.
In order to handle the overcapture, it is recommended to read the data before the
overcapture flag. This is to avoid missing an overcapture which could happen after reading
the flag and before reading the data.
Note: IC interrupt and/or DMA requests can be generated by software by setting the
corresponding CCxG bit in the TIMx_EGR register.
14.3.7 PWM input mode
This mode is a particular case of input capture mode. The procedure is the same except:
Two ICx signals are mapped on the same TIx input.
These 2 ICx signals are active on edges with opposite polarity.
One of the two TIxFP signals is selected as trigger input and the slave mode controller
is configured in reset mode.
Advanced-control timers (TIM1&TIM8) RM0008
302/1093 Doc ID 13902 Rev 13
For example, you can measure the period (in TIMx_CCR1 register) and the duty cycle (in
TIMx_CCR2 register) of the PWM applied on TI1 using the following procedure (depending
on CK_INT frequency and prescaler value):
Select the active input for TIMx_CCR1: write the CC1S bits to 01 in the TIMx_CCMR1
register (TI1 selected).
Select the active polarity for TI1FP1 (used both for capture in TIMx_CCR1 and counter
clear): write the CC1P bit to 0 (active on rising edge).
Select the active input for TIMx_CCR2: write the CC2S bits to 10 in the TIMx_CCMR1
register (TI1 selected).
Select the active polarity for TI1FP2 (used for capture in TIMx_CCR2): write the CC2P
bit to 1 (active on falling edge).
Select the valid trigger input: write the TS bits to 101 in the TIMx_SMCR register
(TI1FP1 selected).
Configure the slave mode controller in reset mode: write the SMS bits to 100 in the
TIMx_SMCR register.
Enable the captures: write the CC1E and CC2E bits to 1 in the TIMx_CCER register.
Figure 82. PWM input mode timing
1. The PWM input mode can be used only with the TIMx_CH1/TIMx_CH2 signals due to the fact that only
TI1FP1 and TI2FP2 are connected to the slave mode controller.
14.3.8 Forced output mode
In output mode (CCxS bits = 00 in the TIMx_CCMRx register), each output compare signal
(OCxREF and then OCx/OCxN) can be forced to active or inactive level directly by software,
independently of any comparison between the output compare register and the counter.
To force an output compare signal (OCXREF/OCx) to its active level, you just need to write
101 in the OCxM bits in the corresponding TIMx_CCMRx register. Thus OCXREF is forced
high (OCxREF is always active high) and OCx get opposite value to CCxP polarity bit.
For example: CCxP=0 (OCx active high) => OCx is forced to high level.
The OCxREF signal can be forced low by writing the OCxM bits to 100 in the TIMx_CCMRx
register.
TI1
TIMx_CNT 0000 0001 0002 0003 0004 0000 0004
TIMx_CCR1
TIMx_CCR2
0004
0002
IC1 capture
IC2 capture
reset counter
IC2 capture
pulse width
IC1 capture
period
measurement measurement
ai15413
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 303/1093
Anyway, the comparison between the TIMx_CCRx shadow register and the counter is still
performed and allows the flag to be set. Interrupt and DMA requests can be sent
accordingly. This is described in the output compare mode section below.
14.3.9 Output compare mode
This function is used to control an output waveform or indicating when a period of time has
elapsed.
When a match is found between the capture/compare register and the counter, the output
compare function:
Assigns the corresponding output pin to a programmable value defined by the output
compare mode (OCxM bits in the TIMx_CCMRx register) and the output polarity (CCxP
bit in the TIMx_CCER register). The output pin can keep its level (OCXM=000), be set
active (OCxM=001), be set inactive (OCxM=010) or can toggle (OCxM=011) on match.
Sets a flag in the interrupt status register (CCxIF bit in the TIMx_SR register).
Generates an interrupt if the corresponding interrupt mask is set (CCXIE bit in the
TIMx_DIER register).
Sends a DMA request if the corresponding enable bit is set (CCxDE bit in the
TIMx_DIER register, CCDS bit in the TIMx_CR2 register for the DMA request
selection).
The TIMx_CCRx registers can be programmed with or without preload registers using the
OCxPE bit in the TIMx_CCMRx register.
In output compare mode, the update event UEV has no effect on OCxREF and OCx output.
The timing resolution is one count of the counter. Output compare mode can also be used to
output a single pulse (in One Pulse mode).
Procedure:
1. Select the counter clock (internal, external, prescaler).
2. Write the desired data in the TIMx_ARR and TIMx_CCRx registers.
3. Set the CCxIE bit if an interrupt request is to be generated.
4. Select the output mode. For example:
Write OCxM = 011 to toggle OCx output pin when CNT matches CCRx
Write OCxPE = 0 to disable preload register
Write CCxP = 0 to select active high polarity
Write CCxE = 1 to enable the output
5. Enable the counter by setting the CEN bit in the TIMx_CR1 register.
The TIMx_CCRx register can be updated at any time by software to control the output
waveform, provided that the preload register is not enabled (OCxPE=0, else TIMx_CCRx
shadow register is updated only at the next update event UEV). An example is given in
Figure 83.
Advanced-control timers (TIM1&TIM8) RM0008
304/1093 Doc ID 13902 Rev 13
Figure 83. Output compare mode, toggle on OC1.
14.3.10 PWM mode
Pulse Width Modulation mode allows you to generate a signal with a frequency determined
by the value of the TIMx_ARR register and a duty cycle determined by the value of the
TIMx_CCRx register.
The PWM mode can be selected independently on each channel (one PWM per OCx
output) by writing 110 (PWM mode 1) or 111 (PWM mode 2) in the OCxM bits in the
TIMx_CCMRx register. You must enable the corresponding preload register by setting the
OCxPE bit in the TIMx_CCMRx register, and eventually the auto-reload preload register (in
upcounting or center-aligned modes) by setting the ARPE bit in the TIMx_CR1 register.
As the preload registers are transferred to the shadow registers only when an update event
occurs, before starting the counter, you have to initialize all the registers by setting the UG
bit in the TIMx_EGR register.
OCx polarity is software programmable using the CCxP bit in the TIMx_CCER register. It
can be programmed as active high or active low. OCx output is enabled by a combination of
the CCxE, CCxNE, MOE, OSSI and OSSR bits (TIMx_CCER and TIMx_BDTR registers).
Refer to the TIMx_CCER register description for more details.
In PWM mode (1 or 2), TIMx_CNT and TIMx_CCRx are always compared to determine
whether TIMx_CCRx TIMx_CNT or TIMx_CNT TIMx_CCRx (depending on the direction
of the counter).
The timer is able to generate PWM in edge-aligned mode or center-aligned mode
depending on the CMS bits in the TIMx_CR1 register.
oc1ref=OC1
TIM1_CNT B200 B201 0039
TIM1_CCR1 003A
Write B201h in the CC1R register
Match detected on CCR1
Interrupt generated if enabled
003B
B201
003A
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 305/1093
PWM edge-aligned mode
Upcounting configuration
Upcounting is active when the DIR bit in the TIMx_CR1 register is low. Refer to the
Upcounting mode on page 286.
In the following example, we consider PWM mode 1. The reference PWM signal
OCxREF is high as long as TIMx_CNT < TIMx_CCRx else it becomes low. If the
compare value in TIMx_CCRx is greater than the auto-reload value (in TIMx_ARR)
then OCxREF is held at 1. If the compare value is 0 then OCxRef is held at 0.
Figure 84 shows some edge-aligned PWM waveforms in an example where
TIMx_ARR=8.
Figure 84. Edge-aligned PWM waveforms (ARR=8)
Downcounting configuration
Downcounting is active when DIR bit in TIMx_CR1 register is high. Refer to the
Downcounting mode on page 289
In PWM mode 1, the reference signal OCxRef is low as long as
TIMx_CNT > TIMx_CCRx else it becomes high. If the compare value in TIMx_CCRx is
greater than the auto-reload value in TIMx_ARR, then OCxREF is held at 1. 0% PWM
is not possible in this mode.
PWM center-aligned mode
Center-aligned mode is active when the CMS bits in TIMx_CR1 register are different from
00 (all the remaining configurations having the same effect on the OCxRef/OCx signals).
The compare flag is set when the counter counts up, when it counts down or both when it
counts up and down depending on the CMS bits configuration. The direction bit (DIR) in the
TIMx_CR1 register is updated by hardware and must not be changed by software. Refer to
the Center-aligned mode (up/down counting) on page 291.
Figure 85 shows some center-aligned PWM waveforms in an example where:
TIMx_ARR=8,
PWM mode is the PWM mode 1,
The flag is set when the counter counts down corresponding to the center-aligned
mode 1 selected for CMS=01 in TIMx_CR1 register.
Counter register
1
0 1 2 3 4 5 6 7 8 0 1
0
OCXREF
CCxIF
OCXREF
CCxIF
OCXREF
CCxIF
OCXREF
CCxIF
CCRx=4
CCRx=8
CCRx>8
CCRx=0
Advanced-control timers (TIM1&TIM8) RM0008
306/1093 Doc ID 13902 Rev 13
Figure 85. Center-aligned PWM waveforms (ARR=8)
Hints on using center-aligned mode:
When starting in center-aligned mode, the current up-down configuration is used. It
means that the counter counts up or down depending on the value written in the DIR bit
in the TIMx_CR1 register. Moreover, the DIR and CMS bits must not be changed at the
same time by the software.
Writing to the counter while running in center-aligned mode is not recommended as it
can lead to unexpected results. In particular:
The direction is not updated if you write a value in the counter that is greater than
the auto-reload value (TIMx_CNT>TIMx_ARR). For example, if the counter was
counting up, it continues to count up.
The direction is updated if you write 0 or write the TIMx_ARR value in the counter
but no Update Event UEV is generated.
The safest way to use center-aligned mode is to generate an update by software
(setting the UG bit in the TIMx_EGR register) just before starting the counter and not to
write the counter while it is running.
CCxF
0 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 0 1 Counter register
CCRx = 4
OCxREF
CMS=01
CMS=10
CMS=11
CCxF
CCRx = 7
OCxREF
CMS=10 or 11
CCxF
CCRx = 8
OCxREF
CMS=01
CMS=10
CMS=11
'1'
CCxF
CCRx > 8
OCxREF
CMS=01
CMS=10
CMS=11
'1'
CCxF
CCRx = 0
OCxREF
CMS=01
CMS=10
CMS=11
'0'
ai14681b
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 307/1093
14.3.11 Complementary outputs and dead-time insertion
The advanced-control timers (TIM1&TIM8) can output two complementary signals and
manage the switching-off and the switching-on instants of the outputs.
This time is generally known as dead-time and you have to adjust it depending on the
devices you have connected to the outputs and their characteristics (intrinsic delays of level-
shifters, delays due to power switches...)
You can select the polarity of the outputs (main output OCx or complementary OCxN)
independently for each output. This is done by writing to the CCxP and CCxNP bits in the
TIMx_CCER register.
The complementary signals OCx and OCxN are activated by a combination of several
control bits: the CCxE and CCxNE bits in the TIMx_CCER register and the MOE, OISx,
OISxN, OSSI and OSSR bits in the TIMx_BDTR and TIMx_CR2 registers. Refer to Table 83:
Output control bits for complementary OCx and OCxN channels with break feature on
page 339 for more details. In particular, the dead-time is activated when switching to the
IDLE state (MOE falling down to 0).
Dead-time insertion is enabled by setting both CCxE and CCxNE bits, and the MOE bit if the
break circuit is present. There is one 10-bit dead-time generator for each channel. From a
reference waveform OCxREF, it generates 2 outputs OCx and OCxN. If OCx and OCxN are
active high:
The OCx output signal is the same as the reference signal except for the rising edge,
which is delayed relative to the reference rising edge.
The OCxN output signal is the opposite of the reference signal except for the rising
edge, which is delayed relative to the reference falling edge.
If the delay is greater than the width of the active output (OCx or OCxN) then the
corresponding pulse is not generated.
The following figures show the relationships between the output signals of the dead-time
generator and the reference signal OCxREF. (we suppose CCxP=0, CCxNP=0, MOE=1,
CCxE=1 and CCxNE=1 in these examples)
Figure 86. Complementary output with dead-time insertion.
Figure 87. Dead-time waveforms with delay greater than the negative pulse.
delay
delay
OCxREF
OCx
OCxN
delay
OCxREF
OCx
OCxN
Advanced-control timers (TIM1&TIM8) RM0008
308/1093 Doc ID 13902 Rev 13
Figure 88. Dead-time waveforms with delay greater than the positive pulse.
The dead-time delay is the same for each of the channels and is programmable with the
DTG bits in the TIMx_BDTR register. Refer to Section 14.4.18: TIM1&TIM8 break and dead-
time register (TIMx_BDTR) on page 343 for delay calculation.
Re-directing OCxREF to OCx or OCxN
In output mode (forced, output compare or PWM), OCxREF can be re-directed to the OCx
output or to OCxN output by configuring the CCxE and CCxNE bits in the TIMx_CCER
register.
This allows you to send a specific waveform (such as PWM or static active level) on one
output while the complementary remains at its inactive level. Other alternative possibilities
are to have both outputs at inactive level or both outputs active and complementary with
dead-time.
Note: When only OCxN is enabled (CCxE=0, CCxNE=1), it is not complemented and becomes
active as soon as OCxREF is high. For example, if CCxNP=0 then OCxN=OCxRef. On the
other hand, when both OCx and OCxN are enabled (CCxE=CCxNE=1) OCx becomes
active when OCxREF is high whereas OCxN is complemented and becomes active when
OCxREF is low.
14.3.12 Using the break function
When using the break function, the output enable signals and inactive levels are modified
according to additional control bits (MOE, OSSI and OSSR bits in the TIMx_BDTR register,
OISx and OISxN bits in the TIMx_CR2 register). In any case, the OCx and OCxN outputs
cannot be set both to active level at a given time. Refer to Table 83: Output control bits for
complementary OCx and OCxN channels with break feature on page 339 for more details.
The break source can be either the break input pin or a clock failure event, generated by the
Clock Security System (CSS), from the Reset Clock Controller. For further information on
the Clock Security System, refer to Section 7.2.7: Clock security system (CSS) on page 94.
When exiting from reset, the break circuit is disabled and the MOE bit is low. You can enable
the break function by setting the BKE bit in the TIMx_BDTR register. The break input
polarity can be selected by configuring the BKP bit in the same register. BKE and BKP can
be modified at the same time. When the BKE and BKP bits are written, a delay of 1 APB
clock cycle is applied before the writing is effective. Consequently, it is necessary to wait 1
APB clock period to correctly read back the bit after the write operation.
Because MOE falling edge can be asynchronous, a resynchronization circuit has been
inserted between the actual signal (acting on the outputs) and the synchronous control bit
(accessed in the TIMx_BDTR register). It results in some delays between the asynchronous
and the synchronous signals. In particular, if you write MOE to 1 whereas it was low, you
delay
OCxREF
OCx
OCxN
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 309/1093
must insert a delay (dummy instruction) before reading it correctly. This is because you write
the asynchronous signal and read the synchronous signal.
When a break occurs (selected level on the break input):
The MOE bit is cleared asynchronously, putting the outputs in inactive state, idle state
or in reset state (selected by the OSSI bit). This feature functions even if the MCU
oscillator is off.
Each output channel is driven with the level programmed in the OISx bit in the
TIMx_CR2 register as soon as MOE=0. If OSSI=0 then the timer releases the enable
output else the enable output remains high.
When complementary outputs are used:
The outputs are first put in reset state inactive state (depending on the polarity).
This is done asynchronously so that it works even if no clock is provided to the
timer.
If the timer clock is still present, then the dead-time generator is reactivated in
order to drive the outputs with the level programmed in the OISx and OISxN bits
after a dead-time. Even in this case, OCx and OCxN cannot be driven to their
active level together. Note that because of the resynchronization on MOE, the
dead-time duration is a bit longer than usual (around 2 ck_tim clock cycles).
If OSSI=0 then the timer releases the enable outputs else the enable outputs
remain or become high as soon as one of the CCxE or CCxNE bits is high.
The break status flag (BIF bit in the TIMx_SR register) is set. An interrupt can be
generated if the BIE bit in the TIMx_DIER register is set. A DMA request can be sent if
the BDE bit in the TIMx_DIER register is set.
If the AOE bit in the TIMx_BDTR register is set, the MOE bit is automatically set again
at the next update event UEV. This can be used to perform a regulation, for instance.
Else, MOE remains low until you write it to 1 again. In this case, it can be used for
security and you can connect the break input to an alarm from power drivers, thermal
sensors or any security components.
Note: The break inputs is acting on level. Thus, the MOE cannot be set while the break input is
active (neither automatically nor by software). In the meantime, the status flag BIF cannot be
cleared.
The break can be generated by the BRK input which has a programmable polarity and an
enable bit BKE in the TIMx_BDTR Register.
In addition to the break input and the output management, a write protection has been
implemented inside the break circuit to safeguard the application. It allows you to freeze the
configuration of several parameters (dead-time duration, OCx/OCxN polarities and state
when disabled, OCxM configurations, break enable and polarity). You can choose from 3
levels of protection selected by the LOCK bits in the TIMx_BDTR register. Refer to
Section 14.4.18: TIM1&TIM8 break and dead-time register (TIMx_BDTR) on page 343. The
LOCK bits can be written only once after an MCU reset.
The Figure 89 shows an example of behavior of the outputs in response to a break.
Advanced-control timers (TIM1&TIM8) RM0008
310/1093 Doc ID 13902 Rev 13
Figure 89. Output behavior in response to a break.
delay
OCxREF
BREAK (MOE
OCx
(OCxN not implemented, CCxP=0, OISx=1)
OCx
(OCxN not implemented, CCxP=0, OISx=0)
OCx
(OCxN not implemented, CCxP=1, OISx=1)
OCx
(OCxN not implemented, CCxP=1, OISx=0)
OCx
OCxN
(CCxE=1, CCxP=0, OISx=0, CCxNE=1, CCxNP=0, OISxN=1)
delay delay
delay
OCx
OCxN
(CCxE=1, CCxP=0, OISx=1, CCxNE=1, CCxNP=1, OISxN=1)
delay delay
delay
OCx
OCxN
(CCxE=1, CCxP=0, OISx=0, CCxNE=0, CCxNP=0, OISxN=1)
)
delay
OCx
OCxN
(CCxE=1, CCxP=0, OISx=1, CCxNE=0, CCxNP=0, OISxN=0)
OCx
OCxN
(CCxE=1, CCxP=0, CCxNE=0, CCxNP=0, OISx=OISxN=0 or OISx=OISxN=1)
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 311/1093
14.3.13 Clearing the OCxREF signal on an external event
The OCxREF signal for a given channel can be driven Low by applying a High level to the
ETRF input (OCxCE enable bit of the corresponding TIMx_CCMRx register set to 1). The
OCxREF signal remains Low until the next update event, UEV, occurs.
This function can only be used in output compare and PWM modes, and does not work in
forced mode.
For example, the OCxREF signal) can be connected to the output of a comparator to be
used for current handling. In this case, the ETR must be configured as follow:
1. The External Trigger Prescaler should be kept off: bits ETPS[1:0] of the TIMx_SMCR
register set to 00.
2. The external clock mode 2 must be disabled: bit ECE of the TIMx_SMCR register set to
0.
3. The External Trigger Polarity (ETP) and the External Trigger Filter (ETF) can be
configured according to the user needs.
Figure 90 shows the behavior of the OCxREF signal when the ETRF Input becomes High,
for both values of the enable bit OCxCE. In this example, the timer TIMx is programmed in
PWM mode.
Figure 90. Clearing TIMx OCxREF
OCxREF
counter (CNT)
OCxREF
ETRF
(OCxCE=0)
(OCxCE=1)
OCREF_CLR
becomes high
OCREF_CLR
still high
(CCRx)
Advanced-control timers (TIM1&TIM8) RM0008
312/1093 Doc ID 13902 Rev 13
14.3.14 6-step PWM generation
When complementary outputs are used on a channel, preload bits are available on the
OCxM, CCxE and CCxNE bits. The preload bits are transferred to the shadow bits at the
COM commutation event. Thus you can program in advance the configuration for the next
step and change the configuration of all the channels at the same time. COM can be
generated by software by setting the COM bit in the TIMx_EGR register or by hardware (on
TRGI rising edge).
A flag is set when the COM event occurs (COMIF bit in the TIMx_SR register), which can
generate an interrupt (if the COMIE bit is set in the TIMx_DIER register) or a DMA request
(if the COMDE bit is set in the TIMx_DIER register).
The Figure 91 describes the behavior of the OCx and OCxN outputs when a COM event
occurs, in 3 different examples of programmed configurations.
Figure 91. 6-step generation, COM example (OSSR=1)
(CCRx)
OCx
OCxN
Write COM to 1
counter (CNT)
OCxREF
COM event
CCxE=1
CCxNE=0
OCxM=100
OCx
OCxN
CCxE=0
CCxNE=1
OCxM=101
OCx
OCxN
CCxE=1
CCxNE=0
OCxM=100
Example 1
Example 2
Example 3
write OCxM to 100
CCxE=1
CCxNE=0
OCxM=100 (forced inactive)
CCxE=1
CCxNE=0
OCxM=100 (forced inactive)
Write CCxNE to 1
and OCxM to 101
write CCxNE to 0
and OCxM to 100
CCxE=1
CCxNE=0
OCxM=100 (forced inactive)
ai14910
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 313/1093
14.3.15 One-pulse mode
One-pulse mode (OPM) is a particular case of the previous modes. It allows the counter to
be started in response to a stimulus and to generate a pulse with a programmable length
after a programmable delay.
Starting the counter can be controlled through the slave mode controller. Generating the
waveform can be done in output compare mode or PWM mode. You select One-pulse mode
by setting the OPM bit in the TIMx_CR1 register. This makes the counter stop automatically
at the next update event UEV.
A pulse can be correctly generated only if the compare value is different from the counter
initial value. Before starting (when the timer is waiting for the trigger), the configuration must
be:
In upcounting: CNT < CCRx ARR (in particular, 0 < CCRx)
In downcounting: CNT > CCRx
Figure 92. Example of one pulse mode.
For example you may want to generate a positive pulse on OC1 with a length of t
PULSE
and
after a delay of t
DELAY
as soon as a positive edge is detected on the TI2 input pin.
Lets use TI2FP2 as trigger 1:
Map TI2FP2 to TI2 by writing CC2S=01 in the TIMx_CCMR1 register.
TI2FP2 must detect a rising edge, write CC2P=0 in the TIMx_CCER register.
Configure TI2FP2 as trigger for the slave mode controller (TRGI) by writing TS=110 in
the TIMx_SMCR register.
TI2FP2 is used to start the counter by writing SMS to 110 in the TIMx_SMCR register
(trigger mode).
TI2
OC1REF
C
o
u
n
t
e
r
t
0
TIM1_ARR
TIM1_CCR1
OC1
t
DELAY
t
PULSE
Advanced-control timers (TIM1&TIM8) RM0008
314/1093 Doc ID 13902 Rev 13
The OPM waveform is defined by writing the compare registers (taking into account the
clock frequency and the counter prescaler).
The t
DELAY
is defined by the value written in the TIMx_CCR1 register.
The t
PULSE
is defined by the difference between the auto-reload value and the compare
value (TIMx_ARR - TIMx_CCR1).
Lets say you want to build a waveform with a transition from 0 to 1 when a compare
match occurs and a transition from 1 to 0 when the counter reaches the auto-reload
value. To do this you enable PWM mode 2 by writing OC1M=111 in the TIMx_CCMR1
register. You can optionally enable the preload registers by writing OC1PE=1 in the
TIMx_CCMR1 register and ARPE in the TIMx_CR1 register. In this case you have to
write the compare value in the TIMx_CCR1 register, the auto-reload value in the
TIMx_ARR register, generate an update by setting the UG bit and wait for external
trigger event on TI2. CC1P is written to 0 in this example.
In our example, the DIR and CMS bits in the TIMx_CR1 register should be low.
You only want 1 pulse (Single mode), so you write '1 in the OPM bit in the TIMx_CR1
register to stop the counter at the next update event (when the counter rolls over from the
auto-reload value back to 0). When OPM bit in the TIMx_CR1 register is set to '0', so the
Repetitive Mode is selected.
Particular case: OCx fast enable:
In One-pulse mode, the edge detection on TIx input set the CEN bit which enables the
counter. Then the comparison between the counter and the compare value makes the
output toggle. But several clock cycles are needed for these operations and it limits the
minimum delay t
DELAY
min we can get.
If you want to output a waveform with the minimum delay, you can set the OCxFE bit in the
TIMx_CCMRx register. Then OCxRef (and OCx) are forced in response to the stimulus,
without taking in account the comparison. Its new level is the same as if a compare match
had occurred. OCxFE acts only if the channel is configured in PWM1 or PWM2 mode.
14.3.16 Encoder interface mode
To select Encoder Interface mode write SMS=001 in the TIMx_SMCR register if the
counter is counting on TI2 edges only, SMS=010 if it is counting on TI1 edges only and
SMS=011 if it is counting on both TI1 and TI2 edges.
Select the TI1 and TI2 polarity by programming the CC1P and CC2P bits in the TIMx_CCER
register. When needed, you can program the input filter as well.
The two inputs TI1 and TI2 are used to interface to an incremental encoder. Refer to
Table 81. The counter is clocked by each valid transition on TI1FP1 or TI2FP2 (TI1 and TI2
after input filter and polarity selection, TI1FP1=TI1 if not filtered and not inverted,
TI2FP2=TI2 if not filtered and not inverted) assuming that it is enabled (CEN bit in
TIMx_CR1 register written to 1). The sequence of transitions of the two inputs is evaluated
and generates count pulses as well as the direction signal. Depending on the sequence the
counter counts up or down, the DIR bit in the TIMx_CR1 register is modified by hardware
accordingly. The DIR bit is calculated at each transition on any input (TI1 or TI2), whatever
the counter is counting on TI1 only, TI2 only or both TI1 and TI2.
Encoder interface mode acts simply as an external clock with direction selection. This
means that the counter just counts continuously between 0 and the auto-reload value in the
TIMx_ARR register (0 to ARR or ARR down to 0 depending on the direction). So you must
configure TIMx_ARR before starting. in the same way, the capture, compare, prescaler,
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 315/1093
repetition counter, trigger output features continue to work as normal. Encoder mode and
External clock mode 2 are not compatible and must not be selected together.
In this mode, the counter is modified automatically following the speed and the direction of
the incremental encoder and its content, therefore, always represents the encoders
position. The count direction correspond to the rotation direction of the connected sensor.
The table summarizes the possible combinations, assuming TI1 and TI2 dont switch at the
same time.
An external incremental encoder can be connected directly to the MCU without external
interface logic. However, comparators are normally be used to convert the encoders
differential outputs to digital signals. This greatly increases noise immunity. The third
encoder output which indicate the mechanical zero position, may be connected to an
external interrupt input and trigger a counter reset.
The Figure 93 gives an example of counter operation, showing count signal generation and
direction control. It also shows how input jitter is compensated where both edges are
selected. This might occur if the sensor is positioned near to one of the switching points. For
this example we assume that the configuration is the following:
CC1S=01 (TIMx_CCMR1 register, TI1FP1 mapped on TI1).
CC2S=01 (TIMx_CCMR2 register, TI1FP2 mapped on TI2).
CC1P=0 (TIMx_CCER register, TI1FP1 non-inverted, TI1FP1=TI1).
CC2P=0 (TIMx_CCER register, TI1FP2 non-inverted, TI1FP2= TI2).
SMS=011 (TIMx_SMCR register, both inputs are active on both rising and falling
edges).
CEN=1 (TIMx_CR1 register, Counter enabled).
Table 81. Counting direction versus encoder signals
Active edge
Level on
opposite
signal (TI1FP1
for TI2, TI2FP2
for TI1)
TI1FP1 signal TI2FP2 signal
Rising Falling Rising Falling
Counting on
TI1 only
High Down Up No Count No Count
Low Up Down No Count No Count
Counting on
TI2 only
High No Count No Count Up Down
Low No Count No Count Down Up
Counting on
TI1 and TI2
High Down Up Up Down
Low Up Down Down Up
Advanced-control timers (TIM1&TIM8) RM0008
316/1093 Doc ID 13902 Rev 13
Figure 93. Example of counter operation in encoder interface mode.
Figure 94 gives an example of counter behavior when TI1FP1 polarity is inverted (same
configuration as above except CC1P=1).
Figure 94. Example of encoder interface mode with TI1FP1 polarity inverted.
The timer, when configured in Encoder Interface mode provides information on the sensors
current position. You can obtain dynamic information (speed, acceleration, deceleration) by
measuring the period between two encoder events using a second timer configured in
capture mode. The output of the encoder which indicates the mechanical zero can be used
for this purpose. Depending on the time between two events, the counter can also be read
at regular times. You can do this by latching the counter value into a third input capture
register if available (then the capture signal must be periodic and can be generated by
another timer). when available, it is also possible to read its value through a DMA request
generated by a real-time clock.
TI1
forward forward backward jitter jitter
up down up
TI2
Counter
TI1
forward forward backward jitter jitter
up down
TI2
Counter
down
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 317/1093
14.3.17 Timer input XOR function
The TI1S bit in the TIMx_CR2 register, allows the input filter of channel 1 to be connected to
the output of a XOR gate, combining the three input pins TIMx_CH1, TIMx_CH2 and
TIMx_CH3.
The XOR output can be used with all the timer input functions such as trigger or input
capture. An example of this feature used to interface Hall sensors is given in Section 14.3.18
below.
14.3.18 Interfacing with Hall sensors
This is done using the advanced-control timers (TIM1 or TIM8) to generate PWM signals to
drive the motor and another timer TIMx (TIM2, TIM3, TIM4 or TIM5) referred to as
interfacing timer in Figure 95. The interfacing timer captures the 3 timer input pins (CC1,
CC2, CC3) connected through a XOR to the TI1 input channel (selected by setting the TI1S
bit in the TIMx_CR2 register).
The slave mode controller is configured in reset mode; the slave input is TI1F_ED. Thus,
each time one of the 3 inputs toggles, the counter restarts counting from 0. This creates a
time base triggered by any change on the Hall inputs.
On the interfacing timer, capture/compare channel 1 is configured in capture mode,
capture signal is TRC (See Figure 78: Capture/compare channel (example: channel 1 input
stage) on page 299). The captured value, which corresponds to the time elapsed between 2
changes on the inputs, gives information about motor speed.
The interfacing timer can be used in output mode to generate a pulse which changes the
configuration of the channels of the advanced-control timer (TIM1 or TIM8) (by triggering a
COM event). The TIM1 timer is used to generate PWM signals to drive the motor. To do this,
the interfacing timer channel must be programmed so that a positive pulse is generated after
a programmed delay (in output compare or PWM mode). This pulse is sent to the advanced-
control timer (TIM1 or TIM8) through the TRGO output.
Example: you want to change the PWM configuration of your advanced-control timer TIM1
after a programmed delay each time a change occurs on the Hall inputs connected to one of
the TIMx timers.
Configure 3 timer inputs ORed to the TI1 input channel by writing the TI1S bit in the
TIMx_CR2 register to 1,
Program the time base: write the TIMx_ARR to the max value (the counter must be
cleared by the TI1 change. Set the prescaler to get a maximum counter period longer
than the time between 2 changes on the sensors,
Program the channel 1 in capture mode (TRC selected): write the CC1S bits in the
TIMx_CCMR1 register to 01. You can also program the digital filter if needed,
Program the channel 2 in PWM 2 mode with the desired delay: write the OC2M bits to
111 and the CC2S bits to 00 in the TIMx_CCMR1 register,
Select OC2REF as trigger output on TRGO: write the MMS bits in the TIMx_CR2
register to 101,
In the advanced-control timer TIM1, the right ITR input must be selected as trigger input, the
timer is programmed to generate PWM signals, the capture/compare control signals are
preloaded (CCPC=1 in the TIMx_CR2 register) and the COM event is controlled by the
trigger input (CCUS=1 in the TIMx_CR2 register). The PWM control bits (CCxE, OCxM) are
Advanced-control timers (TIM1&TIM8) RM0008
318/1093 Doc ID 13902 Rev 13
written after a COM event for the next step (this can be done in an interrupt subroutine
generated by the rising edge of OC2REF).
The Figure 95 describes this example.
Figure 95. Example of hall sensor interface
counter (CNT)
TRGO=OC2REF
(CCR2)
OC1
OC1N
COM
Write CCxE, CCxNE
TIH1
TIH2
TIH3
CCR1
OC2
OC2N
OC3
OC3N
C7A3 C7A8 C794 C7A5 C7AB C796
and OCxM for next step
I
n
t
e
r
f
a
c
i
n
g
t
i
m
e
r
a
d
v
a
n
c
e
d
-
c
o
n
t
r
o
l
t
i
m
e
r
s
(
T
I
M
1
&
T
I
M
8
)
ai17335
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 319/1093
14.3.19 TIMx and external trigger synchronization
The TIMx timer can be synchronized with an external trigger in several modes: Reset mode,
Gated mode and Trigger mode.
Slave mode: Reset mode
The counter and its prescaler can be reinitialized in response to an event on a trigger input.
Moreover, if the URS bit from the TIMx_CR1 register is low, an update event UEV is
generated. Then all the preloaded registers (TIMx_ARR, TIMx_CCRx) are updated.
In the following example, the upcounter is cleared in response to a rising edge on TI1 input:
Configure the channel 1 to detect rising edges on TI1. Configure the input filter duration
(in this example, we dont need any filter, so we keep IC1F=0000). The capture
prescaler is not used for triggering, so you dont need to configure it. The CC1S bits
select the input capture source only, CC1S = 01 in the TIMx_CCMR1 register. Write
CC1P=0 in TIMx_CCER register to validate the polarity (and detect rising edges only).
Configure the timer in reset mode by writing SMS=100 in TIMx_SMCR register. Select
TI1 as the input source by writing TS=101 in TIMx_SMCR register.
Start the counter by writing CEN=1 in the TIMx_CR1 register.
The counter starts counting on the internal clock, then behaves normally until TI1 rising
edge. When TI1 rises, the counter is cleared and restarts from 0. In the meantime, the
trigger flag is set (TIF bit in the TIMx_SR register) and an interrupt request, or a DMA
request can be sent if enabled (depending on the TIE and TDE bits in TIMx_DIER register).
The following figure shows this behavior when the auto-reload register TIMx_ARR=0x36.
The delay between the rising edge on TI1 and the actual reset of the counter is due to the
resynchronization circuit on TI1 input.
Figure 96. Control circuit in reset mode
00
Counter clock = ck_cnt = ck_psc
Counter register 01 02 03 00 01 02 03 32 33 34 35 36
UG
TI1
31 30
TIF
Advanced-control timers (TIM1&TIM8) RM0008
320/1093 Doc ID 13902 Rev 13
Slave mode: Gated mode
The counter can be enabled depending on the level of a selected input.
In the following example, the upcounter counts only when TI1 input is low:
Configure the channel 1 to detect low levels on TI1. Configure the input filter duration
(in this example, we dont need any filter, so we keep IC1F=0000). The capture
prescaler is not used for triggering, so you dont need to configure it. The CC1S bits
select the input capture source only, CC1S=01 in TIMx_CCMR1 register. Write
CC1P=1 in TIMx_CCER register to validate the polarity (and detect low level only).
Configure the timer in gated mode by writing SMS=101 in TIMx_SMCR register. Select
TI1 as the input source by writing TS=101 in TIMx_SMCR register.
Enable the counter by writing CEN=1 in the TIMx_CR1 register (in gated mode, the
counter doesnt start if CEN=0, whatever is the trigger input level).
The counter starts counting on the internal clock as long as TI1 is low and stops as soon as
TI1 becomes high. The TIF flag in the TIMx_SR register is set both when the counter starts
or stops.
The delay between the rising edge on TI1 and the actual stop of the counter is due to the
resynchronization circuit on TI1 input.
Figure 97. Control circuit in gated mode
Counter clock = ck_cnt = ck_psc
Counter register 35 36 37 38 32 33 34
TI1
31 30
cnt_en
TIF
Write TIF=0
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 321/1093
Slave mode: Trigger mode
The counter can start in response to an event on a selected input.
In the following example, the upcounter starts in response to a rising edge on TI2 input:
Configure the channel 2 to detect rising edges on TI2. Configure the input filter duration
(in this example, we dont need any filter, so we keep IC2F=0000). The capture
prescaler is not used for triggering, so you dont need to configure it. The CC2S bits are
configured to select the input capture source only, CC2S=01 in TIMx_CCMR1 register.
Write CC2P=1 in TIMx_CCER register to validate the polarity (and detect low level
only).
Configure the timer in trigger mode by writing SMS=110 in TIMx_SMCR register. Select
TI2 as the input source by writing TS=110 in TIMx_SMCR register.
When a rising edge occurs on TI2, the counter starts counting on the internal clock and the
TIF flag is set.
The delay between the rising edge on TI2 and the actual start of the counter is due to the
resynchronization circuit on TI2 input.
Figure 98. Control circuit in trigger mode
Slave mode: external clock mode 2 + trigger mode
The external clock mode 2 can be used in addition to another slave mode (except external
clock mode 1 and encoder mode). In this case, the ETR signal is used as external clock
input, and another input can be selected as trigger input (in reset mode, gated mode or
trigger mode). It is recommended not to select ETR as TRGI through the TS bits of
TIMx_SMCR register.
In the following example, the upcounter is incremented at each rising edge of the ETR signal
as soon as a rising edge of TI1 occurs:
1. Configure the external trigger input circuit by programming the TIMx_SMCR register as
follows:
ETF = 0000: no filter
ETPS=00: prescaler disabled
ETP=0: detection of rising edges on ETR and ECE=1 to enable the external clock
mode 2.
Counter clock = ck_cnt = ck_psc
Counter register 35 36 37 38 34
TI2
cnt_en
TIF
Advanced-control timers (TIM1&TIM8) RM0008
322/1093 Doc ID 13902 Rev 13
2. Configure the channel 1 as follows, to detect rising edges on TI:
IC1F=0000: no filter.
The capture prescaler is not used for triggering and does not need to be
configured.
CC1S=01in TIMx_CCMR1 register to select only the input capture source
CC1P=0 in TIMx_CCER register to validate the polarity (and detect rising edge
only).
3. Configure the timer in trigger mode by writing SMS=110 in TIMx_SMCR register. Select
TI1 as the input source by writing TS=101 in TIMx_SMCR register.
A rising edge on TI1 enables the counter and sets the TIF flag. The counter then counts on
ETR rising edges.
The delay between the rising edge of the ETR signal and the actual reset of the counter is
due to the resynchronization circuit on ETRP input.
Figure 99. Control circuit in external clock mode 2 + trigger mode
14.3.20 Timer synchronization
The TIM timers are linked together internally for timer synchronization or chaining. Refer to
Section 15.3.15: Timer synchronization on page 380 for details.
14.3.21 Debug mode
When the microcontroller enters debug mode (Cortex-M3 core halted), the TIMx counter
either continues to work normally or stops, depending on DBG_TIMx_STOP configuration
bit in DBG module. For more details, refer to Section 31.16.2: Debug support for timers,
watchdog, bxCAN and I2C.
Counter clock = CK_CNT = CK_PSC
Counter register 35 36 34
ETR
CEN/CNT_EN
TIF
TI1
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 323/1093
14.4 TIM1&TIM8 registers
Refer toSection 2.1 on page 46 for a list of abbreviations used in register descriptions.
The peripheral registers can be accessed by half-words (16-bit) or words (32-bit).
14.4.1 TIM1&TIM8 control register 1 (TIMx_CR1)
Address offset: 0x00
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
CKD[1:0] ARPE CMS[1:0] DIR OPM URS UDIS CEN
rw rw rw rw rw rw rw rw rw rw
Bits 15:10 Reserved, always read as 0
Bits 9:8 CKD[1:0]: Clock division
This bit-field indicates the division ratio between the timer clock (CK_INT) frequency and the
dead-time and sampling clock (t
DTS
)used by the dead-time generators and the digital filters
(ETR, TIx),
00: t
DTS
=t
CK_INT
01: t
DTS
=2*t
CK_INT
10: t
DTS
=4*t
CK_INT
11: Reserved, do not program this value
Bit 7 ARPE: Auto-reload preload enable
0: TIMx_ARR register is not buffered
1: TIMx_ARR register is buffered
Bits 6:5 CMS[1:0]: Center-aligned mode selection
00: Edge-aligned mode. The counter counts up or down depending on the direction bit
(DIR).
01: Center-aligned mode 1. The counter counts up and down alternatively. Output compare
interrupt flags of channels configured in output (CCxS=00 in TIMx_CCMRx register) are set
only when the counter is counting down.
10: Center-aligned mode 2. The counter counts up and down alternatively. Output compare
interrupt flags of channels configured in output (CCxS=00 in TIMx_CCMRx register) are set
only when the counter is counting up.
11: Center-aligned mode 3. The counter counts up and down alternatively. Output compare
interrupt flags of channels configured in output (CCxS=00 in TIMx_CCMRx register) are set
both when the counter is counting up or down.
Note: It is not allowed to switch from edge-aligned mode to center-aligned mode as long as
the counter is enabled (CEN=1)
Bit 4 DIR: Direction
0: Counter used as upcounter
1: Counter used as downcounter
Note: This bit is read only when the timer is configured in Center-aligned mode or Encoder
mode.
Bit 3 OPM: One pulse mode
0: Counter is not stopped at update event
1: Counter stops counting at the next update event (clearing the bit CEN)
Advanced-control timers (TIM1&TIM8) RM0008
324/1093 Doc ID 13902 Rev 13
14.4.2 TIM1&TIM8 control register 2 (TIMx_CR2)
Address offset: 0x04
Reset value: 0x0000
Bit 2 URS: Update request source
This bit is set and cleared by software to select the UEV event sources.
0: Any of the following events generate an update interrupt or DMA request if enabled.
These events can be:
Counter overflow/underflow
Setting the UG bit
Update generation through the slave mode controller
1: Only counter overflow/underflow generates an update interrupt or DMA request if
enabled.
Bit 1 UDIS: Update disable
This bit is set and cleared by software to enable/disable UEV event generation.
0: UEV enabled. The Update (UEV) event is generated by one of the following events:
Counter overflow/underflow
Setting the UG bit
Update generation through the slave mode controller
Buffered registers are then loaded with their preload values.
1: UEV disabled. The Update event is not generated, shadow registers keep their value
(ARR, PSC, CCRx). However the counter and the prescaler are reinitialized if the UG bit is
set or if a hardware reset is received from the slave mode controller.
Bit 0 CEN: Counter enable
0: Counter disabled
1: Counter enabled
Note: External clock, gated mode and encoder mode can work only if the CEN bit has been
previously set by software. However trigger mode can set the CEN bit automatically by
hardware.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res.
OIS4 OIS3N OIS3 OIS2N OIS2 OIS1N OIS1 TI1S MMS[2:0] CCDS CCUS
Res.
CCPC
rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bit 15 Reserved, always read as 0
Bit 14 OIS4: Output Idle state 4 (OC4 output)
refer to OIS1 bit
Bit 13 OIS3N: Output Idle state 3 (OC3N output)
refer to OIS1N bit
Bit 12 OIS3: Output Idle state 3 (OC3 output)
refer to OIS1 bit
Bit 11 OIS2N: Output Idle state 2 (OC2N output)
refer to OIS1N bit
Bit 10 OIS2: Output Idle state 2 (OC2 output)
refer to OIS1 bit
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 325/1093
Bit 9 OIS1N: Output Idle state 1 (OC1N output)
0: OC1N=0 after a dead-time when MOE=0
1: OC1N=1 after a dead-time when MOE=0
Note: This bit can not be modified as long as LOCK level 1, 2 or 3 has been programmed
(LOCK bits in TIMx_BDTR register).
Bit 8 OIS1: Output Idle state 1 (OC1 output)
0: OC1=0 (after a dead-time if OC1N is implemented) when MOE=0
1: OC1=1 (after a dead-time if OC1N is implemented) when MOE=0
Note: This bit can not be modified as long as LOCK level 1, 2 or 3 has been programmed
(LOCK bits in TIMx_BDTR register).
Bit 7 TI1S: TI1 selection
0: The TIMx_CH1 pin is connected to TI1 input
1: The TIMx_CH1, CH2 and CH3 pins are connected to the TI1 input (XOR combination)
Bits 6:4 MMS[1:0]: Master mode selection
These bits allow to select the information to be sent in master mode to slave timers for
synchronization (TRGO). The combination is as follows:
000: Reset - the UG bit from the TIMx_EGR register is used as trigger output (TRGO). If the
reset is generated by the trigger input (slave mode controller configured in reset mode) then
the signal on TRGO is delayed compared to the actual reset.
001: Enable - the Counter Enable signal CNT_EN is used as trigger output (TRGO). It is
useful to start several timers at the same time or to control a window in which a slave timer is
enable. The Counter Enable signal is generated by a logic OR between CEN control bit and
the trigger input when configured in gated mode. When the Counter Enable signal is
controlled by the trigger input, there is a delay on TRGO, except if the master/slave mode is
selected (see the MSM bit description in TIMx_SMCR register).
010: Update - The update event is selected as trigger output (TRGO). For instance a master
timer can then be used as a prescaler for a slave timer.
011: Compare Pulse - The trigger output send a positive pulse when the CC1IF flag is to be
set (even if it was already high), as soon as a capture or a compare match occurred.
(TRGO).
100: Compare - OC1REF signal is used as trigger output (TRGO)
101: Compare - OC2REF signal is used as trigger output (TRGO)
110: Compare - OC3REF signal is used as trigger output (TRGO)
111: Compare - OC4REF signal is used as trigger output (TRGO)
Bit 3 CCDS: Capture/compare DMA selection
0: CCx DMA request sent when CCx event occurs
1: CCx DMA requests sent when update event occurs
Bit 2 CCUS: Capture/compare control update selection
0: When capture/compare control bits are preloaded (CCPC=1), they are updated by setting
the COMG bit only
1: When capture/compare control bits are preloaded (CCPC=1), they are updated by setting
the COMG bit or when an rising edge occurs on TRGI
Note: This bit acts only on channels that have a complementary output.
Bit 1 Reserved, always read as 0
Bit 0 CCPC: Capture/compare preloaded control
0: CCxE, CCxNE and OCxM bits are not preloaded
1: CCxE, CCxNE and OCxM bits are preloaded, after having been written, they are updated
only when COM bit is set.
Note: This bit acts only on channels that have a complementary output.
Advanced-control timers (TIM1&TIM8) RM0008
326/1093 Doc ID 13902 Rev 13
14.4.3 TIM1&TIM8 slave mode control register (TIMx_SMCR)
Address offset: 0x08
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
ETP ECE ETPS[1:0] ETF[3:0] MSM TS[2:0] Res. SMS[2:0]
rw rw rw rw rw rw rw rw rw rw rw rw Res. rw rw rw
Bit 15 ETP: External trigger polarity
This bit selects whether ETR or ETR is used for trigger operations
0: ETR is non-inverted, active at high level or rising edge.
1: ETR is inverted, active at low level or falling edge.
Bit 14 ECE: External clock enable
This bit enables External clock mode 2.
0: External clock mode 2 disabled
1: External clock mode 2 enabled. The counter is clocked by any active edge on the ETRF
signal.
Note: 1: Setting the ECE bit has the same effect as selecting external clock mode 1 with
TRGI connected to ETRF (SMS=111 and TS=111).
2: It is possible to simultaneously use external clock mode 2 with the following slave
modes: reset mode, gated mode and trigger mode. Nevertheless, TRGI must not be
connected to ETRF in this case (TS bits must not be 111).
3: If external clock mode 1 and external clock mode 2 are enabled at the same time,
the external clock input is ETRF.
Bits 13:12 ETPS[1:0]: External trigger prescaler
External trigger signal ETRP frequency must be at most 1/4 of TIMxCLK frequency. A
prescaler can be enabled to reduce ETRP frequency. It is useful when inputting fast external
clocks.
00: Prescaler OFF
01: ETRP frequency divided by 2
10: ETRP frequency divided by 4
11: ETRP frequency divided by 8
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 327/1093
Bits 11:8 ETF[3:0]: External trigger filter
This bit-field then defines the frequency used to sample ETRP signal and the length of the
digital filter applied to ETRP. The digital filter is made of an event counter in which N events
are needed to validate a transition on the output:
0000: No filter, sampling is done at f
DTS
0001: f
SAMPLING
=f
CK_INT
, N=2
0010: f
SAMPLING
=f
CK_INT
, N=4
0011: f
SAMPLING
=f
CK_INT
, N=8
0100: f
SAMPLING
=f
DTS
/2, N=6
0101: f
SAMPLING
=f
DTS
/2, N=8
0110: f
SAMPLING
=f
DTS
/4, N=6
0111: f
SAMPLING
=f
DTS
/4, N=8
1000: f
SAMPLING
=f
DTS
/8, N=6
1001: f
SAMPLING
=f
DTS
/8, N=8
1010: f
SAMPLING
=f
DTS
/16, N=5
1011: f
SAMPLING
=f
DTS
/16, N=6
1100: f
SAMPLING
=f
DTS
/16, N=8
1101: f
SAMPLING
=f
DTS
/32, N=5
1110: f
SAMPLING
=f
DTS
/32, N=6
1111: f
SAMPLING
=f
DTS
/32, N=8
Bit 7 MSM: Master/slave mode
0: No action
1: The effect of an event on the trigger input (TRGI) is delayed to allow a perfect
synchronization between the current timer and its slaves (through TRGO). It is useful if we
want to synchronize several timers on a single external event.
Bits 6:4 TS[2:0]: Trigger selection
This bit-field selects the trigger input to be used to synchronize the counter.
000: Internal Trigger 0 (ITR0)
001: Internal Trigger 1 (ITR1)
010: Internal Trigger 2 (ITR2)
011: Internal Trigger 3 (ITR3)
100: TI1 Edge Detector (TI1F_ED)
101: Filtered Timer Input 1 (TI1FP1)
110: Filtered Timer Input 2 (TI2FP2)
111: External Trigger input (ETRF)
See Table 82: TIMx Internal trigger connection on page 328 for more details on ITRx meaning
for each Timer.
Note: These bits must be changed only when they are not used (e.g. when SMS=000) to
avoid wrong edge detections at the transition.
Bit 3 Reserved, must be kept at reset value.
Advanced-control timers (TIM1&TIM8) RM0008
328/1093 Doc ID 13902 Rev 13
14.4.4 TIM1&TIM8 DMA/interrupt enable register (TIMx_DIER)
Address offset: 0x0C
Reset value: 0x0000
Bits 2:0 SMS: Slave mode selection
When external signals are selected the active edge of the trigger signal (TRGI) is linked to
the polarity selected on the external input (see Input Control register and Control Register
description.
000: Slave mode disabled - if CEN = 1 then the prescaler is clocked directly by the internal
clock.
001: Encoder mode 1 - Counter counts up/down on TI2FP2 edge depending on TI1FP1
level.
010: Encoder mode 2 - Counter counts up/down on TI1FP1 edge depending on TI2FP2
level.
011: Encoder mode 3 - Counter counts up/down on both TI1FP1 and TI2FP2 edges
depending on the level of the other input.
100: Reset Mode - Rising edge of the selected trigger input (TRGI) reinitializes the counter
and generates an update of the registers.
101: Gated Mode - The counter clock is enabled when the trigger input (TRGI) is high. The
counter stops (but is not reset) as soon as the trigger becomes low. Both start and stop of
the counter are controlled.
110: Trigger Mode - The counter starts at a rising edge of the trigger TRGI (but it is not
reset). Only the start of the counter is controlled.
111: External Clock Mode 1 - Rising edges of the selected trigger (TRGI) clock the counter.
Note: The gated mode must not be used if TI1F_ED is selected as the trigger input
(TS=100). Indeed, TI1F_ED outputs 1 pulse for each transition on TI1F, whereas the
gated mode checks the level of the trigger signal.
Table 82. TIMx Internal trigger connection
(1)
1. When a timer is not present in the product, the corresponding trigger ITRx is not available.
Slave TIM ITR0 (TS = 000) ITR1 (TS = 001) ITR2 (TS = 010) ITR3 (TS = 011)
TIM1 TIM5 TIM2 TIM3 TIM4
TIM8 TIM1 TIM2 TIM4 TIM5
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res.
TDE COMDE CC4DE CC3DE CC2DE CC1DE UDE BIE TIE COMIE CC4IE CC3IE CC2IE CC1IE UIE
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bit 15 Reserved, must be kept at reset value.
Bit 14 TDE: Trigger DMA request enable
0: Trigger DMA request disabled
1: Trigger DMA request enabled
Bit 13 COMDE: COM DMA request enable
0: COM DMA request disabled
1: COM DMA request enabled
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 329/1093
Bit 12 CC4DE: Capture/Compare 4 DMA request enable
0: CC4 DMA request disabled
1: CC4 DMA request enabled
Bit 11 CC3DE: Capture/Compare 3 DMA request enable
0: CC3 DMA request disabled
1: CC3 DMA request enabled
Bit 10 CC2DE: Capture/Compare 2 DMA request enable
0: CC2 DMA request disabled
1: CC2 DMA request enabled
Bit 9 CC1DE: Capture/Compare 1 DMA request enable
0: CC1 DMA request disabled
1: CC1 DMA request enabled
Bit 8 UDE: Update DMA request enable
0: Update DMA request disabled
1: Update DMA request enabled
Bit 7 BIE: Break interrupt enable
0: Break interrupt disabled
1: Break interrupt enabled
Bit 6 TIE: Trigger interrupt enable
0: Trigger interrupt disabled
1: Trigger interrupt enabled
Bit 5 COMIE: COM interrupt enable
0: COM interrupt disabled
1: COM interrupt enabled
Bit 4 CC4IE: Capture/Compare 4 interrupt enable
0: CC4 interrupt disabled
1: CC4 interrupt enabled
Bit 3 CC3IE: Capture/Compare 3 interrupt enable
0: CC3 interrupt disabled
1: CC3 interrupt enabled
Bit 2 CC2IE: Capture/Compare 2 interrupt enable
0: CC2 interrupt disabled
1: CC2 interrupt enabled
Bit 1 CC1IE: Capture/Compare 1 interrupt enable
0: CC1 interrupt disabled
1: CC1 interrupt enabled
Bit 0 UIE: Update interrupt enable
0: Update interrupt disabled
1: Update interrupt enabled
Advanced-control timers (TIM1&TIM8) RM0008
330/1093 Doc ID 13902 Rev 13
14.4.5 TIM1&TIM8 status register (TIMx_SR)
Address offset: 0x10
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
CC4OF CC3OF CC2OF CC1OF Res. BIF TIF COMIF CC4IF CC3IF CC2IF CC1IF UIF
rc_w0 rc_w0 rc_w0 rc_w0 Res. rc_w0 rc_w0 rc_w0 rc_w0 rc_w0 rc_w0 rc_w0 rc_w0
Bits 15:13 Reserved, must be kept at reset value.
Bit 12 CC4OF: Capture/Compare 4 overcapture flag
refer to CC1OF description
Bit 11 CC3OF: Capture/Compare 3 overcapture flag
refer to CC1OF description
Bit 10 CC2OF: Capture/Compare 2 overcapture flag
refer to CC1OF description
Bit 9 CC1OF: Capture/Compare 1 overcapture flag
This flag is set by hardware only when the corresponding channel is configured in input
capture mode. It is cleared by software by writing it to 0.
0: No overcapture has been detected.
1: The counter value has been captured in TIMx_CCR1 register while CC1IF flag was
already set
Bit 8 Reserved, must be kept at reset value.
Bit 7 BIF: Break interrupt flag
This flag is set by hardware as soon as the break input goes active. It can be cleared by
software if the break input is not active.
0: No break event occurred.
1: An active level has been detected on the break input.
Bit 6 TIF: Trigger interrupt flag
This flag is set by hardware on trigger event (active edge detected on TRGI input when the
slave mode controller is enabled in all modes but gated mode, both edges in case gated
mode is selected). It is cleared by software.
0: No trigger event occurred.
1: Trigger interrupt pending.
Bit 5 COMIF: COM interrupt flag
This flag is set by hardware on COM event (when Capture/compare Control bits - CCxE,
CCxNE, OCxM - have been updated). It is cleared by software.
0: No COM event occurred.
1: COM interrupt pending.
Bit 4 CC4IF: Capture/Compare 4 interrupt flag
refer to CC1IF description
Bit 3 CC3IF: Capture/Compare 3 interrupt flag
refer to CC1IF description
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 331/1093
14.4.6 TIM1&TIM8 event generation register (TIMx_EGR)
Address offset: 0x14
Reset value: 0x0000
Bit 2 CC2IF: Capture/Compare 2 interrupt flag
refer to CC1IF description
Bit 1 CC1IF: Capture/Compare 1 interrupt flag
If channel CC1 is configured as output:
This flag is set by hardware when the counter matches the compare value, with some
exception in center-aligned mode (refer to the CMS bits in the TIMx_CR1 register
description). It is cleared by software.
0: No match.
1: The content of the counter TIMx_CNT matches the content of the TIMx_CCR1 register.
When the contents of TIMx_CCR1 are greater than the contents of TIMx_ARR, the CC1IF
bit goes high on the counter overflow (in upcounting and up/down-counting modes) or
underflow (in downcounting mode)
If channel CC1 is configured as input:
This bit is set by hardware on a capture. It is cleared by software or by reading the
TIMx_CCR1 register.
0: No input capture occurred
1: The counter value has been captured in TIMx_CCR1 register (An edge has been
detected on IC1 which matches the selected polarity)
Bit 0 UIF: Update interrupt flag
This bit is set by hardware on an update event. It is cleared by software.
0: No update occurred.
1: Update interrupt pending. This bit is set by hardware when the registers are updated:
At overflow or underflow regarding the repetition counter value (update if repetition
counter = 0) and if the UDIS=0 in the TIMx_CR1 register.
When CNT is reinitialized by software using the UG bit in TIMx_EGR register, if URS=0
and UDIS=0 in the TIMx_CR1 register.
When CNT is reinitialized by a trigger event (refer to Section 14.4.3: TIM1&TIM8 slave
mode control register (TIMx_SMCR)), if URS=0 and UDIS=0 in the TIMx_CR1 register.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
BG TG COMG CC4G CC3G CC2G CC1G UG
w w w w w w w w
Bits 15:8 Reserved, must be kept at reset value.
Bit 7 BG: Break generation
This bit is set by software in order to generate an event, it is automatically cleared by
hardware.
0: No action
1: A break event is generated. MOE bit is cleared and BIF flag is set. Related interrupt or
DMA transfer can occur if enabled.
Advanced-control timers (TIM1&TIM8) RM0008
332/1093 Doc ID 13902 Rev 13
Bit 6 TG: Trigger generation
This bit is set by software in order to generate an event, it is automatically cleared by
hardware.
0: No action
1: The TIF flag is set in TIMx_SR register. Related interrupt or DMA transfer can occur if
enabled.
Bit 5 COMG: Capture/Compare control update generation
This bit can be set by software, it is automatically cleared by hardware
0: No action
1: When CCPC bit is set, it allows to update CCxE, CCxNE and OCxM bits
Note: This bit acts only on channels having a complementary output.
Bit 4 CC4G: Capture/Compare 4 generation
refer to CC1G description
Bit 3 CC3G: Capture/Compare 3 generation
refer to CC1G description
Bit 2 CC2G: Capture/Compare 2 generation
refer to CC1G description
Bit 1 CC1G: Capture/Compare 1 generation
This bit is set by software in order to generate an event, it is automatically cleared by
hardware.
0: No action
1: A capture/compare event is generated on channel 1:
If channel CC1 is configured as output:
CC1IF flag is set, Corresponding interrupt or DMA request is sent if enabled.
If channel CC1 is configured as input:
The current value of the counter is captured in TIMx_CCR1 register. The CC1IF flag is set,
the corresponding interrupt or DMA request is sent if enabled. The CC1OF flag is set if the
CC1IF flag was already high.
Bit 0 UG: Update generation
This bit can be set by software, it is automatically cleared by hardware.
0: No action
1: Reinitialize the counter and generates an update of the registers. Note that the prescaler
counter is cleared too (anyway the prescaler ratio is not affected). The counter is cleared if
the center-aligned mode is selected or if DIR=0 (upcounting), else it takes the auto-reload
value (TIMx_ARR) if DIR=1 (downcounting).
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 333/1093
14.4.7 TIM1&TIM8 capture/compare mode register 1 (TIMx_CCMR1)
Address offset: 0x18
Reset value: 0x0000
The channels can be used in input (capture mode) or in output (compare mode). The
direction of a channel is defined by configuring the corresponding CCxS bits. All the other
bits of this register have a different function in input and in output mode. For a given bit,
OCxx describes its function when the channel is configured in output, ICxx describes its
function when the channel is configured in input. So you must take care that the same bit
can have a different meaning for the input stage and for the output stage.
Output compare mode:
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
OC2
CE
OC2M[2:0]
OC2
PE
OC2
FE
CC2S[1:0]
OC1
CE
OC1M[2:0]
OC1
PE
OC1
FE
CC1S[1:0]
IC2F[3:0] IC2PSC[1:0] IC1F[3:0] IC1PSC[1:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bit 15 OC2CE: Output Compare 2 clear enable
Bits 14:12 OC2M[2:0]: Output Compare 2 mode
Bit 11 OC2PE: Output Compare 2 preload enable
Bit 10 OC2FE: Output Compare 2 fast enable
Bits 9:8 CC2S[1:0]: Capture/Compare 2 selection
This bit-field defines the direction of the channel (input/output) as well as the used input.
00: CC2 channel is configured as output
01: CC2 channel is configured as input, IC2 is mapped on TI2
10: CC2 channel is configured as input, IC2 is mapped on TI1
11: CC2 channel is configured as input, IC2 is mapped on TRC. This mode is working only if
an internal trigger input is selected through the TS bit (TIMx_SMCR register)
Note: CC2S bits are writable only when the channel is OFF (CC2E = 0 in TIMx_CCER).
Bit 7 OC1CE: Output Compare 1 clear enable
OC1CE: Output Compare 1 Clear Enable
0: OC1Ref is not affected by the ETRF Input
1: OC1Ref is cleared as soon as a High level is detected on ETRF input
Advanced-control timers (TIM1&TIM8) RM0008
334/1093 Doc ID 13902 Rev 13
Bits 6:4 OC1M: Output Compare 1 mode
These bits define the behavior of the output reference signal OC1REF from which OC1 and
OC1N are derived. OC1REF is active high whereas OC1 and OC1N active level depends on
CC1P and CC1NP bits.
000: Frozen - The comparison between the output compare register TIMx_CCR1 and the
counter TIMx_CNT has no effect on the outputs.(this mode is used to generate a timing
base).
001: Set channel 1 to active level on match. OC1REF signal is forced high when the counter
TIMx_CNT matches the capture/compare register 1 (TIMx_CCR1).
010: Set channel 1 to inactive level on match. OC1REF signal is forced low when the
counter TIMx_CNT matches the capture/compare register 1 (TIMx_CCR1).
011: Toggle - OC1REF toggles when TIMx_CNT=TIMx_CCR1.
100: Force inactive level - OC1REF is forced low.
101: Force active level - OC1REF is forced high.
110: PWM mode 1 - In upcounting, channel 1 is active as long as TIMx_CNT<TIMx_CCR1
else inactive. In downcounting, channel 1 is inactive (OC1REF=0) as long as
TIMx_CNT>TIMx_CCR1 else active (OC1REF=1).
111: PWM mode 2 - In upcounting, channel 1 is inactive as long as TIMx_CNT<TIMx_CCR1
else active. In downcounting, channel 1 is active as long as TIMx_CNT>TIMx_CCR1 else
inactive.
Note: 1: These bits can not be modified as long as LOCK level 3 has been programmed
(LOCK bits in TIMx_BDTR register) and CC1S=00 (the channel is configured in
output).
2: In PWM mode 1 or 2, the OCREF level changes only when the result of the
comparison changes or when the output compare mode switches from frozen mode
to PWM mode.
Bit 3 OC1PE: Output Compare 1 preload enable
0: Preload register on TIMx_CCR1 disabled. TIMx_CCR1 can be written at anytime, the
new value is taken in account immediately.
1: Preload register on TIMx_CCR1 enabled. Read/Write operations access the preload
register. TIMx_CCR1 preload value is loaded in the active register at each update event.
Note: 1: These bits can not be modified as long as LOCK level 3 has been programmed
(LOCK bits in TIMx_BDTR register) and CC1S=00 (the channel is configured in
output).
2: The PWM mode can be used without validating the preload register only in one
pulse mode (OPM bit set in TIMx_CR1 register). Else the behavior is not guaranteed.
Bit 2 OC1FE: Output Compare 1 fast enable
This bit is used to accelerate the effect of an event on the trigger in input on the CC output.
0: CC1 behaves normally depending on counter and CCR1 values even when the trigger is
ON. The minimum delay to activate CC1 output when an edge occurs on the trigger input is
5 clock cycles.
1: An active edge on the trigger input acts like a compare match on CC1 output. Then, OC is
set to the compare level independently from the result of the comparison. Delay to sample
the trigger input and to activate CC1 output is reduced to 3 clock cycles. OCFE acts only if
the channel is configured in PWM1 or PWM2 mode.
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 335/1093
Input capture mode
Bits 1:0 CC1S: Capture/Compare 1 selection
This bit-field defines the direction of the channel (input/output) as well as the used input.
00: CC1 channel is configured as output
01: CC1 channel is configured as input, IC1 is mapped on TI1
10: CC1 channel is configured as input, IC1 is mapped on TI2
11: CC1 channel is configured as input, IC1 is mapped on TRC. This mode is working only if
an internal trigger input is selected through TS bit (TIMx_SMCR register)
Note: CC1S bits are writable only when the channel is OFF (CC1E = 0 in TIMx_CCER).
Bits 15:12 IC2F: Input capture 2 filter
Bits 11:10 IC2PSC[1:0]: Input capture 2 prescaler
Bits 9:8 CC2S: Capture/Compare 2 selection
This bit-field defines the direction of the channel (input/output) as well as the used input.
00: CC2 channel is configured as output
01: CC2 channel is configured as input, IC2 is mapped on TI2
10: CC2 channel is configured as input, IC2 is mapped on TI1
11: CC2 channel is configured as input, IC2 is mapped on TRC. This mode is working only if an
internal trigger input is selected through TS bit (TIMx_SMCR register)
Note: CC2S bits are writable only when the channel is OFF (CC2E = 0 in TIMx_CCER).
Bits 7:4 IC1F[3:0]: Input capture 1 filter
This bit-field defines the frequency used to sample TI1 input and the length of the digital filter applied
to TI1. The digital filter is made of an event counter in which N events are needed to validate a
transition on the output:
0000: No filter, sampling is done at f
DTS
0001: f
SAMPLING
=f
CK_INT
, N=2
0010: f
SAMPLING
=f
CK_INT
, N=4
0011: f
SAMPLING
=f
CK_INT
, N=8
0100: f
SAMPLING
=f
DTS
/2, N=6
0101: f
SAMPLING
=f
DTS
/2, N=8
0110: f
SAMPLING
=f
DTS
/4, N=6
0111: f
SAMPLING
=f
DTS
/4, N=8
1000: f
SAMPLING
=f
DTS
/8, N=6
1001: f
SAMPLING
=f
DTS
/8, N=8
1010: f
SAMPLING
=f
DTS
/16, N=5
1011: f
SAMPLING
=f
DTS
/16, N=6
1100: f
SAMPLING
=f
DTS
/16, N=8
1101: f
SAMPLING
=f
DTS
/32, N=5
1110: f
SAMPLING
=f
DTS
/32, N=6
1111: f
SAMPLING
=f
DTS
/32, N=8
Bits 3:2 IC1PSC: Input capture 1 prescaler
This bit-field defines the ratio of the prescaler acting on CC1 input (IC1).
The prescaler is reset as soon as CC1E=0 (TIMx_CCER register).
00: no prescaler, capture is done each time an edge is detected on the capture input
01: capture is done once every 2 events
10: capture is done once every 4 events
11: capture is done once every 8 events
Advanced-control timers (TIM1&TIM8) RM0008
336/1093 Doc ID 13902 Rev 13
14.4.8 TIM1&TIM8 capture/compare mode register 2 (TIMx_CCMR2)
Address offset: 0x1C
Reset value: 0x0000
Refer to the above CCMR1 register description.
Output compare mode
Bits 1:0 CC1S: Capture/Compare 1 Selection
This bit-field defines the direction of the channel (input/output) as well as the used input.
00: CC1 channel is configured as output
01: CC1 channel is configured as input, IC1 is mapped on TI1
10: CC1 channel is configured as input, IC1 is mapped on TI2
11: CC1 channel is configured as input, IC1 is mapped on TRC. This mode is working only if an
internal trigger input is selected through TS bit (TIMx_SMCR register)
Note: CC1S bits are writable only when the channel is OFF (CC1E = 0 in TIMx_CCER).
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
OC4
CE
OC4M[2:0]
OC4
PE
OC4
FE
CC4S[1:0]
OC3
CE.
OC3M[2:0]
OC3
PE
OC3
FE
CC3S[1:0]
IC4F[3:0] IC4PSC[1:0] IC3F[3:0] IC3PSC[1:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bit 15 OC4CE: Output compare 4 clear enable
Bits 14:12 OC4M: Output compare 4 mode
Bit 11 OC4PE: Output compare 4 preload enable
Bit 10 OC4FE: Output compare 4 fast enable
Bits 9:8 CC4S: Capture/Compare 4 selection
This bit-field defines the direction of the channel (input/output) as well as the used input.
00: CC4 channel is configured as output
01: CC4 channel is configured as input, IC4 is mapped on TI4
10: CC4 channel is configured as input, IC4 is mapped on TI3
11: CC4 channel is configured as input, IC4 is mapped on TRC. This mode is working only if
an internal trigger input is selected through TS bit (TIMx_SMCR register)
Note: CC4S bits are writable only when the channel is OFF (CC4E = 0 in TIMx_CCER).
Bit 7 OC3CE: Output compare 3 clear enable
Bits 6:4 OC3M: Output compare 3 mode
Bit 3 OC3PE: Output compare 3 preload enable
Bit 2 OC3FE: Output compare 3 fast enable
Bits 1:0 CC3S: Capture/Compare 3 selection
This bit-field defines the direction of the channel (input/output) as well as the used input.
00: CC3 channel is configured as output
01: CC3 channel is configured as input, IC3 is mapped on TI3
10: CC3 channel is configured as input, IC3 is mapped on TI4
11: CC3 channel is configured as input, IC3 is mapped on TRC. This mode is working only if
an internal trigger input is selected through TS bit (TIMx_SMCR register)
Note: CC3S bits are writable only when the channel is OFF (CC3E = 0 in TIMx_CCER).
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 337/1093
Input capture mode
14.4.9 TIM1&TIM8 capture/compare enable register (TIMx_CCER)
Address offset: 0x20
Reset value: 0x0000
Bits 15:12 IC4F: Input capture 4 filter
Bits 11:10 IC4PSC: Input capture 4 prescaler
Bits 9:8 CC4S: Capture/Compare 4 selection
This bit-field defines the direction of the channel (input/output) as well as the used input.
00: CC4 channel is configured as output
01: CC4 channel is configured as input, IC4 is mapped on TI4
10: CC4 channel is configured as input, IC4 is mapped on TI3
11: CC4 channel is configured as input, IC4 is mapped on TRC. This mode is working only
if an internal trigger input is selected through TS bit (TIMx_SMCR register)
Note: CC4S bits are writable only when the channel is OFF (CC4E = 0 in TIMx_CCER).
Bits 7:4 IC3F: Input capture 3 filter
Bits 3:2 IC3PSC: Input capture 3 prescaler
Bits 1:0 CC3S: Capture/compare 3 selection
This bit-field defines the direction of the channel (input/output) as well as the used input.
00: CC3 channel is configured as output
01: CC3 channel is configured as input, IC3 is mapped on TI3
10: CC3 channel is configured as input, IC3 is mapped on TI4
11: CC3 channel is configured as input, IC3 is mapped on TRC. This mode is working only
if an internal trigger input is selected through TS bit (TIMx_SMCR register)
Note: CC3S bits are writable only when the channel is OFF (CC3E = 0 in TIMx_CCER).
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
CC4P CC4E CC3NP CC3NE CC3P CC3E CC2NP CC2NE CC2P CC2E CC1NP CC1NE CC1P CC1E
rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:14 Reserved, must be kept at reset value.
Bit 13 CC4P: Capture/Compare 4 output polarity
refer to CC1P description
Bit 12 CC4E: Capture/Compare 4 output enable
refer to CC1E description
Bit 11 CC3NP: Capture/Compare 3 complementary output polarity
refer to CC1NP description
Bit 10 CC3NE: Capture/Compare 3 complementary output enable
refer to CC1NE description
Bit 9 CC3P: Capture/Compare 3 output polarity
refer to CC1P description
Bit 8 CC3E: Capture/Compare 3 output enable
refer to CC1E description
Advanced-control timers (TIM1&TIM8) RM0008
338/1093 Doc ID 13902 Rev 13
Bit 7 CC2NP: Capture/Compare 2 complementary output polarity
refer to CC1NP description
Bit 6 CC2NE: Capture/Compare 2 complementary output enable
refer to CC1NE description
Bit 5 CC2P: Capture/Compare 2 output polarity
refer to CC1P description
Bit 4 CC2E: Capture/Compare 2 output enable
refer to CC1E description
Bit 3 CC1NP: Capture/Compare 1 complementary output polarity
0: OC1N active high.
1: OC1N active low.
Note: This bit is not writable as soon as LOCK level 2 or 3 has been programmed (LOCK bits
in TIMx_BDTR register) and CC1S=00 (the channel is configured in output).
Bit 2 CC1NE: Capture/Compare 1 complementary output enable
0: Off - OC1N is not active. OC1N level is then function of MOE, OSSI, OSSR, OIS1, OIS1N
and CC1E bits.
1: On - OC1N signal is output on the corresponding output pin depending on MOE, OSSI,
OSSR, OIS1, OIS1N and CC1E bits.
Bit 1 CC1P: Capture/Compare 1 output polarity
CC1 channel configured as output:
0: OC1 active high
1: OC1 active low
CC1 channel configured as input:
This bit selects whether IC1 or IC1 is used for trigger or capture operations.
0: non-inverted: capture is done on a rising edge of IC1. When used as external trigger, IC1
is non-inverted.
1: inverted: capture is done on a falling edge of IC1. When used as external trigger, IC1 is
inverted.
This bit is not writable as soon as LOCK level 2 or 3 has been programmed (LOCK bits in
TIMx_BDTR register).
Note:
Bit 0 CC1E: Capture/Compare 1 output enable
CC1 channel configured as output:
0: Off - OC1 is not active. OC1 level is then function of MOE, OSSI, OSSR, OIS1, OIS1N
and CC1NE bits.
1: On - OC1 signal is output on the corresponding output pin depending on MOE, OSSI,
OSSR, OIS1, OIS1N and CC1NE bits.
CC1 channel configured as input:
This bit determines if a capture of the counter value can actually be done into the input
capture/compare register 1 (TIMx_CCR1) or not.
0: Capture disabled.
1: Capture enabled.
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 339/1093
Note: The state of the external I/O pins connected to the complementary OCx and OCxN
channels depends on the OCx and OCxN channel state and the GPIOand AFIO registers.
Table 83. Output control bits for complementary OCx and OCxN channels with
break feature
Control bits
Output states
(1)
1. When both outputs of a channel are not used (CCxE = CCxNE = 0), the OISx, OISxN, CCxP and CCxNP
bits must be kept cleared.
MOE
bit
OSSI
bit
OSSR
bit
CCxE
bit
CCxNE
bit
OCx output state OCxN output state
1 X
0 0 0
Output Disabled (not
driven by the timer)
OCx=0, OCx_EN=0
Output Disabled (not driven by
the timer)
OCxN=0, OCxN_EN=0
0 0 1
Output Disabled (not
driven by the timer)
OCx=0, OCx_EN=0
OCxREF + Polarity
OCxN=OCxREF xor CCxNP,
OCxN_EN=1
0 1 0
OCxREF + Polarity
OCx=OCxREF xor CCxP,
OCx_EN=1
Output Disabled (not driven by
the timer)
OCxN=0, OCxN_EN=0
0 1 1
OCREF + Polarity + dead-
time
OCx_EN=1
Complementary to OCREF (not
OCREF) + Polarity + dead-time
OCxN_EN=1
1 0 0
Output Disabled (not
driven by the timer)
OCx=CCxP, OCx_EN=0
Output Disabled (not driven by
the timer)
OCxN=CCxNP, OCxN_EN=0
1 0 1
Off-State (output enabled
with inactive state)
OCx=CCxP, OCx_EN=1
OCxREF + Polarity
OCxN=OCxREF xor CCxNP,
OCxN_EN=1
1 1 0
OCxREF + Polarity
OCx=OCxREF xor CCxP,
OCx_EN=1
Off-State (output enabled with
inactive state)
OCxN=CCxNP, OCxN_EN=1
1 1 1
OCREF + Polarity + dead-
time
OCx_EN=1
Complementary to OCREF (not
OCREF) + Polarity + dead-time
OCxN_EN=1
0
0
X
0 0
Output Disabled (not driven by the timer)
Asynchronously: OCx=CCxP, OCx_EN=0, OCxN=CCxNP,
OCxN_EN=0
Then if the clock is present: OCx=OISx and OCxN=OISxN
after a dead-time, assuming that OISx and OISxN do not
correspond to OCX and OCxN both in active state.
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1 Off-State (output enabled with inactive state)
Asynchronously: OCx=CCxP, OCx_EN=1, OCxN=CCxNP,
OCxN_EN=1
Then if the clock is present: OCx=OISx and OCxN=OISxN
after a dead-time, assuming that OISx and OISxN do not
correspond to OCX and OCxN both in active state
1 1 0
1 1 1
Advanced-control timers (TIM1&TIM8) RM0008
340/1093 Doc ID 13902 Rev 13
14.4.10 TIM1&TIM8 counter (TIMx_CNT)
Address offset: 0x24
Reset value: 0x0000
14.4.11 TIM1&TIM8 prescaler (TIMx_PSC)
Address offset: 0x28
Reset value: 0x0000
14.4.12 TIM1&TIM8 auto-reload register (TIMx_ARR)
Address offset: 0x2C
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CNT[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 CNT[15:0]: Counter value
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
PSC[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 PSC[15:0]: Prescaler value
The counter clock frequency (CK_CNT) is equal to f
CK_PSC
/ (PSC[15:0] + 1).
PSC contains the value to be loaded in the active prescaler register at each update event
(including when the counter is cleared through UG bit of TIMx_EGR register or through trigger
controller when configured in reset mode).
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
ARR[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 ARR[15:0]: Prescaler value
ARR is the value to be loaded in the actual auto-reload register.
Refer to the Section 14.3.1: Time-base unit on page 285 for more details about ARR update
and behavior.
The counter is blocked while the auto-reload value is null.
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 341/1093
14.4.13 TIM1&TIM8 repetition counter register (TIMx_RCR)
Address offset: 0x30
Reset value: 0x0000
14.4.14 TIM1&TIM8 capture/compare register 1 (TIMx_CCR1)
Address offset: 0x34
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
REP[7:0]
rw rw rw rw rw rw rw rw
Bits 15:8 Reserved, must be kept at reset value.
Bits 7:0 REP[7:0]: Repetition counter value
These bits allow the user to set-up the update rate of the compare registers (i.e. periodic
transfers from preload to active registers) when preload registers are enable, as well as the
update interrupt generation rate, if this interrupt is enable.
Each time the REP_CNT related downcounter reaches zero, an update event is generated and
it restarts counting from REP value. As REP_CNT is reloaded with REP value only at the
repetition update event U_RC, any write to the TIMx_RCR register is not taken in account until
the next repetition update event.
It means in PWM mode (REP+1) corresponds to:
the number of PWM periods in edge-aligned mode
the number of half PWM period in center-aligned mode.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CCR1[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 CCR1[15:0]: Capture/Compare 1 value
If channel CC1 is configured as output:
CCR1 is the value to be loaded in the actual capture/compare 1 register (preload value).
It is loaded permanently if the preload feature is not selected in the TIMx_CCMR1 register (bit
OC1PE). Else the preload value is copied in the active capture/compare 1 register when an
update event occurs.
The active capture/compare register contains the value to be compared to the counter
TIMx_CNT and signaled on OC1 output.
If channel CC1 is configured as input:
CCR1 is the counter value transferred by the last input capture 1 event (IC1).
Advanced-control timers (TIM1&TIM8) RM0008
342/1093 Doc ID 13902 Rev 13
14.4.15 TIM1&TIM8 capture/compare register 2 (TIMx_CCR2)
Address offset: 0x38
Reset value: 0x0000
14.4.16 TIM1&TIM8 capture/compare register 3 (TIMx_CCR3)
Address offset: 0x3C
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CCR2[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 CCR2[15:0]: Capture/Compare 2 value
If channel CC2 is configured as output:
CCR2 is the value to be loaded in the actual capture/compare 2 register (preload value).
It is loaded permanently if the preload feature is not selected in the TIMx_CCMR2 register (bit
OC2PE). Else the preload value is copied in the active capture/compare 2 register when an
update event occurs.
The active capture/compare register contains the value to be compared to the counter
TIMx_CNT and signalled on OC2 output.
If channel CC2 is configured as input:
CCR2 is the counter value transferred by the last input capture 2 event (IC2).
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CCR3[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 CCR3[15:0]: Capture/Compare value
If channel CC3 is configured as output:
CCR3 is the value to be loaded in the actual capture/compare 3 register (preload value).
It is loaded permanently if the preload feature is not selected in the TIMx_CCMR3 register (bit
OC3PE). Else the preload value is copied in the active capture/compare 3 register when an
update event occurs.
The active capture/compare register contains the value to be compared to the counter
TIMx_CNT and signalled on OC3 output.
If channel CC3 is configured as input:
CCR3 is the counter value transferred by the last input capture 3 event (IC3).
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 343/1093
14.4.17 TIM1&TIM8 capture/compare register 4 (TIMx_CCR4)
Address offset: 0x40
Reset value: 0x0000
14.4.18 TIM1&TIM8 break and dead-time register (TIMx_BDTR)
Address offset: 0x44
Reset value: 0x0000
Note: As the bits AOE, BKP, BKE, OSSI, OSSR and DTG[7:0] can be write-locked depending on
the LOCK configuration, it can be necessary to configure all of them during the first write
access to the TIMx_BDTR register.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CCR4[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 CCR4[15:0]: Capture/Compare value
If channel CC4 is configured as output:
CCR4 is the value to be loaded in the actual capture/compare 4 register (preload value).
It is loaded permanently if the preload feature is not selected in the TIMx_CCMR4 register (bit
OC4PE). Else the preload value is copied in the active capture/compare 4 register when an
update event occurs.
The active capture/compare register contains the value to be compared to the counter
TIMx_CNT and signalled on OC4 output.
If channel CC4 is configured as input:
CCR4 is the counter value transferred by the last input capture 4 event (IC4).
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
MOE AOE BKP BKE OSSR OSSI LOCK[1:0] DTG[7:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bit 15 MOE: Main output enable
This bit is cleared asynchronously by hardware as soon as the break input is active. It is set
by software or automatically depending on the AOE bit. It is acting only on the channels
which are configured in output.
0: OC and OCN outputs are disabled or forced to idle state.
1: OC and OCN outputs are enabled if their respective enable bits are set (CCxE, CCxNE in
TIMx_CCER register).
See OC/OCN enable description for more details (Section 14.4.9: TIM1&TIM8
capture/compare enable register (TIMx_CCER) on page 337).
Bit 14 AOE: Automatic output enable
0: MOE can be set only by software
1: MOE can be set by software or automatically at the next update event (if the break input is
not be active)
Note: This bit can not be modified as long as LOCK level 1 has been programmed (LOCK bits
in TIMx_BDTR register).
Advanced-control timers (TIM1&TIM8) RM0008
344/1093 Doc ID 13902 Rev 13
Bit 13 BKP: Break polarity
0: Break input BRK is active low
1: Break input BRK is active high
Note: This bit can not be modified as long as LOCK level 1 has been programmed (LOCK bits
in TIMx_BDTR register).
Note: Any write operation to this bit takes a delay of 1 APB clock cycle to become effective.
Bit 12 BKE: Break enable
0: Break inputs (BRK and CCS clock failure event) disabled
1; Break inputs (BRK and CCS clock failure event) enabled
Note: This bit cannot be modified when LOCK level 1 has been programmed (LOCK bits in
TIMx_BDTR register).
Note: Any write operation to this bit takes a delay of 1 APB clock cycle to become effective.
Bit 11 OSSR: Off-state selection for Run mode
This bit is used when MOE=1 on channels having a complementary output which are
configured as outputs. OSSR is not implemented if no complementary output is implemented
in the timer.
See OC/OCN enable description for more details (Section 14.4.9: TIM1&TIM8
capture/compare enable register (TIMx_CCER) on page 337).
0: When inactive, OC/OCN outputs are disabled (OC/OCN enable output signal=0).
1: When inactive, OC/OCN outputs are enabled with their inactive level as soon as CCxE=1
or CCxNE=1. Then, OC/OCN enable output signal=1
Note: This bit can not be modified as soon as the LOCK level 2 has been programmed (LOCK
bits in TIMx_BDTR register).
Bit 10 OSSI: Off-state selection for Idle mode
This bit is used when MOE=0 on channels configured as outputs.
See OC/OCN enable description for more details (Section 14.4.9: TIM1&TIM8
capture/compare enable register (TIMx_CCER) on page 337).
0: When inactive, OC/OCN outputs are disabled (OC/OCN enable output signal=0).
1: When inactive, OC/OCN outputs are forced first with their idle level as soon as CCxE=1 or
CCxNE=1. OC/OCN enable output signal=1)
Note: This bit can not be modified as soon as the LOCK level 2 has been programmed (LOCK
bits in TIMx_BDTR register).
Bits 9:8 LOCK[1:0]: Lock configuration
These bits offer a write protection against software errors.
00: LOCK OFF - No bit is write protected.
01: LOCK Level 1 = DTG bits in TIMx_BDTR register, OISx and OISxN bits in TIMx_CR2
register and BKE/BKP/AOE bits in TIMx_BDTR register can no longer be written.
10: LOCK Level 2 = LOCK Level 1 + CC Polarity bits (CCxP/CCxNP bits in TIMx_CCER
register, as long as the related channel is configured in output through the CCxS bits) as well
as OSSR and OSSI bits can no longer be written.
11: LOCK Level 3 = LOCK Level 2 + CC Control bits (OCxM and OCxPE bits in
TIMx_CCMRx registers, as long as the related channel is configured in output through the
CCxS bits) can no longer be written.
Note: The LOCK bits can be written only once after the reset. Once the TIMx_BDTR register
has been written, their content is frozen until the next reset.
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 345/1093
14.4.19 TIM1&TIM8 DMA control register (TIMx_DCR)
Address offset: 0x48
Reset value: 0x0000
Bits 7:0 DTG[7:0]: Dead-time generator setup
This bit-field defines the duration of the dead-time inserted between the complementary
outputs. DT correspond to this duration.
DTG[7:5]=0xx => DT=DTG[7:0]x t
dtg
with t
dtg
=t
DTS
.
DTG[7:5]=10x => DT=(64+DTG[5:0])xt
dtg
with T
dtg
=2xt
DTS
.
DTG[7:5]=110 => DT=(32+DTG[4:0])xt
dtg
with T
dtg
=8xt
DTS
.
DTG[7:5]=111 => DT=(32+DTG[4:0])xt
dtg
with T
dtg
=16xt
DTS
.
Example if T
DTS
=125ns (8MHz), dead-time possible values are:
0 to 15875 ns by 125 ns steps,
16 us to 31750 ns by 250 ns steps,
32 us to 63us by 1 us steps,
64 us to 126 us by 2 us steps
Note: This bit-field can not be modified as long as LOCK level 1, 2 or 3 has been programmed
(LOCK bits in TIMx_BDTR register).
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
DBL[4:0]
Reserved
DBA[4:0]
rw rw rw rw rw rw rw rw rw rw
Bits 15:13 Reserved, always read as 0
Bits 12:8 DBL[4:0]: DMA burst length
This 5-bit vector defines the number of DMA transfers (the timer recognizes a burst transfer
when a read or a write access is done t
the TIMx_DMAR address)
00000: 1 transfer
00001: 2 transfers
00010: 3 transfers
...
10001: 18 transfers
Bits 7:5 Reserved, always read as 0
Bits 4:0 DBA[4:0]: DMA base address
This 5-bits vector defines the base-address for DMA transfers (when read/write access are
done through the TIMx_DMAR address). DBA is defined as an offset starting from the
address of the TIMx_CR1 register.
Example:
00000: TIMx_CR1,
00001: TIMx_CR2,
00010: TIMx_SMCR,
...
Example: Let us consider the following transfer: DBL = 7 transfers and DBA = TIMx_CR1. In
this case the transfer is done to/from 7 registers starting from the TIMx_CR1 address.
Advanced-control timers (TIM1&TIM8) RM0008
346/1093 Doc ID 13902 Rev 13
14.4.20 TIM1&TIM8 DMA address for full transfer (TIMx_DMAR)
Address offset: 0x4C
Reset value: 0x0000
Example of how to use the DMA burst feature
In this example the timer DMA burst feature is used to update the contents of the CCRx
registers (x = 2, 3, 4) with the DMA transferring half words into the CCRx registers.
This is done in the following steps:
1. Configure the corresponding DMA channel as follows:
DMA channel peripheral address is the DMAR register address
DMA channel memory address is the address of the buffer in the RAM containing
the data to be transferred by DMA into CCRx registers.
Number of data to transfer = 3 (See note below).
Circular mode disabled.
2. Configure the DCR register by configuring the DBA and DBL bit fields as follows:
DBL = 3 transfers, DBA = 0xE.
3. Enable the TIMx update DMA request (set the UDE bit in the DIER register).
4. Enable TIMx
5. Enable the DMA channel
Note: This example is for the case where every CCRx register to be updated once. If every CCRx
register is to be updated twice for example, the number of data to transfer should be 6. Let's
take the example of a buffer in the RAM containing data1, data2, data3, data4, data5 and
data6. The data is transferred to the CCRx registers as follows: on the first update DMA
request, data1 is transferred to CCR2, data2 is transferred to CCR3, data3 is transferred to
CCR4 and on the second update DMA request, data4 is transferred to CCR2, data5 is
transferred to CCR3 and data6 is transferred to CCR4.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
DMAB[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 DMAB[15:0]: DMA register for burst accesses
A read or write operation to the DMAR register accesses the register located at the address
(TIMx_CR1 address) + (DBA + DMA index) x 4
where TIMx_CR1 address is the address of the control register 1, DBA is the DMA base
address configured in TIMx_DCR register, DMA index is automatically controlled by the DMA
transfer, and ranges from 0 to DBL (DBL configured in TIMx_DCR).
RM0008 Advanced-control timers (TIM1&TIM8)
Doc ID 13902 Rev 13 347/1093
14.4.21 TIM1&TIM8 register map
TIM1&TIM8 registers are mapped as 16-bit addressable registers as described in the table
below:
Table 84. TIM1&TIM8 register map and reset values
Offset Register
3
1
3
0
2
9
2
8
2
7
2
6
2
5
2
4
2
3
2
2
2
1
2
0
1
9
1
8
1
7
1
6
1
5
1
4
1
3
1
2
1
1
1
0
9 8 7 6 5 4 3 2 1 0
0x00
TIMx_CR1
Reserved
CKD
[1:0]
A
R
P
E
CMS
[1:0] D
I
R
O
P
M
U
R
S
U
D
I
S
C
E
N
Reset value 0 0 0 0 0 0 0 0 0 0
0x04
TIMx_CR2
Reserved
O
I
S
4
O
I
S
3
N
O
I
S
3
O
I
S
2
N
O
I
S
2
O
I
S
1
N
O
I
S
1
T
I
1
S
MMS[2:0]
C
C
D
S
C
C
U
S
R
e
s
e
r
v
e
d
C
C
P
C
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x08
TIMx_SMCR
Reserved
E
T
P
E
C
E ETPS
[1:0]
ETF[3:0]
M
S
M
TS[2:0]
R
e
s
e
r
v
e
d
SMS[2:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x0C
TIMx_DIER
Reserved
T
D
E
C
O
M
D
E
C
C
4
D
E
C
C
3
D
E
C
C
2
D
E
C
C
1
D
E
U
D
E
B
I
E
T
I
E
C
O
M
I
E
C
C
4
I
E
C
C
3
I
E
C
C
2
I
E
C
C
1
I
E
U
I
E
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x10
TIMx_SR
Reserved
C
C
4
O
F
C
C
3
O
F
C
C
2
O
F
C
C
1
O
F
R
e
s
e
r
v
e
d
B
I
F
T
I
F
C
O
M
I
F
C
C
4
I
F
C
C
3
I
F
C
C
2
I
F
C
C
1
I
F
U
I
F
Reset value 0 0 0 0 0 0 0 0 0 0 0 0
0x14
TIMx_EGR
Reserved B
G
T
G
C
O
M
C
C
4
G
C
C
3
G
C
C
2
G
C
C
1
G
U
G
Reset value 0 0 0 0 0 0 0 0
0x18
TIMx_CCMR1
Output Compare
mode
Reserved
O
C
2
C
E
OC2M
[2:0]
O
C
2
P
E
O
C
2
F
E
CC2S
[1:0]
O
C
1
C
E
OC1M
[2:0]
O
C
1
P
E
O
C
1
F
E
CC1S
[1:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIMx_CCMR1
Input Capture
mode
Reserved
IC2F[3:0]
IC2
PSC
[1:0]
CC2S
[1:0]
IC1F[3:0]
IC1
PSC
[1:0]
CC1S
[1:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x1C
TIMx_CCMR2
Output Compare
mode
Reserved
O
2
4
C
E
OC4M
[2:0]
O
C
4
P
E
O
C
4
F
E
CC4S
[1:0]
O
C
3
C
E
OC3M
[2:0]
O
C
3
P
E
O
C
3
F
E
CC3S
[1:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIMx_CCMR2
Input Capture
mode
Reserved
IC4F[3:0]
IC4
PSC
[1:0]
CC4S
[1:0]
IC3F[3:0]
IC3
PSC
[1:0]
CC3S
[1:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x20
TIMx_CCER
Reserved
C
C
4
P
C
C
4
E
C
C
3
N
P
C
C
3
N
E
C
C
3
P
C
C
3
E
C
C
2
N
P
C
C
2
N
E
C
C
2
P
C
C
2
E
C
C
1
N
P
C
C
1
N
E
C
C
1
P
C
C
1
E
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x24
TIMx_CNT
Reserved
CNT[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x28
TIMx_PSC
Reserved
PSC[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x2C
TIMx_ARR
Reserved
ARR[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x30
TIMx_RCR
Reserved
REP[7:0]
Reset value 0 0 0 0 0 0 0 0
Advanced-control timers (TIM1&TIM8) RM0008
348/1093 Doc ID 13902 Rev 13
Refer to Table 3 on page 50 for the register boundary addresses.
0x34
TIMx_CCR1
Reserved
CCR1[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x38
TIMx_CCR2
Reserved
CCR2[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x3C
TIMx_CCR3
Reserved
CCR3[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x40
TIMx_CCR4
Reserved
CCR4[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x44
TIMx_BDTR
Reserved
M
O
E
A
O
E
B
K
P
B
K
E
O
S
S
R
O
S
S
I
LOCK
[1:0]
DT[7:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x48
TIMx_DCR
Reserved
DBL[4:0]
Reserved
DBA[4:0]
Reset value 0 0 0 0 0 0 0 0 0 0
0x4C
TIMx_DMAR
Reserved
DMAB[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Table 84. TIM1&TIM8 register map and reset values (continued)
Offset Register
3
1
3
0
2
9
2
8
2
7
2
6
2
5
2
4
2
3
2
2
2
1
2
0
1
9
1
8
1
7
1
6
1
5
1
4
1
3
1
2
1
1
1
0
9 8 7 6 5 4 3 2 1 0
RM0008 General-purpose timers (TIM2 to TIM5)
Doc ID 13902 Rev 13 349/1093
15 General-purpose timers (TIM2 to TIM5)
Low-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 16 and 32 Kbytes.
Medium-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 64 and 128 Kbytes.
High-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 256 and 512 Kbytes.
XL-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 768 Kbytes and 1 Mbyte.
Connectivity line devices are STM32F105xx and STM32F107xx microcontrollers.
This Section applies to the whole STM32F10xxx family, unless otherwise specified.
15.1 TIM2 to TIM5 introduction
The general-purpose timers consist of a 16-bit auto-reload counter driven by a
programmable prescaler.
They may be used for a variety of purposes, including measuring the pulse lengths of input
signals (input capture) or generating output waveforms (output compare and PWM).
Pulse lengths and waveform periods can be modulated from a few microseconds to several
milliseconds using the timer prescaler and the RCC clock controller prescalers.
The timers are completely independent, and do not share any resources. They can be
synchronized together as described in Section 15.3.15.
General-purpose timers (TIM2 to TIM5) RM0008
350/1093 Doc ID 13902 Rev 13
15.2 TIMx main features
General-purpose TIMx timer features include:
16-bit up, down, up/down auto-reload counter.
16-bit programmable prescaler used to divide (also on the fly) the counter clock
frequency by any factor between 1 and 65535.
Up to 4 independent channels for:
Input capture
Output compare
PWM generation (Edge- and Center-aligned modes)
One-pulse mode output
Synchronization circuit to control the timer with external signals and to interconnect
several timers.
Interrupt/DMA generation on the following events:
Update: counter overflow/underflow, counter initialization (by software or
internal/external trigger)
Trigger event (counter start, stop, initialization or count by internal/external trigger)
Input capture
Output compare
Supports incremental (quadrature) encoder and hall-sensor circuitry for positioning
purposes
Trigger input for external clock or cycle-by-cycle current management
RM0008 General-purpose timers (TIM2 to TIM5)
Doc ID 13902 Rev 13 351/1093
Figure 100. General-purpose timer block diagram
15.3 TIMx functional description
15.3.1 Time-base unit
The main block of the programmable timer is a 16-bit counter with its related auto-reload
register. The counter can count up, down or both up and down. The counter clock can be
divided by a prescaler.
The counter, the auto-reload register and the prescaler register can be written or read by
software. This is true even when the counter is running.
The time-base unit includes:
Counter Register (TIMx_CNT)
Prescaler Register (TIMx_PSC):
Auto-Reload Register (TIMx_ARR)
Autoreload register
Capture/compare 1 register
Capture/compare 2 register
U
U
U
CC1I
CC2I
Trigger
controller
Stop, clear or up/down
TI1FP1
TI2FP2
ITR0
ITR1
ITR2
ITR3
TRGI
Encoder
Interface
Capture/compare 3 register
U
CC3I
output
control
OC1
TRGO
OC1REF
OC2REF
OC3REF
U
UI
Reset, enable, up/down, count,
Capture/compare 4 register
U
CC4I
OC4REF
Prescaler
Prescaler
IC4PS
IC3PS
IC1
IC2
Prescaler
Prescaler
Input filter &
edge detector
IC2PS
IC1PS
TI1FP1
OC2
OC3
OC4
Reg
event
Notes:
Preload registers transferred
to active registers on U event
according to control bit
interrupt & DMA output
TGI
TRC
TRC
IC3
IC4
ITR
TRC
TI1F_ED
Input filter &
edge detector
Input filter &
edge detector
Input filter &
edge detector
CC1I
CC2I
CC3I
CC4I
TI1FP2
TI2FP1
TI2FP2
TI3FP3
TRC
TRC
TI3FP4
TI4FP3
TI4FP4
TI4
TI3
TI1
TI2
XOR
TIMx_CH1
TIMx_CH2
TIMx_CH3
TIMx_CH4
TIMx_CH1
TIMx_CH2
TIMx_CH3
TIMx_CH4
to other timers
TIMxCLK from RCC
Prescaler counter
+/-
CK_PSC
PSC CNT CK_CNT
controller
mode
Slave
Internal Clock (CK_INT)
ETR
Input filter
Polarity selection & edge
detector & prescaler
ETRP
ETRF
TIMx_ETR
ETRF
to DAC/ADC
output
control
output
control
output
control
General-purpose timers (TIM2 to TIM5) RM0008
352/1093 Doc ID 13902 Rev 13
The auto-reload register is preloaded. Writing to or reading from the auto-reload register
accesses the preload register. The content of the preload register are transferred into the
shadow register permanently or at each update event (UEV), depending on the auto-reload
preload enable bit (ARPE) in TIMx_CR1 register. The update event is sent when the counter
reaches the overflow (or underflow when downcounting) and if the UDIS bit equals 0 in the
TIMx_CR1 register. It can also be generated by software. The generation of the update
event is described in detail for each configuration.
The counter is clocked by the prescaler output CK_CNT, which is enabled only when the
counter enable bit (CEN) in TIMx_CR1 register is set (refer also to the slave mode controller
description to get more details on counter enabling).
Note that the actual counter enable signal CNT_EN is set 1 clock cycle after CEN.
Prescaler description
The prescaler can divide the counter clock frequency by any factor between 1 and 65536. It
is based on a 16-bit counter controlled through a 16-bit register (in the TIMx_PSC register).
It can be changed on the fly as this control register is buffered. The new prescaler ratio is
taken into account at the next update event.
Figure 101 and Figure 102 give some examples of the counter behavior when the prescaler
ratio is changed on the fly:
Figure 101. Counter timing diagram with prescaler division change from 1 to 2
CK_PSC
00
CNT_EN
Timer clock = CK_CNT
Counter register
Update event (UEV)
0
F9 FA FB FC F7
Prescaler control register 0 1
Write a new value in TIMx_PSC
01 02 03
Prescaler buffer 0 1
Prescaler counter 0 1 0 1 0 1 0 1
F8
RM0008 General-purpose timers (TIM2 to TIM5)
Doc ID 13902 Rev 13 353/1093
Figure 102. Counter timing diagram with prescaler division change from 1 to 4
15.3.2 Counter modes
Upcounting mode
In upcounting mode, the counter counts from 0 to the auto-reload value (content of the
TIMx_ARR register), then restarts from 0 and generates a counter overflow event.
An Update event can be generated at each counter overflow or by setting the UG bit in the
TIMx_EGR register (by software or by using the slave mode controller).
The UEV event can be disabled by software by setting the UDIS bit in TIMx_CR1 register.
This is to avoid updating the shadow registers while writing new values in the preload
registers. Then no update event occurs until the UDIS bit has been written to 0. However,
the counter restarts from 0, as well as the counter of the prescaler (but the prescale rate
does not change). In addition, if the URS bit (update request selection) in TIMx_CR1
register is set, setting the UG bit generates an update event UEV but without setting the UIF
flag (thus no interrupt or DMA request is sent). This is to avoid generating both update and
capture interrupts when clearing the counter on the capture event.
When an update event occurs, all the registers are updated and the update flag (UIF bit in
TIMx_SR register) is set (depending on the URS bit):
The buffer of the prescaler is reloaded with the preload value (content of the TIMx_PSC
register)
The auto-reload shadow register is updated with the preload value (TIMx_ARR)
The following figures show some examples of the counter behavior for different clock
frequencies when TIMx_ARR=0x36.
CK_PSC
00
CNT_EN
Timer clock = CK_CNT
Counter register
Update event (UEV)
0
F9 FA FB FC F7
Prescaler control register 0 3
Write a new value in TIMx_PSC
Prescaler buffer 0 3
Prescaler counter 0 1 2 3 0 1 2 3
F8 01
General-purpose timers (TIM2 to TIM5) RM0008
354/1093 Doc ID 13902 Rev 13
Figure 103. Counter timing diagram, internal clock divided by 1
Figure 104. Counter timing diagram, internal clock divided by 2
Figure 105. Counter timing diagram, internal clock divided by 4
CK_INT
00
CNT_EN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
Counter overflow
Update event (UEV)
01 02 03 04 05 06 07 32 33 34 35 36 31
CK_INT
0035 0000 0001 0002 0003
CNT_EN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
0034 0036
Counter overflow
Update event (UEV)
0000 0001
CNT_EN
TImer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
0035 0036
Counter overflow
Update event (UEV)
CK_INT
RM0008 General-purpose timers (TIM2 to TIM5)
Doc ID 13902 Rev 13 355/1093
Figure 106. Counter timing diagram, internal clock divided by N
Figure 107. Counter timing diagram, Update event when ARPE=0 (TIMx_ARR not
preloaded)
Timer clock = CK_CNT
Counter register 00 1F 20
Update interrupt flag (UIF)
Counter overflow
Update event (UEV)
CK_INT
00
CNT_EN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
Counter overflow
Update event (UEV)
01 02 03 04 05 06 07 32 33 34 35 36 31
Auto-reload register FF 36
Write a new value in TIMx_ARR
CK_INT
General-purpose timers (TIM2 to TIM5) RM0008
356/1093 Doc ID 13902 Rev 13
Figure 108. Counter timing diagram, Update event when ARPE=1 (TIMx_ARR
preloaded)
Downcounting mode
In downcounting mode, the counter counts from the auto-reload value (content of the
TIMx_ARR register) down to 0, then restarts from the auto-reload value and generates a
counter underflow event.
An Update event can be generate at each counter underflow or by setting the UG bit in the
TIMx_EGR register (by software or by using the slave mode controller)
The UEV update event can be disabled by software by setting the UDIS bit in TIMx_CR1
register. This is to avoid updating the shadow registers while writing new values in the
preload registers. Then no update event occurs until UDIS bit has been written to 0.
However, the counter restarts from the current auto-reload value, whereas the counter of the
prescaler restarts from 0 (but the prescale rate doesnt change).
In addition, if the URS bit (update request selection) in TIMx_CR1 register is set, setting the
UG bit generates an update event UEV but without setting the UIF flag (thus no interrupt or
DMA request is sent). This is to avoid generating both update and capture interrupts when
clearing the counter on the capture event.
When an update event occurs, all the registers are updated and the update flag (UIF bit in
TIMx_SR register) is set (depending on the URS bit):
The buffer of the prescaler is reloaded with the preload value (content of the TIMx_PSC
register).
The auto-reload active register is updated with the preload value (content of the
TIMx_ARR register). Note that the auto-reload is updated before the counter is
reloaded, so that the next period is the expected one.
The following figures show some examples of the counter behavior for different clock
frequencies when TIMx_ARR=0x36.
00
CNT_EN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
Counter overflow
Update event (UEV)
01 02 03 04 05 06 07 F1 F2 F3 F4 F5 F0
Auto-reload preload register F5 36
Auto-reload shadow register F5 36
Write a new value in TIMx_ARR
CK_PSC
RM0008 General-purpose timers (TIM2 to TIM5)
Doc ID 13902 Rev 13 357/1093
Figure 109. Counter timing diagram, internal clock divided by 1
Figure 110. Counter timing diagram, internal clock divided by 2
Figure 111. Counter timing diagram, internal clock divided by 4
CK_INT
36
CNT_EN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
Counter underflow (cnt_udf)
Update event (UEV)
35 34 33 32 31 30 2F 04 03 02 01 00 05
CK_INT
0001 0036 0035 0034 0033
CNT_EN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
0002 0000
Counter underflow
Update event (UEV)
0036 0035
CNT_EN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
0001 0000
Counter underflow
Update event (UEV)
CK_INT
General-purpose timers (TIM2 to TIM5) RM0008
358/1093 Doc ID 13902 Rev 13
Figure 112. Counter timing diagram, internal clock divided by N
Figure 113. Counter timing diagram, Update event when repetition counter is not
used
Center-aligned mode (up/down counting)
In center-aligned mode, the counter counts from 0 to the auto-reload value (content of the
TIMx_ARR register) 1, generates a counter overflow event, then counts from the auto-
reload value down to 1 and generates a counter underflow event. Then it restarts counting
from 0.
Center-aligned mode is active when the CMS bits in TIMx_CR1 register are not equal to
'00'. The Output compare interrupt flag of channels configured in output is set when: the
counter counts down (Center aligned mode 1, CMS = "01"), the counter counts up (Center
aligned mode 2, CMS = "10") the counter counts up and down (Center aligned mode 3,
CMS = "11").
In this mode, the direction bit (DIR from TIMx_CR1 register) cannot be written. It is updated
by hardware and gives the current direction of the counter.
The update event can be generated at each counter overflow and at each counter underflow
or by setting the UG bit in the TIMx_EGR register (by software or by using the slave mode
controller) also generates an update event. In this case, the counter restarts counting from
0, as well as the counter of the prescaler.
Timer clock = CK_CNT
Counter register 36 20 1F
Update interrupt flag (UIF)
Counter underflow
Update event (UEV)
CK_INT
00
36
CNT_EN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
Counter underflow
Update event (UEV)
35 34 33 32 31 30 2F 04 03 02 01 00 05
Auto-reload register FF 36
Write a new value in TIMx_ARR
CK_INT
RM0008 General-purpose timers (TIM2 to TIM5)
Doc ID 13902 Rev 13 359/1093
The UEV update event can be disabled by software by setting the UDIS bit in TIMx_CR1
register. This is to avoid updating the shadow registers while writing new values in the
preload registers. Then no update event occurs until the UDIS bit has been written to 0.
However, the counter continues counting up and down, based on the current auto-reload
value.
In addition, if the URS bit (update request selection) in TIMx_CR1 register is set, setting the
UG bit generates an update event UEV but without setting the UIF flag (thus no interrupt or
DMA request is sent). This is to avoid generating both update and capture interrupt when
clearing the counter on the capture event.
When an update event occurs, all the registers are updated and the update flag (UIF bit in
TIMx_SR register) is set (depending on the URS bit):
The buffer of the prescaler is reloaded with the preload value (content of the TIMx_PSC
register).
The auto-reload active register is updated with the preload value (content of the
TIMx_ARR register). Note that if the update source is a counter overflow, the auto-
reload is updated before the counter is reloaded, so that the next period is the expected
one (the counter is loaded with the new value).
The following figures show some examples of the counter behavior for different clock
frequencies.
Figure 114. Counter timing diagram, internal clock divided by 1, TIMx_ARR=0x6
1. Here, center-aligned mode 1 is used (for more details refer to Section 15.4.1: TIMx control register 1 (TIMx_CR1) on
page 386).
CK_INT
02
CNT_EN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
Counter underflow
Update event (UEV)
03 04 05 06 05 04 03 03 02 01 00 01 04
Counter overflow
General-purpose timers (TIM2 to TIM5) RM0008
360/1093 Doc ID 13902 Rev 13
Figure 115. Counter timing diagram, internal clock divided by 2
Figure 116. Counter timing diagram, internal clock divided by 4, TIMx_ARR=0x36
1. Center-aligned mode 2 or 3 is used with an UIF on overflow.
Figure 117. Counter timing diagram, internal clock divided by N
0002 0000 0001 0002 0003
CNT_EN
TImer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
0003 0001
Counter underflow
Update event (UEV)
CK_INT
CK_INT
0036 0035
CNT_EN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
0034 0035
Counter overflow (cnt_ovf)
Update event (UEV)
Timer clock = CK_CNT
Counter register 00 20 1F
Update interrupt flag (UIF)
Counter underflow
Update event (UEV)
CK_INT
01
RM0008 General-purpose timers (TIM2 to TIM5)
Doc ID 13902 Rev 13 361/1093
Figure 118. Counter timing diagram, Update event with ARPE=1 (counter underflow)
Figure 119. Counter timing diagram, Update event with ARPE=1 (counter overflow)
00
CNT_EN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
Counter underflow
Update event (UEV)
01 02 03 04 05 06 07 05 04 03 02 01 06
Auto-reload preload register FD 36
Write a new value in TIMx_ARR
Auto-reload active register FD 36
CK_INT
36
CNT_EN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
Counter overflow
Update event (UEV)
35 34 33 32 31 30 2F F8 F9 FA FB FC F7
Auto-reload preload register FD 36
Write a new value in TIMx_ARR
Auto-reload active register FD 36
CK_INT
General-purpose timers (TIM2 to TIM5) RM0008
362/1093 Doc ID 13902 Rev 13
15.3.3 Clock selection
The counter clock can be provided by the following clock sources:
Internal clock (CK_INT)
External clock mode1: external input pin (TIx)
External clock mode2: external trigger input (ETR)
Internal trigger inputs (ITRx): using one timer as prescaler for another timer, for
example, you can configure Timer 1 to act as a prescaler for Timer 2. Refer to : Using
one timer as prescaler for another on page 381 for more details.
Internal clock source (CK_INT)
If the slave mode controller is disabled (SMS=000 in the TIMx_SMCR register), then the
CEN, DIR (in the TIMx_CR1 register) and UG bits (in the TIMx_EGR register) are actual
control bits and can be changed only by software (except UG which remains cleared
automatically). As soon as the CEN bit is written to 1, the prescaler is clocked by the internal
clock CK_INT.
Figure 120 shows the behavior of the control circuit and the upcounter in normal mode,
without prescaler.
Figure 120. Control circuit in normal mode, internal clock divided by 1
External clock source mode 1
This mode is selected when SMS=111 in the TIMx_SMCR register. The counter can count
at each rising or falling edge on a selected input.
CK_INT
00
Counter clock = CK_CNT = CK_PSC
COUNTER REGISTER 01 02 03 04 05 06 07 32 33 34 35 36 31
CEN=CNT_EN
UG
CNT_INIT
RM0008 General-purpose timers (TIM2 to TIM5)
Doc ID 13902 Rev 13 363/1093
Figure 121. TI2 external clock connection example
For example, to configure the upcounter to count in response to a rising edge on the TI2
input, use the following procedure:
For example, to configure the upcounter to count in response to a rising edge on the TI2
input, use the following procedure:
1. Configure channel 2 to detect rising edges on the TI2 input by writing CC2S= 01 in the
TIMx_CCMR1 register.
2. Configure the input filter duration by writing the IC2F[3:0] bits in the TIMx_CCMR1
register (if no filter is needed, keep IC2F=0000).
Note: The capture prescaler is not used for triggering, so you dont need to configure it.
3. Select rising edge polarity by writing CC2P=0 in the TIMx_CCER register.
4. Configure the timer in external clock mode 1 by writing SMS=111 in the TIMx_SMCR
register.
5. Select TI2 as the input source by writing TS=110 in the TIMx_SMCR register.
6. Enable the counter by writing CEN=1 in the TIMx_CR1 register.
When a rising edge occurs on TI2, the counter counts once and the TIF flag is set.
The delay between the rising edge on TI2 and the actual clock of the counter is due to the
resynchronization circuit on TI2 input.
Figure 122. Control circuit in external clock mode 1
CK_INT
encoder
mode
external clock
mode 1
external clock
mode 2
internal clock
mode
ETRF
TRGI
TI1F
TI2F or
or
or
(internal clock)
CK_PSC
ECE
TIMx_SMCR
SMS[2:0]
ITRx
TI1F_ED
TI1FP1
TI2FP2
ETRF
TIMx_SMCR
TS[2:0]
TI2
0
1
TIMx_CCER
CC2P
Filter
ICF[3:0]
TIMx_CCMR1
Edge
Detector
TI2F_Rising
TI2F_Falling
110
001
100
101
111
Counter clock = CK_CNT = CK_PSC
Counter register 35 36 34
TI2
CNT_EN
TIF
Write TIF=0
General-purpose timers (TIM2 to TIM5) RM0008
364/1093 Doc ID 13902 Rev 13
External clock source mode 2
This mode is selected by writing ECE=1 in the TIMx_SMCR register.
The counter can count at each rising or falling edge on the external trigger input ETR.
The Figure 123 gives an overview of the external trigger input block.
Figure 123. External trigger input block
For example, to configure the upcounter to count each 2 rising edges on ETR, use the
following procedure:
1. As no filter is needed in this example, write ETF[3:0]=0000 in the TIMx_SMCR register.
2. Set the prescaler by writing ETPS[1:0]=01 in the TIMx_SMCR register
3. Select rising edge detection on the ETR pin by writing ETP=0 in the TIMx_SMCR
register
4. Enable external clock mode 2 by writing ECE=1 in the TIMx_SMCR register.
5. Enable the counter by writing CEN=1 in the TIMx_CR1 register.
The counter counts once each 2 ETR rising edges.
The delay between the rising edge on ETR and the actual clock of the counter is due to the
resynchronization circuit on the ETRP signal.
Figure 124. Control circuit in external clock mode 2
ETR
0
1
TIMx_SMCR
ETP
divider
/1, /2, /4, /8
ETPS[1:0]
ETRP
filter
ETF[3:0]
downcounter
CK_INT
TIMx_SMCR TIMx_SMCR
ETR pin
CK_INT
encoder
mode
external clock
mode 1
external clock
mode 2
internal clock
mode
ETRF
TRGI
TI1F
TI2F or
or
or
(internal clock)
CK_PSC
ECE
TIMx_SMCR
SMS[2:0]
Counter clock = CK_CNT = CK_PSC
Counter register 35 36 34
ETR
CNT_EN
CK_INT
ETRP
ETRF
RM0008 General-purpose timers (TIM2 to TIM5)
Doc ID 13902 Rev 13 365/1093
15.3.4 Capture/compare channels
Each Capture/Compare channel is built around a capture/compare register (including a
shadow register), a input stage for capture (with digital filter, multiplexing and prescaler) and
an output stage (with comparator and output control).
The following figure gives an overview of one Capture/Compare channel.
The input stage samples the corresponding TIx input to generate a filtered signal TIxF.
Then, an edge detector with polarity selection generates a signal (TIxFPx) which can be
used as trigger input by the slave mode controller or as the capture command. It is
prescaled before the capture register (ICxPS).
Figure 125. Capture/compare channel (example: channel 1 input stage)
The output stage generates an intermediate waveform which is then used for reference:
OCxRef (active high). The polarity acts at the end of the chain.
Figure 126. Capture/compare channel 1 main circuit
TI1
0
1
TIMx_CCER
CC1P
divider
/1, /2, /4, /8
ICPS[1:0]
TI1F_ED
filter
ICF[3:0]
downcounter
TIMx_CCMR1
Edge
Detector
TI1F_Rising
TI1F_Falling
to the slave mode controller
TI1FP1
11
01
TIMx_CCMR1
CC1S[1:0]
IC1
TI2FP1
TRC
(from channel 2)
(from slave mode
controller)
10
f
DTS
TIMx_CCER
CC1E
IC1PS
TI1F
0
1
TI2F_rising
TI2F_falling
(from channel 2)
CC1E
Capture/Compare Shadow Register
comparator
Capture/Compare Preload Register
Counter
IC1PS
CC1S[0]
CC1S[1]
capture
input
mode
S
R
read CCR1H
read CCR1L
read_in_progress
capture_transfer
CC1S[0]
CC1S[1]
S
R
write CCR1H
write CCR1L
write_in_progress
output
mode
UEV
OC1PE
(from time
compare_transfer
APB Bus
8 8
h
i
g
h
l
o
w
(
i
f
1
6
-
b
i
t
)
MCU-peripheral interface
TIMx_CCMR1
OC1PE
base unit)
CNT>CCR1
CNT=CCR1
TIMx_EGR
CC1G
General-purpose timers (TIM2 to TIM5) RM0008
366/1093 Doc ID 13902 Rev 13
Figure 127. Output stage of capture/compare channel (channel 1)
The capture/compare block is made of one preload register and one shadow register. Write
and read always access the preload register.
In capture mode, captures are actually done in the shadow register, which is copied into the
preload register.
In compare mode, the content of the preload register is copied into the shadow register
which is compared to the counter.
15.3.5 Input capture mode
In Input capture mode, the Capture/Compare Registers (TIMx_CCRx) are used to latch the
value of the counter after a transition detected by the corresponding ICx signal. When a
capture occurs, the corresponding CCXIF flag (TIMx_SR register) is set and an interrupt or
a DMA request can be sent if they are enabled. If a capture occurs while the CCxIF flag was
already high, then the over-capture flag CCxOF (TIMx_SR register) is set. CCxIF can be
cleared by software by writing it to 0 or by reading the captured data stored in the
TIMx_CCRx register. CCxOF is cleared when you write it to 0.
The following example shows how to capture the counter value in TIMx_CCR1 when TI1
input rises. To do this, use the following procedure:
Select the active input: TIMx_CCR1 must be linked to the TI1 input, so write the CC1S
bits to 01 in the TIMx_CCMR1 register. As soon as CC1S becomes different from 00,
the channel is configured in input and the TIMx_CCR1 register becomes read-only.
Program the input filter duration you need with respect to the signal you connect to the
timer (when the input is one of the TIx (ICxF bits in the TIMx_CCMRx register). Lets
imagine that, when toggling, the input signal is not stable during at must 5 internal clock
cycles. We must program a filter duration longer than these 5 clock cycles. We can
validate a transition on TI1 when 8 consecutive samples with the new level have been
Output mode
CNT > CCR1
CNT = CCR1 controller
TIMx_CCMR1
OC1M[2:0]
oc1ref
0
1
CC1P
TIMx_CCER
Output
Enable
Circuit
OC1
CC1E
TIMx_CCER
To the master mode
controller
ETRF
0
1
OCREF_CLR
ocref_clr_int
OCCS
TIMx_SMCR
ai17187
RM0008 General-purpose timers (TIM2 to TIM5)
Doc ID 13902 Rev 13 367/1093
detected (sampled at f
DTS
frequency). Then write IC1F bits to 0011 in the
TIMx_CCMR1 register.
Select the edge of the active transition on the TI1 channel by writing the CC1P bit to 0
in the TIMx_CCER register (rising edge in this case).
Program the input prescaler. In our example, we wish the capture to be performed at
each valid transition, so the prescaler is disabled (write IC1PS bits to 00 in the
TIMx_CCMR1 register).
Enable capture from the counter into the capture register by setting the CC1E bit in the
TIMx_CCER register.
If needed, enable the related interrupt request by setting the CC1IE bit in the
TIMx_DIER register, and/or the DMA request by setting the CC1DE bit in the
TIMx_DIER register.
When an input capture occurs:
The TIMx_CCR1 register gets the value of the counter on the active transition.
CC1IF flag is set (interrupt flag). CC1OF is also set if at least two consecutive captures
occurred whereas the flag was not cleared.
An interrupt is generated depending on the CC1IE bit.
A DMA request is generated depending on the CC1DE bit.
In order to handle the overcapture, it is recommended to read the data before the
overcapture flag. This is to avoid missing an overcapture which could happen after reading
the flag and before reading the data.
Note: IC interrupt and/or DMA requests can be generated by software by setting the
corresponding CCxG bit in the TIMx_EGR register.
15.3.6 PWM input mode
This mode is a particular case of input capture mode. The procedure is the same except:
Two ICx signals are mapped on the same TIx input.
These 2 ICx signals are active on edges with opposite polarity.
One of the two TIxFP signals is selected as trigger input and the slave mode controller
is configured in reset mode.
General-purpose timers (TIM2 to TIM5) RM0008
368/1093 Doc ID 13902 Rev 13
For example, you can measure the period (in TIMx_CCR1 register) and the duty cycle (in
TIMx_CCR2 register) of the PWM applied on TI1 using the following procedure (depending
on CK_INT frequency and prescaler value):
Select the active input for TIMx_CCR1: write the CC1S bits to 01 in the TIMx_CCMR1
register (TI1 selected).
Select the active polarity for TI1FP1 (used both for capture in TIMx_CCR1 and counter
clear): write the CC1P to 0 (active on rising edge).
Select the active input for TIMx_CCR2: write the CC2S bits to 10 in the TIMx_CCMR1
register (TI1 selected).
Select the active polarity for TI1FP2 (used for capture in TIMx_CCR2): write the CC2P
bit to 1 (active on falling edge).
Select the valid trigger input: write the TS bits to 101 in the TIMx_SMCR register
(TI1FP1 selected).
Configure the slave mode controller in reset mode: write the SMS bits to 100 in the
TIMx_SMCR register.
Enable the captures: write the CC1E and CC2E bits to 1 in the TIMx_CCER register.
Figure 128. PWM input mode timing
1. The PWM input mode can be used only with the TIMx_CH1/TIMx_CH2 signals due to the fact that only
TI1FP1 and TI2FP2 are connected to the slave mode controller.
15.3.7 Forced output mode
In output mode (CCxS bits = 00 in the TIMx_CCMRx register), each output compare signal
(OCxREF and then OCx) can be forced to active or inactive level directly by software,
independently of any comparison between the output compare register and the counter.
To force an output compare signal (ocxref/OCx) to its active level, you just need to write 101
in the OCxM bits in the corresponding TIMx_CCMRx register. Thus ocxref is forced high
(OCxREF is always active high) and OCx get opposite value to CCxP polarity bit.
e.g.: CCxP=0 (OCx active high) => OCx is forced to high level.
ocxref signal can be forced low by writing the OCxM bits to 100 in the TIMx_CCMRx
register.
TI1
TIMx_CNT 0000 0001 0002 0003 0004 0000 0004
TIMx_CCR1
TIMx_CCR2
0004
0002
IC1 capture
IC2 capture
reset counter
IC2 capture
pulse width
IC1 capture
period
measurement measurement
ai15413
RM0008 General-purpose timers (TIM2 to TIM5)
Doc ID 13902 Rev 13 369/1093
Anyway, the comparison between the TIMx_CCRx shadow register and the counter is still
performed and allows the flag to be set. Interrupt and DMA requests can be sent
accordingly. This is described in the Output Compare Mode section.
15.3.8 Output compare mode
This function is used to control an output waveform or indicating when a period of time has
elapsed.
When a match is found between the capture/compare register and the counter, the output
compare function:
Assigns the corresponding output pin to a programmable value defined by the output
compare mode (OCxM bits in the TIMx_CCMRx register) and the output polarity (CCxP
bit in the TIMx_CCER register). The output pin can keep its level (OCXM=000), be set
active (OCxM=001), be set inactive (OCxM=010) or can toggle (OCxM=011) on match.
Sets a flag in the interrupt status register (CCxIF bit in the TIMx_SR register).
Generates an interrupt if the corresponding interrupt mask is set (CCXIE bit in the
TIMx_DIER register).
Sends a DMA request if the corresponding enable bit is set (CCxDE bit in the
TIMx_DIER register, CCDS bit in the TIMx_CR2 register for the DMA request
selection).
The TIMx_CCRx registers can be programmed with or without preload registers using the
OCxPE bit in the TIMx_CCMRx register.
In output compare mode, the update event UEV has no effect on ocxref and OCx output.
The timing resolution is one count of the counter. Output compare mode can also be used to
output a single pulse (in One-pulse mode).
Procedure:
1. Select the counter clock (internal, external, prescaler).
2. Write the desired data in the TIMx_ARR and TIMx_CCRx registers.
3. Set the CCxIE and/or CCxDE bits if an interrupt and/or a DMA request is to be
generated.
4. Select the output mode. For example, you must write OCxM=011, OCxPE=0, CCxP=0
and CCxE=1 to toggle OCx output pin when CNT matches CCRx, CCRx preload is not
used, OCx is enabled and active high.
5. Enable the counter by setting the CEN bit in the TIMx_CR1 register.
The TIMx_CCRx register can be updated at any time by software to control the output
waveform, provided that the preload register is not enabled (OCxPE=0, else TIMx_CCRx
shadow register is updated only at the next update event UEV). An example is given in
Figure 129.
General-purpose timers (TIM2 to TIM5) RM0008
370/1093 Doc ID 13902 Rev 13
Figure 129. Output compare mode, toggle on OC1.
15.3.9 PWM mode
Pulse width modulation mode allows you to generate a signal with a frequency determined
by the value of the TIMx_ARR register and a duty cycle determined by the value of the
TIMx_CCRx register.
The PWM mode can be selected independently on each channel (one PWM per OCx
output) by writing 110 (PWM mode 1) or 111 (PWM mode 2) in the OCxM bits in the
TIMx_CCMRx register. You must enable the corresponding preload register by setting the
OCxPE bit in the TIMx_CCMRx register, and eventually the auto-reload preload register (in
upcounting or center-aligned modes) by setting the ARPE bit in the TIMx_CR1 register.
As the preload registers are transferred to the shadow registers only when an update event
occurs, before starting the counter, you have to initialize all the registers by setting the UG
bit in the TIMx_EGR register.
OCx polarity is software programmable using the CCxP bit in the TIMx_CCER register. It
can be programmed as active high or active low. OCx output is enabled by the CCxE bit in
the TIMx_CCER register. Refer to the TIMx_CCERx register description for more details.
In PWM mode (1 or 2), TIMx_CNT and TIMx_CCRx are always compared to determine
whether TIMx_CCRx TIMx_CNT or TIMx_CNT TIMx_CCRx (depending on the direction
of the counter). However, to comply with the OCREF_CLR functionality (OCREF can be
cleared by an external event through the ETR signal until the next PWM period), the OCREF
signal is asserted only:
When the result of the comparison changes, or
When the output compare mode (OCxM bits in TIMx_CCMRx register) switches from
the frozen configuration (no comparison, OCxM=000) to one of the PWM modes
(OCxM=110 or 111).
This forces the PWM by software while the timer is running.
The timer is able to generate PWM in edge-aligned mode or center-aligned mode
depending on the CMS bits in the TIMx_CR1 register.
OC1REF=OC1
TIMx_CNT B200 B201 0039
TIMx_CCR1 003A
Write B201h in the CC1R register
Match detected on CCR1
Interrupt generated if enabled
003B
B201
003A
RM0008 General-purpose timers (TIM2 to TIM5)
Doc ID 13902 Rev 13 371/1093
PWM edge-aligned mode
Upcounting configuration
Upcounting is active when the DIR bit in the TIMx_CR1 register is low. Refer to the Section :
Upcounting mode on page 353.
In the following example, we consider PWM mode 1. The reference PWM signal OCxREF is
high as long as TIMx_CNT <TIMx_CCRx else it becomes low. If the compare value in
TIMx_CCRx is greater than the auto-reload value (in TIMx_ARR) then OCxREF is held at 1.
If the compare value is 0 then OCxREF is held at 0. Figure 130 shows some edge-aligned
PWM waveforms in an example where TIMx_ARR=8.
Figure 130. Edge-aligned PWM waveforms (ARR=8)
Downcounting configuration
Downcounting is active when DIR bit in TIMx_CR1 register is high. Refer to Downcounting
mode on page 356
In PWM mode 1, the reference signal ocxref is low as long as TIMx_CNT>TIMx_CCRx else
it becomes high. If the compare value in TIMx_CCRx is greater than the auto-reload value in
TIMx_ARR, then ocxref is held at 1. 0% PWM is not possible in this mode.
PWM center-aligned mode
Center-aligned mode is active when the CMS bits in TIMx_CR1 register are different from
00 (all the remaining configurations having the same effect on the ocxref/OCx signals). The
compare flag is set when the counter counts up, when it counts down or both when it counts
up and down depending on the CMS bits configuration. The direction bit (DIR) in the
TIMx_CR1 register is updated by hardware and must not be changed by software. Refer to
the Center-aligned mode (up/down counting) on page 358.
Figure 131 shows some center-aligned PWM waveforms in an example where:
TIMx_ARR=8,
PWM mode is the PWM mode 1,
The flag is set when the counter counts down corresponding to the center-aligned
mode 1 selected for CMS=01 in TIMx_CR1 register.
Counter register
1
0 1 2 3 4 5 6 7 8 0 1
0
OCxREF
CCxIF
OCxREF
CCxIF
OCxREF
CCxIF
OCxREF
CCxIF
CCRx=4
CCRx=8
CCRx>8
CCRx=0
General-purpose timers (TIM2 to TIM5) RM0008
372/1093 Doc ID 13902 Rev 13
Figure 131. Center-aligned PWM waveforms (ARR=8)
Hints on using center-aligned mode:
When starting in center-aligned mode, the current up-down configuration is used. It
means that the counter counts up or down depending on the value written in the DIR bit
in the TIMx_CR1 register. Moreover, the DIR and CMS bits must not be changed at the
same time by the software.
Writing to the counter while running in center-aligned mode is not recommended as it
can lead to unexpected results. In particular:
The direction is not updated if you write a value in the counter that is greater than
the auto-reload value (TIMx_CNT>TIMx_ARR). For example, if the counter was
counting up, it continues to count up.
The direction is updated if you write 0 or write the TIMx_ARR value in the counter
but no Update Event UEV is generated.
The safest way to use center-aligned mode is to generate an update by software
(setting the UG bit in the TIMx_EGR register) just before starting the counter and not to
write the counter while it is running.
CCxF
0 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 0 1 Counter register
CCRx = 4
OCxREF
CMS=01
CMS=10
CMS=11
CCxF
CCRx = 7
OCxREF
CMS=10 or 11
CCxF
CCRx = 8
OCxREF
CMS=01
CMS=10
CMS=11
'1'
CCxF
CCRx > 8
OCxREF
CMS=01
CMS=10
CMS=11
'1'
CCxF
CCRx = 0
OCxREF
CMS=01
CMS=10
CMS=11
'0'
ai14681b
RM0008 General-purpose timers (TIM2 to TIM5)
Doc ID 13902 Rev 13 373/1093
15.3.10 One-pulse mode
One-pulse mode (OPM) is a particular case of the previous modes. It allows the counter to
be started in response to a stimulus and to generate a pulse with a programmable length
after a programmable delay.
Starting the counter can be controlled through the slave mode controller. Generating the
waveform can be done in output compare mode or PWM mode. You select One-pulse mode
by setting the OPM bit in the TIMx_CR1 register. This makes the counter stop automatically
at the next update event UEV.
A pulse can be correctly generated only if the compare value is different from the counter
initial value. Before starting (when the timer is waiting for the trigger), the configuration must
be:
In upcounting: CNT<CCRx ARR (in particular, 0<CCRx),
In downcounting: CNT>CCRx.
Figure 132. Example of one-pulse mode.
For example you may want to generate a positive pulse on OC1 with a length of t
PULSE
and
after a delay of t
DELAY
as soon as a positive edge is detected on the TI2 input pin.
Lets use TI2FP2 as trigger 1:
Map TI2FP2 on TI2 by writing IC2S=01 in the TIMx_CCMR1 register.
TI2FP2 must detect a rising edge, write CC2P=0 in the TIMx_CCER register.
Configure TI2FP2 as trigger for the slave mode controller (TRGI) by writing TS=110 in
the TIMx_SMCR register.
TI2FP2 is used to start the counter by writing SMS to 110 in the TIMx_SMCR register
(trigger mode).
The OPM waveform is defined by writing the compare registers (taking into account the
clock frequency and the counter prescaler).
The t
DELAY
is defined by the value written in the TIMx_CCR1 register.
The t
PULSE
is defined by the difference between the auto-reload value and the compare
value (TIMx_ARR - TIMx_CCR1).
Lets say you want to build a waveform with a transition from 0 to 1 when a compare
match occurs and a transition from 1 to 0 when the counter reaches the auto-reload
TI2
OC1REF
C
o
u
n
t
e
r
t
0
TIM1_ARR
TIM1_CCR1
OC1
t
DELAY
t
PULSE
General-purpose timers (TIM2 to TIM5) RM0008
374/1093 Doc ID 13902 Rev 13
value. To do this you enable PWM mode 2 by writing OC1M=111 in the TIMx_CCMR1
register. You can optionally enable the preload registers by writing OC1PE=1 in the
TIMx_CCMR1 register and ARPE in the TIMx_CR1 register. In this case you have to
write the compare value in the TIMx_CCR1 register, the auto-reload value in the
TIMx_ARR register, generate an update by setting the UG bit and wait for external
trigger event on TI2. CC1P is written to 0 in this example.
In our example, the DIR and CMS bits in the TIMx_CR1 register should be low.
You only want 1 pulse (Single mode), so you write '1 in the OPM bit in the TIMx_CR1
register to stop the counter at the next update event (when the counter rolls over from the
auto-reload value back to 0). When OPM bit in the TIMx_CR1 register is set to '0', so the
Repetitive Mode is selected.
Particular case: OCx fast enable:
In One-pulse mode, the edge detection on TIx input set the CEN bit which enables the
counter. Then the comparison between the counter and the compare value makes the
output toggle. But several clock cycles are needed for these operations and it limits the
minimum delay t
DELAY
min we can get.
If you want to output a waveform with the minimum delay, you can set the OCxFE bit in the
TIMx_CCMRx register. Then OCxRef (and OCx) is forced in response to the stimulus,
without taking in account the comparison. Its new level is the same as if a compare match
had occurred. OCxFE acts only if the channel is configured in PWM1 or PWM2 mode.
15.3.11 Clearing the OCxREF signal on an external event
The OCxREF signal for a given channel can be reset by applying a high level on the ETRF
input (OCxCE enable bit set to 1 in the corresponding TIMx_CCMRx register). OCxREF
remains low until the next update event (UEV) occurs.
This function can be used only in the output compare and PWM modes. It does not work in
forced mode.
For example, the OCxREF signal can be connected to the output of a comparator to be used
for current handling. In this case, ETR must be configured as follows:
1. The external trigger prescaler should be kept off: bits ETPS[1:0] in the TIMx_SMCR
register are cleared to 00.
2. The external clock mode 2 must be disabled: bit ECE in the TIM1_SMCR register is
cleared to 0.
3. The external trigger polarity (ETP) and the external trigger filter (ETF) can be
configured according to the applications needs.
Figure 133 shows the behavior of the OCxREF signal when the ETRF input becomes high,
for both values of the OCxCE enable bit. In this example, the timer TIMx is programmed in
PWM mode.
RM0008 General-purpose timers (TIM2 to TIM5)
Doc ID 13902 Rev 13 375/1093
Figure 133. Clearing TIMx OCxREF
15.3.12 Encoder interface mode
To select Encoder Interface mode write SMS=001 in the TIMx_SMCR register if the counter
is counting on TI2 edges only, SMS=010 if it is counting on TI1 edges only and SMS=011 if
it is counting on both TI1 and TI2 edges.
Select the TI1 and TI2 polarity by programming the CC1P and CC2P bits in the TIMx_CCER
register. When needed, you can program the input filter as well.
The two inputs TI1 and TI2 are used to interface to an incremental encoder. Refer to
Table 85. The counter is clocked by each valid transition on TI1FP1 or TI2FP2 (TI1 and TI2
after input filter and polarity selection, TI1FP1=TI1 if not filtered and not inverted,
TI2FP2=TI2 if not filtered and not inverted) assuming that it is enabled (CEN bit in
TIMx_CR1 register written to 1). The sequence of transitions of the two inputs is evaluated
and generates count pulses as well as the direction signal. Depending on the sequence the
counter counts up or down, the DIR bit in the TIMx_CR1 register is modified by hardware
accordingly. The DIR bit is calculated at each transition on any input (TI1 or TI2), whatever
the counter is counting on TI1 only, TI2 only or both TI1 and TI2.
Encoder interface mode acts simply as an external clock with direction selection. This
means that the counter just counts continuously between 0 and the auto-reload value in the
TIMx_ARR register (0 to ARR or ARR down to 0 depending on the direction). So you must
configure TIMx_ARR before starting. In the same way, the capture, compare, prescaler,
trigger output features continue to work as normal.
In this mode, the counter is modified automatically following the speed and the direction of
the incremental encoder and its content, therefore, always represents the encoders
position. The count direction correspond to the rotation direction of the connected sensor.
The table summarizes the possible combinations, assuming TI1 and TI2 dont switch at the
same time.
OCxREF
counter (CNT)
OCxREF
ETRF
(OCxCE=0)
(OCxCE=1)
OCREF_CLR
becomes high
OCREF_CLR
still high
(CCRx)
General-purpose timers (TIM2 to TIM5) RM0008
376/1093 Doc ID 13902 Rev 13
An external incremental encoder can be connected directly to the MCU without external
interface logic. However, comparators are normally be used to convert the encoders
differential outputs to digital signals. This greatly increases noise immunity. The third
encoder output which indicate the mechanical zero position, may be connected to an
external interrupt input and trigger a counter reset.
Figure 134 gives an example of counter operation, showing count signal generation and
direction control. It also shows how input jitter is compensated where both edges are
selected. This might occur if the sensor is positioned near to one of the switching points. For
this example we assume that the configuration is the following:
CC1S= 01 (TIMx_CCMR1 register, TI1FP1 mapped on TI1)
CC2S= 01 (TIMx_CCMR2 register, TI2FP2 mapped on TI2)
CC1P=0 (TIMx_CCER register, TI1FP1 noninverted, TI1FP1=TI1)
CC2P=0 (TIMx_CCER register, TI2FP2 noninverted, TI2FP2=TI2)
SMS= 011 (TIMx_SMCR register, both inputs are active on both rising and falling
edges)
CEN= 1 (TIMx_CR1 register, Counter is enabled)
Figure 134. Example of counter operation in encoder interface mode
Table 85. Counting direction versus encoder signals
Active edge
Level on opposite
signal (TI1FP1 for
TI2, TI2FP2 for TI1)
TI1FP1 signal TI2FP2 signal
Rising Falling Rising Falling
Counting on
TI1 only
High Down Up No Count No Count
Low Up Down No Count No Count
Counting on
TI2 only
High No Count No Count Up Down
Low No Count No Count Down Up
Counting on
TI1 and TI2
High Down Up Up Down
Low Up Down Down Up
TI1
forward forward backward jitter jitter
up down up
TI2
Counter
RM0008 General-purpose timers (TIM2 to TIM5)
Doc ID 13902 Rev 13 377/1093
Figure 135 gives an example of counter behavior when TI1FP1 polarity is inverted (same
configuration as above except CC1P=1).
Figure 135. Example of encoder interface mode with TI1FP1 polarity inverted
The timer, when configured in Encoder Interface mode provides information on the sensors
current position. You can obtain dynamic information (speed, acceleration, deceleration) by
measuring the period between two encoder events using a second timer configured in
capture mode. The output of the encoder which indicates the mechanical zero can be used
for this purpose. Depending on the time between two events, the counter can also be read
at regular times. You can do this by latching the counter value into a third input capture
register if available (then the capture signal must be periodic and can be generated by
another timer). when available, it is also possible to read its value through a DMA request
generated by a Real-Time clock.
15.3.13 Timer input XOR function
The TI1S bit in the TIM1_CR2 register, allows the input filter of channel 1 to be connected to
the output of a XOR gate, combining the three input pins TIMx_CH1 to TIMx_CH3.
The XOR output can be used with all the timer input functions such as trigger or input
capture.
An example of this feature used to interface Hall sensors is given in Section 14.3.18 on page
317.
15.3.14 Timers and external trigger synchronization
The TIMx Timers can be synchronized with an external trigger in several modes: Reset
mode, Gated mode and Trigger mode.
Slave mode: Reset mode
The counter and its prescaler can be reinitialized in response to an event on a trigger input.
Moreover, if the URS bit from the TIMx_CR1 register is low, an update event UEV is
generated. Then all the preloaded registers (TIMx_ARR, TIMx_CCRx) are updated.
In the following example, the upcounter is cleared in response to a rising edge on TI1 input:
Configure the channel 1 to detect rising edges on TI1. Configure the input filter duration
(in this example, we dont need any filter, so we keep IC1F=0000). The capture
TI1
forward forward backward jitter jitter
up down
TI2
Counter
down
General-purpose timers (TIM2 to TIM5) RM0008
378/1093 Doc ID 13902 Rev 13
prescaler is not used for triggering, so you dont need to configure it. The CC1S bits
select the input capture source only, CC1S = 01 in the TIMx_CCMR1 register. Write
CC1P=0 in TIMx_CCER register to validate the polarity (and detect rising edges only).
Configure the timer in reset mode by writing SMS=100 in TIMx_SMCR register. Select
TI1 as the input source by writing TS=101 in TIMx_SMCR register.
Start the counter by writing CEN=1 in the TIMx_CR1 register.
The counter starts counting on the internal clock, then behaves normally until TI1 rising
edge. When TI1 rises, the counter is cleared and restarts from 0. In the meantime, the
trigger flag is set (TIF bit in the TIMx_SR register) and an interrupt request, or a DMA
request can be sent if enabled (depending on the TIE and TDE bits in TIMx_DIER register).
The following figure shows this behavior when the auto-reload register TIMx_ARR=0x36.
The delay between the rising edge on TI1 and the actual reset of the counter is due to the
resynchronization circuit on TI1 input.
Figure 136. Control circuit in reset mode
Slave mode: Gated mode
The counter can be enabled depending on the level of a selected input.
In the following example, the upcounter counts only when TI1 input is low:
Configure the channel 1 to detect low levels on TI1. Configure the input filter duration
(in this example, we dont need any filter, so we keep IC1F=0000). The capture
prescaler is not used for triggering, so you dont need to configure it. The CC1S bits
select the input capture source only, CC1S=01 in TIMx_CCMR1 register. Write
CC1P=1 in TIMx_CCER register to validate the polarity (and detect low level only).
Configure the timer in gated mode by writing SMS=101 in TIMx_SMCR register. Select
TI1 as the input source by writing TS=101 in TIMx_SMCR register.
Enable the counter by writing CEN=1 in the TIMx_CR1 register (in gated mode, the
counter doesnt start if CEN=0, whatever is the trigger input level).
The counter starts counting on the internal clock as long as TI1 is low and stops as soon as
TI1 becomes high. The TIF flag in the TIMx_SR register is set both when the counter starts
or stops.
The delay between the rising edge on TI1 and the actual stop of the counter is due to the
resynchronization circuit on TI1 input.
00
Counter clock = CK_CNT = CK_PSC
Counter register 01 02 03 00 01 02 03 32 33 34 35 36
UG
TI1
31 30
TIF
RM0008 General-purpose timers (TIM2 to TIM5)
Doc ID 13902 Rev 13 379/1093
Figure 137. Control circuit in gated mode
Slave mode: Trigger mode
The counter can start in response to an event on a selected input.
In the following example, the upcounter starts in response to a rising edge on TI2 input:
Configure the channel 2 to detect rising edges on TI2. Configure the input filter duration
(in this example, we dont need any filter, so we keep IC2F=0000). The capture
prescaler is not used for triggering, so you dont need to configure it. CC2S bits are
selecting the input capture source only, CC2S=01 in TIMx_CCMR1 register. Write
CC2P=1 in TIMx_CCER register to validate the polarity (and detect low level only).
Configure the timer in trigger mode by writing SMS=110 in TIMx_SMCR register. Select
TI2 as the input source by writing TS=110 in TIMx_SMCR register.
When a rising edge occurs on TI2, the counter starts counting on the internal clock and the
TIF flag is set.
The delay between the rising edge on TI2 and the actual start of the counter is due to the
resynchronization circuit on TI2 input.
Figure 138. Control circuit in trigger mode
Slave mode: External Clock mode 2 + trigger mode
The external clock mode 2 can be used in addition to another slave mode (except external
clock mode 1 and encoder mode). In this case, the ETR signal is used as external clock
input, and another input can be selected as trigger input when operating in reset mode,
gated mode or trigger mode. It is recommended not to select ETR as TRGI through the TS
bits of TIMx_SMCR register.
Counter clock = CK_CNT = CK_PSC
Counter register 35 36 37 38 32 33 34
TI1
31 30
CNT_EN
TIF
Write TIF=0
Counter clock = CK_CNT = CK_PSC
Counter register 35 36 37 38 34
TI2
CNT_EN
TIF
General-purpose timers (TIM2 to TIM5) RM0008
380/1093 Doc ID 13902 Rev 13
In the following example, the upcounter is incremented at each rising edge of the ETR signal
as soon as a rising edge of TI1 occurs:
1. Configure the external trigger input circuit by programming the TIMx_SMCR register as
follows:
ETF = 0000: no filter
ETPS=00: prescaler disabled
ETP=0: detection of rising edges on ETR and ECE=1 to enable the external clock
mode 2.
2. Configure the channel 1 as follows, to detect rising edges on TI:
IC1F=0000: no filter.
The capture prescaler is not used for triggering and does not need to be
configured.
CC1S=01in TIMx_CCMR1 register to select only the input capture source
CC1P=0 in TIMx_CCER register to validate the polarity (and detect rising edge
only).
3. Configure the timer in trigger mode by writing SMS=110 in TIMx_SMCR register. Select
TI1 as the input source by writing TS=101 in TIMx_SMCR register.
A rising edge on TI1 enables the counter and sets the TIF flag. The counter then counts on
ETR rising edges.
The delay between the rising edge of the ETR signal and the actual reset of the counter is
due to the resynchronization circuit on ETRP input.
Figure 139. Control circuit in external clock mode 2 + trigger mode
15.3.15 Timer synchronization
The TIMx timers are linked together internally for timer synchronization or chaining. When
one Timer is configured in Master Mode, it can reset, start, stop or clock the counter of
another Timer configured in Slave Mode.
Figure 140: Master/Slave timer example presents an overview of the trigger selection and
the master mode selection blocks.
Counter clock = CK_CNT = CK_PSC
Counter register 35 36 34
ETR
CEN/CNT_EN
TIF
TI1
RM0008 General-purpose timers (TIM2 to TIM5)
Doc ID 13902 Rev 13 381/1093
Using one timer as prescaler for another
Figure 140. Master/Slave timer example
For example, you can configure Timer 1 to act as a prescaler for Timer 2. Refer to
Figure 140. To do this:
Configure Timer 1 in master mode so that it outputs a periodic trigger signal on each
update event UEV. If you write MMS=010 in the TIM1_CR2 register, a rising edge is
output on TRGO1 each time an update event is generated.
To connect the TRGO1 output of Timer 1 to Timer 2, Timer 2 must be configured in
slave mode using ITR1 as internal trigger. You select this through the TS bits in the
TIM2_SMCR register (writing TS=000).
Then you put the slave mode controller in external clock mode 1 (write SMS=111 in the
TIM2_SMCR register). This causes Timer 2 to be clocked by the rising edge of the
periodic Timer 1 trigger signal (which correspond to the timer 1 counter overflow).
Finally both timers must be enabled by setting their respective CEN bits (TIMx_CR1
register).
Note: If OCx is selected on Timer 1 as trigger output (MMS=1xx), its rising edge is used to clock
the counter of timer 2.
Using one timer to enable another timer
In this example, we control the enable of Timer 2 with the output compare 1 of Timer 1.
Refer to Figure 140 for connections. Timer 2 counts on the divided internal clock only when
OC1REF of Timer 1 is high. Both counter clock frequencies are divided by 3 by the
prescaler compared to CK_INT (f
CK_CNT
= f
CK_INT
/3).
Configure Timer 1 master mode to send its Output Compare 1 Reference (OC1REF)
signal as trigger output (MMS=100 in the TIM1_CR2 register).
Configure the Timer 1 OC1REF waveform (TIM1_CCMR1 register).
Configure Timer 2 to get the input trigger from Timer 1 (TS=000 in the TIM2_SMCR
register).
Configure Timer 2 in gated mode (SMS=101 in TIM2_SMCR register).
Enable Timer 2 by writing 1 in the CEN bit (TIM2_CR1 register).
Start Timer 1 by writing 1 in the CEN bit (TIM1_CR1 register).
Note: The counter 2 clock is not synchronized with counter 1, this mode only affects the Timer 2
counter enable signal.
TRGO1
UEV
ITR1
Prescaler Counter
SMS TS MMS
TIM1 TIM2
Master
mode
control
Slave
mode
control
CK_PSC
Prescaler Counter
Clock
Input
selection
trigger
General-purpose timers (TIM2 to TIM5) RM0008
382/1093 Doc ID 13902 Rev 13
Figure 141. Gating timer 2 with OC1REF of timer 1
In the example in Figure 141, the Timer 2 counter and prescaler are not initialized before
being started. So they start counting from their current value. It is possible to start from a
given value by resetting both timers before starting Timer 1. You can then write any value
you want in the timer counters. The timers can easily be reset by software using the UG bit
in the TIMx_EGR registers.
In the next example, we synchronize Timer 1 and Timer 2. Timer 1 is the master and starts
from 0. Timer 2 is the slave and starts from 0xE7. The prescaler ratio is the same for both
timers. Timer 2 stops when Timer 1 is disabled by writing 0 to the CEN bit in the TIM1_CR1
register:
Configure Timer 1 master mode to send its Output Compare 1 Reference (OC1REF)
signal as trigger output (MMS=100 in the TIM1_CR2 register).
Configure the Timer 1 OC1REF waveform (TIM1_CCMR1 register).
Configure Timer 2 to get the input trigger from Timer 1 (TS=000 in the TIM2_SMCR
register).
Configure Timer 2 in gated mode (SMS=101 in TIM2_SMCR register).
Reset Timer 1 by writing 1 in UG bit (TIM1_EGR register).
Reset Timer 2 by writing 1 in UG bit (TIM2_EGR register).
Initialize Timer 2 to 0xE7 by writing 0xE7 in the timer 2 counter (TIM2_CNTL).
Enable Timer 2 by writing 1 in the CEN bit (TIM2_CR1 register).
Start Timer 1 by writing 1 in the CEN bit (TIM1_CR1 register).
Stop Timer 1 by writing 0 in the CEN bit (TIM1_CR1 register).
TIMER 2-TIF
Write TIF=0
FC FD FE FF 00
3045 3047 3048
CK_INT
TIMER1-OC1REF
TIMER1-CNT
TIMER2-CNT
01
3046
RM0008 General-purpose timers (TIM2 to TIM5)
Doc ID 13902 Rev 13 383/1093
Figure 142. Gating timer 2 with Enable of timer 1
Using one timer to start another timer
In this example, we set the enable of Timer 2 with the update event of Timer 1. Refer to
Figure 140 for connections. Timer 2 starts counting from its current value (which can be
nonzero) on the divided internal clock as soon as the update event is generated by Timer 1.
When Timer 2 receives the trigger signal its CEN bit is automatically set and the counter
counts until we write 0 to the CEN bit in the TIM2_CR1 register. Both counter clock
frequencies are divided by 3 by the prescaler compared to CK_INT (f
CK_CNT
= f
CK_INT
/3).
Configure Timer 1 master mode to send its Update Event (UEV) as trigger output
(MMS=010 in the TIM1_CR2 register).
Configure the Timer 1 period (TIM1_ARR registers).
Configure Timer 2 to get the input trigger from Timer 1 (TS=000 in the TIM2_SMCR
register).
Configure Timer 2 in trigger mode (SMS=110 in TIM2_SMCR register).
Start Timer 1 by writing 1 in the CEN bit (TIM1_CR1 register).
Figure 143. Triggering timer 2 with update of timer 1
TIMER 2-TIF
Write TIF=0
75 00 01
CK_INT
TIMER1-CEN=CNT_EN
TIMER1-CNT
TIMER2-CNT
02
TIMER1-CNT_INIT
AB 00 E7 E8 E9
TIMER2-CNT_INIT
TIMER2
write CNT
TIMER 2-TIF
Write TIF=0
FD FE FF 00 01
45 47 48
CK_INT
TIMER1-UEV
TIMER1-CNT
TIMER2-CNT
02
46
TIMER2-CEN=CNT_EN
General-purpose timers (TIM2 to TIM5) RM0008
384/1093 Doc ID 13902 Rev 13
As in the previous example, you can initialize both counters before starting counting.
Figure 144 shows the behavior with the same configuration as in Figure 143 but in trigger
mode instead of gated mode (SMS=110 in the TIM2_SMCR register).
Figure 144. Triggering timer 2 with Enable of timer 1
Using one timer as prescaler for another timer
For example, you can configure Timer 1 to act as a prescaler for Timer 2. Refer to
Figure 140 for connections. To do this:
Configure Timer 1 master mode to send its Update Event (UEV) as trigger output
(MMS=010 in the TIM1_CR2 register). then it outputs a periodic signal on each counter
overflow.
Configure the Timer 1 period (TIM1_ARR registers).
Configure Timer 2 to get the input trigger from Timer 1 (TS=000 in the TIM2_SMCR
register).
Configure Timer 2 in external clock mode 1 (SMS=111 in TIM2_SMCR register).
Start Timer 2 by writing 1 in the CEN bit (TIM2_CR1 register).
Start Timer 1 by writing 1 in the CEN bit (TIM1_CR1 register).
Starting 2 timers synchronously in response to an external trigger
In this example, we set the enable of timer 1 when its TI1 input rises, and the enable of
Timer 2 with the enable of Timer 1. Refer to Figure 140 for connections. To ensure the
TIMER 2-TIF
Write TIF=0
75 00 01
CK_INT
TIMER1-CEN=CNT_EN
TIMER1-CNT
TIMER2-CNT
02
TIMER1-CNT_INIT
CD 00 E7 E8 EA
TIMER2-CNT_INIT
TIMER2
write CNT
E9
RM0008 General-purpose timers (TIM2 to TIM5)
Doc ID 13902 Rev 13 385/1093
counters are aligned, Timer 1 must be configured in Master/Slave mode (slave with respect
to TI1, master with respect to Timer 2):
Configure Timer 1 master mode to send its Enable as trigger output (MMS=001 in the
TIM1_CR2 register).
Configure Timer 1 slave mode to get the input trigger from TI1 (TS=100 in the
TIM1_SMCR register).
Configure Timer 1 in trigger mode (SMS=110 in the TIM1_SMCR register).
Configure the Timer 1 in Master/Slave mode by writing MSM=1 (TIM1_SMCR register).
Configure Timer 2 to get the input trigger from Timer 1 (TS=000 in the TIM2_SMCR
register).
Configure Timer 2 in trigger mode (SMS=110 in the TIM2_SMCR register).
When a rising edge occurs on TI1 (Timer 1), both counters starts counting synchronously on
the internal clock and both TIF flags are set.
Note: In this example both timers are initialized before starting (by setting their respective UG
bits). Both counters starts from 0, but you can easily insert an offset between them by
writing any of the counter registers (TIMx_CNT). You can see that the master/slave mode
insert a delay between CNT_EN and CK_PSC on timer 1.
Figure 145. Triggering timer 1 and 2 with timer 1 TI1 input
15.3.16 Debug mode
When the microcontroller enters debug mode (Cortex-M3 core - halted), the TIMx counter
either continues to work normally or stops, depending on DBG_TIMx_STOP configuration
bit in DBGMCU module. For more details, refer to Section 31.16.2: Debug support for
timers, watchdog, bxCAN and I2C.
00 01
CK_INT
TIMER1-CEN=CNT_EN
TIMER1-CNT
TIMER 1-TI1
TIMER 1-CK_PSC
02 03 04 05 06 07 08 09
TIMER1-TIF
00 01
TIMER2-CEN=CNT_EN
TIMER2-CNT
TIMER 2-CK_PSC
02 03 04 05 06 07 08 09
TIMER2-TIF
General-purpose timers (TIM2 to TIM5) RM0008
386/1093 Doc ID 13902 Rev 13
15.4 TIMx2 to TIM5 registers
Refer to Section 2.1 on page 46 for a list of abbreviations used in register descriptions.
The peripheral registers can be accessed by half-words (16-bit) or words (32-bit).
15.4.1 TIMx control register 1 (TIMx_CR1)
Address offset: 0x00
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
CKD[1:0] ARPE CMS DIR OPM URS UDIS CEN
rw rw rw rw rw rw rw rw rw rw
Bits 15:10 Reserved, always read as 0
Bits 9:8 CKD: Clock division
This bit-field indicates the division ratio between the timer clock (CK_INT) frequency and
sampling clock used by the digital filters (ETR, TIx),
00: t
DTS
= t
CK_INT
01: t
DTS
= 2 t
CK_INT
10: t
DTS
= 4 t
CK_INT
11: Reserved
Bit 7 ARPE: Auto-reload preload enable
0: TIMx_ARR register is not buffered
1: TIMx_ARR register is buffered
Bits 6:5 CMS: Center-aligned mode selection
00: Edge-aligned mode. The counter counts up or down depending on the direction bit
(DIR).
01: Center-aligned mode 1. The counter counts up and down alternatively. Output compare
interrupt flags of channels configured in output (CCxS=00 in TIMx_CCMRx register) are set
only when the counter is counting down.
10: Center-aligned mode 2. The counter counts up and down alternatively. Output compare
interrupt flags of channels configured in output (CCxS=00 in TIMx_CCMRx register) are set
only when the counter is counting up.
11: Center-aligned mode 3. The counter counts up and down alternatively. Output compare
interrupt flags of channels configured in output (CCxS=00 in TIMx_CCMRx register) are set
both when the counter is counting up or down.
Note: It is not allowed to switch from edge-aligned mode to center-aligned mode as long as
the counter is enabled (CEN=1)
Bit 4 DIR: Direction
0: Counter used as upcounter
1: Counter used as downcounter
Note: This bit is read only when the timer is configured in Center-aligned mode or Encoder
mode.
Bit 3 OPM: One-pulse mode
0: Counter is not stopped at update event
1: Counter stops counting at the next update event (clearing the bit CEN)
RM0008 General-purpose timers (TIM2 to TIM5)
Doc ID 13902 Rev 13 387/1093
Bit 2 URS: Update request source
This bit is set and cleared by software to select the UEV event sources.
0: Any of the following events generate an update interrupt or DMA request if enabled.
These events can be:
Counter overflow/underflow
Setting the UG bit
Update generation through the slave mode controller
1: Only counter overflow/underflow generates an update interrupt or DMA request if enabled.
Bit 1 UDIS: Update disable
This bit is set and cleared by software to enable/disable UEV event generation.
0: UEV enabled. The Update (UEV) event is generated by one of the following events:
Counter overflow/underflow
Setting the UG bit
Update generation through the slave mode controller
Buffered registers are then loaded with their preload values.
1: UEV disabled. The Update event is not generated, shadow registers keep their value
(ARR, PSC, CCRx). However the counter and the prescaler are reinitialized if the UG bit is
set or if a hardware reset is received from the slave mode controller.
Bit 0 CEN: Counter enable
0: Counter disabled
1: Counter enabled
Note: External clock, gated mode and encoder mode can work only if the CEN bit has been
previously set by software. However trigger mode can set the CEN bit automatically by
hardware.
CEN is cleared automatically in one-pulse mode, when an update event occurs.
General-purpose timers (TIM2 to TIM5) RM0008
388/1093 Doc ID 13902 Rev 13
15.4.2 TIMx control register 2 (TIMx_CR2)
Address offset: 0x04
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
TI1S MMS[2:0] CCDS
Reserved
rw rw rw rw rw
Bits 15:8 Reserved, must be kept at reset value.
Bit 7 TI1S: TI1 selection
0: The TIMx_CH1 pin is connected to TI1 input
1: The TIMx_CH1, CH2 and CH3 pins are connected to the TI1 input (XOR combination)
See also Section 14.3.18: Interfacing with Hall sensors on page 317
Bits 6:4 MMS: Master mode selection
These bits allow to select the information to be sent in master mode to slave timers for
synchronization (TRGO). The combination is as follows:
000: Reset - the UG bit from the TIMx_EGR register is used as trigger output (TRGO). If the
reset is generated by the trigger input (slave mode controller configured in reset mode) then
the signal on TRGO is delayed compared to the actual reset.
001: Enable - the Counter enable signal, CNT_EN, is used as trigger output (TRGO). It is
useful to start several timers at the same time or to control a window in which a slave timer is
enabled. The Counter Enable signal is generated by a logic OR between CEN control bit and
the trigger input when configured in gated mode.
When the Counter Enable signal is controlled by the trigger input, there is a delay on TRGO,
except if the master/slave mode is selected (see the MSM bit description in TIMx_SMCR
register).
010: Update - The update event is selected as trigger output (TRGO). For instance a master
timer can then be used as a prescaler for a slave timer.
011: Compare Pulse - The trigger output send a positive pulse when the CC1IF flag is to be
set (even if it was already high), as soon as a capture or a compare match occurred. (TRGO)
100: Compare - OC1REF signal is used as trigger output (TRGO)
101: Compare - OC2REF signal is used as trigger output (TRGO)
110: Compare - OC3REF signal is used as trigger output (TRGO)
111: Compare - OC4REF signal is used as trigger output (TRGO)
Bit 3 CCDS: Capture/compare DMA selection
0: CCx DMA request sent when CCx event occurs
1: CCx DMA requests sent when update event occurs
Bits 2:0 Reserved, always read as 0
RM0008 General-purpose timers (TIM2 to TIM5)
Doc ID 13902 Rev 13 389/1093
15.4.3 TIMx slave mode control register (TIMx_SMCR)
Address offset: 0x08
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
ETP ECE ETPS[1:0] ETF[3:0] MSM TS[2:0]
Res.
SMS[2:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bit 15 ETP: External trigger polarity
This bit selects whether ETR or ETR is used for trigger operations
0: ETR is non-inverted, active at high level or rising edge
1: ETR is inverted, active at low level or falling edge
Bit 14 ECE: External clock enable
This bit enables External clock mode 2.
0: External clock mode 2 disabled
1: External clock mode 2 enabled. The counter is clocked by any active edge on the ETRF
signal.
1: Setting the ECE bit has the same effect as selecting external clock mode 1 with TRGI
connected to ETRF (SMS=111 and TS=111).
2: It is possible to simultaneously use external clock mode 2 with the following slave modes:
reset mode, gated mode and trigger mode. Nevertheless, TRGI must not be connected to
ETRF in this case (TS bits must not be 111).
3: If external clock mode 1 and external clock mode 2 are enabled at the same time, the
external clock input is ETRF.
Bits 13:12 ETPS: External trigger prescaler
External trigger signal ETRP frequency must be at most 1/4 of CK_INT frequency. A
prescaler can be enabled to reduce ETRP frequency. It is useful when inputting fast external
clocks.
00: Prescaler OFF
01: ETRP frequency divided by 2
10: ETRP frequency divided by 4
11: ETRP frequency divided by 8
Bits 11:8 ETF[3:0]: External trigger filter
This bit-field then defines the frequency used to sample ETRP signal and the length of the
digital filter applied to ETRP. The digital filter is made of an event counter in which N events
are needed to validate a transition on the output:
0000: No filter, sampling is done at f
DTS
1000: f
SAMPLING
=f
DTS
/8, N=6
0001: f
SAMPLING
=f
CK_INT
, N=2 1001: f
SAMPLING
=f
DTS
/8, N=8
0010: f
SAMPLING
=f
CK_INT
, N=4 1010: f
SAMPLING
=f
DTS
/16, N=5
0011: f
SAMPLING
=f
CK_INT
, N=8 1011: f
SAMPLING
=f
DTS
/16, N=6
0100: f
SAMPLING
=f
DTS
/2, N=6 1100: f
SAMPLING
=f
DTS
/16, N=8
0101: f
SAMPLING
=f
DTS
/2, N=8 1101: f
SAMPLING
=f
DTS
/32, N=5
0110: f
SAMPLING
=f
DTS
/4, N=6 1110: f
SAMPLING
=f
DTS
/32, N=6
0111: f
SAMPLING
=f
DTS
/4, N=8 1111: f
SAMPLING
=f
DTS
/32, N=8
Bit 7 MSM: Master/Slave mode
0: No action
1: The effect of an event on the trigger input (TRGI) is delayed to allow a perfect
synchronization between the current timer and its slaves (through TRGO). It is useful if we
want to synchronize several timers on a single external event.
General-purpose timers (TIM2 to TIM5) RM0008
390/1093 Doc ID 13902 Rev 13
Bits 6:4 TS: Trigger selection
This bit-field selects the trigger input to be used to synchronize the counter.
000: Internal Trigger 0 (ITR0).
001: Internal Trigger 1 (ITR1).
010: Internal Trigger 2 (ITR2).
011: Internal Trigger 3 (ITR3).
100: TI1 Edge Detector (TI1F_ED)
101: Filtered Timer Input 1 (TI1FP1)
110: Filtered Timer Input 2 (TI2FP2)
111: External Trigger input (ETRF)
See Table 86: TIMx Internal trigger connection on page 390for more details on ITRx
meaning for each Timer.
Note: These bits must be changed only when they are not used (e.g. when SMS=000) to
avoid wrong edge detections at the transition.
Bit 3 Reserved, must be kept at reset value.
Bits 2:0 SMS: Slave mode selection
When external signals are selected the active edge of the trigger signal (TRGI) is linked to
the polarity selected on the external input (see Input Control register and Control Register
description.
000: Slave mode disabled - if CEN = 1 then the prescaler is clocked directly by the internal
clock.
001: Encoder mode 1 - Counter counts up/down on TI2FP2 edge depending on TI1FP1
level.
010: Encoder mode 2 - Counter counts up/down on TI1FP1 edge depending on TI2FP2
level.
011: Encoder mode 3 - Counter counts up/down on both TI1FP1 and TI2FP2 edges
depending on the level of the other input.
100: Reset Mode - Rising edge of the selected trigger input (TRGI) reinitializes the counter
and generates an update of the registers.
101: Gated Mode - The counter clock is enabled when the trigger input (TRGI) is high. The
counter stops (but is not reset) as soon as the trigger becomes low. Both start and stop of
the counter are controlled.
110: Trigger Mode - The counter starts at a rising edge of the trigger TRGI (but it is not
reset). Only the start of the counter is controlled.
111: External Clock Mode 1 - Rising edges of the selected trigger (TRGI) clock the counter.
Note: The gated mode must not be used if TI1F_ED is selected as the trigger input (TS=100).
Indeed, TI1F_ED outputs 1 pulse for each transition on TI1F, whereas the gated mode
checks the level of the trigger signal.
Table 86. TIMx Internal trigger connection
(1)
1. When a timer is not present in the product, the corresponding trigger ITRx is not available.
Slave TIM ITR0 (TS = 000) ITR1 (TS = 001) ITR2 (TS = 010) ITR3 (TS = 011)
TIM2 TIM1 TIM8 TIM3 TIM4
TIM3 TIM1 TIM2 TIM5 TIM4
TIM4 TIM1 TIM2 TIM3 TIM8
TIM5 TIM2 TIM3 TIM4 TIM8
RM0008 General-purpose timers (TIM2 to TIM5)
Doc ID 13902 Rev 13 391/1093
15.4.4 TIMx DMA/Interrupt enable register (TIMx_DIER)
Address offset: 0x0C
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res.
TDE
Res
CC4DE CC3DE CC2DE CC1DE UDE
Res.
TIE
Res
CC4IE CC3IE CC2IE CC1IE UIE
rw rw rw rw rw rw rw rw rw rw rw rw
Bit 15 Reserved, must be kept at reset value.
Bit 14 TDE: Trigger DMA request enable
0: Trigger DMA request disabled.
1: Trigger DMA request enabled.
Bit 13 Reserved, always read as 0
Bit 12 CC4DE: Capture/Compare 4 DMA request enable
0: CC4 DMA request disabled.
1: CC4 DMA request enabled.
Bit 11 CC3DE: Capture/Compare 3 DMA request enable
0: CC3 DMA request disabled.
1: CC3 DMA request enabled.
Bit 10 CC2DE: Capture/Compare 2 DMA request enable
0: CC2 DMA request disabled.
1: CC2 DMA request enabled.
Bit 9 CC1DE: Capture/Compare 1 DMA request enable
0: CC1 DMA request disabled.
1: CC1 DMA request enabled.
Bit 8 UDE: Update DMA request enable
0: Update DMA request disabled.
1: Update DMA request enabled.
Bit 7 Reserved, must be kept at reset value.
Bit 6 TIE: Trigger interrupt enable
0: Trigger interrupt disabled.
1: Trigger interrupt enabled.
Bit 5 Reserved, must be kept at reset value.
Bit 4 CC4IE: Capture/Compare 4 interrupt enable
0: CC4 interrupt disabled.
1: CC4 interrupt enabled.
Bit 3 CC3IE: Capture/Compare 3 interrupt enable
0: CC3 interrupt disabled.
1: CC3 interrupt enabled.
General-purpose timers (TIM2 to TIM5) RM0008
392/1093 Doc ID 13902 Rev 13
15.4.5 TIMx status register (TIMx_SR)
Address offset: 0x10
Reset value: 0x0000
Bit 2 CC2IE: Capture/Compare 2 interrupt enable
0: CC2 interrupt disabled.
1: CC2 interrupt enabled.
Bit 1 CC1IE: Capture/Compare 1 interrupt enable
0: CC1 interrupt disabled.
1: CC1 interrupt enabled.
Bit 0 UIE: Update interrupt enable
0: Update interrupt disabled.
1: Update interrupt enabled.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
CC4OF CC3OF CC2OF CC1OF
Reserved
TIF
Res
CC4IF CC3IF CC2IF CC1IF UIF
rc_w0 rc_w0 rc_w0 rc_w0 rc_w0 rc_w0 rc_w0 rc_w0 rc_w0 rc_w0
Bit 15:13 Reserved, must be kept at reset value.
Bit 12 CC4OF: Capture/Compare 4 overcapture flag
refer to CC1OF description
Bit 11 CC3OF: Capture/Compare 3 overcapture flag
refer to CC1OF description
Bit 10 CC2OF: Capture/compare 2 overcapture flag
refer to CC1OF description
Bit 9 CC1OF: Capture/Compare 1 overcapture flag
This flag is set by hardware only when the corresponding channel is configured in input
capture mode. It is cleared by software by writing it to 0.
0: No overcapture has been detected.
1: The counter value has been captured in TIMx_CCR1 register while CC1IF flag was
already set
Bits 8:7 Reserved, must be kept at reset value.
Bit 6 TIF: Trigger interrupt flag
This flag is set by hardware on trigger event (active edge detected on TRGI input when the
slave mode controller is enabled in all modes but gated mode, both edges in case gated
mode is selected). It is cleared by software.
0: No trigger event occurred.
1: Trigger interrupt pending.
Bit 5 Reserved, always read as 0
Bit 4 CC4IF: Capture/Compare 4 interrupt flag
refer to CC1IF description
Bit 3 CC3IF: Capture/Compare 3 interrupt flag
refer to CC1IF description
Bit 2 CC2IF: Capture/Compare 2 interrupt flag
refer to CC1IF description
RM0008 General-purpose timers (TIM2 to TIM5)
Doc ID 13902 Rev 13 393/1093
Bit 1 CC1IF: Capture/compare 1 interrupt flag
If channel CC1 is configured as output:
This flag is set by hardware when the counter matches the compare value, with some
exception in center-aligned mode (refer to the CMS bits in the TIMx_CR1 register
description). It is cleared by software.
0: No match.
1: The content of the counter TIMx_CNT has matched the content of the TIMx_CCR1
register.
If channel CC1 is configured as input:
This bit is set by hardware on a capture. It is cleared by software or by reading the
TIMx_CCR1 register.
0: No input capture occurred.
1: The counter value has been captured in TIMx_CCR1 register (An edge has been detected
on IC1 which matches the selected polarity).
Bit 0 UIF: Update interrupt flag
This bit is set by hardware on an update event. It is cleared by software.
0: No update occurred.
1: Update interrupt pending. This bit is set by hardware when the registers are updated:
At overflow or underflow regarding the repetition counter value (update if repetition
counter = 0) and if the UDIS=0 in the TIMx_CR1 register.
When CNT is reinitialized by software using the UG bit in TIMx_EGR register, if URS=0
and UDIS=0 in the TIMx_CR1 register.
When CNT is reinitialized by a trigger event (refer to the synchro control register
description), if URS=0 and UDIS=0 in the TIMx_CR1 register.
General-purpose timers (TIM2 to TIM5) RM0008
394/1093 Doc ID 13902 Rev 13
15.4.6 TIMx event generation register (TIMx_EGR)
Address offset: 0x14
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
TG
Res.
CC4G CC3G CC2G CC1G UG
w w w w w w
Bits 15:7 Reserved, must be kept at reset value.
Bit 6 TG: Trigger generation
This bit is set by software in order to generate an event, it is automatically cleared by
hardware.
0: No action
1: The TIF flag is set in TIMx_SR register. Related interrupt or DMA transfer can occur if
enabled.
Bit 5 Reserved, must be kept at reset value.
Bit 4 CC4G: Capture/compare 4 generation
refer to CC1G description
Bit 3 CC3G: Capture/compare 3 generation
refer to CC1G description
Bit 2 CC2G: Capture/compare 2 generation
refer to CC1G description
Bit 1 CC1G: Capture/compare 1 generation
This bit is set by software in order to generate an event, it is automatically cleared by
hardware.
0: No action
1: A capture/compare event is generated on channel 1:
If channel CC1 is configured as output:
CC1IF flag is set, Corresponding interrupt or DMA request is sent if enabled.
If channel CC1 is configured as input:
The current value of the counter is captured in TIMx_CCR1 register. The CC1IF flag is set,
the corresponding interrupt or DMA request is sent if enabled. The CC1OF flag is set if the
CC1IF flag was already high.
Bit 0 UG: Update generation
This bit can be set by software, it is automatically cleared by hardware.
0: No action
1: Re-initialize the counter and generates an update of the registers. Note that the prescaler
counter is cleared too (anyway the prescaler ratio is not affected). The counter is cleared if
the center-aligned mode is selected or if DIR=0 (upcounting), else it takes the auto-reload
value (TIMx_ARR) if DIR=1 (downcounting).
RM0008 General-purpose timers (TIM2 to TIM5)
Doc ID 13902 Rev 13 395/1093
15.4.7 TIMx capture/compare mode register 1 (TIMx_CCMR1)
Address offset: 0x18
Reset value: 0x0000
The channels can be used in input (capture mode) or in output (compare mode). The
direction of a channel is defined by configuring the corresponding CCxS bits. All the other
bits of this register have a different function in input and in output mode. For a given bit,
OCxx describes its function when the channel is configured in output, ICxx describes its
function when the channel is configured in input. So you must take care that the same bit
can have a different meaning for the input stage and for the output stage.
Output compare mode
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
OC2CE OC2M[2:0] OC2PE OC2FE
CC2S[1:0]
OC1CE OC1M[2:0] OC1PE OC1FE
CC1S[1:0]
IC2F[3:0] IC2PSC[1:0] IC1F[3:0] IC1PSC[1:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bit 15 OC2CE: Output compare 2 clear enable
Bits 14:12 OC2M[2:0]: Output compare 2 mode
Bit 11 OC2PE: Output compare 2 preload enable
Bit 10 OC2FE: Output compare 2 fast enable
Bits 9:8 CC2S[1:0]: Capture/Compare 2 selection
This bit-field defines the direction of the channel (input/output) as well as the used input.
00: CC2 channel is configured as output
01: CC2 channel is configured as input, IC2 is mapped on TI2
10: CC2 channel is configured as input, IC2 is mapped on TI1
11: CC2 channel is configured as input, IC2 is mapped on TRC. This mode is working only
if an internal trigger input is selected through the TS bit (TIMx_SMCR register)
Note: CC2S bits are writable only when the channel is OFF (CC2E = 0 in TIMx_CCER).
Bit 7 OC1CE: Output compare 1 clear enable
OC1CE: Output Compare 1 Clear Enable
0: OC1Ref is not affected by the ETRF input
1: OC1Ref is cleared as soon as a High level is detected on ETRF input
General-purpose timers (TIM2 to TIM5) RM0008
396/1093 Doc ID 13902 Rev 13
Bits 6:4 OC1M: Output compare 1 mode
These bits define the behavior of the output reference signal OC1REF from which OC1 and
OC1N are derived. OC1REF is active high whereas OC1 and OC1N active level depends
on CC1P and CC1NP bits.
000: Frozen - The comparison between the output compare register TIMx_CCR1 and the
counter TIMx_CNT has no effect on the outputs.(this mode is used to generate a timing
base).
001: Set channel 1 to active level on match. OC1REF signal is forced high when the counter
TIMx_CNT matches the capture/compare register 1 (TIMx_CCR1).
010: Set channel 1 to inactive level on match. OC1REF signal is forced low when the
counter TIMx_CNT matches the capture/compare register 1 (TIMx_CCR1).
011: Toggle - OC1REF toggles when TIMx_CNT=TIMx_CCR1.
100: Force inactive level - OC1REF is forced low.
101: Force active level - OC1REF is forced high.
110: PWM mode 1 - In upcounting, channel 1 is active as long as TIMx_CNT<TIMx_CCR1
else inactive. In downcounting, channel 1 is inactive (OC1REF=0) as long as
TIMx_CNT>TIMx_CCR1 else active (OC1REF=1).
111: PWM mode 2 - In upcounting, channel 1 is inactive as long as
TIMx_CNT<TIMx_CCR1 else active. In downcounting, channel 1 is active as long as
TIMx_CNT>TIMx_CCR1 else inactive.
Note: 1: These bits can not be modified as long as LOCK level 3 has been programmed
(LOCK bits in TIMx_BDTR register) and CC1S=00 (the channel is configured in
output).
2: In PWM mode 1 or 2, the OCREF level changes only when the result of the
comparison changes or when the output compare mode switches from frozen mode
to PWM mode.
Bit 3 OC1PE: Output compare 1 preload enable
0: Preload register on TIMx_CCR1 disabled. TIMx_CCR1 can be written at anytime, the
new value is taken in account immediately.
1: Preload register on TIMx_CCR1 enabled. Read/Write operations access the preload
register. TIMx_CCR1 preload value is loaded in the active register at each update event.
Note: 1: These bits can not be modified as long as LOCK level 3 has been programmed
(LOCK bits in TIMx_BDTR register) and CC1S=00 (the channel is configured in
output).
2: The PWM mode can be used without validating the preload register only in one-
pulse mode (OPM bit set in TIMx_CR1 register). Else the behavior is not guaranteed.
Bit 2 OC1FE: Output compare 1 fast enable
This bit is used to accelerate the effect of an event on the trigger in input on the CC output.
0: CC1 behaves normally depending on counter and CCR1 values even when the trigger is
ON. The minimum delay to activate CC1 output when an edge occurs on the trigger input is
5 clock cycles.
1: An active edge on the trigger input acts like a compare match on CC1 output. Then, OC
is set to the compare level independently from the result of the comparison. Delay to sample
the trigger input and to activate CC1 output is reduced to 3 clock cycles. OCFE acts only if
the channel is configured in PWM1 or PWM2 mode.
Bits 1:0 CC1S: Capture/Compare 1 selection
This bit-field defines the direction of the channel (input/output) as well as the used input.
00: CC1 channel is configured as output.
01: CC1 channel is configured as input, IC1 is mapped on TI1.
10: CC1 channel is configured as input, IC1 is mapped on TI2.
11: CC1 channel is configured as input, IC1 is mapped on TRC. This mode is working only
if an internal trigger input is selected through TS bit (TIMx_SMCR register)
Note: CC1S bits are writable only when the channel is OFF (CC1E = 0 in TIMx_CCER).
RM0008 General-purpose timers (TIM2 to TIM5)
Doc ID 13902 Rev 13 397/1093
Input capture mode
Bits 15:12 IC2F: Input capture 2 filter
Bits 11:10 IC2PSC[1:0]: Input capture 2 prescaler
Bits 9:8 CC2S: Capture/compare 2 selection
This bit-field defines the direction of the channel (input/output) as well as the used input.
00: CC2 channel is configured as output.
01: CC2 channel is configured as input, IC2 is mapped on TI2.
10: CC2 channel is configured as input, IC2 is mapped on TI1.
11: CC2 channel is configured as input, IC2 is mapped on TRC. This mode is working only if
an internal trigger input is selected through TS bit (TIMx_SMCR register)
Note: CC2S bits are writable only when the channel is OFF (CC2E = 0 in TIMx_CCER).
Bits 7:4 IC1F: Input capture 1 filter
This bit-field defines the frequency used to sample TI1 input and the length of the digital filter
applied to TI1. The digital filter is made of an event counter in which N events are needed to
validate a transition on the output:
0000: No filter, sampling is done at f
DTS
1000: f
SAMPLING
=f
DTS
/8, N=6
0001: f
SAMPLING
=f
CK_INT
, N=2 1001: f
SAMPLING
=f
DTS
/8, N=8
0010: f
SAMPLING
=f
CK_INT
, N=4 1010: f
SAMPLING
=f
DTS
/16, N=5
0011: f
SAMPLING
=f
CK_INT
, N=8 1011: f
SAMPLING
=f
DTS
/16, N=6
0100: f
SAMPLING
=f
DTS
/2, N=6 1100: f
SAMPLING
=f
DTS
/16, N=8
0101: f
SAMPLING
=f
DTS
/2, N=8 1101: f
SAMPLING
=f
DTS
/32, N=5
0110: f
SAMPLING
=f
DTS
/4, N=6 1110: f
SAMPLING
=f
DTS
/32, N=6
0111: f
SAMPLING
=f
DTS
/4, N=8 1111: f
SAMPLING
=f
DTS
/32, N=8
Note: In current silicon revision, f
DTS
is replaced in the formula by CK_INT when ICxF[3:0]= 1,
2 or 3.
Bits 3:2 IC1PSC: Input capture 1 prescaler
This bit-field defines the ratio of the prescaler acting on CC1 input (IC1).
The prescaler is reset as soon as CC1E=0 (TIMx_CCER register).
00: no prescaler, capture is done each time an edge is detected on the capture input
01: capture is done once every 2 events
10: capture is done once every 4 events
11: capture is done once every 8 events
Bits 1:0 CC1S: Capture/Compare 1 selection
This bit-field defines the direction of the channel (input/output) as well as the used input.
00: CC1 channel is configured as output
01: CC1 channel is configured as input, IC1 is mapped on TI1
10: CC1 channel is configured as input, IC1 is mapped on TI2
11: CC1 channel is configured as input, IC1 is mapped on TRC. This mode is working only if
an internal trigger input is selected through TS bit (TIMx_SMCR register)
Note: CC1S bits are writable only when the channel is OFF (CC1E = 0 in TIMx_CCER).
General-purpose timers (TIM2 to TIM5) RM0008
398/1093 Doc ID 13902 Rev 13
15.4.8 TIMx capture/compare mode register 2 (TIMx_CCMR2)
Address offset: 0x1C
Reset value: 0x0000
Refer to the above CCMR1 register description.
Output compare mode
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
OC4CE OC4M[2:0] OC4PE OC4FE
CC4S[1:0]
OC3CE OC3M[2:0] OC3PE OC3FE
CC3S[1:0]
IC4F[3:0] IC4PSC[1:0] IC3F[3:0] IC3PSC[1:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bit 15 OC4CE: Output compare 4 clear enable
Bits 14:12 OC4M: Output compare 4 mode
Bit 11 OC4PE: Output compare 4 preload enable
Bit 10 OC4FE: Output compare 4 fast enable
Bits 9:8 CC4S: Capture/Compare 4 selection
This bit-field defines the direction of the channel (input/output) as well as the used input.
00: CC4 channel is configured as output
01: CC4 channel is configured as input, IC4 is mapped on TI4
10: CC4 channel is configured as input, IC4 is mapped on TI3
11: CC4 channel is configured as input, IC4 is mapped on TRC. This mode is working only if
an internal trigger input is selected through TS bit (TIMx_SMCR register)
Note: CC4S bits are writable only when the channel is OFF (CC4E = 0 in TIMx_CCER).
Bit 7 OC3CE: Output compare 3 clear enable
Bits 6:4 OC3M: Output compare 3 mode
Bit 3 OC3PE: Output compare 3 preload enable
Bit 2 OC3FE: Output compare 3 fast enable
Bits 1:0 CC3S: Capture/Compare 3 selection
This bit-field defines the direction of the channel (input/output) as well as the used input.
00: CC3 channel is configured as output
01: CC3 channel is configured as input, IC3 is mapped on TI3
10: CC3 channel is configured as input, IC3 is mapped on TI4
11: CC3 channel is configured as input, IC3 is mapped on TRC. This mode is working only if
an internal trigger input is selected through TS bit (TIMx_SMCR register)
Note: CC3S bits are writable only when the channel is OFF (CC3E = 0 in TIMx_CCER).
RM0008 General-purpose timers (TIM2 to TIM5)
Doc ID 13902 Rev 13 399/1093
Input capture mode
15.4.9 TIMx capture/compare enable register (TIMx_CCER)
Address offset: 0x20
Reset value: 0x0000
Bits 15:12 IC4F: Input capture 4 filter
Bits 11:10 IC4PSC: Input capture 4 prescaler
Bits 9:8 CC4S: Capture/Compare 4 selection
This bit-field defines the direction of the channel (input/output) as well as the used input.
00: CC4 channel is configured as output
01: CC4 channel is configured as input, IC4 is mapped on TI4
10: CC4 channel is configured as input, IC4 is mapped on TI3
11: CC4 channel is configured as input, IC4 is mapped on TRC. This mode is working only if
an internal trigger input is selected through TS bit (TIMx_SMCR register)
Note: CC4S bits are writable only when the channel is OFF (CC4E = 0 in TIMx_CCER).
Bits 7:4 IC3F: Input capture 3 filter
Bits 3:2 IC3PSC: Input capture 3 prescaler
Bits 1:0 CC3S: Capture/Compare 3 selection
This bit-field defines the direction of the channel (input/output) as well as the used input.
00: CC3 channel is configured as output
01: CC3 channel is configured as input, IC3 is mapped on TI3
10: CC3 channel is configured as input, IC3 is mapped on TI4
11: CC3 channel is configured as input, IC3 is mapped on TRC. This mode is working only if
an internal trigger input is selected through TS bit (TIMx_SMCR register)
Note: CC3S bits are writable only when the channel is OFF (CC3E = 0 in TIMx_CCER).
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
CC4P CC4E
Reserved
CC3P CC3E
Reserved
CC2P CC2E
Reserved
CC1P CC1E
rw rw rw rw rw rw rw rw
Bits 15:14 Reserved, must be kept at reset value.
Bit 13 CC4P: Capture/Compare 4 output polarity
refer to CC1P description
Bit 12 CC4E: Capture/Compare 4 output enable
refer to CC1E description
Bits 11:10 Reserved, must be kept at reset value.
Bit 9 CC3P: Capture/Compare 3 output polarity
refer to CC1P description
Bit 8 CC3E: Capture/Compare 3 output enable
refer to CC1E description
Bits 7:6 Reserved, must be kept at reset value.
Bit 5 CC2P: Capture/Compare 2 output polarity
refer to CC1P description
General-purpose timers (TIM2 to TIM5) RM0008
400/1093 Doc ID 13902 Rev 13
Note: The state of the external IO pins connected to the standard OCx channels depends on the
OCx channel state and the GPIOand AFIO registers.
15.4.10 TIMx counter (TIMx_CNT)
Address offset: 0x24
Reset value: 0x0000
Bit 4 CC2E: Capture/Compare 2 output enable
refer to CC1E description
Bits 3:2 Reserved, must be kept at reset value.
Bit 1 CC1P: Capture/Compare 1 output polarity
CC1 channel configured as output:
0: OC1 active high.
1: OC1 active low.
CC1 channel configured as input:
This bit selects whether IC1 or IC1 is used for trigger or capture operations.
0: non-inverted: capture is done on a rising edge of IC1. When used as external trigger, IC1
is non-inverted.
1: inverted: capture is done on a falling edge of IC1. When used as external trigger, IC1 is
inverted.
Bit 0 CC1E: Capture/Compare 1 output enable
CC1 channel configured as output:
0: Off - OC1 is not active.
1: On - OC1 signal is output on the corresponding output pin.
CC1 channel configured as input:
This bit determines if a capture of the counter value can actually be done into the input
capture/compare register 1 (TIMx_CCR1) or not.
0: Capture disabled.
1: Capture enabled.
Table 87. Output control bit for standard OCx channels
CCxE bit OCx output state
0 Output Disabled (OCx=0, OCx_EN=0)
1 OCx=OCxREF + Polarity, OCx_EN=1
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CNT[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 CNT[15:0]: Counter value
RM0008 General-purpose timers (TIM2 to TIM5)
Doc ID 13902 Rev 13 401/1093
15.4.11 TIMx prescaler (TIMx_PSC)
Address offset: 0x28
Reset value: 0x0000
15.4.12 TIMx auto-reload register (TIMx_ARR)
Address offset: 0x2C
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
PSC[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 PSC[15:0]: Prescaler value
The counter clock frequency CK_CNT is equal to f
CK_PSC
/ (PSC[15:0] + 1).
PSC contains the value to be loaded in the active prescaler register at each update event.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
ARR[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 ARR[15:0]: Prescalervalue
ARR is the value to be loaded in the actual auto-reload register.
Refer to the Section 15.3.1: Time-base unit on page 351 for more details about ARR update
and behavior.
The counter is blocked while the auto-reload value is null.
General-purpose timers (TIM2 to TIM5) RM0008
402/1093 Doc ID 13902 Rev 13
15.4.13 TIMx capture/compare register 1 (TIMx_CCR1)
Address offset: 0x34
Reset value: 0x0000
15.4.14 TIMx capture/compare register 2 (TIMx_CCR2)
Address offset: 0x38
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CCR1[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 CCR1[15:0]: Capture/Compare 1 value
If channel CC1 is configured as output:
CCR1 is the value to be loaded in the actual capture/compare 1 register (preload value).
It is loaded permanently if the preload feature is not selected in the TIMx_CCMR1 register (bit
OC1PE). Else the preload value is copied in the active capture/compare 1 register when an
update event occurs.
The active capture/compare register contains the value to be compared to the counter
TIMx_CNT and signaled on OC1 output.
If channel CC1is configured as input:
CCR1 is the counter value transferred by the last input capture 1 event (IC1).
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CCR2[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 CCR2[15:0]: Capture/Compare 2 value
If channel CC2 is configured as output:
CCR2 is the value to be loaded in the actual capture/compare 2 register (preload value).
It is loaded permanently if the preload feature is not selected in the TIMx_CCMR2 register
(bit OC2PE). Else the preload value is copied in the active capture/compare 2 register when
an update event occurs.
The active capture/compare register contains the value to be compared to the counter
TIMx_CNT and signalled on OC2 output.
If channel CC2 is configured as input:
CCR2 is the counter value transferred by the last input capture 2 event (IC2).
RM0008 General-purpose timers (TIM2 to TIM5)
Doc ID 13902 Rev 13 403/1093
15.4.15 TIMx capture/compare register 3 (TIMx_CCR3)
Address offset: 0x3C
Reset value: 0x0000
15.4.16 TIMx capture/compare register 4 (TIMx_CCR4)
Address offset: 0x40
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CCR3[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 CCR3[15:0]: Capture/Compare value
If channel CC3 is configured as output:
CCR3 is the value to be loaded in the actual capture/compare 3 register (preload value).
It is loaded permanently if the preload feature is not selected in the TIMx_CCMR3 register
(bit OC3PE). Else the preload value is copied in the active capture/compare 3 register when
an update event occurs.
The active capture/compare register contains the value to be compared to the counter
TIMx_CNT and signalled on OC3 output.
If channel CC3is configured as input:
CCR3 is the counter value transferred by the last input capture 3 event (IC3).
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CCR4[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 CCR4[15:0]: Capture/Compare value
1/ if CC4 channel is configured as output (CC4S bits):
CCR4 is the value to be loaded in the actual capture/compare 4 register (preload value).
It is loaded permanently if the preload feature is not selected in the TIMx_CCMR4 register (bit
OC4PE). Else the preload value is copied in the active capture/compare 4 register when an
update event occurs.
The active capture/compare register contains the value to be compared to the counter
TIMx_CNT and signalled on OC4 output.
2/ if CC4 channel is configured as input (CC4S bits in TIMx_CCMR4 register):
CCR4 is the counter value transferred by the last input capture 4 event (IC4).
General-purpose timers (TIM2 to TIM5) RM0008
404/1093 Doc ID 13902 Rev 13
15.4.17 TIMx DMA control register (TIMx_DCR)
Address offset: 0x48
Reset value: 0x0000
15.4.18 TIMx DMA address for full transfer (TIMx_DMAR)
Address offset: 0x4C
Reset value: 0x0000
Example of how to use the DMA burst feature
In this example the timer DMA burst feature is used to update the contents of the CCRx
registers (x = 2, 3, 4) with the DMA transferring half words into the CCRx registers.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
DBL[4:0]
Reserved
DBA[4:0]
rw rw rw rw rw rw rw rw rw rw
Bits 15:13 Reserved, always read as 0
Bits 12:8 DBL[4:0]: DMA burst length
This 5-bit vector defines the number of DMA transfers (the timer recognizes a burst transfer
when a read or a write access is done to the TIMx_DMAR address).
00000: 1 transfer,
00001: 2 transfers,
00010: 3 transfers,
...
10001: 18 transfers.
Bits 7:5 Reserved, always read as 0
Bits 4:0 DBA[4:0]: DMA base address
This 5-bit vector defines the base-address for DMA transfers (when read/write access are
done through the TIMx_DMAR address). DBA is defined as an offset starting from the
address of the TIMx_CR1 register.
Example:
00000: TIMx_CR1,
00001: TIMx_CR2,
00010: TIMx_SMCR,
...
Example: Let us consider the following transfer: DBL = 7 transfers & DBA = TIMx_CR1. In this
case the transfer is done to/from 7 registers starting from the TIMx_CR1 address..
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
DMAB[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 DMAB[15:0]: DMA register for burst accesses
A read or write operation to the DMAR register accesses the register located at the address
(TIMx_CR1 address) + (DBA + DMA index) x 4
where TIMx_CR1 address is the address of the control register 1, DBA is the DMA base
address configured in TIMx_DCR register, DMA index is automatically controlled by the DMA
transfer, and ranges from 0 to DBL (DBL configured in TIMx_DCR).
RM0008 General-purpose timers (TIM2 to TIM5)
Doc ID 13902 Rev 13 405/1093
This is done in the following steps:
1. Configure the corresponding DMA channel as follows:
DMA channel peripheral address is the DMAR register address
DMA channel memory address is the address of the buffer in the RAM containing
the data to be transferred by DMA into CCRx registers.
Number of data to transfer = 3 (See note below).
Circular mode disabled.
2. Configure the DCR register by configuring the DBA and DBL bit fields as follows:
DBL = 3 transfers, DBA = 0xE.
3. Enable the TIMx update DMA request (set the UDE bit in the DIER register).
4. Enable TIMx
5. Enable the DMA channel
Note: This example is for the case where every CCRx register to be updated once. If every CCRx
register is to be updated twice for example, the number of data to transfer should be 6. Let's
take the example of a buffer in the RAM containing data1, data2, data3, data4, data5 and
data6. The data is transferred to the CCRx registers as follows: on the first update DMA
request, data1 is transferred to CCR2, data2 is transferred to CCR3, data3 is transferred to
CCR4 and on the second update DMA request, data4 is transferred to CCR2, data5 is
transferred to CCR3 and data6 is transferred to CCR4.
15.4.19 TIMx register map
TIMx registers are mapped as described in the table below:
Table 88. TIMx register map and reset values
Offset Register
3
1
3
0
2
9
2
8
2
7
2
6
2
5
2
4
2
3
2
2
2
1
2
0
1
9
1
8
1
7
1
6
1
5
1
4
1
3
1
2
1
1
1
0
9 8 7 6 5 4 3 2 1 0
0x00
TIMx_CR1
Reserved
CKD
[1:0]
A
R
P
E
CMS
[1:0] D
I
R
O
P
M
U
R
S
U
D
I
S
C
E
N
Reset value 0 0 0 0 0 0 0 0 0 0
0x04
TIMx_CR2
Reserved
T
I
1
S MMS[2:0
]
C
C
D
S
Reserve
d
Reset value 0 0 0 0 0
0x08
TIMx_SMCR
Reserved
E
T
P
E
C
E
ETP
S
[1:0]
ETF[3:0]
M
S
M
TS[2:0]
R
e
s
e
r
v
e
d
SMS[2:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x0C
TIMx_DIER
Reserved
T
D
E
C
O
M
D
E
C
C
4
D
E
C
C
3
D
E
C
C
2
D
E
C
C
1
D
E
U
D
E
R
e
s
e
r
v
e
d
T
I
E
R
e
s
e
r
v
e
d
C
C
4
I
E
C
C
3
I
E
C
C
2
I
E
C
C
1
I
E
U
I
E
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0
0x10
TIMx_SR
Reserved
C
C
4
O
F
C
C
3
O
F
C
C
2
O
F
C
C
1
O
F
R
e
s
e
r
v
e
d
T
I
F
R
e
s
e
r
v
e
d
C
C
4
I
F
C
C
3
I
F
C
C
2
I
F
C
C
1
I
F
U
I
F
Reset value 0 0 0 0 0 0 0 0 0 0
0x14
TIMx_EGR
Reserved
T
G
R
e
s
e
r
v
e
d
C
C
4
G
C
C
3
G
C
C
2
G
C
C
1
G
U
G
Reset value 0 0 0 0 0 0
General-purpose timers (TIM2 to TIM5) RM0008
406/1093 Doc ID 13902 Rev 13
0x18
TIMx_CCMR1
Output
Compare
mode
Reserved
O
C
2
C
E
OC2M
[2:0]
O
C
2
P
E
O
C
2
F
E
CC2
S
[1:0]
O
C
1
C
E
OC1M
[2:0]
O
C
1
P
E
O
C
1
F
E
CC1
S
[1:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIMx_CCMR1
Input Capture
mode Reserved
IC2F[3:0]
IC2
PSC
[1:0]
CC2
S
[1:0]
IC1F[3:0]
IC1
PSC
[1:0]
CC1
S
[1:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x1C
TIMx_CCMR2
Output
Compare
mode
Reserved
O
2
4
C
E
OC4M
[2:0]
O
C
4
P
E
O
C
4
F
E
CC4
S
[1:0]
O
C
3
C
E
OC3M
[2:0]
O
C
3
P
E
O
C
3
F
E
CC3
S
[1:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIMx_CCMR2
Input Capture
mode Reserved
IC4F[3:0]
IC4
PSC
[1:0]
CC4
S
[1:0]
IC3F[3:0]
IC3
PSC
[1:0]
CC3
S
[1:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x20
TIMx_CCER
Reserved C
C
4
P
C
C
4
E
R
e
s
e
r
v
e
d
C
C
3
P
C
C
3
E
R
e
s
e
r
v
e
d
C
C
2
P
C
C
2
E
R
e
s
e
r
v
e
d
C
C
1
P
C
C
1
E
Reset value 0 0 0 0 0 0 0 0
0x24
TIMx_CNT CNT[32:16] (TIM5 only, reserved on the other timers) CNT[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x28
TIMx_PSC
Reserved
PSC[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x2C
TIMx_ARR
Reserved
ARR[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x30 Reserved
0x34
TIMx_CCR1
Reserved
CCR1[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x38
TIMx_CCR2
Reserved
CCR2[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x3C
TIMx_CCR3
Reserved
CCR3[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x40
TIMx_CCR4
Reserved
CCR4[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x44 Reserved
Table 88. TIMx register map and reset values
Offset Register
3
1
3
0
2
9
2
8
2
7
2
6
2
5
2
4
2
3
2
2
2
1
2
0
1
9
1
8
1
7
1
6
1
5
1
4
1
3
1
2
1
1
1
0
9 8 7 6 5 4 3 2 1 0
RM0008 General-purpose timers (TIM2 to TIM5)
Doc ID 13902 Rev 13 407/1093
Refer to Table 1 on page 50 for the register boundary addresses.
0x48
TIMx_DCR
Reserved
DBL[4:0]
Reserve
d
DBA[4:0]
Reset value 0 0 0 0 0 0 0 0 0 0
0x4C
TIMx_DMAR
Reserved
DMAB[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Table 88. TIMx register map and reset values
Offset Register
3
1
3
0
2
9
2
8
2
7
2
6
2
5
2
4
2
3
2
2
2
1
2
0
1
9
1
8
1
7
1
6
1
5
1
4
1
3
1
2
1
1
1
0
9 8 7 6 5 4 3 2 1 0
General-purpose timers (TIM9 to TIM14) RM0008
408/1093 Doc ID 13902 Rev 13
16 General-purpose timers (TIM9 to TIM14)
Low-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 16 and 32 Kbytes.
Medium-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 64 and 128 Kbytes.
High-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 256 and 512 Kbytes.
XL-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 768 Kbytes and 1 Mbyte.
Connectivity line devices are STM32F105xx and STM32F107xx microcontrollers.
This section applies to XL-density devices only.
16.1 TIM9 to TIM14 introduction
The TIM9 to TIM14 general-purpose timers consist of a 16-bit auto-reload counter driven by
a programmable prescaler.
They may be used for a variety of purposes, including measuring the pulse lengths of input
signals (input capture) or generating output waveforms (output compare, PWM).
Pulse lengths and waveform periods can be modulated from a few microseconds to several
milliseconds using the timer prescaler and the RCC clock controller prescalers.
The TIM9 to TIM14 timers are completely independent, and do not share any resources.
They can be synchronized together as described in Section 16.4.12.
RM0008 General-purpose timers (TIM9 to TIM14)
Doc ID 13902 Rev 13 409/1093
16.2 TIM9 to TIM14 main features
16.2.1 TIM9/TIM12 main features
The features of the TIM9/TIM12 general-purpose timers include:
16-bit auto-reload upcounter
16-bit programmable prescaler used to divide the counter clock frequency by any factor
between 1 and 65535 (can be changed on the fly)
Up to 2 independent channels for:
Input capture
Output compare
PWM generation (edge-aligned mode)
One-pulse mode output
Synchronization circuit to control the timer with external signals and to interconnect
several timers together
Interrupt generation on the following events:
Update: counter overflow, counter initialization (by software or internal trigger)
Trigger event (counter start, stop, initialization or count by internal trigger)
Input capture
Output compare
Figure 146. General-purpose timer block diagram (TIM9 and TIM12)
Auto-reload register
Capture/Compare 1 register
Capture/Compare 2 register
U
U
U
CC1I
CC2I
Trigger
controller
Stop, Clear
TI1FP1
TI2FP2
ITR0
ITR1
ITR2
ITR3
TRGI
output
control
OC1 OC1REF
OC2REF
U
UI
Reset, Enable, Count
IC1
IC2
Prescaler
Prescaler
Input filter &
Edge detector
IC2PS
IC1PS
TI1FP1
output
control
OC2
Reg
event
Notes:
Preload registers transferred
to active registers on U event
according to control bit
interrupt
TGI
TRC
TRC
ITR
TRC
TI1F_ED
Input filter &
Edge detector
CC1I
CC2I
TI1FP2
TI2FP1
TI2FP2
TI1
TI2
TIMx_CH1
TIMx_CH2
TIMx_CH1
TIMx_CH2
Prescaler COUNTER
+/-
CK_PSC
PSC CNT
CK_CNT
controller
mode
Slave
Internal clock (CK_INT)
ai17190
General-purpose timers (TIM9 to TIM14) RM0008
410/1093 Doc ID 13902 Rev 13
16.3 TIM10/TIM11 and TIM13/TIM14 main features
The features of general-purpose timers TIM10/TIM11 and TIM13/TIM14 include:
16-bit auto-reload upcounter
16-bit programmable prescaler used to divide the counter clock frequency by any factor
between 1 and 65535 (can be changed on the fly)
independent channel for:
Input capture
Output compare
PWM generation (edge-aligned mode)
Interrupt generation on the following events:
Update: counter overflow, counter initialization (by software)
Input capture
Output compare
Figure 147. General-purpose timer block diagram (TIM10/11/13/14)
Autoreload register
Capture/Compare 1 register
U
U
CC1
Stop, Clear
output
control
OC1 OC1REF
U
U
C1
Prescaler
nput filter &
edge detector
C1PS
T1FP1
Reg
event
Notes:
Preload registers transferred
to active registers on U event
according to control bit
interrupt & DMA output
CC1
T1
TMx_CH1
prescaler counter
+/-
CK_PSC
PSC CNT
CK_CNT
nternal clock (CK_NT)
MS19801V1
Trigger
Controller
Enable
counter
TMx_CH1
RM0008 General-purpose timers (TIM9 to TIM14)
Doc ID 13902 Rev 13 411/1093
16.4 TIM9 to TIM14 functional description
16.4.1 Time-base unit
The main block of the programmable advanced-control timer is a 16-bit counter with its
related auto-reload register. The counter can count up. The counter clock can be divided by
a prescaler.
The counter, the auto-reload register and the prescaler register can be written or read by
software. This is true even when the counter is running.
The time-base unit includes:
Counter register (TIMx_CNT)
Prescaler register (TIMx_PSC)
Auto-reload register (TIMx_ARR)
The auto-reload register is preloaded. Writing to or reading from the auto-reload register
accesses the preload register. The content of the preload register are transferred into the
shadow register permanently or at each update event (UEV), depending on the auto-reload
preload enable bit (ARPE) in TIMx_CR1 register. The update event is sent when the counter
reaches the overflow and if the UDIS bit equals 0 in the TIMx_CR1 register. It can also be
generated by software. The generation of the update event is described in detailed for each
configuration.
The counter is clocked by the prescaler output CK_CNT, which is enabled only when the
counter enable bit (CEN) in TIMx_CR1 register is set (refer also to the slave mode controller
description to get more details on counter enabling).
Note that the counter starts counting 1 clock cycle after setting the CEN bit in the TIMx_CR1
register.
Prescaler description
The prescaler can divide the counter clock frequency by any factor between 1 and 65536. It
is based on a 16-bit counter controlled through a 16-bit register (in the TIMx_PSC register).
It can be changed on the fly as this control register is buffered. The new prescaler ratio is
taken into account at the next update event.
Figure 149 and Figure 150 give some examples of the counter behavior when the prescaler
ratio is changed on the fly.
General-purpose timers (TIM9 to TIM14) RM0008
412/1093 Doc ID 13902 Rev 13
Figure 148. Counter timing diagram with prescaler division change from 1 to 2
Figure 149. Counter timing diagram with prescaler division change from 1 to 4
16.4.2 Counter modes
Upcounting mode
In upcounting mode, the counter counts from 0 to the auto-reload value (content of the
TIMx_ARR register), then restarts from 0 and generates a counter overflow event.
Setting the UG bit in the TIMx_EGR register (by software or by using the slave mode
controller on TIM9 and TIM12) also generates an update event.
The UEV event can be disabled by software by setting the UDIS bit in the TIMx_CR1
register. This is to avoid updating the shadow registers while writing new values in the
preload registers. Then no update event occurs until the UDIS bit has been written to 0.
However, the counter restarts from 0, as well as the counter of the prescaler (but the
prescale rate does not change). In addition, if the URS bit (update request selection) in
TIMx_CR1 register is set, setting the UG bit generates an update event UEV but without
CK_PSC
00
CEN
Timer clock = CK_CNT
Counter register
Update event (UEV)
0
F9 FA FB FC F7
Prescaler control register 0 1
Write a new value in TIMx_PSC
01 02 03
Prescaler buffer 0 1
Prescaler counter 0 1 0 1 0 1 0 1
F8
CK_PSC
00
CEN
Timer clock = CK_CNT
Counter register
Update event (UEV)
0
F9 FA FB FC F7
Prescaler control register 0 3
Write a new value in TIMx_PSC
Prescaler buffer 0 3
Prescaler counter 0 1 2 3 0 1 2 3
F8 01
RM0008 General-purpose timers (TIM9 to TIM14)
Doc ID 13902 Rev 13 413/1093
setting the UIF flag (thus no interrupt is sent). This is to avoid generating both update and
capture interrupts when clearing the counter on the capture event.
When an update event occurs, all the registers are updated and the update flag (UIF bit in
TIMx_SR register) is set (depending on the URS bit):
The auto-reload shadow register is updated with the preload value (TIMx_ARR),
The buffer of the prescaler is reloaded with the preload value (content of the TIMx_PSC
register).
The following figures show some examples of the counter behavior for different clock
frequencies when TIMx_ARR=0x36.
Figure 150. Counter timing diagram, internal clock divided by 1
Figure 151. Counter timing diagram, internal clock divided by 2
CK_PSC
00
CNT_EN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
Counter overflow
Update event (UEV)
01 02 03 04 05 06 07 32 33 34 35 36 31
CK_PSC
0035 0000 0001 0002 0003
CNT_EN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
0034 0036
Counter overflow
Update event (UEV)
General-purpose timers (TIM9 to TIM14) RM0008
414/1093 Doc ID 13902 Rev 13
Figure 152. Counter timing diagram, internal clock divided by 4
Figure 153. Counter timing diagram, internal clock divided by N
Figure 154. Counter timing diagram, update event when ARPE=0 (TIMx_ARR not
preloaded)
CK_PSC
0000 0001
CNT_EN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
0035 0036
Counter overflow
Update event (UEV)
Timer clock = CK_CNT
Counter register 00 1F 20
Update interrupt flag (UIF)
Counter overflow
Update event (UEV)
CK_PSC
CK_PSC
00
CEN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
Counter overflow
Update event (UEV)
01 02 03 04 05 06 07 32 33 34 35 36 31
Auto-reload register FF 36
Write a new value in TIMx_ARR
RM0008 General-purpose timers (TIM9 to TIM14)
Doc ID 13902 Rev 13 415/1093
Figure 155. Counter timing diagram, update event when ARPE=1 (TIMx_ARR
preloaded)
16.4.3 Clock selection
The counter clock can be provided by the following clock sources:
Internal clock (CK_INT)
External clock mode1 (for TIM9 and TIM12): external input pin (TIx)
Internal trigger inputs (ITRx) (for TIM9 and TIM12): connecting the trigger output from
another timer. Refer to Using one timer as prescaler for another for more details.
Internal clock source (CK_INT)
The internal clock source is the default clock source for TIM10/TIM11 and TIM13/TIM14.
For TIM9 and TIM12, the internal clock source is selected when the slave mode controller is
disabled (SMS=000). The CEN bit in the TIMx_CR1 register and the UG bit in the
TIMx_EGR register are then used as control bits and can be changed only by software
(except for UG which remains cleared). As soon as the CEN bit is programmed to 1, the
prescaler is clocked by the internal clock CK_INT.
Figure 156 shows the behavior of the control circuit and the upcounter in normal mode,
without prescaler.
CK_PSC
00
CEN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
Counter overflow
Update event (UEV)
01 02 03 04 05 06 07 F1 F2 F3 F4 F5 F0
Auto-reload preload register F5 36
Auto-reload shadow register F5 36
Write a new value in TIMx_ARR
General-purpose timers (TIM9 to TIM14) RM0008
416/1093 Doc ID 13902 Rev 13
Figure 156. Control circuit in normal mode, internal clock divided by 1
External clock source mode 1(TIM9 and TIM12)
This mode is selected when SMS=111 in the TIMx_SMCR register. The counter can count
at each rising or falling edge on a selected input.
Figure 157. TI2 external clock connection example
For example, to configure the upcounter to count in response to a rising edge on the TI2
input, use the following procedure:
1. Configure channel 2 to detect rising edges on the TI2 input by writing CC2S = 01 in
the TIMx_CCMR1 register.
2. Configure the input filter duration by writing the IC2F[3:0] bits in the TIMx_CCMR1
register (if no filter is needed, keep IC2F=0000).
3. Select the rising edge polarity by writing CC2P=0 and CC2NP=0 in the TIMx_CCER
register.
4. Configure the timer in external clock mode 1 by writing SMS=111 in the TIMx_SMCR
register.
5. Select TI2 as the trigger input source by writing TS=110 in the TIMx_SMCR register.
6. Enable the counter by writing CEN=1 in the TIMx_CR1 register.
Note: The capture prescaler is not used for triggering, so you dont need to configure it.
When a rising edge occurs on TI2, the counter counts once and the TIF flag is set.
The delay between the rising edge on TI2 and the actual clock of the counter is due to the
resynchronization circuit on TI2 input.
Internal clock
00
Counter clock = CK_CNT = CK_PSC
Counter register 01 02 03 04 05 06 07 32 33 34 35 36 31
CEN=CNT_EN
UG
CNT_INIT
CK_INT
external clock
mode 1
internal clock
mode
TRGI
TI1F
TI2F or
or
or
(internal clock)
CK_PSC
TIMx_SMCR
SMS[2:0]
ITRx
TI1_ED
TI1FP1
TI2FP2
TIMx_SMCR
TS[2:0]
TI2
0
1
TIMx_CCER
CC2P
Filter
ICF[3:0]
TIMx_CCMR1
Edge
Detector
TI2F_Rising
TI2F_Falling
110
0xx
100
101
RM0008 General-purpose timers (TIM9 to TIM14)
Doc ID 13902 Rev 13 417/1093
Figure 158. Control circuit in external clock mode 1
16.4.4 Capture/compare channels
Each Capture/Compare channel is built around a capture/compare register (including a
shadow register), a input stage for capture (with digital filter, multiplexing and prescaler) and
an output stage (with comparator and output control).
Figure 159 to Figure 161 give an overview of one capture/compare channel.
The input stage samples the corresponding TIx input to generate a filtered signal TIxF.
Then, an edge detector with polarity selection generates a signal (TIxFPx) which can be
used as trigger input by the slave mode controller or as the capture command. It is
prescaled before the capture register (ICxPS).
Figure 159. Capture/compare channel (example: channel 1 input stage)
The output stage generates an intermediate waveform which is then used for reference:
OCxRef (active high). The polarity acts at the end of the chain.
Counter clock = CK_CNT = CK_PSC
Counter register 35 36 34
TI2
CNT_EN
TIF
Write TIF=0
TI1
0
1
TIMx_CCER
CC1P/CC1NP
divider
/1, /2, /4, /8
ICPS[1:0]
TI1F_ED
filter
ICF[3:0]
downcounter
TIMx_CCMR1
Edge
Detector
TI1F_Rising
TI1F_Falling
to the slave mode controller
TI1FP1
11
01
TIMx_CCMR1
CC1S[1:0]
IC1
TI2FP1
TRC
(from channel 2)
(from slave mode
controller)
10
f
DTS
TIMx_CCER
CC1E
IC1PS
TI1F
0
1
TI2F_rising
TI2F_falling
(from channel 2)
General-purpose timers (TIM9 to TIM14) RM0008
418/1093 Doc ID 13902 Rev 13
Figure 160. Capture/compare channel 1 main circuit
Figure 161. Output stage of capture/compare channel (channel 1)
The capture/compare block is made of one preload register and one shadow register. Write
and read always access the preload register.
In capture mode, captures are actually done in the shadow register, which is copied into the
preload register.
In compare mode, the content of the preload register is copied into the shadow register
which is compared to the counter.
16.4.5 Input capture mode
In Input capture mode, the Capture/Compare Registers (TIMx_CCRx) are used to latch the
value of the counter after a transition detected by the corresponding ICx signal. When a
capture occurs, the corresponding CCXIF flag (TIMx_SR register) is set and an interrupt or
a DMA request can be sent if they are enabled. If a capture occurs while the CCxIF flag was
already high, then the over-capture flag CCxOF (TIMx_SR register) is set. CCxIF can be
CC1E
Capture/compare shadow register
comparator
Capture/compare preload register
Counter
IC1PS
CC1S[0]
CC1S[1]
capture
input
mode
S
R
read CCR1H
read CCR1L
read_in_progress
capture_transfer
CC1S[0]
CC1S[1]
S
R
write CCR1H
write CCR1L
write_in_progress
output
mode
UEV
OC1PE
(from time
compare_transfer
APB Bus
8 8
h
i
g
h
l
o
w
(
i
f
1
6
-
b
i
t
)
MCU-peripheral interface
TIM1_CCMR1
OC1PE
base unit)
CNT>CCR1
CNT=CCR1
TIM1_EGR
CC1G
Output mode
CNT > CCR1
CNT = CCR1 controller
TMx_CCMR1
OC1M[2:0]
OC1_REF
0
1
CC1P
TMx_CCER
Output
enable
circuit
OC1
CC1E TMx_CCER
To the master mode
controller
ai17720
RM0008 General-purpose timers (TIM9 to TIM14)
Doc ID 13902 Rev 13 419/1093
cleared by software by writing it to 0 or by reading the captured data stored in the
TIMx_CCRx register. CCxOF is cleared when you write it to 0.
The following example shows how to capture the counter value in TIMx_CCR1 when TI1
input rises. To do this, use the following procedure:
1. Select the active input: TIMx_CCR1 must be linked to the TI1 input, so write the CC1S
bits to 01 in the TIMx_CCMR1 register. As soon as CC1S becomes different from 00,
the channel is configured in input mode and the TIMx_CCR1 register becomes read-
only.
2. Program the input filter duration you need with respect to the signal you connect to the
timer (when the input is one of the TIx (ICxF bits in the TIMx_CCMRx register). Lets
imagine that, when toggling, the input signal is not stable during at must 5 internal clock
cycles. We must program a filter duration longer than these 5 clock cycles. We can
validate a transition on TI1 when 8 consecutive samples with the new level have been
detected (sampled at f
DTS
frequency). Then write IC1F bits to 0011 in the
TIMx_CCMR1 register.
3. Select the edge of the active transition on the TI1 channel by programming CC1P and
CC1NP bits to 00 in the TIMx_CCER register (rising edge in this case).
4. Program the input prescaler. In our example, we wish the capture to be performed at
each valid transition, so the prescaler is disabled (write IC1PS bits to 00 in the
TIMx_CCMR1 register).
5. Enable capture from the counter into the capture register by setting the CC1E bit in the
TIMx_CCER register.
6. If needed, enable the related interrupt request by setting the CC1IE bit in the
TIMx_DIER register.
When an input capture occurs:
The TIMx_CCR1 register gets the value of the counter on the active transition.
CC1IF flag is set (interrupt flag). CC1OF is also set if at least two consecutive captures
occurred whereas the flag was not cleared.
An interrupt is generated depending on the CC1IE bit.
In order to handle the overcapture, it is recommended to read the data before the
overcapture flag. This is to avoid missing an overcapture which could happen after reading
the flag and before reading the data.
Note: IC interrupt requests can be generated by software by setting the corresponding CCxG bit in
the TIMx_EGR register.
16.4.6 PWM input mode (only for TIM9/12)
This mode is a particular case of input capture mode. The procedure is the same except:
Two ICx signals are mapped on the same TIx input.
These 2 ICx signals are active on edges with opposite polarity.
One of the two TIxFP signals is selected as trigger input and the slave mode controller
is configured in reset mode.
For example, you can measure the period (in TIMx_CCR1 register) and the duty cycle (in
TIMx_CCR2 register) of the PWM applied on TI1 using the following procedure (depending
on CK_INT frequency and prescaler value):
General-purpose timers (TIM9 to TIM14) RM0008
420/1093 Doc ID 13902 Rev 13
1. Select the active input for TIMx_CCR1: write the CC1S bits to 01 in the TIMx_CCMR1
register (TI1 selected).
2. Select the active polarity for TI1FP1 (used both for capture in TIMx_CCR1 and counter
clear): program the CC1P and CC1NP bits to 00 (active on rising edge).
3. Select the active input for TIMx_CCR2: write the CC2S bits to 10 in the TIMx_CCMR1
register (TI1 selected).
4. Select the active polarity for TI1FP2 (used for capture in TIMx_CCR2): program the
CC2P and CC2NP bits to 11 (active on falling edge).
5. Select the valid trigger input: write the TS bits to 101 in the TIMx_SMCR register
(TI1FP1 selected).
6. Configure the slave mode controller in reset mode: write the SMS bits to 100 in the
TIMx_SMCR register.
7. Enable the captures: write the CC1E and CC2E bits to 1 in the TIMx_CCER register.
Figure 162. PWM input mode timing
1. The PWM input mode can be used only with the TIMx_CH1/TIMx_CH2 signals due to the fact that only
TI1FP1 and TI2FP2 are connected to the slave mode controller.
16.4.7 Forced output mode
In output mode (CCxS bits = 00 in the TIMx_CCMRx register), each output compare signal
(OCxREF and then OCx) can be forced to active or inactive level directly by software,
independently of any comparison between the output compare register and the counter.
To force an output compare signal (OCXREF/OCx) to its active level, you just need to write
101 in the OCxM bits in the corresponding TIMx_CCMRx register. Thus OCXREF is forced
high (OCxREF is always active high) and OCx get opposite value to CCxP polarity bit.
For example: CCxP=0 (OCx active high) => OCx is forced to high level.
The OCxREF signal can be forced low by writing the OCxM bits to 100 in the TIMx_CCMRx
register.
Anyway, the comparison between the TIMx_CCRx shadow register and the counter is still
performed and allows the flag to be set. Interrupt requests can be sent accordingly. This is
described in the output compare mode section below.
TI1
TIMx_CNT 0000 0001 0002 0003 0004 0000 0004
TIMx_CCR1
TIMx_CCR2
0004
0002
IC1 capture
IC2 capture
reset counter
IC2 capture
pulse width
IC1 capture
period
measurement measurement
ai15413
RM0008 General-purpose timers (TIM9 to TIM14)
Doc ID 13902 Rev 13 421/1093
16.4.8 Output compare mode
This function is used to control an output waveform or indicating when a period of time has
elapsed.
When a match is found between the capture/compare register and the counter, the output
compare function:
1. Assigns the corresponding output pin to a programmable value defined by the output
compare mode (OCxM bits in the TIMx_CCMRx register) and the output polarity (CCxP
bit in the TIMx_CCER register). The output pin can keep its level (OCXM=000), be set
active (OCxM=001), be set inactive (OCxM=010) or can toggle (OCxM=011) on
match.
2. Sets a flag in the interrupt status register (CCxIF bit in the TIMx_SR register).
3. Generates an interrupt if the corresponding interrupt mask is set (CCXIE bit in the
TIMx_DIER register).
The TIMx_CCRx registers can be programmed with or without preload registers using the
OCxPE bit in the TIMx_CCMRx register.
In output compare mode, the update event UEV has no effect on OCxREF and OCx output.
The timing resolution is one count of the counter. Output compare mode can also be used to
output a single pulse (in One-pulse mode).
Procedure:
1. Select the counter clock (internal, external, prescaler).
2. Write the desired data in the TIMx_ARR and TIMx_CCRx registers.
3. Set the CCxIE bit if an interrupt request is to be generated.
4. Select the output mode. For example:
Write OCxM = 011 to toggle OCx output pin when CNT matches CCRx
Write OCxPE = 0 to disable preload register
Write CCxP = 0 to select active high polarity
Write CCxE = 1 to enable the output
5. Enable the counter by setting the CEN bit in the TIMx_CR1 register.
The TIMx_CCRx register can be updated at any time by software to control the output
waveform, provided that the preload register is not enabled (OCxPE=0, else TIMx_CCRx
shadow register is updated only at the next update event UEV). An example is given in
Figure 163.
General-purpose timers (TIM9 to TIM14) RM0008
422/1093 Doc ID 13902 Rev 13
Figure 163. Output compare mode, toggle on OC1.
16.4.9 PWM mode
Pulse Width Modulation mode allows you to generate a signal with a frequency determined
by the value of the TIMx_ARR register and a duty cycle determined by the value of the
TIMx_CCRx register.
The PWM mode can be selected independently on each channel (one PWM per OCx
output) by writing 110 (PWM mode 1) or 111 (PWM mode 2) in the OCxM bits in the
TIMx_CCMRx register. You must enable the corresponding preload register by setting the
OCxPE bit in the TIMx_CCMRx register, and eventually the auto-reload preload register (in
upcounting or center-aligned modes) by setting the ARPE bit in the TIMx_CR1 register.
As the preload registers are transferred to the shadow registers only when an update event
occurs, before starting the counter, you have to initialize all the registers by setting the UG
bit in the TIMx_EGR register.
The OCx polarity is software programmable using the CCxP bit in the TIMx_CCER register.
It can be programmed as active high or active low. The OCx output is enabled by the CCxE
bit in the TIMx_CCER register. Refer to the TIMx_CCERx register description for more
details.
In PWM mode (1 or 2), TIMx_CNT and TIMx_CCRx are always compared to determine
whether TIMx_CNT TIMx_CCRx.
The timer is able to generate PWM in edge-aligned mode only since the counter is
upcounting.
PWM edge-aligned mode
In the following example, we consider PWM mode 1. The reference PWM signal OCxREF is
high as long as TIMx_CNT < TIMx_CCRx else it becomes low. If the compare value in
TIMx_CCRx is greater than the auto-reload value (in TIMx_ARR) then OCxREF is held at
1. If the compare value is 0 then OCxRef is held at 0. Figure 164 shows some edge-
aligned PWM waveforms in an example where TIMx_ARR=8.
oc1ref=OC1
TIM1_CNT B200 B201 0039
TIM1_CCR1 003A
Write B201h in the CC1R register
Match detected on CCR1
Interrupt generated if enabled
003B
B201
003A
RM0008 General-purpose timers (TIM9 to TIM14)
Doc ID 13902 Rev 13 423/1093
Figure 164. Edge-aligned PWM waveforms (ARR=8)
16.4.10 One-pulse mode (only for TIM9/12)
One-pulse mode (OPM) is a particular case of the previous modes. It allows the counter to
be started in response to a stimulus and to generate a pulse with a programmable length
after a programmable delay.
Starting the counter can be controlled through the slave mode controller. Generating the
waveform can be done in output compare mode or PWM mode. You select One-pulse mode
by setting the OPM bit in the TIMx_CR1 register. This makes the counter stop automatically
at the next update event UEV.
A pulse can be correctly generated only if the compare value is different from the counter
initial value. Before starting (when the timer is waiting for the trigger), the configuration must
be as follows:
CNT < CCRx ARR (in particular, 0 < CCRx)
Figure 165. Example of one pulse mode.
Counter register
0 1 2 3 4 5 6 7 8 0 1
OCXREF
CCxIF
OCXREF
CCxIF
OCXREF
CCxIF
OCXREF
CCxIF
CCRx=4
CCRx=8
CCRx>8
CCRx=0
TI2
OC1REF
C
o
u
n
t
e
r
t
0
TIM1_ARR
TIM1_CCR1
OC1
t
DELAY
t
PULSE
General-purpose timers (TIM9 to TIM14) RM0008
424/1093 Doc ID 13902 Rev 13
For example you may want to generate a positive pulse on OC1 with a length of t
PULSE
and
after a delay of t
DELAY
as soon as a positive edge is detected on the TI2 input pin.
Use TI2FP2 as trigger 1:
1. Map TI2FP2 to TI2 by writing CC2S=01 in the TIMx_CCMR1 register.
2. TI2FP2 must detect a rising edge, write CC2P=0 and CC2NP = 0 in the TIMx_CCER
register.
3. Configure TI2FP2 as trigger for the slave mode controller (TRGI) by writing TS=110 in
the TIMx_SMCR register.
4. TI2FP2 is used to start the counter by writing SMS to 110 in the TIMx_SMCR register
(trigger mode).
The OPM waveform is defined by writing the compare registers (taking into account the
clock frequency and the counter prescaler).
The t
DELAY
is defined by the value written in the TIMx_CCR1 register.
The t
PULSE
is defined by the difference between the auto-reload value and the compare
value (TIMx_ARR - TIMx_CCR1).
Lets say you want to build a waveform with a transition from 0 to 1 when a compare
match occurs and a transition from 1 to 0 when the counter reaches the auto-reload
value. To do this you enable PWM mode 2 by writing OC1M=111 in the TIMx_CCMR1
register. You can optionally enable the preload registers by writing OC1PE=1 in the
TIMx_CCMR1 register and ARPE in the TIMx_CR1 register. In this case you have to
write the compare value in the TIMx_CCR1 register, the auto-reload value in the
TIMx_ARR register, generate an update by setting the UG bit and wait for external
trigger event on TI2. CC1P is written to 0 in this example.
You only want 1 pulse (Single mode), so you write '1 in the OPM bit in the TIMx_CR1
register to stop the counter at the next update event (when the counter rolls over from the
auto-reload value back to 0). When OPM bit in the TIMx_CR1 register is set to '0', so the
Repetitive Mode is selected.
Particular case: OCx fast enable
In One-pulse mode, the edge detection on TIx input set the CEN bit which enables the
counter. Then the comparison between the counter and the compare value makes the
output toggle. But several clock cycles are needed for these operations and it limits the
minimum delay t
DELAY
min we can get.
If you want to output a waveform with the minimum delay, you can set the OCxFE bit in the
TIMx_CCMRx register. Then OCxRef (and OCx) are forced in response to the stimulus,
without taking in account the comparison. Its new level is the same as if a compare match
had occurred. OCxFE acts only if the channel is configured in PWM1 or PWM2 mode.
16.4.11 TIM9/12 external trigger synchronization
The TIM9/12 timers can be synchronized with an external trigger in several modes: Reset
mode, Gated mode and Trigger mode.
Slave mode: Reset mode
The counter and its prescaler can be reinitialized in response to an event on a trigger input.
Moreover, if the URS bit from the TIMx_CR1 register is low, an update event UEV is
generated. Then all the preloaded registers (TIMx_ARR, TIMx_CCRx) are updated.
In the following example, the upcounter is cleared in response to a rising edge on TI1 input:
RM0008 General-purpose timers (TIM9 to TIM14)
Doc ID 13902 Rev 13 425/1093
1. Configure the channel 1 to detect rising edges on TI1. Configure the input filter duration
(in this example, we dont need any filter, so we keep IC1F=0000). The capture
prescaler is not used for triggering, so you dont need to configure it. The CC1S bits
select the input capture source only, CC1S = 01 in the TIMx_CCMR1 register.
Program CC1P and CC1NP to 00 in TIMx_CCER register to validate the polarity (and
detect rising edges only).
2. Configure the timer in reset mode by writing SMS=100 in TIMx_SMCR register. Select
TI1 as the input source by writing TS=101 in TIMx_SMCR register.
3. Start the counter by writing CEN=1 in the TIMx_CR1 register.
The counter starts counting on the internal clock, then behaves normally until TI1 rising
edge. When TI1 rises, the counter is cleared and restarts from 0. In the meantime, the
trigger flag is set (TIF bit in the TIMx_SR register) and an interrupt request can be sent if
enabled (depending on the TIE bit in TIMx_DIER register).
The following figure shows this behavior when the auto-reload register TIMx_ARR=0x36.
The delay between the rising edge on TI1 and the actual reset of the counter is due to the
resynchronization circuit on TI1 input.
Figure 166. Control circuit in reset mode
Slave mode: Gated mode
The counter can be enabled depending on the level of a selected input.
In the following example, the upcounter counts only when TI1 input is low:
1. Configure the channel 1 to detect low levels on TI1. Configure the input filter duration
(in this example, we dont need any filter, so we keep IC1F=0000). The capture
prescaler is not used for triggering, so you dont need to configure it. The CC1S bits
select the input capture source only, CC1S=01 in TIMx_CCMR1 register. Program
CC1P=1 and CC1NP= 0 in TIMx_CCER register to validate the polarity (and detect
low level only).
2. Configure the timer in gated mode by writing SMS=101 in TIMx_SMCR register.
Select TI1 as the input source by writing TS=101 in TIMx_SMCR register.
3. Enable the counter by writing CEN=1 in the TIMx_CR1 register (in gated mode, the
counter doesnt start if CEN=0, whatever is the trigger input level).
The counter starts counting on the internal clock as long as TI1 is low and stops as soon as
TI1 becomes high. The TIF flag in the TIMx_SR register is set both when the counter starts
or stops.
The delay between the rising edge on TI1 and the actual stop of the counter is due to the
resynchronization circuit on TI1 input.
00
Counter clock = ck_cnt = ck_psc
Counter register 01 02 03 00 01 02 03 32 33 34 35 36
UG
TI1
31 30
TIF
General-purpose timers (TIM9 to TIM14) RM0008
426/1093 Doc ID 13902 Rev 13
Figure 167. Control circuit in gated mode
Slave mode: Trigger mode
The counter can start in response to an event on a selected input.
In the following example, the upcounter starts in response to a rising edge on TI2 input:
1. Configure the channel 2 to detect rising edges on TI2. Configure the input filter duration
(in this example, we dont need any filter, so we keep IC2F=0000). The capture
prescaler is not used for triggering, so you dont need to configure it. The CC2S bits are
configured to select the input capture source only, CC2S=01 in TIMx_CCMR1 register.
Program CC2P=1 and CC2NP=0 in TIMx_CCER register to validate the polarity (and
detect low level only).
2. Configure the timer in trigger mode by writing SMS=110 in TIMx_SMCR register.
Select TI2 as the input source by writing TS=110 in TIMx_SMCR register.
When a rising edge occurs on TI2, the counter starts counting on the internal clock and the
TIF flag is set.
The delay between the rising edge on TI2 and the actual start of the counter is due to the
resynchronization circuit on TI2 input.
Figure 168. Control circuit in trigger mode
Counter clock = ck_cnt = ck_psc
Counter register 35 36 37 38 32 33 34
TI1
31 30
cnt_en
TIF
Write TIF=0
Counter clock = ck_cnt = ck_psc
Counter register 35 36 37 38 34
TI2
cnt_en
TIF
RM0008 General-purpose timers (TIM9 to TIM14)
Doc ID 13902 Rev 13 427/1093
16.4.12 Timer synchronization (TIM9/12)
The TIM timers are linked together internally for timer synchronization or chaining. Refer to
Section 15.3.15: Timer synchronization on page 380 for details.
16.4.13 Debug mode
When the microcontroller enters debug mode (Cortex-M3 core halted), the TIMx counter
either continues to work normally or stops, depending on DBG_TIMx_STOP configuration
bit in DBG module. For more details, refer to Section 31.16.2: Debug support for timers,
watchdog, bxCAN and I2C.
General-purpose timers (TIM9 to TIM14) RM0008
428/1093 Doc ID 13902 Rev 13
16.5 TIM9 and TIM12 registers
Refer to Section 1.1 for a list of abbreviations used in register descriptions.
16.5.1 TIM9/12 control register 1 (TIMx_CR1)
Address offset: 0x00
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
CKD[1:0] ARPE
reserved
OPM URS UDIS CEN
rw rw rw rw rw rw rw
Bits 15:10 Reserved, always read as 0
Bits 9:8 CKD: Clock division
This bit-field indicates the division ratio between the timer clock (CK_INT) frequency and
sampling clock used by the digital filters (TIx),
00: t
DTS
= t
CK_INT
01: t
DTS
= 2 t
CK_INT
10: t
DTS
= 4 t
CK_INT
11: Reserved
Bit 7 ARPE: Auto-reload preload enable
0: TIMx_ARR register is not buffered.
1: TIMx_ARR register is buffered.
Bits 6:4 Reserved
Bit 3 OPM: One-pulse mode
0: Counter is not stopped on the update event
1: Counter stops counting on the next update event (clearing the CEN bit).
Bit 2 URS: Update request source
This bit is set and cleared by software to select the UEV event sources.
0: Any of the following events generates an update interrupt if enabled:
Counter overflow
Setting the UG bit
1: Only counter overflow generates an update interrupt if enabled.
Bit 1 UDIS: Update disable
This bit is set and cleared by software to enable/disable update event (UEV) generation.
0: UEV enabled. An UEV is generated by one of the following events:
Counter overflow
Setting the UG bit
Buffered registers are then loaded with their preload values.
1: UEV disabled. No UEV is generated, shadow registers keep their value (ARR, PSC,
CCRx). The counter and the prescaler are reinitialized if the UG bit is set.
Bit 0 CEN: Counter enable
0: Counter disabled
1: Counter enabled
CEN is cleared automatically in one-pulse mode, when an update event occurs.
RM0008 General-purpose timers (TIM9 to TIM14)
Doc ID 13902 Rev 13 429/1093
16.5.2 TIM9/12 slave mode control register (TIMx_SMCR)
Address offset: 0x08
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
TS[2:0]
Res.
SMS[2:0]
rw rw rw rw rw rw
Bits 15:7 Reserved, must be kept at reset value.
Bits 6:4 TS: Trigger selection
This bitfield selects the trigger input to be used to synchronize the counter.
000: Internal Trigger 0 (ITR0)
001: Internal Trigger 1 (ITR1)
010: Internal Trigger 2 (ITR2)
011: Internal Trigger 3 (ITR3)
100: TI1 Edge Detector (TI1F_ED)
101: Filtered Timer Input 1 (TI1FP1)
110: Filtered Timer Input 2 (TI2FP2)
111: Reserved.
See Table 89: TIMx internal trigger connection on page 430 for more details on the meaning
of ITRx for each timer.
Note: These bits must be changed only when they are not used (e.g. when SMS=000) to
avoid wrong edge detections at the transition.
Bit 3 Reserved, must be kept at reset value.
Bits 2:0 SMS: Slave mode selection
When external signals are selected, the active edge of the trigger signal (TRGI) is linked to
the polarity selected on the external input (see Input control register and Control register
descriptions.
000: Slave mode disabled - if CEN = 1 then the prescaler is clocked directly by the internal
clock
001: Reserved
010: Reserved
011: Reserved
100: Reset mode - Rising edge of the selected trigger input (TRGI) reinitializes the counter
and generates an update of the registers
101: Gated mode - The counter clock is enabled when the trigger input (TRGI) is high. The
counter stops (but is not reset) as soon as the trigger becomes low. Counter starts and
stops are both controlled
110: Trigger mode - The counter starts on a rising edge of the trigger TRGI (but it is not
reset). Only the start of the counter is controlled
111: External clock mode 1 - Rising edges of the selected trigger (TRGI) clock the counter
Note: The Gated mode must not be used if TI1F_ED is selected as the trigger input
(TS=100). Indeed, TI1F_ED outputs 1 pulse for each transition on TI1F, whereas the
Gated mode checks the level of the trigger signal.
General-purpose timers (TIM9 to TIM14) RM0008
430/1093 Doc ID 13902 Rev 13
16.5.3 TIM9/12 Interrupt enable register (TIMx_DIER)
Address offset: 0x0C
Reset value: 0x0000
Table 89. TIMx internal trigger connection
Slave TIM ITR0 (TS = 000) ITR1 (TS = 001) ITR2 (TS = 010) ITR3 (TS = 011)
TIM2 TIM1 TIM8 TIM3 TIM4
TIM3 TIM1 TIM2 TIM5 TIM4
TIM4 TIM1 TIM2 TIM3 TIM8
TIM5 TIM2 TIM3 TIM4 TIM8
TIM9 TIM2 TIM3 TIM10 TIM11
TIM12 TIM4 TIM5 TIM13 TIM14
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
TIE
Res
CC2IE CC1IE UIE
rw rw rw rw
Bit 15:7 Reserved, must be kept at reset value.
Bit 6 TIE: Trigger interrupt enable
0: Trigger interrupt disabled.
1: Trigger interrupt enabled.
Bit 5:3 Reserved, must be kept at reset value.
Bit 2 CC2IE: Capture/Compare 2 interrupt enable
0: CC2 interrupt disabled.
1: CC2 interrupt enabled.
Bit 1 CC1IE: Capture/Compare 1 interrupt enable
0: CC1 interrupt disabled.
1: CC1 interrupt enabled.
Bit 0 UIE: Update interrupt enable
0: Update interrupt disabled.
1: Update interrupt enabled.
RM0008 General-purpose timers (TIM9 to TIM14)
Doc ID 13902 Rev 13 431/1093
16.5.4 TIM9/12 status register (TIMx_SR)
Address offset: 0x10
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
CC2OF CC1OF
Reserved
TIF
Reserved
CC2IF CC1IF UIF
rc_w0 rc_w0 rc_w0 rc_w0 rc_w0 rc_w0
Bit 15:11 Reserved, must be kept at reset value.
Bit 10 CC2OF: Capture/compare 2 overcapture flag
refer to CC1OF description
Bit 9 CC1OF: Capture/Compare 1 overcapture flag
This flag is set by hardware only when the corresponding channel is configured in input
capture mode. It is cleared by software by writing it to 0.
0: No overcapture has been detected.
1: The counter value has been captured in TIMx_CCR1 register while CC1IF flag was
already set
Bits 8:7 Reserved, must be kept at reset value.
Bit 6 TIF: Trigger interrupt flag
This flag is set by hardware on trigger event (active edge detected on TRGI input when the
slave mode controller is enabled in all modes but gated mode. It is set when the counter
starts or stops when gated mode is selected. It is cleared by software.
0: No trigger event occurred.
1: Trigger interrupt pending.
Bit 5:3 Reserved, always read as 0
Bit 2 CC2IF: Capture/Compare 2 interrupt flag
refer to CC1IF description
Bit 1 CC1IF: Capture/compare 1 interrupt flag
If channel CC1 is configured as output:
This flag is set by hardware when the counter matches the compare value. It is cleared by
software.
0: No match.
1: The content of the counter TIMx_CNT matches the content of the TIMx_CCR1 register.
When the contents of TIMx_CCR1 are greater than the contents of TIMx_ARR, the CC1IF
bit goes high on the counter overflow.
If channel CC1 is configured as input:
This bit is set by hardware on a capture. It is cleared by software or by reading the
TIMx_CCR1 register.
0: No input capture occurred.
1: The counter value has been captured in TIMx_CCR1 register (an edge has been detected
on IC1 which matches the selected polarity).
General-purpose timers (TIM9 to TIM14) RM0008
432/1093 Doc ID 13902 Rev 13
16.5.5 TIM9/12 event generation register (TIMx_EGR)
Address offset: 0x14
Reset value: 0x0000
Bit 0 UIF: Update interrupt flag
This bit is set by hardware on an update event. It is cleared by software.
0: No update occurred.
1: Update interrupt pending. This bit is set by hardware when the registers are updated:
At overflow and if UDIS=0 in the TIMx_CR1 register.
When CNT is reinitialized by software using the UG bit in TIMx_EGR register, if URS=0
and UDIS=0 in the TIMx_CR1 register.
When CNT is reinitialized by a trigger event (refer to the synchro control register
description), if URS=0 and UDIS=0 in the TIMx_CR1 register.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
TG
Reserved
CC2G CC1G UG
w w w w
Bits 15:7 Reserved, must be kept at reset value.
Bit 6 TG: Trigger generation
This bit is set by software in order to generate an event, it is automatically cleared by
hardware.
0: No action
1: The TIF flag is set in the TIMx_SR register. Related interrupt can occur if enabled
Bits 5:3 Reserved, must be kept at reset value.
Bit 2 CC2G: Capture/compare 2 generation
refer to CC1G description
Bit 1 CC1G: Capture/compare 1 generation
This bit is set by software to generate an event, it is automatically cleared by hardware.
0: No action
1: A capture/compare event is generated on channel 1:
If channel CC1 is configured as output:
the CC1IF flag is set, the corresponding interrupt is sent if enabled.
If channel CC1 is configured as input:
The current counter value is captured in the TIMx_CCR1 register. The CC1IF flag is set, the
corresponding interrupt is sent if enabled. The CC1OF flag is set if the CC1IF flag was
already high.
Bit 0 UG: Update generation
This bit can be set by software, it is automatically cleared by hardware.
0: No action
1: Re-initializes the counter and generates an update of the registers. The prescaler counter
is also cleared and the prescaler ratio is not affected. The counter is cleared.
RM0008 General-purpose timers (TIM9 to TIM14)
Doc ID 13902 Rev 13 433/1093
16.5.6 TIM9/12 capture/compare mode register 1 (TIMx_CCMR1)
Address offset: 0x18
Reset value: 0x0000
The channels can be used in input (capture mode) or in output (compare mode). The
direction of a channel is defined by configuring the corresponding CCxS bits. All the other
bits in this register have different functions in input and output modes. For a given bit, OCxx
describes its function when the channel is configured in output mode, ICxx describes its
function when the channel is configured in input mode. So you must take care that the same
bit can have different meanings for the input stage and the output stage.
Output compare mode
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
OC2CE OC2M[2:0] OC2PE OC2FE
CC2S[1:0]
OC1CE OC1M[2:0] OC1PE OC1FE
CC1S[1:0]
IC2F[3:0] IC2PSC[1:0] IC1F[3:0] IC1PSC[1:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bit 15 OC2CE: Output compare 2 clear enable
Bits 14:12 OC2M[2:0]: Output compare 2 mode
Bit 11 OC2PE: Output compare 2 preload enable
Bit 10 OC2FE: Output compare 2 fast enable
Bits 9:8 CC2S[1:0]: Capture/Compare 2 selection
This bitfield defines the direction of the channel (input/output) as well as the used input.
00: CC2 channel is configured as output
01: CC2 channel is configured as input, IC2 is mapped on TI2
10: CC2 channel is configured as input, IC2 is mapped on TI1
11: CC2 channel is configured as input, IC2 is mapped on TRC. This mode works only if an
internal trigger input is selected through the TS bit (TIMx_SMCR register
Note: The CC2S bits are writable only when the channel is OFF (CC2E = 0 in TIMx_CCER).
Bit 7 OC1CE: Output compare 1 clear enable
OC1CE: Output Compare 1 Clear Enable
0: OC1Ref is not affected by the ETRF input
1: OC1Ref is cleared as soon as a High level is detected on ETRF input
General-purpose timers (TIM9 to TIM14) RM0008
434/1093 Doc ID 13902 Rev 13
Bits 6:4 OC1M: Output compare 1 mode
These bits define the behavior of the output reference signal OC1REF from which OC1 and
OC1N are derived. OC1REF is active high whereas the active levels of OC1 and OC1N
depend on the CC1P and CC1NP bits, respectively.
000: Frozen - The comparison between the output compare register TIMx_CCR1 and the
counter TIMx_CNT has no effect on the outputs.(this mode is used to generate a timing
base).
001: Set channel 1 to active level on match. The OC1REF signal is forced high when the
TIMx_CNT counter matches the capture/compare register 1 (TIMx_CCR1).
010: Set channel 1 to inactive level on match. The OC1REF signal is forced low when the
TIMx_CNT counter matches the capture/compare register 1 (TIMx_CCR1).
011: Toggle - OC1REF toggles when TIMx_CNT=TIMx_CCR1
100: Force inactive level - OC1REF is forced low
101: Force active level - OC1REF is forced high
110: PWM mode 1 - In upcounting, channel 1 is active as long as TIMx_CNT<TIMx_CCR1
else it is inactive. In downcounting, channel 1 is inactive (OC1REF=0) as long as
TIMx_CNT>TIMx_CCR1, else it is active (OC1REF=1)
111: PWM mode 2 - In upcounting, channel 1 is inactive as long as
TIMx_CNT<TIMx_CCR1 else it is active. In downcounting, channel 1 is active as long as
TIMx_CNT>TIMx_CCR1 else it is inactive.
Note: In PWM mode 1 or 2, the OCREF level changes only when the result of the
comparison changes or when the output compare mode switches from frozen mode
to PWM mode.
Bit 3 OC1PE: Output compare 1 preload enable
0: Preload register on TIMx_CCR1 disabled. TIMx_CCR1 can be written at anytime, the
new value is taken into account immediately
1: Preload register on TIMx_CCR1 enabled. Read/Write operations access the preload
register. TIMx_CCR1 preload value is loaded into the active register at each update event
Note: The PWM mode can be used without validating the preload register only in one-pulse
mode (OPM bit set in the TIMx_CR1 register). Else the behavior is not guaranteed.
Bit 2 OC1FE: Output compare 1 fast enable
This bit is used to accelerate the effect of an event on the trigger in input on the CC output.
0: CC1 behaves normally depending on the counter and CCR1 values even when the
trigger is ON. The minimum delay to activate the CC1 output when an edge occurs on the
trigger input is 5 clock cycles
1: An active edge on the trigger input acts like a compare match on the CC1 output. Then,
OC is set to the compare level independently of the result of the comparison. Delay to
sample the trigger input and to activate CC1 output is reduced to 3 clock cycles. OC1FE
acts only if the channel is configured in PWM1 or PWM2 mode.
Bits 1:0 CC1S: Capture/Compare 1 selection
This bitfield defines the direction of the channel (input/output) as well as the used input.
00: CC1 channel is configured as output
01: CC1 channel is configured as input, IC1 is mapped on TI1
10: CC1 channel is configured as input, IC1 is mapped on TI2
11: CC1 channel is configured as input, IC1 is mapped on TRC. This mode works only if an
internal trigger input is selected through the TS bit (TIMx_SMCR register)
Note: The CC1S bits are writable only when the channel is OFF (CC1E = 0 in TIMx_CCER).
RM0008 General-purpose timers (TIM9 to TIM14)
Doc ID 13902 Rev 13 435/1093
Input capture mode
Bits 15:12 IC2F: Input capture 2 filter
Bits 11:10 IC2PSC[1:0]: Input capture 2 prescaler
Bits 9:8 CC2S: Capture/compare 2 selection
This bitfield defines the direction of the channel (input/output) as well as the used input.
00: CC2 channel is configured as output
01: CC2 channel is configured as input, IC2 is mapped on TI2
10: CC2 channel is configured as input, IC2 is mapped on TI1
11: CC2 channel is configured as input, IC2 is mapped on TRC. This mode works only if an
internal trigger input is selected through the TS bit (TIMx_SMCR register)
Note: The CC2S bits are writable only when the channel is OFF (CC2E = 0 in TIMx_CCER).
Bits 7:4 IC1F: Input capture 1 filter
This bitfield defines the frequency used to sample the TI1 input and the length of the digital
filter applied to TI1. The digital filter is made of an event counter in which N events are
needed to validate a transition on the output:
0000: No filter, sampling is done at f
DTS
1000: f
SAMPLING
=f
DTS
/8, N=6
0001: f
SAMPLING
=f
CK_INT
, N=2 1001: f
SAMPLING
=f
DTS
/8, N=8
0010: f
SAMPLING
=f
CK_INT
, N=4 1010: f
SAMPLING
=f
DTS
/16, N=5
0011: f
SAMPLING
=f
CK_INT
, N=8 1011: f
SAMPLING
=f
DTS
/16, N=6
0100: f
SAMPLING
=f
DTS
/2, N=6 1100: f
SAMPLING
=f
DTS
/16, N=8
0101: f
SAMPLING
=f
DTS
/2, N=8 1101: f
SAMPLING
=f
DTS
/32, N=5
0110: f
SAMPLING
=f
DTS
/4, N=6 1110: f
SAMPLING
=f
DTS
/32, N=6
0111: f
SAMPLING
=f
DTS
/4, N=8 1111: f
SAMPLING
=f
DTS
/32, N=8
Note: In the current silicon revision, f
DTS
is replaced in the formula by CK_INT when
ICxF[3:0]= 1, 2 or 3.
Bits 3:2 IC1PSC: Input capture 1 prescaler
This bitfield defines the ratio of the prescaler acting on the CC1 input (IC1).
The prescaler is reset as soon as CC1E=0 (TIMx_CCER register).
00: no prescaler, capture is done each time an edge is detected on the capture input
01: capture is done once every 2 events
10: capture is done once every 4 events
11: capture is done once every 8 events
Bits 1:0 CC1S: Capture/Compare 1 selection
This bitfield defines the direction of the channel (input/output) as well as the used input.
00: CC1 channel is configured as output
01: CC1 channel is configured as input, IC1 is mapped on TI1
10: CC1 channel is configured as input, IC1 is mapped on TI2
11: CC1 channel is configured as input, IC1 is mapped on TRC. This mode is working only if
an internal trigger input is selected through TS bit (TIMx_SMCR register)
Note: The CC1S bits are writable only when the channel is OFF (CC1E = 0 in TIMx_CCER).
General-purpose timers (TIM9 to TIM14) RM0008
436/1093 Doc ID 13902 Rev 13
16.5.7 TIM9/12 capture/compare enable register (TIMx_CCER)
Address offset: 0x20
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
CC2NP
Res.
CC2P CC2E CC1NP
Res.
CC1P CC1E
rw rw rw rw rw rw
Bits 15:8 Reserved, must be kept at reset value.
Bit 7 CC2NP: Capture/Compare 2 output Polarity
refer to CC1NP description
Bits 6 Reserved, must be kept at reset value.
Bit 5 CC2P: Capture/Compare 2 output Polarity
refer to CC1P description
Bit 4 CC2E: Capture/Compare 2 output enable
refer to CC1E description
Bit 3 CC1NP: Capture/Compare 1 complementary output Polarity
CC1 channel configured as output: CC1NP must be kept cleared
CC1 channel configured as input: CC1NP is used in conjunction with CC1P to define
TI1FP1/TI2FP1 polarity (refer to CC1P description).
Bits 2 Reserved, must be kept at reset value.
Bit 1 CC1P: Capture/Compare 1 output Polarity.
CC1 channel configured as output:
0: OC1 active high.
1: OC1 active low.
CC1 channel configured as input:
CC1NP/CC1P bits select TI1FP1 and TI2FP1 polarity for trigger or capture operations.
00: noninverted/rising edge
Circuit is sensitive to TIxFP1 rising edge (capture, trigger in reset, external clock or trigger
mode), TIxFP1 is not inverted (trigger in gated mode, encoder mode).
01: inverted/falling edge
Circuit is sensitive to TIxFP1 falling edge (capture, trigger in reset, external clock or trigger
mode), TIxFP1 is inverted (trigger in gated mode, encoder mode).
10: reserved, do not use this configuration.
Note: 11: noninverted/both edges
Circuit is sensitive to both TIxFP1 rising and falling edges (capture, trigger in reset,
external clock or trigger mode), TIxFP1 is not inverted (trigger in gated mode). This
configuration must not be used for encoder mode.
Bit 0 CC1E: Capture/Compare 1 output enable.
CC1 channel configured as output:
0: Off - OC1 is not active.
1: On - OC1 signal is output on the corresponding output pin.
CC1 channel configured as input:
This bit determines if a capture of the counter value can actually be done into the input
capture/compare register 1 (TIMx_CCR1) or not.
0: Capture disabled.
1: Capture enabled.
RM0008 General-purpose timers (TIM9 to TIM14)
Doc ID 13902 Rev 13 437/1093
Note: The states of the external I/O pins connected to the standard OCx channels depend on the
state of the OCx channel and on the GPIO registers.
16.5.8 TIM9/12 counter (TIMx_CNT)
Address offset: 0x24
Reset value: 0x0000 0000
16.5.9 TIM9/12 prescaler (TIMx_PSC)
Address offset: 0x28
Reset value: 0x0000
16.5.10 TIM9/12 auto-reload register (TIMx_ARR)
Address offset: 0x2C
Reset value: 0x0000 0000
Table 90. Output control bit for standard OCx channels
CCxE bit OCx output state
0 Output disabled (OCx=0, OCx_EN=0)
1 OCx=OCxREF + Polarity, OCx_EN=1
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CNT[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 CNT[15:0]: Counter value
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
PSC[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 PSC[15:0]: Prescaler value
The counter clock frequency CK_CNT is equal to f
CK_PSC
/ (PSC[15:0] + 1).
PSC contains the value to be loaded into the active prescaler register at each update event.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
ARR[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 ARR[15:0]: Auto-reload value
ARR is the value to be loaded into the actual auto-reload register.
Refer to the Section 16.4.1: Time-base unit on page 411 for more details about ARR update
and behavior.
The counter is blocked while the auto-reload value is null.
General-purpose timers (TIM9 to TIM14) RM0008
438/1093 Doc ID 13902 Rev 13
16.5.11 TIM9/12 capture/compare register 1 (TIMx_CCR1)
Address offset: 0x34
Reset value: 0x0000 0000
16.5.12 TIM9/12 capture/compare register 2 (TIMx_CCR2)
Address offset: 0x38
Reset value: 0x0000 0000
16.5.13 TIM9/12 register map
TIM9/12 registers are mapped as 16-bit addressable registers as described in the table
below:
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CCR1[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 CCR1[15:0]: Capture/Compare 1 value
If channel CC1 is configured as output:
CCR1 is the value to be loaded into the actual capture/compare 1 register (preload value).
It is loaded permanently if the preload feature is not selected in the TIMx_CCMR1 register
(OC1PE bit). Else the preload value is copied into the active capture/compare 1 register when
an update event occurs.
The active capture/compare register contains the value to be compared to the TIMx_CNT
counter and signaled on the OC1 output.
If channel CC1is configured as input:
CCR1 is the counter value transferred by the last input capture 1 event (IC1).
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CCR2[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 CCR2[15:0]: Capture/Compare 2 value
If channel CC2 is configured as output:
CCR2 is the value to be loaded into the actual capture/compare 2 register (preload value).
It is loaded permanently if the preload feature is not selected in the TIMx_CCMR2 register
(OC2PE bit). Else the preload value is copied into the active capture/compare 2 register when
an update event occurs.
The active capture/compare register contains the value to be compared to the TIMx_CNT
counter and signalled on the OC2 output.
If channel CC2 is configured as input:
CCR2 is the counter value transferred by the last input capture 2 event (IC2).
Table 91. TIM9/12 register map and reset values
Offset Register
3
1
3
0
2
9
2
8
2
7
2
6
2
5
2
4
2
3
2
2
2
1
2
0
1
9
1
8
1
7
1
6
1
5
1
4
1
3
1
2
1
1
1
0
9 8 7 6 5 4 3 2 1 0
0x00
TIMx_CR1
Reserved
CKD
[1:0]
A
R
P
E
R
e
s
e
r
v
e
d
R
e
s
e
r
v
e
d
R
e
s
e
r
v
e
d
O
P
M
U
R
S
U
D
I
S
C
E
N
Reset value 0 0 0 0 0 0 0
RM0008 General-purpose timers (TIM9 to TIM14)
Doc ID 13902 Rev 13 439/1093
Refer to Table 3 on page 50 for the register boundary addresses.
0x08
TIMx_SMCR
Reserved
TS[2:0]
R
e
s
e
r
v
e
d
SMS[2:0]
Reset value 0 0 0 0 0 0
0x0C
TIMx_DIER
Reserved T
I
E
R
e
s
e
r
v
e
d
R
e
s
e
r
v
e
d
R
e
s
e
r
v
e
d
C
C
2
I
E
C
C
1
I
E
U
I
E
Reset value 0 0 0 0
0x10
TIMx_SR
Reserved
C
C
2
O
F
C
C
1
O
F
R
e
s
e
r
v
e
d
R
e
s
e
r
v
e
d
T
I
F
R
e
s
e
r
v
e
d
R
e
s
e
r
v
e
d
R
e
s
e
r
v
e
d
C
C
2
I
F
C
C
1
I
F
U
I
F
Reset value 0 0 0 0 0 0
0x14
TIMx_EGR
Reserved T
G
R
e
s
e
r
v
e
d
R
e
s
e
r
v
e
d
R
e
s
e
r
v
e
d
C
C
2
G
C
C
1
G
U
G
Reset value 0 0 0 0
0x18
TIMx_CCMR1
Output Compare
mode
Reserved
OC2M
[2:0]
O
C
2
P
E
O
C
2
F
E
CC2S
[1:0]
R
e
s
e
r
v
e
d
OC1M
[2:0]
O
C
1
P
E
O
C
1
F
E
CC1S
[1:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIMx_CCMR1
Input Capture
mode
Reserved
IC2F[3:0]
IC2
PSC
[1:0]
CC2S
[1:0]
IC1F[3:0]
IC1
PSC
[1:0]
CC1S
[1:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x1C Reserved
0x20
TIMx_CCER
Reserved
C
C
2
N
P
R
e
s
e
r
v
e
d
C
C
2
P
C
C
2
E
C
C
1
N
P
R
e
s
e
r
v
e
d
C
C
1
P
C
C
1
E
Reset value 0 0 0 0 0 0
0x24
TIMx_CNT
Reserved
CNT[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x28
TIMx_PSC
Reserved
PSC[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x2C
TIMx_ARR
Reserved
ARR[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x30 Reserved
0x34
TIMx_CCR1
Reserved
CCR1[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x38
TIMx_CCR2
Reserved
CCR2[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x3C to
0x4C
Reserved
Table 91. TIM9/12 register map and reset values (continued)
Offset Register
3
1
3
0
2
9
2
8
2
7
2
6
2
5
2
4
2
3
2
2
2
1
2
0
1
9
1
8
1
7
1
6
1
5
1
4
1
3
1
2
1
1
1
0
9 8 7 6 5 4 3 2 1 0
General-purpose timers (TIM9 to TIM14) RM0008
440/1093 Doc ID 13902 Rev 13
16.6 TIM10/11/13/14 registers
16.6.1 TIM10/11/13/14 control register 1 (TIMx_CR1)
Address offset: 0x00
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
CKD[1:0] ARPE
Reserved
URS UDIS CEN
rw rw rw rw rw rw
Bits 15:10 Reserved, must be kept at reset value.
Bits 9:8 CKD: Clock division
This bit-field indicates the division ratio between the timer clock (CK_INT) frequency and
sampling clock used by the digital filters (ETR, TIx),
00: t
DTS
= t
CK_INT
01: t
DTS
= 2 t
CK_INT
10: t
DTS
= 4 t
CK_INT
11: Reserved
Bit 7 ARPE: Auto-reload preload enable
0: TIMx_ARR register is not buffered
1: TIMx_ARR register is buffered
Bits 6:3 Reserved, must be kept at reset value.
Bit 2 URS: Update request source
This bit is set and cleared by software to select the update interrupt (UEV) sources.
0: Any of the following events generate an UEV if enabled:
Counter overflow
Setting the UG bit
1: Only counter overflow generates an UEV if enabled.
Bit 1 UDIS: Update disable
This bit is set and cleared by software to enable/disable update interrupt (UEV) event
generation.
0: UEV enabled. An UEV is generated by one of the following events:
Counter overflow
Setting the UG bit.
Buffered registers are then loaded with their preload values.
1: UEV disabled. No UEV is generated, shadow registers keep their value (ARR, PSC,
CCRx). The counter and the prescaler are reinitialized if the UG bit is set.
Bit 0 CEN: Counter enable
0: Counter disabled
1: Counter enabled
RM0008 General-purpose timers (TIM9 to TIM14)
Doc ID 13902 Rev 13 441/1093
16.6.2 TIM10/11/13/14 Interrupt enable register (TIMx_DIER)
Address offset: 0x0C
Reset value: 0x0000
16.6.3 TIM10/11/13/14 status register (TIMx_SR)
Address offset: 0x10
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
CC1IE UIE
rw rw
Bits 15:2 Reserved, must be kept at reset value.
Bit 1 CC1IE: Capture/Compare 1 interrupt enable
0: CC1 interrupt disabled
1: CC1 interrupt enabled
Bit 0 UIE: Update interrupt enable
0: Update interrupt disabled
1: Update interrupt enabled
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
CC1OF
Reserved
CC1IF UIF
rc_w0 rc_w0 rc_w0
Bit 15:10 Reserved, must be kept at reset value.
Bit 9 CC1OF: Capture/Compare 1 overcapture flag
This flag is set by hardware only when the corresponding channel is configured in input
capture mode. It is cleared by software by writing it to 0.
0: No overcapture has been detected.
1: The counter value has been captured in TIMx_CCR1 register while CC1IF flag was
already set
Bits 8:2 Reserved, must be kept at reset value.
Bit 1 CC1IF: Capture/compare 1 interrupt flag
If channel CC1 is configured as output:
This flag is set by hardware when the counter matches the compare value. It is cleared by
software.
0: No match.
1: The content of the counter TIMx_CNT matches the content of the TIMx_CCR1 register.
When the contents of TIMx_CCR1 are greater than the contents of TIMx_ARR, the CC1IF bit
goes high on the counter overflow.
If channel CC1 is configured as input:
This bit is set by hardware on a capture. It is cleared by software or by reading the
TIMx_CCR1 register.
0: No input capture occurred.
1: The counter value has been captured in TIMx_CCR1 register (an edge has been detected
on IC1 which matches the selected polarity).
General-purpose timers (TIM9 to TIM14) RM0008
442/1093 Doc ID 13902 Rev 13
16.6.4 TIM10/11/13/14 event generation register (TIMx_EGR)
Address offset: 0x14
Reset value: 0x0000
Bit 0 UIF: Update interrupt flag
This bit is set by hardware on an update event. It is cleared by software.
0: No update occurred.
1: Update interrupt pending. This bit is set by hardware when the registers are updated:
At overflow and if UDIS=0 in the TIMx_CR1 register.
When CNT is reinitialized by software using the UG bit in TIMx_EGR register, if
URS=0 and UDIS=0 in the TIMx_CR1 register.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
CC1G UG
w w
Bits 15:2 Reserved, must be kept at reset value.
Bit 1 CC1G: Capture/compare 1 generation
This bit is set by software in order to generate an event, it is automatically cleared by
hardware.
0: No action
1: A capture/compare event is generated on channel 1:
If channel CC1 is configured as output:
CC1IF flag is set, Corresponding interrupt or is sent if enabled.
If channel CC1 is configured as input:
The current value of the counter is captured in TIMx_CCR1 register. The CC1IF flag is set,
the corresponding interrupt is sent if enabled. The CC1OF flag is set if the CC1IF flag was
already high.
Bit 0 UG: Update generation
This bit can be set by software, it is automatically cleared by hardware.
0: No action
1: Re-initialize the counter and generates an update of the registers. Note that the prescaler
counter is cleared too (anyway the prescaler ratio is not affected). The counter is cleared.
RM0008 General-purpose timers (TIM9 to TIM14)
Doc ID 13902 Rev 13 443/1093
16.6.5 TIM10/11/13/14 capture/compare mode register 1
(TIMx_CCMR1)
Address offset: 0x18
Reset value: 0x0000
The channels can be used in input (capture mode) or in output (compare mode). The
direction of a channel is defined by configuring the corresponding CCxS bits. All the other
bits of this register have a different function in input and in output mode. For a given bit,
OCxx describes its function when the channel is configured in output, ICxx describes its
function when the channel is configured in input. So you must take care that the same bit
can have a different meaning for the input stage and for the output stage.
Output compare mode
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved OC1M[2:0] OC1PE OC1FE
CC1S[1:0]
Reserved IC1F[3:0] IC1PSC[1:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:7 Reserved
Bits 6:4 OC1M: Output compare 1 mode
These bits define the behavior of the output reference signal OC1REF from which OC1 is
derived. OC1REF is active high whereas OC1 active level depends on CC1P bit.
000: Frozen. The comparison between the output compare register TIMx_CCR1 and the
counter TIMx_CNT has no effect on the outputs.
001: Set channel 1 to active level on match. OC1REF signal is forced high when the counter
TIMx_CNT matches the capture/compare register 1 (TIMx_CCR1).
010: Set channel 1 to inactive level on match. OC1REF signal is forced low when the
counter TIMx_CNT matches the capture/compare register 1 (TIMx_CCR1).
011: Toggle - OC1REF toggles when TIMx_CNT = TIMx_CCR1.
100: Force inactive level - OC1REF is forced low.
101: Force active level - OC1REF is forced high.
110: PWM mode 1 - Channel 1 is active as long as TIMx_CNT < TIMx_CCR1 else inactive.
111: PWM mode 2 - Channel 1 is inactive as long as TIMx_CNT < TIMx_CCR1 else active.
Note: In PWM mode 1 or 2, the OCREF level changes when the result of the comparison
changes or when the output compare mode switches from frozen to PWM mode.
Bit 3 OC1PE: Output compare 1 preload enable
0: Preload register on TIMx_CCR1 disabled. TIMx_CCR1 can be written at anytime, the
new value is taken in account immediately.
1: Preload register on TIMx_CCR1 enabled. Read/Write operations access the preload
register. TIMx_CCR1 preload value is loaded in the active register at each update event.
Note: The PWM mode can be used without validating the preload register only in one pulse
mode (OPM bit set in TIMx_CR1 register). Else the behavior is not guaranteed.
General-purpose timers (TIM9 to TIM14) RM0008
444/1093 Doc ID 13902 Rev 13
Input capture mode
Bit 2 OC1FE: Output compare 1 fast enable
This bit is used to accelerate the effect of an event on the trigger in input on the CC output.
0: CC1 behaves normally depending on counter and CCR1 values even when the trigger is
ON. The minimum delay to activate CC1 output when an edge occurs on the trigger input is
5 clock cycles.
1: An active edge on the trigger input acts like a compare match on CC1 output. OC is then
set to the compare level independently of the result of the comparison. Delay to sample the
trigger input and to activate CC1 output is reduced to 3 clock cycles. OC1FE acts only if the
channel is configured in PWM1 or PWM2 mode.
Bits 1:0 CC1S: Capture/Compare 1 selection
This bit-field defines the direction of the channel (input/output) as well as the used input.
00: CC1 channel is configured as output.
01: CC1 channel is configured as input, IC1 is mapped on TI1.
10: CC1 channel is configured as input, IC1 is mapped on TI2
11: CC1 channel is configured as input, IC1 is mapped on TRC. This mode is working only
if an internal trigger input is selected through TS bit (TIMx_SMCR register)
Note: CC1S bits are writable only when the channel is OFF (CC1E = 0 in TIMx_CCER).
Bits 15:8 Reserved
Bits 7:4 IC1F: Input capture 1 filter
This bit-field defines the frequency used to sample TI1 input and the length of the digital filter
applied to TI1. The digital filter is made of an event counter in which N events are needed to
validate a transition on the output:
0000: No filter, sampling is done at f
DTS
1000: f
SAMPLING
=f
DTS
/8, N=6
0001: f
SAMPLING
=f
CK_INT
, N=2 1001: f
SAMPLING
=f
DTS
/8, N=8
0010: f
SAMPLING
=f
CK_INT
, N=4 1010: f
SAMPLING
=f
DTS
/16, N=5
0011: f
SAMPLING
=f
CK_INT
, N=8 1011: f
SAMPLING
=f
DTS
/16, N=6
0100: f
SAMPLING
=f
DTS
/2, N=6 1100: f
SAMPLING
=f
DTS
/16, N=8
0101: f
SAMPLING
=f
DTS
/2, N=8 1101: f
SAMPLING
=f
DTS
/32, N=5
0110: f
SAMPLING
=f
DTS
/4, N=6 1110: f
SAMPLING
=f
DTS
/32, N=6
0111: f
SAMPLING
=f
DTS
/4, N=8 1111: f
SAMPLING
=f
DTS
/32, N=8
Note: In current silicon revision, f
DTS
is replaced in the formula by CK_INT when ICxF[3:0]= 1,
2 or 3.
Bits 3:2 IC1PSC: Input capture 1 prescaler
This bit-field defines the ratio of the prescaler acting on CC1 input (IC1).
The prescaler is reset as soon as CC1E=0 (TIMx_CCER register).
00: no prescaler, capture is done each time an edge is detected on the capture input
01: capture is done once every 2 events
10: capture is done once every 4 events
11: capture is done once every 8 events
Bits 1:0 CC1S: Capture/Compare 1 selection
This bit-field defines the direction of the channel (input/output) as well as the used input.
00: CC1 channel is configured as output
01: CC1 channel is configured as input, IC1 is mapped on TI1
10: CC1 channel is configured as input, IC1 is mapped on TI2
11: CC1 channel is configured as input, IC1 is mapped on TRC. This mode is working only if
an internal trigger input is selected through TS bit (TIMx_SMCR register)
Note: CC1S bits are writable only when the channel is OFF (CC1E = 0 in TIMx_CCER).
RM0008 General-purpose timers (TIM9 to TIM14)
Doc ID 13902 Rev 13 445/1093
16.6.6 TIM10/11/13/14 capture/compare enable register
(TIMx_CCER)
Address offset: 0x20
Reset value: 0x0000
Note: The state of the external I/O pins connected to the standard OCx channels depends on the
OCx channel state and the GPIO registers.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
CC1NP
Res.
CC1P CC1E
rw rw rw
Bits 15:4 Reserved, must be kept at reset value.
Bit 3 CC1NP: Capture/Compare 1 complementary output Polarity.
CC1 channel configured as output: CC1NP must be kept cleared.
CC1 channel configured as input: CC1NP bit is used in conjunction with CC1P to define
TI1FP1 polarity (refer to CC1P description).
Bit 2 Reserved, must be kept at reset value.
Bit 1 CC1P: Capture/Compare 1 output Polarity.
CC1 channel configured as output:
0: OC1 active high
1: OC1 active low
CC1 channel configured as input:
The CC1P bit selects TI1FP1 and TI2FP1 polarity for trigger or capture operations.
00: noninverted/rising edge
Circuit is sensitive to TI1FP1 rising edge (capture mode), TI1FP1 is not inverted.
01: inverted/falling edge
Circuit is sensitive to TI1FP1 falling edge (capture mode), TI1FP1 is inverted.
10: reserved, do not use this configuration.
11: noninverted/both edges
Circuit is sensitive to both TI1FP1 rising and falling edges (capture mode), TI1FP1 is not
inverted.
Bit 0 CC1E: Capture/Compare 1 output enable.
CC1 channel configured as output:
0: Off - OC1 is not active
1: On - OC1 signal is output on the corresponding output pin
CC1 channel configured as input:
This bit determines if a capture of the counter value can actually be done into the input
capture/compare register 1 (TIMx_CCR1) or not.
0: Capture disabled
1: Capture enabled
Table 92. Output control bit for standard OCx channels
CCxE bit OCx output state
0 Output Disabled (OCx=0, OCx_EN=0)
1 OCx=OCxREF + Polarity, OCx_EN=1
General-purpose timers (TIM9 to TIM14) RM0008
446/1093 Doc ID 13902 Rev 13
16.6.7 TIM10/11/13/14 counter (TIMx_CNT)
Address offset: 0x24
Reset value: 0x0000
16.6.8 TIM10/11/13/14 prescaler (TIMx_PSC)
Address offset: 0x28
Reset value: 0x0000
16.6.9 TIM10/11/13/14 auto-reload register (TIMx_ARR)
Address offset: 0x2C
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CNT[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 CNT[15:0]: Counter value
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
PSC[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 PSC[15:0]: Prescaler value
The counter clock frequency CK_CNT is equal to f
CK_PSC
/ (PSC[15:0] + 1).
PSC contains the value to be loaded in the active prescaler register at each update event.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
ARR[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 ARR[15:0]: Auto-reload value
ARR is the value to be loaded in the actual auto-reload register.
Refer to the Section 16.4.1: Time-base unit on page 411 for more details about ARR update
and behavior.
The counter is blocked while the auto-reload value is null.
RM0008 General-purpose timers (TIM9 to TIM14)
Doc ID 13902 Rev 13 447/1093
16.6.10 TIM10/11/13/14 capture/compare register 1 (TIMx_CCR1)
Address offset: 0x34
Reset value: 0x0000
16.6.11 TIM10/11/13/14 register map
TIMx registers are mapped as 16-bit addressable registers as described in the tables below:
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CCR1[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 CCR1[15:0]: Capture/Compare 1 value
If channel CC1 is configured as output:
CCR1 is the value to be loaded in the actual capture/compare 1 register (preload value).
It is loaded permanently if the preload feature is not selected in the TIMx_CCMR1 register (bit
OC1PE). Else the preload value is copied in the active capture/compare 1 register when an
update event occurs.
The active capture/compare register contains the value to be compared to the counter
TIMx_CNT and signaled on OC1 output.
If channel CC1is configured as input:
CCR1 is the counter value transferred by the last input capture 1 event (IC1).
Table 93. TIM10/11/13/14 register map and reset values
Offset Register
3
1
3
0
2
9
2
8
2
7
2
6
2
5
2
4
2
3
2
2
2
1
2
0
1
9
1
8
1
7
1
6
1
5
1
4
1
3
1
2
1
1
1
0
9 8 7 6 5 4 3 2 1 0
0x00
TIMx_CR1
Reserved
CKD
[1:0]
A
R
P
E
R
e
s
e
r
v
e
d
R
e
s
e
r
v
e
d
R
e
s
e
r
v
e
d
R
e
s
e
r
v
e
d
U
R
S
U
D
I
S
C
E
N
Reset value 0 0 0 0 0 0
0x08
TIMx_SMCR
Not Available
Reset value
0x0C
TIMx_DIER
Reserved
C
C
1
I
E
U
I
E
Reset value 0 0
0x10
TIMx_SR
Reserved
C
C
1
O
F
Reserved
C
C
1
I
F
U
I
F
Reset value 0 0 0
0x14
TIMx_EGR
Reserved
C
C
1
G
U
G
Reset value 0 0
0x18
TIMx_CCMR1
Output compare
mode
Reserved
OC1M
[2:0]
O
C
1
P
E
O
C
1
F
E
CC1S
[1:0]
Reset value 0 0 0 0 0 0 0
TIMx_CCMR1
Input capture
mode
Reserved
IC1F[3:0]
IC1
PSC
[1:0]
CC1S
[1:0]
Reset value 0 0 0 0 0 0 0 0
0x1C Reserved
0x20
TIMx_CCER
Reserved
R
e
s
e
r
v
e
d
R
e
s
e
r
v
e
d
R
e
s
e
r
v
e
d
R
e
s
e
r
v
e
d
R
e
s
e
r
v
e
d
R
e
s
e
r
v
e
d
R
e
s
e
r
v
e
d
C
C
1
N
P
R
e
s
e
r
v
e
d
C
C
1
P
C
C
1
E
Reset value 0 0 0
General-purpose timers (TIM9 to TIM14) RM0008
448/1093 Doc ID 13902 Rev 13
Refer to Table 3 on page 50 for the register boundary addresses.
0x24
TIMx_CNT
Reserved
CNT[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x28
TIMx_PSC
Reserved
PSC[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x2C
TIMx_ARR
Reserved
ARR[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x30 Reserved
0x34
TIMx_CCR1
Reserved
CCR1[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x38 to
0x4C
Reserved
Table 93. TIM10/11/13/14 register map and reset values (continued)
Offset Register
3
1
3
0
2
9
2
8
2
7
2
6
2
5
2
4
2
3
2
2
2
1
2
0
1
9
1
8
1
7
1
6
1
5
1
4
1
3
1
2
1
1
1
0
9 8 7 6 5 4 3 2 1 0
RM0008 Basic timers (TIM6&TIM7)
Doc ID 13902 Rev 13 449/1093
17 Basic timers (TIM6&TIM7)
Low-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 16 and 32 Kbytes.
Medium-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 64 and 128 Kbytes.
High-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 256 and 512 Kbytes.
XL-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 768 Kbytes and 1 Mbyte.
Connectivity line devices are STM32F105xx and STM32F107xx microcontrollers.
This section applies to high-density and XL-density STM32F101xx and STM32F103xx
devices, and to connectivity line devices only.
17.1 TIM6&TIM7 introduction
The basic timers TIM6 and TIM7 consist of a 16-bit auto-reload counter driven by a
programmable prescaler.
They may be used as generic timers for time-base generation but they are also specifically
used to drive the digital-to-analog converter (DAC). In fact, the timers are internally
connected to the DAC and are able to drive it through their trigger outputs.
The timers are completely independent, and do not share any resources.
17.2 TIM6&TIM7 main features
Basic timer (TIM6&TIM7) features include:
16-bit auto-reload upcounter
16-bit programmable prescaler used to divide (also on the fly) the counter clock
frequency by any factor between 1 and 65535
Synchronization circuit to trigger the DAC
Interrupt/DMA generation on the update event: counter overflow
Basic timers (TIM6&TIM7) RM0008
450/1093 Doc ID 13902 Rev 13
Figure 169. Basic timer block diagram
17.3 TIM6&TIM7 functional description
17.3.1 Time-base unit
The main block of the programmable timer is a 16-bit upcounter with its related auto-reload
register. The counter clock can be divided by a prescaler.
The counter, the auto-reload register and the prescaler register can be written or read by
software. This is true even when the counter is running.
The time-base unit includes:
Counter Register (TIMx_CNT)
Prescaler Register (TIMx_PSC)
Auto-Reload Register (TIMx_ARR)
The auto-reload register is preloaded. The preload register is accessed each time an
attempt is made to write or read the auto-reload register. The contents of the preload
register are transferred into the shadow register permanently or at each update event UEV,
depending on the auto-reload preload enable bit (ARPE) in the TIMx_CR1 register. The
update event is sent when the counter reaches the overflow value and if the UDIS bit equals
0 in the TIMx_CR1 register. It can also be generated by software. The generation of the
update event is described in detail for each configuration.
The counter is clocked by the prescaler output CK_CNT, which is enabled only when the
counter enable bit (CEN) in the TIMx_CR1 register is set.
Note that the actual counter enable signal CNT_EN is set 1 clock cycle after CEN.
U
Trigger
controller
Stop, Clear or up
TRGO
U
UI
Reset, Enable, Count,
event
Preload registers transferred
to active registers on U event according to control bit
interrupt & DMA output
to DAC
COUNTER
CK_PSC
CNT
CK_CNT
Controller
Internal clock (CK_INT)
TIMxCLK from RCC
Prescaler
PSC
Auto-reload Register
Flag
ai14749b
RM0008 Basic timers (TIM6&TIM7)
Doc ID 13902 Rev 13 451/1093
Prescaler description
The prescaler can divide the counter clock frequency by any factor between 1 and 65536. It
is based on a 16-bit counter controlled through a 16-bit register (in the TIMx_PSC register).
It can be changed on the fly as the TIMx_PSC control register is buffered. The new
prescaler ratio is taken into account at the next update event.
Figure 170 and Figure 171 give some examples of the counter behavior when the prescaler
ratio is changed on the fly.
Figure 170. Counter timing diagram with prescaler division change from 1 to 2
Figure 171. Counter timing diagram with prescaler division change from 1 to 4
CK_PSC
00
CNT_EN
Timer clock = CK_CNT
Counter register
Update event (UEV)
0
F9 FA FB FC F7
Prescaler control register 0 1
Write a new value in TIMx_PSC
01 02 03
Prescaler buffer 0 1
Prescaler counter 0 1 0 1 0 1 0 1
F8
CK_PSC
00
CNT_EN
Timer clock = CK_CNT
Counter register
Update event (UEV)
0
F9 FA FB FC F7
Prescaler control register 0 3
Write a new value in TIMx_PSC
Prescaler buffer 0 3
Prescaler counter 0 1 2 3 0 1 2 3
F8 01
Basic timers (TIM6&TIM7) RM0008
452/1093 Doc ID 13902 Rev 13
17.3.2 Counting mode
The counter counts from 0 to the auto-reload value (contents of the TIMx_ARR register),
then restarts from 0 and generates a counter overflow event.
An update event can be generate at each counter overflow or by setting the UG bit in the
TIMx_EGR register (by software or by using the slave mode controller).
The UEV event can be disabled by software by setting the UDIS bit in the TIMx_CR1
register. This avoids updating the shadow registers while writing new values into the preload
registers. In this way, no update event occurs until the UDIS bit has been written to 0,
however, the counter and the prescaler counter both restart from 0 (but the prescale rate
does not change). In addition, if the URS (update request selection) bit in the TIMx_CR1
register is set, setting the UG bit generates an update event UEV, but the UIF flag is not set
(so no interrupt or DMA request is sent).
When an update event occurs, all the registers are updated and the update flag (UIF bit in
the TIMx_SR register) is set (depending on the URS bit):
The buffer of the prescaler is reloaded with the preload value (contents of the
TIMx_PSC register)
The auto-reload shadow register is updated with the preload value (TIMx_ARR)
The following figures show some examples of the counter behavior for different clock
frequencies when TIMx_ARR = 0x36.
Figure 172. Counter timing diagram, internal clock divided by 1
CK_INT
00
CNT_EN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
Counter overflow
Update event (UEV)
01 02 03 04 05 06 07 32 33 34 35 36 31
RM0008 Basic timers (TIM6&TIM7)
Doc ID 13902 Rev 13 453/1093
Figure 173. Counter timing diagram, internal clock divided by 2
Figure 174. Counter timing diagram, internal clock divided by 4
Figure 175. Counter timing diagram, internal clock divided by N
CK_INT
0035 0000 0001 0002 0003
CNT_EN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
0034 0036
Counter overflow
Update event (UEV)
0000 0001
CNT_EN
TImer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
0035 0036
Counter overflow
Update event (UEV)
CK_INT
Timer clock = CK_CNT
Counter register 00 1F 20
Update interrupt flag (UIF)
Counter overflow
Update event (UEV)
CK_INT
Basic timers (TIM6&TIM7) RM0008
454/1093 Doc ID 13902 Rev 13
Figure 176. Counter timing diagram, update event when ARPE = 0 (TIMx_ARR not
preloaded)
Figure 177. Counter timing diagram, update event when ARPE=1 (TIMx_ARR
preloaded)
17.3.3 Clock source
The counter clock is provided by the Internal clock (CK_INT) source.
The CEN (in the TIMx_CR1 register) and UG bits (in the TIMx_EGR register) are actual
control bits and can be changed only by software (except for UG that remains cleared
automatically). As soon as the CEN bit is written to 1, the prescaler is clocked by the internal
clock CK_INT.
Figure 178 shows the behavior of the control circuit and the upcounter in normal mode,
without prescaler.
00
CNT_EN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
Counter overflow
Update event (UEV)
01 02 03 04 05 06 07 32 33 34 35 36 31
Auto-reload register FF 36
Write a new value in TIMx_ARR
CK_INT
00
CNT_EN
Timer clock = CK_CNT
Counter register
Update interrupt flag (UIF)
Counter overflow
Update event (UEV)
01 02 03 04 05 06 07 F1 F2 F3 F4 F5 F0
Auto-reload preload register F5 36
Auto-reload shadow register F5 36
Write a new value in TIMx_ARR
CK_PSC
RM0008 Basic timers (TIM6&TIM7)
Doc ID 13902 Rev 13 455/1093
Figure 178. Control circuit in normal mode, internal clock divided by 1
17.3.4 Debug mode
When the microcontroller enters the debug mode (Cortex-M3 core - halted), the TIMx
counter either continues to work normally or stops, depending on the DBG_TIMx_STOP
configuration bit in the DBG module. For more details, refer to Section 31.16.2: Debug
support for timers, watchdog, bxCAN and I2C.
17.4 TIM6&TIM7 registers
Refer to Section 2.1 on page 46 for a list of abbreviations used in register descriptions.
The peripheral registers can be accessed by half-words (16-bit) or words (32-bit).
17.4.1 TIM6&TIM7 control register 1 (TIMx_CR1)
Address offset: 0x00
Reset value: 0x0000
CK_INT
00
Counter clock = CK_CNT = CK_PSC
Counter register 01 02 03 04 05 06 07 32 33 34 35 36 31
CEN=CNT_EN
UG
CNT_INIT
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
ARPE
Reserved
OPM URS UDIS CEN
rw rw rw rw rw
Bits 15:8 Reserved, always read as 0
Bit 7 ARPE: Auto-reload preload enable
0: TIMx_ARR register is not buffered.
1: TIMx_ARR register is buffered.
Bits 6:4 Reserved, always read as 0
Bit 3 OPM: One-pulse mode
0: Counter is not stopped at update event
1: Counter stops counting at the next update event (clearing the CEN bit).
Basic timers (TIM6&TIM7) RM0008
456/1093 Doc ID 13902 Rev 13
Bit 2 URS: Update request source
This bit is set and cleared by software to select the UEV event sources.
0: Any of the following events generates an update interrupt or DMA request if enabled.
These events can be:
Counter overflow/underflow
Setting the UG bit
Update generation through the slave mode controller
1: Only counter overflow/underflow generates an update interrupt or DMA request if
enabled.
Bit 1 UDIS: Update disable
This bit is set and cleared by software to enable/disable UEV event generation.
0: UEV enabled. The Update (UEV) event is generated by one of the following events:
Counter overflow/underflow
Setting the UG bit
Update generation through the slave mode controller
Buffered registers are then loaded with their preload values.
1: UEV disabled. The Update event is not generated, shadow registers keep their value
(ARR, PSC). However the counter and the prescaler are reinitialized if the UG bit is set or if
a hardware reset is received from the slave mode controller.
Bit 0 CEN: Counter enable
0: Counter disabled
1: Counter enabled
Note: Gated mode can work only if the CEN bit has been previously set by software. However
trigger mode can set the CEN bit automatically by hardware.
CEN is cleared automatically in one-pulse mode, when an update event occurs.
RM0008 Basic timers (TIM6&TIM7)
Doc ID 13902 Rev 13 457/1093
17.4.2 TIM6&TIM7 control register 2 (TIMx_CR2)
Address offset: 0x04
Reset value: 0x0000
17.4.3 TIM6&TIM7 DMA/Interrupt enable register (TIMx_DIER)
Address offset: 0x0C
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
MMS[2:0]
Reserved
rw rw rw
Bits 15:7 Reserved, must be kept at reset value.
Bits 6:4 MMS: Master mode selection
These bits are used to select the information to be sent in master mode to slave timers for
synchronization (TRGO). The combination is as follows:
000: Reset - the UG bit from the TIMx_EGR register is used as a trigger output (TRGO). If
reset is generated by the trigger input (slave mode controller configured in reset mode) then
the signal on TRGO is delayed compared to the actual reset.
001: Enable - the Counter enable signal, CNT_EN, is used as a trigger output (TRGO). It is
useful to start several timers at the same time or to control a window in which a slave timer
is enabled. The Counter Enable signal is generated by a logic OR between CEN control bit
and the trigger input when configured in gated mode.
When the Counter Enable signal is controlled by the trigger input, there is a delay on TRGO,
except if the master/slave mode is selected (see the MSM bit description in the TIMx_SMCR
register).
010: Update - The update event is selected as a trigger output (TRGO). For instance a
master timer can then be used as a prescaler for a slave timer.
Bits 3:0 Reserved, always read as 0
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
UDE
Reserved
UIE
rw rw
Bit 15:9 Reserved, must be kept at reset value.
Bit 8 UDE: Update DMA request enable
0: Update DMA request disabled.
1: Update DMA request enabled.
Bit 7:1 Reserved, must be kept at reset value.
Bit 0 UIE: Update interrupt enable
0: Update interrupt disabled.
1: Update interrupt enabled.
Basic timers (TIM6&TIM7) RM0008
458/1093 Doc ID 13902 Rev 13
17.4.4 TIM6&TIM7 status register (TIMx_SR)
Address offset: 0x10
Reset value: 0x0000
17.4.5 TIM6&TIM7 event generation register (TIMx_EGR)
Address offset: 0x14
Reset value: 0x0000
17.4.6 TIM6&TIM7 counter (TIMx_CNT)
Address offset: 0x24
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
UIF
rc_w0
Bits 15:1 Reserved, must be kept at reset value.
Bit 0 UIF: Update interrupt flag
This bit is set by hardware on an update event. It is cleared by software.
0: No update occurred.
1: Update interrupt pending. This bit is set by hardware when the registers are updated:
At overflow or underflow regarding the repetition counter value and if UDIS = 0 in the
TIMx_CR1 register.
When CNT is reinitialized by software using the UG bit in the TIMx_EGR register, if
URS = 0 and UDIS = 0 in the TIMx_CR1 register.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
UG
w
Bits 15:1 Reserved, must be kept at reset value.
Bit 0 UG: Update generation
This bit can be set by software, it is automatically cleared by hardware.
0: No action.
1: Re-initializes the timer counter and generates an update of the registers. Note that the
prescaler counter is cleared too (but the prescaler ratio is not affected).
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CNT[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 CNT[15:0]: Counter value
RM0008 Basic timers (TIM6&TIM7)
Doc ID 13902 Rev 13 459/1093
17.4.7 TIM6&TIM7 prescaler (TIMx_PSC)
Address offset: 0x28
Reset value: 0x0000
17.4.8 TIM6&TIM7 auto-reload register (TIMx_ARR)
Address offset: 0x2C
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
PSC[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 PSC[15:0]: Prescaler value
The counter clock frequency CK_CNT is equal to f
CK_PSC
/ (PSC[15:0] + 1).
PSC contains the value to be loaded into the active prescaler register at each update event.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
ARR[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 ARR[15:0]: Prescaler value
ARR is the value to be loaded into the actual auto-reload register.
Refer to Section 17.3.1: Time-base unit on page 450 for more details about ARR update and
behavior.
The counter is blocked while the auto-reload value is null.
Basic timers (TIM6&TIM7) RM0008
460/1093 Doc ID 13902 Rev 13
17.4.9 TIM6&TIM7 register map
TIMx registers are mapped as 16-bit addressable registers as described in the table below:
Refer to for the register boundary addresses.
Table 94. TIM6&TIM7 register map and reset values
Offset Register
3
1
3
0
2
9
2
8
2
7
2
6
2
5
2
4
2
3
2
2
2
1
2
0
1
9
1
8
1
7
1
6
1
5
1
4
1
3
1
2
1
1
1
0
9 8 7 6 5 4 3 2 1 0
0x00
TIMx_CR1
Reserved
A
R
P
E
R
e
s
e
r
v
e
d
O
P
M
U
R
S
U
D
I
S
C
E
N
Reset value 0 0 0 0 0
0x04
TIMx_CR2
Reserved
MMS[2:0]
R
e
s
e
r
v
e
d
Reset value 0 0 0
0x08 Reserved
0x0C
TIMx_DIER
Reserved
U
D
E
R
e
s
e
r
v
e
d
U
I
E
Reset value 0 0
0x10
TIMx_SR
Reserved U
I
F
Reset value 0
0x14
TIMx_EGR
Reserved U
G
Reset value 0
0x18 Reserved
0x1C Reserved
0x20 Reserved
0x24
TIMx_CNT
Reserved
CNT[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x28
TIMx_PSC
Reserved
PSC[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x2C
TIMx_ARR
Reserved
ARR[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
RM0008 Real-time clock (RTC)
Doc ID 13902 Rev 13 461/1093
18 Real-time clock (RTC)
Low-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 16 and 32 Kbytes.
Medium-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 64 and 128 Kbytes.
High-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 256 and 512 Kbytes.
XL-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 768 Kbytes and 1 Mbyte.
Connectivity line devices are STM32F105xx and STM32F107xx microcontrollers.
This section applies to the whole STM32F10xxx family, unless otherwise specified.
18.1 RTC introduction
The real-time clock is an independent timer. The RTC provides a set of continuously running
counters which can be used, with suitable software, to provide a clock-calendar function.
The counter values can be written to set the current time/date of the system.
The RTC core and clock configuration (RCC_BDCR register) are in the Backup domain,
which means that RTC setting and time are kept after reset or wakeup from Standby mode.
After reset, access to the Backup registers and RTC is disabled and the Backup domain
(BKP) is protected against possible parasitic write access. To enable access to the Backup
registers and the RTC, proceed as follows:
enable the power and backup interface clocks by setting the PWREN and BKPEN bits
in the RCC_APB1ENR register
set the DBP bit the Power Control Register (PWR_CR) to enable access to the Backup
registers and RTC.
Real-time clock (RTC) RM0008
462/1093 Doc ID 13902 Rev 13
18.2 RTC main features
Programmable prescaler: division factor up to 2
20
32-bit programmable counter for long-term measurement
Two separate clocks: PCLK1 for the APB1 interface and RTC clock (must be at least
four times slower than the PCLK1 clock)
The RTC clock source could be any of the following ones:
HSE clock divided by 128
LSE oscillator clock
LSI oscillator clock (refer to Section 7.2.8: RTC clock for details)
Two separate reset types:
The APB1 interface is reset by system reset
The RTC Core (Prescaler, Alarm, Counter and Divider) is reset only by a Backup
domain reset (see Section 7.1.3: Backup domain reset on page 88).
Three dedicated maskable interrupt lines:
Alarm interrupt, for generating a software programmable alarm interrupt.
Seconds interrupt, for generating a periodic interrupt signal with a programmable
period length (up to 1 second).
Overflow interrupt, to detect when the internal programmable counter rolls over to
zero.
RM0008 Real-time clock (RTC)
Doc ID 13902 Rev 13 463/1093
18.3 RTC functional description
18.3.1 Overview
The RTC consists of two main units (see Figure 179 on page 463). The first one (APB1
Interface) is used to interface with the APB1 bus. This unit also contains a set of 16-bit
registers accessible from the APB1 bus in read or write mode (for more information refer to
Section 18.4: RTC registers on page 466). The APB1 interface is clocked by the APB1 bus
clock in order to interface with the APB1 bus.
The other unit (RTC Core) consists of a chain of programmable counters made of two main
blocks. The first block is the RTC prescaler block, which generates the RTC time base
TR_CLK that can be programmed to have a period of up to 1 second. It includes a 20-bit
programmable divider (RTC Prescaler). Every TR_CLK period, the RTC generates an
interrupt (Second Interrupt) if it is enabled in the RTC_CR register. The second block is a
32-bit programmable counter that can be initialized to the current system time. The system
time is incremented at the TR_CLK rate and compared with a programmable date (stored in
the RTC_ALR register) in order to generate an alarm interrupt, if enabled in the RTC_CR
control register.
Figure 179. RTC simplified block diagram
32-bit programmable
=
Reload
APB1 interface
APB1 bus
NVIC interrupt
controller
OWF
rising
edge
counter
OWIE
SECF
SECIE
ALRF
ALRIE
Standby mode
exit
powered in Standby
powered in Standby
not powered in Standby
not powered in Standby
powered in Standby
not powered in Standby
Backup domain
RTC_PRL
RTC_DIV RTC_CNT
ai14969b
PCLK1
RTCCLK
RTC_CR
RTC_ALR
WKUP pin
WKP_STDBY
RTC_Alarm
RTC_Alarm
RTC_Overflow
RTC_Second
TR_CLK
RTC prescaler
Real-time clock (RTC) RM0008
464/1093 Doc ID 13902 Rev 13
18.3.2 Resetting RTC registers
All system registers are asynchronously reset by a System Reset or Power Reset, except for
RTC_PRL, RTC_ALR, RTC_CNT, and RTC_DIV.
The RTC_PRL, RTC_ALR, RTC_CNT, and RTC_DIV registers are reset only by a Backup
Domain reset. Refer to Section 7.1.3 on page 88.
18.3.3 Reading RTC registers
The RTC core is completely independent from the RTC APB1 interface.
Software accesses the RTC prescaler, counter and alarm values through the APB1 interface
but the associated readable registers are internally updated at each rising edge of the RTC
clock resynchronized by the RTC APB1 clock. This is also true for the RTC flags.
This means that the first read to the RTC APB1 registers may be corrupted (generally read
as 0) if the APB1 interface has previously been disabled and the read occurs immediately
after the APB1 interface is enabled but before the first internal update of the registers. This
can occur if:
A system reset or power reset has occurred
The MCU has just woken up from Standby mode (see Section 5.3: Low-power modes)
The MCU has just woken up from Stop mode (see Section 5.3: Low-power modes)
In all the above cases, the RTC core has been kept running while the APB1 interface was
disabled (reset, not clocked or unpowered).
Consequently when reading the RTC registers, after having disabled the RTC APB1
interface, the software must first wait for the RSF bit (Register Synchronized Flag) in the
RTC_CRL register to be set by hardware.
Note that the RTC APB1 interface is not affected by WFI and WFE low-power modes.
18.3.4 Configuring RTC registers
To write in the RTC_PRL, RTC_CNT, RTC_ALR registers, the peripheral must enter
Configuration Mode. This is done by setting the CNF bit in the RTC_CRL register.
In addition, writing to any RTC register is only enabled if the previous write operation is
finished. To enable the software to detect this situation, the RTOFF status bit is provided in
the RTC_CR register to indicate that an update of the registers is in progress. A new value
can be written to the RTC registers only when the RTOFF status bit value is 1.
Configuration procedure:
1. Poll RTOFF, wait until its value goes to 1
2. Set the CNF bit to enter configuration mode
3. Write to one or more RTC registers
4. Clear the CNF bit to exit configuration mode
5. Poll RTOFF, wait until its value goes to 1 to check the end of the write operation.
The write operation only executes when the CNF bit is cleared; it takes at least three
RTCCLK cycles to complete.
RM0008 Real-time clock (RTC)
Doc ID 13902 Rev 13 465/1093
18.3.5 RTC flag assertion
The RTC Second flag (SECF) is asserted on each RTC Core clock cycle before the update
of the RTC Counter.
The RTC Overflow flag (OWF) is asserted on the last RTC Core clock cycle before the
counter reaches 0x0000.
The RTC_Alarm and RTC Alarm flag (ALRF) (see Figure 180) are asserted on the last RTC
Core clock cycle before the counter reaches the RTC Alarm value stored in the Alarm
register increased by one (RTC_ALR + 1). The write operation in the RTC Alarm and RTC
Second flag must be synchronized by using one of the following sequences:
Use the RTC Alarm interrupt and inside the RTC interrupt routine, the RTC Alarm
and/or RTC Counter registers are updated.
Wait for SECF bit to be set in the RTC Control register. Update the RTC Alarm and/or
the RTC Counter register.
Figure 180. RTC second and alarm waveform example with PR=0003, ALARM=00004
Figure 181. RTC Overflow waveform example with PR=0003
RTC_CNT 0000 0001
RTC_PR 0002 0001 0000 0003 0002 0001 0000 0003
0002
RTC_ALARM
0002 0001 0000 0003
0003
0002 0001 0000 0003
0004
0002 0001 0000 0003
ALRF can be cleared by software
RTC_Second
RTCCLK
0005
0002 0001 0000 0003
(not powered
in Standby)
1 RTCCLK
RTC_CNT FFFFFFFB FFFFFFFC
RTC_PR 0002 0001 0000 0003 0002 0001 0000 0003
FFFFFFFD
RTC_Overflow
0002 0001 0000 0003
FFFFFFFE
0002 0001 0000 0003
FFFFFFFF
0002 0001 0000 0003
OWF can be cleared by software
RTC_Second
RTCCLK
0000
0002 0001 0000 0003
(not powered
in Standby)
1 RTCCLK
Real-time clock (RTC) RM0008
466/1093 Doc ID 13902 Rev 13
18.4 RTC registers
Refer to Section 2.1 on page 46 for a list of abbreviations used in register descriptions.
The peripheral registers can be accessed by half-words (16-bit) or words (32-bit).
18.4.1 RTC control register high (RTC_CRH)
Address offset: 0x00
Reset value: 0x0000
These bits are used to mask interrupt requests. Note that at reset all interrupts are disabled,
so it is possible to write to the RTC registers to ensure that no interrupt requests are pending
after initialization. It is not possible to write to the RTC_CRH register when the peripheral is
completing a previous write operation (flagged by RTOFF=0, see Section 18.3.4 on page
464).
The RTC functions are controlled by this control register. Some bits must be written using a
specific configuration procedure (see Configuration procedure:).
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
OWIE ALRIE SECIE
rw rw rw
Bits 15:3 Reserved, forced by hardware to 0.
Bit 2 OWIE: Overflow interrupt enable
0: Overflow interrupt is masked.
1: Overflow interrupt is enabled.
Bit 1 ALRIE: Alarm interrupt enable
0: Alarm interrupt is masked.
1: Alarm interrupt is enabled.
Bit 0 SECIE: Second interrupt enable
0: Second interrupt is masked.
1: Second interrupt is enabled.
RM0008 Real-time clock (RTC)
Doc ID 13902 Rev 13 467/1093
18.4.2 RTC control register low (RTC_CRL)
Address offset: 0x04
Reset value: 0x0020
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
RTOFF CNF RSF OWF ALRF SECF
r rw rc_w0 rc_w0 rc_w0 rc_w0
Bits 15:6 Reserved, forced by hardware to 0.
Bit 5 RTOFF: RTC operation OFF
With this bit the RTC reports the status of the last write operation performed on its registers,
indicating if it has been completed or not. If its value is 0 then it is not possible to write to any
of the RTC registers. This bit is read only.
0: Last write operation on RTC registers is still ongoing.
1: Last write operation on RTC registers terminated.
Bit 4 CNF: Configuration flag
This bit must be set by software to enter in configuration mode so as to allow new values to
be written in the RTC_CNT, RTC_ALR or RTC_PRL registers. The write operation is only
executed when the CNF bit is reset by software after has been set.
0: Exit configuration mode (start update of RTC registers).
1: Enter configuration mode.
Bit 3 RSF: Registers synchronized flag
This bit is set by hardware at each time the RTC_CNT and RTC_DIV registers are updated
and cleared by software. Before any read operation after an APB1 reset or an APB1 clock
stop, this bit must be cleared by software, and the user application must wait until it is set to
be sure that the RTC_CNT, RTC_ALR or RTC_PRL registers are synchronized.
0: Registers not yet synchronized.
1: Registers synchronized.
Bit 2 OWF: Overflow flag
This bit is set by hardware when the 32-bit programmable counter overflows. An interrupt is
generated if OWIE=1 in the RTC_CRH register. It can be cleared only by software. Writing 1
has no effect.
0: Overflow not detected
1: 32-bit programmable counter overflow occurred.
Bit 1 ALRF: Alarm flag
This bit is set by hardware when the 32-bit programmable counter reaches the threshold set
in the RTC_ALR register. An interrupt is generated if ALRIE=1 in the RTC_CRH register. It
can be cleared only by software. Writing 1 has no effect.
0: Alarm not detected
1: Alarm detected
Bit 0 SECF: Second flag
This bit is set by hardware when the 32-bit programmable prescaler overflows, thus
incrementing the RTC counter. Hence this flag provides a periodic signal with a period
corresponding to the resolution programmed for the RTC counter (usually one second). An
interrupt is generated if SECIE=1 in the RTC_CRH register. It can be cleared only by
software. Writing 1 has no effect.
0: Second flag condition not met.
1: Second flag condition met.
Real-time clock (RTC) RM0008
468/1093 Doc ID 13902 Rev 13
The functions of the RTC are controlled by this control register. It is not possible to write to
the RTC_CR register while the peripheral is completing a previous write operation (flagged
by RTOFF=0, see Section 18.3.4 on page 464).
Note: 1 Any flag remains pending until the appropriate RTC_CR request bit is reset by software,
indicating that the interrupt request has been granted.
2 At reset the interrupts are disabled, no interrupt requests are pending and it is possible to
write to the RTC registers.
3 The OWF, ALRF, SECF and RSF bits are not updated when the APB1 clock is not running.
4 The OWF, ALRF, SECF and RSF bits can only be set by hardware and only cleared by
software.
5 If ALRF = 1 and ALRIE = 1, the RTC global interrupt is enabled. If EXTI Line 17 is also
enabled through the EXTI Controller, both the RTC global interrupt and the RTC Alarm
interrupt are enabled.
6 If ALRF = 1, the RTC Alarm interrupt is enabled if EXTI Line 17 is enabled through the EXTI
Controller in interrupt mode. When the EXTI Line 17 is enabled in event mode, a pulse is
generated on this line (no RTC Alarm interrupt generation).
18.4.3 RTC prescaler load register (RTC_PRLH / RTC_PRLL)
The Prescaler Load registers keep the period counting value of the RTC prescaler. They are
write-protected by the RTOFF bit in the RTC_CR register, and a write operation is allowed if
the RTOFF value is 1.
RTC prescaler load register high (RTC_PRLH)
Address offset: 0x08
Write only (see Section 18.3.4 on page 464)
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
PRL[19:16]
w w w w
Bits 15:4 Reserved, forced by hardware to 0.
Bits 3:0 PRL[19:16]: RTC prescaler reload value high
These bits are used to define the counter clock frequency according to the following formula:
f
TR_CLK
= f
RTCCLK
/(PRL[19:0]+1)
RM0008 Real-time clock (RTC)
Doc ID 13902 Rev 13 469/1093
RTC prescaler load register low (RTC_PRLL)
Address offset: 0x0C
Write only (see Section 18.3.4 on page 464)
Reset value: 0x8000
Note: If the input clock frequency (f
RTCCLK
) is 32.768 kHz, write 7FFFh in this register to get a
signal period of 1 second.
18.4.4 RTC prescaler divider register (RTC_DIVH / RTC_DIVL)
During each period of TR_CLK, the counter inside the RTC prescaler is reloaded with the
value stored in the RTC_PRL register. To get an accurate time measurement it is possible to
read the current value of the prescaler counter, stored in the RTC_DIV register, without
stopping it. This register is read-only and it is reloaded by hardware after any change in the
RTC_PRL or RTC_CNT registers.
RTC prescaler divider register high (RTC_DIVH)
Address offset: 0x10
Reset value: 0x0000
RTC prescaler divider register low (RTC_DIVL)
Address offset: 0x14
Reset value: 0x8000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
PRL[15:0]
w w w w w w w w w w w w w w w w
Bits 15:0 PRL[15:0]: RTC prescaler reload value low
These bits are used to define the counter clock frequency according to the following formula:
f
TR_CLK
= f
RTCCLK
/(PRL[19:0]+1)
Caution: The zero value is not recommended. RTC interrupts and flags cannot be
asserted correctly.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
RTC_DIV[19:16]
r r r r
Bits 15:4 Reserved
Bits 3:0 RTC_DIV[19:16]: RTC clock divider high
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
RTC_DIV[15:0]
r r r r r r r r r r r r r r r r
Bits 15:0 RTC_DIV[15:0]: RTC clock divider low
Real-time clock (RTC) RM0008
470/1093 Doc ID 13902 Rev 13
18.4.5 RTC counter register (RTC_CNTH / RTC_CNTL)
The RTC core has one 32-bit programmable counter, accessed through two 16-bit registers;
the count rate is based on the TR_CLK time reference, generated by the prescaler.
RTC_CNT registers keep the counting value of this counter. They are write-protected by bit
RTOFF in the RTC_CR register, and a write operation is allowed if the RTOFF value is 1. A
write operation on the upper (RTC_CNTH) or lower (RTC_CNTL) registers directly loads the
corresponding programmable counter and reloads the RTC Prescaler. When reading, the
current value in the counter (system date) is returned.
RTC counter register high (RTC_CNTH)
Address offset: 0x18
Reset value: 0x0000
RTC counter register low (RTC_CNTL)
Address offset: 0x1C
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
RTC_CNT[31:16]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 RTC_CNT[31:16]: RTC counter high
Reading the RTC_CNTH register, the current value of the high part of the RTC Counter
register is returned. To write to this register it is necessary to enter configuration mode (see
Section 18.3.4: Configuring RTC registers on page 464).
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
RTC_CNT[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 RTC_CNT[15:0]: RTC counter low
Reading the RTC_CNTL register, the current value of the lower part of the RTC Counter
register is returned. To write to this register it is necessary to enter configuration mode (see
Section 18.3.4: Configuring RTC registers on page 464).
RM0008 Real-time clock (RTC)
Doc ID 13902 Rev 13 471/1093
18.4.6 RTC alarm register high (RTC_ALRH / RTC_ALRL)
When the programmable counter reaches the 32-bit value stored in the RTC_ALR register,
an alarm is triggered and the RTC_alarmIT interrupt request is generated. This register is
write-protected by the RTOFF bit in the RTC_CR register, and a write operation is allowed if
the RTOFF value is 1.
RTC alarm register high (RTC_ALRH)
Address offset: 0x20
Write only (see Section 18.3.4 on page 464)
Reset value: 0xFFFF
RTC alarm register low (RTC_ALRL)
Address offset: 0x24
Write only (see Section 18.3.4 on page 464)
Reset value: 0xFFFF
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
RTC_ALR[31:16]
w w w w w w w w w w w w w w w w
Bits 15:0 RTC_ALR[31:16]: RTC alarm high
The high part of the alarm time is written by software in this register. To write to this register
it is necessary to enter configuration mode (see Section 18.3.4: Configuring RTC registers
on page 464).
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
RTC_ALR[15:0]
w w w w w w w w w w w w w w w w
Bits 15:0 RTC_ALR[15:0]: RTC alarm low
The low part of the alarm time is written by software in this register. To write to this register it
is necessary to enter configuration mode (see Section 18.3.4: Configuring RTC registers on
page 464).
Real-time clock (RTC) RM0008
472/1093 Doc ID 13902 Rev 13
18.4.7 RTC register map
RTC registers are mapped as 16-bit addressable registers as described in the table below:
Refer to Table 3 on page 50 for the register boundary addresses.
Table 95. RTC register map and reset values
Offset Register
3
1
3
0
2
9
2
8
2
7
2
6
2
5
2
4
2
3
2
2
2
1
2
0
1
9
1
8
1
7
1
6
1
5
1
4
1
3
1
2
1
1
1
0
9 8 7 6 5 4 3 2 1 0
0x00
RTC_CRH
Reserved
O
W
I
E
A
L
R
I
E
S
E
C
I
E
Reset value 0 0 0
0x04
RTC_CRL
Reserved
R
T
O
F
F
C
N
F
R
S
F
O
W
F
A
L
R
F
S
E
C
F
Reset value 1 0 0 0 0 0
0x08
RTC_PRLH
Reserved
PRL[19:16]
Reset value 0 0 0 0
0x0C
RTC_PRLL
Reserved
PRL[15:0]
Reset value 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x10
RTC_DIVH
Reserved
DIV[31:16]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x14
RTC_DIVL
Reserved
DIV[15:0]
Reset value 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x18 RTC_CNTH
Reserved
CNT[13:16]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x1C
RTC_CNTL
Reserved
CNT[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x20
RTC_ALRH
Reserved
ALR[31:16]
Reset value 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0x24
RTC_ALRL
Reserved
ALR[15:0]
Reset value 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
RM0008 Independent watchdog (IWDG)
Doc ID 13902 Rev 13 473/1093
19 Independent watchdog (IWDG)
Low-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 16 and 32 Kbytes.
Medium-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 64 and 128 Kbytes.
High-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 256 and 512 Kbytes.
XL-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 768 Kbytes and 1 Mbyte.
Connectivity line devices are STM32F105xx and STM32F107xx microcontrollers.
This section applies to the whole STM32F10xxx family, unless otherwise specified.
19.1 IWDG introduction
The STM32F10xxx have two embedded watchdog peripherals which offer a combination of
high safety level, timing accuracy and flexibility of use. Both watchdog peripherals
(Independent and Window) serve to detect and resolve malfunctions due to software failure,
and to trigger system reset or an interrupt (window watchdog only) when the counter
reaches a given timeout value.
The independent watchdog (IWDG) is clocked by its own dedicated low-speed clock (LSI)
and thus stays active even if the main clock fails. The window watchdog (WWDG) clock is
prescaled from the APB1 clock and has a configurable time-window that can be
programmed to detect abnormally late or early application behavior.
The IWDG is best suited to applications which require the watchdog to run as a totally
independent process outside the main application, but have lower timing accuracy
constraints. The WWDG is best suited to applications which require the watchdog to react
within an accurate timing window. For further information on the window watchdog, refer to
Section 20 on page 478.
19.2 IWDG main features
Free-running downcounter
clocked from an independent RC oscillator (can operate in Standby and Stop modes)
Reset (if watchdog activated) when the downcounter value of 0x000 is reached
19.3 IWDG functional description
Figure 182 shows the functional blocks of the independent watchdog module.
When the independent watchdog is started by writing the value 0xCCCC in the Key register
(IWDG_KR), the counter starts counting down from the reset value of 0xFFF. When it
reaches the end of count value (0x000) a reset signal is generated (IWDG reset).
Independent watchdog (IWDG) RM0008
474/1093 Doc ID 13902 Rev 13
Whenever the key value 0xAAAA is written in the IWDG_KR register, the IWDG_RLR value
is reloaded in the counter and the watchdog reset is prevented.
19.3.1 Hardware watchdog
If the Hardware watchdog feature is enabled through the device option bits, the watchdog
is automatically enabled at power-on, and will generate a reset unless the Key register is
written by the software before the counter reaches end of count.
19.3.2 Register access protection
Write access to the IWDG_PR and IWDG_RLR registers is protected. To modify them, you
must first write the code 0x5555 in the IWDG_KR register. A write access to this register
with a different value will break the sequence and register access will be protected again.
This implies that it is the case of the reload operation (writing 0xAAAA).
A status register is available to indicate that an update of the prescaler or the down-counter
reload value is on going.
19.3.3 Debug mode
When the microcontroller enters debug mode (Cortex-M3 core halted), the IWDG counter
either continues to work normally or stops, depending on DBG_IWDG_STOP configuration
bit in DBG module. For more details, refer to Section 31.16.2: Debug support for timers,
watchdog, bxCAN and I2C.
Figure 182. Independent watchdog block diagram
Note: The watchdog function is implemented in the V
DD
voltage domain that is still functional in
Stop and Standby modes.
IWDG RESET
prescaler
12-bit downcounter
IWDG_PR
Prescaler register
IWDG_RLR
Reload register
8-bit
LSI
IWDG_KR
Key register
1.8 V voltage domain
V
DD
voltage domain
IWDG_SR
Status register
12-bit reload value
(40 kHz)
Table 96. Min/max IWDG timeout period at 40 kHz (LSI)
(1)
Prescaler divider PR[2:0] bits
Min timeout (ms) RL[11:0]=
0x000
Max timeout (ms) RL[11:0]=
0xFFF
/4 0 0.1 409.6
/8 1 0.2 819.2
/16 2 0.4 1638.4
/32 3 0.8 3276.8
/64 4 1.6 6553.6
RM0008 Independent watchdog (IWDG)
Doc ID 13902 Rev 13 475/1093
The LSI can be calibrated so as to compute the IWDG timeout with an acceptable accuracy.
For more details refer to LSI clock on page 93.
19.4 IWDG registers
Refer to Section 2.1 on page 46 for a list of abbreviations used in register descriptions.
The peripheral registers can be accessed by half-words (16-bit) or words (32-bit).
19.4.1 Key register (IWDG_KR)
Address offset: 0x00
Reset value: 0x0000 0000 (reset by Standby mode)
/128 5 3.2 13107.2
/256 6 (or 7) 6.4 26214.4
1. These timings are given for a 40 kHz clock but the microcontrollers internal RC frequency can vary from 30
to 60 kHz. Moreover, given an exact RC oscillator frequency, the exact timings still depend on the phasing
of the APB interface clock versus the LSI clock so that there is always a full RC period of uncertainty.
Table 96. Min/max IWDG timeout period at 40 kHz (LSI) (continued)
(1)
Prescaler divider PR[2:0] bits
Min timeout (ms) RL[11:0]=
0x000
Max timeout (ms) RL[11:0]=
0xFFF
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
KEY[15:0]
w w w w w w w w w w w w w w w w
Bits 31:16 Reserved, read as 0.
Bits 15:0 KEY[15:0]: Key value (write only, read 0000h)
These bits must be written by software at regular intervals with the key value AAAAh,
otherwise the watchdog generates a reset when the counter reaches 0.
Writing the key value 5555h to enable access to the IWDG_PR and IWDG_RLR registers
(see Section 19.3.2)
Writing the key value CCCCh starts the watchdog (except if the hardware watchdog option is
selected)
Independent watchdog (IWDG) RM0008
476/1093 Doc ID 13902 Rev 13
19.4.2 Prescaler register (IWDG_PR)
Address offset: 0x04
Reset value: 0x0000 0000
19.4.3 Reload register (IWDG_RLR)
Address offset: 0x08
Reset value: 0x0000 0FFF (reset by Standby mode)
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
PR[2:0]
rw rw rw
Bits 31:3 Reserved, read as 0.
Bits 2:0 PR[2:0]: Prescaler divider
These bits are write access protected seeSection 19.3.2. They are written by software to
select the prescaler divider feeding the counter clock. PVU bit of IWDG_SR must be reset in
order to be able to change the prescaler divider.
000: divider /4
001: divider /8
010: divider /16
011: divider /32
100: divider /64
101: divider /128
110: divider /256
111: divider /256
Note: Reading this register returns the prescaler value from the VDD voltage domain. This
value may not be up to date/valid if a write operation to this register is ongoing. For this
reason the value read from this register is valid only when the PVU bit in the IWDG_SR
register is reset.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
RL[11:0]
rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:12 Reserved, read as 0.
Bits11:0 RL[11:0]: Watchdog counter reload value
These bits are write access protected see Section 19.3.2. They are written by software to
define the value to be loaded in the watchdog counter each time the value AAAAh is written
in the IWDG_KR register. The watchdog counter counts down from this value. The timeout
period is a function of this value and the clock prescaler. Refer to Table 96.
The RVU bit in the IWDG_SR register must be reset in order to be able to change the reload
value.
Note: Reading this register returns the reload value from the VDD voltage domain. This value
may not be up to date/valid if a write operation to this register is ongoing on this register.
For this reason the value read from this register is valid only when the RVU bit in the
IWDG_SR register is reset.
RM0008 Independent watchdog (IWDG)
Doc ID 13902 Rev 13 477/1093
19.4.4 Status register (IWDG_SR)
Address offset: 0x0C
Reset value: 0x0000 0000 (not reset by Standby mode)
Note: If several reload values or prescaler values are used by application, it is mandatory to wait
until RVU bit is reset before changing the reload value and to wait until PVU bit is reset
before changing the prescaler value. However, after updating the prescaler and/or the
reload value it is not necessary to wait until RVU or PVU is reset before continuing code
execution (even in case of low-power mode entry, the write operation is taken into account
and will complete)
19.4.5 IWDG register map
The following table gives the IWDG register map and reset values.
Refer to Table 3 on page 50 for the register boundary addresses.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
RVU PVU
r r
Bits 31:2 Reserved
Bit 1 RVU: Watchdog counter reload value update
This bit is set by hardware to indicate that an update of the reload value is ongoing. It is reset
by hardware when the reload value update operation is completed in the V
DD
voltage domain
(takes up to 5 RC 40 kHz cycles).
Reload value can be updated only when RVU bit is reset.
Bit 0 PVU: Watchdog prescaler value update
This bit is set by hardware to indicate that an update of the prescaler value is ongoing. It is
reset by hardware when the prescaler update operation is completed in the V
DD
voltage
domain (takes up to 5 RC 40 kHz cycles).
Prescaler value can be updated only when PVU bit is reset.
Table 97. IWDG register map and reset values
Offset Register
3
1
3
0
2
9
2
8
2
7
2
6
2
5
2
4
2
3
2
2
2
1
2
0
1
9
1
8
1
7
1
6
1
5
1
4
1
3
1
2
1
1
1
0
9 8 7 6 5 4 3 2 1 0
0x00
IWDG_KR
Reserved
KEY[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x04
IWDG_PR
Reserved
PR[2:0]
Reset value 0 0 0
0x08
IWDG_RLR
Reserved
RL[11:0]
Reset value 1 1 1 1 1 1 1 1 1 1 1 1
0x0C
IWDG_SR
Reserved R
V
U
P
V
U
Reset value 0 0
Window watchdog (WWDG) RM0008
478/1093 Doc ID 13902 Rev 13
20 Window watchdog (WWDG)
Low-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 16 and 32 Kbytes.
Medium-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 64 and 128 Kbytes.
High-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 256 and 512 Kbytes.
XL-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 768 Kbytes and 1 Mbyte.
Connectivity line devices are STM32F105xx and STM32F107xx microcontrollers.
This section applies to the whole STM32F10xxx family, unless otherwise specified.
20.1 WWDG introduction
The window watchdog is used to detect the occurrence of a software fault, usually
generated by external interference or by unforeseen logical conditions, which causes the
application program to abandon its normal sequence. The watchdog circuit generates an
MCU reset on expiry of a programmed time period, unless the program refreshes the
contents of the downcounter before the T6 bit becomes cleared. An MCU reset is also
generated if the 7-bit downcounter value (in the control register) is refreshed before the
downcounter has reached the window register value. This implies that the counter must be
refreshed in a limited window.
20.2 WWDG main features
Programmable free-running downcounter
Conditional reset
Reset (if watchdog activated) when the downcounter value becomes less than
0x40
Reset (if watchdog activated) if the downcounter is reloaded outside the window
(see Figure 184)
Early wakeup interrupt (EWI): triggered (if enabled and the watchdog activated) when
the downcounter is equal to 0x40.
20.3 WWDG functional description
If the watchdog is activated (the WDGA bit is set in the WWDG_CR register) and when the
7-bit downcounter (T[6:0] bits) rolls over from 0x40 to 0x3F (T6 becomes cleared), it initiates
a reset. If the software reloads the counter while the counter is greater than the value stored
in the window register, then a reset is generated.
RM0008 Window watchdog (WWDG)
Doc ID 13902 Rev 13 479/1093
Figure 183. Watchdog block diagram
The application program must write in the WWDG_CR register at regular intervals during
normal operation to prevent an MCU reset. This operation must occur only when the counter
value is lower than the window register value. The value to be stored in the WWDG_CR
register must be between 0xFF and 0xC0:
Enabling the watchdog
The watchdog is always disabled after a reset. It is enabled by setting the WDGA bit in the
WWDG_CR register, then it cannot be disabled again except by a reset.
Controlling the downcounter
This downcounter is free-running: It counts down even if the watchdog is disabled. When the
watchdog is enabled, the T6 bit must be set to prevent generating an immediate reset.
The T[5:0] bits contain the number of increments which represents the time delay before the
watchdog produces a reset. The timing varies between a minimum and a maximum value
due to the unknown status of the prescaler when writing to the WWDG_CR register (see
Figure 184).The Configuration register (WWDG_CFR) contains the high limit of the window:
To prevent a reset, the downcounter must be reloaded when its value is lower than the
window register value and greater than 0x3F. Figure 184 describes the window watchdog
process.
Note: The T6 bit can be used to generate a software reset (the WDGA bit is set and the T6 bit is
cleared).
Advanced watchdog interrupt feature
The Early Wakeup Interrupt (EWI) can be used if specific safety operations or data logging
must be performed before the actual reset is generated. The EWI interrupt is enabled by
setting the EWI bit in the WWDG_CFR register. When the downcounter reaches the value
0x40, an EWI interrupt is generated and the corresponding interrupt service routine (ISR)
can be used to trigger specific actions (such as communications or data logging), before
resetting the device.
In some applications, the EWI interrupt can be used to manage a software system check
and/or system recovery/graceful degradation, without generating a WWDG reset. In this
RESET
WDGA
6-bit downcounter (CNT)
T6
Watchdog control register (WWDG_CR)
T1 T2 T3 T4 T5
-
W6 W0
Watchdog configuration register (WWDG_CFR)
W1 W2 W3 W4 W5
comparator
T6:0 > W6:0
CMP
= 1 when
Write WWDG_CR
WDG prescaler
(WDGTB)
PCLK1
T0
(from RCC clock controller)
Window watchdog (WWDG) RM0008
480/1093 Doc ID 13902 Rev 13
case, the corresponding interrupt service routine (ISR) should reload the WWDG counter to
avoid the WWDG reset, then trigger the required actions.
The EWI interrupt is cleared by writing '0' to the EWIF bit in the WWDG_SR register.
Note: When the EWI interrupt cannot be served, e.g. due to a system lock in a higher priority task,
the WWDG reset will eventually be generated.
20.4 How to program the watchdog timeout
You can use the formula in Figure 184 to calculate the WWDG timeout.
Warning: When writing to the WWDG_CR register, always write 1 in the
T6 bit to avoid generating an immediate reset.
Figure 184. Window watchdog timing diagram
The formula to calculate the timeout value is given by:
where:
t
WWDG
: WWDG timeout
t
PCLK1
: APB1 clock period measured in ms
T6 bit
Reset
W[6:0]
T[6:0] CNT downcounter
time
Refresh window Refresh not allowed
0x3F
ai17101
t
WWDG
t
PCLK1
4096 2
WDGTB
t 5:0 [ ] 1 + ( ) = ms ( )
RM0008 Window watchdog (WWDG)
Doc ID 13902 Rev 13 481/1093
20.5 Debug mode
When the microcontroller enters debug mode (Cortex-M3 core halted), the WWDG counter
either continues to work normally or stops, depending on DBG_WWDG_STOP configuration
bit in DBG module. For more details, refer to Section 31.16.2: Debug support for timers,
watchdog, bxCAN and I2C.
Table 98. Min-max timeout value @36 MHz (PCLK1)
WDGTB Min timeout value Max timeout value
0 113 s 7.28 ms
1 227 s 14.56 ms
2 455 s 29.12 ms
3 910 s 58.25 ms
Window watchdog (WWDG) RM0008
482/1093 Doc ID 13902 Rev 13
20.6 WWDG registers
Refer to Section 2.1 on page 46 for a list of abbreviations used in register descriptions.
The peripheral registers can be accessed by half-words (16-bit) or words (32-bit).
20.6.1 Control register (WWDG_CR)
Address offset: 0x00
Reset value: 0x7F
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
WDGA T[6:0]
rs rw
Bits 31:8 Reserved
Bit 7 WDGA: Activation bit
This bit is set by software and only cleared by hardware after a reset. When WDGA = 1, the
watchdog can generate a reset.
0: Watchdog disabled
1: Watchdog enabled
Bits 6:0 T[6:0]: 7-bit counter (MSB to LSB)
These bits contain the value of the watchdog counter. It is decremented every (4096 x
2
WDGTB
) PCLK1 cycles. A reset is produced when it rolls over from 0x40 to 0x3F (T6
becomes cleared).
RM0008 Window watchdog (WWDG)
Doc ID 13902 Rev 13 483/1093
20.6.2 Configuration register (WWDG_CFR)
Address offset: 0x04
Reset value: 0x7F
20.6.3 Status register (WWDG_SR)
Address offset: 0x08
Reset value: 0x00
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
EWI
WDGTB[1:0]
W[6:0]
rs rw rw
Bit 31:10 Reserved
Bit 9 EWI: Early wakeup interrupt
When set, an interrupt occurs whenever the counter reaches the value 0x40. This interrupt is
only cleared by hardware after a reset.
Bits 8:7 WDGTB[1:0]: Timer base
The time base of the prescaler can be modified as follows:
00: CK Counter Clock (PCLK1 div 4096) div 1
01: CK Counter Clock (PCLK1 div 4096) div 2
10: CK Counter Clock (PCLK1 div 4096) div 4
11: CK Counter Clock (PCLK1 div 4096) div 8
Bits 6:0 W[6:0]: 7-bit window value
These bits contain the window value to be compared to the downcounter.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
EWIF
rc_w0
Bit 31:1Reserved
Bit 0 EWIF: Early wakeup interrupt flag
This bit is set by hardware when the counter has reached the value 0x40. It must be cleared
by software by writing 0. A write of 1 has no effect. This bit is also set if the interrupt is not
enabled.
Window watchdog (WWDG) RM0008
484/1093 Doc ID 13902 Rev 13
20.6.4 WWDG register map
The following table gives the WWDG register map and reset values.
Refer to Table 3 on page 50 for the register boundary addresses.
Table 99. WWDG register map and reset values
Offset Register
3
1
3
0
2
9
2
8
2
7
2
6
2
5
2
4
2
3
2
2
2
1
2
0
1
9
1
8
1
7
1
6
1
5
1
4
1
3
1
2
1
1
1
0
9 8 7 6 5 4 3 2 1 0
0x00
WWDG_CR
Reserved
W
D
G
A
T[6:0]
Reset value 0 1 1 1 1 1 1 1
0x04
WWDG_CFR
Reserved E
W
I
W
D
G
T
B
1
W
D
G
T
B
0
W[6:0]
Reset value 0 0 0 1 1 1 1 1 1 1
0x08
WWDG_SR
Reserved
E
W
I
F
Reset value 0
RM0008 Flexible static memory controller (FSMC)
Doc ID 13902 Rev 13 485/1093
21 Flexible static memory controller (FSMC)
Low-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 16 and 32 Kbytes.
Medium-density devices are STM32F101xx and STM32F103xx microcontrollers where
the Flash memory density ranges between 32 and 128 Kbytes.
High-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 256 and 512 Kbytes.
XL-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 768 Kbytes and 1 Mbyte.
Connectivity line devices are STM32F105xx and STM32F107xx microcontrollers.
This section applies to high-density and XL-density devices only.
21.1 FSMC main features
The FSMC block is able to interface with synchronous and asynchronous memories and 16-
bit PC memory cards. Its main purpose is to:
Translate the AHB transactions into the appropriate external device protocol
Meet the access timing requirements of the external devices
All external memories share the addresses, data and control signals with the controller.
Each external device is accessed by means of a unique chip select. The FSMC performs
only one access at a time to an external device.
The FSMC has the following main features:
Interfaces with static memory-mapped devices including:
Static random access memory (SRAM)
Read-only memory (ROM)
NOR Flash memory
PSRAM (4 memory banks)
Two banks of NAND Flash with ECC hardware that checks up to 8 Kbytes of data
16-bit PC Card compatible devices
Supports burst mode access to synchronous devices (NOR Flash and PSRAM)
8- or 16-bit wide databus
Independent chip select control for each memory bank
Independent configuration for each memory bank
Programmable timings to support a wide range of devices, in particular:
Programmable wait states (up to 15)
Programmable bus turnaround cycles (up to 15)
Programmable output enable and write enable delays (up to 15)
Independent read and write timings and protocol, so as to support the widest
variety of memories and timings
Flexible static memory controller (FSMC) RM0008
486/1093 Doc ID 13902 Rev 13
Write enable and byte lane select outputs for use with PSRAM and SRAM devices
Translation of 32-bit wide AHB transactions into consecutive 16-bit or 8-bit accesses to
external 16-bit or 8-bit devices
A Write FIFO, 2 words long, each word is 32 bits wide, only stores data and not the
address. Therefore, this FIFO only buffers AHB write burst transactions. This makes it
possible to write to slow memories and free the AHB quickly for other operations. Only
one burst at a time is buffered: if a new AHB burst or single transaction occurs while an
operation is in progress, the FIFO is drained. The FSMC will insert wait states until the
current memory access is complete).
External asynchronous wait control
The FSMC registers that define the external device type and associated characteristics are
usually set at boot time and do not change until the next reset or power-up. However, it is
possible to change the settings at any time.
21.2 Block diagram
The FSMC consists of four main blocks:
The AHB interface (including the FSMC configuration registers)
The NOR Flash/PSRAM controller
The NAND Flash/PC Card controller
The external device interface
The block diagram is shown in Figure 185.
RM0008 Flexible static memory controller (FSMC)
Doc ID 13902 Rev 13 487/1093
Figure 185. FSMC block diagram
21.3 AHB interface
The AHB slave interface enables internal CPUs and other bus master peripherals to access
the external static memories.
AHB transactions are translated into the external device protocol. In particular, if the
selected external memory is 16 or 8 bits wide, 32-bit wide transactions on the AHB are split
into consecutive 16- or 8-bit accesses.
The FSMC generates an AHB error in the following conditions:
When reading or writing to an FSMC bank which is not enabled
When reading or writing to the NOR Flash bank while the FACCEN bit is reset in the
FSMC_BCRx register.
When reading or writing to the PC Card banks while the input pin FSMC_CD (Card
Presence Detection) is low.
A
H
B
b
u
s
FSMC interrupt to NVIC
NOR
HCLK
From clock
controller
controller
memory
NAND/PC Card
controller
memory
Configuration
Registers
signals
NAND
signals
Shared
signals
NOR/PSRAM
FSMC_NE[4:1]
FSMC_NL (or NADV)
FSMC_NWAIT
FSMC_A[25:0]
FSMC_D[15:0]
FSMC_NOE
FSMC_NWE
FSMC_NIORD
FSMC_NREG
FSMC_CD
signals
PC Card
ai14718c
FSMC_NBL[1:0]
FSMC_NCE[3:2]
FSMC_INT[3:2]
FSMC_INTR
FSMC_NCE4_1
FSMC_NCE4_2
FSMC_NIOWR
FSMC_NIOS16
FSMC_CLK
Flexible static memory controller (FSMC) RM0008
488/1093 Doc ID 13902 Rev 13
The effect of this AHB error depends on the AHB master which has attempted the R/W
access:
If it is the Cortex-M3 CPU, a hard fault interrupt is generated
If is a DMA, a DMA transfer error is generated and the corresponding DMA channel is
automatically disabled.
The AHB clock (HCLK) is the reference clock for the FSMC.
21.3.1 Supported memories and transactions
General transaction rules
The requested AHB transaction data size can be 8-, 16- or 32-bit wide whereas the
accessed external device has a fixed data width. This may lead to inconsistent transfers.
Therefore, some simple transaction rules must be followed:
AHB transaction size and memory data size are equal
There is no issue in this case.
AHB transaction size is greater than the memory size
In this case, the FSMC splits the AHB transaction into smaller consecutive memory
accesses in order to meet the external data width.
AHB transaction size is smaller than the memory size
Asynchronous transfers may or not be consistent depending on the type of external
device.
Asynchronous accesses to devices that have the byte select feature (SRAM,
ROM, PSRAM).
In this case, the FSMC allows read/write transactions and accesses the right data
through its byte lanes BL[1:0]
Asynchronous accesses to devices that do not have the byte select feature (NOR
and NAND Flash 16-bit).
This situation occurs when a byte access is requested to a 16-bit wide Flash
memory. Clearly, the device cannot be accessed in byte mode (only 16-bit words
can be read from/written to the Flash memory) therefore:
a) Write transactions are not allowed
b) Read transactions are allowed (the controller reads the entire 16-bit memory word
and uses the needed byte only).
Configuration registers
The FSMC can be configured using a register set. See Section 21.5.6, for a detailed
description of the NOR Flash/PSRAM controller registers. See Section 21.6.8, for a detailed
description of the NAND Flash/PC Card registers.
RM0008 Flexible static memory controller (FSMC)
Doc ID 13902 Rev 13 489/1093
21.4 External device address mapping
From the FSMC point of view, the external memory is divided into 4 fixed-size banks of 256
Mbytes each (Refer to Figure 186):
Bank 1 used to address up to 4 NOR Flash or PSRAM memory devices. This bank is
split into 4 NOR/PSRAM regions with 4 dedicated Chip Select.
Banks 2 and 3 used to address NAND Flash devices (1 device per bank)
Bank 4 used to address a PC Card device
For each bank the type of memory to be used is user-defined in the Configuration register.
Figure 186. FSMC memory banks
21.4.1 NOR/PSRAM address mapping
HADDR[27:26] bits are used to select one of the four memory banks as shown in Table 100.
Bank 1
NAND Flash
NOR / PSRAM
Supported memory type Banks
4 64 MB
6000 0000h
6FFF FFFFh
Address
7000 0000h
7FFF FFFFh
8000 0000h
8FFF FFFFh
9000 0000h
9FFF FFFFh
Bank 2
4 64 MB
Bank 3
4 64 MB
Bank 4
4 64 MB
PC Card
ai14719
Table 100. NOR/PSRAM bank selection
HADDR[27:26]
(1)
1. HADDR are internal AHB address lines that are translated to external memory.
Selected bank
00 Bank 1 NOR/PSRAM 1
01 Bank 1 NOR/PSRAM 2
10 Bank 1 NOR/PSRAM 3
11 Bank 1 NOR/PSRAM 4
Flexible static memory controller (FSMC) RM0008
490/1093 Doc ID 13902 Rev 13
HADDR[25:0] contain the external memory address. Since HADDR is a byte address
whereas the memory is addressed in words, the address actually issued to the memory
varies according to the memory data width, as shown in the following table.
Wrap support for NOR Flash/PSRAM
Wrap burst mode for synchronous memories is not supported. The memories must be
configured in linear burst mode of undefined length.
21.4.2 NAND/PC Card address mapping
In this case, three banks are available, each of them divided into memory spaces as
indicated in Table 102.
For NAND Flash memory, the common and attribute memory spaces are subdivided into
three sections (see in Table 103 below) located in the lower 256 Kbytes:
Data section (first 64 Kbytes in the common/attribute memory space)
Command section (second 64 Kbytes in the common / attribute memory space)
Address section (next 128 Kbytes in the common / attribute memory space)
Table 101. External memory address
Memory width
(1)
1. In case of a 16-bit external memory width, the FSMC will internally use HADDR[25:1] to generate the
address for external memory FSMC_A[24:0].
Whatever the external memory width (16-bit or 8-bit), FSMC_A[0] should be connected to external memory
address A[0].
Data address issued to the memory Maximum memory capacity (bits)
8-bit HADDR[25:0] 64 Mbytes x 8 = 512 Mbit
16-bit HADDR[25:1] >> 1 64 Mbytes/2 x 16 = 512 Mbit
Table 102. Memory mapping and timing registers
Start address End address FSMC Bank Memory space Timing register
0x9C00 0000 0x9FFF FFFF
Bank 4 - PC card
I/O FSMC_PIO4 (0xB0)
0x9800 0000 0x9BFF FFFF Attribute FSMC_PATT4 (0xAC)
0x9000 0000 0x93FF FFFF Common FSMC_PMEM4 (0xA8)
0x8800 0000 0x8BFF FFFF
Bank 3 - NAND Flash
Attribute FSMC_PATT3 (0x8C)
0x8000 0000 0x83FF FFFF Common FSMC_PMEM3 (0x88)
0x7800 0000 0x7BFF FFFF
Bank 2- NAND Flash
Attribute FSMC_PATT2 (0x6C)
0x7000 0000 0x73FF FFFF Common FSMC_PMEM2 (0x68)
Table 103. NAND bank selections
Section name HADDR[17:16] Address range
Address section 1X 0x020000-0x03FFFF
Command section 01 0x010000-0x01FFFF
Data section 00 0x000000-0x0FFFF
RM0008 Flexible static memory controller (FSMC)
Doc ID 13902 Rev 13 491/1093
The application software uses the 3 sections to access the NAND Flash memory:
To send a command to NAND Flash memory: the software must write the command
value to any memory location in the command section.
To specify the NAND Flash address that must be read or written: the software must
write the address value to any memory location in the address section. Since an
address can be 4 or 5 bytes long (depending on the actual memory size), several
consecutive writes to the address section are needed to specify the full address.
To read or write data: the software reads or writes the data value from or to any
memory location in the data section.
Since the NAND Flash memory automatically increments addresses, there is no need to
increment the address of the data section to access consecutive memory locations.
21.5 NOR Flash/PSRAM controller
The FSMC generates the appropriate signal timings to drive the following types of
memories:
Asynchronous SRAM and ROM
8-bit
16-bit
32-bit
PSRAM (Cellular RAM)
Asynchronous mode
Burst mode
NOR Flash
Asynchronous mode or burst mode
Multiplexed or nonmultiplexed
The FSMC outputs a unique chip select signal NE[4:1] per bank. All the other signals
(addresses, data and control) are shared.
For synchronous accesses, the FSMC issues the clock (CLK) to the selected external
device. This clock is a submultiple of the HCLK clock. The size of each bank is fixed and
equal to 64 Mbytes.
Each bank is configured by means of dedicated registers (see Section 21.5.6).
The programmable memory parameters include access timings (see Table 104) and support
for wait management (for PSRAM and NOR Flash accessed in burst mode).
Table 104. Programmable NOR/PSRAM access parameters
Parameter Function Access mode Unit Min. Max.
Address
setup
Duration of the address
setup phase
Asynchronous
AHB clock cycle
(HCLK)
1 16
Address hold
Duration of the address hold
phase
Asynchronous,
muxed I/Os
AHB clock cycle
(HCLK)
2 16
Data setup
Duration of the data setup
phase
Asynchronous
AHB clock cycle
(HCLK)
2 256
Flexible static memory controller (FSMC) RM0008
492/1093 Doc ID 13902 Rev 13
21.5.1 External memory interface signals
Table 105, Table 106 and Table 107 list the signals that are typically used to interface NOR
Flash, SRAM and PSRAM.
Note: Prefix N. specifies the associated signal as active low.
NOR Flash, nonmultiplexed I/Os
NOR Flash memories are addressed in 16-bit words. The maximum capacity is 512 Mbit (26
address lines).
NOR Flash, multiplexed I/Os
Bust turn
Duration of the bus
turnaround phase
Asynchronous and
synchronous read
AHB clock cycle
(HCLK)
1 16
Clock divide
ratio
Number of AHB clock cycles
(HCLK) to build one memory
clock cycle (CLK)
Synchronous
AHB clock cycle
(HCLK)
1 16
Data latency
Number of clock cycles to
issue to the memory before
the first data of the burst
Synchronous
Memory clock
cycle (CLK)
2 17
Table 104. Programmable NOR/PSRAM access parameters (continued)
Parameter Function Access mode Unit Min. Max.
Table 105. Nonmultipled I/O NOR Flash
FSMC signal name I/O Function
CLK O Clock (for synchronous burst)
A[25:0] O Address bus
D[15:0] I/O Bidirectional data bus
NE[x] O Chip select, x = 1..4
NOE O Output enable
NWE O Write enable
NL(=NADV) O
Latch enable (this signal is called address
valid, NADV, by some NOR Flash devices)
NWAIT I NOR Flash wait input signal to the FSMC
Table 106. Multiplexed I/O NOR Flash
FSMC signal name I/O Function
CLK O Clock (for synchronous burst)
A[25:16] O Address bus
AD[15:0] I/O 16-bit multiplexed, bidirectional address/data bus
NE[x] O Chip select, x = 1..4
NOE O Output enable
RM0008 Flexible static memory controller (FSMC)
Doc ID 13902 Rev 13 493/1093
NOR-Flash memories are addressed in 16-bit words. The maximum capacity is 512 Mbit
(26 address lines).
PSRAM/SRAM
PSRAM memories are addressed in 16-bit words. The maximum capacity is 512 Mbit (26
address lines).
NWE O Write enable
NL(=NADV) O
Latch enable (this signal is called address valid, NADV, by some NOR
Flash devices)
NWAIT I NOR Flash wait input signal to the FSMC
Table 107. Nonmultiplexed I/Os PSRAM/SRAM
FSMC signal name I/O Function
CLK O Clock (only for PSRAM synchronous burst)
A[25:0] O Address bus
D[15:0] I/O Data bidirectional bus
NE[x] O Chip select, x = 1..4 (called NCE by PSRAM (Cellular RAM i.e. CRAM))
NOE O Output enable
NWE O Write enable
NL(= NADV) O Address valid only for PSRAM input (memory signal name: NADV)
NWAIT I PSRAM wait input signal to the FSMC
NBL[1] O Upper byte enable (memory signal name: NUB)
NBL[0] O Lowed byte enable (memory signal name: NLB)
Table 106. Multiplexed I/O NOR Flash (continued)
FSMC signal name I/O Function
Flexible static memory controller (FSMC) RM0008
494/1093 Doc ID 13902 Rev 13
21.5.2 Supported memories and transactions
Table 108 below displays an example of the supported devices, access modes and
transactions when the memory data bus is 16-bit for NOR, PSRAM and SRAM.
Transactions not allowed (or not supported) by the FSMC in this example appear in gray.
Table 108. NOR Flash/PSRAM supported memories and transactions
Device Mode R/W
AHB
data
size
Memory
data size
Allowed/
not
allowed
Comments
NOR Flash
(muxed I/Os
and nonmuxed
I/Os)
Asynchronous R 8 16 Y
Asynchronous W 8 16 N
Asynchronous R 16 16 Y
Asynchronous W 16 16 Y
Asynchronous R 32 16 Y
Split into 2 FSMC
accesses
Asynchronous W 32 16 Y
Split into 2 FSMC
accesses
Asynchronous
page
R - 16 N Mode is not supported
Synchronous R 8 16 N
Synchronous R 16 16 Y
Synchronous R 32 16 Y
PSRAM
(multiplexed
I/Os and
nonmultiplexed
I/Os)
Asynchronous R 8 16 Y
Asynchronous W 8 16 Y Use of byte lanes NBL[1:0]
Asynchronous R 16 16 Y
Asynchronous W 16 16 Y
Asynchronous R 32 16 Y
Split into 2 FSMC
accesses
Asynchronous W 32 16 Y
Split into 2 FSMC
accesses
Asynchronous
page
R - 16 N Mode is not supported
Synchronous R 8 16 N
Synchronous R 16 16 Y
Synchronous R 32 16 Y
Synchronous W 8 16 Y Use of byte lanes NBL[1:0]
Synchronous W 16/32 16 Y
RM0008 Flexible static memory controller (FSMC)
Doc ID 13902 Rev 13 495/1093
21.5.3 General timing rules
Signals synchronization
All controller output signals change on the rising edge of the internal clock (HCLK)
In synchronous read and write mode, the output data changes on the falling edge of the
memory clock (FSMC_CLK).
21.5.4 NOR Flash/PSRAM controller asynchronous transactions
Asynchronous static memories (NOR Flash, SRAM)
Signals are synchronized by the internal clock HCLK. This clock is not issued to the
memory
The FSMC always samples the data before de-asserting the chip select signal NE. This
guarantees that the memory data-hold timing constraint is met (chip enable high to
data transition, usually 0 ns min.)
When extended mode is set, it is possible to mix modes A, B, C and D in read and write
(it is for instance possible to read in mode A and write in mode B).
SRAM and
ROM
Asynchronous R 8 / 16 16 Y Use of byte lanes NBL[1:0]
Asynchronous W 8 / 16 16 Y Use of byte lanes NBL[1:0]
Asynchronous R 32 16 Y
Split into 2 FSMC
accesses
Asynchronous W 32 16 Y
Split into 2 FSMC
accesses
Table 108. NOR Flash/PSRAM supported memories and transactions (continued)
Device Mode R/W
AHB
data
size
Memory
data size
Allowed/
not
allowed
Comments
Flexible static memory controller (FSMC) RM0008
496/1093 Doc ID 13902 Rev 13
Mode 1 - SRAM/CRAM
Figure 187. Mode1 read accesses
Figure 188. Mode1 write accesses
A[25:0]
NOE
(ADDSET +1) (DATAST + 1)
Memory transaction
Data strobe
NEx
D[15:0]
HCLK cycles HCLK cycles
NWE
NBL[1:0]
data driven
by memory
ai14720c
High
2 HCLK
cycles
Data sampled
A[25:0]
NOE
(ADDSET +1) (DATAST + 1)
Memory transaction
NEx
D[15:0]
HCLK cycles HCLK cycles
NWE
NBL[1:0]
data driven by FSMC
ai14721c
1HCLK
RM0008 Flexible static memory controller (FSMC)
Doc ID 13902 Rev 13 497/1093
The one HCLK cycle at the end of the write transaction helps guarantee the address and
data hold time after the NWE rising edge. Due to the presence of this one HCLK cycle, the
DATAST value must be greater than zero (DATAST > 0).
Table 109. FSMC_BCRx bit fields
Bit
number
Bit name Value to set
31-16 0x0000
15 ASYNCWAIT Set to 1 if the memory supports this feature. Otherwise keep at 0.
14-10 0x0
9 WAITPOL Meaningful only if bit 15 is 1
8 BURSTEN 0x0
7 -
6 FACCEN -
5-4 MWID As needed
3-2 MTYP As needed, exclude 10 (NOR Flash)
1 MUXEN 0x0
0 MBKEN 0x1
Table 110. FSMC_BTRx bit fields
Bit
number
Bit name Value to set
31-16 0x0000
15-8 DATAST
Duration of the second access phase (DATAST+1 HCLK cycles for
write accesses, DATAST+3 HCLK cycles for read accesses).
This value cannot be 0 (minimum is 1).
7-4 0x0
3-0 ADDSET Duration of the first access phase (ADDSET+1 HCLK cycles).
Flexible static memory controller (FSMC) RM0008
498/1093 Doc ID 13902 Rev 13
Mode A - SRAM/PSRAM (CRAM) OE toggling
Figure 189. ModeA read accesses
Figure 190. ModeA write accesses
The differences compared with mode1 are the toggling of NOE and the independent read
and write timings.
A[25:0]
NOE
(ADDSET +1) (DATAST + 1)
Memory transaction
Data strobe
NEx
D[15:0]
HCLK cycles HCLK cycles
NWE
NBL[1:0]
data driven
by memory
ai14722c
High
2 HCLK
cycles
Data sampled
A[25:0]
NOE
(ADDSET +1) (DATAST + 1)
Memory transaction
NEx
D[15:0]
HCLK cycles HCLK cycles
NWE
NBL[1:0]
data driven by FSMC
ai14721c
1HCLK
RM0008 Flexible static memory controller (FSMC)
Doc ID 13902 Rev 13 499/1093
Table 111. FSMC_BCRx bit fields
Bit
number
Bit name Value to set
31-16 0x0000
15 ASYNCWAIT Set to 1 if the memory supports this feature. Otherwise keep at 0.
14 EXTMOD 0x1
13-10 0x0
9 WAITPOL Meaningful only if bit 15 is 1
8 BURSTEN 0x0
7 -
6 FACCEN -
5-4 MWID As needed
3-2 MTYP As needed, exclude 10 (NOR Flash)
1 MUXEN 0x0
0 MBKEN 0x1
Table 112. FSMC_BTRx bit fields
Bit
number
Bit name Value to set
31-30 0x0
29-28 ACCMOD 0x0
27-16 0x000
15-8 DATAST
Duration of the second access phase (DATAST+3 HCLK cycles) in
read. This value cannot be 0 (minimum is 1)
7-4 0x0
3-0 ADDSET Duration of the first access phase (ADDSET+1 HCLK cycles) in read.
Table 113. FSMC_BWTRx bit fields
Bit
number
Bit name Value to set
31-30 0x0
29-28 ACCMOD 0x0
27-16 0x000
15-8 DATAST
Duration of the second access phase (DATAST+1 HCLK cycles) in
write. This value cannot be 0 (minimum is 1).
7-4 0x0
3-0 ADDSET
Duration of the first access phase (ADDSET+1 HCLK cycles) in write
Flexible static memory controller (FSMC) RM0008
500/1093 Doc ID 13902 Rev 13
Mode 2/B - NOR Flash
Figure 191. Mode2/B read accesses
Figure 192. Mode2 write accesses
A[25:0]
NOE
(ADDSET +1) (DATAST + 1)
Memory transaction
Data strobe
NEx
D[15:0]
HCLK cycles HCLK cycles
NWE
NADV
data driven
by memory
ai14724c
High
2 HCLK
cycles
Data sampled
A[25:0]
NOE
(ADDSET +1) (DATAST + 1)
Memory transaction
NEx
D[15:0]
HCLK cycles HCLK cycles
NWE
NADV
data driven by FSMC
ai14723b
1HCLK
RM0008 Flexible static memory controller (FSMC)
Doc ID 13902 Rev 13 501/1093
Figure 193. ModeB write accesses
The differences with mode1 are the toggling of NADV and the independent read and write
timings when extended mode is set (Mode B).
Table 114. FSMC_BCRx bit fields
Bit
number
Bit name Value to set
31-16 0x0000
15 ASYNCWAIT Set to 1 if the memory supports this feature. Otherwise keep at 0.
14 EXTMOD 0x1 for mode B, 0x0 for mode 2
13-10 0x0
9 WAITPOL Meaningful only if bit 15 is 1
8 BURSTEN 0x0
7 -
6 FACCEN 0x1
5-4 MWID As needed
3-2 MTYP 10 (NOR Flash)
1 MUXEN 0x0
0 MBKEN 0x1
A[25:0]
NOE
(ADDSET +1) (DATAST + 1)
Memory transaction
NEx
D[15:0]
HCLK cycles HCLK cycles
NWE
NADV
data driven by FSMC
ai15110b
1HCLK
Flexible static memory controller (FSMC) RM0008
502/1093 Doc ID 13902 Rev 13
Note: The FSMC_BWTRx register is valid only if extended mode is set (mode B), otherwise all its
content is dont care.
Table 115. FSMC_BTRx bit fields
Bit number Bit name Value to set
31-30 0x0
29-28 ACCMOD 0x1 if extended mode is set
27-16 0x000
15-8 DATAST
Duration of the access second phase (DATAST+3 HCLK cycles) in
read. This value can not be 0 (minimum is 1)
7-4 0x0
3-0 ADDSET
Duration of the access first phase (ADDSET+1 HCLK cycles) in
read.
Table 116. FSMC_BWTRx bit fields
Bit
number
Bit name Value to set
31-30 0x0
29-28 ACCMOD 0x1 if extended mode is set
27-16 0x000
15-8 DATAST
Duration of the access second phase (DATAST+1 HCLK cycles) in
write. This value can not be 0 (minimum is 1).
7-4 0x0
3-0 ADDSET
Duration of the access first phase (ADDSET+1 HCLK cycles) in
write.
RM0008 Flexible static memory controller (FSMC)
Doc ID 13902 Rev 13 503/1093
Mode C - NOR Flash - OE toggling
Figure 194. ModeC read accesses
Figure 195. ModeC write accesses
The differences compared with mode1 are the toggling of NOE and NADV and the
independent read and write timings.
A[25:0]
NOE
(ADDSET +1) (DATAST + 1)
Memory transaction
Data strobe
NEx
D[15:0]
HCLK cycles HCLK cycles
NWE
NADV
data driven
by memory
ai14725c
High
2 HCLK
cycles
Data sampled
A[25:0]
NOE
(ADDSET +1) (DATAST + 1)
Memory transaction
NEx
D[15:0]
HCLK cycles HCLK cycles
NWE
NADV
data driven by FSMC
ai14723b
1HCLK
Flexible static memory controller (FSMC) RM0008
504/1093 Doc ID 13902 Rev 13
Table 117. FSMC_BCRx bit fields
Bit No. Bit name Value to set
31-16 0x0000
15 ASYNCWAIT Set to 1 if the memory supports this feature. Otherwise keep at 0.
14 EXTMOD 0x1
13-10 0x0
9 WAITPOL Meaningful only if bit 15 is 1
8 BURSTEN 0x0
7 -
6 FACCEN 1
5-4 MWID As needed
3-2 MTYP 0x02 (NOR Flash)
1 MUXEN 0x0
0 MBKEN 0x1
Table 118. FSMC_BTRx bit fields
Bit No. Bit name Value to set
31-30 0x0
29-28 ACCMOD 0x2
27-16 0x000
15-8 DATAST
Duration of the second access phase (DATAST+3 HCLK cycles) in
read. This value cannot be 0 (minimum is 1)
7-4 0x0
3-0 ADDSET
Duration of the first access phase (ADDSET+1 HCLK cycles) in
read.
Table 119. FSMC_BWTRx bit fields
Bit No. Bit name Value to set
31-30 0x0
29-28 ACCMOD 0x2
27-16 0x000
15-8 DATAST
Duration of the second access phase (DATAST+1 HCLK cycles) in
write. This value cannot be 0 (minimum is 1)
7-4 0x0
3-0 ADDSET
Duration of the first access phase (ADDSET+1 HCLK cycles) in
write.
RM0008 Flexible static memory controller (FSMC)
Doc ID 13902 Rev 13 505/1093
Mode D - asynchronous access with extended address
Figure 196. ModeD read accesses
ModeD write accessesThe differences with mode1 are the toggling of NADV, NOE that
goes on toggling after NADV changes and the independent read and write timings.
A[25:0]
NOE
(ADDSET +1) (DATAST + 1)
Memory transaction
Data strobe
NEx
D[15:0]
HCLK cycles HCLK cycles
NWE
NADV
data driven
by memory
ai14726c
High
(ADDHLD + 1)
HCLK cycles
2 HCLK
cycles
Data sampled
A[25:0]
NOE
(ADDSET +1) (DATAST + 1)
Memory transaction
NEx
D[15:0]
HCLK cycles HCLK cycles
NWE
NADV
data driven by FSMC
ai14727c
1HCLK
(ADDHLD + 1)
HCLK cycles
Flexible static memory controller (FSMC) RM0008
506/1093 Doc ID 13902 Rev 13
Table 120. FSMC_BCRx bit fields
Bit No. Bit name Value to set
31-16 0x0000
15 ASYNCWAIT Set to 1 if the memory supports this feature. Otherwise keep at 0.
14 EXTMOD 0x1
13-10 0x0
9 WAITPOL Meaningful only if bit 15 is 1
8 BURSTEN 0x0
7 -
6 FACCEN Set according to memory support
5-4 MWID As needed
3-2 MTYP As needed
1 MUXEN 0x0
0 MBKEN 0x1
Table 121. FSMC_BTRx bit fields
Bit No. Bit name Value to set
31-30 0x0
29-28 ACCMOD 0x2
27-16 0x000
15-8 DATAST
Duration of the second access phase (DATAST+3 HCLK cycles) in
read. This value cannot be 0 (minimum is 1)
7-4 ADDHLD
Duration of the middle phase of the read access (ADDHLD+1 HCLK
cycles)
3-0 ADDSET
Duration of the first access phase (ADDSET+1 HCLK cycles) in
read.
Table 122. FSMC_BWTRx bit fields
Bit No. Bit name Value to set
31-30 0x0
29-28 ACCMOD 0x2
27-16 0x000
15-8 DATAST
Duration of the second access phase (DATAST+1 HCLK cycles) in
write. This value cannot be 0 (minimum is 1)
7-4 ADDHLD
Duration of the middle phase of the write access (ADDHLD+1 HCLK
cycles)
3-0 ADDSET Duration of the first access phase (ADDSET+1 HCLK cycles) in write.
RM0008 Flexible static memory controller (FSMC)
Doc ID 13902 Rev 13 507/1093
Mode muxed - asynchronous access muxed NOR Flash
Figure 197. Multiplexed read accesses
1. The bus turnaround delay (BUSTURN + 1) and the delay between side-by-side transactions overlap, so
BUSTURN 5 has not impact.
Figure 198. Multiplexed write accesses
The difference with mode D is the drive of the lower address byte(s) on the databus.
A[25:16]
NOE
(ADDSET +1) (DATAST + 1)
Memory transaction
Data strobe
NEx
AD[15:0]
HCLK cycles HCLK cycles
NWE
NADV
data driven
by memory
ai14728c
High
(ADDHLD + 1)
HCLK cycles
Lower address
(BUSTURN + 1)
(1)
HCLK cycles
2 HCLK
cycles
Data sampled
1HCLK cycle
A[25:16]
NOE
(ADDSET +1) (DATAST + 2)
Memory transaction
NEx
AD[15:0]
HCLK cycles HCLK cycles
NWE
NADV
data driven by FSMC
ai14729c
1HCLK
ADDHLD
HCLK cycles
Lower address
Flexible static memory controller (FSMC) RM0008
508/1093 Doc ID 13902 Rev 13
WAIT management in asynchronous accesses
If the asynchronous memory asserts a WAIT signal to advise that it's not yet ready to accept
or to provide data, the ASYNCWAIT bit has to be set in FSMC_BCRx register.
If the WAIT signal is active (high or low depending on the WAITPOL bit), the second access
phase (Data setup phase) programmed by the DATAST bits, is extended until WAIT
becomes inactive. Unlike the data setup phase, the first access phases (Address setup and
Address hold phases), programmed by the ADDSET and ADDHLD bits, are not WAIT
sensitive and so they are not prolonged.
The data phase must be programmed so that WAIT can be detected 4 HCLK cycles before
the data sampling. The following cases must be considered:
Table 123. FSMC_BCRx bit fields
Bit No. Bit name Value to set
31-16 0x0000
15 ASYNCWAIT Set to 1 if the memory supports this feature. Otherwise keep at 0.
14 EXTMOD 0x0
13-10 0x0
9 WAITPOL Meaningful only if bit 15 is 1
8 BURSTEN 0x0
7 -
6 FACCEN 0x1
5-4 MWID As needed
3-2 MTYP 0x2 (NOR)
1 MUXEN 0x1
0 MBKEN 0x1
Table 124. FSMC_BTRx bit fields
Bit No. Bit name Value to set
31-20 0x0000
19-16 BUSTURN
Duration of the last phase of the access (BUSTURN+1 HCLK)
15-8 DATAST
Duration of the second access phase (DATAST+3 HCLK cycles for
read accesses and DATAST+1 HCLK cycles for write accesses).
This value cannot be 0 (minimum is 1)
7-4 ADDHLD
Duration of the middle phase of the access (ADDHLD+1 HCLK
cycles).This value cannot be 0 (minimum is 1).
3-0 ADDSET Duration of the first access phase (ADDSET+1 HCLK cycles).
RM0008 Flexible static memory controller (FSMC)
Doc ID 13902 Rev 13 509/1093
1. Memory asserts the WAIT signal aligned to NOE/NWE which toggles:
data_setup phase >= 4 * HCLK + max_wait_assertion_time
2. Memory asserts the WAIT signal aligned to NEx (or NOE/NWE not toggling):
if max_wait_assertion_time > (address_phase + hold_phase)
data_setup phase >= 4 * HCLK + (max_wait_assertion_time - address_phase -
hold_phase)
otherwise
data_setup phase >= 4 * HCLK
Where max_wait_assertion_time is the maximum time taken by the memory to assert the
WAIT signal once NEx/NOE/NWE is low.
The Figure 199 and Figure 200 show the number of HCLK clock cycles that memory
access is extended after WAIT is removed by the asynchronous memory (independently of
the above cases).
Figure 199. Asynchronous wait during a read access
A[25:0]
NOE
4HCLK
Memory transaction
NWAT
D[15:0]
NEx
data driven
by memory
ai18471
address phase
don't care
data_setup phase
Flexible static memory controller (FSMC) RM0008
510/1093 Doc ID 13902 Rev 13
Figure 200. Asynchronous wait during a write access
A[25:0]
NWE
Memory transaction
NWAIT
D[15:0]
NEx
data driven by FSMC
ai15797
3HCLK
address phase
dont care
data phase
1HCLK
RM0008 Flexible static memory controller (FSMC)
Doc ID 13902 Rev 13 511/1093
21.5.5 Synchronous burst transactions
The memory clock, CLK, is a submultiple of HCLK according to the value of parameter
CLKDIV.
NOR Flash memories specify a minimum time from NADV assertion to CLK high. To meet
this constraint, the FSMC does not issue the clock to the memory during the first internal
clock cycle of the synchronous access (before NADV assertion). This guarantees that the
rising edge of the memory clock occurs in the middle of the NADV low pulse.
Data latency versus NOR Flash latency
The data latency is the number of cycles to wait before sampling the data. The DATLAT
value must be consistent with the latency value specified in the NOR Flash configuration
register. The FSMC does not include the clock cycle when NADV is low in the data latency
count.
Caution: Some NOR Flash memories include the NADV Low cycle in the data latency count, so the
exact relation between the NOR Flash latency and the FMSC DATLAT parameter can be
either of:
NOR Flash latency = DATLAT + 2
NOR Flash latency = DATLAT + 3
Some recent memories assert NWAIT during the latency phase. In such cases DATLAT can
be set to its minimum value. As a result, the FSMC samples the data and waits long enough
to evaluate if the data are valid. Thus the FSMC detects when the memory exits latency and
real data are taken.
Other memories do not assert NWAIT during latency. In this case the latency must be set
correctly for both the FSMC and the memory, otherwise invalid data are mistaken for good
data, or valid data are lost in the initial phase of the memory access.
Single-burst transfer
When the selected bank is configured in synchronous burst mode, if an AHB single-burst
transaction is requested, the FSMC performs a burst transaction of length 1 (if the AHB
transfer is 16-bit), or length 2 (if the AHB transfer is 32-bit) and de-assert the chip select
signal when the last data is strobed.
Clearly, such a transfer is not the most efficient in terms of cycles (compared to an
asynchronous read). Nevertheless, a random asynchronous access would first require to re-
program the memory access mode, which would altogether last longer.
Wait management
For synchronous burst NOR Flash, NWAIT is evaluated after the programmed latency
period, (DATALAT+2) CLK clock cycles.
If NWAIT is sensed active (low level when WAITPOL = 0, high level when WAITPOL = 1),
wait states are inserted until NWAIT is sensed inactive (high level when WAITPOL = 0, low
level when WAITPOL = 1).
When NWAIT is inactive, the data is considered valid either immediately (bit WAITCFG = 1)
or on the next clock edge (bit WAITCFG = 0).
Flexible static memory controller (FSMC) RM0008
512/1093 Doc ID 13902 Rev 13
During wait-state insertion via the NWAIT signal, the controller continues to send clock
pulses to the memory, keeping the chip select and output enable signals valid, and does not
consider the data valid.
There are two timing configurations for the NOR Flash NWAIT signal in burst mode:
Flash memory asserts the NWAIT signal one data cycle before the wait state (default
after reset)
Flash memory asserts the NWAIT signal during the wait state
These two NOR Flash wait state configurations are supported by the FSMC, individually for
each chip select, thanks to the WAITCFG bit in the FSMC_BCRx registers (x = 0..3).
Figure 201. Wait configurations
Addr[15:0] data data
addr[25:16]
Memory transaction = burst of 4 half words
HCLK
CLK
A[25:16]
NADV
NWAIT
(WAITCFG = 1)
A/D[15:0]
inserted wait state
data
NWAIT
(WAITCFG = 0)
ai15798
RM0008 Flexible static memory controller (FSMC)
Doc ID 13902 Rev 13 513/1093
Figure 202. Synchronous multiplexed read mode - NOR, PSRAM (CRAM)
1. Byte lane outputs BL are not shown; for NOR access, they are held high, and, for PSRAM (CRAM) access, they are held
low.
Addr[15:0] data data
addr[25:16]
Memory transaction = burst of 4 half words
HCLK
CLK
A[25:16]
NEx
NOE
NWE
High
NADV
NWAT
(WATCFG = 1)
A/D[15:0]
1 clock
cycle
1 clock
cycle
(DATALAT + 2)
inserted wait state
Data strobes
ai17723b
CLK cycles
data data
Data strobes
Table 125. FSMC_BCRx bit fields
Bit No. Bit name Value to set
31-20 0x0000
19 CBURSTRW No effect on synchronous read
18-15 0x0
14 EXTMOD 0x0
13 WAITEN
When high, the first data after latency period is taken as always
valid, regardless of the wait from memory value
12 WREN no effect on synchronous read
11 WAITCFG to be set according to memory
10 WRAPMOD no effect
9 WAITPOL to be set according to memory
Flexible static memory controller (FSMC) RM0008
514/1093 Doc ID 13902 Rev 13
8 BURSTEN 0x1
7 FWPRLVL Set to protect memory from accidental write access
6 FACCEN Set according to memory support
5-4 MWID As needed
3-2 MTYP 0x1 or 0x2
1 MUXEN As needed
0 MBKEN 0x1
Table 126. FSMC_BTRx bit fields
Bit No. Bit name Value to set
27-24 DATLAT Data latency
23-20 CLKDIV
0x0 to get CLK = HCLK (not supported)
0x1 to get CLK = 2 HCLK
19-16 BUSTURN no effect
15-8 DATAST no effect
7-4 ADDHLD no effect
3-0 ADDSET no effect
Table 125. FSMC_BCRx bit fields (continued)
Bit No. Bit name Value to set
RM0008 Flexible static memory controller (FSMC)
Doc ID 13902 Rev 13 515/1093
Figure 203. Synchronous multiplexed write mode - PSRAM (CRAM)
1. Memory must issue NWAIT signal one cycle in advance, accordingly WAITCFG must be programmed to 0.
2. Byte Lane (NBL) outputs are not shown, they are held low while NEx is active.
Addr[15:0] data
addr[25:16]
Memory transaction = burst of 2 half words
HCLK
CLK
A[25:16]
NEx
NOE
NWE
Hi-Z
NADV
NWAIT
(WAITCFG = 0)
A/D[15:0]
1 clock
cycle
1 clock
cycle
(DATALAT + 2)
inserted wait state
ai14731d
CLK cycles
data
Flexible static memory controller (FSMC) RM0008
516/1093 Doc ID 13902 Rev 13
Table 127. FSMC_BCRx bit fields
Bit No. Bit name Value to set
31-20 0x0000
19 CBURSTRW 0x1
18-15 0x0
14 EXTMOD 0x0
13 WAITEN
When high, the first data after latency period is taken as always
valid, regardless of the wait from memory value
12 WREN no effect on synchronous read
11 WAITCFG 0x0
10 WRAPMOD no effect
9 WAITPOL to be set according to memory
8 BURSTEN no effect on synchronous write
7 FWPRLVL Set to protect memory from accidental writes
6 FACCEN Set according to memory support
5-4 MWID As needed
3-2 MTYP 0x1
1 MUXEN As needed
0 MBKEN 0x1
Table 128. FSMC_BTRx bit fields
Bit No. Bit name Value to set
31-30 - 0x0
27-24 DATLAT Data latency
23-20 CLKDIV
0 to get CLK = HCLK (not supported)
1 to get CLK = 2 HCLK
19-16 BUSTURN No effect
15-8 DATAST No effect
7-4 ADDHLD No effect
3-0 ADDSET No effect
RM0008 Flexible static memory controller (FSMC)
Doc ID 13902 Rev 13 517/1093
21.5.6 NOR/PSRAM controller registers
The peripheral registers have to be accessed by words (32-bit).
SRAM/NOR-Flash chip-select control registers 1..4 (FSMC_BCR1..4)
Address offset: 0xA000 0000 + 8 * (x 1), x = 1...4
Reset value: 0x0000 30DX
This register contains the control information of each memory bank, used for SRAMs, ROMs
and asynchronous or burst NOR Flash memories.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
C
B
U
R
S
T
R
W
Reserved
A
S
C
Y
C
W
A
I
T
E
X
T
M
O
D
W
A
I
T
E
N
W
R
E
N
W
A
I
T
C
F
G
W
R
A
P
M
O
D
W
A
I
T
P
O
L
B
U
R
S
T
E
N
R
e
s
e
r
v
e
d
F
A
C
C
E
N
M
W
I
D
M
T
Y
P
M
U
X
E
N
M
B
K
E
N
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bit 19 CBURSTRW: Write burst enable.
For Cellular RAM, the bit enables synchronous burst protocol during write operations. For Flash
memory access in burst mode, this bit enables/disables the wait state insertion via the NWAIT
signal. The enable bit for the synchronous burst protocol during read access is the BURSTEN bit in
the FSMC_BCRx register.
0: Write operations are always performed in asynchronous mode
1: Write operations are performed in synchronous mode.
Bit 15 ASYNCWAIT: Wait signal during asynchronous transfers
This bit enables the FSMC to use the wait signal even during an asynchronous protocol.
0: NWAIT signal is not taken in to account when running an asynchronous protocol (default after
reset)
1: NWAIT signal is taken in to account when running an asynchronous protocol
Bit 14 EXTMOD: Extended mode enable.
This bit enables the FSMC to program inside the FSMC_BWTR register, so it allows different
timings for read and write.
0: values inside FSMC_BWTR register are not taken into account (default after reset)
1: values inside FSMC_BWTR register are taken into account
Bit 13 WAITEN: Wait enable bit.
For Flash memory access in burst mode, this bit enables/disables wait-state insertion via the
NWAIT signal:
0: NWAIT signal is disabled (its level not taken into account, no wait state inserted after the
programmed Flash latency period)
1: NWAIT signal is enabled (its level is taken into account after the programmed Flash latency
period to insert wait states if asserted) (default after reset)
Bit 12 WREN: Write enable bit.
This bit indicates whether write operations are enabled/disabled in the bank by the FSMC:
0: Write operations are disabled in the bank by the FSMC, an AHB error is reported,
1: Write operations are enabled for the bank by the FSMC (default after reset).
Flexible static memory controller (FSMC) RM0008
518/1093 Doc ID 13902 Rev 13
Bit 11 WAITCFG: Wait timing configuration.
For memory access in burst mode, the NWAIT signal indicates whether the data from the memory
are valid or if a wait state must be inserted. This configuration bit determines if NWAIT is asserted
by the memory one clock cycle before the wait state or during the wait state:
0: NWAIT signal is active one data cycle before wait state (default after reset),
1: NWAIT signal is active during wait state (not for Cellular RAM).
Bit 10 WRAPMOD: Wrapped burst mode support.
Defines whether the controller will or not split an AHB burst wrap access into two linear accesses.
Valid only when accessing memories in burst mode
0: Direct wrapped burst is not enabled (default after reset),
1: Direct wrapped burst is enabled.
Note: This bit has no effect as the CPU and DMA cannot generate wrapping burst transfers.
Bit 9 WAITPOL: Wait signal polarity bit.
Defines the polarity of the wait signal from memory. Valid only when accessing the memory in burst
mode:
0: NWAIT active low (default after reset),
1: NWAIT active high.
Bit 8 BURSTEN: Burst enable bit.
Enables the burst access mode for the memory. Valid only with synchronous burst memories:
0: Burst access mode disabled (default after reset)
1: Burst access mode enable
Bit 7 Reserved.
Bit 6 FACCEN: Flash access enable
Enables NOR Flash memory access operations.
0: Corresponding NOR Flash memory access is disabled
1: Corresponding NOR Flash memory access is enabled (default after reset)
Bits 5:4 MWID: Memory databus width.
Defines the external memory device width, valid for all type of memories.
00: 8 bits,
01: 16 bits (default after reset),
10: reserved, do not use,
11: reserved, do not use.
Bits 3:2 MTYP: Memory type.
Defines the type of external memory attached to the corresponding memory bank:
00: SRAM, ROM (default after reset for Bank 2...4)
01: PSRAM (Cellular RAM: CRAM)
10: NOR Flash(default after reset for Bank 1)
11: reserved
Bit 1 MUXEN: Address/data multiplexing enable bit.
When this bit is set, the address and data values are multiplexed on the databus, valid only with
NOR and PSRAM memories:
0: Address/Data nonmultiplexed
1: Address/Data multiplexed on databus (default after reset)
Bit 0 MBKEN: Memory bank enable bit.
Enables the memory bank. After reset Bank1 is enabled, all others are disabled. Accessing a
disabled bank causes an ERROR on AHB bus.
0: Corresponding memory bank is disabled
1: Corresponding memory bank is enabled
RM0008 Flexible static memory controller (FSMC)
Doc ID 13902 Rev 13 519/1093
SRAM/NOR-Flash chip-select timing registers 1..4 (FSMC_BTR1..4)
Address offset: 0xA000 0000 + 0x04 + 8 * (x 1), x = 1..4
Reset value: 0x0FFF FFFF
This register contains the control information of each memory bank, used for SRAMs, ROMs
and NOR Flash memories. If the EXTMOD bit is set in the FSMC_BCRx register, then this
register is partitioned for write and read access, that is, 2 registers are available: one to
configure read accesses (this register) and one to configure write accesses (FSMC_BWTRx
registers).
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
R
e
s
e
r
v
e
d
A
C
C
M
O
D
D
A
T
L
A
T
C
L
K
D
I
V
B
U
S
T
U
R
N
D
A
T
A
S
T
A
D
D
H
L
D
A
D
D
S
E
T
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 29:28 ACCMOD: Access mode
Specifies the asynchronous access modes as shown in the timing diagrams. These bits are
taken into account only when the EXTMOD bit in the FSMC_BCRx register is 1.
00: access mode A
01: access mode B
10: access mode C
11: access mode D
Bits 27:24 DATLAT: Data latency for synchronous burst NOR Flash memory
For NOR Flash with synchronous burst mode enabled, defines the number of memory clock
cycles (+2) to issue to the memory before getting the first data:
0000: Data latency of 2 CLK clock cycles for first burst access
1111: Data latency of 17 CLK clock cycles for first burst access (default value after reset)
Note: This timing parameter is not expressed in HCLK periods, but in Flash clock (CLK)
periods. In asynchronous NOR Flash, SRAM or ROM accesses, this value is don't care.
In the case of CRAM, this field must be set to 0.
Bits 23:20 CLKDIV: Clock divide ratio (for CLK signal)
Defines the period of CLK clock output signal, expressed in number of HCLK cycles:
0000: Reserved
0001: CLK period = 2 HCLK periods
0010: CLK period = 3 HCLK periods
1111: CLK period = 16 HCLK periods (default value after reset)
In asynchronous NOR Flash, SRAM or ROM accesses, this value is dont care.
Bits 19:16 BUSTURN: Bus turnaround phase duration
These bits are written by software to insert the bus turnaround delay after a read access only
from multiplexed NOR Flash memory to avoid bus contention if the controller needs to drive
addresses on the databus for the next side-by-side transaction. BUSTURN can be set to the
minimum if the slowest memory does not take more than 6 HCLK clock cycles to put the
databus in Hi-Z state.
0000: BUSTURN phase duration = 1 HCLK clock cycle
...
1111: BUSTURN phase duration = 16 HCLK clock cycles (default value after reset)
Flexible static memory controller (FSMC) RM0008
520/1093 Doc ID 13902 Rev 13
Note: PSRAMs (CRAMs) have a variable latency due to internal refresh. Therefore these
memories issue the NWAIT signal during the whole latency phase to prolong the latency as
needed.
With PSRAMs (CRAMs) the filed DATLAT must be set to 0, so that the FSMC exits its
latency phase soon and starts sampling NWAIT from memory, then starts to read or write
when the memory is ready.
This method can be used also with the latest generation of synchronous Flash memories
that issue the NWAIT signal, unlike older Flash memories (check the datasheet of the
specific Flash memory being used).
SRAM/NOR-Flash write timing registers 1..4 (FSMC_BWTR1..4)
Address offset: 0xA000 0000 + 0x104 + 8 * (x 1), x = 1...4
Bits 15:8 DATAST: Data-phase duration
These bits are written by software to define the duration of the data phase (refer to
Figure 187 to Figure 198), used in SRAMs, ROMs and asynchronous NOR Flash accesses:
0000 0000: Reserved
0000 0001: DATAST phase duration = 2 HCLK clock cycles
0000 0010: DATAST phase duration = 3 HCLK clock cycles
...
1111 1111: DATAST phase duration = 256 HCLK clock cycles (default value after reset)
For each memory type and access mode data-phase duration, please refer to the respective
figure (Figure 187 to Figure 198).
Example: Mode1, read access, DATAST=1: Data-phase duration= DATAST+3 = 4 HCLK
clock cycles.
Note: In synchronous accesses, this value is don't care.
Bits 7:4 ADDHLD: Address-hold phase duration
These bits are written by software to define the duration of the address hold phase (refer to
Figure 196 to Figure 198), used in mode D and multiplexed accesses:
0000: Reserved
0001: ADDHLD phase duration = 2 HCLK clock cycle
0010: ADDHLD phase duration = 3 HCLK clock cycle
...
1111: ADDHLD phase duration = 16 HCLK clock cycles (default value after reset)
For each access mode address-hold phase duration, please refer to the respective figure
(Figure 196 to Figure 198).
Example: ModeD, read access, ADDHLD=1: Address-hold phase duration = ADDHLD + 1 =2
HCLK clock cycles.
Note: In synchronous accesses, this value is not used, the address hold phase is always 1
memory clock period duration.
Bits 3:0 ADDSET: Address setup phase duration
These bits are written by software to define the duration of the address setup phase (refer to
Figure 187 to Figure 198), used in SRAMs, ROMs and asynchronous NOR Flash accesses:
0000: ADDSET phase duration = 1 HCLK clock cycle
...
1111: ADDSET phase duration = 16 HCLK clock cycles (default value after reset)
For each access mode address setup phase duration, please refer to the respective figure
(refer to Figure 187 to Figure 198).
Example: Mode2, read access, ADDSET=1: Address setup phase duration = ADDSET + 1 =
2 HCLK clock cycles.
Note: In synchronous accesses, this value is dont care.
RM0008 Flexible static memory controller (FSMC)
Doc ID 13902 Rev 13 521/1093
Reset value: 0x0FFF FFFF
This register contains the control information of each memory bank, used for SRAMs, ROMs
and NOR Flash memories. When the EXTMOD bit is set in the FSMC_BCRx register, then
this register is active for write access.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res.
ACCM
OD
DATLAT CLKDIV
Reserved
DATAST ADDHLD ADDSET
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 29:28 ACCMOD: Access mode.
Specifies the asynchronous access modes as shown in the next timing diagrams.These bits are
taken into account only when the EXTMOD bit in the FSMC_BCRx register is 1.
00: access mode A
01: access mode B
10: access mode C
11: access mode D
Bits 27:24 DATLAT: Data latency (for synchronous burst NOR Flash).
For NOR Flash with Synchronous burst mode enabled, defines the number of memory clock cycles
(+2) to issue to the memory before getting the first data:
0000: (0x0) Data latency of 2 CLK clock cycles for first burst access
...
1111: (0xF) Data latency of 17 CLK clock cycles for first burst access (default value after reset)
Note: This timing parameter is not expressed in HCLK periods, but in Flash clock (CLK) periods. In
asynchronous NOR Flash, SRAM or ROM accesses, this value is dont care. In case of
CRAM, this field must be set to 0
Bits 23:20 CLKDIV: Clock divide ratio (for CLK signal).
Defines the period of CLK clock output signal, expressed in number of HCLK cycles:
0000: Reserved
0001 CLK period = 2 HCLK periods
0010 CLK period = 3 HCLK periods
1111: CLK period = 16 HCLK periods (default value after reset)
In asynchronous NOR Flash, SRAM or ROM accesses, this value is dont care.
Bits 19:16 Reserved
Bits 15:8 DATAST: Data-phase duration.
These bits are written by software to define the duration of the data phase (refer to Figure 187 to
Figure 198), used in SRAMs, ROMs and asynchronous NOR Flash accesses:
0000 0000: Reserved
0000 0001: DATAST phase duration = 2 HCLK clock cycles
0000 0010: DATAST phase duration = 3 HCLK clock cycles
...
1111 1111: DATAST phase duration = 16 HCLK clock cycles (default value after reset)
Note: In synchronous accesses, this value is don't care.
Flexible static memory controller (FSMC) RM0008
522/1093 Doc ID 13902 Rev 13
21.6 NAND Flash/PC Card controller
The FSMC generates the appropriate signal timings to drive the following types of device:
NAND Flash
8-bit
16-bit
16-bit PC Card compatible devices
The NAND/PC Card controller can control three external banks. Bank 2 and bank 3 support
NAND Flash devices. Bank 4 supports PC Card devices.
Each bank is configured by means of dedicated registers (Section 21.6.8). The
programmable memory parameters include access timings (shown in Table 129) and ECC
configuration.
Bits 7:4 ADDHLD: Address-hold phase duration.
These bits are written by software to define the duration of the address hold phase (refer to
Figure 196 to Figure 198), used in SRAMs, ROMs and asynchronous multiplexed NOR Flash
accesses:
0000: Reserved
0001: ADDHLD phase duration = 2 HCLK clock cycle
0010: ADDHLD phase duration = 3 HCLK clock cycle
...
1111: ADDHLD phase duration = 16 HCLK clock cycles (default value after reset)
Note: In synchronous NOR Flash accesses, this value is not used, the address hold phase is always
1 Flash clock period duration.
Bits 3:0 ADDSET: Address setup phase duration.
These bits are written by software to define the duration of the address setup phase in HCLK
cycles (refer to Figure 196 to Figure 198), used in SRAMs, ROMs and asynchronous NOR Flash
accessed:
0000: ADDSET phase duration = 1 HCLK clock cycle
...
1111: ADDSET phase duration = 16 HCLK clock cycles (default value after reset)
Note: In synchronous NOR Flash accesses, this value is dont care.
RM0008 Flexible static memory controller (FSMC)
Doc ID 13902 Rev 13 523/1093
21.6.1 External memory interface signals
The following tables list the signals that are typically used to interface NAND Flash and PC
Card.
Caution: When using a PC Card or a CompactFlash in I/O mode, the NIOS16 input pin must remain
at ground level during the whole operation, otherwise the FSMC may not operate properly.
This means that the NIOS16 input pin must not be connected to the card, but directly to
ground (only 16-bit accesses are allowed).
Note: Prefix N. specifies the associated signal as active low.
8-bit NAND Flash
t
There is no theoretical capacity limitation as the FSMC can manage as many address
cycles as needed.
Table 129. Programmable NAND/PC Card access parameters
Parameter Function Access mode Unit Min. Max.
Memory setup
time
Number of clock cycles (HCLK)
to set up the address before the
command assertion
Read/Write
AHB clock cycle
(HCLK)
1 256
Memory wait
Minimum duration (HCLK clock
cycles) of the command assertion
Read/Write
AHB clock cycle
(HCLK)
2 256
Memory hold
Number of clock cycles (HCLK)
to hold the address (and the data
in case of a write access) after
the command de-assertion
Read/Write
AHB clock cycle
(HCLK)
1 255
Memory
databus high-Z
Number of clock cycles (HCLK)
during which the databus is kept
in high-Z state after the start of a
write access
Write
AHB clock cycle
(HCLK)
0 255
Table 130. 8-bit NAND Flash
FSMC signal name I/O Function
A[17] O NAND Flash address latch enable (ALE) signal
A[16] O NAND Flash command latch enable (CLE) signal
D[7:0] I/O 8-bit multiplexed, bidirectional address/data bus
NCE[x] O Chip select, x = 2, 3
NOE(= NRE) O Output enable (memory signal name: read enable, NRE)
NWE O Write enable
NWAIT/INT[3:2] I NAND Flash ready/busy input signal to the FSMC
Flexible static memory controller (FSMC) RM0008
524/1093 Doc ID 13902 Rev 13
16-bit NAND Flash
There is no theoretical capacity limitation as the FSMC can manage as many address
cycles as needed.
Table 131. 16-bit NAND Flash
FSMC signal name I/O Function
A[17] O NAND Flash address latch enable (ALE) signal
A[16] O NAND Flash command latch enable (CLE) signal
D[15:0] I/O 16-bit multiplexed, bidirectional address/data bus
NCE[x] O Chip select, x = 2, 3
NOE(= NRE) O Output enable (memory signal name: read enable, NRE)
NWE O Write enable
NWAIT/INT[3:2] I NAND Flash ready/busy input signal to the FSMC
Table 132. 16-bit PC Card
FSMC signal name I/O Function
A[10:0] O Address bus
NIOS16 I
Data transfer in I/O space. It must be shorted to GND (16-bit transfer
only)
NIORD O Output enable for I/O space
NIOWR O Write enable for I/O space
NREG O Register signal indicating if access is in Common or Attribute space
D[15:0] I/O Bidirectional databus
NCE4_1 O Chip select 1
NCE4_2 O Chip select 2 (indicates if access is 16-bit or 8-bit)
NOE O Output enable in Common and in Attribute space
NWE O Write enable in Common and in Attribute space
NWAIT I
PC Card wait input signal to the FSMC (memory signal name
IORDY)
INTR I
PC Card interrupt to the FSMC (only for PC Cards that can generate
an interrupt)
CD I
PC Card presence detection. Active high. If an access is performed
to the PC Card banks while CD is low, an AHB error is generated.
Refer to Section 21.3: AHB interface
RM0008 Flexible static memory controller (FSMC)
Doc ID 13902 Rev 13 525/1093
21.6.2 NAND Flash / PC Card supported memories and transactions
Table 133 below shows the supported devices, access modes and transactions.
Transactions not allowed (or not supported) by the NAND Flash / PC Card controller appear
in gray.
21.6.3 Timing diagrams for NAND and PC Card
Each PC Card/CompactFlash and NAND Flash memory bank is managed through a set of
registers:
Control register: FSMC_PCRx
Interrupt status register: FSMC_SRx
ECC register: FSMC_ECCRx
Timing register for Common memory space: FSMC_PMEMx
Timing register for Attribute memory space: FSMC_PATTx
Timing register for I/O space: FSMC_PIOx
Each timing configuration register contains three parameters used to define number of
HCLK cycles for the three phases of any PC Card/CompactFlash or NAND Flash access,
plus one parameter that defines the timing for starting driving the databus in the case of a
write. Figure 204 shows the timing parameter definitions for common memory accesses,
knowing that Attribute and I/O (only for PC Card) memory space access timings are similar.
Table 133. Supported memories and transactions
Device Mode R/W
AHB
data size
Memory
data size
Allowed/
not allowed
Comments
NAND 8-bit
Asynchronous R 8 8 Y
Asynchronous W 8 8 Y
Asynchronous R 16 8 Y Split into 2 FSMC accesses
Asynchronous W 16 8 Y Split into 2 FSMC accesses
Asynchronous R 32 8 Y Split into 4 FSMC accesses
Asynchronous W 32 8 Y Split into 4 FSMC accesses
NAND 16-bit
Asynchronous R 8 16 Y
Asynchronous W 8 16 N
Asynchronous R 16 16 Y
Asynchronous W 16 16 Y
Asynchronous R 32 16 Y Split into 2 FSMC accesses
Asynchronous W 32 16 Y Split into 2 FSMC accesses
Flexible static memory controller (FSMC) RM0008
526/1093 Doc ID 13902 Rev 13
Figure 204. NAND/PC Card controller timing for common memory access
1. NOE remains high (inactive) during write access. NWE remains high (inactive) during read access.
2. NCEx goes low as soon as NAND access is requested and remains low until a different memory bank is accessed.
21.6.4 NAND Flash operations
The command latch enable (CLE) and address latch enable (ALE) signals of the NAND
Flash device are driven by some address signals of the FSMC controller. This means that to
send a command or an address to the NAND Flash memory, the CPU has to perform a write
to a certain address in its memory space.
A typical page read operation from the NAND Flash device is as follows:
1. Program and enable the corresponding memory bank by configuring the FSMC_PCRx
and FSMC_PMEMx (and for some devices, FSMC_PATTx, see Section 21.6.5: NAND
Flash pre-wait functionality on page 527) registers according to the characteristics of
the NAND Flash (PWID bits for the databus width of the NAND Flash, PTYP = 1,
PWAITEN = 1, PBKEN = 1, see section Common memory space timing register 2..4
(FSMC_PMEM2..4) on page 533 for timing configuration).
2. The CPU performs a byte write in the common memory space, with data byte equal to
one Flash command byte (for example 0x00 for Samsung NAND Flash devices). The
CLE input of the NAND Flash is active during the write strobe (low pulse on NWE), thus
the written byte is interpreted as a command by the NAND Flash. Once the command
is latched by the NAND Flash device, it does not need to be written for the following
page read operations.
3. The CPU can send the start address (STARTAD) for a read operation by writing four
bytes (or three for smaller capacity devices), STARTAD[7:0], then STARTAD[16:9],
STARTAD[24:17] and finally STARTAD[25] for 64 Mb x 8 bit NAND Flash) in the
common memory or attribute space. The ALE input of the NAND Flash device is active
during the write strobe (low pulse on NWE), thus the written bytes are interpreted as
HCLK
Address
NCEx
(2)
NREG,
NIOW,
NIOR
NWE,
NOE
(1)
write_data
read_data
ai14732d
High
Valid
MEMxSET + 1 MEMxWAIT + 1 MEMxHOLD + 1
MEMxHIZ
RM0008 Flexible static memory controller (FSMC)
Doc ID 13902 Rev 13 527/1093
the start address for read operations. Using the attribute memory space makes it
possible to use a different timing configuration of the FSMC, which can be used to
implement the prewait functionality needed by some NAND Flash memories (see
details in Section 21.6.5: NAND Flash pre-wait functionality on page 527).
4. The controller waits for the NAND Flash to be ready (R/NB signal high) to become
active, before starting a new access (to same or another memory bank). While waiting,
the controller maintains the NCE signal active (low).
5. The CPU can then perform byte read operations in the common memory space to read
the NAND Flash page (data field + Spare field) byte by byte.
6. The next NAND Flash page can be read without any CPU command or address write
operation, in three different ways:
by simply performing the operation described in step 5
a new random address can be accessed by restarting the operation at step 3
a new command can be sent to the NAND Flash device by restarting at step 2
21.6.5 NAND Flash pre-wait functionality
Some NAND Flash devices require that, after writing the last part of the address, the
controller wait for the R/NB signal to go low as shown in Figure 205.
Figure 205. Access to non CE dont care NAND-Flash
1. CPU wrote byte 0x00 at address 0x7001 0000.
2. CPU wrote byte A7~A0 at address 0x7002 0000.
3. CPU wrote byte A16~A9 at address 0x7002 0000.
4. CPU wrote byte A24~A17 at address 0x7002 0000.
5. CPU wrote byte A25 at address 0x7802 0000: FSMC performs a write access using FSMC_PATT2 timing
definition, where ATTHOLD 7 (providing that (7+1) HCLK = 112 ns > t
WB
max). This guarantees that
NCE remains low until R/NB goes low and high again (only requested for NAND Flash memories where
NCE is not dont care).
NCE
NOE
I/O[7:0]
R/NB
ai14733
High
tWB
CLE
ALE
0x00 A7-A0 A16-A9 A24-A17 A25
tR
NWE
(1) (2) (3) (4) (5)
NCE must stay low
Flexible static memory controller (FSMC) RM0008
528/1093 Doc ID 13902 Rev 13
When this functionality is needed, it can be guaranteed by programming the MEMHOLD
value to meet the t
WB
timing, however any CPU read or write access to the NAND Flash
then has the hold delay of (MEMHOLD + 1) HCLK cycles inserted from the rising edge of
the NWE signal to the next access.
To overcome this timing constraint, the attribute memory space can be used by
programming its timing register with an ATTHOLD value that meets the t
WB
timing, and
leaving the MEMHOLD value at its minimum. Then, the CPU must use the common memory
space for all NAND Flash read and write accesses, except when writing the last address
byte to the NAND Flash device, where the CPU must write to the attribute memory space.
21.6.6 Error correction code computation ECC (NAND Flash)
The FSMC PC-Card controller includes two error correction code computation hardware
blocks, one per memory bank. They are used to reduce the host CPU workload when
processing the error correction code by software in the system.
These two registers are identical and associated with bank 2 and bank 3, respectively. As a
consequence, no hardware ECC computation is available for memories connected to bank
4.
The error correction code (ECC) algorithm implemented in the FSMC can perform 1-bit error
correction and 2-bit error detection per 256, 512, 1 024, 2 048, 4 096 or 8 192 bytes read
from or written to NAND Flash.
The ECC modules monitor the NAND Flash databus and read/write signals (NCE and NWE)
each time the NAND Flash memory bank is active.
The functional operations are:
When access to NAND Flash is made to bank 2 or bank 3, the data present on the
D[15:0] bus is latched and used for ECC computation.
When access to NAND Flash occurs at any other address, the ECC logic is idle, and
does not perform any operation. Thus, write operations for defining commands or
addresses to NAND Flash are not taken into account for ECC computation.
Once the desired number of bytes has been read from/written to the NAND Flash by the
host CPU, the FSMC_ECCR2/3 registers must be read in order to retrieve the computed
value. Once read, they should be cleared by resetting the ECCEN bit to zero. To compute a
new data block, the ECCEN bit must be set to one in the FSMC_PCR2/3 registers.
21.6.7 PC Card/CompactFlash operations
Address spaces & memory accesses
The FSMC supports Compact Flash storage or PC Cards in Memory Mode and I/O Mode
(True IDE mode is not supported).
The Compact Flash storage and PC Cards are made of 3 memory spaces:
Common Memory Space
Attribute Space
I/O Memory Space
The nCE2 and nCE1 pins (FSMC_NCE4_2 and FSMC_NCE4_1 respectively) select the
card and indicate whether a byte or a word operation is being performed: nCE2 accesses
RM0008 Flexible static memory controller (FSMC)
Doc ID 13902 Rev 13 529/1093
the odd byte on D15-8 and nCE1 accesses the even byte on D7-0 if A0=0 or the odd byte on
D7-0 if A0=1. The full word is accessed on D15-0 if both nCE2 and nCE1 are low.
The memory space is selected by asserting low nOE for read accesses or nWE for write
accesses, combined with the low assertion of nCE2/nCE1 and nREG.
If pin nREG=1 during the memory access, the common memory space is selected
If pin nREG=0 during the memory access, the attribute memory space is selected
The I/O Space is selected by asserting low nIORD for read accesses or nIOWR for write
accesses [instead of nOE/nWE for memory Space], combined with nCE2/nCE1. Note that
nREG must also be asserted low during accesses to I/O Space.
Three type of accesses are allowed for a 16-bit PC Card:
Accesses to Common Memory Space for data storage can be either 8-bit accesses at
even addresses or 16 bit AHB accesses.
Note that 8-bit accesses at odd addresses are not supported and will not lead to the
low assertion of nCE2. A 32-bit AHB request is translated into two 16-bit memory
accesses.
Accesses to Attribute Memory Space where the PC Card stores configuration
information are limited to 8-bit AHB accesses at even addresses.
Note that a 16-bit AHB access will be converted into a single 8-bit memory transfer:
nCE1 will be asserted low, NCE2 will be asserted high and only the even Byte on D7-
D0 will be valid. Instead a 32-bit AHB access will be converted into two 8-bit memory
transfers at even addresses: nCE1 will be asserted low, NCE2 will be asserted high
and only the even bytes will be valid.
Accesses to I/O Space must be limited to AHB 16 bit accesses.
Table 134. 16-bit PC-Card signals and access type
n
C
E
2
n
C
E
1
n
R
E
G
n
O
E
/
n
W
E
n
I
O
R
D
/
n
I
O
W
R
A
1
0
A
9
A
7
-
1
A
0
Space Access Type
Allowed/not
Allowed
1 0 1 0 1 X X X-X X
Common
Memory
Space
Read/Write byte on D7-D0
YES
YES YES
0 1 1 0 1 X X X-X X Read/Write byte on D15-D8 Not supported
0 0 1 0 1 X X X-X 0 Read/Write word on D15-D0 YES
X 0 0 0 1 0 1 X-X 0
Attribute
Space
Read or Write Configuration
Registers
YES
X 0 0 0 1 0 0 X-X 0
Read or Write CIS (Card
Information Structure)
YES
1 0 0 0 1 X X X-X 1 Invalid
Attribute
Space
Read or Write (odd address) YES
0 1 0 0 1 X X X-X x Read or Write (odd address) YES
Flexible static memory controller (FSMC) RM0008
530/1093 Doc ID 13902 Rev 13
The FSMC Bank 4 gives access to those 3 memory spaces as described in Section 21.4.2:
NAND/PC Card address mapping - Table 102: Memory mapping and timing registers
Wait Feature
The CompactFlash Storage or PC Card may request the FSMC to extend the length of the
access phase programmed by MEMWAITx/ATTWAITx/IOWAITx bits, asserting the nWAIT
signal after nOE/nWE or nIORD/nIOWR activation if the wait feature is enabled through the
PWAITEN bit in the FSMC_PCRx register. In order to detect the nWAIT assertion correctly,
the MEMWAITx/ATTWAITx/IOWAITx bits must be programmed as follows:
xxWAITx >= 4 + max_wait_assertion_time/HCLK
Where max_wait_assertion_time is the maximum time taken by NWAIT to go low once
nOE/nWE or nIORD/nIOWR is low.
After the de-assertion of nWAIT, the FSMC extends the WAIT phase for 4 HCLK clock
cycles.
1 0 0 1 0 X X X-X 0
I/O space
Read Even Byte on D7-0 Not supported
1 0 0 1 0 X X X-X 1 Read Odd Byte on D7-0 Not supported
1 0 0 1 0 X X X-X 0 Write Even Byte on D7-0 Not supported
1 0 0 1 0 X X X-X 1 Write Odd Byte on D7-0 Not supported
0 0 0 1 0 X X X-X 0 Read Word on D15-0 YES
0 0 0 1 0 X X X-X 0 Write word on D15-0 YES
0 1 0 1 0 X X X-X X Read Odd Byte on D15-8 Not supported
0 1 0 1 0 X X X-X X Write Odd Byte on D15-8 Not supported
Table 134. 16-bit PC-Card signals and access type (continued)
n
C
E
2
n
C
E
1
n
R
E
G
n
O
E
/
n
W
E
n
I
O
R
D
/
n
I
O
W
R
A
1
0
A
9
A
7
-
1
A
0
Space Access Type
Allowed/not
Allowed
RM0008 Flexible static memory controller (FSMC)
Doc ID 13902 Rev 13 531/1093
21.6.8 NAND Flash/PC Card controller registers
The peripheral registers have to be accessed by words (32-bit).
PC Card/NAND Flash control registers 2..4 (FSMC_PCR2..4)
Address offset: 0xA0000000 + 0x40 + 0x20 * (x 1), x = 2..4
Reset value: 0x0000 0018
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
ECCPS TAR TCLR
Res.
E
C
C
E
N
PWID
P
T
Y
P
P
B
K
E
N
P
W
A
I
T
E
N
R
e
s
e
r
v
e
d
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 19:17 ECCPS: ECC page size.
Defines the page size for the extended ECC:
000: 256 bytes
001: 512 bytes
010: 1024 bytes
011: 2048 bytes
100: 4096 bytes
101: 8192 bytes
Bits 16:13 TAR: ALE to RE delay.
Sets time from ALE low to RE low in number of AHB clock cycles (HCLK).
Time is: t_ar = (TAR + SET + 4) THCLK where THCLK is the HCLK clock period
0000: 1 HCLK cycle (default)
1111: 16 HCLK cycles
Note: SET is MEMSET or ATTSET according to the addressed space.
Bits 12:9 TCLR: CLE to RE delay.
Sets time from CLE low to RE low in number of AHB clock cycles (HCLK).
Time is t_clr = (TCLR + SET + 4) THCLK where THCLK is the HCLK clock period
0000: 1 HCLK cycle (default)
1111: 16 HCLK cycles
Note: SET is MEMSET or ATTSET according to the addressed space.
Bits 8:7 Reserved.
Bits 6 ECCEN: ECC computation logic enable bit
0: ECC logic is disabled and reset (default after reset),
1: ECC logic is enabled.
Bits 5:4 PWID: Databus width.
Defines the external memory device width.
00: 8 bits (default after reset)
01: 16 bits (mandatory for PC Card)
10: reserved, do not use
11: reserved, do not use
Bit 3 PTYP: Memory type.
Defines the type of device attached to the corresponding memory bank:
0: PC Card, CompactFlash, CF+ or PCMCIA
1: NAND Flash (default after reset)
Flexible static memory controller (FSMC) RM0008
532/1093 Doc ID 13902 Rev 13
FIFO status and interrupt register 2..4 (FSMC_SR2..4)
Address offset: 0xA000 0000 + 0x44 + 0x20 * (x-1), x = 2..4
Reset value: 0x0000 0040
This register contains information about FIFO status and interrupt. The FSMC has a FIFO
that is used when writing to memories to store up to16 words of data from the AHB.
This is used to quickly write to the AHB and free it for transactions to peripherals other than
the FSMC, while the FSMC is draining its FIFO into the memory. This register has one of its
bits that indicates the status of the FIFO, for ECC purposes.
The ECC is calculated while the data are written to the memory, so in order to read the
correct ECC the software must wait until the FIFO is empty.
Bit 2 PBKEN: PC Card/NAND Flash memory bank enable bit.
Enables the memory bank. Accessing a disabled memory bank causes an ERROR on AHB
bus
0: Corresponding memory bank is disabled (default after reset)
1: Corresponding memory bank is enabled
Bit 1 PWAITEN: Wait feature enable bit.
Enables the Wait feature for the PC Card/NAND Flash memory bank:
0: disabled
1: enabled
Note: For a PC Card, when the wait feature is enabled, the MEMWAITx/ATTWAITx/IOWAITx
bits must be programmed to a value as follows:
xxWAITx 4 + max_wait_assertion_time/HCLK
Where max_wait_assertion_time is the maximum time taken by NWAIT to go low once
nOE/nWE or nIORD/nIOWR is low.
Bit 0 Reserved.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
R
e
s
e
r
v
e
d
F
E
M
P
T
I
F
E
N
I
L
E
N
I
R
E
N
I
F
S
I
L
S
I
R
S
r rw rw rw rw rw rw
Bit 6 FEMPT: FIFO empty.
Read-only bit that provides the status of the FIFO
0: FIFO not empty
1: FIFO empty
Bit 5 IFEN: Interrupt falling edge detection enable bit
0: Interrupt falling edge detection request disabled
1: Interrupt falling edge detection request enabled
Bit 4 ILEN: Interrupt high-level detection enable bit
0: Interrupt high-level detection request disabled
1: Interrupt high-level detection request enabled
Bit 3 IREN: Interrupt rising edge detection enable bit
0: Interrupt rising edge detection request disabled
1: Interrupt rising edge detection request enabled
RM0008 Flexible static memory controller (FSMC)
Doc ID 13902 Rev 13 533/1093
Common memory space timing register 2..4 (FSMC_PMEM2..4)
Address offset: Address: 0xA000 0000 + 0x48 + 0x20 * (x 1), x = 2..4
Reset value: 0xFCFC FCFC
Each FSMC_PMEMx (x = 2..4) read/write register contains the timing information for PC
Card or NAND Flash memory bank x, used for access to the common memory space of the
16-bit PC Card/CompactFlash, or to access the NAND Flash for command, address write
access and data read/write access.
Bit 2 IFS: Interrupt falling edge status
The flag is set by hardware and reset by software.
0: No interrupt falling edge occurred
1: Interrupt falling edge occurred
Bit 1 ILS: Interrupt high-level status
The flag is set by hardware and reset by software.
0: No Interrupt high-level occurred
1: Interrupt high-level occurred
Bit 0 IRS: Interrupt rising edge status
The flag is set by hardware and reset by software.
0: No interrupt rising edge occurred
1: Interrupt rising edge occurred
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
MEMHIZx MEMHOLDx MEMWAITx MEMSETx
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:24 MEMHIZx: Common memory x databus HiZ time
Defines the number of HCLK (+1 only for NAND) clock cycles during which the databus is
kept in HiZ after the start of a PC Card/NAND Flash write access to common memory space
on socket x. Only valid for write transaction:
0000 0000: (0x00) 0 HCLK cycle (for PC Card)
1111 1111: (0xFF) 255 HCLK cycles (for PC Card) - (default value after reset)
Bits 23:16 MEMHOLDx: Common memory x hold time
Defines the number of HCLK clock cycles to hold address (and data for write access) after
the command deassertion (NWE, NOE), for PC Card/NAND Flash read or write access to
common memory space on socket x:
0000 0000: reserved
0000 0001: 1 HCLK cycle
1111 1111: 255 HCLK cycles (default value after reset)
Bits 15:8 MEMWAITx: Common memory x wait time
Defines the minimum number of HCLK (+1) clock cycles to assert the command (NWE,
NOE), for PC Card/NAND Flash read or write access to common memory space on socket
x. The duration for command assertion is extended if the wait signal (NWAIT) is active (low)
at the end of the programmed value of HCLK:
0000 0000: reserved
0000 0001: 2HCLK cycles (+ wait cycle introduced by deasserting NWAIT)
1111 1111: 256 HCLK cycles (+ wait cycle introduced by the Card deasserting NWAIT)
(default value after reset)
Flexible static memory controller (FSMC) RM0008
534/1093 Doc ID 13902 Rev 13
Attribute memory space timing registers 2..4 (FSMC_PATT2..4)
Address offset: 0xA000 0000 + 0x4C + 0x20 * (x 1), x = 2..4
Reset value: 0xFCFC FCFC
Each FSMC_PATTx (x = 2..4) read/write register contains the timing information for PC
Card/CompactFlash or NAND Flash memory bank x. It is used for 8-bit accesses to the
attribute memory space of the PC Card/CompactFlash or to access the NAND Flash for the
last address write access if the timing must differ from that of previous accesses (for
Ready/Busy management, refer to Section 21.6.5: NAND Flash pre-wait functionality).
Bits 7:0 MEMSETx: Common memory x setup time
Defines the number of HCLK (+1 for PC Card, +2 for NAND) clock cycles to set up the
address before the command assertion (NWE, NOE), for PC Card/NAND Flash read or write
access to common memory space on socket x:
0000 0000: 1 HCLK cycle (for PC Card) / HCLK cycles (for NAND Flash)
1111 1111: 256 HCLK cycles (for PC Card) / 257 HCLK cycles (for NAND Flash) - (default
value after reset)
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
ATTHIZx ATTHOLDx ATTWAITx ATTSETx
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:24 ATTHIZx: Attribute memory x databus HiZ time
Defines the number of HCLK clock cycles during which the databus is kept in HiZ after the
start of a PC CARD/NAND Flash write access to attribute memory space on socket x. Only
valid for write transaction:
0000 0000: 0 HCLK cycle
1111 1111: 255 HCLK cycles (default value after reset)
Bits 23:16 ATTHOLDx: Attribute memory x hold time
Defines the number of HCLK clock cycles to hold address (and data for write access) after
the command deassertion (NWE, NOE), for PC Card/NAND Flash read or write access to
attribute memory space on socket x
0000 0000: reserved
0000 0001: 1 HCLK cycle
1111 1111: 255 HCLK cycles (default value after reset)
Bits 15:8 ATTWAITx: Attribute memory x wait time
Defines the minimum number of HCLK (+1) clock cycles to assert the command (NWE,
NOE), for PC Card/NAND Flash read or write access to attribute memory space on socket x.
The duration for command assertion is extended if the wait signal (NWAIT) is active (low) at
the end of the programmed value of HCLK:
0000 0000: reserved
0000 0001: 2 HCLK cycles (+ wait cycle introduced by deassertion of NWAIT)
1111 1111: 256 HCLK cycles (+ wait cycle introduced by the card deasserting NWAIT)
(default value after reset)
RM0008 Flexible static memory controller (FSMC)
Doc ID 13902 Rev 13 535/1093
I/O space timing register 4 (FSMC_PIO4)
Address offset: 0xA000 0000 + 0xB0
Reset value: 0xFCFCFCFC
The FSMC_PIO4 read/write registers contain the timing information used to gain access to
the I/O space of the 16-bit PC Card/CompactFlash.
Bits 7:0 ATTSETx: Attribute memory x setup time
Defines the number of HCLK (+1) clock cycles to set up address before the command
assertion (NWE, NOE), for PC CARD/NAND Flash read or write access to attribute memory
space on socket x:
0000 0000: 1 HCLK cycle
1111 1111: 256 HCLK cycles (default value after reset)
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
IOHIZx IOHOLDx IOWAITx IOSETx
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:24 IOHIZx: I/O x databus HiZ time
Defines the number of HCLK clock cycles during which the databus is kept in HiZ after the
start of a PC Card write access to I/O space on socket x. Only valid for write transaction:
0000 0000: 0 HCLK cycle
1111 1111: 255 HCLK cycles (default value after reset)
Bits 23:16 IOHOLDx: I/O x hold time
Defines the number of HCLK clock cycles to hold address (and data for write access) after
the command deassertion (NWE, NOE), for PC Card read or write access to I/O space on
socket x:
0000 0000: reserved
0000 0001: 1 HCLK cycle
1111 1111: 255 HCLK cycles (default value after reset)
Bits 15:8 IOWAITx: I/O x wait time
Defines the minimum number of HCLK (+1) clock cycles to assert the command (SMNWE,
SMNOE), for PC Card read or write access to I/O space on socket x. The duration for
command assertion is extended if the wait signal (NWAIT) is active (low) at the end of the
programmed value of HCLK:
0000 0000: reserved, do not use this value
0000 0001: 2 HCLK cycles (+ wait cycle introduced by deassertion of NWAIT)
1111 1111: 256 HCLK cycles (+ wait cycle introduced by the Card deasserting NWAIT)
(default value after reset)
Bits 7:0 IOSETx: I/O x setup time
Defines the number of HCLK (+1) clock cycles to set up the address before the command
assertion (NWE, NOE), for PC Card read or write access to I/O space on socket x:
0000 0000: 1 HCLK cycle
1111 1111: 256 HCLK cycles (default value after reset)
Flexible static memory controller (FSMC) RM0008
536/1093 Doc ID 13902 Rev 13
ECC result registers 2/3 (FSMC_ECCR2/3)
Address offset: 0xA000 0000 + 0x54 + 0x20 * (x 1), x = 2 or 3
Reset value: 0x0000 0000
These registers contain the current error correction code value computed by the ECC
computation modules of the FSMC controller (one module per NAND Flash memory bank).
When the CPU reads the data from a NAND Flash memory page at the correct address
(refer to Section 21.6.6: Error correction code computation ECC (NAND Flash)), the data
read from or written to the NAND Flash are processed automatically by ECC computation
module. At the end of X bytes read (according to the ECCPS field in the FSMC_PCRx
registers), the CPU must read the computed ECC value from the FSMC_ECCx registers,
and then verify whether these computed parity data are the same as the parity value
recorded in the spare area, to determine whether a page is valid, and, to correct it if
applicable. The FSMC_ECCRx registers should be cleared after being read by setting the
ECCEN bit to zero. For computing a new data block, the ECCEN bit must be set to one.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
ECCx
r
Bits 31:0 ECCx: ECC result
This field provides the value computed by the ECC computation logic. Table 135 hereafter
describes the contents of these bit fields.
Table 135. ECC result relevant bits
ECCPS[2:0] Page size in bytes ECC bits
000 256 ECC[21:0]
001 512 ECC[23:0]
010 1024 ECC[25:0]
011 2048 ECC[27:0]
100 4096 ECC[29:0]
101 8192 ECC[31:0]
RM0008 Flexible static memory controller (FSMC)
Doc ID 13902 Rev 13 537/1093
21.6.9 FSMC register map
The following table summarizes the FSMC registers.
Table 136. FSMC register map
Offset Register
3
1
3
0
2
9
2
8
2
7
2
6
2
5
2
4
2
3
2
2
2
1
2
0
1
9
1
8
1
7
1
6
1
5
1
4
1
3
1
2
1
1
1
0
9 8 7 6 5 4 3 2 1 0
0xA000
0000
FSMC_BCR1 Reserved
C
B
U
R
S
T
R
W
Reserved
A
S
Y
N
C
W
A
I
T
E
X
T
M
O
D
W
A
I
T
E
N
W
R
E
N
W
A
I
T
C
F
G
W
A
I
T
P
O
L
B
U
R
S
T
E
N
R
e
s
e
r
v
e
d
F
A
C
C
E
N
M
W
I
D
M
T
Y
P
M
U
X
E
N
M
B
K
E
N
Reset value
0xA000
0008
FSMC_BCR2 Reserved
C
B
U
R
S
T
R
W
Reserved
A
S
Y
N
C
W
A
I
T
E
X
T
M
O
D
W
A
I
T
E
N
W
R
E
N
W
A
I
T
C
F
G
W
R
A
P
M
O
D
W
A
I
T
P
O
L
B
U
R
S
T
E
N
R
e
s
e
r
v
e
d
F
A
C
C
E
N
M
W
I
D
M
T
Y
P
M
U
X
E
N
M
B
K
E
N
0xA000
0010
FSMC_BCR3 Reserved
C
B
U
R
S
T
R
W
Reserved
A
S
Y
N
C
W
A
I
T
E
X
T
M
O
D
W
A
I
T
E
N
W
R
E
N
W
A
I
T
C
F
G
W
R
A
P
M
O
D
W
A
I
T
P
O
L
B
U
R
S
T
E
N
R
e
s
e
r
v
e
d
F
A
C
C
E
N
M
W
I
D
M
T
Y
P
M
U
X
E
N
M
B
K
E
N
0xA000
0018
FSMC_BCR4 Reserved
C
B
U
R
S
T
R
W
Reserved
A
S
Y
N
C
W
A
I
T
E
X
T
M
O
D
W
A
I
T
E
N
W
R
E
N
W
A
I
T
C
F
G
W
R
A
P
M
O
D
W
A
I
T
P
O
L
B
U
R
S
T
E
N
R
e
s
e
r
v
e
d
F
A
C
C
E
N
M
W
I
D
M
T
Y
P
M
U
X
E
N
M
B
K
E
N
0xA000
0004
FSMC_BTR1 Res.
ACCM
OD
DATLAT CLKDIV BUSTURN DATAST ADDHLD ADDSET
0xA000
000C
FSMC_BTR2 Res.
ACCM
OD
DATLAT CLKDIV BUSTURN DATAST ADDHLD ADDSET
0xA000
0014
FSMC_BTR3 Res.
ACCM
OD
DATLAT CLKDIV BUSTURN DATAST ADDHLD ADDSET
0xA000
001C
FSMC_BTR4 Res.
ACCM
OD
DATLAT CLKDIV BUSTURN DATAST ADDHLD ADDSET
0xA000
0104
FSMC_BWTR1 Res.
ACCM
OD
DATLAT CLKDIV Reserved DATAST ADDHLD ADDSET
0xA000
010C
FSMC_BWTR2 Res.
ACCM
OD
DATLAT CLKDIV Reserved DATAST ADDHLD ADDSET
0xA000
0114
FSMC_BWTR3 Res.
ACCM
OD
DATLAT CLKDIV Reserved DATAST ADDHLD ADDSET
0xA000
011C
FSMC_BWTR4 Res.
ACCM
OD
DATLAT CLKDIV Reserved DATAST ADDHLD ADDSET
0xA000
0060
FSMC_PCR2 Reserved ECCPS TAR TCLR Res.
E
C
C
E
N
PWID
P
T
Y
P
P
B
K
E
N
P
W
A
I
T
E
N
R
e
s
e
r
v
e
d
0xA000
0080
FSMC_PCR3 Reserved ECCPS TAR TCLR Res.
E
C
C
E
N
PWID
P
T
Y
P
P
B
K
E
N
P
W
A
I
T
E
N
R
e
s
e
r
v
e
d
0xA000
00A0
FSMC_PCR4 Reserved ECCPS TAR TCLR Res.
E
C
C
E
N
PWID
P
T
Y
P
P
B
K
E
N
P
W
A
I
T
E
N
R
e
s
e
r
v
e
d
0xA000
0064
FSMC_SR2 Reserved
F
E
M
P
T
I
F
E
N
I
L
E
N
I
R
E
N
I
F
S
I
L
S
I
R
S
0xA000
0084
FSMC_SR3 Reserved
F
E
M
P
T
I
F
E
N
I
L
E
N
I
R
E
N
I
F
S
I
L
S
I
R
S
0xA000
00A4
FSMC_SR4 Reserved
F
E
M
P
T
I
F
E
N
I
L
E
N
I
R
E
N
I
F
S
I
L
S
I
R
S
0xA000
0068
FSMC_PMEM2 MEMHIZx MEMHOLDx MEMWAITx MEMSETx
0xA000
0088
FSMC_PMEM3 MEMHIZx MEMHOLDx MEMWAITx MEMSETx
Flexible static memory controller (FSMC) RM0008
538/1093 Doc ID 13902 Rev 13
Refer to Table 3 on page 50 for the register boundary addresses.
0xA000
00A8
FSMC_PMEM4 MEMHIZx MEMHOLDx MEMWAITx MEMSETx
0xA000
006C
FSMC_PATT2 ATTHIZx ATTHOLDx ATTWAITx ATTSETx
0xA000
008C
FSMC_PATT3 ATTHIZx ATTHOLDx ATTWAITx ATTSETx
0xA000
00AC
FSMC_PATT4 ATTHIZx ATTHOLDx ATTWAITx ATTSETx
0xA000
00B0
FSMC_PIO4 IOHIZx IOHOLDx IOWAITx IOSETx
0xA000
0074
FSMC_ECCR2 ECCx
0xA000
0094
FSMC_ECCR3 ECCx
Table 136. FSMC register map (continued)
Offset Register
3
1
3
0
2
9
2
8
2
7
2
6
2
5
2
4
2
3
2
2
2
1
2
0
1
9
1
8
1
7
1
6
1
5
1
4
1
3
1
2
1
1
1
0
9 8 7 6 5 4 3 2 1 0
RM0008 Secure digital input/output interface (SDIO)
Doc ID 13902 Rev 13 539/1093
22 Secure digital input/output interface (SDIO)
Low-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 16 and 32 Kbytes.
Medium-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 64 and 128 Kbytes.
High-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 256 and 512 Kbytes.
XL-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 768 Kbytes and 1 Mbyte.
Connectivity line devices are STM32F105xx and STM32F107xx microcontrollers.
This section applies to high-density and XL-density performance line devices only.
22.1 SDIO main features
The SD/SDIO MMC card host interface (SDIO) provides an interface between the AHB
peripheral bus and MultiMediaCards (MMCs), SD memory cards, SDIO cards and CE-ATA
devices.
The MultiMediaCard system specifications are available through the MultiMediaCard
Association website at www.mmca.org, published by the MMCA technical committee.
SD memory card and SD I/O card system specifications are available through the SD card
Association website at www.sdcard.org.
CE-ATA system specifications are available through the CE-ATA workgroup website at
www.ce-ata.org.
The SDIO features include the following:
Full compliance with MultiMediaCard System Specification Version 4.2. Card support
for three different databus modes: 1-bit (default), 4-bit and 8-bit
Full compatibility with previous versions of MultiMediaCards (forward compatibility)
Full compliance with SD Memory Card Specifications Version 2.0
Full compliance with SD I/O Card Specification Version 2.0: card support for two
different databus modes: 1-bit (default) and 4-bit
Full support of the CE-ATA features (full compliance with CE-ATA digital protocol
Rev1.1)
Data transfer up to 48 MHz for the 8 bit mode
Data and command output enable signals to control external bidirectional drivers.
Note: 1 The SDIO does not have an SPI-compatible communication mode.
2 The SD memory card protocol is a superset of the MultiMediaCard protocol as defined in the
MultiMediaCard system specification V2.11. Several commands required for SD memory
devices are not supported by either SD I/O-only cards or the I/O portion of combo cards.
Some of these commands have no use in SD I/O devices, such as erase commands, and
thus are not supported in the SDIO. In addition, several commands are different between SD
memory cards and SD I/O cards and thus are not supported in the SDIO. For details refer to
SD I/O card Specification Version 1.0. CE-ATA is supported over the MMC electrical
Secure digital input/output interface (SDIO) RM0008
540/1093 Doc ID 13902 Rev 13
interface using a protocol that utilizes the existing MMC access primitives. The interface
electrical and signaling definition is as defined in the MMC reference.
The MultiMediaCard/SD bus connects cards to the controller.
The current version of the SDIO supports only one SD/SDIO/MMC4.2 card at any one time
and a stack of MMC4.1 or previous.
22.2 SDIO bus topology
Communication over the bus is based on command and data transfers.
The basic transaction on the MultiMediaCard/SD/SD I/O bus is the command/response
transaction. These types of bus transaction transfer their information directly within the
command or response structure. In addition, some operations have a data token.
Data transfers to/from SD/SDIO memory cards are done in data blocks. Data transfers
to/from MMC are done data blocks or streams. Data transfers to/from the CE-ATA Devices
are done in data blocks.
Figure 206. SDIO no response and no data operations
Figure 207. SDIO (multiple) block read operation
Operation (no response) Operation (no data)
SDIO_CMD
SDIO_D
From host to card(s) From host to card From card to host
Response Command Command
ai14734
ai14735
Command Response
Data block crc Data block crc Data block crc
Block read operation
Multiple block read operation
Data stop operation
From host to card From card to host
data from card to host
Stop command
stops data transfer
Command Response SDIO_CMD
SDIO_D
RM0008 Secure digital input/output interface (SDIO)
Doc ID 13902 Rev 13 541/1093
Figure 208. SDIO (multiple) block write operation
Note: The SDIO will not send any data as long as the Busy signal is asserted (SDIO_D0 pulled
low).
Figure 209. SDIO sequential read operation
Figure 210. SDIO sequential write operation
ai14737
Block write operation
Data stop operation
Multiple block write operation
From host to card From card to host
Data from host to card
Stop command
stops data transfer
Optional cards Busy.
Needed for CE-ATA
Command Response Command Response
Data block crc Busy Busy Data block crc Busy
SDIO_CMD
SDIO_D
ai14738
Data stop operation
From card to host
Stop command
stops data transfer
Command Response Command Response
Data transfer operation
Data stream
From host to
card(s)
Data from card to host
SDIO_CMD
SDIO_D
ai14739
Data stop operation
From card to host
Stop command
stops data transfer
Command Response Command Response
Data transfer operation
Data stream
From host to
card(s)
Data from host to card
SDIO_CMD
SDIO_D
Secure digital input/output interface (SDIO) RM0008
542/1093 Doc ID 13902 Rev 13
22.3 SDIO functional description
The SDIO consists of two parts:
The SDIO adapter block provides all functions specific to the MMC/SD/SD I/O card
such as the clock generation unit, command and data transfer.
The AHB interface accesses the SDIO adapter registers, and generates interrupt and
DMA request signals.
Figure 211. SDIO block diagram
By default SDIO_D0 is used for data transfer. After initialization, the host can change the
databus width.
If a MultiMediaCard is connected to the bus, SDIO_D0, SDIO_D[3:0] or SDIO_D[7:0] can be
used for data transfer. MMC V3.31 or previous, supports only 1 bit of data so only SDIO_D0
can be used.
If an SD or SD I/O card is connected to the bus, data transfer can be configured by the host
to use SDIO_D0 or SDIO_D[3:0]. All data lines are operating in push-pull mode.
SDIO_CMD has two operational modes:
Open-drain for initialization (only for MMCV3.31 or previous)
Push-pull for command transfer (SD/SD I/O card MMC4.2 use push-pull drivers also for
initialization)
SDIO_CK is the clock to the card: one bit is transferred on both command and data lines
with each clock cycle. The clock frequency can vary between 0 MHz and 20 MHz (for a
MultiMediaCard V3.31), between 0 and 48 MHz for a MultiMediaCard V4.0/4.2, or between
0 and 25 MHz (for an SD/SD I/O card).
The SDIO uses two clock signals:
SDIO adapter clock (SDIOCLK = HCLK)
AHB bus clock (HCLK/2)
PCLK2 and SDIO_CK clock frequencies must respect the following condition:
The signals shown in Table 137 are used on the MultiMediaCard/SD/SD I/O card bus.
AHB bus
AHB
Interrupts and
HCLK/2
SDIO_CK
adapter interface
DMA request
SDIOCLK
SDIO
SDIO
SDIO_D[7:0]
SDIO_CMD
ai14740
Frequenc PCLK2 ( ) 3 8 Frequency SDIO_CK ( ) =
RM0008 Secure digital input/output interface (SDIO)
Doc ID 13902 Rev 13 543/1093
22.3.1 SDIO adapter
Figure 212 shows a simplified block diagram of an SDIO adapter.
Figure 212. SDIO adapter
The SDIO adapter is a multimedia/secure digital memory card bus master that provides an
interface to a multimedia card stack or to a secure digital memory card. It consists of five
subunits:
Adapter register block
Control unit
Command path
Data path
Data FIFO
Note: The adapter registers and FIFO use the AHB bus clock domain (HCLK/2). The control unit,
command path and data path use the SDIO adapter clock domain (SDIOCLK).
Adapter register block
The adapter register block contains all system registers. This block also generates the
signals that clear the static flags in the multimedia card. The clear signals are generated
when 1 is written into the corresponding bit location in the SDIO Clear register.
Table 137. SDIO I/O definitions
Pin Direction Description
SDIO_CK Output
MultiMediaCard/SD/SDIO card clock. This pin is the clock from
host to card.
SDIO_CMD Bidirectional
MultiMediaCard/SD/SDIO card command. This pin is the
bidirectional command/response signal.
SDIO_D[7:0] Bidirectional
MultiMediaCard/SD/SDIO card data. These pins are the
bidirectional databus.
To AHB
interface
ai14740
Control unit
Command
path
Data path
Adapter
registers
FIFO
SDIO_CK
SDIO_CMD
SDIO_D[7:0]
HCLK/2 SDIOCLK
C
a
r
d
b
u
s
SDIO adapter
Secure digital input/output interface (SDIO) RM0008
544/1093 Doc ID 13902 Rev 13
Control unit
The control unit contains the power management functions and the clock divider for the
memory card clock.
There are three power phases:
power-off
power-up
power-on
Figure 213. Control unit
The control unit is illustrated in Figure 213. It consists of a power management subunit and
a clock management subunit.
The power management subunit disables the card bus output signals during the power-off
and power-up phases.
The clock management subunit generates and controls the SDIO_CK signal. The SDIO_CK
output can use either the clock divide or the clock bypass mode. The clock output is inactive:
after reset
during the power-off or power-up phases
if the power saving mode is enabled and the card bus is in the Idle state (eight clock
periods after both the command and data path subunits enter the Idle phase)
Command path
The command path unit sends commands to and receives responses from the cards.
ai14804
Power management
Clock
management
Adapter
registers
SDIO_CK
Control unit
To command and data path
RM0008 Secure digital input/output interface (SDIO)
Doc ID 13902 Rev 13 545/1093
Figure 214. SDIO adapter command path
Command path state machine (CPSM)
When the command register is written to and the enable bit is set, command
transfer starts. When the command has been sent, the command path state
machine (CPSM) sets the status flags and enters the Idle state if a response is not
required. If a response is required, it waits for the response (see Figure 215 on
page 546). When the response is received, the received CRC code and the
internally generated code are compared, and the appropriate status flags are set.
ai14805
CMD
Status
flag
Control
logic
Command
timer
CRC
Argument
Shift
register CMD
Response
registers
To control unit
SDIO_CMDin
SDIO_CMDout
To AHB interface
Adapter registers
Secure digital input/output interface (SDIO) RM0008
546/1093 Doc ID 13902 Rev 13
Figure 215. Command path state machine (CPSM)
When the Wait state is entered, the command timer starts running. If the timeout is reached
before the CPSM moves to the Receive state, the timeout flag is set and the Idle state is
entered.
Note: The command timeout has a fixed value of 64 SDIO_CK clock periods.
If the interrupt bit is set in the command register, the timer is disabled and the CPSM waits
for an interrupt request from one of the cards. If a pending bit is set in the command register,
the CPSM enters the Pend state, and waits for a CmdPend signal from the data path
subunit. When CmdPend is detected, the CPSM moves to the Send state. This enables the
data counter to trigger the stop command transmission.
Note: The CPSM remains in the Idle state for at least eight SDIO_CK periods to meet the N
CC
and
N
RC
timing constraints. N
CC
is the minimum delay between two host commands, and N
RC
is
the minimum delay between the host command and the card response.
Idle
Pend
Send
Wait
Receive
Last Data
CPSM
disabled
Enabled and
command start
CPSM disabled or
no response
Wait for response
Response
started
Response received or
disabled or command
CRC failed
CPSM Disabled or
command timeout
CPSM Enabled and
pending command
ai14806b
Wait_CPL
Response Received in CE-ATA
mode and no interrupt and
wait for CE-ATA Command
Completion signal enabled
Response Received in CE-ATA mode and
no interrupt and wait for CE-ATA
Command Completion signal disabled
CE-ATA Command
Completion signal
received or
CPSM disabled or
Command CRC failed
On reset
RM0008 Secure digital input/output interface (SDIO)
Doc ID 13902 Rev 13 547/1093
Figure 216. SDIO command transfer
Command format
Command: a command is a token that starts an operation. Command are sent
from the host either to a single card (addressed command) or to all connected
cards (broadcast command are available for MMC V3.31 or previous). Commands
are transferred serially on the CMD line. All commands have a fixed length of 48
bits. The general format for a command token for MultiMediaCards, SD-Memory
cards and SDIO-Cards is shown in Table 138. CE-ATA commands are an
extension of MMC commands V4.2, and so have the same format.
The command path operates in a half-duplex mode, so that commands and
responses can either be sent or received. If the CPSM is not in the Send state, the
SDIO_CMD output is in the Hi-Z state, as shown in Figure 216 on page 547. Data
on SDIO_CMD are synchronous with the rising edge of SDIO_CK. Table shows
the command format.
Response: a response is a token that is sent from an addressed card (or
synchronously from all connected cards for MMC V3.31 or previous), to the host
as an answer to a previously received command. Responses are transferred
serially on the CMD line.
The SDIO supports two response types. Both use CRC error checking:
48 bit short response
136 bit long response
Note: If the response does not contain a CRC (CMD1 response), the device driver must ignore the
CRC failed status.
Table 138. Command format
Bit position Width Value Description
47 1 0 Start bit
46 1 1 Transmission bit
[45:40] 6 - Command index
[39:8] 32 - Argument
[7:1] 7 - CRC7
0 1 1 End bit
SDIO_CK
SDIO_CMD
Command Response Command
State Idle Send Wait Receive Idle Send
Hi-Z Controller drives Hi-Z Card drives Hi-Z Controller drives
ai14707
at least 8 SDIO_CK cycles
Secure digital input/output interface (SDIO) RM0008
548/1093 Doc ID 13902 Rev 13
The command register contains the command index (six bits sent to a card) and the
command type. These determine whether the command requires a response, and whether
the response is 48 or 136 bits long (see Section 22.9.4 on page 582). The command path
implements the status flags shown in Table 141:
The CRC generator calculates the CRC checksum for all bits before the CRC code. This
includes the start bit, transmitter bit, command index, and command argument (or card
status). The CRC checksum is calculated for the first 120 bits of CID or CSD for the long
response format. Note that the start bit, transmitter bit and the six reserved bits are not used
in the CRC calculation.
The CRC checksum is a 7-bit value:
CRC[6:0] = Remainder [(M(x) * x
7
) / G(x)]
G(x) = x
7
+ x
3
+ 1
M(x) = (start bit) * x
39
+ ... + (last bit before CRC) * x
0
, or
M(x) = (start bit) * x
119
+ ... + (last bit before CRC) * x
0
Table 139. Short response format
Bit position Width Value Description
47 1 0 Start bit
46 1 0 Transmission bit
[45:40] 6 - Command index
[39:8] 32 - Argument
[7:1] 7 - CRC7(or 1111111)
0 1 1 End bit
Table 140. Long response format
Bit position Width Value Description
135 1 0 Start bit
134 1 0 Transmission bit
[133:128] 6 111111 Reserved
[127:1] 127 - CID or CSD (including internal CRC7)
0 1 1 End bit
Table 141. Command path status flags
Flag Description
CMDREND Set if response CRC is OK.
CCRCFAIL Set if response CRC fails.
CMDSENT Set when command (that does not require response) is sent
CTIMEOUT Response timeout.
CMDACT Command transfer in progress.
RM0008 Secure digital input/output interface (SDIO)
Doc ID 13902 Rev 13 549/1093
Data path
The data path subunit transfers data to and from cards. Figure 217 shows a block diagram
of the data path.
Figure 217. Data path
The card databus width can be programmed using the clock control register. If the 4-bit wide
bus mode is enabled, data is transferred at four bits per clock cycle over all four data signals
(SDIO_D[3:0]). If the 8-bit wide bus mode is enabled, data is transferred at eight bits per
clock cycle over all eight data signals (SDIO_D[7:0]). If the wide bus mode is not enabled,
only one bit per clock cycle is transferred over SDIO_D0.
Depending on the transfer direction (send or receive), the data path state machine (DPSM)
moves to the Wait_S or Wait_R state when it is enabled:
Send: the DPSM moves to the Wait_S state. If there is data in the transmit FIFO, the
DPSM moves to the Send state, and the data path subunit starts sending data to a
card.
Receive: the DPSM moves to the Wait_R state and waits for a start bit. When it
receives a start bit, the DPSM moves to the Receive state, and the data path subunit
starts receiving data from a card.
Data path state machine (DPSM)
The DPSM operates at SDIO_CK frequency. Data on the card bus signals is synchronous to
the rising edge of SDIO_CK. The DPSM has six states, as shown in Figure 218: Data path
state machine (DPSM).
ai14808
Transmit
Status
flag
Control
logic
Data
timer
CRC
Receive
Shift
register
To control unit
SDIO_Din[7:0]
SDIO_Dout[7:0]
Data FIFO
Data path
Secure digital input/output interface (SDIO) RM0008
550/1093 Doc ID 13902 Rev 13
Figure 218. Data path state machine (DPSM)
Idle: the data path is inactive, and the SDIO_D[7:0] outputs are in Hi-Z. When the data
control register is written and the enable bit is set, the DPSM loads the data counter
with a new value and, depending on the data direction bit, moves to either the Wait_S
or the Wait_R state.
Wait_R: if the data counter equals zero, the DPSM moves to the Idle state when the
receive FIFO is empty. If the data counter is not zero, the DPSM waits for a start bit on
SDIO_D. The DPSM moves to the Receive state if it receives a start bit before a
timeout, and loads the data block counter. If it reaches a timeout before it detects a
start bit, or a start bit error occurs, it moves to the Idle state and sets the timeout status
flag.
Receive: serial data received from a card is packed in bytes and written to the data
FIFO. Depending on the transfer mode bit in the data control register, the data transfer
mode can be either block or stream:
In block mode, when the data block counter reaches zero, the DPSM waits until it
receives the CRC code. If the received code matches the internally generated
CRC code, the DPSM moves to the Wait_R state. If not, the CRC fail status flag is
set and the DPSM moves to the Idle state.
In stream mode, the DPSM receives data while the data counter is not zero. When
the counter is zero, the remaining data in the shift register is written to the data
FIFO, and the DPSM moves to the Wait_R state.
If a FIFO overrun error occurs, the DPSM sets the FIFO error flag and moves to the Idle
state:
Wait_S: the DPSM moves to the Idle state if the data counter is zero. If not, it waits until
the data FIFO empty flag is deasserted, and moves to the Send state.
Idle
Busy
Send
Wait_R
Receive
End of packet
Disabled or CRC fail
or timeout
Not busy
Disabled or
end of data
Data ready
End of packet or
end of data or
FIFO overrun
Enable and not send
Disabled or
Rx FIFO empty or timeout or
start bit error
Disabled or FIFO underrun or
end of data or CRC fail
ai14809b
Wait_S
Start bit
On reset
Disabled or CRC fail
Enable and send
DPSM disabled
Read Wait DPSM enabled and
Read Wait Started
and SD I/O mode enabled
ReadWait Stop
Data received and
Read Wait Started and
SD I/O mode enabled
RM0008 Secure digital input/output interface (SDIO)
Doc ID 13902 Rev 13 551/1093
Note: The DPSM remains in the Wait_S state for at least two clock periods to meet the N
WR
timing
requirements, where N
WR
is the number of clock cycles between the reception of the card
response and the start of the data transfer from the host.
Send: the DPSM starts sending data to a card. Depending on the transfer mode bit in
the data control register, the data transfer mode can be either block or stream:
In block mode, when the data block counter reaches zero, the DPSM sends an
internally generated CRC code and end bit, and moves to the Busy state.
In stream mode, the DPSM sends data to a card while the enable bit is high and
the data counter is not zero. It then moves to the Idle state.
If a FIFO underrun error occurs, the DPSM sets the FIFO error flag and moves to the
Idle state.
Busy: the DPSM waits for the CRC status flag:
If it does not receive a positive CRC status, it moves to the Idle state and sets the
CRC fail status flag.
If it receives a positive CRC status, it moves to the Wait_S state if SDIO_D0 is not
low (the card is not busy).
If a timeout occurs while the DPSM is in the Busy state, it sets the data timeout flag and
moves to the Idle state.
The data timer is enabled when the DPSM is in the Wait_R or Busy state, and
generates the data timeout error:
When transmitting data, the timeout occurs if the DPSM stays in the Busy state for
longer than the programmed timeout period
When receiving data, the timeout occurs if the end of the data is not true, and if the
DPSM stays in the Wait_R state for longer than the programmed timeout period.
Data: data can be transferred from the card to the host or vice versa. Data is
transferred via the data lines. They are stored in a FIFO of 32 words, each word is 32
bits wide.
Data FIFO
The data FIFO (first-in-first-out) subunit is a data buffer with a transmit and receive unit.
The FIFO contains a 32-bit wide, 32-word deep data buffer, and transmit and receive logic.
Because the data FIFO operates in the AHB clock domain (HCLK/2), all signals from the
subunits in the SDIO clock domain (SDIOCLK) are resynchronized.
Table 142. Data token format
Description Start bit Data CRC16 End bit
Block Data 0 - yes 1
Stream Data 0 - no 1
Secure digital input/output interface (SDIO) RM0008
552/1093 Doc ID 13902 Rev 13
Depending on the TXACT and RXACT flags, the FIFO can be disabled, transmit enabled, or
receive enabled. TXACT and RXACT are driven by the data path subunit and are mutually
exclusive:
The transmit FIFO refers to the transmit logic and data buffer when TXACT is
asserted
The receive FIFO refers to the receive logic and data buffer when RXACT is
asserted
Transmit FIFO:
Data can be written to the transmit FIFO through the AHB interface when the SDIO is
enabled for transmission.
The transmit FIFO is accessible via 32 sequential addresses. The transmit FIFO
contains a data output register that holds the data word pointed to by the read pointer.
When the data path subunit has loaded its shift register, it increments the read pointer
and drives new data out.
If the transmit FIFO is disabled, all status flags are deasserted. The data path subunit
asserts TXACT when it transmits data.
Receive FIFO
When the data path subunit receives a word of data, it drives the data on the write
databus. The write pointer is incremented after the write operation completes. On the
read side, the contents of the FIFO word pointed to by the current value of the read
pointer is driven onto the read databus. If the receive FIFO is disabled, all status flags
are deasserted, and the read and write pointers are reset. The data path subunit
asserts RXACT when it receives data. Table 144 lists the receive FIFO status flags.
The receive FIFO is accessible via 32 sequential addresses.
Table 143. Transmit FIFO status flags
Flag Description
TXFIFOF Set to high when all 32 transmit FIFO words contain valid data.
TXFIFOE Set to high when the transmit FIFO does not contain valid data.
TXFIFOHE
Set to high when 8 or more transmit FIFO words are empty. This flag can be used
as a DMA request.
TXDAVL
Set to high when the transmit FIFO contains valid data. This flag is the inverse of
the TXFIFOE flag.
TXUNDERR
Set to high when an underrun error occurs. This flag is cleared by writing to the
SDIO Clear register.
RM0008 Secure digital input/output interface (SDIO)
Doc ID 13902 Rev 13 553/1093
22.3.2 SDIO AHB interface
The AHB interface generates the interrupt and DMA requests, and accesses the SDIO
adapter registers and the data FIFO. It consists of a data path, register decoder, and
interrupt/DMA logic.
SDIO interrupts
The interrupt logic generates an interrupt request signal that is asserted when at least one
of the selected status flags is high. A mask register is provided to allow selection of the
conditions that will generate an interrupt. A status flag generates the interrupt request if a
corresponding mask flag is set.
SDIO/DMA interface: procedure for data transfers between the SDIO and
memory
In the example shown, the transfer is from the SDIO host controller to an MMC (512 bytes
using CMD24 (WRITE_BLOCK). The SDIO FIFO is filled by data stored in a memory using
the DMA controller.
1. Do the card identification process
2. Increase the SDIO_CK frequency
3. Select the card by sending CMD7
4. Configure the DMA2 as follows:
a) Enable DMA2 controller and clear any pending interrupts
b) Program the DMA2_Channel4 source address register with the memory locations
base address and DMA2_Channel4 destination address register with the
SDIO_FIFO register address
c) Program DMA2_Channel4 control register (memory increment, not peripheral
increment, peripheral and source width is word size)
d) Enable DMA2_Channel4
Table 144. Receive FIFO status flags
Flag Description
RXFIFOF Set to high when all 32 receive FIFO words contain valid data
RXFIFOE Set to high when the receive FIFO does not contain valid data.
RXFIFOHF
Set to high when 8 or more receive FIFO words contain valid data. This flag can be
used as a DMA request.
RXDAVL
Set to high when the receive FIFO is not empty. This flag is the inverse of the
RXFIFOE flag.
RXOVERR
Set to high when an overrun error occurs. This flag is cleared by writing to the SDIO
Clear register.
Secure digital input/output interface (SDIO) RM0008
554/1093 Doc ID 13902 Rev 13
5. Send CMD24 (WRITE_BLOCK) as follows:
a) Program the SDIO data length register (SDIO data timer register should be
already programmed before the card identification process)
b) Program the SDIO argument register with the address location of the card where
data is to be transferred
c) Program the SDIO command register: CmdIndex with 24 (WRITE_BLOCK);
WaitResp with 1 (SDIO card host waits for a response); CPSMEN with 1 (SDIO
card host enabled to send a command). Other fields are at their reset value.
d) Wait for SDIO_STA[6] = CMDREND interrupt, then program the SDIO data control
register: DTEN with 1 (SDIO card host enabled to send data); DTDIR with 0
(from controller to card); DTMODE with 0 (block data transfer); DMAEN with 1
(DMA enabled); DBLOCKSIZE with 0x9 (512 bytes). Other fields are dont care.
e) Wait for SDIO_STA[10] = DBCKEND
6. Check that no channels are still enabled by polling the DMA Enabled Channel Status
register.
22.4 Card functional description
22.4.1 Card identification mode
While in card identification mode the host resets all cards, validates the operation voltage
range, identifies cards and sets a relative card address (RCA) for each card on the bus. All
data communications in the card identification mode use the command line (CMD) only.
22.4.2 Card reset
The GO_IDLE_STATE command (CMD0) is the software reset command and it puts the
MultiMediaCard and SD memory in the Idle state. The IO_RW_DIRECT command (CMD52)
resets the SD I/O card. After power-up or CMD0, all cards output bus drivers are in the high-
impedance state and the cards are initialized with a default relative card address
(RCA=0x0001) and with a default driver stage register setting (lowest speed, highest driving
current capability).
22.4.3 Operating voltage range validation
All cards can communicate with the SDIO card host using any operating voltage within the
specification range. The supported minimum and maximum V
DD
values are defined in the
operation conditions register (OCR) on the card.
Cards that store the card identification number (CID) and card specific data (CSD) in the
payload memory are able to communicate this information only under data-transfer V
DD
conditions. When the SDIO card host module and the card have incompatible V
DD
ranges,
the card is not able to complete the identification cycle and cannot send CSD data. For this
purpose, the special commands, SEND_OP_COND (CMD1), SD_APP_OP_COND (ACMD41
for SD Memory), and IO_SEND_OP_COND (CMD5 for SD I/O), are designed to provide a
mechanism to identify and reject cards that do not match the V
DD
range desired by the
SDIO card host. The SDIO card host sends the required V
DD
voltage window as the
operand of these commands. Cards that cannot perform data transfer in the specified range
disconnect from the bus and go to the inactive state.
RM0008 Secure digital input/output interface (SDIO)
Doc ID 13902 Rev 13 555/1093
By using these commands without including the voltage range as the operand, the SDIO
card host can query each card and determine the common voltage range before placing out-
of-range cards in the inactive state. This query is used when the SDIO card host is able to
select a common voltage range or when the user requires notification that cards are not
usable.
22.4.4 Card identification process
The card identification process differs for MultiMediaCards and SD cards. For
MultiMediaCard cards, the identification process starts at clock rate F
od
. The SDIO_CMD
line output drivers are open-drain and allow parallel card operation during this process. The
registration process is accomplished as follows:
1. The bus is activated.
2. The SDIO card host broadcasts SEND_OP_COND (CMD1) to receive operation
conditions.
3. The response is the wired AND operation of the operation condition registers from all
cards.
4. Incompatible cards are placed in the inactive state.
5. The SDIO card host broadcasts ALL_SEND_CID (CMD2) to all active cards.
6. The active cards simultaneously send their CID numbers serially. Cards with outgoing
CID bits that do not match the bits on the command line stop transmitting and must wait
for the next identification cycle. One card successfully transmits a full CID to the SDIO
card host and enters the Identification state.
7. The SDIO card host issues SET_RELATIVE_ADDR (CMD3) to that card. This new
address is called the relative card address (RCA); it is shorter than the CID and
addresses the card. The assigned card changes to the Standby state, it does not react
to further identification cycles, and its output switches from open-drain to push-pull.
8. The SDIO card host repeats steps 5 through 7 until it receives a timeout condition.
For the SD card, the identification process starts at clock rate F
od
, and the SDIO_CMD line
output drives are push-pull drivers instead of open-drain. The registration process is
accomplished as follows:
1. The bus is activated.
2. The SDIO card host broadcasts SD_APP_OP_COND (ACMD41).
3. The cards respond with the contents of their operation condition registers.
4. The incompatible cards are placed in the inactive state.
5. The SDIO card host broadcasts ALL_SEND_CID (CMD2) to all active cards.
6. The cards send back their unique card identification numbers (CIDs) and enter the
Identification state.
7. The SDIO card host issues SET_RELATIVE_ADDR (CMD3) to an active card with an
address. This new address is called the relative card address (RCA); it is shorter than
the CID and addresses the card. The assigned card changes to the Standby state. The
SDIO card host can reissue this command to change the RCA. The RCA of the card is
the last assigned value.
8. The SDIO card host repeats steps 5 through 7 with all active cards.
For the SD I/O card, the registration process is accomplished as follows:
Secure digital input/output interface (SDIO) RM0008
556/1093 Doc ID 13902 Rev 13
1. The bus is activated.
2. The SDIO card host sends IO_SEND_OP_COND (CMD5).
3. The cards respond with the contents of their operation condition registers.
4. The incompatible cards are set to the inactive state.
5. The SDIO card host issues SET_RELATIVE_ADDR (CMD3) to an active card with an
address. This new address is called the relative card address (RCA); it is shorter than
the CID and addresses the card. The assigned card changes to the Standby state. The
SDIO card host can reissue this command to change the RCA. The RCA of the card is
the last assigned value.
22.4.5 Block write
During block write (CMD24 - 27) one or more blocks of data are transferred from the host to
the card with a CRC appended to the end of each block by the host. A card supporting block
write is always able to accept a block of data defined by WRITE_BL_LEN. If the CRC fails,
the card indicates the failure on the SDIO_D line and the transferred data are discarded and
not written, and all further transmitted blocks (in multiple block write mode) are ignored.
If the host uses partial blocks whose accumulated length is not block aligned and, block
misalignment is not allowed (CSD parameter WRITE_BLK_MISALIGN is not set), the card
will detect the block misalignment error before the beginning of the first misaligned block.
(ADDRESS_ERROR error bit is set in the status register). The write operation will also be
aborted if the host tries to write over a write-protected area. In this case, however, the card
will set the WP_VIOLATION bit.
Programming of the CID and CSD registers does not require a previous block length setting.
The transferred data is also CRC protected. If a part of the CSD or CID register is stored in
ROM, then this unchangeable part must match the corresponding part of the receive buffer.
If this match fails, then the card reports an error and does not change any register contents.
Some cards may require long and unpredictable times to write a block of data. After
receiving a block of data and completing the CRC check, the card begins writing and holds
the SDIO_D line low if its write buffer is full and unable to accept new data from a new
WRITE_BLOCK command. The host may poll the status of the card with a SEND_STATUS
command (CMD13) at any time, and the card will respond with its status. The
READY_FOR_DATA status bit indicates whether the card can accept new data or whether
the write process is still in progress. The host may deselect the card by issuing CMD7 (to
select a different card), which will place the card in the Disconnect state and release the
SDIO_D line(s) without interrupting the write operation. When reselecting the card, it will
reactivate busy indication by pulling SDIO_D to low if programming is still in progress and
the write buffer is unavailable.
22.4.6 Block read
In Block read mode the basic unit of data transfer is a block whose maximum size is defined
in the CSD (READ_BL_LEN). If READ_BL_PARTIAL is set, smaller blocks whose start and
end addresses are entirely contained within one physical block (as defined by
READ_BL_LEN) may also be transmitted. A CRC is appended to the end of each block,
ensuring data transfer integrity. CMD17 (READ_SINGLE_BLOCK) initiates a block read and
after completing the transfer, the card returns to the Transfer state.
CMD18 (READ_MULTIPLE_BLOCK) starts a transfer of several consecutive blocks.
RM0008 Secure digital input/output interface (SDIO)
Doc ID 13902 Rev 13 557/1093
The host can abort reading at any time, within a multiple block operation, regardless of its
type. Transaction abort is done by sending the stop transmission command.
If the card detects an error (for example, out of range, address misalignment or internal
error) during a multiple block read operation (both types) it stops the data transmission and
remains in the data state. The host must than abort the operation by sending the stop
transmission command. The read error is reported in the response to the stop transmission
command.
If the host sends a stop transmission command after the card transmits the last block of a
multiple block operation with a predefined number of blocks, it is responded to as an illegal
command, since the card is no longer in the data state. If the host uses partial blocks whose
accumulated length is not block-aligned and block misalignment is not allowed, the card
detects a block misalignment error condition at the beginning of the first misaligned block
(ADDRESS_ERROR error bit is set in the status register).
22.4.7 Stream access, stream write and stream read (MultiMediaCard only)
In stream mode, data is transferred in bytes and no CRC is appended at the end of each
block.
Stream write (MultiMediaCard only)
WRITE_DAT_UNTIL_STOP (CMD20) starts the data transfer from the SDIO card host to the
card, beginning at the specified address and continuing until the SDIO card host issues a
stop command. When partial blocks are allowed (CSD parameter WRITE_BL_PARTIAL is
set), the data stream can start and stop at any address within the card address space,
otherwise it can only start and stop at block boundaries. Because the amount of data to be
transferred is not determined in advance, a CRC cannot be used. When the end of the
memory range is reached while sending data and no stop command is sent by the SD card
host, any additional transferred data are discarded.
The maximum clock frequency for a stream write operation is given by the following
equation fields of the card-specific data register:
Maximumspeed = maximum write frequency
TRANSPEED = maximum data transfer rate
writebllen = maximum write data block length
NSAC = data read access time 2 in CLK cycles
TAAC = data read access time 1
R2WFACTOR = write speed factor
If the host attempts to use a higher frequency, the card may not be able to process the data
and stop programming, set the OVERRUN error bit in the status register, and while ignoring
all further data transfer, wait (in the receive data state) for a stop command. The write
operation is also aborted if the host tries to write over a write-protected area. In this case,
however, the card sets the WP_VIOLATION bit.
Maximumspeed MIN TRANSPEED
8 2
writebllen
( ) NSAC ( )
TAAC R2WFACTOR
------------------------------------------------------------------------ ( , ) =
Secure digital input/output interface (SDIO) RM0008
558/1093 Doc ID 13902 Rev 13
Stream read (MultiMediaCard only)
READ_DAT_UNTIL_STOP (CMD11) controls a stream-oriented data transfer.
This command instructs the card to send its data, starting at a specified address, until the
SDIO card host sends STOP_TRANSMISSION (CMD12). The stop command has an
execution delay due to the serial command transmission and the data transfer stops after
the end bit of the stop command. When the end of the memory range is reached while
sending data and no stop command is sent by the SDIO card host, any subsequent data
sent are considered undefined.
The maximum clock frequency for a stream read operation is given by the following equation
and uses fields of the card specific data register.
Maximumspeed = maximum read frequency
TRANSPEED = maximum data transfer rate
readbllen = maximum read data block length
writebllen = maximum write data block length
NSAC = data read access time 2 in CLK cycles
TAAC = data read access time 1
R2WFACTOR = write speed factor
If the host attempts to use a higher frequency, the card is not able to sustain data transfer. If
this happens, the card sets the UNDERRUN error bit in the status register, aborts the
transmission and waits in the data state for a stop command.
22.4.8 Erase: group erase and sector erase
The erasable unit of the MultiMediaCard is the erase group. The erase group is measured in
write blocks, which are the basic writable units of the card. The size of the erase group is a
card-specific parameter and defined in the CSD.
The host can erase a contiguous range of Erase Groups. Starting the erase process is a
three-step sequence.
First the host defines the start address of the range using the ERASE_GROUP_START
(CMD35) command, next it defines the last address of the range using the
ERASE_GROUP_END (CMD36) command and, finally, it starts the erase process by issuing
the ERASE (CMD38) command. The address field in the erase commands is an Erase
Group address in byte units. The card ignores all LSBs below the Erase Group size,
effectively rounding the address down to the Erase Group boundary.
If an erase command is received out of sequence, the card sets the ERASE_SEQ_ERROR
bit in the status register and resets the whole sequence.
If an out-of-sequence (neither of the erase commands, except SEND_STATUS) command
received, the card sets the ERASE_RESET status bit in the status register, resets the erase
sequence and executes the last command.
If the erase range includes write protected blocks, they are left intact and only nonprotected
blocks are erased. The WP_ERASE_SKIP status bit in the status register is set.
Maximumspeed MIN TRANSPEED
8 2
readbllen
( ) NSAC ( )
TAAC R2WFACTOR
----------------------------------------------------------------------- ( , ) =
RM0008 Secure digital input/output interface (SDIO)
Doc ID 13902 Rev 13 559/1093
The card indicates that an erase is in progress by holding SDIO_D low. The actual erase
time may be quite long, and the host may issue CMD7 to deselect the card.
22.4.9 Wide bus selection or deselection
Wide bus (4-bit bus width) operation mode is selected or deselected using
SET_BUS_WIDTH (ACMD6). The default bus width after power-up or GO_IDLE_STATE
(CMD0) is 1 bit. SET_BUS_WIDTH (ACMD6) is only valid in a transfer state, which means
that the bus width can be changed only after a card is selected by
SELECT/DESELECT_CARD (CMD7).
22.4.10 Protection management
Three write protection methods for the cards are supported in the SDIO card host module:
1. internal card write protection (card responsibility)
2. mechanical write protection switch (SDIO card host module responsibility only)
3. password-protected card lock operation
Internal card write protection
Card data can be protected against write and erase. By setting the permanent or temporary
write-protect bits in the CSD, the entire card can be permanently write-protected by the
manufacturer or content provider. For cards that support write protection of groups of
sectors by setting the WP_GRP_ENABLE bit in the CSD, portions of the data can be
protected, and the write protection can be changed by the application. The write protection
is in units of WP_GRP_SIZE sectors as specified in the CSD. The SET_WRITE_PROT and
CLR_WRITE_PROT commands control the protection of the addressed group. The
SEND_WRITE_PROT command is similar to a single block read command. The card sends a
data block containing 32 write protection bits (representing 32 write protect groups starting
at the specified address) followed by 16 CRC bits. The address field in the write protect
commands is a group address in byte units.
The card ignores all LSBs below the group size.
Mechanical write protect switch
A mechanical sliding tab on the side of the card allows the user to set or clear the write
protection on a card. When the sliding tab is positioned with the window open, the card is
write-protected, and when the window is closed, the card contents can be changed. A
matched switch on the socket side indicates to the SDIO card host module that the card is
write-protected. The SDIO card host module is responsible for protecting the card. The
position of the write protect switch is unknown to the internal circuitry of the card.
Password protect
The password protection feature enables the SDIO card host module to lock and unlock a
card with a password. The password is stored in the 128-bit PWD register and its size is set
in the 8-bit PWD_LEN register. These registers are nonvolatile so that a power cycle does
not erase them. Locked cards respond to and execute certain commands. This means that
the SDIO card host module is allowed to reset, initialize, select, and query for status,
however it is not allowed to access data on the card. When the password is set (as indicated
by a nonzero value of PWD_LEN), the card is locked automatically after power-up. As with
the CSD and CID register write commands, the lock/unlock commands are available in the
transfer state only. In this state, the command does not include an address argument and
Secure digital input/output interface (SDIO) RM0008
560/1093 Doc ID 13902 Rev 13
the card must be selected before using it. The card lock/unlock commands have the
structure and bus transaction types of a regular single-block write command. The
transferred data block includes all of the required information for the command (the
password setting mode, the PWD itself, and card lock/unlock). The command data block
size is defined by the SDIO card host module before it sends the card lock/unlock command,
and has the structure shown in Table 158.
The bit settings are as follows:
ERASE: setting it forces an erase operation. All other bits must be zero, and only the
command byte is sent
LOCK_UNLOCK: setting it locks the card. LOCK_UNLOCK can be set simultaneously
with SET_PWD, however not with CLR_PWD
CLR_PWD: setting it clears the password data
SET_PWD: setting it saves the password data to memory
PWD_LEN: it defines the length of the password in bytes
PWD: the password (new or currently used, depending on the command)
The following sections list the command sequences to set/reset a password, lock/unlock the
card, and force an erase.
Setting the password
1. Select a card (SELECT/DESELECT_CARD, CMD7), if none is already selected.
2. Define the block length (SET_BLOCKLEN, CMD16) to send, given by the 8-bit card
lock/unlock mode, the 8-bit PWD_LEN, and the number of bytes of the new password.
When a password replacement is done, the block size must take into account that both
the old and the new passwords are sent with the command.
3. Send LOCK/UNLOCK (CMD42) with the appropriate data block size on the data line
including the 16-bit CRC. The data block indicates the mode (SET_PWD = 1), the
length (PWD_LEN), and the password (PWD) itself. When a password replacement is
done, the length value (PWD_LEN) includes the length of both passwords, the old and
the new one, and the PWD field includes the old password (currently used) followed by
the new password.
4. When the password is matched, the new password and its size are saved into the PWD
and PWD_LEN fields, respectively. When the old password sent does not correspond
(in size and/or content) to the expected password, the LOCK_UNLOCK_FAILED error
bit is set in the card status register, and the password is not changed.
The password length field (PWD_LEN) indicates whether a password is currently set. When
this field is nonzero, there is a password set and the card locks itself after power-up. It is
possible to lock the card immediately in the current power session by setting the
LOCK_UNLOCK bit (while setting the password) or sending an additional command for card
locking.
RM0008 Secure digital input/output interface (SDIO)
Doc ID 13902 Rev 13 561/1093
Resetting the password
1. Select a card (SELECT/DESELECT_CARD, CMD7), if none is already selected.
2. Define the block length (SET_BLOCKLEN, CMD16) to send, given by the 8-bit card
lock/unlock mode, the 8-bit PWD_LEN, and the number of bytes in the currently used
password.
3. Send LOCK/UNLOCK (CMD42) with the appropriate data block size on the data line
including the 16-bit CRC. The data block indicates the mode (CLR_PWD = 1), the
length (PWD_LEN) and the password (PWD) itself. The LOCK_UNLOCK bit is ignored.
4. When the password is matched, the PWD field is cleared and PWD_LEN is set to 0.
When the password sent does not correspond (in size and/or content) to the expected
password, the LOCK_UNLOCK_FAILED error bit is set in the card status register, and
the password is not changed.
Locking a card
1. Select a card (SELECT/DESELECT_CARD, CMD7), if none is already selected.
2. Define the block length (SET_BLOCKLEN, CMD16) to send, given by the 8-bit card
lock/unlock mode (byte 0 in Table 158), the 8-bit PWD_LEN, and the number of bytes of
the current password.
3. Send LOCK/UNLOCK (CMD42) with the appropriate data block size on the data line
including the 16-bit CRC. The data block indicates the mode (LOCK_UNLOCK = 1), the
length (PWD_LEN), and the password (PWD) itself.
4. When the password is matched, the card is locked and the CARD_IS_LOCKED status
bit is set in the card status register. When the password sent does not correspond (in
size and/or content) to the expected password, the LOCK_UNLOCK_FAILED error bit
is set in the card status register, and the lock fails.
It is possible to set the password and to lock the card in the same sequence. In this case,
the SDIO card host module performs all the required steps for setting the password (see
Setting the password on page 560), however it is necessary to set the LOCK_UNLOCK bit
in Step 3 when the new password command is sent.
When the password is previously set (PWD_LEN is not 0), the card is locked automatically
after power on reset. An attempt to lock a locked card or to lock a card that does not have a
password fails and the LOCK_UNLOCK_FAILED error bit is set in the card status register.
Unlocking the card
1. Select a card (SELECT/DESELECT_CARD, CMD7), if none is already selected.
2. Define the block length (SET_BLOCKLEN, CMD16) to send, given by the 8-bit
cardlock/unlock mode (byte 0 in Table 158), the 8-bit PWD_LEN, and the number of
bytes of the current password.
3. Send LOCK/UNLOCK (CMD42) with the appropriate data block size on the data line
including the 16-bit CRC. The data block indicates the mode (LOCK_UNLOCK = 0), the
length (PWD_LEN), and the password (PWD) itself.
4. When the password is matched, the card is unlocked and the CARD_IS_LOCKED
status bit is cleared in the card status register. When the password sent is not correct in
size and/or content and does not correspond to the expected password, the
LOCK_UNLOCK_FAILED error bit is set in the card status register, and the card
remains locked.
Secure digital input/output interface (SDIO) RM0008
562/1093 Doc ID 13902 Rev 13
The unlocking function is only valid for the current power session. When the PWD field is not
clear, the card is locked automatically on the next power-up.
An attempt to unlock an unlocked card fails and the LOCK_UNLOCK_FAILED error bit is set
in the card status register.
Forcing erase
If the user has forgotten the password (PWD content), it is possible to access the card after
clearing all the data on the card. This forced erase operation erases all card data and all
password data.
1. Select a card (SELECT/DESELECT_CARD, CMD7), if none is already selected.
2. Set the block length (SET_BLOCKLEN, CMD16) to 1 byte. Only the 8-bit card
lock/unlock byte (byte 0 in Table 158) is sent.
3. Send LOCK/UNLOCK (CMD42) with the appropriate data byte on the data line including
the 16-bit CRC. The data block indicates the mode (ERASE = 1). All other bits must be
zero.
4. When the ERASE bit is the only bit set in the data field, all card contents are erased,
including the PWD and PWD_LEN fields, and the card is no longer locked. When any
other bits are set, the LOCK_UNLOCK_FAILED error bit is set in the card status
register and the card retains all of its data, and remains locked.
An attempt to use a force erase on an unlocked card fails and the LOCK_UNLOCK_FAILED
error bit is set in the card status register.
22.4.11 Card status register
The response format R1 contains a 32-bit field named card status. This field is intended to
transmit the card status information (which may be stored in a local status register) to the
host. If not specified otherwise, the status entries are always related to the previously issued
command.
Table 145 defines the different entries of the status. The type and clear condition fields in the
table are abbreviated as follows:
Type:
E: error bit
S: status bit
R: detected and set for the actual command response
X: detected and set during command execution. The SDIO card host must poll the card
by issuing the status command to read these bits.
Clear condition:
A: according to the card current state
B: always related to the previous command. Reception of a valid command clears it
(with a delay of one command)
C: clear by read
RM0008 Secure digital input/output interface (SDIO)
Doc ID 13902 Rev 13 563/1093
Table 145. Card status
Bits Identifier Type Value Description
Clear
condition
31
ADDRESS_
OUT_OF_RANGE
E R X
0= no error
1= error
The command address argument was out
of the allowed range for this card.
A multiple block or stream read/write
operation is (although started in a valid
address) attempting to read or write
beyond the card capacity.
C
30 ADDRESS_MISALIGN
0= no error
1= error
The commands address argument (in
accordance with the currently set block
length) positions the first data block
misaligned to the card physical blocks.
A multiple block read/write operation
(although started with a valid
address/block-length combination) is
attempting to read or write a data block
which is not aligned with the physical
blocks of the card.
C
29 BLOCK_LEN_ERROR
0= no error
1= error
Either the argument of a
SET_BLOCKLEN command exceeds the
maximum value allowed for the card, or
the previously defined block length is
illegal for the current command (e.g. the
host issues a write command, the current
block length is smaller than the maximum
allowed value for the card and it is not
allowed to write partial blocks)
C
28 ERASE_SEQ_ERROR
0= no error
1= error
An error in the sequence of erase
commands occurred.
C
27 ERASE_PARAM E X
0= no error
1= error
An invalid selection of erase groups for
erase occurred.
C
26 WP_VIOLATION E X
0= no error
1= error
Attempt to program a write-protected
block. C
25 CARD_IS_LOCKED S R
0 = card
unlocked
1 = card locked
When set, signals that the card is locked
by the host
A
24
LOCK_UNLOCK_
FAILED
E X
0= no error
1= error
Set when a sequence or password error
has been detected in lock/unlock card
command
C
23 COM_CRC_ERROR E R
0= no error
1= error
The CRC check of the previous command
failed.
B
22 ILLEGAL_COMMAND E R
0= no error
1= error
Command not legal for the card state B
21 CARD_ECC_FAILED E X
0= success
1= failure
Card internal ECC was applied but failed
to correct the data.
C
20 CC_ERROR E R
0= no error
1= error
(Undefined by the standard) A card error
occurred, which is not related to the host
command.
C
Secure digital input/output interface (SDIO) RM0008
564/1093 Doc ID 13902 Rev 13
19 ERROR E X
0= no error
1= error
(Undefined by the standard) A generic
card error related to the (and detected
during) execution of the last host
command (e.g. read or write failures).
C
18 Reserved
17 Reserved
16 CID/CSD_OVERWRITE E X
0= no error 1=
error
Can be either of the following errors:
The CID register has already been
written and cannot be overwritten
The read-only section of the CSD does
not match the card contents
An attempt to reverse the copy (set as
original) or permanent WP
(unprotected) bits was made
C
15 WP_ERASE_SKIP E X
0= not protected
1= protected
Set when only partial address space
was erased due to existing write
C
14 CARD_ECC_DISABLED S X
0= enabled
1= disabled
The command has been executed without
using the internal ECC.
A
13 ERASE_RESET
0= cleared
1= set
An erase sequence was cleared before
executing because an out of erase
sequence command was received
(commands other than CMD35, CMD36,
CMD38 or CMD13)
C
12:9 CURRENT_STATE S R
0 = Idle
1 = Ready
2 = Ident
3 = Stby
4 = Tran
5 = Data
6 = Rcv
7 = Prg
8 = Dis
9 = Btst
10-15 = reserved
The state of the card when receiving the
command. If the command execution
causes a state change, it will be visible to
the host in the response on the next
command. The four bits are interpreted as
a binary number between 0 and 15.
B
8 READY_FOR_DATA S R
0= not ready 1
= ready
Corresponds to buffer empty signalling on
the bus
7 SWITCH_ERROR E X
0= no error
1= switch error
If set, the card did not switch to the
expected mode as requested by the
SWITCH command
B
6 Reserved
5 APP_CMD S R
0 = Disabled
1 = Enabled
The card will expect ACMD, or an
indication that the command has been
interpreted as ACMD
C
4 Reserved for SD I/O Card
Table 145. Card status (continued)
Bits Identifier Type Value Description
Clear
condition
RM0008 Secure digital input/output interface (SDIO)
Doc ID 13902 Rev 13 565/1093
22.4.12 SD status register
The SD status contains status bits that are related to the SD memory card proprietary
features and may be used for future application-specific usage. The size of the SD Status is
one data block of 512 bits. The contents of this register are transmitted to the SDIO card
host if ACMD13 is sent (CMD55 followed with CMD13). ACMD13 can be sent to a card in
transfer state only (card is selected).
Table 146 defines the different entries of the SD status register. The type and clear condition
fields in the table are abbreviated as follows:
Type:
E: error bit
S: status bit
R: detected and set for the actual command response
X: detected and set during command execution. The SDIO card Host must poll the card
by issuing the status command to read these bits
Clear condition:
A: according to the card current state
B: always related to the previous command. Reception of a valid command clears it
(with a delay of one command)
C: clear by read
3 AKE_SEQ_ERROR E R
0= no error
1= error
Error in the sequence of the
authentication process
C
2 Reserved for application specific commands
1
Reserved for manufacturer test mode
0
Table 145. Card status (continued)
Bits Identifier Type Value Description
Clear
condition
Table 146. SD status
Bits Identifier Type Value Description
Clear
condition
511: 510 DAT_BUS_WIDTH S R
00= 1 (default)
01= reserved
10= 4 bit width
11= reserved
Shows the currently defined
databus width that was
defined by
SET_BUS_WIDTH
command
A
509 SECURED_MODE S R
0= Not in the mode
1= In Secured Mode
Card is in Secured Mode of
operation (refer to the SD
Security Specification).
A
508: 496 Reserved
Secure digital input/output interface (SDIO) RM0008
566/1093 Doc ID 13902 Rev 13
SIZE_OF_PROTECTED_AREA
Setting this field differs between standard- and high-capacity cards. In the case of a
standard-capacity card, the capacity of protected area is calculated as follows:
Protected area = SIZE_OF_PROTECTED_AREA_* MULT * BLOCK_LEN.
SIZE_OF_PROTECTED_AREA is specified by the unit in MULT*BLOCK_LEN.
In the case of a high-capacity card, the capacity of protected area is specified in this field:
Protected area = SIZE_OF_PROTECTED_AREA
SIZE_OF_PROTECTED_AREA is specified by the unit in bytes.
SPEED_CLASS
This 8-bit field indicates the speed class and the value can be calculated by P
W
/2 (where
P
W
is the write performance).
495: 480 SD_CARD_TYPE S R
00xxh= SD Memory Cards as
defined in Physical Spec Ver1.01-
2.00 (x= dont care). The
following cards are currently
defined:
0000= Regular SD RD/WR Card.
0001= SD ROM Card
In the future, the 8 LSBs will
be used to define different
variations of an SD memory
card (each bit will define
different SD types). The 8
MSBs will be used to define
SD Cards that do not comply
with current SD physical
layer specification.
A
479: 448
SIZE_OF_PROTE
CT ED_AREA
S R
Size of protected area (See
below)
(See below) A
447: 440 SPEED_CLASS S R
Speed Class of the card (See
below)
(See below) A
439: 432
PERFORMANCE_
MOVE
S R
Performance of move indicated by
1 [MB/s] step.
(See below)
(See below) A
431:428 AU_SIZE S R
Size of AU
(See below)
(See below) A
427:424 Reserved
423:408 ERASE_SIZE S R
Number of AUs to be erased at a
time
(See below) A
407:402 ERASE_TIMEOUT S R
Timeout value for erasing areas
specified by
UNIT_OF_ERASE_AU
(See below) A
401:400 ERASE_OFFSET S R
Fixed offset value added to erase
time.
(See below) A
399:312 Reserved
311:0 Reserved for Manufacturer
Table 146. SD status (continued)
Bits Identifier Type Value Description
Clear
condition
RM0008 Secure digital input/output interface (SDIO)
Doc ID 13902 Rev 13 567/1093
PERFORMANCE_MOVE
This 8-bit field indicates Pm (performance move) and the value can be set by 1 [MB/sec]
steps. If the card does not move used RUs (recording units), Pm should be considered as
infinity. Setting the field to FFh means infinity.
AU_SIZE
This 4-bit field indicates the AU size and the value can be selected in the power of 2 base
from 16 KB.
Table 147. Speed class code field
SPEED_CLASS Value definition
00h Class 0
01h Class 2
02h Class 4
03h Class 6
04h FFh Reserved
Table 148. Performance move field
PERFORMANCE_MOVE Value definition
00h Not defined
01h 1 [MB/sec]
02h 02h 2 [MB/sec]
--------- ---------
FEh 254 [MB/sec]
FFh Infinity
Table 149. AU_SIZE field
AU_SIZE Value definition
00h Not defined
01h 16 KB
02h 32 KB
03h 64 KB
04h 128 KB
05h 256 KB
06h 512 KB
07h 1 MB
08h 2 MB
Secure digital input/output interface (SDIO) RM0008
568/1093 Doc ID 13902 Rev 13
The maximum AU size, which depends on the card capacity, is defined in Table 150. The
card can be set to any AU size between RU size and maximum AU size.
ERASE_SIZE
This 16-bit field indicates NERASE. When NERASE numbers of AUs are erased, the timeout
value is specified by ERASE_TIMEOUT (Refer to ERASE_TIMEOUT). The host should
determine the proper number of AUs to be erased in one operation so that the host can
show the progress of the erase operation. If this field is set to 0, the erase timeout
calculation is not supported.
ERASE_TIMEOUT
This 6-bit field indicates TERASE and the value indicates the erase timeout from offset when
multiple AUs are being erased as specified by ERASE_SIZE. The range of
ERASE_TIMEOUT can be defined as up to 63 seconds and the card manufacturer can
choose any combination of ERASE_SIZE and ERASE_TIMEOUT depending on the
implementation. Determining ERASE_TIMEOUT determines the ERASE_SIZE.
09h 4 MB
Ah Fh Reserved
Table 150. Maximum AU size
Capacity 16 MB-64 MB 128 MB-256 MB 512 MB 1 GB-32 GB
Maximum AU Size 512 KB 1 MB 2 MB 4 MB
Table 151. Erase size field
ERASE_SIZE Value definition
0000h Erase timeout calculation is not supported.
0001h 1 AU
0002h 2 AU
0003h 3 AU
--------- ---------
FFFFh 65535 AU
Table 152. Erase timeout field
ERASE_TIMEOUT Value definition
00 Erase timeout calculation is not supported.
01 1 [sec]
02 2 [sec]
03 3 [sec]
Table 149. AU_SIZE field (continued)
AU_SIZE Value definition
RM0008 Secure digital input/output interface (SDIO)
Doc ID 13902 Rev 13 569/1093
ERASE_OFFSET
This 2-bit field indicates TOFFSET and one of four values can be selected. This field is
meaningless if the ERASE_SIZE and ERASE_TIMEOUT fields are set to 0.
22.4.13 SD I/O mode
SD I/O interrupts
To allow the SD I/O card to interrupt the MultiMediaCard/SD module, an interrupt function is
available on a pin on the SD interface. Pin 8, used as SDIO_D1 when operating in the 4-bit
SD mode, signals the cards interrupt to the MultiMediaCard/SD module. The use of the
interrupt is optional for each card or function within a card. The SD I/O interrupt is level-
sensitive, which means that the interrupt line must be held active (low) until it is either
recognized and acted upon by the MultiMediaCard/SD module or deasserted due to the end
of the interrupt period. After the MultiMediaCard/SD module has serviced the interrupt, the
interrupt status bit is cleared via an I/O write to the appropriate bit in the SD I/O cards
internal registers. The interrupt output of all SD I/O cards is active low and the application
must provide pull-up resistors externally on all data lines (SDIO_D[3:0]). The
MultiMediaCard/SD module samples the level of pin 8 (SDIO_D/IRQ) into the interrupt
detector only during the interrupt period. At all other times, the MultiMediaCard/SD module
ignores this value.
The interrupt period is applicable for both memory and I/O operations. The definition of the
interrupt period for operations with single blocks is different from the definition for multiple-
block data transfers.
SD I/O suspend and resume
Within a multifunction SD I/O or a card with both I/O and memory functions, there are
multiple devices (I/O and memory) that share access to the MMC/SD bus. To share access
to the MMC/SD module among multiple devices, SD I/O and combo cards optionally
implement the concept of suspend/resume. When a card supports suspend/resume, the
MMC/SD module can temporarily halt a data transfer operation to one function or memory
(suspend) to free the bus for a higher-priority transfer to a different function or memory. After
this higher-priority transfer is complete, the original transfer is resumed (restarted) where it
left off. Support of suspend/resume is optional on a per-card basis. To perform the
--------- ---------
63 63 [sec]
Table 153. Erase offset field
ERASE_OFFSET Value definition
0h 0 [sec]
1h 1 [sec]
2h 2 [sec]
3h 3 [sec]
Table 152. Erase timeout field (continued)
ERASE_TIMEOUT Value definition
Secure digital input/output interface (SDIO) RM0008
570/1093 Doc ID 13902 Rev 13
suspend/resume operation on the MMC/SD bus, the MMC/SD module performs the
following steps:
1. Determines the function currently using the SDIO_D [3:0] line(s)
2. Requests the lower-priority or slower transaction to suspend
3. Waits for the transaction suspension to complete
4. Begins the higher-priority transaction
5. Waits for the completion of the higher priority transaction
6. Restores the suspended transaction
SD I/O ReadWait
The optional ReadWait (RW) operation is defined only for the SD 1-bit and 4-bit modes. The
ReadWait operation allows the MMC/SD module to signal a card that it is reading multiple
registers (IO_RW_EXTENDED, CMD53) to temporarily stall the data transfer while allowing
the MMC/SD module to send commands to any function within the SD I/O device. To
determine when a card supports the ReadWait protocol, the MMC/SD module must test
capability bits in the internal card registers. The timing for ReadWait is based on the
interrupt period.
22.4.14 Commands and responses
Application-specific and general commands
The SD card host module system is designed to provide a standard interface for a variety of
applications types. In this environment, there is a need for specific customer/application
features. To implement these features, two types of generic commands are defined in the
standard: application-specific commands (ACMD) and general commands (GEN_CMD).
When the card receives the APP_CMD (CMD55) command, the card expects the next
command to be an application-specific command. ACMDs have the same structure as
regular MultiMediaCard commands and can have the same CMD number. The card
recognizes it as ACMD because it appears after APP_CMD (CMD55). When the command
immediately following the APP_CMD (CMD55) is not a defined application-specific
command, the standard command is used. For example, when the card has a definition for
SD_STATUS (ACMD13), and receives CMD13 immediately following APP_CMD (CMD55),
this is interpreted as SD_STATUS (ACMD13). However, when the card receives CMD7
immediately following APP_CMD (CMD55) and the card does not have a definition for
ACMD7, this is interpreted as the standard (SELECT/DESELECT_CARD) CMD7.
To use one of the manufacturer-specific ACMDs the SD card Host must perform the
following steps:
1. Send APP_CMD (CMD55)
The card responds to the MultiMediaCard/SD module, indicating that the APP_CMD bit
is set and an ACMD is now expected.
2. Send the required ACMD
The card responds to the MultiMediaCard/SD module, indicating that the APP_CMD bit
is set and that the accepted command is interpreted as an ACMD. When a nonACMD is
sent, it is handled by the card as a normal MultiMediaCard command and the
APP_CMD bit in the card status register stays clear.
When an invalid command is sent (neither ACMD nor CMD) it is handled as a standard
MultiMediaCard illegal command error.
RM0008 Secure digital input/output interface (SDIO)
Doc ID 13902 Rev 13 571/1093
The bus transaction for a GEN_CMD is the same as the single-block read or write
commands (WRITE_BLOCK, CMD24 or READ_SINGLE_BLOCK,CMD17). In this case, the
argument denotes the direction of the data transfer rather than the address, and the data
block has vendor-specific format and meaning.
The card must be selected (in transfer state) before sending GEN_CMD (CMD56). The data
block size is defined by SET_BLOCKLEN (CMD16). The response to GEN_CMD (CMD56)
is in R1b format.
Command types
Both application-specific and general commands are divided into the four following types:
broadcast command (BC): sent to all cards; no responses returned.
broadcast command with response (BCR): sent to all cards; responses received
from all cards simultaneously.
addressed (point-to-point) command (AC): sent to the card that is selected; does
not include a data transfer on the SDIO_D line(s).
addressed (point-to-point) data transfer command (ADTC): sent to the card that is
selected; includes a data transfer on the SDIO_D line(s).
Command formats
See Table 138 on page 547 for command formats.
Commands for the MultiMediaCard/SD module
Table 154. Block-oriented write commands
CMD
index
Type Argument
Response
format
Abbreviation Description
CMD23 ac
[31:16] set to 0
[15:0] number
of blocks
R1 SET_BLOCK_COUNT
Defines the number of blocks which
are going to be transferred in the
multiple-block read or write command
that follows.
CMD24 adtc
[31:0] data
address
R1 WRITE_BLOCK
Writes a block of the size selected by
the SET_BLOCKLEN command.
CMD25 adtc
[31:0] data
address
R1 WRITE_MULTIPLE_BLOCK
Continuously writes blocks of data
until a STOP_TRANSMISSION
follows or the requested number of
blocks has been received.
CMD26 adtc [31:0] stuff bits R1 PROGRAM_CID
Programming of the card identification
register. This command must be
issued only once per card. The card
contains hardware to prevent this
operation after the first programming.
Normally this command is reserved
for manufacturer.
CMD27 adtc [31:0] stuff bits R1 PROGRAM_CSD
Programming of the programmable
bits of the CSD.
Secure digital input/output interface (SDIO) RM0008
572/1093 Doc ID 13902 Rev 13
Table 155. Block-oriented write protection commands
CMD
index
Type Argument
Response
format
Abbreviation Description
CMD28 ac
[31:0] data
address
R1b SET_WRITE_PROT
If the card has write protection features,
this command sets the write protection bit
of the addressed group. The properties of
write protection are coded in the card-
specific data (WP_GRP_SIZE).
CMD29 ac
[31:0] data
address
R1b CLR_WRITE_PROT
If the card provides write protection
features, this command clears the write
protection bit of the addressed group.
CMD30 adtc
[31:0] write
protect data
address
R1 SEND_WRITE_PROT
If the card provides write protection
features, this command asks the card to
send the status of the write protection
bits.
CMD31 Reserved
Table 156. Erase commands
CMD
index
Type Argument
Response
format
Abbreviation Description
CMD32
...
CMD34
Reserved. These command indexes cannot be used in order to maintain backward compatibility with older
versions of the MultiMediaCard.
CMD35 ac [31:0] data address R1 ERASE_GROUP_START
Sets the address of the first erase
group within a range to be selected
for erase.
CMD36 ac [31:0] data address R1 ERASE_GROUP_END
Sets the address of the last erase
group within a continuous range to be
selected for erase.
CMD37
Reserved. This command index cannot be used in order to maintain backward compatibility with older
versions of the MultiMediaCards
CMD38 ac [31:0] stuff bits R1 ERASE
Erases all previously selected write
blocks.
Table 157. I/O mode commands
CMD
index
Type Argument
Response
format
Abbreviation Description
CMD39 ac
[31:16] RCA
[15:15] register
write flag
[14:8] register
address
[7:0] register data
R4 FAST_IO
Used to write and read 8-bit (register) data
fields. The command addresses a card and a
register and provides the data for writing if
the write flag is set. The R4 response
contains data read from the addressed
register. This command accesses
application-dependent registers that are not
defined in the MultiMediaCard standard.
RM0008 Secure digital input/output interface (SDIO)
Doc ID 13902 Rev 13 573/1093
22.5 Response formats
All responses are sent via the MCCMD command line SDIO_CMD. The response
transmission always starts with the left bit of the bit string corresponding to the response
code word. The code length depends on the response type.
A response always starts with a start bit (always 0), followed by the bit indicating the
direction of transmission (card = 0). A value denoted by x in the tables below indicates a
variable entry. All responses, except for the R3 response type, are protected by a CRC.
Every command code word is terminated by the end bit (always 1).
There are five types of responses. Their formats are defined as follows:
CMD40 bcr [31:0] stuff bits R5 GO_IRQ_STATE Places the system in the interrupt mode.
CMD41 Reserved
Table 157. I/O mode commands (continued)
CMD
index
Type Argument
Response
format
Abbreviation Description
Table 158. Lock card
CMD
index
Type Argument
Response
format
Abbreviation Description
CMD42 adtc [31:0] stuff bits R1b LOCK_UNLOCK
Sets/resets the password or locks/unlocks
the card. The size of the data block is set
by the SET_BLOCK_LEN command.
CMD43
...
CMD54
Reserved
Table 159. Application-specific commands
CMD
index
Type Argument
Response
format
Abbreviation Description
CMD55 ac
[31:16] RCA
[15:0] stuff bits
R1 APP_CMD
Indicates to the card that the next command
bits is an application specific command rather
than a standard command
CMD56 adtc
[31:1] stuff bits
[0]: RD/WR
Used either to transfer a data block to the card
or to get a data block from the card for general
purpose/application-specific commands. The
size of the data block shall be set by the
SET_BLOCK_LEN command.
CMD57
...
CMD59
Reserved.
CMD60
...
CMD63
Reserved for manufacturer.
Secure digital input/output interface (SDIO) RM0008
574/1093 Doc ID 13902 Rev 13
22.5.1 R1 (normal response command)
Code length = 48 bits. The 45:40 bits indicate the index of the command to be responded to,
this value being interpreted as a binary-coded number (between 0 and 63). The status of the
card is coded in 32 bits.
22.5.2 R1b
It is identical to R1 with an optional busy signal transmitted on the data line. The card may
become busy after receiving these commands based on its state prior to the command
reception.
22.5.3 R2 (CID, CSD register)
Code length = 136 bits. The contents of the CID register are sent as a response to the
CMD2 and CMD10 commands. The contents of the CSD register are sent as a response to
CMD9. Only the bits [127...1] of the CID and CSD are transferred, the reserved bit [0] of
these registers is replaced by the end bit of the response. The card indicates that an erase
is in progress by holding MCDAT low. The actual erase time may be quite long, and the host
may issue CMD7 to deselect the card.
22.5.4 R3 (OCR register)
Code length: 48 bits. The contents of the OCR register are sent as a response to CMD1.
The level coding is as follows: restricted voltage windows = low, card busy = low.
Table 160. R1 response
Bit position Width (bits Value Description
47 1 0 Start bit
46 1 0 Transmission bit
[45:40] 6 X Command index
[39:8] 32 X Card status
[7:1] 7 X CRC7
0 1 1 End bit
Table 161. R2 response
Bit position Width (bits Value Description
135 1 0 Start bit
134 1 0 Transmission bit
[133:128] 6 111111 Command index
[127:1] 127 X Card status
0 1 1 End bit
RM0008 Secure digital input/output interface (SDIO)
Doc ID 13902 Rev 13 575/1093
22.5.5 R4 (Fast I/O)
Code length: 48 bits. The argument field contains the RCA of the addressed card, the
register address to be read out or written to, and its content.
22.5.6 R4b
For SD I/O only: an SDIO card receiving the CMD5 will respond with a unique SDIO
response R4. The format is:
Table 162. R3 response
Bit position Width (bits Value Description
47 1 0 Start bit
46 1 0 Transmission bit
[45:40] 6 111111 Reserved
[39:8] 32 X OCR register
[7:1] 7 1111111 Reserved
0 1 1 End bit
Table 163. R4 response
Bit position Width (bits Value Description
47 1 0 Start bit
46 1 0 Transmission bit
[45:40] 6 111111 Reserved
[39:8] Argument field
[31:16] 16 X RCA
[15:8] 8 X register address
[7:0] 8 X read register contents
[7:1] 7 1111111 CRC7
0 1 1 End bit
Table 164. R4b response
Bit position Width (bits Value Description
47 1 0 Start bit
46 1 0 Transmission bit
[45:40] 6 x Reserved
[39:8] Argument field
39 16 X Card is ready
[38:36] 3 X Number of I/O functions
35 1 X Present memory
[34:32] 3 X Stuff bits
[31:8] 24 X I/O ORC
Secure digital input/output interface (SDIO) RM0008
576/1093 Doc ID 13902 Rev 13
Once an SD I/O card has received a CMD5, the I/O portion of that card is enabled to
respond normally to all further commands. This I/O enable of the function within the I/O card
will remain set until a reset, power cycle or CMD52 with write to I/O reset is received by the
card. Note that an SD memory-only card may respond to a CMD5. The proper response for
a memory-only card would be Present memory = 1 and Number of I/O functions = 0. A
memory-only card built to meet the SD Memory Card specification version 1.0 would detect
the CMD5 as an illegal command and not respond. The I/O aware host will send CMD5. If
the card responds with response R4, the host determines the cards configuration based on
the data contained within the R4 response.
22.5.7 R5 (interrupt request)
Only for MultiMediaCard. Code length: 48 bits. If the response is generated by the host, the
RCA field in the argument will be 0x0.
22.5.8 R6
Only for SD I/O. The normal response to CMD3 by a memory device. It is shown in
Table 166.
[7:1] 7 X Reserved
0 1 1 End bit
Table 164. R4b response (continued)
Bit position Width (bits Value Description
Table 165. R5 response
Bit position Width (bits Value Description
47 1 0 Start bit
46 1 0 Transmission bit
[45:40] 6 111111 CMD40
[39:8] Argument field
[31:16] 16 X
RCA [31:16] of winning
card or of the host
[15:0] 16 X
Not defined. May be used
for IRQ data
[7:1] 7 X CRC7
0 1 1 End bit
Table 166. R6 response
Bit position Width (bits) Value Description
47 1 0 Start bit
46 1 0 Transmission bit
[45:40] 6 101000 CMD40
RM0008 Secure digital input/output interface (SDIO)
Doc ID 13902 Rev 13 577/1093
The card [23:8] status bits are changed when CMD3 is sent to an I/O-only card. In this case,
the 16 bits of response are the SD I/O-only values:
Bit [15] COM_CRC_ERROR
Bit [14] ILLEGAL_COMMAND
Bit [13] ERROR
Bits [12:0] Reserved
22.6 SDIO I/O card-specific operations
The following features are SD I/O-specific operations:
SDIO read wait operation by SDIO_D2 signalling
SDIO read wait operation by stopping the clock
SDIO suspend/resume operation (write and read suspend)
SDIO interrupts
The SDIO supports these operations only if the SDIO_DCTRL[11] bit is set, except for read
suspend that does not need specific hardware implementation.
22.6.1 SDIO I/O read wait operation by SDIO_D2 signalling
It is possible to start the readwait interval before the first block is received: when the data
path is enabled (SDIO_DCTRL[0] bit set), the SDIO-specific operation is enabled
(SDIO_DCTRL[11] bit set), read wait starts (SDI0_DCTRL[10] =0 and SDI_DCTRL[8] =1)
and data direction is from card to SDIO (SDIO_DCTRL[1] = 1), the DPSM directly moves
from Idle to Readwait. In Readwait the DPSM drives SDIO_D2 to 0 after 2 SDIO_CK clock
cycles. In this state, when you set the RWSTOP bit (SDIO_DCTRL[9]), the DPSM remains
in Wait for two more SDIO_CK clock cycles to drive SDIO_D2 to 1 for one clock cycle (in
accordance with SDIO specification). The DPSM then starts waiting again until it receives
data from the card. The DPSM will not start a readwait interval while receiving a block even
if read wait start is set: the readwait interval will start after the CRC is received. The
RWSTOP bit has to be cleared to start a new read wait operation. During the readwait
interval, the SDIO can detect SDIO interrupts on SDIO_D1.
22.6.2 SDIO read wait operation by stopping SDIO_CK
If the SDIO card does not support the previous read wait method, the SDIO can perform a
read wait by stopping SDIO_CK (SDIO_DCTRL is set just like in the method presented in
Section 22.6.1, but SDIO_DCTRL[10] =1): DSPM stops the clock two SDIO_CK cycles after
the end bit of the current received block and starts the clock again after the read wait start
bit is set.
[39:8] Argument
field
[31:16] 16 X RCA [31:16] of winning card or of the host
[15:0] 16 X Not defined. May be used for IRQ data
[7:1] 7 X CRC7
0 1 1 End bit
Table 166. R6 response (continued)
Bit position Width (bits) Value Description
Secure digital input/output interface (SDIO) RM0008
578/1093 Doc ID 13902 Rev 13
As SDIO_CK is stopped, any command can be issued to the card. During a read/wait
interval, the SDIO can detect SDIO interrupts on SDIO_D1.
22.6.3 SDIO suspend/resume operation
While sending data to the card, the SDIO can suspend the write operation. the
SDIO_CMD[11] bit is set and indicates to the CPSM that the current command is a suspend
command. The CPSM analyzes the response and when the ACK is received from the card
(suspend accepted), it acknowledges the DPSM that goes Idle after receiving the CRC
token of the current block.
The hardware does not save the number of the remaining block to be sent to complete the
suspended operation (resume).
The write operation can be suspended by software, just by disabling the DPSM
(SDIO_DCTRL[0] =0) when the ACK of the suspend command is received from the card.
The DPSM enters then the Idle state.
To suspend a read: the DPSM waits in the Wait_r state as the function to be suspended
sends a complete packet just before stopping the data transaction. The application
continues reading RxFIFO until the FIF0 is empty, and the DPSM goes Idle automatically.
22.6.4 SDIO interrupts
SDIO interrupts are detected on the SDIO_D1 line once the SDIO_DCTRL[11] bit is set.
22.7 CE-ATA specific operations
The following features are CE-ATA specific operations:
sending the command completion signal disable to the CE-ATA device
receiving the command completion signal from the CE-ATA device
signaling the completion of the CE-ATA command to the CPU, using the status bit
and/or interrupt.
The SDIO supports these operations only for the CE-ATA CMD61 command, that is, if
SDIO_CMD[14] is set.
22.7.1 Command completion signal disable
Command completion signal disable is sent 8 bit cycles after the reception of a short
response if the enable CMD completion bit, SDIO_CMD[12], is not set and the not interrupt
Enable bit, SDIO_CMD[13], is set.
The CPSM enters the Pend state, loading the command shift register with the disable
sequence 00001 and, the command counter with 43. Eight cycles after, a trigger moves
the CPSM to the Send state. When the command counter reaches 48, the CPSM becomes
Idle as no response is awaited.
22.7.2 Command completion signal enable
If the enable CMD completion bit SDIO_CMD[12] is set and the not interrupt Enable bit
SDIO_CMD[13] is set, the CPSM waits for the command completion signal in the Waitcpl
state.
RM0008 Secure digital input/output interface (SDIO)
Doc ID 13902 Rev 13 579/1093
When 0 is received on the CMD line, the CPSM enters the Idle state. No new command
can be sent for 7 bit cycles. Then, for the last 5 cycles (out of the 7) the CMD line is driven to
1 in push-pull mode.
22.7.3 CE-ATA interrupt
The command completion is signaled to the CPU by the status bit SDIO_STA[23]. This static
bit can be cleared with the clear bit SDIO_ICR[23].
The SDIO_STA[23] status bit can generate an interrupt on each interrupt line, depending on
the mask bit SDIO_MASKx[23].
22.7.4 Aborting CMD61
If the command completion disable signal has not been sent and CMD61 needs to be
aborted, the command state machine must be disabled. It then becomes Idle, and the
CMD12 command can be sent. No command completion disable signal is sent during the
operation.
22.8 HW flow control
The HW flow control functionality is used to avoid FIFO underrun (TX mode) and overrun
(RX mode) errors.
The behavior is to stop SDIO_CK and freeze SDIO state machines. The data transfer is
stalled while the FIFO is unable to transmit or receive data. Only state machines clocked by
SDIOCLK are frozen, the AHB interface is still alive. The FIFO can thus be filled or emptied
even if flow control is activated.
To enable HW flow control, the SDIO_CLKCR[14] register bit must be set to 1. After reset
Flow Control is disabled.
22.9 SDIO registers
The device communicates to the system via 32-bit-wide control registers accessible via
AHB.
The peripheral registers have to be accessed by words (32-bit).
Secure digital input/output interface (SDIO) RM0008
580/1093 Doc ID 13902 Rev 13
22.9.1 SDIO power control register (SDIO_POWER)
Address offset: 0x00
Reset value: 0x0000 0000
Note: At least seven HCLK clock periods are needed between two write accesses to this register.
22.9.2 SDI clock control register (SDIO_CLKCR)
Address offset: 0x04
Reset value: 0x0000 0000
The SDIO_CLKCR register controls the SDIO_CK output clock.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
PWRC
TRL
rw rw
Bits 31:2 Reserved, must be kept at reset value.
[1:0] PWRCTRL: Power supply control bits.
These bits are used to define the current functional state of the card clock:
00: Power-off: the clock to card is stopped.
01: Reserved
10: Reserved power-up
11: Power-on: the card is clocked.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
H
W
F
C
_
E
N
N
E
G
E
D
G
E
WID
BUS
B
Y
P
A
S
S
P
W
R
S
A
V
C
L
K
E
N
CLKDIV
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:15 Reserved, must be kept at reset value.
Bit 14 HWFC_EN: HW Flow Control enable
0b: HW Flow Control is disabled
1b: HW Flow Control is enabled
When HW Flow Control is enabled, the meaning of the TXFIFOE and RXFIFOF interrupt
signals, please see SDIO Status register definition in Section 22.9.11.
Bit 13 NEGEDGE:SDIO_CK dephasing selection bit
0b: SDIO_CK generated on the rising edge of the master clock SDIOCLK
1b: SDIO_CK generated on the falling edge of the master clock SDIOCLK
Bits 12:11 WIDBUS: Wide bus mode enable bit
00: Default bus mode: SDIO_D0 used
01: 4-wide bus mode: SDIO_D[3:0] used
10: 8-wide bus mode: SDIO_D[7:0] used
RM0008 Secure digital input/output interface (SDIO)
Doc ID 13902 Rev 13 581/1093
Note: 1 While the SD/SDIO card or MultiMediaCard is in identification mode, the SDIO_CK
frequency must be less than 400 kHz.
2 The clock frequency can be changed to the maximum card bus frequency when relative
card addresses are assigned to all cards.
3 At least seven HCLK clock periods are needed between two write accesses to this register.
SDIO_CK can also be stopped during the read wait interval for SD I/O cards: in this case the
SDIO_CLKCR register does not control SDIO_CK.
22.9.3 SDIO argument register (SDIO_ARG)
Address offset: 0x08
Reset value: 0x0000 0000
The SDIO_ARG register contains a 32-bit command argument, which is sent to a card as
part of a command message.
Bit 10 BYPASS: Clock divider bypass enable bit
0: Disable bypass: SDIOCLK is divided according to the CLKDIV value before driving the
SDIO_CK output signal.
1: Enable bypass: SDIOCLK directly drives the SDIO_CK output signal.
Bit 9 PWRSAV: Power saving configuration bit
For power saving, the SDIO_CK clock output can be disabled when the bus is idle by setting
PWRSAV:
0: SDIO_CK clock is always enabled
1: SDIO_CK is only enabled when the bus is active
Bit 8 CLKEN: Clock enable bit
0: SDIO_CK is disabled
1: SDIO_CK is enabled
Bits 7:0 CLKDIV: Clock divide factor
This field defines the divide factor between the input clock (SDIOCLK) and the output clock
(SDIO_CK): SDIO_CK frequency = SDIOCLK / [CLKDIV + 2].
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CMDARG
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:0 CMDARG: Command argument
Command argument sent to a card as part of a command message. If a command contains
an argument, it must be loaded into this register before writing a command to the command
register.
Secure digital input/output interface (SDIO) RM0008
582/1093 Doc ID 13902 Rev 13
22.9.4 SDIO command register (SDIO_CMD)
Address offset: 0x0C
Reset value: 0x0000 0000
The SDIO_CMD register contains the command index and command type bits. The
command index is sent to a card as part of a command message. The command type bits
control the command path state machine (CPSM).
Note: 1 At least seven HCLK clock periods are needed between two write accesses to this register.
2 MultiMediaCards can send two kinds of response: short responses, 48 bits long, or long
responses,136 bits long. SD card and SD I/O card can send only short responses, the
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
C
E
-
A
T
A
C
M
D
n
I
E
N
E
N
C
M
D
c
o
m
p
l
S
D
I
O
S
u
s
p
e
n
d
C
P
S
M
E
N
W
A
I
T
P
E
N
D
W
A
I
T
I
N
T
W
A
I
T
R
E
S
P
C
M
D
I
N
D
E
X
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:15 Reserved, must be kept at reset value.
Bit 14 ATACMD: CE-ATA command
If ATACMD is set, the CPSM transfers CMD61.
Bit 13 nIEN: not Interrupt Enable
if this bit is 0, interrupts in the CE-ATA device are enabled.
Bit 12 ENCMDcompl: Enable CMD completion
If this bit is set, the command completion signal is enabled.
Bit 11 SDIOSuspend: SD I/O suspend command
If this bit is set, the command to be sent is a suspend command (to be used only with SDIO
card).
Bit 10 CPSMEN: Command path state machine (CPSM) Enable bit
If this bit is set, the CPSM is enabled.
Bit 9 WAITPEND: CPSM Waits for ends of data transfer (CmdPend internal signal).
If this bit is set, the CPSM waits for the end of data transfer before it starts sending a
command.
Bit 8 WAITINT: CPSM waits for interrupt request
If this bit is set, the CPSM disables command timeout and waits for an interrupt request.
Bits 7:6 WAITRESP: Wait for response bits
They are used to configure whether the CPSM is to wait for a response, and if yes, which
kind of response.
00: No response, expect CMDSENT flag
01: Short response, expect CMDREND or CCRCFAIL flag
10: No response, expect CMDSENT flag
11: Long response, expect CMDREND or CCRCFAIL flag
Bit 5:0 CMDINDEX: Command index
The command index is sent to the card as part of a command message.
RM0008 Secure digital input/output interface (SDIO)
Doc ID 13902 Rev 13 583/1093
argument can vary according to the type of response: the software will distinguish the type
of response according to the sent command. CE-ATA devices send only short responses.
22.9.5 SDIO command response register (SDIO_RESPCMD)
Address offset: 0x10
Reset value: 0x0000 0000
The SDIO_RESPCMD register contains the command index field of the last command
response received. If the command response transmission does not contain the command
index field (long or OCR response), the RESPCMD field is unknown, although it must
contain 111111b (the value of the reserved field from the response).
22.9.6 SDIO response 1..4 register (SDIO_RESPx)
Address offset: (0x10 + (4 x)); x = 1..4
Reset value: 0x0000 0000
The SDIO_RESP1/2/3/4 registers contain the status of a card, which is part of the received
response.
The Card Status size is 32 or 127 bits, depending on the response type.
The most significant bit of the card status is received first. The SDIO_RESP3 register LSB is
always 0b.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
RESPCMD
r r r r r r
Bits 31:6 Reserved, must be kept at reset value.
Bits 5:0 RESPCMD: Response command index
Read-only bit field. Contains the command index of the last command response received.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CARDSTATUSx
r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r r
Bits 31:0 CARDSTATUSx: see Table 167.
Table 167. Response type and SDIO_RESPx registers
Register Short response Long response
SDIO_RESP1 Card Status[31:0] Card Status [127:96]
SDIO_RESP2 Unused Card Status [95:64]
SDIO_RESP3 Unused Card Status [63:32]
SDIO_RESP4 Unused Card Status [31:1]0b
Secure digital input/output interface (SDIO) RM0008
584/1093 Doc ID 13902 Rev 13
22.9.7 SDIO data timer register (SDIO_DTIMER)
Address offset: 0x24
Reset value: 0x0000 0000
The SDIO_DTIMER register contains the data timeout period, in card bus clock periods.
A counter loads the value from the SDIO_DTIMER register, and starts decrementing when
the data path state machine (DPSM) enters the Wait_R or Busy state. If the timer reaches 0
while the DPSM is in either of these states, the timeout status flag is set.
Note: A data transfer must be written to the data timer register and the data length register before
being written to the data control register.
22.9.8 SDIO data length register (SDIO_DLEN)
Address offset: 0x28
Reset value: 0x0000 0000
The SDIO_DLEN register contains the number of data bytes to be transferred. The value is
loaded into the data counter when data transfer starts.
Note: For a block data transfer, the value in the data length register must be a multiple of the block
size (see SDIO_DCTRL). A data transfer must be written to the data timer register and the
data length register before being written to the data control register.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
DATATIME
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:0 DATATIME: Data timeout period
Data timeout period expressed in card bus clock periods.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
DATALENGTH
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:25 Reserved, must be kept at reset value.
Bits 24:0 DATALENGTH: Data length value
Number of data bytes to be transferred.
RM0008 Secure digital input/output interface (SDIO)
Doc ID 13902 Rev 13 585/1093
22.9.9 SDIO data control register (SDIO_DCTRL)
Address offset: 0x2C
Reset value: 0x0000 0000
The SDIO_DCTRL register control the data path state machine (DPSM).
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
S
D
I
O
E
N
R
W
M
O
D
R
W
S
T
O
P
R
W
S
T
A
R
T
DBLOCKSIZE
D
M
A
E
N
D
T
M
O
D
E
D
T
D
I
R
D
T
E
N
rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:12 Reserved, must be kept at reset value.
Bit 11 SDIOEN: SD I/O enable functions
If this bit is set, the DPSM performs an SD I/O-card-specific operation.
Bit 10 RWMOD: Read wait mode
0: Read Wait control stopping SDIO_D2
1: Read Wait control using SDIO_CK
Bit 9 RWSTOP: Read wait stop
0: Read wait in progress if RWSTART bit is set
1: Enable for read wait stop if RWSTART bit is set
Bit 8 RWSTART: Read wait start
If this bit is set, read wait operation starts.
Bits 7:4 DBLOCKSIZE: Data block size
Define the data block length when the block data transfer mode is selected:
0000: (0 decimal) lock length = 2
0
= 1 byte
0001: (1 decimal) lock length = 2
1
= 2 bytes
0010: (2 decimal) lock length = 2
2
= 4 bytes
0011: (3 decimal) lock length = 2
3
= 8 bytes
0100: (4 decimal) lock length = 2
4
= 16 bytes
0101: (5 decimal) lock length = 2
5
= 32 bytes
0110: (6 decimal) lock length = 2
6
= 64 bytes
0111: (7 decimal) lock length = 2
7
= 128 bytes
1000: (8 decimal) lock length = 2
8
= 256 bytes
1001: (9 decimal) lock length = 2
9
= 512 bytes
1010: (10 decimal) lock length = 2
10
= 1024 bytes
1011: (11 decimal) lock length = 2
11
= 2048 bytes
1100: (12 decimal) lock length = 2
12
= 4096 bytes
1101: (13 decimal) lock length = 2
13
= 8192 bytes
1110: (14 decimal) lock length = 2
14
= 16384 bytes
1111: (15 decimal) reserved
Bit 3 DMAEN: DMA enable bit
0: DMA disabled.
1: DMA enabled.
Secure digital input/output interface (SDIO) RM0008
586/1093 Doc ID 13902 Rev 13
Note: At least seven HCLK clock periods are needed between two write accesses to this register.
22.9.10 SDIO data counter register (SDIO_DCOUNT)
Address offset: 0x30
Reset value: 0x0000 0000
The SDIO_DCOUNT register loads the value from the data length register (see
SDIO_DLEN) when the DPSM moves from the Idle state to the Wait_R or Wait_S state. As
data is transferred, the counter decrements the value until it reaches 0. The DPSM then
moves to the Idle state and the data status end flag, DATAEND, is set.
Note: This register should be read only when the data transfer is complete.
Bit 2 DTMODE: Data transfer mode selection 1: Stream or SDIO multibyte data transfer.
0: Block data transfer
1: Stream or SDIO multibyte data transfer on STM32F10xxx XL-density devices.
Stream data transfer on STM32F10xxx high-density devices.
Bit 1 DTDIR: Data transfer direction selection
0: From controller to card.
1: From card to controller.
[0] DTEN: Data transfer enabled bit
Data transfer starts if 1b is written to the DTEN bit. Depending on the direction bit, DTDIR,
the DPSM moves to the Wait_S, Wait_R state or Readwait if RW Start is set immediately at
the beginning of the transfer. It is not necessary to clear the enable bit after the end of a data
transfer but the SDIO_DCTRL must be updated to enable a new data transfer
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
DATACOUNT
r r r r r r r r r r r r r r r r r r r r r r r r r
Bits 31:25 Reserved, must be kept at reset value.
Bits 24:0 DATACOUNT: Data count value
When this bit is read, the number of remaining data bytes to be transferred is returned. Write
has no effect.
RM0008 Secure digital input/output interface (SDIO)
Doc ID 13902 Rev 13 587/1093
22.9.11 SDIO status register (SDIO_STA)
Address offset: 0x34
Reset value: 0x0000 0000
The SDIO_STA register is a read-only register. It contains two types of flag:
Static flags (bits [23:22,10:0]): these bits remain asserted until they are cleared by
writing to the SDIO Interrupt Clear register (see SDIO_ICR)
Dynamic flags (bits [21:11]): these bits change state depending on the state of the
underlying logic (for example, FIFO full and empty flags are asserted and deasserted
as data while written to the FIFO)
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
R
e
s
e
r
v
e
d
C
E
A
T
A
E
N
D
S
D
I
O
I
T
R
X
D
A
V
L
T
X
D
A
V
L
R
X
F
I
F
O
E
T
X
F
I
F
O
E
R
X
F
I
F
O
F
T
X
F
I
F
O
F
R
X
F
I
F
O
H
F
T
X
F
I
F
O
H
E
R
X
A
C
T
T
X
A
C
T
C
M
D
A
C
T
D
B
C
K
E
N
D
S
T
B
I
T
E
R
R
D
A
T
A
E
N
D
C
M
D
S
E
N
T
C
M
D
R
E
N
D
R
X
O
V
E
R
R
T
X
U
N
D
E
R
R
D
T
I
M
E
O
U
T
C
T
I
M
E
O
U
T
D
C
R
C
F
A
I
L
C
C
R
C
F
A
I
L
Res. r r r r r r r r r r r r r r r r r r r r r r r r
Bits 31:24 Reserved, must be kept at reset value.
Bit 23 CEATAEND: CE-ATA command completion signal received for CMD61
Bit 22 SDIOIT: SDIO interrupt received
Bit 21 RXDAVL: Data available in receive FIFO
Bit 20 TXDAVL: Data available in transmit FIFO
Bit 19 RXFIFOE: Receive FIFO empty
Bit 18 TXFIFOE: Transmit FIFO empty
When HW Flow Control is enabled, TXFIFOE signals becomes activated when the FIFO
contains 2 words.
Bit 17 RXFIFOF: Receive FIFO full
When HW Flow Control is enabled, RXFIFOF signals becomes activated 2 words before the
FIFO is full.
Bit 16 TXFIFOF: Transmit FIFO full
Bit 15 RXFIFOHF: Receive FIFO half full: there are at least 8 words in the FIFO
Bit 14 TXFIFOHE: Transmit FIFO half empty: at least 8 words can be written into the FIFO
Bit 13 RXACT: Data receive in progress
Bit 12 TXACT: Data transmit in progress
Bit 11 CMDACT: Command transfer in progress
Bit 10 DBCKEND: Data block sent/received (CRC check passed)
Bit 9 STBITERR: Start bit not detected on all data signals in wide bus mode
Bit 8 DATAEND: Data end (data counter, SDIDCOUNT, is zero)
Bit 7 CMDSENT: Command sent (no response required)
Bit 6 CMDREND: Command response received (CRC check passed)
Bit 5 RXOVERR: Received FIFO overrun error
Secure digital input/output interface (SDIO) RM0008
588/1093 Doc ID 13902 Rev 13
22.9.12 SDIO interrupt clear register (SDIO_ICR)
Address offset: 0x38
Reset value: 0x0000 0000
The SDIO_ICR register is a write-only register. Writing a bit with 1b clears the
corresponding bit in the SDIO_STA Status register.
Bit 4 TXUNDERR: Transmit FIFO underrun error
Bit 3 DTIMEOUT: Data timeout
Bit 2 CTIMEOUT: Command response timeout
The Command TimeOut period has a fixed value of 64 SDIO_CK clock periods.
Bit 1 DCRCFAIL: Data block sent/received (CRC check failed)
Bit 0 CCRCFAIL: Command response received (CRC check failed)
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
C
E
A
T
A
E
N
D
C
S
D
I
O
I
T
C
Reserved
D
B
C
K
E
N
D
C
S
T
B
I
T
E
R
R
C
D
A
T
A
E
N
D
C
C
M
D
S
E
N
T
C
C
M
D
R
E
N
D
C
R
X
O
V
E
R
R
C
T
X
U
N
D
E
R
R
C
D
T
I
M
E
O
U
T
C
C
T
I
M
E
O
U
T
C
D
C
R
C
F
A
I
L
C
C
C
R
C
F
A
I
L
C
rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:24 Reserved, must be kept at reset value.
Bit 23 CEATAENDC: CEATAEND flag clear bit
Set by software to clear the CEATAEND flag.
0: CEATAEND not cleared
1: CEATAEND cleared
Bit 22 SDIOITC: SDIOIT flag clear bit
Set by software to clear the SDIOIT flag.
0: SDIOIT not cleared
1: SDIOIT cleared
Bits 21:11 Reserved, must be kept at reset value.
Bit 10 DBCKENDC: DBCKEND flag clear bit
Set by software to clear the DBCKEND flag.
0: DBCKEND not cleared
1: DBCKEND cleared
Bit 9 STBITERRC: STBITERR flag clear bit
Set by software to clear the STBITERR flag.
0: STBITERR not cleared
1: STBITERR cleared
Bit 8 DATAENDC: DATAEND flag clear bit
Set by software to clear the DATAEND flag.
0: DATAEND not cleared
1: DATAEND cleared
RM0008 Secure digital input/output interface (SDIO)
Doc ID 13902 Rev 13 589/1093
Bit 7 CMDSENTC: CMDSENT flag clear bit
Set by software to clear the CMDSENT flag.
0: CMDSENT not cleared
1: CMDSENT cleared
Bit 6 CMDRENDC: CMDREND flag clear bit
Set by software to clear the CMDREND flag.
0: CMDREND not cleared
1: CMDREND cleared
Bit 5 RXOVERRC: RXOVERR flag clear bit
Set by software to clear the RXOVERR flag.
0: RXOVERR not cleared
1: RXOVERR cleared
Bit 4 TXUNDERRC: TXUNDERR flag clear bit
Set by software to clear TXUNDERR flag.
0: TXUNDERR not cleared
1: TXUNDERR cleared
Bit 3 DTIMEOUTC: DTIMEOUT flag clear bit
Set by software to clear the DTIMEOUT flag.
0: DTIMEOUT not cleared
1: DTIMEOUT cleared
Bit 2 CTIMEOUTC: CTIMEOUT flag clear bit
Set by software to clear the CTIMEOUT flag.
0: CTIMEOUT not cleared
1: CTIMEOUT cleared
Bit 1 DCRCFAILC: DCRCFAIL flag clear bit
Set by software to clear the DCRCFAIL flag.
0: DCRCFAIL not cleared
1: DCRCFAIL cleared
Bit 0 CCRCFAILC: CCRCFAIL flag clear bit
Set by software to clear the CCRCFAIL flag.
0: CCRCFAIL not cleared
1: CCRCFAIL cleared
Secure digital input/output interface (SDIO) RM0008
590/1093 Doc ID 13902 Rev 13
22.9.13 SDIO mask register (SDIO_MASK)
Address offset: 0x3C
Reset value: 0x0000 0000
The interrupt mask register determines which status flags generate an interrupt request by
setting the corresponding bit to 1b.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
C
E
A
T
A
E
N
D
I
E
S
D
I
O
I
T
I
E
R
X
D
A
V
L
I
E
T
X
D
A
V
L
I
E
R
X
F
I
F
O
E
I
E
T
X
F
I
F
O
E
I
E
R
X
F
I
F
O
F
I
E
T
X
F
I
F
O
F
I
E
R
X
F
I
F
O
H
F
I
E
T
X
F
I
F
O
H
E
I
E
R
X
A
C
T
I
E
T
X
A
C
T
I
E
C
M
D
A
C
T
I
E
D
B
C
K
E
N
D
I
E
S
T
B
I
T
E
R
R
I
E
D
A
T
A
E
N
D
I
E
C
M
D
S
E
N
T
I
E
C
M
D
R
E
N
D
I
E
R
X
O
V
E
R
R
I
E
T
X
U
N
D
E
R
R
I
E
D
T
I
M
E
O
U
T
I
E
C
T
I
M
E
O
U
T
I
E
D
C
R
C
F
A
I
L
I
E
C
C
R
C
F
A
I
L
I
E
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:24 Reserved, must be kept at reset value.
Bit 23 CEATAENDIE: CE-ATA command completion signal received interrupt enable
Set and cleared by software to enable/disable the interrupt generated when receiving the
CE-ATA command completion signal.
0: CE-ATA command completion signal received interrupt disabled
1: CE-ATA command completion signal received interrupt enabled
Bit 22 SDIOITIE: SDIO mode interrupt received interrupt enable
Set and cleared by software to enable/disable the interrupt generated when receiving the
SDIO mode interrupt.
0: SDIO Mode Interrupt Received interrupt disabled
1: SDIO Mode Interrupt Received interrupt enabled
Bit 21 RXDAVLIE: Data available in Rx FIFO interrupt enable
Set and cleared by software to enable/disable the interrupt generated by the presence of
data available in Rx FIFO.
0: Data available in Rx FIFO interrupt disabled
1: Data available in Rx FIFO interrupt enabled
Bit 20 TXDAVLIE: Data available in Tx FIFO interrupt enable
Set and cleared by software to enable/disable the interrupt generated by the presence of
data available in Tx FIFO.
0: Data available in Tx FIFO interrupt disabled
1: Data available in Tx FIFO interrupt enabled
Bit 19 RXFIFOEIE: Rx FIFO empty interrupt enable
Set and cleared by software to enable/disable interrupt caused by Rx FIFO empty.
0: Rx FIFO empty interrupt disabled
1: Rx FIFO empty interrupt enabled
Bit 18 TXFIFOEIE: Tx FIFO empty interrupt enable
Set and cleared by software to enable/disable interrupt caused by Tx FIFO empty.
0: Tx FIFO empty interrupt disabled
1: Tx FIFO empty interrupt enabled
Bit 17 RXFIFOFIE: Rx FIFO full interrupt enable
Set and cleared by software to enable/disable interrupt caused by Rx FIFO full.
0: Rx FIFO full interrupt disabled
1: Rx FIFO full interrupt enabled
RM0008 Secure digital input/output interface (SDIO)
Doc ID 13902 Rev 13 591/1093
Bit 16 TXFIFOFIE: Tx FIFO full interrupt enable
Set and cleared by software to enable/disable interrupt caused by Tx FIFO full.
0: Tx FIFO full interrupt disabled
1: Tx FIFO full interrupt enabled
Bit 15 RXFIFOHFIE: Rx FIFO half full interrupt enable
Set and cleared by software to enable/disable interrupt caused by Rx FIFO half full.
0: Rx FIFO half full interrupt disabled
1: Rx FIFO half full interrupt enabled
Bit 14 TXFIFOHEIE: Tx FIFO half empty interrupt enable
Set and cleared by software to enable/disable interrupt caused by Tx FIFO half empty.
0: Tx FIFO half empty interrupt disabled
1: Tx FIFO half empty interrupt enabled
Bit 13 RXACTIE: Data receive acting interrupt enable
Set and cleared by software to enable/disable interrupt caused by data being received (data
receive acting).
0: Data receive acting interrupt disabled
1: Data receive acting interrupt enabled
Bit 12 TXACTIE: Data transmit acting interrupt enable
Set and cleared by software to enable/disable interrupt caused by data being transferred
(data transmit acting).
0: Data transmit acting interrupt disabled
1: Data transmit acting interrupt enabled
Bit 11 CMDACTIE: Command acting interrupt enable
Set and cleared by software to enable/disable interrupt caused by a command being
transferred (command acting).
0: Command acting interrupt disabled
1: Command acting interrupt enabled
Bit 10 DBCKENDIE: Data block end interrupt enable
Set and cleared by software to enable/disable interrupt caused by data block end.
0: Data block end interrupt disabled
1: Data block end interrupt enabled
Bit 9 STBITERRIE: Start bit error interrupt enable
Set and cleared by software to enable/disable interrupt caused by start bit error.
0: Start bit error interrupt disabled
1: Start bit error interrupt enabled
Bit 8 DATAENDIE: Data end interrupt enable
Set and cleared by software to enable/disable interrupt caused by data end.
0: Data end interrupt disabled
1: Data end interrupt enabled
Bit 7 CMDSENTIE: Command sent interrupt enable
Set and cleared by software to enable/disable interrupt caused by sending command.
0: Command sent interrupt disabled
1: Command sent interrupt enabled
Secure digital input/output interface (SDIO) RM0008
592/1093 Doc ID 13902 Rev 13
22.9.14 SDIO FIFO counter register (SDIO_FIFOCNT)
Address offset: 0x48
Reset value: 0x0000 0000
The SDIO_FIFOCNT register contains the remaining number of words to be written to or
read from the FIFO. The FIFO counter loads the value from the data length register (see
SDIO_DLEN) when the data transfer enable bit, DTEN, is set in the data control register
(SDIO_DCTRL register) and the DPSM is at the Idle state. If the data length is not word-
aligned (multiple of 4), the remaining 1 to 3 bytes are regarded as a word.
Bit 6 CMDRENDIE: Command response received interrupt enable
Set and cleared by software to enable/disable interrupt caused by receiving command
response.
0: Command response received interrupt disabled
1: command Response Received interrupt enabled
Bit 5 RXOVERRIE: Rx FIFO overrun error interrupt enable
Set and cleared by software to enable/disable interrupt caused by Rx FIFO overrun error.
0: Rx FIFO overrun error interrupt disabled
1: Rx FIFO overrun error interrupt enabled
Bit 4 TXUNDERRIE: Tx FIFO underrun error interrupt enable
Set and cleared by software to enable/disable interrupt caused by Tx FIFO underrun error.
0: Tx FIFO underrun error interrupt disabled
1: Tx FIFO underrun error interrupt enabled
Bit 3 DTIMEOUTIE: Data timeout interrupt enable
Set and cleared by software to enable/disable interrupt caused by data timeout.
0: Data timeout interrupt disabled
1: Data timeout interrupt enabled
Bit 2 CTIMEOUTIE: Command timeout interrupt enable
Set and cleared by software to enable/disable interrupt caused by command timeout.
0: Command timeout interrupt disabled
1: Command timeout interrupt enabled
Bit 1 DCRCFAILIE: Data CRC fail interrupt enable
Set and cleared by software to enable/disable interrupt caused by data CRC failure.
0: Data CRC fail interrupt disabled
1: Data CRC fail interrupt enabled
Bit 0 CCRCFAILIE: Command CRC fail interrupt enable
Set and cleared by software to enable/disable interrupt caused by command CRC failure.
0: Command CRC fail interrupt disabled
1: Command CRC fail interrupt enabled
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
FIFOCOUNT
r r r r r r r r r r r r r r r r r r r r r r r r
Bits 31:24 Reserved, must be kept at reset value.
Bits 23:0 FIFOCOUNT: Remaining number of words to be written to or read from the FIFO.
RM0008 Secure digital input/output interface (SDIO)
Doc ID 13902 Rev 13 593/1093
22.9.15 SDIO data FIFO register (SDIO_FIFO)
Address offset: 0x80
Reset value: 0x0000 0000
The receive and transmit FIFOs can be read or written as 32-bit wide registers. The FIFOs
contain 32 entries on 32 sequential addresses. This allows the CPU to use its load and store
multiple operands to read from/write to the FIFO.
22.9.16 SDIO register map
The following table summarizes the SDIO registers.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
FIF0Data
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
bits 31:0 FIFOData: Receive and transmit FIFO data
The FIFO data occupies 32 entries of 32-bit words, from address:
SDIO base + 0x080 to SDIO base + 0xFC.
Table 168. SDIO register map
Offset Register
3
1
3
0
2
9
2
8
2
7
2
6
2
5
2
4
2
3
2
2
2
1
2
0
1
9
1
8
1
7
1
6
1
5
1
4
1
3
1
2
1
1
1
0
9 8 7 6 5 4 3 2 1 0
0x00 SDIO_POWER
R
e
s
e
r
v
e
d
P
W
R
C
T
R
L
0x04 SDIO_CLKCR
R
e
s
e
r
v
e
d
H
W
F
C
_
E
N
N
E
G
E
D
G
E
W
I
D
B
U
S
B
Y
P
A
S
S
P
W
R
S
A
V
C
L
K
E
N
C
L
K
D
I
V
0x08 SDIO_ARG CMDARG
0x0C SDIO_CMD
R
e
s
e
r
v
e
d
C
E
-
A
T
A
C
M
D
n
I
E
N
E
N
C
M
D
c
o
m
p
l
S
D
I
O
S
u
s
p
e
n
d
C
P
S
M
E
N
W
A
I
T
P
E
N
D
W
A
I
T
I
N
T
W
A
I
T
R
E
S
P
C
M
D
I
N
D
E
X
0x10
SDIO_RESPCM
D
Reserved RESPCMD
0x14 SDIO_RESP1 CARDSTATUS1
0x18 SDIO_RESP2 CARDSTATUS2
0x1C SDIO_RESP3 CARDSTATUS3
0x20 SDIO_RESP4 CARDSTATUS4
0x24 SDIO_DTIMER DATATIME
0x28 SDIO_DLEN Reserved DATALENGTH
0x2C SDIO_DCTRL
R
e
s
e
r
v
e
d
S
D
I
O
E
N
R
W
M
O
D
R
W
S
T
O
P
R
W
S
T
A
R
T
D
B
L
O
C
K
S
I
Z
E
D
M
A
E
N
D
T
M
O
D
E
D
T
D
I
R
D
T
E
N
0x30 SDIO_DCOUNT Reserved DATACOUNT
0x34 SDIO_STA
R
e
s
e
r
v
e
d
C
E
A
T
A
E
N
D
S
D
I
O
I
T
R
X
D
A
V
L
T
X
D
A
V
L
R
X
F
I
F
O
E
T
X
F
I
F
O
E
R
X
F
I
F
O
F
T
X
F
I
F
O
F
R
X
F
I
F
O
H
F
T
X
F
I
F
O
H
E
R
X
A
C
T
T
X
A
C
T
C
M
D
A
C
T
D
B
C
K
E
N
D
S
T
B
I
T
E
R
R
D
A
T
A
E
N
D
C
M
D
S
E
N
T
C
M
D
R
E
N
D
R
X
O
V
E
R
R
T
X
U
N
D
E
R
R
D
T
I
M
E
O
U
T
C
T
I
M
E
O
U
T
D
C
R
C
F
A
I
L
C
C
R
C
F
A
I
L
Secure digital input/output interface (SDIO) RM0008
594/1093 Doc ID 13902 Rev 13
Refer to Table 3 on page 50 for the register boundary addresses.
0x38 SDIO_ICR
R
e
s
e
r
v
e
d
C
E
A
T
A
E
N
D
C
S
D
I
O
I
T
C
R
e
s
e
r
v
e
d
D
B
C
K
E
N
D
C
S
T
B
I
T
E
R
R
C
D
A
T
A
E
N
D
C
C
M
D
S
E
N
T
C
C
M
D
R
E
N
D
C
R
X
O
V
E
R
R
C
T
X
U
N
D
E
R
R
C
D
T
I
M
E
O
U
T
C
C
T
I
M
E
O
U
T
C
D
C
R
C
F
A
I
L
C
C
C
R
C
F
A
I
L
C
0x3C SDIO_MASK
R
e
s
e
r
v
e
d
C
E
A
T
A
E
N
D
I
E
S
D
I
O
I
T
I
E
R
X
D
A
V
L
I
E
T
X
D
A
V
L
I
E
R
X
F
I
F
O
E
I
E
T
X
F
I
F
O
E
I
E
R
X
F
I
F
O
F
I
E
T
X
F
I
F
O
F
I
E
R
X
F
I
F
O
H
F
I
E
T
X
F
I
F
O
H
E
I
E
R
X
A
C
T
I
E
T
X
A
C
T
I
E
C
M
D
A
C
T
I
E
D
B
C
K
E
N
D
I
E
S
T
B
I
T
E
R
R
I
E
D
A
T
A
E
N
D
I
E
C
M
D
S
E
N
T
I
E
C
M
D
R
E
N
D
I
E
R
X
O
V
E
R
R
I
E
T
X
U
N
D
E
R
R
I
E
D
T
I
M
E
O
U
T
I
E
C
T
I
M
E
O
U
T
I
E
D
C
R
C
F
A
I
L
I
E
C
C
R
C
F
A
I
L
I
E
0x48 SDIO_FIFOCNT Reserved FIFOCOUNT
0x80 SDIO_FIFO FIF0Data
Table 168. SDIO register map (continued)
Offset Register
3
1
3
0
2
9
2
8
2
7
2
6
2
5
2
4
2
3
2
2
2
1
2
0
1
9
1
8
1
7
1
6
1
5
1
4
1
3
1
2
1
1
1
0
9 8 7 6 5 4 3 2 1 0
RM0008 Universal serial bus full-speed device interface (USB)
Doc ID 13902 Rev 13 595/1093
23 Universal serial bus full-speed device interface (USB)
Low-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 16 and 32 Kbytes.
Medium-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 64 and 128 Kbytes.
High-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 256 and 512 Kbytes.
XL-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 768 Kbytes and 1 Mbyte.
Connectivity line devices are STM32F105xx and STM32F107xx microcontrollers.
This section applies to the STM32F103xx performance line and STM32F102xx USB access
line families only.
23.1 USB introduction
The USB peripheral implements an interface between a full-speed USB 2.0 bus and the
APB1 bus.
USB suspend/resume are supported which allows to stop the device clocks for low-power
consumption.
23.2 USB main features
USB specification version 2.0 full-speed compliant
Configurable number of endpoints from 1 to 8
Cyclic redundancy check (CRC) generation/checking, Non-return-to-zero Inverted
(NRZI) encoding/decoding and bit-stuffing
Isochronous transfers support
Double-buffered bulk/isochronous endpoint support
USB Suspend/Resume operations
Frame locked clock pulse generation
Note: In low, medium, high and XL-density devices, the USB and CAN share a dedicated 512-byte
SRAM memory for data transmission and reception, and so they cannot be used
concurrently (the shared RAM is accessed through CAN and USB exclusively). The USB
and CAN can be used in the same application but not at the same time.
23.3 USB functional description
Figure 219 shows the block diagram of the USB peripheral.
Universal serial bus full-speed device interface (USB) RM0008
596/1093 Doc ID 13902 Rev 13
Figure 219. USB peripheral block diagram
The USB peripheral provides an USB compliant connection between the host PC and the
function implemented by the microcontroller. Data transfer between the host PC and the
system memory occurs through a dedicated packet buffer memory accessed directly by the
USB peripheral. The size of this dedicated buffer memory must be according to the number
of endpoints used and the maximum packet size. This dedicated memory is sized to 512
bytes and up to 16 mono-directional or 8 bidirectional endpoints can be used.The USB
peripheral interfaces with the USB host, detecting token packets, handling data
transmission/reception, and processing handshake packets as required by the USB
standard. Transaction formatting is performed by the hardware, including CRC generation
and checking.
Arbiter
Packet
buffer
memory
Register
mapper
Interrupt
mapper
APB1 wrapper
Suspend
timer
Packet
buffer
interface
USB
RX-TX
Clock
recovery
Control
Endpoint
selection
S.I.E.
Control
registers & logic
Interrupt
registers & logic
Analog
Endpoint
registers
DP DM
transceiver
Endpoint
registers
PCLK1 APB1 bus IRQs to NVIC
USB clock (48 MHz)
PCLK1
APB1 interface
RM0008 Universal serial bus full-speed device interface (USB)
Doc ID 13902 Rev 13 597/1093
Each endpoint is associated with a buffer description block indicating where the endpoint
related memory area is located, how large it is or how many bytes must be transmitted.
When a token for a valid function/endpoint pair is recognized by the USB peripheral, the
related data transfer (if required and if the endpoint is configured) takes place. The data
buffered by the USB peripheral is loaded in an internal 16 bit register and memory access to
the dedicated buffer is performed. When all the data has been transferred, if needed, the
proper handshake packet over the USB is generated or expected according to the direction
of the transfer.
At the end of the transaction, an endpoint-specific interrupt is generated, reading status
registers and/or using different interrupt response routines. The microcontroller can
determine:
Which endpoint has to be served
Which type of transaction took place, if errors occurred (bit stuffing, format, CRC,
protocol, missing ACK, over/underrun, etc.)
Special support is offered to Isochronous transfers and high throughput bulk transfers,
implementing a double buffer usage, which allows to always have an available buffer for the
USB peripheral while the microcontroller uses the other one.
The unit can be placed in low-power mode (SUSPEND mode), by writing in the control
register, whenever required. At this time, all static power dissipation is avoided, and the USB
clock can be slowed down or stopped. The detection of activity at the USB inputs, while in
low-power mode, wakes the device up asynchronously. A special interrupt source can be
connected directly to a wakeup line to allow the system to immediately restart the normal
clock generation and/or support direct clock start/stop.
23.3.1 Description of USB blocks
The USB peripheral implements all the features related to USB interfacing, which include
the following blocks:
Serial Interface Engine (SIE): The functions of this block include: synchronization
pattern recognition, bit-stuffing, CRC generation and checking, PID
verification/generation, and handshake evaluation. It must interface with the USB
transceivers and uses the virtual buffers provided by the packet buffer interface for local
data storage,. This unit also generates signals according to USB peripheral events,
such as Start of Frame (SOF), USB_Reset, Data errors etc. and to Endpoint related
events like end of transmission or correct reception of a packet; these signals are then
used to generate interrupts.
Timer: This block generates a start-of-frame locked clock pulse and detects a global
suspend (from the host) when no traffic has been received for 3 ms.
Packet Buffer Interface: This block manages the local memory implementing a set of
buffers in a flexible way, both for transmission and reception. It can choose the proper
buffer according to requests coming from the SIE and locate them in the memory
addresses pointed by the Endpoint registers. It increments the address after each
exchanged word until the end of packet, keeping track of the number of exchanged
bytes and preventing the buffer to overrun the maximum capacity.
Endpoint-Related Registers: Each endpoint has an associated register containing the
endpoint type and its current status. For mono-directional/single-buffer endpoints, a
single register can be used to implement two distinct endpoints. The number of
registers is 8, allowing up to 16 mono-directional/single-buffer or up to 7 double-buffer
endpoints* in any combination. For example the USB peripheral can be programmed to
have 4 double buffer endpoints and 8 single-buffer/mono-directional endpoints.
Universal serial bus full-speed device interface (USB) RM0008
598/1093 Doc ID 13902 Rev 13
Control Registers: These are the registers containing information about the status of
the whole USB peripheral and used to force some USB events, such as resume and
power-down.
Interrupt Registers: These contain the Interrupt masks and a record of the events. They
can be used to inquire an interrupt reason, the interrupt status or to clear the status of a
pending interrupt.
Note: * Endpoint 0 is always used for control transfer in single-buffer mode.
The USB peripheral is connected to the APB1 bus through an APB1 interface, containing
the following blocks:
Packet Memory: This is the local memory that physically contains the Packet Buffers. It
can be used by the Packet Buffer interface, which creates the data structure and can be
accessed directly by the application software. The size of the Packet Memory is 512
bytes, structured as 256 words by 16 bits.
Arbiter: This block accepts memory requests coming from the APB1 bus and from the
USB interface. It resolves the conflicts by giving priority to APB1 accesses, while
always reserving half of the memory bandwidth to complete all USB transfers. This
time-duplex scheme implements a virtual dual-port SRAM that allows memory access,
while an USB transaction is happening. Multiword APB1 transfers of any length are
also allowed by this scheme.
Register Mapper: This block collects the various byte-wide and bit-wide registers of the
USB peripheral in a structured 16-bit wide word set addressed by the APB1.
APB1 Wrapper: This provides an interface to the APB1 for the memory and register. It
also maps the whole USB peripheral in the APB1 address space.
Interrupt Mapper: This block is used to select how the possible USB events can
generate interrupts and map them to three different lines of the NVIC:
USB low-priority interrupt (Channel 20): Triggered by all USB events (Correct
transfer, USB reset, etc.). The firmware has to check the interrupt source before
serving the interrupt.
USB high-priority interrupt (Channel 19): Triggered only by a correct transfer event
for isochronous and double-buffer bulk transfer to reach the highest possible
transfer rate.
USB wakeup interrupt (Channel 42): Triggered by the wakeup event from the USB
Suspend mode.
23.4 Programming considerations
In the following sections, the expected interactions between the USB peripheral and the
application program are described, in order to ease application software development.
23.4.1 Generic USB device programming
This part describes the main tasks required of the application software in order to obtain
USB compliant behavior. The actions related to the most general USB events are taken into
account and paragraphs are dedicated to the special cases of double-buffered endpoints
and Isochronous transfers. Apart from system reset, action is always initiated by the USB
peripheral, driven by one of the USB events described below.
RM0008 Universal serial bus full-speed device interface (USB)
Doc ID 13902 Rev 13 599/1093
23.4.2 System and power-on reset
Upon system and power-on reset, the first operation the application software should perform
is to provide all required clock signals to the USB peripheral and subsequently de-assert its
reset signal so to be able to access its registers. The whole initialization sequence is
hereafter described.
As a first step application software needs to activate register macrocell clock and de-assert
macrocell specific reset signal using related control bits provided by device clock
management logic.
After that, the analog part of the device related to the USB transceiver must be switched on
using the PDWN bit in CNTR register, which requires a special handling. This bit is intended
to switch on the internal voltage references that supply the port transceiver. This circuit has
a defined startup time (t
STARTUP
specified in the datasheet) during which the behavior of the
USB transceiver is not defined. It is thus necessary to wait this time, after setting the PDWN
bit in the CNTR register, before removing the reset condition on the USB part (by clearing
the FRES bit in the CNTR register). Clearing the ISTR register then removes any spurious
pending interrupt before any other macrocell operation is enabled.
At system reset, the microcontroller must initialize all required registers and the packet
buffer description table, to make the USB peripheral able to properly generate interrupts and
data transfers. All registers not specific to any endpoint must be initialized according to the
needs of application software (choice of enabled interrupts, chosen address of packet
buffers, etc.). Then the process continues as for the USB reset case (see further
paragraph).
USB reset (RESET interrupt)
When this event occurs, the USB peripheral is put in the same conditions it is left by the
system reset after the initialization described in the previous paragraph: communication is
disabled in all endpoint registers (the USB peripheral will not respond to any packet). As a
response to the USB reset event, the USB function must be enabled, having as USB
address 0, implementing only the default control endpoint (endpoint address is 0 too). This
is accomplished by setting the Enable Function (EF) bit of the USB_DADDR register and
initializing the EP0R register and its related packet buffers accordingly. During USB
enumeration process, the host assigns a unique address to this device, which must be
written in the ADD[6:0] bits of the USB_DADDR register, and configures any other
necessary endpoint.
When a RESET interrupt is received, the application software is responsible to enable again
the default endpoint of USB function 0 within 10mS from the end of reset sequence which
triggered the interrupt.
Structure and usage of packet buffers
Each bidirectional endpoint may receive or transmit data from/to the host. The received data
is stored in a dedicated memory buffer reserved for that endpoint, while another memory
buffer contains the data to be transmitted by the endpoint. Access to this memory is
performed by the packet buffer interface block, which delivers a memory access request and
waits for its acknowledgement. Since the packet buffer memory has to be accessed by the
microcontroller also, an arbitration logic takes care of the access conflicts, using half APB1
cycle for microcontroller access and the remaining half for the USB peripheral access. In
this way, both the agents can operate as if the packet memory is a dual-port SRAM, without
being aware of any conflict even when the microcontroller is performing back-to-back
Universal serial bus full-speed device interface (USB) RM0008
600/1093 Doc ID 13902 Rev 13
accesses. The USB peripheral logic uses a dedicated clock. The frequency of this dedicated
clock is fixed by the requirements of the USB standard at 48 MHz, and this can be different
from the clock used for the interface to the APB1 bus. Different clock configurations are
possible where the APB1 clock frequency can be higher or lower than the USB peripheral
one.
Note: Due to USB data rate and packet memory interface requirements, the APB1 clock frequency
must be greater than 8 MHz to avoid data overrun/underrun problems.
Each endpoint is associated with two packet buffers (usually one for transmission and the
other one for reception). Buffers can be placed anywhere inside the packet memory
because their location and size is specified in a buffer description table, which is also
located in the packet memory at the address indicated by the USB_BTABLE register. Each
table entry is associated to an endpoint register and it is composed of four 16-bit words so
that table start address must always be aligned to an 8-byte boundary (the lowest three bits
of USB_BTABLE register are always 000). Buffer descriptor table entries are described in
the Section 23.5.3: Buffer descriptor table. If an endpoint is unidirectional and it is neither an
Isochronous nor a double-buffered bulk, only one packet buffer is required (the one related
to the supported transfer direction). Other table locations related to unsupported transfer
directions or unused endpoints, are available to the user. Isochronous and double-buffered
bulk endpoints have special handling of packet buffers (Refer to Section 23.4.4: Isochronous
transfers and Section 23.4.3: Double-buffered endpoints respectively). The relationship
between buffer description table entries and packet buffer areas is depicted in Figure 220.
Figure 220. Packet buffer areas with examples of buffer description table locations
Buffer for
double-buffered
IN Endpoint 3
ADDR0_TX
COUNT0_TX
0000_0000 (00)
ADDR0_RX
COUNT0_RX
ADDR1_TX
COUNT1_TX
ADDR1_RX
COUNT1_RX
ADDR2_RX_0
COUNT2_RX_0
ADDR2_RX_1
COUNT2_RX_1
ADDR3_TX_0
COUNT3_TX_0
0000_0010 (02)
0000_0100 (04)
0000_0110 (06)
0000_1000 (08)
0000_1010 (0A)
0000_1100 (0C)
0000_1110 (0E)
0001_0000 (10)
0001_0010 (12)
0001_0100 (14)
0001_0110 (16)
0001_1000 (18)
0001_1010 (1A)
Buffer description table locations
Transmission
buffer for
Endpoint 0
Reception buffer
for
Endpoint 0
Transmission
buffer for
single-buffered
Endpoint 1
Packet buffers
ADDR3_TX_1
COUNT3_TX_1
0001_1100 (1C)
0001_1110 (1E)
Buffer for
double-buffered
OUT Endpoint 2
ai17109
RM0008 Universal serial bus full-speed device interface (USB)
Doc ID 13902 Rev 13 601/1093
Each packet buffer is used either during reception or transmission starting from the bottom.
The USB peripheral will never change the contents of memory locations adjacent to the
allocated memory buffers; if a packet bigger than the allocated buffer length is received
(buffer overrun condition) the data will be copied to the memory only up to the last available
location.
Endpoint initialization
The first step to initialize an endpoint is to write appropriate values to the
ADDRn_TX/ADDRn_RX registers so that the USB peripheral finds the data to be
transmitted already available and the data to be received can be buffered. The EP_TYPE
bits in the USB_EPnR register must be set according to the endpoint type, eventually using
the EP_KIND bit to enable any special required feature. On the transmit side, the endpoint
must be enabled using the STAT_TX bits in the USB_EPnR register and COUNTn_TX must
be initialized. For reception, STAT_RX bits must be set to enable reception and
COUNTn_RX must be written with the allocated buffer size using the BL_SIZE and
NUM_BLOCK fields. Unidirectional endpoints, except Isochronous and double-buffered bulk
endpoints, need to initialize only bits and registers related to the supported direction. Once
the transmission and/or reception are enabled, register USB_EPnR and locations
ADDRn_TX/ADDRn_RX, COUNTn_TX/COUNTn_RX (respectively), should not be modified
by the application software, as the hardware can change their value on the fly. When the
data transfer operation is completed, notified by a CTR interrupt event, they can be
accessed again to re-enable a new operation.
IN packets (data transmission)
When receiving an IN token packet, if the received address matches a configured and valid
endpoint one, the USB peripheral accesses the contents of ADDRn_TX and COUNTn_TX
locations inside buffer descriptor table entry related to the addressed endpoint. The content
of these locations is stored in its internal 16 bit registers ADDR and COUNT (not accessible
by software). The packet memory is accessed again to read the first word to be transmitted
(Refer to Structure and usage of packet buffers on page 599) and starts sending a DATA0 or
DATA1 PID according to USB_EPnR bit DTOG_TX. When the PID is completed, the first
byte from the word, read from buffer memory, is loaded into the output shift register to be
transmitted on the USB bus. After the last data byte is transmitted, the computed CRC is
sent. If the addressed endpoint is not valid, a NAK or STALL handshake packet is sent
instead of the data packet, according to STAT_TX bits in the USB_EPnR register.
The ADDR internal register is used as a pointer to the current buffer memory location while
COUNT is used to count the number of remaining bytes to be transmitted. Each word read
from the packet buffer memory is transmitted over the USB bus starting from the least
significant byte. Transmission buffer memory is read starting from the address pointed by
ADDRn_TX for COUNTn_TX/2 words. If a transmitted packet is composed of an odd
number of bytes, only the lower half of the last word accessed will be used.
On receiving the ACK receipt by the host, the USB_EPnR register is updated in the following
way: DTOG_TX bit is toggled, the endpoint is made invalid by setting STAT_TX=10 (NAK)
and bit CTR_TX is set. The application software must first identify the endpoint, which is
requesting microcontroller attention by examining the EP_ID and DIR bits in the USB_ISTR
register. Servicing of the CTR_TX event starts clearing the interrupt bit; the application
software then prepares another buffer full of data to be sent, updates the COUNTn_TX table
location with the number of byte to be transmitted during the next transfer, and finally sets
STAT_TX to 11 (VALID) to re-enable transmissions. While the STAT_TX bits are equal to 10
(NAK), any IN request addressed to that endpoint is NAKed, indicating a flow control
Universal serial bus full-speed device interface (USB) RM0008
602/1093 Doc ID 13902 Rev 13
condition: the USB host will retry the transaction until it succeeds. It is mandatory to execute
the sequence of operations in the above mentioned order to avoid losing the notification of a
second IN transaction addressed to the same endpoint immediately following the one which
triggered the CTR interrupt.
OUT and SETUP packets (data reception)
These two tokens are handled by the USB peripheral more or less in the same way; the
differences in the handling of SETUP packets are detailed in the following paragraph about
control transfers. When receiving an OUT/SETUP PID, if the address matches a valid
endpoint, the USB peripheral accesses the contents of the ADDRn_RX and COUNTn_RX
locations inside the buffer descriptor table entry related to the addressed endpoint. The
content of the ADDRn_RX is stored directly in its internal register ADDR. While COUNT is
now reset and the values of BL_SIZE and NUM_BLOCK bit fields, which are read within
COUNTn_RX content are used to initialize BUF_COUNT, an internal 16 bit counter, which is
used to check the buffer overrun condition (all these internal registers are not accessible by
software). Data bytes subsequently received by the USB peripheral are packed in words
(the first byte received is stored as least significant byte) and then transferred to the packet
buffer starting from the address contained in the internal ADDR register while BUF_COUNT
is decremented and COUNT is incremented at each byte transfer. When the end of DATA
packet is detected, the correctness of the received CRC is tested and only if no errors
occurred during the reception, an ACK handshake packet is sent back to the transmitting
host.
In case of wrong CRC or other kinds of errors (bit-stuff violations, frame errors, etc.), data
bytes are still copied in the packet memory buffer, at least until the error detection point, but
ACK packet is not sent and the ERR bit in USB_ISTR register is set. However, there is
usually no software action required in this case: the USB peripheral recovers from reception
errors and remains ready for the next transaction to come. If the addressed endpoint is not
valid, a NAK or STALL handshake packet is sent instead of the ACK, according to bits
STAT_RX in the USB_EPnR register and no data is written in the reception memory buffers.
Reception memory buffer locations are written starting from the address contained in the
ADDRn_RX for a number of bytes corresponding to the received data packet length, CRC
included (i.e. data payload length + 2), or up to the last allocated memory location, as
defined by BL_SIZE and NUM_BLOCK, whichever comes first. In this way, the USB
peripheral never writes beyond the end of the allocated reception memory buffer area. If the
length of the data packet payload (actual number of bytes used by the application) is greater
than the allocated buffer, the USB peripheral detects a buffer overrun condition. in this case,
a STALL handshake is sent instead of the usual ACK to notify the problem to the host, no
interrupt is generated and the transaction is considered failed.
When the transaction is completed correctly, by sending the ACK handshake packet, the
internal COUNT register is copied back in the COUNTn_RX location inside the buffer
description table entry, leaving unaffected BL_SIZE and NUM_BLOCK fields, which
normally do not require to be re-written, and the USB_EPnR register is updated in the
following way: DTOG_RX bit is toggled, the endpoint is made invalid by setting STAT_RX =
10 (NAK) and bit CTR_RX is set. If the transaction has failed due to errors or buffer overrun
condition, none of the previously listed actions take place. The application software must
first identify the endpoint, which is requesting microcontroller attention by examining the
EP_ID and DIR bits in the USB_ISTR register. The CTR_RX event is serviced by first
determining the transaction type (SETUP bit in the USB_EPnR register); the application
software must clear the interrupt flag bit and get the number of received bytes reading the
COUNTn_RX location inside the buffer description table entry related to the endpoint being
RM0008 Universal serial bus full-speed device interface (USB)
Doc ID 13902 Rev 13 603/1093
processed. After the received data is processed, the application software should set the
STAT_RX bits to 11 (Valid) in the USB_EPnR, enabling further transactions. While the
STAT_RX bits are equal to 10 (NAK), any OUT request addressed to that endpoint is
NAKed, indicating a flow control condition: the USB host will retry the transaction until it
succeeds. It is mandatory to execute the sequence of operations in the above mentioned
order to avoid losing the notification of a second OUT transaction addressed to the same
endpoint following immediately the one which triggered the CTR interrupt.
Control transfers
Control transfers are made of a SETUP transaction, followed by zero or more data stages,
all of the same direction, followed by a status stage (a zero-byte transfer in the opposite
direction). SETUP transactions are handled by control endpoints only and are very similar to
OUT ones (data reception) except that the values of DTOG_TX and DTOG_RX bits of the
addressed endpoint registers are set to 1 and 0 respectively, to initialize the control transfer,
and both STAT_TX and STAT_RX are set to 10 (NAK) to let software decide if subsequent
transactions must be IN or OUT depending on the SETUP contents. A control endpoint must
check SETUP bit in the USB_EPnR register at each CTR_RX event to distinguish normal
OUT transactions from SETUP ones. A USB device can determine the number and direction
of data stages by interpreting the data transferred in the SETUP stage, and is required to
STALL the transaction in the case of errors. To do so, at all data stages before the last, the
unused direction should be set to STALL, so that, if the host reverses the transfer direction
too soon, it gets a STALL as a status stage.
While enabling the last data stage, the opposite direction should be set to NAK, so that, if
the host reverses the transfer direction (to perform the status stage) immediately, it is kept
waiting for the completion of the control operation. If the control operation completes
successfully, the software will change NAK to VALID, otherwise to STALL. At the same time,
if the status stage will be an OUT, the STATUS_OUT (EP_KIND in the USB_EPnR register)
bit should be set, so that an error is generated if a status transaction is performed with not-
zero data. When the status transaction is serviced, the application clears the STATUS_OUT
bit and sets STAT_RX to VALID (to accept a new command) and STAT_TX to NAK (to delay
a possible status stage immediately following the next setup).
Since the USB specification states that a SETUP packet cannot be answered with a
handshake different from ACK, eventually aborting a previously issued command to start the
new one, the USB logic doesnt allow a control endpoint to answer with a NAK or STALL
packet to a SETUP token received from the host.
When the STAT_RX bits are set to 01 (STALL) or 10 (NAK) and a SETUP token is received,
the USB accepts the data, performing the required data transfers and sends back an ACK
handshake. If that endpoint has a previously issued CTR_RX request not yet acknowledged
by the application (i.e. CTR_RX bit is still set from a previously completed reception), the
USB discards the SETUP transaction and does not answer with any handshake packet
regardless of its state, simulating a reception error and forcing the host to send the SETUP
token again. This is done to avoid losing the notification of a SETUP transaction addressed
to the same endpoint immediately following the transaction, which triggered the CTR_RX
interrupt.
23.4.3 Double-buffered endpoints
All different endpoint types defined by the USB standard represent different traffic models,
and describe the typical requirements of different kind of data transfer operations. When
large portions of data are to be transferred between the host PC and the USB function, the
Universal serial bus full-speed device interface (USB) RM0008
604/1093 Doc ID 13902 Rev 13
bulk endpoint type is the most suited model. This is because the host schedules bulk
transactions so as to fill all the available bandwidth in the frame, maximizing the actual
transfer rate as long as the USB function is ready to handle a bulk transaction addressed to
it. If the USB function is still busy with the previous transaction when the next one arrives, it
will answer with a NAK handshake and the host PC will issue the same transaction again
until the USB function is ready to handle it, reducing the actual transfer rate due to the
bandwidth occupied by re-transmissions. For this reason, a dedicated feature called
double-buffering can be used with bulk endpoints.
When double-buffering is activated, data toggle sequencing is used to select, which buffer
is to be used by the USB peripheral to perform the required data transfers, using both
transmission and reception packet memory areas to manage buffer swapping on each
successful transaction in order to always have a complete buffer to be used by the
application, while the USB peripheral fills the other one. For example, during an OUT
transaction directed to a reception double-buffered bulk endpoint, while one buffer is being
filled with new data coming from the USB host, the other one is available for the
microcontroller software usage (the same would happen with a transmission double-
buffered bulk endpoint and an IN transaction).
RM0008 Universal serial bus full-speed device interface (USB)
Doc ID 13902 Rev 13 605/1093
Since the swapped buffer management requires the usage of all 4 buffer description table
locations hosting the address pointer and the length of the allocated memory buffers, the
USB_EPnR registers used to implement double-buffered bulk endpoints are forced to be
used as unidirectional ones. Therefore, only one STAT bit pair must be set at a value
different from 00 (Disabled): STAT_RX if the double-buffered bulk endpoint is enabled for
reception, STAT_TX if the double-buffered bulk endpoint is enabled for transmission. In case
it is required to have double-buffered bulk endpoints enabled both for reception and
transmission, two USB_EPnR registers must be used.
To exploit the double-buffering feature and reach the highest possible transfer rate, the
endpoint flow control structure, described in previous chapters, has to be modified, in order
to switch the endpoint status to NAK only when a buffer conflict occurs between the USB
peripheral and application software, instead of doing it at the end of each successful
transaction. The memory buffer which is currently being used by the USB peripheral is
defined by the DTOG bit related to the endpoint direction: DTOG_RX (bit 14 of USB_EPnR
register) for reception double-buffered bulk endpoints or DTOG_TX (bit 6 of USB_EPnR
register) for transmission double-buffered bulk endpoints. To implement the new flow
control scheme, the USB peripheral should know which packet buffer is currently in use by
the application software, so to be aware of any conflict. Since in the USB_EPnR register,
there are two DTOG bits but only one is used by USB peripheral for data and buffer
sequencing (due to the unidirectional constraint required by double-buffering feature) the
other one can be used by the application software to show which buffer it is currently using.
This new buffer flag is called SW_BUF. In the following table the correspondence between
USB_EPnR register bits and DTOG/SW_BUF definition is explained, for the cases of
transmission and reception double-buffered bulk endpoints.
The memory buffer which is currently being used by the USB peripheral is defined by DTOG
buffer flag, while the buffer currently in use by application software is identified by SW_BUF
buffer flag. The relationship between the buffer flag value and the used packet buffer is the
same in both cases, and it is listed in the following table.
Table 169. Double-buffering buffer flag definition
Buffer flag Transmission endpoint Reception endpoint
DTOG DTOG_TX (USB_EPnRbit 6) DTOG_RX (USB_EPnRbit 14)
SW_BUF USB_EPnR bit 14 USB_EPnR bit 6
Universal serial bus full-speed device interface (USB) RM0008
606/1093 Doc ID 13902 Rev 13
Double-buffering feature for a bulk endpoint is activated by:
Writing EP_TYPE bit field at 00 in its USB_EPnR register, to define the endpoint as a
bulk, and
Setting EP_KIND bit at 1 (DBL_BUF), in the same register.
The application software is responsible for DTOG and SW_BUF bits initialization according
to the first buffer to be used; this has to be done considering the special toggle-only property
that these two bits have. The end of the first transaction occurring after having set
DBL_BUF, triggers the special flow control of double-buffered bulk endpoints, which is used
for all other transactions addressed to this endpoint until DBL_BUF remain set. At the end of
each transaction the CTR_RX or CTR_TX bit of the addressed endpoint USB_EPnR
register is set, depending on the enabled direction. At the same time, the affected DTOG bit
in the USB_EPnR register is hardware toggled making the USB peripheral buffer swapping
completely software independent. Unlike common transactions, and the first one after
DBL_BUF setting, STAT bit pair is not affected by the transaction termination and its value
remains 11 (Valid). However, as the token packet of a new transaction is received, the
actual endpoint status will be masked as 10 (NAK) when a buffer conflict between the USB
peripheral and the application software is detected (this condition is identified by DTOG and
SW_BUF having the same value, see Table 170 on page 606). The application software
responds to the CTR event notification by clearing the interrupt flag and starting any
required handling of the completed transaction. When the application packet buffer usage is
over, the software toggles the SW_BUF bit, writing 1 to it, to notify the USB peripheral about
the availability of that buffer. In this way, the number of NAKed transactions is limited only by
the application elaboration time of a transaction data: if the elaboration time is shorter than
the time required to complete a transaction on the USB bus, no re-transmissions due to flow
control will take place and the actual transfer rate will be limited only by the host PC.
Table 170. Bulk double-buffering memory buffers usage
Endpoint
Type
DTOG SW_BUF
Packet buffer used by USB
Peripheral
Packet buffer used by
Application Software
IN
0 1
ADDRn_TX_0 / COUNTn_TX_0
Buffer description table locations.
ADDRn_TX_1 / COUNTn_TX_1
Buffer description table locations.
1 0
ADDRn_TX_1 / COUNTn_TX_1
Buffer description table locations
ADDRn_TX_0 / COUNTn_TX_0
Buffer description table locations.
0 0 None
(1)
1. Endpoint in NAK Status.
ADDRn_TX_0 / COUNTn_TX_0
Buffer description table locations.
1 1 None
(1)
ADDRn_TX_0 / COUNTn_TX_0
Buffer description table locations.
OUT
0 1
ADDRn_RX_0 / COUNTn_RX_0
Buffer description table locations.
ADDRn_RX_1 / COUNTn_RX_1
Buffer description table locations.
1 0
ADDRn_RX_1 / COUNTn_RX_1
Buffer description table locations.
ADDRn_RX_0 / COUNTn_RX_0
Buffer description table locations.
0 0 None
(1)
ADDRn_RX_0 / COUNTn_RX_0
Buffer description table locations.
1 1 None
(1)
ADDRn_RX_1 / COUNTn_RX_1
Buffer description table locations.
RM0008 Universal serial bus full-speed device interface (USB)
Doc ID 13902 Rev 13 607/1093
The application software can always override the special flow control implemented for
double-buffered bulk endpoints, writing an explicit status different from 11 (Valid) into the
STAT bit pair of the related USB_EPnR register. In this case, the USB peripheral will always
use the programmed endpoint status, regardless of the buffer usage condition.
23.4.4 Isochronous transfers
The USB standard supports full speed peripherals requiring a fixed and accurate data
production/consume frequency, defining this kind of traffic as Isochronous. Typical
examples of this data are: audio samples, compressed video streams, and in general any
sort of sampled data having strict requirements for the accuracy of delivered frequency.
When an endpoint is defined to be isochronous during the enumeration phase, the host
allocates in the frame the required bandwidth and delivers exactly one IN or OUT packet
each frame, depending on endpoint direction. To limit the bandwidth requirements, no re-
transmission of failed transactions is possible for Isochronous traffic; this leads to the fact
that an isochronous transaction does not have a handshake phase and no ACK packet is
expected or sent after the data packet. For the same reason, Isochronous transfers do not
support data toggle sequencing and always use DATA0 PID to start any data packet.
The Isochronous behavior for an endpoint is selected by setting the EP_TYPE bits at 10 in
its USB_EPnR register; since there is no handshake phase the only legal values for the
STAT_RX/STAT_TX bit pairs are 00 (Disabled) and 11 (Valid), any other value will produce
results not compliant to USB standard. Isochronous endpoints implement double-buffering
to ease application software development, using both transmission and reception packet
memory areas to manage buffer swapping on each successful transaction in order to have
always a complete buffer to be used by the application, while the USB peripheral fills the
other.
The memory buffer which is currently used by the USB peripheral is defined by the DTOG
bit related to the endpoint direction (DTOG_RX for reception isochronous endpoints,
DTOG_TX for transmission isochronous endpoints, both in the related USB_EPnR
register) according to Table 171.
Table 171. Isochronous memory buffers usage
Endpoint
Type
DTOG bit
value
Packet buffer used by the
USB peripheral
Packet buffer used by the
application software
IN
0
ADDRn_TX_0 / COUNTn_TX_0
buffer description table
locations.
ADDRn_TX_1 / COUNTn_TX_1
buffer description table
locations.
1
ADDRn_TX_1 / COUNTn_TX_1
buffer description table
locations.
ADDRn_TX_0 / COUNTn_TX_0
buffer description table
locations.
OUT
0
ADDRn_RX_0 / COUNTn_RX_0
buffer description table
locations.
ADDRn_RX_1 / COUNTn_RX_1
buffer description table
locations.
1
ADDRn_RX_1 / COUNTn_RX_1
buffer description table
locations.
ADDRn_RX_0 / COUNTn_RX_0
buffer description table
locations.
Universal serial bus full-speed device interface (USB) RM0008
608/1093 Doc ID 13902 Rev 13
As it happens with double-buffered bulk endpoints, the USB_EPnR registers used to
implement Isochronous endpoints are forced to be used as unidirectional ones. In case it is
required to have Isochronous endpoints enabled both for reception and transmission, two
USB_EPnR registers must be used.
The application software is responsible for the DTOG bit initialization according to the first
buffer to be used; this has to be done considering the special toggle-only property that these
two bits have. At the end of each transaction, the CTR_RX or CTR_TX bit of the addressed
endpoint USB_EPnR register is set, depending on the enabled direction. At the same time,
the affected DTOG bit in the USB_EPnR register is hardware toggled making buffer
swapping completely software independent. STAT bit pair is not affected by transaction
completion; since no flow control is possible for Isochronous transfers due to the lack of
handshake phase, the endpoint remains always 11 (Valid). CRC errors or buffer-overrun
conditions occurring during Isochronous OUT transfers are anyway considered as correct
transactions and they always trigger an CTR_RX event. However, CRC errors will anyway
set the ERR bit in the USB_ISTR register to notify the software of the possible data
corruption.
23.4.5 Suspend/Resume events
The USB standard defines a special peripheral state, called SUSPEND, in which the
average current drawn from the USB bus must not be greater than 500 A. This requirement
is of fundamental importance for bus-powered devices, while self-powered devices are not
required to comply to this strict power consumption constraint. In suspend mode, the host
PC sends the notification to not send any traffic on the USB bus for more than 3mS: since a
SOF packet must be sent every mS during normal operations, the USB peripheral detects
the lack of 3 consecutive SOF packets as a suspend request from the host PC and set the
SUSP bit to 1 in USB_ISTR register, causing an interrupt if enabled. Once the device is
suspended, its normal operation can be restored by a so called RESUME sequence, which
can be started from the host PC or directly from the peripheral itself, but it is always
terminated by the host PC. The suspended USB peripheral must be anyway able to detect a
RESET sequence, reacting to this event as a normal USB reset event.
The actual procedure used to suspend the USB peripheral is device dependent since
according to the device composition, different actions may be required to reduce the total
consumption.
A brief description of a typical suspend procedure is provided below, focused on the USB-
related aspects of the application software routine responding to the SUSP notification of
the USB peripheral:
1. Set the FSUSP bit in the USB_CNTR register to 1. This action activates the suspend
mode within the USB peripheral. As soon as the suspend mode is activated, the check
on SOF reception is disabled to avoid any further SUSP interrupts being issued while
the USB is suspended.
2. Remove or reduce any static power consumption in blocks different from the USB
peripheral.
3. Set LP_MODE bit in USB_CNTR register to 1 to remove static power consumption in
the analog USB transceivers but keeping them able to detect resume activity.
4. Optionally turn off external oscillator and device PLL to stop any activity inside the
device.
RM0008 Universal serial bus full-speed device interface (USB)
Doc ID 13902 Rev 13 609/1093
When an USB event occurs while the device is in SUSPEND mode, the RESUME procedure
must be invoked to restore nominal clocks and regain normal USB behavior. Particular care
must be taken to insure that this process does not take more than 10mS when the wakening
event is an USB reset sequence (See Universal Serial Bus Specification for more details).
The start of a resume or reset sequence, while the USB peripheral is suspended, clears the
LP_MODE bit in USB_CNTR register asynchronously. Even if this event can trigger an
WKUP interrupt if enabled, the use of an interrupt response routine must be carefully
evaluated because of the long latency due to system clock restart; to have the shorter
latency before re-activating the nominal clock it is suggested to put the resume procedure
just after the end of the suspend one, so its code is immediately executed as soon as the
system clock restarts. To prevent ESD discharges or any other kind of noise from waking-up
the system (the exit from suspend mode is an asynchronous event), a suitable analog filter
on data line status is activated during suspend; the filter width is about 70ns.
The following is a list of actions a resume procedure should address:
1. Optionally turn on external oscillator and/or device PLL.
2. Clear FSUSP bit of USB_CNTR register.
3. If the resume triggering event has to be identified, bits RXDP and RXDM in the
USB_FNR register can be used according to Table 172, which also lists the intended
software action in all the cases. If required, the end of resume or reset sequence can
be detected monitoring the status of the above mentioned bits by checking when they
reach the 10 configuration, which represent the Idle bus state; moreover at the end of
a reset sequence the RESET bit in USB_ISTR register is set to 1, issuing an interrupt if
enabled, which should be handled as usual.
A device may require to exit from suspend mode as an answer to particular events not
directly related to the USB protocol (e.g. a mouse movement wakes up the whole system).
In this case, the resume sequence can be started by setting the RESUME bit in the
USB_CNTR register to 1 and resetting it to 0 after an interval between 1mS and 15mS (this
interval can be timed using ESOF interrupts, occurring with a 1mS period when the system
clock is running at nominal frequency). Once the RESUME bit is clear, the resume
sequence will be completed by the host PC and its end can be monitored again using the
RXDP and RXDM bits in the USB_FNR register.
Note: The RESUME bit must be anyway used only after the USB peripheral has been put in
suspend mode, setting the FSUSP bit in USB_CNTR register to 1.
Table 172. Resume event detection
[RXDP,RXDM] status Wakeup event Required resume software action
00 Root reset None
10 None (noise on bus) Go back in Suspend mode
01 Root resume None
11 Not allowed (noise on bus) Go back in Suspend mode
Universal serial bus full-speed device interface (USB) RM0008
610/1093 Doc ID 13902 Rev 13
23.5 USB registers
The USB peripheral registers can be divided into the following groups:
Common Registers: Interrupt and Control registers
Endpoint Registers: Endpoint configuration and status
Buffer Descriptor Table: Location of packet memory used to locate data buffers
All register addresses are expressed as offsets with respect to the USB peripheral registers
base address 0x4000 5C00, except the buffer descriptor table locations, which starts at the
address specified by the USB_BTABLE register. Due to the common limitation of APB1
bridges on word addressability, all register addresses are aligned to 32-bit word boundaries
although they are 16-bit wide. The same address alignment is used to access packet buffer
memory locations, which are located starting from 0x4000 6000.
Refer to Section 2.1 on page 46 for a list of abbreviations used in register descriptions.
The peripheral registers can be accessed by half-words (16-bit) or words (32-bit).
23.5.1 Common registers
These registers affect the general behavior of the USB peripheral defining operating mode,
interrupt handling, device address and giving access to the current frame number updated
by the host PC.
USB control register (USB_CNTR)
Address offset: 0x40
Reset value: 0x0003
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CTRM PMAOVRM ERRM WKUPM SUSPM RESETM SOFM ESOFM
Reserved
RESUME FSUSP LP_MODE PDWN FRES
rw rw rw rw rw rw rw rw rw rw rw rw rw
Bit 15 CTRM: Correct transfer interrupt mask
0: Correct Transfer (CTR) Interrupt disabled.
1: CTR Interrupt enabled, an interrupt request is generated when the corresponding bit in the
USB_ISTR register is set.
Bit 14 PMAOVRM: Packet memory area over / underrun interrupt mask
0: PMAOVR Interrupt disabled.
1: PMAOVR Interrupt enabled, an interrupt request is generated when the corresponding bit
in the USB_ISTR register is set.
Bit 13 ERRM: Error interrupt mask
0: ERR Interrupt disabled.
1: ERR Interrupt enabled, an interrupt request is generated when the corresponding bit in
the USB_ISTR register is set.
Bit 12 WKUPM: Wakeup interrupt mask
0: WKUP Interrupt disabled.
1: WKUP Interrupt enabled, an interrupt request is generated when the corresponding bit in
the USB_ISTR register is set.
RM0008 Universal serial bus full-speed device interface (USB)
Doc ID 13902 Rev 13 611/1093
Bit 11 SUSPM: Suspend mode interrupt mask
0: Suspend Mode Request (SUSP) Interrupt disabled.
1: SUSP Interrupt enabled, an interrupt request is generated when the corresponding bit in
the USB_ISTR register is set.
Bit 10 RESETM: USB reset interrupt mask
0: RESET Interrupt disabled.
1: RESET Interrupt enabled, an interrupt request is generated when the corresponding bit in
the USB_ISTR register is set.
Bit 9 SOFM: Start of frame interrupt mask
0: SOF Interrupt disabled.
1: SOF Interrupt enabled, an interrupt request is generated when the corresponding bit in the
USB_ISTR register is set.
Bit 8 ESOFM: Expected start of frame interrupt mask
0: Expected Start of Frame (ESOF) Interrupt disabled.
1: ESOF Interrupt enabled, an interrupt request is generated when the corresponding bit in
the USB_ISTR register is set.
Bits 7:5 Reserved.
Bit 4 RESUME: Resume request
The microcontroller can set this bit to send a Resume signal to the host. It must be activated,
according to USB specifications, for no less than 1mS and no more than 15mS after which
the Host PC is ready to drive the resume sequence up to its end.
Bit 3 FSUSP: Force suspend
Software must set this bit when the SUSP interrupt is received, which is issued when no
traffic is received by the USB peripheral for 3 mS.
0: No effect.
1: Enter suspend mode. Clocks and static power dissipation in the analog transceiver are left
unaffected. If suspend power consumption is a requirement (bus-powered device), the
application software should set the LP_MODE bit after FSUSP as explained below.
Bit 2 LP_MODE: Low-power mode
This mode is used when the suspend-mode power constraints require that all static power
dissipation is avoided, except the one required to supply the external pull-up resistor. This
condition should be entered when the application is ready to stop all system clocks, or
reduce their frequency in order to meet the power consumption requirements of the USB
suspend condition. The USB activity during the suspend mode (WKUP event)
asynchronously resets this bit (it can also be reset by software).
0: No Low-power mode.
1: Enter Low-power mode.
Bit 1 PDWN: Power down
This bit is used to completely switch off all USB-related analog parts if it is required to
completely disable the USB peripheral for any reason. When this bit is set, the USB
peripheral is disconnected from the transceivers and it cannot be used.
0: Exit Power Down.
1: Enter Power down mode.
Bit 0 FRES: Force USB Reset
0: Clear USB reset.
1: Force a reset of the USB peripheral, exactly like a RESET signalling on the USB. The USB
peripheral is held in RESET state until software clears this bit. A USB-RESET interrupt is
generated, if enabled.
Universal serial bus full-speed device interface (USB) RM0008
612/1093 Doc ID 13902 Rev 13
USB interrupt status register (USB_ISTR)
Address offset: 0x44
Reset value: 0x0000 0000
This register contains the status of all the interrupt sources allowing application software to
determine, which events caused an interrupt request.
The upper part of this register contains single bits, each of them representing a specific
event. These bits are set by the hardware when the related event occurs; if the
corresponding bit in the USB_CNTR register is set, a generic interrupt request is generated.
The interrupt routine, examining each bit, will perform all necessary actions, and finally it will
clear the serviced bits. If any of them is not cleared, the interrupt is considered to be still
pending, and the interrupt line will be kept high again. If several bits are set simultaneously,
only a single interrupt will be generated.
Endpoint transaction completion can be handled in a different way to reduce interrupt
response latency. The CTR bit is set by the hardware as soon as an endpoint successfully
completes a transaction, generating a generic interrupt request if the corresponding bit in
USB_CNTR is set. An endpoint dedicated interrupt condition is activated independently
from the CTRM bit in the USB_CNTR register. Both interrupt conditions remain active until
software clears the pending bit in the corresponding USB_EPnR register (the CTR bit is
actually a read only bit). For endpoint-related interrupts, the software can use the Direction
of Transaction (DIR) and EP_ID read-only bits to identify, which endpoint made the last
interrupt request and called the corresponding interrupt service routine.
The user can choose the relative priority of simultaneously pending USB_ISTR events by
specifying the order in which software checks USB_ISTR bits in an interrupt service routine.
Only the bits related to events, which are serviced, are cleared. At the end of the service
routine, another interrupt will be requested, to service the remaining conditions.
To avoid spurious clearing of some bits, it is recommended to clear them with a load
instruction where all bits which must not be altered are written with 1, and all bits to be
cleared are written with 0 (these bits can only be cleared by software). Read-modify-write
cycles should be avoided because between the read and the write operations another bit
could be set by the hardware and the next write will clear it before the microprocessor has
the time to serve the event.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CTR
PMA
OVR
ERR WKUP SUSP RESET SOF ESOF
Reserved
DIR EP_ID[3:0]
r rc_w0 rc_w0 rc_w0 rc_w0 rc_w0 rc_w0 rc_w0 r r r r r
RM0008 Universal serial bus full-speed device interface (USB)
Doc ID 13902 Rev 13 613/1093
The following describes each bit in detail:
Bit 15 CTR: Correct transfer
This bit is set by the hardware to indicate that an endpoint has successfully completed a
transaction; using DIR and EP_ID bits software can determine which endpoint requested the
interrupt. This bit is read-only.
Bit 14 PMAOVR: Packet memory area over / underrun
This bit is set if the microcontroller has not been able to respond in time to an USB memory
request. The USB peripheral handles this event in the following way: During reception an
ACK handshake packet is not sent, during transmission a bit-stuff error is forced on the
transmitted stream; in both cases the host will retry the transaction. The PMAOVR interrupt
should never occur during normal operations. Since the failed transaction is retried by the
host, the application software has the chance to speed-up device operations during this
interrupt handling, to be ready for the next transaction retry; however this does not happen
during Isochronous transfers (no isochronous transaction is anyway retried) leading to a loss
of data in this case. This bit is read/write but only 0 can be written and writing 1 has no
effect.
Bit 13 ERR: Error
This flag is set whenever one of the errors listed below has occurred:
NANS: No ANSwer. The timeout for a host response has expired.
CRC: Cyclic Redundancy Check error. One of the received CRCs, either in the token or in the
data, was wrong.
BST: Bit Stuffing error. A bit stuffing error was detected anywhere in the PID, data, and/or
CRC.
FVIO: Framing format Violation. A non-standard frame was received (EOP not in the right
place, wrong token sequence, etc.).
The USB software can usually ignore errors, since the USB peripheral and the PC host
manage retransmission in case of errors in a fully transparent way. This interrupt can be
useful during the software development phase, or to monitor the quality of transmission over
the USB bus, to flag possible problems to the user (e.g. loose connector, too noisy
environment, broken conductor in the USB cable and so on). This bit is read/write but only 0
can be written and writing 1 has no effect.
Bit 12 WKUP: Wakeup
This bit is set to 1 by the hardware when, during suspend mode, activity is detected that
wakes up the USB peripheral. This event asynchronously clears the LP_MODE bit in the
CTLR register and activates the USB_WAKEUP line, which can be used to notify the rest of
the device (e.g. wakeup unit) about the start of the resume process. This bit is read/write but
only 0 can be written and writing 1 has no effect.
Bit 11 SUSP: Suspend mode request
This bit is set by the hardware when no traffic has been received for 3mS, indicating a
suspend mode request from the USB bus. The suspend condition check is enabled
immediately after any USB reset and it is disabled by the hardware when the suspend mode
is active (FSUSP=1) until the end of resume sequence. This bit is read/write but only 0 can
be written and writing 1 has no effect.
Universal serial bus full-speed device interface (USB) RM0008
614/1093 Doc ID 13902 Rev 13
Bit 10 RESET: USB reset request
Set when the USB peripheral detects an active USB RESET signal at its inputs. The USB
peripheral, in response to a RESET, just resets its internal protocol state machine, generating
an interrupt if RESETM enable bit in the USB_CNTR register is set. Reception and
transmission are disabled until the RESET bit is cleared. All configuration registers do not
reset: the microcontroller must explicitly clear these registers (this is to ensure that the
RESET interrupt can be safely delivered, and any transaction immediately followed by a
RESET can be completed). The function address and endpoint registers are reset by an USB
reset event.
This bit is read/write but only 0 can be written and writing 1 has no effect.
Bit 9 SOF: Start of frame
This bit signals the beginning of a new USB frame and it is set when a SOF packet arrives
through the USB bus. The interrupt service routine may monitor the SOF events to have a
1mS synchronization event to the USB host and to safely read the USB_FNR register which
is updated at the SOF packet reception (this could be useful for isochronous applications).
This bit is read/write but only 0 can be written and writing 1 has no effect.
Bit 8 ESOF: Expected start of frame
This bit is set by the hardware when an SOF packet is expected but not received. The host
sends an SOF packet each mS, but if the hub does not receive it properly, the Suspend Timer
issues this interrupt. If three consecutive ESOF interrupts are generated (i.e. three SOF
packets are lost) without any traffic occurring in between, a SUSP interrupt is generated. This
bit is set even when the missing SOF packets occur while the Suspend Timer is not yet
locked. This bit is read/write but only 0 can be written and writing 1 has no effect.
Bits 7:5 Reserved.
Bit 4 DIR: Direction of transaction
This bit is written by the hardware according to the direction of the successful transaction,
which generated the interrupt request.
If DIR bit=0, CTR_TX bit is set in the USB_EPnR register related to the interrupting endpoint.
The interrupting transaction is of IN type (data transmitted by the USB peripheral to the host
PC).
If DIR bit=1, CTR_RX bit or both CTR_TX/CTR_RX are set in the USB_EPnR register
related to the interrupting endpoint. The interrupting transaction is of OUT type (data
received by the USB peripheral from the host PC) or two pending transactions are waiting to
be processed.
This information can be used by the application software to access the USB_EPnR bits
related to the triggering transaction since it represents the direction having the interrupt
pending. This bit is read-only.
Bits 3:0 EP_ID[3:0]: Endpoint Identifier
These bits are written by the hardware according to the endpoint number, which generated
the interrupt request. If several endpoint transactions are pending, the hardware writes the
endpoint identifier related to the endpoint having the highest priority defined in the following
way: Two endpoint sets are defined, in order of priority: Isochronous and double-buffered bulk
endpoints are considered first and then the other endpoints are examined. If more than one
endpoint from the same set is requesting an interrupt, the EP_ID bits in USB_ISTR register
are assigned according to the lowest requesting endpoint register, EP0R having the highest
priority followed by EP1R and so on. The application software can assign a register to each
endpoint according to this priority scheme, so as to order the concurring endpoint requests in
a suitable way. These bits are read only.
RM0008 Universal serial bus full-speed device interface (USB)
Doc ID 13902 Rev 13 615/1093
USB frame number register (USB_FNR)
Address offset: 0x48
Reset value: 0x0XXX where X is undefined
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
RXDP RXDM LCK LSOF[1:0] FN[10:0]
r r r r r r r r r r r r r r r r
Bit 15 RXDP: Receive data + line status
This bit can be used to observe the status of received data plus upstream port data line. It
can be used during end-of-suspend routines to help determining the wakeup event.
Bit 14 RXDM: Receive data - line status
This bit can be used to observe the status of received data minus upstream port data line. It
can be used during end-of-suspend routines to help determining the wakeup event.
Bit 13 LCK: Locked
This bit is set by the hardware when at least two consecutive SOF packets have been
received after the end of an USB reset condition or after the end of an USB resume
sequence. Once locked, the frame timer remains in this state until an USB reset or USB
suspend event occurs.
Bits 12:11 LSOF[1:0]: Lost SOF
These bits are written by the hardware when an ESOF interrupt is generated, counting the
number of consecutive SOF packets lost. At the reception of an SOF packet, these bits are
cleared.
Bits 10:0 FN[10:0]: Frame number
This bit field contains the 11-bits frame number contained in the last received SOF packet.
The frame number is incremented for every frame sent by the host and it is useful for
Isochronous transfers. This bit field is updated on the generation of an SOF interrupt.
Universal serial bus full-speed device interface (USB) RM0008
616/1093 Doc ID 13902 Rev 13
USB device address (USB_DADDR)
Address offset: 0x4C
Reset value: 0x0000
Buffer table address (USB_BTABLE)
Address offset: 0x50
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
EF ADD6 ADD5 ADD4 ADD3 ADD2 ADD1 ADD0
rw rw rw rw rw rw rw rw
Bits 15:8 Reserved
Bit 7 EF: Enable function
This bit is set by the software to enable the USB device. The address of this device is
contained in the following ADD[6:0] bits. If this bit is at 0 no transactions are handled,
irrespective of the settings of USB_EPnR registers.
Bits 6:0 ADD[6:0]: Device address
These bits contain the USB function address assigned by the host PC during the
enumeration process. Both this field and the Endpoint Address (EA) field in the associated
USB_EPnR register must match with the information contained in a USB token in order to
handle a transaction to the required endpoint.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
BTABLE[15:3]
Reserved
rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:3 BTABLE[15:3]: Buffer table
These bits contain the start address of the buffer allocation table inside the dedicated packet
memory. This table describes each endpoint buffer location and size and it must be aligned
to an 8 byte boundary (the 3 least significant bits are always 0). At the beginning of every
transaction addressed to this device, the USP peripheral reads the element of this table
related to the addressed endpoint, to get its buffer start location and the buffer size (Refer to
Structure and usage of packet buffers on page 599).
Bits 2:0 Reserved, forced by hardware to 0.
RM0008 Universal serial bus full-speed device interface (USB)
Doc ID 13902 Rev 13 617/1093
23.5.2 Endpoint-specific registers
The number of these registers varies according to the number of endpoints that the USB
peripheral is designed to handle. The USB peripheral supports up to 8 bidirectional
endpoints. Each USB device must support a control endpoint whose address (EA bits) must
be set to 0. The USB peripheral behaves in an undefined way if multiple endpoints are
enabled having the same endpoint number value. For each endpoint, an USB_EPnR
register is available to store the endpoint specific information.
USB endpoint n register (USB_EPnR), n=[0..7]
Address offset: 0x00 to 0x1C
Reset value: 0x0000
They are also reset when an USB reset is received from the USB bus or forced through bit
FRES in the CTLR register, except the CTR_RX and CTR_TX bits, which are kept
unchanged to avoid missing a correct packet notification immediately followed by an USB
reset event. Each endpoint has its USB_EPnR register where n is the endpoint identifier.
Read-modify-write cycles on these registers should be avoided because between the read
and the write operations some bits could be set by the hardware and the next write would
modify them before the CPU has the time to detect the change. For this purpose, all bits
affected by this problem have an invariant value that must be used whenever their
modification is not required. It is recommended to modify these registers with a load
instruction where all the bits, which can be modified only by the hardware, are written with
their invariant value.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CTR_
RX
DTOG
_RX
STAT_RX[1:0] SETUP
EP
TYPE[1:0]
EP_
KIND
CTR_
TX
DTOG_
TX
STAT_TX[1:0] EA[3:0]
rc_w0 t t t r rw rw rw rc_w0 t t t rw rw rw rw
Bit 15 CTR_RX: Correct Transfer for reception
This bit is set by the hardware when an OUT/SETUP transaction is successfully completed
on this endpoint; the software can only clear this bit. If the CTRM bit in USB_CNTR register
is set accordingly, a generic interrupt condition is generated together with the endpoint
related interrupt condition, which is always activated. The type of occurred transaction, OUT
or SETUP, can be determined from the SETUP bit described below.
A transaction ended with a NAK or STALL handshake does not set this bit, since no data is
actually transferred, as in the case of protocol errors or data toggle mismatches.
This bit is read/write but only 0 can be written, writing 1 has no effect.
Universal serial bus full-speed device interface (USB) RM0008
618/1093 Doc ID 13902 Rev 13
Bit 14 DTOG_RX: Data Toggle, for reception transfers
If the endpoint is not Isochronous, this bit contains the expected value of the data toggle bit
(0=DATA0, 1=DATA1) for the next data packet to be received. Hardware toggles this bit,
when the ACK handshake is sent to the USB host, following a data packet reception having
a matching data PID value; if the endpoint is defined as a control one, hardware clears this
bit at the reception of a SETUP PID addressed to this endpoint.
If the endpoint is using the double-buffering feature this bit is used to support packet buffer
swapping too (Refer to Section 23.4.3: Double-buffered endpoints).
If the endpoint is Isochronous, this bit is used only to support packet buffer swapping since
no data toggling is used for this sort of endpoints and only DATA0 packet are transmitted
(Refer to Section 23.4.4: Isochronous transfers). Hardware toggles this bit just after the end
of data packet reception, since no handshake is used for isochronous transfers.
This bit can also be toggled by the software to initialize its value (mandatory when the
endpoint is not a control one) or to force specific data toggle/packet buffer usage. When the
application software writes 0, the value of DTOG_RX remains unchanged, while writing 1
makes the bit value toggle. This bit is read/write but it can be only toggled by writing 1.
Bits 13:12 STAT_RX [1:0]: Status bits, for reception transfers
These bits contain information about the endpoint status, which are listed in Table 173:
Reception status encoding on page 620.These bits can be toggled by software to initialize
their value. When the application software writes 0, the value remains unchanged, while
writing 1 makes the bit value toggle. Hardware sets the STAT_RX bits to NAK when a
correct transfer has occurred (CTR_RX=1) corresponding to a OUT or SETUP (control only)
transaction addressed to this endpoint, so the software has the time to elaborate the
received data before it acknowledge a new transaction
Double-buffered bulk endpoints implement a special transaction flow control, which control
the status based upon buffer availability condition (Refer to Section 23.4.3: Double-buffered
endpoints).
If the endpoint is defined as Isochronous, its status can be only VALID or DISABLED, so
that the hardware cannot change the status of the endpoint after a successful transaction. If
the software sets the STAT_RX bits to STALL or NAK for an Isochronous endpoint, the
USB peripheral behavior is not defined. These bits are read/write but they can be only
toggled by writing 1.
Bit 11 SETUP: Setup transaction completed
This bit is read-only and it is set by the hardware when the last completed transaction is a
SETUP. This bit changes its value only for control endpoints. It must be examined, in the
case of a successful receive transaction (CTR_RX event), to determine the type of
transaction occurred. To protect the interrupt service routine from the changes in SETUP
bits due to next incoming tokens, this bit is kept frozen while CTR_RX bit is at 1; its state
changes when CTR_RX is at 0. This bit is read-only.
Bits 10:9 EP_TYPE[1:0]: Endpoint type
These bits configure the behavior of this endpoint as described in Table 174: Endpoint type
encoding on page 620. Endpoint 0 must always be a control endpoint and each USB
function must have at least one control endpoint which has address 0, but there may be
other control endpoints if required. Only control endpoints handle SETUP transactions,
which are ignored by endpoints of other kinds. SETUP transactions cannot be answered
with NAK or STALL. If a control endpoint is defined as NAK, the USB peripheral will not
answer, simulating a receive error, in the receive direction when a SETUP transaction is
received. If the control endpoint is defined as STALL in the receive direction, then the
SETUP packet will be accepted anyway, transferring data and issuing the CTR interrupt. The
reception of OUT transactions is handled in the normal way, even if the endpoint is a control
one.
Bulk and interrupt endpoints have very similar behavior and they differ only in the special
feature available using the EP_KIND configuration bit.
The usage of Isochronous endpoints is explained in Section 23.4.4: Isochronous transfers
RM0008 Universal serial bus full-speed device interface (USB)
Doc ID 13902 Rev 13 619/1093
Bit 8 EP_KIND: Endpoint kind
The meaning of this bit depends on the endpoint type configured by the EP_TYPE bits.
Table 175 summarizes the different meanings.
DBL_BUF: This bit is set by the software to enable the double-buffering feature for this bulk
endpoint. The usage of double-buffered bulk endpoints is explained in Section 23.4.3:
Double-buffered endpoints.
STATUS_OUT: This bit is set by the software to indicate that a status out transaction is
expected: in this case all OUT transactions containing more than zero data bytes are
answered STALL instead of ACK. This bit may be used to improve the robustness of the
application to protocol errors during control transfers and its usage is intended for control
endpoints only. When STATUS_OUT is reset, OUT transactions can have any number of
bytes, as required.
Bit 7 CTR_TX: Correct Transfer for transmission
This bit is set by the hardware when an IN transaction is successfully completed on this
endpoint; the software can only clear this bit. If the CTRM bit in the USB_CNTR register is
set accordingly, a generic interrupt condition is generated together with the endpoint related
interrupt condition, which is always activated.
A transaction ended with a NAK or STALL handshake does not set this bit, since no data is
actually transferred, as in the case of protocol errors or data toggle mismatches.
This bit is read/write but only 0 can be written.
Bit 6 DTOG_TX: Data Toggle, for transmission transfers
If the endpoint is non-isochronous, this bit contains the required value of the data toggle bit
(0=DATA0, 1=DATA1) for the next data packet to be transmitted. Hardware toggles this bit
when the ACK handshake is received from the USB host, following a data packet
transmission. If the endpoint is defined as a control one, hardware sets this bit to 1 at the
reception of a SETUP PID addressed to this endpoint.
If the endpoint is using the double buffer feature, this bit is used to support packet buffer
swapping too (Refer to Section 23.4.3: Double-buffered endpoints)
If the endpoint is Isochronous, this bit is used to support packet buffer swapping since no
data toggling is used for this sort of endpoints and only DATA0 packet are transmitted (Refer
to Section 23.4.4: Isochronous transfers). Hardware toggles this bit just after the end of data
packet transmission, since no handshake is used for Isochronous transfers.
This bit can also be toggled by the software to initialize its value (mandatory when the
endpoint is not a control one) or to force a specific data toggle/packet buffer usage. When
the application software writes 0, the value of DTOG_TX remains unchanged, while writing
1 makes the bit value toggle. This bit is read/write but it can only be toggled by writing 1.
Bits 5:4 STAT_TX [1:0]: Status bits, for transmission transfers
These bits contain the information about the endpoint status, listed in Table 176. These bits
can be toggled by the software to initialize their value. When the application software writes
0, the value remains unchanged, while writing 1 makes the bit value toggle. Hardware sets
the STAT_TX bits to NAK, when a correct transfer has occurred (CTR_TX=1) corresponding
to a IN or SETUP (control only) transaction addressed to this endpoint. It then waits for the
software to prepare the next set of data to be transmitted.
Double-buffered bulk endpoints implement a special transaction flow control, which controls
the status based on buffer availability condition (Refer to Section 23.4.3: Double-buffered
endpoints).
If the endpoint is defined as Isochronous, its status can only be VALID or DISABLED.
Therefore, the hardware cannot change the status of the endpoint after a successful
transaction. If the software sets the STAT_TX bits to STALL or NAK for an Isochronous
endpoint, the USB peripheral behavior is not defined. These bits are read/write but they can
be only toggled by writing 1.
Universal serial bus full-speed device interface (USB) RM0008
620/1093 Doc ID 13902 Rev 13
Bits 3:0 EA[3:0]: Endpoint address
Software must write in this field the 4-bit address used to identify the transactions directed to
this endpoint. A value must be written before enabling the corresponding endpoint.
Table 173. Reception status encoding
STAT_RX[1:0] Meaning
00 DISABLED: all reception requests addressed to this endpoint are ignored.
01
STALL: the endpoint is stalled and all reception requests result in a STALL
handshake.
10 NAK: the endpoint is naked and all reception requests result in a NAK handshake.
11 VALID: this endpoint is enabled for reception.
Table 174. Endpoint type encoding
EP_TYPE[1:0] Meaning
00 BULK
01 CONTROL
10 ISO
11 INTERRUPT
Table 175. Endpoint kind meaning
EP_TYPE[1:0] EP_KIND Meaning
00 BULK DBL_BUF
01 CONTROL STATUS_OUT
10 ISO Not used
11 INTERRUPT Not used
Table 176. Transmission status encoding
STAT_TX[1:0] Meaning
00 DISABLED: all transmission requests addressed to this endpoint are ignored.
01
STALL: the endpoint is stalled and all transmission requests result in a STALL
handshake.
10
NAK: the endpoint is naked and all transmission requests result in a NAK
handshake.
11 VALID: this endpoint is enabled for transmission.
RM0008 Universal serial bus full-speed device interface (USB)
Doc ID 13902 Rev 13 621/1093
23.5.3 Buffer descriptor table
Although the buffer descriptor table is located inside the packet buffer memory, its entries
can be considered as additional registers used to configure the location and size of the
packet buffers used to exchange data between the USB macro cell and the STM32F10xxx.
Due to the common APB bridge limitation on word addressability, all packet memory
locations are accessed by the APB using 32-bit aligned addresses, instead of the actual
memory location addresses utilized by the USB peripheral for the USB_BTABLE register
and buffer description table locations.
In the following pages two location addresses are reported: the one to be used by
application software while accessing the packet memory, and the local one relative to USB
Peripheral access. To obtain the correct STM32F10xxx memory address value to be used in
the application software while accessing the packet memory, the actual memory location
address must be multiplied by two. The first packet memory location is located at
0x4000 6000. The buffer descriptor table entry associated with the USB_EPnR registers is
described below.
A thorough explanation of packet buffers and the buffer descriptor table usage can be found
in Structure and usage of packet buffers on page 599.
Transmission buffer address n (USB_ADDRn_TX)
Address offset: [USB_BTABLE] + n*16
USB local address: [USB_BTABLE] + n*8
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
ADDRn_TX[15:1] -
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw -
Bits 15:1 ADDRn_TX[15:1]: Transmission buffer address
These bits point to the starting address of the packet buffer containing data to be transmitted
by the endpoint associated with the USB_EPnR register at the next IN token addressed to it.
Bit 0 Must always be written as 0 since packet memory is word-wide and all packet buffers must be
word-aligned.
Universal serial bus full-speed device interface (USB) RM0008
622/1093 Doc ID 13902 Rev 13
Transmission byte count n (USB_COUNTn_TX)
Address offset: [USB_BTABLE] + n*16 + 4
USB local Address: [USB_BTABLE] + n*8 + 2
Note: Double-buffered and Isochronous IN Endpoints have two USB_COUNTn_TX registers:
named USB_COUNTn_TX_1 and USB_COUNTn_TX_0 with the following content.
Reception buffer address n (USB_ADDRn_RX)
Address offset: [USB_BTABLE] + n*16 + 8
USB local Address: [USB_BTABLE] + n*8 + 4
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
COUNTn_TX[9:0]
rw rw rw rw rw rw rw rw rw rw
Bits 15:10 These bits are not used since packet size is limited by USB specifications to 1023 bytes. Their
value is not considered by the USB peripheral.
Bits 9:0 COUNTn_TX[9:0]: Transmission byte count
These bits contain the number of bytes to be transmitted by the endpoint associated with the
USB_EPnR register at the next IN token addressed to it.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
COUNTn_TX_1[9:0]
rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
COUNTn_TX_0[9:0]
rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
ADDRn_RX[15:1] -
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw -
Bits 15:1 ADDRn_RX[15:1]: Reception buffer address
These bits point to the starting address of the packet buffer, which will contain the data
received by the endpoint associated with the USB_EPnR register at the next OUT/SETUP
token addressed to it.
Bit 0 This bit must always be written as 0 since packet memory is word-wide and all packet buffers
must be word-aligned.
RM0008 Universal serial bus full-speed device interface (USB)
Doc ID 13902 Rev 13 623/1093
Reception byte count n (USB_COUNTn_RX)
Address offset: [USB_BTABLE] + n*16 + 12
USB local Address: [USB_BTABLE] + n*8 + 6
This table location is used to store two different values, both required during packet
reception. The most significant bits contains the definition of allocated buffer size, to allow
buffer overflow detection, while the least significant part of this location is written back by the
USB peripheral at the end of reception to give the actual number of received bytes. Due to
the restrictions on the number of available bits, buffer size is represented using the number
of allocated memory blocks, where block size can be selected to choose the trade-off
between fine-granularity/small-buffer and coarse-granularity/large-buffer. The size of
allocated buffer is a part of the endpoint descriptor and it is normally defined during the
enumeration process according to its maxPacketSize parameter value (See Universal
Serial Bus Specification).
Note: Double-buffered and Isochronous IN Endpoints have two USB_COUNTn_TX
registers: named USB_COUNTn_TX_1 and USB_COUNTn_TX_0 with the
following content.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
BLSIZE NUM_BLOCK[4:0] COUNTn_RX[9:0]
rw rw rw rw rw rw r r r r r r r r r r
Bit 15 BL_SIZE: BLock size
This bit selects the size of memory block used to define the allocated buffer area.
If BL_SIZE=0, the memory block is 2 byte large, which is the minimum block allowed in a
word-wide memory. With this block size the allocated buffer size ranges from 2 to 62 bytes.
If BL_SIZE=1, the memory block is 32 byte large, which allows to reach the maximum
packet length defined by USB specifications. With this block size the allocated buffer size
ranges from 32 to 1024 bytes, which is the longest packet size allowed by USB standard
specifications.
Bits 14:10 NUM_BLOCK[4:0]: Number of blocks
These bits define the number of memory blocks allocated to this packet buffer. The actual
amount of allocated memory depends on the BL_SIZE value as illustrated in Table 177.
Bits 9:0 COUNTn_RX[9:0]: Reception byte count
These bits contain the number of bytes received by the endpoint associated with the
USB_EPnR register during the last OUT/SETUP transaction addressed to it.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
BLSIZE
_1
NUM_BLOCK_1[4:0] COUNTn_RX_1[9:0]
rw rw rw rw rw rw r r r r r r r r r r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
BLSIZE
_0
NUM_BLOCK_0[4:0] COUNTn_RX_0[9:0]
rw rw rw rw rw rw r r r r r r r r r r
Universal serial bus full-speed device interface (USB) RM0008
624/1093 Doc ID 13902 Rev 13
23.5.4 USB register map
The table below provides the USB register map and reset values.
Table 177. Definition of allocated buffer memory
Value of
NUM_BLOCK[4:0]
Memory allocated
when BL_SIZE=0
Memory allocated
when BL_SIZE=1
0 (00000) Not allowed 32 bytes
1 (00001) 2 bytes 64 bytes
2 (00010) 4 bytes 96 bytes
3 (00011) 6 bytes 128 bytes
... ... ...
15 (01111) 30 bytes 512 bytes
16 (10000) 32 bytes N/A
17 (10001) 34 bytes N/A
18 (10010) 36 bytes N/A
... ... ...
30 (11110) 60 bytes N/A
31 (11111) 62 bytes N/A
Table 178. USB register map and reset values
Offset Register
3
1
3
0
2
9
2
8
2
7
2
6
2
5
2
4
2
3
2
2
2
1
2
0
1
9
1
8
1
7
1
6
1
5
1
4
1
3
1
2
1
1
1
0
9 8 7 6 5 4 3 2 1 0
0x00
USB_EP0R
Reserved
C
T
R
_
R
X
D
T
O
G
_
R
X
STAT_
RX
[1:0]
S
E
T
U
P EP
TYPE
[1:0]
E
P
_
K
I
N
D
C
T
R
_
T
X
D
T
O
G
_
T
X
STAT_
TX
[1:0]
EA[3:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x04
USB_EP1R
Reserved
C
T
R
_
R
X
D
T
O
G
_
R
X
STAT_
RX
[1:0]
S
E
T
U
P EP
TYPE
[1:0]
E
P
_
K
I
N
D
C
T
R
_
T
X
D
T
O
G
_
T
X
STAT_
TX
[1:0]
EA[3:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x08
USB_EP2R
Reserved
C
T
R
_
R
X
D
T
O
G
_
R
X
STAT_
RX
[1:0]
S
E
T
U
P EP
TYPE
[1:0]
E
P
_
K
I
N
D
C
T
R
_
T
X
D
T
O
G
_
T
X
STAT_
TX
[1:0]
EA[3:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x0C
USB_EP3R
Reserved
C
T
R
_
R
X
D
T
O
G
_
R
X
STAT_
RX
[1:0]
S
E
T
U
P EP
TYPE
[1:0]
E
P
_
K
I
N
D
C
T
R
_
T
X
D
T
O
G
_
T
X
STAT_
TX
[1:0]
EA[3:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x10
USB_EP4R
Reserved
C
T
R
_
R
X
D
T
O
G
_
R
X
STAT_
RX
[1:0]
S
E
T
U
P EP
TYPE
[1:0]
E
P
_
K
I
N
D
C
T
R
_
T
X
D
T
O
G
_
T
X
STAT_
TX
[1:0]
EA[3:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x14
USB_EP5R
Reserved
C
T
R
_
R
X
D
T
O
G
_
R
X
STAT_
RX
[1:0]
S
E
T
U
P EP
TYPE
[1:0]
E
P
_
K
I
N
D
C
T
R
_
T
X
D
T
O
G
_
T
X
STAT_
TX
[1:0]
EA[3:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
RM0008 Universal serial bus full-speed device interface (USB)
Doc ID 13902 Rev 13 625/1093
Refer to Table 3 on page 50 for the register boundary addresses.
0x18
USB_EP6R
Reserved
C
T
R
_
R
X
D
T
O
G
_
R
X
STAT_
RX
[1:0]
S
E
T
U
P EP
TYPE
[1:0]
E
P
_
K
I
N
D
C
T
R
_
T
X
D
T
O
G
_
T
X
STAT_
TX
[1:0]
EA[3:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x1C
USB_EP7R
Reserved
C
T
R
_
R
X
D
T
O
G
_
R
X
STAT_
RX
[1:0]
S
E
T
U
P EP
TYPE
[1:0]
E
P
_
K
I
N
D
C
T
R
_
T
X
D
T
O
G
_
T
X
STAT_
TX
[1:0]
EA[3:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x20-
0x3F
Reserved
0x40
USB_CNTR
Reserved
C
T
R
M
P
M
A
O
V
R
M
E
R
R
M
W
K
U
P
M
S
U
S
P
M
R
E
S
E
T
M
S
O
F
M
E
S
O
F
M
Reserved
R
E
S
U
M
E
F
S
U
S
P
L
P
M
O
D
E
P
D
W
N
F
R
E
S
Reset value 0 0 0 0 0 0 0 0 0 0 0 1 1
0x44
USB_ISTR
Reserved
C
T
R
P
M
A
O
V
R
E
R
R
W
K
U
P
S
U
S
P
R
E
S
E
T
S
O
F
E
S
O
F
Reserved D
I
R
EP_ID[3:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0
0x48
USB_FNR
Reserved
R
X
D
P
R
X
D
M
L
C
K LSOF
[1:0]
FN[10:0]
Reset value 0 0 0 0 0 x x x x x x x x x x x
0x4C
USB_DADDR
Reserved
EF ADD[6:0]
Reset value 0 0 0 0 0 0 0 0
0x50
USB_BTABLE
Reserved
BTABLE[15:3]
Reserved
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0
Table 178. USB register map and reset values (continued)
Offset Register
3
1
3
0
2
9
2
8
2
7
2
6
2
5
2
4
2
3
2
2
2
1
2
0
1
9
1
8
1
7
1
6
1
5
1
4
1
3
1
2
1
1
1
0
9 8 7 6 5 4 3 2 1 0
Controller area network (bxCAN) RM0008
626/1093 Doc ID 13902 Rev 13
24 Controller area network (bxCAN)
Low-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 16 and 32 Kbytes.
Medium-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 64 and 128 Kbytes.
High-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 256 and 512 Kbytes.
XL-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 768 Kbytes and 1 Mbyte.
Connectivity line devices are STM32F105xx and STM32F107xx microcontrollers.
This section applies to the connectivity line and STM32F103xx performance line only.
24.1 bxCAN introduction
The Basic Extended CAN peripheral, named bxCAN, interfaces the CAN network. It
supports the CAN protocols version 2.0A and B. It has been designed to manage a high
number of incoming messages efficiently with a minimum CPU load. It also meets the
priority requirements for transmit messages.
For safety-critical applications, the CAN controller provides all hardware functions for
supporting the CAN Time Triggered Communication option.
24.2 bxCAN main features
Supports CAN protocol version 2.0 A, B Active
Bit rates up to 1 Mbit/s
Supports the Time Triggered Communication option
Transmission
Three transmit mailboxes
Configurable transmit priority
Time Stamp on SOF transmission
Reception
Two receive FIFOs with three stages
Scalable filter banks:
28 filter banks shared between CAN1 and CAN2 in connectivity line devices
14 filter banks in other STM32F10xxx devices
Identifier list feature
Configurable FIFO overrun
Time Stamp on SOF reception
RM0008 Controller area network (bxCAN)
Doc ID 13902 Rev 13 627/1093
Time-triggered communication option
Disable automatic retransmission mode
16-bit free running timer
Time Stamp sent in last two data bytes
Management
Maskable interrupts
Software-efficient mailbox mapping at a unique address space
Dual CAN (connectivity line only)
CAN1: Master bxCAN for managing the communication between a Slave bxCAN and
the 512-byte SRAM memory
CAN2: Slave bxCAN, with no direct access to the SRAM memory.
The two bxCAN cells share the 512-byte SRAM memory (see Figure 222 on page 629)
Note: In low, medium-, high- and XL-density devices the USB and CAN share a dedicated 512-
byte SRAM memory for data transmission and reception, and so they cannot be used
concurrently (the shared SRAM is accessed through CAN and USB exclusively). The USB
and CAN can be used in the same application but not at the same time.
24.3 bxCAN general description
In todays CAN applications, the number of nodes in a network is increasing and often
several networks are linked together via gateways. Typically the number of messages in the
system (and thus to be handled by each node) has significantly increased. In addition to the
application messages, Network Management and Diagnostic messages have been
introduced.
An enhanced filtering mechanism is required to handle each type of message.
Furthermore, application tasks require more CPU time, therefore real-time constraints
caused by message reception have to be reduced.
A receive FIFO scheme allows the CPU to be dedicated to application tasks for a long
time period without losing messages.
The standard HLP (Higher Layer Protocol) based on standard CAN drivers requires an
efficient interface to the CAN controller.
Figure 221. CAN network topology
C
A
N
n
o
d
e
1
C
A
N
n
o
d
e
2
C
A
N
n
o
d
e
n
CAN CAN
High Low
CAN CAN
Rx Tx
CAN
Transceiver
CAN
Controller
MCU
CAN Bus
Application
Controller area network (bxCAN) RM0008
628/1093 Doc ID 13902 Rev 13
24.3.1 CAN 2.0B active core
The bxCAN module handles the transmission and the reception of CAN messages fully
autonomously. Standard identifiers (11-bit) and extended identifiers (29-bit) are fully
supported by hardware.
24.3.2 Control, status and configuration registers
The application uses these registers to:
Configure CAN parameters, e.g. baud rate
Request transmissions
Handle receptions
Manage interrupts
Get diagnostic information
24.3.3 Tx mailboxes
Three transmit mailboxes are provided to the software for setting up messages. The
transmission Scheduler decides which mailbox has to be transmitted first.
24.3.4 Acceptance filters
The bxCAN provides 28 scalable/configurable identifier filter banks for selecting the
incoming messages the software needs and discarding the others. In other devices there
are 14 scalable/configurable identifier filter banks.
Receive FIFO
Two receive FIFOs are used by hardware to store the incoming messages. Three complete
messages can be stored in each FIFO. The FIFOs are managed completely by hardware.
RM0008 Controller area network (bxCAN)
Doc ID 13902 Rev 13 629/1093
Figure 222. Dual CAN block diagram (connectivity devices)
24.4 bxCAN operating modes
bxCAN has three main operating modes: initialization, normal and Sleep. After a
hardware reset, bxCAN is in Sleep mode to reduce power consumption and an internal pull-
up is active on CANTX. The software requests bxCAN to enter initialization or Sleep mode
by setting the INRQ or SLEEP bits in the CAN_MCR register. Once the mode has been
entered, bxCAN confirms it by setting the INAK or SLAK bits in the CAN_MSR register and
the internal pull-up is disabled. When neither INAK nor SLAK are set, bxCAN is in normal
mode. Before entering normal mode bxCAN always has to synchronize on the CAN bus.
26
..
Acceptance Filters
..
3
2
1
Filter
0
27
Transmission
Scheduler
Mailbox 0
1
2
Receive FIFO 1
Mailbox 0
1
2
Receive FIFO 0
Mailbox 0
1
2
Tx Mailboxes
Transmission
Scheduler
Mailbox 0
1
2
Receive FIFO 1
Mailbox 0
1
2
Receive FIFO 0
Mailbox 0
1
2
Tx Mailboxes
Memory
Access
Controller
Master Control
Master Status
Rx FIFO 0 Status
Rx FIFO 1 Status
Error Status
Bit Timing
Interrupt Enable
C
o
n
t
r
o
l
/
S
t
a
t
u
s
/
C
o
n
f
i
g
u
r
a
t
i
o
n
Tx Status
Master Control
Master Status
Rx FIFO 0 Status
Rx FIFO 1 Status
Error Status
Bit Timing
Filter Mode
Filter Scale
Interrupt Enable
C
o
n
t
r
o
l
/
S
t
a
t
u
s
/
C
o
n
f
i
g
u
r
a
t
i
o
n
Tx Status
Filter FIFO Assign
Filter Master
Filter Activation
CAN 2.0B Active Core
CAN2 (Slave)
CAN 2.0B Active Core
CAN1 (Master) with 512 bytes SRAM
Master
r e t s a M r e t s a M
Master Filters
Slave
Slave Slave
Slave Filters
(n to 27) (0 to n)
Note: CAN 2 start filter bank number n is configurable by writing to
the CAN2SB[5:0] bits in the CAN_ FMR register.
ai16094
Controller area network (bxCAN) RM0008
630/1093 Doc ID 13902 Rev 13
To synchronize, bxCAN waits until the CAN bus is idle, this means 11 consecutive recessive
bits have been monitored on CANRX.
24.4.1 Initialization mode
The software initialization can be done while the hardware is in Initialization mode. To enter
this mode the software sets the INRQ bit in the CAN_MCR register and waits until the
hardware has confirmed the request by setting the INAK bit in the CAN_MSR register.
To leave Initialization mode, the software clears the INQR bit. bxCAN has left Initialization
mode once the INAK bit has been cleared by hardware.
While in Initialization Mode, all message transfers to and from the CAN bus are stopped and
the status of the CAN bus output CANTX is recessive (high).
Entering Initialization Mode does not change any of the configuration registers.
To initialize the CAN Controller, software has to set up the Bit Timing (CAN_BTR) and CAN
options (CAN_MCR) registers.
To initialize the registers associated with the CAN filter banks (mode, scale, FIFO
assignment, activation and filter values), software has to set the FINIT bit (CAN_FMR). Filter
initialization also can be done outside the initialization mode.
Note: When FINIT=1, CAN reception is deactivated.
The filter values also can be modified by deactivating the associated filter activation bits (in
the CAN_FA1R register).
If a filter bank is not used, it is recommended to leave it non active (leave the corresponding
FACT bit cleared).
24.4.2 Normal mode
Once the initialization has been done, the software must request the hardware to enter
Normal mode, to synchronize on the CAN bus and start reception and transmission.
Entering Normal mode is done by clearing the INRQ bit in the CAN_MCR register and
waiting until the hardware has confirmed the request by clearing the INAK bit in the
CAN_MSR register. Afterwards, the bxCAN synchronizes with the data transfer on the CAN
bus by waiting for the occurrence of a sequence of 11 consecutive recessive bits ( Bus Idle)
before it can take part in bus activities and start message transfer.
The initialization of the filter values is independent from Initialization Mode but must be done
while the filter is not active (corresponding FACTx bit cleared). The filter scale and mode
configuration must be configured before entering Normal Mode.
24.4.3 Sleep mode (low power)
To reduce power consumption, bxCAN has a low-power mode called Sleep mode. This
mode is entered on software request by setting the SLEEP bit in the CAN_MCR register. In
this mode, the bxCAN clock is stopped, however software can still access the bxCAN
mailboxes.
If software requests entry to initialization mode by setting the INRQ bit while bxCAN is in
Sleep mode, it must also clear the SLEEP bit.
bxCAN can be woken up (exit Sleep mode) either by software clearing the SLEEP bit or on
detection of CAN bus activity.
RM0008 Controller area network (bxCAN)
Doc ID 13902 Rev 13 631/1093
On CAN bus activity detection, hardware automatically performs the wakeup sequence by
clearing the SLEEP bit if the AWUM bit in the CAN_MCR register is set. If the AWUM bit is
cleared, software has to clear the SLEEP bit when a wakeup interrupt occurs, in order to exit
from Sleep mode.
Note: If the wakeup interrupt is enabled (WKUIE bit set in CAN_IER register) a wakeup interrupt
will be generated on detection of CAN bus activity, even if the bxCAN automatically performs
the wakeup sequence.
After the SLEEP bit has been cleared, Sleep mode is exited once bxCAN has synchronized
with the CAN bus, refer to Figure 223: bxCAN operating modes. The Sleep mode is exited
once the SLAK bit has been cleared by hardware.
Figure 223. bxCAN operating modes
1. ACK = The wait state during which hardware confirms a request by setting the INAK or SLAK bits in the
CAN_MSR register
2. SYNC = The state during which bxCAN waits until the CAN bus is idle, meaning 11 consecutive recessive
bits have been monitored on CANRX
24.5 Test mode
Test mode can be selected by the SILM and LBKM bits in the CAN_BTR register. These bits
must be configured while bxCAN is in Initialization mode. Once test mode has been
selected, the INRQ bit in the CAN_MCR register must be reset to enter Normal mode.
24.5.1 Silent mode
The bxCAN can be put in Silent mode by setting the SILM bit in the CAN_BTR register.
In Silent mode, the bxCAN is able to receive valid data frames and valid remote frames, but
it sends only recessive bits on the CAN bus and it cannot start a transmission. If the bxCAN
has to send a dominant bit (ACK bit, overload flag, active error flag), the bit is rerouted
internally so that the CAN Core monitors this dominant bit, although the CAN bus may
remain in recessive state. Silent mode can be used to analyze the traffic on a CAN bus
without affecting it by the transmission of dominant bits (Acknowledge Bits, Error Frames).
SIeep
InitiaIization
NormaI
Reset
SLAK= 1
NAK = 0
SLAK= 0
NAK = 1
SLAK= 0
NAK = 0
S
L
E
E
P
.
N
R
Q
.
A
C
K
S
L
E
E
P
.
N
R
Q
.
A
C
K
NRQ . ACK
NRQ . SYNC . SLEEP
S
L
E
E
P
.
A
C
K
S
L
E
E
P
.
S
Y
N
C
.
N
R
Q
ai15902
Controller area network (bxCAN) RM0008
632/1093 Doc ID 13902 Rev 13
Figure 224. bxCAN in silent mode
24.5.2 Loop back mode
The bxCAN can be set in Loop Back Mode by setting the LBKM bit in the CAN_BTR
register. In Loop Back Mode, the bxCAN treats its own transmitted messages as received
messages and stores them (if they pass acceptance filtering) in a Receive mailbox.
Figure 225. bxCAN in loop back mode
This mode is provided for self-test functions. To be independent of external events, the CAN
Core ignores acknowledge errors (no dominant bit sampled in the acknowledge slot of a
data / remote frame) in Loop Back Mode. In this mode, the bxCAN performs an internal
feedback from its Tx output to its Rx input. The actual value of the CANRX input pin is
disregarded by the bxCAN. The transmitted messages can be monitored on the CANTX pin.
24.5.3 Loop back combined with silent mode
It is also possible to combine Loop Back mode and Silent mode by setting the LBKM and
SILM bits in the CAN_BTR register. This mode can be used for a Hot Selftest, meaning the
bxCAN can be tested like in Loop Back mode but without affecting a running CAN system
connected to the CANTX and CANRX pins. In this mode, the CANRX pin is disconnected
from the bxCAN and the CANTX pin is held recessive.
bxCAN
CANTX CANRX
Tx Rx
=1
bxCAN
CANTX CANRX
Tx Rx
RM0008 Controller area network (bxCAN)
Doc ID 13902 Rev 13 633/1093
Figure 226. bxCAN in combined mode
24.6 STM32F10xxx in Debug mode
When the microcontroller enters the debug mode (Cortex-M3 core halted), the bxCAN
continues to work normally or stops, depending on:
the DBG_CAN1_STOP bit for CAN1 or the DBG_CAN2_STOP bit for CAN2 in the DBG
module. For more details, refer to Section 31.16.2: Debug support for timers, watchdog,
bxCAN and I2C.
the DBF bit in CAN_MCR. For more details, refer to Section 24.9.2: CAN control and
status registers.
24.7 bxCAN functional description
24.7.1 Transmission handling
In order to transmit a message, the application must select one empty transmit mailbox, set
up the identifier, the data length code (DLC) and the data before requesting the transmission
by setting the corresponding TXRQ bit in the CAN_TIxR register. Once the mailbox has left
empty state, the software no longer has write access to the mailbox registers. Immediately
after the TXRQ bit has been set, the mailbox enters pending state and waits to become the
highest priority mailbox, see Transmit Priority. As soon as the mailbox has the highest
priority it will be scheduled for transmission. The transmission of the message of the
scheduled mailbox will start (enter transmit state) when the CAN bus becomes idle. Once
the mailbox has been successfully transmitted, it will become empty again. The hardware
indicates a successful transmission by setting the RQCP and TXOK bits in the CAN_TSR
register.
If the transmission fails, the cause is indicated by the ALST bit in the CAN_TSR register in
case of an Arbitration Lost, and/or the TERR bit, in case of transmission error detection.
Transmit priority
By identifier:
When more than one transmit mailbox is pending, the transmission order is given by the
identifier of the message stored in the mailbox. The message with the lowest identifier value
has the highest priority according to the arbitration of the CAN protocol. If the identifier
values are equal, the lower mailbox number will be scheduled first.
By transmit request order:
bxCAN
CANTX CANRX
Tx Rx
=1
Controller area network (bxCAN) RM0008
634/1093 Doc ID 13902 Rev 13
The transmit mailboxes can be configured as a transmit FIFO by setting the TXFP bit in the
CAN_MCR register. In this mode the priority order is given by the transmit request order.
This mode is very useful for segmented transmission.
Abort
A transmission request can be aborted by the user setting the ABRQ bit in the CAN_TSR
register. In pending or scheduled state, the mailbox is aborted immediately. An abort
request while the mailbox is in transmit state can have two results. If the mailbox is
transmitted successfully the mailbox becomes empty with the TXOK bit set in the
CAN_TSR register. If the transmission fails, the mailbox becomes scheduled, the
transmission is aborted and becomes empty with TXOK cleared. In all cases the mailbox
will become empty again at least at the end of the current transmission.
Nonautomatic retransmission mode
This mode has been implemented in order to fulfil the requirement of the Time Triggered
Communication option of the CAN standard. To configure the hardware in this mode the
NART bit in the CAN_MCR register must be set.
In this mode, each transmission is started only once. If the first attempt fails, due to an
arbitration loss or an error, the hardware will not automatically restart the message
transmission.
At the end of the first transmission attempt, the hardware considers the request as
completed and sets the RQCP bit in the CAN_TSR register. The result of the transmission is
indicated in the CAN_TSR register by the TXOK, ALST and TERR bits.
Figure 227. Transmit mailbox states
EMPTY
TXRQ=1
RQCP=X
TXOK=X
PENDING
RQCP=0
TXOK=0
SCHEDULED
RQCP=0
TXOK=0
Mailbox has
TRANSMIT
RQCP=0
TXOK=0
CAN Bus = IDLE
Transmit failed * NART
Transmit succeeded
Mailbox does not
EMPTY
RQCP=1
TXOK=0
highest priority
have highest priority
EMPTY
RQCP=1
TXOK=1
ABRQ=1
ABRQ=1
Transmit failed * NART
TME = 1
TME = 0
TME = 0
TME = 0
TME = 1
TME = 1
RM0008 Controller area network (bxCAN)
Doc ID 13902 Rev 13 635/1093
24.7.2 Time triggered communication mode
In this mode, the internal counter of the CAN hardware is activated and used to generate the
Time Stamp value stored in the CAN_RDTxR/CAN_TDTxR registers, respectively (for Rx
and Tx mailboxes). The internal counter is incremented each CAN bit time (refer to
Section 24.7.7: Bit timing). The internal counter is captured on the sample point of the Start
Of Frame bit in both reception and transmission.
24.7.3 Reception handling
For the reception of CAN messages, three mailboxes organized as a FIFO are provided. In
order to save CPU load, simplify the software and guarantee data consistency, the FIFO is
managed completely by hardware. The application accesses the messages stored in the
FIFO through the FIFO output mailbox.
Valid message
A received message is considered as valid when it has been received correctly according to
the CAN protocol (no error until the last but one bit of the EOF field) and It passed through
the identifier filtering successfully, see Section 24.7.4: Identifier filtering.
Figure 228. Receive FIFO states
EMPTY
Valid Message FMP=0x00
FOVR=0
PENDING_1
FMP=0x01
FOVR=0
Received
PENDING_2
FMP=0x10
FOVR=0
PENDING_3
FMP=0x11
FOVR=0
Valid Message
Received
Release
OVERRUN
FMP=0x11
FOVR=1 Mailbox
Release
Mailbox
Valid Message
Received
Valid Message
Received
Release
Mailbox
Release
Mailbox
Valid Message
Received
RFOM=1
RFOM=1
RFOM=1
Controller area network (bxCAN) RM0008
636/1093 Doc ID 13902 Rev 13
FIFO management
Starting from the empty state, the first valid message received is stored in the FIFO which
becomes pending_1. The hardware signals the event setting the FMP[1:0] bits in the
CAN_RFR register to the value 01b. The message is available in the FIFO output mailbox.
The software reads out the mailbox content and releases it by setting the RFOM bit in the
CAN_RFR register. The FIFO becomes empty again. If a new valid message has been
received in the meantime, the FIFO stays in pending_1 state and the new message is
available in the output mailbox.
If the application does not release the mailbox, the next valid message will be stored in the
FIFO which enters pending_2 state (FMP[1:0] = 10b). The storage process is repeated for
the next valid message putting the FIFO into pending_3 state (FMP[1:0] = 11b). At this
point, the software must release the output mailbox by setting the RFOM bit, so that a
mailbox is free to store the next valid message. Otherwise the next valid message received
will cause a loss of message.
Refer also to Section 24.7.5: Message storage
Overrun
Once the FIFO is in pending_3 state (i.e. the three mailboxes are full) the next valid
message reception will lead to an overrun and a message will be lost. The hardware
signals the overrun condition by setting the FOVR bit in the CAN_RFR register. Which
message is lost depends on the configuration of the FIFO:
If the FIFO lock function is disabled (RFLM bit in the CAN_MCR register cleared) the
last message stored in the FIFO will be overwritten by the new incoming message. In
this case the latest messages will be always available to the application.
If the FIFO lock function is enabled (RFLM bit in the CAN_MCR register set) the most
recent message will be discarded and the software will have the three oldest messages
in the FIFO available.
Reception related interrupts
Once a message has been stored in the FIFO, the FMP[1:0] bits are updated and an
interrupt request is generated if the FMPIE bit in the CAN_IER register is set.
When the FIFO becomes full (i.e. a third message is stored) the FULL bit in the CAN_RFR
register is set and an interrupt is generated if the FFIE bit in the CAN_IER register is set.
On overrun condition, the FOVR bit is set and an interrupt is generated if the FOVIE bit in
the CAN_IER register is set.
24.7.4 Identifier filtering
In the CAN protocol the identifier of a message is not associated with the address of a node
but related to the content of the message. Consequently a transmitter broadcasts its
message to all receivers. On message reception a receiver node decides - depending on
the identifier value - whether the software needs the message or not. If the message is
needed, it is copied into the SRAM. If not, the message must be discarded without
intervention by the software.
To fulfill this requirement, the bxCAN Controller provides 28 configurable and scalable filter
banks (27-0) to the application. In other devices the bxCAN Controller provides 14
configurable and scalable filter banks (13-0) to the application in order to receive only the
messages the software needs. This hardware filtering saves CPU resources which would be
RM0008 Controller area network (bxCAN)
Doc ID 13902 Rev 13 637/1093
otherwise needed to perform filtering by software. Each filter bank x consists of two 32-bit
registers, CAN_FxR0 and CAN_FxR1.
Scalable width
To optimize and adapt the filters to the application needs, each filter bank can be scaled
independently. Depending on the filter scale a filter bank provides:
One 32-bit filter for the STDID[10:0], EXTID[17:0], IDE and RTR bits.
Two 16-bit filters for the STDID[10:0], RTR, IDE and EXTID[17:15] bits.
Refer to Figure 229.
Furthermore, the filters can be configured in mask mode or in identifier list mode.
Mask mode
In mask mode the identifier registers are associated with mask registers specifying which
bits of the identifier are handled as must match or as dont care.
Identifier list mode
In identifier list mode, the mask registers are used as identifier registers. Thus instead of
defining an identifier and a mask, two identifiers are specified, doubling the number of single
identifiers. All bits of the incoming identifier must match the bits specified in the filter
registers.
Filter bank scale and mode configuration
The filter banks are configured by means of the corresponding CAN_FMR register. To
configure a filter bank it must be deactivated by clearing the FACT bit in the CAN_FAR
register. The filter scale is configured by means of the corresponding FSCx bit in the
CAN_FS1R register, refer to Figure 229. The identifier list or identifier mask mode for the
corresponding Mask/Identifier registers is configured by means of the FBMx bits in the
CAN_FMR register.
To filter a group of identifiers, configure the Mask/Identifier registers in mask mode.
To select single identifiers, configure the Mask/Identifier registers in identifier list mode.
Filters not used by the application should be left deactivated.
Each filter within a filter bank is numbered (called the Filter Number) from 0 to a maximum
dependent on the mode and the scale of each of the filter banks.
Concerning the filter configuration, refer to Figure 229.
Controller area network (bxCAN) RM0008
638/1093 Doc ID 13902 Rev 13
Figure 229. Filter bank scale configuration - register organization
Filter match index
Once a message has been received in the FIFO it is available to the application. Typically,
application data is copied into SRAM locations. To copy the data to the right location the
application has to identify the data by means of the identifier. To avoid this, and to ease the
access to the SRAM locations, the CAN controller provides a Filter Match Index.
This index is stored in the mailbox together with the message according to the filter priority
rules. Thus each received message has its associated filter match index.
The Filter Match index can be used in two ways:
Compare the Filter Match index with a list of expected values.
Use the Filter Match Index as an index on an array to access the data destination
location.
For nonmasked filters, the software no longer has to compare the identifier.
If the filter is masked the software reduces the comparison to the masked bits only.
The index value of the filter number does not take into account the activation state of the
filter banks. In addition, two independent numbering schemes are used, one for each FIFO.
Refer to Figure 230 for an example.
One 32-Bit Filter - Identifier Mask
Two 16-Bit Filters - Identifier Mask
CAN_FxR1[31:24]
CAN_FxR2[31:24]
CAN_FxR1[15:8]
CAN_FxR1[31:24]
CAN_FxR1[7:0]
CAN_FxR1[23:16]
x = filter bank number
F
S
C
x
=
1
F
S
C
x
=
0
1
These bits are located in the CAN_FS1R register
F
i
l
t
e
r
B
a
n
k
S
c
a
l
e
ID
Mask
ID
Mask
STID[10:3] STID[2:0] EXID[12:5] Mapping
STID[10:3]
ID
Mask
Mapping
RTR
Two 32-Bit Filters - Identifier List
ID
ID
STID[10:3] STID[2:0] EXID[12:5] Mapping
Four 16-Bit Filters - Identifier List
ID
ID
STID[10:3]
ID
ID
Mapping
n
n+1
n+2
n+3
n+1
F
i
l
t
e
r
B
a
n
k
M
o
d
e
2
n
n
n+1
EXID[4:0] IDE EXID[17:13]
EXID[17:13]
STID[2:0] RTR IDE EXID[17:15]
F
B
M
x
=
0
F
B
M
x
=
1
Filter
2
These bits are located in the CAN_FM1R register
n
Num.
F
B
M
x
=
0
F
B
M
x
=
1
C
o
n
f
i
g
.
B
i
t
s
1
STID[2:0] RTR IDE EXID[17:15]
0
RTR EXID[4:0] IDE 0
CAN_FxR1[23:16] CAN_FxR1[15:8] CAN_FxR1[7:0]
CAN_FxR2[7:0] CAN_FxR2[15:8] CAN_FxR2[23:16]
CAN_FxR1[31:24]
CAN_FxR2[31:24]
CAN_FxR1[23:16] CAN_FxR1[15:8] CAN_FxR1[7:0]
CAN_FxR2[7:0] CAN_FxR2[15:8] CAN_FxR2[23:16]
CAN_FxR2[15:8]
CAN_FxR2[31:24]
CAN_FxR2[7:0]
CAN_FxR2[23:16]
CAN_FxR1[15:8]
CAN_FxR1[31:24]
CAN_FxR1[7:0]
CAN_FxR1[23:16]
CAN_FxR2[15:8]
CAN_FxR2[31:24]
CAN_FxR2[7:0]
CAN_FxR2[23:16]
ID=Identifier
RM0008 Controller area network (bxCAN)
Doc ID 13902 Rev 13 639/1093
Figure 230. Example of filter numbering
Filter priority rules
Depending on the filter combination it may occur that an identifier passes successfully
through several filters. In this case the filter match value stored in the receive mailbox is
chosen according to the following priority rules:
A 32-bit filter takes priority over a 16-bit filter.
For filters of equal scale, priority is given to the Identifier List mode over the Identifier
Mask mode
For filters of equal scale and mode, priority is given by the filter number (the lower the
number, the higher the priority).
9
8
ID List (32-bit)
ID Mask (32-bit)
ID List (16-bit)
ID List (32-bit)
Deactivated
ID Mask (16-bit)
ID List (32-bit)
Filter
0
1
3
5
6
9
ID Mask (32-bit)
13
FIFO0
Filter
0
1
2
3
4
5
6
7
10
11
12
13
ID Mask (16-bit)
ID List (32-bit)
ID Mask (16-bit)
ID List (16-bit)
Deactivated
ID Mask (16-bit)
ID List (32-bit)
Filter
2
4
7
8
10
11
ID Mask (32-bit)
12
FIFO1
Filter
0
1
2
4
5
6
7
8
11
12
13
14
3
Deactivated
9
10
Num. Num. Bank Bank
ID=Identifier
Controller area network (bxCAN) RM0008
640/1093 Doc ID 13902 Rev 13
Figure 231. Filtering mechanism - example
The example above shows the filtering principle of the bxCAN. On reception of a message,
the identifier is compared first with the filters configured in identifier list mode. If there is a
match, the message is stored in the associated FIFO and the index of the matching filter is
stored in the Filter Match Index. As shown in the example, the identifier matches with
Identifier #2 thus the message content and FMI 2 is stored in the FIFO.
If there is no match, the incoming identifier is then compared with the filters configured in
mask mode.
If the identifier does not match any of the identifiers configured in the filters, the message is
discarded by hardware without disturbing the software.
24.7.5 Message storage
The interface between the software and the hardware for the CAN messages is
implemented by means of mailboxes. A mailbox contains all information related to a
message; identifier, data, control, status and time stamp information.
Transmit mailbox
The software sets up the message to be transmitted in an empty transmit mailbox. The
status of the transmission is indicated by hardware in the CAN_TSR register.
I
d
e
n
t
i
f
i
e
r
L
i
s
t
Message Discarded
I
d
e
n
t
i
f
i
e
r
&
M
a
s
k
Identifier 0
Identifier 1
Identifier 4
Identifier 5
Identifier
2
Mask
Identifier
3
Mask
Identifier
Message Received
Ctrl Data
Identifier #4 Match
Message
Stored
Receive FIFO
No Match
Found
Filter number stored in the
Filter Match Index field
within the CAN_RDTxR
register
FMI
Filter bank
0
2
3
1
4
Example of 3 filter banks in 32-bit Unidentified List mode and
Num
the remaining in 32-bit Identifier Mask mode
RM0008 Controller area network (bxCAN)
Doc ID 13902 Rev 13 641/1093
Receive mailbox
When a message has been received, it is available to the software in the FIFO output
mailbox. Once the software has handled the message (e.g. read it) the software must
release the FIFO output mailbox by means of the RFOM bit in the CAN_RFR register to
make the next incoming message available. The filter match index is stored in the MFMI field
of the CAN_RDTxR register. The 16-bit time stamp value is stored in the TIME[15:0] field of
CAN_RDTxR.
Figure 232. CAN error state diagram
Table 179. Transmit mailbox mapping
Offset to transmit mailbox base address Register name
0 CAN_TIxR
4 CAN_TDTxR
8 CAN_TDLxR
12 CAN_TDHxR
Table 180. Receive mailbox mapping
Offset to receive mailbox base
address (bytes)
Register name
0 CAN_RIxR
4 CAN_RDTxR
8 CAN_RDLxR
12 CAN_RDHxR
ERROR PASSVE
When TEC or REC > 127
When TEC and REC < 128,
ERROR ACTVE
BUS OFF
When TEC > 255 When 128 * 11 recessive bits occur:
ai15903
Controller area network (bxCAN) RM0008
642/1093 Doc ID 13902 Rev 13
24.7.6 Error management
The error management as described in the CAN protocol is handled entirely by hardware
using a Transmit Error Counter (TEC value, in CAN_ESR register) and a Receive Error
Counter (REC value, in the CAN_ESR register), which get incremented or decremented
according to the error condition. For detailed information about TEC and REC management,
please refer to the CAN standard.
Both of them may be read by software to determine the stability of the network.
Furthermore, the CAN hardware provides detailed information on the current error status in
CAN_ESR register. By means of the CAN_IER register (ERRIE bit, etc.), the software can
configure the interrupt generation on error detection in a very flexible way.
Bus-Off recovery
The Bus-Off state is reached when TEC is greater than 255, this state is indicated by BOFF
bit in CAN_ESR register. In Bus-Off state, the bxCAN is no longer able to transmit and
receive messages.
Depending on the ABOM bit in the CAN_MCR register bxCAN will recover from Bus-Off
(become error active again) either automatically or on software request. But in both cases
the bxCAN has to wait at least for the recovery sequence specified in the CAN standard
(128 occurrences of 11 consecutive recessive bits monitored on CANRX).
If ABOM is set, the bxCAN will start the recovering sequence automatically after it has
entered Bus-Off state.
If ABOM is cleared, the software must initiate the recovering sequence by requesting
bxCAN to enter and to leave initialization mode.
Note: In initialization mode, bxCAN does not monitor the CANRX signal, therefore it cannot
complete the recovery sequence. To recover, bxCAN must be in normal mode.
24.7.7 Bit timing
The bit timing logic monitors the serial bus-line and performs sampling and adjustment of
the sample point by synchronizing on the start-bit edge and resynchronizing on the following
edges.
Its operation may be explained simply by splitting nominal bit time into three segments as
follows:
Synchronization segment (SYNC_SEG): a bit change is expected to occur within this
time segment. It has a fixed length of one time quantum (1 x t
CAN
).
Bit segment 1 (BS1): defines the location of the sample point. It includes the
PROP_SEG and PHASE_SEG1 of the CAN standard. Its duration is programmable
between 1 and 16 time quanta but may be automatically lengthened to compensate for
positive phase drifts due to differences in the frequency of the various nodes of the
network.
Bit segment 2 (BS2): defines the location of the transmit point. It represents the
PHASE_SEG2 of the CAN standard. Its duration is programmable between 1 and 8
time quanta but may also be automatically shortened to compensate for negative
phase drifts.
The resynchronization Jump Width (SJW) defines an upper bound to the amount of
lengthening or shortening of the bit segments. It is programmable between 1 and 4 time
quanta.
RM0008 Controller area network (bxCAN)
Doc ID 13902 Rev 13 643/1093
A valid edge is defined as the first transition in a bit time from dominant to recessive bus
level provided the controller itself does not send a recessive bit.
If a valid edge is detected in BS1 instead of SYNC_SEG, BS1 is extended by up to SJW so
that the sample point is delayed.
Conversely, if a valid edge is detected in BS2 instead of SYNC_SEG, BS2 is shortened by
up to SJW so that the transmit point is moved earlier.
As a safeguard against programming errors, the configuration of the Bit Timing Register
(CAN_BTR) is only possible while the device is in Standby mode.
Note: For a detailed description of the CAN bit timing and resynchronization mechanism, please
refer to the ISO 11898 standard.
Figure 233. Bit timing
SYNC_SEG BIT SEGMENT 1 (BS1) BIT SEGMENT 2 (BS2)
NOMINAL BIT TIME
1 x t
q
t
BS1
t
BS2
SAMPLE POINT TRANSMIT POINT
NominalBitTime 1 t
q
t
BS1
t
BS2
+ + =
with:
t
BS1
= t
q
x (TS1[3:0] + 1),
t
BS2
= t
q
x (TS2[2:0] + 1),
t
q
= (BRP[9:0] + 1) x t
PCLK
t
PCLK
= time period of the APB clock,
BRP[9:0], TS1[3:0] and TS2[2:0] are defined in the CAN_BTR Register.
BaudRate
1
NominalBitTime
---------------------------------------------- =
where t
q
refers to the Time quantum
Controller area network (bxCAN) RM0008
644/1093 Doc ID 13902 Rev 13
Figure 234. CAN frames
24.8 bxCAN interrupts
Four interrupt vectors are dedicated to bxCAN. Each interrupt source can be independently
enabled or disabled by means of the CAN Interrupt Enable Register (CAN_IER).
Data Frame or
Remote Frame
Data Field
8 * N
Ctrl Field
6
Arbitration Field
32
CRC Field
16
Ack Field
7
S
O
F
ID DLC CRC
Data Frame (Standard identifier)
44 + 8 * N
Arbitration Field
32
R
T
R
I
D
E
r
0
S
O
F
ID DLC
Remote Frame
44
CRC Field
16 7
CRC
Ctrl Field
6
Overload
Overload Frame
Error
6
Error Delimiter
8
Error Frame
Flag Echo
6
Bus Idle
Inter-Frame Space
Suspend
8
Intermission
3
Transmission
A
C
K
A
C
K
2
2
Inter-Frame Space
or Overload Frame
Inter-Frame Space
Inter-Frame Space
or Overload Frame
Inter-Frame Space
Inter-Frame Space
or Overload Frame Notes:
0 <= N <= 8
SOF = Start Of Frame
ID = Identifier
RTR = Remote Transmission Request
IDE = Identifier Extension Bit
r0 = Reserved Bit
DLC = Data Length Code
CRC = Cyclic Redundancy Code
Error flag: 6 dominant bits if node is error
active else 6 recessive bits.
Suspend transmission: applies to error
passive nodes only.
EOF = End of Frame
ACK = Acknowledge bit
Ctrl = Control
Data Frame or
Remote Frame Any Frame
Inter-Frame Space
or Error Frame
End Of Frame or
Error Delimiter or
Overload Delimiter
Ack Field
EOF
R
T
R
I
D
E
r
0
EOF
Data Field
8 * N
Ctrl Field
6 32
CRC Field
16
Ack Field
7
S
O
F
ID DLC CRC
Data Frame (Extended Identifier)
64 + 8 * N
A
C
K
2
Inter-Frame Space
or Overload Frame Inter-Frame Space
S
R
R
I
D
E
EOF
R
T
R r
1
r
0
32
6
Overload
8 6
Overload
Flag Echo Delimiter
Flag
ai15154
Arbitration Field Arbitration Field
RM0008 Controller area network (bxCAN)
Doc ID 13902 Rev 13 645/1093
Figure 235. Event flags and interrupt generation
The transmit interrupt can be generated by the following events:
Transmit mailbox 0 becomes empty, RQCP0 bit in the CAN_TSR register set.
Transmit mailbox 1 becomes empty, RQCP1 bit in the CAN_TSR register set.
Transmit mailbox 2 becomes empty, RQCP2 bit in the CAN_TSR register set.
The FIFO 0 interrupt can be generated by the following events:
Reception of a new message, FMP0 bits in the CAN_RF0R register are not 00.
FIFO0 full condition, FULL0 bit in the CAN_RF0R register set.
FIFO0 overrun condition, FOVR0 bit in the CAN_RF0R register set.
The FIFO 1 interrupt can be generated by the following events:
Reception of a new message, FMP1 bits in the CAN_RF1R register are not 00.
FIFO1 full condition, FULL1 bit in the CAN_RF1R register set.
FIFO1 overrun condition, FOVR1 bit in the CAN_RF1R register set.
RQCP0
RQCP1
FMP1
CAN_TSR
+
TMEIE
CAN_IER
TRANSMIT
&
FMPIE1
FULL1
&
FFIE1
FOVR1
&
FOVIE1
&
+
CAN_RF1R
FIFO 1
EWGF
EWGIE
EPVF
EPVIE
BOFF
BOFIE
1LEC6
LECIE
&
&
&
&
CAN_ESR
+
&
ERRIE
INTERRUPT
INTERRUPT
FMP0
&
FMPIE0
FULL0
&
FFIE0
FOVR0
&
FOVIE0
+
CAN_RF0R
FIFO 0
INTERRUPT
RQCP2
WKUI
&
WKUIE
CAN_MSR
+
INTERRUPT
ERROR
STATUS CHANGE
ERRI
SLAKI
SLKIE
&
CAN_MSR
Controller area network (bxCAN) RM0008
646/1093 Doc ID 13902 Rev 13
The error and status change interrupt can be generated by the following events:
Error condition, for more details on error conditions please refer to the CAN Error
Status register (CAN_ESR).
Wakeup condition, SOF monitored on the CAN Rx signal.
Entry into Sleep mode.
24.9 CAN registers
The peripheral registers have to be accessed by words (32-bit).
24.9.1 Register access protection
Erroneous access to certain configuration registers can cause the hardware to temporarily
disturb the whole CAN network. Therefore the CAN_BTR register can be modified by
software only while the CAN hardware is in initialization mode.
Although the transmission of incorrect data will not cause problems at the CAN network
level, it can severely disturb the application. A transmit mailbox can be only modified by
software while it is in empty state, refer to Figure 227: Transmit mailbox states.
The filter values can be modified either deactivating the associated filter banks or by setting
the FINIT bit. Moreover, the modification of the filter configuration (scale, mode and FIFO
assignment) in CAN_FMxR, CAN_FSxR and CAN_FFAR registers can only be done when
the filter initialization mode is set (FINIT=1) in the CAN_FMR register.
24.9.2 CAN control and status registers
Refer to Section 1.1 for a list of abbreviations used in register descriptions.
CAN master control register (CAN_MCR)
Address offset: 0x00
Reset value: 0x0001 0002
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
DBF
rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
RESET
Reserved
TTCM ABOM AWUM NART RFLM TXFP SLEEP INRQ
rs rw rw rw rw rw rw rw rw
Bits 31:17 Reserved, forced by hardware to 0.
Bit 16 DBF: Debug freeze
0: CAN working during debug
1: CAN reception/transmission frozen during debug. Reception FIFOs can still be
accessed/controlled normally.
RM0008 Controller area network (bxCAN)
Doc ID 13902 Rev 13 647/1093
Bit 15 RESET: bxCAN software master reset
0: Normal operation.
1: Force a master reset of the bxCAN -> Sleep mode activated after reset (FMP bits and
CAN_MCR register are initialized to the reset values). This bit is automatically reset to 0.
Bits 14:8 Reserved, forced by hardware to 0.
Bit 7 TTCM: Time triggered communication mode
0: Time Triggered Communication mode disabled.
1: Time Triggered Communication mode enabled
Note: For more information on Time Triggered Communication mode, please refer to
Section 24.7.2: Time triggered communication mode.
Bit 6 ABOM: Automatic bus-off management
This bit controls the behavior of the CAN hardware on leaving the Bus-Off state.
0: The Bus-Off state is left on software request, once 128 occurrences of 11 recessive bits
have been monitored and the software has first set and cleared the INRQ bit of the
CAN_MCR register.
1: The Bus-Off state is left automatically by hardware once 128 occurrences of 11 recessive
bits have been monitored.
For detailed information on the Bus-Off state please refer to Section 24.7.6: Error
management.
Bit 5 AWUM: Automatic wakeup mode
This bit controls the behavior of the CAN hardware on message reception during Sleep
mode.
0: The Sleep mode is left on software request by clearing the SLEEP bit of the CAN_MCR
register.
1: The Sleep mode is left automatically by hardware on CAN message detection.
The SLEEP bit of the CAN_MCR register and the SLAK bit of the CAN_MSR register are
cleared by hardware.
Bit 4 NART: No automatic retransmission
0: The CAN hardware will automatically retransmit the message until it has been
successfully transmitted according to the CAN standard.
1: A message will be transmitted only once, independently of the transmission result
(successful, error or arbitration lost).
Bit 3 RFLM: Receive FIFO locked mode
0: Receive FIFO not locked on overrun. Once a receive FIFO is full the next incoming
message will overwrite the previous one.
1: Receive FIFO locked against overrun. Once a receive FIFO is full the next incoming
message will be discarded.
Bit 2 TXFP: Transmit FIFO priority
This bit controls the transmission order when several mailboxes are pending at the same
time.
0: Priority driven by the identifier of the message
1: Priority driven by the request order (chronologically)
Bit 1 SLEEP: Sleep mode request
This bit is set by software to request the CAN hardware to enter the Sleep mode. Sleep
mode will be entered as soon as the current CAN activity (transmission or reception of a
CAN frame) has been completed.
This bit is cleared by software to exit Sleep mode.
This bit is cleared by hardware when the AWUM bit is set and a SOF bit is detected on the
CAN Rx signal.
This bit is set after reset - CAN starts in Sleep mode.
Controller area network (bxCAN) RM0008
648/1093 Doc ID 13902 Rev 13
CAN master status register (CAN_MSR)
Address offset: 0x04
Reset value: 0x0000 0C02
Bit 0 INRQ: Initialization request
The software clears this bit to switch the hardware into normal mode. Once 11 consecutive
recessive bits have been monitored on the Rx signal the CAN hardware is synchronized and
ready for transmission and reception. Hardware signals this event by clearing the INAK bit in
the CAN_MSR register.
Software sets this bit to request the CAN hardware to enter initialization mode. Once
software has set the INRQ bit, the CAN hardware waits until the current CAN activity
(transmission or reception) is completed before entering the initialization mode. Hardware
signals this event by setting the INAK bit in the CAN_MSR register.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved.
RX SAMP RXM TXM
Reserved
SLAKI WKUI ERRI SLAK INAK
r r r r rc_w1 rc_w1 rc_w1 r r
Bits 31:12 Reserved, forced by hardware to 0.
Bit 11 RX: CAN Rx signal
Monitors the actual value of the CAN_RX Pin.
Bit 10 SAMP: Last sample point
The value of RX on the last sample point (current received bit value).
Bit 9 RXM: Receive mode
The CAN hardware is currently receiver.
Bit 8 TXM: Transmit mode
The CAN hardware is currently transmitter.
Bits 7:5 Reserved, forced by hardware to 0.
Bit 4 SLAKI: Sleep acknowledge interrupt
When SLKIE=1, this bit is set by hardware to signal that the bxCAN has entered Sleep
Mode. When set, this bit generates a status change interrupt if the SLKIE bit in the
CAN_IER register is set.
This bit is cleared by software or by hardware, when SLAK is cleared.
Note: When SLKIE=0, no polling on SLAKI is possible. In this case the SLAK bit can be
polled.
Bit 3 WKUI: Wakeup interrupt
This bit is set by hardware to signal that a SOF bit has been detected while the CAN
hardware was in Sleep mode. Setting this bit generates a status change interrupt if the
WKUIE bit in the CAN_IER register is set.
This bit is cleared by software.
RM0008 Controller area network (bxCAN)
Doc ID 13902 Rev 13 649/1093
CAN transmit status register (CAN_TSR)
Address offset: 0x08
Reset value: 0x1C00 0000
Bit 2 ERRI: Error interrupt
This bit is set by hardware when a bit of the CAN_ESR has been set on error detection and
the corresponding interrupt in the CAN_IER is enabled. Setting this bit generates a status
change interrupt if the ERRIE bit in the CAN_IER register is set.
This bit is cleared by software.
Bit 1 SLAK: Sleep acknowledge
This bit is set by hardware and indicates to the software that the CAN hardware is now in
Sleep mode. This bit acknowledges the Sleep mode request from the software (set SLEEP
bit in CAN_MCR register).
This bit is cleared by hardware when the CAN hardware has left Sleep mode (to be
synchronized on the CAN bus). To be synchronized the hardware has to monitor a
sequence of 11 consecutive recessive bits on the CAN RX signal.
Note: The process of leaving Sleep mode is triggered when the SLEEP bit in the CAN_MCR
register is cleared. Please refer to the AWUM bit of the CAN_MCR register description
for detailed information for clearing SLEEP bit
Bit 0 INAK: Initialization acknowledge
This bit is set by hardware and indicates to the software that the CAN hardware is now in
initialization mode. This bit acknowledges the initialization request from the software (set
INRQ bit in CAN_MCR register).
This bit is cleared by hardware when the CAN hardware has left the initialization mode (to
be synchronized on the CAN bus). To be synchronized the hardware has to monitor a
sequence of 11 consecutive recessive bits on the CAN RX signal.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
LOW2 LOW1 LOW0 TME2 TME1 TME0 CODE[1:0]
ABRQ
2
Reserved
TERR
2
ALST2
TXOK
2
RQCP
2
r r r r r r r r rs rc_w1 rc_w1 rc_w1 rc_w1
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
ABRQ
1
Reserved
Res.
TERR
1
ALST1
TXOK
1
RQCP
1
ABRQ
0
Reserved
TERR
0
ALST0
TXOK
0
RQCP
0
rs rc_w1 rc_w1 rc_w1 rc_w1 rs rc_w1 rc_w1 rc_w1 rc_w1
Bit 31 LOW2: Lowest priority flag for mailbox 2
This bit is set by hardware when more than one mailbox are pending for transmission and
mailbox 2 has the lowest priority.
Bit 30 LOW1: Lowest priority flag for mailbox 1
This bit is set by hardware when more than one mailbox are pending for transmission and
mailbox 1 has the lowest priority.
Bit 29 LOW0: Lowest priority flag for mailbox 0
This bit is set by hardware when more than one mailbox are pending for transmission and
mailbox 0 has the lowest priority.
Note: The LOW[2:0] bits are set to zero when only one mailbox is pending.
Bit 28 TME2: Transmit mailbox 2 empty
This bit is set by hardware when no transmit request is pending for mailbox 2.
Controller area network (bxCAN) RM0008
650/1093 Doc ID 13902 Rev 13
Bit 27 TME1: Transmit mailbox 1 empty
This bit is set by hardware when no transmit request is pending for mailbox 1.
Bit 26 TME0: Transmit mailbox 0 empty
This bit is set by hardware when no transmit request is pending for mailbox 0.
Bits 25:24 CODE[1:0]: Mailbox code
In case at least one transmit mailbox is free, the code value is equal to the number of the
next transmit mailbox free.
In case all transmit mailboxes are pending, the code value is equal to the number of the
transmit mailbox with the lowest priority.
Bit 23 ABRQ2: Abort request for mailbox 2
Set by software to abort the transmission request for the corresponding mailbox.
Cleared by hardware when the mailbox becomes empty.
Setting this bit has no effect when the mailbox is not pending for transmission.
Bits 22:20 Reserved, forced by hardware to 0.
Bit 19 TERR2: Transmission error of mailbox 2
This bit is set when the previous TX failed due to an error.
Bit 18 ALST2: Arbitration lost for mailbox 2
This bit is set when the previous TX failed due to an arbitration lost.
Bit 17 TXOK2: Transmission OK of mailbox 2
The hardware updates this bit after each transmission attempt.
0: The previous transmission failed
1: The previous transmission was successful
This bit is set by hardware when the transmission request on mailbox 2 has been completed
successfully. Please refer to Figure 227.
Bit 16 RQCP2: Request completed mailbox2
Set by hardware when the last request (transmit or abort) has been performed.
Cleared by software writing a 1 or by hardware on transmission request (TXRQ2 set in
CAN_TMID2R register).
Clearing this bit clears all the status bits (TXOK2, ALST2 and TERR2) for Mailbox 2.
Bit 15 ABRQ1: Abort request for mailbox 1
Set by software to abort the transmission request for the corresponding mailbox.
Cleared by hardware when the mailbox becomes empty.
Setting this bit has no effect when the mailbox is not pending for transmission.
Bits 14:12 Reserved, forced by hardware to 0.
Bit 11 TERR1: Transmission error of mailbox1
This bit is set when the previous TX failed due to an error.
Bit 10 ALST1: Arbitration lost for mailbox1
This bit is set when the previous TX failed due to an arbitration lost.
Bit 9 TXOK1: Transmission OK of mailbox1
The hardware updates this bit after each transmission attempt.
0: The previous transmission failed
1: The previous transmission was successful
This bit is set by hardware when the transmission request on mailbox 1 has been completed
successfully. Please refer to Figure 227
RM0008 Controller area network (bxCAN)
Doc ID 13902 Rev 13 651/1093
CAN receive FIFO 0 register (CAN_RF0R)
Address offset: 0x0C
Reset value: 0x00
Bit 8 RQCP1: Request completed mailbox1
Set by hardware when the last request (transmit or abort) has been performed.
Cleared by software writing a 1 or by hardware on transmission request (TXRQ1 set in
CAN_TI1R register).
Clearing this bit clears all the status bits (TXOK1, ALST1 and TERR1) for Mailbox 1.
Bit 7 ABRQ0: Abort request for mailbox0
Set by software to abort the transmission request for the corresponding mailbox.
Cleared by hardware when the mailbox becomes empty.
Setting this bit has no effect when the mailbox is not pending for transmission.
Bits 6:4 Reserved, forced by hardware to 0.
Bit 3 TERR0: Transmission error of mailbox0
This bit is set when the previous TX failed due to an error.
Bit 2 ALST0: Arbitration lost for mailbox0
This bit is set when the previous TX failed due to an arbitration lost.
Bit 1 TXOK0: Transmission OK of mailbox0
The hardware updates this bit after each transmission attempt.
0: The previous transmission failed
1: The previous transmission was successful
This bit is set by hardware when the transmission request on mailbox 1 has been completed
successfully. Please refer to Figure 227
Bit 0 RQCP0: Request completed mailbox0
Set by hardware when the last request (transmit or abort) has been performed.
Cleared by software writing a 1 or by hardware on transmission request (TXRQ0 set in
CAN_TI0R register).
Clearing this bit clears all the status bits (TXOK0, ALST0 and TERR0) for Mailbox 0.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
RFOM0 FOVR0 FULL0
Res.
FMP0[1:0]
rs rc_w1 rc_w1 r r
Bit 31:6 Reserved, forced by hardware to 0.
Bit 5 RFOM0: Release FIFO 0 output mailbox
Set by software to release the output mailbox of the FIFO. The output mailbox can only be
released when at least one message is pending in the FIFO. Setting this bit when the FIFO
is empty has no effect. If at least two messages are pending in the FIFO, the software has to
release the output mailbox to access the next message.
Cleared by hardware when the output mailbox has been released.
Controller area network (bxCAN) RM0008
652/1093 Doc ID 13902 Rev 13
CAN receive FIFO 1 register (CAN_RF1R)
Address offset: 0x10
Reset value: 0x00
Bit 4 FOVR0: FIFO 0 overrun
This bit is set by hardware when a new message has been received and passed the filter
while the FIFO was full.
This bit is cleared by software.
Bit 3 FULL0: FIFO 0 full
Set by hardware when three messages are stored in the FIFO.
This bit is cleared by software.
Bit 2 Reserved, forced by hardware to 0.
Bits 1:0 FMP0[1:0]: FIFO 0 message pending
These bits indicate how many messages are pending in the receive FIFO.
FMP is increased each time the hardware stores a new message in to the FIFO. FMP is
decreased each time the software releases the output mailbox by setting the RFOM0 bit.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
RFOM1 FOVR1 FULL1
Res.
FMP1[1:0]
rs rc_w1 rc_w1 r r
Bits 31:6 Reserved, forced by hardware to 0.
Bit 5 RFOM1: Release FIFO 1 output mailbox
Set by software to release the output mailbox of the FIFO. The output mailbox can only be
released when at least one message is pending in the FIFO. Setting this bit when the FIFO
is empty has no effect. If at least two messages are pending in the FIFO, the software has to
release the output mailbox to access the next message.
Cleared by hardware when the output mailbox has been released.
Bit 4 FOVR1: FIFO 1 overrun
This bit is set by hardware when a new message has been received and passed the filter
while the FIFO was full.
This bit is cleared by software.
Bit 3 FULL1: FIFO 1 full
Set by hardware when three messages are stored in the FIFO.
This bit is cleared by software.
Bit 2 Reserved, forced by hardware to 0.
Bits 1:0 FMP1[1:0]: FIFO 1 message pending
These bits indicate how many messages are pending in the receive FIFO1.
FMP1 is increased each time the hardware stores a new message in to the FIFO1. FMP is
decreased each time the software releases the output mailbox by setting the RFOM1 bit.
RM0008 Controller area network (bxCAN)
Doc ID 13902 Rev 13 653/1093
CAN interrupt enable register (CAN_IER)
Address offset: 0x14
Reset value: 0x00
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
SLKIE WKUIE
rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
ERRIE
Reserved
LEC
IE
BOF
IE
EPV
IE
EWG
IE
Res.
FOV
IE1
FF
IE1
FMP
IE1
FOV
IE0
FF
IE0
FMP
IE0
TME
IE
rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:18 Reserved, forced by hardware to 0.
Bit 17 SLKIE: Sleep interrupt enable
0: No interrupt when SLAKI bit is set.
1: Interrupt generated when SLAKI bit is set.
Bit 16 WKUIE: Wakeup interrupt enable
0: No interrupt when WKUI is set.
1: Interrupt generated when WKUI bit is set.
Bit 15 ERRIE: Error interrupt enable
0: No interrupt will be generated when an error condition is pending in the CAN_ESR.
1: An interrupt will be generation when an error condition is pending in the CAN_ESR.
Bits 14:12 Reserved, forced by hardware to 0.
Bit 11 LECIE: Last error code interrupt enable
0: ERRI bit will not be set when the error code in LEC[2:0] is set by hardware on error
detection.
1: ERRI bit will be set when the error code in LEC[2:0] is set by hardware on error detection.
Bit 10 BOFIE: Bus-off interrupt enable
0: ERRI bit will not be set when BOFF is set.
1: ERRI bit will be set when BOFF is set.
Bit 9 EPVIE: Error passive interrupt enable
0: ERRI bit will not be set when EPVF is set.
1: ERRI bit will be set when EPVF is set.
Bit 8 EWGIE: Error warning interrupt enable
0: ERRI bit will not be set when EWGF is set.
1: ERRI bit will be set when EWGF is set.
Bit 7 Reserved, forced by hardware to 0.
Bit 6 FOVIE1: FIFO overrun interrupt enable
0: No interrupt when FOVR is set.
1: Interrupt generation when FOVR is set.
Bit 5 FFIE1: FIFO full interrupt enable
0: No interrupt when FULL bit is set.
1: Interrupt generated when FULL bit is set.
Controller area network (bxCAN) RM0008
654/1093 Doc ID 13902 Rev 13
CAN error status register (CAN_ESR)
Address offset: 0x18
Reset value: 0x00
Bit 4 FMPIE1: FIFO message pending interrupt enable
0: No interrupt generated when state of FMP[1:0] bits are not 00b.
1: Interrupt generated when state of FMP[1:0] bits are not 00b.
Bit 3 FOVIE0: FIFO overrun interrupt enable
0: No interrupt when FOVR bit is set.
1: Interrupt generated when FOVR bit is set.
Bit 2 FFIE0: FIFO full interrupt enable
0: No interrupt when FULL bit is set.
1: Interrupt generated when FULL bit is set.
Bit 1 FMPIE0: FIFO message pending interrupt enable
0: No interrupt generated when state of FMP[1:0] bits are not 00b.
1: Interrupt generated when state of FMP[1:0] bits are not 00b.
Bit 0 TMEIE: Transmit mailbox empty interrupt enable
0: No interrupt when RQCPx bit is set.
1: Interrupt generated when RQCPx bit is set.
Note: Refer to Section 24.8: bxCAN interrupts.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
REC[7:0] TEC[7:0]
r r r r r r r r r r r r r r r r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
LEC[2:0]
Res.
BOFF EPVF EWGF
rw rw rw r r r
Bits 31:24 REC[7:0]: Receive error counter
The implementing part of the fault confinement mechanism of the CAN protocol. In case of
an error during reception, this counter is incremented by 1 or by 8 depending on the error
condition as defined by the CAN standard. After every successful reception the counter is
decremented by 1 or reset to 120 if its value was higher than 128. When the counter value
exceeds 127, the CAN controller enters the error passive state.
Bits 23:16 TEC[7:0]: Least significant byte of the 9-bit transmit error counter
The implementing part of the fault confinement mechanism of the CAN protocol.
Bits 15:7 Reserved, forced by hardware to 0.
RM0008 Controller area network (bxCAN)
Doc ID 13902 Rev 13 655/1093
CAN bit timing register (CAN_BTR)
Address offset: 0x1C
Reset value: 0x0123 0000
Note: This register can only be accessed by the software when the CAN hardware is in
initialization mode.
Bits 6:4 LEC[2:0]: Last error code
This field is set by hardware and holds a code which indicates the error condition of the last
error detected on the CAN bus. If a message has been transferred (reception or
transmission) without error, this field will be cleared to 0.
The LEC[2:0] bits can be set to value 0b111 by software. They are updated by hardware to
indicate the current communication status.
000: No Error
001: Stuff Error
010: Form Error
011: Acknowledgment Error
100: Bit recessive Error
101: Bit dominant Error
110: CRC Error
111: Set by software
Bit 3 Reserved, forced by hardware to 0.
Bit 2 BOFF: Bus-off flag
This bit is set by hardware when it enters the bus-off state. The bus-off state is entered on
TEC overflow, greater than 255, refer to Section 24.7.6 on page 642.
Bit 1 EPVF: Error passive flag
This bit is set by hardware when the Error Passive limit has been reached (Receive Error
Counter or Transmit Error Counter>127).
Bit 0 EWGF: Error warning flag
This bit is set by hardware when the warning limit has been reached
(Receive Error Counter or Transmit Error Counter96).
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
SILM LBKM
Reserved
SJW[1:0] Res. TS2[2:0] TS1[3:0]
rw rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
BRP[9:0]
rw rw rw rw rw rw rw rw rw rw
Bit 31 SILM: Silent mode (debug)
0: Normal operation
1: Silent Mode
Bit 30 LBKM: Loop back mode (debug)
0: Loop Back Mode disabled
1: Loop Back Mode enabled
Bits 29:26 Reserved, forced by hardware to 0.
Controller area network (bxCAN) RM0008
656/1093 Doc ID 13902 Rev 13
24.9.3 CAN mailbox registers
This chapter describes the registers of the transmit and receive mailboxes. Refer to
Section 24.7.5: Message storage on page 640 for detailed register mapping.
Transmit and receive mailboxes have the same registers except:
The FMI field in the CAN_RDTxR register.
A receive mailbox is always write protected.
A transmit mailbox is write-enabled only while empty, corresponding TME bit in the
CAN_TSR register set.
There are 3 TX Mailboxes and 2 RX Mailboxes. Each RX Mailbox allows access to a 3 level
depth FIFO, the access being offered only to the oldest received message in the FIFO.
Each mailbox consist of 4 registers.
Bits 25:24 SJW[1:0]: Resynchronization jump width
These bits define the maximum number of time quanta the CAN hardware is allowed to
lengthen or shorten a bit to perform the resynchronization.
t
RJW
= t
CAN
x (SJW[1:0] + 1)
Bit 23 Reserved, forced by hardware to 0.
Bits 22:20 TS2[2:0]: Time segment 2
These bits define the number of time quanta in Time Segment 2.
t
BS2
= t
CAN
x (TS2[2:0] + 1)
Bits 19:16 TS1[3:0]: Time segment 1
These bits define the number of time quanta in Time Segment 1
t
BS1
= t
CAN
x (TS1[3:0] + 1)
For more information on bit timing, please refer to Section 24.7.7: Bit timing on page 642.
Bits 15:10 Reserved, forced by hardware to 0.
Bits 9:0 BRP[9:0]: Baud rate prescaler
These bits define the length of a time quanta.
t
q
= (BRP[9:0]+1) x t
PCLK
CAN_RI0R
CAN_RDT0R
CAN_RL0R
CAN_RH0R
CAN_TI0R
CAN_TDT0R
CAN_TDL0R
CAN_TDH0R
FIFO0 Three Tx Mailboxes
CAN_RI1R
CAN_RDT1R
CAN_RL1R
CAN_RH1R
FIFO1
CAN_TI1R
CAN_TDT1R
CAN_TDL1R
CAN_TDH1R
CAN_TI2R
CAN_TDT2R
CAN_TDL2R
CAN_TDH2R
RM0008 Controller area network (bxCAN)
Doc ID 13902 Rev 13 657/1093
CAN TX mailbox identifier register (CAN_TIxR) (x=0..2)
Address offsets: 0x180, 0x190, 0x1A0
Reset value: undefined (except bit 0, TXRQ = 0)
Note: 1 All TX registers are write protected when the mailbox is pending transmission (TMEx reset).
2 This register also implements the TX request control (bit 0) - reset value 0.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
STID[10:0]/EXID[28:18] EXID[17:13]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
EXID[12:0] IDE RTR TXRQ
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:21 STID[10:0]/EXID[28:18]: Standard identifier or extended identifier
The standard identifier or the MSBs of the extended identifier (depending on the IDE bit
value).
Bit 20:3 EXID[17:0]: Extended identifier
The LSBs of the extended identifier.
Bit 2 IDE: Identifier extension
This bit defines the identifier type of message in the mailbox.
0: Standard identifier.
1: Extended identifier.
Bit 1 RTR: Remote transmission request
0: Data frame
1: Remote frame
Bit 0 TXRQ: Transmit mailbox request
Set by software to request the transmission for the corresponding mailbox.
Cleared by hardware when the mailbox becomes empty.
Controller area network (bxCAN) RM0008
658/1093 Doc ID 13902 Rev 13
CAN mailbox data length control and time stamp register (CAN_TDTxR)
(x=0..2)
All bits of this register are write protected when the mailbox is not in empty state.
Address offsets: 0x184, 0x194, 0x1A4
Reset value: undefined
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
TIME[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
TGT
Reserved
DLC[3:0]
rw rw rw rw rw
Bits 31:16 TIME[15:0]: Message time stamp
This field contains the 16-bit timer value captured at the SOF transmission.
Bits 15:9 Reserved
Bit 8 TGT: Transmit global time
This bit is active only when the hardware is in the Time Trigger Communication mode, TTCM
bit of the CAN_MCR register is set.
0: Time stamp TIME[15:0] is not sent.
1: Time stamp TIME[15:0] value is sent in the last two data bytes of the 8-byte message:
TIME[7:0] in data byte 7 and TIME[15:8] in data byte 6, replacing the data written in
CAN_TDHxR[31:16] register (DATA6[7:0] and DATA7[7:0]). DLC must be programmed as 8
in order these two bytes to be sent over the CAN bus.
Bits 7:4 Reserved
Bits 3:0 DLC[3:0]: Data length code
This field defines the number of data bytes a data frame contains or a remote frame request.
A message can contain from 0 to 8 data bytes, depending on the value in the DLC field.
RM0008 Controller area network (bxCAN)
Doc ID 13902 Rev 13 659/1093
CAN mailbox data low register (CAN_TDLxR) (x=0..2)
All bits of this register are write protected when the mailbox is not in empty state.
Address offsets: 0x188, 0x198, 0x1A8
Reset value: undefined
CAN mailbox data high register (CAN_TDHxR) (x=0..2)
All bits of this register are write protected when the mailbox is not in empty state.
Address offsets: 0x18C, 0x19C, 0x1AC
Reset value: undefined
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
DATA3[7:0] DATA2[7:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
DATA1[7:0] DATA0[7:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:24 DATA3[7:0]: Data byte 3
Data byte 3 of the message.
Bits 23:16 DATA2[7:0]: Data byte 2
Data byte 2 of the message.
Bits 15:8 DATA1[7:0]: Data byte 1
Data byte 1 of the message.
Bits 7:0 DATA0[7:0]: Data byte 0
Data byte 0 of the message.
A message can contain from 0 to 8 data bytes and starts with byte 0.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
DATA7[7:0] DATA6[7:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
DATA5[7:0] DATA4[7:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:24 DATA7[7:0]: Data byte 7
Data byte 7 of the message.
Note: If TGT of this message and TTCM are active, DATA7 and DATA6 will be replaced by the
TIME stamp value.
Bits 23:16 DATA6[7:0]: Data byte 6
Data byte 6 of the message.
Bits 15:8 DATA5[7:0]: Data byte 5
Data byte 5 of the message.
Bits 7:0 DATA4[7:0]: Data byte 4
Data byte 4 of the message.
Controller area network (bxCAN) RM0008
660/1093 Doc ID 13902 Rev 13
CAN receive FIFO mailbox identifier register (CAN_RIxR) (x=0..1)
Address offsets: 0x1B0, 0x1C0
Reset value: undefined
Note: All RX registers are write protected.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
STID[10:0]/EXID[28:18] EXID[17:13]
r r r r r r r r r r r r r r r r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
EXID[12:0] IDE RTR
Res.
r r r r r r r r r r r r r r r
Bits 31:21 STID[10:0]/EXID[28:18]: Standard identifier or extended identifier
The standard identifier or the MSBs of the extended identifier (depending on the IDE bit
value).
Bits 20:3 EXID[17:0]: Extended identifier
The LSBs of the extended identifier.
Bit 2 IDE: Identifier extension
This bit defines the identifier type of message in the mailbox.
0: Standard identifier.
1: Extended identifier.
Bit 1 RTR: Remote transmission request
0: Data frame
1: Remote frame
Bit 0 Reserved
RM0008 Controller area network (bxCAN)
Doc ID 13902 Rev 13 661/1093
CAN receive FIFO mailbox data length control and time stamp register
(CAN_RDTxR) (x=0..1)
Address offsets: 0x1B4, 0x1C4
Reset value: undefined
Note: All RX registers are write protected.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
TIME[15:0]
r r r r r r r r r r r r r r r r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
FMI[7:0]
Reserved
DLC[3:0]
r r r r r r r r r r r r
Bits 31:16 TIME[15:0]: Message time stamp
This field contains the 16-bit timer value captured at the SOF detection.
Bits 15:8 FMI[7:0]: Filter match index
This register contains the index of the filter the message stored in the mailbox passed
through. For more details on identifier filtering please refer to Section 24.7.4: Identifier
filtering on page 636 - Filter Match Index paragraph.
Bits 7:4 Reserved, forced by hardware to 0.
Bits 3:0 DLC[3:0]: Data length code
This field defines the number of data bytes a data frame contains (0 to 8). It is 0 in the case
of a remote frame request.
Controller area network (bxCAN) RM0008
662/1093 Doc ID 13902 Rev 13
CAN receive FIFO mailbox data low register (CAN_RDLxR) (x=0..1)
All bits of this register are write protected when the mailbox is not in empty state.
Address offsets: 0x1B8, 0x1C8
Reset value: undefined
Note: All RX registers are write protected.
CAN receive FIFO mailbox data high register (CAN_RDHxR) (x=0..1)
Address offsets: 0x1BC, 0x1CC
Reset value: undefined
Note: All RX registers are write protected.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
DATA3[7:0] DATA2[7:0]
r r r r r r r r r r r r r r r r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
DATA1[7:0] DATA0[7:0]
r r r r r r r r r r r r r r r r
Bits 31:24 DATA3[7:0]: Data Byte 3
Data byte 3 of the message.
Bits 23:16 DATA2[7:0]: Data Byte 2
Data byte 2 of the message.
Bits 15:8 DATA1[7:0]: Data Byte 1
Data byte 1 of the message.
Bits 7:0 DATA0[7:0]: Data Byte 0
Data byte 0 of the message.
A message can contain from 0 to 8 data bytes and starts with byte 0.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
DATA7[7:0] DATA6[7:0]
r r r r r r r r r r r r r r r r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
DATA5[7:0] DATA4[7:0]
r r r r r r r r r r r r r r r r
Bits 31:24 DATA7[7:0]: Data Byte 7
Data byte 3 of the message.
Bits 23:16 DATA6[7:0]: Data Byte 6
Data byte 2 of the message.
RM0008 Controller area network (bxCAN)
Doc ID 13902 Rev 13 663/1093
24.9.4 CAN filter registers
CAN filter master register (CAN_FMR)
Address offset: 0x200
Reset value: 0x2A1C 0E01
Note: All bits of this register are set and cleared by software.
Bits 15:8 DATA5[7:0]: Data Byte 5
Data byte 1 of the message.
Bits 7:0 DATA4[7:0]: Data Byte 4
Data byte 0 of the message.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
FINIT
rw
Reserved
CAN2SB[5:0]
Reserved
FINIT
rw rw rw rw rw rw rw
Bits 31:14 Reserved, forced to reset value
Bits 13:8 CAN2SB[5:0]: CAN2 start bank
These bits are set and cleared by software. They define the start bank for the CAN2
interface (Slave) in the range 1 to 27.
Bits 7:1 Reserved, forced to reset value
Bit 0 FINIT: Filter init mode
Initialization mode for filter banks
0: Active filters mode.
1: Initialization mode for the filters.
Controller area network (bxCAN) RM0008
664/1093 Doc ID 13902 Rev 13
CAN filter mode register (CAN_FM1R)
Address offset: 0x204
Reset value: 0x00
Note: This register can be written only when the filter initialization mode is set (FINIT=1) in the
CAN_FMR register.
Note: Please refer to Figure 229: Filter bank scale configuration - register organization on
page 638
CAN filter scale register (CAN_FS1R)
Address offset: 0x20C
Reset value: 0x00
Note: This register can be written only when the filter initialization mode is set (FINIT=1) in the
CAN_FMR register.
Note: Please refer to Figure 229: Filter bank scale configuration - register organization on
page 638
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
FBM27 FBM26 FBM25 FBM24 FBM23 FBM22 FBM21 FBM20 FBM19 FBM18 FBM17 FBM16
rw rw rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
FBM15 FBM14 FBM13 FBM12 FBM11 FBM10 FBM9 FBM8 FBM7 FBM6 FBM5 FBM4 FBM3 FBM2 FBM1 FBM0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:28 Reserved, must be kept at reset value.
Bits 27:0 FBMx: Filter mode
Mode of the registers of Filter x.
0: Two 32-bit registers of filter bank x are in Identifier Mask mode.
1: Two 32-bit registers of filter bank x are in Identifier List mode.
Note: Bits 27:14 are available in connectivity line devices only and are reserved otherwise.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
FSC27 FSC26 FSC25 FSC24 FSC23 FSC22 FSC21 FSC20 FSC19 FSC18 FSC17 FSC16
rw rw rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
FSC15 FSC14 FSC13 FSC12 FSC11 FSC10 FSC9 FSC8 FSC7 FSC6 FSC5 FSC4 FSC3 FSC2 FSC1 FSC0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:28 Reserved, forced by hardware to 0.
Bits 27:0 FSCx: Filter scale configuration
These bits define the scale configuration of Filters 13-0.
0: Dual 16-bit scale configuration
1: Single 32-bit scale configuration
Note: Bits 27:14 are available in connectivity line devices only and are reserved otherwise.
RM0008 Controller area network (bxCAN)
Doc ID 13902 Rev 13 665/1093
CAN filter FIFO assignment register (CAN_FFA1R)
Address offset: 0x214
Reset value: 0x00
Note: This register can be written only when the filter initialization mode is set (FINIT=1) in the
CAN_FMR register.
CAN filter activation register (CAN_FA1R)
Address offset: 0x21C
Reset value: 0x00
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
FFA27 FFA26 FFA25 FFA24 FFA23 FFA22 FFA21 FFA20 FFA19 FFA18 FFA17 FFA16
rw rw rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
FFA15 FFA14 FFA13 FFA12 FFA11 FFA10 FFA9 FFA8 FFA7 FFA6 FFA5 FFA4 FFA3 FFA2 FFA1 FFA0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:28 Reserved, forced by hardware to 0.
Bits 27:0 FFAx: Filter FIFO assignment for filter x
The message passing through this filter will be stored in the specified FIFO.
0: Filter assigned to FIFO 0
1: Filter assigned to FIFO 1
Note: Bits 27:14 are available in connectivity line devices only and are reserved otherwise.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
FACT27 FACT26 FACT25 FACT24 FACT23 FACT22 FACT21 FACT20 FACT19 FACT18 FACT17 FACT16
rw rw rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
FACT15 FACT14 FACT13 FACT12 FACT11 FACT10 FACT9 FACT8 FACT7 FACT6 FACT5 FACT4 FACT3 FACT2 FACT1 FACT0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:28 Reserved, forced by hardware to 0.
Bits 27:0 FACTx: Filter active
The software sets this bit to activate Filter x. To modify the Filter x registers (CAN_FxR[0:7]),
the FACTx bit must be cleared or the FINIT bit of the CAN_FMR register must be set.
0: Filter x is not active
1: Filter x is active
Note: Bits 27:14 are available in connectivity line devices only and are reserved otherwise.
Controller area network (bxCAN) RM0008
666/1093 Doc ID 13902 Rev 13
Filter bank i register x (CAN_FiRx) (i=0..27, x=1, 2)
Address offsets: 0x240..0x31C
Reset value: undefined
Note: n connectivity line devices there are 28 filter banks, i=0 .. 27, in other devices there are 14
filter banks i = 0 ..13. Each filter bank i is composed of two 32-bit registers, CAN_FiR[2:1].
This register can only be modified when the FACTx bit of the CAN_FAxR register is cleared
or when the FINIT bit of the CAN_FMR register is set.
In all configurations:
Note: Depending on the scale and mode configuration of the filter the function of each register can
differ. For the filter mapping, functions description and mask registers association, refer to
Section 24.7.4: Identifier filtering on page 636.
A Mask/Identifier register in mask mode has the same bit mapping as in identifier list
mode.
For the register mapping/addresses of the filter banks please refer to the Table 181 on
page 667.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
FB31 FB30 FB29 FB28 FB27 FB26 FB25 FB24 FB23 FB22 FB21 FB20 FB19 FB18 FB17 FB16
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
FB15 FB14 FB13 FB12 FB11 FB10 FB9 FB8 FB7 FB6 FB5 FB4 FB3 FB2 FB1 FB0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:0 FB[31:0]: Filter bits
Identifier
Each bit of the register specifies the level of the corresponding bit of the expected identifier.
0: Dominant bit is expected
1: Recessive bit is expected
Mask
Each bit of the register specifies whether the bit of the associated identifier register must
match with the corresponding bit of the expected identifier or not.
0: Dont care, the bit is not used for the comparison
1: Must match, the bit of the incoming identifier must have the same level has specified in
the corresponding identifier register of the filter.
RM0008 Controller area network (bxCAN)
Doc ID 13902 Rev 13 667/1093
24.9.5 bxCAN register map
Refer to Table 3 on page 50 for the register boundary addresses. In connectivity line
devices, the registers from offset 0x200 to 31C are present only in CAN1.
Table 181. bxCAN register map and reset values
Offset Register
3
1
3
0
2
9
2
8
2
7
2
6
2
5
2
4
2
3
2
2
2
1
2
0
1
9
1
8
1
7
1
6
1
5
1
4
1
3
1
2
1
1
1
0
9 8 7 6 5 4 3 2 1 0
0x000
CAN_MCR
Reserved
D
B
F
R
E
S
E
T
Reserved
T
T
C
M
A
B
O
M
A
W
U
M
N
A
R
T
R
F
L
M
T
X
F
P
S
L
E
E
P
I
N
R
Q
Reset value 1 0 0 0 0 0 0 0 1 0
0x004
CAN_MSR
Reserved R
X
S
A
M
P
R
X
M
T
X
M
Reserved
S
L
A
K
I
W
K
U
I
E
R
R
I
S
L
A
K
I
N
A
K
Reset value 1 1 0 0 0 0 0 1 0
0x008
CAN_TSR LOW[2:0] TME[2:0]
C
O
D
E
[
1
:
0
]
A
B
R
Q
2
Reserved
T
E
R
R
2
A
L
S
T
2
T
X
O
K
2
R
Q
C
P
2
A
B
R
Q
1
Reserved
T
E
R
R
1
A
L
S
T
1
T
X
O
K
1
R
Q
C
P
1
A
B
R
Q
0
Reserved
T
E
R
R
0
A
L
S
T
0
T
X
O
K
0
R
Q
C
P
0
Reset value 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x00C
CAN_RF0R
Reserved
R
F
O
M
0
F
O
V
R
0
F
U
L
L
0
R
e
s
e
r
v
e
d
F
M
P
0
[
1
:
0
]
Reset value 0 0 0 0 0
0x010
CAN_RF1R
Reserved
R
F
O
M
1
F
O
V
R
1
F
U
L
L
1
R
e
s
e
r
v
e
d
F
M
P
1
[
1
:
0
]
Reset value 0 0 0 0 0
0x014
CAN_IER
Reserved
S
L
K
I
E
W
K
U
I
E
E
R
R
I
E
Reserved
L
E
C
I
E
B
O
F
I
E
E
P
V
I
E
E
W
G
I
E
R
e
s
e
r
v
e
d
F
O
V
I
E
1
F
F
I
E
1
F
M
P
I
E
1
F
O
V
I
E
0
F
F
I
E
0
F
M
P
I
E
0
T
M
E
I
E
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x018
CAN_ESR REC[7:0] TEC[7:0]
Reserved
L
E
C
[
2
:
0
]
R
e
s
e
r
v
e
d
B
O
F
F
E
P
V
F
E
W
G
F
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x01C
CAN_BTR
S
I
L
M
L
B
K
M
Reserved
S
J
W
[
1
:
0
]
R
e
s
e
r
v
e
d
TS2[2:0] TS1[3:0]
Reserved
BRP[9:0]
Reset value 0 0 0 0 0 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0
0x020-
0x17F
Reserved
0x180
CAN_TI0R STID[10:0]/EXID[28:18] EXID[17:0]
I
D
E
R
T
R
T
X
R
Q
Reset value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x 0
0x184
CAN_TDT0R TIME[15:0]
Reserved
T
G
T
Reserved
DLC[3:0]
Reset value x x x x x x x x x x x x x x x x x x x x x
0x188
CAN_TDL0R DATA3[7:0] DATA2[7:0] DATA1[7:0] DATA0[7:0]
Reset value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
0x18C
CAN_TDH0R DATA7[7:0] DATA6[7:0] DATA5[7:0] DATA4[7:0]
Reset value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
0x190
CAN_TI1R STID[10:0]/EXID[28:18] EXID[17:0]
I
D
E
R
T
R
T
X
R
Q
Reset value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x 0
Controller area network (bxCAN) RM0008
668/1093 Doc ID 13902 Rev 13
0x194
CAN_TDT1R TIME[15:0]
Reserved
T
G
T
Reserved
DLC[3:0]
Reset value x x x x x x x x x x x x x x x x x x x x x
0x198
CAN_TDL1R DATA3[7:0] DATA2[7:0] DATA1[7:0] DATA0[7:0]
Reset value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
0x19C
CAN_TDH1R DATA7[7:0] DATA6[7:0] DATA5[7:0] DATA4[7:0]
Reset value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
0x1A0
CAN_TI2R STID[10:0]/EXID[28:18] EXID[17:0]
I
D
E
R
T
R
T
X
R
Q
Reset value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x 0
0x1A4
CAN_TDT2R TIME[15:0]
Reserved
T
G
T
Reserved
DLC[3:0]
Reset value x x x x x x x x x x x x x x x x x x x x x
0x1A8
CAN_TDL2R DATA3[7:0] DATA2[7:0] DATA1[7:0] DATA0[7:0]
Reset value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
0x1AC
CAN_TDH2R DATA7[7:0] DATA6[7:0] DATA5[7:0] DATA4[7:0]
Reset value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
0x1B0
CAN_RI0R STID[10:0]/EXID[28:18] EXID[17:0]
I
D
E
R
T
R
R
e
s
e
r
v
e
d
Reset value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
0x1B4
CAN_RDT0R TIME[15:0] FMI[7:0]
Reserved
DLC[3:0]
Reset value x x x x x x x x x x x x x x x x x x x x x x x x x x x x
0x1B8
CAN_RDL0R DATA3[7:0] DATA2[7:0] DATA1[7:0] DATA0[7:0]
Reset value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
0x1BC
CAN_RDH0R DATA7[7:0] DATA6[7:0] DATA5[7:0] DATA4[7:0]
Reset value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
0x1C0
CAN_RI1R STID[10:0]/EXID[28:18] EXID[17:0]
I
D
E
R
T
R
R
e
s
e
r
v
e
d
Reset value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
0x1C4
CAN_RDT1R TIME[15:0] FMI[7:0]
Reserved
DLC[3:0]
Reset value x x x x x x x x x x x x x x x x x x x x x x x x x x x x
0x1C8
CAN_RDL1R DATA3[7:0] DATA2[7:0] DATA1[7:0] DATA0[7:0]
Reset value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
0x1CC
CAN_RDH1R DATA7[7:0] DATA6[7:0] DATA5[7:0] DATA4[7:0]
Reset value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
0x1D0-
0x1FF
Reserved
Table 181. bxCAN register map and reset values (continued)
Offset Register
3
1
3
0
2
9
2
8
2
7
2
6
2
5
2
4
2
3
2
2
2
1
2
0
1
9
1
8
1
7
1
6
1
5
1
4
1
3
1
2
1
1
1
0
9 8 7 6 5 4 3 2 1 0
RM0008 Controller area network (bxCAN)
Doc ID 13902 Rev 13 669/1093
0x200
CAN_FMR
Reserved
CAN2SB[5:0]
Reserved
F
I
N
I
T
Reset value 0 0 1 1 1 0 1
0x204
CAN_FM1R
Reserved
FBM[27:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x208 Reserved
0x20C
CAN_FS1R
Reserved
FSC[27:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x210 Reserved
0x214
CAN_FFA1R
Reserved
FFA[27:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x218 Reserved
0x21C
CAN_FA1R
Reserved
FACT[27:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x220 Reserved
0x224-
0x23F
Reserved
0x240
CAN_F0R1 FB[31:0]
Reset value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
0x244
CAN_F0R2 FB[31:0]
Reset value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
0x248
CAN_F1R1 FB[31:0]
Reset value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
0x24C
CAN_F1R2 FB[31:0]
Reset value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
.
.
.
.
.
.
.
.
.
.
.
.
0x318
CAN_F27R1 FB[31:0]
Reset value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
0x31C
CAN_F27R2 FB[31:0]
Reset value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
Table 181. bxCAN register map and reset values (continued)
Offset Register
3
1
3
0
2
9
2
8
2
7
2
6
2
5
2
4
2
3
2
2
2
1
2
0
1
9
1
8
1
7
1
6
1
5
1
4
1
3
1
2
1
1
1
0
9 8 7 6 5 4 3 2 1 0
Serial peripheral interface (SPI) RM0008
670/1093 Doc ID 13902 Rev 13
25 Serial peripheral interface (SPI)
Low-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 16 and 32 Kbytes.
Medium-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 64 and 128 Kbytes.
High-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 256 and 512 Kbytes.
XL-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 768 Kbytes and 1 Mbyte.
Connectivity line devices are STM32F105xx and STM32F107xx microcontrollers.
25.1 SPI introduction
In high-density, XL-density and connectivity line devices, the SPI interface gives the
flexibility to get either the SPI protocol or the I
2
S audio protocol. By default, it is the SPI
function that is selected. It is possible to switch the interface from SPI to I
2
S by software.
In low- and medium-density devices, the I
2
S protocol is not available.
The serial peripheral interface (SPI) allows half/ full-duplex, synchronous, serial
communication with external devices. The interface can be configured as the master and in
this case it provides the communication clock (SCK) to the external slave device. The
interface is also capable of operating in multimaster configuration.
It may be used for a variety of purposes, including Simplex synchronous transfers on two
lines with a possible bidirectional data line or reliable communication using CRC checking.
The I
2
S is also a synchronous, serial communication interface with a 3-pin protocol. It can
address four different audio standards including the I
2
S Phillips standard, the MSB- and
LSB-justified standards and the PCM standard. It can operate in slave or master mode with
half-duplex communication. Master clock may be provided by the interface to an external
slave component when the I
2
S is configured as the communication master.
Warning: Since some SPI3/I2S3 pins are shared with JTAG pins
(SPI3_NSS/I2S3_WS with JTDI and SPI3_SCK/I2S3_CK with
JTDO), they are not controlled by the IO controller and are
reserved for JTAG usage (after each Reset).
For this purpose, prior to configure the SPI3/I2S3 pins, the
user has to disable the JTAG and use the SWD interface
(when debugging the application), or disable both JTAG/SWD
interfaces (for standalone applications). For more
information on the configuration of JTAG/SWD interface pins,
please refer to Section 9.3.5: JTAG/SWD alternate function
remapping.
RM0008 Serial peripheral interface (SPI)
Doc ID 13902 Rev 13 671/1093
25.2 SPI and I
2
S main features
25.2.1 SPI features
Full-duplex synchronous transfers on three lines
Simplex synchronous transfers on two lines with or without a bidirectional data line
8- or 16-bit transfer frame format selection
Master or slave operation
Multimaster mode capability
8 master mode baud rate prescalers (f
PCLK
/2 max.)
Slave mode frequency (f
PCLK
/2 max)
Faster communication for both master and slave
NSS management by hardware or software for both master and slave: dynamic change
of master/slave operations
Programmable clock polarity and phase
Programmable data order with MSB-first or LSB-first shifting
Dedicated transmission and reception flags with interrupt capability
SPI bus busy status flag
Hardware CRC feature for reliable communication:
CRC value can be transmitted as last byte in Tx mode
Automatic CRC error checking for last received byte
Master mode fault, overrun and CRC error flags with interrupt capability
1-byte transmission and reception buffer with DMA capability: Tx and Rx requests
Serial peripheral interface (SPI) RM0008
672/1093 Doc ID 13902 Rev 13
25.2.2 I
2
S features
Simplex communication (only transmitter or receiver)
Master or slave operations
8-bit programmable linear prescaler to reach accurate audio sample frequencies (from
8 kHz to 96 kHz)
Data format may be 16-bit, 24-bit or 32-bit
Packet frame is fixed to 16-bit (16-bit data frame) or 32-bit (16-bit, 24-bit, 32-bit data
frame) by audio channel
Programmable clock polarity (steady state)
Underrun flag in slave transmission mode and Overrun flag in reception mode (master
and slave)
16-bit register for transmission and reception with one data register for both channel
sides
Supported I
2
S protocols:
I
2
S Phillips standard
MSB-justified standard (left-justified)
LSB-justified standard (right-justified)
PCM standard (with short and long frame synchronization on 16-bit channel frame
or 16-bit data frame extended to 32-bit channel frame)
Data direction is always MSB first
DMA capability for transmission and reception (16-bit wide)
Master clock may be output to drive an external audio component. Ratio is fixed at
256 F
S
(where F
S
is the audio sampling frequency)
In connectivity line devices, both I
2
S (I2S2 and I2S3) have a dedicated PLL (PLL3) to
generate an even more accurate clock.
RM0008 Serial peripheral interface (SPI)
Doc ID 13902 Rev 13 673/1093
25.3 SPI functional description
25.3.1 General description
The block diagram of the SPI is shown in Figure 236.
Figure 236. SPI block diagram
Usually, the SPI is connected to external devices through 4 pins:
MISO: Master In / Slave Out data. This pin can be used to transmit data in slave mode
and receive data in master mode.
MOSI: Master Out / Slave In data. This pin can be used to transmit data in master
mode and receive data in slave mode.
SCK: Serial Clock output for SPI masters and input for SPI slaves.
NSS: Slave select. This is an optional pin to select a slave device. This pin acts as a
chip select to let the SPI master communicate with slaves individually and to avoid
contention on the data lines. Slave NSS inputs can be driven by standard IO ports on
the master device. The NSS pin may also be used as an output if enabled (SSOE bit)
and driven low if the SPI is in master configuration. In this manner, all NSS pins from
devices connected to the Master NSS pin see a low level and become slaves when
they are configured in NSS hardware mode. When configured in master mode with
NSS configured as an input (MSTR=1 and SSOE=0) and if NSS is pulled low, the SPI
enters the master mode fault state: the MSTR bit is automatically cleared and the
device is configured in slave mode (refer to Section 25.3.10: Error flags on page 691).
MOSI
MISO
Baud rate generator
SCK
Master control logic
Communication
control
SPE BR2 BR1 BR0 MSTR CPOL CPHA
BR[2:0]
RXNE
LSB
BIDI
MODE
BIDI
OE
SSM SSI
BSY OVR
MOD
RXNE TXE
ERR TXE
0 0
DFF
0 SSOE
CRC
EN
0
RX
ONLY
CRC
Next
CRC
ERR
0
1
NSS
IE
F
FIRST
SPI_CR1
SPI_CR2
SPI_SR
TXDM
AEN
RXDM
AEN IE IE
Address and data bus
Read
Rx buffer
Shift register
LSB first
Tx buffer
Write
ai14744
Serial peripheral interface (SPI) RM0008
674/1093 Doc ID 13902 Rev 13
A basic example of interconnections between a single master and a single slave is
illustrated in Figure 237.
Figure 237. Single master/ single slave application
1. Here, the NSS pin is configured as an input.
The MOSI pins are connected together and the MISO pins are connected together. In this
way data is transferred serially between master and slave (most significant bit first).
The communication is always initiated by the master. When the master device transmits
data to a slave device via the MOSI pin, the slave device responds via the MISO pin. This
implies full-duplex communication with both data out and data in synchronized with the
same clock signal (which is provided by the master device via the SCK pin).
Slave select (NSS) pin management
Hardware or software slave select management can be set using the SSM bit in the
SPI_CR1 register.
Software NSS management (SSM = 1) : with this configuration, slave select
information is driven internally by the SSI bit value in register SPI_CR1. The external
NSS pin remains free for other application uses.
Hardware NSS management (SSM = 0): in this case two configurations are available
depending on the NSS output configuration (SSOE bit in register SPI_CR1).
NSS output enable (SSM=0,SSOE = 1) : this configuration is only used when the
device is set as master. The NSS signal is driven low when the master starts the
communication and is kept low until the SPI is disabled.
NSS output disable (SSM=0, SSOE = 0) : For devices set as master, this configuration
allows multi master capability. For devices set as slave, the NSS pin works as a classical
NSS input, the slave is selected when the NSS line is at low level and is not selected if NSS
line is at high level.
Clock phase and clock polarity
Four possible timing relationships may be chosen by software, using the CPOL and CPHA
bits in the SPI_CR1 register. The CPOL (clock polarity) bit controls the steady state value of
the clock when no data is being transferred. This bit affects both master and slave modes. If
CPOL is reset, the SCK pin has a low-level idle state. If CPOL is set, the SCK pin has a
high-level idle state.
8-bit shift register
SPI clock
generator
8-bit shift register
MISO
MOSI MOSI
MISO
SCK SCK
Slave Master
NSS
(1)
NSS
(1)
V
DD
MSBit LSBit MSBit LSBit
Not used if NSS is managed
by software
ai14745
RM0008 Serial peripheral interface (SPI)
Doc ID 13902 Rev 13 675/1093
If the CPHA (clock phase) bit is set, the second edge on the SCK pin (falling edge if the
CPOL bit is reset, rising edge if the CPOL bit is set) is the MSBit capture strobe. Data are
latched on the occurrence of the second clock transition. If the CPHA bit is reset, the first
edge on the SCK pin (falling edge if CPOL bit is set, rising edge if CPOL bit is reset) is the
MSBit capture strobe. Data are latched on the occurrence of the first clock transition.
The combination of the CPOL (clock polarity) and CPHA (clock phase) bits selects the data
capture clock edge.
Figure 238, shows an SPI transfer with the four combinations of the CPHA and CPOL bits.
The diagram may be interpreted as a master or slave timing diagram where the SCK pin, the
MISO pin, the MOSI pin are directly connected between the master and the slave device.
Note: 1 Prior to changing the CPOL/CPHA bits the SPI must be disabled by resetting the SPE bit.
2 Master and slave must be programmed with the same timing mode.
3 The idle state of SCK must correspond to the polarity selected in the SPI_CR1 register (by
pulling up SCK if CPOL=1 or pulling down SCK if CPOL=0).
4 The Data Frame Format (8- or 16-bit) is selected through the DFF bit in SPI_CR1 register,
and determines the data length during transmission/reception.
Figure 238. Data clock timing diagram
1. These timings are shown with the LSBFIRST bit reset in the SPI_CR1 register.
CPOL = 1
CPOL = 0
MSBit LSBit
MSBit LSBit
MSO
MOS
NSS
(to slave)
Capture strobe
CPHA =1
CPOL = 1
CPOL = 0
MSBit LSBit
MSBit LSBit
MSO
MOS
NSS
(to slave)
Capture strobe
CPHA =0
8 or 16 bits depending on the Data frame format bit (see DFF in SP_CR1)
8 or 16 bits depending on the Data frame format bit (see DFF in SP_CR1)
ai17154b
Serial peripheral interface (SPI) RM0008
676/1093 Doc ID 13902 Rev 13
Data frame format
Data can be shifted out either MSB-first or LSB-first depending on the value of the
LSBFIRST bit in the SPI_CR1 Register.
Each data frame is 8 or 16 bits long depending on the size of the data programmed using
the DFF bit in the SPI_CR1 register. The selected data frame format is applicable for
transmission and/or reception.
25.3.2 Configuring the SPI in slave mode
In the slave configuration, the serial clock is received on the SCK pin from the master
device. The value set in the BR[2:0] bits in the SPI_CR1 register, does not affect the data
transfer rate.
Note: It is recommended to enable the SPI slave before the master sends the clock. If not,
undesired data transmission might occur. The data register of the slave needs to be ready
before the first edge of the communication clock or before the end of the ongoing
communication. It is mandatory to have the polarity of the communication clock set to the
steady state value before the slave and the master are enabled.
Follow the procedure below to configure the SPI in slave mode:
Procedure
1. Set the DFF bit to define 8- or 16-bit data frame format
2. Select the CPOL and CPHA bits to define one of the four relationships between the
data transfer and the serial clock (see Figure 238). For correct data transfer, the CPOL
and CPHA bits must be configured in the same way in the slave device and the master
device.
3. The frame format (MSB-first or LSB-first depending on the value of the LSBFIRST bit in
the SPI_CR1 register) must be the same as the master device.
4. In Hardware mode (refer to Slave select (NSS) pin management on page 674), the
NSS pin must be connected to a low level signal during the complete byte transmit
sequence. In NSS software mode, set the SSM bit and clear the SSI bit in the SPI_CR1
register.
5. Clear the MSTR bit and set the SPE bit (both in the SPI_CR1 register) to assign the
pins to alternate functions.
In this configuration the MOSI pin is a data input and the MISO pin is a data output.
Transmit sequence
The data byte is parallel-loaded into the Tx buffer during a write cycle.
The transmit sequence begins when the slave device receives the clock signal and the most
significant bit of the data on its MOSI pin. The remaining bits (the 7 bits in 8-bit data frame
format, and the 15 bits in 16-bit data frame format) are loaded into the shift-register. The
TXE flag in the SPI_SR register is set on the transfer of data from the Tx Buffer to the shift
register and an interrupt is generated if the TXEIE bit in the SPI_CR2 register is set.
RM0008 Serial peripheral interface (SPI)
Doc ID 13902 Rev 13 677/1093
Receive sequence
For the receiver, when data transfer is complete:
The Data in shift register is transferred to Rx Buffer and the RXNE flag (SPI_SR
register) is set
An Interrupt is generated if the RXNEIE bit is set in the SPI_CR2 register.
After the last sampling clock edge the RXNE bit is set, a copy of the data byte received in
the shift register is moved to the Rx buffer. When the SPI_DR register is read, the SPI
peripheral returns this buffered value.
Clearing of the RXNE bit is performed by reading the SPI_DR register.
Serial peripheral interface (SPI) RM0008
678/1093 Doc ID 13902 Rev 13
25.3.3 Configuring the SPI in master mode
In the master configuration, the serial clock is generated on the SCK pin.
Procedure
1. Select the BR[2:0] bits to define the serial clock baud rate (see SPI_CR1 register).
2. Select the CPOL and CPHA bits to define one of the four relationships between the
data transfer and the serial clock (see Figure 238).
3. Set the DFF bit to define 8- or 16-bit data frame format
4. Configure the LSBFIRST bit in the SPI_CR1 register to define the frame format.
5. If the NSS pin is required in input mode, in hardware mode, connect the NSS pin to a
high-level signal during the complete byte transmit sequence. In NSS software mode,
set the SSM and SSI bits in the SPI_CR1 register. If the NSS pin is required in output
mode, the SSOE bit only should be set.
6. The MSTR and SPE bits must be set (they remain set only if the NSS pin is connected
to a high-level signal).
In this configuration the MOSI pin is a data output and the MISO pin is a data input.
Transmit sequence
The transmit sequence begins when a byte is written in the Tx Buffer.
The data byte is parallel-loaded into the shift register (from the internal bus) during the first
bit transmission and then shifted out serially to the MOSI pin MSB first or LSB first
depending on the LSBFIRST bit in the SPI_CR1 register. The TXE flag is set on the transfer
of data from the Tx Buffer to the shift register and an interrupt is generated if the TXEIE bit in
the SPI_CR2 register is set.
Receive sequence
For the receiver, when data transfer is complete:
The data in the shift register is transferred to the RX Buffer and the RXNE flag is set
An interrupt is generated if the RXNEIE bit is set in the SPI_CR2 register
At the last sampling clock edge the RXNE bit is set, a copy of the data byte received in the
shift register is moved to the Rx buffer. When the SPI_DR register is read, the SPI
peripheral returns this buffered value.
Clearing the RXNE bit is performed by reading the SPI_DR register.
A continuous transmit stream can be maintained if the next data to be transmitted is put in
the Tx buffer once the transmission is started. Note that TXE flag should be 1 before any
attempt to write the Tx buffer is made.
Note: When a master is communicating with SPI slaves which need to be de-selected between
transmissions, the NSS pin must be configured as GPIO or another GPIO must be used and
toggled by software.
RM0008 Serial peripheral interface (SPI)
Doc ID 13902 Rev 13 679/1093
25.3.4 Configuring the SPI for Simplex communication
The SPI is capable of operating in simplex mode in 2 configurations.
1 clock and 1 bidirectional data wire
1 clock and 1 data wire (receive-only or transmit-only)
1 clock and 1 bidirectional data wire (BIDIMODE=1)
This mode is enabled by setting the BIDIMODE bit in the SPI_CR1 register. In this mode
SCK is used for the clock and MOSI in master or MISO in slave mode is used for data
communication. The transfer direction (Input/Output) is selected by the BIDIOE bit in the
SPI_CR1 register. When this bit is 1, the data line is output otherwise it is input.
1 clock and 1 unidirectional data wire (BIDIMODE=0)
In this mode, the application can use the SPI either in transmit-only mode or in receive-only
mode.
Transmit-only mode is similar to full-duplex mode (BIDIMODE=0, RXONLY=0): the data
are transmitted on the transmit pin (MOSI in master mode or MISO in slave mode) and
the receive pin (MISO in master mode or MOSI in slave mode) can be used as a
general-purpose IO. In this case, the application just needs to ignore the Rx buffer (if
the data register is read, it does not contain the received value).
In receive-only mode, the application can disable the SPI output function by setting the
RXONLY bit in the SPI_CR2 register. In this case, it frees the transmit IO pin (MOSI in
master mode or MISO in slave mode), so it can be used for other purposes.
To start the communication in receive-only mode, configure and enable the SPI:
In master mode, the communication starts immediately and stops when the SPE bit is
cleared and the current reception stops. There is no need to read the BSY flag in this
mode. It is always set when an SPI communication is ongoing.
In slave mode, the SPI continues to receive as long as the NSS is pulled down (or the
SSI bit is cleared in NSS software mode) and the SCK is running.
25.3.5 Data transmission and reception procedures
Rx and Tx buffers
In reception, data are received and then stored into an internal Rx buffer while In
transmission, data are first stored into an internal Tx buffer before being transmitted.
A read access of the SPI_DR register returns the Rx buffered value whereas a write access
to the SPI_DR stores the written data into the Tx buffer.
Serial peripheral interface (SPI) RM0008
680/1093 Doc ID 13902 Rev 13
Start sequence in master mode
In full-duplex (BIDIMODE=0 and RXONLY=0)
The sequence begins when data are written into the SPI_DR register (Tx buffer).
The data are then parallel loaded from the Tx buffer into the 8-bit shift register
during the first bit transmission and then shifted out serially to the MOSI pin.
At the same time, the received data on the MISO pin is shifted in serially to the 8-
bit shift register and then parallel loaded into the SPI_DR register (Rx buffer).
In unidirectional receive-only mode (BIDIMODE=0 and RXONLY=1)
The sequence begins as soon as SPE=1
Only the receiver is activated and the received data on the MISO pin are shifted in
serially to the 8-bit shift register and then parallel loaded into the SPI_DR register
(Rx buffer).
In bidirectional mode, when transmitting (BIDIMODE=1 and BIDIOE=1)
The sequence begins when data are written into the SPI_DR register (Tx buffer).
The data are then parallel loaded from the Tx buffer into the 8-bit shift register
during the first bit transmission and then shifted out serially to the MOSI pin.
No data are received.
In bidirectional mode, when receiving (BIDIMODE=1 and BIDIOE=0)
The sequence begins as soon as SPE=1 and BIDIOE=0.
The received data on the MOSI pin are shifted in serially to the 8-bit shift register
and then parallel loaded into the SPI_DR register (Rx buffer).
The transmitter is not activated and no data are shifted out serially to the MOSI
pin.
Start sequence in slave mode
In full-duplex mode (BIDIMODE=0 and RXONLY=0)
The sequence begins when the slave device receives the clock signal and the first
bit of the data on its MOSI pin. The 7 remaining bits are loaded into the shift
register.
At the same time, the data are parallel loaded from the Tx buffer into the 8-bit shift
register during the first bit transmission, and then shifted out serially to the MISO
pin. The software must have written the data to be sent before the SPI master
device initiates the transfer.
In unidirectional receive-only mode (BIDIMODE=0 and RXONLY=1)
The sequence begins when the slave device receives the clock signal and the first
bit of the data on its MOSI pin. The 7 remaining bits are loaded into the shift
register.
The transmitter is not activated and no data are shifted out serially to the MISO
pin.
In bidirectional mode, when transmitting (BIDIMODE=1 and BIDIOE=1)
The sequence begins when the slave device receives the clock signal and the first
bit in the Tx buffer is transmitted on the MISO pin.
The data are then parallel loaded from the Tx buffer into the 8-bit shift register
during the first bit transmission and then shifted out serially to the MISO pin. The
software must have written the data to be sent before the SPI master device
initiates the transfer.
No data are received.
RM0008 Serial peripheral interface (SPI)
Doc ID 13902 Rev 13 681/1093
In bidirectional mode, when receiving (BIDIMODE=1 and BIDIOE=0)
The sequence begins when the slave device receives the clock signal and the first
bit of the data on its MISO pin.
The received data on the MISO pin are shifted in serially to the 8-bit shift register
and then parallel loaded into the SPI_DR register (Rx buffer).
The transmitter is not activated and no data are shifted out serially to the MISO
pin.
Handling data transmission and reception
The TXE flag (Tx buffer empty) is set when the data are transferred from the Tx buffer to the
shift register. It indicates that the internal Tx buffer is ready to be loaded with the next data.
An interrupt can be generated if the TXEIE bit in the SPI_CR2 register is set. Clearing the
TXE bit is performed by writing to the SPI_DR register.
Note: The software must ensure that the TXE flag is set to 1 before attempting to write to the Tx
buffer. Otherwise, it overwrites the data previously written to the Tx buffer.
The RXNE flag (Rx buffer not empty) is set on the last sampling clock edge, when the data
are transferred from the shift register to the Rx buffer. It indicates that data are ready to be
read from the SPI_DR register. An interrupt can be generated if the RXNEIE bit in the
SPI_CR2 register is set. Clearing the RXNE bit is performed by reading the SPI_DR
register.
For some configurations, the BSY flag can be used during the last data transfer to wait until
the completion of the transfer.
Full-duplex transmit and receive procedure in master or slave mode
(BIDIMODE=0 and RXONLY=0)
The software has to follow this procedure to transmit and receive data (see Figure 239 and
Figure 240):
1. Enable the SPI by setting the SPE bit to 1.
2. Write the first data item to be transmitted into the SPI_DR register (this clears the TXE
flag).
3. Wait until TXE=1 and write the second data item to be transmitted. Then wait until
RXNE=1 and read the SPI_DR to get the first received data item (this clears the RXNE
bit). Repeat this operation for each data item to be transmitted/received until the n1
received data.
4. Wait until RXNE=1 and read the last received data.
5. Wait until TXE=1 and then wait until BSY=0 before disabling the SPI.
This procedure can also be implemented using dedicated interrupt subroutines launched at
each rising edges of the RXNE or TXE flag.
Serial peripheral interface (SPI) RM0008
682/1093 Doc ID 13902 Rev 13
Figure 239. TXE/RXNE/BSY behavior in Master / full-duplex mode (BIDIMODE=0 and RXONLY=0)
in the case of continuous transfers
Figure 240. TXE/RXNE/BSY behavior in Slave / full-duplex mode (BIDIMODE=0, RXONLY=0) in the
case of continuous transfers
MISO/MOSI (in)
Tx buffer
DATA1 = 0xA1
TXE flag
0xF2
BSY flag
0xF3
software
writes 0xF1
into SPI_DR
software waits
until TXE=1 and
writes 0xF2 into
SPI_DR
software waits
until RXNE=1
and reads 0xA1
from SPI_DR
set by hardware
cleared by software
set by hardware
cleared by software set by hardware
set by hardware
SCK
DATA 2 = 0xA2 DATA 3 = 0xA3
reset by hardware
Example in Master mode with CPOL=1, CPHA=1
0xF1
RXNE flag
(write SPI_DR)
Rx buffer
set by hardware
MISO/MOSI (out)
DATA1 = 0xF1 DATA2 = 0xF2 DATA3 = 0xF3
(read SPI_DR)
0xA1 0xA2 0xA3
software waits
until TXE=1 and
writes 0xF3 into
SPI_DR
software waits
until RXNE=1
and reads 0xA2
from SPI_ DR
software waits
until RXNE=1
and reads 0xA3
from SPI_DR
b0 b1 b2 b3 b4 b5 b6 b7 b0 b1 b2 b3 b4 b5 b6 b7 b0 b1 b2 b3 b4 b5 b6 b7
b0 b1 b2 b3 b4 b5 b6 b7 b0 b1 b2 b3 b4 b5 b6 b7 b0 b1 b2 b3 b4 b5 b6 b7
cleared by software
ai17343
0xF1
set by cleared by software
MISO/MOSI (in)
Tx buffer
DATA 1 = 0xA1
TXE flag
0xF2
BSY flag
0xF3
software
writes 0xF1
into SPI_DR
software waits
until TXE=1 and
writes 0xF2 into
SPI_DR
software waits
until RXNE=1
and reads 0xA1
from SPI_DR
set by hardware
cleared by software
set by hardware
cleared by software set by hardware
SCK
DATA 2 = 0xA2 DATA 3 = 0xA3
reset by hardware
Example in Slave mode with CPOL=1, CPHA=1
RXNE flag
(write to SPI_DR)
Rx buffer
set by hardware
MISO/MOSI (out)
DATA 1 = 0xF1 DATA 2 = 0xF2 DATA 3 = 0xF3
(read from SPI_DR)
0xA1 0xA2 0xA3
software waits
until TXE=1 and
writes 0xF3 into
SPI_DR
software waits
until RXNE=1
and reads 0xA2
from SPI_ DR
software waits
until RXNE=1
and reads 0xA3
from SPI_DR
b0 b1 b2 b3 b4 b5 b6 b7 b0 b1 b2 b3 b4 b5 b6 b7 b0 b1 b2 b3 b4 b5 b6 b7
b0 b1 b2 b3 b4 b5 b6 b7 b0 b1 b2 b3 b4 b5 b6 b7 b0 b1 b2 b3 b4 b5 b6 b7
cleared by software
ai17344
RM0008 Serial peripheral interface (SPI)
Doc ID 13902 Rev 13 683/1093
Transmit-only procedure (BIDIMODE=0 RXONLY=0)
In this mode, the procedure can be reduced as described below and the BSY bit can be
used to wait until the completion of the transmission (see Figure 241 and Figure 242).
1. Enable the SPI by setting the SPE bit to 1.
2. Write the first data item to send into the SPI_DR register (this clears the TXE bit).
3. Wait until TXE=1 and write the next data item to be transmitted. Repeat this step for
each data item to be transmitted.
4. After writing the last data item into the SPI_DR register, wait until TXE=1, then wait until
BSY=0, this indicates that the transmission of the last data is complete.
This procedure can be also implemented using dedicated interrupt subroutines launched at
each rising edge of the TXE flag.
Note: 1 During discontinuous communications, there is a 2 APB clock period delay between the
write operation to SPI_DR and the BSY bit setting. As a consequence, in transmit-only
mode, it is mandatory to wait first until TXE is set and then until BSY is cleared after writing
the last data.
2 After transmitting two data items in transmit-only mode, the OVR flag is set in the SPI_SR
register since the received data are never read.
Figure 241. TXE/BSY behavior in Master transmit-only mode (BIDIMODE=0 and RXONLY=0) in the
case of continuous transfers
0xF1
Tx buffer
TXE flag
0xF2
BSY flag
0xF3
software writes
0xF1 into
SPI_DR
software waits
until TXE=1 and
writes 0xF2 into
SPI_DR
set by hardware
cleared by software
set by hardware
cleared by software set by hardware
set by hardware
SCK
reset by hardware
Example in Master mode with CPOL=1, CPHA=1
(write to SPI_DR)
MISO/MOSI (out)
DATA 1 = 0xF1 DATA 2 = 0xF2 DATA 3 = 0xF3
software waits
until TXE=1 and
writes 0xF3 into
SPI_DR
software waits until BSY=0 software waits until TXE=1
b0 b1 b2 b3 b4 b5 b6 b7 b0 b1 b2 b3 b4 b5 b6 b7 b0 b1 b2 b3 b4 b5 b6 b7
ai17345
Serial peripheral interface (SPI) RM0008
684/1093 Doc ID 13902 Rev 13
Figure 242. TXE/BSY in Slave transmit-only mode (BIDIMODE=0 and RXONLY=0) in the case of
continuous transfers
Bidirectional transmit procedure (BIDIMODE=1 and BIDIOE=1)
In this mode, the procedure is similar to the procedure in Transmit-only mode except that the
BIDIMODE and BIDIOE bits both have to be set in the SPI_CR2 register before enabling the
SPI.
Unidirectional receive-only procedure (BIDIMODE=0 and RXONLY=1)
In this mode, the procedure can be reduced as described below (see Figure 243):
1. Set the RXONLY bit in the SPI_CR2 register.
2. Enable the SPI by setting the SPE bit to 1:
a) In master mode, this immediately activates the generation of the SCK clock, and
data are serially received until the SPI is disabled (SPE=0).
b) In slave mode, data are received when the SPI master device drives NSS low and
generates the SCK clock.
3. Wait until RXNE=1 and read the SPI_DR register to get the received data (this clears
the RXNE bit). Repeat this operation for each data item to be received.
This procedure can also be implemented using dedicated interrupt subroutines launched at
each rising edge of the RXNE flag.
Note: If it is required to disable the SPI after the last transfer, follow the recommendation described
in Section 25.3.8: Disabling the SPI on page 689.
0xF1
Tx buffer
TXE flag
0xF2
BSY flag
0xF3
software writes
0xF1 into
SPI_DR
software waits
until TXE=1 and
writes 0xF2 into
SPI_DR
set by hardware
cleared by software
set by hardware
cleared by software set by hardware
set by hardware
SCK
reset by hardware
Example in slave mode with CPOL=1, CPHA=1
(write to SPI_DR)
MISO/MOSI (out)
DATA 1 = 0xF1 DATA 2 = 0xF2 DATA 3 = 0xF3
software waits
until TXE=1 and
writes 0xF3 into
SPI_DR
software waits until BSY=0 software waits until TXE=1
b0 b1 b2 b3 b4 b5 b6 b7 b0 b1 b2 b3 b4 b5 b6 b7 b0 b1 b2 b3 b4 b5 b6 b7
ai17346
RM0008 Serial peripheral interface (SPI)
Doc ID 13902 Rev 13 685/1093
Figure 243. RXNE behavior in receive-only mode (BIDIRMODE=0 and RXONLY=1) in the case of
continuous transfers
Bidirectional receive procedure (BIDIMODE=1 and BIDIOE=0)
In this mode, the procedure is similar to the Receive-only mode procedure except that the
BIDIMODE bit has to be set and the BIDIOE bit cleared in the SPI_CR2 register before
enabling the SPI.
Continuous and discontinuous transfers
When transmitting data in master mode, if the software is fast enough to detect each rising
edge of TXE (or TXE interrupt) and to immediately write to the SPI_DR register before the
ongoing data transfer is complete, the communication is said to be continuous. In this case,
there is no discontinuity in the generation of the SPI clock between each data item and the
BSY bit is never cleared between each data transfer.
On the contrary, if the software is not fast enough, this can lead to some discontinuities in
the communication. In this case, the BSY bit is cleared between each data transmission
(see Figure 244).
In Master receive-only mode (RXONLY=1), the communication is always continuous and the
BSY flag is always read at 1.
In slave mode, the continuity of the communication is decided by the SPI master device. In
any case, even if the communication is continuous, the BSY flag goes low between each
transfer for a minimum duration of one SPI clock cycle (see Figure 242).
MISO/MOSI (in)
DATA 1 = 0xA1
software waits until RXNE=1
and reads 0xA1 from SPI_DR
SCK
DATA 2 = 0xA2 DATA 3 = 0xA3
Example with CPOL=1, CPHA=1, RXONLY=1
RXNE flag
Rx buffer
set by hardware
(read from SPI_DR)
0xA1 0xA2 0xA3
software waits until RXNE=1
and reads 0xA2 from SPI_DR
software waits until RXNE=1
and reads 0xA3 from SPI_DR
b0 b1 b2 b3 b4 b5 b6 b7 b0 b1 b2 b3 b4 b5 b6 b7 b0 b1 b2 b3 b4 b5 b6 b7
cleared by software
ai17347
Serial peripheral interface (SPI) RM0008
686/1093 Doc ID 13902 Rev 13
Figure 244. TXE/BSY behavior when transmitting (BIDIRMODE=0 and RXONLY=0) in the case of
discontinuous transfers
25.3.6 CRC calculation
A CRC calculator has been implemented for communication reliability. Separate CRC
calculators are implemented for transmitted data and received data. The CRC is calculated
using a programmable polynomial serially on each bit. It is calculated on the sampling clock
edge defined by the CPHA and CPOL bits in the SPI_CR1 register.
Note: This SPI offers two kinds of CRC calculation standard which depend directly on the data
frame format selected for the transmission and/or reception: 8-bit data (CR8) and 16-bit data
(CRC16).
CRC calculation is enabled by setting the CRCEN bit in the SPI_CR1 register. This action
resets the CRC registers (SPI_RXCRCR and SPI_TXCRCR). In full duplex or transmitter
only mode, when the transfers are managed by the software (CPU mode), it is necessary to
write the bit CRCNEXT immediately after the last data to be transferred is written to the
SPI_DR. At the end of this last data transfer, the SPI_TXCRCR value is transmitted.
In receive only mode and when the transfers are managed by software (CPU mode), it is
necessary to write the CRCNEXT bit after the second last data has been received. The
CRC is received just after the last data reception and the CRC check is then performed.
At the end of data and CRC transfers, the CRCERR flag in the SPI_SR register is set if
corruption occurs during the transfer.
If data are present in the TX buffer, the CRC value is transmitted only after the transmission
of the data byte. During CRC transmission, the CRC calculator is switched off and the
register value remains unchanged.
Note: Please refer to the product datasheet for availability of this feature.
MOSI (out)
Tx buffer
DATA 1 = 0xF1
TXE flag
0xF1
BSY flag
0xF2
software writes 0xF1
into SPI_DR
software waits until TXE=1 but is
late to write 0xF2 into SPI_DR
software waits until TXE=1 but
is late to write 0xF3 into
SPI_DR
SCK
3 F x 0 = 3 A T A D 2 F x 0 = 2 A T A D
Example with CPOL=1, CPHA=1
0xF3
software waits
until TXE=1
software waits until BSY=0
(write to SPI_DR)
b0 b1 b2 b3 b4 b5 b6 b7 b0 b1 b2 b3 b4 b5 b6 b7 b0 b1 b2 b3 b4 b5 b6 b7
ai17348
RM0008 Serial peripheral interface (SPI)
Doc ID 13902 Rev 13 687/1093
SPI communication using the CRC is possible through the following procedure:
1. Program the CPOL, CPHA, LSBFirst, BR, SSM, SSI and MSTR values.
2. Program the polynomial in the SPI_CRCPR register.
3. Enable the CRC calculation by setting the CRCEN bit in the SPI_CR1 register. This
also clears the SPI_RXCRCR and SPI_TXCRCR registers.
4. Enable the SPI by setting the SPE bit in the SPI_CR1 register.
5. Start the communication and sustain the communication until all but one byte or half-
word have been transmitted or received.
In full duplex or transmitter-only mode, when the transfers are managed by
software, when writing the last byte or half word to the Tx buffer, set the
CRCNEXT bit in the SPI_CR1 register to indicate that the CRC will be transmitted
after the transmission of the last byte.
In receiver only mode, set the bit CRCNEXT just after the reception of the second
to last data to prepare the SPI to enter in CRC Phase at the end of the reception of
the last data. CRC calculation is frozen during the CRC transfer.
6. After the transfer of the last byte or half word, the SPI enters the CRC transfer and
check phase. In full duplex mode or receiver-only mode, the received CRC is compared
to the SPI_RXCRCR value. If the value does not match, the CRCERR flag in SPI_SR is
set and an interrupt can be generated when the ERRIE bit in the SPI_CR2 register is
set.
Note: When the SPI is in slave mode, be careful to enable CRC calculation only when the clock is
stable, that is, when the clock is in the steady state. If not, a wrong CRC calculation may be
done. In fact, the CRC is sensitive to the SCK slave input clock as soon as CRCEN is set,
and this, whatever the value of the SPE bit.
With high bitrate frequencies, be careful when transmitting the CRC. As the number of used
CPU cycles has to be as low as possible in the CRC transfer phase, it is forbidden to call
software functions in the CRC transmission sequence to avoid errors in the last data and
CRC reception. In fact, CRCNEXT bit has to be written before the end of the
transmission/reception of the last data.
For high bit rate frequencies, it is advised to use the DMA mode to avoid the degradation of
the SPI speed performance due to CPU accesses impacting the SPI bandwidth.
When the STM32F10xxx are configured as slaves and the NSS hardware mode is used, the
NSS pin needs to be kept low between the data phase and the CRC phase.
When the SPI is configured in slave mode with the CRC feature enabled, CRC calculation
takes place even if a high level is applied on the NSS pin. This may happen for example in
case of a multislave environment where the communication master addresses slaves
alternately.
Between a slave deselection (high level on NSS) and a new slave selection (low level on
NSS), the CRC value should be cleared on both master and slave sides in order to
resynchronize the master and slave for their respective CRC calculation.
To clear the CRC, follow the procedure below:
1. Disable SPI (SPE = 0)
2. Clear the CRCEN bit
3. Set the CRCEN bit
4. Enable the SPI (SPE = 1)
Serial peripheral interface (SPI) RM0008
688/1093 Doc ID 13902 Rev 13
25.3.7 Status flags
Three status flags are provided for the application to completely monitor the state of the SPI
bus.
Tx buffer empty flag (TXE)
When it is set, this flag indicates that the Tx buffer is empty and the next data to be
transmitted can be loaded into the buffer. The TXE flag is cleared when writing to the
SPI_DR register.
Rx buffer not empty (RXNE)
When set, this flag indicates that there are valid received data in the Rx buffer. It is cleared
when SPI_DR is read.
BUSY flag
This BSY flag is set and cleared by hardware (writing to this flag has no effect). The BSY
flag indicates the state of the communication layer of the SPI.
When BSY is set, it indicates that the SPI is busy communicating. There is one exception in
master mode / bidirectional receive mode (MSTR=1 and BDM=1 and BDOE=0) where the
BSY flag is kept low during reception.
The BSY flag is useful to detect the end of a transfer if the software wants to disable the SPI
and enter Halt mode (or disable the peripheral clock). This avoids corrupting the last
transfer. For this, the procedure described below must be strictly respected.
The BSY flag is also useful to avoid write collisions in a multimaster system.
The BSY flag is set when a transfer starts, with the exception of master mode / bidirectional
receive mode (MSTR=1 and BDM=1 and BDOE=0).
It is cleared:
when a transfer is finished (except in master mode if the communication is continuous)
when the SPI is disabled
when a master mode fault occurs (MODF=1)
When communication is not continuous, the BSY flag is low between each communication.
When communication is continuous:
in master mode, the BSY flag is kept high during all the transfers
in slave mode, the BSY flag goes low for one SPI clock cycle between each transfer
Note: Do not use the BSY flag to handle each data transmission or reception. It is better to use the
TXE and RXNE flags instead.
RM0008 Serial peripheral interface (SPI)
Doc ID 13902 Rev 13 689/1093
25.3.8 Disabling the SPI
When a transfer is terminated, the application can stop the communication by disabling the
SPI peripheral. This is done by clearing the SPE bit.
For some configurations, disabling the SPI and entering the Halt mode while a transfer is
ongoing can cause the current transfer to be corrupted and/or the BSY flag might become
unreliable.
To avoid any of those effects, it is recommended to respect the following procedure when
disabling the SPI:
In master or slave full-duplex mode (BIDIMODE=0, RXONLY=0)
1. Wait until RXNE=1 to receive the last data
2. Wait until TXE=1
3. Then wait until BSY=0
4. Disable the SPI (SPE=0) and, eventually, enter the Halt mode (or disable the peripheral
clock)
In master or slave unidirectional transmit-only mode (BIDIMODE=0,
RXONLY=0) or bidirectional transmit mode (BIDIMODE=1, BIDIOE=1)
After the last data is written into the SPI_DR register:
1. Wait until TXE=1
2. Then wait until BSY=0
3. Disable the SPI (SPE=0) and, eventually, enter the Halt mode (or disable the peripheral
clock)
In master unidirectional receive-only mode (MSTR=1, BIDIMODE=0,
RXONLY=1) or bidirectional receive mode (MSTR=1, BIDIMODE=1, BIDIOE=0)
This case must be managed in a particular way to ensure that the SPI does not initiate a
new transfer:
1. Wait for the second to last occurrence of RXNE=1 (n1)
2. Then wait for one SPI clock cycle (using a software loop) before disabling the SPI
(SPE=0)
3. Then wait for the last RXNE=1 before entering the Halt mode (or disabling the
peripheral clock)
Note: In master bidirectional receive mode (MSTR=1 and BDM=1 and BDOE=0), the BSY flag is
kept low during transfers.
In slave receive-only mode (MSTR=0, BIDIMODE=0, RXONLY=1) or
bidirectional receive mode (MSTR=0, BIDIMODE=1, BIDOE=0)
1. You can disable the SPI (write SPE=1) at any time: the current transfer will complete
before the SPI is effectively disabled
2. Then, if you want to enter the Halt mode, you must first wait until BSY = 0 before
entering the Halt mode (or disabling the peripheral clock).
Serial peripheral interface (SPI) RM0008
690/1093 Doc ID 13902 Rev 13
25.3.9 SPI communication using DMA (direct memory addressing)
To operate at its maximum speed, the SPI needs to be fed with the data for transmission and
the data received on the Rx buffer should be read to avoid overrun. To facilitate the transfers,
the SPI features a DMA capability implementing a simple request/acknowledge protocol.
A DMA access is requested when the enable bit in the SPI_CR2 register is enabled.
Separate requests must be issued to the Tx and Rx buffers (see Figure 245 and
Figure 246):
In transmission, a DMA request is issued each time TXE is set to 1. The DMA then
writes to the SPI_DR register (this clears the TXE flag).
In reception, a DMA request is issued each time RXNE is set to 1. The DMA then reads
the SPI_DR register (this clears the RXNE flag).
When the SPI is used only to transmit data, it is possible to enable only the SPI Tx DMA
channel. In this case, the OVR flag is set because the data received are not read.
When the SPI is used only to receive data, it is possible to enable only the SPI Rx DMA
channel.
In transmission mode, when the DMA has written all the data to be transmitted (flag TCIF is
set in the DMA_ISR register), the BSY flag can be monitored to ensure that the SPI
communication is complete. This is required to avoid corrupting the last transmission before
disabling the SPI or entering the Stop mode. The software must first wait until TXE=1 and
then until BSY=0.
Note: During discontinuous communications, there is a 2 APB clock period delay between the
write operation to SPI_DR and the BSY bit setting. As a consequence, it is mandatory to
wait first until TXE=1 and then until BSY=0 after writing the last data.
Figure 245. Transmission using DMA
0xF1
Tx buffer
TXE flag
0xF2
BSY flag
0xF3
set by hardware
clear by DMA write
set by hardware
cleared by DMA write set by hardware
set by hardware
SCK
reset
Example with CPOL=1, CPHA=1
(write to SPI_DR)
MISO/MOSI (out)
DATA 1 = 0xF1 DATA 2 = 0xF2 DATA 3 = 0xF3
software configures the
DMA SPI Tx channel
to send 3 data items
and enables the SPI
DMA writes to SPI_DR
DMA request
ignored by the DMA because
DMA TCIF flag
set by hardware clear by software
DMA writes
DATA1 into
SPI_DR
by hardware
DMA writes
DATA2 into
SPI_DR
DMA writes
DATA3 into
SPI_DR
software waits until BSY=0
(DMA transfer complete)
DMA transfer is
complete (TCIF=1 in
DMA_ISR)
software waits
until TXE=1
DMA transfer is complete
b0 b1 b2 b3 b4 b5 b6 b7 b0 b1 b2 b3 b4 b5 b6 b7 b0 b1 b2 b3 b4 b5 b6 b7
ai17349
RM0008 Serial peripheral interface (SPI)
Doc ID 13902 Rev 13 691/1093
Figure 246. Reception using DMA
DMA capability with CRC
When SPI communication is enabled with CRC communication and DMA mode, the
transmission and reception of the CRC at the end of communication are automatic that is
without using the bit CRCNEXT. After the CRC reception, the CRC must be read in the
SPI_DR register to clear the RXNE flag.
At the end of data and CRC transfers, the CRCERR flag in SPI_SR is set if corruption
occurs during the transfer.
25.3.10 Error flags
Master mode fault (MODF)
Master mode fault occurs when the master device has its NSS pin pulled low (in NSS
hardware mode) or SSI bit low (in NSS software mode), this automatically sets the MODF
bit. Master mode fault affects the SPI peripheral in the following ways:
The MODF bit is set and an SPI interrupt is generated if the ERRIE bit is set.
The SPE bit is cleared. This blocks all output from the device and disables the SPI
interface.
The MSTR bit is cleared, thus forcing the device into slave mode.
Use the following software sequence to clear the MODF bit:
1. Make a read or write access to the SPI_SR register while the MODF bit is set.
2. Then write to the SPI_CR1 register.
To avoid any multiple slave conflicts in a system comprising several MCUs, the NSS pin
must be pulled high during the MODF bit clearing sequence. The SPE and MSTR bits can
be restored to their original state after this clearing sequence.
MISO/MOSI (in)
DATA 1 = 0xA1
software configures the
DMA SPI Rx channel
to receive 3 data items
and enables the SPI
SCK
DATA 2 = 0xA2 DATA 3 = 0xA3
Example with CPOL=1, CPHA=1
RXNE flag
Rx buffer
set by hardware
(read from SPI_DR)
0xA1 0xA2 0xA3
DMA request
DMA reads
DATA3 from
SPI_DR
flag DMA TCIF
set by hardware
clear
by software
DMA read from SPI_DR
The DMA transfer is
complete (TCIF=1 in
DMA_ISR)
DMA reads
DATA2 from
SPI_DR
DMA reads
DATA1 from
SPI_DR
(DMA transfer complete)
b0 b1 b2 b3 b4 b5 b6 b7 b0 b1 b2 b3 b4 b5 b6 b7 b0 b1 b2 b3 b4 b5 b6 b7
clear by DMA read
ai17350
Serial peripheral interface (SPI) RM0008
692/1093 Doc ID 13902 Rev 13
As a security, hardware does not allow the setting of the SPE and MSTR bits while the
MODF bit is set.
In a slave device the MODF bit cannot be set. However, in a multimaster configuration, the
device can be in slave mode with this MODF bit set. In this case, the MODF bit indicates that
there might have been a multimaster conflict for system control. An interrupt routine can be
used to recover cleanly from this state by performing a reset or returning to a default state.
Overrun condition
An overrun condition occurs when the master device has sent data bytes and the slave
device has not cleared the RXNE bit resulting from the previous data byte transmitted.
When an overrun condition occurs:
the OVR bit is set and an interrupt is generated if the ERRIE bit is set.
In this case, the receiver buffer contents will not be updated with the newly received data
from the master device. A read from the SPI_DR register returns this byte. All other
subsequently transmitted bytes are lost.
Clearing the OVR bit is done by a read from the SPI_DR register followed by a read access
to the SPI_SR register.
CRC error
This flag is used to verify the validity of the value received when the CRCEN bit in the
SPI_CR1 register is set. The CRCERR flag in the SPI_SR register is set if the value
received in the shift register does not match the receiver SPI_RXCRCR value.
25.3.11 SPI interrupts
Table 182. SPI interrupt requests
Interrupt event Event flag Enable Control bit
Transmit buffer empty flag TXE TXEIE
Receive buffer not empty flag RXNE RXNEIE
Master Mode fault event MODF
ERRIE Overrun error OVR
CRC error flag CRCERR
RM0008 Serial peripheral interface (SPI)
Doc ID 13902 Rev 13 693/1093
25.4 I
2
S functional description
25.4.1 The I
2
S audio protocol is not available in low- and medium-density devices. This section
concerns only high-density, XL-density and connectivity line devices. I
2
S general
description
The block diagram of the I
2
S is shown in Figure 247.
Figure 247. I
2
S block diagram
The SPI could function as an audio I
2
S interface when the I
2
S capability is enabled (by
setting the I2SMOD bit in the SPI_I2SCFGR register). This interface uses almost the same
pins, flags and interrupts as the SPI.
Tx buffer
Shift register
16-bit
Communication
Rx buffer
16-bit
MOSI/ SD
Master control logic
MISO
SPI
baud rate generator
CK
I2SMOD
LSB first
LSB
First
SPE BR2 BR1 BR0
MSTR CPOL CPHA
Bidi
mode
Bidi
OE
CRC
EN
CRC
Next
DFF
Rx
only
SSM
SSI
Address and data bus
control
NSS/WS
BSY OVR MODF
CRC
ERR
CH
SIDE
TxE RxNE
I
2
S clock generator
MCK
I2S_CK
I2S
MOD
I2SE
CH
DATLEN
LEN
CKPOL
I2SCFG I2SSTD
MCKOE ODD I2SDIV[7:0]
[1:0] [1:0] [1:0]
UDR
I2SxCLK
ai14748
Serial peripheral interface (SPI) RM0008
694/1093 Doc ID 13902 Rev 13
The I
2
S shares three common pins with the SPI:
SD: Serial Data (mapped on the MOSI pin) to transmit or receive the two time-
multiplexed data channels (in simplex mode only).
WS: Word Select (mapped on the NSS pin) is the data control signal output in master
mode and input in slave mode.
CK: Serial Clock (mapped on the SCK pin) is the serial clock output in master mode
and serial clock input in slave mode.
An additional pin could be used when a master clock output is needed for some external
audio devices:
MCK: Master Clock (mapped separately) is used, when the I
2
S is configured in master
mode (and when the MCKOE bit in the SPI_I2SPR register is set), to output this
additional clock generated at a preconfigured frequency rate equal to 256 F
S
, where
F
S
is the audio sampling frequency.
The I
2
S uses its own clock generator to produce the communication clock when it is set in
master mode. This clock generator is also the source of the master clock output. Two
additional registers are available in I
2
S mode. One is linked to the clock generator
configuration SPI_I2SPR and the other one is a generic I
2
S configuration register
SPI_I2SCFGR (audio standard, slave/master mode, data format, packet frame, clock
polarity, etc.).
The SPI_CR1 register and all CRC registers are not used in the I
2
S mode. Likewise, the
SSOE bit in the SPI_CR2 register and the MODF and CRCERR bits in the SPI_SR are not
used.
The I
2
S uses the same SPI register for data transfer (SPI_DR) in 16-bit wide mode.
25.4.2 Supported audio protocols
The three-line bus has to handle only audio data generally time-multiplexed on two
channels: the right channel and the left channel. However there is only one 16-bit register for
the transmission or the reception. So, it is up to the software to write into the data register
the adequate value corresponding to the considered channel side, or to read the data from
the data register and to identify the corresponding channel by checking the CHSIDE bit in
the SPI_SR register. Channel Left is always sent first followed by the channel right (CHSIDE
has no meaning for the PCM protocol).
Four data and packet frames are available. Data may be sent with a format of:
16-bit data packed in 16-bit frame
16-bit data packed in 32-bit frame
24-bit data packed in 32-bit frame
32-bit data packed in 32-bit frame
When using 16-bit data extended on 32-bit packet, the first 16 bits (MSB) are the significant
bits, the 16-bit LSB is forced to 0 without any need for software action or DMA request (only
one read/write operation).
The 24-bit and 32-bit data frames need two CPU read or write operations to/from the
SPI_DR or two DMA operations if the DMA is preferred for the application. For 24-bit data
frame specifically, the 8 nonsignificant bits are extended to 32 bits with 0-bits (by hardware).
For all data formats and communication standards, the most significant bit is always sent
first (MSB first).
The I
2
S interface supports four audio standards, configurable using the I2SSTD[1:0] and
PCMSYNC bits in the SPI_I2SCFGR register.
RM0008 Serial peripheral interface (SPI)
Doc ID 13902 Rev 13 695/1093
I
2
S Phillips standard
For this standard, the WS signal is used to indicate which channel is being transmitted. It is
activated one CK clock cycle before the first bit (MSB) is available.
Figure 248. I
2
S Phillips protocol waveforms (16/32-bit full accuracy, CPOL = 0)
Data are latched on the falling edge of CK (for the transmitter) and are read on the rising
edge (for the receiver). The WS signal is also latched on the falling edge of CK.
Figure 249. I
2
S Phillips standard waveforms (24-bit frame with CPOL = 0)
This mode needs two write or read operations to/from the SPI_DR.
In transmission mode:
if 0x8EAA33 has to be sent (24-bit):
MSB LSB MSB
CK
WS
SD
Channel left
Channel right
May be 16-bit, 32-bit
Transmission
Reception
CK
WS
SD
Channel left 32-bit
Channel right
MSB LSB
8-bit remaining
0 forced
24-bit data
Transmission
Reception
Serial peripheral interface (SPI) RM0008
696/1093 Doc ID 13902 Rev 13
Figure 250. Transmitting 0x8EAA33
In reception mode:
if data 0x8EAA33 is received:
Figure 251. Receiving 0x8EAA33
Figure 252. I
2
S Phillips standard (16-bit extended to 32-bit packet frame with
CPOL = 0)
When 16-bit data frame extended to 32-bit channel frame is selected during the I
2
S
configuration phase, only one access to SPI_DR is required. The 16 remaining bits are
forced by hardware to 0x0000 to extend the data to 32-bit format.
If the data to transmit or the received data are 0x76A3 (0x76A30000 extended to 32-bit), the
operation shown in Figure 253 is required.
0x8EAA 0x33XX
Only the 8 MSBs are sent to complete the 24 bits
First write to Data register
Second write to Data register
8 LSB bits have no meaning and could be
anything
0x8EAA 0x3300
Only the 8MSB are right
First read from Data register
Second read from Data register
The 8 LSB will always be 00
CK
WS
SD
Channel left 32-bit
Channel right
MSB LSB
16-bit remaining
16-bit data
0 forced
Transmission Reception
RM0008 Serial peripheral interface (SPI)
Doc ID 13902 Rev 13 697/1093
Figure 253. Example
For transmission, each time an MSB is written to SPI_DR, the TXE flag is set and its
interrupt, if allowed, is generated to load SPI_DR with the new value to send. This takes
place even if 0x0000 have not yet been sent because it is done by hardware.
For reception, the RXNE flag is set and its interrupt, if allowed, is generated when the first
16 MSB half-word is received.
In this way, more time is provided between two write or read operations, which prevents
underrun or overrun conditions (depending on the direction of the data transfer).
MSB justified standard
For this standard, the WS signal is generated at the same time as the first data bit, which is
the MSBit.
Figure 254. MSB Justified 16-bit or 32-bit full-accuracy length with CPOL = 0
Data are latched on the falling edge of CK (for transmitter) and are read on the rising edge
(for the receiver).
0X76A3
Only one access to SPI_DR
MSB LSB MSB
CK
WS
SD
Channel left
Channel right
May be 16-bit, 32-bit
Transmission
Reception
Serial peripheral interface (SPI) RM0008
698/1093 Doc ID 13902 Rev 13
Figure 255. MSB Justified 24-bit frame length with CPOL = 0
Figure 256. MSB Justified 16-bit extended to 32-bit packet frame with CPOL = 0
LSB justified standard
This standard is similar to the MSB justified standard (no difference for the 16-bit and 32-bit
full-accuracy frame formats).
Figure 257. LSB justified 16-bit or 32-bit full-accuracy with CPOL = 0
CK
WS
SD
Channel left 32-bit
Channel right
MSB LSB
8-bit remaining
0 forced
24-bit data
Transmission
Reception
CK
WS
SD
Channel left 32-bit
Channel right
MSB LSB
16-bit remaining
0 forced
16-bit data
Transmission Reception
MSB LSB MSB
CK
WS
SD
Channel left
Channel right
May be 16-bit, 32-bit
Transmission Reception
RM0008 Serial peripheral interface (SPI)
Doc ID 13902 Rev 13 699/1093
Figure 258. LSB Justified 24-bit frame length with CPOL = 0
In transmission mode:
If data 0x3478AE have to be transmitted, two write operations to the SPI_DR register
are required from software or by DMA. The operations are shown below.
Figure 259. Operations required to transmit 0x3478AE
In reception mode:
If data 0x3478AE are received, two successive read operations from SPI_DR are
required on each RXNE event.
Figure 260. Operations required to receive 0x3478AE
CK
WS
SD
Channel left 32-bit
Channel right
MSB LSB
24-bit remaining
0 forced
8-bit data
Transmission Reception
0xXX34 0x78AE
First write to Data register
Second write to Data register
Only the 8 LSB bits of the half-word
are significant. Whatever the 8 MSBs
a field of 0x00 is forced instead
conditioned by TXE = 1
conditioned by TXE = 1
0x0034 0x78AE
First read from Data register
Second read from Data register
Only the 8 LSB bits of the half-word
are significant. Whatever the 8 MSBs,
a field of 0x00 is forced instead
conditioned by RXNE = 1
conditioned by RXNE = 1
Serial peripheral interface (SPI) RM0008
700/1093 Doc ID 13902 Rev 13
Figure 261. LSB Justified 16-bit extended to 32-bit packet frame with CPOL = 0
When 16-bit data frame extended to 32-bit channel frame is selected during the I
2
S
configuration phase, Only one access to SPI_DR is required. The 16 remaining bits are
forced by hardware to 0x0000 to extend the data to 32-bit format. In this case it corresponds
to the half-word MSB.
If the data to transmit or the received data are 0x76A3 (0x0000 76A3 extended to 32-bit),
the operation shown in Figure 262 is required.
Figure 262. Example of LSB justified 16-bit extended to 32-bit packet frame
In transmission mode, when TXE is asserted, the application has to write the data to be
transmitted (in this case 0x76A3). The 0x000 field is transmitted first (extension on 32-bit).
TXE is asserted again as soon as the effective data (0x76A3) is sent on SD.
In reception mode, RXNE is asserted as soon as the significant half-word is received (and
not the 0x0000 field).
In this way, more time is provided between two write or read operations to prevent underrun
or overrun conditions.
PCM standard
For the PCM standard, there is no need to use channel-side information. The two PCM
modes (short and long frame) are available and configurable using the PCMSYNC bit in
SPI_I2SCFGR.
CK
WS
SD
Channel left 32-bit
Channel right
MSB LSB
16-bit remaining
0 forced
16-bit data
Transmission Reception
0X76A3
Only one access to SPI_DR
RM0008 Serial peripheral interface (SPI)
Doc ID 13902 Rev 13 701/1093
Figure 263. PCM standard waveforms (16-bit)
For long frame synchronization, the WS signal assertion time is fixed 13 bits in master
mode.
For short frame synchronization, the WS synchronization signal is only one cycle long.
Figure 264. PCM standard waveforms (16-bit extended to 32-bit packet frame)
Note: For both modes (master and slave) and for both synchronizations (short and long), the
number of bits between two consecutive pieces of data (and so two synchronization signals)
needs to be specified (DATLEN and CHLEN bits in the SPI_I2SCFGR register) even in slave
mode.
25.4.3 Clock generator
The I
2
S bitrate determines the dataflow on the I
2
S data line and the I
2
S clock signal
frequency.
I
2
S bitrate = number of bits per channel number of channels sampling audio frequency
For a 16-bit audio, left and right channel, the I
2
S bitrate is calculated as follows:
I
2
S bitrate = 16 2 F
S
MSB LSB MSB
CK
WS
SD
16-bit
WS
up to 13-bit
short
frame
long
frame
MSB
CK
WS
SD
16-bit
WS
up to 13-bit
short
frame
long
frame
LSB
Serial peripheral interface (SPI) RM0008
702/1093 Doc ID 13902 Rev 13
It will be: I
2
S bitrate = 32 x 2 x F
S
if the packet length is 32-bit wide.
Figure 265. Audio sampling frequency definition
When the master mode is configured, a specific action needs to be taken to properly
program the linear divider in order to communicate with the desired audio frequency.
Figure 266. I
2
S clock generator architecture
1. Where x could be 2 or 3.
Figure 265 presents the communication clock architecture. The I2SxCLK source is the
system clock (provided by the HSI, the HSE or the PLL and sourcing the AHB clock). For
connectivity line devices, the I2SxCLK source can be either SYSCLK or the PLL3 VCO (2
PLL3CLK) clock in order to have maximum accuracy. This selection is made using the
I2S2SRC and I2S3SRC bits in the RCC_CFGR2 register.
The audio sampling frequency may be 96 kHz, 48 kHz, 44.1 kHz, 32 kHz, 22.05 kHz,
16 kHz, 11.025 kHz or 8 kHz (or any other value within this range). In order to reach the
desired frequency, the linear divider needs to be programmed according to the formulas
below:
When the master clock is generated (MCKOE in the SPI_I2SPR register is set):
F
S
= I2SxCLK / [(16*2)*((2*I2SDIV)+ODD)*8)] when the channel frame is 16-bit wide
F
S
= I2SxCLK / [(32*2)*((2*I2SDIV)+ODD)*4)] when the channel frame is 32-bit wide
When the master clock is disabled (MCKOE bit cleared):
F
S
= I2SxCLK / [(16*2)*((2*I2SDIV)+ODD))] when the channel frame is 16-bit wide
F
S
= I2SxCLK / [(32*2)*((2*I2SDIV)+ODD))] when the channel frame is 32-bit wide
16-bit or 32-bit Left channel
16-bit or 32-bit Right channel
sampling point
sampling point
32-bits or 64-bits
F
S
F
S
: Audio sampling frequency
8-bit
Divider +
Linear
CK
ODD
I2SDIV[7:0]
I2SxCLK
CHLEN I2SMOD
reshaping stage
Divider by 4
Div2
1
0
MCKOE
MCKOE
MCK
0
1
RM0008 Serial peripheral interface (SPI)
Doc ID 13902 Rev 13 703/1093
Table 183, Table 184 and Table 185 provide example precision values for different clock
configurations.
Note: Other configurations are possible that allow optimum clock precision.
Table 183. Audio-frequency precision using standard 8 MHz HSE (high-density and XL-density
devices only)
SYSCLK
(MHz)
I2S_DIV I2S_ODD
MCLK
Target f
S
(Hz)
Real f
S
(KHz) Error
16-bit 32-bit 16-bit 32-bit 16-bit 32-bit 16-bit 32-bit
72 11 6 1 0 No 96000 97826.09 93750 1.90% 2.34%
72 23 11 1 1 No 48000 47872.34 48913.04 0.27% 1.90%
72 25 13 1 0 No 44100 44117.65 43269.23 0.04% 1.88%
72 35 17 0 1 No 32000 32142.86 32142.86 0.44% 0.44%
72 51 25 0 1 No 22050 22058.82 22058.82 0.04% 0.04%
72 70 35 1 0 No 16000 15675.75 16071.43 0.27% 0.45%
72 102 51 0 0 No 11025 11029.41 11029.41 0.04% 0.04%
72 140 70 1 1 No 8000 8007.11 7978.72 0.09% 0.27%
72 2 2 0 0 Yes 96000 70312.15 70312.15 26.76% 26.76%
72 3 3 0 0 Yes 48000 46875 46875 2.34% 2.34%
72 3 3 0 0 Yes 44100 46875 46875 6.29% 6.29%
72 9 9 0 0 Yes 32000 31250 31250 2.34% 2.34%
72 6 6 1 1 Yes 22050 21634.61 21634.61 1.88% 1.88%
72 9 9 0 0 Yes 16000 15625 15625 2.34% 2.34%
72 13 13 0 0 Yes 11025 10817.30 10817.30 1.88% 1.88%
72 17 17 1 1 Yes 8000 8035.71 8035.71 0.45% 0.45%
Serial peripheral interface (SPI) RM0008
704/1093 Doc ID 13902 Rev 13
Table 184. Audio-frequency precision using standard 25 MHz and PLL3
(connectivity line devices only)
Data
length
PREDIV2 PLL3MUL I2SDIV I2SODD MCLK
Target
fs(Hz)
Real fs (KHz) Error
32 6 14 9 1 No 96000 95942.9825 0.0594%
16 6 14 38 0 No 48000 47971.4912 0.0594%
32 6 14 19 0 No 48000 47971.4912 0.0594%
16 8 14 31 0 No 44100 44102.823 0.0064%
32 8 14 15 1 No 44100 44102.823 0.0064%
16 5 13 63 1 No 32000 31988.189 0.0369%
32 8 20 30 1 No 32000 32018.443 0.0576%
16 8 14 62 0 No 22050 22051.4113 0.0064%
32 8 14 31 0 No 22050 22051.4113 0.0064%
16 7 20 139 1 No 16000 16001.0241 0.0064%
32 5 13 63 1 No 16000 15994.0945 0.0369%
16 8 14 124 0 No 11025 11025.7056 0.0064%
32 8 14 62 0 No 11025 11025.7056 0.0064%
16 7 10 139 1 No 8000 8000.51203 0.0064%
32 7 20 139 1 No 8000 8000.51203 0.0064%
16 5 10 2 0 Yes 96000 97656.25 1.7253%
32 5 10 2 0 Yes 96000 97656.25 1.7253%
16 7 12 3 1 Yes 48000 47831.6327 0.3508%
32 7 12 3 1 Yes 48000 47831.6327 0.3508%
16 5 9 4 0 Yes 44100 43945.3125 0.3508%
32 5 9 4 0 Yes 44100 43945.3125 0.3508%
16 5 9 5 1 Yes 32000 31960.2273 0.1243%
32 5 9 5 1 Yes 32000 31960.2273 0.1243%
16 5 13 11 1 Yes 22050 22078.8043 0.1306%
32 5 13 11 1 Yes 22050 22078.8043 0.1306%
16 5 9 11 0 Yes 16000 15980.1136 0.1243%
32 5 9 11 0 Yes 16000 15980.1136 0.1243%
16 8 14 15 1 Yes 11025 11025.7056 0.0064%
32 8 14 15 1 Yes 11025 11025.7056 0.0064%
16 8 20 30 1 Yes 8000 8004.61066 0.0576%
32 8 20 30 1 Yes 8000 8004.61066 0.0576%
RM0008 Serial peripheral interface (SPI)
Doc ID 13902 Rev 13 705/1093
Table 185. Audio-frequency precision using standard 14.7456 MHz and PLL3
(connectivity line devices only)
Data
length
PREDIV2 PLL3MUL I2SDIV I2SODD MCLK
Target
fs(Hz)
Real fs
(KHz)
Error
16 3 10 16 0 No 96000 96000 0.0000%
32 3 10 8 0 No 96000 96000 0.0000%
16 3 10 32 0 No 48000 48000 0.0000%
32 3 10 16 0 No 48000 48000 0.0000%
16 4 9 23 1 No 44100 44119.148 0.0434%
32 4 13 17 0 No 44100 44047.059 0.1200%
16 3 10 48 0 No 32000 32000 0.0000%
32 3 10 24 0 No 32000 32000 0.0000%
16 4 20 104 1 No 22050 22047.8469 0.0098%
32 4 9 32 1 No 22050 22059.5745 0.0434%
16 3 10 96 0 No 16000 16000 0.0000%
32 3 10 48 0 No 16000 16000 0.0000%
16 4 20 209 1 No 11025 11023.923 0.0098%
32 4 20 104 1 No 11025 11023.923 0.0098%
16 3 10 192 0 No 8000 8000 0.0000%
32 3 10 96 0 No 8000 8000 0.0000%
16 3 10 2 0 Yes 96000 96000 0.0000%
32 3 10 2 0 Yes 96000 96000 0.0000%
16 3 10 4 0 Yes 48000 48000 0.0000%
32 3 10 4 0 Yes 48000 48000 0.0000%
16 4 20 6 1 Yes 44100 44307.6923 0.4710%
32 4 20 6 1 Yes 44100 44307.6923 0.4710%
16 3 10 6 0 Yes 32000 32000 0.0000%
32 3 10 6 0 Yes 32000 32000 0.0000%
16 4 13 8 1 Yes 22050 22023.5294 0.1200%
32 4 13 8 1 Yes 22050 22023.5294 0.1200%
16 3 10 12 0 Yes 16000 16000 0.0000%
32 3 10 12 0 Yes 16000 16000 0.0000%
16 4 13 17 0 Yes 11025 11029.7872 0.0434%
32 4 13 17 0 Yes 11025 11029.7872 0.0434%
16 3 10 24 0 Yes 8000 8000 0.0000%
32 3 10 24 0 Yes 8000 8000 0.0000%
Serial peripheral interface (SPI) RM0008
706/1093 Doc ID 13902 Rev 13
25.4.4 I
2
S master mode
The I
2
S can be configured in master mode. This means that the serial clock is generated on
the CK pin as well as the Word Select signal WS. Master clock (MCK) may be output or not,
thanks to the MCKOE bit in the SPI_I2SPR register.
Procedure
1. Select the I2SDIV[7:0] bits in the SPI_I2SPR register to define the serial clock baud
rate to reach the proper audio sample frequency. The ODD bit in the SPI_I2SPR
register also has to be defined.
2. Select the CKPOL bit to define the steady level for the communication clock. Set the
MCKOE bit in the SPI_I2SPR register if the master clock MCK needs to be provided to
the external DAC/ADC audio component (the I2SDIV and ODD values should be
computed depending on the state of the MCK output, for more details refer to
Section 25.4.3: Clock generator).
3. Set the I2SMOD bit in SPI_I2SCFGR to activate the I
2
S functionalities and choose the
I
2
S standard through the I2SSTD[1:0] and PCMSYNC bits, the data length through the
DATLEN[1:0] bits and the number of bits per channel by configuring the CHLEN bit.
Select also the I
2
S master mode and direction (Transmitter or Receiver) through the
I2SCFG[1:0] bits in the SPI_I2SCFGR register.
4. If needed, select all the potential interruption sources and the DMA capabilities by
writing the SPI_CR2 register.
5. The I2SE bit in SPI_I2SCFGR register must be set.
WS and CK are configured in output mode. MCK is also an output, if the MCKOE bit in
SPI_I2SPR is set.
Transmission sequence
The transmission sequence begins when a half-word is written into the Tx buffer.
Assumedly, the first data written into the Tx buffer correspond to the channel Left data.
When data are transferred from the Tx buffer to the shift register, TXE is set and data
corresponding to the channel Right have to be written into the Tx buffer. The CHSIDE flag
indicates which channel is to be transmitted. It has a meaning when the TXE flag is set
because the CHSIDE flag is updated when TXE goes high.
A full frame has to be considered as a Left channel data transmission followed by a Right
channel data transmission. It is not possible to have a partial frame where only the left
channel is sent.
The data half-word is parallel loaded into the 16-bit shift register during the first bit
transmission, and then shifted out, serially, to the MOSI/SD pin, MSB first. The TXE flag is
set after each transfer from the Tx buffer to the shift register and an interrupt is generated if
the TXEIE bit in the SPI_CR2 register is set.
For more details about the write operations depending on the I
2
S standard mode selected,
refer to Section 25.4.2: Supported audio protocols).
To ensure a continuous audio data transmission, it is mandatory to write the SPI_DR with
the next data to transmit before the end of the current transmission.
To switch off the I
2
S, by clearing I2SE, it is mandatory to wait for TXE = 1 and BSY = 0.
RM0008 Serial peripheral interface (SPI)
Doc ID 13902 Rev 13 707/1093
Reception sequence
The operating mode is the same as for the transmission mode except for the point 3 (refer to
the procedure described in Section 25.4.4: I2S master mode), where the configuration
should set the master reception mode through the I2SCFG[1:0] bits.
Whatever the data or channel length, the audio data are received by 16-bit packets. This
means that each time the Rx buffer is full, the RXNE flag is set and an interrupt is generated
if the RXNEIE bit is set in SPI_CR2 register. Depending on the data and channel length
configuration, the audio value received for a right or left channel may result from one or two
receptions into the Rx buffer.
Clearing the RXNE bit is performed by reading the SPI_DR register.
CHSIDE is updated after each reception. It is sensitive to the WS signal generated by the
I
2
S cell.
For more details about the read operations depending on the I
2
S standard mode selected,
refer to Section 25.4.2: Supported audio protocols.
If data are received while the previously received data have not been read yet, an overrun is
generated and the OVR flag is set. If the ERRIE bit is set in the SPI_CR2 register, an
interrupt is generated to indicate the error.
To switch off the I
2
S, specific actions are required to ensure that the I
2
S completes the
transfer cycle properly without initiating a new data transfer. The sequence depends on the
configuration of the data and channel lengths, and on the audio protocol mode selected. In
the case of:
16-bit data length extended on 32-bit channel length (DATLEN = 00 and CHLEN = 1)
using the LSB justified mode (I2SSTD = 10)
a) Wait for the second to last RXNE = 1 (n 1)
b) Then wait 17 I
2
S clock cycles (using a software loop)
c) Disable the I
2
S (I2SE = 0)
16-bit data length extended on 32-bit channel length (DATLEN = 00 and CHLEN = 1) in
MSB justified, I
2
S or PCM modes (I2SSTD = 00, I2SSTD = 01 or I2SSTD = 11,
respectively)
a) Wait for the last RXNE
b) Then wait 1 I
2
S clock cycle (using a software loop)
c) Disable the I
2
S (I2SE = 0)
For all other combinations of DATLEN and CHLEN, whatever the audio mode selected
through the I2SSTD bits, carry out the following sequence to switch off the I
2
S:
a) Wait for the second to last RXNE = 1 (n 1)
b) Then wait one I
2
S clock cycle (using a software loop)
c) Disable the I
2
S (I2SE = 0)
Note: The BSY flag is kept low during transfers.
25.4.5 I
2
S slave mode
For the slave configuration, the I
2
S can be configured in transmission or reception mode.
The operating mode is following mainly the same rules as described for the I
2
S master
configuration. In slave mode, there is no clock to be generated by the I
2
S interface. The
Serial peripheral interface (SPI) RM0008
708/1093 Doc ID 13902 Rev 13
clock and WS signals are input from the external master connected to the I
2
S interface.
There is then no need, for the user, to configure the clock.
The configuration steps to follow are listed below:
1. Set the I2SMOD bit in the SPI_I2SCFGR register to reach the I
2
S functionalities and
choose the I
2
S standard through the I2SSTD[1:0] bits, the data length through the
DATLEN[1:0] bits and the number of bits per channel for the frame configuring the
CHLEN bit. Select also the mode (transmission or reception) for the slave through the
I2SCFG[1:0] bits in SPI_I2SCFGR register.
2. If needed, select all the potential interrupt sources and the DMA capabilities by writing
the SPI_CR2 register.
3. The I2SE bit in SPI_I2SCFGR register must be set.
Transmission sequence
The transmission sequence begins when the external master device sends the clock and
when the NSS_WS signal requests the transfer of data. The slave has to be enabled before
the external master starts the communication. The I
2
S data register has to be loaded before
the master initiates the communication.
For the I
2
S, MSB justified and LSB justified modes, the first data item to be written into the
data register corresponds to the data for the left channel. When the communication starts,
the data are transferred from the Tx buffer to the shift register. The TXE flag is then set in
order to request the right channel data to be written into the I
2
S data register.
The CHSIDE flag indicates which channel is to be transmitted. Compared to the master
transmission mode, in slave mode, CHSIDE is sensitive to the WS signal coming from the
external master. This means that the slave needs to be ready to transmit the first data before
the clock is generated by the master. WS assertion corresponds to left channel transmitted
first.
Note: The I2SE has to be written at least two PCLK cycles before the first clock of the master
comes on the CK line.
The data half-word is parallel-loaded into the 16-bit shift register (from the internal bus)
during the first bit transmission, and then shifted out serially to the MOSI/SD pin MSB first.
The TXE flag is set after each transfer from the Tx buffer to the shift register and an interrupt
is generated if the TXEIE bit in the SPI_CR2 register is set.
Note that the TXE flag should be checked to be at 1 before attempting to write the Tx buffer.
For more details about the write operations depending on the I
2
S standard mode selected,
refer to Section 25.4.2: Supported audio protocols.
To secure a continuous audio data transmission, it is mandatory to write the SPI_DR
register with the next data to transmit before the end of the current transmission. An
underrun flag is set and an interrupt may be generated if the data are not written into the
SPI_DR register before the first clock edge of the next data communication. This indicates
to the software that the transferred data are wrong. If the ERRIE bit is set into the SPI_CR2
register, an interrupt is generated when the UDR flag in the SPI_SR register goes high. In
this case, it is mandatory to switch off the I
2
S and to restart a data transfer starting from the
left channel.
To switch off the I
2
S, by clearing the I2SE bit, it is mandatory to wait for TXE = 1 and BSY =
0.
RM0008 Serial peripheral interface (SPI)
Doc ID 13902 Rev 13 709/1093
Reception sequence
The operating mode is the same as for the transmission mode except for the point 1 (refer to
the procedure described in Section 25.4.5: I2S slave mode), where the configuration should
set the master reception mode using the I2SCFG[1:0] bits in the SPI_I2SCFGR register.
Whatever the data length or the channel length, the audio data are received by 16-bit
packets. This means that each time the RX buffer is full, the RXNE flag in the SPI_SR
register is set and an interrupt is generated if the RXNEIE bit is set in the SPI_CR2 register.
Depending on the data length and channel length configuration, the audio value received for
a right or left channel may result from one or two receptions into the RX buffer.
The CHSIDE flag is updated each time data are received to be read from SPI_DR. It is
sensitive to the external WS line managed by the external master component.
Clearing the RXNE bit is performed by reading the SPI_DR register.
For more details about the read operations depending the I
2
S standard mode selected, refer
to Section 25.4.2: Supported audio protocols.
If data are received while the precedent received data have not yet been read, an overrun is
generated and the OVR flag is set. If the bit ERRIE is set in the SPI_CR2 register, an
interrupt is generated to indicate the error.
To switch off the I
2
S in reception mode, I2SE has to be cleared immediately after receiving
the last RXNE = 1.
Note: The external master components should have the capability of sending/receiving data in 16-
bit or 32-bit packets via an audio channel.
25.4.6 Status flags
Three status flags are provided for the application to fully monitor the state of the I
2
S bus.
Busy flag (BSY)
The BSY flag is set and cleared by hardware (writing to this flag has no effect). It indicates
the state of the communication layer of the I
2
S.
When BSY is set, it indicates that the I
2
S is busy communicating. There is one exception in
master receive mode (I2SCFG = 11) where the BSY flag is kept low during reception.
The BSY flag is useful to detect the end of a transfer if the software needs to disable the I
2
S.
This avoids corrupting the last transfer. For this, the procedure described below must be
strictly respected.
The BSY flag is set when a transfer starts, except when the I
2
S is in master receiver mode.
The BSY flag is cleared:
when a transfer completes (except in master transmit mode, in which the
communication is supposed to be continuous)
when the I
2
S is disabled
When communication is continuous:
In master transmit mode, the BSY flag is kept high during all the transfers
In slave mode, the BSY flag goes low for one I
2
S clock cycle between each transfer
Note: Do not use the BSY flag to handle each data transmission or reception. It is better to use the
TXE and RXNE flags instead.
Serial peripheral interface (SPI) RM0008
710/1093 Doc ID 13902 Rev 13
Tx buffer empty flag (TXE)
When set, this flag indicates that the Tx buffer is empty and the next data to be transmitted
can then be loaded into it. The TXE flag is reset when the Tx buffer already contains data to
be transmitted. It is also reset when the I
2
S is disabled (I2SE bit is reset).
RX buffer not empty (RXNE)
When set, this flag indicates that there are valid received data in the RX Buffer. It is reset
when SPI_DR register is read.
Channel Side flag (CHSIDE)
In transmission mode, this flag is refreshed when TXE goes high. It indicates the channel
side to which the data to transfer on SD has to belong. In case of an underrun error event in
slave transmission mode, this flag is not reliable and I
2
S needs to be switched off and
switched on before resuming the communication.
In reception mode, this flag is refreshed when data are received into SPI_DR. It indicates
from which channel side data have been received. Note that in case of error (like OVR) this
flag becomes meaningless and the I
2
S should be reset by disabling and then enabling it
(with configuration if it needs changing).
This flag has no meaning in the PCM standard (for both Short and Long frame modes).
When the OVR or UDR flag in the SPI_SR is set and the ERRIE bit in SPI_CR2 is also set,
an interrupt is generated. This interrupt can be cleared by reading the SPI_SR status
register (once the interrupt source has been cleared).
25.4.7 Error flags
There are two error flags for the I
2
S cell.
Underrun flag (UDR)
In slave transmission mode this flag is set when the first clock for data transmission appears
while the software has not yet loaded any value into SPI_DR. It is available when the
I2SMOD bit in SPI_I2SCFGR is set. An interrupt may be generated if the ERRIE bit in
SPI_CR2 is set.
The UDR bit is cleared by a read operation on the SPI_SR register.
Overrun flag (OVR)
This flag is set when data are received and the previous data have not yet been read from
SPI_DR. As a result, the incoming data are lost. An interrupt may be generated if the ERRIE
bit is set in SPI_CR2.
In this case, the receive buffer contents are not updated with the newly received data from
the transmitter device. A read operation to the SPI_DR register returns the previous
correctly received data. All other subsequently transmitted half-words are lost.
Clearing the OVR bit is done by a read operation on the SPI_DR register followed by a read
access to the SPI_SR register.
RM0008 Serial peripheral interface (SPI)
Doc ID 13902 Rev 13 711/1093
25.4.8 I
2
S interrupts
Table 186 provides the list of I
2
S interrupts.
25.4.9 DMA features
DMA is working in exactly the same way as for the SPI mode. There is no difference on the
I
2
S. Only the CRC feature is not available in I
2
S mode since there is no data transfer
protection system.
Table 186. I
2
S interrupt requests
Interrupt event Event flag Enable Control bit
Transmit buffer empty flag TXE TXEIE
Receive buffer not empty flag RXNE RXNEIE
Overrun error OVR
ERRIE
Underrun error UDR
Serial peripheral interface (SPI) RM0008
712/1093 Doc ID 13902 Rev 13
25.5 SPI and I
2
S registers
Refer to Section 2.1 on page 46 for a list of abbreviations used in register descriptions.
The peripheral registers can be accessed by half-words (16-bit) or words (32-bit).
25.5.1 SPI control register 1 (SPI_CR1) (not used in I
2
S mode)
Address offset: 0x00
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
BIDI
MODE
BIDI
OE
CRC
EN
CRC
NEXT
DFF
RX
ONLY
SSM SSI
LSB
FIRST
SPE BR [2:0] MSTR CPOL CPHA
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bit 15 BIDIMODE: Bidirectional data mode enable
0: 2-line unidirectional data mode selected
1: 1-line bidirectional data mode selected
Note: Not used in I
2
S mode
Bit 14 BIDIOE: Output enable in bidirectional mode
This bit combined with the BIDImode bit selects the direction of transfer in bidirectional mode
0: Output disabled (receive-only mode)
1: Output enabled (transmit-only mode)
Note: In master mode, the MOSI pin is used and in slave mode, the MISO pin is used.
Not used in I
2
S mode
Bit 13 CRCEN: Hardware CRC calculation enable
0: CRC calculation disabled
1: CRC calculation enabled
Note: This bit should be written only when SPI is disabled (SPE = 0) for correct operation
Not used in I
2
S mode
Bit 12 CRCNEXT: CRC transfer next
0: Data phase (no CRC phase)
1: Next transfer is CRC (CRC phase)
Note: When the SPI is configured in full duplex or transmitter only modes, CRCNEXT must be
written as soon as the last data is written to the SPI_DR register.
When the SPI is configured in receiver only mode, CRCNEXT must be set after the
second last data reception.
This bit should be kept cleared when the transfers are managed by DMA.
Not used in I
2
S mode
Bit 11 DFF: Data frame format
0: 8-bit data frame format is selected for transmission/reception
1: 16-bit data frame format is selected for transmission/reception
Note: This bit should be written only when SPI is disabled (SPE = 0) for correct operation
Not used in I
2
S mode
RM0008 Serial peripheral interface (SPI)
Doc ID 13902 Rev 13 713/1093
Bit 10 RXONLY: Receive only
This bit combined with the BIDImode bit selects the direction of transfer in 2-line
unidirectional mode. This bit is also useful in a multislave system in which this particular
slave is not accessed, the output from the accessed slave is not corrupted.
0: Full duplex (Transmit and receive)
1: Output disabled (Receive-only mode)
Note: Not used in I
2
S mode
Bit 9 SSM: Software slave management
When the SSM bit is set, the NSS pin input is replaced with the value from the SSI bit.
0: Software slave management disabled
1: Software slave management enabled
Note: Not used in I
2
S mode
Bit 8 SSI: Internal slave select
This bit has an effect only when the SSM bit is set. The value of this bit is forced onto the
NSS pin and the IO value of the NSS pin is ignored.
Note: Not used in I
2
S mode
Bit 7 LSBFIRST: Frame format
0: MSB transmitted first
1: LSB transmitted first
Note: This bit should not be changed when communication is ongoing.
Not used in I
2
S mode
Bit 6 SPE: SPI enable
0: Peripheral disabled
1: Peripheral enabled
Note: 1- Not used in I
2
S mode.
Note: 2- When disabling the SPI, follow the procedure described in Section 25.3.8: Disabling the
SPI.
Bits 5:3 BR[2:0]: Baud rate control
000: f
PCLK
/2 100: f
PCLK
/32
001: f
PCLK
/4 101: f
PCLK
/64
010: f
PCLK
/8 110: f
PCLK
/128
011: f
PCLK
/16 111: f
PCLK
/256
Note: These bits should not be changed when communication is ongoing.
Not used in I
2
S mode
Bit 2 MSTR: Master selection
0: Slave configuration
1: Master configuration
Note: This bit should not be changed when communication is ongoing.
Not used in I
2
S mode
Bit1 CPOL: Clock polarity
0: CK to 0 when idle
1: CK to 1 when idle
Note: This bit should not be changed when communication is ongoing.
Not used in I
2
S mode
Serial peripheral interface (SPI) RM0008
714/1093 Doc ID 13902 Rev 13
25.5.2 SPI control register 2 (SPI_CR2)
Address offset: 0x04
Reset value: 0x0000
Bit 0 CPHA: Clock phase
0: The first clock transition is the first data capture edge
1: The second clock transition is the first data capture edge
Note: This bit should not be changed when communication is ongoing.
Note: Not used in I
2
S mode
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
TXEIE RXNEIE ERRIE Res. Res. SSOE TXDMAEN RXDMAEN
rw rw rw rw rw rw
Bits 15:8 Reserved, must be kept at reset value.
Bit 7 TXEIE: Tx buffer empty interrupt enable
0: TXE interrupt masked
1: TXE interrupt not masked. Used to generate an interrupt request when the TXE flag is set.
Bit 6 RXNEIE: RX buffer not empty interrupt enable
0: RXNE interrupt masked
1: RXNE interrupt not masked. Used to generate an interrupt request when the RXNE flag is
set.
Bit 5 ERRIE: Error interrupt enable
This bit controls the generation of an interrupt when an error condition occurs (CRCERR,
OVR, MODF in SPI mode and UDR, OVR in I
2
S mode).
0: Error interrupt is masked
1: Error interrupt is enabled
Bits 4:3 Reserved, must be kept at reset value.
Bit 2 SSOE: SS output enable
0: SS output is disabled in master mode and the cell can work in multimaster configuration
1: SS output is enabled in master mode and when the cell is enabled. The cell cannot work
in a multimaster environment.
Note: Not used in I
2
S mode
Bit 1 TXDMAEN: Tx buffer DMA enable
When this bit is set, the DMA request is made whenever the TXE flag is set.
0: Tx buffer DMA disabled
1: Tx buffer DMA enabled
Bit 0 RXDMAEN: Rx buffer DMA enable
When this bit is set, the DMA request is made whenever the RXNE flag is set.
0: Rx buffer DMA disabled
1: Rx buffer DMA enabled
RM0008 Serial peripheral interface (SPI)
Doc ID 13902 Rev 13 715/1093
25.5.3 SPI status register (SPI_SR)
Address offset: 0x08
Reset value: 0x0002
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
BSY OVR MODF
CRC
ERR
UDR
CHSID
E
TXE RXNE
r r r rc_w0 r r r r
Bits 15:8 Reserved, must be kept at reset value.
Bit 7 BSY: Busy flag
0: SPI (or I2S)not busy
1: SPI (or I2S)is busy in communication or Tx buffer is not empty
This flag is set and cleared by hardware.
Note: BSY flag must be used with caution: refer to Section 25.3.7: Status flags and
Section 25.3.8: Disabling the SPI.
Bit 6 OVR: Overrun flag
0: No overrun occurred
1: Overrun occurred
This flag is set by hardware and reset by a software sequence. Refer to Section 25.4.7 on
page 710 for the software sequence.
Bit 5 MODF: Mode fault
0: No mode fault occurred
1: Mode fault occurred
This flag is set by hardware and reset by a software sequence. Refer to Section 25.3.10 on
page 691 for the software sequence.
Note: Not used in I
2
S mode
Bit 4 CRCERR: CRC error flag
0: CRC value received matches the SPI_RXCRCR value
1: CRC value received does not match the SPI_RXCRCR value
This flag is set by hardware and cleared by software writing 0.
Note: Not used in I
2
S mode
Bit 3 UDR: Underrun flag
0: No underrun occurred
1: Underrun occurred
This flag is set by hardware and reset by a software sequence. Refer to Section 25.4.7 on
page 710 for the software sequence.
Note: Not used in SPI mode
Bit 2 CHSIDE: Channel side
0: Channel Left has to be transmitted or has been received
1: Channel Right has to be transmitted or has been received
Note: Not used for the SPI mode. No meaning in PCM mode
Bit 1 TXE: Transmit buffer empty
0: Tx buffer not empty
1: Tx buffer empty
Bit 0 RXNE: Receive buffer not empty
0: Rx buffer empty
1: Rx buffer not empty
Serial peripheral interface (SPI) RM0008
716/1093 Doc ID 13902 Rev 13
25.5.4 SPI data register (SPI_DR)
Address offset: 0x0C
Reset value: 0x0000
25.5.5 SPI CRC polynomial register (SPI_CRCPR) (not used in I
2
S
mode)
Address offset: 0x10
Reset value: 0x0007
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
DR[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 DR[15:0]: Data register
Data received or to be transmitted.
The data register is split into 2 buffers - one for writing (Transmit Buffer) and another one for
reading (Receive buffer). A write to the data register will write into the Tx buffer and a read
from the data register will return the value held in the Rx buffer.
Notes for the SPI mode:
Depending on the data frame format selection bit (DFF in SPI_CR1 register), the data
sent or received is either 8-bit or 16-bit. This selection has to be made before enabling
the SPI to ensure correct operation.
For an 8-bit data frame, the buffers are 8-bit and only the LSB of the register
(SPI_DR[7:0]) is used for transmission/reception. When in reception mode, the MSB of
the register (SPI_DR[15:8]) is forced to 0.
For a 16-bit data frame, the buffers are 16-bit and the entire register, SPI_DR[15:0] is
used for transmission/reception.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CRCPOLY[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 15:0 CRCPOLY[15:0]: CRC polynomial register
This register contains the polynomial for the CRC calculation.
The CRC polynomial (0007h) is the reset value of this register. Another polynomial can be
configured as required.
Note: Not used for the I
2
S mode.
RM0008 Serial peripheral interface (SPI)
Doc ID 13902 Rev 13 717/1093
25.5.6 SPI RX CRC register (SPI_RXCRCR) (not used in I
2
S mode)
Address offset: 0x14
Reset value: 0x0000
25.5.7 SPI TX CRC register (SPI_TXCRCR) (not used in I
2
S mode)
Address offset: 0x18
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
RXCRC[15:0]
r r r r r r r r r r r r r r r r
Bits 15:0 RXCRC[15:0]: Rx CRC register
When CRC calculation is enabled, the RxCRC[15:0] bits contain the computed CRC value of
the subsequently received bytes. This register is reset when the CRCEN bit in SPI_CR1
register is written to 1. The CRC is calculated serially using the polynomial programmed in
the SPI_CRCPR register.
Only the 8 LSB bits are considered when the data frame format is set to be 8-bit data (DFF
bit of SPI_CR1 is cleared). CRC calculation is done based on any CRC8 standard.
The entire 16-bits of this register are considered when a 16-bit data frame format is selected
(DFF bit of the SPI_CR1 register is set). CRC calculation is done based on any CRC16
standard.
Note: A read to this register when the BSY Flag is set could return an incorrect value.
Not used for the I
2
S mode.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
TXCRC[15:0]
r r r r r r r r r r r r r r r r
Bits 15:0 TXCRC[15:0]: Tx CRC register
When CRC calculation is enabled, the TxCRC[7:0] bits contain the computed CRC value of
the subsequently transmitted bytes. This register is reset when the CRCEN bit of SPI_CR1
is written to 1. The CRC is calculated serially using the polynomial programmed in the
SPI_CRCPR register.
Only the 8 LSB bits are considered when the data frame format is set to be 8-bit data (DFF
bit of SPI_CR1 is cleared). CRC calculation is done based on any CRC8 standard.
The entire 16-bits of this register are considered when a 16-bit data frame format is selected
(DFF bit of the SPI_CR1 register is set). CRC calculation is done based on any CRC16
standard.
Note: A read to this register when the BSY flag is set could return an incorrect value.
Not used for I
2
S mode.
Serial peripheral interface (SPI) RM0008
718/1093 Doc ID 13902 Rev 13
25.5.8 SPI_I
2
S configuration register (SPI_I2SCFGR)
Address offset: 0x1C
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
I2SMOD I2SE I2SCFG
PCMSY
NC
Reserved
I2SSTD CKPOL DATLEN CHLEN
rw rw rw rw rw rw rw rw rw rw rw
Bits 15:12 Reserved, must be kept at reset value.
Bit 11 I2SMOD: I2S mode selection
0: SPI mode is selected
1: I2S mode is selected
Note: This bit should be configured when the SPI or I
2
S is disabled
Bit 10 I2SE: I2S Enable
0: I
2
S peripheral is disabled
1: I
2
S peripheral is enabled
Note: Not used in SPI mode
Bit 9:8 I2SCFG: I2S configuration mode
00: Slave - transmit
01: Slave - receive
10: Master - transmit
11: Master - receive
Note: This bit should be configured when the I
2
S is disabled.
Not used for the SPI mode
Bit 7 PCMSYNC: PCM frame synchronization
0: Short frame synchronization
1: Long frame synchronization
Note: This bit has a meaning only if I2SSTD = 11 (PCM standard is used)
Not used for the SPI mode
Bit 6 Reserved: forced at 0 by hardware
Bit 5:4 I2SSTD: I2S standard selection
00: I
2
S Phillips standard.
01: MSB justified standard (left justified)
10: LSB justified standard (right justified)
11: PCM standard
For more details on I
2
S standards, refer to Section 25.4.2 on page 694. Not used in SPI mode.
Note: For correct operation, these bits should be configured when the I
2
S is disabled.
Bit 3 CKPOL: Steady state clock polarity
0: I
2
S clock steady state is low level
1: I
2
S clock steady state is high level
Note: For correct operation, this bit should be configured when the I
2
S is disabled.
Not used in SPI mode
Serial peripheral interface (SPI) RM0008
719/1093 Doc ID 13902 Rev 13
25.5.9 SPI_I
2
S prescaler register (SPI_I2SPR)
Address offset: 0x20
Reset value: 0000 0010 (0x0002)
Bit 2:1 DATLEN: Data length to be transferred
00: 16-bit data length
01: 24-bit data length
10: 32-bit data length
11: Not allowed
Note: For correct operation, these bits should be configured when the I
2
S is disabled.
Not used in SPI mode.
Bit 0 CHLEN: Channel length (number of bits per audio channel)
0: 16-bit wide
1: 32-bit wide
The bit write operation has a meaning only if DATLEN = 00 otherwise the channel length is fixed to
32-bit by hardware whatever the value filled in. Not used in SPI mode.
Note: For correct operation, this bit should be configured when the I
2
S is disabled.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
MCKOE ODD I2SDIV
rw rw rw
Bits 15:10 Reserved, must be kept at reset value.
Bit 9 MCKOE: Master clock output enable
0: Master clock output is disabled
1: Master clock output is enabled
Note: This bit should be configured when the I
2
S is disabled. It is used only when the I
2
S is in master
mode.
Not used in SPI mode.
Bit 8 ODD: Odd factor for the prescaler
0: real divider value is = I2SDIV *2
1: real divider value is = (I2SDIV * 2)+1
Refer to Section 25.4.3 on page 701. Not used in SPI mode.
Note: This bit should be configured when the I
2
S is disabled. It is used only when the I
2
S is in master
mode.
Bit 7:0 I2SDIV: I2S Linear prescaler
I2SDIV [7:0] = 0 or I2SDIV [7:0] = 1 are forbidden values.
Refer to Section 25.4.3 on page 701. Not used in SPI mode.
Note: These bits should be configured when the I
2
S is disabled. It is used only when the I
2
S is in
master mode.
Serial peripheral interface (SPI) RM0008
720/1093 Doc ID 13902 Rev 13
25.5.10 SPI register map
The table provides shows the SPI register map and reset values.
Refer to Table 3 on page 50 for the register boundary addresses.
Table 187. SPI register map and reset values
Offset Register
3
1
3
0
2
9
2
8
2
7
2
6
2
5
2
4
2
3
2
2
2
1
2
0
1
9
1
8
1
7
1
6
1
5
1
4
1
3
1
2
1
1
1
0
9 8 7 6 5 4 3 2 1 0
0x00
SPI_CR1
Reserved
B
I
D
I
M
O
D
E
B
I
D
I
O
E
C
R
C
E
N
C
R
C
N
E
X
T
D
F
F
R
X
O
N
L
Y
S
S
M
S
S
I
L
S
B
F
I
R
S
T
S
P
E
BR [2:0]
M
S
T
R
C
P
O
L
C
P
H
A
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x04
SPI_CR2
Reserved
T
X
E
I
E
R
X
N
E
I
E
E
R
R
I
E
R
e
s
e
r
v
e
d
S
S
O
E
T
X
D
M
A
E
N
R
X
D
M
A
E
N
Reset value 0 0 0 0 0 0
0x08
SPI_SR
Reserved
B
S
Y
O
V
R
M
O
D
F
C
R
C
E
R
R
U
D
R
C
H
S
I
D
E
T
X
E
R
X
N
E
Reset value 0 0 0 0 0 0 1 0
0x0C
SPI_DR
Reserved
DR[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x10
SPI_CRCPR
Reserved
CRCPOLY[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
0x14
SPI_RXCRCR
Reserved
RxCRC[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x18
SPI_TXCRCR
Reserved
TxCRC[15:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x1C
SPI_I2SCFGR
Reserved
I
2
S
M
O
D
I
2
S
E
I
2
S
C
F
G
P
C
M
S
Y
N
C
R
e
s
e
r
v
e
d
I
2
S
S
T
D
C
K
P
O
L
D
A
T
L
E
N
C
H
L
E
N
Reset value 0 0 0 0 0 0 0 0 0 0 0
0x20
SPI_I2SPR
Reserved
M
C
K
O
E
O
D
D
I2SDIV
Reset value 0 0 0 0 0 0 0 0 1 0
RM0008 Inter-integrated circuit (I
2
C) interface
Doc ID 13902 Rev 13 721/1093
26 Inter-integrated circuit (I
2
C) interface
Low-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 16 and 32 Kbytes.
Medium-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 64 and 128 Kbytes.
High-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 256 and 512 Kbytes.
XL-density devices are STM32F101xx and STM32F103xx microcontrollers where the
Flash memory density ranges between 768 Kbytes and 1 Mbyte.
Connectivity line devices are STM32F105xx and STM32F107xx microcontrollers.
This section applies to the whole STM32F10xxx family, unless otherwise specified.
26.1 I
2
C introduction
I
2
C (inter-integrated circuit) bus Interface serves as an interface between the microcontroller
and the serial I
2
C bus. It provides multimaster capability, and controls all I
2
C bus-specific
sequencing, protocol, arbitration and timing. It supports standard and fast speed modes. It is
also SMBus 2.0 compatible.
It may be used for a variety of purposes, including CRC generation and verification, SMBus
(system management bus) and PMBus (power management bus).
Depending on specific device implementation DMA capability can be available for reduced
CPU overload.
26.2 I
2
C main features
Parallel-bus/I
2
C protocol converter
Multimaster capability: the same interface can act as Master or Slave
I
2
C Master features:
Clock generation
Start and Stop generation
I
2
C Slave features:
Programmable I
2
C Address detection
Dual Addressing Capability to acknowledge 2 slave addresses
Stop bit detection
Generation and detection of 7-bit/10-bit addressing and General Call
Supports different communication speeds:
Standard Speed (up to 100 kHz),
Fast Speed (up to 400 kHz)
Status flags:
Transmitter/Receiver mode flag
End-of-Byte transmission flag
Inter-integrated circuit (I
2
C) interface RM0008
722/1093 Doc ID 13902 Rev 13
I
2
C busy flag
Error flags:
Arbitration lost condition for master mode
Acknowledgement failure after address/ data transmission
Detection of misplaced start or stop condition
Overrun/Underrun if clock stretching is disabled
2 Interrupt vectors:
1 Interrupt for successful address/ data communication
1 Interrupt for error condition
Optional clock stretching
1-byte buffer with DMA capability
Configurable PEC (packet error checking) generation or verification:
PEC value can be transmitted as last byte in Tx mode
PEC error checking for last received byte
SMBus 2.0 Compatibility:
25 ms clock low timeout delay
10 ms master cumulative clock low extend time
25 ms slave cumulative clock low extend time
Hardware PEC generation/verification with ACK control
Address Resolution Protocol (ARP) supported
PMBus Compatibility
Note: Some of the above features may not be available in certain products. The user should refer
to the product data sheet, to identify the specific features supported by the I
2
C interface
implementation.
26.3 I
2
C functional description
In addition to receiving and transmitting data, this interface converts it from serial to parallel
format and vice versa. The interrupts are enabled or disabled by software. The interface is
connected to the I
2
C bus by a data pin (SDA) and by a clock pin (SCL). It can be connected
with a standard (up to 100 kHz) or fast (up to 400 kHz) I
2
C bus.
26.3.1 Mode selection
The interface can operate in one of the four following modes:
Slave transmitter
Slave receiver
Master transmitter
Master receiver
By default, it operates in slave mode. The interface automatically switches from slave to
master, after it generates a START condition and from master to slave, if an arbitration loss
or a Stop generation occurs, allowing multimaster capability.
RM0008 Inter-integrated circuit (I
2
C) interface
Doc ID 13902 Rev 13 723/1093
Communication flow
In Master mode, the I
2
C interface initiates a data transfer and generates the clock signal. A
serial data transfer always begins with a start condition and ends with a stop condition. Both
start and stop conditions are generated in master mode by software.
In Slave mode, the interface is capable of recognizing its own addresses (7 or 10-bit), and
the General Call address. The General Call address detection may be enabled or disabled
by software.
Data and addresses are transferred as 8-bit bytes, MSB first. The first byte(s) following the
start condition contain the address (one in 7-bit mode, two in 10-bit mode). The address is
always transmitted in Master mode.
A 9th clock pulse follows the 8 clock cycles of a byte transfer, during which the receiver must
send an acknowledge bit to the transmitter. Refer to Figure 267.
Figure 267. I
2
C bus protocol
Acknowledge may be enabled or disabled by software. The I
2
C interface addresses (dual
addressing 7-bit/ 10-bit and/or general call address) can be selected by software.
The block diagram of the I
2
C interface is shown in Figure 268.
SCL
SDA
1 2 8 9
MSB
ACK
Stop Start
condition condition
Inter-integrated circuit (I
2
C) interface RM0008
724/1093 Doc ID 13902 Rev 13
Figure 268. I
2
C block diagram
1. SMBA is an optional signal in SMBus mode. This signal is not applicable if SMBus is disabled.
26.3.2 I
2
C slave mode
By default the I
2
C interface operates in Slave mode. To switch from default Slave mode to
Master mode a Start condition generation is needed.
The peripheral input clock must be programmed in the I2C_CR2 register in order to
generate correct timings. The peripheral input clock frequency must be at least:
2 MHz in Standard mode
4 MHz in Fast mode
As soon as a start condition is detected, the address is received from the SDA line and sent
to the shift register. Then it is compared with the address of the interface (OAR1) and with
OAR2 (if ENDUAL=1) or the General Call address (if ENGC = 1).
Note: In 10-bit addressing mode, the comparison includes the header sequence (11110xx0),
where xx denotes the two most significant bits of the address.
Header or address not matched: the interface ignores it and waits for another Start
condition.
Header matched (10-bit mode only): the interface generates an acknowledge pulse if the
ACK bit is set and waits for the 8-bit slave address.
Data shift register
Comparator
Own address register
Clock control
Status registers
Control registers
Control
Clock
control
Data
control
SCL
logic
Dual address register
Data register
PEC register
Interrupts
PEC calculation
SMBA
SDA
Register (CCR)
(SR1&SR2)
(CR1&CR2)
DMA requests & ACK
ai17189
RM0008 Inter-integrated circuit (I
2
C) interface
Doc ID 13902 Rev 13 725/1093
Address matched: the interface generates in sequence:
An acknowledge pulse if the ACK bit is set
The ADDR bit is set by hardware and an interrupt is generated if the ITEVFEN bit is
set.
If ENDUAL=1, the software has to read the DUALF bit to check which slave address
has been acknowledged.
In 10-bit mode, after receiving the address sequence the slave is always in Receiver mode.
It will enter Transmitter mode on receiving a repeated Start condition followed by the header
sequence with matching address bits and the least significant bit set (11110xx1).
The TRA bit indicates whether the slave is in Receiver or Transmitter mode.
Slave transmitter
Following the address reception and after clearing ADDR, the slave sends bytes from the
DR register to the SDA line via the internal shift register.
The slave stretches SCL low until ADDR is cleared and DR filled with the data to be sent
(see Figure 269 Transfer sequencing EV1 EV3).
When the acknowledge pulse is received:
The TxE bit is set by hardware with an interrupt if the ITEVFEN and the ITBUFEN bits
are set.
If TxE is set and some data were not written in the I2C_DR register before the end of the
next data transmission, the BTF bit is set and the interface waits until BTF is cleared by a
read to I2C_SR1 followed by a write to the I2C_DR register, stretching SCL low.
Figure 269. Transfer sequence diagram for slave transmitter
7-bit slave transmitter
10-bit slave transmitter
Legend: S= Start, S
r
= Repeated Start, P= Stop, A= Acknowledge, NA= Non-acknowledge,
EVx= Event (with interrupt if ITEVFEN=1)
EV1: ADDR=1, cleared by reading SR1 followed by reading SR2
EV3-1: TxE=1, shift register empty, data register empty, write Data1 in DR.
EV3: TxE=1, shift register not empty, data register empty, cleared by writing DR
EV3-2: AF=1; AF is cleared by writing 0 in AF bit of SR1 register.
S Address A Data1 A Data2 A
.....
DataN NA P
EV1 EV3-1 EV3 EV3 EV3 EV3-2
S Header A Address A
EV1
S
r
Header A Data1 A
....
DataN NA P
EV1 EV3_1 EV3 EV3 EV3-2
ai18209
Inter-integrated circuit (I
2
C) interface RM0008
726/1093 Doc ID 13902 Rev 13
Slave receiver
Following the address reception and after clearing ADDR, the slave receives bytes from the
SDA line into the DR register via the internal shift register. After each byte the interface
generates in sequence:
An acknowledge pulse if the ACK bit is set
The RxNE bit is set by hardware and an interrupt is generated if the ITEVFEN and
ITBUFEN bit is set.
If RxNE is set and the data in the DR register is not read before the end of the next data
reception, the BTF bit is set and the interface waits until BTF is cleared by a read from
I2C_SR1 followed by a read from the I2C_DR register, stretching SCL low (see Figure 270
Transfer sequencing).
Figure 270. Transfer sequence diagram for slave receiver
1. The EV1 event stretches SCL low until the end of the corresponding software sequence.
2. The EV2 software sequence must be completed before the end of the current byte transfer
3. After checking the SR1 register content, the user should perform the complete clearing sequence for each
flag found set.
Thus, for ADDR and STOPF flags, the following sequence is required inside the I2C interrupt routine:
READ SR1
if (ADDR == 1) {READ SR1; READ SR2}
if (STOPF == 1) {READ SR1; WRITE CR1}
The purpose is to make sure that both ADDR and STOPF flags are cleared if both are found set.
Closing slave communication
After the last data byte is transferred a Stop Condition is generated by the master. The
interface detects this condition and sets:
The STOPF bit and generates an interrupt if the ITEVFEN bit is set.
The STOPF is cleared by a read of the SR1 register followed by a write to the CR1 register
(see Figure 270: Transfer sequence diagram for slave receiver EV4).
26.3.3 I
2
C master mode
In Master mode, the I
2
C interface initiates a data transfer and generates the clock signal. A
serial data transfer always begins with a Start condition and ends with a Stop condition.
7-bit slave receiver
10-bit slave receiver
Legend: S= Start, S
r
= Repeated Start, P= Stop, A= Acknowledge,
EVx= Event (with interrupt if ITEVFEN=1)
EV1: ADDR=1, cleared by reading SR1 followed by reading SR2
EV2: RxNE=1 cleared by reading DR register.
EV4: STOPF=1, cleared by reading SR1 register followed by writing to the CR1 register
S Address A Data1 A Data2 A
.....
DataN A P
EV1 EV2 EV2 EV2 EV4
S Header A Address A Data1 A
.....
DataN A P
EV1 EV2 EV2 EV4
ai18208
RM0008 Inter-integrated circuit (I
2
C) interface
Doc ID 13902 Rev 13 727/1093
Master mode is selected as soon as the Start condition is generated on the bus with a
START bit.
The following is the required sequence in master mode.
Program the peripheral input clock in I2C_CR2 Register in order to generate correct
timings
Configure the clock control registers
Configure the rise time register
Program the I2C_CR1 register to enable the peripheral
Set the START bit in the I2C_CR1 register to generate a Start condition
The peripheral input clock frequency must be at least:
2 MHz in Standard mode
4 MHz in Fast mode
Start condition
Setting the START bit causes the interface to generate a Start condition and to switch to
Master mode (M/SL bit set) when the BUSY bit is cleared.
Note: In master mode, setting the START bit causes the interface to generate a ReStart condition
at the end of the current byte transfer.
Once the Start condition is sent:
The SB bit is set by hardware and an interrupt is generated if the ITEVFEN bit is set.
Then the master waits for a read of the SR1 register followed by a write in the DR register
with the Slave address (see Figure 271 & Figure 272 Transfer sequencing EV5).
Slave address transmission
Then the slave address is sent to the SDA line via the internal shift register.
In 10-bit addressing mode, sending the header sequence causes the following event:
The ADD10 bit is set by hardware and an interrupt is generated if the ITEVFEN bit
is set.
Then the master waits for a read of the SR1 register followed by a write in the DR
register with the second address byte (see Figure 271 & Figure 272 Transfer
sequencing).
The ADDR bit is set by hardware and an interrupt is generated if the ITEVFEN bit
is set.
Then the master waits for a read of the SR1 register followed by a read of the SR2
register (see Figure 271 & Figure 272 Transfer sequencing).
In 7-bit addressing mode, one address byte is sent.
As soon as the address byte is sent,
The ADDR bit is set by hardware and an interrupt is generated if the ITEVFEN bit
is set.
Then the master waits for a read of the SR1 register followed by a read of the SR2
register (see Figure 271 & Figure 272 Transfer sequencing).
Inter-integrated circuit (I
2
C) interface RM0008
728/1093 Doc ID 13902 Rev 13
The master can decide to enter Transmitter or Receiver mode depending on the LSB of the
slave address sent.
In 7-bit addressing mode,
To enter Transmitter mode, a master sends the slave address with LSB reset.
To enter Receiver mode, a master sends the slave address with LSB set.
In 10-bit addressing mode,
To enter Transmitter mode, a master sends the header (11110xx0) and then the
slave address, (where xx denotes the two most significant bits of the address).
To enter Receiver mode, a master sends the header (11110xx0) and then the
slave address. Then it should send a repeated Start condition followed by the
header (11110xx1), (where xx denotes the two most significant bits of the
address).
The TRA bit indicates whether the master is in Receiver or Transmitter mode.
Master transmitter
Following the address transmission and after clearing ADDR, the master sends bytes from
the DR register to the SDA line via the internal shift register.
The master waits until the first data byte is written into I2C_DR (see Figure 271 Transfer
sequencing EV8_1).
When the acknowledge pulse is received:
The TxE bit is set by hardware and an interrupt is generated if the ITEVFEN and
ITBUFEN bits are set.
If TxE is set and a data byte was not written in the DR register before the end of the last data
transmission, BTF is set and the interface waits until BTF is cleared bya read from I2C_SR1
followed by a write to I2C_DR, stretching SCL low.
Closing the communication
After the last byte is written to the DR register, the STOP bit is set by software to generate a
Stop condition (see Figure 271 Transfer sequencing EV8_2). The interface automatically
goes back to slave mode (M/SL bit cleared).
Note: Stop condition should be programmed during EV8_2 event, when either TxE or BTF is set.
RM0008 Inter-integrated circuit (I
2
C) interface
Doc ID 13902 Rev 13 729/1093
Figure 271. Transfer sequence diagram for master transmitter
7-bit master transmitter
10-bit master transmitter
Legend: S= Start, S
r
= Repeated Start, P= Stop, A= Acknowledge,
EVx= Event (with interrupt if ITEVFEN = 1)
EV5: SB=1, cleared by reading SR1 register followed by writing DR register with Address.
EV6: ADDR=1, cleared by reading SR1 register followed by reading SR2.
EV8_1: TxE=1, shift register empty, data register empty, write Data1 in DR.
EV8: TxE=1, shift register not empty, data register empty, cleared by writing DR register .
EV8_2: TxE=1, BTF = 1, Program Stop request. TxE and BTF are cleared by hardware by the Stop condition
EV9: ADD10=1, cleared by reading SR1 register followed by writing DR register.
S Address A Data1 A Data2 A
.....
DataN A P
EV5 EV6 EV8_1 EV8 EV8 EV8 EV8_2
S Header A Address A Data1 A
.....
DataN A P
EV5 EV9 EV6 EV8_1 EV8 EV8 EV8_2
ai15881b
Notes: 1- The EV5, EV6, EV9, EV8_1 and EV8_2 events stretch SCL low until the end of the corresponding software sequence.
2- The EV8 software sequence must complete before the end of the current byte transfer. In case EV8 software
sequence can not be managed before the current byte end of transfer, it is recommended to use BTF instead
of TXE with the drawback of slowing the communication.
Inter-integrated circuit (I
2
C) interface RM0008
730/1093 Doc ID 13902 Rev 13
Master receiver
Following the address transmission and after clearing ADDR, the I
2
C interface enters
Master Receiver mode. In this mode the interface receives bytes from the SDA line into the
DR register via the internal shift register. After each byte the interface generates in
sequence:
1. An acknowledge pulse if the ACK bit is set
2. The RxNE bit is set and an interrupt is generated if the ITEVFEN and ITBUFEN bits are
set (see Figure 272 Transfer sequencing EV7).
If the RxNE bit is set and the data in the DR register is not read before the end of the last
data reception, the BTF bit is set by hardware and the interface waits until BTF is cleared by
a read in the SR1 register followed by a read in the DR register, stretching SCL low.
Closing the communication
Method 1: This method is for the case when the I2C is used with interrupts that have the
highest priority in the application.
The master sends a NACK for the last byte received from the slave. After receiving this
NACK, the slave releases the control of the SCL and SDA lines. Then the master can send
a Stop/Restart condition.
1. To generate the nonacknowledge pulse after the last received data byte, the ACK bit
must be cleared just after reading the second last data byte (after second last RxNE
event).
2. To generate the Stop/Restart condition, software must set the STOP/START bit just
after reading the second last data byte (after the second last RxNE event).
3. In case a single byte has to be received, the Acknowledge disable and the Stop
condition generation are made just after EV6 (in EV6_1, just after ADDR is cleared).
After the Stop condition generation, the interface goes automatically back to slave mode
(M/SL bit cleared).
RM0008 Inter-integrated circuit (I
2
C) interface
Doc ID 13902 Rev 13 731/1093
Figure 272. Method 1: transfer sequence diagram for master receiver
1. If a single byte is received, it is NA.
2. The EV5, EV6 and EV9 events stretch SCL low until the end of the corresponding software sequence.
3. The EV7 software sequence must complete before the end of the current byte transfer. In case EV7
software sequence can not be managed before the current byte end of transfer, it is recommended to use
BTF instead of RXNE with the drawback of slowing the communication.
4. The EV6_1 or EV7_1 software sequence must complete before the ACK pulse of the current byte transfer.
Method 2: This method is for the case when the I2C is used with interrupts that do not have
the highest priority in the application or when the I2C is used with polling.
With this method, DataN_2 is not read, so that after DataN_1, the communication is
stretched (both RxNE and BTF are set). Then, clear the ACK bit before reading DataN-2 in
DR to ensure it is be cleared before the DataN Acknowledge pulse. After that, just after
reading DataN_2, set the STOP/ START bit and read DataN_1. After RxNE is set, read
DataN. This is illustrated below:
7-bit master receiver
10-bit master receiver
Legend: S= Start, S
r
= Repeated Start, P= Stop, A= Acknowledge, NA= Non-acknowledge,
EVx= Event (with interrupt if TEVFEN=1)
EV5: SB=1, cleared by reading SR1 register followed by writing DR register.
EV6: ADDR=1, cleared by reading SR1 register followed by reading SR2. n 10-bit master receiver mode, this se-
quence should be followed by writing CR2 with START = 1.
EV7: RxNE=1 cleared by reading DR register.
EV7_1: RxNE=1 cleared by reading DR register, program ACK=0 and STOP request
EV9: ADD10=1, cleared by reading SR1 register followed by writing DR register.
S Address A Data1 A
(1)
Data2 A
.....
DataN NA P
EV5 EV6 EV7 EV7 EV7_1 EV7
S Header A Address A
EV5 EV9 EV6
S
r
Header A Data1 A
(1)
.....
EV5 EV6 EV7 EV6_1
EV6_1: no associated flag event, used for 1 byte reception only. The Acknowledge disable and Stop condition
generation are made just after EV6, that is after ADDR is cleared.
Data2 A
EV7
DataN NA P
EV7_1 EV7
ai15882
EV6_1
Inter-integrated circuit (I
2
C) interface RM0008
732/1093 Doc ID 13902 Rev 13
Figure 273. Method 2: transfer sequence diagram for master receiver when N>2
1. The EV5, EV6 and EV9 events stretch SCL low until the end of the corresponding software sequence.
2. The EV7 software sequence must complete before the end of the current byte transfer.In case EV7
software sequence can not be managed before the current byte end of transfer, it is recommended to use
BTF instead of RXNE with the drawback of slowing the communication.
When 3 bytes remain to be read:
RxNE = 1 => Nothing (DataN-2 not read).
DataN-1 received
BTF = 1 because both shift and data registers are full: DataN-2 in DR and DataN-1 in
the shift register => SCL tied low: no other data will be received on the bus.
Clear ACK bit
Read DataN-2 in DR => This will launch the DataN reception in the shift register
DataN received (with a NACK)
Program START/STOP
Read DataN-1
RxNE = 1
Read DataN
A Address S
EV5 EV6
A Data1 A Data2
EV7 EV7
A DataN-2 A DataN-1
EV7_2
NA DataN
EV7
P
Legend: S = Start, S
r
= Repeated Start, P = Stop, A = Acknowledge, NA = Non-acknowledge,
EV5: SB=1, cleared by reading SR1 register followed by writing the DR register.
EV6: ADDR1, cleared by reading SR1 register followed by reading SR2.
In 10-bit master receiver mode, this sequence should be followed by writing CR2 with START = 1.
EV7: RxNE=1, cleared by reading DR register
EV7_2: BTF = 1, DataN-2 in DR and DataN-1 in shift register, program ACK = 0, Read DataN-2 in DR.
Program STOP = 1, read DataN-1.
7- bit master receiver
10- bit master receiver
A Header S
EV5 EV9
A Data1 A Data2
EV7 EV7
A DataN-2 A DataN-1
EV7_2
NA DataN
EV7
P
A Address
EV6
A Header S
r
EV5 EV6
EV9: ADD10= 1, cleared by reading SR1 register followed by writing DR register.
EVx = Event (with interrupt if ITEVFEN = 1)
RM0008 Inter-integrated circuit (I
2
C) interface
Doc ID 13902 Rev 13 733/1093
The procedure described above is valid for N>2. The cases where a single byte or two bytes
are to be received should be handled differently, as described below:
Case of a single byte to be received:
In the ADDR event, clear the ACK bit.
Clear ADDR
Program the STOP/START bit.
Read the data after the RxNE flag is set.
Case of two bytes to be received:
Set POS and ACK
Wait for the ADDR flag to be set
Clear ADDR
Clear ACK
Wait for BTF to be set
Program STOP
Read DR twice
Figure 274. Method 2: transfer sequence diagram for master receiver when N=2
1. The EV5, EV6 and EV9 events stretch SCL low until the end of the corresponding software sequence.
2. The EV6_1 software sequence must complete before the ACK pulse of the current byte transfer.
A Address S
EV5 EV6
A Data1 Data2
EV7_3
NA P
Legend: S = Start, S
r
= Repeated Start, P = Stop, A = Acknowledge, NA = Non-acknowledge,
EV5: SB=1, cleared by reading SR1 register followed by writing the DR register.
EV6: ADDR1, cleared by reading SR1 register followed by reading SR2.
In 10-bit master receiver mode, this sequence should be followed by writing CR2 with START = 1.
EV6_1: No associated flag event. The acknowledge disable should be done just after EV6, that is after ADDR is cleared.
EVx = Event (with interrupt if ITEVFEN = 1)
EV6_1
EV7_3: BTF = 1, program STOP = 1, read DR twice (Read Data1 and Data2) just after programming the STOP.
7- bit master receiver
10- bit master receiver
A Header S
EV5 EV9
A Address
EV6
A Data1 Data2
EV7_3
NA P
EV6_1
A Header S
r
EV5 EV6
EV9: ADD10= 1, cleared by reading SR1 register followed by writing DR register.
Inter-integrated circuit (I
2
C) interface RM0008
734/1093 Doc ID 13902 Rev 13
Figure 275. Method 2: transfer sequence diagram for master receiver when N=1
1. The EV5, EV6 and EV9 events stretch SCL low until the end of the corresponding software sequence.
26.3.4 Error conditions
The following are the error conditions which may cause communication to fail.
Bus error (BERR)
This error occurs when the I
2
C interface detects an external Stop or Start condition during
an address or a data transfer. In this case:
the BERR bit is set and an interrupt is generated if the ITERREN bit is set
in Slave mode: data are discarded and the lines are released by hardware:
in case of a misplaced Start, the slave considers it is a restart and waits for an
address, or a Stop condition
in case of a misplaced Stop, the slave behaves like for a Stop condition and the
lines are released by hardware
In Master mode: the lines are not released and the state of the current transmission is
not affected. It is up to the software to abort or not the current transmission
Acknowledge failure (AF)
This error occurs when the interface detects a nonacknowledge bit. In this case:
the AF bit is set and an interrupt is generated if the ITERREN bit is set
a transmitter which receives a NACK must reset the communication:
If Slave: lines are released by hardware
If Master: a Stop or repeated Start condition must be generated by software
A Address S
EV5
NA Data1
EV7
P
Legend: S = Start, S
r
= Repeated Start, P = Stop, A = Acknowledge, NA = Non-acknowledge,
EV5: SB=1, cleared by reading SR1 register followed by writing the DR register.
EV6_3: ADDR = 1, program ACK = 0, clear ADDR by reading SR1 register followed by reading SR2 register, program
.
EV6_3
STOP =1 just after ADDR is cleared.
Note: The EV6_3 software sequence must complete before the current byte end of transfer.
10- bit master receiver
A Header S
EV5 EV9
A Address
EV6
7- bit master receiver
NA Data1
EV7
P
EV6_3
A Header S
r
EV5
EV9: ADD10= 1, cleared by reading SR1 register followed by writing DR register.
EVx = Event (with interrupt if ITEVFEN = 1)
EV7: RxNE =1, cleared by reading DR register.
EV6: ADDR =1, cleared by reading SR1 resister followed by reading SR2 register.
RM0008 Inter-integrated circuit (I
2
C) interface
Doc ID 13902 Rev 13 735/1093
Arbitration lost (ARLO)
This error occurs when the I
2
C interface detects an arbitration lost condition. In this case,
the ARLO bit is set by hardware (and an interrupt is generated if the ITERREN bit is
set)
the I
2
C Interface goes automatically back to slave mode (the M/SL bit is cleared). When
the I
2
C loses the arbitration, it is not able to acknowledge its slave address in the same
transfer, but it can acknowledge it after a repeated Start from the winning master.
lines are released by hardware
Overrun/underrun error (OVR)
An overrun error can occur in slave mode when clock stretching is disabled and the I
2
C
interface is receiving data. The interface has received a byte (RxNE=1) and the data in DR
has not been read, before the next byte is received by the interface. In this case,
The last received byte is lost.
In case of Overrun error, software should clear the RxNE bit and the transmitter should
re-transmit the last received byte.
Underrun error can occur in slave mode when clock stretching is disabled and the I
2
C
interface is transmitting data. The interface has not updated the DR with the next byte
(TxE=1), before the clock comes for the next byte. In this case,
The same byte in the DR register will be sent again
The user should make sure that data received on the receiver side during an underrun
error are discarded and that the next bytes are written within the clock low time
specified in the I
2
C bus standard.
For the first byte to be transmitted, the DR must be written after ADDR is cleared and before
the first SCL rising edge. If not possible, the receiver must discard the first data.
26.3.5 SDA/SCL line control
If clock stretching is enabled:
Transmitter mode: If TxE=1 and BTF=1: the interface holds the clock line low
before transmission to wait for the microcontroller to read SR1 and then write the
byte in the Data Register (both buffer and shift register are empty).
Receiver mode: If RxNE=1 and BTF=1: the interface holds the clock line low after
reception to wait for the microcontroller to read SR1 and then read the byte in the
Data Register (both buffer and shift register are full).
If clock stretching is disabled in Slave mode:
Overrun Error in case of RxNE=1 and no read of DR has been done before the
next byte is received. The last received byte is lost.
Underrun Error in case TxE=1 and no write into DR has been done before the next
byte must be transmitted. The same byte will be sent again.
Write Collision not managed.
Inter-integrated circuit (I
2
C) interface RM0008
736/1093 Doc ID 13902 Rev 13
26.3.6 SMBus
Introduction
The System Management Bus (SMBus) is a two-wire interface through which various
devices can communicate with each other and with the rest of the system. It is based on I
2
C
principles of operation. SMBus provides a control bus for system and power management
related tasks. A system may use SMBus to pass messages to and from devices instead of
toggling individual control lines.
The System Management Bus Specification refers to three types of devices. A slave is a
device that is receiving or responding to a command. A master is a device that issues
commands, generates the clocks, and terminates the transfer. A host is a specialized master
that provides the main interface to the system's CPU. A host must be a master-slave and
must support the SMBus host notify protocol. Only one host is allowed in a system.
Similarities between SMBus and I
2
C
2 wire bus protocol (1 Clk, 1 Data) + SMBus Alert line optional
Master-slave communication, Master provides clock
Multi master capability
SMBus data format similar to I
2
C 7-bit addressing format (Figure 267).
Differences between SMBus and I
2
C
The following table describes the differences between SMBus and I
2
C.
SMBus application usage
With System Management Bus, a device can provide manufacturer information, tell the
system what its model/part number is, save its state for a suspend event, report different
types of errors, accept control parameters, and return its status. SMBus provides a control
bus for system and power management related tasks.
Device identification
Any device that exists on the System Management Bus as a slave has a unique address
called the Slave Address. For the list of reserved slave addresses, refer to the SMBus
specification ver. 2.0 (http://smbus.org/specs/).
Table 188. SMBus vs. I
2
C
SMBus I
2
C
Max. speed 100 kHz Max. speed 400 kHz
Min. clock speed 10 kHz No minimum clock speed
35 ms clock low timeout No timeout
Logic levels are fixed Logic levels are V
DD
dependent
Different address types (reserved, dynamic etc.) 7-bit, 10-bit and general call slave address types
Different bus protocols (quick command, process
call etc.)
No bus protocols
RM0008 Inter-integrated circuit (I
2
C) interface
Doc ID 13902 Rev 13 737/1093
Bus protocols
The SMBus specification supports up to 9 bus protocols. For more details of these protocols
and SMBus address types, refer to SMBus specification ver. 2.0 (http://smbus.org/specs/).
These protocols should be implemented by the user software.
Address resolution protocol (ARP)
SMBus slave address conflicts can be resolved by dynamically assigning a new unique
address to each slave device. The Address Resolution Protocol (ARP) has the following
attributes:
Address assignment uses the standard SMBus physical layer arbitration mechanism
Assigned addresses remain constant while device power is applied; address retention
through device power loss is also allowed
No additional SMBus packet overhead is incurred after address assignment. (i.e.
subsequent accesses to assigned slave addresses have the same overhead as
accesses to fixed address devices.)
Any SMBus master can enumerate the bus
Unique device identifier (UDID)
In order to provide a mechanism to isolate each device for the purpose of address
assignment, each device must implement a unique device identifier (UDID).
For the details on 128 bit UDID and more information on ARP, refer to SMBus specification
ver. 2.0 (http://smbus.org/specs/).
SMBus alert mode
SMBus Alert is an optional signal with an interrupt line for devices that want to trade their
ability to master for a pin. SMBA is a wired-AND signal just as the SCL and SDA signals are.
SMBA is used in conjunction with the SMBus General Call Address. Messages invoked with
the SMBus are 2 bytes long.
A slave-only device can signal the host through SMBA that it wants to talk by setting ALERT
bit in I2C_CR1 register. The host processes the interrupt and simultaneously accesses all
SMBA devices through the Alert Response Address (known as ARA having a value 0001
100X). Only the device(s) which pulled SMBA low will acknowledge the Alert Response
Address. This status is identified using SMBALERT Status flag in I2C_SR1 register. The
host performs a modified Receive Byte operation. The 7 bit device address provided by the
slave transmit device is placed in the 7 most significant bits of the byte. The eighth bit can be
a zero or one.
If more than one device pulls SMBA low, the highest priority (lowest address) device will win
communication rights via standard arbitration during the slave address transfer. After
acknowledging the slave address the device must disengage its SMBA pull-down. If the host
still sees SMBA low when the message transfer is complete, it knows to read the ARA
again.
A host which does not implement the SMBA signal may periodically access the ARA.
For more details on SMBus Alert mode, refer to SMBus specification ver. 2.0
(http://smbus.org/specs/).
Inter-integrated circuit (I
2
C) interface RM0008
738/1093 Doc ID 13902 Rev 13
Timeout error
There are differences in the timing specifications between I
2
C and SMBus.
SMBus defines a clock low timeout, TIMEOUT of 35 ms. Also SMBus specifies TLOW:
SEXT as the cumulative clock low extend time for a slave device. SMBus specifies TLOW:
MEXT as the cumulative clock low extend time for a master device. For more details on
these timeouts, refer to SMBus specification ver. 2.0 (http://smbus.org/specs/).
The status flag Timeout or Tlow Error in I2C_SR1 shows the status of this feature.
How to use the interface in SMBus mode
To switch from I
2
C mode to SMBus mode, the following sequence should be performed.
Set the SMBus bit in the I2C_CR1 register
Configure the SMBTYPE and ENARP bits in the I2C_CR1 register as required for the
application
If you want to configure the device as a master, follow the Start condition generation
procedure in Section 26.3.3: I2C master mode. Otherwise, follow the sequence in
Section 26.3.2: I2C slave mode.
The application has to control the various SMBus protocols by software.
SMB Device Default Address acknowledged if ENARP=1 and SMBTYPE=0
SMB Host Header acknowledged if ENARP=1 and SMBTYPE=1
SMB Alert Response Address acknowledged if SMBALERT=1
26.3.7 DMA requests
DMA requests (when enabled) are generated only for data transfer. DMA requests are
generated by Data Register becoming empty in transmission and Data Register becoming
full in reception. The DMA request must be served before the end of the current byte
transfer. When the number of data transfers which has been programmed for the
corresponding DMA channel is reached, the DMA controller sends an End of Transfer EOT
signal to the I
2
C interface and generates a Transfer Complete interrupt if enabled:
Master transmitter: In the interrupt routine after the EOT interrupt, disable DMA
requests then wait for a BTF event before programming the Stop condition.
Master receiver: when the number of bytes to be received is equal to or greater than
two, the DMA controller sends a hardware signal, EOT_1, corresponding to the last but
one data byte (number_of_bytes 1). If, in the I2C_CR2 register, the LAST bit is set,
I
2
C automatically sends a NACK after the next byte following EOT_1. The user can
generate a Stop condition in the DMA Transfer Complete interrupt routine if enabled.
Transmission using DMA
DMA mode can be enabled for transmission by setting the DMAEN bit in the I2C_CR2
register. Data will be loaded from a Memory area configured using the DMA peripheral (refer
to the DMA specification) to the I2C_DR register whenever the TxE bit is set. To map a DMA
channel for I
2
C transmission, perform the following sequence. Here x is the channel number.
RM0008 Inter-integrated circuit (I
2
C) interface
Doc ID 13902 Rev 13 739/1093
1. Set the I2C_DR register address in the DMA_CPARx register. The data will be moved
to this address from the memory after each TxE event.
2. Set the memory address in the DMA_CMARx register. The data will be loaded into
I2C_DR from this memory after each TxE event.
3. Configure the total number of bytes to be transferred in the DMA_CNDTRx register.
After each TxE event, this value will be decremented.
4. Configure the channel priority using the PL[0:1] bits in the DMA_CCRx register
5. Set the DIR bit and, in the DMA_CCRx register, configure interrupts after half transfer
or full transfer depending on application requirements.
6. Activate the channel by setting the EN bit in the DMA_CCRx register.
When the number of data transfers which has been programmed in the DMA Controller
registers is reached, the DMA controller sends an End of Transfer EOT/ EOT_1 signal to the
I
2
C interface and the DMA generates an interrupt, if enabled, on the DMA channel interrupt
vector.
Note: Do not enable the ITBUFEN bit in the I2C_CR2 register if DMA is used for transmission.
Reception using DMA
DMA mode can be enabled for reception by setting the DMAEN bit in the I2C_CR2 register.
Data will be loaded from the I2C_DR register to a Memory area configured using the DMA
peripheral (refer to the DMA specification) whenever a data byte is received. To map a DMA
channel for I
2
C reception, perform the following sequence. Here x is the channel number.
1. Set the I2C_DR register address in DMA_CPARx register. The data will be moved from
this address to the memory after each RxNE event.
2. Set the memory address in the DMA_CMARx register. The data will be loaded from the
I2C_DR register to this memory area after each RxNE event.
3. Configure the total number of bytes to be transferred in the DMA_CNDTRx register.
After each RxNE event, this value will be decremented.
4. Configure the channel priority using the PL[0:1] bits in the DMA_CCRx register
5. Reset the DIR bit and configure interrupts in the DMA_CCRx register after half transfer
or full transfer depending on application requirements.
6. Activate the channel by setting the EN bit in the DMA_CCRx register.
When the number of data transfers which has been programmed in the DMA Controller
registers is reached, the DMA controller sends an End of Transfer EOT/ EOT_1 signal to the
I
2
C interface and DMA generates an interrupt, if enabled, on the DMA channel interrupt
vector.
Note: Do not enable the ITBUFEN bit in the I2C_CR2 register if DMA is used for reception.
Inter-integrated circuit (I
2
C) interface RM0008
740/1093 Doc ID 13902 Rev 13
26.3.8 Packet error checking
A PEC calculator has been implemented to improve the reliability of communication. The
PEC is calculated by using the C(x) = x
8
+ x
2
+ x + 1 CRC-8 polynomial serially on each bit.
PEC calculation is enabled by setting the ENPEC bit in the I2C_CR1 register. PEC is a
CRC-8 calculated on all message bytes including addresses and R/W bits.
In transmission: set the PEC transfer bit in the I2C_CR1 register after the TxE
event corresponding to the last byte. The PEC will be transferred after the last
transmitted byte.
In reception: set the PEC bit in the I2C_CR1 register after the RxNE event
corresponding to the last byte so that the receiver sends a NACK if the next
received byte is not equal to the internally calculated PEC. In case of Master-
Receiver, a NACK must follow the PEC whatever the check result.The PEC must
be set before the ACK pulse of the current byte reception.
A PECERR error flag/interrupt is also available in the I2C_SR1 register.
If DMA and PEC calculation are both enabled:-
In transmission: when the I
2
C interface receives an EOT signal from the DMA
controller, it automatically sends a PEC after the last byte.
In reception: when the I
2
C interface receives an EOT_1 signal from the DMA
controller, it will automatically consider the next byte as a PEC and will check it. A
DMA request is generated after PEC reception.
To allow intermediate PEC transfers, a control bit is available in the I2C_CR2 register
(LAST bit) to determine if it is really the last DMA transfer or not. If it is the last DMA
request for a master receiver, a NACK is automatically sent after the last received byte.
PEC calculation is corrupted by an arbitration loss.
26.4 I
2
C interrupts
The table below gives the list of I
2
C interrupt requests.
T
Table 189. I
2
C Interrupt requests
Interrupt event Event flag Enable control bit
Start bit sent (Master) SB
ITEVFEN
Address sent (Master) or Address matched (Slave) ADDR
10-bit header sent (Master) ADD10
Stop received (Slave) STOPF
Data byte transfer finished BTF
Receive buffer not empty RxNE
ITEVFEN and ITBUFEN
Transmit buffer empty TxE
RM0008 Inter-integrated circuit (I
2
C) interface
Doc ID 13902 Rev 13 741/1093
Note: 1 SB, ADDR, ADD10, STOPF, BTF, RxNE and TxE are logically ORed on the same interrupt
channel.
2 BERR, ARLO, AF, OVR, PECERR, TIMEOUT and SMBALERT are logically ORed on the
same interrupt channel.
Figure 276. I
2
C interrupt mapping diagram
Bus error BERR
ITERREN
Arbitration loss (Master) ARLO
Acknowledge failure AF
Overrun/Underrun OVR
PEC error PECERR
Timeout/Tlow error TIMEOUT
SMBus Alert SMBALERT
Table 189. I
2
C Interrupt requests (continued)
Interrupt event Event flag Enable control bit
ADDR
SB
ADD10
RxNE
TxE
BTF
it_event
ARLO
BERR
AF
OVR
PECERR
TIMEOUT
SMBALERT
ITERREN
it_error
ITEVFEN
ITBUFEN
STOPF
Inter-integrated circuit (I
2
C) interface RM0008
742/1093 Doc ID 13902 Rev 13
26.5 I
2
C debug mode
When the microcontroller enters the debug mode (Cortex-M3 core halted), the SMBUS
timeout either continues to work normally or stops, depending on the
DBG_I2Cx_SMBUS_TIMEOUT configuration bits in the DBG module. For more details,
refer to Section 31.16.2: Debug support for timers, watchdog, bxCAN and I2C on
page 1066.
26.6 I
2
C registers
Refer to Section 2.1 on page 46 for a list of abbreviations used in register descriptions.
The peripheral registers can be accessed by half-words (16-bit) or words (32-bit).
26.6.1 I
2
C Control register 1 (I2C_CR1)
Address offset: 0x00
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
SWRST
Res.
ALERT PEC POS ACK STOP START
NO
STRETCH
ENGC ENPEC ENARP
SMB
TYPE
Res.
SMBUS PE
rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bit 15 SWRST: Software reset
When set, the I2C is under reset state. Before resetting this bit, make sure the I2C lines are
released and the bus is free.
0: I
2
C Peripheral not under reset
1: I
2
C Peripheral under reset state
Note: This bit can be used in case the BUSY bit is set to 1 when no stop condition has been
detected on the bus.
Bit 14 Reserved, forced by hardware to 0.
Bit 13 ALERT: SMBus alert
This bit is set and cleared by software, and cleared by hardware when PE=0.
0: Releases SMBA pin high. Alert Response Address Header followed by NACK.
1: Drives SMBA pin low. Alert Response Address Header followed by ACK.
Bit 12 PEC: Packet error checking
This bit is set and cleared by software, and cleared by hardware when PEC is transferred or
by a START or Stop condition or when PE=0.
0: No PEC transfer
1: PEC transfer (in Tx or Rx mode)
Note: PEC calculation is corrupted by an arbitration loss.
RM0008 Inter-integrated circuit (I
2
C) interface
Doc ID 13902 Rev 13 743/1093
Bit 11 POS: Acknowledge/PEC Position (for data reception)
This bit is set and cleared by software and cleared by hardware when PE=0.
0: ACK bit controls the (N)ACK of the current byte being received in the shift register. The
PEC bit indicates that current byte in shift register is a PEC.
1: ACK bit controls the (N)ACK of the next byte which will be received in the shift register.
The PEC bit indicates that the next byte in the shift register is a PEC
Note: The POS bit is used when the procedure for reception of 2 bytes (see Method 2:
transfer sequence diagram for master receiver when N=2) is followed. It must be
configured before data reception starts. In this case, to NACK the 2nd byte, the ACK bit
must be cleared just after ADDR is cleared. To check the 2nd byte as PEC, the PEC bit
must be set during the ADDR stretch event after configuring the POS bit.
Bit 10 ACK: Acknowledge enable
This bit is set and cleared by software and cleared by hardware when PE=0.
0: No acknowledge returned
1: Acknowledge returned after a byte is received (matched address or data)
Bit 9 STOP: Stop generation
The bit is set and cleared by software, cleared by hardware when a Stop condition is
detected, set by hardware when a timeout error is detected.
In Master Mode:
0: No Stop generation.
1: Stop generation after the current byte transfer or after the current Start condition is sent.
In Slave mode:
0: No Stop generation.
1: Release the SCL and SDA lines after the current byte transfer.
Note: When the STOP, START or PEC bit is set, the software must not perform any write
access to I2C_CR1 before this bit is cleared by hardware. Otherwise there is a risk of
setting a second STOP, START or PEC request.
Bit 8 START: Start generation
This bit is set and cleared by software and cleared by hardware when start is sent or PE=0.
In Master Mode:
0: No Start generation
1: Repeated start generation
In Slave mode:
0: No Start generation
1: Start generation when the bus is free
Bit 7 NOSTRETCH: Clock stretching disable (Slave mode)
This bit is used to disable clock stretching in slave mode when ADDR or BTF flag is set, until
it is reset by software.
0: Clock stretching enabled
1: Clock stretching disabled
Bit 6 ENGC: General call enable
0: General call disabled. Address 00h is NACKed.
1: General call enabled. Address 00h is ACKed.
Bit 5 ENPEC: PEC enable
0: PEC calculation disabled
1: PEC calculation enabled
Inter-integrated circuit (I
2
C) interface RM0008
744/1093 Doc ID 13902 Rev 13
26.6.2 I
2
C Control register 2 (I2C_CR2)
Address offset: 0x04
Reset value: 0x0000
Bit 4 ENARP: ARP enable
0: ARP disable
1: ARP enable
SMBus Device default address recognized if SMBTYPE=0
SMBus Host address recognized if SMBTYPE=1
Bit 3 SMBTYPE: SMBus type
0: SMBus Device
1: SMBus Host
Bit 2 Reserved, forced by hardware to 0.
Bit 1 SMBUS: SMBus mode
0: I
2
C mode
1: SMBus mode
Bit 0 PE: Peripheral enable
0: Peripheral disable
1: Peripheral enable: the corresponding IOs are selected as alternate functions depending
on SMBus bit.
Note: If this bit is reset while a communication is on going, the peripheral is disabled at the
end of the current communication, when back to IDLE state.
All bit resets due to PE=0 occur at the end of the communication.
In master mode, this bit must not be reset before the end of the communication.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
LAST
DMA
EN
ITBUF
EN
ITEVT
EN
ITERR
EN
Reserved
FREQ[5:0]
rw rw rw rw rw rw rw rw rw rw rw
Bits 15:13 Reserved, forced by hardware to 0.
Bit 12 LAST: DMA last transfer
0: Next DMA EOT is not the last transfer
1: Next DMA EOT is the last transfer
Note: This bit is used in master receiver mode to permit the generation of a NACK on the last
received data.
Bit 11 DMAEN: DMA requests enable
0: DMA requests disabled
1: DMA request enabled when TxE=1 or RxNE =1
Bit 10 ITBUFEN: Buffer interrupt enable
0: TxE = 1 or RxNE = 1 does not generate any interrupt.
1:TxE = 1 or RxNE = 1 generates Event Interrupt (whatever the state of DMAEN)
RM0008 Inter-integrated circuit (I
2
C) interface
Doc ID 13902 Rev 13 745/1093
Bit 9 ITEVTEN: Event interrupt enable
0: Event interrupt disabled
1: Event interrupt enabled
This interrupt is generated when:
SB = 1 (Master)
ADDR = 1 (Master/Slave)
ADD10= 1 (Master)
STOPF = 1 (Slave)
BTF = 1 with no TxE or RxNE event
TxE event to 1 if ITBUFEN = 1
RxNE event to 1if ITBUFEN = 1
Bit 8 ITERREN: Error interrupt enable
0: Error interrupt disabled
1: Error interrupt enabled
This interrupt is generated when:
BERR = 1
ARLO = 1
AF = 1
OVR = 1
PECERR = 1
TIMEOUT = 1
SMBALERT = 1
Bits 7:6 Reserved, forced by hardware to 0.
Bits 5:0 FREQ[5:0]: Peripheral clock frequency
The peripheral clock frequency must be configured using the input APB clock frequency (I2C
peripheral connected to APB). The minimum allowed frequency is 2 MHz, the maximum
frequency is limited by the maximum APB frequency (36 MHz) and an intrinsic limitation of
46 MHz.
0b000000: Not allowed
0b000001: Not allowed
0b000010: 2 MHz
...
0b100100: 36 MHz
Higher than 0b100100: Not allowed
Inter-integrated circuit (I
2
C) interface RM0008
746/1093 Doc ID 13902 Rev 13
26.6.3 I
2
C Own address register 1 (I2C_OAR1)
Address offset: 0x08
Reset value: 0x0000
26.6.4 I
2
C Own address register 2 (I2C_OAR2)
Address offset: 0x0C
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
ADD
MODE
Reserved
ADD[9:8] ADD[7:1] ADD0
rw rw rw rw rw rw rw rw rw rw rw
Bit 15 ADDMODE Addressing mode (slave mode)
0: 7-bit slave address (10-bit address not acknowledged)
1: 10-bit slave address (7-bit address not acknowledged)
Bit 14 Should always be kept at 1 by software.
Bits 13:10 Reserved, forced by hardware to 0.
Bits 9:8 ADD[9:8]: Interface address
7-bit addressing mode: dont care
10-bit addressing mode: bits9:8 of address
Bits 7:1 ADD[7:1]: Interface address
bits 7:1 of address
Bit 0 ADD0: Interface address
7-bit addressing mode: dont care
10-bit addressing mode: bit 0 of address
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
ADD2[7:1] ENDUAL
rw rw rw rw rw rw rw rw
Bits 15:8 Reserved, forced by hardware to 0.
Bits 7:1 ADD2[7:1]: Interface address
bits 7:1 of address in dual addressing mode
Bit 0 ENDUAL: Dual addressing mode enable
0: Only OAR1 is recognized in 7-bit addressing mode
1: Both OAR1 and OAR2 are recognized in 7-bit addressing mode
RM0008 Inter-integrated circuit (I
2
C) interface
Doc ID 13902 Rev 13 747/1093
26.6.5 I
2
C Data register (I2C_DR)
Address offset: 0x10
Reset value: 0x0000
26.6.6 I
2
C Status register 1 (I2C_SR1)
Address offset: 0x14
Reset value: 0x0000
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved
DR[7:0]
rw rw rw rw rw rw rw rw
Bits 15:8 Reserved, forced by hardware to 0.
Bits 7:0 DR[7:0] 8-bit data register
Byte received or to be transmitted to the bus.
Transmitter mode: Byte transmission starts automatically when a byte is written in the DR
register. A continuous transmit stream can be maintained if the next data to be transmitted
is put in DR once the transmission is started (TxE=1)
Receiver mode: Received byte is copied into DR (RxNE=1). A continuous transmit stream
can be maintained if DR is read before the next data byte is received (RxNE=1).
Note: In slave mode, the address is not copied into DR.
Note: Write collision is not managed (DR can be written if TxE=0).
Note: If an ARLO event occurs on ACK pulse, the received byte is not copied into DR and so
cannot be read.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
SMB
ALERT
TIME
OUT
Res.
PEC
ERR
OVR AF ARLO BERR TxE RxNE
Res.
STOPF ADD10 BTF ADDR SB
rc_w0 rc_w0 rc_w0 rc_w0 rc_w0 rc_w0 rc_w0 r r r r r r r
Bit 15 SMBALERT: SMBus alert
In SMBus host mode:
0: no SMBALERT
1: SMBALERT event occurred on pin
In SMBus slave mode:
0: no SMBALERT response address header
1: SMBALERT response address header to SMBALERT LOW received
Cleared by software writing 0, or by hardware when PE=0.
Bit 14 TIMEOUT: Timeout or Tlow error
0: No timeout error
1: SCL remained LOW for 25 ms (Timeout)
or
Master cumulative clock low extend time more than 10 ms (Tlow:mext)
or
Slave cumulative clock low extend time more than 25 ms (Tlow:sext)
When set in slave mode: slave resets the communication and lines are released by hardware
When set in master mode: Stop condition sent by hardware
Cleared by software writing 0, or by hardware when PE=0.
Note: This functionality is available only in SMBus mode.
Inter-integrated circuit (I
2
C) interface RM0008
748/1093 Doc ID 13902 Rev 13
Bit 13 Reserved, forced by hardware to 0.
Bit 12 PECERR: PEC Error in reception
0: no PEC error: receiver returns ACK after PEC reception (if ACK=1)
1: PEC error: receiver returns NACK after PEC reception (whatever ACK)
Cleared by software writing 0, or by hardware when PE=0.