Rm0377 Ultralowpower Stm32l0x1 Advanced Armbased 32bit Mcus Stmicroelectronics
Rm0377 Ultralowpower Stm32l0x1 Advanced Armbased 32bit Mcus Stmicroelectronics
Reference manual
Ultra-low-power STM32L0x1 advanced Arm®-based
32-bit MCUs
Introduction
This reference manual targets application developers. It provides complete information on
how to use the STM32L0x1 microcontroller memory and peripherals.
The STM32L0x1 is a line of microcontrollers with different memory sizes, packages and
peripherals.
For ordering information, mechanical and electrical device characteristics please refer to the
corresponding datasheets.
For information on the Arm® Cortex®-M0+ core, refer to the Cortex®-M0+ Technical
Reference Manual.
The STM32L0x1 microcontrollers include state-of-the-art patented technology.
Related documents
• Cortex®-M0+ Technical Reference Manual, available from www.arm.com.
• STM32L0 Series Cortex®-M0+ programming manual (PM0223).
• STM32L0x1 datasheets.
• STM32L0x1 erratasheet.
Contents
1 Documentation conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
1.1 General information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
1.2 List of abbreviations for registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
1.3 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
1.4 Availability of peripherals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
1.5 Product category definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
List of tables
List of figures
Figure 149. Output stage of capture/compare channel (channel 1 and 2). . . . . . . . . . . . . . . . . . . . . . 459
Figure 150. PWM input mode timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
Figure 151. Output compare mode, toggle on OC1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
Figure 152. Edge-aligned PWM waveforms (ARR=8) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
Figure 153. Center-aligned PWM waveforms (ARR=8) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
Figure 154. Clearing TIMx OCxREF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
Figure 155. Example of one pulse mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
Figure 156. Example of counter operation in encoder interface mode . . . . . . . . . . . . . . . . . . . . . . . . 470
Figure 157. Example of encoder interface mode with TI1FP1 polarity inverted . . . . . . . . . . . . . . . . . 471
Figure 158. Control circuit in reset mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
Figure 159. Control circuit in gated mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
Figure 160. Control circuit in trigger mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Figure 161. Basic timer block diagram. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
Figure 162. Counter timing diagram with prescaler division change from 1 to 2 . . . . . . . . . . . . . . . . . 496
Figure 163. Counter timing diagram with prescaler division change from 1 to 4 . . . . . . . . . . . . . . . . . 496
Figure 164. Counter timing diagram, internal clock divided by 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
Figure 165. Counter timing diagram, internal clock divided by 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
Figure 166. Counter timing diagram, internal clock divided by 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
Figure 167. Counter timing diagram, internal clock divided by N. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
Figure 168. Counter timing diagram, update event when ARPE = 0 (TIMx_ARR not
preloaded). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
Figure 169. Counter timing diagram, update event when ARPE=1 (TIMx_ARR
preloaded). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500
Figure 170. Control circuit in normal mode, internal clock divided by 1 . . . . . . . . . . . . . . . . . . . . . . . . 501
Figure 171. Low-power timer block diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
Figure 172. Glitch filter timing diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510
Figure 173. LPTIM output waveform, single counting mode configuration . . . . . . . . . . . . . . . . . . . . . 512
Figure 174. LPTIM output waveform, Single counting mode configuration
and Set-once mode activated (WAVE bit is set) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
Figure 175. LPTIM output waveform, Continuous counting mode configuration . . . . . . . . . . . . . . . . . 513
Figure 176. Waveform generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514
Figure 177. Encoder mode counting sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
Figure 178. Independent watchdog block diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528
Figure 179. Watchdog block diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
Figure 180. Window watchdog timing diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539
Figure 181. RTC block diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
Figure 182. I2C1/3 block diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
Figure 183. I2C2 block diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591
Figure 184. I2C bus protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
Figure 185. Setup and hold timings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595
Figure 186. I2C initialization flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598
Figure 187. Data reception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
Figure 188. Data transmission . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 600
Figure 189. Slave initialization flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603
Figure 190. Transfer sequence flow for I2C slave transmitter, NOSTRETCH = 0. . . . . . . . . . . . . . . . 605
Figure 191. Transfer sequence flow for I2C slave transmitter, NOSTRETCH = 1. . . . . . . . . . . . . . . . 606
Figure 192. Transfer bus diagrams for I2C slave transmitter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
Figure 193. Transfer sequence flow for slave receiver with NOSTRETCH = 0 . . . . . . . . . . . . . . . . . 608
Figure 194. Transfer sequence flow for slave receiver with NOSTRETCH = 1 . . . . . . . . . . . . . . . . . 609
Figure 195. Transfer bus diagrams for I2C slave receiver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609
Figure 196. Master clock generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611
Figure 197. Master initialization flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613
1 Documentation conventions
a. Arm is a registered trademark of Arm Limited (or its subsidiaries) in the US and/or elsewhere.
b. This is an exhaustive list of all abbreviations applicable to STMicroelectronics microcontrollers, some of
them may not be used in the current document.
1.3 Glossary
This section gives a brief definition of acronyms and abbreviations used in this document:
• Sector: 32 pages write protection granularity in the Code area
• Page: 32 words for Code and System Memory areas, 1 word for Data, Factory Option
and User Option areas
• Word: data of 32-bit length.
• Half-word: data of 16-bit length.
• Byte: data of 8-bit length.
• IAP (in-application programming): IAP is the ability to re-program the Flash memory
of a microcontroller while the user program is running.
• ICP (in-circuit programming): ICP is the ability to program the Flash memory of a
microcontroller using the JTAG protocol, the SWD protocol or the bootloader while the
device is mounted on the user application board.
• Option bytes: product configuration bits stored in the Flash memory.
• OBL: option byte loader.
• AHB: advanced high-performance bus.
• NVM: non-volatile memory.
• ECC: error code correction.
• DMA: direct memory access.
• MIF: NVM interface.
• PCROP: proprietary code readout protection.
STM32L011x
8 Kbytes - - -
STM32L021x (AES)
STM32L011x STM32L031x
16 Kbytes - -
STM32L021x (AES) STM32L041x (AES)
STM32L031x
32 Kbytes - STM32L051x -
STM32L041x (AES)
64 Kbytes - - STM32L051x STM32L071x
STM32L071x
128 Kbytes - - -
STM32L081x (AES)
STM32L071x
192 Kbytes - - -
STM32L081x (AES)
MIF
Memory interface NVM memory
GPIO ports Cortex
IOPORT System bus
A,B,C,D,E,H M0+
SRAM
Busmatrix
DMA
Controller DMA
(Channels
1 to 7)
SYSCFG
FIREWALL
PWR
EXTI
AHB2APB ADC
APB buses
AHB bus
Bridges COMP1/2
TIM2/3/6/7/21/22
Reset and LPTIM1
clock IWDG
controller WWDG
(RCC) RTC
DBGMCU
CRC I2C1/2/3
USART1/2/3/4/LPUART1
SPI1/2
DMA request
MS34749V2
1. Refer to Table 1: STM32L0x1 memory density, to Table 2: Overview of features per category and to the device datasheets
for the GPIO ports and peripherals available on your device.
2.1.3 BusMatrix
The BusMatrix manages the access arbitration between masters. The arbitration uses a
Round Robin algorithm. The BusMatrix is composed of two masters (CPU, DMA) and three
slaves (NVM interface, SRAM, AHB2APB1/2 bridges).
AHB/APB bridges
The AHB/APB bridge provide full synchronous connections between the AHB and the 2
APB buses. APB1 and APB2 operate at a maximum frequency of 32 MHz.
Refer to Section 2.2.2: Memory map and register boundary addresses on page 52 for the
address mapping of the peripherals connected to this bridge.
After each device reset, all peripheral clocks are disabled (except for the SRAM and MIF).
Before using a peripheral you have to enable its clock in the RCC_AHBENR,
RCC_APB2ENR, RCC_APB1ENR or RCC_IOPENR register.
Note: When a 16- or 8-bit access is performed on an APB register, the access is transformed into
a 32-bit access: the bridge duplicates the 16- or 8-bit data to feed the 32-bit vector.
2.2.1 Introduction
Program memory, data memory, registers and I/O ports are organized within the same linear
4-Gbyte address space.
The bytes are coded in memory in Little Endian format. The lowest numbered byte in a word
is considered the word’s least significant byte and the highest numbered byte the most
significant.
The addressable memory space is divided into eight main blocks, of 512 Mbytes each.
0xFFFF FFFF
reserved
6
0xC000 0000
0x4002 63FF
5 AHB
0x4002 0000
0x4001 8000
4 0x1FFF FFFF
Option bytes APB2
0x8000 0000 0x4001 0000
System
memory reserved
3 0x4000 8000
APB1
0x6000 0000
0x4000 0000
2 reserved
1 Flash system
memory
0x2000 0000 SRAM 0x0800 0000
reserved
CODE
0 Flash, system
memory or
0x0000 0000 SRAM,
depending on
BOOT
configuration
0x0000 0000
Reserved
MS34761V2
All the memory map areas that are not allocated to on-chip memories and peripherals are
considered “Reserved”. For the detailed mapping of available memory and register areas,
refer to the following table.
The following table gives the boundary addresses of the peripherals available in the
devices.
Flash program
0X0800 0000 - 0X0802 FFFF up to 192 K -
memory
System
NVM 0x1FF0 0000 - 0x1FF0 1FFF 8K -
memory
Factory option
0x1FF8 0020 - 0x1FF8 007F 96 -
bytes
User option
0x1FF8 0000 - 0x1FF8 001F 32 -
bytes
1. Refer to Table 1: STM32L0x1 memory density, to Table 2: Overview of features per category and to the device datasheets
for the GPIO ports and peripherals available on your device. The memory area corresponding to unavailable GPIO ports or
peripherals are reserved.
The boot mode configuration is latched on the 2nd rising edge of SYSCLK after reset. For
category 1 devices, the value present on BOOT0 pin is latched on NRST rising edge. It is up
to the user to set nBOOT1 and BOOT0 to select the required boot mode.
The boot mode configuration is also re-sampled when exiting from Standby mode, except
for category 1 devices where BOOT0 pin is latched on NRST rising edge. Consequently the
boot mode configuration must not be modified in Standby mode (except for category 1
devices). After this startup delay has elapsed, the CPU fetches the top-of-stack value from
address 0x0000 0000, then starts code execution from the boot memory at 0x0000 0004.
Depending on the selected boot mode, Flash program memory, system memory or SRAM is
accessible as follows:
• Boot from Flash program memory: the Flash program memory is aliased in the boot
memory space (0x0000 0000), but still accessible from its original memory space
(0x0800 0000). In other words, the Flash memory contents can be accessed starting
from address 0x0000 0000 or 0x0800 0000.
• Boot from system memory: the system memory is aliased in the boot memory space
(0x0000 0000), but still accessible from its original memory space (0x1FF0 0000).
• Boot from the embedded SRAM: the SRAM is aliased in the boot memory space
(0x0000 0000), but it is still accessible from its original memory space (0x2000 0000).
Physical remap
Once the boot pin and bit are selected, the application software can modify the memory
accessible in the code area. This modification is performed by programming the
MEM_MODE bits in the SYSCFG memory remap register (SYSCFG_CFGR1).
Embedded bootloader
The embedded bootloader is located in the System memory, programmed by ST during
production. It is used to reprogram the Flash memory using one of the following serial
interfaces:
• For category 1 devices: USART2 or SPI1.
• For category 2 devices: USART2 or SPI1.
• For category 3 devices: USART1, USART2, SPI1 or SPI2
• For category 5 devices: USART1, USART2, SPI1, SPI2, I2C1 or I2C2.
For details concerning the bootloader serial interface corresponding I/O, refer to your device
datasheet.
For further details on STM32 bootloader, please refer to AN2606.
3.1 Introduction
The non-volatile memory (NVM) is composed of:
• Up to 192 Kbytes of Flash program memory. This area is used to store the application
code.
• Up to 6 Kbytes of data EEPROM
• An information block:
– Up to 8 Kbytes of System memory
– Up to 8x4 bytes of user Option bytes
– Up to 96 bytes of factory Option bytes
Flash program . . . .
. . . .
memory . . . .
Flash program . . . .
. . . .
memory . . . .
Flash program 0x0800 7080 - 0x0800 70FF 128 bytes Page 225
sector 7
memory(1) - - -
0x0800 7F80 - 0x0800 7FFF 128 bytes Page 255
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
- - -
0x0801 7F80- 0x0801 7FFF 128 bytes Page 767 sector 23
. . . .
. . . .
. . . .
Table 10. NVM organization for UFB = 0 (128 Kbyte category 5 devices)
NVM NVM addresses Size (bytes) Name Description
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
sector 31
- - -
0x0801 FF80 - 0x0801 FFFF 128 bytes Page 1023
Table 10. NVM organization for UFB = 0 (128 Kbyte category 5 devices) (continued)
NVM NVM addresses Size (bytes) Name Description
Table 11. Flash memory and data EEPROM remapping (128 Kbyte category 5 devices)
NVM addresses Remapped addresses
Table 12. NVM organization for UFB = 0 (64 Kbyte category 5 devices)(1)
NVM NVM addresses Size (bytes) Name Description
. . . .
Flash program
. . . . Bank 1
memory . . . .
- - -
sector 15
- - -
Data EEPROM 0x0808 0C00 - 0x0808 17FF 3 Kbytes - Data EEPROM Bank 2
1. Flash memory and data EEPROM remapping is not possible on 64 Kbyte category 5 devices.
When entering System memory, you can either execute the bootloader (for Flash update) or
execute Dual Bank Jump (see Table 13).
When protection level2 is enabled, the bootloader is never executed to perform a Flash
update.
When the conditions a, b, and c described below are fulfilled, it is equivalent to configuring
boot pins for System memory boot (BOOT0 = 1 and BOOT1 = 0). In this case when
protection level2 is disabled, normal bootloader operations are executed.
a) BFB2 bit is set.
b) Both banks do not contain valid code.
c) Boot pins configured as follows: BOOT0 = 0 and BOOT1 = x.
When the BFB2 bit is set, and Bank 2 and/or Bank 1 contain valid user application code, the
Dual Bank Boot is always performed (bootloader always jumps to the user code).
Consequently, if you have set the BFB2 bit (to boot from Bank 2) then, to be able to execute
the bootloader code for Flash update when protection level2 is disabled, you have to:
a) Set the BFB2 bit to 0, BOOT0 = 1 and BOOT1 = 0 or,
b) Program the content of address 0x0801 8000/0x0801 0000 (base address of
Bank2) and 0x0800 0000 (base address of Bank1) to 0x0.
You can read the NVM by word (4 bytes), half-word (2 bytes) or byte.
When the NVM features only one bank, it is not possible to read the NVM during a
write/erase operation. If a write/erase operation is ongoing, the reading will be in a wait state
until the write/erase operation completes, stalling the master that requested the read
operation, except when the address is read-protected. In this case, the error is sent to the
master by a hard fault or a memory interface flag; no stall is generated and no read is
waiting.
When two banks are available (category 5 devices), read operations from one bank can be
performed while write or erase operations are performed on the other bank.
Table 14. Link between master clock power range and frequencies
Maximum frequency Maximum frequency
Name Power range
(with 1 wait state) (without wait states)
Table 15 shows the delays to read a word in the NVM. Comparing the complete time to read
a word (Ttotal) with the clock period, you can see that in Range 3 no wait state is necessary,
also with the maximum frequency (4.2 MHz) allowed by the device. Ttotal is the time that the
NVM needs to return a value, and not the complete time to read it (from memory to Core
through the memory interface); all remaining time is lost.
32 MHz 31.25 1
Range 1 46.1 ns
16 MHz 62.5 0
16 MHz 62.5 1
Range 2 86.8 ns
8 MHz 125 0
4 MHz 250 0
Range 3 184.6 ns
2 MHz 500 0
Data buffering
In the NVM, six buffers can impact the performance (and in some conditions help to reduce
the power consumption) during read operations, both for fetch and data. The structure of
one buffer is shown on Figure 3.
Address
Value
History
MS32395V1
Each buffer stores 3 different types of information: address, data and history. In a read
operation, if the address is found, the memory interface can return data without accessing
the NVM. Data in the buffer is 32 bit wide (even if the master only reads 8 or 16 bits), so that
a value can be returned whatever the size used in a previous reading. The history is used to
know if the content of a buffer is valid and to delete (with a new value) the older one.
The buffers are used to store the value received by the NVM during normal read operations,
and for speculative readings. Disabling the speculative reading makes that only the data
requested by masters is stored in buffers, if enabled (default). This can increase the
performance as no wait state is necessary if the value is already available in buffers, and
reduce the power consumption as the number of reads in memory is reduced and all
combinatorial paths from memory are stable.
The buffers are divided in groups to manage different tasks. The number of buffers in every
group can change starting from the configuration selected by the user (see Table 16). The
total number of buffers used is always 6 (if enabled). The history is always managed by
group.
The memory interface always searches if a particular address is available in all buffers
without checking the group of buffers and if the read is fetch or data.
At reset or after a write/erase operation that changes several addresses, all buffers are
empty and the history is set to EMPTY. After a program by word, half-word or byte, only the
buffer with the concerned address is cleaned.
1 - - 0 0 0 0 0
0 0 0 3 0 1 0 2
0 1 0 2 1 1 0 2
0 0 1 3 0 1 1 1
0 1 1 2 1 1 1 1
If a value in a buffer is not empty, the history shows the time elapsed between the moment it
has been read or written. The history is organized as a list of values from the latest to the
oldest one. At a given instant, only one buffer in a group can have a particular value of
history (except the empty value). Moving a buffer to the latest position, all other buffers in
the group move one step further, thus maintaining the order. The history is changed to the
latest position when the buffer is read (the master requests for the buffer content) or written
(with a new value from the NVM). The memory interface always writes the oldest buffer (or
one empty buffer, if any) of the right group when a new address is required in memory.
Three configuration bits of the FLASH_ACR register are used to manage the buffering:
• DISAB_BUF
Setting this bit disables all buffers. When this bit is 1, the prefetch or the pre-read
operations cannot be enabled and if, for example, the master requests the same
address twice, two readings are generated in the NVM.
• PRFTEN
Setting this bit to 1 (with DISAB_BUF to 0) enables the prefetch. When the memory
interface does not have any operation in progress, the address following the last
address fetched is read and stored in a buffer.
• PRE_READ
Setting this bit to 1 (with DISAB_BUF to 0) enables the pre-read. When the memory
interface does not have any operation in progress or prefetch to execute, the address
following the last data address is read and stored in a buffer.
Fetch and prefetch
A memory interface fetch is a read from the NVM to execute the operation that has been
read. The memory interface does not check the master who performs the read operation, or
the location it reads from, but it only verifies if the read operation is done to execute what
has been read. It means that a fetch can be performed:
• in all areas,
• with any size (16 or 32 bits).
The memory interface stores in the buffers:
• The address of jumps so that, in a loop, it is only necessary to access the NVM the first
time, because then the jump address is already available.
• The last read address so that, when performing a fetching on 16 bits, the other 16 bits
are already available.
To manage the fetch, the memory interface uses 4 buffers: at reset (DISAB_BUF = 0,
PRFTEN = 0, PRE_READ = 0). 3 buffers are used to manage the jumps and 1 buffer to
store the last value fetched. With this configuration, the 4 buffers for fetch are organized in 2
groups with separate histories: the group for loops and the group for the last value fetched.
Setting the PRFTEN bit to 1 enables the prefetch. The prefetch is a speculative read in the
NVM, which is executed when no read is requested by masters, and where the memory
interface reads from the last address fetched increased by 4 (one word). This read is with a
lower priority and it is aborted if a master requests a read (data or fetch) to a different
address than the prefetch one. When the prefetch is enabled, one buffer for loops is moved
to a new group (of only one buffer) to store the prefetched value: 2 buffers continue to store
the jumps, 1 buffer is used for prefetch and 1 buffer is used for the last value.
The memory interface can only prefetch one address, so the function is temporarily disabled
when no fetch is done and the prefetch is already completed. After a prefetch, if the master
requests the prefetched value, the content of the prefetch buffer is copied to the last value
buffer and a new prefetch is enabled. If, instead, the master requests a different address,
the content of the prefetch buffer is lost, a read in the NVM is started (if necessary) and,
when it is complete, a new prefetch is enabled at the new address fetched increased by 4.
The prefetch can only increase the performance when reading with 1 wait state and for
mostly linear codes: the user must evaluate the pros and cons to enable or not the prefetch
in every situation. The prefetch increases the consumption because many more readings
are done in the NVM (and not all of them will be used by the master). To see the advantages
of prefetch on Dhrystone code, refer to the Dhrystone performances section.
Figure 4 shows the timing to fetch a linear code in the NVM when the prefetch is disabled,
both for 0 wait state (a) and 1 wait state (b). You can compare these two sequences with the
ones in Figure 5, when the prefetch is enabled, to have an idea of the advantages of a
prefetch on a linear code with 0 and 1 wait states.
MS32396V1
Figure 5 shows the timing to fetch and execute instructions from the NVM with 0 wait states
(a) and 1 wait state (b) when the prefetch is enabled. The read executed by the prefetch
appears in green.
Read as data and pre-read
A data read from the memory interface, corresponds to any read operation that is not a
fetch. The master reads operation constants and parameters as data. All reads done by
DMA (to copy from one address to another) are read as data. No check is done on the
location of the data read (can be in every area of the NVM).
At reset, (DISAB_BUF = 0, PRFTEN = 0, PRE_READ = 0), the memory interface uses 2
buffers organized in one group to store the last two values read as data.
In some particular cases (for example when the DMA is reading a lot of consecutive words
in the NVM), it can be useful to enable the pre-read (PRE_READ = 1 with DISAB_BUF = 0).
The pre-read works exactly like the prefetch: it is a speculative reading at the last data
address increased by 4 (one word). With this configuration, one buffer of data is moved to a
new group to store the pre-read value, while the second buffer continues to store the last
value read. For a prefetch, the pre-read value is copied in the last read value if the master
requests it, or is lost if the master requests a different address.
The pre-read has a lower priority than a normal read or a prefetch operation: this means that
it will be launched only when no other type of read is ongoing. Pay attention to the fact that
a pre-read used in a wrong situation can be harmful: in a code where a data read is not
done linearly, reducing the number of buffers (from 2 to 1) used for the last read value can
increase the number of accesses to the NVM (and the time to read the value). Moreover,
this can generate a delay on prefetch. An example of this situation is the code Dhrystone,
whose results are shown in the corresponding section.
As for a prefetch operation, the user must select the right moment to enable and disable the
pre-read.
MS32397V1
1 X X Buffers disabled
0 0 0 Buffer enabled: no speculative reading is done
Prefetch enabled: speculative reading on fetch
0 1 0
enabled
pre-read enabled: speculative reading on data
0 0 1
enabled
Prefetch and pre-read enabled: speculative reading
0 1 1
on fetch and data enabled
Dhrystone performances
The Dhrystone test is used to evaluate the memory interface performances. The test has
been executed in all memory interface configurations. Refer to Table 18 for a summary of
the results.
Common parameters are:
• the matrix size is 20 x 20
• the loop is executed 1757 times
• the version of Arm® compiler is 4.1 [Build 561]
Here is some explanation about the results:
0 1 0 0 953 15.25
0 0 0 0 953 15.25
0 0 1 0 953 15.25
0 0 0 1 953 15.25
0 0 1 1 953 15.25
1 1 0 0 677 21.66
1 0 0 0 690 22.08
1 0 1 0 823 26.34
1 0 0 1 691 22.11
1 0 1 1 816 26.11
• The pre-read is not useful for this test: when enabled with the prefetch, it reduces the
memory interface performance because only one buffer is used to store the last data
read and, in this code, the master rarely reads the data linearly. This justifies the very
small increase of performance when enabled without a prefetch.
• The buffers (without speculative readings) with 1 wait state give a little advantage that
can be considered without any costs.
• At a 0 wait state, the best performance (as certified by Arm®) may be due to a different
code alignment during the compilation.
Write/erase protocol
To write/erase memory content when the protections have been removed, the user needs
to:
1. configure the operation to execute,
2. send to the memory interface the right number of data, writing one or several
addresses in the NVM,
3. wait for the operation to complete.
During the waiting time, the user can prepare the next operation (except in very particular
cases) writing the new configuration and starting to write data for the next write/erase
operation.
The waiting time depends on the type of operation. A write/erase can last from Tprog (3.2
ms) to 2 x Tglob (3.7 ms) + Tprog (3.2 ms). The memory interface can be configured to write
a half-page (16 words in the Flash program memory) with only one waiting time. This can
reduce the time to program a big amount of data.
Two different protocols can be used: single programming and multiple programming
operation.
Single programming operation
With this protocol, the software has to write a value in a not-protected address of the NVM.
When the memory interface receives this writing request, it stalls the master for some
pulses of clock (for more details, see Table 19) while it checks the protections and the
previous value and it latches the new value inside the NVM. The software can then start to
configure the next operation. The operation will complete when the EOP bit of FLASH_SR
register rises (if it was 0 at the operation start). The operation time is resumed in Table 21
for all operations.
Multiple programming operation (half page)
You can write a half-page (16 words) in Flash program memory, To execute this protocol,
follow the next conditions:
• PGAERR bit in the FLASH_SR register has to be zero (no previous alignment errors).
• The first address has to be half-page aligned (the 6 lower bits of the address have to be
at zero).
• All 16 words must be in the same half-page (address bits 7 to 31 must be the same for
all 16 words). This means that the first address sets the half-page and the next ones
must be inside this half-page. The written data will be stored sequentially in the next
addresses. It is not important that the addresses increase or change (for example, the
same address can be used 16 times), as the memory interface will automatically
increase the address internally.
• Only words (32 bits) can be written.
When the memory interface receives the first address, it stalls the master for some pulses of
clock while it checks the protections and the previous value and it latches the new value
inside the NVM (for more details, see Table 19). Then, the memory interface waits for the
second address. No read is accepted: only a fetch will be executed, but it aborts the ongoing
write operation. After the second address, the memory interface stalls the core for a short
time (less than the previous one) to perform a check and to latch it in the NVM before
waiting for the next one. This sequence continues until all 16 words have been latched
inside the NVM. A wrong alignment or size will abort the write operation. If the 16 addresses
are correctly latched, the memory interface starts the write operation. The operation will
complete when EOP bit of FLASH_SR register rises (if it was 0 at the operation start). The
operation time is resumed in Table 21.
This protocol can be used either through application code running from RAM or through
DMA with application code running from RAM or core sleeping.
Unlocking/locking operations
Before performing a write/erase operation, it is necessary to enable it. The user can write
into the Flash program memory, data EEPROM and Option bytes areas.
To perform a write/erase operation, unlock PELOCK bit of the FLASH_PECR register. When
this bit is unlocked (its value is 0), the other bits of the same register can be modified. When
PELOCK is 0, the write/erase operations can be executed in the data EEPROM.
To write/erase the Flash program memory, unlock PRGLOCK bit of the FLASH_PECR
register. The bit can only be unlocked when PELOCK is 0.
To write/erase the user Option bytes, unlock OPTLOCK bit of the FLASH_PECR register.
The bit can only be unlocked when PELOCK is 0. No relation exists between PRGLOCK
and OPTLOCK: the first one can be unlocked when the second one is locked and vice
versa.
Unlocking the data EEPROM and the FLASH_PECR register
After a reset, the data EEPROM and the FLASH_PECR register are not accessible in write
mode because PELOCK bit in the FLASH_PECR register is set. The same unlocking
sequence unprotects both of them at the same time.
The following sequence is used to unlock the data EEPROM and the FLASH_PECR
register:
• Write PEKEY1 = 0x89ABCDEF to the FLASH_PEKEYR register
• Write PEKEY2 = 0x02030405 to the FLASH_PEKEYR register
For code example, refer to A.3.1: Unlocking the data EEPROM and FLASH_PECR register
code example.
Any wrong key sequence will lock up FLASH_PECR until the next reset and generate a
hard fault. Idem if the master tries to write another register between the two key sequences
or if it uses the wrong key. A reading access does not generate an error and does not
interrupt the sequence. A hard fault is returned in any of the four cases below:
• After the first write access if the PEKEY1 value entered is erroneous.
• During the second write access if PEKEY1 is correctly entered but the value of
PEKEY2 does not match.
• If there is any attempt to write a third value to PEKEYR (pay attention: this is also true
for the debugger).
• If there is any attempt to write a different register of the memory interface between
PEKEY1 and PEKEY2.
When properly executed, the unlocking sequence clears PELOCK bit in the FLASH_PECR
register.
To lock FLASH_PECR and the data EEPROM again, the software only needs to set
PELOCK bit in FLASH_PECR. When locked again, PELOCK bit needs a new sequence to
return to 0.
For code example, refer to A.3.2: Locking data EEPROM and FLASH_PECR register code
example.
Unlocking the Flash program memory
An additional protection is implemented to write/erase the Flash program memory.
After a reset, the Flash program memory is no more accessible in write mode: PRGLOCK
bit is set in the FLASH_PECR register. A write access to the Flash program memory is
granted by clearing PRGLOCK bit.
The following sequence is used to unlock the Flash program memory:
• Unlock the FLASH_PECR register (see the Unlocking the data EEPROM and the
FLASH_PECR register section).
• Write PRGKEY1 = 0x8C9DAEBF to the FLASH_PRGKEYR register.
• Write PRGKEY2 = 0x13141516 to the FLASH_PRGKEYR register.
For code example, refer to A.3.3: Unlocking the NVM program memory code example.
If the keys are written with PELOCK set to 1, no error is generated and PRGLOCK remains
at 1. It will be unlocked while re-executing the sequence with PELOCK = 0.
Any wrong key sequence will lock up PRGLOCK in FLASH_PECR until the next reset, and
return a hard fault. A hard fault is returned in any of the four cases below:
• After the first write access if the entered PRGKEY1 value is erroneous.
• During the second write access if PRGKEY1 is correctly entered but the PRGKEY2
value does not match.
• If there is any attempt to write a third value to PRGKEYR (this is also true for the
debugger).
• If there is any attempt to write a different register of the memory interface between
PRGKEY1 and PRGKEY2.
When properly executed, the unlocking sequence clears the PRGLOCK bit and the Flash
program memory is write-accessible.
To lock the Flash program memory again, the software only needs to set PRGLOCK bit in
FLASH_PECR. When locked again, PRGLOCK bit needs a new sequence to return to 0. If
PELOCK returns to 1 (locked), PRGLOCK is automatically locked, too.
Unlocking the Option bytes area
An additional write protection is implemented on the Option bytes area. It is necessary to
unlock OPTLOCK to reload or write/erase the Option bytes area.
After a reset, the Option bytes area is not accessible in write mode: OPTLOCK bit in the
FLASH_PECR register is set. A write access to the Option bytes area is granted by clearing
OPTLOCK.
The following sequence is used to unlock the Option bytes area:
1. Unlock the FLASH_PECR register (see the Unlocking the data EEPROM and the
FLASH_PECR register section).
2. Write OPTKEY1 = 0xFBEAD9C8 to the FLASH_OPTKEYR register.
3. Write OPTKEY2 = 0x24252627 to the FLASH_OPTKEYR register.
For code example, refer to A.3.4: Unlocking the option bytes area code example.
If the keys are written with PELOCK = 1, no error is generated, OPTLOCK remains at 1 and
it will be unlocked when re-executing the sequence with PELOCK to 0.
Any wrong key sequence will lock up OPTLOCK in FLASH_PECR until the next reset, and
return a hard fault. A hard fault is returned in any of the four cases below:
• After the first write access if the OPTKEY1 value entered is erroneous.
• During the second write access if OPTKEY1 is correctly entered but the OPTKEY2
value does not match.
• If there is any attempt to write a third value to OPTKEYR (this is also true for the
debugger).
• If there is any attempt to write a different register of the memory interface between
OPTKEY1 and OPTKEY2.
When properly executed, the unlocking sequence clears the OPTLOCK bit and the Option
bytes area is write-accessible.
To lock the Option bytes area again, the software only needs to set OPTLOCK bit in
FLASH_PECR. When relocked, OPTLOCK bit needs a new sequence to return to 0. If
PELOCK returns to 1 (locked), OPTLOCK is automatically locked, too.
Select between different types of operations
When the necessary unlock sequence has been executed (PELOCK, PRGLOCK and
OPTLOCK), the user can enable different types of write and erase operations, writing the
right configuration in the FLASH_PECR register. The bits involved are:
• PRG
• DATA
• FIX
• ERASE
• FPRG
This operation aims at writing a word in the Option bytes area. The Option bytes area
can only be written in Level 0 or Level 1.
The user must consider that, in a word, the 16 higher bits (from 16 to 31) have to be the
complement of the 16 lower bits (from 0 to 15): a mismatch between the higher and
lower parts of data would generate an error during the Option bytes loading (see
Section 3.8: Option bytes) and force the memory interface to load the default values.
The memory interface does not check at the write time if the data is correctly
complemented. The user must write the desired value at the right address with a word
size.
As for data EEPROM, the memory interface deletes the previous content before
writing, if necessary. If the data to write is at 0, the memory interface does not execute
the useless write operation. When only a write operation or only an erase operation is
executed, the duration is Tprog (3.2 ms). If both are executed, the duration is 2 x Tprog
(6.4 ms). The memory interface can be forced to execute every time both erase and
write operations set the FIX flag to 1.
Some configurations need a closer attention because they change the protections. The
memory interface can change the Option bytes write in a Mass Erase or force some
bits not to reduce the protections: for more details, see Section 3.4.4: Write/erase
protection management.
• Duration
Tprog (3.2 ms) or 2 x Tprog (6.4 ms).
• Options
FIX bit can be set to force the memory interface to execute every time an erase (to
delete the old content) and a write operation (to write the new data) occur. This gives a
fix time to program for every data value and for previous data.
For code example, refer to A.3.7: Program Option byte code example.
– Other categories
NOTZEROERR is set to 1. Writing a word to an address containing a non-null
value is not performed.
When a half-page operation starts, the memory interface waits for 16 addresses/data,
aborting (with a hard fault) all read accesses that are not a fetch (refer to Fetch and
prefetch). A fetch stops the half-page operation. The memory content remains
unchanged, the FWWERR error is set in the FLASH_SR register. To complete the half-
page programming operation, all the desired values should be written again.
• Duration
Tprog (3.2 ms).
For code example, refer to A.3.10: Program half-page to Flash program memory code
example.
Mass erase
• Purpose
Remove the read and write protection on the Flash program memory and data
EEPROM.
• Size
Erase only by word.
• Address
To generate a mass erase, it is necessary to write 0x015500AA to the first Option bytes
address (bits 31 to 25 and 15 to 9 are not complemented because they are not used,
and not checked) with Level 1 as the actual level.
• Protocol
Single programming operation.
• Requests
PELOCK = 0, OPTLOCK = 0, Protection Level = 1, the lower nibble of data has to be
0xAA (Level 0), with 0x55 as the third nibble.
• Errors
WRPERR is set to 1 if PELOCK = 1 or OPTLOCK = 1.
WRPERR is set to 1 if the actual protection level is 2 (the Option bytes area cannot be
written in Level 2).
SIZERR is set to 1 if the size is not the word.
• Description
This operation is similar to the write user Option byte operation: the memory interface
changes it in a mass erase when the actual Protection Level is 1 and the requested
Protection Level is 0. The user must write the desired value in the first address of the
Option bytes area with a word size.
A mass erase deletes the content of the Flash program memory and data EEPROM,
changes the protection level to Level 0 and disables PcROP. (WPRMOD = 0). The bits
write protection and BOR_LEVEL remain unchanged.
Unlike all other operations, the software cannot request new writing operations while a
mass erase is ongoing. To be sure that a mass erase has completed, the software can
reset the EOP bit of FLASH_SR register before the write operation and check when
EOP goes to 1 (End Of Program). If this limitation is not respected, a wrong value may
be written in the Flash program memory and data EEPROM when the Protection Level
is written, thus adding unwanted protections (also for mismatch) that could make the
device useless.
• Duration
2 x Tprog (6.4 ms) + Tglob (3.7 ms)
For code example, refer to A.3.12: Mass erase code example.
Timing tables
Previous data = 0
Tprog (3.2 ms)
FIX = 0
Previous data /= 0
Write to data EEPROM New data = 0
Tprog (3.2 ms)
Size = word
FIX = 0
Other situations 2 x Tprog (6.4 ms)
Erase data EEPROM - Tprog (3.2 ms)
Previous data = 0
Tprog (3.2 ms)
FIX = 0
Previous data /= 0
Write Option bytes
New data = 0 Tprog (3.2 ms)
FIX = 0
Other situations 2 x Tprog (6.4 ms)
Erase Option bytes - Tprog (3.2 ms)
Program a single word in
- Tprog (3.2 ms)
Flash program memory
Program a half-page in
- Tprog (3.2 ms)
Flash program memory
Erase a page in Flash
- Tprog (3.2 ms)
program memory
Mass erase - 2 x Tprog (6.4 ms) + Tglob (3.7 ms)
Status register
The FLASH_SR Status Register gives some information on the memory interface or the
NVM status (operation(s) ongoing) and about errors that happened.
BSY
This flags is set and reset by hardware. It is set to 1 every time the memory interface
executes a write/erase operation, and it informs that no other operation can be executed. If
a new operation is requested, different behaviors can occur:
• Waiting for read, or waiting for write/erase, or waiting for option loading:
If the software requests a write operation while a write/erase operation is executing
(HVOFF = 0), the memory interface stalls the master and has the pending operation
execute as soon as the write/erase operation is complete.
• Hard fault:
If the software requests a data read in a half-page operation when the memory
interface is waiting for the next address/data (BSY is already 1 but HVOFF = 0), the
memory interface generates a hard fault (because it cannot execute the read) and
continues to wait for missing addresses.
• RDERR error:
If the software requests a read operation while a write/erase operation is executing
(HVOFF = 0) but the address is protected, the memory interface rises the flag and
continues to wait for the end of the write/erase operation.
• Write abort:
If the software fetches in the NVM when the memory interface is waiting for an
address/data in a half-page operation, the write/erase operation is aborted, the
FWWERR flag is raised and the fetch is executed.
EOP
This flag is set by hardware and reset by software. The software can reset it writing 1 in the
status register. This bit is set when the write/erase operation is completed and the memory
interface can work on other operations (or start to work on pending operations).
It is useful to clear it before starting a new write/erase operation, in order to know when the
actual operation is complete. It is very important to wait for this flag to rise when a mass
erase is ongoing, before requesting a new operation.
HVOFF
This flag is set and reset by hardware and it is a memory interface information copy coming
from the NVM: it informs when the High-Voltage Regulators are on (= 0) or off (= 1).
PGAERR
This flag is set by hardware and reset by software. It informs when an alignment error
happened. It is raised when:
• The first address in a half-page operation is not aligned to a half-page (lower 6 bits
equal to zero).
• A half-page change happened in a half-page operation (the addresses from 2 to 16 in a
half-page operation are not in the same half-page, selected by the first address).
An alignment error aborts the write/erase operation and an interrupt can be generated (if
ERRIE = 1 in the FLASH_PECR register). The content of the NVM is not changed.
If this flag is set, the memory interface blocks all other half-page operations.
Write Level 1
RDP = 0xCC RDP /= 0xAA
RDP /= 0xCC
Write
(default) RDP = 0xAA
Write
Mass erase
RDP /= 0xCC and
Write RDP /= 0xAA
RDP /= 0xAA and
RDP /= 0xCC
Level 2 Level 0
Write
RDP = 0xAA
RDP decrease
RDP increase
RDP unchanged
MS34776V1
Any read access performed as data (see Read as data and pre-read) in a protected sector
will trigger the RDERR flag in the FLASH_SR register. Any read-protected sector is also
write-protected and any write access to one of these sectors will trigger the WRPERR flag in
the FLASH_SR register.
The only way to remove a protection from a sector is to request a mass erase (which
changes the protection level to 0 and disables PcROP): when PcROP is disabled, the
protection on sectors can be changed freely.
Table 23. Memory access vs mode, protection and Flash program memory sectors
Mode
User
Flash program memory (including In Application User
sectors Programming) in Debug, or
no Debug, or with Boot in RAM, or
no Boot in RAM, or with Boot in System memory
no Boot in System memory
Level 1
RDP Level 2 Level 0 Level 1 Level 2
Level 0
Table 23. Memory access vs mode, protection and Flash program memory sectors (continued)
Mode
User
Flash program memory (including In Application User
sectors Programming) in Debug, or
no Debug, or with Boot in RAM, or
no Boot in RAM, or with Boot in System memory
no Boot in System memory
Level 1
RDP Level 2 Level 0 Level 1 Level 2
Level 0
Read
• If no operation is ongoing and the read address is not protected, the read is executed
without delays and with the actual configurations.
• If the read address is protected, the operation is filtered (the read requested is never
sent to the memory) and an error is raised.
• If the read address is not protected but the memory interface is busy and cannot
perform the operation, the read is put on hold to be executed as soon as possible.
Write/erase
• If no operation is ongoing and the write address is not protected, the write/erase will
start immediately; after some clock pulses (see Table 19) during which the bus and the
master are blocked, the memory interface continues the operation freeing the bus and
the master.
• If the address is protected, the write/erase is filtered (the write/erase requested is never
sent to the memory) and an error is raised.
• If the address is not protected but one or several conditions are not met, the operation
is aborted (the abort needs more time to be executed because the NVM and data
EEPROM need to return to default configuration) and an error is raised.
• If the address to write/erase is not protected and all rules are respected, and if the
memory interface is busy, the operation is put on hold to be executed as soon as
possible.
3.6.4 Power-down
To put the NVM in power-down, it is necessary to execute an unlocking sequence.
The following sequence is used to unlock RUN_PD bit of the FLASH_ACR register:
• Write PDKEY1 = 0x04152637 to the FLASH_PDKEYR register.
• Write PEKEY2 = 0xFAFBFCFD to the FLASH_PDKEYR register.
It is necessary to write the two keys without constraints about other read or write. No error is
generated if the wrong key is used: when both have been written, RUN_PD bit is unlocked
and can be written to 1, putting the NVM in power-down mode.
Resetting the RUN_PD flag to 0 (making the NVM available) automatically resets the
sequence and the two keys are requested to re-enable RUN_PD.
Write to registers
In the configuration registers of the memory interface, there are two types of bits:
• the bits that can be written to directly
• the bits needing a particular sequence to unlock.
To know which category a bit belongs to, see the next sections where every bit is explained
in details.
When it is possible to write directly to a register or a key-register, the user must write the
expected value at the register address. If the address is not correct, no error is generated. If
the user tries to modify a read-only register, no error is generated and the modify operation
does not take any effect. It is possible to write registers by byte, half-word and word.
When an unlock sequence is necessary, the correct values to use are given.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
DISAB_BUF
PRE_READ
SLEEP_PD
LATENCY
RUN_PD
PRFTEN
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
rw rw rw rw rw rw
OBL_LAUNCH
NZDISABLE
ERRIE
EOPIE
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
PARRALELBANK
PRG_LOCK
OPT_LOCK
PE_LOCK
ERASE
PROG
FPRG
DATA
FIX
rw rw rw rw rw rw rs rs rs
Bit 8 FIX
0: An erase phase is automatically performed, when necessary, before a program operation
in the data EEPROM and the Option bytes areas. The programming time can be:
Tprog (program operation) or 2 * Tprog (erase + program operations).
1: The program operation is always performed with a preliminary erase and the
programming time is: 2 * Tprog.
Note: This bit can be modified when PELOCK is 0. It is reset when PELOCK is set.
Bits 7:5 Reserved, must be kept at reset value
Bit 4 DATA
0: Data EEPROM not selected.
1: Data memory selected.
Note: This bit can be modified when PELOCK is 0. It is reset when PELOCK is set.This bit is
not very useful as the page and word have the same size in the data EEPROM, but it is
used to identify an erase operation (by page) from a word operation.
Bit 3 PROG
This bit is used for half-page program operations and for page erase operations in the Flash
program memory.
0: The Flash program memory is not selected.
1: The Flash program memory is selected.
Note: This bit can be modified when PELOCK is 0. It is reset when PELOCK is set.
Bits 31:0 This is a write-only register. With a sequence of two write operations (the first one with
0x04152637 and the second one with 0xFAFBFCFD), the write size being that of a word, it is
possible to unlock the RUN_PD bit of the FLASH_ACR register. For more details, refer to
Section 3.6.4: Power-down.
Bits 31:0 This is a write-only register. With a sequence of two write operations (the first one with
0x89ABCDEF and the second one with 0x02030405), the write size being that of a word, it is
possible to unlock the FLASH_PECR register. For more details, refer to Unlocking the data
EEPROM and the FLASH_PECR register.
Bits 31:0 This is a write-only register. With a sequence of two write operations (the first one with
0x8C9DAEBF and the second one with 0x13141516), the write size being that of a word, it is
possible to unlock the Flash program memory. The sequence can only be executed when
PELOCK is already unlocked. For more details, refer to Unlocking the Flash program memory.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
FLASH_OPTKEYR[31:16]
w w w w 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
FLASH_OPTKEYR[15:0]
w w w w w w w w w w w w w w w w
Bits 31:0 This is a write-only register. With a sequence of two write operations (the first one with
0xFBEAD9C8 and the second one with 0x24252627), the write size being that of a word, it is
possible to unlock the Option bytes area and the OBL_LAUNCH bit. The sequence can only be
executed when PELOCK is already unlocked. For more details, refer to Unlocking the Option
bytes area.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
NOTZERO
FWWER
ERR
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
rc_w1 rc_w1
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
OPTVERR
WRPERR
PGAERR
SIZERR
RDERR
ENDHV
READY
EOP
BSY
Res. Res. Res. Res. Res. Res. Res.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
BOR_LEV[3:0]
nRST_STDBY
nBOOT_SEL
nRTS_STOP
WDG_SW
nBOOT1
nBOOT0
BFB2
Res. Res. Res. Res. Res.
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
WPRMOD
RDPROT
Res. Res. Res. Res. Res. Res. Res.
r r r r r r r r r
Bit 31 nBOOT1
This bit is used in conjunction with BOOT0 signal to configure the device boot mode (see
Section 2.4: Boot configuration).
If there is a mismatch on this configuration during the Option bytes loading, it is loaded with 1.
If the device is protected at Level 2, BOOT0 and nBOOT1 lose their meaning: the boot is
always forced in the Flash program memory.
Bit 30 nBOOT0
This bit is available on category 1 devices only.
When nBOOT_SEL is set, nBOOT0 bit defines the value of BOOT0 signal that is used to
select the device boot mode (see Section 2.4: Boot configuration).
Bit 29 nBOOT_SEL
0: BOOT0 signal is defined by BOOT0 pin value (default mode)
1: BOOT0 signal is defined by nBOOT0 option bit
This bit is available on category 1 devices only. It is held at ‘0’ on other devices.
Bits 28:24 Reserved, must be kept at reset value
Bit 23 BFB2: Boot from Bank 2
This bit contains the user option byte loaded by the device OPTL. This bit is used to boot
from Bank 2. Actually this bit indicates whether a boot from System memory or from Flash
program memory has been selected. If boot from System memory is selected, the jump to
Bank 1 or Bank 2 is performed by software depending on the value of the first two words at
the beginning of each bank. When BFB2 is set, user Flash memory is not aliased at address
0. Instead, the System Flash memory is aliased at address 0 through MEM_MODE bits in
SYSCFG_CFGR1. When BFB2 is set, the PRIMASK is set at code startup. It prevents the
activation of all exceptions that have a configurable priority.
0: BOOT from Bank 1 (category 5 devices) or USER Flash memory (other categories)
1: BOOT from System memory
Note: This bit is available in category 5 devices only.
Bit 22 nRST_STDBY
If there is a mismatch on this configuration during the Option bytes loading, it is loaded with 1.
0: Reset generated when entering the Standby mode.
1: No reset generated.
Bit 21 nRST_STOP
If there is a mismatch on this configuration during the Option bytes loading, it is loaded with 1.
0: Reset generated when entering the Stop mode.
1: No reset generated.
Bit 20 WDG_SW
If there is a mismatch on this configuration during the Option bytes loading, it is loaded with 1.
0: Hardware watchdog.
1: Software watchdog.
Bits 19:16 BOR_LEV: Brownout reset threshold level
These bits reset the threshold level for a 1.45 V to 1.55 V voltage range (power-down only). In
this particular case, VDD must have been above VBOR0 to start the device OBL sequence, in
order to disable the BOR. The power-down is then monitored by the PDR. If the BOR is
disabled, a “grey zone” exists between 1.65 V and the VPDR threshold (this means VDD can
be below the minimum operating voltage (1.65 V) without any reset until the VPDR threshold).
If there is a mismatch on this configuration during the Option bytes loading, it is loaded with
0x8.
0xxx: BOR OFF. This is the reset threshold level for the 1.45 V - 1.55 V voltage range
(power-down only).
In this particular case, VDD must have been above BOR LEVEL 1 to start the device OBL
sequence in order to disable the BOR. The power-down is then monitored by the PDR.
Note: If the BOR is disabled, a "grey zone" exists between 1.65 V and the VPDR threshold
(this means that VDD may be below the minimum operating voltage (1.65 V) without
causing a reset until it crosses the VPDR threshold)
1000: BOR LEVEL 1 is the reset threshold level for VBOR0 (around 1.8 V)
1001: BOR LEVEL 2 is the reset threshold level for VBOR1 (around 2.0 V)
1010: BOR LEVEL 3 is the reset threshold level for VBOR2 (around 2.5 V)
1011: BOR LEVEL 4 is the reset threshold level for VBOR3 (around 2.7 V).
1100: BOR LEVEL 5 is the reset threshold level for VBOR4 (around 3.0 V)
Note: Refer to the device datasheets for the exact definition of BOR levels.
Bits 15:9 Reserved, must be kept at reset value
Bit 8 WPRMOD
This bit selects between write and read protection of Flash program memory sectors. If there is
a mismatch on this configuration during the Option bytes loading, it is loaded with 1.
0: PCROP disabled. The WRPROT bits are used as a write protection on a sector.
1: PCROP enabled. The WRPROT bits are used as a read protection on a sector.
Bits 7:0 RDPROT: Read protection
These bits contain the protection level loaded during the Option byte loading. If there is a
mismatch on this configuration during the Option bytes loading, it is loaded with 0x00.
0xAA: Level 0
0xCC: Level 2
Others: Level 1
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
WRPROT1[31:16]
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
WRPROT1[15:0]
r r r r r r r r r r r r r r r r
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
WRPROT2 [15:0]
r r r r r r r r r r r r r r r r
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
10
11
9
8
7
6
5
4
3
2
1
0
set
DESAB_8BUF
PRE_READ
SLEEP_PD
LATENCY
RUN_PD
PRFTEN
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
FLASH_ACR
0x00
0x00000000 0 0 0 0 0 0
PARALLELBANK
OBL_LAUNCH
NZDISABLE
PRGLOCK
OPTLOCK
PELOCK
ERASE
ERRIE
EOPIE
FPRG
DATA
PRG
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
FIX
FLASH_PECR
0x004
0x00000007 0 0 0 0 0 0 0 0 0 0 1 1 1
FLASH_
PDKEYR[31:0]
0x008 PDKEYR
0x00000000 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
FLASH_
PKEYR[31:0]
0x00C PKEYR
0x00000000 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
FLASH_
PRGKEYR[31:0]
PRGKEYR
0x010
0x00000000 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
FLASH_
OPTKEYR[31:0]
OPTKEYR
0x014
0x00000000 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
NOTZEROERR
OPTVERR
FWWERR
WRPERR
PGAERR
SIZERR
RDERR
ENDHV
READY
EOP
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
BSY
FLASH_SR
0x018
0x0000000C 0 0 0 0 0 0 0 1 1 0 0
nBOOT_SEL
nRST_STOP
nRST_STBY
WPRMOD
WDG_SW
nBOOT1
nBOOT0
BFB2
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
0xXXXX0XXX X X X X X X X X X X X X X X X X X X X X
FLASH_
WRPROT1[31:0]
WRPROT1
0x020
0x0000XXXX 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
FLASH_
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
WRPROT2[15:0]
WRPROT2
0x080
0xXXX 0000 X X X X X X X X X X X X X X X X
Complemented Complemented
Option byte 1 Option byte 0
Option byte 1 Option byte 0
Refer to Section 3.7.8: Option bytes register (FLASH_OPTR) and Section 3.7.9: Write
protection register 1 (FLASH_WRPROT1) for the meaning of each bit.
4.1 Introduction
The CRC (cyclic redundancy check) calculation unit is used to get a CRC code from 8-, 16-
or 32-bit data word and a generator polynomial.
Among other applications, CRC-based techniques are used to verify data transmission or
storage integrity. In the scope of the functional safety standards, they offer a means of
verifying the Flash memory integrity. The CRC calculation unit helps compute a signature of
the software during runtime, to be compared with a reference signature generated at link
time and stored at a given memory location.
CRC_CR
CRC computation
CRC_POL
CRC_IDR
MS19882V3
The data size can be dynamically adjusted to minimize the number of write accesses for a
given number of bytes. For instance, a CRC for 5 bytes can be computed with a word write
followed by a byte write.
The input data can be reversed, to manage the various endianness schemes. The reversing
operation can be performed on 8 bits, 16 bits and 32 bits depending on the REV_IN[1:0] bits
in the CRC_CR register.
For example: input data 0x1A2B3C4D is used for CRC calculation as:
• 0x58D43CB2 with bit-reversal done by byte
• 0xD458B23C with bit-reversal done by half-word
• 0xB23CD458 with bit-reversal done on the full word
The output data can also be reversed by setting the REV_OUT bit in the CRC_CR register.
The operation is done at bit level: for example, output data 0x11223344 is converted into
0x22CC4488.
The CRC calculator can be initialized to a programmable value using the RESET control bit
in the CRC_CR register (the default value is 0xFFFFFFFF).
The initial CRC value can be programmed with the CRC_INIT register. The CRC_DR
register is automatically initialized upon CRC_INIT register write access.
The CRC_IDR register can be used to hold a temporary value related to CRC calculation. It
is not affected by the RESET bit in the CRC_CR register.
Polynomial programmability
The polynomial coefficients are fully programmable through the CRC_POL register, and the
polynomial size can be configured to be 7, 8, 16 or 32 bits by programming the
POLYSIZE[1:0] bits in the CRC_CR register. Even polynomials are not supported.
Note: The type of an even polynomial is X+X2+..+Xn, while the type of an odd polynomial is
1+X+X2+..+Xn.
If the CRC data is less than 32-bit, its value can be read from the least significant bits of the
CRC_DR register.
To obtain a reliable CRC calculation, the change on-fly of the polynomial value or size can
not be performed during a CRC calculation. As a result, if a CRC calculation is ongoing, the
application must either reset it or perform a CRC_DR read before changing the polynomial.
The default polynomial value is the CRC-32 (Ethernet) polynomial: 0x4C11DB7.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
DR[31:16]
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
DR[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
REV_
Res. Res. Res. Res. Res. Res. Res. Res. REV_IN[1:0] POLYSIZE[1:0] Res. Res. RESET
OUT
rw rw rw rw rw rs
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
CRC_INIT[31:16]
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
CRC_INIT[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
POL[31:16]
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
POL[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Offset Register
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
10
11
9
8
7
6
5
4
3
2
1
0
name
CRC_DR DR[31:0]
0x00
Reset value 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
CRC_IDR IDR[7:0]
0x04
Reset value 0 0 0 0 0 0 0 0
POLYSIZE[1:0]
REV_IN[1:0]
REV_OUT
RESET
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
CRC_CR
0x08
Reset value 0 0 0 0 0 0
CRC_INIT CRC_INIT[31:0]
0x10
Reset value 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
CRC_POL POL[31:0]
0x14
Reset value 0 0 0 0 0 1 0 0 1 1 0 0 0 0 0 1 0 0 0 1 1 1 0 1 1 0 1 1 0 1 1 1
5 Firewall (FW)
5.1 Introduction
The Firewall is made to protect a specific part of code or data into the Non-Volatile Memory,
and/or to protect the Volatile data into the SRAM from the rest of the code executed outside
the protected area.
I
N
AHB Slave T
AHB Master 1 E Flash program
R memory and data
CORTEX M0+
F EEPROM
A
B C
U E
S
MS32388V3
Debug consideration
In debug mode, if the Firewall is opened, the accesses by the debugger to the protected
segments are not blocked. For this reason, the Read out level 2 protection must be active in
conjunction with the Firewall implementation.
If the debug is needed, it is possible to proceed in the following way:
• A dummy code having the same API as the protected code may be developed during
the development phase of the final user code. This dummy code may send back
coherent answers (in terms of function and potentially timing if needed), as the
protected code should do in production phase.
• In the development phase, the protected code can be given to the customer-end under
NDA agreement and its software can be developed in level 0 protection. The customer-
end code needs to embed an IAP located in a write protected segment in order to allow
future code updates when the production parts will be Level 2 ROP.
Write protection
In order to offer a maximum security level, the following points need to be respected:
• It is mandatory to keep a write protection on the part of the code enabling the Firewall.
This activation code should be located outside the segments protected by the Firewall.
• The write protection is also mandatory on the code segment protected by the Firewall.
• The page including the reset vector must be write-protected.
Interrupts management
The code protected by the Firewall must not be interruptible. It is up to the user code to
disable any interrupt source before executing the code protected by the Firewall. If this
constraint is not respected, if an interrupt comes while the protected code is executed
(Firewall opened), the Firewall will be closed as soon as the interrupt subroutine is
executed. When the code returns back to the protected code area, a Firewall alarm will raise
since the “call gate” sequence will not be applied and a reset will be generated.
Concerning the interrupt vectors and the first user page in the Flash program memory:
• If the first user page (including the reset vector) is protected by the Firewall, the NVIC
vector should be reprogrammed outside the protected segment.
• If the first user page is not protected by the Firewall, the interrupt vectors may be kept
at this location.
There is no interrupt generated by the Firewall.
Code segment
This segment is located into the Flash program memory. It should contain the code to
execute which requires the Firewall protection. The segment must be reached using the
“call gate” entry sequence to open the Firewall. A system reset is generated if the “call gate”
entry sequence is not respected (refer to Opening the Firewall) and if the Firewall is enabled
using the FWDIS bit in the system configuration register. The length of the segment and the
segment base address must be configured before enabling the Firewall (refer to
Section 5.3.5: Firewall initialization).
The Volatile data segment is a bit different from the two others. The segment can be:
• Shared (VDS bit in the register)
It means that the area and the data located into this segment can be shared between
the protected code and the user code executed in a non-protected area. The access is
allowed whether the Firewall is opened or closed or disabled.
The VDS bit gets priority over the VDE bit, this last bit value being ignored in such a
case. It means that the Volatile data segment can execute parts of code located there
without any need to open the Firewall before executing the code.
• Execute
The VDE bit is considered as soon as the VDS bit = 0 in the FW_CR register. If the
VDS bit = 1, refer to the description above on the Volatile data segment sharing. If VDS
= 0 and VDE = 1, the Volatile data segment is executable. To avoid a system reset
generation from the Firewall, the “call gate” sequence should be applied on the Volatile
data segment to open the Firewall as an entry point for the code execution.
Segments properties
Each segment has a specific length register to define the segment size to be protected by
the Firewall: CSL register for the Code segment length register, NVDSL for the Non-volatile
data segment length register, and VDSL register for the Volatile data segment length
register. Granularity and area ranges for each of the segments are presented in Table 31.
Firewall disable
(reset)
Illegal accesses to
the protected Enable the firewall Protected code jumps
segments (FWDIS = 0) to an unprotected
segment and FPA = 0
Firewall Firewall
closed opened
MS32390V4
rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. LENG[21:16]
rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw
rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. LENG[21:16]
rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. VDE VDS FPA
rw rw rw
This register is protected in the same way as the Non-volatile data segment (refer to
Section 5.3.5: Firewall initialization).
0xC
0x20
0x18
0x14
0x10
5.4.8
0x1C
Offset
RM0377
FW_CR
FW_CSL
FW_VDSL
FW_CSSA
Register
Reset Value
Reset Value
Reset Value
Reset Value
Reset Value
Reset Value
Reset Value
Reset Value
FW_NVDSL
FW_VDSSA
FW_NVDSSA
Res. Res. Res. Res. Res. Res. Res. Res. Res. 31
Res. Res. Res. Res. Res. Res. Res. Res. Res. 30
Res. Res. Res. Res. Res. Res. Res. Res. Res. 29
Res. Res. Res. Res. Res. Res. Res. Res. Res. 28
Res. Res. Res. Res. Res. Res. Res. Res. Res. 27
Res. Res. Res. Res. Res. Res. Res. Res. Res. 26
Res. Res. Res. Res. Res. Res. Res. Res. Res. 25
Firewall register map
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
RM0377 Rev 10
0
0
0
0
0
0
0
0
0
0
14
0
0
0
0
ADD
0
0
0
0
0
0
VDE Res. Res. Res. Res. Res. Res. Res. Res. 2
0
VDS Res. Res. Res. Res. Res. Res. Res. Res. 1
0
FPA Res. Res. Res. Res. Res. Res. Res. Res. 0
Firewall (FW)
135/905
135
Power control (PWR) RM0377
VDDA domain
(from 1.65 V up to VDDA) VREF+
ADC
Temp. Sensor
Reset block
(VDD) VDDA
PLL
(VSS) VSSA
VCore domain
VDD domain
Flash memory
IO supply
Standby
circuitry(Wakeup
logic, IWDG, RTC, Core memories
VSS Digital
LSE crystal 32K
osc., RCC) peripherals
VDD
Voltage regulator
Dynamic voltage
scaling
MSv34752V2
Range 1
Range 1 is the “high performance” range.
The voltage regulator outputs a 1.8 V voltage (typical) as long as the VDD input voltage is
above 1.71 V. Flash program and erase operations can be performed in this range.
When VDD is below 2.0 V, the CPU frequency changes from initial to final state must respect
the following conditions:
• fCPUfinal < 4xfCPUinitial.
• In addition, a 5 μs delay must be respected between two changes. For example to
switch from 4.2 to 32 MHz, switch from 4.2 to 16 MHz, wait for 5 μs, then switch from
16 to 32 MHz.
Range 2 and 3
The regulator can also be programmed to output a regulated 1.5 V (typical, range 2) or a
1.2 V (typical, range 3) without any limitations on VDD (1.65 to 3.6 V).
• At 1.5 V, the Flash memory is still functional but with medium read access time. This is
the “medium performance” range. Program and erase operations on the Flash memory
are still possible.
• At 1.2 V, the Flash memory is still functional but with slow read access time. This is the
“low performance” range. Program and erase operations on the Flash memory are not
possible under these conditions.
Refer to Table 33 for details on the performance for each range.
MHz
32
32 MHz
1WS
16
16 MHz
16 MHz
12
FCPU > 8 MHz
1WS
8
8 MHz
4
4.2 MHz
0WS 0WS 0WS
Rang e 1
Ra nge 2
Range 3
MS32792V1
select by software PVD threshold 2 (2.26 V typical). For more details on the PVD, refer
to Section 6.2.3.
2. Adapt the clock frequency to the voltage range that will be selected at next step:
Below 1.71 V, the system clock frequency is limited to 16 MHz for range 2 and 4.2 MHz
for range 3.
3. Select the required voltage range:
Note that when VDD is below 1.71 V, only range 2 or range 3 can be selected.
Note: When VCORE range 2 or range 3 is selected and VDD drops below 1.71 V, no system
reconfiguration is required.
6.1.8 Voltage range and limitations when VDD ranges from 1.71 V to 2.0 V
The STM32L0x1 voltage regulator is based on an architecture designed for Ultra-low-power
a. It does not use any external capacitor. Such regulator is sensitive to fast changes of load.
In this case, the output voltage is reduced for a short period of time. Considering that the
core voltage must be higher than 1.65 V to ensure a 32 MHz operation, this phenomenon is
critical for very low VDD voltages (e.g. 1.71 V VDD minimum value).
To guarantee 32 MHz operation at VDD =1.8 V±5%, with 1 wait state, and VCORE range 1,
the CPU frequency in run mode must be managed to prevent any changes exceeding a
ratio of 4 in one shot. A delay of 5 μs must be respected between 2 changes. There is no
limitation when waking up from low-power mode.
VDD /VDD A
V PVD 100 mV
hysteresis
V BO R 100 mV
hysteresis
VPOR/ VPDR
IT enabled
PVD output
BOR reset
(NRST)
BOR/PDR reset
(NRST)
POR/PDR reset
(NRST)
PVD
BOR always active
BOR disabled by option byte
POR/PDR (BOR not available)
ai17211b
VDD/VDDA
POR
PDR
Temporization
tRSTTEMPO
Reset
MS32793V1
And when the BOR option is disabled by option byte, the power-down reset is controlled by
the PDR and a “gray zone” exists between the 1.65 V and VPDR.
VBOR is configured through device option bytes. By default, the Level 4 threshold is
activated. 5 programmable VBOR thresholds can be selected.
• BOR Level 1 (VBOR0): reset threshold level for 1.69 to 1.80 V voltage range
• BOR Level 2 (VBOR1): reset threshold level for 1.94 to 2.1 V voltage range
• BOR Level 3 (VBOR2): reset threshold level for 2.3 to 2.49 V voltage range
• BOR Level 4 (VBOR3): reset threshold level for 2.54 to 2.74 V voltage range
• BOR Level 5 (VBOR4): reset threshold level for 2.77 to 3.0 V voltage range
When the supply voltage (VDD) drops below the selected VBOR threshold, a device reset is
generated. When the VDD is above the VBOR upper limit the device reset is released and the
system can start.
BOR can be disabled by programming the device option bytes. To disable the BOR function,
VDD must have been higher than VBOR0 to start the device option byte programming
sequence. The power-on and power-down is then monitored by the POR and PDR (see
Section 6.2.1: Power-on reset (POR)/power-down reset (PDR))
The BOR threshold hysteresis is ~100 mV (between the rising and the falling edge of the
supply voltage).
100mV
BOR threshold hysteresis
Reset
MS32794V1
generated when VDD drops below the PVD threshold. As an example the service routine
could perform emergency shutdown tasks.
VDD/VDDA
100mV
PVD threshold hysteresis
PVD output
MSv32795V2
PDDS, LPSDSR
Any EXTI line
or LPDS bits +
(configured in the EXTI In low-power
Stop SLEEPDEEP bit +
registers, internal and mode
WFI, Return from
external lines)
ISR or WFE HSI16(1), HSE
All VCORE
Standby WKUP pin rising edge, and MSI
domain clocks
RTC alarm (Alarm A or oscillators
PDDS bit + OFF
Alarm B), RTC Wakeup OFF
SLEEPDEEP bit +
event, RTC tamper OFF
WFI, Return from
event, RTC timestamp
ISR or WFE
event, external reset in
NRST pin, IWDG reset
1. HSI16 can run in Stop mode provided HSI16KERON is set in Clock control register (RCC_CR).
Note: To be able to read the RTC calendar register when the APB1 clock frequency is less than
seven times the RTC clock frequency (7*RTCLCK), the software must read the calendar
time and date registers twice.
If the second read of the RTC_TR gives the same result as the first read, this ensures that
the data is correct. Otherwise a third read access must be done.
Voltage regulator in low-power mode and the Flash memory switched off
WFI (Wait for Interrupt) or WFE (wait for event) while:
– SLEEPDEEP = 0 and
– No interrupt (for WFI) or event (for WFE) is pending
Refer to the Cortex®-M0+ System Control register (see PM0223 programming
manual).
Mode entry
On return from ISR while:
– SLEEPDEEP = 0 and
– SLEEPONEXIT = 1 and
– No interrupt is pending
Refer to the Cortex®-M0+ System Control register (see PM0223 programming
manual).
Voltage regulator in Main regulator mode and the Flash memory switched on
If WFI or Return from ISR was used for entry:
Interrupt: Refer to Table 53: List of vectors
If WFE was used for entry and SEVONPEND = 0
Mode exit
Wakeup event: Refer to Section 12.3.2: Wakeup event management
If WFE was used for entry and SVONPEND = 1
Interrupt event when disabled in NVIC (refer to Table 53: List of vectors) or
wakeup event (refer to Section 12.3.2: Wakeup event management)
Wakeup latency Regulator wakeup time from low-power mode
To further reduce power consumption in Stop mode, the internal voltage regulator can be put
in low-power mode. This is configured by the LPSDSR or LPDS bit in the PWR_CR register
(see Section 6.4.1). The internal voltage regulator can also be kept in Main mode but the
consumption will be much higher. As a result, it is always implicitly assumed that the
regulator is in low-power mode during Stop mode. The only advantage of keeping the
regulator in Main mode is that the wakeup time from Stop mode is shorter.
If Flash memory programming or an access to the APB domain is ongoing, the Stop mode
entry is delayed until the memory or APB access has completed.
In Stop mode, the following features can be selected by programming individual control bits:
• Independent watchdog (IWDG): the IWDG is started by writing to its Key register or by
hardware option. Once started it cannot be stopped except by a Reset. Refer to
Section 20.3: IWDG functional description in Section 20: Independent watchdog
(IWDG).
• Real-time clock (RTC): this is configured by the RTCEN bit in the RCC_CSR register
(see Section 7.3.20).
• Internal RC oscillator (LSI RC): this is configured by the LSION bit in the RCC_CSR
register.
• External 32.768 kHz oscillator (LSE OSC): this is configured by the LSEON bit in the
RCC_CSR register.
The ADC can also consume power in Stop mode, unless they are disabled before entering
it. To disable them, the ADDIS bit in the ADC_CR register must be set to 1 must be written
to 0.
Debug mode
By default, the debug connection is lost if the application puts the MCU in Stop or Standby
mode while the debug features are used. This is due to the fact that the Cortex®-M0+ core is
no longer clocked.
However, by setting some configuration bits in the DBG_CR register, the software can be
debugged even when using the low-power modes extensively. For more details, refer to
Section 27.9.1: Debug support for low-power modes.
6.3.11 Waking up the device from Stop and Standby modes using the RTC
and comparators
The MCU can be woken up from low-power mode by an RTC Alarm event, an RTC Wakeup
event, a tamper event, a time-stamp event, or a comparator event, without depending on an
external interrupt (Auto-wakeup mode).
These RTC alternate functions can wake up the system from Stop and Standby low-power
modes while the comparator events can only wake up the system from Stop mode.
The system can also wake up from low-power modes without depending on an external
interrupt (Auto-wakeup mode) by using the RTC alarm or the RTC wakeup events.
The RTC provides a programmable time base for waking up from Stop or Standby mode at
regular intervals. For this purpose, two of the three alternative RTC clock sources can be
selected by programming the RTCSEL[1:0] bits in the RCC_CSR register (see
Section 7.3.20):
• Low-power 32.768 kHz external crystal oscillator (LSE OSC).
This clock source provides a precise time base with very low-power consumption (less
than 1 µA added consumption in typical conditions)
• Low-power internal RC oscillator (LSI RC)
This clock source has the advantage of saving the cost of the 32.768 kHz crystal. This
internal RC Oscillator is designed to use minimum power consumption.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. LPDS
rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
DS_EE
Res. LPRUN VOS[1:0] FWU ULP DBP PLS[2:0] PVDE CSBF CWUF PDDS LPSDSR
_KOFF
rw rw rw rw rw rw rw rw rw rw rw rc_w1 rc_w1 rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
EWUP EWUP EWUP REG VREFIN
Res. Res. Res. Res. Res. Res. Res. VOSF PVDO SBF WUF
3 2 1 LPF TRDYF
rw rw rw r r r r r r
Offset Register
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
10
11
9
8
7
6
5
4
3
2
1
0
DS_EE_KOFF.
LPSDSR
LPRUN
VOS
CWUF
PDDS
PVDE
CSBF
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
LPDS
FWU
DBP
ULP
PWR_CR PLS[2:0]
0x000 [1:0]
Reset value 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
VREFINTRDYF
REGLPF
EWUP3
EWUP2
EWUP1
PVDO
VOSF
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
WUF
SBF
PWR_CSR
0x004
Reset value 0 0 0 0 0 1 0 0 0
7.1 Reset
There are three types of reset, defined as system reset, power reset and RTC domain reset.
Software reset
The SYSRESETREQ bit in Cortex®-M0+ AIRCR register (Application Interrupt and Reset
Control Register) must be set to force a software reset on the device. Refer to Arm®
Cortex®-M0+ Technical Reference Manual for more details.
VDD
External NRST
Filter
reset
WWDG reset
IWDG reset
Pulse Firewall reset
generator Software reset
(min 20 μs) Low-power manager reset
Option byte loader reset
BOR reset
MSv41924V2
7.2 Clocks
Four different clock sources can be used to drive the system clock (SYSCLK):
• HSI16 (high-speed internal) oscillator clock
• HSE (high-speed external) oscillator clock
The HSE external quartz connexion is available only on cat. 2 devices in LQFP48
package.
• PLL clock
• MSI (multispeed internal) oscillator clock
The MSI at 2.1MHz is used as system clock source after startup from power reset,
system or RTC domain reset, and after wake-up from Standby mode.
The HSI16, HSI16 divided by 4, or the MSI at any of its possible frequency can be used
to wake up from Stop mode.
The devices have two secondary clock sources:
• 37 kHz low speed internal RC (LSI RC) which drives the independent watchdog and
optionally the RTC used for Auto-wakeup from Stop/Standby mode and the LPTIMER.
• 32.768 kHz low speed external crystal (LSE crystal) which optionally drives the real-
time clock (RTCCLK), the LPTIMER and USARTs.
Each clock source can be switched on or off independently when it is not used to optimize
power consumption.
Several prescalers can be used to configure the AHB frequency and the two APBs (APB1
and APB2) domains. The maximum frequency of AHB, APB1 and the APB2 domains is
32 MHz. It depends on the device voltage range. For more details refer to Section 6.1.4:
Dynamic voltage scaling management.
All the peripheral clocks are derived from the system clock (SYSCLK) except:
• The ADC can be derived either from the APB clock or the HSI16 clock.
• The LPUART1 and USART1/2 clock which is derived (selected by software) from one
of the four following sources:
– system clock
– HSI16 clock
– LSE clock
– APB clock (PCLK)
• The I2C1 clock which is derived (selected by software) from one of the three following
sources:
– system clock
– HSI16 clock
– APB clock (PCLK)
• The LPTIMER clock which is derived (selected by software) from one of the four
following sources:
– HSI16 clock
– LSE clock
– LSI clock
– APB clock (PCLK)
• The RTC clock which is derived from the following clock sources:
– LSE clock,
– LSI clock,
– 4 MHz HSE_RTC (HSE divided by a programmable prescaler).
• IWDG clock which is always the LSI clock.
The system clock (SYSCLK) frequency must be higher or equal to the RTC clock frequency.
The RCC feeds the Cortex® System Timer (SysTick) external clock with the AHB clock
(HCLK) divided by 8. The SysTick can work either with this clock or with the Cortex® clock
(HCLK), configurable in the SysTick Control and Status Register.
@V18
Peripherals
LSI enable
LPTIMCLK
LSE Peripherals
enable
HSI16
SYSCLK
PCLK Peripherals LPUART/
enable UARTCLK
I2C1CLK
MSv34747V2
1. For full details about the internal and external clock source characteristics, please refer to the “Electrical
characteristics” section in your device datasheet.
The timer clock frequencies are automatically fixed by hardware. There are two cases:
1. If the APB prescaler is 1, the timer clock frequencies are set to the same frequency as
that of the APB domain to which the timers are connected.
2. Otherwise, they are set to twice (×2) the frequency of the APB domain to which the
timers are connected.
fCLK acts as Cortex®-M0+ free running clock. For more details refer to the Section 27:
Debug support (DBG).
MSv36151V1
OSC_IN OSC_OUT
Crystal/Ceramic
resonators for
category 2 (LQFP48
only), category 3 and
CL1 CL2
5 devices Load
capacitors
MSv31916V1
Calibration
RC oscillator frequencies can vary from one chip to another due to manufacturing process
variations, this is why each device is factory calibrated by ST for 1% accuracy at an ambient
temperature, TA, of 25 °C.
After reset, the factory calibration value is loaded in the HSI16CAL[7:0] bits in the Internal
Clock Sources Calibration Register (RCC_ICSCR) (see Section 7.3.2).
If the application is subject to voltage or temperature variations, this may affect the RC
oscillator speed. You can trim the HSI16 frequency in the application by using the
HSI16TRIM[4:0] bits in the RCC_ICSCR register. For more details on how to measure the
HSI16 frequency variation please refer to Section 7.2.14: Internal/external clock
measurement using TIM21.
The HSI16RDY flag in the RCC_CR register indicates whether the HSI16 oscillator is stable
or not. At startup, the HSI16 RC output clock is not released until this bit is set by hardware.
The HSI16 RC oscillator can be switched on and off using the HSI16ON bit in the RCC_CR
register.
The MSI RC can be switched on and off by using the MSION bit in the RCC_CR register
(see Section 7.3.1).
It can also be used as a backup clock source (auxiliary clock) if the HSE crystal oscillator
fails. Refer to Section 7.2.9: HSE clock security system (CSS) on page 177.
Calibration
The MSI RC oscillator frequency can vary from one chip to another due to manufacturing
process variations, this is why each device is factory calibrated by ST for 1% accuracy at an
ambient temperature, TA, of 25 °C.
After reset, the factory calibration value is loaded in the MSICAL[7:0] bits in the
RCC_ICSCR register. If the application is subject to voltage or temperature variations, this
may affect the RC oscillator speed. You can trim the MSI frequency in the application by
using the MSITRIM[7:0] bits in the RCC_ICSCR register. For more details on how to
measure the MSI frequency variation please refer to Section 7.2.14: Internal/external clock
measurement using TIM21.
7.2.4 PLL
The internal PLL can be clocked by the HSI16 RC or HSE clocks.
The PLL input clock frequency must range between 2 and 24 MHz.
The desired frequency is obtained by using the multiplication factor and output division
embedded in the PLL:
• The system clock is derived from the PLL VCO divided by the output division factor.
Note: The application software must set correctly the PLL multiplication factor to avoid exceeding
96 MHz as PLLVCO when the product is in range 1,
48 MHz as PLLVCO when the product is in range 2,
24 MHz when the product is in range 3.
It must also set correctly the output division to avoid exceeding 32 MHz as SYSCLK.
The minimum input clock frequency for PLL is 2 MHz (when using HSE as PLL source).
The PLL configuration (selection of the source clock, multiplication factor and output division
factor) must be performed before enabling the PLL. Once the PLL is enabled, these
parameters cannot be changed.
To modify the PLL configuration, proceed as follows:
1. Disable the PLL by setting PLLON to 0.
2. Wait until PLLRDY is cleared. The PLL is now fully stopped.
3. Change the desired parameter.
4. Enable the PLL again by setting PLLON to 1.
An interrupt can be generated when the PLL is ready if enabled in the RCC_CIER register
(see Section 7.3.4).
For code example, refer to A.4.2: PLL configuration modification code example.
LSI measurement
The frequency dispersion of the LSI oscillator can be measured to have accurate RTC time
base and/or IWDG timeout (when LSI is used as clock source for these peripherals) with an
acceptable accuracy. For more details, refer to the electrical characteristics section of the
datasheets. For more details on how to measure the LSI frequency, please refer to
Section 7.2.14: Internal/external clock measurement using TIM21.
If the HSE oscillator is used directly or indirectly as the system clock (indirectly means: it is
used as PLL input clock, and the PLL clock is used as system clock), a detected failure
causes a switch of the system clock and the disabling of the HSE oscillator. If the HSE
oscillator clock is the clock entry of the PLL used as system clock when the failure occurs,
the PLL is disabled too.
When an HSE failure occurs, the system clock can be switched to the MSI or to the internal
16-MHz HSI clock depending on the value of STOPWUCK bit in the RCC_CFGR register.
Note: Category 1 devices do not feature HSE clock security system. The HSE clock is available
only in bypass mode.
TI1_RMP[2:0] TIM21
GPIO
MSI TI(1)
LSI
HSE_RTC
LSE
ETR
LSE
GPIO TI(2)
MS32913V1
TIM21 has an input multiplexer that selects which of the I/O or the internal clock is to trigger
the input capture. This selection is performed through the TI1_RMP [2:0] bits in the
TIM21_OR register.
The primary purpose of connecting the LSE to the channel 1 input capture is to be able to
accurately measure the HSI16 and MSI system clocks (for this, either the HSI16 or MSI
should be used as the system clock source). The number of HSI16 (MSI, respectively) clock
counts between consecutive edges of the LSE signal provides a measure of the internal
clock period. Taking advantage of the high precision of LSE crystals (typically a few tens of
ppm’s), it is possible to determine the internal clock frequency with the same resolution, and
trim the source to compensate for manufacturing-process- and/or temperature- and voltage-
related frequency deviations.
The MSI and HSI16 oscillators both have dedicated user-accessible calibration bits for this
purpose.
The basic concept consists in providing a relative measurement (e.g. the HSI16/LSE ratio):
the precision is therefore closely related to the ratio between the two clock sources. The
higher the ratio, the better the measurement.
It is however not possible to have a good enough resolution when the MSI clock is low
(typically below 1 MHz). In this case, it is advised to:
• accumulate the results of several captures in a row
• use the timer’s input capture prescaler (up to 1 capture every 8 periods)
• use the RTC_OUT signal at 512 Hz (when the RTC is clocked by the LSE) as the input
for the channel1 input capture. This improves the measurement precision
TIM21 can also be used to measure the LSI, MSI, or HSE_RTC: this is useful for
applications with no crystal. The ultra-low-power LSI oscillator has a wide manufacturing
process deviation: by measuring it as a function of the HSI16 clock source, its frequency
can be determined with the precision of the HSI16.The HSE_RTC frequency (HSE divided
by a programmable prescaler) being relatively high (4 MHz), the relative frequency
measurement is not very accurate. Its main purpose is consequently to obtain a rough
indication of the external crystal frequency. This can be useful to meet the requirements of
the IEC 60730/IEC 61335 standards, which require to be able to determine harmonic or
subharmonic frequencies (–50/+100% deviations).
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
PLL CSSHS HSE HSE HSE
Res. Res. Res. Res. Res. Res. PLLON Res. Res. RTCPRE[1:0]
RDY EON. BYP RDY ON
r rw rw rw rw rw r rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
MSI HSI16 HSI16 HSI16 HSI16 HSI16K HSI16
Res. Res. Res. Res. Res. Res. MSION Res. Res.
RDY OUTEN DIVF DIVEN RDYF ERON ON
r rw rw r rw r rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
MSITRIM[7:0] MSICAL[7:0]
rw rw rw rw rw rw rw rw r r r r r r r r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
MSIRANGE[2:0] HSI16TRIM[4:0] HSI16CAL[7:0]
rw rw rw rw rw rw rw rw r r r r r r r r
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
PLL
Res. MCOPRE[2:0] MCOSEL[3:0] PLLDIV[1:0] PLLMUL[3:0] Res.
SRC
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
STOP
Res. PPRE2[2:0] PPRE1[2:0] HPRE[3:0] SWS[1:0] SW[1:0]
WUCK.
rw rw rw rw rw rw rw rw rw rw rw r r rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CSS MSI PLL HSE HSI16 LSE LSI
Res. Res. Res. Res. Res. Res. Res. Res. Res.
LSE RDYIE RDYIE RDYIE RDYIE RDYIE RDYIE
rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CSS CSS MSI PLL HSE HSI16 LSE LSI
Res. Res. Res. Res. Res. Res. Res. Res.
HSEF LSEF RDYF RDYF RDYF RDYF RDYF RDYF
r r r r r r r r r
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CSS CSS MSI PLL HSE HSI16 LSE LSI
Res. Res. Res. Res. Res. Res. Res. Res.
HSEC LSEC RDYC RDYC RDYC RDYC RDYIC RDYC
w w w w w w w w
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
IOPH IOPER IOPD IOPC IOPB IOPA
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
RST ST RST RST RST RST
rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
CRYP
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
RST
rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CRC MIF DMA
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
RST RST RST
rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
DBG
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
RST
rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
USART1 SPI1 ADC TIM22 TIM21 SYSCF
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
RST RST RST RST RST GRST
rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
LPTIM1 I2C3R PWR I2C2R I2C1R USART5 USART4 LPUART1 USART2
Res. Res. Res. Res. Res. Res. Res.
RST ST RST ST ST RST RST RST RST
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
TIM7R TIM6RS TIM3RS
SPI2R WWDG TIM2
Res. Res. Res. Res. Res. Res. Res. Res. ST T Res. Res. T
ST RST RST
rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
IOPH IOPE IOPD IOPC IOPB IOPA
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
EN EN EN EN EN EN
rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
CRYP
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
EN
rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CRC MIF DMA
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
EN EN EN
rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
DBG
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
EN
rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
USART1 SPI1 ADC TIM22 TIM21 SYSCF
Res. Res. Res. Res. Res. FWEN Res. Res. Res. Res.
EN EN EN EN EN EN
rw rw rw rs rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
LPTIM1 I2C3E PWRE I2C2E I2C1E USART5 USART4 LPUART1 USART2
Res. Res Res. Res. Res. Res. Res.
EN N N N N EN EN EN EN
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
SPI2E WWDG TIM7E TIM2E
Res. Res. Res. Res. Res. Res. Res. Res. TIM6EN Res. Res. TIM3EN
N EN N N
rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
IOPHS IOPES IOPDS IOPCS IOPBS IOPAS
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
MEN MEN MEN MEN MEN MEN
rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
CRYP
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
SMEN
rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CRC SRAM MIF DMA
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
SMEN SMEN SMEN SMEN
rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
DBG
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
SMEN
rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
USART1 SPI1 ADC TIM22 TIM21 SYSCF
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
SMEN SMEN SMEN SMEN SMEN SMEN
rw rw rw rw rw rw
Bit 2 TIM21SMEN: TIM21 timer clock enable during Sleep mode bit
This bit is set and cleared by software.
0: TIM21 clock disabled in Sleep mode
1: TIM21 clock enabled in Sleep mode (if enabled by TIM21EN)
Bit 1 Reserved, must be kept at reset value.
Bit 0 SYSCFGSMEN: System configuration controller clock enable during Sleep mode bit
This bit is set and cleared by software.
0: System configuration controller clock disabled in Sleep mode
1: System configuration controller clock enabled in Sleep mode
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
LPTIM1 I2C3S PWRS I2C2S I2C1S USART5 USART4 LPUART1 USART2
Res. Res. Res. Res. Res. Res. Res.
SMEN MEN MEN MEN MEN SMEN SMEN SMEN SMEN
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
SPI2S WWDG TIM7S TIM6SM TIM3SM TIM2S
Res. Res. Res. Res. Res. Res. Res. Res. Res.
MEN SMEN MEN EN EN MEN
rw rw rw rw rw rw
Bit 31 LPTIM1SMEN: Low-power timer clock enable during Sleep mode bit
This bit is set and cleared by software.
0: Low-power timer clock disabled in Sleep mode
1: Low-power timer clock enabled in Sleep mode (if enabled by LPTIM1EN)
Bit 30 I2C3SMEN: I2C3 clock enable during Sleep mode bit
This bit is set and cleared by software.
0: I2C3 clock disabled in Sleep mode
1: I2C3 clock enabled in Sleep mode (if enabled by I2C3EN)
Bit 29 Reserved, must be kept at reset value.
Bit 28 PWRSMEN: Power interface clock enable during Sleep mode bit
This bit is set and cleared by software.
0: Power interface clock disabled in Sleep mode
1: Power interface clock enabled in Sleep mode (if enabled by PWREN)
Bit 27 Reserved, must be kept at reset value.
Bits 26:23 Reserved, must be kept at reset value.
Bit 22 I2C2SMEN: I2C2 clock enable during Sleep mode bit
This bit is set and cleared by software.
0: I2C2 clock disabled in Sleep mode
1: I2C2 clock enabled in Sleep mode (if enabled by I2C2EN)
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
LPTIM1 LPTIM1S I2C3SE I2C3SE
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
SEL1 EL0 L1 L0
rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
I2C1 I2C1 LPUART1 LPUART1 USART2 USART2 USART1 USART1
Res. Res. Res. Res. Res. Res. Res. Res.
SEL1 SEL0 SEL1 SEL0 SEL1 SEL0 SEL1 SEL0
rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
LPWR WWDG IWDG SFT POR PIN OBL FW RTC RTC
RMVF Res. Res. Res. RTCSEL[1:0]
RSTF RSTF RSTF RSTF RSTF RSTF RS TF RSTF RST. EN
r r r r r r r r rt_w rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CSSLS CSSLS LSE LSI
Res. LSEDRV[1:0] LSERDY LSEON Res. Res. Res. Res. Res. Res. LSION
ED EON BYP RDY
r rw rw rw r rw r rw
If the LSE or LSI is used as RTC clock source, the RTC continues to work in Stop and
Standby low-power modes, and can be used as wake-up source. However, when the HSE
clock is used as RTC clock source, the RTC cannot be used in Stop and Standby low-power
modes.
Bit 15 Reserved, must be kept at reset value.
Bit 14 CSSLSED: CSS on LSE failure detection flag
This bit is set by hardware to indicate when a failure has been detected by the clock security
system on the external 32 kHz oscillator (LSE).
It is cleared by a power-on reset or by an RTC software reset (RTCRST bit).
0: No failure detected on LSE (32 kHz oscillator)
1: Failure detected on LSE (32 kHz oscillator)
Bit 13 CSSLSEON CSS on LSE enable bit
This bit is set by software to enable the Clock Security System on LSE (32 kHz oscillator).
CSSLSEON must be enabled after the LSE and LSI oscillators are enabled (LSEON and
LSION bits enabled) and ready (LSERDY and LSIRDY flags set by hardware), and after the
RTCSEL bit is selected.
Once enabled this bit cannot be disabled, except after an LSE failure detection (CSSLSED
=1). In that case the software MUST disable the CSSLSEON bit.
Reset by power on reset and RTC software reset (RTCRST bit).
0: CSS on LSE (32 kHz oscillator) OFF
1: CSS on LSE (32 kHz oscillator) ON
Bits 12-11 LSEDRV; LSE oscillator Driving capability bits
These bits are set by software to select the driving capability of the LSE oscillator.
They are cleared by a power-on reset or an RTC reset. Once “00” has been written, the
content of LSEDRV cannot be changed by software.
00: Lowest drive
01: Medium low drive
10: Medium high drive
11: Highest drive
0x20
0x18
0x14
0x10
0x04
0x00
Off-
0x1C
0x0C
7.3.21
RM0377
RCC_CR
RCC_CIFR
RCC_CIER
Register
RCC_CICR
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
RCC_CFGR
RCC_ICSCR
RCC_IOPRSTR
RCC_AHBRSTR
0
Res. Res. Res. Res. Res. Res. Res. 31
0
0
Res. Res. Res. Res. Res. Res. 30
0
0
E
Res. Res. Res. Res. Res. Res. 29
[2:0]
MCOPR
0
0
Res. Res. Res. Res. Res. Res. 28
0
RCC register map
0
0
Res. Res. Res. Res. Res. Res.
MSITRIM[7:0]
26
0
0
0
Res. Res. Res. Res. Res. PLL RDY 25
L [3:0]
MCOSE
0
0
0
0
CRYPRST Res. Res. Res. Res. PLL ON 24
0
Res. Res. Res. Res. Res. Res. 23
DIV
PLL
[1:0]
x
0
Res. Res. Res. Res. Res. Res. 22
0
Res. Res. Res. Res. Res. 21
[1:0]
RTC
PRE
0
0
Res. Res. Res. Res. Res. 20
0]
x
0
Res. Res. Res. Res. Res. CSSLSEON 19
PLLMUL[3:
x
0
Res. Res. Res. Res. Res. HSEBYP
MSICAL[7:0]
Reserved
18
RM0377 Rev 10
x
X X 0 X 0
0
0
0
0
1
0
1
MSIRAN
0
0
1
PPRE2
0
0
0
0
9
[2:0]
PPRE1
HSI16TRIM[4:0
0
0
0
1
0
0
MIFRST Res. CSSHSEC. CSSHSEF. Res. MSION 8
x
0
0
0
0
Res. IOPHRS CSSLSEC. CSSLSEF. CSSLSE Res. 7
x
0
0
0
0
0
0
0
0
0
0
0
0
Res. IOPERST PLLRDYC PLLRDYF PLLRDYIE HSI16DIVF 4
x
0
0
0
0
0
Res. IOPDRS HSERDYC HSERDYF HSERDYIE HSI16DIVEN 3
[1:0]
x
0
0
0
0
0
SWS
0
0
0
0
0
Res. IOPBRST LSERDYC LSERDYF LSERDYIE HSI16KERON 1
SW
[1:0]
x
0
0
0
0
0
0
0
213/905
0
215
set
0x44
0x40
0x38
0x34
0x30
0x28
0x24
Off-
0x3C
0x2C
214/905
Register
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
RCC_IOPENR
RCC_AHBENR
RCC_IOPSMEN
RCC_APB1ENR
RCC_APB2ENR
RCC_APB1RSTR
RCC_APB2RSTR
RCC_AHBSMENR
RCC_APB2SMENR
0
0
Res. Res. Res. LPTIM1EN Res. Res. Res. LPTIM1RST Res. 31
0
0
Res. Res. Res. I2C3EN Res. Res. Res. I2C3RST Res. 30
Res. Res. Res. Res. Res. Res. Res. Res. Res. 29
Reset and clock control (RCC)
0
0
Res. Res. Res. PWREN Res. Res. Res. PWRRST Res. 28
Res. Res. Res. Res. Res. Res. Res. Res. Res. 27
Res. Res. Res. Res. Res. Res. Res. Res. Res. 26
Res. Res. Res. Res. Res. Res. Res. Res. Res. 25
1
0
Res. CRYPSMEN Res. Res. Res. CRYPEN Res. Res. Res. 24
Res. Res. Res. Res Res. Res. Res. Res. Res. 23
1
0
0
0
0
DBGSMEN Res. Res. I2C2EN DBGEN Res. Res. I2C2RST DBGRST 22
0
0
Res. Res. Res. I2C1EN Res. Res. Res. I2C1RST Res. 21
0
0
Res. Res. Res. USART5EN Res. Res. Res. USART5RST Res. 20
0
0
0
0
RM0377 Rev 10
0
0
0
Res. Res. Res. Res. Res. Res. Res. Res. Res. 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. 15
1
0
0
0
0
1
1
0
0
0
0
0
1
1
0
0
1
1
1
0
1
0
0
0
0
0
0
1
0
1
0
1
0
0
1
0
1
0
1
1
0
0
0
0
0
1
0
0
set
0x50
0x48
Off-
0x4C
RM0377
RCC_CSR
Register
Reset value
Reset value
Reset value
RCC_CCIPR
RCC_APB1SMENR
0
1
LPWRSTF Res. LPTIM1SMEN 31
0
1
WWDGRSTF Res. I2C3SMEN 30
0
IWDGRSTF Res. Res. 29
0
1
SFTRSTF Res. PWRSMEN 28
1
PORRSTF Res. Res. 27
1
PINRSTF Res Res. 26
0
OBLRSTF Res. Res. 25
0
FWRSTF Res. Res. 24
0
RMVF Res. Res 23
1
0
0
1
0
0
1
RM0377 Rev 10
0
0
1
0
0
I2C3SEL0 Res. 16
Res. Res. Res. 15
0
1
I2C1SEL0 Res. 12
LSE
[1:0]
DRV
0
0
1
LPUART1SEL1 WWDGSMEN 11
0
0
9
Table 44. RCC register map and reset values (continued)
215/905
0
215
General-purpose I/Os (GPIO) RM0377
8.1 Introduction
Each general-purpose I/O port has four 32-bit configuration registers (GPIOx_MODER,
GPIOx_OTYPER, GPIOx_OSPEEDR and GPIOx_PUPDR), two 32-bit data registers
(GPIOx_IDR and GPIOx_ODR) and a 32-bit set/reset register (GPIOx_BSRR). In addition
all GPIOs have a 32-bit locking register (GPIOx_LCKR) and two 32-bit alternate function
selection registers (GPIOx_AFRH and GPIOx_AFRL).
Figure 19 and Figure 20 show the basic structures of a standard and a 5-Volt tolerant I/O
port bit, respectively. Table 45 gives the possible port bit configurations.
Analog
To on-chip
peripheral Alternate function input
Protection
trigger on/off
Pull diode
up
Input driver I/O pin
Output data register
Write
MS31476V1
To on-chip
peripheral
Alternate function input
on/off
Input data register
Read (1)
VDDIOx VDD_FT
TTL Schmitt
Bit set/reset registers
Protection
trigger on/off
Pull diode
up
Input driver I/O pin
Write
Output data register
ai15939d
1. VDD_FT is a potential specific to 5-Volt tolerant I/Os and different from VDD.
0 0 0 GP output PP
0 0 1 GP output PP + PU
0 1 0 GP output PP + PD
0 SPEED 1 1 Reserved
01
1 [1:0] 0 0 GP output OD
1 0 1 GP output OD + PU
1 1 0 GP output OD + PD
1 1 1 Reserved (GP output OD)
0 0 0 AF PP
0 0 1 AF PP + PU
0 1 0 AF PP + PD
0 SPEED 1 1 Reserved
10
1 [1:0] 0 0 AF OD
1 0 1 AF OD + PU
1 1 0 AF OD + PD
1 1 1 Reserved
x x x 0 0 Input Floating
x x x 0 1 Input PU
00
x x x 1 0 Input PD
x x x 1 1 Reserved (input floating)
x x x 0 0 Input/output Analog
x x x 0 1
11
x x x 1 0 Reserved
x x x 1 1
1. GP = general-purpose, PP = push-pull, PU = pull-up, PD = pull-down, OD = open-drain, AF = alternate
function.
When the pin is configured as output, the value written to the output data register
(GPIOx_ODR) is output on the I/O pin. It is possible to use the output driver in push-pull
mode or open-drain mode (only the low level is driven, high level is HI-Z).
The input data register (GPIOx_IDR) captures the data present on the I/O pin at every AHB
clock cycle.
All GPIO pins have weak internal pull-up and pull-down resistors, which can be activated or
not depending on the value in the GPIOx_PUPDR register.
The LOCK sequence (refer to Section 8.4.8: GPIO port configuration lock register
(GPIOx_LCKR) (x = A to E and H)) can only be performed using a word (32-bit long)
access to the GPIOx_LCKR register due to the fact that GPIOx_LCKR bit 16 has to be set
at the same time as the [15:0] bits.
For code example, refer to A.5.1: Locking mechanism code example.
For more details refer to LCKR register description in Section 8.4.8: GPIO port configuration
lock register (GPIOx_LCKR) (x = A to E and H).
MS31477V1
on
Read
VDDIOxVDDIOx
TTL Schmitt on/off
Bit set/reset registers
trigger protection
Pull diode
Input driver up
Write
Output data register
I/O pin
Output driver VDD on/off
Pull protection
P-MOS down diode
Output
control VSS VSS
N-MOS
Read/write
VSS push-pull or
open-drain
From on-chip
peripheral Alternate function output
MSv34756V1
Analog
To on-chip
peripheral
Input data register
Read off
0
VDDIOx
Bit set/reset registers
TTL Schmitt
trigger protection
Write diode
Output data register
Input driver
I/O pin
protection
diode
Read/write VSS
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
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
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
OT15 OT14 OT13 OT12 OT11 OT10 OT9 OT8 OT7 OT6 OT5 OT4 OT3 OT2 OT1 OT0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
OSPEED15 OSPEED14 OSPEED13 OSPEED12 OSPEED11 OSPEED10 OSPEED9 OSPEED8
[1:0] [1:0] [1:0] [1:0] [1:0] [1:0] [1:0] [1: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
OSPEED7 OSPEED6 OSPEED5 OSPEED4 OSPEED3 OSPEED2 OSPEED1 OSPEED0
[1:0] [1:0] [1:0] [1:0] [1:0] [1:0] [1:0] [1:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
PUPD15[1:0] PUPD14[1:0] PUPD13[1:0] PUPD12[1:0] PUPD11[1:0] PUPD10[1:0] PUPD9[1:0] PUPD8[1: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
PUPD7[1:0] PUPD6[1:0] PUPD5[1:0] PUPD4[1:0] PUPD3[1:0] PUPD2[1:0] PUPD1[1:0] PUPD0[1:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
ID15 ID14 ID13 ID12 ID11 ID10 ID9 ID8 ID7 ID6 ID5 ID4 ID3 ID2 ID1 ID0
r r r r r r r r r r r r r r r r
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
OD15 OD14 OD13 OD12 OD11 OD10 OD9 OD8 OD7 OD6 OD5 OD4 OD3 OD2 OD1 OD0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
BR15 BR14 BR13 BR12 BR11 BR10 BR9 BR8 BR7 BR6 BR5 BR4 BR3 BR2 BR1 BR0
w w w w 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
BS15 BS14 BS13 BS12 BS11 BS10 BS9 BS8 BS7 BS6 BS5 BS4 BS3 BS2 BS1 BS0
w w w w w w w w w w w w w w w w
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. LCKK
rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
LCK15 LCK14 LCK13 LCK12 LCK11 LCK10 LCK9 LCK8 LCK7 LCK6 LCK5 LCK4 LCK3 LCK2 LCK1 LCK0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
AFSEL7[3:0] AFSEL6[3:0] AFSEL5[3:0] AFSEL4[3: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
AFSEL3[3:0] AFSEL2[3:0] AFSEL1[3:0] AFSEL0[3:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:0 AFSEL[7:0][3:0]: Alternate function selection for port x I/O pin y (y = 7 to 0)
These bits are written by software to configure alternate function I/Os.
0000: AF0
0001: AF1
0010: AF2
0011: AF3
0100: AF4
0101: AF5
0110: AF6
0111: AF7
Others: Reserved
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
AFSEL15[3:0] AFSEL14[3:0] AFSEL13[3:0] AFSEL12[3: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
AFSEL11[3:0] AFSEL10[3:0] AFSEL9[3:0] AFSEL8[3:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:0 AFSEL[15:8][3:0]: Alternate function selection for port x I/O pin y (y = 15 to 8)
These bits are written by software to configure alternate function I/Os.
0000: AF0
0001: AF1
0010: AF2
0011: AF3
0100: AF4
0101: AF5
0110: AF6
0111: AF7
Others: Reserved
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
BR15 BR14 BR13 BR12 BR11 BR10 BR9 BR8 BR7 BR6 BR5 BR4 BR3 BR2 BR1 BR0
w w w w w w w w w w w w w w w w
0x1C
0x0C
8.4.12
RM0377
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
GPIOx_IDR
GPIOx_ODR
GPIOx_AFRL
GPIOx_AFRH
GPIOx_LCKR
GPIOx_BSRR
GPIOA_PUPDR
GPIOx_MODER
GPIOA_MODER
GPIOx_OTYPER
(where x = A..E,H)
(where x = A..E,H)
(where x = A..E,H)
(where x = A..E,H)
(where x = A..E,H)
(where x = A..E,H)
(where x = B..E,H)
(where x = A..E,H)
GPIOx_OSPEEDR
(where x = B..E, H)
GPIOA_OSPEEDR
Offset Register name
0
0
0
0
0
0
1
1
Res. BR15 Res. Res. Res. 31
PUPD15[1:0] OSPEED15[1:0] OSPEED15[1:0] MODE15[1:0] MODE15[1:0]
0
0
0
0
0
0
1
1
Res. BR14 Res. Res. Res. 30
]
0
0
0
1
0
0
1
1
Res. BR13 Res. Res. Res. 29
PUPD14[1:0] OSPEED14[1:0] OSPEED14[1:0] MODE14[1:0] MODE14[1:0]
0
0
0
0
0
0
1
0
Res. BR12 Res. Res. Res. 28
0
0
0
0
0
1
1
1
Res. BR11 Res. Res. Res. 27
PUPD13[1:0] OSPEED13[1:0] OSPEED13[1:0] MODE13[1:0] MODE13[1:0]
GPIO register map
0
0
0
1
0
1
1
Res. BR10 Res. Res. Res. 0
26
]
0
0
0
0
0
0
1
1
Res. BR9 Res. Res. Res. 25
PUPD12[1:0] OSPEED12[1:0] OSPEED12[1:0] MODE12[1:0] MODE12[1:0]
0
0
0
0
0
0
1
1
0
0
0
0
0
0
1
1
0
0
0
0
0
0
1
1
]
0
0
0
0
0
0
1
1
0
0
0
0
0
0
1
1
0
0
0
0
0
0
1
1
0
0
0
0
0
0
1
1
RM0377 Rev 10
0
0
0
0
0
0
1
1
0
0
0
0
0
0
1
1
0
LCKK BR0 Res. Res. Res. 16
0
0
0
0
0
0
1
1
0
0
0
0
0
0
0
0
0
1
1
0
0
0
]
x
0
0
0
0
0
0
1
1
0
0
0
0
0
0
0
0
0
1
1
0
0
0
0
0
0
0
0
0
1
1
0
0
0
0
0
0
0
0
0
1
1
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
1
1
0
0
0
0
0
0
0
0
0
1
1
0
0
0
0
0
0
0
0
0
0
0
0
1
1
0
0
0
0
0
0
0
0
0
1
1
0
0
0
0
0
0
0
0
0
1
1
0
0
0
0
0
0
0
0
0
1
1
0
0
0
0
0
0
0
0
0
1
1
0
0
0
0
0
0
0
0
0
1
1
231/905
General-purpose I/Os (GPIO)
232
General-purpose I/Os (GPIO) RM0377
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
10
11
9
8
7
6
5
4
3
2
1
0
GPIOx_BRR
BR15
BR14
BR13
BR12
BR10
BR11
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
BR9
BR8
BR7
BR6
BR5
BR4
BR3
BR2
BR1
BR0
0x28 (where x = A..E,H)
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
9.1 Introduction
The devices feature a set of configuration registers. The main purposes of the system
configuration controller are the following:
• Remapping memories
• Remapping some trigger sources to timer input capture channels
• Managing external interrupts line multiplexing to the internal edge detector
• Enabling dedicated functions such as input capture multiplexing or oscillator pin
remapping
• I2C Fm+ mode management
• Firewall management
• Temperature sensor and Internal voltage reference management (including for
Comparator and ADC purposes).
The Cortex®-M0+ can wake up from WFE (Wait For Event) when a transition occurs on the
eventin input signal. To support semaphore management in multiprocessor environment,
the core can also output events on the signal output EVENTOUT, during SEV instruction
execution.
In STM32L0x1 devices, an event input can be generated by an external interrupt line or by
an RTC alarm interrupt. It is also possible to select which output pin is connected to the
EVENTOUT signal of the Cortex®-M0+. The EVENTOUT multiplexing is managed by the
GPIO alternate function capability (see Section 8.4.9: GPIO alternate function low register
(GPIOx_AFRL) (x = A to E and H) and Section 8.4.10: GPIO alternate function high register
(GPIOx_AFRH) (x = A to E and H)).
Note: EVENTOUT is not mapped on all GPIOs (for example PC13, PC14, PC15).
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. Res. Res. Res. Res. Res. BOOT_MODE Res. Res. Res. Res. UFB Res. MEM_MODE
r r rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
I2C3_ I2C_PB9 I2C_PB8 I2C_PB7 I2C_PB6
Res. I2C2_FMP I2C1_FMP Res. Res. Res. FWDIS
FMP _FMP _FMP _FMP _FMP
rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
REF_ VREFINT
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
LOCK _RDYF
rs r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
ENBUF_ ENBUF_ ENBUF_
SEL_VREF EN_VR
Res. Res. Res. VREFINT_ Res. Res. SENSOR VREFINT Res. Res. Res. Res. Res.
_OUT EFINT
COMP2 _ADC _ ADC
rw rw rw rw rw rw
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
EXTI3[3:0] EXTI2[3:0] EXTI1[3:0] EXTI0[3:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
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
EXTI7[3:0] EXTI6[3:0] EXTI5[3:0] EXTI4[3:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
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
EXTI11[3:0] EXTI10[3:0] EXTI9[3:0] EXTI8[3:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
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
EXTI15[3:0] EXTI14[3:0] EXTI13[3:0] EXTI12[3:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Offset Register
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
10
11
9
8
7
6
5
4
3
2
1
0
BOOT_MODE
MEM_MODE
UFB.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
SYSCFG_CFGR1
0x00
Reset value x x x x x
I2C_PB9_FMP
I2C_PB8_FMP
I2C_PB7_FMP
I2C_PB6_FMP
I2C3_FMP
I2C2_FMP
I2C1_FMP
FWDISEN
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
0x04 SYSCFG_CFGR2
Reset value 0 0 0 0 0 0 0 1
SYSCFG_
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Offset Register
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
10
11
9
8
7
6
5
4
3
2
1
0
SYSCFG_
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
EXTI11[3:0] EXTI10[3:0] EXTI9[3:0] EXTI8[3:0]
0x10 EXTICR3
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
SYSCFG_
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
EXTI15[3:0] EXTI14[3:0] EXTI13[3:0] EXTI12[3:0]
0x14 EXTICR4
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x18 COMP1_CTRL
Refer to Section 14: Comparator (COMP)
0x1C COMP2_CTRL
ENBUF_VREFINT_COMP2
ENBUF_VREFINT_ADC
ENBUF_SENSOR_ADC
SEL_VREF_OUT
VREFINT_RDYF
EN_VREFINT
REF_LOCK
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
SYSCFG_CFGR3
0x20
Reset value 0 0 0 0 0 0 0 0 0
10.1 Introduction
The direct memory access (DMA) controller is a bus master and system peripheral.
The DMA is used to perform programmable data transfers between memory-mapped
peripherals and/or memories, upon the control of an off-loaded CPU.
The DMA controller features a single AHB master architecture.
There is one instance of DMA with up to 7 channels, except for the category 1 devices that
only feature 5 channels.
Each channel is dedicated to managing memory access requests from one or more
peripherals. The DMA includes an arbiter for handling the priority between DMA requests.
10.3.1 DMA
DMA is implemented with the hardware configuration parameters shown in the table below.
High priority
ADC, TIM2_CH3, AES_IN
SW trigger 1
(MEM2MEM bit) C1S
4
ADC, SPI1_RX, USART1_TX,
LPUART1_TX, I2C1_TX, I2C3_TX,
TIM2_UP, TIM6_UP,
AES_OUT, TIM3_CH3,
USART4_RX, USART5_RX,
USART2_TX(1) C2S
SW trigger 2
(MEM2MEM bit) 4
SPI1_TX, USART1_RX,I2C3_RX
LPUART1_RX, I2C1_RX,
TIM2_CH2, TIM3_CH4,TIM3_UP,
USART4_TX,USART5_TX,
USART2_RX(1) , AES_OUT C3S
SW trigger 3
(MEM2MEM bit) 4 Internal DMA
SPI2_RX, USART1_TX,
request
USART2_TX, I2C2_TX, I2C3_TX,
(1)
ADC , SPI_RX(1), LPUART1_TX(1),
I2C1_TX(1), TIM2_CH4,TIM7_UP C4S
SW trigger 4
(MEM2MEM bit) 4
SPI2_TX, USART1_RX,
USART2_RX, I2C2_RX, DMA_CSELR
TIM2_CH1, TIM3_CH1, I2C3_RX, ,
SPI_TX(1), LPUART1_RX(1),
I2C1_RX(1), AES_IN
SW trigger 5 C5S
(MEM2MEM bit)
4
SPI2_RX, USART2_RX,
LPUART1_RX, I2C1_TX,
TIM3_TRIG, USART4_RX,
USART5_RX C6S
SW trigger 6
(MEM2MEM bit) 4
SPI2_TX, USART2_TX,
USART4_TX, USART5_TX,
LPUART1_TX, I2C1_RX,
TIM2_CH2, TIM2_CH4
C7S Low priority
SW trigger 7
(MEM2MEM bit) 4
MSv34755V5
FLITF Flash
System
Cortex-
M0+
Bus matrix
SRAM
DMA Reset &
Ch.1
Clock control
Ch.2 DMA CRC
(RCC)
up to
Ch.7 Bridge
Arbiter APB
AHB Slave
MSv34758V2
The DMA controller performs direct memory transfer by sharing the AHB system bus with
other system masters. The bus matrix implements round-robin scheduling. DMA requests
may stop the CPU access to the system bus for a number of bus cycles, when CPU and
DMA target the same destination (memory or peripheral).
According to its configuration through the AHB slave interface, the DMA controller arbitrates
between the DMA channels and their associated received requests. The DMA controller
also schedules the DMA data transfers over the single AHB port master.
The DMA controller generates an interrupt per channel to the interrupt controller.
Pointer incrementation
The peripheral and memory pointers may be automatically incremented after each transfer,
depending on the PINC and MINC bits of the DMA_CCRx register.
If the incremented mode is enabled (PINC or MINC set to 1), the address of the next
transfer is the address of the previous one incremented by 1, 2 or 4, depending on the data
size defined in PSIZE[1:0] or MSIZE[1:0]. The first transfer address is the one programmed
in the DMA_CPARx or DMA_CMARx register. During transfers, these registers keep the
initially programmed value. The current transfer addresses (in the current internal
peripheral/memory address register) are not accessible by software.
If the channel x is configured in non-circular mode, no DMA request is served after the last
data transfer (once the number of single data to transfer reaches zero). The DMA channel
must be disabled in order to reload a new number of data items into the DMA_CNDTRx
register.
Note: If the channel x 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 data 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 and DMA_CMARx registers.
register content may not correctly reflect the remaining data transfers versus the
aborted source and destination buffer/register.
• Abort and restart a channel
This corresponds to the software sequence: disable an active channel, then
reconfigure the channel and enable it again.
This is supported by the hardware if the following conditions are met:
– The application guarantees that, when the software is disabling the channel, a
DMA data transfer is not occurring at the same time over its master port. For
example, the application can first disable the peripheral in DMA mode, in order to
ensure that there is no pending hardware DMA request from this peripheral.
– The software must operate separated write accesses to the same DMA_CCRx
register: First disable the channel. Second reconfigure the channel for a next block
transfer including the DMA_CCRx if a configuration change is needed. There are
read-only DMA_CCRx register fields when DMA_CCRx.EN=1. Finally enable
again the channel.
When a channel transfer error occurs, the EN bit of the DMA_CCRx register is cleared by
hardware. This EN bit can not be set again by software to re-activate the channel x, until the
TEIFx bit of the DMA_ISR register is set.
Memory-to-memory mode
The DMA channels may operate without being triggered by a request from a peripheral. This
mode is called memory-to-memory mode, and is initiated by software.
If the MEM2MEM bit in the DMA_CCRx register is set, the channel, if enabled, initiates
transfers. The transfer stops once the DMA_CNDTRx register reaches zero.
Note: The memory-to-memory mode must not be used in circular mode. Before enabling a
channel in memory-to-memory mode (MEM2MEM = 1), the software must clear the CIRC
bit of the DMA_CCRx register.
Peripheral-to-peripheral mode
Any DMA channel can operate in peripheral-to-peripheral mode:
• when the hardware request from a peripheral is selected to trigger the DMA channel
This peripheral is the DMA initiator and paces the data transfer from/to this peripheral
to/from a register belonging to another memory-mapped peripheral (this one being not
configured in DMA mode).
• when no peripheral request is selected and connected to the DMA channel
The software configures a register-to-register transfer by setting the MEM2MEM bit of
the DMA_CCRx register.
Table 50. Programmable data width and endian behavior (when PINC = MINC = 1)
Source Destinat
port ion port Destination
Number Source content:
width width content:
of data address / data
(MSIZE (PSIZE address / data
items to (DMA_CMARx if DMA transfers
if if (DMA_CPARx if
transfer DIR = 1, else
DIR = 1, DIR = 1, DIR = 1, else
(NDT) DMA_CPARx)
else else DMA_CMARx)
PSIZE) MSIZE)
@0x0 / B0 1: read B0[7:0] @0x0 then write 00B0[15:0] @0x0 @0x0 / 00B0
@0x1 / B1 2: read B1[7:0] @0x1 then write 00B1[15:0] @0x2 @0x2 / 00B1
8 16 4
@0x2 / B2 3: read B2[7:0] @0x2 then write 00B2[15:0] @0x4 @0x4 / 00B2
@0x3 / B3 4: read B3[7:0] @0x3 then write 00B3[15:0] @0x6 @0x6 / 00B3
@0x0 / B0 1: read B0[7:0] @0x0 then write 000000B0[31:0] @0x0 @0x0 / 000000B0
@0x1 / B1 2: read B1[7:0] @0x1 then write 000000B1[31:0] @0x4 @0x4 / 000000B1
8 32 4
@0x2 / B2 3: read B2[7:0] @0x2 then write 000000B2[31:0] @0x8 @0x8 / 000000B2
@0x3 / B3 4: read B3[7:0] @0x3 then write 000000B3[31:0] @0xC @0xC / 000000B3
@0x0 / B1B0 1: read B1B0[15:0] @0x0 then write B0[7:0] @0x0 @0x0 / B0
@0x2 / B3B2 2: read B3B2[15:0] @0x2 then write B2[7:0] @0x1 @0x1 / B2
16 8 4
@0x4 / B5B4 3: read B5B4[15:0] @0x4 then write B4[7:0] @0x2 @0x2 / B4
@0x6 / B7B6 4: read B7B6[15:0] @0x6 then write B6[7:0] @0x3 @0x3 / B6
@0x0 / B1B0 1: read B1B0[15:0] @0x0 then write B1B0[15:0] @0x0 @0x0 / B1B0
@0x2 / B3B2 2: read B3B2[15:0] @0x2 then write B3B2[15:0] @0x2 @0x2 / B3B2
16 16 4
@0x4 / B5B4 3: read B5B4[15:0] @0x4 then write B5B4[15:0] @0x4 @0x4 / B5B4
@0x6 / B7B6 4: read B7B6[15:0] @0x6 then write B7B6[15:0] @0x6 @0x6 / B7B6
@0x0 / B1B0 1: read B1B0[15:0] @0x0 then write 0000B1B0[31:0] @0x0 @0x0 / 0000B1B0
@0x2 / B3B2 2: read B3B2[15:0] @0x2 then write 0000B3B2[31:0] @0x4 @0x4 / 0000B3B2
16 32 4
@0x4 / B5B4 3: read B5B4[15:0] @0x4 then write 0000B5B4[31:0] @0x8 @0x8 / 0000B5B4
@0x6 / B7B6 4: read B7B6[15:0] @0x6 then write 0000B7B6[31:0] @0xC @0xC / 0000B7B6
@0x0 / B3B2B1B0 1: read B3B2B1B0[31:0] @0x0 then write B0[7:0] @0x0 @0x0 / B0
@0x4 / B7B6B5B4 2: read B7B6B5B4[31:0] @0x4 then write B4[7:0] @0x1 @0x1 / B4
32 8 4
@0x8 / BBBAB9B8 3: read BBBAB9B8[31:0] @0x8 then write B8[7:0] @0x2 @0x2 / B8
@0xC / BFBEBDBC 4: read BFBEBDBC[31:0] @0xC then write BC[7:0] @0x3 @0x3 / BC
@0x0 / B3B2B1B0 1: read B3B2B1B0[31:0] @0x0 then write B1B0[15:0] @0x0 @0x0 / B1B0
@0x4 / B7B6B5B4 2: read B7B6B5B4[31:0] @0x4 then write B5B4[15:0] @0x2 @0x2 / B5B4
32 16 4
@0x8 / BBBAB9B8 3: read BBBAB9B8[31:0] @0x8 then write B9B8[15:0] @0x4 @0x4 / B9B8
@0xC / BFBEBDBC 4: read BFBEBDBC[31:0] @0xC then write BDBC[15:0] @0x6 @0x6 / BDBC
@0x0 / B3B2B1B0 1: read B3B2B1B0[31:0] @0x0 then write B3B2B1B0[31:0] @0x0 @0x0 / B3B2B1B0
@0x4 / B7B6B5B4 2: read B7B6B5B4[31:0] @0x4 then write B7B6B5B4[31:0] @0x4 @0x4 / B7B6B5B4
32 32 4
@0x8 / BBBAB9B8 3: read BBBAB9B8[31:0] @0x8 then write BBBAB9B8[31:0] @0x8 @0x8 / BBBAB9B8
@0xC / BFBEBDBC 4: read BFBEBDBC[31:0] @0xC then write BFBEBDBC[31:0] @0xC @0xC / BFBEBDBC
Assuming the AHB/APB bridge is an AHB 32-bit slave peripheral that does not take into
account the HSIZE data, any AHB byte or half-word transfer is changed into a 32-bit APB
transfer as described below:
• An AHB byte write transfer of 0xB0 to one of the 0x0, 0x1, 0x2 or 0x3 addresses, is
converted to an APB word write transfer of 0xB0B0B0B0 to the 0x0 address.
• An AHB half-word write transfer of 0xB1B0 to the 0x0 or 0x2 addresses, is converted to
an APB word write transfer of 0xB1B0B1B0 to the 0x0 address.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. 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
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
CHTIF7
CTCIF7
CHTIF6
CTCIF6
CHTIF5
CTCIF5
CTEIF7
CTEIF6
CTEIF5
CGIF7
CGIF6
CGIF5
Res. Res. Res. Res.
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
CHTIF4
CTCIF4
CHTIF3
CTCIF3
CHTIF2
CTCIF2
CHTIF1
CTCIF1
CTEIF4
CTEIF3
CTEIF2
CTEIF1
CGIF4
CGIF3
CGIF2
CGIF1
w w w w w w w w w w w w w w w w
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
MEM2
Res. PL[1:0] MSIZE[1:0] PSIZE[1:0] MINC PINC CIRC DIR TEIE HTIE TCIE EN
MEM
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
NDT[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
PA[31:16]
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
PA[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
MA[31:16]
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
MA[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. C7S[3:0] C6S[3:0] C5S[3: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
C4S[3:0] C3S[3:0] C2S[3:0] C1S[3:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Offset Register
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
10
11
9
8
7
6
5
4
3
2
1
0
HTIF7
TCIF7
HTIF6
TCIF6
HTIF5
TCIF5
HTIF4
TCIF4
HTIF3
TCIF3
HTIF2
TCIF2
HTIF1
TCIF1
TEIF7
TEIF6
TEIF5
TEIF4
TEIF3
TEIF2
TEIF1
GIF7
GIF6
GIF5
GIF4
GIF3
GIF2
GIF1
Res.
Res.
Res.
Res.
DMA_ISR
0x000
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
CTCIF7
CTCIF6
CTCIF5
CTCIF4
CTCIF3
CTCIF2
CTCIF1
CHTIF7
CHTIF6
CHTIF5
CHTIF4
CHTIF3
CHTIF2
CHTIF1
CTEIF7
CTEIF6
CTEIF5
CTEIF4
CTEIF3
CTEIF2
CTEIF1
CGIF7
CGIF6
CGIF5
CGIF4
CGIF3
CGIF2
CGIF1
Res.
Res.
Res.
Res.
DMA_IFCR
0x004
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
MEM2MEM
MSIZE[1:0]
PSIZE[1:0]
PL[1:0]
MINC
CIRC
PINC
HTIE
TCIE
TEIE
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
DIR
EN
DMA_CCR1
0x008
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Offset Register
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
10
11
9
8
7
6
5
4
3
2
1
0
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
DMA_CNDTR1 NDTR[15:0]
0x00C
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CPAR1 PA[31:0]
0x010
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
DMA_CMAR1 MA[31:0]
0x014
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 Reserved.
MEM2MEM
MSIZE[1:0]
PSIZE[1:0]
PL[1:0]
MINC
CIRC
PINC
HTIE
TCIE
TEIE
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
DIR
EN
DMA_CCR2
0x01C
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
DMA_CNDTR2 NDTR[15:0]
0x020
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CPAR2 PA[31:0]
0x024
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
DMA_CMAR2 MA[31:0]
0x028
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 Reserved.
MEM2MEM
MSIZE[1:0]
PSIZE[1:0]
PL[1:0]
MINC
CIRC
PINC
HTIE
TCIE
TEIE
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
DIR
EN
DMA_CCR3
0x030
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
DMA_CNDTR3 NDTR[15:0]
0x034
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CPAR3 PA[31:0]
0x038
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
DMA_CMAR3 MA[31:0]
0x03C
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 Reserved.
MEM2MEM
MSIZE[1:0]
PSIZE[1:0]
PL[1:0]
MINC
CIRC
PINC
HTIE
TCIE
TEIE
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
DIR
EN
DMA_CCR4
0x044
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
DMA_CNDTR4 NDTR[15:0]
0x048
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CPAR4 PA[31:0]
0x04C
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
DMA_CMAR4 MA[31:0]
0x050
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 Reserved.
MEM2MEM
MSIZE[1:0]
PSIZE[1:0]
PL[1:0]
MINC
CIRC
PINC
HTIE
TCIE
TEIE
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
DIR
EN
DMA_CCR5
0x058
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
DMA_CNDTR5 NDTR[15:0]
0x05C
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CPAR5 PA[31:0]
0x060
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
Offset Register
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
10
11
9
8
7
6
5
4
3
2
1
0
DMA_CMAR5 MA[31:0]
0x064
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 Reserved.
MEM2MEM
MSIZE[1:0]
PSIZE[1:0]
PL[1:0]
MINC
CIRC
PINC
HTIE
TCIE
TEIE
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
DIR
EN
DMA_CCR6
0x06C
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
DMA_CNDTR6 NDTR[15:0]
0x070
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CPAR6 PA[31:0]
0x074
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
DMA_CMAR6 MA[31:0]
0x078
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 Reserved.
MEM2MEM
MSIZE[1:0]
PSIZE[1:0]
PL[1:0]
MINC
CIRC
PINC
HTIE
TCIE
TEIE
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
DIR
EN
DMA_CCR7
0x080
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
DMA_CNDTR7 NDTR[15:0]
0x084
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CPAR7 PA[31:0]
0x088
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
DMA_CMAR7 MA[31:0]
0x08C
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 to
Reserved Reserved.
0x0A4
Res.
Res.
Res.
Res.
- - - Reserved 0x0000_0000
-3 fixed Reset Reset 0x0000_0004
Non maskable interrupt. The RCC
-2 fixed NMI_Handler Clock Security System (CSS) is 0x0000_0008
linked to the NMI vector.
-1 fixed HardFault_Handler All class of fault 0x0000_000C
0x0000_0010 -
- - - Reserved
0x0000_002B
System service call via SWI
3 settable SVC_Handler 0x0000_002C
instruction
0x0000_0030 -
- - - Reserved
0x0000_0037
5 settable PendSV_Handler Pendable request for system service 0x0000_0038
6 settable SysTick_Handler System tick timer 0x0000_003C
12.1 Introduction
The extended interrupts and events controller (EXTI) manages the external and internal
asynchronous events/interrupts and generates the event request to the CPU/interrupt
controller plus a wake-up request to the power controller.
The EXTI allows the management of up to 30 event lines which can wake up the device
from Stop mode.
Some of the lines are configurable: in this case the active edge can be chosen
independently, and a status flag indicates the source of the interrupt. The configurable lines
are used by the I/Os external interrupts, and by few peripherals. Some of the lines are
direct: they are used by some peripherals to generate a wakeup from Stop event or
interrupt. In this case the status flag is provided by the peripheral.
Each line can be masked independently for interrupt or event generation.
Te EXTI controller also allows to emulate, by programming to a dedicated register, events or
interrupts by software multiplexed with the corresponding hardware event line.
Note: The interrupts or events associated to the direct lines are triggered only when the system is
in Stop mode. If the system is still running, no interrupt/event is generated by the EXTI.
Figure 27. Extended interrupts and events controller (EXTI) block diagram
APB bus
Interrupts
Configurable Edge detect
events circuit
Events
Rising
Stop mode
Direct edge
events detect.
Wakeup
MSv32798V1
PA0 EXTI0
PB0
PC0
PA1
EXTI1
PB1
PC1
PA2
PB2 EXTI2
PC2
PD2
PA15
PB15 EXTI15
PC15
MSv34757V1
Note: Refer to the datasheet for the list of available I/O ports.
The 30 lines are connected as shown in Table 54: EXTI lines connections:
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. IM29 IM28 Res. IM26 IM25 IM24 IM23 IM22 IM21 IM20 IM19 Res. IM17 IM16
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
IM15 IM14 IM13 IM12 IM11 IM10 IM9 IM8 IM7 IM6 IM5 IM4 IM3 IM2 IM1 IM0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. EM29 EM28 Res. EM26 EM25 EM24 EM23 EM22 EM21 EM20 EM19 Res. EM17 EM16
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
EM15 EM14 EM13 EM12 EM11 EM10 EM9 EM8 EM7 EM6 EM5 EM4 EM3 EM2 EM1 EM0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. RT22 RT21 RT20 RT19 Res. RT17 RT16
rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 RT16 2 1 0
RT15 RT14 RT13 RT12 RT11 RT10 RT9 RT8 RT7 RT6 RT5 RT4 RT3 RT2 RT1 RT0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Note: The configurable wakeup lines are edge triggered, no glitch must be generated on these
lines.
If a rising edge on the configurable interrupt line occurs while writing to the EXTI_RTSR
register, the pending bit will not be set.
Rising and falling edge triggers can be set for the same interrupt line. In this configuration,
both generate a trigger condition.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. FT22 FT21 FT20 FT19 Res. FT17 FT16
rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
FT15 FT14 FT13 FT12 FT11 FT10 FT9 FT8 FT7 FT6 FT5 FT4 FT3 FT2 FT1 FT0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Note: The configurable wakeup lines are edge triggered, no glitch must be generated on these
lines.
If a falling edge on the configurable interrupt line occurs while writing to the EXTI_FTSR
register, the pending bit will not be set.
Rising and falling edge triggers can be set for the same interrupt line. In this configuration,
both generate a trigger condition.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. SWI22 SWI21 SWI20 SWI19 Res. SWI17 SWI16
rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
SWI15 SWI14 SWI13 SWI12 SWI11 SWI10 SWI9 SWI8 SWI7 SWI6 SWI5 SWI4 SWI3 SWI2 SWI1 SWI0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. PIF22 PIF21 PIF20 PIF19 Res. PIF17 PIF16
rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
PIF15 PIF14 PIF13 PIF12 PIF11 PIF10 PIF9 PIF8 PIF7 PIF6 PIF5 PIF4 PIF3 PIF2 PIF1 PIF0
rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1
Table 55. Extended interrupt/event controller register map and reset values
Offset Register
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
10
11
9
8
7
6
5
4
3
2
1
0
IM[29:28]
Res.
Res.
Res.
Res.
EXTI_IMR IM[26:19] IM[17:0]
0x00
Reset value 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
EM[29:28]
Res.
Res.
Res.
Res.
EXTI_EMR EM[26:19] EM[17:0]
0x04
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
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
EXTI_SWIER SWI[17:0]
0x10 [22:19]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
PIF
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
EXTI_PR PIF[17:0]
0x14 [22:19]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
13.1 Introduction
The 12-bit ADC is a successive approximation analog-to-digital converter. It has up to 18
multiplexed channels allowing it to measure signals from 16 external and 2 internal sources.
A/D conversion of the various channels can be performed in single, continuous, scan or
discontinuous mode. The result of the ADC is stored in a left-aligned or right-aligned 16-bit
data register.
The analog watchdog feature allows the application to detect if the input voltage goes
outside the user-defined higher or lower thresholds.
An efficient low-power mode is implemented to allow very low consumption at low
frequency.
A built-in hardware oversampler allows analog performances to be improved while off-
loading the related computational burden from the CPU.
Analog supply
SCANDIR up/ 1.8 to 3.6 V AREADY
down EOSMP ADC interrupt
AUTOFF auto-off EOS IRQ
CH_SEL[18:0] EOC CPU
mode
CONT single/ ADEN/ADDIS OVR master
DATA[15:0] AWD
cont.
AHB
AHB
to slave
Supply and APB
VREFINT ADCAL self- reference master
APB
calibration DMA
Input interface
VSENSE
selection SAR ADC
VIN DMA request
& scan
ADC_IN[15:0] control DMAEN
VIN[x] DMACFG
SMP[2:0] Converted data Over-
sampling time start sampler
TRG0
TRG1
TRG2
H/W
TRG3 trigger DISCEN
TRG4 discontinuous
EXTEN[1:0]
TRG5 mode
trigger enable
TRG6 and edge selection
TRG7
EXTSEL[2:0]
trigger selection
MSv34764V7
1. TRGi are mapped at product level. Refer to Table External triggers in Section 13.3.1: ADC pins and internal signals.
Input, analog power Analog power supply and positive reference voltage
VDDA
supply for the ADC
Input, analog supply
VSSA Ground for analog power supply
ground
ADC_INx Analog input signals Up to 16 external analog input channels
The software must follow the procedure described below to manage the ADC in low-power
mode:
1. Make sure that the ADC is disabled (ADEN = 0).
2. Write ADVREGEN = 0.
3. Enter low-power mode.
4. Resume from low-power mode.
5. Check that REGLPF = 0.
6. Enable the ADC voltage regulator by using the sequence described in Section :
ADVREG enable sequence (ADVREGEN = 1 in ADC_CR).
7. Write ADC_CR ADEN = 1 and wait until ADC_CR ADRDY = 1.
8. Write ADRDY = 1 to clear it.
It is still possible to save and restore the calibration factor by software to save time when
re-starting the ADC (as long as temperature and voltage are stable during the ADC power
down).
The calibration factor can be written if the ADC is enabled but not converting (ADEN = 1 and
ADSTART = 0). Then, at the next start of conversion, the calibration factor is automatically
injected into the analog ADC. This loading is transparent and does not add any cycle
latency to the start of the conversion.
Software calibration procedure
1. Ensure that ADEN = 0 and DMAEN = 0.
2. Set ADCAL = 1.
3. Wait until ADCAL = 0 (or until EOCAL = 1). This can be handled by interrupt if the
interrupt is enabled by setting the EOCALIE bit in the ADC_IER register. The ADCAL
bit can remain set for some time even after EOCAL has been set. As a result, the
software must wait for ADCAL = 0 after EOCAL = 1 to be able to set ADEN = 1 for next
ADC conversions.
4. The calibration factor can be read from bits 6:0 of ADC_DR or ADC_CALFACT
registers.
For code example, refer to A.8.1: Calibration code example.
ADCAL
CALIBRATION
ADC_DR[6:0] 0x00 FACTOR
ADC_CALFACT[6:0]
by S/W by H/W
MS33703V1
If the ADC voltage regulator was not previously set, it is automatically enabled when setting
ADCAL = 1 (bit ADVREGEN is automatically set by hardware). In this case, the ADC
calibration time is longer to take into account the stabilization time of the ADC voltage
regulator.
At the end of the calibration, the ADC voltage regulator remains enabled.
ADC state Ready (not converting) Converting channel Ready Converting channel
Updating (Single ended) (Single ended)
calibration
Internal
calibration factor[6:0] F1 F2
Start conversion
(hardware or software)
WRITE ADC_CALFACT
CALFACT[6:0] F2
by S/W by H/W
MS31925V1
ADEN
t STAB
ADR DY
ADDIS
ADC
OFF Startup RDY CONVERTING CH RDY REQ
stat -OF OFF
by S/W by H/W
MS30264V2
Note: In Auto-off mode (AUTOFF = 1) the power-on/off phases are performed automatically, by
hardware and the ADRDY flag is not set.
RCC ADITF
(Reset & Clock Controller)
APB interface
PCLK
Bits CKMODE[1:0]
of ADC_CFGR2
/1 or /2 or /4 Others
Analog ADC_CK Analog
ADC
00
ADC /1,2,4,6,8,10,12
asynchronous 16,32,64,128,256
clock Bits CKMODE[1:0]
of ADC_CFGR2
Bits PRESC[3:0]
of ADC_CCR
MSv31926V2
1. Refer to Section Reset and clock control (RCC) for how the PCLK clock and ADC asynchronous clock are
enabled.
The input clock of the analog ADC can be selected between two different clock sources (see
Figure 33: ADC clock scheme to see how the PCLK clock and the ADC asynchronous clock
are enabled):
a) The ADC clock can be a specific clock source, named “ADC asynchronous clock“
which is independent and asynchronous with the APB clock.
Refer to RCC Section for more information on generating this clock source.
To select this scheme, bits CKMODE[1:0] of the ADC_CFGR2 register must be
reset.
b) The ADC clock can be derived from the APB clock of the ADC bus interface,
divided by a programmable factor (1, 2 or 4) according to bits CKMODE[1:0].
To select this scheme, bits CKMODE[1:0] of the ADC_CFGR2 register must be
different from “00”.
For code example, refer to A.8.4: ADC clock selection code example.
In option a), the generated ADC clock can eventually be divided by a prescaler (1, 2, 4, 6, 8,
10, 12, 16, 32, 64, 128, 256) when programming the bits PRESC[3:0] in the ADC_CCR
register).
Option a) has the advantage of reaching the maximum ADC clock frequency whatever the
APB clock scheme selected.
Option b) has the advantage of bypassing the clock domain resynchronizations. This can be
useful when the ADC is triggered by a timer and if the application requires that the ADC is
precisely triggered without any uncertainty (otherwise, an uncertainty of the trigger instant is
added by the resynchronizations between the two clock domains).
Table 59. Latency between trigger and start of conversion(1)
Latency between the trigger event
ADC clock source CKMODE[1:0]
and the start of conversion
Caution: When selecting CKMODE[1:0] = 11 (PCLK divided by 1), the user must ensure that the
PCLK has a 50% duty cycle. This is done by selecting a system clock with a 50% duty cycle
and configuring the APB prescaler in bypass modes in the RCC (refer to there Reset and
clock controller section). If an internal source clock is selected, the AHB and APB prescalers
do not divide the clock.
Low frequency
When selecting an analog ADC clock frequency lower than 3.5 MHz, it is mandatory to first
enable the Low Frequency Mode by setting bit LFMEN = 1 into the ADC_CCR register
STM32L0x1
ADC
Channel selection
VIN[0] Fast channel
ADC_IN0
VIN[1]
ADC_IN1
VIN[2]
ADC_IN2
VIN[3]
ADC_IN3
VIN[6]
ADC_IN6
VIN[7] VREF+
ADC_IN7
VIN[8]
ADC_IN8
VIN
SAR
VIN[9]
ADC_IN9 ADC1
VIN[10]
ADC_IN10
VIN[11] VREF-
ADC_IN11
VIN[12]
ADC_IN12
VIN[13]
ADC_IN13
VIN[14]
ADC_IN14
VIN[15]
ADC_IN15
VIN[16]
Reserved
VIN[17]
VREFINT
VIN[18]
VSENSE
MSv68734V1
the need for software having to set the ADSTART bit again and ensures the next trigger
event is not missed.
13.3.13 Timings
The elapsed time between the start of a conversion and the end of conversion is the sum of
the configured sampling time plus the successive approximation time depending on data
resolution:
tCONV = tSMPL + tSAR = 93.8 ns |min + 781.3 ns |12bit = 0.875 µs |min (for fADC_CLK = 16 MHz)
(3) (3)
WLATENCY WLATENCY WLATENCY (3)
ADC_DR
MSv33174V1
1. EXTEN = 00 or EXTEN ≠ 00
2. Trigger latency (refer to datasheet for more details)
3. ADC_DR register write latency (refer to datasheet for more details)
set by SW cleared by HW
ADSTOP
ADC_DR DATA N-1
MS30337V1
Note: The polarity of the external trigger can be changed only when the ADC is not converting
(ADSTART = 0).
The EXTSEL[2:0] control bits are used to select which of 8 possible events can trigger
conversions.
Refer to Table 58: External triggers in Section 13.3.1: ADC pins and internal signals for the
list of all the external triggers that can be used for regular conversion.
The software source trigger events can be generated by setting the ADSTART bit in the
ADC_CR register.
Note: The trigger selection can be changed only when the ADC is not converting (ADSTART = 0).
ADSTART(1)
EOC
EOS
SCANDIR
ADC state(2) RDY CH0 CH9 CH10 CH17 RDY CH17 CH10 CH9 CH0 RDY
by S/W by H/W
MSv30338V3
ADSTART(1)
EOC
EOS
ADSTP
SCANDIR
ADC state(2) RDY CH0 CH9 CH10 CH17 CH0 CH9 CH10 STP RDY CH17 CH10
by S/W by H/W
MSv30339V2
ADSTART(1)
EOC
EOS
TRGx(1)
ADC state(2) RDY CH0 CH1 CH2 CH3 RDY CH0 CH1 CH2 CH3 RDY
ADC_DR D0 D1 D2 D3 D0 D1 D2 D3
by S/W by H/W
triggered ignored
MSv30340V2
ADSTART(1)
EOC
EOS
ADSTP
TRGx(1)
ADC state(2) RDY CH0 CH1 CH2 CH3 CH0 CH1 CH2 CH3 CH0 STOP RDY
ADC_DR D0 D1 D2 D3 D0 D1 D2 D3
by S/W by H/W
triggered ignored
MSv30341V2
ALIGN RES 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
MS30342V1
ADSTART(1)
EOC
EOS
OVR
ADSTP
TRGx(1)
ADC state(2)
RDY CH0 CH1 CH2 CH0 CH1 CH2 CH0 STOP RDY
ADC_DR
(OVRMOD=0) D0 D1 D2 D0
ADC_DR
(OVRMOD=1) D0 D1 D2 D0 D1 D2
by S/W by H/W
triggered
MSv30343V3
13.5.4 Managing converted data without using the DMA without overrun
It may be useful to let the ADC convert one or more channels without reading the data after
each conversion. In this case, the OVRMOD bit must be configured at 1 and the OVR flag
should be ignored by the software. When OVRMOD = 1, an overrun event does not prevent
the ADC from continuing to convert and the ADC_DR register always contains the latest
conversion data.
When DMA mode is enabled (DMAEN bit set in the ADC_CFGR1 register), a DMA request
is generated after the conversion of each channel. This allows the transfer of the converted
data from the ADC_DR register to the destination location selected by the software.
Note: The DMAEN bit in the ADC_CFGR1 register must be set after the ADC calibration phase.
Despite this, if an overrun occurs (OVR = 1) because the DMA could not serve the DMA
transfer request in time, the ADC stops generating DMA requests and the data
corresponding to the new conversion is not transferred by the DMA. Which means that all
the data transferred to the RAM can be considered as valid.
Depending on the configuration of OVRMOD bit, the data is either preserved or overwritten
(refer to Section 13.5.2: ADC overrun (OVR, OVRMOD) on page 297).
The DMA transfer requests are blocked until the software clears the OVR bit.
Two different DMA modes are proposed depending on the application use and are
configured with bit DMACFG in the ADC_CFGR1 register:
• DMA one shot mode (DMACFG = 0).
This mode should be selected when the DMA is programmed to transfer a fixed
number of data words.
• DMA circular mode (DMACFG = 1)
This mode should be selected when programming the DMA in circular mode.
ADSTART
EOC
EOS
ADSTP
ADC state RDY CH1 DLY CH2 DLY CH3 DLY CH1 DLY STOP RDY
ADC_DR D1 D2 D3 D1
by S/W by H/W
MSv30344V2
TRGx
EOC
EOS
ADC_DR Read
access
ADC state RDY Startup CH1 CH2 CH3 CH4 OFF Startup
ADC_DR D1 D2 D3 D4
by S/W by H/W
triggered
MSv30345V2
1. EXTSEL = TRGx, EXTEN = 01 (rising edge), CONT = x, ADSTART = 1, CHSEL = 0xF, SCANDIR = 0, WAIT = 1,
AUTOFF = 1
For code example, refer to A.8.12: Auto off and no wait mode sequence code example.
TRGx
EOC
EOS
ADC_DR Read
access DLY DLY DLY DLY
OFF
OFF
ADC state RDY Startup CH1 OFF Startup CH2 Startup CH3 OFF Startup CH1 CH2
D1 D2 D3 D4
ADC_DR
by S/W by H/W
triggered
MSv30346V2
1. EXTSEL = TRGx, EXTEN = 01 (rising edge), CONT = x, ADSTART = 1, CHSEL = 0xF, SCANDIR = 0, WAIT = 1,
AUTOFF = 1
For code example, refer to A.8.13: Auto off and wait mode sequence code example.
Table 63 shows how to configure the AWDSGL and AWDEN bits in the ADC_CFGR1
register to enable the analog watchdog on one or more channels.
Analog voltage
Guarded area
Lower threshold LTx
MS45396V1
None x 0
All channels 0 1
Single(1) channel 1 1
1. Selected by the AWDCH[4:0] bits
ADC STATE RDY Conversion1 Conversion2 Conversion3 Conversion4 Conversion5 Conversion6 Conversion7
inside outside inside outside outside outside inside
EOC FLAG
ADC_AWD1_OUT
MSv65326V1
Figure 49. ADC_AWD1_OUT signal generation (AWD flag not cleared by software)
ADC STATE RDY Conversion1 Conversion2 Conversion3 Conversion4 Conversion5 Conversion6 Conversion7
not cleared by SW
AWD FLAG
ADC_AWD1_OUT
ADC STATE Conversion1 Conversion2 Conversion1 Conversion2 Conversion1 Conversion2 Conversion1 Conversion2
EOC FLAG
EOS FLAG
Cleared Cleared
by SW by SW
AWD FLAG
ADCy_AWD1_OUT
MSv65328V1
Threshould updated
MSv65329V1
13.8 Oversampler
The oversampling unit performs data preprocessing to offload the CPU. It can handle
multiple conversions and average them into a single data with increased data width, up to
16-bit.
It provides a result with the following form, where N and M can be adjusted:
n = N–1
1
Result = ----- ×
M Conversion ( t n )
n=0
19 15 11 7 3 0
Raw 20-bit data
Shifting
15 0
Truncation
and rounding
MS31928V2
The Figure 53 gives a numerical example of the processing, from a raw 20-bit accumulated
data to the final 16-bit result.
19 15 11 7 3
Raw 20-bit data: 3 B 7 D 7
15 0
Final result after 5-bits shift
and rounding to nearest 1 D B F
MS31929V1
The Table 64 below gives the data format for the various N and M combination, for a raw
conversion data equal to 0xFFF.
Table 64. Maximum output results vs N and M. Grayed values indicates truncation
1-bit 2-bit 3-bit 4-bit 5-bit 6-bit 7-bit 8-bit
Oversa No-shift
Max shift shift shift shift shift shift shift shift
mpling OVSS =
ratio Raw data OVSS = OVSS = OVSS = OVSS = OVSS = OVSS = OVSS = OVSS =
0000
0001 0010 0011 0100 0101 0110 0111 1000
2x 0x1FFE 0x1FFE 0x0FFF 0x0800 0x0400 0x0200 0x0100 0x0080 0x0040 0x0020
4x 0x3FFC 0x3FFC 0x1FFE 0x0FFF 0x0800 0x0400 0x0200 0x0100 0x0080 0x0040
8x 0x7FF8 0x7FF8 0x3FFC 0x1FFE 0x0FFF 0x0800 0x0400 0x0200 0x0100 0x0080
16x 0xFFF0 0xFFF0 0x7FF8 0x3FFC 0x1FFE 0x0FFF 0x0800 0x0400 0x0200 0x0100
32x 0x1FFE0 0xFFE0 0xFFF0 0x7FF8 0x3FFC 0x1FFE 0x0FFF 0x0800 0x0400 0x0200
64x 0x3FFC0 0xFFC0 0xFFE0 0xFFF0 0x7FF8 0x3FFC 0x1FFE 0x0FFF 0x0800 0x0400
128x 0x7FF80 0xFF80 0xFFC0 0xFFE0 0xFFF0 0x7FF8 0x3FFC 0x1FFE 0x0FFF 0x0800
256x 0xFFF00 0xFF00 0xFF80 0xFFC0 0xFFE0 0xFFF0 0x7FF8 0x3FFC 0x1FFE 0x0FFF
sequence. New data are provided every N conversion, with an equivalent delay equal to N x
tCONV = N x (tSMPL + tSAR). The flags features are raised as following:
• the end of the sampling phase (EOSMP) is set after each sampling phase
• the end of conversion (EOC) occurs once every N conversions, when the oversampled
result is available
• the end of sequence (EOCSEQ) occurs once the sequence of oversampled data is
completed (i.e. after N x sequence length conversions total)
Trigger Trigger
MS33700V1
Main features
• Linearity: ±2 °C max., precision depending on calibration
Temperature VSENSE
Address/data bus
ADC VIN[18]
sensor
converted
data
ADC
Internal VREFINT
power block ADC VIN[17]
MS34765V2
TS_CAL2_TEMP – TS_CAL1_TEMP
Temperature ( in °C ) = ---------------------------------------------------------------------------------------------------- × ( TS_DATA – TS_CAL1 ) + TS_CAL1_TEMP
TS_CAL2 – TS_CAL1
Where:
• TS_CAL2 is the temperature sensor calibration value acquired at TS_CAL2_TEMP
(refer to the datasheet for TS_CAL2 value)
• TS_CAL1 is the temperature sensor calibration value acquired at TS_CAL1_TEMP
(refer to the datasheet for TS_CAL1 value)
• TS_DATA is the actual temperature sensor output value converted by ADC
Refer to the specific device datasheet for more information about TS_CAL1 and
TS_CAL2 calibration points.
For code example, refer to A.8.17: Temperature computation code example.
Note: The sensor has a startup time after waking from power down mode before it can output
VSENSE at the correct level. The ADC also has a startup time after power-on, so to minimize
the delay, the ADEN and TSEN bits should be set at the same time.
Calculating the actual VDDA voltage using the internal reference voltage
The VDDA power supply voltage applied to the device may be subject to variation or not
precisely known. The embedded internal voltage reference (VREFINT) and its calibration
data, acquired by the ADC during the manufacturing process at VDDA_Charac, can be used to
evaluate the actual VDDA voltage level.
The following formula gives the actual VDDA voltage supplying the device:
VDDA = VDDA_Charac x VREFINT_CAL / VREFINT_DATA
Where:
• VDDA_Charac is the value of VDDA voltage characterized at VREFINT during the
manufacturing process. It is specified in the device datasheet.
• VREFINT_CAL is the VREFINT calibration value
• VREFINT_DATA is the actual VREFINT output value converted by ADC
For applications where VDDA value is not known, you must use the internal voltage
reference and VDDA can be replaced by the expression provided in Section : Calculating the
actual VDDA voltage using the internal reference voltage, resulting in the following formula:
V DDA_Charac × VREFINT_CAL × ADC_DATA x
V CHANNELx = ----------------------------------------------------------------------------------------------------------------------
-
VREFINT_DATA × FULL_SCALE
Where:
• VDDA_Charac is the value of VDDA voltage characterized at VREFINT during the
manufacturing process. It is specified in the device datasheet.
• VREFINT_CAL is the VREFINT calibration value
• ADC_DATAx is the value measured by the ADC on channelx (right-aligned)
• VREFINT_DATA is the actual VREFINT output value converted by the ADC
• full_SCALE is the maximum digital value of the ADC output. For example with 12-bit
resolution, it is 212 - 1 = 4095 or with 8-bit resolution, 28 - 1 = 255.
Note: If ADC measurements are done using an output format other than 12 bit right-aligned, all the
parameters must first be converted to a compatible format before the calculation is done.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. Res. Res. Res. EOCAL Res. Res. Res. AWD Res. Res. OVR EOS EOC EOSMP ADRDY
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
EOCAL EOSMP ADRDY
Res. Res. Res. Res. Res. Res. Res. AWDIE Res. Res. OVRIE EOSIE EOCIE
IE IE IE
rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
ADVR
ADCAL Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
EGEN
rs rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
ADSTA
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. ADSTP Res. ADDIS ADEN
RT
rs rs rs rs
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. AWDCH[4:0] Res. Res. AWDEN AWDSGL Res. Res. Res. Res. Res. DISCEN
rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
SCAND DMAC
AUTOFF WAIT CONT OVRMOD EXTEN[1:0] Res. EXTSEL[2:0] ALIGN RES[1:0] DMAEN
IR FG
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
CKMODE[1:0] Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. Res. Res. Res. Res. Res. TOVS OVSS[3:0] OVSR[2:0] Res. OVSE
rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. SMP[2:0]
rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
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
rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
CHSEL CHSEL CHSEL
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
18 17 16
rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CHSEL CHSEL CHSEL CHSEL CHSEL CHSEL CHSEL CHSEL CHSEL CHSEL CHSEL CHSEL CHSEL CHSEL CHSEL CHSEL
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
DATA[15:0]
r r r r r r r r r r r r r r r r
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. Res. Res. Res. Res. Res. Res. Res. Res. CALFACT[6:0]
rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
VREF
Res. Res. Res. Res. Res. Res. LFMEN Res. TSEN PRESC[3:0] Res. Res.
EN
rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
Offset Register
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
10
11
9
8
7
6
5
4
3
2
1
0
EOSMP
ADRDY
EOCAL
AWD
OVR
EOC
EOS
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
ADC_ISR
0x00
Reset value 0 0 0 0 0 0 0
EOSMPIE
ADRDYIE
EOCALIE
AWDIE
OVRIE
EOCIE
EOSIE
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
ADC_IER
0x04
Reset value 0 0 0 0 0 0 0
ADVREGEN
ADSTART
ADCAL
ADSTP
ADDIS
ADEN
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
ADC_CR
0x08
Reset value 0 0 0 0 0 0
EXTEN[1:0]
OVRMOD
SCANDIR
DMACFG
AWDSGL
AUTOFF
DISCEN
AWDEN
DMAEN
ALIGN
CONT
EXTSEL RES
WAIT
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
ADC_CFGR1 AWDCH[4:0]
0x0C [2:0] [1: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
0xB4
0x3C
0x2C
0x1C
0x308
Offset
328/905
ADC_TR
ADC_DR
Reserved
Reserved
Reserved
Reserved
Reserved
Reserved
Reserved
ADC_CCR
Register
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
ADC_SMPR
ADC_CFGR2
ADC_CHSELR
ADC_CALFACT
0
Res. Res. Res. Res. Res. Res. 31
CKMODE[1:0]
0
Res. Res. Res. Res. Res. Res. 30
Res. Res. Res. Res. Res. Res. Res 29
Res. Res. Res. Res. Res. Res. Res. 28
1
Res. Res. Res. Res. Res. Res. 27
1
Res. Res. Res. Res. Res. Res. 26
Analog-to-digital converter (ADC)
0
1
LFMEN Res. Res. Res. Res. Res. 25
1
Res. Res. Res. Res. Res. Res. 24
0
TSEN Res. Res. Res. 1 Res. Res. 23
0
1
VREFEN Res. Res. Res. Res. Res. 22
0
1
0
1
0
1
0
1
RM0377 Rev 10
17
1 1
Reserved
Reserved
Reserved
Reserved
Reserved
Reserved
Reserved
0 0
0
0
0
8
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Res. CHSEL2 2
CALFACT[6:0]
0
0
0
0
OVSE
0
0
0
0
0 0 0
0 0
Res. CHSEL0 0
RM0377
RM0377 Comparator (COMP)
14 Comparator (COMP)
14.1 Introduction
STM32L0x1 devices embed two ultra-low-power comparators COMP1, and COMP2 that
can be used either as standalone devices (all terminal are available on I/Os) or combined
with the timers.
The comparators can be used for a variety of functions including:
• Wakeup from low-power mode triggered by an analog signal,
• Analog signal conditioning,
• Cycle-by-cycle current control loop when combined with a PWM output from a timer.
COMP1INNSEL COMP1
VREFINT
PA0 COMP1POLARITY
PA4 - Wakeup
PA5 EXTI line 21
GPIOx
PA1 ++ COMP1VALUE
TIM2_ETR
TIM2_CH4
COMP1WM TIM21_ETR
TIM21_CH2
TIM22_ETR
COMP2INPSEL
COMP2 TIM22_CH1
PA3 LPTIM_ETR
COMP2INNSEL PA7 (1)
LPTIM_CH2
PB4
PB5 + COMP2POLARITY Wakeup
VREFINT
PA2 PB6 EXTI line 22
PA4 PB7 GPIOx
VREFINT Scaler -
PA5 COMP2VALUE
¼ VREFINT
½ VREFINT TIM2_ETR
¾ VREFINT TIM2_CH4
PB3 TIM21_ETR
TIM21_CH2
ENBUF_ TIM22_ETR
VREFINT TIM22_CH1
_COMP2 LPTIM_ETR
LPTIM_CH2
MSv34753V3
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
COMP1 COMP1
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
LOCK VALUE
rs r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
COMP1 COMP1 COMP1 COMP1INN COMP1
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
POLARITY LPTIMIN1 WM SEL EN
rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
COMP2 COMP2
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
LOCK VALUE
rs r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
COMP2 COMP2 COMP2 COMP2 COMP2
Res. Res. COMP2INPSEL Res. COMP2INNSEL Res. Res.
POLARITY LPTIMIN1 LPTIMIN2 SPEED EN
rw rw rw rw rw rw rw rw rw rw rw
0x1C
Offset
336/905
14.5.3
Register
Reset value
Reset value
COMP2_CSR
COMP1_CSR
0
0
COMP2LOCK COMP1LOCK 31
Comparator (COMP)
0
0
COMP2VALUE COMP1VALUE 30
Res. Res. 29
Res. Res. 28
Res. Res. 27
Res. Res. 26
Res. Res. 25
COMP register map
Res. Res. 24
Res. Res. 23
Res. Res. 22
Res. Res. 21
Res. Res. 20
Res. Res. 19
Res. Res. 18
Res. Res. 17
RM0377 Rev 10
Res. Res. 16
0
0
COMP2POLARITY COMP1POLARITY 15
Res. Res. 14
0
COMP2LPTIMIN1 Res. 13
The following table summarizes the comparator registers.
0
0
COMP2LPTIMIN2 COMP1LPTIMIN1 12
Res. Res. 11
Table 67. COMP register map and reset values
Res. 10
0
COMP2INPSEL Res. 9
Refer to Section 2.2 on page 51 for the register boundary addresses.
0
0
COMP1WM 8
Res. Res. 7
0
Res. 6
0
0
COMP2INNSEL 5
COMP1INNSEL
0
0
4
0
COMP2SPEED Res. 3
Res. Res. 2
Res. Res. 1
0
0
COMP2EN COMP1EN 0
RM0377
RM0377 AES hardware accelerator (AES)
15.1 Introduction
The AES hardware accelerator (AES) encrypts or decrypts data, using an algorithm and
implementation fully compliant with the advanced encryption standard (AES) defined in
Federal information processing standards (FIPS) publication 197.
Multiple chaining modes are supported (ECB, CBC, CTR), for key size of 128 bits.
The AES accelerator is a 32-bit AHB peripheral. It supports DMA single transfers for
incoming and outgoing data (two DMA channels required).
The AES peripheral provides hardware acceleration to AES cryptographic algorithms
packaged in STM32 cryptographic library.
AES is an AMBA AHB slave peripheral, accessible through 32-bit word single accesses only
(otherwise an AHB bus error is generated and write accesses are ignored).
AES
32-bit
access Banked registers
aes_in_dma DMA
aes_out_dma interface Control Logic
IRQ
aes_it
interface
MSv42155V1
Chaining modes
The following chaining modes are supported by AES, selected through the CHMOD[1:0]
bitfield of the AES_CR register:
• Electronic code book (ECB)
• Cipher block chaining (CBC)
• Counter (CTR)
Note: The chaining mode may be changed only when AES is disabled (bit EN of the AES_CR
register set).
Principle of each AES chaining mode is provided in the following subsections.
Detailed information is in dedicated sections, starting from Section 15.4.8: AES basic
chaining modes (ECB, CBC).
Decryption
Plaintext block 1 Plaintext block 2 Plaintext block 3
output
key
scheduling Ciphertext block 1 Ciphertext block 2 Ciphertext block 3
MSv42140V1
ECB is the simplest mode of operation. There are no chaining operations, and no special
initialization stage. The message is divided into blocks and each block is encrypted or
decrypted separately.
Note: For decryption, a special key scheduling is required before processing the first block.
Encryption
Plaintext block 1 Plaintext block 2 Plaintext block 3
initialization
vector
Decryption
Plaintext block 1 Plaintext block 2 Plaintext block 3
initialization
vector
Legend key key key
Decrypt Decrypt Decrypt
input
output
key
scheduling Ciphertext block 1 Ciphertext block 2 Ciphertext block 3
MSv42141V1
In CBC mode the output of each block chains with the input of the following block. To make
each message unique, an initialization vector is used during the first block processing.
Note: For decryption, a special key scheduling is required before processing the first block.
Decryption
Counter +1 Counter +1 Counter
output
Plaintext block 1 Plaintext block 2 Plaintext block 3
XOR
Ciphertext block 1 Ciphertext block 2 Ciphertext block 3
MSv42142V1
The CTR mode uses the AES core to generate a key stream. The keys are then XORed
with the plaintext to obtain the ciphertext as specified in NIST Special Publication 800-38A,
Recommendation for Block Cipher Modes of Operation.
Note: Unlike with ECB and CBC modes, no key scheduling is required for the CTR decryption,
since in this chaining scheme the AES core is always used in encryption mode for producing
the key stream, or counter blocks.
success success
success success
success success
End End
MSv42146V1
Initialization of AES
To initialize AES, first disable it by clearing the EN bit of the AES_CR register. Then perform
the following steps in any order:
• Configure the AES mode, by programming the MODE[1:0] bitfield of the AES_CR
register.
– For encryption, Mode 1 must be selected (MODE[1:0] = 00).
– For decryption, Mode 3 must be selected (MODE[1:0] = 10), unless ECB or CBC
chaining modes are used. In this latter case, an initial key derivation of the
encryption key must be performed, as described in Section 15.4.5: AES
decryption key preparation.
• Select the chaining mode, by programming the CHMOD[1:0] bitfield of the AES_CR
register
• Write a symmetric key into the AES_KEYRx registers .
• Configure the data type (1-, 8-, 16- or 32-bit), with the DATATYPE[1:0] bitfield in the
AES_CR register.
• When it is required (for example in CBC or CTR chaining modes), write the initialization
vectors into the AES_IVRx register.
Data append
This section describes different ways of appending data for processing, where the size of
data to process is not a multiple of 128 bits.
For ECB or CBC mode, refer to Section 15.4.6: AES ciphertext stealing and data padding.
The second-last and the last block management in these cases is more complex than in the
sequence described in this section.
Data append through polling
This method uses flag polling to control the data append.
For all other cases, the data is appended through the following sequence:
1. Enable the AES peripheral by setting the EN bit of the AES_CR register.
2. Repeat the following sub-sequence until the payload is entirely processed:
a) Write four input data words into the AES_DINR register.
b) Wait until the status flag CCF is set in the AES_SR, then read the four data words
from the AES_DOUTR register.
c) Clear the CCF flag, by setting the CCFC bit of the AES_CR register.
d) If the data block just processed is the second-last block of the message and the
significant data in the last block to process is inferior to 128 bits, pad the
remainder of the last block with zeros
3. Discard the data that is not part of the payload, then disable the AES peripheral by
clearing the EN bit of the AES_CR register.
Note: Up to three wait cycles are automatically inserted between two consecutive writes to the
AES_DINR register, to allow sending the key to the AES processor.
Data append using interrupt
The method uses interrupt from the AES peripheral to control the data append, through the
following sequence:
1. Enable interrupts from AES by setting the CCFIE bit of the AES_CR register.
2. Enable the AES peripheral by setting the EN bit of the AES_CR register.
3. Write first four input data words into the AES_DINR register.
4. Handle the data in the AES interrupt service routine, upon interrupt:
a) Read four output data words from the AES_DOUTR register.
b) Clear the CCF flag and thus the pending interrupt, by setting the CCFC bit of the
AES_CR register
c) If the data block just processed is the second-last block of an message and the
significant data in the last block to process is inferior to 128 bits, pad the
remainder of the last block with zeros. Then proceed with point 4e).
d) If the data block just processed is the last block of the message, discard the data
that is not part of the payload, then disable the AES peripheral by clearing the EN
bit of the AES_CR register and quit the interrupt service routine.
e) Write next four input data words into the AES_DINR register and quit the interrupt
service routine.
Note: AES is tolerant of delays between consecutive read or write operations, which allows, for
example, an interrupt from another peripheral to be served between two AES computations.
Data append using DMA
With this method, all the transfers and processing are managed by DMA and AES. To use
the method, proceed as follows:
1. Prepare the last four-word data block (if the data to process does not fill it completely),
by padding the remainder of the block with zeros.
2. Configure the DMA controller so as to transfer the data to process from the memory to
the AES peripheral input and the processed data from the AES peripheral output to the
memory, as described in Section 15.4.13: AES DMA interface. Configure the DMA
controller so as to generate an interrupt on transfer completion.
3. Enable the AES peripheral by setting the EN bit of the AES_CR register
4. Enable DMA requests by setting the DMAINEN and DMAOUTEN bits of the AES_CR
register.
5. Upon DMA interrupt indicating the transfer completion, get the AES-processed data
from the memory.
Note: The CCF flag has no use with this method, because the reading of the AES_DOUTR
register is managed by DMA automatically, without any software action, at the end of the
computation phase.
WR WR WR WR RD RD RD RD
Wait until flag CCF = 1
EK3 EK2 EK1 EK0 DK3 DK2 DK1 DK0
MSB LSB MSB LSB
If the software stores the initial key prepared for decryption, it is enough to do the key
schedule operation only once for all the data to be decrypted with a given cipher key.
Note: Alternative key preparation is to select Mode 4 by setting to 11 the MODE[1:0] bitfield of the
AES_CR register. In this case Mode 3 cannot be used.
Message 1 Message 2
128-bit block 1
128-bit block 1
128-bit block 2
128-bit block 4
AES resume
128-bit block 5
sequence
128-bit block 6
...
MSv42148V1
O1 O2
Legend Swap AES core Swap
DATATYPE[1:0] management DATATYPE[1:0] management
input
In ECB encrypt mode, the 128-bit plaintext input data block Px in the AES_DINR register
first goes through bit/byte/half-word swapping. The swap result Ix is processed with the AES
core set in encrypt mode, using a 128--bit key. The encryption result Ox goes through
bit/byte/half-word swapping, then is stored in the AES_DOUTR register as 128-bit ciphertext
output data block Cx. The ECB encryption continues in this way until the last complete
plaintext block is encrypted.
Figure 65 illustrates the electronic codebook (ECB) decryption.
O1 O2
Legend Swap Swap
DATATYPE[1:0] management DATATYPE[1:0] management
input
output
AES_DOUTR (plaintext P1) AES_DOUTR (plaintext P2)
MSv19106V2
To perform an AES decryption in the ECB mode, the secret key has to be prepared by
collecting the last-round encryption key (which requires to first execute the complete key
schedule for encryption), and using it as the first-round key for the decryption of the
ciphertext. This preparation is supported by the AES core.
In ECB decrypt mode, the 128-bit ciphertext input data block C1 in the AES_DINR register
first goes through bit/byte/half-word swapping. The keying sequence is reversed compared
to that of the ECB encryption. The swap result I1 is processed with the AES core set in
decrypt mode, using the formerly prepared decryption key. The decryption result goes
through bit/byte/half-word swapping, then is stored in the AES_DOUTR register as 128-bit
plaintext output data block P1. The ECB decryption continues in this way until the last
complete ciphertext block is decrypted.
Figure 66 illustrates the cipher block chaining (CBC) encryption mode.
output
AES_DOUTR (ciphertext C1) AES_DOUTR (ciphertext C2)
XOR
MSv19107V2
In CBC encrypt mode, the first plaintext input block, after bit/byte/half-word swapping (P1’),
is XOR-ed with a 128-bit IVI bitfield (initialization vector and counter), producing the I1 input
data for encrypt with the AES core, using a 128- key. The resulting 128-bit output block O1,
after swapping operation, is used as ciphertext C1. The O1 data is then XOR-ed with the
second-block plaintext data P2’ to produce the I2 input data for the AES core to produce the
second block of ciphertext data. The chaining of data blocks continues in this way until the
last plaintext block in the message is encrypted.
If the message size is not a multiple of 128 bits, the final partial data block is encrypted in
the way explained in Section 15.4.6: AES ciphertext stealing and data padding.
Figure 67 illustrates the cipher block chaining (CBC) decryption mode.
I1 I2
AES_KEYRx (KEY) AES_KEYRx (KEY)
Decrypt Decrypt
AES_IVRx (IV) O1 O2
IVI
P1' P2'
Legend
DATATYPE[1:0] Swap DATATYPE[1:0] Swap
management management
input
output
AES_DOUTR (plaintext P1) AES_DOUTR (plaintext P2)
XOR
MSv19104V2
In CBC decrypt mode, like in ECB decrypt mode, the secret key must be prepared to
perform an AES decryption.
After the key preparation process, the decryption goes as follows: the first 128-bit ciphertext
block (after the swap operation) is used directly as the AES core input block I1 for decrypt
operation, using the 128-bit key. Its output O1 is XOR-ed with the 128-bit IVI field (that must
be identical to that used during encryption) to produce the first plaintext block P1.
The second ciphertext block is processed in the same way as the first block, except that the
I1 data from the first block is used in place of the initialization vector.
The decryption continues in this way until the last complete ciphertext block is decrypted.
If the message size is not a multiple of 128 bits, the final partial data block is decrypted in
the way explained in Section 15.4.6: AES ciphertext stealing and data padding.
For more information on data swapping, refer to Section 15.4.10: AES data registers and
data swapping.
WR WR WR WR RD RD RD RD
Wait until flag CCF = 1
PT3 PT2 PT1 PT0 CT3 CT2 CT1 CT0
MSB LSB MSB LSB
9. Repeat steps 6,7,8 to process all the blocks encrypted with the same key.
WR WR WR WR RD RD RD RD
Wait until flag CCF = 1
CT3 CT2 CT1 CT0 PT3 PT2 PT1 PT0
MSB LSB MSB LSB
16-byte boundaries
Zero
ICB Ciphertext (C) 0 padding
decrypt
4-byte boundaries
Plaintext (P)
Initialization vector (IV) Counter
MSv42156V1
output
AES_DOUTR (plaintext P1) AES_DOUTR (plaintext P2)
XOR
MSv18942V2
In CTR mode, the cryptographic core output (also called keystream) Ox is XOR-ed with
relevant input block (Px' for encryption, Cx' for decryption), to produce the correct output
block (Cx' for encryption, Px' for decryption). Initialization vectors in AES must be initialized
as shown in Table 69.
Unlike in CBC mode that uses the AES_IVRx registers only once when processing the first
data block, in CTR mode AES_IVRx registers are used for processing each data block, and
the AES peripheral increments the counter bits of the initialization vector (leaving the nonce
bits unchanged).
CTR decryption does not differ from CTR encryption, since the core always encrypts the
current counter block to produce the key stream that is then XOR-ed with the plaintext (CTR
encryption) or ciphertext (CTR decryption) input. In CTR mode, the MODE[1:0] bitfield
settings 11, 10 or 00 default all to encryption mode, and the setting 01 (key derivation) is
forbidden.
Data swapping
The AES peripheral can be configured to perform a bit-, a byte-, a half-word-, or no
swapping on the input data word in the AES_DINR register, before loading it to the AES
processing core, and on the data output from the AES processing core, before sending it to
the AES_DOUTR register. The choice depends on the type of data. For example, a byte
swapping is used for an ASCII text stream.
The data swap type is selected through the DATATYPE[1:0] bitfield of the AES_CR register.
The selection applies both to the input and the output of the AES core.
For different data swap types, Figure 73 shows the construction of AES processing core
input buffer data P127..0, from the input data entered through the AES_DINR register, or the
construction of the output data available through the AES_DOUTR register, from the AES
processing core output buffer data P127..0.
1 2 3 4
D127 D96 D95
P95 D64 D63 D32 D31 D0
MSB LSB
1 2 3 4
D111 D96 D127 D112 D79 D64 D95 D80 D47 D32 D63 D48 D15 D0 D31 D16
MSB LSB
1 2 3 4
D103..96 D111.104 D119..112 D127..120 D71...64 D79..72 D87..80 D95..88 D39...32 D47...40 D55...48 D63...56 D7...0 D15...8 D23...16 D31...24
MSB LSB
1 2 3 4
D96 D97 D98 D125 D126 D127 D64 D65 D66 D93 D94 D95 D32 D33 D34 D61 D62 D63 D0 D1 D2 D29 D30 D31
MSB LSB
Legend: AES input/output data block in memory MSB most significant bit (127) of memory data block / AEC core buffer
AES core input/output buffer data LSB least significant bit (0) of memory data block / AEC core buffer
Zero padding (example) 1 4 Order of write to AES_DINR / read from AES_DOUTR
Data swap Dx input/output data bit ‘x’ MSv42153V2
Note: The data in AES key registers (AES_KEYRx) and initialization registers (AES_IVRx) are not
sensitive to the swap mode selection.
Data padding
Figure 73 also gives an example of memory data block padding with zeros such that the
zeroed bits after the data swap form a contiguous zone at the MSB end of the AES core
input buffer. The example shows the padding of an input data block containing:
• 48 message bits, with DATATYPE[1:0] = 01
• 56 message bits, with DATATYPE[1:0] = 10
• 34 message bits, with DATATYPE[1:0] = 11
The key for encryption or decryption may be written into these registers when the AES
peripheral is disabled.
The key registers are not affected by the data swapping controlled by DATATYPE[1:0]
bitfield of the AES_CR register.
Figure 74. DMA transfer of a 128-bit data block during input phase
Chronological order
Increasing address
Memory accessed through DMA
Word3 Word2 Word1 Word0
System
D127 DIN[127:96] D96 D95 DIN[95:64] D64 D63 DIN[63:32] D32 D31 DIN[31:0] D0
MSB LSB
1 2 3 4
AES_DINR
peripheral
(No swapping) 1 2 3 4
AES
word to be read from the AES_DOUTR register. It asserts four DMA requests to transfer one
128-bit (four-word) output data block to memory, as shown in Figure 75.
See Table 72 for recommended DMA configuration.
Figure 75. DMA transfer of a 128-bit data block during output phase
Chronological order
Increasing address
Memory accessed through DMA
Word3 Word2 Word1 Word0
System
D127 DOUT[127:96] D96 D95 DOUT[95:64] D64 D63 DOUT[63:32] D32 D31 DOUT[31:0] D0
MSB LSB
1 2 3 4
AES_DOUTR
peripheral
(No swapping) 1 2 3 4
AES
DMA single requests are generated by AES until it is disabled. So, after the data output
phase at the end of processing of a 128-bit data block, AES switches automatically to a new
data input phase for the next data block, if any.
When the data transferring between AES and memory is managed by DMA, the CCF flag is
not relevant and can be ignored (left set) by software. It must only be cleared when
transiting back to data transferring managed by software. See Suspend/resume operations
in ECB/CBC modes in Section 15.4.8: AES basic chaining modes (ECB, CBC) as example.
CCF
CCFIE
RDERR
ERRIE
MSv42162V1
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
DMAO DMAIN
Res. Res. Res. ERRIE CCFIE ERRC CCFC CHMOD[1:0] MODE[1:0] DATATYPE[1:0] EN
UTEN EN
rw rw rw rw rw rw rw rw rw rw rw rw rw
When the bit is set, DMA requests are automatically generated by AES during the output data
phase. This feature is only effective when Mode 1 or Mode 3 is selected through the MODE[1:0]
bitfield. It is not effective for Mode 2 (key derivation).
Usage of DMA with Mode 4 (single decryption) is not recommended.
Bit 11 DMAINEN: DMA input enable
This bit enables/disables data transferring with DMA, in the input phase:
0: Disable
1: Enable
When the bit is set, DMA requests are automatically generated by AES during the input data phase.
This feature is only effective when Mode 1 or Mode 3 is selected through the MODE[1:0] bitfield. It is
not effective for Mode 2 (key derivation).
Usage of DMA with Mode 4 (single decryption) is not recommended.
Bit 10 ERRIE: Error interrupt enable
This bit enables or disables (masks) the AES interrupt generation when RDERR and/or WRERR is
set:
0: Disable (mask)
1: Enable
Bit 9 CCFIE: CCF interrupt enable
This bit enables or disables (masks) the AES interrupt generation when CCF (computation complete
flag) is set:
0: Disable (mask)
1: Enable
The bitfield value change is allowed only when AES is disabled, so as to avoid an unpredictable
behavior.
Bits 4:3 MODE[1:0]: AES operating mode
This bitfield selects the AES operating mode:
00: Mode 1: encryption
01: Mode 2: key derivation (or key preparation for ECB/CBC decryption)
10: Mode 3: decryption
11: Mode 4: key derivation then single decryption
The bitfield value change is allowed only when AES is disabled, so as to avoid an unpredictable
behavior. Any attempt to selecting Mode 4 while either ECB or CBC chaining mode is not selected,
defaults to effective selection of Mode 3. It is not possible to select a Mode 3 following a Mode 4.
Bits 2:1 DATATYPE[1:0]: Data type selection
This bitfield defines the format of data written in the AES_DINR register or read from the
AES_DOUTR register, through selecting the mode of data swapping:
00: None
01: Half-word (16-bit)
10: Byte (8-bit)
11: Bit
For more details, refer to Section 15.4.10: AES data registers and data swapping.
The bitfield value change is allowed only when AES is disabled, so as to avoid an unpredictable
behavior.
Bit 0 EN: AES enable
This bit enables/disables the AES peripheral:
0: Disable
1: Enable
At any moment, clearing then setting the bit re-initializes the AES peripheral.
This bit is automatically cleared by hardware when the key preparation process ends (Mode 2).
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
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
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. WRERR RDERR CCF
r r r r r r r r r r r r r r r r
The flag is set by hardware. It is cleared by software upon setting the ERRC bit of the AES_CR
register.
Upon the flag setting, an interrupt is generated if enabled through the ERRIE bit of the AES_CR
register.
The flag setting has no impact on the AES operation.
The flag is not effective when key derivation mode is selected.
Bit 1 RDERR: Read error flag
This flag indicates the detection of an unexpected read operation from the AES_DOUTR register
(during computation or data input phase):
0: Not detected
1: Detected
The flag is set by hardware. It is cleared by software upon setting the ERRC bit of the AES_CR
register.
Upon the flag setting, an interrupt is generated if enabled through the ERRIE bit of the AES_CR
register.
The flag setting has no impact on the AES operation.
The flag is not effective when key derivation mode is selected.
Bit 0 CCF: Computation completed flag
This flag indicates whether the computation is completed:
0: Not completed
1: Completed
The flag is set by hardware upon the completion of the computation. It is cleared by software, upon
setting the CCFC bit of the AES_CR register.
Upon the flag setting, an interrupt is generated if enabled through the CCFIE bit of the AES_CR
register.
The flag is significant only when the DMAOUTEN bit is 0. It may stay high when DMA_EN is 1.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
DIN[x+31:x+16]
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
DIN[x+15:x]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bits 31:0 This bitfield feeds a 32-bit input buffer. A 4-fold sequential write to this bitfield during the input phase
DIN[x+31:x]: One of four 32-bit words of a 128-bit input data block being written into the peripheral
virtually writes a complete 128-bit block of input data to the AES peripheral. Upon each write, the
data from the input buffer are handled by the data swap block according to the DATATYPE[1:0]
bitfield, then written into the AES core 128-bit input buffer.
The substitution for “x”, from the first to the fourth write operation, is: 96, 64, 32, and 0. In other
words, data from the first to the fourth write operation are: DIN[127:96], DIN[95:64], DIN[63:32], and
DIN[31:0].
The data signification of the input data block depends on the AES operating mode:
- Mode 1 (encryption): plaintext
- Mode 2 (key derivation): the bitfield is not used (AES_KEYRx registers used for input)
- Mode 3 (decryption) and Mode 4 (key derivation then single decryption): ciphertext
The data swap operation is described in Section 15.4.10: AES data registers and data swapping on
page 355.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
DOUT[x+31:x+16]
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
DOUT[x+15:0]
r r r r r r r r r r r r r r r r
Bits 31:0 DOUT[x+31:x]: One of four 32-bit words of a 128-bit output data block being read from the peripheral
This bitfield fetches a 32-bit output buffer. A 4-fold sequential read of this bitfield, upon the
computation completion (CCF set), virtually reads a complete 128-bit block of output data from the
AES peripheral. Before reaching the output buffer, the data produced by the AES core are handled
by the data swap block according to the DATATYPE[1:0] bitfield.
The substitution for DOUT[x+31:x], from the first to the fourth read operation, is: 96, 64, 32, and 0. In
other words, data from the first to the fourth read operation are: DOUT[127:96], DOUT[95:64],
DOUT[63:32], and DOUT[31:0].
The data signification of the output data block depends on the AES operating mode:
- Mode 1 (encryption): ciphertext
- Mode 2 (key derivation): the bitfield is not used (AES_KEYRx registers used for output).
- Mode 3 (decryption) and Mode 4 (key derivation then single decryption): plaintext
The data swap operation is described in Section 15.4.10: AES data registers and data swapping on
page 355.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
KEY[31:16]
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
KEY[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
KEY[63:48]
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
KEY[47:32]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
KEY[95:80]
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
KEY[79:64]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
KEY[127:112]
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
KEY[111:96]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
IVI[31:16]
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
IVI[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
IVI[63:48]
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
IVI[47:32]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
IVI[95:80]
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
IVI[79:64]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
IVI[127:112]
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
IVI[111:96]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
10
11
9
8
7
6
5
4
3
2
1
0
DATATYPE[1:0]
CHMOD[1:0]
DMAOUTEN
MODE[1:0]
DMAINEN
ERRIE
CCFIE
ERRC
CCFC
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
AES_CR
EN
0x0000
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
10
11
9
8
7
6
5
4
3
2
1
0
WRERR
RDERR
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
CCF
AES_SR
0x0004
Reset value 0 0 0
AES_DINR
DIN[x+31:x]
0x0008 x=96,64,32,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
AES_DOUTR
0x000 DOUT[x+31:x]
x=96,64,32,0
C
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
AES_KEYR0 KEY[31:0]
0x0010
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
AES_KEYR1 KEY[63:32]
0x0014
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
AES_KEYR2 KEY[95:64]
0x0018
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
AES_IVR0 IVI[31:0]
0x0020
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
AES_IVR1 IVI[63:32]
0x0024
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
AES_IVR2 IVI[95:64]
0x0028
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
TI1FP1 Encoder
TI2FP2 interface
U
Auto-reload register UI
Stop, clear or up/down
U
CK_PSC PSC CK_CNT +/- CNT counter
prescaler
CC1I U CC1I
XOR TI1FP1 OC1REF
TI1 Input filter & IC1 IC1PS Output OC1 TIMx_CH1
TI1FP2 Prescaler Capture/Compare 1 register
edge detector control
TIMx_CH1 TRC
CC2I
U CC2I
TI2FP1
TI2 Input filter & IC2 Output OC2
TIMx_CH2 TI2FP2 Prescaler
IC2PS Capture/Compare 2 register OC2REF TIMx_CH2
edge detector control
TRC
CC3I CC3I
U
TI3FP3 OC3REF
TI3 Input filter & IC3 IC3PS Output OC3 TIMx_CH3
TI3FP4 Prescaler Capture/Compare 3 register
TIMx_CH3 edge detector control
TRC CC4I
U CC4I
TI4FP3
TI4 Input filter & IC4 Output OC4
TIMx_CH4 TI4FP4 Prescaler
IC4PS Capture/Compare 4 register OC4REF TIMx_CH4
edge detector control
TRC
ETRF
Notes:
Preload registers transferred
Reg
to active registers on U event
according to control bit
Event
MS19673V1
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/32-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 78 and Figure 16.3.2 give some examples of the counter behavior when the
prescaler ratio is changed on the fly:
Figure 78. Counter timing diagram with prescaler division change from 1 to 2
CK_PSC
CEN
Timerclock = CK_CNT
Counter register F7 F8 F9 FA FB FC 00 01 02 03
Prescaler buffer 0 1
Prescaler counter 0 0 1 0 1 0 1 0 1
MS31076V2
Figure 79. Counter timing diagram with prescaler division change from 1 to 4
CK_PSC
CEN
Timerclock = CK_CNT
F7 F8 F9 FA FB FC 00 01
Counter register
0 3
Prescaler control register
0 3
Prescaler buffer
0 0 1 2 3 0 1 2 3
Prescaler counter
MS31077V2
CK_PSC
CNT_EN
Timerclock = CK_CNT
Counter register 31 32 33 34 35 36 00 01 02 03 04 05 06 07
Counter overflow
MS31078V2
CK_PSC
CNT_EN
Timerclock = CK_CNT
Counter overflow
MS31079V2
CK_PSC
CNT_EN
Timerclock = CK_CNT
Counter overflow
MS31080V2
CK_PSC
Timerclock = CK_CNT
Counter register 1F 20 00
Counter overflow
MS31081V2
Figure 84. Counter timing diagram, Update event when ARPE=0 (TIMx_ARR not
preloaded)
CK_PSC
CEN
Timerclock = CK_CNT
Counter register 31 32 33 34 35 36 00 01 02 03 04 05 06 07
Counter overflow
Auto-reload preload
register FF 36
Figure 85. Counter timing diagram, Update event when ARPE=1 (TIMx_ARR
preloaded)
CK_PSC
CEN
Timerclock = CK_CNT
Counter register F0 F1 F2 F3 F4 F5 00 01 02 03 04 05 06 07
Counter overflow
Auto-reload shadow
register F5 36
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 doesn’t 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.
CK_PSC
CNT_EN
Timerclock = CK_CNT
Counter register 05 04 03 02 01 00 36 35 34 33 32 31 30 2F
Counter underflow
(cnt_udf)
MS31184V1
CK_PSC
CNT_EN
Timerclock = CK_CNT
Counter underflow
MS31185V1
CK_PSC
CNT_EN
Timerclock = CK_CNT
Counter underflow
MS31186V1
CK_PSC
Timerclock = CK_CNT
Counter register 20 1F 00 36
Counter underflow
MS31187V1
Figure 90. Counter timing diagram, Update event when repetition counter
is not used
CK_PSC
CEN
Timerclock = CK_CNT
Counter register 05 04 03 02 01 00 36 35 34 33 32 31 30 2F
Counter underflow
Auto-reload preload
register FF 36
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.
CK_PSC
CEN
Timerclock = CK_CNT
Counter register 04 03 02 01 00 01 02 03 04 05 06 05 04 03
Counter underflow
Counter overflow
MS31189V1
1. Here, center-aligned mode 1 is used (for more details refer to Section 16.4.1: TIMx control register 1
(TIMx_CR1) on page 415).
CK_PSC
CNT_EN
Timerclock = CK_CNT
Counter underflow
MS31190V1
CK_PSC
CNT_EN
Timerclock = CK_CNT
Counter overflow
CK_PSC
Timerclock = CK_CNT
Counter register 20 1F 01 00
Counter underflow
MS31192V1
Figure 95. Counter timing diagram, Update event with ARPE=1 (counter underflow)
CK_PSC
CEN
Timerclock = CK_CNT
Counter register 06 05 04 03 02 01 00 01 02 03 04 05 06 07
Counter underflow
Auto-reload preload
register FD 36
Auto-reload active
register FD 36
MS31193V1
Figure 96. Counter timing diagram, Update event with ARPE=1 (counter overflow)
CK_PSC
CEN
Counter register F7 F8 F9 FA FB FC 36 35 34 33 32 31 30 2F
Counter overflow
MS31194V1
Internal clock
CEN=CNT_EN
UG
CNT_INIT
Counter register 31 32 33 34 35 36 00 01 02 03 04 05 06 07
MS31085V2
TIMx_SMCR
TS[2:0]
or TI2F or
TI1F or Encoder
ITRx mode
0xx
TI1_ED
100 TRGI External clock
TI1FP1 mode 1 CK_PSC
TI2F_Rising 101
TI2 Edge 0 TI2FP2 ETRF External clock
Filter 110
detector 1 ETRF mode 2
TI2F_Falling 111
CK_INT Internal clock
mode
ICF[3:0] CC2P (internal clock)
TIMx_CCMR1 TIMx_CCER
ECE SMS[2:0]
TIMx_SMCR
MS31196V1
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 it does not need to be configured.
3. Select 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 input source by writing TS=110 in the TIMx_SMCR register.
6. Enable the counter by writing CEN=1 in the TIMx_CR1 register.
For code example, refer to A.9.1: Upcounter on TI2 rising edge code example.
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.
TI2
CNT_EN
Counter register 34 35 36
TIF
Write TIF=0
MS31087V2
or TI2F or
TI1F or Encoder
mode
ECE SMS[2:0]
TIMx_SMCR
MS33116V1
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.
For code example, refer to A.9.2: Up counter on each 2 ETR rising edges code example.
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.
f CK_INT
CNT_EN
ETR
ETRP
ETRF
Counter clock =
CK_INT =CK_PSC
Counter register 34 35 36
MS33111V2
TI1F_ED
To the slave mode controller
TI1 TI1F_Rising
Filter TI1F Edge 0 TI1FP1
fDTS downcounter TI1F_Falling 01
detector 1
TI2FP1 IC1 Divider IC1PS
10
/1, /2, /4, /8
ICF[3:0] CC1P/CC1NP TRC
11
TIMx_CCMR1 TIMx_CCER (from slave mode
TI2F_Rising controller)
0
(from channel 2)
CC1S[1:0] ICPS[1:0] CC1E
TI2F_Falling
1
(from channel 2) TIMx_CCMR1 TIMx_CCER
MS33115V1
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.
APB Bus
MCU-peripheral interface
(if 16-bit)
8 8
high
S write CCR1H
low
MS33144V1
TIMx_SMCR
OCCS
To the master
OCREF_CLR 0
mode controller
ETRF 1
ocref_clr_int
CNT > CCR1 0
Output OC1REF Output OC1
mode enable
CNT = CCR1 1 circuit
controller
CC1P
TIMx_CCER CC1E TIM1_CCER
OC1M[2:0]
TIMx_CCMR1
MS33146V1
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.
detected (sampled at fDTS 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 writing the 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, and/or the DMA request by setting the CC1DE bit in the
TIMx_DIER register.
For code example, refer to A.9.3: Input capture configuration code example.
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.
For code example, refer to A.9.4: Input capture data management code example.
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.
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 106.
OC1REF= OC1
MS31092V1
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.
Counter register 0 1 2 3 4 5 6 7 8 0 1
OCXREF
CCRx=4
CCxIF
OCXREF
CCRx=8
CCxIF
OCXREF ‘1’
CCRx>8
CCxIF
OCXREF ‘0’
CCRx=0
CCxIF
MS31093V1
Downcounting configuration
Downcounting is active when DIR bit in TIMx_CR1 register is high. Refer to Section :
Downcounting mode on page 378.
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.
Counter register 0 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 0 1
OCxREF
CCRx = 4
CCxIF CMS=01
CMS=10
CMS=11
OCxREF
CCRx=7
CMS=10 or 11
CCxIF
‘1’
OCxREF
CCRx=8
CCxIF CMS=01
CMS=10
CMS=11
‘1’
OCxREF
CCRx>8
CCxIF CMS=01
CMS=10
CMS=11
‘0’
OCxREF
CCRx=0
CCxIF CMS=01
CMS=10
CMS=11
AI14681b
TI2
OC1REF
OC1
TIM1_ARR
Counter
TIM1_CCR1
0
tDELAY tPULSE t
MS31099V1
For example one may want to generate a positive pulse on OC1 with a length of tPULSE and
after a delay of tDELAY as soon as a positive edge is detected on the TI2 input pin.
Let’s use TI2FP2 as trigger 1:
1. Map TI2FP2 on 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).
For code example, refer to A.9.16: One-Pulse mode code example.
The OPM waveform is defined by writing the compare registers (taking into account the
clock frequency and the counter prescaler).
• The tDELAY is defined by the value written in the TIMx_CCR1 register.
• The tPULSE is defined by the difference between the auto-reload value and the compare
value (TIMx_ARR - TIMx_CCR1+1).
• Let’s say one 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 PWM mode 2 must be enabled by writing OC1M=111 in the
TIMx_CCMR1 register. Optionally the preload registers can be enabled by writing
OC1PE=1 in the TIMx_CCMR1 register and ARPE in the TIMx_CR1 register. In this
case one has 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.
Since only 1 pulse (Single mode) is needed, a 1 must be written 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.
(CCRx)
Counter (CNT)
ETRF
OCxREF
(OCxCE = ‘0’)
OCxREF
(OCxCE = ‘1’)
OCxREF_CLR OCxREF_CLR
becomes high still high
MS33105V1
1. In case of a PWM with a 100% duty cycle (if CCRx>ARR), OCxREF is enabled again at the next counter
overflow.
position. The count direction correspond to the rotation direction of the connected sensor.
The table summarizes the possible combinations, assuming TI1 and TI2 do not 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 encoder’s
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 111 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, CC1NP = ‘0’ (TIMx_CCER register, TI1FP1 noninverted, TI1FP1=TI1)
• CC2P=0, CC2NP = ‘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)
For code example, refer to A.9.11: Encoder interface code example.
TI1
TI2
Counter
up down up
MS33107V1
Figure 112 gives an example of counter behavior when TI1FP1 polarity is inverted (same
configuration as above except CC1P=1).
Figure 112. Example of encoder interface mode with TI1FP1 polarity inverted
TI1
TI2
Counter
down up down
MS33108V1
The timer, when configured in Encoder Interface mode provides information on the sensor’s
current position. Dynamic information can be obtained (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. This can be done 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
UG
Counter register 30 31 32 33 34 35 36 00 01 02 03 00 01 02 03
TIF
MS31401V2
TI1
cnt_en
Counter register 30 31 32 33 34 35 36 37 38
TIF
Write TIF=0
MS31402V1
1. The configuration “CCxP=CCxNP=1” (detection of both rising and falling edges) does not have any effect
in gated mode because gated mode acts on a level and not on an edge.
TI2
cnt_en
Counter register 34 35 36 37 38
TIF
MS31403V1
TI1
CEN/CNT_EN
ETR
Counter register 34 35 36
TIF
MS33110V1
UEV
Master Slave CK_PSC
TRGO1 ITR1
mode mode
Prescaler Counter control control
Prescaler Counter
Input
trigger
selection
MS33136V1
For example, Timer x can be configured to act as a prescaler for Timer y. Refer to
Figure 117. To do this, follow the sequence below:
1. Configure Timer x in master mode so that it outputs a periodic trigger signal on each
update event UEV. If MMS=010 is written in the TIMx_CR2 register, a rising edge is
output on TRGO1 each time an update event is generated.
2. To connect the TRGO1 output of Timer x to Timer y, Timer y must be configured in
slave mode using ITR1 as internal trigger. This is selected through the TS bits in the
TIMy_SMCR register (writing TS=000).
3. Then the slave mode controller must be put in external clock mode 1 (write SMS=111 in
the TIMy_SMCR register). This causes Timer y to be clocked by the rising edge of the
periodic Timer x trigger signal (which correspond to the timer x counter overflow).
4. Finally both timers must be enabled by setting their respective CEN bits (TIMx_CR1
register).
For code example, refer to A.9.17: Timer prescaling another timer code example.
Note: If OCx is selected on Timer x as trigger output (MMS=1xx), its rising edge is used to clock
the counter of timer y.
CK_INT
TIMERx-OC1REF
TIMERx-CNT FC FD FE FF 00 01
TIMERy-TIF
Write TIF = 0
MS33137V1
In the example in Figure 118, the Timer y 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 x. Then any value can be written
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 x and Timer y. Timer x is the master and starts
from 0. Timer y is the slave and starts from 0xE7. The prescaler ratio is the same for both
timers. Timer y stops when Timer x is disabled by writing ‘0 to the CEN bit in the TIMy_CR1
register:
1. Configure Timer x master mode to send its Output Compare 1 Reference (OC1REF)
signal as trigger output (MMS=100 in the TIMx_CR2 register).
2. Configure the Timer x OC1REF waveform (TIMx_CCMR1 register).
3. Configure Timer y to get the input trigger from Timer x (TS=000 in the TIMy_SMCR
register).
4. Configure Timer y in gated mode (SMS=101 in TIMy_SMCR register).
5. Reset Timer x by writing ‘1 in UG bit (TIMx_EGR register).
6. Reset Timer y by writing ‘1 in UG bit (TIMy_EGR register).
7. Initialize Timer y to 0xE7 by writing ‘0xE7’ in the timer y counter (TIMy_CNTL).
8. Enable Timer y by writing ‘1 in the CEN bit (TIMy_CR1 register).
9. Start Timer x by writing ‘1 in the CEN bit (TIMx_CR1 register).
10. Stop Timer x by writing ‘0 in the CEN bit (TIMx_CR1 register).
For code example, refer to A.9.19: Master and slave synchronization code example.
CK_INT
TIMERx-CEN=CNT_EN
TIMERx-CNT_INIT
TIMERx-CNT 75 00 01 02
TIMERy-CNT AB 00 E7 E8 E9
TIMERy-CNT_INIT
TIMERy-write CNT
TIMERy-TIF
Write TIF = 0
MS33138V1
CK_INT
TIMERx-UEV
TIMERx-CNT FD FE FF 00 01 02
TIMERy-CNT 45 46 47 48
TIMERy-CEN=CNT_EN
TIMERy-TIF
Write TIF = 0
MS33139V1
As in the previous example, both counters can be initialized before starting counting.
Figure 121 shows the behavior with the same configuration as in Figure 120 but in trigger
mode instead of gated mode (SMS=110 in the TIMy_SMCR register).
CK_INT
TIMERx-CEN=CNT_EN
TIMERx-CNT_INIT
TIMERx-CNT 75 00 01 02
TIMERy-CNT CD 00 E7 E8 E9 EA
TIMERy-CNT_INIT
TIMERy
write CNT
TIMERy-TIF
Write TIF = 0
MS33140V1
CK_INT
TIMERx-TI1
TIMERx-CEN=CNT_EN
TIMERx-CK_PSC
TIMERx-CNT 00 01 02 03 04 05 06 07 08 09
TIMERx-TIF
TIMERy-CEN=CNT_EN
TIMERy-CK_PSC
TIMERy-CNT 00 01 02 03 04 05 06 07 08 09
TIMERy-TIF
MS33141V1
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
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
rc_w0 rc_w0 rc_w0 rc_w0 rc_w0 rc_w0 rc_w0 rc_w0 rc_w0 rc_w0
w w w w w w
rw rw rw rw rw rw rw rw rw rw rw rw
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
CCR1[15:0]
rw/r rw/r rw/r rw/r rw/r rw/r rw/r rw/r rw/r rw/r rw/r rw/r rw/r rw/r rw/r rw/r
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
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. TI4_RMP ETR_RMP
rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. TI_RMP ETR_RMP
rw rw rw rw rw
ARPE
UDIS
OPM
CEN
URS
Res.
Res.
Res.
Res.
Res.
Res.
DIR
TIMx_CR1 CKD [1:0] CMS[1:0]
0x00
Reset value 0 0 0 0 0 0 0 0 0 0
CCDS
TI1S
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
TIMx_CR2 MMS[2:0]
0x04
Reset value ETPS [1:0] 0 0 0 0 0
MSM
ECE
Res.
ETP
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
CC4DE
CC3DE
CC2DE
CC1DE
CC4IE
CC3IE
CC2IE
CC1IE
UDE
Res.
Res.
Res.
Res.
TDE
UIE
TIE
TIMx_DIER
0x0C
Reset value 0 0 0 0 0 0 0 0 0 0 0 0
CC4OF
CC3OF
CC2OF
CC1OF
CC4IF
CC3IF
CC2IF
CC1IF
Res.
Res.
Res.
Res.
Res.
Res.
UIF
TIF
TIMx_SR
0x10
Reset value 0 0 0 0 0 0 0 0 0 0
CC4G
CC3G
CC2G
CC1G
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
UG
TIMx_EGR
TG
0x14
Reset value 0 0 0 0 0 0
TIMx_CCMR1
OC2CE
OC1CE
OC2PE
OC1PE
OC2FE
OC1FE
OC2M OC1M
Output CC2S [1:0] CC1S [1:0]
[2:0] [2:0]
Compare mode
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x18
TIMx_CCMR1 IC2 IC1
Input Capture IC2F[3:0] PSC CC2S [1:0] IC1F[3:0] PSC CC1S [1:0]
mode [1:0] [1:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIMx_CCMR2
OC4CE
OC3CE
OC4PE
OC3PE
OC4FE
OC3FE
OC4M OC3M
Output CC4S [1:0] CC3S [1:0]
[2:0] [2:0]
Compare mode
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x1C
TIMx_CCMR2 IC4 IC3
Input Capture IC4F[3:0] PSC CC4S [1:0] IC3F[3:0] PSC CC3S [1:0]
mode [1:0] [1:0]
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
CC4NP
CC3NP
CC2NP
CC1NP
CC4P
CC4E
CC3P
CC3E
CC2P
CC2E
CC1P
CC1E
Res.
Res.
Res.
Res.
TIMx_CCER
0x20
Reset value 0 0 0 0 0 0 0 0 0 0 0 0
TIMx_CNT CNT[15:0]
0x24
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIMx_PSC PSC[15:0]
0x28
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIMx_ARR ARR[15:0]
0x2C
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x30 Res.
TIMx_CCR1 CCR1[15:0]
0x34
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIMx_CCR2 CCR2[15:0]
0x38
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIMx_CCR3 CCR3[15:0]
0x3C
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIMx_CCR4 CCR4[15:0]
0x40
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x44 Res.
Res.
Res.
Res.
Res.
Res.
Res.
TIMx_DCR DBL[4:0] DBA[4:0]
0x48
Reset value 0 0 0 0 0 0 0 0 0 0
TIMx_DMAR DMAB[15:0]
0x4C
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ETR_RMP
TI4_RMP
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
TIM2_OR
0x50
Reset value 0 0 0 0
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
17.1 Introduction
The TIM21/22 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 TIM21/22 timers are completely independent, and do not share any resources. They
can be synchronized together as described in Section 17.3.14.
TGI
ITR0 ITR Slave
ITR1 Reset, enable, up, count
TRC TRGI controller
mode
TI1F_ED
Encoder
interface
TI1FP1
TI2FP2
U
Auto-reload register UI
Stop, Clear U
CK_PSC PSC CK_CNT +/- CNT counter
prescaler
CC1I U CC1I
TI1FP1 OC1REF OC1
TI1 Input filter & TI1FP2 IC1 IC1PS Capture/Compare 1 register Output
TIMx_CH1 Prescaler TIMx_CH1
edge detector control
TRC CC2I
U CC2I
TI2FP1
TIMx_CH2
TI2 Input filter & IC2 IC2PS Capture/Compare 2 register OC2REF Output OC2
edge detector TI2FP2 Prescaler control TIMx_CH2
TRC
Notes:
Preload registers transferred ETRF
Reg
to active registers on U event
according to control bit
Event
Interrupt MSv33704V2
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 125 and Figure 126 give some examples of the counter behavior when the prescaler
ratio is changed on the fly.
Figure 124. Counter timing diagram with prescaler division change from 1 to 2
CK_PSC
CEN
Timerclock = CK_CNT
Counter register F7 F8 F9 FA FB FC 00 01 02 03
Prescaler buffer 0 1
Prescaler counter 0 0 1 0 1 0 1 0 1
MS31076V2
Figure 125. Counter timing diagram with prescaler division change from 1 to 4
CK_PSC
CEN
Timerclock = CK_CNT
F7 F8 F9 FA FB FC 00 01
Counter register
0 3
Prescaler control register
0 3
Prescaler buffer
0 0 1 2 3 0 1 2 3
Prescaler counter
MS31077V2
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.
CK_PSC
CNT_EN
Timerclock = CK_CNT
Counter register 31 32 33 34 35 36 00 01 02 03 04 05 06 07
Counter overflow
MS31078V2
CK_PSC
CNT_EN
Timerclock = CK_CNT
Counter overflow
MS31079V2
CK_PSC
CNT_EN
Timerclock = CK_CNT
Counter overflow
MS31080V2
CK_PSC
Timerclock = CK_CNT
Counter register 1F 20 00
Counter overflow
MS31081V2
Figure 130. Counter timing diagram, update event when ARPE=0 (TIMx_ARR not
preloaded)
CK_PSC
CEN
Timerclock = CK_CNT
Counter register 31 32 33 34 35 36 00 01 02 03 04 05 06 07
Counter overflow
Auto-reload preload
register FF 36
Figure 131. Counter timing diagram, update event when ARPE=1 (TIMx_ARR
preloaded)
CK_PSC
CEN
Timerclock = CK_CNT
Counter register F0 F1 F2 F3 F4 F5 00 01 02 03 04 05 06 07
Counter overflow
Auto-reload shadow
register F5 36
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 doesn’t 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.
CK_PSC
CNT_EN
Timerclock = CK_CNT
Counter register 05 04 03 02 01 00 36 35 34 33 32 31 30 2F
Counter underflow
(cnt_udf)
MS31184V1
CK_PSC
CNT_EN
Timerclock = CK_CNT
Counter underflow
MS31185V1
CK_PSC
CNT_EN
Timerclock = CK_CNT
Counter underflow
MS31186V1
CK_PSC
Timerclock = CK_CNT
Counter register 20 1F 00 36
Counter underflow
MS31187V1
CK_PSC
CEN
Timerclock = CK_CNT
Counter register 04 03 02 01 00 01 02 03 04 05 06 05 04 03
Counter underflow
Counter overflow
MS31189V1
1. Here, center-aligned mode 1 is used (for more details refer to Section 17.4.1: TIM21/22 control register 1
(TIMx_CR1) on page 475).
CK_PSC
CNT_EN
Timerclock = CK_CNT
Counter underflow
MS31190V1
CK_PSC
CNT_EN
Timerclock = CK_CNT
Counter overflow
CK_PSC
Timerclock = CK_CNT
Counter register 20 1F 01 00
Counter underflow
MS31192V1
Figure 140. Counter timing diagram, Update event with ARPE=1 (counter underflow)
CK_PSC
CEN
Timerclock = CK_CNT
Counter register 06 05 04 03 02 01 00 01 02 03 04 05 06 07
Counter underflow
Auto-reload preload
register FD 36
Auto-reload active
register FD 36
MS31193V1
Figure 141. Counter timing diagram, Update event with ARPE=1 (counter overflow)
CK_PSC
CEN
Counter register F7 F8 F9 FA FB FC 36 35 34 33 32 31 30 2F
Counter overflow
MS31194V1
Internal clock
CEN=CNT_EN
UG
CNT_INIT
Counter register 31 32 33 34 35 36 00 01 02 03 04 05 06 07
MS31085V2
TIMx_SMCR
TS[2:0]
or TI2F or
TI1F or Encoder
ITRx mode
0xx
TI1_ED
100 TRGI External clock
TI1FP1 mode 1 CK_PSC
TI2F_Rising 101
TI2 Edge 0 TI2FP2 ETRF External clock
Filter 110
detector 1 ETRF mode 2
TI2F_Falling 111
CK_INT Internal clock
mode
ICF[3:0] CC2P (internal clock)
TIMx_CCMR1 TIMx_CCER
ECE SMS[2:0]
TIMx_SMCR
MS31196V1
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.
For code example, refer to A.9.1: Upcounter on TI2 rising edge code example.
Note: The capture prescaler is not used for triggering, so it does not need to be configured.
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.
TI2
CNT_EN
Counter register 34 35 36
TIF
Write TIF=0
MS31087V2
or TI2F or
TI1F or Encoder
mode
ECE SMS[2:0]
TIMx_SMCR
MS33116V1
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.
For code example, refer to A.9.2: Up counter on each 2 ETR rising edges code example.
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.
f CK_INT
CNT_EN
ETR
ETRP
ETRF
Counter clock =
CK_INT =CK_PSC
Counter register 34 35 36
MS33111V2
TI1F_ED
To the slave mode controller
TI1 TI1F_Rising
Filter TI1F Edge 0 TI1FP1
fDTS downcounter TI1F_Falling 01
detector 1
TI2FP1 IC1 Divider IC1PS
10
/1, /2, /4, /8
ICF[3:0] CC1P/CC1NP TRC
11
TIMx_CCMR1 TIMx_CCER (from slave mode
TI2F_Rising controller)
0
(from channel 2)
CC1S[1:0] ICPS[1:0] CC1E
TI2F_Falling
1
(from channel 2) TIMx_CCMR1 TIMx_CCER
MS33115V1
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.
APB Bus
MCU-peripheral interface
(if 16-bit)
8 8
high
S write CCR1H
low
ETRF
To the master
mode controller
CCxP
OCxM[2:0] TIMx_CCER CCxE
TIMx_CCMR1 TIMx_CCER
MSv33714V1
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.
detected (sampled at fDTS 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.
For code example, refer to A.9.3: Input capture configuration code example.
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.
For code example, refer to A.9.4: Input capture data management code example.
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.
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.
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 151.
OC1REF= OC1
MS31092V1
The timer is able to generate PWM in edge-aligned mode only since the counter is
upcounting.
• Upcounting configuration
Upcounting is active when the DIR bit in the TIMx_CR1 register is low. Refer to the
Upcounting mode on page 443.
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 152 shows some edge-aligned PWM waveforms in an example where
TIMx_ARR=8.
For code example, refer to A.9.8: Edge-aligned PWM configuration example.
Counter register 0 1 2 3 4 5 6 7 8 0 1
OCXREF
CCRx=4
CCxIF
OCXREF
CCRx=8
CCxIF
OCXREF ‘1’
CCRx>8
CCxIF
OCXREF ‘0’
CCRx=0
CCxIF
MS31093V1
• Downcounting configuration
Downcounting is active when DIR bit in TIMx_CR1 register is high. Refer to the
Downcounting mode on page 447
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.
Counter register 0 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 0 1
OCxREF
CCRx = 4
CCxIF CMS=01
CMS=10
CMS=11
OCxREF
CCRx=7
CMS=10 or 11
CCxIF
‘1’
OCxREF
CCRx=8
CCxIF CMS=01
CMS=10
CMS=11
‘1’
OCxREF
CCRx>8
CCxIF CMS=01
CMS=10
CMS=11
‘0’
OCxREF
CCRx=0
CCxIF CMS=01
CMS=10
CMS=11
AI14681b
(CCRx)
Counter (CNT)
ETRF
OCxREF
(OCxCE = ‘0’)
OCxREF
(OCxCE = ‘1’)
OCxREF_CLR OCxREF_CLR
becomes high still high
MS33105V1
Note: In case of a PWM with a 100% duty cycle (if CCRx>ARR), then OCxREF is enabled again at
the next counter overflow.
TI2
OC1REF
OC1
TIM1_ARR
Counter
TIM1_CCR1
0
tDELAY tPULSE t
MS31099V1
For example one may want to generate a positive pulse on OC1 with a length of tPULSE and
after a delay of tDELAY 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 tDELAY is defined by the value written in the TIMx_CCR1 register.
• The tPULSE is defined by the difference between the auto-reload value and the compare
value (TIMx_ARR - TIMx_CCR1+1).
• Let’s say one 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 PWM mode 2 must be enabled by writing OC1M=’111’ in the
TIMx_CCMR1 register. Optionally the preload registers can be enabled by writing
OC1PE=’1’ in the TIMx_CCMR1 register and ARPE in the TIMx_CR1 register. In this
case one has 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.
For code example, refer to A.9.16: One-Pulse mode code example.
Since only 1 pulse (Single mode) is needed, a 1 must be written 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.
An external incremental encoder can be connected directly to the MCU without external
interface logic. However, comparators are normally be used to convert the encoder’s
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 156 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 and CC1NP = ‘0’ (TIMx_CCER register, TI1FP1 noninverted, TI1FP1=TI1)
• CC2P and CC2NP = ‘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)
For code example, refer to A.9.11: Encoder interface code example.
TI1
TI2
Counter
up down up
MS33107V1
Figure 157 gives an example of counter behavior when TI1FP1 polarity is inverted (same
configuration as above except CC1P=1).
Figure 157. Example of encoder interface mode with TI1FP1 polarity inverted
TI1
TI2
Counter
down up down
MS33108V1
The timer, when configured in Encoder Interface mode provides information on the sensor’s
current position. Dynamic information can be obtained (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. This can be done 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.
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.
TI1
UG
Counter register 30 31 32 33 34 35 36 00 01 02 03 00 01 02 03
TIF
MS31401V2
TI1
cnt_en
Counter register 30 31 32 33 34 35 36 37 38
TIF
Write TIF=0
MS31402V1
TI2
cnt_en
Counter register 34 35 36 37 38
TIF
MS31403V1
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
Res. Res. Res. Res. Res. Res. Res. Res. Res. MMS[2:0] Res. Res. Res. Res.
rw rw rw
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
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. Res. Res. Res. Res. Res. Res. Res. Res. TIE Res. Res. Res. CC2IE CC1IE UIE
rw rw rw rw
w w w w
rw rw rw rw rw rw
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.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. TI2_RMP TI1_RMP ETR_RMP
rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. TI1_RMP ETR_RMP
rw rw rw rw
Offset Register
15
14
13
12
10
11
0
ARPE
CKD CMS
UDIS
OPM
CEN
URS
Res.
Res.
Res.
Res.
Res.
Res.
DIR
TIMx_CR1
0x00 [1:0] [1:0]
Reset value 0 0 0 0 0 0 0 0
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
TIMx_CR2 MMS[2:0]
0x04
Reset value 0 0 0
ETPS[1:0]
MSM
Res.
ECE
ETP
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
CC2IE
CC1IE
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
UIE
TIE
TIMx_DIER
0x0C
Reset value 0 0 0 0
CC2OF
CC1OF
CC2IF
CC1IF
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
UIF
TIMx_SR TIF
0x10
Reset value 0 0 0 0 0 0
CC2G
CC1G
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
UG
TG
TIMx_EGR
0x14
Reset value 0 0 0 0
TIMx_CCMR1
OC2PE
OC1PE
OC2FE
OC1FE
Res.
Output Compare
[2:0] [1:0] [2:0] [1:0]
mode
CC1NP
CC2P
CC2E
CC1P
CC1E
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
TIMx_CCER
0x20
Reset value 0 0 0 0 0 0
TIMx_CNT CNT[15:0]
0x24
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Offset Register
15
14
13
12
10
11
0
TIMx_PSC PSC[15:0]
0x28
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIMx_ARR ARR[15:0]
0x2C
Reset value 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0x30 Res.
TIMx_CCR1 CCR1[15:0]
0x34
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIMx_CCR2 CCR2[15:0]
0x38
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x3C to
Res.
0x4C
TIMx_CCR2 CCR2[15:0]
0x38
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ETR_RMP
TI2_RMP
TI1_RMP
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
TIM21_OR
0x50
Reset value 0 0 0 0 0 0
ETR_RMP
TI1_RMP
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
TIM22_OR
0x50
Reset value 0 0 0 0
18.1 Introduction
The basic timers TIM6, TIM7 consist of a 16-bit auto-reload counter driven by a
programmable prescaler.
Trigger TRGO
Internal clock (CK_INT) Controller
TIMxCLK from RCC
Auto-reload register
U UI
Stop, clear or up
U
CK_PSC PSC CK_CNT +
prescaler CNT counter
Notes:
Preload registers transferred
Reg
to active registers on U event
according to control bit
Event
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 162 and Figure 163 give some examples of the counter behavior when the prescaler
ratio is changed on the fly.
Figure 162. Counter timing diagram with prescaler division change from 1 to 2
CK_PSC
CEN
Timerclock = CK_CNT
Counter register F7 F8 F9 FA FB FC 00 01 02 03
Prescaler buffer 0 1
Prescaler counter 0 0 1 0 1 0 1 0 1
MS31076V2
Figure 163. Counter timing diagram with prescaler division change from 1 to 4
CK_PSC
CEN
Timerclock = CK_CNT
F7 F8 F9 FA FB FC 00 01
Counter register
0 3
Prescaler control register
0 3
Prescaler buffer
0 0 1 2 3 0 1 2 3
Prescaler counter
MS31077V2
CK_PSC
CNT_EN
Timerclock = CK_CNT
Counter register 31 32 33 34 35 36 00 01 02 03 04 05 06 07
Counter overflow
MS31078V2
CK_PSC
CNT_EN
Timerclock = CK_CNT
Counter overflow
MS31079V2
CK_PSC
CNT_EN
Timerclock = CK_CNT
Counter overflow
MS31080V2
CK_PSC
Timerclock = CK_CNT
Counter register 1F 20 00
Counter overflow
MS31081V2
Figure 168. Counter timing diagram, update event when ARPE = 0 (TIMx_ARR not
preloaded)
CK_PSC
CEN
Timerclock = CK_CNT
Counter register 31 32 33 34 35 36 00 01 02 03 04 05 06 07
Counter overflow
Auto-reload preload
register FF 36
Figure 169. Counter timing diagram, update event when ARPE=1 (TIMx_ARR
preloaded)
CK_PSC
CEN
Timerclock = CK_CNT
Counter register F0 F1 F2 F3 F4 F5 00 01 02 03 04 05 06 07
Counter overflow
Auto-reload shadow
register F5 36
Internal clock
CEN=CNT_EN
UG
CNT_INIT
Counter register 31 32 33 34 35 36 00 01 02 03 04 05 06 07
MS31085V2
rw rw rw
rw rw
rc_w0
Offset Register
15
14
13
12
10
11
0
ARPE
UDIS
OPM
CEN
URS
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
TIMx_CR1
0x00
Reset value 0 0 0 0 0
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
TIMx_CR2 MMS[2:0]
0x04
Reset value 0 0 0
0x08 Res.
UDE
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
UIE
TIMx_DIER
0x0C
Reset value 0 0
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
UIF
TIMx_SR
0x10
Reset value 0
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
UG
TIMx_EGR
0x14
Reset value 0
0x18 Res.
0x1C Res.
0x20 Res.
TIMx_CNT CNT[15:0]
0x24
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIMx_PSC PSC[15:0]
0x28
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIMx_ARR ARR[15:0]
0x2C
Reset value 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
19.1 Introduction
The LPTIM is a 16-bit timer that benefits from the ultimate developments in power
consumption reduction. Thanks to its diversity of clock sources, the LPTIM is able to keep
running in all power modes except for Standby mode. Given its capability to run even with
no internal clock source, the LPTIM can be used as a “Pulse Counter” which can be useful
in some applications. Also, the LPTIM capability to wake up the system from low-power
modes, makes it suitable to realize “Timeout functions” with extremely low power
consumption.
The LPTIM introduces a flexible clock scheme that provides the needed functionalities and
performance, while minimizing the power consumption.
Encoder mode X
1. X = supported.
LPTIM
APB_ITF
Kernel
Up/down
Encoder Glitch
Input 2
filter
Glitch
Input 1
filter
up to 8 ext
trigger
Glitch sw
filter trigger
16-bit ARR
RCC
Mux trigger
1
Out
‘1' 0 COUNT 16-bit counter
APB clock MODE
1
LSE CLKMUX 0 Prescaler
LSI
HSI16 16-bit compare
CKSEL
MS32468V2
CLKMUX
Input
Filter out
Note: In case no internal clock signal is provided, the digital filter must be deactivated by setting
the CKFLT and TRGFLT bits to ‘0’. In that case, an external analog filter may be used to
protect the LPTIM external inputs against glitches.
19.4.5 Prescaler
The LPTIM 16-bit counter is preceded by a configurable power-of-2 prescaler. The prescaler
division ratio is controlled by the PRESC[2:0] 3-bit field. The table below lists all the possible
division ratios:
000 /1
001 /2
010 /4
011 /8
100 /16
101 /32
110 /64
111 /128
One-shot mode
To enable the one-shot counting, the SNGSTRT bit must be set.
A new trigger event will re-start the timer. Any trigger event occurring after the counter starts
and before the counter reaches ARR will be discarded.
In case an external trigger is selected, each external trigger event arriving after the
SNGSTRT bit is set, and after the counter register has stopped (contains zero value), will
start the counter for a new one-shot counting cycle as shown in Figure 173.
LPTIM_ARR
Compare
PWM
LPTIM_ARR
Compare
Discarded trigger
PWM
In case of software start (TRIGEN[1:0] = ‘00’), the SNGSTRT setting will start the counter for
one-shot counting.
Continous mode
To enable the continuous counting, the CNTSTRT bit must be set.
In case an external trigger is selected, an external trigger event arriving after CNTSTRT is
set will start the counter for continuous counting. Any subsequent external trigger event will
be discarded as shown in Figure 175.
In case of software start (TRIGEN[1:0] = ‘00’), setting CNTSTRT will start the counter for
continuous counting.
LPTIM_ARR
Compare
PWM
SNGSTRT and CNTSTRT bits can only be set when the timer is enabled (The ENABLE bit
is set to ‘1’). It is possible to change “on the fly” from One-shot mode to Continuous mode.
If the Continuous mode was previously selected, setting SNGSTRT will switch the LPTIM to
the One-shot mode. The counter (if active) will stop as soon as it reaches ARR.
If the One-shot mode was previously selected, setting CNTSTRT will switch the LPTIM to
the Continuous mode. The counter (if active) will restart as soon as it reaches ARR.
The LPTIM output waveform can be configured through the WAVE bit as follow:
• Resetting the WAVE bit to ‘0’ forces the LPTIM to generate either a PWM waveform or
a One pulse waveform depending on which bit is set: CNTSTRT or SNGSTRT.
• Setting the WAVE bit to ‘1’ forces the LPTIM to generate a Set-once mode waveform.
The WAVPOL bit controls the LPTIM output polarity. The change takes effect immediately,
so the output default value will change immediately after the polarity is re-configured, even
before the timer is enabled.
Signals with frequencies up to the LPTIM clock frequency divided by 2 can be generated.
Figure 176 below shows the three possible waveforms that can be generated on the LPTIM
output. Also, it shows the effect of the polarity change using the WAVPOL bit.
LPTIM_ARR
Compare
PWM
Set once
PWM
Set once
MS32467V2
counter comparator. Within this latency period, any additional write into these registers must
be avoided.
The ARROK flag and the CMPOK flag in the LPTIM_ISR register indicate when the write
operation is completed to respectively the LPTIM_ARR register and the LPTIM_CMP
register.
After a write to the LPTIM_ARR register or the LPTIM_CMP register, a new write operation
to the same register can only be performed when the previous write operation is completed.
Any successive write before respectively the ARROK flag or the CMPOK flag be set, will
lead to unpredictable results.
The following figure shows a counting sequence for Encoder mode where both-edge
sensitivity is configured.
Caution: In this mode the LPTIM must be clocked by an internal clock source, so the CKSEL bit must
be maintained to its reset value which is equal to ‘0’. Also, the prescaler division ratio must
be equal to its reset value which is 1 (PRESC[2:0] bits must be ‘000’).
T1
T2
Counter
up down up
MS32491V1
Sleep No effect. LPTIM interrupts cause the device to exit Sleep mode.
Stop The LPTIM peripheral is active when it is clocked by LSE or LSI. LPTIM
interrupts cause the device to exit Stop mode
The LPTIM peripheral is powered down and must be reinitialized after
Standby
exiting Standby mode.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
ARR CMP EXT
Res. Res. Res. Res. Res. Res. Res. Res. Res. DOWN UP ARRM CMPM
OK OK TRIG
r r r r r r r
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
DOWN ARRO CMPO EXTTR ARRM CMPM
Res. Res. Res. Res. Res. Res. Res. Res. Res. UPCF
CF KCF KCF IGCF CF CF
w w w w w w w
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
DOWNI ARRO CMPO EXT ARRM CMPM
Res. Res. Res. Res. Res. Res. Res. Res. Res. UPIE
E KIE KIE TRIGIE IE IE
rw rw rw rw rw rw rw
Caution: The LPTIM_IER register must only be modified when the LPTIM is disabled (ENABLE bit reset to ‘0’)
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
COUNT
Res. Res. Res. Res. Res. Res. Res. ENC PRELOAD WAVPOL WAVE TIMOUT TRIGEN[1:0] Res.
MODE
rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
TRIGSEL[2:0] Res. PRESC[2:0] Res. TRGFLT[1:0] Res. CKFLT[1:0] CKPOL[1:0] CKSEL
rw rw rw rw rw rw rw rw rw rw rw rw rw
Caution: The LPTIM_CFGR register must only be modified when the LPTIM is disabled (ENABLE bit
reset to ‘0’).
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CNT SNG ENA
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
STRT STRT BLE
rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CMP[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Caution: The LPTIM_CMP register must only be modified when the LPTIM is enabled (ENABLE bit
set to ‘1’).
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
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
Caution: The LPTIM_ARR register must only be modified when the LPTIM is enabled (ENABLE bit
set to ‘1’).
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CNT[15:0]
r r r r r r r r r r r r r r r r
0x01C
0x00C
19.7.9
RM0377
LPTIM_CR
LPTIM_IER
LPTIM_ISR
LPTIM_ICR
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
LPTIM_CNT
LPTIM_ARR
LPTIM_CMP
LPTIM_CFGR
Offset Register name
Res. Res. Res. Res. Res. Res. Res. Res. 31
Res. Res. Res. Res. Res. Res. Res. Res. 30
Res. Res. Res. Res. Res. Res. Res. Res. 29
Res. Res. Res. Res. Res. Res. Res. Res. 28
Res. Res. Res. Res. Res. Res. Res. Res. 27
LPTIM register map
RM0377 Rev 10
TRIGEN
0 0 0 0 0 0 0 0
Res. Res. Res. Res. Res. Res. Res. 17
Res. Res. Res. Res. Res. Res. Res. Res. 16
The following table summarizes the LPTIM registers.
0 0 0
Res. Res. Res. Res. 13
Res. Res. Res. Res. Res. 12
Res. Res. Res. Res. 11
Table 91. LPTIM register map and reset values
0 0 0
Res. Res. Res. Res. 9
If LPTIM does not support encoder mode feature, this bit is reserved. Please refer to Section 19.3: LPTIM implementation.
Res. DOWNIE(1) DOWNCF(1) DOWN(1) 6
CNT[15:0]
ARR[15:0]
CMP[15:0]
Res. Res. UPIE(1) UPCF(1) UP(1) 5
Res. ARROKIE ARROKCF ARROK 4
CKFLT
Res. CMPOKIE CMPOKCF CMPOK 3
CNTSTRT EXTTRIGIE EXTTRIGCF EXTTRIG 2
CKPOL
SNGSTRT ARRMIE ARRMCF ARRM 1
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 1
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 0 0 0 0 0 0
0 0 0 0 0 0 0
527/905
Low-power timer (LPTIM)
527
Independent watchdog (IWDG) RM0377
20.1 Introduction
The devices feature an embedded watchdog peripheral that offers a combination of high
safety level, timing accuracy and flexibility of use. The Independent watchdog peripheral
detects and solves malfunctions due to software failure, and triggers system reset 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 IWDG is best suited for applications that require the watchdog to run as a totally
independent process outside the main application, but have lower timing accuracy
constraints. For further information on the window watchdog, refer to Section 21 on page
537.
VDD
Prescaler register Status register Reload register Key register
IWDG_PR IWDG_SR IWDG_RLR IWDG_KR
MSv37838V1
1. The register interface is located in the VDD voltage domain. The watchdog function is located in the VDD
voltage domain, still functional in Standby mode.
When the independent watchdog is started by writing the value 0x0000 CCCC in the IWDG
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).
Whenever the key value 0x0000 AAAA is written in the IWDG key register (IWDG_KR), the
IWDG_RLR value is reloaded in the counter and the watchdog reset is prevented.
Once running, the IWDG cannot be stopped.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
KEY[15:0]
w w w w w w w w w w w w w w w w
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. PR[2:0]
rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. Res. Res. Res. RL[11:0]
rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. WVU RVU PVU
r r r
Note: If several reload, prescaler, or window values are used by the application, it is mandatory to
wait until RVU bit is reset before changing the reload value, to wait until PVU bit is reset
before changing the prescaler value, and to wait until WVU bit is reset before changing the
window value. However, after updating the prescaler and/or the reload/window value it is not
necessary to wait until RVU or PVU or WVU is reset before continuing code execution
except in case of low-power mode entry.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. Res. Res. Res. WIN[11:0]
rw rw rw rw rw rw rw rw rw rw rw rw
0x0C
Offset
536/905
20.4.6
name
IWDG_SR
IWDG_PR
IWDG_KR
Register
Reset value
Reset value
Reset value
Reset value
Reset value
IWDG_RLR
IWDG_WINR
Res. Res. Res. Res. Res. 31
Res. Res. Res. Res. Res. 30
Res. Res. Res. Res. Res. 29
Res. Res. Res. Res. Res. 28
Res. Res. Res. Res. Res. 27
Independent watchdog (IWDG)
25
Res. Res. Res. Res. Res. 24
Res. Res. Res. Res. Res. 23
Res. Res. Res. Res. Res. 22
Res. Res. Res. Res. Res. 21
Res. Res. Res. Res. Res. 20
Res. Res. Res. Res. Res. 19
Res. Res. Res. Res. Res. 18
Res. Res. Res. Res. Res. 17
RM0377 Rev 10
Res. Res. Res. Res. Res. 16
0
Res. Res. 11
Table 92. IWDG register map and reset values
1
1
0
Res. Res. 10
The following table gives the IWDG register map and reset values.
1
1
0
Res. Res. 9
Refer to Section 2.2 on page 51 for the register boundary addresses.
1
1
0
Res. Res. 8
1
1
0
Res. Res. 7
KEY[15:0]
1
1
0
Res. Res. 6
1
1
0
Res. Res. 5
RL[11:0]
WIN[11:0]
1
1
0
Res. Res. 4
1
1
0
Res. Res. 3
1
1
0
0
0
WVU 2
1
1
0
0
0
RVU 1
PR[2:0]
1
1
0
0
0
PVU
RM0377
0
RM0377 System window watchdog (WWDG)
21.1 Introduction
The system window watchdog (WWDG) 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 down-counter before the T6 bit becomes cleared. An MCU
reset is also generated if the 7-bit down-counter value (in the control register) is refreshed
before the down-counter has reached the window register value. This implies that the
counter must be refreshed in a limited window.
The 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 WWDG is best suited for applications which require the watchdog to react within an
accurate timing window.
WWDG
Register interface CMP = 1 when
W[6:0] T[6:0] > W[6:0]
APB bus
WWDG_CFR
CMP
wwdg_out_rst
WWDG_SR WDGA
Write to WWDG_CR
T[6:0] T6
= 0x40 ?
readback
Logic
WWDG_CR T[6:0] EWI wwdg_it
cnt_out EWIF
preload
7-bit DownCounter (CNT)
MS47214V1
T[6:0]
W[6:0]
0x3F
Time
Tpclk x 4096 x 2WDGTB
0x41
0x40
0x3F
wwdg_ewit
EWIF = 0
wwdg_rst
T6 bit
MS47266V1
where:
tWWDG: WWDG timeout
tPCLK: APB1 clock period measured in ms
4096: value corresponding to internal divider
As an example, if APB1 frequency is 32 MHz, WDGTB[1:0] is set to 3 and T[5:0] is set to 63:
3
t WWDG = ( 1 ⁄ 32000 ) × 4096 × 2 × ( 63 + 1 ) = 65.54ms
Refer to the datasheet for the minimum and maximum values of tWWDG.
For code example, refer to A.12.1: WWDG configuration code example.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. Res. Res. Res. Res. Res. Res. Res. WDGA T[6:0]
rs rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. Res. Res. Res. Res. Res. EWI WDGTB[1:0] W[6:0]
rs rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. EWIF
rc_w0
Offset Register
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
10
11
9
8
7
6
5
4
3
2
1
0
WDGA
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
WWDG_CR T[6:0]
0x000
Reset value 0 1 1 1 1 1 1 1
WDGTB1
WDGTB0
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
EWI
WWDG_CFR W[6:0]
0x004
Reset value 0 0 0 1 1 1 1 1 1 1
EWIF
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
WWDG_SR
0x008
Reset value 0
22.1 Introduction
The RTC provides an automatic wakeup to manage all low-power modes.
The real-time clock (RTC) is an independent BCD timer/counter. The RTC provides a time-
of-day clock/calendar with programmable alarm interrupts.
The RTC includes also a periodic programmable wakeup flag with interrupt capability.
Two 32-bit registers contain the seconds, minutes, hours (12- or 24-hour format), day (day
of week), date (day of month), month, and year, expressed in binary coded decimal format
(BCD). The sub-seconds value is also available in binary format.
Compensations for 28-, 29- (leap year), 30-, and 31-day months are performed
automatically. Daylight saving time compensation can also be performed.
Additional 32-bit registers contain the programmable alarm subseconds, seconds, minutes,
hours, day, and date.
A digital calibration feature is available to compensate for any deviation in crystal oscillator
accuracy.
After RTC domain reset, all RTC registers are protected against possible parasitic write
accesses.
As long as the supply voltage remains in the operating range, the RTC never stops,
regardless of the device status (Run mode, low-power mode or under reset).
Prescaler
2, 4, 8, 16
RTC_CALIB
Ouput RTC_OUT
RTC_ALARM control
RTC_WUTR
WUTF
16-bit wakeup
auto reload timer
OSEL[1:0]
Alarm A
= ALRAF
RTC_ALRMAR
RTC_ALRMASSR
Alarm B
= ALRBF
RTC_ALRMBR
RTC_ALRMBSSR
MS33460V5
RTC_ALARM 0
01 or 10 or 11 Don’t care 0 Don’t care Don’t care
output OD 1
RTC_ALARM 0
01 or 10 or 11 Don’t care 1 Don’t care Don’t care
output PP 1
RTC_CALIB
00 1 0 Don’t care Don’t care Don’t care
output PP
00 0 Don’t care
RTC_TAMP1
00 1 Don’t care 1 0
input floating 1
01 or 10 or 11 0
00 0 Don’t care
RTC_TS and
RTC_TAMP1 00 1 Don’t care 1 1
input floating 1
01 or 10 or 11 0
00 0 Don’t care
RTC_TS input
00 1 Don’t care 0 1
floating 1
01 or 10 or 11 0
00 0 Don’t care
Wakeup pin or
Standard 00 1 Don’t care 0 0
GPIO 1
01 or 10 or 11 0
1. OD: open drain; PP: push-pull.
00 0 - -
00 1 0 RTC_CALIB -
01 or 10 or 11 Don’t care RTC_ALARM -
00 0 - -
00 1 - RTC_CALIB
1
01 or 10 or 11 0 - RTC_ALARM
01 or 10 or 11 1 RTC_ALARM RTC_CALIB
The ck_apre clock is used to clock the binary RTC_SSR subseconds downcounter. When it
reaches 0, RTC_SSR is reloaded with the content of PREDIV_S.
fck_spre is given by the following formula:
f RTCCLK
f CK_SPRE = ----------------------------------------------------------------------------------------------
( PREDIV_S + 1 ) × ( PREDIV_A + 1 )
The ck_spre clock can be used either to update the calendar or as timebase for the 16-bit
wakeup auto-reload timer. To obtain short timeout periods, the 16-bit wakeup auto-reload
timer can also run with the RTCCLK divided by the programmable 4-bit asynchronous
prescaler (see Section 22.4.6: Periodic auto-wakeup for details).
BYPSHAD control bit in the RTC_CR register. By default, this bit is cleared, and the user
accesses the shadow registers.
When reading the RTC_SSR, RTC_TR or RTC_DR registers in BYPSHAD=0 mode, the
frequency of the APB clock (fAPB) must be at least 7 times the frequency of the RTC clock
(fRTCCLK).
The shadow registers are reset by system reset.
When the periodic wakeup interrupt is enabled by setting the WUTIE bit in the RTC_CR
register, it can exit the device from low-power modes.
The periodic wakeup flag can be routed to the RTC_ALARM output provided it has been
enabled through bits OSEL[1:0] of RTC_CR register. RTC_ALARM output polarity can be
configured through the POL bit in the RTC_CR register.
System reset, as well as low-power modes (Sleep, Stop and Standby) have no influence on
the wakeup timer.
Note: After a system reset, the application can read the INITS flag in the RTC_ISR register to
check if the calendar has been initialized or not. If this flag equals 0, the calendar has not
been initialized since the year field is set at its RTC domain reset default value (0x00).
To read the calendar after initialization, the software must first check that the RSF flag is set
in the RTC_ISR register.
For code example, refer to A.13.1: RTC calendar configuration code example.
If the APB1 clock frequency is less than seven times the RTC clock frequency, the software
must read the calendar time and date registers twice. If the second read of the RTC_TR
gives the same result as the first read, this ensures that the data is correct. Otherwise a third
read access must be done. In any case the APB1 clock frequency must never be lower than
the RTC clock frequency.
The RSF bit is set in RTC_ISR register each time the calendar registers are copied into the
RTC_SSR, RTC_TR and RTC_DR shadow registers. The copy is performed every
RTCCLK cycle. To ensure consistency between the 3 values, reading either RTC_SSR or
RTC_TR locks the values in the higher-order calendar shadow registers until RTC_DR is
read. In case the software makes read accesses to the calendar in a time interval smaller
than 1 RTCCLK period: RSF must be cleared by software after the first calendar read, and
then the software must wait until RSF is set before reading again the RTC_SSR, RTC_TR
and RTC_DR registers.
After waking up from low-power mode (Stop or Standby), RSF must be cleared by software.
The software must then wait until it is set again before reading the RTC_SSR, RTC_TR and
RTC_DR registers.
The RSF bit must be cleared after wakeup and not before entering low-power mode.
After a system reset, the software must wait until RSF is set before reading the RTC_SSR,
RTC_TR and RTC_DR registers. Indeed, a system reset resets the shadow registers to
their default values.
After an initialization (refer to Calendar initialization and configuration on page 550): the
software must wait until RSF is set before reading the RTC_SSR, RTC_TR and RTC_DR
registers.
After synchronization (refer to Section 22.4.10: RTC synchronization): the software must
wait until RSF is set before reading the RTC_SSR, RTC_TR and RTC_DR registers.
For code example, refer to A.13.4: RTC read calendar code example.
When the BYPSHAD control bit is set in the RTC_CR register (bypass shadow
registers)
Reading the calendar registers gives the values from the calendar counters directly, thus
eliminating the need to wait for the RSF bit to be set. This is especially useful after exiting
from low-power modes (STOP or Standby), since the shadow registers are not updated
during these modes.
When the BYPSHAD bit is set to 1, the results of the different registers might not be
coherent with each other if an RTCCLK edge occurs between two read accesses to the
registers. Additionally, the value of one of the registers may be incorrect if an RTCCLK edge
occurs during the read operation. The software must read all the registers twice, and then
compare the results to confirm that the data is coherent and correct. Alternatively, the
software can just compare the two results of the least-significant calendar register.
Note: While BYPSHAD=1, instructions which read the calendar registers require one extra APB
cycle to complete.
On the contrary, the following registers are reset to their default values by a RTC domain
reset and are not affected by a system reset: the RTC current calendar registers, the RTC
control register (RTC_CR), the prescaler register (RTC_PRER), the RTC calibration register
(RTC_CALR), the RTC shift register (RTC_SHIFTR), the RTC timestamp registers
(RTC_TSSSR, RTC_TSTR and RTC_TSDR), the RTC tamper configuration register
(RTC_TAMPCR), the RTC backup registers (RTC_BKPxR), the wakeup timer register
(RTC_WUTR), the Alarm A and Alarm B registers (RTC_ALRMASSR/RTC_ALRMAR and
RTC_ALRMBSSR/RTC_ALRMBR), and the Option register (RTC_OR).
In addition, when it is clocked by the LSE, the RTC keeps on running under system reset if
the reset source is different from the RTC domain reset one (refer to the RTC clock section
of the Reset and clock controller for details on the list of RTC clock sources not affected by
system reset). When a RTC domain reset occurs, the RTC is stopped and all the RTC
registers are set to their reset values.
detection is enabled (REFCKON bit of RTC_CR set to 1), the calendar is still clocked by the
LSE, and RTC_REFIN is used to compensate for the imprecision of the calendar update
frequency (1 Hz).
Each 1 Hz clock edge is compared to the nearest RTC_REFIN clock edge (if one is found
within a given time window). In most cases, the two clock edges are properly aligned. When
the 1 Hz clock becomes misaligned due to the imprecision of the LSE clock, the RTC shifts
the 1 Hz clock a bit so that future 1 Hz clock edges are aligned. Thanks to this mechanism,
the calendar becomes as precise as the reference clock.
The RTC detects if the reference clock source is present by using the 256 Hz clock
(ck_apre) generated from the 32.768 kHz quartz. The detection is performed during a time
window around each of the calendar updates (every 1 s). The window equals 7 ck_apre
periods when detecting the first reference clock edge. A smaller window of 3 ck_apre
periods is used for subsequent calendar updates.
Each time the reference clock is detected in the window, the synchronous prescaler which
outputs the ck_spre clock is forced to reload. This has no effect when the reference clock
and the 1 Hz clock are aligned because the prescaler is being reloaded at the same
moment. When the clocks are not aligned, the reload shifts future 1 Hz clock edges a little
for them to be aligned with the reference clock.
If the reference clock halts (no reference clock edge occurred during the 3 ck_apre window),
the calendar is updated continuously based solely on the LSE clock. The RTC then waits for
the reference clock using a large 7 ck_apre period detection window centered on the
ck_spre edge.
When the RTC_REFIN detection is enabled, PREDIV_A and PREDIV_S must be set to their
default values:
• PREDIV_A = 0x007F
• PREVID_S = 0x00FF
Note: RTC_REFIN clock detection is not available in Standby mode.
Note: CALM[8:0] (RTC_CALR) specifies the number of RTCCLK pulses to be masked during the
32-second cycle. Setting the bit CALM[0] to ‘1’ causes exactly one pulse to be masked
during the 32-second cycle at the moment when cal_cnt[19:0] is 0x80000; CALM[1]=1
causes two other cycles to be masked (when cal_cnt is 0x40000 and 0xC0000); CALM[2]=1
causes four other cycles to be masked (cal_cnt = 0x20000/0x60000/0xA0000/ 0xE0000);
and so on up to CALM[8]=1 which causes 256 clocks to be masked (cal_cnt = 0xXX800).
While CALM allows the RTC frequency to be reduced by up to 487.1 ppm with fine
resolution, the bit CALP can be used to increase the frequency by 488.5 ppm. Setting CALP
to ‘1’ effectively inserts an extra RTCCLK pulse every 211 RTCCLK cycles, which means
that 512 clocks are added during every 32-second cycle.
Using CALM together with CALP, an offset ranging from -511 to +512 RTCCLK cycles can
be added during the 32-second cycle, which translates to a calibration range of -487.1 ppm
to +488.5 ppm with a resolution of about 0.954 ppm.
The formula to calculate the effective calibrated frequency (FCAL) given the input frequency
(FRTCCLK) is as follows:
FCAL = FRTCCLK x [1 + (CALP x 512 - CALM) / (220 + CALM - CALP x 512)]
However, this measurement error can be eliminated if the measurement period is the same
length as the calibration cycle period. In this case, the only error observed is the error due to
the resolution of the digital calibration.
• By default, the calibration cycle period is 32 seconds.
Using this mode and measuring the accuracy of the 1 Hz output over exactly 32 seconds
guarantees that the measure is within 0.477 ppm (0.5 RTCCLK cycles over 32 seconds, due
to the limitation of the calibration resolution).
• CALW16 bit of the RTC_CALR register can be set to 1 to force a 16- second calibration
cycle period.
In this case, the RTC precision can be measured during 16 seconds with a maximum error
of 0.954 ppm (0.5 RTCCLK cycles over 16 seconds). However, since the calibration
resolution is reduced, the long term RTC precision is also reduced to 0.954 ppm: CALM[0]
bit is stuck at 0 when CALW16 is set to 1.
• CALW8 bit of the RTC_CALR register can be set to 1 to force a 8- second calibration
cycle period.
In this case, the RTC precision can be measured during 8 seconds with a maximum error of
1.907 ppm (0.5 RTCCLK cycles over 8s). The long term RTC precision is also reduced to
1.907 ppm: CALM[1:0] bits are stuck at 00 when CALW8 is set to 1.
Re-calibration on-the-fly
The calibration register (RTC_CALR) can be updated on-the-fly while RTC_ISR/INITF=0, by
using the follow process:
1. Poll the RTC_ISR/RECALPF (re-calibration pending flag).
2. If it is set to 0, write a new value to RTC_CALR, if necessary. RECALPF is then
automatically set to 1
3. Within three ck_apre cycles after the write operation to RTC_CALR, the new calibration
settings take effect.
For code example, refer to A.13.5: RTC calibration code example.
Note: TSF is set 2 ck_apre cycles after the time-stamp event occurs due to synchronization
process.
There is no delay in the setting of TSOVF. This means that if two time-stamp events are
close together, TSOVF can be seen as '1' while TSF is still '0'. As a consequence, it is
recommended to poll TSOVF only after TSF has been set.
Caution: If a time-stamp event occurs immediately after the TSF bit is supposed to be cleared, then
both TSF and TSOVF bits are set.To avoid masking a time-stamp event occurring at the
same moment, the application must not write ‘0’ into TSF bit unless it has already read it to
‘1’.
Optionally, a tamper event can cause a time-stamp to be recorded. See the description of
the TAMPTS control bit in Section 22.7.16: RTC tamper configuration register
(RTC_TAMPCR).
A new tamper occurring on the same pin cannot be detected during the latency described
above and 2.5 ck_rtc additional cycles.
By setting the TAMPIE bit in the RTC_TAMPCR register, an interrupt is generated when a
tamper detection event occurs (when TAMPxF is set). Setting TAMPIE is not allowed when
one or more TAMPxMF is set.
When TAMPIE is cleared, each tamper pin event interrupt can be individually enabled by
setting the corresponding TAMPxIE bit in the RTC_TAMPCR register. Setting TAMPxIE is
not allowed when the corresponding TAMPxMF is set.
The RTC_TAMPx inputs are precharged through the I/O internal pull-up resistance before
its state is sampled, unless disabled by setting TAMPPUDIS to 1,The duration of the
precharge is determined by the TAMPPRCH bits, allowing for larger capacitances on the
RTC_TAMPx inputs.
The trade-off between tamper detection latency and power consumption through the pull-up
can be optimized by using TAMPFREQ to determine the frequency of the sampling for level
detection.
Note: Refer to the datasheets for the electrical characteristics of the pull-up resistors.
For code example, refer to A.13.6: RTC tamper and time stamp configuration code example.
Alarm output
The RTC_ALARM pin can be configured in output open drain or output push-pull using the
control bit RTC_ALARM_TYPE in the RTC_OR register.
Note: Once the RTC_ALARM output is enabled, it has priority over RTC_CALIB (COE bit is don't
care and must be kept cleared).
No effect
Sleep
RTC interrupts cause the device to exit the Sleep mode.
The RTC remains active when the RTC clock source is LSE or LSI. RTC alarm, RTC
Stop tamper event, RTC timestamp event, and RTC Wakeup cause the device to exit the Stop
mode.
The RTC remains active when the RTC clock source is LSE or LSI. RTC alarm, RTC
Standby tamper event, RTC timestamp event, and RTC Wakeup cause the device to exit the
Standby mode.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. PM HT[1:0] HU[3:0]
rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. MNT[2:0] MNU[3:0] Res. ST[2:0] SU[3:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. YT[3:0] YU[3:0]
rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
WDU[2:0] MT MU[3:0] Res. Res. DT[1:0] DU[3:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. COE OSEL[1:0] POL COSEL BKP SUB1H ADD1H
rw rw rw rw rw rw w w
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
BYPS
TSIE WUTIE ALRBIE ALRAIE TSE WUTE ALRBE ALRAE Res. FMT REFCKON TSEDGE WUCKSEL[2:0]
HAD
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Note: Bits 7, 6 and 4 of this register can be written in initialization mode only (RTC_ISR/INITF = 1).
WUT = Wakeup unit counter value. WUT = (0x0000 to 0xFFFF) + 0x10000 added when
WUCKSEL[2:1 = 11].
Bits 2 to 0 of this register can be written only when RTC_CR WUTE bit = 0 and RTC_ISR
WUTWF bit = 1.
It is recommended not to change the hour during the calendar hour increment as it could
mask the incrementation of the calendar hour.
ADD1H and SUB1H changes are effective in the next second.
This register is write protected. The write access procedure is described in RTC register
write protection on page 550.
Caution: TSE must be reset when TSEDGE is changed to avoid spuriously setting of TSF.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. RECALPF
r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
ALRB
TAMP3F TAMP2F TAMP1F TSOVF TSF WUTF ALRBF ALRAF INIT INITF RSF INITS SHPF WUTWF ALRAWF
WF
rc_w0 rc_w0 rc_w0 rc_w0 rc_w0 rc_w0 rc_w0 rc_w0 rw r rc_w0 r r r r r
Note: The bits ALRAF, ALRBF, WUTF and TSF are cleared 2 APB clock cycles after programming
them to 0.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. PREDIV_A[6:0]
rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. PREDIV_S[14:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
WUT[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
MSK4 WDSEL DT[1:0] DU[3:0] MSK3 PM HT[1:0] HU[3: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
MSK2 MNT[2:0] MNU[3:0] MSK1 ST[2:0] SU[3:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
MSK4 WDSEL DT[1:0] DU[3:0] MSK3 PM HT[1:0] HU[3: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
MSK2 MNT[2:0] MNU[3:0] MSK1 ST[2:0] SU[3:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. Res. Res. Res. Res. Res. Res. Res. KEY[7:0]
w w w w w w w w
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
SS[15:0]
r r r r r r r r r r r r r r r r
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
ADD1S Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. SUBFS[14:0]
w w w w w w w w w w w w w w w
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. PM HT[1:0] HU[3:0]
r r r r r r r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. MNT[2:0] MNU[3:0] Res. ST[2:0] SU[3:0]
r r r r r r r r r r r r r r
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
WDU[2:0] MT MU[3:0] Res. Res. DT[1:0] DU[3:0]
r r r r r r r r r r r r r r
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
SS[15:0]
r r r r r r r r r r r r r r r r
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CALW
CALP CALW8 Res. Res. Res. Res. CALM[8:0]
16
rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
TAMP3 TAMP2 TAMP1
TAMP3 TAMP3 TAMP2 TAMP2 TAMP1 TAMP1
Res. Res. Res. Res. Res. Res. Res. NO NO NO
MF IE MF IE MF IE
ERASE ERASE ERASE
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
TAMP TAMPPRCH TAMP TAMP3 TAMP3 TAMP2 TAMP2 TAMPI TAMP1 TAMP1
TAMPFLT[1:0] TAMPFREQ[2:0]
PUDIS [1:0] TS TRG E TRG E E TRG E
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Caution: When TAMPFLT = 0, TAMPxE must be reset when TAMPxTRG is changed to avoid
spuriously setting TAMPxF.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. MASKSS[3:0] Res. Res. Res. Res. Res. Res. Res. Res.
rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. SS[14:0]
rw rw rw rw rw rw rw rw rw rw rw rw w rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. MASKSS[3:0] Res. Res. Res. Res. Res. Res. Res. Res.
rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. SS[14:0]
rw rw rw rw rw rw rw rw rw rw rw rw w rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
RTC_ RTC_
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. OUT_ ALARM
RMP _TYPE
rw rw
Bit 0 RTC_ALARM_TYPE: RTC_ALARM output type on PC13 (category 3 and 5)/ on PA2
(category 1 and 2)
This bit is set and cleared by software
On category 3 and 5 devices:
0: RTC_ALARM, when mapped on PC13, is open-drain output
1: RTC_ALARM, when mapped on PC13, is push-pull output
10
11
9
8
7
6
5
4
3
2
1
0
name
HT[1:0]
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
PM
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DT[1:0]
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
MT
Reset value 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1
WUCKSEL[2:0]
REFCKON
BYPSHAD
OSEL[1:0]
TSEDGE
ALRBIE
ALRAIE
COSEL
ADD1H
SUB1H
ALRBE
ALRAE
WUTIE
WUTE
TSIE
COE
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
FMT
POL
BKP
TSE
RTC_CR
0x08
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
RECALPF
ALRBWF
.TAMP2F
ALRAWF
WUT WF
TAMP3F
TAMP1F
TSOVF
ALRBF
ALRAF
WUTF
SHPF
INITS
INITF
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
RSF
TSF
INIT
RTC_ISR
0x0C
Reset value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
RTC_WUTR WUT[15:0]
0x14
Reset value 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
WDSEL
DT[1:0]
HT[1:0]
MSK4
MSK3
MSK2
MSK1
PM
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
0x3C
0x2C
Offset
586/905
RTC_
RTC_
name
RTC_SSR
RTC_WPR
Register
RTC_TSTR
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
RTC_TSDR
RTC_ CALR
ALRMBSSR
ALRMASSR
RTC_TSSSR
RTC_SHIFTR
RTC_TAMPCR
RTC_ALRMBR
0
0
Res. Res. Res. Res. Res. Res. Res. ADD1S Res. Res. MSK4 31
Real-time clock (RTC)
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. 0 WDSEL
0
30
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. 29
DT[1:0]
0
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. 28
0
0
0
0
0
0
[3:0]
[3:0]
0
0
0
MASKSS
MASKSS
0
0
0
0
0
0
Res. Res. TAMP3NOERASE Res. Res. Res. Res. Res. Res. Res. MSK3 23
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
RM0377 Rev 10
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
CALW8 Res. 14
TAMPPRCH[1:0]
0
0
0
0
0
0
0
0
0
0
WDU[1:0]
CALW16 MNT[2:0] Res. 13
0
0
0
0
0
0
0
0
0
MNT[2:0]
Res. MT Res. 12
TAMPFLT[1:0]
0
0
0
0
0
0
0
0
0
Res. Res. 11
0
0
0
0
0
0
0
0
0
Res. Res. 10
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Res. 8
0
0
0
0
0
0
0
0
0
SS[14:0]
SS[14:0]
0
0
0
0
0
0
0
0
0
0
TAMP3TRG Res. 6
SUBFS[14:0]
0
0
0
0
0
0
0
0
0
0
0
TAMP3E 5
DT[1:0]
ST[2:0]
ST[2:0]
0
0
0
0
0
0
0
0
0
0
0
TAMP2TRG 4
KEY
0
0
0
0
0
0
0
0
0
0
0
.TAMP2E
CALM[8:0]
3
0
0
0
0
0
0
0
0
0
0
0
.TAMPIE 2
0
0
0
0
0
0
0
0
0
0
0
TAMP1TRG 1
SU[3:0]
SU[3:0]
DU[3:0]
0
0
0
0
0
0
0
0
0
0
0
TAMP1E
RM0377
0
RM0377 Real-time clock (RTC)
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
10
11
9
8
7
6
5
4
3
2
1
0
name
RTC_ALARM_TYPE
RTC_OUT_RMP
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
RTC_ OR
0x4C
Reset value 0 0
RTC_BKP0R BKP[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
23.1 Introduction
The I2C (inter-integrated circuit) bus interface handles communications between the
microcontroller and the serial I2C bus. It provides multimaster capability, and controls all I2C
bus-specific sequencing, protocol, arbitration and timing. It supports Standard-mode (Sm),
Fast-mode (Fm) and Fast-mode Plus (Fm+).
It is also SMBus (system management bus) and PMBus (power management bus)
compatible.
DMA can be used to reduce CPU overload.
The following additional features are also available depending on the product
implementation (see Section 23.3: I2C implementation):
• SMBus specification rev 3.0 compatibility:
– Hardware PEC (packet error checking) generation and verification with ACK
control
– Command and data acknowledge control
– Address resolution protocol (ARP) support
– Host and Device support
– SMBus alert
– Timeouts and idle condition detection
• PMBus rev 1.3 standard compatibility
• Independent clock: a choice of independent clock sources allowing the I2C
communication speed to be independent from the PCLK reprogramming
• Wakeup from Stop mode on address match.
If SMBus feature is supported: the additional optional SMBus Alert pin (SMBA) is also
available.
I2CCLK
i2c_ker_ck
Data control
Digital Analog
Shift register noise noise GPIO
filter I2C_SDA
filter logic
SMBUS
PEC
generation/
check
Wakeup
on
address
match Clock control
Master clock
generation Digital Analog
noise noise
Slave clock GPIO I2C_SCL
filter filter
stretching logic
SMBus
Timeout
check
SMBus Alert
control/status I2C_SMBA
PCLK
i2c_pclk Registers
APB bus
MSv46198V2
The I2C1/3 is clocked by an independent clock source which allows the I2C to operate
independently from the PCLK frequency.
For I2C I/Os supporting 20mA output current drive for Fast-mode Plus operation, the driving
capability is enabled through control bits in the system configuration controller (SYSCFG).
Refer to Section 23.3: I2C implementation.
I2CCLK
PCLK
Data control
Digital Analog
Shift register noise noise GPIO
filter I2C1_SDA
filter logic
SMBUS
PEC
generation/
check
Wakeup
on
address
match Clock control
Master clock
generation Digital Analog
noise noise
Slave clock GPIO I2C1_SCL
filter filter
stretching logic
SMBus
Timeout
check
SMBus Alert
control/status I2C1_SMBA
Registers
APB bus
MSv46199V2
For I2C I/Os supporting 20 mA output current drive for Fast-mode Plus operation, the driving
capability is enabled through control bits in the system configuration controller (SYSCFG).
Refer to Section 23.3: I2C implementation.
By default, it operates in slave mode. The interface automatically switches from slave to
master when it generates a START condition, and from master to slave if an arbitration loss
or a STOP generation occurs, allowing multimaster capability.
Communication flow
In Master mode, the I2C 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 can be enabled or disabled by
software. The reserved SMBus addresses can also be enabled 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 ninth 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 the following figure.
SDA
MSB ACK
SCL
1 2 8 9
Start Stop
condition condition
MS19854V1
Acknowledge can be enabled or disabled by software. The I2C interface addresses can be
selected by software.
Noise filters
Before enabling the I2C peripheral by setting the PE bit in I2C_CR1 register, the user must
configure the noise filters, if needed. By default, an analog noise filter is present on the SDA
and SCL inputs. This analog filter is compliant with the I2C specification which requires the
suppression of spikes with a pulse width up to 50 ns in Fast-mode and Fast-mode Plus. The
user can disable this analog filter by setting the ANFOFF bit, and/or select a digital filter by
configuring the DNF[3:0] bit in the I2C_CR1 register.
When the digital filter is enabled, the level of the SCL or the SDA line is internally changed
only if it remains stable for more than DNF x I2CCLK periods. This allows spikes with a
programmable length of 1 to 15 I2CCLK periods to be suppressed.
Caution: Changing the filter configuration is not allowed when the I2C is enabled.
I2C timings
The timings must be configured in order to guarantee a correct data hold and setup time,
used in master and slave modes. This is done by programming the PRESC[3:0],
SCLDEL[3:0] and SDADEL[3:0] bits in the I2C_TIMINGR register.
The STM32CubeMX tool calculates and provides the I2C_TIMINGR content in the I2C
configuration window
SDA
tHD;DAT
Data hold time: in case of transmission, the data is sent on SDA output after
the SDADEL delay, if it is already available in I2C_TXDR.
SCLDEL
SCL stretched low by the I2C
SCL
SDA
tSU;DAT
SU;STA
Data setup time: in case of transmission, the SCLDEL counter starts
when the data is sent on SDA output. MSv40108V1
MS49608V1
• When the SCL falling edge is internally detected, a delay is inserted before sending
SDA output. This delay is tSDADEL = SDADEL x tPRESC + tI2CCLK where tPRESC = (PRESC+1)
x tI2CCLK.
TSDADEL impacts the hold time tHD;DAT.
In order to bridge the undefined region of the SDA transition (rising edge usually worst
case), the user must program SCLDEL in such a way that:
{[tr (max) + tSU;DAT (min)] / [(PRESC+1)] x tI2CCLK]} - 1 <= SCLDEL
Refer to Table 104: I2C-SMBus specification data setup and hold times for tr and tSU;DAT
standard values.
The SDA and SCL transition time values to be used are the ones in the application. Using
the maximum values from the standard increases the constraints for the SDADEL and
SCLDEL calculation, but ensures the feature whatever the application.
Note: At every clock pulse, after SCL falling edge detection, the I2C master or slave stretches SCL
low during at least [(SDADEL+SCLDEL+1) x (PRESC+1) + 1] x tI2CCLK, in both transmission
and reception modes. In transmission mode, in case the data is not yet written in I2C_TXDR
when SDADEL counter is finished, the I2C keeps on stretching SCL low until the next data
is written. Then new data MSB is sent on SDA output, and SCLDEL counter starts,
continuing stretching SCL low to guarantee the data setup time.
If NOSTRETCH = 1 in slave mode, the SCL is not stretched. Consequently the SDADEL
must be programmed in such a way to guarantee also a sufficient setup time.
Additionally, in master mode, the SCL clock high and low levels must be configured by
programming the PRESC[3:0], SCLH[7:0] and SCLL[7:0] bits in the I2C_TIMINGR register.
• When the SCL falling edge is internally detected, a delay is inserted before releasing
the SCL output. This delay is tSCLL = (SCLL+1) x tPRESC where tPRESC = (PRESC+1) x
tI2CCLK.
tSCLL impacts the SCL low time tLOW .
• When the SCL rising edge is internally detected, a delay is inserted before forcing the
SCL output to low level. This delay is tSCLH = (SCLH+1) x tPRESC where tPRESC =
(PRESC+1) x tI2CCLK. tSCLH impacts the SCL high time tHIGH .
Initial settings
Configure PRESC[3:0],
End
MS19847V2
Reception
The SDA input fills the shift register. After the eighth SCL pulse (when the complete data
byte is received), the shift register is copied into I2C_RXDR register if it is empty (RXNE =
0). If RXNE = 1, meaning that the previous received data byte has not yet been read, the
SCL line is stretched low until I2C_RXDR is read. The stretch is inserted between the eighth
and ninth SCL pulse (before the acknowledge pulse).
RXNE
rd data0 rd data1
MS19848V1
Transmission
If the I2C_TXDR register is not empty (TXE=0), its content is copied into the shift register
after the ninth SCL pulse (the Acknowledge pulse). Then the shift register content is shifted
out on SDA line. If TXE = 1, meaning that no data is written yet in I2C_TXDR, SCL line is
stretched low until I2C_TXDR is written. The stretch is done after the ninth SCL pulse.
data1
data2
Shift register xx xx xx
TXE
wr data1 wr data2
MS19849V1
By default, the slave uses its clock stretching capability, which means that it stretches the
SCL signal at low level when needed, in order to perform software actions. If the master
does not support clock stretching, the I2C must be configured with NOSTRETCH = 1 in the
I2C_CR1 register.
After receiving an ADDR interrupt, if several addresses are enabled the user must read the
ADDCODE[6:0] bits in the I2C_ISR register in order to check which address matched. DIR
flag must also be checked in order to know the transfer direction.
Slave
initialization
Initial settings
End
MS19850V2
For code example, refer to A.14.1: I2C configured in slave mode code example.
Slave transmitter
A transmit interrupt status (TXIS) is generated when the I2C_TXDR register becomes
empty. An interrupt is generated if the TXIE bit is set in the I2C_CR1 register.
The TXIS bit is cleared when the I2C_TXDR register is written with the next data byte to be
transmitted.
When a NACK is received, the NACKF bit is set in the I2C_ISR register and an interrupt is
generated if the NACKIE bit is set in the I2C_CR1 register. The slave automatically releases
the SCL and SDA lines in order to let the master perform a STOP or a RESTART condition.
The TXIS bit is not set when a NACK is received.
When a STOP is received and the STOPIE bit is set in the I2C_CR1 register, the STOPF
flag is set in the I2C_ISR register and an interrupt is generated. In most applications, the
SBC bit is usually programmed to ‘0’. In this case, If TXE = 0 when the slave address is
received (ADDR = 1), the user can choose either to send the content of the I2C_TXDR
register as the first data byte, or to flush the I2C_TXDR register by setting the TXE bit in
order to program a new data byte.
In Slave byte control mode (SBC = 1), the number of bytes to be transmitted must be
programmed in NBYTES in the address match interrupt subroutine (ADDR = 1). In this
case, the number of TXIS events during the transfer corresponds to the value programmed
in NBYTES.
Caution: When NOSTRETCH = 1, the SCL clock is not stretched while the ADDR flag is set, so the
user cannot flush the I2C_TXDR register content in the ADDR subroutine, in order to
program the first data byte. The first data byte to be sent must be previously programmed in
the I2C_TXDR register:
• This data can be the data written in the last TXIS event of the previous transmission
message.
• If this data byte is not the one to be sent, the I2C_TXDR register can be flushed by
setting the TXE bit in order to program a new data byte. The STOPF bit must be
cleared only after these actions, in order to guarantee that they are executed before the
first data transmission starts, following the address acknowledge.
If STOPF is still set when the first data transmission starts, an underrun error is
generated (the OVR flag is set).
If a TXIS event is needed, (transmit interrupt or transmit DMA request), the user must
set the TXIS bit in addition to the TXE bit, in order to generate a TXIS event.
Figure 190. Transfer sequence flow for I2C slave transmitter, NOSTRETCH = 0
Slave
transmission
Slave initialization
No
I2C_ISR.ADDR
=1?
Yes
SCL
stretched
Read ADDCODE and DIR in I2C_ISR
Optional: Set I2C_ISR.TXE = 1
Set I2C_ICR.ADDRCF
No
I2C_ISR.TXIS
=1?
Yes
Write I2C_TXDR.TXDATA
MS19851V2
Figure 191. Transfer sequence flow for I2C slave transmitter, NOSTRETCH = 1
Slave
transmission
Slave initialization
No
No
I2C_ISR.TXIS I2C_ISR.STOPF
=1? =1?
Yes Yes
Set I2C_ICR.STOPCF
MS19852V2
S Address A A A data3 NA P
SCL stretch
data1 data2
TXE
EV1: ADDR ISR: check ADDCODE and DIR, set TXE, set ADDRCF
EV2: TXIS ISR: wr data1
EV3: TXIS ISR: wr data2
EV4: TXIS ISR: wr data3
EV5: TXIS ISR: wr data4 (not sent)
legend :
Example I2C slave transmitter 3 bytes without 1st data flush,
NOSTRETCH=0: transmission
ADDR TXIS TXIS TXIS reception
SCL stretch
S Address A data1 A data2 A data3 NA P
TXE
legend:
Example I2C slave transmitter 3 bytes, NOSTRETCH=1:
transmission
TXIS TXIS TXIS STOPF
reception
TXE
EV1: wr data1
EV2: TXIS ISR: wr data2
EV3: TXIS ISR: wr data3
EV4: TXIS ISR: wr data4 (not sent)
EV5: STOPF ISR: (optional: set TXE and TXIS), set STOPCF
MS19853V2
For code example, refer to A.14.2: I2C slave transmitter code example.
Slave receiver
RXNE is set in I2C_ISR when the I2C_RXDR is full, and generates an interrupt if RXIE is
set in I2C_CR1. RXNE is cleared when I2C_RXDR is read.
When a STOP is received and STOPIE is set in I2C_CR1, STOPF is set in I2C_ISR and an
interrupt is generated.
Figure 193. Transfer sequence flow for slave receiver with NOSTRETCH = 0
Slave reception
Slave initialization
No
I2C_ISR.ADDR
=1?
Yes
SCL
stretched
Read ADDCODE and DIR in I2C_ISR
Set I2C_ICR.ADDRCF
No
I2C_ISR.RXNE
=1?
Yes
Write I2C_RXDR.RXDATA
MS19855V2
Figure 194. Transfer sequence flow for slave receiver with NOSTRETCH = 1
Slave reception
Slave initialization
No
No
I2C_ISR.RXNE I2C_ISR.STOPF
=1? =1?
Yes Yes
MS19856V2
SCL stretch
S Address A data1 A data2 A data3 A
RXNE
transmission
RXNE RXNE RXNE reception
RXNE
For code example, refer to A.14.3: I2C slave receiver code example.
tSYNC2 SCLH
SCLL
tSYNC1
SCL
SCL high level detected SCL high level detected SCL high level detected
SCLH counter starts SCLH counter starts SCLH counter starts
SCLL SCLL
MS19858V1
Caution: In order to be I2C or SMBus compliant, the master clock must respect the timings given the
table below.
Note: SCLL is also used to generate the tBUF and tSU:STA timings.
SCLH is also used to generate the tHD:STA and tSU:STO timings.
Refer to Section 23.4.11: I2C_TIMINGR register configuration examples for examples of
I2C_TIMINGR settings vs. I2CCLK frequency.
master re-launches automatically the slave address transmission until ACK is received. In
this case ADDRCF must be set if a NACK is received from the slave, in order to stop
sending the slave address.
If the I2C is addressed as a slave (ADDR = 1) while the START bit is set, the I2C switches to
slave mode and the START bit is cleared, when the ADDRCF bit is set.
Note: The same procedure is applied for a Repeated Start condition. In this case BUSY = 1.
Master
initialization
Initial settings
End
MS19859V2
For code example, refer to A.14.4: I2C configured in master mode to receive code example
andA.14.5: I2C configured in master mode to transmit code example.
11110XX 0 11110XX 1
Write Read
MSv41066V1
• If the master addresses a 10-bit address slave, transmits data to this slave and then
reads data from the same slave, a master transmission flow must be done first. Then a
repeated start is set with the 10 bit slave address configured with HEAD10R = 1. In this
case the master sends this sequence: ReStart + Slave address 10-bit header Read.
11110XX 0
Write
11110XX 1
Slave address
Sr R/W A DATA A DATA A P
1st 7 bits
Read
MS19823V1
Master transmitter
In the case of a write transfer, the TXIS flag is set after each byte transmission, after the
ninth SCL pulse when an ACK is received.
A TXIS event generates an interrupt if the TXIE bit is set in the I2C_CR1 register. The flag is
cleared when the I2C_TXDR register is written with the next data byte to be transmitted.
The number of TXIS events during the transfer corresponds to the value programmed in
NBYTES[7:0]. If the total number of data bytes to be sent is greater than 255, reload mode
must be selected by setting the RELOAD bit in the I2C_CR2 register. In this case, when
NBYTES data have been transferred, the TCR flag is set and the SCL line is stretched low
until NBYTES[7:0] is written to a non-zero value.
The TXIS flag is not set when a NACK is received.
• When RELOAD=0 and NBYTES data have been transferred:
– In automatic end mode (AUTOEND=1), a STOP is automatically sent.
– In software end mode (AUTOEND=0), the TC flag is set and the SCL line is
stretched low in order to perform software actions:
A RESTART condition can be requested by setting the START bit in the I2C_CR2
register with the proper slave address configuration, and number of bytes to be
transferred. Setting the START bit clears the TC flag and the START condition is
sent on the bus.
A STOP condition can be requested by setting the STOP bit in the I2C_CR2
register. Setting the STOP bit clears the TC flag and the STOP condition is sent on
the bus.
• If a NACK is received: the TXIS flag is not set, and a STOP condition is automatically
sent after the NACK reception. the NACKF flag is set in the I2C_ISR register, and an
interrupt is generated if the NACKIE bit is set.
Figure 200. Transfer sequence flow for I2C master transmitter for N≤255 bytes
Master
transmission
Master initialization
NBYTES = N
AUTOEND = 0 for RESTART; 1 for STOP
Configure slave address
Set I2C_CR2.START
No
No
I2C_ISR.NACKF = I2C_ISR.TXIS
1? =1?
Yes Yes
Write I2C_TXDR
End
NBYTES No
transmitted?
Yes
Yes
I2C_ISR.TC =
1?
End
MS19860V2
Figure 201. Transfer sequence flow for I2C master transmitter for N>255 bytes
Master
transmission
Master initialization
No
No
I2C_ISR.NACKF I2C_ISR.TXIS
= 1? = 1?
Yes Yes
Write I2C_TXDR
End
No
NBYTES
transmitted ?
Yes
Yes
I2C_ISR.TC
= 1?
Set I2C_CR2.START
with slave addess No
NBYTES ...
I2C_ISR.TCR
= 1?
Yes
IF N< 256
NBYTES = N; N = 0; RELOAD = 0
AUTOEND = 0 for RESTART; 1 for STOP
End
ELSE
NBYTES = 0xFF; N = N-255
RELOAD = 1
MS19861V3
reception
S Address A data1 A data2 A P
SCL stretch
INIT EV1 EV2
TXE
NBYTES xx 2
transmission
S Address A data1 A data2 A ReS Address
reception
NBYTES xx 2
MS19862V2
For code example, refer to A.14.6: I2C master transmitter code example.
Master receiver
In the case of a read transfer, the RXNE flag is set after each byte reception, after the eighth
SCL pulse. An RXNE event generates an interrupt if the RXIE bit is set in the I2C_CR1
register. The flag is cleared when I2C_RXDR is read.
If the total number of data bytes to be received is greater than 255, reload mode must be
selected by setting the RELOAD bit in the I2C_CR2 register. In this case, when
NBYTES[7:0] data have been transferred, the TCR flag is set and the SCL line is stretched
low until NBYTES[7:0] is written to a non-zero value.
• When RELOAD=0 and NBYTES[7:0] data have been transferred:
– In automatic end mode (AUTOEND=1), a NACK and a STOP are automatically
sent after the last received byte.
– In software end mode (AUTOEND=0), a NACK is automatically sent after the last
received byte, the TC flag is set and the SCL line is stretched low in order to allow
software actions:
A RESTART condition can be requested by setting the START bit in the I2C_CR2
register with the proper slave address configuration, and number of bytes to be
transferred. Setting the START bit clears the TC flag and the START condition,
followed by slave address, are sent on the bus.
A STOP condition can be requested by setting the STOP bit in the I2C_CR2
register. Setting the STOP bit clears the TC flag and the STOP condition is sent on
the bus.
Figure 203. Transfer sequence flow for I2C master receiver for N≤255 bytes
Master reception
Master initialization
NBYTES = N
AUTOEND = 0 for RESTART; 1 for STOP
Configure slave address
Set I2C_CR2.START
No
I2C_ISR.RXNE
=1?
Yes
Read I2C_RXDR
NBYTES No
received?
Yes
Yes
I2C_ISR.TC =
1?
End
MS19863V2
Figure 204. Transfer sequence flow for I2C master receiver for N >255 bytes
Master reception
Master initialization
No
I2C_ISR.RXNE
=1?
Yes
Read I2C_RXDR
NBYTES No
received?
Yes
Yes
I2C_ISR.TC =
1?
Yes
IF N< 256
NBYTES =N; N=0;RELOAD=0
AUTOEND=0 for RESTART; 1 for STOP
ELSE
NBYTES =0xFF;N=N-255
RELOAD=1
End
MS19864V2
RXNE RXNE
legend:
reception
INIT EV1 EV2
SCL stretch
NBYTES xx 2
transmission
S Address A data1 A data2 NA ReS Address
reception
NBYTES
xx 2 N
MS19865V1
For code example refer to A.14.7: I2C master receiver code example.
PRESC 1 1 0 0
SCLL 0xC7 0x13 0x9 0x6
tSCLL 200 x 250 ns = 50 µs 20 x 250 ns = 5.0 µs 10 x 125 ns = 1250 ns 7 x 125 ns = 875 ns
SCLH 0xC3 0xF 0x3 0x3
tSCLH 196 x 250 ns = 49 µs 16 x 250 ns = 4.0µs 4 x 125 ns = 500 ns 4 x 125 ns = 500 ns
(1)
tSCL ~100 µs(2) ~10 µs(2) ~2500 ns(3) ~2000 ns(4)
SDADEL 0x2 0x2 0x1 0x0
tSDADEL 2 x 250 ns = 500 ns 2 x 250 ns = 500 ns 1 x 125 ns = 125 ns 0 ns
SCLDEL 0x4 0x4 0x3 0x1
tSCLDEL 5 x 250 ns = 1250 ns 5 x 250 ns = 1250 ns 4 x 125 ns = 500 ns 2 x 125 ns = 250 ns
1. SCL period tSCL is greater than tSCLL + tSCLH due to SCL internal detection delay. Values provided for tSCL are examples
only.
2. tSYNC1 + tSYNC2 minimum value is 4 x tI2CCLK = 500 ns. Example with tSYNC1 + tSYNC2 = 1000 ns.
3. tSYNC1 + tSYNC2 minimum value is 4 x tI2CCLK = 500 ns. Example with tSYNC1 + tSYNC2 = 750 ns.
4. tSYNC1 + tSYNC2 minimum value is 4 x tI2CCLK = 500 ns. Example with tSYNC1 + tSYNC2 = 655 ns.
PRESC 3 3 1 0
SCLL 0xC7 0x13 0x9 0x4
tSCLL 200 x 250 ns = 50 µs 20 x 250 ns = 5.0 µs 10 x 125 ns = 1250 ns 5 x 62.5 ns = 312.5 ns
SCLH 0xC3 0xF 0x3 0x2
tSCLH 196 x 250 ns = 49 µs 16 x 250 ns = 4.0 µs 4 x 125 ns = 500 ns 3 x 62.5 ns = 187.5 ns
(1)
tSCL ~100 µs(2) ~10 µs(2) ~2500 ns(3) ~1000 ns(4)
SDADEL 0x2 0x2 0x2 0x0
tSDADEL 2 x 250 ns = 500 ns 2 x 250 ns = 500 ns 2 x 125 ns = 250 ns 0 ns
SCLDEL 0x4 0x4 0x3 0x2
tSCLDEL 5 x 250 ns = 1250 ns 5 x 250 ns = 1250 ns 4 x 125 ns = 500 ns 3 x 62.5 ns = 187.5 ns
1. SCL period tSCL is greater than tSCLL + tSCLH due to SCL internal detection delay. Values provided for tSCL are examples
only.
2. tSYNC1 + tSYNC2 minimum value is 4 x tI2CCLK = 250 ns. Example with tSYNC1 + tSYNC2 = 1000 ns.
3. tSYNC1 + tSYNC2 minimum value is 4 x tI2CCLK = 250 ns. Example with tSYNC1 + tSYNC2 = 750 ns.
4. tSYNC1 + tSYNC2 minimum value is 4 x tI2CCLK = 250 ns. Example with tSYNC1 + tSYNC2 = 500 ns.
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 I2C
principles of operation. The SMBus provides a control bus for system and power
management related tasks.
This peripheral is compatible with the SMBus specification (http://smbus.org).
The System Management Bus Specification refers to three types of devices.
• A slave is a device that receives or responds 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.
This peripheral can be configured as master or slave device, and also as a host.
Bus protocols
There are eleven possible command protocols for any given device. A device may use any
or all of the eleven protocols to communicate. The protocols are Quick Command, Send
Byte, Receive Byte, Write Byte, Write Word, Read Byte, Read Word, Process Call, Block
Read, Block Write and Block Write-Block Read Process Call. These protocols should be
implemented by the user software.
For more details of these protocols, refer to SMBus specification (http://smbus.org).
SMBus alert
The SMBus ALERT optional signal is supported. A slave-only device can signal the host
through the SMBALERT# pin that it wants to talk. The host processes the interrupt and
simultaneously accesses all SMBALERT# devices through the alert response address
(0b0001 100). Only the device(s) which pulled SMBALERT# low acknowledges the alert
response address.
When configured as a slave device(SMBHEN=0), the SMBA pin is pulled low by setting the
ALERTEN bit in the I2C_CR1 register. The Alert Response Address is enabled at the same
time.
When configured as a host (SMBHEN=1), the ALERT flag is set in the I2C_ISR register
when a falling edge is detected on the SMBA pin and ALERTEN=1. An interrupt is
generated if the ERRIE bit is set in the I2C_CR1 register. When ALERTEN=0, the ALERT
line is considered high even if the external SMBA pin is low.
If the SMBus ALERT pin is not needed, the SMBA pin can be used as a standard GPIO if
ALERTEN=0.
Timeouts
This peripheral embeds hardware timers in order to be compliant with the 3 timeouts defined
in SMBus specification.
Start Stop
tLOW:SEXT
ClkAck ClkAck
tLOW:MEXT tLOW:MEXT tLOW:MEXT
SMBCLK
SMBDAT
MS19866V1
Timeout detection
The timeout detection is enabled by setting the TIMOUTEN and TEXTEN bits in the
I2C_TIMEOUTR register. The timers must be programmed in such a way that they detect a
timeout before the maximum time given in the SMBus specification.
• tTIMEOUT check
In order to enable the tTIMEOUT check, the 12-bit TIMEOUTA[11:0] bits must be
programmed with the timer reload value in order to check the tTIMEOUT parameter. The
TIDLE bit must be configured to ‘0’ in order to detect the SCL low level timeout.
Then the timer is enabled by setting the TIMOUTEN in the I2C_TIMEOUTR register.
If SCL is tied low for a time greater than (TIMEOUTA+1) x 2048 x tI2CCLK, the TIMEOUT
flag is set in the I2C_ISR register.
Refer to Table 111: Examples of TIMEOUTA settings for various I2CCLK frequencies
(max tTIMEOUT = 25 ms).
Caution: Changing the TIMEOUTA[11:0] bits and TIDLE bit configuration is not allowed when the
TIMEOUTEN bit is set.
• tLOW:SEXT and tLOW:MEXT check
Depending on if the peripheral is configured as a master or as a slave, The 12-bit
TIMEOUTB timer must be configured in order to check tLOW:SEXT for a slave and
tLOW:MEXT for a master. As the standard specifies only a maximum, the user can choose
the same value for the both.
Then the timer is enabled by setting the TEXTEN bit in the I2C_TIMEOUTR register.
If the SMBus peripheral performs a cumulative SCL stretch for a time greater than
(TIMEOUTB+1) x 2048 x tI2CCLK, and in the timeout interval described in Bus idle
detection on page 626 section, the TIMEOUT flag is set in the I2C_ISR register.
Refer to Table 112: Examples of TIMEOUTB settings for various I2CCLK frequencies
Caution: Changing the TIMEOUTB configuration is not allowed when the TEXTEN bit is set.
Caution: Changing the TIMEOUTA and TIDLE configuration is not allowed when the TIMEOUTEN is
set.
that case the total number of TXIS interrupts is NBYTES - 1 and the content of the
I2C_PECR register is automatically transmitted if the master requests an extra byte after the
NBYTES - 1 data transfer.
Caution: The PECBYTE bit has no effect when the RELOAD bit is set.
Figure 207. Transfer sequence flow for SMBus slave transmitter N bytes + PEC
SMBus slave
transmission
Slave initialization
No
I2C_ISR.ADDR =
1?
Yes
No
I2C_ISR.TXIS
=1?
Yes
Write I2C_TXDR.TXDATA
MS19867V2
Figure 208. Transfer bus diagrams for SMBus slave transmitter (SBC=1)
legend:
Example SMBus slave transmitter 2 bytes + PEC,
transmission
ADDR TXIS TXIS reception
NBYTES 3
EV1: ADDR ISR: check ADDCODE, program NBYTES=3, set PECBYTE, set ADDRCF
EV2: TXIS ISR: wr data1
EV3: TXIS ISR: wr data2
MS19869V2
Figure 209. Transfer sequence flow for SMBus slave receiver N Bytes + PEC
SMBus slave
reception
Slave initialization
No
I2C_ISR.ADDR =
1?
Yes
No
I2C_ISR.RXNE =1?
I2C_ISR.TCR = 1?
Yes
Read I2C_RXDR.RXDATA
Program I2C_CR2.NACK = 0
I2C_CR2.NBYTES = 1
N=N-1
No
N = 1?
Yes
Read I2C_RXDR.RXDATA
Program RELOAD = 0
NACK = 0 and NBYTES = 1
No
I2C_ISR.RXNE =1?
Yes
Read I2C_RXDR.RXDATA
End
MS19868V2
Figure 210. Bus transfer diagrams for SMBus slave receiver (SBC=1)
legend:
Example SMBus slave receiver 2 bytes + PEC
transmission
ADDR RXNE RXNE RXNE
reception
NBYTES 3
EV1: ADDR ISR: check ADDCODE and DIR, program NBYTES = 3, PECBYTE=1, RELOAD=0, set ADDRCF
EV2: RXNE ISR: rd data1
EV3: RXNE ISR: rd data2
EV4: RXNE ISR: rd PEC
Example SMBus slave receiver 2 bytes + PEC, with ACK control legend :
(RELOAD=1/0) transmission
ADDR RXNE,TCR RXNE,TCR RXNE
reception
NBYTES 1
EV1: ADDR ISR: check ADDCODE and DIR, program NBYTES = 1, PECBYTE=1, RELOAD=1, set ADDRCF
EV2: RXNE-TCR ISR: rd data1, program NACK=0 and NBYTES = 1
EV3: RXNE-TCR ISR: rd data2, program NACK=0, NBYTES = 1 and RELOAD=0
EV4: RXNE-TCR ISR: rd PEC
MS19870V2
This section is relevant only when the SMBus feature is supported. Refer to Section 23.3:
I2C implementation.
In addition to I2C master transfer management (refer to Section 23.4.10: I2C master mode),
some additional software flows are provided to support the SMBus.
When the SMBus master wants to send a RESTART condition after the PEC, software
mode must be selected (AUTOEND=0). In this case, once NBYTES - 1 have been
transmitted, the I2C_PECR register content is transmitted and the TC flag is set after the
PEC transmission, stretching the SCL line low. The RESTART condition must be
programmed in the TC interrupt subroutine.
Caution: The PECBYTE bit has no effect when the RELOAD bit is set.
Example SMBus master transmitter 2 bytes + PEC, automatic end mode (STOP)
TXIS TXIS
legend:
reception
INIT EV1 EV2
SCL stretch
TXE
NBYTES xx 3
INIT: program Slave address, program NBYTES = 3, AUTOEND=1, set PECBYTE, set START
EV1: TXIS ISR: wr data1
EV2: TXIS ISR: wr data2
Example SMBus master transmitter 2 bytes + PEC, software end mode (RESTART)
TC legend:
TXIS TXIS
transmission
S Address A data1 A data2 A PEC A Rstart Address
reception
xx 3 N
NBYTES
INIT: program Slave address, program NBYTES = 3, AUTOEND=0, set PECBYTE, set START
EV1: TXIS ISR: wr data1
EV2: TXIS ISR: wr data2
EV3: TC ISR: program Slave address, program NBYTES = N, set START
MS19871V2
Example SMBus master receiver 2 bytes + PEC, automatic end mode (STOP)
reception
INIT EV1 EV2 EV3
SCL stretch
NBYTES xx 3
INIT: program Slave address, program NBYTES = 3, AUTOEND=1, set PECBYTE, set START
EV1: RXNE ISR: rd data1
EV2: RXNE ISR: rd data2
EV3: RXNE ISR: rd PEC
Example SMBus master receiver 2 bytes + PEC, software end mode (RESTART)
transmission
S Address A data1 A data2 A PEC NA Restart Address
reception
NBYTES
xx 3 N
INIT: program Slave address, program NBYTES = 3, AUTOEND=0, set PECBYTE, set START
EV1: RXNE ISR: rd data1
EV2: RXNE ISR: rd data2
EV3: RXNE ISR: read PEC
EV4: TC ISR: program Slave address, program NBYTES = N, set START
MS19872V2
Alert (ALERT)
This section is relevant only when the SMBus feature is supported. Refer to Section 23.3:
I2C implementation.
The ALERT flag is set when the I2C interface is configured as a Host (SMBHEN=1), the
alert pin detection is enabled (ALERTEN=1) and a falling edge is detected on the SMBA pin.
An interrupt is generated if the ERRIE bit is set in the I2C_CR1 register.
Sleep No effect. I2C interrupts cause the device to exit the Sleep mode.
The I2C registers content is kept. If WUPEN = 1 and I2C is clocked by an internal
oscillator (HSI16): the address recognition is functional. The I2C address match
Stop(1)
condition causes the device to exit the Stop mode. If WUPEN=0: the I2C must be
disabled before entering Stop mode
The I2C peripheral is powered down and must be reinitialized after exiting
Standby
Standby mode.
1. Refer to Section 23.3: I2C implementation for information about the Stop modes supported by each
instance. If wakeup from a specific Stop mode is not supported, the instance must be disabled before
entering this Stop mode.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
ALERT SMBD SMBH WUPE NOSTR
Res. Res. Res. Res. Res. Res. Res. Res. PECEN GCEN SBC
EN EN EN N ETCH
rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
RXDMA TXDMA ANF STOP NACK ADDR
Res. DNF[3:0] ERRIE TCIE RXIE TXIE PE
EN EN OFF IE IE IE
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
PEC AUTOE RE
Res. Res. Res. Res. Res. NBYTES[7:0]
BYTE ND LOAD
rs 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
HEAD1 RD_
NACK STOP START ADD10 SADD[9:0]
0R WRN
rs rs rs rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
OA1
OA1EN Res. Res. Res. Res. OA1[9:0]
MODE
rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
OA2EN Res. Res. Res. Res. OA2MSK[2:0] OA2[7:1] Res.
rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
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
SCLH[7:0] SCLL[7:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Note: This register must be configured when the I2C is disabled (PE = 0).
Note: The STM32CubeMX tool calculates and provides the I2C_TIMINGR content in the I2C
Configuration window.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
TEXTEN Res. Res. Res. TIMEOUTB[11:0]
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
TIMOUTEN Res. Res. TIDLE TIMEOUTA[11:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Note: If the SMBus feature is not supported, this register is reserved and forced by hardware to
“0x00000000”. Refer to Section 23.3: I2C implementation.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. ADDCODE[6:0] DIR
r r r r r r r r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
TIME PEC
BUSY Res. ALERT OVR ARLO BERR TCR TC STOPF NACKF ADDR RXNE TXIS TXE
OUT ERR
r r r r r r r r r r r r r rs rs
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
ALERT TIMOU ARLOC BERRC STOPC NACKC ADDR
Res. Res. PECCF OVRCF Res. Res. Res. Res. Res.
CF TCF F F F F CF
w w w w w w w w w
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
r r r r r r r r
Note: If the SMBus feature is not supported, this register is reserved and forced by hardware to
“0x00000000”. Refer to Section 23.3: I2C implementation.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. Res. Res. Res. Res. Res. Res. Res. RXDATA[7:0]
r r r r r r r r
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. Res. Res. Res. Res. Res. Res. Res. TXDATA[7:0]
rw rw rw rw rw rw rw rw
0xC
0x24
0x20
0x18
0x14
0x10
0x1C
Offset
RM0377
23.7.12
I2C_
I2C_
name
I2C_ISR
I2C_ICR
I2C_CR2
I2C_CR1
TIMINGR
I2C_PECR
I2C_OAR2
I2C_OAR1
I2C_RXDR
Register
TIMEOUTR
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
0
0
Res. Res. Res. Res. TEXTEN Res. Res. Res. Res. 31
0
Res. Res. Res. Res. Res. Res. Res. Res. Res. 30
0
Res. Res. Res. Res. Res. Res. Res. Res. Res. 29
0
PRESC[3:0]
Res. Res. Res. Res. Res. Res. Res. Res. Res. 28
0
I2C register map
0
0
Res. Res. Res. Res. Res. Res. Res. PECBYTE Res. 26
0
0
Res. Res. Res. Res. Res. Res. Res. AUTOEND Res. 25
0
0
Res. Res. Res. Res. Res. Res. Res. RELOAD Res. 24
0
0
0
0
0
0
0
0
0
0
[3:0]
0
0
0
0
0
SCLDEL
0
0
0
0
0
TIMEOUTB[11:0]
0
0
0
0
0
ADDCODE[6:0]
0
0
0
0
0
NBYTES[7:0]
[3:0]
0
0
0
0
0
RM0377 Rev 10
Res. Res. Res. Res. Res. NOSTRETCH 17
SDADEL
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Res. Res. ALERTCF ALERT Res. Res. Res. START Res. 13
0
0
0
0
0
0
Res. Res. TIMOUTCF TIMEOUT TIDLE Res. Res. HEAD10R ANFOFF 12
0
0
0
0
0
0
Res. Res. PECCF PECERR Res. Res. ADD10 11
Table 116. I2C register map and reset values
SCLH[7:0]
0
0
0
0
0
0
0
0
The table below provides the I2C register map and reset values.
0
0
0
0
0
0
0
0
Res. Res. ARLOCF ARLO 9
DNF[3:0]
K [2:0]
OA2MS
0
0
0
0
0
0
0
0
Res. Res. BERRCF BERR 8
0
0
0
0
0
0
0
0
0
Res. TCR ERRIE 7
0
0
0
0
0
0
0
0
0
Res. TC TCIE 6
0
0
0
0
0
0
0
0
0
0
STOPCF STOPF STOPIE 5
0
0
0
0
0
0
0
0
0
0
NACKCF NACKF NACKIE 4
OA1[9:0]
TIMEOUTA[11:0]
SADD[9:0]
OA2[7:1]
0
0
0
0
0
0
0
0
0
0
ADDRCF ADDR ADDRIE 3
PEC[7:0]
SCLL[7:0]
0
0
0
0
0
0
0
0
0
RXDATA[7:0]
Res. RXNE RXIE 2
0
0
0
0
0
0
0
0
0
Res. TXIS TXIE 1
1
0
0
0
0
0
0
0
0
655/905
Inter-integrated circuit (I2C) interface
656
Inter-integrated circuit (I2C) interface RM0377
Offset Register
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
10
11
9
8
7
6
5
4
3
2
1
0
name
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
I2C_TXDR TXDATA[7:0]
0x28
Reset value 0 0 0 0 0 0 0 0
24.1 Introduction
The universal synchronous asynchronous receiver transmitter (USART) offers a flexible
means of Full-duplex data exchange with external equipment requiring an industry standard
NRZ asynchronous serial data format. The USART offers a very wide range of baud rates
using a programmable baud rate generator.
It supports synchronous one-way communication and Half-duplex Single-wire
communication, as well as multiprocessor communications. It also supports the LIN (Local
Interconnect Network), Smartcard protocol and IrDA (Infrared Data Association) SIR
ENDEC specifications and Modem operations (CTS/RTS).
High speed data communication is possible by using the DMA (direct memory access) for
multibuffer configuration.
Serial data are transmitted and received through these pins in normal USART mode. The
frames are comprised of:
• An Idle Line prior to transmission or reception
• A start bit
• A data word (7, 8 or 9 bits) least significant bit first
• 0.5, 1, 1.5, 2 stop bits indicating that the frame is complete
• The USART interface uses a baud rate generator
• A status register (USART_ISR)
• Receive and transmit data registers (USART_RDR, USART_TDR)
• A baud rate register (USART_BRR)
• A guard-time register (USART_GTPR) in case of Smartcard mode.
Refer to Section 24.8: USART registers on page 701 for the definitions of each bit.
The following pin is required to interface in synchronous mode and Smartcard mode:
• CK: Clock output. This pin outputs the transmitter data clock for synchronous
transmission corresponding to SPI master mode (no clock pulses on start bit and stop
bit, and a software option to send a clock pulse on the last data bit). In parallel, data
can be received synchronously on RX. This can be used to control peripherals that
have shift registers. The clock phase and polarity are software programmable. In
Smartcard mode, CK output can provide the clock to the smartcard.
The following pins are required in RS232 Hardware flow control mode:
• CTS: Clear To Send blocks the data transmission at the end of the current transfer
(when high)
• RTS: Request to send indicates that the USART is ready to receive data (when low).
The following pin is required in RS485 Hardware control mode:
• DE: Driver Enable activates the transmission mode of the external transceiver.
Note: DE and RTS share the same pin.
PRDATA PWDATA
Write Read DR (data register)
(CPU or DMA) (CPU or DMA)
USART_GTPR register
GT PSC CK control CK
RTS/ Hardware
DE flow
CTS controller Receiver
clock
Transmit Wakeup Receiver
control unit control
USART
interrupt
control
USART_BRR register
TE Transmitter
rate controller
/USARTDIV or 2/USARTDIV
Transmitter (depending on the
BRR[15:0]
clock oversampling mode)
(Note 1)
Receiver rate
RE controller
fCK
(Note 2) Conventional baud rate generator
MS19821V8
1. For details on coding USARTDIV in the USART_BRR register, refer to Section 24.5.4: USART baud rate
generation.
2. fCK can be fLSE, fHSI, fPCLK, fSYS.
Clock **
Start
Idle frame bit
Clock **
Start
Idle frame bit
Clock **
Start
Idle frame bit
Character transmission
During an USART transmission, data shifts out least significant bit first (default
configuration) on the TX pin. In this mode, the USART_TDR register consists of a buffer
(TDR) between the internal bus and the transmit shift register (see Figure 213).
Every character is preceded by a start bit which is a logic level low for one bit period. The
character is terminated by a configurable number of stop bits.
The following stop bits are supported by USART: 0.5, 1, 1.5 and 2 stop bits.
Note: The TE bit must be set before writing the data to be transmitted to the USART_TDR.
The TE bit should not be reset during transmission of data. Resetting the TE bit during the
transmission will corrupt the data on the TX pin as the baud rate counters will get frozen.
The current data being transmitted will be lost.
An idle frame will be sent after the TE bit is enabled.
Configurable stop bits
The number of stop bits to be transmitted with every character can be programmed in
Control register 2, bits 13,12.
• 1 stop bit: This is the default value of number of stop bits.
• 2 stop bits: This will be supported by normal USART, Single-wire and Modem modes.
• 1.5 stop bits: To be used in Smartcard mode.
• 0.5 stop bit: To be used when receiving data in Smartcard mode.
An idle frame transmission will include the stop bits.
A break transmission will be 10 low bits (when M[1:0] = 00) or 11 low bits (when M[1:0] = 01)
or 9 low bits (when M[1:0] = 10) followed by 2 stop bits (see Figure 215). It is not possible to
transmit long breaks (break of length greater than 9/10/11 low bits).
When a transmission is taking place, a write instruction to the USART_TDR register stores
the data in the TDR register; next, the data is copied in the shift register at the end of the
currently ongoing transmission.
When no transmission is taking place, a write instruction to the USART_TDR register places
the data in the shift register, the data transmission starts, and the TXE bit is set.
If a frame is transmitted (after the stop bit) and the TXE bit is set, the TC bit goes high. An
interrupt is generated if the TCIE bit is set in the USART_CR1 register.
After writing the last data in the USART_TDR register, it is mandatory to wait for TC=1
before disabling the USART or causing the microcontroller to enter the low-power mode
(see Figure 216: TC/TXE behavior when transmitting).
TX line
Set by hardware Set by hardware
TXE flag cleared by software cleared by software Set by hardware
USART_DR F1 F2 F3
Set by hardware
TC flag
ai17121b
For code example, refer to A.15.3: USART transfer complete code example.
Break characters
Setting the SBKRQ bit transmits a break character. The break frame length depends on the
M bits (see Figure 214).
If a ‘1’ is written to the SBKRQ bit, a break character is sent on the TX line after completing
the current character transmission. The SBKF bit is set by the write operation and it is reset
by hardware when the break character is completed (during the stop bits after the break
character). The USART inserts a logic 1 signal (STOP) for the duration of 2 bits at the end of
the break frame to guarantee the recognition of the start bit of the next frame.
In the case the application needs to send the break character following all previously
inserted data, including the ones not yet transmitted, the software should wait for the TXE
flag assertion before setting the SBKRQ bit.
Idle characters
Setting the TE bit drives the USART to send an idle frame before the first data frame.
Note: If the sequence is not complete, the start bit detection aborts and the receiver returns to the
idle state (no flag is set), where it waits for a falling edge.
The start bit is confirmed (RXNE flag set, interrupt generated if RXNEIE=1) if the 3 sampled
bits are at 0 (first sampling on the 3rd, 5th and 7th bits finds the 3 bits at 0 and second
sampling on the 8th, 9th and 10th bits also finds the 3 bits at 0).
The start bit is validated (RXNE flag set, interrupt generated if RXNEIE=1) but the NF noise
flag is set if,
a) for both samplings, 2 out of the 3 sampled bits are at 0 (sampling on the 3rd, 5th
and 7th bits and sampling on the 8th, 9th and 10th bits)
or
b) for one of the samplings (sampling on the 3rd, 5th and 7th bits or sampling on the
8th, 9th and 10th bits), 2 out of the 3 bits are found at 0.
If neither conditions a. or b. are met, the start detection aborts and the receiver returns to the
idle state (no flag is set).
Character reception
During an USART reception, data shifts in least significant bit first (default configuration)
through the RX pin. In this mode, the USART_RDR register consists of a buffer (RDR)
between the internal bus and the receive shift register.
Character reception procedure
1. Program the M bits in USART_CR1 to define the word length.
2. Select the desired baud rate using the baud rate register USART_BRR
3. Program the number of stop bits in USART_CR2.
4. Enable the USART by writing the UE bit in USART_CR1 register to 1.
5. Select DMA enable (DMAR) in USART_CR3 if multibuffer communication is to take
place. Configure the DMA register as explained in multibuffer communication.
6. Set the RE bit USART_CR1. This enables the receiver which begins searching for a
start bit.
For code example, refer to A.15.4: USART receiver configuration code example.
When a character is received:
• The RXNE bit is set to indicate that the content of the shift register is transferred to the
RDR. In other words, data has been received and can be read (as well as its
associated error flags).
• An interrupt is generated if the RXNEIE bit is set.
• The error flags can be set if a frame error, noise or an overrun error has been detected
during reception. PE flag can also be set with RXNE.
• In multibuffer, RXNE is set after every byte received and is cleared by the DMA read of
the Receive data Register.
• In single buffer mode, clearing the RXNE bit is performed by a software read to the
USART_RDR register. The RXNE flag can also be cleared by writing 1 to the RXFRQ
in the USART_RQR register. The RXNE bit must be cleared before the end of the
reception of the next character to avoid an overrun error.
For code example, refer to A.15.5: USART receive byte code example.
Break character
When a break character is received, the USART handles it as a framing error.
Idle character
When an idle frame is detected, there is the same procedure as for a received data
character plus an interrupt if the IDLEIE bit is set.
Overrun error
An overrun error occurs when a character is received when RXNE has not been reset. Data
can not be transferred from the shift register to the RDR register until the RXNE bit is
cleared.
The RXNE flag is set after every byte received. An overrun error occurs if RXNE flag is set
when the next data is received or the previous DMA request has not been serviced. When
an overrun error occurs:
• The ORE bit is set.
• The RDR content will not be lost. The previous data is available when a read to
USART_RDR is performed.
• The shift register will be overwritten. After that point, any data received during overrun
is lost.
• An interrupt is generated if either the RXNEIE bit is set or EIE bit is set.
• The ORE bit is reset by setting the ORECF bit in the ICR register.
Note: The ORE bit, when set, indicates that at least 1 data has been lost. There are two
possibilities:
- if RXNE=1, then the last valid data is stored in the receive register RDR and can be read,
- if RXNE=0, then it means that the last valid data has already been read and thus there is
nothing to be read in the RDR. This case can occur when the last valid data is read in the
RDR at the same time as the new (and lost) data is received.
Programming the ONEBIT bit in the USART_CR3 register selects the method used to
evaluate the logic level. There are two options:
• The majority vote of the three samples in the center of the received bit. In this case,
when the 3 samples used for the majority vote are not equal, the NF bit is set
• A single sample in the center of the received bit
Depending on the application:
– select the three samples’ majority vote method (ONEBIT=0) when operating in a
noisy environment and reject the data when a noise is detected (refer to
Figure 118) because this indicates that a glitch occurred during the sampling.
– select the single sample method (ONEBIT=1) when the line is noise-free to
increase the receiver’s tolerance to clock deviations (see Section 24.5.5:
Tolerance of the USART receiver to clock deviation on page 674). In this case the
NF bit will never be set.
When noise is detected in a frame:
• The NF bit is set at the rising edge of the RXNE bit.
• The invalid data is transferred from the Shift register to the USART_RDR register.
• No interrupt is generated in case of single byte communication. However this bit rises
at the same time as the RXNE bit which itself generates an interrupt. In case of
multibuffer communication an interrupt will be issued if the EIE bit is set in the
USART_CR3 register.
The NF bit is reset by setting NFCF bit in ICR register.
Note: Oversampling by 8 is not available in LIN, Smartcard and IrDA modes. In those modes, the
OVER8 bit is forced to ‘0’ by hardware.
RX line
sampled values
Sample clock 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
6/16
7/16 7/16
One bit time
MSv31152V1
RX line
sampled values
Sample
clock (x8) 1 2 3 4 5 6 7 8
2/8
3/8 3/8
One bit time
MSv31153V1
000 0 0
001 1 0
010 1 0
011 1 1
100 1 0
101 1 1
110 1 1
111 0 1
Framing error
A framing error is detected when the stop bit is not recognized on reception at the expected
time, following either a de-synchronization or excessive noise.
When the framing error is detected:
• The FE bit is set by hardware
• The invalid data is transferred from the Shift register to the USART_RDR register.
• No interrupt is generated in case of single byte communication. However this bit rises
at the same time as the RXNE bit which itself generates an interrupt. In case of
multibuffer communication an interrupt will be issued if the EIE bit is set in the
USART_CR3 register.
The FE bit is reset by writing 1 to the FECF in the USART_ICR register.
Equation 1: Baud rate for standard USART (SPI mode included) (OVER8 = 0 or 1)
In case of oversampling by 16, the equation is:
f CK
Tx/Rx baud = --------------------------------
USARTDIV
USARTDIV is an unsigned fixed point number that is coded on the USART_BRR register.
• When OVER8 = 0, BRR = USARTDIV.
• When OVER8 = 1
– BRR[2:0] = USARTDIV[3:0] shifted 1 bit to the right.
– BRR[3] must be kept cleared.
– BRR[15:4] = USARTDIV[15:4]
Note: The baud counters are updated to the new value in the baud registers after a write operation
to USART_BRR. Hence the baud rate register value should not be changed during
communication.
In case of oversampling by 16 or 8, USARTDIV must be greater than or equal to 16d.
Example 1
To obtain 9600 baud with fCK = 8 MHz.
• In case of oversampling by 16:
USARTDIV = 8 000 000/9600
BRR = USARTDIV = 833d = 0341h
• In case of oversampling by 8:
USARTDIV = 2 * 8 000 000/9600
USARTDIV = 1666,66 (1667d = 683h)
BRR[3:0] = 3h >> 1 = 1h
BRR = 0x681
Example 2
To obtain 921.6 kbaud with fCK = 32 MHz.
• In case of oversampling by 16:
USARTDIV = 32 000 000/921 600
BRR = USARTDIV = 35d = 23h
• In case of oversampling by 8:
USARTDIV = 2 * 32 000 000/921 600
USARTDIV = 70d = 46h
BRR[3:0] = USARTDIV[3:0] >> 1 = 6h >> 1 = 3h
BRR = 0x43
Table 119. Error calculation for programmed baud rates at fCK = 32 MHz in both cases of
oversampling by 16 or by 8(1)
Baud rate Oversampling by 16 (OVER8 = 0) Oversampling by 8 (OVER8 = 1)
% Error =
(Calculated -
S.No Desired Actual BRR Actual BRR % Error
Desired)B.Rate /
Desired B.Rate
where
DWU is the error due to sampling point deviation when the wakeup from Stop mode is
used.
when M[1:0] = 01:
t WUUSART
DWU = --------------------------
-
11 × Tbit
Table 120. Tolerance of the USART receiver when BRR [3:0] = 0000
OVER8 bit = 0 OVER8 bit = 1
M bits
ONEBIT=0 ONEBIT=1 ONEBIT=0 ONEBIT=1
Table 121. Tolerance of the USART receiver when BRR [3:0] is different from 0000
OVER8 bit = 0 OVER8 bit = 1
M bits
ONEBIT=0 ONEBIT=1 ONEBIT=0 ONEBIT=1
00 3.33% 3.88% 2% 3%
01 3.03% 3.53% 1.82% 2.73%
10 3.7% 4.31% 2.22% 3.33%
Note: The data specified in Table 120 and Table 121 may slightly differ in the special case when
the received frames contain some Idle frames of exactly 10-bit durations when M bits = 00
(11-bit durations when M bits =01 or 9- bit durations when M bits = 10).
detection range (bit duration not between 16 and 65536 clock periods (oversampling by 16)
and not between 8 and 65536 clock periods (oversampling by 8)).
The RXNE interrupt will signal the end of the operation.
At any later time, the auto baud rate detection may be relaunched by resetting the ABRF
flag (by writing a 0).
Note: If the USART is disabled (UE=0) during an auto baud rate operation, the BRR value may be
corrupted.
RXNE RXNE
MSv31154V1
Note: If the MMRQ is set while the IDLE character has already elapsed, mute mode will not be
entered (RWU is not set).
If the USART is activated while the line is IDLE, the idle state is detected after the duration
of one IDLE frame (not only after the reception of one character frame).
RX IDLE Addr=0 Data 1 Data 2 IDLE Addr=1 Data 3 Data 4 Addr=2 Data 5
Non-matching address
MSv31155V1
Modbus/RTU
In this mode, the end of one block is recognized by a “silence” (idle line) for more than 2
character times. This function is implemented through the programmable timeout function.
The timeout function and interrupt must be activated, through the RTOEN bit in the
USART_CR2 register and the RTOIE in the USART_CR1 register. The value corresponding
to a timeout of 2 character times (for example 22 x bit duration) must be programmed in the
RTO register. when the receive line is idle for this duration, after the last stop bit is received,
an interrupt is generated, informing the software that the current block reception is
completed.
Modbus/ASCII
In this mode, the end of a block is recognized by a specific (CR/LF) character sequence.
The USART manages this mechanism using the character match function.
By programming the LF ASCII code in the ADD[7:0] field and by activating the character
match interrupt (CMIE=1), the software is informed when a LF has been received and can
check the CR/LF in the DMA buffer.
Even parity
The parity bit is calculated to obtain an even number of “1s” inside the frame of the 6, 7 or 8
LSB bits (depending on M bits values) and the parity bit.
As an example, if data=00110101, and 4 bits are set, then the parity bit will be 0 if even
parity is selected (PS bit in USART_CR1 = 0).
Odd parity
The parity bit is calculated to obtain an odd number of “1s” inside the frame made of the 6, 7
or 8 LSB bits (depending on M bits values) and the parity bit.
As an example, if data=00110101 and 4 bits set, then the parity bit will be 1 if odd parity is
selected (PS bit in USART_CR1 = 1).
LIN transmission
The procedure explained in Section 24.5.2: USART transmitter has to be applied for LIN
Master transmission. It must be the same as for normal USART transmission with the
following differences:
• Clear the M bits to configure 8-bit word length.
• Set the LINEN bit to enter LIN mode. In this case, setting the SBKRQ bit sends 13 ‘0’
bits as a break character. Then 2 bits of value ‘1’ are sent to allow the next start
detection.
LIN reception
When LIN mode is enabled, the break detection circuit is activated. The detection is totally
independent from the normal USART receiver. A break can be detected whenever it occurs,
during Idle state or during a frame.
When the receiver is enabled (RE=1 in USART_CR1), the circuit looks at the RX input for a
start signal. The method for detecting start bits is the same when searching break
characters or data. After a start bit has been detected, the circuit samples the next bits
exactly like for the data (on the 8th, 9th and 10th samples). If 10 (when the LBDL = 0 in
USART_CR2) or 11 (when LBDL=1 in USART_CR2) consecutive bits are detected as ‘0,
and are followed by a delimiter character, the LBDF flag is set in USART_ISR. If the LBDIE
bit=1, an interrupt is generated. Before validating the break, the delimiter is checked for as it
signifies that the RX line has returned to a high level.
If a ‘1’ is sampled before the 10 or 11 have occurred, the break detection circuit cancels the
current detection and searches for a start bit again.
If the LIN mode is disabled (LINEN=0), the receiver continues working as normal USART,
without taking into account the break detection.
If the LIN mode is enabled (LINEN=1), as soon as a framing error occurs (i.e. stop bit
detected at ‘0’, which will be the case for any break frame), the receiver stops until the break
detection circuit receives either a ‘1’, if the break word was not complete, or a delimiter
character if a break has been detected.
The behavior of the break detector state machine and the break flag is shown on the
Figure 222: Break detection in LIN mode (11-bit break length - LBDL bit is set) on page 682.
Examples of break frames are given on Figure 223: Break detection in LIN mode vs.
Framing error detection on page 683.
Figure 222. Break detection in LIN mode (11-bit break length - LBDL bit is set)
Case 1: break signal not long enough => break discarded, LBDF is not set
Break frame
RX line
Capture strobe
Break state
Idle Bit0 Bit1 Bit2 Bit3 Bit4 Bit5 Bit6 Bit7 Bit8 Bit9 Bit10 Idle
machine
Read samples 0 0 0 0 0 0 0 0 0 0 1
Case 2: break signal just long enough => break detected, LBDF is set
Break frame
RX line
Delimiter is immediate
Capture strobe
Break state
Idle Bit0 Bit1 Bit2 Bit3 Bit4 Bit5 Bit6 Bit7 Bit8 Bit9 B10 Idle
machine
Read samples 0 0 0 0 0 0 0 0 0 0 0
LBDF
Case 3: break signal long enough => break detected, LBDF is set
Break frame
RX line
Capture strobe
Break state
Idle Bit0 Bit1 Bit2 Bit3 Bit4 Bit5 Bit6 Bit7 Bit8 Bit9 Bit10 wait delimiter Idle
machine
Read samples 0 0 0 0 0 0 0 0 0 0 0
LBDF
MSv31156V1
Figure 223. Break detection in LIN mode vs. Framing error detection
RXNE /FE
LBDF
RXNE /FE
LBDF
MSv31157V1
Note: The CK pin works in conjunction with the TX pin. Thus, the clock is provided only if the
transmitter is enabled (TE=1) and data is being transmitted (the data register USART_TDR
written). This means that it is not possible to receive synchronous data without transmitting
data.
The LBCL, CPOL and CPHA bits have to be selected when the USART is disabled (UE=0)
to ensure that the clock pulses function correctly.
For code example, refer to A.15.6: USART LIN mode code example.
RX Data out
TX Data in
Synchronous device
USART
(slave SPI)
CK Clock
MSv31158V2
Data on TX
0 1 2 3 4 5 6 7
(from master)
Start LSB MSB Stop
Data on RX
0 1 2 3 4 5 6 7
(from slave)
LSB MSB
*
Capture strobe
*LBCL bit controls last data pulse
MSv34709V2
Clock (CPOL=0,
CPHA=0) *
Clock (CPOL=0,
CPHA=1) *
Clock (CPOL=1, *
CPHA=0)
Clock (CPOL=1, *
CPHA=1)
Data on TX
0 1 2 3 4 5 6 7 8
(from master)
Start LSB MSB Stop
Data on RX
0 1 2 3 4 5 6 7 8
(from slave)
LSB MSB
Capture *
strobe
*LBCL bit controls last data pulse
MSv34710V1
CK
(capture strobe on CK rising
edge in this example)
tSETUP tHOLD
Note: The function of CK is different in Smartcard mode. Refer to Section 24.5.13: USART
Smartcard mode for more details.
WithParity error
Guard time
S 0 1 2 3 4 5 6 7 p
Start bit
Line pulled low by receiver
during stop in case of parity error
MSv31162V1
When connected to a smartcard, the TX output of the USART drives a bidirectional line that
is also driven by the smartcard. The TX pin must be configured as open drain.
Smartcard mode implements a single wire half duplex communication protocol.
• Transmission of data from the transmit shift register is guaranteed to be delayed by a
minimum of 1/2 baud clock. In normal operation a full transmit shift register starts
shifting on the next baud clock edge. In Smartcard mode this transmission is further
delayed by a guaranteed 1/2 baud clock.
• In transmission, if the smartcard detects a parity error, it signals this condition to the
USART by driving the line low (NACK). This NACK signal (pulling transmit line low for 1
baud clock) causes a framing error on the transmitter side (configured with 1.5 stop
bits). The USART can handle automatic re-sending of data according to the protocol.
The number of retries is programmed in the SCARCNT bit field. If the USART
continues receiving the NACK after the programmed number of retries, it stops
transmitting and signals the error as a framing error. The TXE bit can be set using the
TXFRQ bit in the USART_RQR register.
• Smartcard auto-retry in transmission: a delay of 2.5 baud periods is inserted between
the NACK detection by the USART and the start bit of the repeated character. The TC
bit is set immediately at the end of reception of the last repeated character (no guard-
time). If the software wants to repeat it again, it must insure the minimum 2 baud
periods required by the standard.
• If a parity error is detected during reception of a frame programmed with a 1.5 stop bits
period, the transmit line is pulled low for a baud clock period after the completion of the
receive frame. This is to indicate to the smartcard that the data transmitted to the
USART has not been correctly received. A parity error is NACKed by the receiver if the
NACK control bit is set, otherwise a NACK is not transmitted (to be used in T=1 mode).
If the received character is erroneous, the RXNE/receive DMA request is not activated.
According to the protocol specification, the smartcard must resend the same character.
If the received character is still erroneous after the maximum number of retries
specified in the SCARCNT bit field, the USART stops transmitting the NACK and
signals the error as a parity error.
• Smartcard auto-retry in reception: the BUSY flag remains set if the USART NACKs the
card but the card doesn’t repeat the character.
• In transmission, the USART inserts the Guard Time (as programmed in the Guard Time
register) between two successive characters. As the Guard Time is measured after the
stop bit of the previous character, the GT[7:0] register must be programmed to the
desired CGT (Character Guard Time, as defined by the 7816-3 specification) minus 12
(the duration of one character).
• The assertion of the TC flag can be delayed by programming the Guard Time register.
In normal operation, TC is asserted when the transmit shift register is empty and no
further transmit requests are outstanding. In Smartcard mode an empty transmit shift
register triggers the Guard Time counter to count up to the programmed value in the
Guard Time register. TC is forced low during this time. When the Guard Time counter
reaches the programmed value TC is asserted high.
• The TCBGT flag can be used to detect the end of data transfer without waiting for
guard time completion. This flag is set just after the end of frame transmission and if no
NACK has been received from the card. The TCGBT flag is available in category 1
devices only.
• The de-assertion of TC flag is unaffected by Smartcard mode.
• If a framing error is detected on the transmitter end (due to a NACK from the receiver),
the NACK is not detected as a start bit by the receive block of the transmitter.
According to the ISO protocol, the duration of the received NACK can be 1 or 2 baud
clock periods.
• On the receiver side, if a parity error is detected and a NACK is transmitted the receiver
does not detect the NACK as a start bit.
Note: A break character is not significant in Smartcard mode. A 0x00 data with a framing error is
treated as data and not as a break.
No Idle frame is transmitted when toggling the TE bit. The Idle frame (as defined for the
other configurations) is not defined by the ISO protocol.
Figure 229 details how the NACK signal is sampled by the USART. In this example the
USART is transmitting data and is configured with 1.5 stop bits. The receiver part of the
USART is enabled in order to check the integrity of the data and the NACK signal.
Figure 229. Parity error detection using the 1.5 stop bits
Sampling at Sampling at
8th, 9th, 10th 8th, 9th, 10th
Sampling at Sampling at
8th, 9th, 10th 8th, 9th, 10th
MSv31163V1
The USART can provide a clock to the smartcard through the CK output. In Smartcard
mode, CK is not associated to the communication but is simply derived from the internal
peripheral input clock through a 5-bit prescaler. The division ratio is configured in the
In case of an error in the block length, the end of the block is signaled by the RTO interrupt
(Character wait Time overflow).
Note: The error checking code (LRC/CRC) must be computed/verified by software.
Method 2
The USART is programmed in 9-bit/no-parity mode, no bit inversion. In this mode it receives
any of the two TS patterns as:
(H) LHHL LLL LLH = 0x103 -> inverse convention to be chosen
(H) LHHL HHH LLH = 0x13B -> direct convention to be chosen
The software checks the received character against these two patterns and, if any of them
match, then programs the USART accordingly for the next character reception.
If none of the two is recognized, a card reset may be generated in order to restart the
negotiation.
• The IrDA specification requires the acceptance of pulses greater than 1.41 µs. The
acceptable pulse width is programmable. Glitch detection logic on the receiver end
filters out pulses of width less than 2 PSC periods (PSC is the prescaler value
programmed in the USART_GTPR). Pulses of width less than 1 PSC period are always
rejected, but those of width greater than one and less than two periods may be
accepted or rejected, those greater than 2 periods will be accepted as a pulse. The
IrDA encoder/decoder doesn’t work when PSC=0.
• The receiver can communicate with a low-power transmitter.
• In IrDA mode, the STOP bits in the USART_CR2 register must be configured to “1 stop
bit”.
For code example, refer to A.15.10: USART IrDA mode code example.
SIREN
TX
OR USART_TX
SIR
Transmit IrDA_OUT
Encoder
USART
SIR
RX
Receive IrDA_IN
DEcoder
USART_RX
MSv31164V2
Start Stop
bit bit
0 1 0 1 0 0 1 1 0 1
TX
IrDA_OUT
Bit period 3/16
IrDA_IN
RX 0 1 0 1 0 0 1 1 0 1
MSv31165V1
In transmission mode, once the DMA has written all the data to be transmitted (the TCIF flag
is set in the DMA_ISR register), the TC flag can be monitored to make sure that the USART
communication is complete. This is required to avoid corrupting the last transmission before
disabling the USART or entering Stop mode. Software must wait until TC=1. The TC flag
remains cleared during all data transfers and it is set by hardware at the end of transmission
of the last frame.
USART_TDR F1 F2 F3
TC flag Set by
hardware
DMA writes
USART_TDR
Cleared
DMA TCIF flag by
Set by hardware software
(transfer
complete)
ai17192b
Set by hardware
RXNE flag cleared by DMA read
DMA request
USART_RDR F1 F2 F3
DMA reads
USART_RDR
Cleared
DMA TCIF flag Set by hardware by
(transfer complete) software
USART 1 USART 2
TX RX
TX circuit RX circuit
CTS RTS
RX TX
RX circuit TX circuit
RTS CTS
MSv31169V2
RS232 RTS and CTS flow control can be enabled independently by writing the RTSE and
CTSE bits respectively to 1 (in the USART_CR3 register).
RTS
MSv68794V1
CTS CTS
CTS
Note: For correct behavior, CTS must be deasserted at least 3 USART clock source periods
before the end of the current character. In addition it should be noted that the CTSCF flag
may not be set for pulses shorter than 2 x PCLK periods.
For code example, refer to A.15.12: USART hardware flow control code example.
Note: If the USART kernel clock is kept ON during Stop mode, there is no constraint on the
maximum baud rate that allows waking up from Stop mode. It is the same as in Run mode.
• USART source clock is LSE
Same principle as described in case of USART source clock is HSI with the difference
that the LSE is ON in Stop mode, but the LSE clock is not propagated to USART if the
USART is not requesting it. The LSE clock is not OFF but there is a clock gating to
avoid useless consumption.
When the USART clock source is configured to be fLSE or fHSI, it is possible to keep enabled
this clock during STOP mode by setting the UCESM bit in USART_CR3 control register.
The MCU wakeup from Stop mode can be done using the standard RXNE interrupt. In this
case, the RXNEIE bit must be set before entering Stop mode.
Alternatively, a specific interrupt may be selected through the WUS bit fields.
In order to be able to wake up the MCU from Stop mode, the UESM bit in the USART_CR1
control register must be set prior to entering Stop mode.
When the wakeup event is detected, the WUF flag is set by hardware and a wakeup
interrupt is generated if the WUFIE bit is set.
Note: Before entering Stop mode, the user must ensure that the USART is not performing a
transfer. BUSY flag cannot ensure that Stop mode is never entered during a running
reception.
The WUF flag is set when a wakeup event is detected, independently of whether the MCU is
in Stop or in an active mode.
When entering Stop mode just after having initialized and enabled the receiver, the REACK
bit must be checked to ensure the USART is actually enabled.
When DMA is used for reception, it must be disabled before entering Stop mode and re-
enabled upon exit from Stop mode.
The wakeup from Stop mode feature is not available for all modes. For example it doesn’t
work in SPI mode because the SPI operates in master mode only.
Let us take this example: OVER8 = 0, M bits = 10, ONEBIT = 1, BRR [3:0] = 0000.
In these conditions, according to Table 120: Tolerance of the USART receiver when BRR
[3:0] = 0000, the USART receiver tolerance is 4.86 %.
DTRA + DQUANT + DREC + DTCL + DWU < USART receiver's tolerance
DWU max = tWUUSART / (9 x Tbit Min)
Tbit Min = tWUUSART / (9 x DWU max)
If we consider an ideal case where the parameters DTRA, DQUANT, DREC and DTCL are
at 0%, the DWU max is 4.86 %. In reality, we need to consider at least the HSI inaccuracy.
Let us consider HSI inaccuracy = 1 %, tWUUSART = 8.1 μs (in case of Stop mode with main
regulator in Run mode, Range 1 ):
DWU max = 4.86 % - 1 % = 3.86 %
Tbit min = 8.1 µs / (9 ₓ 3.86 %) = 23.31 μs.
In these conditions, the maximum baud rate allowing to wakeup correctly from Stop mode is
1/23.31 μs = 42 kbaud.
Sleep No effect. USART interrupt causes the device to exit Sleep mode.
Low-power run No effect.
No effect. USART interrupt causes the device to exit Low-power sleep
Low-power sleep
mode.
The USART is able to wake up the MCU from Stop mode when the UESM
bit is set and the USART clock is set to HSI16 or LSE.
Stop
The MCU wakeup from Stop mode can be done using the standard RXNE
interrupt.
The USART is powered down and must be reinitialized when the device
Standby
has exited from Standby mode.
The USART interrupt events are connected to the same interrupt vector (see Figure 237).
• During transmission: Transmission Complete, Transmission complete before guard
time, Clear to Send, Transmit data Register empty or Framing error (in Smartcard
mode) interrupt.
• During reception: Idle Line detection, Overrun error, Receive data register not empty,
Parity error, LIN break detection, Noise Flag, Framing Error, Character match, etc.
These events generate an interrupt if the corresponding Enable Control Bit is set.
MSv19820V1
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. M1 EOBIE RTOIE DEAT[4:0] DEDT[4:0]
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
OVER8 CMIE MME M0 WAKE PCE PS PEIE TXEIE TCIE RXNEIE IDLEIE TE RE UESM UE
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
MSBFI
ADD[7:4] ADD[3:0] RTOEN ABRMOD[1:0] ABREN DATAINV TXINV RXINV
RST
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
SWAP LINEN STOP[1:0] CLKEN CPOL CPHA LBCL Res. LBDIE LBDL ADDM7 Res. Res. Res. Res.
rw rw rw rw rw rw rw rw rw rw rw
Note: The 3 bits (CPOL, CPHA, LBCL) should not be written while the transmitter is enabled.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
TCBGT SCARC SCARC SCARC
Res. Res. Res. Res. Res. Res. Res. UCESM WUFIE WUS1 WUS0 Res.
IE NT2 NT1 NT0
rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
OVRDI ONEBI
DEP DEM DDRE CTSIE CTSE RTSE DMAT DMAR SCEN NACK HDSEL IRLP IREN EIE
S T
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
BRR[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
GT[7:0] PSC[7:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
BLEN[7:0] RTO[23:16]
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
RTO[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Note: RTOR can be written on the fly. If the new value is lower than or equal to the counter, the
RTOF flag is set.
This register is reserved and forced by hardware to “0x00000000” when the Receiver
timeout feature is not supported. Please refer to Section 24.4: USART implementation on
page 659.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. TXFRQ RXFRQ MMRQ SBKRQ ABRRQ
w w w w w
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. TCBGT Res. Res. REACK TEACK WUF RWU SBKF CMF BUSY
r r r r r r r r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
ABRF ABRE Res. EOBF RTOF CTS CTSIF LBDF TXE TC RXNE IDLE ORE NF FE PE
r r r r r r r r r r r r r r r
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. WUCF Res. Res. CMCF Res.
rc_w1 rc_w1
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
TCBGT
Res. Res. Res. EOBCF RTOCF Res. CTSCF LBDCF TCCF Res. IDLECF ORECF NCF FECF PECF
CF
rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. Res. Res. Res. Res. Res. Res. RDR[8:0]
r r r r r r r r r
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. Res. Res. Res. Res. Res. Res. TDR[8:0]
rw rw rw rw rw rw rw rw rw
0x1C
0x0C
Offset
RM0377
24.8.12
Register
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
USART_ISR
USART_ICR
USART_CR3
USART_CR2
USART_CR1
USART_RDR
USART_BRR
USART_RQR
USART_GTPR
USART_RTOR
0
0
Res. Res. Res. Res. Res. Res. Res. Res. 31
0
0
Res. Res. Res. Res. Res. Res. Res. Res. 30
0
0
Res. Res. Res. Res. Res. Res. Res. Res. 29
ADD[7:4]
0
0
0
Res. Res. Res. Res. Res. Res. Res. M1 28
0
0
0
Res. Res. Res. Res. Res. Res. Res. EOBIE 27
BLEN[7:0]
0
0
0
Res. Res. Res. Res. Res. Res. Res. RTOIE 26
1
0
0
0
Res. Res. TCBGT Res. Res. Res. Res. DEAT4 25
USART register map
ADD[3:0]
0
0
0
0
Res. Res. Res. Res. Res. Res. TCBGTIE DEAT3 24
0
0
0
Res. Res. Res. Res. Res. Res. UCESM RTOEN 0 DEAT2 23
0
0
0
0
0
Res. Res. REACK Res. Res. Res. WUFIE ABRMOD1 DEAT1 22
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
RM0377 Rev 10
0
0
0
0
0
0
0
0
0
0
0
Res. Res. ABRF Res. DEP SWAP OVER8 15
0
0
0
0
0
0
0
Res. Res. ABRE Res. DEM LINEN CMIE 14
0
0
0
0
0
0
Res. Res. Res. Res. DDRE MME 13
[1:0]
0
0
0
0
0
0
0
0
STOP
0
0
0
0
0
0
0
0
GT[7:0]
Res. RTOCF RTOF Res. ONEBIT CLKEN WAKE 11
RTO[23:0]
0
0
0
0
0
0
0
Table 125. USART register map and reset values
The table below gives the USART register map and reset values.
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
IDLECF IDLE TXFRQ NACK ADDM7 IDLEIE 4
0
0
0
0
0
0
0
X X X X X X
RDR[8:0]
ORECF ORE RXFRQ HDSEL Res. TE 3
PSC[7:0]
0
0
0
0
0
0
0
X
NCF NF MMRQ IRLP Res. RE 2
0
0
0
0
0
0
0
X
FECF FE SBKRQ IREN Res. UESM 1
0
0
0
0
0
0
0
X
PECF PE ABRRQ EIE Res. UE 0
Universal synchronous/asynchronous receiver transmitter (USART/UART)
723/905
767
Universal synchronous/asynchronous receiver transmitter (USART/UART) RM0377
Offset Register
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
10
11
9
8
7
6
5
4
3
2
1
0
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
Res.
USART_TDR TDR[8:0]
0x28
Reset value X X X X X X X X X
25.1 Introduction
The low-power universal asynchronous receiver transmitted (LPUART) is an UART which
allows Full-duplex UART communications with a limited power consumption. Only
32.768 kHz LSE clock is required to allow UART communications up to 9600 baud. Higher
baud rates can be reached when the LPUART is clocked by clock sources different from the
LSE clock.
Even when the microcontroller is in Stop mode, the LPUART can wait for an incoming UART
frame while having an extremely low energy consumption. The LPUART includes all
necessary hardware support to make asynchronous serial communications possible with
minimum power consumption.
It supports Half-duplex Single-wire communications and Modem operations (CTS/RTS).
It also supports multiprocessor communications.
DMA (direct memory access) can be used for data transmission/reception.
Through these pins, serial data is transmitted and received in normal LPUART mode as
frames comprising:
• An Idle Line prior to transmission or reception
• A start bit
• A data word (7 or 8 or 9 bits) least significant bit first
• 1, 2 stop bits indicating that the frame is complete
• The LPUART interface uses a baud rate generator
• A status register (LPUART_ISR)
• Receive and transmit data registers (LPUART_RDR, LPUART_TDR)
• A baud rate register (LPUART_BRR)
Refer to Section 25.7: LPUART registers for the definitions of each bit.
The following pins are required in RS232 Hardware flow control mode:
• CTS: Clear To Send blocks the data transmission at the end of the current transfer
when high
• RTS: Request to send indicates that the LPUART is ready to receive data (when low).
The following pin is required in RS485 Hardware control mode:
• DE: Driver Enable activates the transmission mode of the external transceiver.
Note: DE and RTS share the same pin.
PRDATA PWDATA
Read Write DR (data register)
(CPU or DMA) (CPU or DMA)
TX
Transmit data register Receive data register
(TDR) (RDR)
RX LPUART_GTPR register
GT PSC CK control CK
RTS/ Hardware
DE flow
CTS controller Receiver
clock
Transmit Wakeup Receiver
control unit control
LPUART
interrupt
control
LPUARTx_BRR register
Transmitter
rate controller
Transmitter
/LPUARTDIV BRR[19:0]
clock
Clock **
Start
Idle frame bit
Clock **
Start
Idle frame bit
Clock **
Start
Idle frame bit
Character transmission
During an LPUART transmission, data shifts out least significant bit first (default
configuration) on the TX pin. In this mode, the LPUART_TDR register consists of a buffer
(TDR) between the internal bus and the transmit shift register (see Figure 213).
Every character is preceded by a start bit which is a logic level low for one bit period. The
character is terminated by a configurable number of stop bits.
The following stop bits are supported by LPUART: 1 and 2 stop bits.
Note: The TE bit must be set before writing the data to be transmitted to the LPUART_TDR.
The TE bit should not be reset during transmission of data. Resetting the TE bit during the
transmission will corrupt the data on the TX pin as the baud rate counters will get frozen.
The current data being transmitted will be lost.
An idle frame will be sent after the TE bit is enabled.
Configurable stop bits
The number of stop bits to be transmitted with every character can be programmed in
Control register 2, bits 13,12.
• 1 stop bit: This is the default value of number of stop bits.
• 2 stop bits: This will be supported by normal LPUART, Single-wire and Modem
modes.
An idle frame transmission will include the stop bits.
A break transmission will be 10 low bits (when M[1:0] = 00) or 11 low bits (when M[1:0] = 01)
or 9 low bits (when M[1:0] = 10) followed by 2 stop bits. It is not possible to transmit long
breaks (break of length greater than 9/10/11 low bits).
a) 1 Stop bit
Possible
Data frame Next Next data frame
parity bit
start
Start bit Bit0 Bit1 Bit2 Bit3 Bit4 Bit5 Bit6 Bit7 Stop
bit
bit
CLOCK **
b) 2 Stop bits
Possible
Data frame Next Next data frame
parity bit
start
Start bit Bit0 Bit1 Bit2 Bit3 Bit4 Bit5 Bit6 Bit7 2
bit
Stop
bits
MS31885V1
TX line
Set by hardware Set by hardware
TXE flag cleared by software cleared by software Set by hardware
LPUART_DR F1 F2 F3
Set by hardware
TC flag
Software
Software waits until TXE=1 TC is not set TC is not set TC is set
enables the and writes F2 into DR
LPUART because TXE=0 because TXE=0 because TXE=1
MSv31889V1
Break characters
Setting the SBKRQ bit transmits a break character. The break frame length depends on the
M bits (see Figure 239).
If a ‘1’ is written to the SBKRQ bit, a break character is sent on the TX line after completing
the current character transmission. The SBKF bit is set by the write operation and it is reset
by hardware when the break character is completed (during the stop bits after the break
character). The LPUART inserts a logic 1 signal (STOP) for the duration of 2 bits at the end
of the break frame to guarantee the recognition of the start bit of the next frame.
In the case the application needs to send the break character following all previously
inserted data, including the ones not yet transmitted, the software should wait for the TXE
flag assertion before setting the SBKRQ bit.
Idle characters
Setting the TE bit drives the LPUART to send an idle frame before the first data frame.
Character reception
During an LPUART reception, data shifts in least significant bit first (default configuration)
through the RX pin. In this mode, the LPUART_RDR register consists of a buffer (RDR)
between the internal bus and the received shift register.
Character reception procedure
1. Program the M bits in LPUART_CR1 to define the word length.
2. Select the desired baud rate using the baud rate register LPUART_BRR
3. Program the number of stop bits in LPUART_CR2.
4. Enable the LPUART by writing the UE bit in LPUART_CR1 register to 1.
5. Select DMA enable (DMAR) in LPUART_CR3 if multibuffer communication is to take
place. Configure the DMA register as explained in multibuffer communication.
6. Set the RE bit LPUART_CR1. This enables the receiver which begins searching for a
start bit.
When a character is received
• The RXNE bit is set. It indicates that the content of the shift register is transferred to the
RDR. In other words, data has been received and can be read (as well as its
associated error flags).
• An interrupt is generated if the RXNEIE bit is set.
• The error flags can be set if a frame error, noise or an overrun error has been detected
during reception. PE flag can also be set with RXNE.
• In multibuffer, RXNE is set after every byte received and is cleared by the DMA read of
the Receive data Register.
• In single buffer mode, clearing the RXNE bit is performed by a software read to the
LPUART_RDR register. The RXNE flag can also be cleared by writing 1 to the RXFRQ
in the LPUART_RQR register. The RXNE bit must be cleared before the end of the
reception of the next character to avoid an overrun error.
Break character
When a break character is received, the LPUART handles it as a framing error.
Idle character
When an idle frame is detected, there is the same procedure as for a received data
character plus an interrupt if the IDLEIE bit is set.
Overrun error
An overrun error occurs when a character is received when RXNE has not been reset. Data
can not be transferred from the shift register to the RDR register until the RXNE bit is
cleared.
The RXNE flag is set after every byte received. An overrun error occurs if RXNE flag is set
when the next data is received or the previous DMA request has not been serviced. When
an overrun error occurs:
• The ORE bit is set.
• The RDR content will not be lost. The previous data is available when a read to
LPUART_RDR is performed.
• The shift register will be overwritten. After that point, any data received during overrun
is lost.
• An interrupt is generated if either the RXNEIE bit is set or EIE bit is set.
• The ORE bit is reset by setting the ORECF bit in the ICR register.
Note: The ORE bit, when set, indicates that at least 1 data has been lost. There are two
possibilities:
- if RXNE=1, then the last valid data is stored in the receive register RDR and can be read,
- if RXNE=0, then it means that the last valid data has already been read and thus there is
nothing to be read in the RDR. This case can occur when the last valid data is read in the
RDR at the same time as the new (and lost) data is received.
Framing error
A framing error is detected when the stop bit is not recognized on reception at the expected
time, following either a de-synchronization or excessive noise.
When the framing error is detected:
• The FE bit is set by hardware.
• The invalid data is transferred from the Shift register to the LPUART_RDR register.
• No interrupt is generated in case of single byte communication. However this bit rises
at the same time as the RXNE bit which itself generates an interrupt. In case of
multibuffer communication an interrupt will be issued if the EIE bit is set in the
LPUART_CR3 register.
The FE bit is reset by writing 1 to the FECF in the LPUART_ICR register.
256 × f CK
Tx/Rx baud = -----------------------------------
-
LPUARTDIV
Table 127. Error calculation for programmed baud rates at fck = 32.768 kHz
Baud rate fCK = 32.768 kHz
Table 128. Error calculation for programmed baud rates at fck = 32 MHz
Baud rate fCK = 32.768 kHz
where
DWU is the error due to sampling point deviation when the wakeup from Stop mode is
used.
when M[1:0] = 01:
t WULPUART
DWU = -----------------------------
-
11 × Tbit
Note: The data specified in Table 129 may slightly differ in the special case when the received
frames contain some Idle frames of exactly 10-bit durations when M bits = 00 (11-bit
durations when M bits =01 or 9- bit durations when M bits = 10).
RXNE RXNE
MSv31154V1
Note: If the MMRQ is set while the IDLE character has already elapsed, mute mode will not be
entered (RWU is not set).
If the LPUART is activated while the line is IDLE, the idle state is detected after the duration
of one IDLE frame (not only after the reception of one character frame).
RX IDLE Addr=0 Data 1 Data 2 IDLE Addr=1 Data 3 Data 4 Addr=2 Data 5
Non-matching address
MSv31888V2
Even parity
The parity bit is calculated to obtain an even number of “1s” inside the frame which is made
of the 6, 7 or 8 LSB bits (depending on M bits values) and the parity bit.
As an example, if data=00110101, and 4 bits are set, then the parity bit will be 0 if even
parity is selected (PS bit in LPUART_CR1 = 0).
Odd parity
The parity bit is calculated to obtain an odd number of “1s” inside the frame made of the 6, 7
or 8 LSB bits (depending on M bits values) and the parity bit.
As an example, if data=00110101 and 4 bits set, then the parity bit will be 1 if odd parity is
selected (PS bit in LPUART_CR1 = 1).
TX line
Set by hardware cleared Set by hardware cleared by
by DMA read DMA read
TXE flag Set by hardware
a
LPUART_TDR F1 F2 F3
Software
The DMA
configures DMA writes F1
DMA writes F2 DMA writes F3 transfer is
DMA to send 3 into Software waits until TC=1
into into complete
data blocks
and enables LPUART_TDR LPUART_TDR LPUART_TDR (TCIF=1 in
DMA_ISR)
LPUART
MSv31890V2
DMA request
LPUART_RDR F1 F2 F3
DMA reads
LPUART_RDR
Cleared by
DMA TCIF flag Set by hardware software
(transfer complete)
MSv31891V3
LPUART 1 LPUART 2
TX RX
TX circuit RX circuit
CTS RTS
RX TX
RX circuit TX circuit
RTS CTS
MSv31892V2
RS232 RTS and CTS flow control can be enabled independently by writing the RTSE and
CTSE bits respectively to 1 (in the LPUART_CR3 register).
RTS
MSv68794V1
CTS CTS
CTS
Note: For correct behavior, CTS must be deasserted at least 3 LPUART clock source periods
before the end of the current character. In addition it should be noted that the CTSCF flag
may not be set for pulses shorter than 2 x PCLK periods.
Sleep No effect. USART interrupt causes the device to exit Sleep mode.
Low-power run No effect.
No effect. USART interrupt causes the device to exit Low-power sleep
Low-power sleep
mode.
The LPUART is able to wake up the MCU from Stop mode when the
UESM bit is set and the LPUART clock is set to HSI16 or LSE.
Stop
The MCU wakeup from Stop mode can be done using either the standard
RXNE or the WUF interrupt.
The LPUART is powered down and must be reinitialized when the device
Standby
has exited from Standby mode.
The LPUART interrupt events are connected to the same interrupt vector (see Figure 237).
• During transmission: Transmission Complete, Clear to Send, Transmit data Register
empty or Framing error interrupt.
• During reception: Idle Line detection, Overrun error, Receive data register not empty,
Parity error, Noise Flag, Framing Error, Character match, etc.
These events generate an interrupt if the corresponding Enable Control Bit is set.
TC
TCIE
TXE
TXEIE
CTSIF
CTSIE
IDLE
IDLEIE
LPUART
RXNEIE
ORE interrupt
RXNEIE
RXNE
PE
PEIE
LBDF
LBDIE
FE
NF
ORE EIE
CMF
CMIE
WUF
WUFIE
MS31886V1
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. M1 Res. Res. DEAT[4:0] DEDT[4: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
Res. CMIE MME M0 WAKE PCE PS PEIE TXEIE TCIE RXNEIE IDLEIE TE RE UESM UE
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
MSBFI
ADD[7:4] ADD[3:0] Res. Res. Res. Res. DATAINV TXINV RXINV
RST
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
SWAP Res. STOP[1:0] Res. Res. Res. Res. Res. Res. Res. ADDM7 Res. Res. Res. Res.
rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. UCESM WUFIE WUS[2:0] Res. Res. Res. Res.
rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
OVR HD
DEP DEM DDRE Res. CTSIE CTSE RTSE DMAT DMAR Res. Res. Res. Res. EIE
DIS SEL
rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. BRR[19:16]
rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
BRR[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Note: It is forbidden to write values less than 0x300 in the LPUART_BRR register.
Provided that LPUARTx_BRR must be > = 0x300 and LPUART_BRR is 20-bit, a care
should be taken when generating high baud rates using high fck values. fck must be in the
range [3 x baud rate,.4096 x baud rate].
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. RXFRQ MMRQ SBKRQ Res.
w w w
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
RE TE
Res. Res. Res. Res. Res. Res. Res. Res. Res. WUF RWU SBKF CMF BUSY
ACK ACK
r r r r r r r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. Res. Res. Res. Res. CTS CTSIF Res. TXE TC RXNE IDLE ORE NF FE PE
r r r r r r r r r r
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. WUCF Res. Res. CMCF Res.
w w
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. Res. Res. Res. Res. Res. CTSCF Res. Res. TCCF Res. IDLECF ORECF NCF FECF PECF
w w w w w w w
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. Res. Res. Res. Res. Res. Res. RDR[8:0]
r r r r r r r r r
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Res. Res. Res. Res. Res. Res. Res. TDR[8:0]
rw rw rw rw rw rw rw rw rw
0x1C
0x0C
0x10-
Offset
RM0377
25.7.10
CR3
CR2
CR1
TDR
BRR
RDR
RQR
LPUART_
LPUART_
LPUART_
LPUART_
LPUART_
LPUART_
LPUART_
Register
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
LPUART_ISR
LPUART_ICR
0
Res. Res. Res. Res. Res. Res. Res. Res. 31
0
Res. Res. Res. Res. Res. Res. Res. Res. 30
0
Res. Res. Res. Res. Res. Res. Res. Res. 29
ADD[7:4]
0
0
Res. Res. Res. Res. Res. Res. Res. M1 28
0
Res. Res. Res. Res. Res. Res. Res. Res. 27
0
Res. Res. Res. Res. Res. Res. Res. Res. 26
0
0
Res. Res. Res. Res. Res. Res. Res. DEAT4 25
ADD[3:0]
0
0
Res. Res. Res. Res. Res. Res. Res. DEAT3 24
LPUART register map
0
Res. Res. Res. Res. Res. Res. UCESM Res. 0 DEAT2 23
0
0
0
Res. Res. Res. REACK Res. Res. WUFIE Res. DEAT1 22
0
0
0
[1:0]
0
0
0
0
WUS
Res. Res. WUCF WUF Res. Res. Res. DEDT4 20
0
0
0
0
0
0
0
0
0
0
Reserved
RM0377 Rev 10
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
STOP
0
0
0
0
0
0
0
0
0
0
0
Res. Res. CTSCF CTSIF Res. CTSE Res. PS 9
BRR[19:0]
X
X
Res. Res. Res. RTSE Res. PEIE 8
1
0
0
0
X
X
Res. TXE Res. DMAT Res. TXEIE 7
0
1
0
0
0
X
X
TCCF TC Res. DMAR Res. TCIE 6
0
0
0
X
X
Res. RXNE Res. Res. Res. RXNEIE 5
0
0
0
0
X
X
Res. IDLE Res. Res. ADDM7 IDLEIE 4
0
0
0
0
0
X
X
TDR[8:0]
RDR[8:0]
ORECF ORE RXFRQ HDSEL Res. TE 3
0
0
0
0
X
X
NCF NF MMRQ Res. Res. RE 2
0
0
0
0
X
X
FECF FE SBKRQ Res. Res. UESM 1
0
0
0
0
X
X
Low-power universal asynchronous receiver transmitter (LPUART)
767/905
PECF PE Res. EIE Res. UE 0
767
Serial peripheral interface/ inter-IC sound (SPI/I2S) RM0377
26.1 Introduction
The SPI/I²S interface can be used to communicate with external devices using the SPI
protocol or the I2S audio protocol. SPI or I2S mode is selectable by software. SPI mode is
selected by default after a device reset.
The serial peripheral interface (SPI) protocol supports half-duplex, full-duplex and simplex
synchronous, serial communication with external devices. The interface can be configured
as 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.
The Inter-IC sound (I2S) protocol is also a synchronous serial communication interface. It
can operate in slave or master mode with half-duplex communication. Full duplex
operations are possible by combining two I2S blocks.
It can address four different audio standards including the Philips I2S standard, the MSB-
and LSB-justified standards and the PCM standard.
Read
Rx
buffer
CRC controller
MOSI
MISO Shift register
LSBFIRST CRCEN
CPOL CRCNEXT
CPHA
DFF
TX
buffer
Write Communication
BIDIOE controller
BIDIMODE
RXOLNY
Baud rate
SCK Internal NSS
generator BR[2:0]
NSS
NSS logic
MSv33711V1
Four I/O pins are dedicated to SPI communication with external devices.
• MISO: Master In / Slave Out data. In the general case, this pin is used to transmit data
in slave mode and receive data in master mode.
• MOSI: Master Out / Slave In data. In the general case, this pin is used to transmit data
in master mode and receive data in slave mode.
• SCK: Serial Clock output pin for SPI masters and input pin for SPI slaves.
• NSS: Slave select pin. Depending on the SPI and NSS settings, this pin can be used to
either:
– select an individual slave device for communication
– synchronize the data frame or
– detect a conflict between multiple masters
See Section 26.3.5: Slave select (NSS) pin management for details.
The SPI bus allows the communication between one master device and one or more slave
devices. The bus consists of at least two wires - one for the clock signal and the other for
synchronous data transfer. Other signals can be added depending on the data exchange
between SPI nodes and their slave select signal management.
Full-duplex communication
By default, the SPI is configured for full-duplex communication. In this configuration, the
shift registers of the master and slave are linked using two unidirectional lines between the
MOSI and the MISO pins. During SPI communication, data is shifted synchronously on the
SCK clock edges provided by the master. The master transmits the data to be sent to the
slave via the MOSI line and receives data from the slave via the MISO line. When the data
frame transfer is complete (all the bits are shifted) the information between the master and
slave is exchanged.
MISO MISO
Rx shift register Tx shift register
MOSI MOSI
Tx shift register Rx shift register
MSv39623V1
1. The NSS pins can be used to provide a hardware control flow between master and slave. Optionally, the
pins can be left unused by the peripheral. Then the flow has to be handled internally for both master and
slave. For more details see Section 26.3.5: Slave select (NSS) pin management.
Half-duplex communication
The SPI can communicate in half-duplex mode by setting the BIDIMODE bit in the
SPIx_CR1 register. In this configuration, one single cross connection line is used to link the
shift registers of the master and slave together. During this communication, the data is
synchronously shifted between the shift registers on the SCK clock edge in the transfer
direction selected reciprocally by both master and slave with the BDIOE bit in their
SPIx_CR1 registers. In this configuration, the master’s MISO pin and the slave’s MOSI pin
are free for other application uses and act as GPIOs.
(2)
MISO MISO
Rx shift register Tx shift register
(3)
MOSI 1kΩ (2)
Tx shift register MOSI Rx shift register
MSv39624V1
1. The NSS pins can be used to provide a hardware control flow between master and slave. Optionally, the
pins can be left unused by the peripheral. Then the flow has to be handled internally for both master and
slave. For more details see Section 26.3.5: Slave select (NSS) pin management.
2. In this configuration, the master’s MISO pin and the slave’s MOSI pin can be used as GPIOs.
3. A critical situation can happen when communication direction is changed not synchronously between two
nodes working at bidirectionnal mode and new transmitter accesses the common data line while former
transmitter still keeps an opposite value on the line (the value depends on SPI configuration and
communication data). Both nodes then fight while providing opposite output levels on the common line
temporary till next node changes its direction settings correspondingly, too. It is suggested to insert a serial
resistance between MISO and MOSI pins at this mode to protect the outputs and limit the current blowing
between them at this situation.
Simplex communications
The SPI can communicate in simplex mode by setting the SPI in transmit-only or in receive-
only using the RXONLY bit in the SPIx_CR2 register. In this configuration, only one line is
used for the transfer between the shift registers of the master and slave. The remaining
MISO and MOSI pins pair is not used for communication and can be used as standard
GPIOs.
• Transmit-only mode (RXONLY=0): The configuration settings are the same as for full-
duplex. The application has to ignore the information captured on the unused input pin.
This pin can be used as a standard GPIO.
• Receive-only mode (RXONLY=1): The application can disable the SPI output function
by setting the RXONLY bit. In slave configuration, the MISO output is disabled and the
pin can be used as a GPIO. The slave continues to receive data from the MOSI pin
while its slave select signal is active (see 26.3.5: Slave select (NSS) pin management).
Received data events appear depending on the data buffer configuration. In the master
configuration, the MOSI output is disabled and the pin can be used as a GPIO. The
clock signal is generated continuously as long as the SPI is enabled. The only way to
stop the clock is to clear the RXONLY bit or the SPE bit and wait until the incoming
pattern from the MISO pin is finished and fills the data buffer structure, depending on its
configuration.
MSv39625V1
1. The NSS pins can be used to provide a hardware control flow between master and slave. Optionally, the
pins can be left unused by the peripheral. Then the flow has to be handled internally for both master and
slave. For more details see Section 26.3.5: Slave select (NSS) pin management.
2. An accidental input information is captured at the input of transmitter Rx shift register. All the events
associated with the transmitter receive flow must be ignored in standard transmit only mode (e.g. OVF
flag).
3. In this configuration, both the MISO pins can be used as GPIOs.
Note: Any simplex communication can be alternatively replaced by a variant of the half-duplex
communication with a constant setting of the transaction direction (bidirectional mode is
enabled while BDIO bit is not changed).
NSS (1)
MISO MISO
Rx shift register Tx shift register
MOSI MOSI
Tx shift register Rx shift register
MISO
Tx shift register
MOSI
Rx shift register
SCK
NSS
Slave 2
MISO
Tx shift register
MOSI
Rx shift register
SCK
NSS
Slave 3
MSv39626V1
1. NSS pin is not used on master side at this configuration. It has to be managed internally (SSM=1, SSI=1) to
prevent any MODF error.
2. As MISO pins of the slaves are connected together, all slaves must have the GPIO configuration of their
MISO pin set as alternate function open-drain (see Section 8.3.7: I/O alternate function input/output on
page 221).
MISO MISO
Rx (Tx) shift register Rx (Tx) shift register
MOSI MOSI
Tx (Rx) shift register Tx (Rx) shift register
MSv39628V1
1. The NSS pin is configured at hardware input mode at both nodes. Its active level enables the MISO line
output control as the passive node is configured as a slave.
– NSS output enable (SSM=0,SSOE = 1): this configuration is only used when the
MCU is set as master. The NSS pin is managed by the hardware. The NSS signal
is driven low as soon as the SPI is enabled in master mode (SPE=1), and is kept
low until the SPI is disabled (SPE =0).
– NSS output disable (SSM=0, SSOE = 0): if the microcontroller is acting as the
master on the bus, this configuration allows multimaster capability. If the NSS pin
is pulled low in this mode, the SPI enters master mode fault state and the device is
automatically reconfigured in slave mode. In slave mode, the NSS pin works as a
standard “chip select” input and the slave is selected while NSS line is at low level.
NSS Master
Slave mode
Inp. mode
Vdd OK Non active
1 Vss Conflict Active
NSS Input
0
NSS GPIO
pin logic
aiv14746e
CPOL = 0
NSS
(to slave)
Capture strobe
CPHA =0
CPOL = 1
CPOL = 0
NSS
(to slave)
Capture strobe
ai17154d
Refer to Section 26.3.11: Communication using DMA (direct memory addressing) for details
on how to handle DMA.
Tx buffer handling
The data frame is loaded from the Tx buffer into the shift register during the first bit
transmission. Bits are then shifted out serially from the shift register to a dedicated output
pin depending on LSBFIRST bit setting.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 of the
SPI_CR2 register is set. Clearing the TXE bit is performed by writing to the SPI_DR register.
A continuous transmit stream can be achieved if the next data to be transmitted are stored
in the Tx buffer while previous frame transmission is still ongoing. When the software writes
to Tx buffer while the TXE flag is not set, the data waiting for transaction is overwritten.
Rx buffer handling
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.
If a device has not cleared the RXNE bit resulting from the previous data byte transmitted,
an overrun condition occurs when the next value is buffered. The OVR bit is set and an
interrupt is generated if the ERRIE bit is set.
Another way to manage the data exchange is to use DMA (see Section 10.2: DMA main
features).
Sequence handling
The BSY bit is set when a current data frame transaction is ongoing. When the clock signal
runs continuously, the BSY flag remains set between data frames on the master side.
However, on the slave side, it becomes low for a minimum duration of one SPI clock cycle
between each data frame transfer.
For some configurations, the BSY flag can be used during the last data transfer to wait until
the completion of the transfer.
When a receive-only mode is configured on the master side, either in half-duplex
(BIDIMODE=1, BIDIOE=0) or simplex configuration (BIDIMODE=0, RXONLY=1), the
master starts the receive sequence as soon as the SPI is enabled. Then the clock signal is
provided by the master and it does not stop until either the SPI or the receive-only mode is
disabled by the master. The master receives data frames continuously up to this moment.
While the master can provide all the transactions in continuous mode (SCK signal is
continuous), it has to respect slave capability to handle data flow and its content at anytime.
When necessary, the master must slow down the communication and provide either a
slower clock or separate frames or data sessions with sufficient delays. Be aware there is no
underflow error signal for slave operating in SPI mode, and that data from the slave are
always transacted and processed by the master even if the slave cannot not prepare them
correctly in time. It is preferable for the slave to use DMA, especially when data frames are
shorter and bus rate is high.
Each sequence must be encased by the NSS pulse in parallel with the multislave system to
select just one of the slaves for communication. In single slave systems, using NSS to
control the slave is not necessary. However, the NSS pulse can be used to synchronize the
slave with the beginning of each data transfer sequence. NSS can be managed either by
software or by hardware (see Section 26.3.4: Multi-master communication).
Refer to Figure 258 and Figure 259 for a description of continuous transfers in master / full-
duplex and slave full-duplex mode.
SCK
software software waits software waits software waits software waits software waits
writes 0xF1 until TXE=1 and until RXNE=1 until TXE=1 and until RXNE=1 until RXNE=1
into SPI_DR writes 0xF2 into and reads 0xA1 writes 0xF3 into and reads 0xA2 and reads 0xA3
SPI_DR from SPI_DR SPI_DR from SPI_ DR from SPI_DR
ai17343
For code example, refer to A.17.3: SPI full duplex communication code example.
SCK
software software waits software waits software waits software waits software waits
writes 0xF1 until TXE=1 and until RXNE=1 until TXE=1 and until RXNE=1 until RXNE=1
into SPI_DR writes 0xF2 into and reads 0xA1 writes 0xF3 into and reads 0xA2 and reads 0xA3
SPI_DR from SPI_DR SPI_DR from SPI_ DR from SPI_DR
ai17344
Note: During discontinuous communications, there is a 2 APB clock period delay between the
write operation to the SPI_DR register and BSY bit setting. As a consequence it is
mandatory to wait first until TXE is set and then until BSY is cleared after writing the last
data.
The correct disable procedure for certain receive-only modes is:
1. Interrupt the receive flow by disabling SPI (SPE=0) in the specific time window while
the last data frame is ongoing.
2. Wait until BSY=0 (the last data frame is processed).
3. Read received data.
Note: To stop a continuous receive sequence, a specific time window must be respected during
the reception of the last data frame. It starts when the first bit is sampled and ends before
the last bit transfer starts.
SCK
TXE flag cleared by DMA write clear by DMA write set by hardware
reset
BSY flag set by hardware by hardware
software configures the DMA writes DMA writes DMA writes DMA transfer is software waits software waits until BSY=0
DMA SPI Tx channel DATA1 into DATA2 into DATA3 into complete (TCIF=1 in until TXE=1
to send 3 data items SPI_DR SPI_DR SPI_DR DMA_ISR)
and enables the SPI
ai17349
SCK
DMA request
Rx buffer
0xA1 0xA2 0xA3
(read from SPI_DR)
clear
set by hardware
flag DMA TCIF by software
(DMA transfer complete)
software configures the DMA reads DMA reads DMA reads The DMA transfer is
DMA SPI Rx channel DATA1 from DATA2 from DATA3 from complete (TCIF=1 in
to receive 3 data items SPI_DR SPI_DR SPI_DR DMA_ISR)
and enables the SPI
ai17350
The BSY flag is cleared under any one of the following conditions:
• When the SPI is correctly disabled
• When a fault is detected in Master mode (MODF bit set to 1)
• In Master mode, when it finishes a data transmission and no new data is ready to be
sent
• In Slave mode, when the BSY flag is set to '0' for at least one SPI clock cycle between
each data transfer.
Note: It is recommended to use always the TXE and RXNE flags (instead of the BSY flags) to
handle data transmission or reception operations.
26.4.1 TI mode
TI protocol in master mode
The SPI interface is compatible with the TI protocol. The FRF bit of the SPIx_CR2 register
can be used to configure the SPI to be compliant with this protocol.
The clock polarity and phase are forced to conform to the TI protocol requirements whatever
the values set in the SPIx_CR1 register. NSS management is also specific to the TI protocol
which makes the configuration of NSS management through the SPIx_CR1 and SPIx_CR2
registers (SSM, SSI, SSOE) impossible in this case.
In slave mode, the SPI baud rate prescaler is used to control the moment when the MISO
pin state changes to HiZ when the current transaction finishes (see Figure 262). Any baud
rate can be used, making it possible to determine this moment with optimal flexibility.
However, the baud rate is generally set to the external master clock baud rate. The delay for
the MISO signal to become HiZ (trelease) depends on internal resynchronization and on the
baud rate value set in through the BR[2:0] bits in the SPIx_CR1 register. It is given by the
formula:
t baud_rate t baud_rate
---------------------- + 4 × t pclk < t release < ---------------------
- + 6 × t pclk
2 2
If the slave detects a misplaced NSS pulse during a data frame transaction the TIFRE flag is
set.
This feature is not available for Motorola SPI communications (FRF bit set to 0).
Note: To detect TI frame errors in slave transmitter only mode by using the Error interrupt
(ERRIE=1), the SPI must be configured in 2-line unidirectional mode by setting BIDIMODE
and BIDIOE to 1 in the SPI_CR1 register. When BIDIMODE is set to 0, OVR is set to 1
because the data register is never read and error interrupts are always generated, while
when BIDIMODE is set to 1, data are not received and OVR is never set.
Figure 262 shows the SPI communication waveforms when TI mode is selected.
NSS
tri ng
g
t RELEASE
in
in
i
er
er
sa r
pl
pl
pl
e
gg
gg
gg
m
m
sa
sa
tri
tr i
SCK
FRAME 1 FRAME 2
MS19835V2
CRC principle
CRC calculation is enabled by setting the CRCEN bit in the SPIx_CR1 register before the
SPI is enabled (SPE = 1). The CRC value is calculated using an odd programmable
polynomial on each bit. The calculation is processed on the sampling clock edge defined by
the CPHA and CPOL bits in the SPIx_CR1 register. The calculated CRC value is checked
automatically at the end of the data block as well as for transfer managed by CPU or by the
DMA. When a mismatch is detected between the CRC calculated internally on the received
data and the CRC sent by the transmitter, a CRCERR flag is set to indicate a data corruption
error. The right procedure for handling the CRC calculation depends on the SPI
configuration and the chosen transfer management.
Note: The polynomial value should only be odd. No even values are supported.
The received CRC is stored in the Rx buffer like any other data frame.
A CRC-format transaction takes one more data frame to communicate at the end of data
sequence.
When the last CRC data is received, an automatic check is performed comparing the
received value and the value in the SPIx_RXCRC register. Software has to check the
CRCERR flag in the SPIx_SR register to determine if the data transfers were corrupted or
not. Software clears the CRCERR flag by writing '0' to it.
After the CRC reception, the CRC value is stored in the Rx buffer and must be read in the
SPIx_DR register in order to clear the RXNE flag.
Tx buffer
CRC CH
16-bit BSY OVR MODF UDR TxE RxNE FRE
ERR SIDE
MOSI/SD
Shift register
MISO LSB first Communication
16-bit control
Rx buffer
NSS/WS
I2S
I2SE
MOD
SPI LSB
SPE BR2 BR1 BR0 MSTR CPOL CPHA
baud rate generator First
CK
I2SMOD
MCK I2SxCLK
MCKOE ODD I2SDIV[7:0]
MS32126V1
STM32 STM32
MCK (O) MCK (O)
spix_tx_dm SD (O) spix_rx_dm SD (I)
a SPI2Sx a SPI2Sx
(MASTER-TX) CK (O) (MASTER-RX) CK (O)
External External
WS (O) WS (O)
slave slave
device device
WS (I) WS (I)
spix_rx_dm SPI2Sy CK (I) spix_tx_dm SPI2Sy CK (I)
a (SLAVE-RX) a (SLAVE-TX)
SD (I) SD (O)
MSv42093V1
The I2S interface supports four audio standards, configurable using the I2SSTD[1:0] and
PCMSYNC bits in the SPIx_I2SCFGR register.
Figure 265. I2S Philips protocol waveforms (16/32-bit full accuracy, CPOL = 0)
CK
WS transmission reception
Channel left
Channel
right
MS19591V1
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 266. I2S Philips standard waveforms (24-bit frame with CPOL = 0)
CK
WS Transmission Reception
MS19592V1
This mode needs two write or read operations to/from the SPIx_DR register.
• In transmission mode:
If 0x8EAA33 has to be sent (24-bit):
MS19593V1
• In reception mode:
If data 0x8EAA33 is received:
MS19594V1
Figure 269. I2S Philips standard (16-bit extended to 32-bit packet frame with
CPOL = 0)
CK
WS Transmission Reception
MS19599V1
When 16-bit data frame extended to 32-bit channel frame is selected during the I2S
configuration phase, only one access to the SPIx_DR register 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 270 is required.
Figure 270. Example of 16-bit data frame extended to 32-bit channel frame
0x76A3
MS19595V1
For transmission, each time an MSB is written to SPIx_DR, the TXE flag is set and its
interrupt, if allowed, is generated to load the SPIx_DR register 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).
Figure 271. MSB Justified 16-bit or 32-bit full-accuracy length with CPOL = 0
CK
WS Transmission Reception
Channel left
Channel right
MS30100 V1
Data are latched on the falling edge of CK (for transmitter) and are read on the rising edge
(for the receiver).
CK
WS Transmission Reception
Channel right
MS30101V1
Figure 273. MSB justified 16-bit extended to 32-bit packet frame with CPOL = 0
CK
WS Transmission Reception
Channel right
MS30102V1
CK
WS
Transmission Reception
16- or 32-bit data
SD
MSB LSB MSB
Channel left
Channel right
MS30103V1
CK
WS Reception
Transmission
8-bit data 24-bit remaining
SD 0 forced
MSB LSB
MS30104V1
• In transmission mode:
If data 0x3478AE have to be transmitted, two write operations to the SPIx_DR register
are required by software or by DMA. The operations are shown below.
0xXX34 0x78AE
• In reception mode:
If data 0x3478AE are received, two successive read operations from the SPIx_DR
register are required on each RXNE event.
0xXX34 0x78AE
MS19597V1
Figure 278. LSB justified 16-bit extended to 32-bit packet frame with CPOL = 0
CK
Reception
WS
Transmission
16-bit data 16-bit remaining
SD 0 forced
MSB LSB
When 16-bit data frame extended to 32-bit channel frame is selected during the I2S
configuration phase, Only one access to the SPIx_DR register 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 279 is required.
Figure 279. Example of 16-bit data frame extended to 32-bit channel frame
Only one access to the SPIx-DR register
0x76A3
MS19598V1
In transmission mode, when a TXE event occurs, the application has to write the data to be
transmitted (in this case 0x76A3). The 0x000 field is transmitted first (extension on 32-bit).
The TXE flag is set 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
SPIx_I2SCFGR register.
CK
WS
short frame
13-bits
WS
long frame
MS30106V1
For long frame synchronization, the WS signal assertion time is fixed to 13 bits in master
mode.
For short frame synchronization, the WS synchronization signal is only one cycle long.
Figure 281. PCM standard waveforms (16-bit extended to 32-bit packet frame)
CK
WS
short frame
Up to 13-bits
WS
long frame
16 bits
SD MSB LSB
MS30107V1
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 SPIx_I2SCFGR register) even in
slave mode.
32- or 64-bits
FS
sampling point sampling point
MS30108V1
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 283 presents the communication clock architecture. The I2SxCLK clock is provided
by the RCC block, refer to the RCC section for details.
MCK
MCKOE
I²SMOD
CHLEN
MS30109V1
1. Where x = 2.
The audio sampling frequency may be 192 KHz, 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 SPIx_I2SPR register is set):
fS = I2SxCLK / [(16*2)*((2*I2SDIV)+ODD)*8)] when the channel frame is 16-bit wide
fS = I2SxCLK / [(32*2)*((2*I2SDIV)+ODD)*4)] when the channel frame is 32-bit wide
When the master clock is disabled (MCKOE bit cleared):
fS = I2SxCLK / [(16*2)*((2*I2SDIV)+ODD))] when the channel frame is 16-bit wide
fS = I2SxCLK / [(32*2)*((2*I2SDIV)+ODD))] when the channel frame is 32-bit wide
Table 136 provides example precision values for different clock configurations.
Note: Other configurations are possible that allow optimum clock precision.
Procedure
1. Select the I2SDIV[7:0] bits in the SPIx_I2SPR register to define the serial clock baud
rate to reach the proper audio sample frequency. The ODD bit in the SPIx_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 SPIx_I2SPR register if the master clock MCK needs to be provided
to the external 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 26.6.4: Clock generator).
3. Set the I2SMOD bit in the SPIx_I2SCFGR register to activate the I2S functions and
choose the I2S 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 I2S master mode and direction (Transmitter or Receiver)
through the I2SCFG[1:0] bits in the SPIx_I2SCFGR register.
4. If needed, select all the potential interrupt sources and the DMA capabilities by writing
the SPIx_CR2 register.
5. The I2SE bit in SPIx_I2SCFGR register must be set.
WS and CK are configured in output mode. MCK is also an output, if the MCKOE bit in
SPIx_I2SPR is set.
Transmission sequence
The transmission sequence begins when a half-word is written into the Tx buffer.
Lets assume the first data written into the Tx buffer corresponds to the left channel data.
When data are transferred from the Tx buffer to the shift register, TXE is set and data
corresponding to the right channel 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 SPIx_CR2 register is set.
For more details about the write operations depending on the I2S Standard-mode selected,
refer to Section 26.6.3: Supported audio protocols).
To ensure a continuous audio data transmission, it is mandatory to write the SPIx_DR
register with the next data to transmit before the end of the current transmission.
To switch off the I2S, by clearing I2SE, it is mandatory to wait for TXE = 1 and BSY = 0.
Reception sequence
The operating mode is the same as for transmission mode except for the point 3 (refer to the
procedure described in Section 26.6.5: 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 SPIx_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 SPIx_DR register.
CHSIDE is updated after each reception. It is sensitive to the WS signal generated by the
I2S cell.
For more details about the read operations depending on the I2S Standard-mode selected,
refer to Section 26.6.3: 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 SPIx_CR2 register, an
interrupt is generated to indicate the error.
To switch off the I2S, specific actions are required to ensure that the I2S 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 I2S clock cycles (using a software loop)
c) Disable the I2S (I2SE = 0)
• 16-bit data length extended on 32-bit channel length (DATLEN = 00 and CHLEN = 1) in
MSB justified, I2S or PCM modes (I2SSTD = 00, I2SSTD = 01 or I2SSTD = 11,
respectively)
a) Wait for the last RXNE
b) Then wait 1 I2S clock cycle (using a software loop)
c) Disable the I2S (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 I2S:
a) Wait for the second to last RXNE = 1 (n – 1)
b) Then wait one I2S clock cycle (using a software loop)
c) Disable the I2S (I2SE = 0)
Note: The BSY flag is kept low during transfers.
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 I2S data register has to be loaded before
the master initiates the communication.
For the I2S, 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 I2S 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 SPIx_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 I2S Standard-mode selected,
refer to Section 26.6.3: Supported audio protocols.
To secure a continuous audio data transmission, it is mandatory to write the SPIx_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
SPIx_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 SPIx_CR2
register, an interrupt is generated when the UDR flag in the SPIx_SR register goes high. In
this case, it is mandatory to switch off the I2S and to restart a data transfer starting from the
left channel.
To switch off the I2S, by clearing the I2SE bit, it is mandatory to wait for TXE = 1 and
BSY = 0.
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 26.6.6: I2S slave mode), where the configuration should
set the master reception mode using the I2SCFG[1:0] bits in the SPIx_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 SPIx_SR
register is set and an interrupt is generated if the RXNEIE bit is set in the SPIx_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 the SPIx_DR
register. It is sensitive to the external WS line managed by the external master component.
Clearing the RXNE bit is performed by reading the SPIx_DR register.
For more details about the read operations depending the I2S Standard-mode selected,
refer to Section 26.6.3: Supported audio protocols.
If data are received while the preceding 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 SPIx_CR2 register, an
interrupt is generated to indicate the error.
To switch off the I2S 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.
The BSY flag is useful to detect the end of a transfer if the software needs to disable the I2S.
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 I2S 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 I2S 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 I2S 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.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
BIDI BIDI CRC CRC RX LSB
DFF SSM SSI SPE BR [2:0] MSTR CPOL CPHA
MODE OE EN NEXT ONLY FIRST
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Res. Res. Res. Res. Res. Res. Res. Res. TXEIE RXNEIE ERRIE FRF Res. SSOE TXDMAEN RXDMAEN
rw rw rw rw rw rw rw
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
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
PCMSY
Res. Res. Res. Res. I2SMOD I2SE I2SCFG Res. I2SSTD CKPOL DATLEN CHLEN
NC
rw rw rw rw rw rw rw rw rw rw rw
0x1C
0x0C
Offset
RM0377
26.7.10
SPI_SR
SPI_DR
Register
SPI_CR2
SPI_CR1
SPI_I2SPR
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
Reset value
SPI_CRCPR
SPI_TXCRCR
SPI_RXCRCR
SPI_I2SCFGR
Res. Res. Res. Res. Res. Res. Res. Res. Res. 31
Res. Res. Res. Res. Res. Res. Res. Res. Res. 30
Res. Res. Res. Res. Res. Res. Res. Res. Res. 29
Res. Res. Res. Res. Res. Res. Res. Res. Res. 28
SPI register map
RM0377 Rev 10
Res. Res. Res. Res. Res. Res. Res. Res. Res. 17
Res. Res. Res. Res. Res. Res. Res. Res. Res. 16
Res. Res. Res. Res. BIDIMODE 15
Res. Res. Res. Res. BIDIOE 14
Res. Res. Res. Res. CRCEN 13
Res. Res. Res. Res. CRCNEXT 12
Table 138. SPI register map and reset values
0 0 0 0 0
PCMSYNC BSY TXEIE LSBFIRST 7
DR[15:0]
TxCRC[15:0]
RxCRC[15:0]
MODF ERRIE
CRCPOLY[15:0]
I2SSTD 5
0 0 0 0
CRCERR FRF
BR
4
[2:0]
I2SDIV
CHSIDE SSOE MSTR 2
DATLEN
TXE TXDMAEN CPOL 1
0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
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
0 0 0 0 0 0 0 0 1 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 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0
0 0 0
Serial peripheral interface/ inter-IC sound (SPI/I2S)
817/905
CHLEN RXNE RXDMAEN CPHA 0
817
Debug support (DBG) RM0377
27.1 Overview
The STM32L0x1 devices are built around a Cortex®-M0+ core which contains hardware
extensions for advanced debugging features. The debug extensions allow the core to be
stopped either on a given instruction fetch (breakpoint) or data access (watchpoint). When
stopped, the core’s internal state and the system’s external state may be examined. Once
examination is complete, the core and the system may be restored and program execution
resumed.
The debug features are used by the debugger host when connecting to and debugging the
STM32L0x1 MCUs.
One interface for debug is available:
• Serial wire
Figure 284. Block diagram of STM32L0x1 MCU and Cortex®-M0+-level debug support
Bus matrix
System
interface
Cortex-M0
Core
Debug AP
DWT
BPU
MS19240V2
1. The debug features embedded in the Cortex®-M0+ core are a subset of the Arm® CoreSight Design Kit.
® ®
The Arm Cortex -M0+ core provides integrated on-chip debug support. It is comprised of:
• SW-DP: Serial wire
• BPU: Break point unit
• DWT: Data watchpoint trigger
DBG_IDCODE
Address: 0x4001 5800
Only 32-bit access supported. Read-only
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
REV_ID
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
Res. Res. Res. Res. DEV_ID
r r r r r r r r r r r
0x1000 Rev A
0x1008 Rev 1, Z - Rev Z -
0x1018 - - Rev Y -
0x1038 - - Rev 1, P, Q, X -
0x2000 - Rev B - Rev B
0x2008 - Rev Y - Rev 1, P, Q, Z
0x2018 - Rev 1, X - -
Refer to the Cortex®-M0+ TRM for a detailed description of DPACC and APACC registers.
The packet request is always followed by the turnaround time (default 1 bit) where neither
the host nor target drive the line.
001: FAULT
0..2 ACK 010: WAIT
100: OK
The ACK Response must be followed by a turnaround time only if it is a READ transaction
or if a WAIT or FAULT acknowledge has been received.
WDATA or
0..31 Write or Read data
RDATA
32 Parity Single parity of the 32 data bits
The DATA transfer must be followed by a turnaround time only if it is a READ transaction.
Further details of the SW-DP state machine can be found in the Cortex®-M0+ TRM and the
CoreSight Design Kit r1p0 TRM.
Table 145. 32-bit debug port registers addressed through the shifted value A[3:2]
Address A[3:2] value Description
These registers are not reset by a system reset. They are only reset by a power-on reset.
Refer to the Cortex®-M0+ TRM for further details.
To Halt on reset, it is necessary to:
• enable the bit0 (VC_CORRESET) of the Debug and Exception Monitor Control
Register
• enable the bit0 (C_DEBUGEN) of the Debug Halting Control and Status Register
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
DBG_
DBG_ DBG_
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. STAND
STOP SLEEP
BY
rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
DBG_LPTIMER_STO
DBG_I2C3_STOP
DBG_I2C1_STOP
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
P
rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
DBG_WWDG_STOP
DBG_IWDG_STOP
.DBG_TIM3_STOP
.DBG_TIM2_STOP
DBG_TIM7_STOP
DBG_TIM6_STOP
DBG_RTC_STOP
rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
DBG_TIM22_STOP
DBG_TIM21_STOP
Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res. Res.
rw rw
Addr.
832/905
27.10
DBG_
DBG_
DBG_
IDCODE
DBG_CR
APB2_FZ
APB1_FZ
Register
Reset value
Reset value
Reset value
Reset value(1)
0
DBG_LPTIMER_STO
X
Res. Res. 31
Debug support (DBG)
0
X
Res. DBG_I2C3_STOP. Res. 30
X
Res. Res. Res. 29
X
Res. Res. Res. 28
X
Res. Res. Res. 27
X
Res. Res. Res. 26
X
Res. Res. Res. 25
DBG register map
0
X X X X
RM0377 Rev 10
X
0
Res. DBG_IWDG_STOP Res. Res. 12
0
X
0
X
0
X
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
F_SIZE
r r r r r r r r r r r r r r r r
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
U_ID(31:16)
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
U_ID(15:0)
r r r r r r r r r r r r r r r r
63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48
U_ID(63:48)
r r r r r r r r r r r r r r r r
47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32
U_ID(47:32)
r r r r r r r r r r r r r r r r
95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80
U_ID(95:80)
r r r r r r r r r r r r r r r r
79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64
U_ID(79:64)
r r r r r r r r r r r r r r r r
A.1 Introduction
This appendix shows the code examples of the sequence described in this Reference
Manual.
These code examples are extracted from the STM32L0xx Snippet firmware package
STM32SnippetsL0 available on www.st.com.
These code examples used the peripheral bit and register description from the CMSIS
header file (stm32l0xx.h).
Code lines starting with // should be uncommented if the given register has been modified
before.
tickstart = Tick;
while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL) /* (4) */
{
if ((Tick - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
{
error = ERROR_CLKSWITCH_TIMEOUT; /* Report an error */
return;
}
}
Note: Tick is a global variable incremented in the SysTick ISR each millisecond.
{
/* For robust implementation, add here time-out management */
}
if ((FLASH->PECR & FLASH_PECR_PELOCK) == 0) /* (2) */
{
if ((FLASH->PECR & FLASH_PECR_PRGLOCK) != 0) /* (3) */
{
FLASH->PRGKEYR = FLASH_PRGKEY1; /* (4) */
FLASH->PRGKEYR = FLASH_PRGKEY2;
}
}
/* (3) Enter in wait for interrupt. The EOP check is done in the Flash ISR
*/
/* (6) Reset the ERASE and DATA bits in the FLASH_PECR register
to disable the page erase */
FLASH->PECR |= FLASH_PECR_ERASE | FLASH_PECR_DATA; /* (1) */
*(__IO uint32_t *)addr = (uint32_t)0; /* (2) */
__WFI(); /* (3) */
FLASH->PECR &= ~(FLASH_PECR_ERASE | FLASH_PECR_DATA); /* (4) */
* Retval None
*/
__INLINE __RAM_FUNC void OptionByteErase(uint8_t index)
{
/* (1) Set the ERASE bit in the FLASH_PECR register
to enable option byte erasing */
/* (2) Write a 32-bit word value at the option byte address to be erased
to start the erase sequence */
/* (3) Wait until the BSY bit is reset in the FLASH_SR register */
/* (4) Check the EOP flag in the FLASH_SR register */
/* (5) Clear EOP flag by software by writing EOP at 1 */
/* (6) Reset the ERASE and PROG bits in the FLASH_PECR register
to disable the page erase */
FLASH->PECR |= FLASH_PECR_ERASE; /* (1) */
*(__IO uint32_t *)(OB_BASE + index) = 0; /* (2) */
while ((FLASH->SR & FLASH_SR_BSY) != 0) /* (3) */
{
/* For robust implementation, add here time-out management */
}
if ((FLASH->SR & FLASH_SR_EOP) != 0) /* (4) */
{
FLASH->SR |= FLASH_SR_EOP; /* (5) */
}
else
{
/* Manage the error cases */
}
FLASH->PECR &= ~(FLASH_PECR_ERASE); /* (6) */
}
Note: This function must be loaded in RAM.
}
else
{
/* Manage the error cases */
}
/**
* This function handles RCC interrupt request
* and switch the system clock to HSE.
* Param None
* Retval None
*/
void RCC_CRS_IRQHandler(void)
{
/* (1) Check the flag HSE ready */
/* (2) Clear the flag HSE ready */
/* (3) Switch the system clock to HSE */
if ((RCC->CifR & RCC_CifR_HSERDYF) != 0) /* (1) */
{
RCC->CICR |= RCC_CICR_HSERDYC; /* (2) */
RCC->CFGR = ((RCC->CFGR & (~RCC_CFGR_SW)) | RCC_CFGR_SW_HSE); /* (3) */
}
else
{
/* Manage error */
}
}
A.5 GPIOs
A.6 DMA
A.8 ADC
{
while ((ADC1->ISR & ADC_ISR_ADRDY) == 0) /* (3) */
{
/* For robust implementation, add here time-out management */
}
}
- (int32_t) *TEMP30_CAL_ADDR );
temperature = temperature * (int32_t)(130 - 30);
temperature = temperature / (int32_t)(*TEMP130_CAL_ADDR -
*TEMP30_CAL_ADDR);
temperature = temperature + 30;
return(temperature);
}
A.9 Timers
}
}
else
{
/* Manage error */
}
Note: This code manages only single counter overflows. To manage several counter overflows,
the update interrupt must be enabled (UIE = 1) and properly managed.
TIMx->CCMR1 |= TIM_CCMR1_CC2S_0; /* (1 */
//TIMx->CCER &= ~TIM_CCER_CC2P; /* (2) */
TIMx->SMCR |= TIM_SMCR_SMS_2 | TIM_SMCR_SMS_1 \
| TIM_SMCR_TS_2 | TIM_SMCR_TS_1; /* (3) */
TIMx->PSC = 3999; /* (4) */
| TIM_CCMR1_OC1PE
#if PULSE_WITHOUT_DELAY > 0
| TIM_CCMR1_OC1FE
#endif
; /* (4) */
TIMx->CCER |= TIM_CCER_CC1E; /* (5) */
TIMx->CR1 |= TIM_CR1_OPM | TIM_CR1_ARPE; /* (6) */
/* (6) Set TIMx Autoreload to 999 in order to get an overflow (so an UEV)
each 100ms */
/* (7) Set capture compare register to a value between 0 and 999 */
TIMx->CR2 |= TIM_CR2_MMS_2; /* (1 */
TIMx->CCMR1 |= TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1; /* (2) */
TIMy->SMCR |= TIM_SMCR_SMS_2 | TIM_SMCR_SMS_0; /* (3) */
TIMx->PSC = 2; /* (4) */
TIMy->PSC = 2; /* (5) */
TIMx->ARR = 999; /* (6) */
TIMx-> CCR1 = 700; /* (7) */
/* (1) Enable the slave counter first by writing CEN=1 in the TIMy_CR1
register. */
/* (2) Enable the master counter by writing CEN=1 in the TIMx_CR1
register. */
TIMy->CR1 |= TIM_CR1_CEN; /* (1) */
TIMx->CR1 |= TIM_CR1_CEN; /* (2) */
TIMy_SMCR register. */
/* (4) Set TIMx prescaler to 2 */
/* (5) Set TIMy prescaler to 2 */
/* (6) Set TIMx Autoreload to 99 in order to get an overflow (so an UEV)
each 10ms */
/* (7) Set capture compare register to a value between 0 and 99 */
TIMx->CR2 |= TIM_CR2_MMS_0; /* (1 */
TIMx->CCMR1 |= TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1; /* (2) */
TIMy->SMCR |= TIM_SMCR_SMS_2 | TIM_SMCR_SMS_0; /* (3) */
TIMx->PSC = 2; /* (4) */
TIMy->PSC = 2; /* (5) */
TIMx->ARR = 99; /* (6) */
TIMx-> CCR1 = 25; /* (7) */
{
TimeToCompute = RTC->TR; /* get time */
DateToCompute = RTC->DR; /* need to read date also */
}
A.14.8 I2C configured in master mode to transmit with DMA code example
/* (1) Timing register value is computed with the AN4235 xls file,
fast Mode @400kHz with I2CCLK = 16MHz, rise time = 100ns, fall time =
10ns */
/* (2) Periph enable */
/* (3) Slave address = 0x5A, write transfer, 2 bytes to transmit, autoend */
I2C2->TIMINGR = (uint32_t)0x00300619; /* (1) */
I2C2->CR1 = I2C_CR1_PE | I2C_CR1_TXDMAEN; /* (2) */
I2C2->CR2 = I2C_CR2_AUTOEND | (SIZE_OF_DATA << 16) |
(I2C1_OWN_ADDRESS<<1); /* (3) */
A.14.9 I2C configured in slave mode to receive with DMA code example
/* (1) Timing register value is computed with the AN4235 xls file,
fast Mode @400kHz with I2CCLK = 16MHz, rise time = 100ns, fall time =
10ns */
/* (2) Periph enable, receive DMA enable */
/* (3) 7-bit address = 0x5A */
/* (4) Enable own address 1 */
I2C1->TIMINGR = (uint32_t)0x00300619; /* (1) */
I2C1->CR1 = I2C_CR1_PE | I2C_CR1_RXDMAEN | I2C_CR1_ADDRIE; /* (2) */
I2C1->OAR1 |= (uint32_t)(I2C1_OWN_ADDRESS << 1); /* (3) */
I2C1->OAR1 |= I2C_OAR1_OA1EN; /* (4) */
Revision history
CRC
04-May-2015 2 Updated Section 4.2: CRC main features and Section :
Polynomial programmability.
FIREWALL
Updated Section 5.3.5: Firewall initialization.
PWR
Removed limitation related to 1.8V minimum VDDA for ADC and
updated VREF+ in Section 6.1: Power supplies. Updated
packages in Section 6.1.1: Independent A/D converter supply
and reference voltage. Updated Figure 10: Power supply
overview. Updated Range 1 description in Section 6.1.4:
Dynamic voltage scaling management.
Updated Table 34: Summary of low-power modes.
Added Section 6.3.5: Entering low-power mode and Section
6.3.6: Exiting low-power mode.
Updated Section 6.3.7: Sleep mode to remove details on mode
entry and exit and updated Table 35: Sleep-now and Table 36:
Sleep-on-exit.
PWR (continued)
Updated Section 6.3.8: Low-power sleep mode (LP sleep) to
remove details on mode entry and exit and updated Table 37:
Sleep-now (Low-power sleep) and Table 38: Sleep-on-exit (Low-
power sleep).
Updated Section 6.3.9: Stop mode to remove details on mode
entry and exit and updated Table 39: Stop mode.
Updated Section 6.3.10: Standby mode to remove details on
mode entry and exit and updated Table 40: Standby mode
Updated LPRUN bit description in Section 6.4.1: PWR power
control register (PWR_CR).
Added EWUP3 bit in Section 6.4.2: PWR power control/status
register (PWR_CSR).
RCC
Updated ADC clock in Section 7.2: Clocks.
Added HSE failure in Section 7.2.9: HSE clock security system
(CSS).
Section 7.3.6: Clock interrupt clear register (RCC_CICR):
changed all bit access type to ‘w, renamed USB bit into UFB and
bit moved to bit 3.
04-May-2015 2 (continued)
Renamed MIFIEN into FWEN and description updated in Section
7.3.13: APB2 peripheral clock enable register (RCC_APB2ENR).
Updated Section 7.3.20: Control/status register (RCC_CSR).
Section 7.3.10: APB1 peripheral reset register
(RCC_APB1RSTR): Added USART4RST, USART5RST,
TIM3RST, TIM7RST and I2C3RST. Renamed UARTxRST bits
into USARTxRST.
Added IOPERST in Section 7.3.7: GPIO reset register
(RCC_IOPRSTR), IOPEENR in Section 7.3.11: GPIO clock
enable register (RCC_IOPENR), and IOPESMEN in Section
7.3.15: GPIO clock enable in Sleep mode register
(RCC_IOPSMENR).
Section 7.3.14: APB1 peripheral clock enable register
(RCC_APB1ENR): Added USART4EN, USART5EN, TIM3EN
and TIM7EN and I2C3EN. Renamed UARTxEN bits into
USARTxEN.
Section 7.3.18: APB1 peripheral clock enable in Sleep mode
register (RCC_APB1SMENR): Added USART4SMEN,
USART5SMEN, TIM3SMEN, TIM7SMEN and I2C3SMEN.
Renamed UARTxSMEN bits into USARTxSMEN.
Added I2C3SEL bits in Section 7.3.19: Clock configuration
register (RCC_CCIPR).
GPIOs
Add Port E for category 2 and 5 devices.
SYSCFG
Updated Figure 1: System architecture to add STM32L07/08
peripherals. Added UBS bit in Section 9.2.1: SYSCFG memory
remap register (SYSCFG_CFGR1). Replaced REF_CFGR3 by
SYSCFG_CFGR3. Added I2C3_FMP bit and updated CAPA bits
in Section 9.2.2: SYSCFG peripheral mode configuration register
(SYSCFG_CFGR2).
Updated Section 9.2.4: SYSCFG external interrupt configuration
register 1 (SYSCFG_EXTICR1), Section 9.2.5: SYSCFG
external interrupt configuration register 2 (SYSCFG_EXTICR2),
Section 9.2.6: SYSCFG external interrupt configuration register
3 (SYSCFG_EXTICR3) and Section 9.2.7: SYSCFG external
interrupt configuration register 4 (SYSCFG_EXTICR4).
DMA
Updated DMA mapping/channel selection for category 2 and 5
devices. Updated Figure 26: DMA request mapping to add AES.
INTERRUPTS
Changed number of priority levels from 16 to 4.
04-May-2015 2 (continued)
Updated Table 52: List of vectors and Table 53: EXTI lines
connections to add peripherals for category 2 and 5 devices and
update vectors 17 and 18.
Added bit24 in all EXTI registers.
ADC
Updated Figure 28: ADC block diagram.
Section 13.4.1: ADC voltage regulator (ADVREGEN): changed
REF_CTRL into REF_CFGR3 and ENBUF_EN_VREFINT_ADC
into ENBUF_VREFINT_ADC.
Removed limitation related to 1.8 V VDDA minimum value.
Changed VDDA= 3.3 V into 3 V in Section 13.10: Temperature
sensor and internal reference voltage.
Updated AWDCH bitfield definition in Section 13.12.4: ADC
configuration register 1 (ADC_CFGR1).
COMP
Updated Figure 51: Comparator 1 and 2 block diagrams.
Added COMP1LPTIMIN1 in Section 14.5.1: Comparator 1
control and status register (COMP1_CSR). Added
COMP2LPTIMIN2 and COMP2LPTIMIN1, and updated
COMP2INSEL definition in Section 14.5.2: Comparator 2 control
and status register (COMP2_CSR).
Basic timers
Added TIMER7.
LPTIM
Updated TRIGSEL description in Section 19.6.4: LPTIM
configuration register (LPTIM_CFGR). Added ext_trig5 in Table
82: LPTIM external trigger connection.
WWDG
Updated Figure 174: Watchdog block diagram and timeout
formula and example in Section 21.3.4: How to program the
watchdog timeout.
RTC
Added tamper 3 event (category 5 devices only).
Updated WUCKSEL bits in Figure 173: RTC block diagram.
Section 22.4.5: Programmable alarms: Changed MSK0 to MSK1
in caution note.
I2C
Updated NOSTRECH definition in Section 23.7.1: Control
register 1 (I2C_CR1).
USART
Added USART4/5 for category 2 and 5 devices.
Updated Figure 209: USART block diagram.
Added Low-power modes sections.
Updated Section : Single byte communication.
Updated Table 106: Error calculation for programmed baud rates
at fCK = 32 MHz in both cases of oversampling by 16 or by 8.
Updated Figure 226: IrDA SIR ENDEC- block diagram, Figure
228: Transmission using DMA and Figure 229: Reception using
DMA.
Removed UCESM bit from USARTx_CR3 as well as the
capability to keep enabled USART clock during Stop mode.
Updated REACK flag description in USARTx_ISR register.
LPUART
Updated Figure 234: LPUART block diagram.
04-May-2015 2 (continued)
Added Low-power modes sections.
Removed note in Section 25.4.1: LPUART character description.
Updated Table 113: Error calculation for programmed baudrates
at fck = 32,768 KHz
Updated Table 127: LPUART interrupt requests.
Changed LPUARTx_RDR and LPUARTx_TDR reset values in
Table 128: LPUART register map and reset values.
Removed UCESM bit from LPUART_CR3 as well as the
capability to keep enabled LPUART clock during Stop mode.
SPI
Updated Table 131: Audio-frequency precision using standard 8
MHz HSE.
DEBUG
Updated REV_ID bitfield in Section : DBG_IDCODE. Added bits
to support I2C3, TIM3 and TIM7 in Section 27.9.4: Debug MCU
APB1 freeze register (DBG_APB1_FZ).
GPIOs:
Updated OSPEEDy[1:0] definition in Section 8.4.3: GPIO port
output speed register (GPIOx_OSPEEDR) (x = A..E and H).
ADC:
Replaced AUTDLY by WAIT in Figure 28: ADC block diagram.
Updated Section : Analog reference for the ADC internal voltage
regulator. Updated ADC enable sequence in Section 13.4.3:
ADC on-off control (ADEN, ADDIS, ADRDY). Changed EXTSEL
into EXTEN in Section 13.4.10: Starting conversions (ADSTART)
and ADSTART bit description in Section 13.12.3: ADC control
register (ADC_CR).
LPTIM:
Updated Section 19.4.7: Operating mode.
Added Section Figure 168.: LPTIM output waveform, single
counting mode configuration, Section Figure 169.: LPTIM output
03-Aug-2015 3 waveform, Single counting mode configuration and Set-once
mode activated (WAVE bit is set) and Section Figure 170.:
LPTIM output waveform, Continuous counting mode
configuration.
Updated CNT bitfield definition in Section 19.6.8: LPTIM counter
register (LPTIM_CNT).
RTC:
Updated step 3 in Section : Programming the wakeup timer.
Modified WUTWF description in Section 22.7.4: RTC
initialization and status register (RTC_ISR)
I2C:
Added information on the stretch mechanism in Section : I2C
timings.
Updated definition of SCLDEL[3:0] and SDADEL[3:0] bits in
Timing register (I2C_TIMINGR).
USART:
Updated note related to RTO counter in Section : Block mode
(T=1).
LPUART:
Updated Figure 234: LPUART block diagram.
DEBUG:
Updated SWDIO bidirectional management in Section 27.5.1:
SWD protocol introduction.
03-Aug-2015 3 (continued)
Comparators (COMP)
Updated COMP2INPSEL bit definition to add PA7 (see Section
14.5.2: Comparator 2 control and status register
(COMP2_CSR)).
TIM2/3
20-Nov-2015 4 (continued)
Updated ETR_RMP bit definition in TIM2 option register
(TIM2_OR).
SPI/I2S:
Updated Figure 247, Figure 248, Figure 249 and Figure 250.
Updated and added notes below Figure 247, Figure 248 and
Figure 249.
Added Section 26.3.4: Multi-master communication.
Code examples:
Updated Section A.3.7: Program Option byte code example and
Section A.3.9: Program a single word to Flash program memory
code example, Section A.3.10: Program half-page to Flash
program memory code example and Section A.3.11: Erase a
page in Flash program memory code example.
Updated Section A.8.5: Single conversion sequence code
example - Software trigger, Section A.8.6: Continuous
20-Nov-2015 4 (continued) conversion sequence code example - Software trigger, Section
A.8.7: Single conversion sequence code example - Hardware
trigger, Section A.8.8: Continuous conversion sequence code
example - Hardware trigger, Section A.8.11: Wait mode
sequence code example, Section A.8.12: Auto off and no wait
mode sequence code example, Section A.8.13: Auto off and wait
mode sequence code example, Section A.8.14: Analog
watchdog code example and Section A.8.16: Temperature
configuration code example.
Updated Section A.9.4: Input capture data management code
example and Section A.9.10: ETR configuration to clear
OCxREF code example.
Updated Section A.13.1: RTC calendar configuration code
example, Section A.13.5: RTC calibration code example and
Section A.13.7: RTC tamper and time stamp code example.
Updated Section A.15.3: USART transfer complete code
example, Section A.15.6: USART LIN mode code example,
Section A.15.7: USART synchronous mode code example,
Section A.15.8: USART single-wire half-duplex code example,
Section A.15.9: USART smartcard mode code example, Section
A.15.10: USART IrDA mode code example, Section A.15.11:
USART DMA code example and Section A.15.12: USART
hardware flow control code example.
TIMER21/22
Updated SMS bit definition in Section 17.4.3: TIM21/22 slave
mode control register (TIMx_SMCR).
Restricted Table 82: TIM21/22 register map and reset values to
16 bits instead of 32.
TIMER6/7
Restricted Table 83: TIM6/7 register map and reset values to 16
bits instead of 32.
Debug
Updated Section 27.9.1: Debug support for low-power modes.
Updated Section 27.9.3: Debug MCU configuration register
(DBG_CR).
Added Table 135: REV-ID values in Section : DBG_IDCODE.
FIREWALL
Updated LENG bitfield description in Section 5.4.6: Volatile data
segment length (FW_VDSL).
Comparator (COMP)
Updated COMPx_CSR to add a note related to VREFINT in
COMP2INNSEL bit description.
Power controller
Updated Section 6.2.3: Programmable voltage detector (PVD).
Updated Section : Exiting Standby mode.
Updated VOSF bit description in Section 6.4.2: PWR power
control/status register (PWR_CSR).
Debug
Updated Cortex-M0+ ID code in Section 27.5.3: SW-DP state
machine (reset, idle states, ID code) and Section 27.5.5: SW-DP
registers.
Introduction:
Added errata sheet in the list of reference documents.
Added indication that patents apply to the devices.
Removed Arm logo.
Index
A EXTI_IMR . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
EXTI_PR . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
ADC_CALFACT . . . . . . . . . . . . . . . . . . . . . . .325
EXTI_RTSR . . . . . . . . . . . . . . . . . . . . . . . . . . 274
ADC_CCR . . . . . . . . . . . . . . . . . . . . . . . . . . .326
EXTI_SWIER . . . . . . . . . . . . . . . . . . . . . . . . . 275
ADC_CFGR1 . . . . . . . . . . . . . . . . . . . . . . . . .318
ADC_CFGR2 . . . . . . . . . . . . . . . . . . . . . . . . .322
ADC_CHSELR . . . . . . . . . . . . . . . . . . . . . . . .324 F
ADC_CR . . . . . . . . . . . . . . . . . . . . . . . . . . . .316 FLASH_ACR . . . . . . . . . . . . . . . . . . . . . . . . . 101
ADC_DR . . . . . . . . . . . . . . . . . . . . . . . . . . . .325 FLASH_CR . . . . . . . . . . . . . . . . . . . . . . . . . . 106
ADC_IER . . . . . . . . . . . . . . . . . . . . . . . . . . . .314 FLASH_KEYR . . . . . . . . . . . . . . . . . . . . . . . . 102
ADC_ISR . . . . . . . . . . . . . . . . . . . . . . . . . . . .313 FLASH_OPTKEYR . . . . . . . . . . . . . . . . 106-107
ADC_SMPR . . . . . . . . . . . . . . . . . . . . . . . . . .323 FLASH_OPTR . . . . . . . . . . . . . . . . . . . . . . . . 110
ADC_TR . . . . . . . . . . . . . . . . . . . . . . . . . . . . .324 FLASH_PDKEYR . . . . . . . . . . . . . . . . . . . . . 106
AES_CR . . . . . . . . . . . . . . . . . . . . . . . . . . . . .362 FLASH_PECR . . . . . . . . . . . . . . . . . . . . . . . . 102
AES_DINR . . . . . . . . . . . . . . . . . . . . . . . . . . .365 FLASH_PEKEYR . . . . . . . . . . . . . . . . . . . . . 106
AES_DOUTR . . . . . . . . . . . . . . . . . . . . . . . . .365 FLASH_PRGKEYR . . . . . . . . . . . . . . . . . . . . 106
AES_IVR . . . . . . . . . . . . . . . . . . . . . . . . . . . .368 FLASH_SR . . . . . . . . . . . . . . . . . . . . . . 106, 108
AES_KEYRx . . . . . . . . . . . . . . . . . . . . . . . . .366 FLASH_WRPROT1 . . . . . . . . . . . . . . . . . . . . 112
AES_SR . . . . . . . . . . . . . . . . . . . . . . . . . . . . .364 FLASH_WRPROT2 . . . . . . . . . . . . . . . . . . . . 113
FW_CR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
C FW_CSL . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
FW_CSSA . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
COMP1_CSR . . . . . . . . . . . . . . . . . . . . . . . . .331
FW_NVDSL . . . . . . . . . . . . . . . . . . . . . . . . . . 132
COMP2_CSR . . . . . . . . . . . . . . . . . . . . . . . . .333
FW_NVDSSA . . . . . . . . . . . . . . . . . . . . . . . . 132
CRC_CR . . . . . . . . . . . . . . . . . . . . . . . . . . . .121
FW_VDSL . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
CRC_DR . . . . . . . . . . . . . . . . . . . . . . . . . . . .120
FW_VDSSA . . . . . . . . . . . . . . . . . . . . . . . . . . 133
CRC_IDR . . . . . . . . . . . . . . . . . . . . . . . . . . . .120
CRC_INIT . . . . . . . . . . . . . . . . . . . . . . . . . . . .122
CRC_POL . . . . . . . . . . . . . . . . . . . . . . . . . . .122 G
GPIOx_AFRH . . . . . . . . . . . . . . . . . . . . . . . . 230
D GPIOx_AFRL . . . . . . . . . . . . . . . . . . . . . . . . 229
GPIOx_BRR . . . . . . . . . . . . . . . . . . . . . . . . . 230
DBG_APB1_FZ . . . . . . . . . . . . . . . . . . . . . . .829
GPIOx_BSRR . . . . . . . . . . . . . . . . . . . . . . . . 228
DBG_APB2_FZ . . . . . . . . . . . . . . . . . . . . . . .831
GPIOx_IDR . . . . . . . . . . . . . . . . . . . . . . . . . . 227
DBG_CR . . . . . . . . . . . . . . . . . . . . . . . . . . . .827
GPIOx_LCKR . . . . . . . . . . . . . . . . . . . . . . . . 228
DBG_IDCODE . . . . . . . . . . . . . . . . . . . . . . . .820
GPIOx_MODER . . . . . . . . . . . . . . . . . . . . . . 225
DBGMCU_CR . . . . . . . . . . . . . . . . . . . . . . . .827
GPIOx_ODR . . . . . . . . . . . . . . . . . . . . . . . . . 227
DMA_CCRx . . . . . . . . . . . . . . . . . . . . . . . . . .256
GPIOx_OSPEEDR . . . . . . . . . . . . . . . . . . . . 226
DMA_CMARx . . . . . . . . . . . . . . . . . . . . . . . . .260
GPIOx_OTYPER . . . . . . . . . . . . . . . . . . . . . . 225
DMA_CNDTRx . . . . . . . . . . . . . . . . . . . . . . . .259
GPIOx_PUPDR . . . . . . . . . . . . . . . . . . . . . . . 226
DMA_CPARx . . . . . . . . . . . . . . . . . . . . . . . . .260
DMA_IFCR . . . . . . . . . . . . . . . . . . . . . . . . . . .255
DMA_ISR . . . . . . . . . . . . . . . . . . . . . . . . . . . .253 I
DMA1_CSELR . . . . . . . . . . . . . . . . . . . . . . . .262 I2C_CR1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
I2C_CR2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644
E I2C_ICR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652
I2C_ISR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650
EXTI_EMR . . . . . . . . . . . . . . . . . . . . . . . . . . .273
I2C_OAR1 . . . . . . . . . . . . . . . . . . . . . . . . . . . 646
EXTI_FTSR . . . . . . . . . . . . . . . . . . . . . . . . . .275
I2C_OAR2 . . . . . . . . . . . . . . . . . . . . . . . . . . . 647
U
USART_BRR . . . . . . . . . . . . . . . . . . . . . . . . .712
USART_CR1 . . . . . . . . . . . . . . . . . . . . . . . . .701
USART_CR2 . . . . . . . . . . . . . . . . . . . . . . . . .704
USART_CR3 . . . . . . . . . . . . . . . . . . . . . . . . .708
USART_GTPR . . . . . . . . . . . . . . . . . . . . . . . .712
USART_ICR . . . . . . . . . . . . . . . . . . . . . . . . . .720
USART_ISR . . . . . . . . . . . . . . . . . . . . . . . . . .715
USART_RDR . . . . . . . . . . . . . . . . . . . . . . . . .722
USART_RQR . . . . . . . . . . . . . . . . . . . . . . . . .714
USART_RTOR . . . . . . . . . . . . . . . . . . . . . . . .713
USART_TDR . . . . . . . . . . . . . . . . . . . . . . . . .722
W
WWDG_CFR . . . . . . . . . . . . . . . . . . . . . . . . .541
WWDG_CR . . . . . . . . . . . . . . . . . . . . . . . . . .540
WWDG_SR . . . . . . . . . . . . . . . . . . . . . . . . . .541
STMicroelectronics NV and its subsidiaries (“ST”) reserve the right to make changes, corrections, enhancements, modifications, and
improvements to ST products and/or to this document at any time without notice. Purchasers should obtain the latest relevant information on
ST products before placing orders. ST products are sold pursuant to ST’s terms and conditions of sale in place at the time of order
acknowledgment.
Purchasers are solely responsible for the choice, selection, and use of ST products and ST assumes no liability for application assistance or
the design of Purchasers’ products.
Resale of ST products with provisions different from the information set forth herein shall void any warranty granted by ST for such product.
ST and the ST logo are trademarks of ST. For additional information about ST trademarks, please refer to www.st.com/trademarks. All other
product or service names are the property of their respective owners.
Information in this document supersedes and replaces information previously supplied in any prior versions of this document.