FT32F0xxx8 RM V1.30 en
FT32F0xxx8 RM V1.30 en
FT32F0xxx8
Reference manual
Introduction
This reference manual targets application developers. It provides complete information on how to use the
FT32F030x8/FT32F032x8/FT32F072x8 microcontroller memory and peripherals.
The FT32F0xxx8 is a family of microcontrollers with different memory sizes, packages and peripherals.
For ordering information, mechanical and electrical device characteristics, please refer to the corresponding
datasheet.
® ® ®
For information on the ARM Cortex -M0 core, please refer to the Cortex -M0 technical reference manual.
Related documents
®
Cortex -M0 technical reference manual, available from: http://infocenter.arm.com.
FT32F0xxx8 datasheets available from Fremont Micro Devices website: www.fremontmicro.com.
Rev1.3 1 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
Contents
1. Documentation conventions ................................................................................................................... 17
1.1. List of abbreviations for registers .................................................................................................. 17
1.2. Glossary ........................................................................................................................................ 17
1.3. Peripheral availability .................................................................................................................... 17
2. System and memory overview ............................................................................................................... 18
2.1. System architecture ...................................................................................................................... 18
2.2. Memory organization ..................................................................................................................... 20
2.2.1. Introduction ....................................................................................................................... 20
2.2.2. Memory map and register boundary addresses ............................................................... 21
2.3. Embedded SRAM ......................................................................................................................... 22
2.4. Flash memory overview ................................................................................................................ 23
2.5. Boot configuration ......................................................................................................................... 23
3. Embedded Flash memory ...................................................................................................................... 25
3.1. Flash main features ....................................................................................................................... 25
3.2. Flash memory functional description ............................................................................................ 25
3.2.1. Flash memory organization .............................................................................................. 25
3.2.2. Read operations ............................................................................................................... 26
3.2.3. Flash program and erase operations ............................................................................... 27
3.2.4. Memory protection ............................................................................................................ 31
3.2.5. Flash interrupts ................................................................................................................. 34
3.3. Flash register map ........................................................................................................................ 34
3.3.1. FLASH_ACR .................................................................................................................... 35
3.3.2. FLASH_KEYR .................................................................................................................. 36
3.3.3. FLASH_OPTKEYR ........................................................................................................... 36
3.3.4. FLASH_SR ....................................................................................................................... 37
3.3.5. FLASH_CR ....................................................................................................................... 38
3.3.6. FLASH_AR ....................................................................................................................... 39
3.3.7. FLASH_OBR .................................................................................................................... 39
3.3.8. FLASH_WRPR ................................................................................................................. 41
4. Option Byte............................................................................................................................................. 42
4.1. Option Byte Introduction ................................................................................................................ 42
4.2. Option Byte register map .............................................................................................................. 42
4.2.1. User and read protection option byte ............................................................................... 43
4.2.2. User data option byte........................................................................................................ 44
4.2.3. Write protection option byte1 ............................................................................................ 45
4.2.4. Write protection option byte2 ............................................................................................ 45
5. Cyclic redundancy check calculation unit (CRC) ................................................................................... 46
5.1. CRC main features ........................................................................................................................ 46
5.2. CRC functional description............................................................................................................ 46
5.3. CRC register map ......................................................................................................................... 48
5.3.1. CRC_DR ........................................................................................................................... 49
5.3.2. CRC_IDR .......................................................................................................................... 49
5.3.3. CRC_CR ........................................................................................................................... 49
5.3.4. CRC_INIT ......................................................................................................................... 50
Rev1.3 2 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
Rev1.3 3 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
Rev1.3 4 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
Rev1.3 5 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
Rev1.3 6 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
Rev1.3 7 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
Rev1.3 8 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
Rev1.3 9 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
Rev1.3 10 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
Rev1.3 11 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
Rev1.3 12 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
Rev1.3 13 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
Rev1.3 14 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
Rev1.3 15 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
Rev1.3 16 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
1. Documentation conventions
1.2. Glossary
This section gives a brief definition of acronyms and abbreviations used in this document:
Word: data of 32-bit length.
Half-word: data of 16-bit length.
Byte: data of 8-bit length.
SWD-DP(SWD DEBUG PORT): SWD-DP provides a 2-pin (clock and data) interface based on the
Serial Wire Debug (SWD) protocol. Please refer to the Cortex®-M0 technical reference manual.
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_LAUNCH: option byte loader.
AHB: advanced high-performance bus.
APB: advanced peripheral bus.
For peripheral availability and number across all sales types, please refer to the particular device datasheet.
Rev1.3 17 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
SRAM
2m3s
busMatrix GPIO PWR
I2C1/I2C2
UART1/UART2
CRC SPI1/SPI2
IWDG
DMAC WWDG
RTC
USB TIM1/3/6/14/15/16/17
AHB
LEB
EXTI
RCC ADC
IOSH1/IOSH2
COMP
TOUCH DAC
OP1/OP2
AHB2APB
CRS
APB1 DBGMCU
bridge
DIV
System bus
This bus connects the system bus of the Cortex ®-M0 core (peripherals bus) to a BusMatrix which manages
the arbitration between the core and the DMA.
DMA bus
Rev1.3 18 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
This bus connects the AHB master interface of the DMA to the BusMatrix which manages the access of CPU
and DMA to SRAM, Flash memory and peripherals.
BusMatrix
The BusMatrix manages the access arbitration between the core system bus and the DMA master bus. The
arbitration uses a Round Robin algorithm.
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.
Rev1.3 19 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
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 8 main blocks, of 512 Mbyte each.
0x4800 17FF
AHB
0x4800 0000
0xFFFF FFFF
7
Reserved
0xE010 0000
0xE000 0000 M0 peripherals
6 0x4002 4400
Reserved
5 0x4002 0000
Reserved
4 0x4001 8000
Reserved
0x1FFF FFFF
0x8000 0000 Reserved
APB
0x1FFF FA00
3 Option Bytes 0x4001 0000
Reserved 0x1FFFF800
All the memory areas that are not allocated to on-chip memories and peripherals are considered “Reserved”.
For the detailed mapping of available memory and register areas, please refer to the Memory map and
register boundary addresses chapter and peripheral chapters.
Rev1.3 20 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
See the datasheet corresponding to your device for a comprehensive diagram of the memory map.
The following table gives the boundary addresses of the peripherals available in the devices.
Table 2-1 Peripheral register boundary addresses
Bus Boundary address Size Peripheral
0x4800 1800 - 0x5FFF FFFF ~384 MB Reserved
0x4800 1400 - 0x4800 17FF 1 kB GPIOF
0x4800 1000 - 0x4800 13FF 1 kB Reserved
0x4800 0C00 - 0x4800 0FFF 1 kB GPIOD
0x4800 0800 - 0x4800 0BFF 1 kB GPIOC
0x4800 0400 - 0x4800 07FF 1 kB GPIOB
0x4800 0000 - 0x4800 03FF 1 kB GPIOA
0x4002 4400 - 0x47FF FFFF ~128 MB Reserved
AHB 0x4002 4000 - 0x4002 43FF 1 kB TSC
0x4002 3400 - 0x4002 3FFF 3 kB Reserved
0x4002 3000 - 0x4002 33FF 1 kB CRC
0x4002 2400 - 0x4002 2FFF 3 kB Reserved
0x4002 2000 - 0x4002 23FF 1 kB Flash Interface
0x4002 1400 - 0x4002 1FFF 3 kB Reserved
0x4002 1000 - 0x4002 13FF 1 kB RCC
0x4002 0400 - 0x4002 0FFF 3 kB Reserved
0x4002 0000 - 0x4002 03FF 1 kB DMA
APB 0x4001 8000 - 0x4001 FFFF 32 kB Reserved
0x4001 5C00 - 0x4001 7FFF 9 kB Reserved
0x4001 5800 - 0x4001 5BFF 1 kB DBGMCU
0x4001 4C00 - 0x4001 57FF 3 kB Reserved
0x4001 4800 - 0x4001 4BFF 1 kB TIM17
0x4001 4400 - 0x4001 47FF 1 kB TIM16
0x4001 4000 - 0x4001 43FF 1 kB TIM15
0x4001 3C00 - 0x4001 3FFF 1 kB Reserved
0x4001 3800 - 0x4001 3BFF 1 kB USART1
0x4001 3400 - 0x4001 37FF 1 kB Reserved
0x4001 3000 - 0x4001 33FF 1 kB SPI1
0x4001 2C00 - 0x4001 2FFF 1 kB TIM1
0x4001 2800 - 0x4001 2BFF 1 kB Reserved
0x4001 2400 - 0x4001 27FF 1 kB ADC
0x4001 0800 - 0x4001 23FF 7 kB Reserved
0x4001 0400 - 0x4001 07FF 1 kB EXTI
0x4001 0000 - 0x4001 03FF 1 kB SYSCFG+COMP+OP0+DAC
0x4000 7400 - 0x4000 FFFF 35 kB Reserved
Rev1.3 21 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
FT32F0xxx8 devices feature 8 Kbytes of static SRAM. This RAM can be accessed as bytes, half-words (16
bits) or full words (32 bits). This memory can be addressed at maximum system clock frequency without wait
state and thus by both CPU and DMA.
Rev1.3 22 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
In the FT32F0xxx8, three different boot modes can be selected through the BOOT0 pin and boot
configuration bits nBOOT1 in the User option byte, as shown in the following table.
Table 2-2 Boot modes
Boot mode configuration
Mode
nBOOT1 BOOT0 pin
x 0 Main Flash memory is selected as boot area
1 1 System memory is selected as boot area
0 1 Embedded SRAM is selected as boot area
The boot mode configuration is latched on the 4th rising edge of SYSCLK after a reset. It is up to the user to
set boot mode configuration related to the required boot mode.
The boot mode configuration is also re-sampled when exiting from Standby mode. Consequently they must
be kept in the required Boot mode configuration in Standby mode. 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, main Flash memory, system memory or SRAM is accessible as
follows:
Boot from main Flash memory: the main Flash 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 (0x1FFF E800)
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).
Rev1.3 23 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
Physical remap
Once the boot mode is selected, the application software can modify the memory accessible in the code area.
This modification is performed by programming the MEM_MODE bits in SYSCFG1 register. Unlike Cortex®
M3 and M4, the M0 CPU does not support the vector table relocation. For application code which is located
in a different address than 0x0800 0000, some additional code must be added in order to be able to serve the
application interrupts. A solution will be to relocate by software the vector table to the internal SRAM:
Copy the vector table from the Flash (mapped at the base of the application load address) to the base
address of the SRAM at 0x2000 0000.
Rev1.3 24 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
The Flash memory is organized as 64-bit wide memory cells that can be used for storing both code and data
constants. The memory organization of FT32F0xxx8 devices is based on a main Flash memory block
containing up to 128 pages of 0.5 Kbyte or up to 16 sectors of 4 Kbytes (2 pages).
Table 3-1 Flash memory organization
Flash area Address Size(kB) Name Description
Main area 0x8000000~0x80001FF 0.5 Page0 Sector0
0x8000200~0x80003FF 0.5 Page1
0x8000400~0x80005FF 0.5 Page2
0x8000600~0x80007FF 0.5 Page3
0x8000800~0x80009FF 0.5 Page4
0x8000A00~0x8000BFF 0.5 Page5
0x8000C00~0x8000DFF 0.5 Page6
0x8000E00~0x8000FFF 0.5 Page7
… … … …
0x800F000~0x800F1FF 0.5 Page120 Sector15
0x800F200~0x800F3FF 0.5 Page121
0x800F400~0x800F5FF 0.5 Page122
0x800F600~0x800F7FF 0.5 Page123
0x800F800~0x800F9FF 0.5 Page124
Rev1.3 25 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
The embedded Flash module can be addressed directly, as a common memory space. Any data read
operation accesses the content of the Flash module through dedicated read senses and provides the
requested data.The instruction fetch and the data access are both done through the same AHB bus. Read
accesses can be performed with the following options managed through the Flash access control register
(FLASH_ACR):
Instruction fetch: Prefetch buffer enabled for a faster CPU execution
Latency: number of wait states for a correct read operation (from 0 to 1)
Instruction fetch
The Cortex®-M0 fetches the instruction over the AHB bus. The prefetch block aims at increasing the
efficiency of instruction fetching.
Prefetch buffer
The prefetch buffer is 4 blocks wide where each block consists of 4 byte. The prefetch blocks are
direct-mapped. A block can be completely replaced on a single read to the Flash memory as the size of the
block matches the bandwidth of the Flash memory. The implementation of this prefetch buffer makes a
faster CPU execution possible as the CPU fetches one word at a time with the next word readily available in
the prefetch buffer.
Prefetch controller
The prefetch controller decides to access the Flash memory depending on the available space in the
prefetch buffer. The Controller initiates a read request when there is at least two block free in the prefetch
buffer.After reset, the state of the prefetch buffer is on .Only when SYSCLK is lower than 24 MHz and the
AHB clock not divided , the prefetch buffer can be switched on/off and it is usually switched on/off during
the initialization routine, while the microcontroller is running on the internal 8 MHz RC (HSI) oscillator.
Access latency
In order to maintain the control signals to read the Flash memory, the ratio of the prefetch controller clock
period to the access time of the Flash memory has to be programmed in the Flash access control register
with the LATENCY[2:0] bits. This value gives the number of cycles needed to maintain the control signals of
the Flash memory and correctly read the required data. After reset, the value is zero and only one cycle
without additional wait states is required to access the Flash memory.
Rev1.3 26 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
The FT32F0xxx8 embedded Flash memory can be programmed using in-circuit programming or
in-application programming.
The in-circuit programming (ICP) method is used to update the entire contents of the Flash memory, using
the SWD protocol or the boot loader to load the user application into the microcontroller. ICP offers quick
and efficient design iterations and eliminates unnecessary package handling or socketing of devices.
In contrast to the ICP method, in-application programming (IAP) can use any communication interface
supported by the microcontroller (I/Os, USB, USART, I2C, SPI, etc.) to download programming data into
memory. IAP allows the user to re-program the Flash memory while the application is running. Nevertheless,
part of the application has to have been previously programmed in the Flash memory using ICP.
The program and erase operations can be performed over the whole product voltage range. They are
managed through the following seven Flash registers:
Key register (FLASH_KEYR)
Option byte key register (FLASH_OPTKEYR)
Flash control register (FLASH_CR)
Flash status register (FLASH_SR)
Flash address register (FLASH_AR)
Option byte register (FLASH_OBR)
Write protection register (FLASH_WRPR)
An ongoing Flash memory operation will not block the CPU as long as the CPU does not access the Flash
memory. On the contrary, during a program/erase operation to the Flash memory, any attempt to read the
Flash memory will stall the bus. The read operation will proceed correctly once the program/erase operation
has completed. This means that code or data fetches cannot be made while a program/erase operation is
ongoing.
Unlocking the Flash memory
After reset, the Flash memory is protected against unwanted write or erase operations. The FLASH_CR
register is not accessible in write mode, except for the OBL_LAUNCH bit, used to reload the option bits. An
unlocking sequence should be written to the FLASH_KEYR register to open the access to the FLASH_CR
register. This sequence consists of two write operations:
Write KEY1 = 0x45670123
Write KEY2 = 0xCDEF89AB
Any wrong sequence locks up the FLASH_CR register until the next reset.
In the case of a wrong key sequence, a bus error is detected and a Hard Fault interrupt is generated. This is
done after the first write cycle if KEY1 does not match, or during the second write cycle if KEY1 has been
correctly written but KEY2 does not match. The FLASH_CR register can be locked again by user software by
writing the LOCK bit in the FLASH_CR register to 1.
Main Flash memory programming
The main Flash memory can be programmed 16 bits at a time. The program operation is started when the
CPU writes a half-word into a main Flash memory address with the PG bit of the FLASH_CR register set.
Any attempt to write data that are not half-word long will result in a bus error generating a Hard Fault
interrupt.
Rev1.3 27 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
Perform unlock
LOCK=1? Y
sequency
Write MAPG to 1
Perform word
write at the
desired address
Y
BUSY=1?
Check the
programmed value
Rev1.3 28 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
Perform unlock
LOCK=1? Y
sequence
Write into
FLASH_AR an
address within the
page to erase
BUSY=1?
Rev1.3 29 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
Perform unlock
LOCK=1? Y
sequence
BUSY=1?
Rev1.3 30 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
state of the read protection option byte protects the Flash memory.
Erase procedure
The option byte erase sequence is as follows:
1. Check that no Flash memory operation is ongoing by reading the BSY bit in the FLASH_SR register
2. Unlock the OPTWRE bit in the FLASH_CR register
3. Set the OPTER bit in the FLASH_CR register
4. Set the STRT bit in the FLASH_CR register
5. Wait for the BSY bit to be reset
6. Read the erased option byte and verify
The user area of the Flash memory can be protected against read by untrusted code. The pages of the
Flash memory can also be protected against unwanted write due to loss of program counter contexts. The
write-protection granularity is one sector (eight pages).
1)Read protection
The read protection is activated by setting the RDP option byte and then, by applying a system reset to
reload the new RDP option byte.
Note: If the read protection is set while the debugger is still connected through JTAG/SWD, apply a POR
(power-on reset) instead of a system reset.
There are three levels of read protection from no protection (level 0) to maximum protection or no debug
(level 2). The Flash memory is protected when the RDP option byte and its complement contain the pair of
values.
Table 3-2 Flash memory read protection status
RDP nRDP Read protection level
Level 0 (FMD production
0xAA 0x55
configuration)
Any value (not necessarily
Any value except 0xAA or 0xCC complementary) except 0x55 and Level 1
0x33
0xCC 0x33 Level 2
The System memory area is read accessible whatever the protection level. It is never accessible for
program/erase operation
Level 0: no protection
Read, program and erase operations into the main Flash memory area are possible. The option byte are as
well accessible by all operations.
Level 1: read protection
This is the default protection level when RDP option byte is erased. It is defined as well when RDP value is
0xFF, or even if the complement is not correct.
User mode: Code executing in user mode can access main Flash memory and option byte with all
operations.
Debug, boot RAM and boot loader modes: In debug mode or when code is running from boot
Rev1.3 31 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
Table 3-3 Access status versus protection level and execution modes
Debug / Boot From RAM / Boot
Protection User execution
Flash From System memory
level
Read Write Erase Read Write Erase
4
1 Yes Yes Yes No No No
Main area 1 1 1
2 Yes Yes Yes N/A N/A N/A
2
1 Yes No No Yes No No
System area 1 1 1
2 Yes No No N/A N/A N/A
3 4 3,4
1 Yes Yes Yes Yes Yes Yes
UserOption 5 1 1 1
2 Yes Yes No N/A N/A N/A
Note:
1. When the protection level 2 is active, the Debug port, the boot from RAM and the boot from System
memory are disabled.
2. The system memory is only read-accessible, whatever the protection level (0, 1 or 2) and execution
mode.
3. The main Flash memory is erased when the RDP option byte is changed from level 1 to level 0
(0xAA).
4. When the RDP level 1 is active, the embedded boot loader don’t allow to read or write the Option byte,
except to remove the RDP protection (move from level 1 to level 0).
5. All option byte can be programmed, except the RDP byte.
2)Write protection
The write protection is implemented with a granularity of one sector. It is activated by configuring the WRPx
option byte, and then by reloading them by setting the OBL_LAUNCH bit in the FLASH_CR register. If a
program or an erase operation is performed on a protected sector, the Flash memory returns a WRPRTERR
protection error flag in the Flash memory Status Register (FLASH_SR).
Write unprotection
To disable the write protection, two application cases are provided:
Case 1: Read protection disabled after the write unprotection:
- Erase the entire option byte area by using the OPTER bit in the Flash memory control register
(FLASH_CR).
- Program the code 0xA5 in the RDP byte to unprotect the memory. This operation forces a Mass
Erase of the main Flash memory.
Rev1.3 32 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
- Set the OBL_LAUNCH bit in the Flash control register (FLASH_CR) to reload the option byte (and
the new WRP[1:0] byte), and to disable the write protection.
Case 2: Read protection maintained active after the write unprotection, useful for in- application
programming with a user boot loader:
- Erase the entire option byte area by using the OPTER bit in the Flash memory control register
(FLASH_CR).
- Set the OBL_LAUNCH bit in the Flash control register (FLASH_CR) to reload the option byte
(and the new WRP[1:0] byte), and to disable the write protection.
Option byte write protection
The option byte are always read-accessible and write-protected by default. To gain write access
(Program/Erase) to the option byte, a sequence of keys (same as for lock) has to be written into the
OPTKEYR. A correct sequence of keys gives write access to the option byte and this is indicated by
OPTWRE in the FLASH_CR register being set. Write access can be disabled by resetting the bit through
software.
Rev1.3 33 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
9
8
7
6
5
4
3
LATENCY 2
1
0
PRFTBS
PRFTBE
[3:0]
FLASH_ACR
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
0x00
Reset x x x x x x x x x x x x x x x x x x x x x x x x x x 0 1 0 0 0 1
FLASH_KEYR FKEY[31:0]
0x04
Reset 0 0 0 0 0 0 0 0 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_
OPTKEY[31:0]
0x08 OPTKEYR
Reset 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
WRPRTERR
PGERR
BUSY
EOP
FLASH_SR
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
0x0C
Reset x x x x x x x x x x x x x x x x x x x x x x x x x x 0 0 x 0 x 0
OBL_LAUNCH
OPTWRE
OPTPG
OPTER
EOPIE
ERRIE
LOCK
STRT
MER
PER
PG
FLASH_CR
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
0x10
Reset x x x x x x x x x x x x x x x x x x 0 0 x 0 0 x 1 0 0 0 x 0 0 0
FLASH_AR FAR[31:0]
0x14
Reset 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
VDDA_MONITOR
nRST_STDBY
nRST_STOP
RDPRT[1:0]
WDG_SW
OPTERR
nBOOT1
_
_
_
_
_
0x1C
Reset 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_WRPR WRP[31:0]
0x20
Reset 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
Rev1.3 34 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
3.3.1. FLASH_ACR
Address Offset:0x00
Reset value:0x0000 0010
Rev1.3 35 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
3.3.2. FLASH_KEYR
Address Offset:0x04
Reset value:0x0000 0000
3.3.3. FLASH_OPTKEYR
Address Offset:0x08
Reset value:0x0000 0000
Rev1.3 36 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
3.3.4. FLASH_SR
Address Offset:0x0C
Reset value:0x0000 0000
Rev1.3 37 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
3.3.5. FLASH_CR
Address Offset:0x10
Reset value:0x0000 0080
Rev1.3 38 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
3.3.6. FLASH_AR
Address Offset:0x14
Reset value:0x0000 0000
3.3.7. FLASH_OBR
Address Offset:0x1C
Reset value:0xXXXX XXXX,read only
Rev1.3 39 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
Rev1.3 40 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
3.3.8. FLASH_WRPR
Address Offset:0x20
Reset value:0xXXXX XXXX
Rev1.3 41 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
4. Option Byte
There are up to 8 option byte. They are configured by the end user depending on the application
requirements. As a configuration example, the watchdog may be selected in hardware or software mode.
The organization of these byte inside the information block is as shown in Table 4-1.
The option byte can be read from the memory locations listed in Table 4-1 or from the Option byte register
(FLASH_OBR).
Note: The new programmed option byte (user, read/write protection) are loaded after a system reset.
Table 4-1 Option byte organization
Address [31:24] [23:16] [15:8] [7:0]
0x1FFFF800 nUSER USER nRDP RDP
0x1FFFF804 nDATA1 DATA1 nDATA0 DATA0
0x1FFFF808 nWRP1 WRP1 nWRP0 WRP0
0x1FFFF80C nWRP3 WRP3 nWRP2 WRP2
0x1FFFF810 nUDMY1 UDMY1 nUDMY0 UDMY0
On every system reset, the option byte loader (OBL) reads the information block and stores the data into the
Option byte register (FLASH_OBR) and the Write protection register (FLASH_WRPR). Each option byte
also has its complement in the information block. During option loading, by verifying the option bit and its
complement, it is possible to check that the loading has correctly taken place. If this is not the case, an
option byte error (OPTERR) is generated. When a comparison error occurs, the corresponding option byte
is forced to 0xFF. The comparator is disabled when the option byte and its complement are both equal to
0xFF (Electrical Erase state).
Rev1.3 42 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
Offset Register
9
8
7
6
5
4
3
2
1
0
USER
VDDA_MONITOR
nRST_STDBY
nRST_STOP
WDG_SW
nBOOT1
User and
nUSER nRDP RDP
read protection
_
_
_
0x00
production
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0
value
Address Offset:0x00
Reset value:0x00FF 55AA
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 nUSER
Type RW RW RW RW RW RW RW RW
23:16 — — VDDA_ nBOOT1 — nRST_ nRST_ WDG_
MONITOR STDBY STOP SW
Type RW RW RW RW RW RW RW RW
15:8 nRDP
Type RW RW RW RW RW RW RW RW
7:0 RDP
Type RW RW RW RW RW RW RW RW
Rev1.3 43 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
Address Offset:0x04
Reset value:0x00FF 00FF
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 nData1
Type RW RW RW RW RW RW RW RW
23:16 Data1
Type RW RW RW RW RW RW RW RW
15:8 nData0
Type RW RW RW RW RW RW RW RW
7:0 Data0
Type RW RW RW RW RW RW RW RW
Rev1.3 44 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
Address Offset:0x08
Reset value:0x00FF 00FF
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 nWRP1
Type RW RW RW RW RW RW RW RW
23:16 WRP1
Type RW RW RW RW RW RW RW RW
15:8 nWRP0
Type RW RW RW RW RW RW RW RW
7:0 WRP0
Type RW RW RW RW RW RW RW RW
Address Offset:0x0C
Reset value:0x00FF 00FF
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 nWRP3
Type RW RW RW RW RW RW RW RW
23:16 WRP3
Type RW RW RW RW RW RW RW RW
15:8 nWRP2
Type RW RW RW RW RW RW RW RW
7:0 WRP2
Type RW RW RW RW RW RW RW RW
Rev1.3 45 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
AHB Bus
CRC computation
Rev1.3 46 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
For the other registers only 32-bit access is allowed. The duration of the computation depends on data width:
4 AHB clock cycles for 32-bit, 2 AHB clock cycles for 16-bit, 1 AHB clock cycles for 8-bit.
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.
An input buffer allows to immediately write a second data without waiting for any wait states due to the
previous CRC calculation.
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.
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.
Rev1.3 47 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
9
8
7
6
5
4
3
2
1
0
CRC_DR DR[31:0]
0x00
Reset 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
IDR[7:0]
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
CRC_IDR
0x04
Reset x x x x x x x x x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0
REV_IN[1:0]
RESET
REV_OUT
CRC_CR
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x08
Reset x x x x x x x x x x x x x x x x x x x x x x x x 0 0 0x x x x 0
CRC_INIT CRC_INIT[31:0]
0x10
Reset 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
Rev1.3 48 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
5.3.1. CRC_DR
5.3.2. CRC_IDR
5.3.3. CRC_CR
Rev1.3 49 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
5.3.4. CRC_INIT
Rev1.3 50 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
FT32F0xxx8 devices require a 5.0 V operating supply voltage, and embeds two voltage regulators in order
to supply the internal 1.6 V, 1.5V digital power ___domain.
A/D
VSSA
converter
Reset block
VDDA PLL
1.6V Domain
VSS I/O
IWDG Core
PMU Memories
VDD Voltage regulator 1.6V
Digital
Peripherals
1.5V
RTC
RTC Domain
FT32F0xxx8 embeds two voltage regulators, one of them is 1.6V, supply for core, memories and digital
peripherals, the other is 1.5V, supply for RTC.
The 1.6V voltage regulator is always enabled after Reset. It works in three different modes depending on the
application modes.
Rev1.3 51 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
In Run mode, the regulator supplies full power to the 1.6 V ___domain (core, memories and digital
peripherals).
In Stop mode the regulator supplies low-power to the 1.6 V ___domain, preserving contents of registers
and SRAM
In Standby Mode, the regulator is powered off. The contents of the registers and SRAM are lost except
for the Standby circuitry.
The device has an integrated power-on reset (POR) and power-down reset (PDR) circuits which are always
active and ensure proper operation above a threshold of VPOR.
The POR monitors only the VDD supply voltage. During the startup phase VDDA must arrive first and be equal
to VDD.
The PDR monitors both the VDD and VDDA supply voltages. However, the VDDA power supply supervisor can
be disabled to reduce the power consumption if the application is designed to make sure that VDDA is higher
than or equal to VDD.
VDD/VDDA
VPOR
40mV
hysteresis VPDR
Temporization
tRSTTEMPO
Reset
Rev1.3 52 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
You can use the PVD to monitor the VDD power supply by comparing it to a threshold selected by the
PLS[3:0] bits. The PVD is enabled by setting the PVDE bit.
A PVDO flag is available, to indicate if VDD is higher or lower than the PVD threshold. This event is internally
connected to the EXTI line16 and can generate an interrupt if enabled through the EXTI registers.
VDD
VPVD threshold
100mV
hysteresis
PVD output
By default, the microcontroller is in Run mode after a system or a power Reset. Several low- power modes
are available to save power when the CPU does not need to be kept running, for example when waiting for
an external event. It is up to the user to select the mode that gives the best compromise between low-power
consumption, short startup time and available wakeup sources.
Rev1.3 53 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
In addition, the power consumption in Run mode can be reduce by one of the following means:
Slowing down the system clocks
Gating the clocks to the APB and AHB peripherals when they are unused.
Table 6-1 Low-power mode summary
Effect on 1.6V Effect on VDD
Mode Voltage
Entry wakeup ___domain ___domain
name regulatior
clocks clocks
WFI Any interrupt CPU clock
OFF
no effect on
Sleep 1 None ON
WFE Wakeup event other clocks
or analog
clock sources
ON or in low-
Any EXTI line
PDDS and LPDS power mode
(configured in the
Stop bits+SLEEPDEEP+WFI (depends on
EXTI registers)
or WFE PWR_CR
All 1.6V HSI、HSE、
register)
___domain HSI48 and
WKUP pin rising 2
clocks OFF PLL OFF
PDDS and edge, RTC alarm,
Standby SLEEPDEEP bit +WFI external reset in OFF
or WFE NRST pin, IWDG
reset
Note 1: Executing WFE single instruction may not be able to enter low power mode, It is recommended to
execute SEV, WFE, and WFE consecutive three instructions to enter low power mode. Executing WFI single
instruction can enter low power mode.
Note 2: When the HSI14ON=1 which HSI14 clock enable bit in the RCC_CR2 register , the HSI14 clock
keeps running.
In Run mode the speed of the system clocks can be reduced by programming the prescaler registers
(RCC_CFGR). These prescalers can also be used to slow down peripherals before entering Sleep mode.
In Run mode, the AHB clock (HCLK) and the APB clock (PCLK) for individual peripherals and memories can
be stopped at any time to reduce power consumption.
To further reduce power consumption in Sleep mode the peripheral clocks can be disabled prior to executing
the WFI or WFE instructions.
Rev1.3 54 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
In the Sleep mode, all I/O pins keep the same state as in the Run mode.
Rev1.3 55 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
The Stop mode is based on the ARM® Cortex®-M0 deep sleep mode combined with peripheral clock gating.
The voltage regulator can be configured either in normal or low- power mode. In Stop mode, all clocks in the
1.6 V ___domain are stopped, the PLL, the HSI and the HSE oscillators are disabled. SRAM and register
contents are preserved.
In the Stop mode, all I/O pins keep the same state as in the Run mode.
If Flash memory programming is ongoing, the Stop mode entry is delayed until the memory access is
finished.
If an access to the APB ___domain is ongoing, The Stop mode entry is delayed until the APB access is finished.
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.
Real-time clock (RTC): this is configured by the RCC_BDCR register
Internal RC oscillator (LSI): this is configured by the LSION bit in the RCC_CSR register
External 32.768 kHz oscillator (LSE): this is configured by the LSEON bit in the RCC_BDCR register
The ADC can also consume power during Stop mode, unless it is disabled before entering this mode.
When the voltage regulator operates in low-power mode, an additional startup delay is incurred when
waking up from Stop mode. By keeping the internal regulator ON during Stop mode, the consumption is
higher although the startup time is reduced.
Rev1.3 56 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
The Standby mode allows to achieve the lowest power consumption. It is based on the ARM ® Cortex®-M0
deepsleep mode, with the voltage regulator disabled. The 1.6 V ___domain is consequently powered off. The
PLL, the HSI oscillator and the HSE oscillator are also switched off. SRAM and register contents are lost
except for registers in the Standby circuitry and 1.5V voltage regulator.
RTC normally work in standby mode if 1.5V voltage regulator enabled before entering the standby mode.
The power consumption can be reduced if 1.5V voltage regulator disabled before entering the standby mode
Entering Standby mode
In Standby 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.
Real-time clock (RTC): this is configured by the RCC_BDC Rregister
Internal RC oscillator (LSI): this is configured by the LSION bit in the RCC_CSR register
External 32.768 kHz oscillator (LSE): this is configured by the LSEON bit in the RCC_BDCR register
The microcontroller exits the Standby mode when an external reset (NRST pin), an IWDG reset, a rising
edge on one of the enabled WKUPx pins or an RTC event occurs. All registers are reset after wakeup from
Standby except for PWR_CSR register.
After waking up from Standby mode, program execution restarts in the same way as after a Reset. The SBF
Rev1.3 57 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
status flag in the PWR_CSR register indicates that the MCU was in Standby mode.
Table 6-5 Standby mode
Standby mode Description
Mode entry WFI (Wait for Interrupt) or WFE (Wait for Event) while:
SLEEPDEEP=1
PDDS=1
Clear WUF bit
Mode exit WKUP pin rising edge
RTC alarm event’s rising edge
External Reset in NRST pin,
IWDG Reset.
Wakeup latency Reset phase
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 ARM ® Cortex®-M0 core is no longer
clocked.However, by setting some configuration bits in the DBGMCU_CR register, the software can be
debugged even when using the low-power modes extensively.
The RTC can be used to wakeup the MCU from low-power mode by means of the RTC alarm. For this
purpose, two of the three alternative RTC clock sources can be selected by programming the RTCSEL[1:0]
bits in the RCC_BDCR register
Low-power 32.768 kHz external crystal oscillator, this clock source provides a precise time base with
very low-power consumption
Low-power internal RC Oscillator: This clock source has the advantage of saving the cost of the 32.768
kHz crystal. This internal RC Oscillator is designed to add minimum power consumption.
To wakeup from Stop mode with an RTC alarm event, it is necessary to:
Configure the EXTI Line 17 to be sensitive to rising edge
Configure the RTC to generate the RTC alarm
To wakeup from Standby mode, there is no need to configure the EXTI Line 17.
Rev1.3 58 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
offset Register
PLS[3] 9
CWUF 2
0
PVDE
PDDS
CSBF
LDPS
DBP
[2:0]
PLS
PWR_CR
-
-
0x00
Reset x x x x x x x x x x x x x x x x x x x x x x 1 0 0 0 0 0 0 0 0 0
EWUP2
EWUP1
PVDO
WUF
SBF
PWR_CSR
-
-
0x04
Reset x x x x x x x x x x x x x x x x x x x x x x 0 0 x x x x x 0 0 0
6.3.1. PWR_CR
Address Offset:0x00
Reset value:0x0000 0200
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
23:16 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
15:8 — PLS[3] DBP
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RW RW
7:0 PLS[2:0] PVDE CSBF CWUF PDDS LDPS
Type RW RW RW RW RW-0 RW-0 RW RW
Rev1.3 59 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
4’b1011:2.88V
4’b1100:2.98V
4’b1101:3.08V
4’b1110:3.18V
4’b1111:3.28V
4 PVDE 0:PVD disabled
1:PVD enabled
3 CSBF Clear standby flag,This bit is always read as 0.
0: No effect
1:Clear the SBF Standby Flag (write)
2 CWUF Clear wakeup flag,This bit is always read as 0
0:No effect
1:Clear the WUF Wakeup Flag
1 PDDS Power down deepsleep.
This bit is set and cleared by software.
0: Enter Stop mode when the CPU enters Deepsleep. The regulator status
depends on the LPDS bit.
1: Enter Standby mode when the CPU enters Deepsleep.
0 LDPS Low-power deepsleep This bit is set and cleared by software
0:Voltage regulator on during Stop mode
1:Voltage regulator in low-power mode during Stop mode
6.3.2. PWR_CSR
Address Offset:0x04
Reset value:0x0000 0000
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
23:16 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
15:8 — EWUP2 EWUP1
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RW RW
7:0 — PVDO SBF WUF
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO RO RO
Rev1.3 60 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
pull down configuration (rising edge on WKUPx pin wakes-up the system
from Standby mode).
7:3 NA Resrved
2 PVDO PVD output
This bit is set/cleared by hardware, and effective when PVDE =1
0:VDD > PVD threshold voltage
1:VDD < PVD threshold voltage
1 SBF Standby flag
This bit is set by hardware when the device enters Standby mode and it is
cleared only by a POR/PDR or by setting the CSBF bit
0: Device has not been in Standby mode
1: Device has been in Standby mode
0 WUF Wakeup flag This bit is set by hardware to indicate that the device received
a wakeup event. It is cleared by a system reset
0: No wakeup event occurred
1: A wakeup event was received from one of the enabled WKUPx pins or
from the RTC alarm.
Note: An additional wakeup event is detected if one WKUPx pin is enabled
(by setting the EWUPx bit) when its pin level is already high.
Rev1.3 61 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
7.1. Reset
There are three types of reset, defined as system reset, power reset and RTC ___domain reset.
VDD
Rpu
NRST
Filter System reset
WWDG reset
IWDG reset
Pulse
Power reset
generator Software reset
(min 20us) Low-power management reset
Option byte loader reset
Rev1.3 62 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
routine vector is fixed at address 0x0000_0004 in the memory map.The system reset signal provided to the
device is output on the NRST pin. The pulse generator guarantees a minimum reset pulse duration of 20 µs
for each internal reset source. In case of an external reset, the reset pulse is generated while the NRST pin
is asserted low.
NRST pin can be configured to debounce and it is only effective for external reset source., the debounce
time is configure by the RCC_RSTCR register.
Software reset:
The SYSRESETREQ bit in ARM® Cortex®-M0 Application Interrupt and Reset Control Register must be set
to force a software reset on the device.
An RTC ___domain reset only affects the RTC and the RCC_BDCR register. It is generated when one of the
following events occurs.
1. Software reset, triggered by setting the BDRST bit in the RCC_BDCR register
2. POR/PDR reset
7.2. Clocks
Various clock sources can be used to drive the system clock (SYSCLK):
1. HSI 8 MHz RC oscillator clock(HSI)
2. HSI 14 MHz RC oscillator clock(HSI14)
3. HSI 48 MHz RC oscillator clock(HSI48)
4. PLL clock
5. HSE oscillator clock(HSE)
The FT32F0xxx8 device embeds a 40kHz low speed internal crystal for IWDG and RTC. 32.768 kHz low
speed external crystal (LSE crystal) which optionally drives the RTC and USART1, 14 MHz high speed
internal RC (HSI14) dedicated for ADC.
Rev1.3 63 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
All the peripheral clocks are derived from their bus clock:
1. The Flash memory programming interface clock is HCLK.
2. The ADC clock which is derived (selected by software) from the dedicated HSI14 clock or APB clock
(PCLK) divided by 2 or 4
3. The USART1 clock is derived (selected by software) from system clock, HSI, LSE or APB clock(PCLK):
4. The I2C1 clock which is derived (selected by software) from system clock or HSI.
5. The USB clock which is derived (selected by software) from HSI48 and PLL
6. The RTC clock which is derived from the LSE, LSI or from the HSE clock divided by 32.
7. The IWDG clock which is always the LSI clock.
8. The timer clock frequencies are automatically fixed by hardware, if the APB prescaler is 1, the timer
clock frequencies are set to the same frequency as that of the APB ___domain, otherwise, they are set to
twice (x2) the frequency of the APB ___domain.
Rev1.3 64 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
CRS
to ADC
asynchronous
HSI48 RC HSI48 clock input
48MHz
to I2C
HSI RC HSI
8MHz SYSCLK
to AHB bus,
HSI14 RC HSI14
core,Flash,DMA
14MHz /8 to system timer
SW FCLK Cortex free
PLLSRC PLLMUL running clock
PLL AHB APB
/1,2,3, *2,*3, prescaler prescaler PCLK to APB
...16 peripherals
…*16 PLLCLK /1,2,...512 /1,2,4,8,16
PREDIV
HSE
if(APB1
CSS prescaler to TIM1,3,6,
14,15,16,17
=1)*1 else *2
to USART2
4~32MHz PCLK
HSE OSC SYSCLK to USART1
HSI
RTCSEL[1:0] LSE
/32
LSE OSC to RTC
32.768kHz
HSI48
to USB
PLLCLK
LSI RC
to IWDG
40kHz PLLNODIV
PLLCLK /1,2 MCO
HSI MCOPRE to TMI14
HSI48
HSI14 /1,2,4,
HSE … 128 Main clock output
SYSCLK
LSI
LSE
The high speed external clock signal (HSE) can be generated from two possible clock sources:
1. HSE external crystal/ceramic resonator
2. HSE user external clock
The external clock signal has to drive the OSC_IN pin while OSC_OUT pin can be used a GPIO. OSC_IN
Rev1.3 65 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
and OSC_OUT pin need to be used when a crystal is connected to the device and the load capacitor have to
connect to them. The crystal frequency range can be 4 to 32 MHz
The HSE Crystal can be switched on and off using the HSEON bit in the RCC_CR register.
The HSERDY flag in the RCC_CR register indicates if the HSE oscillator is stable or not. At startup, the
clock is not released until this bit is set by hardware. An interrupt can be generated if enabled in the rcc_CIR
register.
Note: Once the oscillator is started, it needs another 6 HSE clock pulses to complete a switching OFF
sequence. If for any reason the oscillations are no more present on the OSC_IN pin, the oscillator cannot be
switched OFF, locking the OSC pins from any other use and introducing unwanted power consumption. To
avoid such situation, it is strongly recommended to always enable the Clock Security System (CSS) which is
able to switch OFF the oscillator even in this case.
In external clock source mode. It can have a frequency of up to 32 MHz. You select this mode by setting the
HSEBYP and HSEON bits in the
The HSI clock signal is generated from an internal 8 MHz RC oscillator and can be used directly as a system
clock or for PLL input.
The HSI RC oscillator has the advantage of providing a clock source at low cost (no external components). It
also has a faster startup time than the HSE crystal oscillator however, even with calibration the frequency is
less accurate than an external crystal oscillator or ceramic resonator.
RC oscillator frequencies can vary from one chip to another due to manufacturing process variations, this is
why each device is factory calibrated by FMD After reset, the factory calibration value is loaded in the
HSICAL[7:0] bits in the RCC_CR register.If the application is subject to voltage or temperature variations this
may affect the RC oscillator speed. You can trim the HSI frequency in the application using the HSITRIM[4:0]
bits in the RCC_CR register.
The HSIRDY flag in the RCC_CR register indicates if the HSI RC is stable or not. At startup, the HSI RC
output clock is not released until this bit is set by hardware.
The HSI RC can be switched on and off using the HSION bit in the RCC_CR register.
The internal PLL can be used to multiply the HSI, a divided HSI48 or the HSE output clock frequency. The
PLL configuration must be done before enabling the PLL. Once the PLL is enabled, these parameters
Rev1.3 66 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
cannot be changed.
The PLL output frequency must be set in the range 16-48 MHz and the minimum frequency is 0.8MHz
The LSE crystal is a 32.768 kHz Low Speed External crystal or ceramic resonator. It has the advantage of
providing a low-power but highly accurate clock source to the real-time clock peripheral (RTC) for
clock/calendar or other timing functions.
The LSE crystal is switched on and off using the LSEON bit in RCC_BDCR register. The crystal oscillator
driving strength can be changed at runtime using the LSEDRV[1:0] bits in the RCC_BDCR register.
The LSERDY flag in the RCC_BDCR register indicates whether the LSE crystal is stable or not. At startup,
the LSE crystal output clock signal is not released until this bit is set by hardware.
To switch ON the LSE oscillator, 4096 LSE clock pulses need to be seen by an internal stabilization counter
after the LSEON bit is set. Even in the case that no crystal or resonator is connected to the device, excessive
external noise on the OSC32_IN pin may still lead the oscillator to start. Once the oscillator is started, it
needs another 6 LSE clock pulses to complete a switching OFF sequence. If for any reason the oscillations
are no more present on the OSC_IN pin, the oscillator cannot be switched OFF, locking the OSC32 pins from
any other use and introducing unwanted power consumption. The only way to recover such situation is to
perform the RTC ___domain reset by software.
In this mode, an external clock source must be provided. It can have a frequency of up to 1 MHz. You select
this mode by setting the LSEBYP and LSEON bits in the RCC_BDCR register. The external clock signal
(square, sinus or triangle) with ~50% duty cycle has to drive the OSC32_IN pin while the OSC32_OUT pin
can be used as GPIO.
Rev1.3 67 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
The LSI RC acts as a low-power clock source that can be kept running in Stop and Standby mode for the
independent watchdog (IWDG) and RTC. The clock frequency is around 40 kHz.
The LSI RC can be switched on and off using the LSION bit in the RCC_CSR. The LSIRDY flag in the
RCC_CSR register indicates if the LSI oscillator is stable or not. At startup, the clock is not released until this
bit is set by hardware.
The HSI14 clock signal is generated from an internal 14 MHz RC oscillator and can be used directly as a
system clock or for ADC clock or output from MCO pin.
The HSI14 RC can be switched on and off using the HSI14ON bit in the RCC_CR2 register. The HSIRDY
flag in the RCC_CR2 register indicates if the HSI14 RC is stable or not. An interrupt can be generated if
enabled in the RCC_CIR.
HSI14 clock can run in the STOP mode.
The HSI48 clock signal is generated from an internal 48 MHz RC oscillator and can be used directly as a
system clock or for USB clock or for PLL input or output from MCO pin.
The internal 48MHz RC oscillator is mainly dedicated to provide a high precision clock to the USB peripheral
by means of a special Clock Recovery System (CRS) circuitry, it will be disabled as soon as the system
enters in Stop or Standby mode. The HSI48 RC can be switched on and off using the HSI48ON bit in the
RCC_CR2 register. The HSIRDY flag in the RCC_CR2 register indicates if the HSI48 RC is stable or not. An
interrupt can be generated if enabled in the RCC_CIR..
Rev1.3 68 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
HSI14 calibrated range is 8’h00~8’hFF, 8’h00~8’h7F is calibrated to small value. 8’h81~8’hFF is calibrate to
big value. 9’h100 is not calibrated.
Various clock sources can be used to drive the system clock (SYSCLK):
1. HSI
2. HSI14
3. HSE
4. HSI48
5. PLL
After a system reset, the HSI oscillator is selected as system clock. When a clock source is used directly as
a system clock, it is not possible to stop it.A switch from one clock source to another occurs only if the target
clock source is ready. SWS[1:0] bits in the RCC_CFGR register and SWS[2] bit in the RCC_CFGR4 register
indicate which clock is currently used as a system clock.
Clock Security System can be activated by software. In this case, the clock detector is enabled after the
HSE oscillator startup delay, and disabled when this oscillator is stopped.
If a failure is detected on the HSE clock, the HSE oscillator is automatically disabled, a clock failure event is
sent to the break input of the advanced-control timers (TIM1) and general- purpose timers (TIM15, TIM16
and TIM17) and an interrupt is generated to inform the software about the failure), allowing the MCU to
perform rescue operations. In the NMI ISR user must clear the CSS interrupt by setting the CSSC bit in the
RCC_CIR
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 to the HSI oscillator and the disabling of the HSE oscillator. If the HSE clock (divided or not) is the clock
Rev1.3 69 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
entry of the PLL used as system clock when the failure occurs, the PLL is disabled too.
The RTC clock source can be either the HSE/32, LSE or LSI clocks. This is selected by programming the
RTCSEL[1:0] bits in the RCC_BDCR register. This selection cannot be modified without resetting the RTC
___domain. The system must be always configured in a way that the PCLK frequency is greater then or equal to
the RTC clock.
If the Independent watchdog (IWDG) is started by either hardware option or software access, the LSI
oscillator is forced ON and cannot be disabled.
The microcontroller clock output (MCO) capability allows the clock to be output onto the external MCO pin.
One of the following clock signals can be selected as the MCO clock:
1. HSI
2. HSI14
3. SYSCLK
4. HSE
5. PLL clock divided by 2 or direct
6. LSE
7. LSI
8. HSI48
The selection is controlled by the MCO[3:0] bits of the RCC_CFGR. The MCO frequency can be reduced by
a configurable binary divider, controlled by the MCOPRE[2:0] bits.
It is possible to indirectly measure the frequency of all on-board clock sources by mean of the TIM14
channel 1 input capture. The input capture channel of the Timer 14 can be a GPIO line or an internal clock of
the MCU. This selection is performed through the TI1_RMP [1:0] bits in the TIM14_OR register.
Sleep mode stops the CPU clock. The memory interface clocks (Flash and SRAM interfaces) can be stopped
by software during sleep mode. The AHB to APB bridge clocks are disabled by hardware during Sleep mode
when all the clocks of the peripherals connected to them are disabled.
Rev1.3 70 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
Stop mode stops all the clocks in the core supply ___domain and disables the PLL and the HSI, HSI48 and HSE
oscillators. Standby mode stops all the clocks in the core supply ___domain and disables the PLL and the HSI,
HSI48, HSI14 and HSE oscillators. The Stop and Standby mode can be overridden for debugging by setting
the DBG_STOP or DBG_STANDBY bits in the DBGMCU_CR register.
When waking up from deepsleep after an interrupt (Stop mode) or reset (Standby mode), the HSI oscillator
is selected as system clock.
If a Flash programming operation is on going, deepsleep mode entry is delayed until the Flash interface
access is finished. If an access to the APB ___domain is ongoing, deepsleep mode entry is delayed until the
APB access is finished.
Rev1.3 71 2024-03-22
0x34
0x30
0x28
0x24
0x20
0x18
0x14
0x10
0x08
0x04
0x00
0x2C
0x1C
0x0C
offset
Rev1.3
Reset
Reset
Reset
Reset
Reset
Reset
Reset
Reset
Reset
Reset
Reset
Reset
Reset
Reset
Register
RCC_CR
RCC_CIR
RCC_CR2
RCC_CSR
RCC_BDCR
RCC_CFGR
RCC_CFGR3
RCC_CFGR2
RCC_AHBENR
RCC_APB2RSTR
RCC_APB1ENR
RCC_APB2ENR
RCC_AHBRSTR
RCC_APB1RSTR
x
x
x
x
x
x
x
x
x
x
x
x
31
0
- - - LPWRSTF - - - - - - - PLL NODIV -
Fremont Micro Devices
x
x
x
x
x
x
x
x
x
x
x
x
0
- - - WWDGRSTF - - - - - - - - 30
x
x
x
x
x
x
x
x
x
x
x
x
0
- - - IWDGRSTF - - - - - - - - 29
x
x
x
x
x
x
x
x
x
x
0
0
0
- - - SFTRSTF - PWREN - - PWRRST - - MCOPRE[2:0] - 28
x
x
x
x
x
x
x
x
x
x
27
0
0
0
- - - PORRSTF - CRSEN - - CRSRST - - -
x
x
x
x
x
x
x
x
x
x
x
x
26
0
- - - PINRSTF - - - - - - - -
HSI48CAL[8:0]
7.3. RCC register map
x
x
x
x
x
x
x
x
x
x
x
25
0
0
- - - OBLRSTF - - - - - - - PLLRDY
MCO[3:0]
x
x
x
x
x
x
x
x
x
x
24
0
0
0
x
x
x
x
x
x
x
x
x
x
23
0
0
0
- - - V18PWRRSTF - USBEN - - USBRST - CSSC - -
x
x
x
x
x
x
22
0
0
0
0
0
0
0
- - - IOPFRST - - I2C2EN DBGMCUEN IOPFEN I2C2RST DBGMCURST HSI48RDYC - -
x
x
x
x
x
x
x
x
21
0
0
0
0
0
x
x
x
x
x
x
x
x
x
20
0
0
0
0
x
x
x
x
x
x
x
x
19
0
0
0
0
0
72
PLLMUL[3:0]
x
x
x
x
x
x
0
0
0
0
0
0
0
x
x
x
0
0
0
0
0
0
0
0
0
0
HSI48RDY - - IOPAREF - - USART2EN TIM16EN IOPAEN USART2RST TIM16RST LSERDYC PLLXTPRE HSERDY 17
x
x
x
x
x
x
16
0
0
0
0
0
0
0
x
x
x
x
x
x
x
x
x
x
15
0
0
0
- - - - RTCEN - - - - - - PLLSRC[0]
x
x
x
x
x
14
0
0
0
0
0
0
0
0
x
x
x
x
x
x
x
x
x
x
13
0
0
0
- - - - RTCISO - - - - - HSI4RDYIE -
x
x
x
x
x
x
x
x
x
12
0
0
0
0
x
x
x
x
x
x
x
11
0
0
0
0
0
0
HSI14CAL[7:0]
x
x
x
x
x
x
x
x
x
x
10
0
0
0
- - - - - - - - - - HSIRDYIE
x
x
x
x
x
x
x
0
0
0
0
0
0
[1:0]
PPRE[2:0]
x
x
x
x
x
x
0
0
0
0
0
0
0
RTCSEL
x
x
x
x
x
x
x
x
1
0
0
0
1
USBSW - - - - - - - - - CSSF
x
x
x
x
x
x
x
x
0
0
0
0
0
- - - - - - - CRCEN - - HSI48RDYF
x
x
x
x
x
x
x
x
0
0
0
0
0
- - - - - - - - - HSI14RDYF
HPRE[3:0]
x
x
x
x
x
0
0
1
0
0
0
0
0
[2:0]
HSI14TRIM[4:0]
LSEDRV
x
x
x
x
x
x
x
x
0
0
0
0
0
0
- - - - - - - - HSERDYF
x
x
x
x
x
x
x
x
0
0
0
1
0
0
x
x
x
x
0
0
0
0
0
0
0
0
0
1
PREDIV[3:0]
x
0
0
0
0
0
0
0
0
0
0
0
0
0
1
SW[1:0]
SWS[1:0] SW[1:0]
USART1
FT32F0xxx8 RM
2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
HSEADD
HSEDRVEN
RCC_HSECFG HSEDRV[2:0]
-
0x38
Reset x x x x x x x x x x x x x x x x x x x x x x x x x x x 0 0 0 0 0
SWS[2]
SW[2]
RCC_CFGR4
-
-
0x3C
Reset x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x 0 0
HSICAL[8]
RCC_TRIM
-
-
0x40
Reset 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
7.3.1. RCC_CR
Address Offset:0x00
Reset value:0x0000 XX83
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 — PLLRDY PLLON
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO RW
23:16 — CSSON HSEBYP HSERDY HSEON
Type RO-0 RO-0 RO-0 RO-0 RW RW RO RW
15:8 HSICAL[7:0]
Type RO RO RO RO RO RO RO RO
7:0 HSITRIM[4:0] — HSIRDY HSION
Type RW RW RW RW RW RO-0 RO RW
Rev1.3 73 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
Rev1.3 74 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
7.3.2. RCC_CFGR
Address Offset:0x04
Reset value:0x0000 0000
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 PLLNODIV MCOPRE[2:0] MCO[3:0]
Type RW RW RW RW RW RW RW RW
23:16 — PLLMUL[3:0] PLLXTPR PLLSRC[
E 1]
Type RO-0 RO-0 RW RW RW RW RW RW
15:8 PLLSRC[0] ADCPRE — PPRE[2:0]
Type RW RW RO-0 RO-0 RO-0 RW RW RW
7:0 HPRE[3:0] SWS[1:0] SW[1:0]
Type RW RW RW RW RO RO RW RW
Rev1.3 75 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
Rev1.3 76 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
7.3.3. RCC_CIR
Address Offset:0x08
Reset value:0x0000 0000
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
23:16 CSSC HSI48RDYC HSI14RDYC PLLRDYC HSERDYC HSIRDYC LSERDYC LSIRDYC
Type WO RO-0 WO WO WO WO WO WO
15:8 — HSI48RDYIE HSI4RDYIE PLLRDYIE HSERDYIE HSIRDYIE LSERDYIE LSIRDYIE
Type RO-0 RW RW RW RW RW RW RW
7:0 CSSF — HSI14RDYF PLLRDYF HSERDYF HSIRDYF LSERDYF LSIRDYF
Type RO RO-0 RO RO RO RO RO RO
Rev1.3 77 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
Rev1.3 78 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
Rev1.3 79 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
7.3.4. RCC_APB2RSTR
Address offset:0x0C
Reset value:0x0000 0000
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
23:16 — DBGMCU – TIM17RST TIM16RST TIM15RST
RST
Type RO-0 RW RO-0 RO-0 RO-0 RW RW RW
15:8 — USART1 — SPI1RST TIM1RST — ADCRST —
RST
Type RO-0 RW RO-0 RW RW RO-0 RW RO-0
7:0 — SYSCFG
RST
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RW
Rev1.3 80 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
0:No effect
10 NA Reserved
9 ADCRST ADC reset Set and cleared by software.
1:Reset ADC
0:No effect
8:1 NA Reserved
0 SYSCFGRST SYSCFG reset Set and cleared by software.
1:Reset SYSCFG
0:No effect
7.3.5. RCC_APB1RSTR
Address offset:0x10
Reset value:0x0000 0000
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 LEBRST — PWRRST CRSRST —
Type RW RO-0 RO-0 RW RW RO-0 RO-0 RO-0
23:16 USBRST I2C2RST I2C1RST — USART2RST —
Type RW RW RW RO-0 RO-0 RO-0 RW RO-0
15:8 — SPI2RST — WWDGRST — TIM14RST
Type RO-0 RW RO-0 RO-0 RW RO-0 RO-0 RW
7:0 — TIM6RST — TIM3RST —
Type RO-0 RO-0 RO-0 RW RO-0 RO-0 RW RO-0
Rev1.3 81 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
7.3.6. RCC_AHBENR
Address offset:0x14
Reset value:0x0000 0004
Note: When the peripheral clock is not active, the peripheral register values may not be readable by software
and the returned value is always 0x0.
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 — TSCEN
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RW
23:16 — IOPFEN — IOPDEN IOPCEN IOPBEN IOPAEN —
Type RO-0 RW RO-0 RW RW RW RW RO-0
15:8 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
Rev1.3 82 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
Rev1.3 83 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
7.3.7. RCC_APB2ENR
Address:0x18
Reset value:0x0000 0000
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
23:16 — DBGMCUEN — TIM17EN TIM16EN TIM15EN
Type RO-0 RW RO-0 RO-0 RO-0 RW RW RW
15:8 — USART1EN — SPI1EN TIM1EN — ADCEN —
Type RO-0 RW RO-0 RW RW RO-0 RW RO-0
7:0 — SYSCFGEN
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RW
Rev1.3 84 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
7.3.8. RCC_APB1ENR
Address offset:0x1C
Reset value:0x0000 0000
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 LEBEN — PWREN CRSEN — — —
Type RW RO-0 RO-0 RW RW RO-0 RO-0 RO-0
23:16 USBEN I2C2EN I2C1EN — USART2EN —
Type RW RW RW RO-0 RO-0 RO-0 RW RO-0
15:8 — SPI2EN — WWDGEN — TIM14EN
Type RO-0 RW RO-0 RO-0 RW RO-0 RO-0 RW
7:0 — TIM6EN — TIM3EN —
Type RO-0 RO-0 RO-0 RW RO-0 RO-0 RW RO-0
Rev1.3 85 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
20:18 NA Reserved
17 USART2EN USART2 clock enable
1:USART2 clock enabled
0:USART2 clock disabled
16:15 NA Reserved
14 SPI2EN SPI2 clock enable
1:SPI2 clock enabled
0:SPI2 clock disabled
13:12 NA Reserved
11 WWDGEN WWDG clock enable
1:WWDG clock enabled
0: WWDG clock disabled
10:9 NA Reserved
8 TIM14EN TIM14 clock enable
1:TIM14 clock enable
0:TIM14 clock disabled
7:5 NA Reserved
4 TIM6EN TIM6 clock enable
1:TIM6 clock enable
0:TIM6 clock disabled
3:2 NA Reserved
1 TIM3EN TIM3 clock enable
1:TIM3 clock enable
0:TIM3 clock disabled
0 NA Reserved
7.3.9. RCC_BDCR
Address offset:0x20
Reset value:0x0000 0010
Note: The LSEON, LSEBYP, RTCSEL and RTCEN bits are in the RTC ___domain. As a result, after Reset, these
bits are write-protected and the DBP bit in the PWR_CR register has to be set before these can be modified.
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
23:16 — BDRST
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RW
15:8 RTCEN RTCPD RTCISO — RTCSEL[1:0]
Type RW RW RO RO-0 RO-0 RO-0 RW RW
7:0 — LSEDRV[2:0] LSEBYP LSERDY LSEON
Type RO-0 RO-0 RW RW RW RW RO RW
Rev1.3 86 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
Rev1.3 87 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
7.3.10. RCC_CSR
Address offset:0x24
Reset value:0x0880 0000
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 LPWRRSTF WWDGRSTF IWDGRSTF SFTRSTF PORRSTF PINRSTF OBLRSTF RMVF
Type RO RO RO RO RO RO RO WO
23:16 VDDLRSTF —
Type RO RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
15:8 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
7:0 — LSIRDY LSION
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO RW
Note: Reset by system Reset, except reset flags by power Reset only
Rev1.3 88 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
Rev1.3 89 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
7.3.11. RCC_AHBRSTR
Address offset:0x28
Reset value:0x0000 0000
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 — TSCRST
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RW
23:16 — IOPFRST — IOPDRST IOPCRST IOPBRST IOPARST —
Type RO-0 RW RO-0 RW RW RW RW RO-0
15:8 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
7:0 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
Rev1.3 90 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
7.3.12. RCC_CFGR2
Address offset:0x2C
Reset value:0x0000 0000
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
23:16 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
15:8 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
7:0 — PREDIV[3:0]
Type RO-0 RO-0 RO-0 RO-0 RW RW RW RW
Rev1.3 91 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
7.3.13. RCC_CFGR3
Address offset:0x30
Reset value:0x0000 0000
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
23:16 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
15:8 — ADCSW
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RW
7:0 USBSW — I2C1SW — USART1SW[1:0]
Type RW RO-0 RO-0 RW RO-0 RO-0 RW RW
7.3.14. RCC_CR2
Address offset:0x34
Reset value:0xXXX0 XX80
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 HSI48CAL[8:1]
Type RO RO RO RO RO RO RO RO
23:16 HSI48CAL[0] — HSI48RDY HSI48ON
Type RO RO-0 RO-0 RO-0 RO-0 RO-0 RO RW
Rev1.3 92 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
15:8 HSI14CAL[7:0]
Type RO RO RO RO RO RO RO RO
7:0 HSI14TRIM[4:0] HSI14DIS HSI14RDY HSI14ON
Type RW RW RW RW RW RW RO RW
Rev1.3 93 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
7.3.15. RCC_HSECFG
Address offset:0x38
Reset value:0x0000 0000
Rev1.3 94 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
7.3.16. RCC_CFGR4
Address offset:0x3C
Reset value:0x0000 0000
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
23:16 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
15:8 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
7:0 — SWS[2] SW[2]
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO RW
7.3.17. RCC_TRIM
Address offset:0x40
Reset value:0x0000 000X
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
23:16 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
15:8 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
7:0 — HSICAL[8]
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RW
Rev1.3 95 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
8.1. Introduction
The clock recovery system (CRS) is an advanced digital controller acting on the internal fine-granularity
trimmable RC oscillator HSI48. The CRS provides a powerful means for oscillator output frequency
evaluation, based on comparison with a selectable synchronization signal. It is capable of doing automatic
adjustment of oscillator trimming based on the measured frequency error value, while keeping the possibility
of a manual trimming.
Rev1.3 96 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
CRS_SYNC
GPIO
SYNCSRC SWSYNC
OSC32_IN
SYNC divider
LSE
(/1,/2,/4,…,/128)
OSC32_OUT SYNC
USB_DP
USB FELIM
USB_DM
RCC
RC 48MHz 16-bit counter
RELOAD
HSI48 To SYSCLK
To PLL
To USB
The CRS synchronization (SYNC) source, selectable through the CRS_CFGR register, can be the signal
from the external CRS_SYNC pin, the LSE clock or the USB SOF signal. For a better robustness of the
Rev1.3 97 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
SYNC input, a simple digital filter (2 out of 3 majority votes, sampled by the HSI48 clock) is implemented to
filter out any glitches. This source signal also has a configurable polarity and can then be divided by a
programmable binary prescaler to obtain a synchronization signal in a suitable frequency range (usually
around 1 kHz).
For more information on the CRS synchronization source configuration, refer to section 8.6.2: CRS_CFGR
It is also possible to generate a synchronization event by software, by setting the SWSYNC bit in the
CRS_CR register.
The frequency error counter is a 16-bit down/up counter which is reloaded with the RELOAD value on each
SYNC event. It starts counting down till it reaches the zero value, where the ESYNC (expected
synchronization) event is generated. Then it starts counting up to the OUTRANGE limit where it eventually
stops (if no SYNC event is received) and generates a SYNCMISS event. The OUTRANGE limit is defined
as the frequency error limit (FELIM field of the CRS_CFGR register) multiplied by 128.
When the SYNC event is detected, the actual value of the frequency error counter and its counting direction
are stored in the FECAP (frequency error capture) field and in the FEDIR (frequency error direction) bit of
the CRS_ISR register. When the SYNC event is detected during the downcounting phase (before reaching
the zero value), it means that the actual frequency is lower than the target (and so, that the TRIM value
should be incremented), while when it is detected during the upcounting phase it means that the actual
frequency is higher (and that the TRIM value should be decremented).
RELOAD
ESYNC
Down Up
WARNING LIMIT
(3 x FELIM)
TOLERANCE LIMIT
(3 x FELIM)
Trimming action: 0 +2 +1 0 -1 -2 0
SYNCMISS
Rev1.3 98 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
The measured frequency error is evaluated by comparing its value with a set of limits:
─ TOLERANCE LIMIT, given directly in the FELIM field of the CRS_CFGR register
─ WARNING LIMIT, defined as 3 * FELIM value
─ OUTRANGE (error limit), defined as 128 * FELIM value
The result of this comparison is used to generate the status indication and also to control the automatic
trimming which is enabled by setting the AUTOTRIMEN bit in the CRS_CR register:
When the frequency error is below the tolerance limit, it means that the actual trimming value in the
TRIM field is the optimal one and that then, no trimming action is necessary.
─ SYNCOK status indicated
─ TRIM value not changed in AUTOTRIM mode
When the frequency error is below the warning limit but above or equal to the tolerance limit, it means
that some trimming action is necessary but that adjustment by one trimming step is enough to reach the
optimal TRIM value.
─ SYNCOK status indicated
─ TRIM value adjusted by one trimming step in AUTOTRIM mode
When the frequency error is above or equal to the warning limit but below the error limit, it means that a
stronger trimming action is necessary, and there is a risk that the optimal TRIM value will not be
reached for the next period.
─ SYNCWARN status indicated
─ TRIM value adjusted by two trimming steps in AUTOTRIM mode
When the frequency error is above or equal to the error limit, it means that the frequency is out of the
trimming range. This can also happen when the SYNC input is not clean or when some SYNC pulse is
missing (for example when one USB SOF is corrupted).
─ SYNCERR or SYNCMISS status indicated
─ TRIM value not changed in AUTOTRIM mode
Note: If the actual value of the TRIM field is so close to its limits that the automatic trimming would force it to
overflow or underflow, then the TRIM value is set just to the limit and the TRIMOVF status is indicated.
In AUTOTRIM mode (AUTOTRIMEN bit set in the CRS_CR register), the TRIM field of CRS_CR is adjusted
by hardware and is read-only.
RELOAD value
The RELOAD value should be selected according to the ratio between the target frequency and the
frequency of the synchronization source after prescaling. It is then decreased by one in order to reach the
expected synchronization on the zero value. The formula is the following:
RELOAD = (fTARGET / fSYNC) - 1
The reset value of the RELOAD field corresponds to a target frequency of 48 MHz and a synchronization
signal frequency of 1 kHz (SOF signal from USB).
FELIM value
Rev1.3 99 2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
The selection of the FELIM value is closely coupled with the HSI48 oscillator characteristics and its typical
trimming step size. The optimal value corresponds to half of the trimming step size, expressed as a number
of HSI48 oscillator clock ticks. The following formula can be used:
FELIM = (fTARGET / fSYNC) * STEP[%] / 100% / 2
The result should be always rounded up to the nearest integer value in order to obtain the best trimming
response. If frequent trimming actions are not wanted in the application, the trimming hysteresis can be
increased by increasing slightly the FELIM value.
The reset value of the FELIM field corresponds to (fTARGET / fSYNC) = 48000 and to a typical trimming step
size of 0.14%.
Note: There is no hardware protection from a wrong configuration of the RELOAD and FELIM fields which
can lead to an erratic trimming response. The expected operational mode requires proper setup of the
RELOAD value (according to the synchronization source frequency), which is also greater than 128 * FELIM
value (OUTRANGE limit).
SYNCWARNIE
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
Offset Register
9
8
SWSYNC 7
6
5
4
ESYNCIE 3
2
1
SYNCOKIE0
ERRIE
CEN
CRS_CR TRIM[5:0]
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x00
Reset x x x x x x x x x x x x x x x x x x 1 0 0 0 0 0 0 0 0 x 0 0 0 0
SYNCSRC
SYNCPOL
SYNC
[1:0]
CRS_CFGR
0x04
[2:0]
Reset 0 x 1 0 x 0 0 0 0 0 1 0 0 0 1 0 1 0 1 1 1 0 1 1 0 1 1 1 1 1 1 1
SYNCWARNF
SYNCMISS
SYNCERR
SYNCOKF
TRIMOVF
ESYNCF
FEDIR
ERRF
CRS_ISR FECAP[15:0]
–
–
–
–
–
–
–
–
0x08
Reset 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 x x x x 0 0 0 x x x x 0 0 0 0
SYNCWARNC
SYNCOKC
ESYNCC
ERRC
CRS_ICR
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x0C
Reset x x x x x x x x x x x x x x x x x x x x x x x x x x x x 0 0 0 0
8.6.1. CRS_CR
Address offset:0x00
Reset value:0x0000 2000
This bit enables the oscillator clock for the frequency error
0:Frequency error counter disabled
1:Frequency error counter enabled
When this bit is set, the CRS_CFGR register is write-protected and cannot
be modified.
4 NA Reserved
3 ESYNCIE Expected SYNC interrupt enable
0:Expected SYNC (ESYNCF) interrupt disabled
1:Expected SYNC (ESYNCF) interrupt enabled
2 ERRIE Synchronization or trimming error interrupt enable
0:Synchronization or trimming error (ERRF) interrupt disabled
1:Synchronization or trimming error (ERRF) interrupt enabled
1 SYNCWARNIE SYNC warning interrupt enable
0:SYNC warning (SYNCWARNF) interrupt disabled
1:SYNC warning (SYNCWARNF) interrupt enabled
0 SYNCOKIE SYNC event OK interrupt enable
0:SYNC warning (SYNCWARNF) interrupt disabled
1:SYNC warning (SYNCWARNF) interrupt enabled
8.6.2. CRS_CFGR
Address offset:0x04
Reset value:0x2022 BB7F
This register can be written only when the frequency error counter is disabled (CEN bit is cleared in
CRS_CR). When the counter is enabled, this register is write-protected.
8.6.3. CRS_ISR
Address offset:0x08
Reset value:0x0000 0000
23:16 FECAP[7:0]
Type RO RO RO RO RO RO RO RO
15:8 FEDIR — TRIMOVF SYNCMISS SYNCERR
Type RO RO-0 RO-0 RO-0 RO-0 RO RO RO
7:0 — ESYNCF ERRF SYNCWAR SYNCOKF
NF
Type RO-0 RO-0 RO-0 RO-0 RO RO RO RO
8.6.4. CRS_ICR
Address offset:0x0C
Reset value:0x0000 0000
9.1. Introduction
GPIO have A, B, C, D, F five groups, each group has 16 I/O ports except group F and group d, group f has 6
I/O ports and group d has 1 I/O port. 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). Ports A and B also have a
32-bit locking register (GPIOx_LCKR) and two 32-bit alternate function selection registers (GPIOx_AFRH
and GPIOx_AFRL).
Subject to the specific hardware characteristics of each I/O port listed in the datasheet, each port bit of the
general-purpose I/O (GPIO) ports can be individually configured by software in several modes:
Input floatin, Input pull-up, Input-pull-down;
Analog;
Output open-drain with pull-up or pull-down capability, output push-pull with pull-up or pull-down
capability;
Alternate function pull-up or pull-down capability.
Each I/O port bit is freely programmable, however the I/O port registers have to be accessed as 32-bit words,
half-words or bytes. The purpose of the GPIOx_BSRR and GPIOx_BRR registers is to allow atomic
read/modify accesses to any of the GPIOx_ODR registers. In this way, there is no risk of an IRQ occurring
between the read and the modify access.
Figure 9-1 shows the basic structures of a standard I/O port bit, Table 9-1 gives the possible port bit
configurations.
Read on/off
VDDIOx
on/off Pull-up
Input driver
write I/O pin
Output data register
VSS
From on-chip Alternate function output
peripheral Push-pull,
open_drain or
disabled
x x x 0 0 input Floating
x x x 0 1 input pull-up
00
x x x 1 0 input pull-down
x x x 1 1 Input/output
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
During and just after reset, the alternate functions are not active and most of the I/O ports are configured in
input floating mode.
The debug pins are in AF pull-up/pull-down after reset:
PA14: SWCLK in pull-down
PA13: SWDIO in pull-up
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. 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 device I/O pins are connected to on-board peripherals/modules through a multiplexer that allows only
one peripheral alternate function (AF) connected to an I/O pin at a time. In this way, there can be no conflict
between peripherals available on the same I/O pin.
Each I/O pin has a multiplexer with up to sixteen alternate function inputs (AF0 to AF15) that can be
configured through the GPIOx_AFRL (for pin 0 to 7) and GPIOx_AFRH (for pin 8 to 15) registers:
After reset the multiplexer selection is alternate function 0 (AF0). The I/Os are configured in alternate
function mode through GPIOx_MODER register.
The specific alternate function assignments for each pin are detailed in the device datasheet.
In addition to this flexible I/O multiplexing architecture, each peripheral has alternate functions mapped onto
different I/O pins to optimize the number of peripherals available in smaller packages.
Select the type, pull-up/pull-down and output speed via the GPIOx_OTYPER,
GPIOx_PUPDR and GPIOx_OSPEEDER registers, respectively.
Configure the desired I/O as an alternate function in the GPIOx_MODER register.
Additional functions:
For the ADC and DAC, configure the desired I/O in analog mode in the
GPIOx_MODER register and configure the required function in the ADC or DAC
registers.
For the additional functions like RTC, WKUPx and oscillators, configure the required
function in the related RTC, PWR and RCC registers. These functions have priority
over the configuration in the standard GPIO registers.
Please refer to the “Alternate function mapping” table in the device datasheet for the detailed mapping of the
alternate function I/O pins.
Each of the GPIO ports has four 32-bit memory-mapped control registers (GPIOx_MODER,
GPIOx_OTYPER, GPIOx_OSPEEDR, GPIOx_PUPDR) to configure up to 16 I/Os. The GPIOx_MODER
register is used to select the I/O mode (input, output, AF, analog). The GPIOx_OTYPER and
GPIOx_OSPEEDR registers are used to select the output type (push- pull or open-drain) and speed. The
GPIOx_PUPDR register is used to select the pull- up/pull-down whatever the I/O direction.
Each GPIO has two 16-bit memory-mapped data registers: input and output data registers (GPIOx_IDR and
GPIOx_ODR). GPIOx_ODR stores the data to be output, it is read/write accessible. The data input through
the I/O are stored into the input data register (GPIOx_IDR), a read-only register.
GPIO output register:GPIOx_ODR(x=A、B、C、D、F)
GPIO input register:GPIOx_IDR(x=A、B、C、D、F)
The bit set reset register (GPIOx_BSRR) is a 32-bit register which allows the application to set and reset
each individual bit in the output data register (GPIOx_ODR). The bit set reset register has twice the size of
GPIOx_ODR.
To each bit in GPIOx_ODR, correspond two control bits in GPIOx_BSRR: BS(i) and BR(i). When written to 1,
bit BS(i) sets the corresponding ODR(i) bit. When written to 1, bit BR(i) resets the ODR(i) corresponding
bit.Writing any bit to 0 in GPIOx_BSRR does not have any effect on the corresponding bit in GPIOx_ODR. If
there is an attempt to both set and reset a bit in GPIOx_BSRR, the set action takes priority.Using the
GPIOx_BSRR register to change the values of individual bits in GPIOx_ODR is a “one-shot” effect that does
not lock the GPIOx_ODR bits. The GPIOx_ODR bits can always be accessed directly. The GPIOx_BSRR
It is possible to freeze the port A and B GPIO control registers by applying a specific write sequence to the
GPIOx_LCKR register. The frozen registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
To write the GPIOx_LCKR register, a specific write / read sequence has to be applied. When the right LOCK
sequence is applied to bit 16 in this register, the value of LCKR[15:0] is used to lock the configuration of the
I/Os (during the write sequence the LCKR[15:0] value must be the same). When the LOCK sequence has
been applied to a port bit, the value of the port bit can no longer be modified until the next MCU reset or
peripheral reset. Each GPIOx_LCKR bit freezes the corresponding bit in the control registers
(GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR, GPIOx_PUPDR, GPIOx_AFRL and
GPIOx_AFRH.)
The GPIOx_AFRL and GPIOx_AFRH register are provided to select one of the alternate function
inputs/outputs available for each I/O. With these registers, you can connect an alternate function to some
other pin as required by your application.
All ports have external interrupt capability. To use external interrupt lines, the given pin must not be
configured in analog mode or being used as oscillator pin, so the input trigger is kept enabled. For more
details please refer to EXTI.
Figure 9-2 shows the input configuration of the I/O port bit
Read on/off
Read/Write
Figure 9-3 shows the output configuration of the I/O port bit
Read on/off
Input data register
VDDIOx
Bit set/reset registers
on/off
Input driver pull-up
I/O pin
Write
Output data registers
VSS
Push-pull or
open-drain
To on-chip
peripheral
Alternate function input
Read on/off
Input data register
VDDIOx
Bit set/reset registers
pull-up
Input driver on/off
Write I/O pin
Output data register
VSS
From on-chip Alternate function output
peripheral Push-pull or
open drain
To on-chip Analog
peripheral
Read on/off
Input driver
Write Output data register I/O pin
Read/write
Some GPIO can use for LED driver. Setting GPIOx_LEDM to choose the appropriate source current and
sink current to satisfied the application requirement.
PB0, PB1, PB3, PB4, PB5, PB6, PB7, PA8, PA9, PA10, PA13, PA14, PA15 can change the current
characteristic to be configured as LED driver
When the HSE or LSE oscillator is switched OFF (default state after reset), the related oscillator pins can be
used as normal GPIOs.When the HSE or LSE oscillator is switched ON (by setting the HSEON or LSEON
bit in the RCC_CSR register) the oscillator takes control of its associated pins and the GPIO configuration of
these pins has no effect.When the oscillator is configured in a user external clock mode, only the pin is
reserved for clock input and the OSC_OUT or OSC32_OUT pin can still be used as normal GPIO.
After configuring the related register, the corresponding I/O will be switched to analog mode., and the GPIO
registers have no effect at this time.For more details please refer to OPx operational amplifier section.
I2C1 fast-mode: Configure the I2C1 register to change the GPIO drive capability which alternate function is
0x1C
0x0C
0x0C
Offset
9.4.
Rev1.3
Reset
Reset
Reset
Reset
Reset
Reset
Reset
Reset
Reset
Reset
Reset
Reset
Reset
Register
GPIOx_IDR
GPIOx_ODR
GPIOx_AFRL
GPIOx_AFRH
GPIOx_LCKR
GPIOx_BSRR
GPIOx_PUPDR
(where x = A B)
(where x = A B)
(where x = A B)
GPIOA_PUPDR
GPIOx_MODER
GPIOA_MODER
GPIOx_OTYPER
GPIOx_OSPEEDR
GPIOA_OSPEEDR
(where x =B..D,F)
(where x =B..D,F)
(where x = B..D,F)
– – – –
x
x
x
x
BR15 #
0
0
0
0
0
0
0
0
0
PUPDR15[1:0] PUPDR15[1:0] OSPEEDR15[1:0] OSPEEDR15[1:0] MODER15[1:0] MODER15[1:0]
– – – –
x
x
x
x
BR14 #
0
0
0
0
0
0
0
0
AFSEL15 [3:0] AFSEL7 [3:0] 0
– – – –
x
x
x
x
BR13 #
0
0
0
0
1
0
0
0
1
x
x
x
x
BR12 #
0
0
0
0
0
0
0
0
0
– – – –
x
x
x
x
BR11 #
0
0
0
0
0
0
1
0
1
x
x
x
x
BR10 #
0
0
0
0
1
0
1
0
0
x
x
x
x
BR9 #
0
0
0
0
0
0
0
0
0
x
x
x
x
BR8 #
0
0
0
0
0
0
0
0
0
GPIO register map
– – – –
x
x
x
x
BR7 #
0
0
0
0
0
0
0
0
0
x
x
x
x
BR6 #
0
0
0
0
0
0
0
0
0
x
x
x
x
BR5 #
0
0
0
0
0
0
0
0
0
x
x
x
x
BR4 #
0
0
0
0
0
0
0
0
0
– – – –
x
x
x
x
BR3 #
0
0
0
0
0
0
0
0
0
117
PUPDR9[1:0] PUPDR9[1:0] OSPEEDR9[1:0] OSPEEDR9[1:0] MODER9[1:0] MODER9[1:0]
– – – –
x
x
x
x
BR2 #
0
0
0
0
0
0
0
0
0
x
x
x
x
BR1 #
0
0
0
0
0
0
0
0
0
x
x
x
BR0 #
0
0
0
0
0
0
0
0
0
0
x
LCK15 BS15 ODR15 IDR15 OT15 #
0
0
0
0
0
0
0
0
0
0
0
0
x
LCK14 BS14 ODR14 IDR14 OT14 #
0
0
0
0
0
0
0
0
0
0
0
0
x
LCK13 BS13 ODR13 IDR13 OT13 #
0
0
0
0
0
0
0
0
0
0
0
0
x
LCK12 BS12 ODR12 IDR12 OT12 #
0
0
0
0
0
0
0
0
0
0
0
0
x
LCK11 BS11 ODR11 IDR11 OT11 #
0
0
0
0
0
0
0
0
0
0
0
0
x
LCK10 BS10 ODR10 IDR10 OT10 #
0
0
0
0
0
0
0
0
0
0
0
0
x
LCK9 BS9 ODR9 IDR9 OT9 9
0
0
0
0
0
0
0
0
0
0
0
0
x
LCK8 BS8 ODR8 IDR8 OT8 8
0
0
0
0
0
0
0
0
0
0
0
0
x
LCK7 BS7 ODR7 IDR7 OT7 7
0
0
0
0
0
0
0
0
0
0
0
0
x
LCK6 BS6 ODR6 IDR6 OT6 6
0
0
0
0
0
0
0
0
0
0
0
0
x
LCK5 BS5 ODR5 IDR5 OT5 5
0
0
0
0
0
0
0
0
0
0
0
0
x
LCK4 BS4 ODR4 IDR4 OT4 4
0
0
0
0
0
0
0
0
0
0
0
0
x
LCK3 BS3 ODR3 IDR3 OT3 3
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
FT32F0xxx8 RM
2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
GPIOx_BRR
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x2C (where x = A.. D,F)
BR15
BR14
BR13
BR12
BR11
BR10
BR9
BR8
BR7
BR6
BR5
BR4
BR3
BR2
BR1
BR0
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
GPIOx_LEDM
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
LEDM15
LEDM14
LEDM13
LEDM10
0x30 (where x = A)
LEDM9
LEDM8
Reset x x x x x x x x x x x x x x x x 0 0 0 x x 0 0 0 x x x x x x x x
GPIOx_LEDM
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x30 (where x = B)
LEDM7
LEDM6
LEDM5
LEDM4
LEDM3
LEDM1
LEDM0
Reset x x x x x x x x x x x x x x x x x x x x x x x x 0 0 0 0 0 x 0 0
9.4.1. GPIOx_MODER
x=A、B、C、D、F
Address offset:0x00
Reset value:
0x2800 0000 (Port A)
0x0000 0000 (Other ports)
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 MODER15[1:0] MODER14[1:0] MODER13[1:0] MODER12[1:0]
Type RW RW RW RW RW RW RW RW
23:16 MODER11[1:0] MODER10[1:0] MODER9[1:0] MODER8[1:0]
Type RW RW RW RW RW RW RW RW
15:8 MODER7[1:0] MODER6[1:0] MODER5[1:0] MODER4[1:0]
Type RW RW RW RW RW RW RW RW
7:0 MODER3[1:0] MODER2[1:0] MODER1[1:0] MODER0[1:0]
Type RW RW RW RW RW RW RW RW
9.4.2. GPIOx_OTYPER
x=A、B、C、D、F
Address offset:0x04
Reset value:0x0000 0000
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
23:16 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
15:8 OT15 OT14 OT13 OT12 OT11 OT10 OT9 OT8
Type RW RW RW RW RW RW RW RW
7:0 OT7 OT6 OT5 OT4 OT3 OT2 OT1 OT0
Type RW RW RW RW RW RW RW RW
9.4.3. GPIOx_OSPEEDR
x=A、B、C、D、F
Address offset:0x08
Reset value:0x0000 0000
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 OSPEEDR15[1:0] OSPEEDR14[1:0] OSPEEDR13[1:0] OSPEEDR12[1:0]
Type RW RW RW RW RW RW RW RW
23:16 OSPEEDR11[1:0] OSPEEDR10[1:0] OSPEEDR9[1:0] OSPEEDR8[1:0]
Type RW RW RW RW RW RW RW RW
15:8 OSPEEDR7[1:0] OSPEEDR6[1:0] OSPEEDR5[1:0] OSPEEDR4[1:0]
Type RW RW RW RW RW RW RW RW
7:0 OSPEEDR3[1:0] OSPEEDR2[1:0] OSPEEDR1[1:0] OSPEEDR0[1:0]
Type RW RW RW RW RW RW RW RW
01:Medium speed
11:High speed
9.4.4. GPIOx_PUPDR
Address offset:0x0C
Reset value:0x2400 0000(Port A)
0x0000 0000(Other ports)
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 PUPDR15[1:0] PUPDR14[1:0] PUPDR13[1:0] PUPDR12[1:0]
Type RW RW RW RW RW RW RW RW
23:16 PUPDR11[1:0] PUPDR10[1:0] PUPDR9[1:0] PUPDR8[1:0]
Type RW RW RW RW RW RW RW RW
15:8 PUPDR7[1:0] PUPDR6[1:0] PUPDR5[1:0] PUPDR4[1:0]
Type RW RW RW RW RW RW RW RW
7:0 PUPDR3[1:0] PUPDR2[1:0] PUPDR1[1:0] PUPDR0[1:0]
Type RW RW RW RW RW RW RW RW
9.4.5. GPIOx_IDR
x=A、B、C、D、F
Address offset:0x10
Reset value:0x0000 xxxx
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
23:16 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
15:8 IDR15 IDR14 IDR13 IDR12 IDR11 IDR10 IDR9 IDR8
Type RO RO RO RO RO RO RO RO
9.4.6. GPIOx_ODR
x=A、B、C、D、F
Address offset:0x14
Reset value:0x0000 0000
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
23:16 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
15:8 ODR15 ODR14 ODR13 ODR12 ODR11 ODR10 ODR9 ODR8
Type RW RW RW RW RW RW RW RW
7:0 ODR7 ODR6 ODR5 ODR4 ODR3 ODR2 ODR1 ODR0
Type RW RW RW RW RW RW RW RW
9.4.7. GPIOx_BSRR
x=A、B、C、D、F
Address offset:0x18
Reset value:0x0000 0000
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 BR15 BR14 BR13 BR12 BR11 BR10 BR9 BR8
Type WO WO WO WO WO WO WO WO
23:16 BR7 BR6 BR5 BR4 BR3 BR2 BR1 BR0
Type WO WO WO WO WO WO WO WO
15:8 BS15 BS14 BS13 BS12 BS11 BS10 BS9 BS8
Type WO WO WO WO WO WO WO WO
9.4.8. GPIOx_LCKR
x=A、B
This register is used to lock the configuration of the port bits when a correct write sequence is applied to bit
16 (LCKK). The value of bits [15:0] is used to lock the configuration of the GPIO. During the write sequence,
the value of LCKR[15:0] must not change. When the LOCK sequence has been applied on a port bit, the
value of this port bit can no longer be modified until the next MCU reset or peripheral reset.
Note: A specific write sequence is used to write to the GPIOx_LCKR register. Only word access (32-bit long)
is allowed during this locking sequence.
Address offset:0x1C
Reset value:0x0000 0000
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
23:16 — LCKK
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RW
15:8 LCK15 LCK14 LCK13 LCK12 LCK11 LCK10 LCK9 LCK8
Type RW RW RW RW RW RW RW RW
7:0 LCK7 LCK6 LCK5 LCK4 LCK3 LCK2 LCK1 LCK0
Type RW RW RW RW RW RW RW RW
9.4.9. GPIOx_AFRL
x=A、B
Address offset:0x20
Reset value:0x0000 0000
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 — AFRSEL7 — AFRSEL6
Type RO-0 RW RW RW RO-0 RW RW RW
9.4.10. GPIOx_AFRH
x=A、B
Address offset:0x24
Reset value:0x0000 0000
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 — AFRSEL15 — AFRSEL14
Type RO-0 RW RW RW RO-0 RW RW RW
9.4.11. GPIOx_BRR
x=A、B、C、D、F
Address offset:0x28
Reset value:0x0000 0000
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
23:16 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
15:8 BR15 BR14 BR13 BR12 BR11 BR10 BR9 BR8
Type WO WO WO WO WO WO WO WO
7:0 BR7 BR6 BR5 BR4 BR3 BR2 BR1 BR0
Type WO WO WO WO WO WO WO WO
9.4.12. GPIOA_LEDM
9.4.13. GPIOB_LEDM
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
USART1_RX_DMA_RMP 10
offset Register
USART1_TX_DMA_RMP 9
0
IRDA_ENV_SEL[1:0]
TIM17_DMA-RMP
TIM16_DMA-RMP
ADC_DMA_RMP
I2C_PB9_FMP
I2C_PB8_FMP
I2C_PB7_FMP
I2C_PB6_FMP
MEM_MODE
I2C1_FMP
SYSCFG_CFGR1
-
-
0x00
Reset x x x x x x x x x x x 0 0 0 0 0 x x x 0 0 0 0 0 0 0 x x x x x x
SYSCFG_EXTICR1 EXTI3[2:0] EXTI2[2:0] EXTI1[2:0] EXTI0[3:0]
-
0x08
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
SYSCFG_EXTICR2 EXTI7[2:0] EXTI6[2:0] EXTI5[2:0] EXTI4[2:0]
-
0x0C
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
SYSCFG_EXTICR3 EXTI11[2:0] EXTI10[2:0] EXTI9[2:0] EXTI8[2:0]
-
0x10
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
SYSCFG_EXTICR4 EXTI5[2:0] EXTI4[2:0] EXTI13[2:0] EXTI12[2:0]
-
0x14
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
LOCKUP_LOCK
PVD_LOCK
SYSCFG_CFGR2
-
-
0x18
Reset x x x x x x x x x x x x x x x x x x x x x x x x x x x x x 0 x 0
10.1.1. SYSCFG_CFGR1
Address offset:0x00
Reset value:0x0000 000X
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 —
Type RW RW RW RW RW RW RW RW
23:16 — — — I2C1_FMP I2C_PB9 I2C_PB8 I2C_PB7 I2C_PB6
_FMP _FMP _FMP _FMP
Type RW RW RW RW RW RW RW RW
15:8 — — — TIM17_ TIM16_ USART1_ USART1_ ADC_
DMA DMA RX_DMA TX_DMA DMA
_RMP _RMP _RMP _RMP _RMP
Type RW RW RW RW RW RW RW RW
7:0 IRDA_ENV_SEL[1:0] — MEM_MODE
Type RW RW RW RW RW RW RW RW
10.1.2. SYSCFG_EXTICR1
Address offset:0x08
Reset value:0x0000 0000
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
23:16 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
15:8 EXTI3[3:0] EXTI2[3:0]
Type RW RW RW RW RW RW RW RW
7:0 EXTI1[3:0] EXTI0[3:0]
Type RW RW RW RW RW RW RW RW
0010:PC3 pin
0011:Reserved
0100:Reserved
0101:Reserved
Other configurations: Reserved
11:8 EXTI2[3:0] EXTI 2 configuration bits
0000:PA2 pin
0001:PB2 pin
0010:PC2 pin
0011:PD2 pin
0100:Reserved
0101:Reserved
Other configurations: Reserved
7:4 EXTI1[3:0] EXTI 1 configuration bits
0000:PA1 pin
0001:PB1 pin
0010:PC1 pin
0011:Reserved
0100:Reserved
0101:PF1 pin
Other configurations: Reserved
3:0 EXTI0[3:0] EXTI 0 configuration bits
0000:PA0 pin
0001:PB0 pin
0010:PC0 pin
0011:Reserved
0100:Reserved
0101:PF0 pin
Other configurations: Reserved
10.1.3. SYSCFG_EXTICR2
Address offset:0x0C
Reset value:0x0000 0000
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
23:16 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
15:8 EXTI7[3:0] EXTI6[3:0]
Type RW RW RW RW RW RW RW RW
10.1.4. SYSCFG_EXTICR3
Address offset:0x10
0101:Reserved
Other configurations: Reserved
10.1.5. SYSCFG_EXTICR4
Address offset:0x14
Reset value:0x0000 0000
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
23:16 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
15:8 EXTI15[3:0] EXTI14[3:0]
Type RW RW RW RW RW RW RW RW
7:0 EXTI3[3:0] EXTI2[3:0]
Type RW RW RW RW RW RW RW RW
0101:Reserved
Other configurations: Reserved
3:0 EXTI12[3:0] EXTI 12 configuration bits
0000:PA12 pin
0001:PB12 pin
0010:PC12 pin
0011:Reserved
0100:Reserved
0101:Reserved
Other configurations: Reserved
10.1.6. SYSCFG_CFGR2
11.1. Introduction
Direct memory access (DMA) is used in order to provide high-speed data transfer between peripherals and
memory as well as memory to memory. Data can be quickly moved by DMA without any CPU actions. This
keeps CPU resources free for other operations.
The DMA controller has up to 5 channels, each dedicated to managing memory access requests from one or
more peripherals. It has an arbiter for handling the priority between DMA requests.
FLITF Flash
System
Cortex-M0
DMA SRAM
Ch.1
Bus matrix
DMA
Ch.2 Reset & clock control
CRC GPIOA GPIOB
(RCC)
up to
Ch.5 Bridge
Arbiter APB
GPIOC GPIOD GPIOF
AHB Slave
ADC TIM1
USART1 TIM3
USART2 TIM6
I2C1 TIM15
I2C2 TIM16
SPI1 TIM17
SPI2
The DMA controller performs direct memory transfer by sharing the system bus with the Cortex ®-M0 core.
The DMA request may stop the CPU access to the system bus for some bus cycles, when the CPU and
DMA are targeting the same destination (memory or peripheral). The bus matrix implements round-robin
scheduling, thus ensuring at least half of the system bus bandwidth (both to memory and peripheral) for the
CPU.
After an event, the peripheral sends a request signal to the DMA Controller. The DMA controller serves the
request depending on the channel priorities. As soon as the DMA Controller accesses the peripheral, an
Acknowledge is sent to the peripheral by the DMA Controller. The peripheral releases its request as soon as
it gets the Acknowledge from the DMA Controller. Once the request is de-asserted by the peripheral, the
DMA Controller release the Acknowledge. If there are more requests, the peripheral can initiate the next
transaction.
In summary, each DMA transfer consists of three operations:
The loading of data from the peripheral data register or a ___location in memory addressed through an
internal current peripheral/memory address register. The start address used for the first transfer is the
11.3.2. Arbiter
The arbiter manages the channel requests based on their priority and launches the peripheral/memory
access sequences.
The priorities are managed in two stages:
Software: each channel priority can be configured in the DMA_CCRx register. There are four levels:
─ Very high priority
─ High priorit
─ Medium priority
─ Low priority
Hardware: if 2 requests have the same software priority level, the channel with the lowest number will
get priority versus the channel with the highest number. For example, channel 2 gets priority over
channel 4.
Each channel can handle DMA transfer between a peripheral register located at a fixed address and a
memory address. The amount of data to be transferred (up to 65535) is programmable. The register which
contains the amount of data items to be transferred is decremented after each transaction.
Pointer incrementation
Peripheral and memory pointers can optionally be automatically post-incremented after each transaction
depending on the PINC and MINC bits in the DMA_CCRx register. If incremented mode is enabled, the
address of the next transfer will be the address of the previous one incremented by 1, 2 or 4 depending on
the chosen data size. The first transfer address is the one programmed in the DMA_CPARx/DMA_CMARx
registers. During transfer operations, these registers keep the initially programmed value. The current
transfer addresses (in the current internal peripheral/memory address register) are not accessible by
software.
If the channel is configured in non-circular mode, no DMA request is served after the last transfer (that is
once the number of data items to be transferred has reached zero). In order to reload a new number of data
items to be transferred into the DMA_CNDTRx register, the DMA channel must be disabled.
Note: If a DMA channel is disabled, the DMA registers are not reset. The DMA channel registers
(DMA_CCRx, DMA_CPARx and DMA_CMARx) retain the initial values programmed during the channel
configuration phase.
In circular mode, after the last transfer, the DMA_CNDTRx register is automatically reloaded with the initially
programmed value. The current internal address registers are reloaded with the base address values from
the DMA_CPARx/DMA_CMARx registers.
Circular mode
Circular mode is available to handle circular buffers and continuous data flows (e.g. ADC scan mode). This
feature can be enabled using the CIRC bit in the DMA_CCRx register. When circular mode is activated, the
number of data to be transferred is automatically reloaded with the initial value programmed during the
channel configuration phase, and the DMA requests continue to be served.
Memory-to-memory mode
The DMA channels can also work without being triggered by a request from a peripheral. This mode is called
Memory to Memory mode.
If the MEM2MEM bit in the DMA_CCRx register is set, then the channel initiates transfers as soon as it is
enabled by software by setting the Enable bit (EN) in the DMA_CCRx register. The transfer stops once the
DMA_CNDTRx register reaches zero. Memory to Memory mode may not be used at the same time as
Circular mode.
When PSIZE and MSIZE are not equal, the DMA performs some data alignments as described in Table 11-1
@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
@0x8 /
BBBAB9B8
@0xC /
BFBEBDBC
@0x0 /
B3B2B1B0
@0x4 / 1:READ B3B2B1B0[31:0] @0x0 then WRITE B1B0[15:0] 0x0 @0x0 / B1B0
B7B6B5B4 2:READ B7B6B5B4[31:0] @0x4 then WRITE B5B4[15:0] 0x2 @0x2 / B5B4
32 16 4
@0x8 / 3:READ BBBAB9B8[31:0] @0x8 then WRITE B9B8[15:0] 0x4 @0x4 / B9B8
BBBAB9B8 4:READ BFBEBDBC[31:0] @0xC then WRITE BDBC[15:0] 0x6 @0x6 / BDBC
@0xC /
BFBEBDBC
B3B2B1B0 0x0
BFBEBDBC 0xC
Addressing an AHB peripheral that does not support byte or halfword write operations
When the DMA initiates an AHB byte or halfword write operation, the data are duplicated on the unused
lanes of the HWDATA[31:0] bus. So when the used AHB slave peripheral does not support byte or halfword
write operations (when HSIZE is not used by the peripheral) and does not generate any error, the DMA
writes the 32 HWDATA bits as shown in the two examples below:
To write the halfword “0xABCD”, the DMA sets the HWDATA bus to “0xABCDABCD” with HSIZE =
HalfWord
To write the byte “0xAB”, the DMA sets the HWDATA bus to “0xABABABAB” with HSIZE = Byte
Assuming that the AHB/APB bridge is an AHB 32-bit slave peripheral that does not take the HSIZE data into
account, it will transform any AHB byte or halfword operation into a 32-bit APB operation in the following
manner:
an AHB byte write operation of the data “0xB0” to 0x0 (or to 0x1, 0x2 or 0x3) will be converted to an
APB word write operation of the data “0xB0B0B0B0” to 0x0
an AHB halfword write operation of the data “0xB1B0” to 0x0 (or to 0x2) will be converted to an APB
word write operation of the data “0xB1B0B1B0” to 0x0
For instance, if you want to write the APB backup registers (16-bit registers aligned to a 32- bit address
boundary), you must configure the memory source size (MSIZE) to “16-bit” and the peripheral destination
size (PSIZE) to “32-bit”.
A DMA transfer error can be generated by reading from or writing to a reserved address space. When a
DMA transfer error occurs during a DMA read or a write access, the faulty channel is automatically disabled
through a hardware clear of its EN bit in the corresponding Channel configuration register (DMA_CCRx). The
channel's transfer error interrupt flag (TEIF) in the DMA_IFR register is set and an interrupt is generated if
the transfer error interrupt enable bit (TEIE) in the DMA_CCRx register is set.
An interrupt can be produced on a Half-transfer, Transfer complete or Transfer error for each DMA channel.
Separate interrupt enable bits are available for flexibility.
Table 11-2 DMA interrupt requests
Interrupt event Event flag Enable control bit
Half-transfer HTIF HTIE
Transfer complete TCIF TCIE
Transfer error TEIF TEIE
DMA controller
The hardware requests from the peripherals (TIMx, ADC, DAC, SPI, I2C, and USARTx) are simply logically
ORed before entering the DMA. This means that on one channel, only one request must be enabled at a
time.
The peripheral DMA requests can be independently activated/de-activated by programming the DMA control
bit in the registers of the corresponding peripheral.
DMA
Fixed hardware priority
SPI1_TX,
USART1_RX(1),
I2C1_RX,TIM1_CH2, Hardware request 3
Internal DMA
TIM3_CH4,TIM3_UP, Ch3 request
TIM6_UP,TIM16_CH1(1), Software trigger 3
TIM16_UP(1) (MEM2MEM位)
SPI2_RX,
USART1_TX(2),
I2C2_TX,USART2_TX,
TIM1_CH4,TIM1_TRIG, Hardware request 4
TIM1_COM, Ch4
TIM3_CH1,TIM3_TRIG, Software trigger 4
TIM16_CH1(2), (MEM2MEM位)
TIM16_UP(2)
SPI2_TX,
USART1_RX(2),
I2C2_RX,USART2_RX, Hardware request 5
TIM1_CH3,TIM1_UP, Ch5
TIM15_CH1, Software trigger 5
TIIM15_UP,TIM15_TRIG, (MEM2MEM位) Low
TIM15_COM priority
31
30
29
28
27
26
25
24
23
22
21
20
TEIF5 19
HTIF5 18
TCIF5 17
16
TEIF4 15
HTIF4 14
TCIF4 13
12
TEIF3 11
HTIF3 10
Offset Register
TCIF3 9
8
TEIF2 7
HTIF2 6
TCIF2 5
4
TEIF1 3
HTIF1 2
TCIF1 1
0
GIF5
GIF4
GIF3
GIF2
GIF1
DMA_ISR
–
–
–
–
–
–
–
–
–
–
–
–
0x00
Reset x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
CTEIF5
CHTIF5
CTCIF5
CTEIF4
CHTIF4
CTCIF4
CTEIF3
CHTIF3
CTCIF3
CTEIF2
CHTIF2
CTCIF2
CTEIF1
CHTIF1
CTCIF1
CGIF5
CGIF4
CGIF3
CGIF2
CGIF1
DMA_IFCR
–
–
–
–
–
–
–
–
–
–
–
–
0x04
Reset x x x x x x x x x x x x 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]
MINC
PINC
CIRC
TEIE
HTIE
TCIE
DIR
PL
EN
DMA_CCR1
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x08 [1:0]
Reset x x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CNDTR1 NDT[15:0]
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x0C
Reset x x x x x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CPAR1 PA[31:0]
0x10
Reset 0 0 0 0 0 0 0 0 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]
0x14
Reset 0 0 0 0 0 0 0 0 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]
MINC
PINC
CIRC
TEIE
HTIE
TCIE
DIR
PL
EN
DMA_CCR2
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x1C [1:0]
Reset x x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CNDTR2 NDT[15:0]
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x20
Reset x x x x x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CPAR2 PA[31:0]
0x24
Reset 0 0 0 0 0 0 0 0 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]
0x28
Reset 0 0 0 0 0 0 0 0 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]
MINC
PINC
CIRC
TEIE
HTIE
TCIE
DIR
PL
EN
DMA_CCR3
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x30 [1:0]
Reset x x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CNDTR3 NDT[11:0]
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x34
Reset x x x x x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CPAR3 PA[31:0]
0x38
Reset 0 0 0 0 0 0 0 0 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]
0x3C
Reset 0 0 0 0 0 0 0 0 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]
MINC
PINC
CIRC
TEIE
HTIE
TCIE
DIR
PL
EN
DMA_CCR4
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x44 [1:0]
Reset x x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CNDTR4 NDT[15:0]
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x48
Reset x x x x x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CPAR4 PA[31:0]
0x4C
Reset 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x50 DMA_CMAR4 MA[31:0]
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
MEM2MEM 14
13
12
11
10
Offset Register
9
8
7
6
5
4
3
2
1
0
MSIZE[1:0]
PSIZE[1:0]
MINC
PINC
CIRC
HTIE
TEIE
TCIE
DIR
PL
EN
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
DMA_CCR5
0x58 [1:0]
Reset x x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CNDTR5 NDT[15:0]
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x5C
Reset x x x x x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CPAR5 PA[31:0]
0x60
Reset 0 0 0 0 0 0 0 0 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_CMAR5 MA[31:0]
0x64
Reset 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
11.4.1. DMA_ISR
Address offset:0x00
Reset value:0x0000 0000
11.4.2. DMA_IFCR
Address offset:0x04
Reset value:0x0000 0000
11.4.3. DMA_CCRx(x=1..5)
11.4.4. DMA_CNDTRx(x=1..5)
Type RW RW RW RW RW RW RW RW
7:0 NDT[7:0]
Type RW RW RW RW RW RW RW RW
11.4.5. DMA_CPARx(x=1..5)
11.4.6. DMA_CMARx(x=1..5)
32 maskable interrupt channels (not including the sixteen ARM ® Cortex®-M0 interrupt lines)
4 programmable priority levels (2 bits of interrupt priority are used)
Low-latency exception and interrupt handling
Power management control
Implementation of System Control Registers
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 and a wake-up request to
the Power Manager.
The active edge of each external interrupt line can be chosen independently, whilst for internal interrupt the
active edge is always the rising one. An interrupt could be left pending: in case of an external one, a status
register is instantiated and indicates the source of the interrupt; an event is always a simple pulse and it’s
used for triggering the core Wake-up. For internal interrupts, the pending status is assured by the generating
IP, so no need for a specific flag. Each input line can be masked independently for interrupt or event
generation, in addition the internal lines are sampled only in STOP mode. This controller allows also to
emulate the (only) external events by software, multiplexed with the corresponding hardware event line, by
writing to a dedicated register.
Rising
edge
detect
External events
Edge detect circuit
event
Event mask
register
Interrupt
Internal events
Rising edge
Stop mode
detect
FT32F0xxx8 is able to handle external or internal events in order to wake up the core (WFE). The wakeup
event can be generated either by:
1 Enabling an interrupt in the peripheral control register but not in the NVIC, and enabling the
SEVONPEND bit in the Cortex-M0 System Control register. When the MCU resumes from WFE, the
EXTI peripheral interrupt pending bit and the peripheral NVIC IRQ channel pending bit (in the NVIC
interrupt clear pending register) have to be cleared.
2 Or by configuring an external or internal EXTI line in event mode. When the CPU resumes from WFE, it
is not necessary to clear the peripheral interrupt pending bit or the NVIC IRQ channel pending bit as the
pending bit corresponding to the event line is not set.
For the external interrupt lines, to generate the interrupt, the interrupt line should be configured and
enabled.This is done by programming the EXTI_RTSR and EXTI_FTSR register with the desired edge
detection and by enabling the interrupt request by writing a ‘1’ to the corresponding bit in the EXTI_IMR
interrupt mask register.
For the internal interrupt lines, the active edge is always the rising edge, the interrupt is enabled by default in
the interrupt mask register and there is no corresponding pending bit in the pending register.
To generate the event, the event line should be configured and enabled. This is done by programming the
EXTI_RTSR and EXTI_FTSR register with the desired edge detection and by enabling the interrupt request
by writing a ‘1’ to the corresponding bit in the EXTI_IMR interrupt mask register.
For the external lines, an interrupt/event request can also be generated by software by writing a ‘1’ in the
software interrupt/event register.
The GPIOs are connected to the 16 external interrupt/event lines in the following manner:
PA0 PA4
PA8
PB0 PB4
EXTI0 EXTI4 PB8 EXTI8
PC0 PC4
PC8
PF0 PF4
EXTI1[3:0] EXTI5[3:0]
PA1 PA5
PB1 PB5
EXTI1 EXTI5
PC1 PC5
PF1 PF5
PA2 PA6
PA14
PB2 PB6
EXTI2 EXTI6 PB14 EXTI14
PC2 PC6
PC14
PD2 PF6
PA3 PA7
PA15
PB3 PB7
EXTI3 EXTI7 PB15 EXTI15
PC3 PC7
PC15
PF7
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
offset Register
0
EXTI_IMR IMR[31:0]
0x00
Reset 0 1 1 1 1 1 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
EXTI_EMR EMR[31:0]
0x04
Reset 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
RTSR[22]
RTSR[22]
RTSR[21]
RTSR[19]
EXTI_RTSR RTSR[17:0]
-
0x08
Reset x x x x x x x x 0 0 0 x 0 x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
FTSR[22]
FTSR[22]
FTSR[21]
FTSR[19]
EXTI_FTSR FTSR[17:0]
-
0x0C
Reset x x x x x x x x 0 0 0 x 0 x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
SWIER[22]
SWIER[22]
SWIER[21]
SWIER[19]
EXTI_SWIER SWIER[17:0]
-
0x10
Reset x x x x x x x x 0 0 0 x 0 x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
PR[22]
PR[22]
PR[21]
PR[19]
EXTI_PR PR[17:0]
-
0x14
Reset x x x x x x x x 0 0 0 x 0 x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
12.3.1. EXTI_IMR
Address offset:0x00
Reset value:0x7F04 0000
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 IMR[31:24]
Type RW RW RW RW RW RW RW RW
23:16 IMR[23:16]
Type RW RW RW RW RW RW RW RW
15:8 IMR[15:8]
Type RW RW RW RW RW RW RW RW
7:0 IMR[7:0]
Type RW RW RW RW RW RW RW RW
12.3.2. EXTI_EMR
Address offset:0x04
Reset value:0x0000 0000
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 EMR[31:24]
Type RW RW RW RW RW RW RW RW
23:16 EMR[23:16]
Type RW RW RW RW RW RW RW RW
15:8 EMR[15:8]
Type RW RW RW RW RW RW RW RW
7:0 EMR[7:0]
Type RW RW RW RW RW RW RW RW
12.3.3. EXTI_RTSR
Address offset:0x08
Reset value:0x0000 0000
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
23:16 RTSR[23] RTSR[22] RTSR[21] — RTSR[19] — RTSR[17 RTSR[16]
]
Type RW RW RW RO-0 RW RO-0 RW RW
15:8 RTSR[15:8]
Type RW RW RW RW RW RW RW RW
7:0 RTSR[7:0]
Type RW RW RW RW RW RW RW RW
0:Rising trigger disabled (for Event and Interrupt) for input line
1:Rising trigger enabled (for Event and Interrupt) for input line
21 RTSR[21] Rising trigger event configuration bit of line 21
0:Rising trigger disabled (for Event and Interrupt) for input line
1:Rising trigger enabled (for Event and Interrupt) for input line
20 NA Reserved
19 RTSR[19] Rising trigger event configuration bit of line 19
0:Rising trigger disabled (for Event and Interrupt) for input line
1:Rising trigger enabled (for Event and Interrupt) for input line
18 NA Reserved
17:0 RTSR[17:0] Rising trigger event configuration bit of line x (x = 17 to 0)
0:Rising trigger disabled (for Event and Interrupt) for input line
1:Rising trigger enabled (for Event and Interrupt) for input line
12.3.4. EXTI_FTSR
Address offset:0x0C
Reset value:0x0000 0000
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
23:16 FTSR[23] FTSR[22] FTSR[21] — FTSR[19] — FTSR[17] FTSR[16]
Type RW RW RW RO-0 RW RO-0 RW RW
15:8 FTSR[15:8]
Type RW RW RW RW RW RW RW RW
7:0 FTSR[7:0]
Type RW RW RW RW RW RW RW RW
1: Falling trigger enabled (for Event and Interrupt) for input line
18 NA Reserved
17:0 FTSR[17:0] Falling trigger event configuration bit of line x (x = 17 to 0)
0: Falling trigger disabled (for Event and Interrupt) for input line
1: Falling trigger enabled (for Event and Interrupt) for input line
12.3.5. EXTI_SWIER
Address offset:0x10
Reset value:0x0000 0000
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
23:16 SWIER[23] SWIER[22] SWIER[21] — SWIER[19] — SWIER[1 SWIER[16]
7]
Type RW RW RW RO-0 RW RO-0 RW RW
15:8 SWIER[15:8]
Type RW RW RW RW RW RW RW RW
7:0 SWIER[7:0]
Type RW RW RW RW RW RW RW RW
12.3.6. EXTI_PR
Address offset:0x14
Reset value:0x0000 0000
Bit 31/23/15/7 30/22/14/6 29/21/13/5 28/20/12/4 27/19/11/3 26/18/10/2 25/17/9/1 24/16/8/0
31:24 —
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
23:16 PR[23] PR[22] PR[21] — PR[19] — PR[17] PR [16]
Type RW RW RW RO-0 RW RO-0 RW RW
15:8 PR [15:8]
Type RW RW RW RW RW RW RW RW
7:0 PR [7:0]
Type RW RW RW RW RW RW RW RW
20 NA Reserved
19 PR [19] Pending bit on line 19
0:No trigger request occurred
1:selected trigger request occurred
This bit is set when the selected edge event arrives on the external interrupt
line. This bit is cleared by writing a 1 to the bit.
18 NA Reserved
17:0 PR [17:0] Pending bit on line x (x = 17 to 0)
0:No trigger request occurred
1:selected trigger request occurred
This bit is set when the selected edge event arrives on the external interrupt
line. This bit is cleared by writing a 1 to the bit.
13.1. Introduction
High performance
─ 12-bit, 10-bit, 8-bit or 6-bit configurable resolution
─ ADC conversion time: 1.0 µs for 12-bit resolution (1 MHz), 0.93 µs conversion time for 10-bit
resolution, faster conversion times can be obtained by lowering resolution.
─ Self-calibration
─ Programmable sampling time
─ Data alignment with built-in data coherency
─ DMA support
─ Precisely control the sample stop time
─ Flexible hardware trigger configuration
─ Acquire maximum 3 channel conversion value at the same time(ADC_INx、IOSH1、IOSH2)
Low-power
─ Application can reduce PCLK frequency for low-power operation while still keeping optimum ADC
performance. For example, 1.0 µs conversion time is kept, whatever the frequency of PCLK
─ Wait mode: prevents ADC overrun in applications with low frequency PCLK
─ Auto off mode: ADC is automatically powered off except during the active conversion phase. This
dramatically reduces the power consumption of the ADC.
Analog input channels
─ 16 external analog inputs
─ 1 channel for internal temperature sensor(VTS)
─ 1 channel for internal reference voltage(VREFINT)
─ 1 I/O Sample and hold circuits(VIOSHx)
─ 1 Operational amplifiers(VOPx)
Start-of-conversion can be initiated:
─ By software
─ By hardware triggers with configurable polarity (internal timer events from TIM1, TIM3, TIM14,
TIM15, TIM16 and TIM17)
Conversion modes
Figure 13-1 shows the ADC block diagram and Table 13-2 gives the ADC pin description.
VDDA VSSA
VREFSEL[1:0]
Internal reference
voltage selection
Reserved
AREADY CPU
EOSMP ADC interrupt
EOSEQ IRQ
2.5 V
EOC
OVR
VREFEN AWD master
Internal reference
A
voltage enable DATA[11:0 AHB H
AUTOFF to slave DMA
B
SCANDIR Auto off mode APB master
Up/down ADEN/ADDIS
CH_SEL[21:0] APB
CONT VREF+ VDD VREF- interface DMA request
Single/cont. Supply and reference
ADCAL
Self- DMAEN
VIOSHx calibration DMACFG
VOPx Input
selecion
VREFINT SMP[2:0] VIN SAR ADC
and scan
VTS control Sampling
ADC_IN[15:0] time
Analog input
channels AWD
Converted Analog
data watchdog
start
Start and
stop control AWDEN
ALIGN AWDSGL
AUTODLY Left/right AWDCH[4:0]
Auto-delayed RES[1:0] LT[11:0]
conv. ADSTART 12, 10, 8, 6bit
ADSTP HT[11:0]
Software
EXTSEL[2:0] trigger
Trigger
source
selection EXTEN[1:0] Hard
Trigger enable and ware
edge selection trigg Sample &hold
TIM1_TRGO DISCEN circuit
er
TIM1_CC4 Discontinuous
mode
TIM3_TRGO
TIM15_TRGO
IOSHx_SMPEN
IOSHx_AMPEN
PBx
+
VOPx
OP VIOSHx
EXTSEL[2:0]
IOSHx_SMPSEL
13.4.1. Calibration(ADCAL)
The ADC has a calibration feature. During the procedure, the ADC calculates a calibration factor which is
internally applied to the ADC until the next ADC power-off. The application must not use the ADC during
calibration and must wait until it is complete.
Calibration should be performed before starting A/D conversion. It removes the offset error which may vary
from chip to chip due to process variation.
The calibration is initiated by software by setting bit ADCAL=1. Calibration can only be initiated when the
ADC is disabled (when ADEN=0). ADCAL bit stays at 1 during all the calibration sequence. It is then cleared
by hardware as soon the calibration completes. After this, the calibration factor can be read from the
tCAB
ADCAL
by S/W by H/W
At MCU power-up, the ADC is disabled and put in power-down mode (ADEN=0).
As shown in Figure 13-3, the ADC needs a stabilization time of tSTAB before it starts converting accurately.
Two control bits are used to enable or disable the ADC:
Set ADEN=1 to enable the ADC. The ADRDY flag is set as soon as the ADC is ready for operation.
Set ADDIS=1 to disable the ADC and put the ADC in power down mode. The ADEN and ADDIS bits are
then automatically cleared by hardware as soon as the ADC is fully disabled.
Conversion can then start either by setting ADSTART=1 (Refer to section 13.5 Conversion on external
trigger and trigger polarity (EXTMOD, EXTEN, EXTSEL, RTENx, FTENx, EXTDLY)or when an external
trigger event occurs if triggers are enabled.
ADEN
tSTAB
ADRDY
ADDIS
by S/W by H/W
Note: In auto-off mode (AUTOFF=1) the power-on/off phases are performed automatically, by hardware and
the ADRDY flag is not set.
The ADC has a dual clock-___domain architecture, so that the ADC can be fed with a clock (ADC asynchronous
clock) independent from the APB clock (PCLK).
RCC ADITF
(Reset & Clock controller) APB
PCLK interface
Bits
CKMODE[1:0] of
ADC_CFGR2
Analog
/2 or /4 Others ADC_CK Analog
ADC
00
ADC
asynchronous Bits
clock CKMODE[1:0] of
ADC_CFGR2
1. Refer to Section7: RCC to see how PCLK and ADC asynchronous clock are enable
The input clock of the analog ADC can be selected between two different clock sources(See Figure 13-4 to
see how PCLK 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 (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”.
Option a) has the advantage of reaching the maximum ADC clock frequency whatever the APB clock
scheme selected.
Option b) has an uncertainty of the trigger instant is added by the resynchronizations between the two clock
domains.
Table 13-3 Latency between trigger and start of conversion
ADC clock source CKMODE[1:0] Latency between the trigger event and the start of conversion
Dedicated 14MHz 00
clock
Latency is not deterministic(jitter,2~3 * TADC)
PCLK divided by 2 01
PCLK divided by 4 10
Software must write to the ADCAL and ADEN bits in the ADC_CR register if the ADC is disabled (ADEN
must be 0).
Software must only write to the ADSTART and ADDIS bits in the ADC_CR register only if the ADC is enabled
and there is no pending request to disable the ADC (ADEN = 1 and ADDIS = 0).
For all the other control bits in the ADC_IER, ADC_CFGRi, ADC_SMPR, ADC_TR, ADC_CHSELR,
ADC_ETCR, ADC_RTENR, ADC_FTENR, ADC_CCR and ADC_CR2 registers, software must only write to
the configuration control bits if the ADC is enabled (ADEN = 1) and if there is no conversion ongoing
(ADSTART = 0).
Software must only write to the ADSTP bit in the ADC_CR register if the ADC is enabled (and possibly
converting) and there is no pending request to disable the ADC (ADSTART = 1 and ADDIS = 0)
Note: There is no hardware protection preventing software from making write operations forbidden by the
above rules. If such a forbidden write access occurs, the ADC may enter an undefined state. To recover
correct operation in this case, the ADC must be disabled (clear ADEN=0 and all the bits in the ADC_CR
register).
Before starting a conversion, the ADC needs to establish a direct connection between the voltage source to
be measured and the embedded sampling capacitor of the ADC. This sampling time must be enough for the
input voltage source to charge the sample and hold capacitor to the input voltage level.
Having a programmable sampling time allows to trim the conversion speed according to the input resistance
of the input voltage source.
The ADC samples the input voltage for a number of ADC clock cycles that can be modified using the
SMP[2:0] bits in the ADC_SMPR register.
ADC sampling can precisely stopped by configure TRGDISSMP bit, which can avoid the uncertainty of clock
___domain crossing. For more details please refer to the timing of I/O Sample and hold circuit
TRGDISSMP=0,
Programming sample time is general for every channel. The sampling time can be change by software at
each conversion if the application need.
TRGDISSMP=1,
For specify the sample top time of the measuring voltage source, the trigger source can stop the sample at a
fixed and certain time by set TRGDISSMP.
Software trigger will stop sampling immediately. Hardware trigger will stop sampling after the delay time set
by EXTDLY[9:0], and it can stopped immediately by set EXTDLY[9:0]=0.
The sample time of the first channel after triggering is forced to 1.5 × ADC clock cycles, but it is not sampling
in this stage because ADC sample is stopped. The programming sample time is effected excpt the first
channel.
In Single conversion mode, the ADC performs a single sequence of conversions, converting all the channels
once. This mode is selected when CONT=0 in the ADC_CFGR1 register. Conversion is started by either:
Setting the ADSTART bit in the ADC_CR register
Hardware trigger event
Inside the sequence, after each conversion is complete:
The converted data are stored in the 16-bit ADC_DR register, the IOSHx channel data are stored in the
ADC_IOSHxDR register.
The EOC (end of conversion) flag is set
An interrupt is generated if the EOCIE bit is set
After the sequence of conversions is complete:
The EOSEQ (end of sequence) flag is set
An interrupt is generated if the EOSEQIE bit is set
Then the ADC stops until a new external trigger event occurs or the ADSTART bit is set again.
Note: To convert a single channel, program a sequence with a length of 1.
In continuous conversion mode, when a software or hardware trigger event occurs, the ADC performs a
sequence of conversions, converting all the channels once and then automatically re-starts and
continuously performs the same sequence of conversions. This mode is selected when CONT=1 in the
ADC_CFGR1 register. Conversion is started by either:
Setting the ADSTART bit in the ADC_CR register
Hardware trigger event
Inside the sequence, after each conversion is complete:
The converted data are stored in the 16-bit ADC_DR register, the IOSHx channel data are stored in the
ADC_IOSHxDR register.
The EOC (end of conversion) flag is set
An interrupt is generated if the EOCIE bit is set
After the sequence of conversions is complete:
The EOSEQ (end of sequence) flag is set
An interrupt is generated if the EOSEQIE bit is set
Then, a new sequence restarts immediately and the ADC continuously repeats the conversion sequence.
13.4.10. Timing
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:
tADC = tSMPL + tSAR = [1.5 |min + 12.5 |12bit] × tADC_CLK
tADC = tSMPL + tSAR = [107.1 ns |min + 892.8 ns |12bit] = 1 μs |min (fADC_CLK = 14 MHz)
tSMPL(1) tSAR(2)
ADSTART set by SW
ADSTART(1)
tLATENCY(2)
(1) TRGDISSMP=0
(2) Trigger latency(Refer to datasheet for more details)
(3)ADC_DR register write latency(Refer to datasheet for more details)
The software can decide to stop any ongoing conversions by setting ADSTP=1 in the ADC_CR register.
This will reset the ADC operation and the ADC will be idle, ready for a new operation.
When the ADSTP bit is set by software, any ongoing conversion is aborted and the result is discarded
(ADC_DR register is not updated with the current conversion).
The scan sequence is also aborted and reset (meaning that restarting the ADC would re- start a new
sequence).
Once this procedure is complete, the ADSTP and ADSTART bits are both cleared by hardware and the
software must wait until ADSTART=0 before starting new conversions.
The I/O Sample and hold circuits mainly for measure the coefficient of the voltage divider circuit because the
ADC cannot measure immediately.
The I/O Sample and hold circuits need to built a connection between measured voltage source and
embedded smaple capacitance before starting holding(IOSHx_SMPEN=1,IOSHx_AMPEN=1). The sample
time should be enough to charge the sample and hold capacitance to the input voltage level according to the
input resistance. The sampled voltage will be held until the next sampling, and the conversion should be
proceed during this time.
Note: The input voltage of I/O Sample and hold circuit is limited, for more details please refer to the
characteristic table in datasheet.
IOSH EOC
PB1 VIOSH
Sample Hold ADC_IN18
VIN VSAMPLE ADC_DR
R1 Sample/Hold Convert
PB0
VIN ADC_IN8
R2
ADEN
tSTAB(1)
EXTEN 00
CONT
DISCEN
WAIT
AUTOFF
CHSEL 0x00040100
IOSH_AMPEN
tIOSH_SAMP(2) tIOSH_HOLD(3)
ADSTART
EOSEQ
tS(5) 12.5 * TAD tS_iosh(4) 12.5 * TAD
ADC state OFF Startup RDY SAMPLING (CH8) CONVERTING (CH8) SAMPLING (CH18) CONVERTING (CH18) RDY
Configure
1. Configure PB0, PB1 as analog input port;
2. Set ADC_CR.ADEN=1, and wait the ADC start stable time (tSTAB);
3. Configure ADC in single mode, no wait state, no auto shutdown, store the old data when the data is
overflow, scan sequence is CH8->CH18, configure ADC_CFGR1 &= ~(0x0001EC04)、 ADC_CHSELR
= 0x00040100;
4. ADC clock and ADC sample time should accord to application requirement and should satisfied 3
sample time which are defined in datasheet: sample time of ADC channel(PB0) (t S), IO(PB1) voltage
sample time(tIOSH_SAMP), ADC sample time of I/O Sample and hold circuit output (tS_iosh);
5. Set ADC_CR2 |= 0x00000300 to initiate I/O Sample and hold circuit and IO(PB1) voltage sample;
6. Set ADC_CR.ADSTART=1 to initiate ADC single mode (CH7->CH20->CH21);
7. Then should wait ADC_ISR.EOSMP=1, set ADC_CR2.IOSH_SMPEN=0 to hold PB1 voltage;
A conversion or a sequence of conversion can be triggered either by software or by an external event (for
example timer capture). If the EXTEN[1:0] control bits are not equal to “00”, then external events are able to
trigger a conversion with the selected polarity. The trigger selection is effective once software has set bit
ADSTART=1.
Any hardware triggers which occur while a conversion is ongoing are ignored.
If bit ADSTART=0, any hardware triggers which occur are ignored.
Table 13-4 provides the correspondence between the EXTEN[1:0] values and the trigger polarity.
Table 13-4 Configuring the trigger polarity
Source EXTEN[1:0]
Trigger detection disabled 00
Detection on rising edge 01
Detection on falling edge 10
Detection on both rising and falling edges 11
Note: The polarity of the external trigger can be changed only when the ADC is not converting
The EXTSEL[2:0] control bits are used to select which of 8 possible events can trigger conversions.
Table 13-5 gives the possible external trigger for regular conversion.
Table 13-5 External triggers(EXTMOD=0)
Name Source EXTSEL[2:0]
TRG0 TIM1_TRGO 000
TRG1 TIM1_CC4 001
TRG2 Reserved 010
TRG3 TIM3_TRGO 011
TRG4 TIM15_TRGO 100
TRG5 Reserved 101
TRG6 Reserved 110
TRG7 Reserved 111
Note: The trigger selection can be changed only when the ADC is not converting
This mode is enabled by setting the DISCEN bit in the ADC_CFGR1 register.
In this mode (DISCEN=1), a hardware or software trigger event is required to start each conversion defined
in the sequence. On the contrary, if DISCEN=0, a single hardware or software trigger event successively
starts all the conversions defined in the sequence.
Example:
DISCEN=1,channels to be converted = 0, 3, 7, 10
─ 1st trigger: channel 0 is converted and an EOC event is generated
─ 2nd trigger: channel 3 is converted and an EOC event is generated
─ 3rd trigger: channel 7 is converted and an EOC event is generated
─ 4th trigger: channel 10 is converted and both EOC and EOSEQ events are generated.
─ 5th trigger: channel 0 is converted an EOC event is generated
─ 6th trigger: channel 3 is converted and an EOC event is generated
─ ...
DISCEN=0, channels to be converted = 0, 3, 7, 10
─ 1st trigger: the complete sequence is converted: channel 0, then 3, 7 and 10. Each conversion
generates an EOC event and the last one also generates an EOSEQ event.
─ Any subsequent trigger events will restart the complete sequence
Note: It is not possible to have both discontinuous mode and continuous mode enabled: it is forbidden to set
both bits DISCEN=1 and CONT=1.
It is possible to obtain faster conversion times (tSAR) by reducing the ADC resolution.
The resolution can be configured to be either 12, 10, 8, or 6 bits by programming the RES[1:0] bits in the
ADC_CFGR1 register. Lower resolution allows faster conversion times for applications where high data
precision is not required.
Note: The RES[1:0] bit must only be changed when the ADEN bit is reset.
The result of the conversion is always 12 bits wide and any unused LSB bits are read as zeros.
Lower resolution reduces the conversion time needed for the successive approximation steps as shown in
Table 13-6
Table 13-6 tSAR timings depending on resolution
tSAR tSMPL(min) tCONV
RES[1:0] tSAR(ns)@ tCONV@
(ADC clock (ADC clock (ADC clock cycles)
bits fADC=14MHz fADC=14MHz
cycles) cycles) (with min. tSMPL)
12 12.5 893 ns 1.5 14 1000 ns
10 11.5 821 ns 1.5 13 928 ns
8 9.5 678 ns 1.5 11 785 ns
6 7.5 535 ns 1.5 9 643 ns
The ADC notifies the application of each end of sequence (EOSEQ) event.
The ADC sets the EOSEQ flag in the ADC_ISR register as soon as the last data result of a conversion
sequence is available in the ADC_DR register. An interrupt can be generated if the EOSEQIE bit is set in the
ADC_IER register. The EOSEQ flag is cleared by software by writing 1 to it.
ADSTART(1)
EOC
EOSEQ
SCANDIR
ADC state(2) RDY CH0 CH9 CH10 CH17 RDY CH17 CH10 CH9 CH0 RDY
by S/W by H/W
ADSTART(1)
EOC
EOSEQ
ADSTP
SCANDIR
ADC state(2) RDY CH0 CH9 CH10 CH17 CH0 CH9 CH10 STP RDY CH17 CH10
by S/W by H/W
ADSTART(1)
EOC
EOSEQ
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
Χ
ADSTART(1)
EOC
EOSEQ
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
Χ
ADC_IOSHxDR 中.At the end of each conversion (when an EOC event occurs), the result of the converted
data is stored in the ADC_DR data register which is 16-bit wide.
The format of the ADC_DR and ADC_IOSHxDR depends on the configured data alignment and resolution.
The ALIGN bit in the ADC_CFGR1 register selects the alignment of the data stored after conversion. Data
can be right-aligned (ALIGN=0) or left-aligned (ALIGN=1) as shown in Figure 13-14
A L IG N RES 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
0x 0 0x 0 D R [11:0]
0x 1 0x 00 D R [9:0]
0
0x 2 0x 00 D R [7:0]
0x 3 0x 000 D R [5:0]
0x 0 D R [11:0] 0x 0
0x 1 D R [9:0] 0x 00
1
0x 2 D R [7:0] 0x 00
0x 3 0x 00 D R [5:0] 0x 0
The overrun flag (OVR) indicates a data overrun event, when the converted data was not read in time by the
CPU or the DMA, before the data from a new conversion is available. The OVR flag will not affected by the
converted data of ADC_IOSHxDR register
The OVR flag is set in the ADC_ISR register if the EOC flag is still at ‘1’ at the time when a new conversion
completes. An interrupt can be generated if the OVRIE bit is set in the ADC_IER register.
When an overrun condition occurs, the ADC keeps operating and can continue to convert unless the
software decides to stop and reset the sequence by setting the ADSTP bit in the ADC_CR register.
The OVR flag is cleared by software by writing 1 to it.
It is possible to configure if the data is preserved or overwritten when an overrun event occurs by
programming the OVRMOD bit in the ADC_CFGR1 register, it is only effected for ADC_DR register and the
ADC_IOSHxDR is always store the last converted data.
OVRMOD=0
─ An overrun event preserves the data register from being overwritten: the old data is maintained
and the new conversion is discarded. If OVR remains at 1, further conversions can be performed
but the resulting data is discarded.
OVRMOD=1
─ The data register is overwritten with the last conversion result and the previous unread data is lost.
If OVR remains at 1, further conversions can be performed and the ADC_DR register always
contains the data from the latest conversion.
ADSTART(1)
EOC
EOSEQ
OVR
ADSTP
TRGx(1)
ADC state(2) RDY CH0 CH1 CH2 CH0 CH1 CH2 CH0 STOP RDY
OVERRUN
ADC_DR
Read Access
ADC_DR
D0 D1 D2 D0
(OVRMOD=0)
ADC_DR
D0 D1 D2 D0 D1 D2
(OVRMOD=1)
by S/W by H/W
triggered ignored
Χ
If the conversions are slow enough, the conversion sequence can be handled by software. In this case the
software must use the EOC flag and its associated interrupt to handle each data result. Each time a
conversion is complete, the EOC bit is set in the ADC_ISR register and the ADC_DR register can be read.
The OVRMOD bit in the ADC_CFGR1 register should be configured to 0 to manage overrun events as an
error.
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.
Since all converted channel values are stored in a single data register, it is efficient to use DMA when
converting more than one channel. This avoids losing the conversion data results stored in the ADC_DR
register.
When DMA mode is enabled (DMAEN bit set to 1 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
13.6.2:ADC overrun(OVR,OVRMOD)).
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 or double buffer mode.
When the DMA transfer is complete (all the transfers configured in the DMA controller have been done):
The content of the ADC data register is frozen.
Any ongoing conversion is aborted and its partial result discarded
No new DMA request is issued to the DMA controller. This avoids generating an overrun error if there
are still conversions which are started.
The scan sequence is stopped and reset
The DMA is stopped
Wait mode conversion can be used to simplify the software as well as optimizing the performance of
applications clocked at low frequency where there might be a risk of ADC overrun occurring.
When the WAIT bit is set to 1 in the ADC_CFGR1 register, a new conversion can start only if the previous
data has been treated, once the ADC_DR register has been read or if the EOC bit has been cleared.
This is a way to automatically adapt the speed of the ADC to the speed of the system that reads the data.
Note:Any hardware triggers which occur while a conversion is ongoing or during the wait time preceding the
read access are ignored.
ADSTART
EOC
EOSEQ
ADSTP
ADC_DR
Read Access
ADC state RDY CH0 DLY CH1 DLY CH2 DLY CH0 DLY STOP RDY
ADC_DR D0 D1 D2 D0
by S/W by H/W
The ADC has an automatic power management feature which is called auto-off mode, and is enabled by
setting AUTOFF=1 in the ADC_CFGR1 register.
When AUTOFF=1, the ADC is always powered off when not converting and automatically wakes-up when a
conversion is started (by software or hardware trigger). A startup-time is automatically inserted between the
trigger event which starts the conversion and the sampling time of the ADC. The ADC is then automatically
disabled once the sequence of conversions is complete.
Auto-off mode can cause a dramatic reduction in the power consumption of applications which need
relatively few conversions or when conversion requests are timed far enough apart (for example with a low
frequency hardware trigger) to justify the extra power and extra time used for switching the ADC on and off.
Auto-off mode can be combined with the wait mode conversion (WAIT=1) for applications clocked at low
frequency. This combination can provide significant power savings if the ADC
Note: Please refer to the section 7 RCC for the description of how to manage the dedicated 14 MHz internal
oscillator. The ADC interface can automatically switch ON/OFF the 14 MHz internal oscillator to save power.
TRGx
EOC
EOSEQ
ADC_DR
Read Access
ADC state RDY Startup CH0 CH1 CH2 CH3 OFF Startup
ADC_DR D0 D1 D2 D3
by S/W by H/W
triggered ignored
Χ
1. EXTMOD=0, EXTSEL=TRGx, EXTEN=01(Rising edge), CONT=x, ADSTART=1, CHSEL=0xF, SCANDIR=0, WAIT=0, AUTOFF=1
TRGx
EOC
EOSEQ
ADC_DR
Read Access
DLY DLY DLY DLY
ADC state RDY Startup CH0 OFF Startup CH1 OFF Startup CH2 OFF Startup CH0 OFF
ADC_DR D0 D1 D2 D0
by S/W by H/W
triggered ignored
Χ
The AWD analog watchdog feature is enabled by setting the AWDEN bit in the ADC_CFGR1 register. It is
used to monitor that either one selected channel or all enabled channels (see Table 13-8 analog watchdog
channel selection) remain within a configured voltage range (window) as shown in Figure 13-19
The AWD analog watchdog status bit is set if the analog voltage converted by the ADC is below a lower
threshold or above a higher threshold. These thresholds are programmed in the 12 least significant bits of
the ADC_HTR and ADC_LTR 16-bit registers. An interrupt can be enabled by setting the AWDIE bit in the
ADC_IER register.
The AWD flag is cleared by software by writing 1 to it.
When converting a data with a resolution of less than 12-bit (according to bits DRES[1:0]), the LSB of the
programmed thresholds must be kept cleared because the internal comparison is always performed on the
full 12-bit raw converted data (left aligned).
Table 13-7 describes how the comparison is performed for all the possible resolutions.
Table 13-7 Analog watchdog comparison
Resolution Analog Watchdog comparison between:
bits Raw converted data, left Comments
(1) Thresholds
RES[1:0] aligned
00: 12-bit DATA[11:0] LT[11:0] and -
HT[11:0]
01: 10-bit DATA[11:2],00 LT[11:0] and The user must configure LT[1:0] and
HT[11:0] HT[1:0] to “00”
10: 8-bit DATA[11:4],0000 LT[11:0] and The user must configure LT[3:0] and
HT[11:0] HT[3:0] to “0000”
11: 6-bit DATA[11:6],000000 LT[11:0] and The user must configure LT[5:0] and
HT[11:0] HT[5:0] to “000000”
1. The watchdog comparison is performed on the raw converted data before any alignment calculation
Table 13-8 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
Higher
thershold
HTR
Guarded area
Lower
threshold
LTR
The temperature sensor can be used to measure the junction temperature (TJ) of the device. The
temperature sensor is internally connected to the ADC_IN16 input channel which is used to convert the
sensor’s output voltage to a digital value. The sampling time for the temperature sensor analog pin must be
greater than the minimum TS_temp value specified in the datasheet. When not in use, the sensor can be put
in power down mode.
The temperature sensor output voltage changes linearly with temperature, however its characteristics may
vary significantly from chip to chip due to the process variations. To improve the accuracy of the temperature
sensor (especially for absolute temperature measurement), calibration values are individually measured for
each part by ST during production test and stored in the system memory area. Refer to the specific device
datasheet for additional information.
The internal voltage reference (VREFINT) provides a stable (bandgap) voltage output for the ADC and
Comparators. VREFINT is internally connected to the ADC_IN17 input channel. The precise voltage of VREFINT
is individually measured for each part by FMD during production test and stored in the system memory area.,
and access by read only mode.
Figure 13-20 shows the block diagram of connections between the temperature sensor, the internal voltage
reference and the ADC.
The TSEN bit must be set to enable the conversion of ADC_IN16 (temperature sensor) and the INTVREFEN
bit must be set to enable the conversion of ADC_IN17 (VREFINT).
Temperature VSENSE
ADC_IN16
sensor
Internal VREFINT
ADC_IN17
power block
110 C - 25 C
Temperatur e( C ) TS_DATA TS_CAL1 25 C
TS_CAL2 - TS_CAL1
Where:
TS_CAL2 is the temperature sensor calibration value acquired at 110°C, stored in the system
Note: The sensor has a startup time after waking from power down mode before it can output V SENSE 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 microcontroller may be subject to variation or not precisely
known. The embedded internal voltage reference (V REFINT) and its calibration data acquired by the ADC
during the manufacturing process at VDDA = 3.3 V can be used to evaluate the actual VDDA voltage level.
The following formula gives the actual VDDA voltage supplying the device:
Where:
VREFINT_CAL is the VREFINT calibration value, stored in the system memory area: 0x1FFF F7BA - 0x1FFF
F7BB
VREFINT_DATA is the actual VREFINT output value converted by ADC
V DDA
V CHANNELx ADC_DATAx
FULL_SCALE
For applications where VDDA value is not known, you must use the internal voltage reference and V DDA can
be replaced by the expression provided in the section Calculating the actual VDDA voltage using the internal
reference voltage resulting in the following formula:
3.3V V REFINT_CAL ADC_DATAx
V CHANNELx
V REFINT_DAT A
FULL_SCALE
Where:
VREFINT_CAL is the VREFINT calibration value, stored in the system memory area: 0x1FFF F7BA - 0x1FFF
F7BB
ADC_DATAx is the value measured by the ADC on channel x (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 will
be 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.
By default, ADC_CR2.VREFEN = 0, Internal reference voltage is disabled, the reference voltage of ADC is
VDDA.
Configure the VREFSEL bit to choose the level of the internal reference voltage: 0.625V, 1.5V, 2.5V.
Then set VREFEN after configured the VREFSEL bit , and ADC reference voltage will be internal reference
voltage.
Note: ADC reference voltage should be configured when the ADC is not proceeding the conversion, and it
has startup stable time, for more details please refer to ADC electrical characteristic
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
Offset Register
9
8
AWD 7
6
5
OVR 4
EOSEQIE EOSEQ3
EOC 2
EOSMPIE EOSMP1
ADRDYIE ADRDY0
ADC_ISR –
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x00
Reset x x x x x x x x x x x x x x x x x x x x x x x x 0 x x 0 0 0 0 0
AWDIE
OVRIE
EOCIE
ADC_IER
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x04
Reset x x x x x x x x x x x x x x x x x x x x x x x x 0 x x 0 0 0 0 0
ADSTART
ADSTP
ADCAL
ADDIS
ADEN
ADC_CR
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x08
Reset 0 x x x x x x x x x x x x x x x x x x x x x x x x x x 0 x 0 0 0
OVRMOD
SCANDIR
AWDSGL
DMACFG
AUTOFF
DISCEN
AWDEN
DMAEN
EXTEN
ALIGN
CONT
WAIT
EXTSEL RES
[1:0]
ADC_CFGR1 AWDCH[4:0]
–
–
–
–
–
–
–
–
–
0x0C [2:0] [1:0]
Reset x 0 0 0 0 0 x x 0 0 x x x x x 0 0 0 0 0 0 0 x 0 0 0 0 0 0 0 0 0
CKMODE
[1:0]
ADC_CFGR2
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x10
Reset 0 0 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
SMP
ADC_SMPR
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x14 [2:0]
Reset x x x x x x x x x x x x x x x x x x x x x x x x x x x x x 0 0 0
ADC_TR HT[11:0] LT[1:0]
–
–
–
–
–
–
–
–
0x20
Reset x x x x 1 1 1 1 1 1 1 1 1 1 1 1 x x x x 0 0 0 0 0 0 0 0 0 0 0 0
CHSEL19
CHSEL18
CHSEL17
CHSEL16
CHSEL15
CHSEL14
CHSEL13
CHSEL12
CHSEL11
CHSEL10
CHSEL9
CHSEL8
CHSEL7
CHSEL6
CHSEL5
CHSEL4
CHSEL3
CHSEL2
CHSEL1
CHSEL0
ADC_CHSELR
–
–
–
–
–
–
–
–
–
–
–
–
0x28
Reset x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ADC_DR DATA[15:0]
–
–
–
–
–
–
–
–
–
INTVREFEN –
–
–
–
–
–
–
0x40
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TSEN
ADC_CCR
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x308
Reset x x x x x x x x 0 0 x x x x x x x x x x x x x x x x x x x x x x
VREF_DECIB
IO_SMPEN
IO_AMPEN
VREFSEL
VREFEN
[1:0]
ADC_CR2
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x30C
Reset x x x x x x x x x x x x x x x x x x x x x x 0 0 x x x 1 0 0 0 x
13.12.1. ADC_ISR
Address offset:0x00
Reset value:0x0000 0000
0:Channel conversion not complete (or the flag event was already
acknowledged and cleared by software)
1:Channel conversion complete
1 EOSMP End of sampling flag
This bit is set by hardware during the conversion, at the end of the sampling
phase.
0: Not at the end of the sampling phase (or the flag event was already
acknowledged and cleared by software)
1:End of sampling phase reached
0 ADRDY ADC ready
This bit is set by hardware after the ADC has been enabled (bit ADEN=1)
and when the ADC reaches a state where it is ready to accept conversion
requests. It is cleared by software writing 1 to it.
0:ADC not yet ready to start conversion (or the flag event was already
acknowledged and cleared by software)
1:ADC is ready to start conversion
Note: In auto-off mode(AUTOFF=1), ADRDY will not be set, power on/off is
executed by hardware
13.12.2. ADC_IER
13.12.3. ADC_CR
13.12.4. ADC_CFGR1
Note: Software is allowed to write these bits only when ADSTART=0 (which
ensures that no conversion is ongoing). Only effected when EXTMOD=0
9 NA Reserved
8:6 EXTSEL[2:0] External trigger selection
These bits select the external event used to trigger the start of conversion
(Refer to Table 13-5. External trigger selection(EXTMOD=0))
Note: Software is allowed to write these bits only when ADSTART=0 (which
ensures that no conversion is ongoing). Only effected when EXTMOD=0 and
EXTEN≠00.
5 ALIGN Data alignment
This bit is set and cleared by software to select right or left alignment. Refer
to Figure 13-14. Data alignment and resolution
0:Right alignment
1:Left alignment
Note: Software is allowed to write these bits only when ADSTART=0 (which
ensures that no conversion is ongoing).
4:3 RES[1:0] Data resolution
These bits are written by software to select the resolution of the conversion.
00:12 bits
01:10 bits
10:8 bits
11:6 bits
Note: Software is allowed to write these bits only when ADEN=0
2 SCANDIR Scan sequence direction
This bit is set and cleared by software to select the direction in which the
channels will be scanned in the sequence.
0:Upward scan(from CHSEL0 to CHSEL21)
1:Backward scan(from CHSEL21 to CHSEL0)
Note: Software is allowed to write these bits only when ADSTART=0 (which
ensures that no conversion is ongoing).
1 DMACFG Direct memory access configuration
This bit is set and cleared by software to select between two DMA modes of
operation and is effective only when DMAEN=1.
0:DMA one shot mode selected
1:DMA circular mode selected
For more details, refer to section 13.6.5:Managing converted data using the
DMA
Note: Software is allowed to write these bits only when ADSTART=0 (which
ensures that no conversion is ongoing).
0 DMAEN Direct memory access enable
This bit is set and cleared by software to enable the generation of DMA
requests. This allows to use the DMA controller to manage automatically the
converted data. For more details, refer to section 13.6.5:Managing
13.12.5. ADC_CFGR2
13.12.6. ADC_SMPR
Address: 0x14
Reset value: 0x0000 0000
13.12.7. ADC_TR
These bits are written by software to define the higher threshold for the
analog watchdog. Refer to section 13.8:Analog window watchdog
(AWDEN,AWDSGL, AWDCH, AWD_HTR/AWD_LTR, AWD).
Note: Software is allowed to write these bits only when ADSTART=0 (which
ensures that no conversion is ongoing).
15:12 NA Reserved
11:0 LT[11:0] Analog watchdog lower threshold
These bits are written by software to define the lower threshold for the
analog watchdog. Refer to section 13.8:Analog window watchdog
(AWDEN,AWDSGL, AWDCH, AWD_HTR/AWD_LTR, AWD).
Note: Software is allowed to write these bits only when ADSTART=0 (which
ensures that no conversion is ongoing).
13.12.8. ADC_CHSELR
13.12.9. ADC_DR
13.12.10. ADC_CCR
13.12.11. ADC_CR2
14. Comparator
14.1. Introduction
The FT32F0xxx8 device embed two general purpose comparators COMP1, 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:
- Wake-up from low-power mode triggered by an analog signal
- Analog signal conditioning
- Cycle-by-cycle current control loop when combined with the DAC and a PWM output from a timer.
14.3.1. Introduction
PAD_PA13
COMP inetrrupt request (to EXTI)
COMP1VIPSEL <2:0>= 100&& COMP1EN ==1'b1
PAD_PB12
COMP1OUT( VDDL )
COMP1VINSEL<1:0> = 11&& COMP1EN ==1'b1 COMP1 D
SE
T
Q D
SE
T
Q PRDATA
PAD_PA5 CLK CL
R Q CL
R Q
DAC
tim1_brkin
COMP2VINSEL<1:0> = 00&&COMP2EN ==1'b1 tim1_ch1in
tim3_ch1in
COMP2VINSEL<1:0> = 01&&COMP2EN ==1'b1
tim3_clrocrf
PAD_PA2
COMP2VINSEL<1:0> = 10&&COMP2EN ==1'b1
COMP2OUT( VDDL )
COMP2 D
SE
T
Q D
SE
T
Q PRDATA
CLK
CL
R Q CL
R Q
WNDWEN == 1
COMP2VIPSEL <1:0>= 00&COMP2EN ==1'b1
PAD_PA3
The I/Os used as comparators inputs must be configured in analog mode in the GPIOs registers.
The comparator output can be connected to the I/Os using the alternate function channel given in “Alternate
function mapping” table in the datasheet.
The output can also be internally redirected to a variety of timer input for the following purposes:
Capture event
OCref_clr event
Input break event
The COMP clock provided by the clock controller is synchronous with the PCLK (APB clock).
There is no clock enable control bit provided in the RCC controller. clock enable bit is common for COMP
and SYSCFG.
There is no clock reset control bit provided in the RCC controller. COMP can only reset by system reset .
Note: The polarity selection logic and the output redirection to the port works independently from the PCLK
clock. This allows the comparator to work even in Stop mode.
The comparators can be used for safety purposes, such as over-current or thermal protection. For
applications having specific functional safety requirements, it is necessary to insure that the comparator
programming cannot be altered in case of spurious register access or program counter corruption. For this
purpose, the comparator control and status registers can be read- only and cnnot be wrote.
Once the programming is completed, if the COMPxLOCK bit be set to 1. this causes the whole COMP
register to become read-only until the next MCU reset
The comparator outputs are internally connected to the EXTI controller, Each comparator has its own EXTI
line and can generate either interrupts or events. The same mechanism is used to exit from low-power
modes.
Digital inputs are converted to the analog output voltages on each DAC channel pin are determined by the
following equation:
DAC1_out=VDACREF* (DAC1DATA<6:0>+1)/128 (0x20 ≤ DAC1DATA<6:0> ≤ 0x7F)
DAC2_out=VDACREF * (DAC2DATA<6:0>)/128 (0x00 ≤ DAC2DATA<6:0> ≤ 0x5F)
30
29
28
27
26
COMP2OUTSEL 25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
Offset Register
COMP1OUTSEL 9
0
COMP2VINSEL
COMP2VIPSEL
COMP1VINSEL
COMP1VIPSEL
COMP2LOCK
COMP1LOCK
COMP2OUT
COMP1OUT
COMP2POL
COMP1POL
COMP2EN
COMP1EN
COMPCSR
WNDWEN
–
–
0x1C
Reset 0 0 x x 0 0 0 0 0 x x 0 1 0 0 0 0 0 x x 0 0 0 0 x x 0 1 0 0 1 0
DACREFSEL
DACCTRL
–
–
0x20
DACEN
Reset x x x x x x x x x x x x x x x x x x x x x x x x x x x x x 0 0 0
DAC1DATA DAC1DATA
–
–
0x24
Reset x x x x x x x x x x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0
DAC2DATA DAC2DATA
–
–
0x28
Reset x x x x x x x x x x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0
14.4.1. COMPCSR
0 : disable
1 : enable
22 NA Reserved
21:20 COMP2VINSEL Comparator 2 negative input selection (default: 01)
00: DAC2_OUT
01: PAD PA2
10: PAD PA4
11: PAD PA5
19:17 COMP2VIPSEL Comparator 2 positive input selection, preferential select window mode
when the window comparator is enabled
000: PAD PA3
001: PAD PA4
010: PAD PA15
011: PAD PB1
100: Operational amplifier output 2 output
101: PAD PA1
110: Operational amplifier output 1 output
16 COMP2EN Comparator 2 enable
0 : disable
1 : enable
15 COMP1LOCK Comparator 1 lock
It can only be cleared by a system reset.
0: COMP_CSR[15:0] bits are read-write
1: COMP_CSR[15:0] bits are read-only.
14 COMP1OUT Comparator 1 output polarity
0: Output is not inverted
1: Output is inverted
13:12 NA Reserved
11 COMP1POL Comparator 1 output polarity
0: Output is not inverted
1: Output is inverted
10:8 COMP1OUTSEL Comparator 1 output selection
000: no selection
001: timer1 break input
010: timer1 input capture 1
011: timer1 Ocrefclear input
100: no selection
101: no selection
110: timer3 input capture 1
111: timer3 Ocrefclear input
7:6 NA Reserved
5:4 COMP1VINSEL Comparator 1 negative input selection (default: 01)
00: DAC1_OUT
14.4.2. DACCTRL
14.4.3. DAC1DATA
14.4.4. DAC2DATA
RF(selectable)
PA2 Rin
Rin
OP0TM
ADC
VSSA
PA0
PA1
OP0OUT
Input offset voltage can be calibrated by software and the procedure is:
1. Set OpxON to 1, turn on the Operational amplifier;
2. Set OpxTM to 1, Operational amplifier in input offset calibration mode;
3. Set OPxNSEL<1:0> to 2’b00, Negative input connect to GND;
4. Set OPxFCAPE to 1;
RF(selectable)
PA2 Rin
Rin
OP0TM
ADC
VSSA
PA0
PA1
OP0OUT
offset Register
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
OP0FCAPE 9
0
OP0PSEL
OPTODIG
OPTOIO
OP0OUT
OP0ON
OP0TM
OP0NSEL
OP_CR OP0FR[2:0] OP0COF[4:0]
-
-
0x30 [1:0]
Reset x x x x x x x x x x x x x x x 0 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0
Note:
1. The base address is common for Operational amplifier and SYSCFG, SYSCFG clock should be enabled
if Operational amplifier is need to use.
2. Operational amplifier can only reset by system reset
15.2.1. OP_CR
1: OP0 as comparator
0: OP0 as not comparator
8 OP0TM OP0 input offset calibration
1: Input offset calibration mode
0: Normal mode
7 OPTODIG OP0 output to register
1: Enables
0: Disables
6 OPTOIO OP0 output to PA0
1: Enables
0: Disables
5:1 OP0COF OP0 input offset calibration
0 OP0ON OP0 enable
1: Enables
0: Disables
The advanced-control timers (TIM1) consist of a 16-bit auto-reload counter driven by a programmable
prescaler.
It may be used for a variety of purposes, including measuring the pulse lengths of input signals (input
capture) or generating output waveforms (output compare, PWM, complementary PWM with dead-time
insertion).
Pulse lengths and waveform periods can be modulated from a few microseconds to several milliseconds
using the timer prescaler and the RCC clock controller prescalers.
The advanced-control (TIM1) and general-purpose (TIMx) timers are completely independent, and do not
share any resources. They can be synchronized together as described in Section 16.3.20.
Internal Clock(CK_INT)
CK_TIM from RCC
ETRP Trigger
ETR Polarity Selection & Edge ETRF Controller TRGO
TIM1_ETR Input Filter
Detector & Prescaler to other timers
to ADC
ITR0
ITR1 ITR TGI Reset, Enable, Up/Down, Count
ITR2 Slave
ITR3 TRC TRGI Mode
Controller
TI1F_ED
TI1FP1 Encoder
TI1FP2 Interface
REP register
Auto-reload register
Repetition
Stop,clear or up/down counter
CK_PSC PSC CK_CNT
+/- CNT counter
prescaler
DTG registers
CC1I UEV
XOR CC1I
TI1FP1 OC1 TIM1_CH1
TIM1_CH1 TI1 Input Filter &
TI1FP2 IC1 IC1PS OC1REF output
Edge Prescaler Capture/Compare 1 Register DTG
control OC1N TIM1_CH1N
detector TRC
CC2I UEV
CC2I
OC2 TIM1_CH2
TIM1_CH2 TI2 Input Filter & TI2FP1 IC2 OC2REF output
Edge TI2FP2 Prescaler IC2PS Capture/Compare 2 Register DTG
control OC2N TIM1_CH2N
detector TRC
CC3I UEV
CC3I
TI3FP3 OC3 TIM1_CH3
TIM1_CH3 TI3 Input Filter & IC3 OC3REF output
Prescaler IC3PS
TI3FP4 DTG
Edge Capture/Compare 3 Register
control OC3N TIM1_CH3N
detector
TRC
CC4I UEV
CC4I
OC4 TIM1_CH4
TIM1_CH4 TI4 Input Filter & TI4FP3 IC4 IC4PS OC4REF output
Edge TI4FP4 Prescaler Capture/Compare 4 Register control
detector TRC
ETRF
TIM1_BKIN BRK Polarity BI
Selection
Notes:
Preload registers transferred
Reg
to active registers on UEV event
according to control bit
event
The main block of the programmable advanced-control timer is a 16-bit counter with its related auto-reload
register. The counter can count up, down or both up and down. The counter clock can be divided by a
prescaler.
The counter, the auto-reload register and the prescaler register can be written or read by software. This is
true even when the counter is running.
The time-base unit includes:
Counter register(TIMx_CNT)
Prescaler register(TIMx_PSC)
Auto-reload register(TIMx_ARR)
Repetition counter register(TIMx_RCR)
The auto-reload register is preloaded. Writing to or reading from the auto-reload register accesses the
preload register. The content of the preload register are transferred into the shadow register permanently or
at each update event (UEV), depending on the auto-reload preload enable bit (ARPE) in TIMx_CR1 register.
The update event is sent when the counter reaches the overflow (or underflow when downcounting) and if
the UDIS bit equals 0 in the TIMx_CR1 register. It can also be generated by software. The generation of the
update event is described in detailed for each configuration.
The counter is clocked by the prescaler output CK_CNT, which is enabled only when the counter enable bit
(CEN) in TIMx_CR1 register is set (refer also to the slave mode controller description to get more details on
counter enabling).
Note that the counter starts counting 1 clock cycle after setting the CEN bit in the TIMx_CR1 register.
Prescaler description
The prescaler can divide the counter clock frequency by any factor between 1 and 65536. It is based on a
16-bit counter controlled through a 16-bit register (in the TIMx_PSC register). It can be changed on the fly as
this control register is buffered. The new prescaler ratio is taken into account at the next update event.
Figure 16-2 and Figure 16-3 give some examples of the counter behavior when the prescaler ratio is
changed on the fly:
CK_PSC
CEN
CK_CNT
Counter register F7 F8 F9 FA FB FC 00 01 02 03
UEV
Prescaler buffer 0 1
Prescaler counter 0 0 1 0 1 0 1 0 1
Figure 16-2 Counter timing diagram with prescaler division change from 1 to 2
CK_PSC
CEN
CK_CNT
Counter register F7 F8 F9 FA FB FC 00 01
UEV
Prescaler buffer 0 3
Prescaler counter 0 0 1 2 3 0 1 2 3
Figure 16-3 Counter timing diagram with prescaler division change from 1 to 4
Upcounting mode
In upcounting mode, the counter counts from 0 to the auto-reload value (content of the TIMx_ARR register),
then restarts from 0 and generates a counter overflow event.
If the repetition counter is used, the update event (UEV) is generated after upcounting is repeated for the
number of times programmed in the repetition counter register (TIMx_RCR). Else the update event is
generated at each counter overflow.
Setting the UG bit in the TIMx_EGR register (by software or by using the slave mode controller) also
generates an update event.
The UEV event can be disabled by software by setting the UDIS bit in the TIMx_CR1 register. This is to
avoid updating the shadow registers while writing new values in the preload registers. Then no update event
occurs until the UDIS bit has been written to 0. However, the counter restarts from 0, as well as the counter
of the prescaler (but the prescale rate does not change). In addition, if the URS bit (update request selection)
in TIMx_CR1 register is set, setting the UG bit generates an update event UEV but without setting the UIF
flag (thus no interrupt or DMA request is sent). This is to avoid generating both update and capture interrupts
when clearing the counter on the capture event.
When an update event occurs, all the registers are updated and the update flag (UIF bit in TIMx_SR register)
is set (depending on the URS bit):
The repetition counter is reloaded with the content of TIMx_RCR register,
The auto-reload shadow register is updated with the preload value (TIMx_ARR),
The buffer of the prescaler is reloaded with the preload value (content of the TIMx_PSC register).
The following figures show some examples of the counter behavior for different clock frequencies when
TIMx_ARR=0x36.
CK_PSC
CNT_EN
CK_CNT
Counter register 31 32 33 34 35 36 00 01 02 03 04 05 06 07
Counter overflow
UEV
UIF
CK_PSC
CNT_EN
CK_CNT
Counter register 34 35 36 00 01 02 03
Counter overflow
UEV
UIF
CK_PSC
CNT_EN
CK_CNT
Counter register 35 36 00 01
Counter overflow
UEV
UIF
CK_PSC
CK_CNT
Counter register 35 36 00 01
Counter overflow
UEV
UIF
CEN
CK_CNT
Counter register 31 32 33 34 35 36 00 01 02 03 04 05 06 07
Counter overflow
UEV
UIF
CK_PSC
CEN
CK_CNT
Counter register F0 F1 F2 F3 F4 F5 00 01 02 03 04 05 06 07
Counter overflow
UEV
UIF
Downcounting mode
In downcounting mode, the counter counts from the auto-reload value (content of the TIMx_ARR register)
down to 0, then restarts from the auto-reload value and generates a counter underflow event.
If the repetition counter is used, the update event (UEV) is generated after downcounting is repeated for the
number of times programmed in the repetition counter register (TIMx_RCR). Else the update event is
generated at each counter underflow.
Setting the UG bit in the TIMx_EGR register (by software or by using the slave mode controller) also
generates an update event.
The UEV update event can be disabled by software by setting the UDIS bit in TIMx_CR1 register. This is to
avoid updating the shadow registers while writing new values in the preload registers. Then no update event
occurs until UDIS bit has been written to 0. However, the counter restarts from the current auto-reload value,
whereas the counter of the prescaler restarts from 0 (but the prescale rate 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 repetition counter is reloaded with the content of TIMx_RCR register
The buffer of the prescaler is reloaded with the preload value (content of the TIMx_PSC register)
The auto-reload active register is updated with the preload value (content of the TIMx_ARR register).
Note that the auto-reload is updated before the counter is reloaded, so that the next period is the
expected one
The following figures show some examples of the counter behavior for different clock frequencies when
TIMx_ARR=0x36.
CK_PSC
CNT_EN
CK_CNT
Counter register 05 04 03 02 01 00 36 35 34 33 32 31 30 2F
Counter underflow
UEV
UIF
CK_PSC
CNT_EN
CK_CNT
Counter register 02 01 00 36 35 34 33
Counter underflow
UEV
UIF
CK_PSC
CNT_EN
CK_CNT
Counter register 01 00 36 35
Counter underflow
UEV
UIF
CK_PSC
CK_CNT
Counter register 20 1F 00 36 01
Counter underflow
UEV
UIF
CK_PSC
CEN
CK_CNT
Counter register 05 04 03 02 01 00 36 35 34 33 32 31 30 2F
Counter underflow
UEV
UIF
Figure 16-14 Counter timing diagram, update event when repetition counter is not used
Center-aligned mode
In center-aligned mode, the counter counts from 0 to the auto-reload value (content of the TIMx_ARR
register) – 1, generates a counter overflow event, then counts from the auto-reload value down to 1 and
generates a counter underflow event. Then it restarts counting from 0.
Center-aligned mode is active when the CMS bits in TIMx_CR1 register are not equal to ‘00’. The Output
compare interrupt flag of channels configured in output is set when: the counter counts down (Center
aligned mode 1, CMS = “01”), the counter counts up (Center aligned mode 2, CMS = “10”) the counter
counts up and down (Center aligned mode 3, CMS = “11”).
In this mode, the DIR direction bit in the TIMx_CR1 register cannot be written. It is updated by hardware and
gives the current direction of the counter.
The update event can be generated at each counter overflow and at each counter underflow or by setting
the UG bit in the TIMx_EGR register (by software or by using the slave mode controller) also generates an
update event. In this case, the counter restarts counting from 0, as well as the counter of the prescaler.
The UEV update event can be disabled by software by setting the UDIS bit in the TIMx_CR1 register. This is
to avoid updating the shadow registers while writing new values in the preload registers. Then no update
event occurs until UDIS bit has been written to 0. However, the counter continues counting up and down,
based on the current auto-reload value.
In addition, if the URS bit (update request selection) in TIMx_CR1 register is set, setting the UG bit
generates an UEV update event but without setting the UIF flag (thus no interrupt or DMA request is sent).
This is to avoid generating both update and capture interrupts when clearing the counter on the capture
event.
When an update event occurs, all the registers are updated and the update flag (UIF bit in TIMx_SR register)
is set (depending on the URS bit):
The repetition counter is reloaded with the content of TIMx_RCR register
The buffer of the prescaler is reloaded with the preload value (content of the TIMx_PSC register)
The auto-reload active register is updated with the preload value (content of the TIMx_ARR register).
Note that if the update source is a counter overflow, the autoreload 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
CNT_EN
CK_CNT
Counter register 04 03 02 01 00 01 02 03 04 05 06 05 04 03
Counter underflow
Counter overflow
UEV
UIF
Figure 16-15 Counter timing diagram, internal clock divided by 1, TIMx_ARR = 0x6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
CK_PSC
CNT_EN
CK_CNT
Counter register 02 01 00 36 35 34 33
Counter underflow
UEV
UIF
CK_PSC
CNT_EN
CK_CNT
Counter register 35 36 36 35
Counter overflow
UEV
UIF
CK_PSC
CK_CNT
Counter register 20 1F 01 00 01
Counter underflow
UEV
UIF
CEN
CK_CNT
Counter register 06 05 04 03 02 01 00 01 02 03 04 05 06 07
Counter underflow
UEV
UIF
Figure 16-19 Counter timing diagram, update event with ARPE=1 (counter underflow)
CK_PSC
CEN
CK_CNT
Counter register F7 F8 F9 FA FB FC 36 35 34 33 32 31 30 2F
Counter overflow
UEV
UIF
Figure 16-20 Counter timing diagram, Update event with ARPE=1 (counter overflow)
Section 16.3.1: Time-base unit describes how the update event (UEV) is generated with respect to the
counter overflows/underflows. It is actually generated only when the repetition counter has reached zero.
This can be useful when generating PWM signals.
This means that data are transferred from the preload registers to the shadow registers (TIMx_ARR
auto-reload register, TIMx_PSC prescaler register, but also TIMx_CCRx capture/compare registers in
compare mode) every N counter overflows or underflows, where N is the value in the TIMx_RCR repetition
counter register.
The repetition counter is decremented:
At each counter overflow in upcounting mode,
At each counter underflow in downcounting mode,
At each counter overflow and at each counter underflow in center-aligned mode. Although this limits the
maximum number of repetition to 128 PWM cycles, it makes it possible to update the duty cycle twice
per PWM period. When refreshing compare registers only once per PWM period in center-aligned
mode, maximum resolution is 2xT ck, due to the symmetry of the pattern.
The repetition counter is an auto-reload type; the repetition rate is maintained as defined by the TIMx_RCR
register value. When the update event is generated by software (by setting the UG bit in TIMx_EGR register)
or by hardware through the slave mode controller, it occurs immediately whatever the value of the repetition
counter is and the repetition counter is reloaded with the content of the TIMx_RCR register.
Counter-aligned
Edge-aligned mode
mode
Upcounting Downcounting
TIMx_RCR=0 UEV
TIMx_RCR=1 UEV
TIMx_RCR=2 UEV
TIMx_RCR=3 UEV
TIMx_RCR=3
and UEV
re-synchronization
(by SW) (by SW) (by SW)
Update event: Preload registers transferred to active registers and update interrupt
UEV
generated
Figure 16-21 Update rate examples depending on mode and TIMx_RCR register settings
bits (in the TIMx_EGR register) are actual control bits and can be changed only by software (except UG
which remains cleared automatically). As soon as the CEN bit is written to 1, the prescaler is clocked by the
internal clock CK_INT.
Figure 16-22 shows the behavior of the control circuit and the upcounter in normal mode, without prescaler.
Interal clock
CNT_EN
UG
CNT_INIT
CK_CNT=CK_PSC
Counter register 31 32 33 34 35 36 00 01 02 03 04 05 06 07
TIMx_SMCR
TS[2:0]
T12F or
TI1F or Encoder
ITRx mode
0xx
TI1_ED
100
TRGI External
TI1FP1 clock mode 1
101 CK_PSC
TI2F_rising
TI2 Edge 0 TI2FP2
Filter TI2F_falling 1 110
Detector
ETRF External
111 ETRF
clock mode 2
ICF[3:0] CC2P
TIMx_CCMR1 TIMx_CCER
CK_INT Internal clock
(internal clock) mode
ECE SMS[2:0]
TIMx_SMCR
For example, to configure the upcounter to count in response to a rising edge on the TI2 input, use the
following procedure:
1. Configure channel 2 to detect rising edges on the TI2 input by writing CC2S = ‘01’ in the TIMx_CCMR1
register.
2. Configure the input filter duration by writing the IC2F[3:0] bits in the TIMx_CCMR1 register (if no filter is
needed, keep IC2F=0000).
3. Select rising edge polarity by writing CC2P=0 in the TIMx_CCER register.
4. Configure the timer in external clock mode 1 by writing SMS=111 in the TIMx_SMCR register.
5. Select TI2 as the trigger input source by writing TS=110 in the TIMx_SMCR register.
6. Enable the counter by writing CEN=1 in the TIMx_CR1 register.
The capture prescaler is not used for triggering, so you don’t need to configure it.
When a rising edge occurs on TI2, the counter counts once and the TIF flag is set.
The delay between the rising edge on TI2 and the actual clock of the counter is due to the resynchronization
circuit on TI2 input.
TI2
CEN
CK_CNT=CK_PSC
Counter register 34 35 36 0
TIF
Write TIF=0
T12F or
TI1F or Encoder
mode
TRGI External
clock mode 1 CK_PSC
fDTS
ETR Filter ETRF External
0 Divider
ETRP down-counter clock mode 2
1 /1,/2,/4,/8
3. Select rising edge detection on the ETR pin by writing ETP=0 in the TIMx_SMCR register.
4. Enable external clock mode 2 by writing ECE=1 in the TIMx_SMCR register.
5. Enable the counter by writing CEN=1 in the TIMx_CR1 register.
The counter counts once each 2 ETR rising edges.
The delay between the rising edge on ETR and the actual clock of the counter is due to the
resynchronization circuit on the ETRP signal.
fCK_INT
CNT_EN
del ay
del ay
ETR
ETRP
ETRF
CK_INT=CK_PSC
COUNTER 34 35 36
Each Capture/Compare channel is built around a capture/compare register (including a shadow register), a
input stage for capture (with digital filter, multiplexing and prescaler) and an output stage (with comparator
and output control).
Figure 16-27 to Figure 16-30 give an overview of one Capture/Compare channel.
The input stage samples the corresponding Tix input to generate a filtered signal TixF. Then, an edge
detector with polarity selection generates a signal (TixFPx) which can be used as trigger input by the slave
mode controller or as the capture command. It is prescaled before the capture register (IcxPS).
TI1F_ED
To the slave mode controller
TI1 TI1F_Rising
Filter Edge 0 TI1FP1
fDTS
downcounter detector TI1F_Falling 1
01
TI2FP1 IC1 Divider IC1PS
10
ICF[3:0] /1,/2,/4,/8
CC1P/CC1NP TRC
TIMx_CCMR1 11
TIMx_CCER (from slave mode
controller)
TI2F_Rising
(from channel 2) 0
CC1S[1:0] ICPS[1:0] CCIE
TI2F_Falling 1
TIMx_CCMR1 TIMx_CCER
(from channel 2)
APB Bus
MCU-peripheral interface
high 8
if 16-bit
low 8
TIMx_SMCR
OCCS
TIMx_SMCR
OCCS
OCREF_CLR
0
ETRF To the master
1 mode
controller
ocref_clr_int
‘0’
0
CNT>CCR4 Output 0 Output OC4
mode OC4REF enable
CNT=CCR4 1 1 circuit
controller
CC4P
TIMx_CCER CC4E TIMx_CCER
MOE OSSI TIMx_BDTR
OC2M[2:0] CC4E OIS4 TIMx_CR2
TIMx_CCMR1 TIMx_CCER
In Input capture mode, the Capture/Compare Registers (TIMx_CCRx) are used to latch the value of the
counter after a transition detected by the corresponding Icx signal. When a capture occurs, the
corresponding CCXIF flag (TIMx_SR register) is set and an interrupt or a DMA request can be sent if they
are enabled. If a capture occurs while the CcxIF flag was already high, then the over-capture flag CcxOF
(TIMx_SR register) is set. CcxIF can be cleared by software by writing it to ‘0’ or by reading the captured
data stored in the TIMx_CCRx register. CcxOF is cleared when you write it to ‘0’.
The following example shows how to capture the counter value in TIMx_CCR1 when TI1 input rises. To do
this, use the following procedure:
Select the active input: TIMx_CCR1 must be linked to the TI1 input, so write the CC1S bits to 01 in the
TIMx_CCMR1 register. As soon as CC1S becomes different from 00, the channel is configured in input
and the TIMx_CCR1 register becomes read-only.
Program the input filter duration you need with respect to the signal you connect to the timer when the
input is one of the Tix (IcxF bits in the TIMx_CCMRx register). Let’s imagine that, when toggling, the
input signal is not stable during at must 5 internal clock cycles. We must program a filter duration longer
than these 5 clock cycles. We can validate a transition on TI1 when 8 consecutive samples with the
new level have been detected (sampled at f DTS frequency). Then write IC1F bits to 0011 in the
TIMx_CCMR1 register.
Program the input prescaler. In our example, we wish the capture to be performed at each valid
transition, so the prescaler is disabled (write IC1PS bits to ‘00’ in the TIMx_CCMR1 register).
Enable capture from the counter into the capture register by setting the CC1E bit in the TIMx_CCER
register.
If needed, enable the related interrupt request by setting the CC1IE bit in the TIMx_DIER register,
and/or the DMA request by setting the CC1DE bit in the TIMx_DIER register.
When an input capture occurs:
The TIMx_CCR1 register gets the value of the counter on the active transition.
CC1IF flag is set (interrupt flag). CC1OF is also set if at least two consecutive captures occurred
whereas the flag was not cleared.
An interrupt is generated depending on the CC1IE bit.
A DMA request is generated depending on the CC1DE bit.
In order to handle the overcapture, it is recommended to read the data before the overcapture flag. This is to
avoid missing an overcapture which could happen after reading the flag and before reading the data.
Note: IC interrupt and/or DMA requests can be generated by software by setting the corresponding CCxG
bit in the TIMx_EGR register.
This mode is a particular case of input capture mode. The procedure is the same except:
Two Icx signals are mapped on the same Tix input.
These 2 Icx signals are active on edges with opposite polarity.
One of the two TixFP signals is selected as trigger input and the slave mode controller is configured in
reset mode.
For example, you can measure the period (in TIMx_CCR1 register) and the duty cycle (in TIMx_CCR2
register) of the PWM applied on TI1 using the following procedure (depending on CK_INT frequency and
prescaler value):
Select the active input for TIMx_CCR1: write the CC1S bits to 01 in the TIMx_CCMR1 register (TI1
selected).
Select the active polarity for TI1FP1 (used both for capture in TIMx_CCR1 and counter clear): write the
CC1P and CC1NP bits to ‘0’ (active on rising edge).
Select the active input for TIMx_CCR2: write the CC2S bits to 10 in the TIMx_CCMR1 register (TI1
selected).
Select the active polarity for TI1FP2 (used for capture in TIMx_CCR2): write the CC2P bit to ‘1’ (active
on falling edge).
Select the valid trigger input: write the TS bits to 101 in the TIMx_SMCR register (TI1FP1 selected).
Configure the slave mode controller in reset mode: write the SMS bits to 100 in the TIMx_SMCR
register.
Enable the captures: write the CC1E and CC2E bits to ‘1’ in the TIMx_CCER register.
TI1
IC1/IC2
COUNTER
5 0 1 2 3 4 5 0
T1CCR1 5
T1CCR2 3
In output mode (CCxS bits = 00 in the TIMx_CCMRx register), each output compare signal (OCxREF and
then Ocx/OcxN) can be forced to active or inactive level directly by software, independently of any
comparison between the output compare register and the counter.
To force an output compare signal (OCXREF/Ocx) to its active level, you just need to write 101 in the OcxM
bits in the corresponding TIMx_CCMRx register. Thus OCXREF is forced high (OCxREF is always active
high) and Ocx get opposite value to CCxP polarity bit.
For example: CCxP=0 (Ocx active high) => Ocx is forced to high level.
The OCxREF signal can be forced low by writing the OcxM bits to 100 in the TIMx_CCMRx register.
Anyway, the comparison between the TIMx_CCRx shadow register and the counter is still performed and
allows the flag to be set. Interrupt and DMA requests can be sent accordingly. This is described in the output
compare mode section below.
This function is used to control an output waveform or indicating when a period of time has elapsed.
When a match is found between the capture/compare register and the counter, the output compare function:
Assigns the corresponding output pin to a programmable value defined by the output compare mode
(OcxM bits in the TIMx_CCMRx register) and the output polarity (CCxP bit in the TIMx_CCER register).
The output pin can keep its level (OCXM=000), be set active (OcxM=001), be set inactive (OcxM=010)
or can toggle (OcxM=011) on match.
Sets a flag in the interrupt status register (CcxIF bit in the TIMx_SR register).
Generates an interrupt if the corresponding interrupt mask is set (CCXIE bit in the TIMx_DIER register).
Sends a DMA request if the corresponding enable bit is set (CcxDE bit in the TIMx_DIER register,
CCDS bit in the TIMx_CR2 register for the DMA request selection).
The TIMx_CCRx registers can be programmed with or without preload registers using the OcxPE bit in the
TIMx_CCMRx register.
In output compare mode, the update event UEV has no effect on OCxREF and Ocx output. The timing
resolution is one count of the counter. Output compare mode can also be used to output a single pulse (in
One Pulse mode).
Output compare mode procedure:
1. Select the counter clock (internal, external, prescaler).
2. Write the desired data in the TIMx_ARR and TIMx_CCRx registers.
3. Set the CcxIE bit if an interrupt request is to be generated.
4. Select the output mode. For example:
− Write OcxM = 011 to toggle Ocx output pin when CNT matches CCRx
− Write OcxPE = 0 to disable preload register
− Write CCxP = 0 to select active high polarity
− Write CcxE = 1 to enable the output
5. Enable the counter by setting the CEN bit in the TIMx_CR1 register.
The TIMx_CCRx register can be updated at any time by software to control the output waveform, provided
that the preload register is not enabled (OcxPE=’0’, else TIMx_CCRx shadow register is updated only at the
next update event UEV). An example is given in Figure 16-32.
oc1ref=OC1
Pulse Width Modulation mode allows you to generate a signal with a frequency determined by the value of
the TIMx_ARR register and a duty cycle determined by the value of the TIMx_CCRx register.
The PWM mode can be selected independently on each channel (one PWM per Ocx output) by writing ‘110’
(PWM mode 1) or ‘111’ (PWM mode 2) in the OcxM bits in the TIMx_CCMRx register. You must enable the
corresponding preload register by setting the OcxPE bit in the TIMx_CCMRx register, and eventually the
auto-reload preload register (in upcounting or center-aligned modes) by setting the ARPE bit in the
TIMx_CR1 register.
As the preload registers are transferred to the shadow registers only when an update event occurs, before
starting the counter, you have to initialize all the registers by setting the UG bit in the TIMx_EGR register.
Ocx polarity is software programmable using the CCxP bit in the TIMx_CCER register. It can be
programmed as active high or active low. Ocx output is enabled by a combination of the CcxE, CcxNE, MOE,
OSSI and OSSR bits (TIMx_CCER and TIMx_BDTR registers).
In PWM mode (1 or 2), TIMx_CNT and TIMx_CCRx are always compared to determine whether
TIMx_CCRx ≤ TIMx_CNT or TIMx_CNT ≤ TIMx_CCRx (depending on the direction of the counter).The timer
is able to generate PWM in edge-aligned mode or center-aligned mode depending on the CMS bits in the
TIMx_CR1 register.
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
Counter register 0 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 0 1 2
OCxREF
CCRx=4 CMS=01
CCxIF CMS=10
CMS=11
OCxREF
CCRx=7
CCxIF CMS=10 or 11
OCxREF
CCRx=8 CMS=01
CCxIF CMS=10
CMS=11
OCxREF
CCRx>8
CMS=01
CCxIF CMS=10
CMS=11
OCxREF
CCRx=0
CMS=01
CCxIF CMS=10
CMS=11
The advanced-control timers (TIM1) can output two complementary signals and manage the switching-off
and the switching-on instants of the outputs.
This time is generally known as dead-time and you have to adjust it depending on the devices you have
connected to the outputs and their characteristics (intrinsic delays of level-shifters, delays due to power
switches...)
You can select the polarity of the outputs (main output Ocx or complementary OcxN) independently for each
output. This is done by writing to the CCxP and CCxNP bits in the TIMx_CCER register.
The complementary signals Ocx and OcxN are activated by a combination of several control bits: the CcxE
and CcxNE bits in the TIMx_CCER register and the MOE, OISx, OISxN, OSSI and OSSR bits in the
TIMx_BDTR and TIMx_CR2 registers. Refer to table:Output control bits for complementary Ocx and OcxN
channels with break feature for more details. In particular, the dead-time is activated when switching to the
IDLE state (MOE falling down to 0).
Dead-time insertion is enabled by setting both CcxE and CcxNE bits, and the MOE bit if the break circuit is
present. There is one 10-bit dead-time generator for each channel. From a reference waveform OCxREF, it
generates 2 outputs Ocx and OcxN. If Ocx and OcxN are active high:
The Ocx output signal is the same as the reference signal except for the rising edge, which is delayed
relative to the reference rising edge.
The OcxN output signal is the opposite of the reference signal except for the rising edge, which is
delayed relative to the reference falling edge.
If the delay is greater than the width of the active output (Ocx or OcxN) then the corresponding pulse is not
generated.
The following figures show the relationships between the output signals of the dead-time generator and the
reference signal OCxREF. (we suppose CCxP=0, CCxNP=0, MOE=1, CcxE=1 and CcxNE=1 in these
examples)
OCxREF
delay
OCx
OCxN
delay
OCxREF
delay
OCx
OCxN
Figure 16-36 Dead-time waveforms with delay greater than the negative pulse.
OCxREF
OCx
delay
OCxN
Figure 16-37 Dead-time waveforms with delay greater than the positive pulse
The dead-time delay is the same for each of the channels and is programmable with the DTG bits in the
TIMx_BDTR register. Refer to Section: TIM1 break and dead-time register (TIM1_BDTR) for delay
calculation.
When using the break function, the output enable signals and inactive levels are modified according to
additional control bits (MOE, OSSI and OSSR bits in the TIMx_BDTR register, OISx and OISxN bits in the
TIMx_CR2 register). In any case, the Ocx and OcxN outputs cannot be set both to active level at a given
time. Refer to Table: Output control bits for complementary Ocx and OcxN channels with break feature for
more details.
Break source can be BKIN input pin or the internal break source:
Core LOCKUP output
PVD output
Failure is detected by the CSS
Comparator output
When exiting from reset, the break circuit is disabled and the MOE bit is low. You can enable the break
function by setting the BKE bit in the TIMx_BDTR register. The break input polarity can be selected by
configuring the BKP bit in the same register. BKE and BKP can be modified at the same time. When the BKE
and BKP bits are written, a delay of 1 APB clock cycle is applied before the writing is effective. Consequently,
it is necessary to wait 1 APB clock period to correctly read back the bit after the write operation.
Because MOE falling edge can be asynchronous, a resynchronization circuit has been inserted between the
actual signal (acting on the outputs) and the synchronous control bit (accessed in the TIMx_BDTR register).
It results in some delays between the asynchronous and the synchronous signals. In particular, if you write
MOE to 1 whereas it was low, you must insert a delay (dummy instruction) before reading it correctly. This is
because you write the asynchronous signal and read the synchronous signal.
When a break occurs (selected level on the break input):
The MOE bit is cleared asynchronously, putting the outputs in inactive state, idle state or in reset state
(selected by the OSSI bit). This feature functions even if the MCU oscillator is off.
Each output channel is driven with the level programmed in the OISx bit in the TIMx_CR2 register as
soon as MOE=0. If OSSI=0 then the timer releases the enable output else the enable output remains
high.
When complementary outputs are used:
− The outputs are first put in reset state inactive state (depending on the polarity). This is done
asynchronously so that it works even if no clock is provided to the timer.
− If the timer clock is still present, then the dead-time generator is reactivated in order to drive the
outputs with the level programmed in the OISx and OISxN bits after a dead-time. Even in this case,
Ocx and OcxN cannot be driven to their active level together. Note that because of the
resynchronization on MOE, the dead-time duration is a bit longer than usual (around 2 ck_tim
clock cycles).
− If OSSI=0 then the timer releases the enable outputs else the enable outputs remain or become
high as soon as one of the CcxE or CcxNE bits is high.
The break status flag (BIF bit in the TIMx_SR register) is set. An interrupt can be generated if the BIE
bit in the TIMx_DIER register is set.
If the AOE bit in the TIMx_BDTR register is set, the MOE bit is automatically set again at the next
update event UEV. This can be used to perform a regulation, for instance. Else, MOE remains low until
you write it to ‘1’ again. In this case, it can be used for security and you can connect the break input to
an alarm from power drivers, thermal sensors or any security components.
Note: The break inputs is acting on level. Thus, the MOE cannot be set while the break input is active
(neither automatically nor by software). In the meantime, the status flag BIF cannot be cleared.
The break can be generated by the BRK input which has a programmable polarity and an enable bit BKE in
the TIMx_BDTR Register.
In addition to the break input and the output management, a write protection has been implemented inside
the break circuit to safeguard the application. It allows you to freeze the configuration of several parameters
(dead-time duration, Ocx/OcxN polarities and state when disabled, OcxM configurations, break enable and
polarity). You can choose from 3 levels of protection selected by the LOCK bits in the TIMx_BDTR register.
Refer to Section 16.4.18: TIM1 break and dead-time register (TIM1_BDTR). The LOCK bits can be written
only once after an MCU reset.
The Figure 16-38 shows an example of behavior of the outputs in response to a break.
OCxREF
OCx
(OCxN not implemented,CCxP=0,OISx=1)
OCx
(OCxN not implemented,CCxP=0,OISx=0)
OCx
(OCxN not implemented,CCxP=1,OISx=1)
OCx
(OCxN not implemented,CCxP=1,OISx=0)
OCx
OCx
OCx
OCxN delay
(CCxE=1,CCxP=0,OISx=1,CCxNE=1,CCxNP=1,OISxN=1)
OCx
OCxN delay
(CCxE=1,CCxP=0,OISx=0,CCxNE=0,CCxNP=0,OISxN=1)
OCx
OCxN
(CCxE=1,CCxP=0,CCxNE=0,CCxNP=0,OISx=OISxN=0 or OISx=OISxN=1)
The OCxREF signal of a given channel can be cleared when a high level is applied on the
OCREF_CLR_INPUT (OcxCE enable bit in the corresponding TIMx_CCMRx register set to 1). OCxREF
remains low until the next update event (UEV) occurs. This function can only be used in Output compare
and PWM modes. It does not work in Forced mode.
OCREF_CLR_INPUT can be selected between the OCREF_CLR input and ETRF (ETR after the filter) by
configuring the OCCS bit in the TIMx_SMCR register.
For example, the OCxREF signal can be connected to the output of a comparator to be used for current
CCRx
Counter
ETRF
OCxREF
(OCxCE=0)
OCxREF
(OCxCE=0)
OCxREF_CLR OCxREF_CLR
become high still high
When complementary outputs are used on a channel, preload bits are available on the OcxM, CcxE and
CcxNE bits. The preload bits are transferred to the shadow bits at the COM commutation event. Thus you
can program in advance the configuration for the next step and change the configuration of all the channels
at the same time. COM can be generated by software by setting the COM bit in the TIMx_EGR register or by
hardware (on TRGI rising edge).
A flag is set when the COM event occurs (COMIF bit in the TIMx_SR register), which can generate an
interrupt (if the COMIE bit is set in the TIMx_DIER register) or a DMA request (if the COMDE bit is set in the
TIMx_DIER register).
The Figure 16-40 describes the behavior of the Ocx and OcxN outputs when a COM event occurs, in 3
different examples of programmed configurations.
CCRx
Counter
OCxREF
Write COM to 1
COM event
One-pulse mode (OPM) is a particular case of the previous modes. It allows the counter to be started in
response to a stimulus and to generate a pulse with a programmable length after a programmable delay.
Starting the counter can be controlled through the slave mode controller. Generating the waveform can be
done in output compare mode or PWM mode. You select One-pulse mode by setting the OPM bit in the
TIMx_CR1 register. This makes the counter stop automatically at the next update event UEV.
A pulse can be correctly generated only if the compare value is different from the counter initial value. Before
starting (when the timer is waiting for the trigger), the configuration must be:
In upcounting: CNT < CCRx ≤ ARR (in particular, 0 < CCRx).
In downcounting: CNT > CCRx
TI2
OC1REF
OC1
TIM1_ARR
TIM1_CCR1
0 Tdelay TIME
Tpulse
are needed for these operations and it limits the minimum delay 𝑇𝑑𝑒𝑙𝑎𝑦 min we can get.
If you want to output a waveform with the minimum delay, you can set the OcxFE bit in the TIMx_CCMRx
register. Then OcxRef (and Ocx) are forced in response to the stimulus, without taking in account the
comparison. Its new level is the same as if a compare match had occurred. OcxFE acts only if the channel is
configured in PWM1 or PWM2 mode.
To select Encoder Interface mode write SMS=‘001’ in the TIMx_SMCR register if the counter is counting on
TI2 edges only, SMS=’010’ if it is counting on TI1 edges only and SMS=’011’ if it is counting on both TI1 and
TI2 edges.
Select the TI1 and TI2 polarity by programming the CC1P and CC2P bits in the TIMx_CCER register. When
needed, you can program the input filter as well. CC1NP and CC2NP must be kept low.
The two inputs TI1 and TI2 are used to interface to an incremental encoder. Refer to Table 16-1. The
counter is clocked by each valid transition on TI1FP1 or TI2FP2 (TI1 and TI2 after input filter and polarity
selection, TI1FP1=TI1 if not filtered and not inverted, TI2FP2=TI2 if not filtered and not inverted) assuming
that it is enabled (CEN bit in TIMx_CR1 register written to ‘1’). The sequence of transitions of the two inputs
is evaluated and generates count pulses as well as the direction signal. Depending on the sequence the
counter counts up or down, the DIR bit in the TIMx_CR1 register is modified by hardware accordingly. The
DIR bit is calculated at each transition on any input (TI1 or TI2), whatever the counter is counting on TI1 only,
TI2 only or both TI1 and TI2.
Encoder interface mode acts simply as an external clock with direction selection. This means that the
counter just counts continuously between 0 and the auto-reload value in the TIMx_ARR register (0 to ARR
or ARR down to 0 depending on the direction). So you must configure TIMx_ARR before starting. In the
same way, the capture, compare, prescaler, repetition counter, trigger output features continue to work as
normal. Encoder mode and External clock mode 2 are not compatible and must not be selected together.
In this mode, the counter is modified automatically following the speed and the direction of the incremental
encoder and its content, therefore, always represents the encoder’s position. The count direction
correspond to the rotation direction of the connected sensor. The table summarizes the possible
combinations, assuming TI1 and TI2 don’t switch at the same time.
Table 16-1 Counting direction versus encoder signals
High Down Up —— ——
TI1
Low Up Down —— ——
High —— —— Up Down
TI2
Low —— —— Down Up
High Down Up Up Down
TI1 or TI2
Low Up Down Down Up
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 16-42 gives an example of counter operation, showing count signal generation and direction control.
It also shows how input jitter is compensated where both edges are selected. This might occur if the sensor
is positioned near to one of the switching points. For this example we assume that the configuration is the
following:
CC1S=01 (TIMx_CCMR1 register, TI1FP1 mapped on TI1).
CC2S=01 (TIMx_CCMR2 register, TI1FP2 mapped on TI2).
CC1P=0 (TIMx_CCER register, TI1FP1 non-inverted, TI1FP1=TI1).
CC2P=0 (TIMx_CCER register, TI1FP2 non-inverted, TI1FP2= TI2).
SMS=011 (TIMx_SMCR register, both inputs are active on both rising and falling edges).
CEN=1 (TIMx_CR1 register, Counter enabled).
TI1
TI2
Counter
UP DOWN UP
TI1
TI2
Counter
DOWN UP DOWN
Figure 16-43 Example of encoder interface mode with TI1FP1 polarity inverted
The timer, when configured in Encoder Interface mode provides information on the sensor’s current position.
You can obtain dynamic information (speed, acceleration, deceleration) by measuring the period between
two encoder events using a second timer configured in capture mode. The output of the encoder which
indicates the mechanical zero can be used for this purpose. Depending on the time between two events, the
counter can also be read at regular times. You can do this by latching the counter value into a third input
capture register if available (then the capture signal must be periodic and can be generated by another
timer). When available, it is also possible to read its value through a DMA request generated by a real-time
clock.
The TI1S bit in the TIMx_CR2 register, allows the input filter of channel 1 to be connected to the output of a
XOR gate, combining the four input pins TIMx_CH1, TIMx_CH2 , TIMx_CH3 and TIMx_CH4.
The XOR output can be used with all the timer input functions such as trigger or input capture.
This is done using the advanced-control timers (TIM1) to generate PWM signals to drive the motor and
another timer TIMx (TIM3) referred to as “interfacing timer”. The “interfacing timer” captures the 3 timer input
pins (CC1, CC2, CC3, CC4) connected through a XOR to the TI1 input channel (selected by setting the TI1S
bit in the TIMx_CR2 register).
The slave mode controller is configured in reset mode; the slave input is TI1F_ED. Thus, each time one of
the 4 inputs toggles, the counter restarts counting from 0. This creates a time base triggered by any change
on the Hall inputs.
On the “interfacing timer”, capture/compare channel 1 is configured in capture mode, capture signal is TRC
The captured value, which corresponds to the time elapsed between 2 changes on the inputs, gives
information about motor speed.
The “interfacing timer” can be used in output mode to generate a pulse which changes the configuration of
the channels of the advanced-control timer (TIM1) (by triggering a COM event). The TIM1 timer is used to
generate PWM signals to drive the motor. To do this, the interfacing timer channel must be programmed so
that a positive pulse is generated after a programmed delay (in output compare or PWM mode). This pulse
is sent to the advanced-control timer (TIM1) through the TRGO output.
Example: you want to change the PWM configuration of your advanced-control timer TIM1 after a
programmed delay each time a change occurs on the Hall inputs connected to one of the TIMx timers.
Configure 4 timer inputs XORed to the TI1 input channel by writing the TI1S bit in the TIMx_CR2
register to ‘1’,
Program the time base: write the TIMx_ARR to the max value (the counter must be cleared by the TI1
change. Set the prescaler to get a maximum counter period longer than the time between 2 changes on
the sensors,
Program channel 1 in capture mode (TRC selected): write the CC1S bits in the TIMx_CCMR1 register
to ‘01’. You can also program the digital filter if needed,
Program channel 2 in PWM 2 mode with the desired delay: write the OC2M bits to ‘111’ and the CC2S
bits to ‘00’ in the TIMx_CCMR1 register,
Select OC2REF as trigger output on TRGO: write the MMS bits in the TIMx_CR2 register to ‘101’,
In the advanced-control timer TIM1, the right ITR input must be selected as trigger input, the timer is
programmed to generate PWM signals, the capture/compare control signals are preloaded (CCPC=1 in the
TIMx_CR2 register) and the COM event is controlled by the trigger input (CCUS=1 in the TIMx_CR2
register). The PWM control bits (CcxE, OcxM) are written after a COM event for the next step (this can be
done in an interrupt subroutine generated by the rising edge of OC2REF).
TIH1
TIH2
TIH3
Counter
CCR2
TRGO=
OC2REF
COM
OC1
OC1N
OC2
OC2N
OC3
OC3N
The TIMx timer can be synchronized with an external trigger in several modes: Reset mode, Gated mode
and Trigger mode.
TI1
UG
ck_cnt=ck_psc
Counter register 30 31 32 33 34 35 36 0 1 2 3 0 1 2 3
TIF
Enable the counter by writing CEN=1 in the TIMx_CR1 register (in gated mode, the counter doesn’t
start if CEN=0, whatever is the trigger input level).
The counter starts counting on the internal clock as long as TI1 is low and stops as soon as TI1 becomes
high. The TIF flag in the TIMx_SR register is set both when the counter starts or stops.
The delay between the rising edge on TI1 and the actual stop of the counter is due to the resynchronization
circuit on TI1 input.
TI1
CNT_EN
ETR
CK_CNT=CK_PSC
Counter register 34 35 36
TIF
The TIM timers are linked together internally for timer synchronization or chaining. Refer to Section 17.3.15:
Timer synchronization for details.
When the microcontroller enters debug mode (Cortex™-M0 core halted), the TIMx counter either continues
to work normally or stops, depending on DBG_TIMx_STOP configuration bit in DBG module.
The following table shows the TIM1 register map and reset values.
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
Address offset Name
0
CMS[1:0]
CKD[1:0]
ARPE
UDIS
OPM
URS
CEN
DIR
TIM1_CR1
–
–
0x00
Reset x x x x x x x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0
OIS3N
OIS2N
OIS1N
CCDS
CCUS
CCPC
OIS4
OIS3
OIS2
OIS1
TI1S
TIM1_CR2 MMS[2:0]
–
–
0x04
Reset x x x x x x x x x x x x x x x x x 0 0 0 0 1 1 1 0 0 0 0 0 0 x 0
ETPS[1:0]
MSM
ECE
ETP
TIM1_SMCR ETF[3:0] TS[2:0] SMS[2:0]
–
–
0x08
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 x 0 0 0
COMDE
CC4DE
CC3DE
CC2DE
CC1DE
COMIE
CC4IE
CC3IE
CC2IE
CC1IE
UDE
TDE
UIE
BIE
TIE
TIM1_DIER
–
0x0C
Reset x x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
CC4OF
CC3OF
CC2OF
CC1OF
COMIF
CC4IF
CC3IF
CC2IF
CC1IF
UIF
BIF
TIF
TIM1_SR
–
0x10 –
Reset x x x x x x x x x x x x x x x x x x x 0 0 0 0 x 0 0 0 0 0 0 0 0
COMG
CC4G
CC3G
CC2G
CC1G
BG
TG
UG
TIM1_EGR
–
0x14
Reset x x x x x x x x x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0
CC2S[1:0]
CC1S[1:0]
OC2CE
TIM1_CCMR
CC2PE
CC2FE
CC1CE
CC1PE
CC1FE
1 OC2M[2:0] OC1M[2:0]
–
(output mode)
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x18
CC2S[1:0]
CC1S[1:0]
IC2F[3:0]
IC1F[3:0]
TIM1_CCMR
IC2PSC IC1PSC
1
–
[1:0] [1:0]
(input mode)
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
CC4S[1:0]
CC3S[1:0]
OC4CE
TIM1_CCMR
CC4PE
CC4FE
CC3CE
CC3PE
CC3FE
2 OC4M[2:0] OC3M[2:0]
–
(output mode)
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x1C
CC4S[1:0]
CC3S[1:0]
IC4F[3:0]
IC3F[3:0]
TIM1_CCMR
IC4PSC IC3PSC
2
–
–
[1:0] [1:0]
(input mode)
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
CC3NP
CC3NE
CC2NP
CC2NE
CC1NP
CC1NE
CC4P
CC4E
CC3P
CC3E
CC2P
CC2E
CC1P
CC1E
TIM1_CCER
–
–
0x20
Reset x x x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIM1_CNT CNT[15:0]
–
–
0x24
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIM1_PSC PSC[15:0]
–
–
0x28
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIM1_ARR ARR[15:0]
–
–
0x2C
Reset x x x x x x x x x x x x x x x x 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
TIM1_RCR REP[7:0]
–
–
0x30
Reset x x x x x x x x x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0
TIM1_CCR1 CCR1[15:0]
–
0x34
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIM1_CCR2 CCR2[15:0]
–
0x38
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIM1_CCR3 CCR3[15:0]
–
0x3C
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIM1_CCR4 CCR4[15:0]
–
0x40
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
LOCK[1:0]
OSSR
MOE
OSSI
AOE
BKP
BKE
TIM1_BDTR DTG[7:0]
–
0x44
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x48
Reset x x x x x x x x x x x x x x x x x x x 0 0 0 0 0 x x x 0 0 0 0 0
TIM1_DMAR DMAB[15:0]
–
0x4C
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
type RW RW RW RW RW RW RO-0 RW
Nevertheless, TRGI must not be connected to ETRF in this case (TS bits
must not be 111).
Note3: If external clock mode 1 and external clock mode 2 are enabled at
the same time, the external clock input is ETRF.
13:12 ETPS[1:0] External trigger prescaler
External trigger signal ETRP frequency must be at most 1/4 of TIMxCLK
frequency. A prescaler can be enabled to reduce ETRP frequency. It is
useful when inputting fast external clocks.
00: Prescaler OFF
01: ETRP frequency divided by 2
10: ETRP frequency divided by 4
11: ETRP frequency divided by 8
11:8 ETF[3:0] External trigger filter This bit-field then defines the frequency used to sample
ETRP signal and the length of the digital filter applied to ETRP. The digital
filter is made of an event counter in which N consecutive events are needed
to validate a transition on the output:
0000: No filter, sampling is done at 𝑓𝑆𝐴𝑀𝑃𝐿𝐼𝑁𝐺 = 𝑓𝐷𝑇𝑆
0001: 𝑓𝑆𝐴𝑀𝑃𝐿𝐼𝑁𝐺 = 𝑓𝐶𝐾_𝐼𝑁𝑇 , N=2
0010: 𝑓𝑆𝐴𝑀𝑃𝐿𝐼𝑁𝐺 = 𝑓𝐶𝐾_𝐼𝑁𝑇 , N=4
0011: 𝑓𝑆𝐴𝑀𝑃𝐿𝐼𝑁𝐺 = 𝑓𝐶𝐾_𝐼𝑁𝑇 , N=8
0100: 𝑓𝑆𝐴𝑀𝑃𝐿𝐼𝑁𝐺 = 𝑓𝐷𝑇𝑆 /2, N=6
0101: 𝑓𝑆𝐴𝑀𝑃𝐿𝐼𝑁𝐺 = 𝑓𝐷𝑇𝑆 /2, N=8
0110: 𝑓𝑆𝐴𝑀𝑃𝐿𝐼𝑁𝐺 = 𝑓𝐷𝑇𝑆 /4, N=6
0111: 𝑓𝑆𝐴𝑀𝑃𝐿𝐼𝑁𝐺 = 𝑓𝐷𝑇𝑆 /4, N=8
1000: 𝑓𝑆𝐴𝑀𝑃𝐿𝐼𝑁𝐺 = 𝑓𝐷𝑇𝑆 /8, N=6
1001: 𝑓𝑆𝐴𝑀𝑃𝐿𝐼𝑁𝐺 = 𝑓𝐷𝑇𝑆 /8, N=8
1010: 𝑓𝑆𝐴𝑀𝑃𝐿𝐼𝑁𝐺 = 𝑓𝐷𝑇𝑆 /16, N=5
1011: 𝑓𝑆𝐴𝑀𝑃𝐿𝐼𝑁𝐺 = 𝑓𝐷𝑇𝑆 /16, N=6
1100: 𝑓𝑆𝐴𝑀𝑃𝐿𝐼𝑁𝐺 = 𝑓𝐷𝑇𝑆 /16, N=8
1101: 𝑓𝑆𝐴𝑀𝑃𝐿𝐼𝑁𝐺 = 𝑓𝐷𝑇𝑆 /32, N=5
1110: 𝑓𝑆𝐴𝑀𝑃𝐿𝐼𝑁𝐺 = 𝑓𝐷𝑇𝑆 /32, N=6
1111: 𝑓𝑆𝐴𝑀𝑃𝐿𝐼𝑁𝐺 = 𝑓𝐷𝑇𝑆 /32, N=8
7 MSM Master/slave mode
0: No action
1: The effect of an event on the trigger input (TRGI) is delayed to allow a
perfect synchronization between the current timer and its slaves (through
TRGO). It is useful if we want to synchronize several timers on a single
external event.
6:4 TS[2:0] Trigger selection This bit-field selects the trigger input to be used to
synchronize the counter.
000: Internal Trigger 0 (ITR0)
001: Reserved
010: Internal Trigger 2 (ITR2)
011: Internal Trigger 3 (ITR3)
100: TI1 Edge Detector (TI1F_ED)
101: Filtered Timer Input 1 (TI1FP1)
110: Filtered Timer Input 2 (TI2FP2)
111: External Trigger input (ETRF)
Note: These bits must be changed only when they are not used (e.g. when
type RO-0 RW RW RW RW RW RW RW
7:0 BIE TIE COMIE CC4IE CC3IE CC2IE CC1IE UIE
type RW RW RW RW RW RW RW RW
23:16 —
type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
15:8 —
type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
7:0 BG TG COMG CC4G CC3G CC2G CC1G UG
type W W W W W W W W
of the comparison changes or when the output compare mode switches from
“frozen” mode to “PWM” mode.
Note3: On channels having a complementary output, this bit field is
preloaded. If the CCPC bit is set in the TIMx_CR2 register then the OC1M
active bits take the new value from the preloaded bits only when a COM
event is generated.
3 OC1PE Output Compare 1 preload enable
0: Preload register on TIMx_CCR1 disabled. TIMx_CCR1 can be written at
anytime, the new value is taken in account immediately.
1: Preload register on TIMx_CCR1 enabled. Read/Write operations access
the preload register. TIMx_CCR1 preload value is loaded in the active
register at each update event.
Note1: These bits can not be modified as long as LOCK level 3 has been
programmed (LOCK bits in TIMx_BDTR register) and CC1S=’00’ (the
channel is configured in output).
Note2: The PWM mode can be used without validating the preload register
only in one pulse mode (OPM bit set in TIMx_CR1 register). Else the
behavior is not guaranteed.
2 OC1FE Output Compare 1 fast enable
This bit is used to accelerate the effect of an event on the trigger in input on
the CC output.
0: CC1 behaves normally depending on counter and CCR1 values even
when the trigger is ON. The minimum delay to activate CC1 output when an
edge occurs on the trigger input is 5 clock cycles.
1: An active edge on the trigger input acts like a compare match on CC1
output. Then, OC is set to the compare level independently from the result of
the comparison. Delay to sample the trigger input and to activate CC1 output
is reduced to 3 clock cycles.
Note:OCFE acts only if the channel is configured in PWM1 or PWM2 mode.
1:0 CC1S[1:0] Capture/Compare 1 selection
This bit-field defines the direction of the channel (input/output) as well as the
used input.
00: CC1 channel is configured as output
01: CC1 channel is configured as input, IC1 is mapped on TI1
10: CC1 channel is configured as input, IC1 is mapped on TI2
11: CC1 channel is configured as input, IC1 is mapped on TRC. This mode
is working only if an internal trigger input is selected through TS bit
(TIMx_SMCR register)
Note: CC1S bits are writable only when the channel is OFF (CC1E = ‘0’ in
TIMx_CCER).
used input.
00: CC2 channel is configured as output
01: CC2 channel is configured as input, IC2 is mapped on TI2
10: CC2 channel is configured as input, IC2 is mapped on TI1
11: CC2 channel is configured as input, IC2 is mapped on TRC. This mode
is working only if an internal trigger input is selected through TS bit
(TIMx_SMCR register)
Note: CC2S bits are writable only when the channel is OFF (CC2E = 0,
CC2NE=0 and updated in TIMx_CCER).
7:4 IC1F[3:0] Input capture 1 filter
This bit-field defines the frequency used to sample TI1 input and the length
of the digital filter applied to TI1. The digital filter is made of an event counter
in which N consecutive events are needed to validate a transition on the
output:
0000:No filter, sampling is done at 𝑓𝑆𝐴𝑀𝑃𝐿𝐼𝑁𝐺 = 𝑓𝐶𝐾_𝐼𝑁𝑇
0001: 𝑓𝑆𝐴𝑀𝑃𝐿𝐼𝑁𝐺 = 𝑓𝐶𝐾_𝐼𝑁𝑇 , N=2
0010: 𝑓𝑆𝐴𝑀𝑃𝐿𝐼𝑁𝐺 = 𝑓𝐶𝐾_𝐼𝑁𝑇 , N=4
0011: 𝑓𝑆𝐴𝑀𝑃𝐿𝐼𝑁𝐺 = 𝑓𝐶𝐾_𝐼𝑁𝑇 , N=8
0100: 𝑓𝑆𝐴𝑀𝑃𝐿𝐼𝑁𝐺 = 𝑓𝐷𝑇𝑆 /2, N=6
0101: 𝑓𝑆𝐴𝑀𝑃𝐿𝐼𝑁𝐺 = 𝑓𝐷𝑇𝑆 /2, N=8
0110: 𝑓𝑆𝐴𝑀𝑃𝐿𝐼𝑁𝐺 = 𝑓𝐷𝑇𝑆 /4, N=6
0111: 𝑓𝑆𝐴𝑀𝑃𝐿𝐼𝑁𝐺 = 𝑓𝐷𝑇𝑆 /4, N=8
1000: 𝑓𝑆𝐴𝑀𝑃𝐿𝐼𝑁𝐺 = 𝑓𝐷𝑇𝑆 /8, N=6
1001: 𝑓𝑆𝐴𝑀𝑃𝐿𝐼𝑁𝐺 = 𝑓𝐷𝑇𝑆 /8, N=8
1010: 𝑓𝑆𝐴𝑀𝑃𝐿𝐼𝑁𝐺 = 𝑓𝐷𝑇𝑆 /16, N=5
1011: 𝑓𝑆𝐴𝑀𝑃𝐿𝐼𝑁𝐺 = 𝑓𝐷𝑇𝑆 /16, N=6
1100: 𝑓𝑆𝐴𝑀𝑃𝐿I𝑁𝐺 = 𝑓𝐷𝑇𝑆 /16, N=8
1101: 𝑓𝑆𝐴𝑀𝑃𝐿𝐼𝑁𝐺 = 𝑓𝐷𝑇𝑆 /32, N=5
1110: 𝑓𝑆𝐴𝑀𝑃𝐿𝐼𝑁𝐺 = 𝑓𝐷𝑇𝑆 /32, N=6
1111: 𝑓𝑆𝐴𝑀𝑃𝐿𝐼𝑁𝐺 = 𝑓𝐷𝑇𝑆 /32, N=8
3:2 IC1PSC[1:0] Input capture 1 prescaler
This bit-field defines the ratio of the prescaler acting on CC1 input (IC1).
The prescaler is reset as soon as CC1E=’0’ (TIMx_CCER register).
00: no prescaler, capture is done each time an edge is detected on the
capture input
01: capture is done once every 2 events
10: capture is done once every 4 events
11: capture is done once every 8 events
1:0 CC1S[1:0] Capture/Compare 1
Selection This bit-field defines the direction of the channel (input/output) as
well as the used input.
00: CC1 channel is configured as output
01: CC1 channel is configured as input, IC1 is mapped on TI1
10: CC1 channel is configured as input, IC1 is mapped on TI2
11: CC1 channel is configured as input, IC1 is mapped on TRC. This mode
is working only if an internal trigger input is selected through TS bit
(TIMx_SMCR register) Note: CC1S bits are writable only when the channel
is OFF (CC1E = ‘0’ in TIMx_CCER).
Note: CC4S bits are writable only when the channel is OFF (CC4E=0 in
TIMx_CCER and update already
7:4 IC3F[3:0] Input capture 3 filter
3:2 IC3PSC[1:0] Input capture 3 prescaler
1:0 CC3S[1:0] Capture/compare 3 selection
This bit-field defines the direction of the channel (input/output) as well as the
used input.
00: CC3 channel is configured as output
01: CC3 channel is configured as input, IC3 is mapped on TI3
10: CC3 channel is configured as input, IC3 is mapped on TI4
11: CC3 channel is configured as input, IC3 is mapped on TRC. This mode
is working only if an internal trigger input is selected through TS bit
(TIMx_SMCR register)
Note: CC3S bits are writable only when the channel is OFF (CC3E=0,
CC3NE=0 in TIMx_CCER and update already)
the new value from the preloaded bits only when a Commutation event is
generated.
0 CC1E Capture/Compare 1 output enable
CC1 channel configured as output:
0: Off
- OC1 is not active. OC1 level is then function of MOE, OSSI, OSSR,
OIS1, OIS1N and CC1NE bits.
1: On
- OC1 signal is output on the corresponding output pin depending on
MOE, OSSI, OSSR, OIS1, OIS1N and CC1NE bits.
CC1 channel configured as input:
This bit determines if a capture of the counter value can actually be done
into the input capture/compare register 1 (TIMx_CCR1) or not.
0: Capture disabled.
1: Capture enabled.
Note: On channels having a complementary output, this bit is preloaded. If
the CCPC bit is set in the TIMx_CR2 register then the CC1E active bit takes
the new value from the preloaded bits only when a Commutation event is
generated.
Table 16-3 Output control bits for complementary Ocx and OcxN channels with break feature
Control bits Output states(1)
MOE OSSI OSSR CcxE CcxNE Ocx output state OcxN output state
Output Disabled (not driven by Output Disabled (not driven by
0 0 0 the timer) the timer)
Ocx=0, Ocx_EN=0 OcxN=0, OcxN_EN=0
Output Disabled (not driven by OCxREF + Polarity
0 0 1 the timer) OcxN=OCxREF
Ocx=0, Ocx_EN=0 xor CCxNP, OcxN_EN=1
OCxREF +Polarity Output Disabled (not driven by
0 1 0 Ocx=OCxREF ^ CCxP the timer)
Ocx_EN=1 OcxN=0, OcxN_EN=0
Complementary to OCxREF +
OCREF + Polarity +dead-time
0 1 1 Polarity +dead-time
Ocx_EN=1
OcxN_EN=1
1 X
Output Disabled (not driven by Output Disabled (not driven by
1 0 0 the timer) the timer)
Ocx=CCxP, Ocx_EN=0 OcxN=CCxNP, OcxN_EN=0
Off-State (output enabled with OCxREF + Polarity
1 0 1 inactive state) OcxN=OCxREF xor CCxNP,
Ocx=CCxP, Ocx_EN=1 OcxN_EN=1
OCxREF + Polarity Off-State (output enabled with
1 1 0 Ocx=OCxREF ^ T1CCxNP inactive state)
Ocx_EN=1 OcxN=T1CCxNP, Ocx_EN=1
Complementary to OCxREF +
OCxREF + Polarity + dead-time
1 1 1 Polarity + dead-time
Ocx_EN=1
OcxN_EN=1
Output Disabled (not driven by Output Disabled (not driven by
0 0 0 the timer) the timer)
Ocx=CCxP, Ocx_EN=0 OcxN=CCxNP, OcxN_EN=0
0 0 1 Output Disabled (not driven by the timer)
0 1 0 Asynchronously: Ocx=CCxP, Ocx_EN=0, OcxN=CCxNP,
OcxN_EN=0
Then if the clock is present: Ocx=OISx and OcxN=OISxN after a
0 1 1
dead-time, assuming that OISx and OISxN do not correspond to
0 X
Ocx and OcxN both in active state.
Output Disabled (not driven by Output Disabled (not driven by
1 0 0 the timer) the timer)
Ocx=CCxP, Ocx_EN=0 OcxN=CCxNP, OcxN_EN=0
1 0 1 Off-State (output enabled with inactive state)
1 1 0 Asynchronously: Ocx=CCxP, Ocx_EN=1, OcxN=CCxNP,
OcxN_EN=1
1 1 1
Then if the clock is present: Ocx=OISx and OcxN=OISxN after a
15:8 DMAB[15:8]
type RW RW RW RW RW RW RW RW
7:0 DMAB[7:0]
type RW RW RW RW RW RW RW RW
The general-purpose timers consist of a 16-bit auto-reload counter driven by a programmable prescaler.
They may be used for a variety of purposes, including measuring the pulse lengths of input signals (input
capture) or generating output waveforms (output compare and PWM).
Pulse lengths and waveform periods can be modulated from a few microseconds to several milliseconds
using the timer prescaler and the RCC clock controller prescalers.
The timers are completely independent, and do not share any resources. They can be synchronized
together as described in Section 17.3.15.
Internal Clock(CK_INT)
CK_TIM from RCC
ETRP Trigger
ETR Polarity Selection & Edge ETRF Controller TRGO
TIM1_ETR Input Filter
Detector & Prescaler to other timers
to DAC/ADC
ITR0
ITR1 ITR TGI Reset, Enable, Up/Down, Count
ITR2 Slave
ITR3 TRC TRGI Mode
Controller
TI1F_ED
TI1FP1 Encoder
TI1FP2 Interface
Auto-reload register UI
Stop,clear or up/down
UEV
CK_PSC PSC CK_CNT
+/- CNT counter
prescaler
CC1I UEV
XOR CC1I
TI1FP1 OC1 TIM1_CH1
TIM1_CH1 TI1 Input Filter & TI1FP2 IC1 IC1PS OC1REF output
Edge Prescaler Capture/Compare 1 Register
control
detector TRC
CC2I UEV
CC2I
OC2 TIM1_CH2
TIM1_CH2 TI2 Input Filter & TI2FP1 IC2 IC2PS OC2REF output
Edge TI2FP2 Prescaler Capture/Compare 2 Register
control
detector TRC
CC3I UEV
CC3I
TI3FP3 OC3 TIM1_CH3
TIM1_CH3 Input Filter & IC3 IC3PS OC3REF output
TI3 TI3FP4
Edge Prescaler Capture/Compare 3 Register
control
detector
TRC
CC4I UEV
CC4I
OC4 TIM1_CH4
TIM1_CH4 TI4 Input Filter & TI4FP3 IC4 IC4PS OC4REF output
Edge TI4FP4 Prescaler Capture/Compare 4 Register control
detector
TRC
ETRF
Notes:
Preload registers transferred
Reg
to active registers on UEV event
according to control bit
event
The main block of the programmable timer is a 16-bit counter with its related auto-reload register. The
counter can count up but also down or both up and down. The counter clock can be divided by a prescaler.
The counter, the auto-reload register and the prescaler register can be written or read by software. This is
true even when the counter is running.
The time-base unit includes:
Counter Register(TIMx_CNT)
Prescaler Register(TIMx_PSC)
Auto-Reload Register(TIMx_ARR)
The auto-reload register is preloaded. Writing to or reading from the auto-reload register accesses the
preload register. The content of the preload register are transferred into the shadow register permanently or
at each update event (UEV), depending on the auto-reload preload enable bit (ARPE) in TIMx_CR1 register.
The update event is sent when the counter reaches the overflow (or underflow when downcounting) and if
the UDIS bit equals 0 in the TIMx_CR1 register. It can also be generated by software. The generation of the
update event is described in detail for each configuration.
The counter is clocked by the prescaler output CK_CNT, which is enabled only when the counter enable bit
(CEN) in TIMx_CR1 register is set (refer also to the slave mode controller description to get more details on
counter enabling).
Note that the actual counter enable signal CNT_EN is set 1 clock cycle after CEN.
Prescaler description
The prescaler can divide the counter clock frequency by any factor between 1 and 65536. It is based on a
16-bit counter controlled through a 16-bit register (in the TIMx_PSC register). It can be changed on the fly as
this control register is buffered. The new prescaler ratio is taken into account at the next update event.
Figure 17-2 and Figure 17-3 give some examples of the counter behavior when the prescaler ratio is
changed on the fly:
CK_PSC
CEN
CK_CNT
Counter register F7 F8 F9 FA FB FC 00 01 02 03
UEV
Prescaler buffer 0 1
Prescaler counter 0 0 1 0 1 0 1 0 1
Figure 17-2 Counter timing diagram with prescaler division change from 1 to 2
CK_PSC
CEN
CK_CNT
Counter register F7 F8 F9 FA FB FC 00 01
UEV
Prescaler buffer 0 3
Prescaler counter 0 0 1 2 3 0 1 2 3
Figure 17-3 Counter timing diagram with prescaler division change from 1 to 4
Upcounting mode
In upcounting mode, the counter counts from 0 to the auto-reload value (content of the TIMx_ARR register),
then restarts from 0 and generates a counter overflow event.
An Update event can be generated at each counter overflow or by setting the UG bit in the TIMx_EGR
register (by software or by using the slave mode controller).
The UEV event can be disabled by software by setting the UDIS bit in TIMx_CR1 register. This is to avoid
updating the shadow registers while writing new values in the preload registers. Then no update event
occurs until the UDIS bit has been written to 0. However, the counter restarts from 0, as well as the counter
of the prescaler (but the prescale rate does not change). In addition, if the URS bit (update request selection)
in TIMx_CR1 register is set, setting the UG bit generates an update event UEV but without setting the UIF
flag (thus no interrupt or DMA request is sent). This is to avoid generating both update and capture interrupts
when clearing the counter on the capture event.
When an update event occurs, all the registers are updated and the update flag (UIF bit in TIMx_SR register)
is set (depending on the URS bit):
The 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
CK_CNT
Counter register 31 32 33 34 35 36 00 01 02 03 04 05 06 07
Counter overflow
UEV
UIF
CK_PSC
CNT_EN
CK_CNT
Counter register 34 35 36 00 01 02 03
Counter overflow
UEV
UIF
CK_PSC
CNT_EN
CK_CNT
Counter register 35 36 00 01
Counter overflow
UEV
UIF
CK_PSC
CK_CNT
Counter register 35 36 00 01
Counter overflow
UEV
UIF
CEN
CK_CNT
Counter register 31 32 33 34 35 36 00 01 02 03 04 05 06 07
Counter overflow
UEV
UIF
CK_PSC
CEN
CK_CNT
Counter register F0 F1 F2 F3 F4 F5 00 01 02 03 04 05 06 07
Counter overflow
UEV
UIF
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)
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
CK_CNT
Counter register 05 04 03 02 01 00 36 35 34 33 32 31 30 2F
Counter underflow
UEV
UIF
CK_PSC
CNT_EN
CK_CNT
Counter register 02 01 00 36 35 34 33
Counter underflow
UEV
UIF
CK_PSC
CNT_EN
CK_CNT
Counter register 01 00 36 35
Counter underflow
UEV
UIF
CK_PSC
CK_CNT
Counter register 20 1F 00 36 01
Counter underflow
UEV
UIF
CK_PSC
CEN
CK_CNT
Counter register 05 04 03 02 01 00 36 35 34 33 32 31 30 2F
Counter underflow
UEV
UIF
Figure 17-14 Counter timing diagram, Update event when repetition counter is not used
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 autoreload 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
CNT_EN
CK_CNT
Counter register 04 03 02 01 00 01 02 03 04 05 06 05 04 03
Counter underflow
Counter overflow
UEV
UIF
CK_PSC
CNT_EN
CK_CNT
Counter register 02 01 00 36 35 34 33
Counter underflow
UEV
UIF
CK_PSC
CNT_EN
CK_CNT
Counter register 35 36 36 35
Counter overflow
UEV
UIF
CK_PSC
CK_CNT
Counter register 20 1F 01 00 01
Counter underflow
UEV
UIF
CEN
CK_CNT
Counter register 06 05 04 03 02 01 00 01 02 03 04 05 06 07
Counter underflow
UEV
UIF
Figure 17-19 Counter timing diagram, Update event with ARPE=1 (counter underflow)
CK_PSC
CEN
CK_CNT
Counter register F7 F8 F9 FA FB FC 36 35 34 33 32 31 30 2F
Counter overflow
UEV
UIF
Figure 17-20 Counter timing diagram, Update event with ARPE=1 (counter overflow)
Interal clock
CNT_EN
UG
CNT_INIT
CK_CNT=CK_PSC
Counter register 31 32 33 34 35 36 00 01 02 03 04 05 06 07
TIMx_SMCR
TS[2:0]
T12F or
TI1F or Encoder
ITRx mode
0xx
TI1_ED
100
TRGI External
TI1FP1 clock mode 1
101 CK_PSC
TI2F_rising
TI2 Edge 0 TI2FP2
Filter TI2F_falling 1 110
Detector
ETRF External
111 ETRF
clock mode 2
ICF[3:0] CC2P
TIMx_CCMR1 TIMx_CCER
CK_INT Internal clock
(internal clock) mode
ECE SMS[2:0]
TIMx_SMCR
For example, to configure the upcounter to count in response to a rising edge on the TI2 input, use the
following procedure:
1. Configure channel 2 to detect rising edges on the TI2 input by writing CC2S= ‘01’ in the TIMx_CCMR1
register.
2. Configure the input filter duration by writing the IC2F[3:0] bits in the TIMx_CCMR1 register (if no filter is
needed, keep IC2F=0000).
3. Select rising edge polarity by writing CC2P=0 in the TIMx_CCER register
4. Configure the timer in external clock mode 1 by writing SMS=111 in the TIMx_SMCR register.
5. Select TI2 as the input source by writing TS=110 in the TIMx_SMCR register.
6. Enable the counter by writing CEN=1 in the TIMx_CR1 register.
Note: The capture prescaler is not used for triggering, so you don’t need to configure it.
When a rising edge occurs on TI2, the counter counts once and the TIF flag is set.
The delay between the rising edge on TI2 and the actual clock of the counter is due to the resynchronization
circuit on TI2 input.
TI2
CEN
CK_CNT=CK_PSC
Counter register 34 35 36 0
TIF
Write TIF=0
T12F or
TI1F or Encoder
mode
TRGI External
clock mode 1 CK_PSC
fDTS
ETR Filter ETRF External
0 Divider
ETRP down-counter clock mode 2
1 /1,/2,/4,/8
For example, to configure the upcounter to count each 2 rising edges on ETR, use the following procedure:
1. As no filter is needed in this example, write ETF[3:0]=0000 in the TIMx_SMCR register.
2. Set the prescaler by writing ETPS[1:0]=01 in the TIMx_SMCR register
3. Select rising edge detection on the ETR pin by writing ETP=0 in the TIMx_SMCR register
4. Enable external clock mode 2 by writing ECE=1 in the TIMx_SMCR register.
5. Enable the counter by writing CEN=1 in the TIMx_CR1 register.
The counter counts once each 2 ETR rising edges
The delay between the rising edge on ETR and the actual clock of the counter is due to the
resynchronization circuit on the ETRP signal.
fCK_INT
CNT_EN
del ay
del ay
ETR
ETRP
ETRF
CK_INT=CK_PSC
COUNTER 34 35 36
Each Capture/Compare channel is built around a capture/compare register (including a shadow register), a
input stage for capture (with digital filter, multiplexing and prescaler) and an output stage (with comparator
and output control).
The input stage samples the corresponding Tix input to generate a filtered signal TixF. Then, an edge
detector with polarity selection generates a signal (TixFPx) which can be used as trigger input by the slave
mode controller or as the capture command. It is prescaled before the capture register(IC1PS).
TI1F_ED
To the slave mode controller
TI1 TI1F_Rising
Filter Edge 0 TI1FP1
fDTS
downcounter detector TI1F_Falling 1
01
TI2FP1 IC1 Divider IC1PS
10
ICF[3:0] /1,/2,/4,/8
CC1P/CC1NP TRC
TIMx_CCMR1 11
TIMx_CCER (from slave
mode controller)
TI2F_Rising
(from channel 2) 0
CC1S[1:0] ICPS[1:0] CCIE
TI2F_Falling 1
TIMx_CCMR1 TIMx_CCER
(from channel 2)
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
high 8
if 16-bit
low 8
write_in_progress S write CCR1H
Read CCR1H S read_in_progress
Capture/Compare Preload Register R write CCR1L
Read CCR1L R CC1S[1]
capture_transfer compare_transfer output
CC1S[0]
CC1S[1] mode
OC1PE OC1PE
CC1S[0] Capture/Compare Shadow Register UEV
from time TIMx_CCMR1
IC1PS capture base unit
CC1E CNT>CCR1
Counter CNT=CCR1
CC1G
TIMx_EGR
TIMx_SMCR
OCCS
OCREF_CLR
0
ETRF To the master
1
mode controller
ocref_clr_int
‘0’
0
CNT>CCR1 Output 0 Output OC1
mode OC1REF enable
CNT=CCR1 1 1 circuit
controller
CC1P
TIMx_CCER CC1E TIMx_CCER
OC1M[2:0] CC1E
TIMx_CCMR1 TIMx_CCER
In Input capture mode, the Capture/Compare Registers (TIMx_CCRx) are used to latch the value of the
counter after a transition detected by the corresponding Icx signal. When a capture occurs, the
corresponding CCXIF flag (TIMx_SR register) is set and an interrupt or a DMA request can be sent if they
are enabled. If a capture occurs while the CcxIF flag was already high, then the over-capture flag CcxOF
(TIMx_SR register) is set. CcxIF can be cleared by software by writing it to 0 or by reading the captured data
stored in the TIMx_CCRx register. CcxOF is cleared when you write it to 0.
The following example shows how to capture the counter value in TIMx_CCR1 when TI1 input rises. To do
this, use the following procedure:
Select the active input: TIMx_CCR1 must be linked to the TI1 input, so write the CC1S bits to 01 in the
TIMx_CCMR1 register. As soon as CC1S becomes different from 00, the channel is configured in input
and the TIMx_CCR1 register becomes read-only.
Program the input filter duration you need with respect to the signal you connect to the timer when the
input is one of the Tix (IcxF bits in the TIMx_CCMRx register). Let’s imagine that, when toggling, the
input signal is not stable during at must 5 internal clock cycles. We must program a filter duration longer
than these 5 clock cycles. We can validate a transition on TI1 when 8 consecutive samples with the
new level have been detected (sampled at fDTS frequency). Then write IC1F bits to 0011 in the
TIMx_CCMR1 register.
Program the input prescaler. In our example, we wish the capture to be performed at each valid
transition, so the prescaler is disabled (write IC1PS bits to 00 in the TIMx_CCMR1 register).
Enable capture from the counter into the capture register by setting the CC1E bit in the TIMx_CCER
register.
If needed, enable the related interrupt request by setting the CC1IE bit in the TIMx_DIER register,
and/or the DMA request by setting the CC1DE bit in the TIMx_DIER register.
When an input capture occurs:
The TIMx_CCR1 register gets the value of the counter on the active transition.
CC1IF flag is set (interrupt flag). CC1OF is also set if at least two consecutive captures occurred
whereas the flag was not cleared.
An interrupt is generated depending on the CC1IE bit.
A DMA request is generated depending on the CC1DE bit.
In order to handle the overcapture, it is recommended to read the data before the overcapture flag. This is to
avoid missing an overcapture which could happen after reading the flag and before reading the data.
IC interrupt and/or DMA requests can be generated by software by setting the corresponding CCxG bit in the
TIMx_EGR register.
This mode is a particular case of input capture mode. The procedure is the same except:
Two Icx signals are mapped on the same Tix input.
These 2 Icx signals are active on edges with opposite polarity.
One of the two TixFP signals is selected as trigger input and the slave mode controller is configured in
reset mode.
For example, you can measure the period (in TIMx_CCR1 register) and the duty cycle (in TIMx_CCR2
register) of the PWM applied on TI1 using the following procedure (depending on CK_INT frequency and
prescaler value):
Select the active input for TIMx_CCR1: write the CC1S bits to 01 in the TIMx_CCMR1 register (TI1
selected).
Select the active polarity for TI1FP1 (used both for capture in TIMx_CCR1 and counter clear): write the
CC1P to ‘0’ (active on rising edge).
Select the active input for TIMx_CCR2: write the CC2S bits to 10 in the TIMx_CCMR1 register (TI1
selected).
Select the active polarity for TI1FP2 (used for capture in TIMx_CCR2): write the CC2P bit to ‘1’ (active
on falling edge).
Select the valid trigger input: write the TS bits to 101 in the TIMx_SMCR register (TI1FP1 selected).
Configure the slave mode controller in reset mode: write the SMS bits to 100 in the TIMx_SMCR
register.
Enable the captures: write the CC1E and CC2E bits to ‘1’ in the TIMx_CCER register.
TI1
IC1/IC2
COUNTER
5 0 1 2 3 4 5 0
T1CCR1 5
T1CCR2 3
In output mode (CCxS bits = 00 in the TIMx_CCMRx register), each output compare signal (OCxREF and
then Ocx/OcxN) can be forced to active or inactive level directly by software, independently of any
comparison between the output compare register and the counter.
To force an output compare signal (ocxref/Ocx) to its active level, you just need to write 101 in the OcxM bits
in the corresponding TIMx_CCMRx register. Thus OCxREF is forced high (OCxREF is always active high)
and Ocx get opposite value to CCxP polarity bit.
e.g.: CCxP=0 (Ocx active high) => Ocx is forced to high level.
OCxREF signal can be forced low by writing the OcxM bits to 100 in the TIMx_CCMRx register.
Anyway, the comparison between the TIMx_CCRx shadow register and the counter is still performed and
allows the flag to be set. Interrupt and DMA requests can be sent accordingly. This is described in the Output
This function is used to control an output waveform or indicating when a period of time has elapsed.
When a match is found between the capture/compare register and the counter, the output compare function:
Assigns the corresponding output pin to a programmable value defined by the output compare mode
(OcxM bits in the TIMx_CCMRx register) and the output polarity (CCxP bit in the TIMx_CCER register).
The output pin can keep its level (OCXM=000), be set active (OcxM=001), be set inactive (OcxM=010)
or can toggle (OcxM=011) on match.
Sets a flag in the interrupt status register (CcxIF bit in the TIMx_SR register).
Generates an interrupt if the corresponding interrupt mask is set (CCXIE bit in the TIMx_DIER register).
Sends a DMA request if the corresponding enable bit is set (CcxDE bit in the TIMx_DIER register,
CCDS bit in the TIMx_CR2 register for the DMA request selection).
The TIMx_CCRx registers can be programmed with or without preload registers using the OcxPE bit in the
TIMx_CCMRx register.
In output compare mode, the update event UEV has no effect on OCxREF and Ocx output. The timing
resolution is one count of the counter. Output compare mode can also be used to output a single pulse (in
One-pulse mode).
Procedure:
1. Select the counter clock (internal, external, prescaler).
2. Write the desired data in the TIMx_ARR and TIMx_CCRx registers.
3. Set the CcxIE bits if an interrupt is to be generated.
4. Select the output mode,For example:
− Write OcxM = 011 to toggle Ocx output pin when CNT matches CCRx
− Write OcxPE = 0 to disable preload register
− Write CCxP = 0 to select active high polarity
− Write CcxE = 1 to enable the output
5. Enable the counter by setting the CEN bit in the TIMx_CR1 register.
The TIMx_CCRx register can be updated at any time by software to control the output waveform, provided
that the preload register is not enabled (OcxPE=0, else TIMx_CCRx shadow register is updated only at the
next update event UEV). An example is given in Figure 17-30.
oc1ref=OC1
Pulse width modulation mode allows you to generate a signal with a frequency determined by the value of
the TIMx_ARR register and a duty cycle determined by the value of the TIMx_CCRx register.
The PWM mode can be selected independently on each channel (one PWM per Ocx output) by writing 110
(PWM mode 1) or ‘111 (PWM mode 2) in the OcxM bits in the TIMx_CCMRx register. You must enable the
corresponding preload register by setting the OcxPE bit in the TIMx_CCMRx register, and eventually the
auto-reload preload register (in upcounting or center-aligned modes) by setting the ARPE bit in the
TIMx_CR1 register.
As the preload registers are transferred to the shadow registers only when an update event occurs, before
starting the counter, you have to initialize all the registers by setting the UG bit in the TIMx_EGR register.
Ocx polarity is software programmable using the CCxP bit in the TIMx_CCER register. It can be
programmed as active high or active low. Ocx output is enabled by a combination of the CcxE, CcxNE, MOE,
OSSI and OSSR bits
In PWM mode (1 or 2), TIMx_CNT and TIMx_CCRx are always compared to determine whether
TIMx_CCRx≤ TIMx_CNT or TIMx_CNT≤ TIMx_CCRx (depending on the direction of the counter). The timer
is able to generate PWM in edge-aligned mode or center-aligned mode depending on the CMS bits in the
TIMx_CR1 register.
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
Counter register 0 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 0 1 2
OCxREF
CCRx=4 CMS=01
CCxIF CMS=10
CMS=11
OCxREF
CCRx=7
CCxIF CMS=10 or 11
OCxREF
CCRx=8 CMS=01
CCxIF CMS=10
CMS=11
OCxREF
CCRx>8
CMS=01
CCxIF CMS=10
CMS=11
OCxREF
CCRx=0
CMS=01
CCxIF CMS=10
CMS=11
Be started in response to a stimulus and to generate a pulse with a programmable length after a
programmable delay.
Starting the counter can be controlled through the slave mode controller. Generating the waveform can be
done in output compare mode or PWM mode. You select One-pulse mode by setting the OPM bit in the
TIMx_CR1 register. This makes the counter stop automatically at the next update event UEV.
A pulse can be correctly generated only if the compare value is different from the counter initial value. Before
starting (when the timer is waiting for the trigger), the configuration must be:
In upcounting: CNT<CCRx≤ ARR (in particular, 0<CCRx),
In downcounting: CNT>CCRx.
TI2
OC1REF
OC1
TIM1_ARR
TIM1_CCR1
0 Tdelay TIME
Tpulse
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.
the OCxREF signal can be connected to the output of a comparator to be used for current handling. In this
case, the ETR must be configured as follow:
1. The external trigger prescaler should be kept off: bits ETPS[1:0] in the TIMx_SMCR register are cleared
to 00.
2. The external clock mode 2 must be disabled: bit ECE in the TIM1_SMCR register is cleared to 0.
3. The external trigger polarity (ETP) and the external trigger filter (ETF) can be configured according to
the application’s needs.
Figure 17-34 shows the behavior of the OCxREF signal when the ETRF input becomes high, for both
values of the OcxCE enable bit. In this example, the timer TIMx is programmed in PWM mode.
CCRx
Counter
ETRF
OCxREF
(OCxCE=0)
OCxREF
(OCxCE=0)
OCxREF_CLR OCxREF_CLR
become high still high
To select Encoder Interface mode write SMS=‘001 in the TIMx_SMCR register if the counter is counting on
TI2 edges only, SMS=010 if it is counting on TI1 edges only and SMS=011 if it is counting on both TI1 and
TI2 edges.
Select the TI1 and TI2 polarity by programming the CC1P and CC2P bits in the TIMx_CCER register.
CC1NP and CC2NP must be kept cleared. When needed, you can program the input filter as well.
The two inputs TI1 and TI2 are used to interface to an incremental encoder. Refer to Table 17-1. The
counter is clocked by each valid transition on TI1FP1 or TI2FP2 (TI1 and TI2after input filter and polarity
selection, TI1FP1=TI1 if not filtered and not inverted, TI2FP2=TI2 if not filtered and not inverted) assuming
that it is enabled (CEN bit in TIMx_CR1 register written to ‘1). The sequence of transitions of the two inputs
is evaluated and generates count pulses as well as the direction signal. Depending on the sequence the
counter counts up or down, the DIR bit in the TIMx_CR1 register is modified by hardware accordingly. The
DIR bit is calculated at each transition on any input (TI1 or TI2), whatever the counter is counting on TI1 only,
TI2 only or both TI1 and TI2.
Encoder interface mode acts simply as an external clock with direction selection. This means that the
counter just counts continuously between 0 and the auto-reload value in the TIMx_ARR register (0 to ARR
or ARR down to 0 depending on the direction). So you must configure TIMx_ARR before starting. In the
same way, the capture, compare, prescaler, trigger output features continue to work as normal.
Encoder mode and External clock mode 2 are not compatible and must not be selected together.
In this mode, the counter is modified automatically following the speed and the direction of the incremental
encoder and its content, therefore, always represents the encoder’s position. The count direction
correspond to the rotation direction of the connected sensor. The table summarizes the possible
combinations, assuming TI1 and TI2 don’t switch at the same time.
Table 17-1 Counting direction versus encoder signals
High Down Up —— ——
TI1
Low Up Down —— ——
High —— —— Up Down
TI2
Low —— —— Down Up
High Down Up Up Down
TI1 or TI2
Low Up Down Down Up
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 17-35 gives an example of counter operation, showing count signal generation and direction control.
It also shows how input jitter is compensated where both edges are selected. This might occur if the sensor
is positioned near to one of the switching points. For this example we assume that the configuration is the
following:
CC1S= 01 (TIMx_CCMR1 register, TI1FP1 mapped on TI1)
CC2S= 01 (TIMx_CCMR2 register, TI2FP2 mapped on TI2)
CC1P=0 (TIMx_CCER register, TI1FP1 noninverted, TI1FP1=TI1)
CC2P=0 (TIMx_CCER register, TI2FP2 noninverted, TI2FP2=TI2)
SMS=011 (TIMx_SMCR register, both inputs are active on both rising and falling edges)
CEN=1 (TIMx_CR1 register, Counter is enabled)
TI1
TI2
Counter
UP DOWN UP
TI1
TI2
Counter
DOWN UP DOWN
Figure 17-36 Example of encoder interface mode with TI1FP1 polarity inverted
The timer, when configured in Encoder Interface mode provides information on the sensor’s current position.
You can obtain dynamic information (speed, acceleration, deceleration) by measuring the period between
two encoder events using a second timer configured in capture mode. The output of the encoder which
indicates the mechanical zero can be used for this purpose. Depending on the time between two events, the
counter can also be read at regular times. You can do this by latching the counter value into a third input
capture register if available (then the capture signal must be periodic and can be generated by another
timer). When available, it is also possible to read its value through a DMA request generated by a Real-Time
clock.
The TI1S bit in the TIMx_CR2 register, allows the input filter of channel 1 to be connected to the output of a
XOR gate, combining the four input pins TIMx_CH1, TIMx_CH2 , TIMx_CH3 and TIMx_CH4.
The XOR output can be used with all the timer input functions such as trigger or input capture.
The TIMx Timers can be synchronized with an external trigger in several modes: Reset mode, Gated mode
and Trigger mode.
TI1
UG
ck_cnt=ck_psc
Counter register 30 31 32 33 34 35 36 0 1 2 3 0 1 2 3
TIF
TI1
synchronization time
CEN
synchronization time
cnt_en
ck_cnt=ck_psc
Counter register 30 31 32 33 34 35 36 37 38
TIF
write TIF=0
TI2
synchorization tim e
CNT_EN
ck_cnt=ck_psc
Counter register 34 35 36 37
TIF
TI1
CNT_EN
ETR
CK_CNT=CK_PSC
Counter register 34 35 36
TIF
The TIMx timers are linked together internally for timer synchronization or chaining. When one Timer is
configured in Master Mode, it can reset, start, stop or clock the counter of another Timer configured in Slave
Mode.
Figure 17-41 Master/Slave timer exampleresents an overview of the trigger selection and the master mode
selection blocks.
TIM1 TIM3
TS
MMS SMS
Clock
UEV
Input
trigger
selection
To connect the TRGO1 output of Timer 1 to Timer 3, Timer 3 must be configured in slave mode using
ITR0 as internal trigger. You select this through the TS bits in the TIM3_SMCR register (writing
TS=000).
Then the Timer2’s slave mode controller should be configured in external clock mode 1 (write SMS=111
in the TIM3_SMCR register). This causes Timer 3 to be clocked by the rising edge of the periodic Timer
1 trigger signal (which correspond to the timer 1 counter overflow).
Finally both timers must be enabled by setting their respective CEN bits within their respective
TIMx_CR1 registers.
Note: If Ocx is selected on Timer 1 as trigger output (MMS=1xx), its rising edge is used to clock the counter
of timer 2.
CK_CNT
TIM1-OC1REF
TIM1-CNT FC FD FE FF 00 01
TIF
Write TIF=0
Configure Timer 1 master mode to send its OC1REF as a trigger output (MMS=100, in the TIM1_CR2
register).
Configure the Timer 1 OC1REF waveform (TIM1_CCMR1 register).
Configure Timer 3 to get the input trigger from Timer 1 (TS=000 in the TIM3_SMCR register).
Configure Timer 3 in gated mode (SMS=101 in TIM3_SMCR register).
Reset Timer 1 by writing ‘1’ in UG bit (TIM1_EGR register).
Reset Timer 3 by writing ‘1’ in UG bit (TIM3_EGR register).
Initialize Timer 3 to 0xE7 by writing ‘0xE7’ in the timer 3 counter (TIM3_CNT).
Enable Timer 3 by writing ‘1’ in the CEN bit (TIM3_CR1 register).
Start Timer 1 by writing ‘1’ in the CEN bit (TIM1_CR1 register).
Stop Timer 1 by writing ‘0’ in the CEN bit (TIM1_CR1 register).
CK_CNT
TIM1-CEN=CNT_EN
TIM1-CNT_INIT
TIM1-CNT 75 00 01 02
TIM3-CNT AB 00 E7 E8 E9
TIM3-CNT_INT
TIM3-write CNT
TIM3-TIF
Write TIF=0
CK_CNT
TIM1-UEV
TIM1-CNT FD FE FF 00 01 02
TIM3-CNT 45 46 47 48
TIM3-CEN=CNT_EN
TIM3-TIF
Write TIF=0
CK_CNT
TIM1-CEN=CNT_EN
TIM1-CNT_INIT
TIM1-CNT 75 00 01 02
TIM3-CNT CD 00 E7 E8 E9 EA
TIM3-CNT_INT
TIM3-write CNT
TIM3-TIF
Write TIF=0
CK_CNT
TIM1-TI1
TIM1-CEN=CNT_EN
TIM1-CK_PSC
TIM1-CNT 00 01 02 03 04 05 06 07
TIM1-TIF
TIM3-CEN=CNT_EN
TIM3-CK_PSC
TIM3-CNT 00 01 02 03 04 05 06 07
TIM3-TIF
When the microcontroller enters debug mode (ARM® Cortex®-M0 core – halted), the TIMx counter either
continues to work normally or stops, depending on DBG_TIMx_STOP configuration bit in DBGMCU module.
The following table shows the TIM3 register map and reset values.
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
Address offset Name
0
CMS[1:0]
CKD[1:0]
ARPE
UDIS
OPM
URS
CEN
DIR
TIM3_CR1
–
–
0x00
Reset x x x x x x x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0
CCDS
TI1S
TIM3_CR2 MMS[2:0]
–
–
0x04
Reset x x x x x x x x x x x x x x x x x x x x x x x x 0 0 0 0 0 x x x
ETPS[1:0]
MSM
ECE
ETP
TIM3_SMCR ETF[3:0] TS[2:0] SMS[2:0]
–
–
0x08
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 x 0 0 0
CC4DE
CC3DE
CC2DE
CC1DE
CC4IE
CC3IE
CC2IE
CC1IE
UDE
TDE
UIE
TIE
TIM3_DIER
–
–
0x0C
Reset x x x x x x x x x x x x x x x x x 0 x 0 0 0 0 0 x 0 x 0 0 0 0 0
CC4OF
CC3OF
CC2OF
CC1OF
CC4IF
CC3IF
CC2IF
CC1IF
UIF
TIF
TIM3_SR
–
–
0x10
Reset x x x x x x x x x x x x x x x x x x x 0 0 0 0 x x 0 x 0 0 0 0 0
CC4G
CC3G
CC2G
CC1G
TG
UG
TIM3_EGR
–
–
0x14
Reset x x x x x x x x x x x x x x x x x x x x x x x x x 0 x 0 0 0 0 0
CC2S[1:0]
CC1S[1:0]
OC2CE
TIM3_CCMR
CC2PE
CC2FE
CC1CE
CC1PE
CC1FE
1 OC2M[2:0] OC1M[2:0]
–
(output mode)
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x18
CC2S[1:0]
CC1S[1:0]
IC2F[3:0]
IC1F[3:0]
TIM3_CCMR
IC2PSC IC1PSC
1
–
[1:0] [1:0]
(input mode)
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
CC4S[1:0]
CC3S[1:0]
OC4CE
TIM3_CCMR
CC4PE
CC4FE
CC3CE
CC3PE
CC3FE
2 OC4M[2:0] OC3M[2:0]
–
(output mode)
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x1C
CC4S[1:0]
CC3S[1:0]
IC4F[3:0]
IC3F[3:0]
TIM3_CCMR
IC4PSC IC3PSC
2
–
[1:0] [1:0]
(input mode)
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
CC3NP
CC2NP
CC1NP
CC4P
CC4E
CC3P
CC3E
CC2P
CC2E
CC1P
CC1E
TIM3_CCER
–
–
0x20
Reset x x x x x x x x x x x x x x x x x x 0 0 0 x 0 0 0 x 0 0 0 x 0 0
TIM3_CNT CNT[15:0]
–
–
0x24
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIM3_PSC PSC[15:0]
–
–
0x28
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIM3_ARR ARR[15:0]
–
–
0x2C
Reset x x x x x x x x x x x x x x x x 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
TIM3_CCR1 CCR1[15:0]
–
0x34
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIM3_CCR2 CCR2[15:0]
–
0x38
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIM3_CCR3 CCR3[15:0]
–
0x3C
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIM3_CCR4 CCR4[15:0]
–
0x40
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x48
Reset x x x x x x x x x x x x x x x x x x x 0 0 0 0 0 x x x 0 0 0 0 0
TIM3_DMAR DMAB[15:0]
–
0x4C
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
15:8 — CKD[1:0]
type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RW RW
7:0 ARPE CMS[1:0] DIR OPM URS UDIS CEN
type RW RW RW RW RW RW RW RW
This bit-field selects the trigger input to be used to synchronize the counter.
000: Internal Trigger 0 (ITR0)
001: Reserved
010: Internal Trigger 2 (ITR2)
011: Internal Trigger 3 (ITR3)
100: TI1 Edge Detector (TI1F_ED)
101: Filtered Timer Input 1 (TI1FP1)
110: Filtered Timer Input 2 (TI2FP2)
111: External Trigger input (ETRF)
Note: These bits must be changed only when they are not used (e.g. when
SMS=000) to avoid wrong edge detections at the transition.
3 OCCS OCREF clear selection.
This bit is used to select the OCREF clear source.
0:OCREF_CLR_INT is connected to the OCREF_CLR input
1: OCREF_CLR_INT is connected to ETRF
2:0 SMS[2:0] Slave mode selection
When external signals are selected the active edge of the trigger signal
(TRGI) is linked to the polarity selected on the external input (see Input
Control register and Control Register description.
000: clock/trigger controller disabled
–if CEN = ‘1’ then the prescaler is clocked directly by the internal clock.
001: Encoder mode 1
–Counter counts up/down on TI2FP1 edge depending on TI1FP2 level.
010: Encoder mode 2
–Counter counts up/down on TI1FP2 edge depending on TI2FP1 level.
011: Encoder mode 3
–Counter counts up/down on both TI1FP1 and TI2FP2 edges depending on
the level of the other input.
100: Reset Mode
–Rising edge of the selected trigger input (TRGI) reinitializes the counter
and generates an update of the registers
101: Gated Mode
–The counter clock is enabled when the trigger input (TRGI) is high. The
counter stops (but is not reset) as soon as the trigger becomes low. Both
start and stop of the counter are controlled.
110: Trigger Mode
–The counter starts at a rising edge of the trigger TRGI (but it is not reset).
Only the start of the counter is controlled
111: External Clock Mode 1
–Rising edges of the selected trigger (TRGI) clock the counter.
Note: The gated mode must not be used if TI1F_ED is selected as the
trigger input (TS=100). Indeed, TI1F_ED outputs 1 pulse for each transition
on TI1F, whereas the gated mode checks the level of the trigger signal.
input when the slave mode controller is enabled in all modes but gated
mode. It is set when the counter starts or stops when gated mode is
selected. It is cleared by software.
0: No trigger event occurred
1: Trigger interrupt pending
5 NA Reserved, undefined
4 CC4IF Capture/Compare 4 interrupt flag Refer to CC1IF description
3 CC3IF Capture/Compare 3 interrupt flag Refer to CC1IF description
2 CC2IF Capture/Compare 2 interrupt flag Refer to CC1IF description
1 CC1IF Capture/compare 1 interrupt flag
If channel CC1 is configured as output:
This flag is set by hardware when the counter matches the compare value,
with some exception in center-aligned mode (refer to the CMS bits in the
TIMx_CR1 register description). It is cleared by software.
0: No match
1: The content of the counter TIMx_CNT matches the content of the
TIMx_CCR1 register. When the contents of TIMx_CCR1 are greater than
the contents of TIMx_ARR, the CC1IF bit goes high on the counter overflow
(in upcounting and up/down-counting modes) or underflow (in downcounting
mode)
If channel CC1 is configured as input:
This bit is set by hardware on a capture. It is cleared by software or by
reading the TIMx_CCR1 register.
0: No input capture occurred
1: The counter value has been captured in TIMx_CCR1 register (An edge
has been detected on IC1 which matches the selected polarity)
0 UIF Update interrupt flag
This bit is set by hardware on an update event. It is cleared by software.
0: No update occurred.
1: Update interrupt pending.
− At overflow or underflow and if UDIS=0 in the TIMx_CR1 register;
− When CNT is reinitialized by software using the UG bit in TIMx_EGR
register, if URS=0 and UDIS=0 in the TIMx_CR1 register.
− When CNT is reinitialized by a trigger event (refer to the synchro control
register description), if URS=0 and UDIS=0 in the TIMx_CR1 register.
used input.
00: CC4 channel is configured as output
01: CC4 channel is configured as input, IC4 is mapped on TI4
10: CC4 channel is configured as input, IC4 is mapped on TI3
11: CC4 channel is configured as input, IC4 is mapped on TRC. This mode
is working only if an internal trigger input is selected through TS bit
(TIMx_SMCR register)
Note: CC4S bits are writable only when the channel is OFF (CC4E=0 in the
TIMx_CCER and and have been updated)
7 OC3CE Output compare 3 clear enable
6:4 OC3M[2:0] Output compare 3 mode
3 OC3PE Output compare 3 preload enable
2 OC3FE Output compare 3 fast enable
1:0 CC3S Capture/Compare 3 selection
This bit-field defines the direction of the channel (input/output) as well as the
used input.
00: CC3 channel is configured as output
01: CC3 channel is configured as input, IC3 is mapped on TI3
10: CC3 channel is configured as input, IC3 is mapped on TI4
11: CC3 channel is configured as input, IC3 is mapped on TRC. This mode
is working only if an internal trigger input is selected through TS bit
(TIMx_SMCR register)
Note: CC3S bits are writable only when the channel is OFF
(CC3E=0,CC3NE=0 in the TIMx_CCER and and have been updated)
15:8 CCR3[15:8]
type RW RW RW RW RW RW RW RW
7:0 CCR3[7:0]
type RW RW RW RW RW RW RW RW
The basic timer TIM6 consists of a 16-bit auto-reload counter driven by a programmable prescaler.
Auto-reload register UI
Stop,clear or up
U
CK_PSC PSC CK_CNT
+/- CNT counter
prescaler
Notes:
Preload registers transferred
Reg
to active registers on UEV event
according to control bit
event
The main block of the programmable timer is a 16-bit upcounter with its related auto-reload register. The
counter clock can be divided by a prescaler.
The counter, the auto-reload register and the prescaler register can be written or read by software. This is
true even when the counter is running.
The time-base unit includes:
Counter Register(TIMx_CNT)
Prescaler Register(TIMx_PSC)
Auto-Reload Register(TIMx_ARR)
The auto-reload register is preloaded. The preload register is accessed each time an attempt is made to
write or read the auto-reload register. The contents of the preload register are transferred into the shadow
register permanently or at each update event UEV, depending on the auto-reload preload enable bit (ARPE)
in the TIMx_CR1 register. The update event is sent when the counter reaches the overflow value and if the
UDIS bit equals 0 in the TIMx_CR1 register. It can also be generated by software.
The counter is clocked by the prescaler output CK_CNT, which is enabled only when the counter enable bit
(CEN) in the TIMx_CR1 register is set.
Note that the actual counter enable signal CNT_EN is set 1 clock cycle after CEN.
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 18-2 and Figure 18-3 give some examples of the counter behavior when the prescaler ratio is
changed on the fly.
CK_PSC
CEN
CK_CNT
Counter register F7 F8 F9 FA FB FC 00 01 02 03
UEV
Prescaler buffer 0 1
Prescaler counter 0 0 1 0 1 0 1 0 1
Figure 18-2 Counter timing diagram with prescaler division change from 1 to 4
CK_PSC
CEN
CK_CNT
Counter register F7 F8 F9 FA FB FC 00 01
UEV
Prescaler buffer 0 3
Prescaler counter 0 0 1 2 3 0 1 2 3
Figure 18-3 Counter timing diagram with prescaler division change from 1 to 4
The counter counts from 0 to the auto-reload value (contents of the TIMx_ARR register), then restarts from 0
and generates a counter overflow event. An update event can be generate at each counter overflow or by
setting the UG bit in the TIMx_EGR register.
The UEV event can be disabled by software by setting the UDIS bit in the TIMx_CR1 register. This avoids
updating the shadow registers while writing new values into the preload registers. In this way, no update
event occurs until the UDIS bit has been written to 0, however, the counter and the prescaler counter both
restart from 0 (but the prescale rate does not change). In addition, if the URS (update request selection) bit
in the TIMx_CR1 register is set, setting the UG bit generates an update event UEV, but the UIF flag is not
set (so no interrupt or DMA request is sent).
When an update event occurs, all the registers are updated and the update flag (UIF bit in the TIMx_SR
register) is set (depending on the URS bit):
The auto-reload shadow register is updated with the preload value (TIMx_ARR).
The buffer of the prescaler is reloaded with the preload value (contents 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
CK_CNT
Counter register 31 32 33 34 35 36 00 01 02 03 04 05 06 07
Counter overflow
UEV
UIF
CK_PSC
CNT_EN
CK_CNT
Counter register 34 35 36 00 01 02 03
Counter overflow
UEV
UIF
CNT_EN
CK_CNT
Counter register 35 36 00 01
Counter overflow
UEV
UIF
CK_PSC
CK_CNT
Counter register 35 36 00 01
Counter overflow
UEV
UIF
CK_PSC
CEN
CK_CNT
Counter register 31 32 33 34 35 36 00 01 02 03 04 05 06 07
Counter overflow
UEV
UIF
CK_PSC
CEN
CK_CNT
Counter register F0 F1 F2 F3 F4 F5 00 01 02 03 04 05 06 07
Counter overflow
UEV
UIF
The CEN (in the TIMx_CR1 register) and UG bits (in the TIMx_EGR register) are actual control bits and can
be changed only by software (except for UG that remains cleared automatically). As soon as the CEN bit is
written to 1, the prescaler is clocked by the internal clock CK_INT.
Figure 18-10 shows the behavior of the control circuit and the upcounter in normal mode, without prescaler.
Interal clock
CNT_EN
UG
CNT_INIT
CK_CNT=CK_PSC
Counter register 31 32 33 34 35 36 00 01 02 03 04 05 06 07
When the microcontroller enters the debug mode (Cortex™-M0 core – halted), the TIMx counter either
continues to work normally or stops, depending on the DBG_TIMx_STOP configuration bit in the DBG
module.
The following table shows the TIM6 register map and reset values.
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
Address offset Name
9
8
7
6
5
4
3
2
1
0
ARPE
UDIS
OPM
CEN
URS
TIM6_CR1
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x00
Reset x x x x x x x x x x x x x x x x x x x x x x x x 0 x x x 0 0 0 0
UDE
UIE
TIM6_DIER
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x0C
Reset x x x x x x x x x x x x x x x x x x x x x x x 0 x x x x x x x 0
UIF
TIM6_SR
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x10
Reset x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x 0
UG
TIM6_EGR
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x14
Reset x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x 0
TIM6_CNT CNT[15:0]
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x24
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIM6_PSC PSC[15:0]
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x28
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIM6_ARR ARR[15:0]
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x2C
Reset x x x x x x x x x x x x x x x x 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1: Update interrupt pending. This bit is set by hardware when the registers
are updated:
− When the counter overflows, if UDIS = 0 in the TIMx_CR1 register
− When CNT is reinitialized by software using the UG bit in the
TIMx_EGR register, if URS = 0 and UDIS = 0 in the TIMx_CR1 register.
The TIM14 general-purpose timer consists of a 16-bit auto-reload counter driven by a programmable
prescaler.
It may be used for a variety of purposes, including measuring the pulse lengths of input signals (input
capture) or generating output waveforms (output compare, PWM).
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 TIM14 timer is completely independent, and does not share any resources.
Auto-reload register UI
Stop,clear or up
U
CK_PSC PSC CK_CNT
+/- CNT counter
prescaler
CC1I UEV
CC1I
OC1 TIMx_CH1
TIMx_CH1 Input Filter & TI1FP1 IC1 OC1REF
TI1 IC1PS output
Edge Prescaler Capture/Compare 1 Register
control
detector
Notes:
Preload registers transferred
Reg
to active registers on UEV event
according to control bit
event
The main block of the programmable advanced-control timer is a 16-bit counter with its related auto-reload
register. The counter can count up. The counter clock can be divided by a prescaler.
The counter, the auto-reload register and the prescaler register can be written or read by software. This is
true even when the counter is running.
The time-base unit includes:
Counter register(TIMx_CNT)
Prescaler register(TIMx_PSC)
Auto-reload register(TIMx_ARR)
The auto-reload register is preloaded. Writing to or reading from the auto-reload register accesses the
preload register. The content of the preload register are transferred into the shadow register permanently or
at each update event (UEV), depending on the auto-reload preload enable bit (ARPE) in TIMx_CR1 register.
The update event is sent when the counter reaches the overflow (or underflow when downcounting) and if
the UDIS bit equals 0 in the TIMx_CR1 register. It can also be generated by software. The generation of the
update event is described in detail for each configuration.
The counter is clocked by the prescaler output CK_CNT, which is enabled only when the counter enable bit
(CEN) in TIMx_CR1 register is set.
Note that the counter starts counting 1 clock cycle after setting the CEN bit in the TIMx_CR1 register.
Prescaler description
The prescaler can divide the counter clock frequency by any factor between 1 and 65536. It is based on a
16-bit counter controlled through a 16-bit register (in the TIMx_PSC register). It can be changed on the fly as
this control register is buffered. The new prescaler ratio is taken into account at the next update event.
Figure 19-2 and Figure 19-3 give some examples of the counter behavior when the prescaler ratio is
changed on the fly.
CK_PSC
CEN
CK_CNT
Counter register F7 F8 F9 FA FB FC 00 01 02 03
UEV
Prescaler buffer 0 1
Prescaler counter 0 0 1 0 1 0 1 0 1
Figure 19-2 Counter timing diagram with prescaler division change from 1 to 2
CK_PSC
CEN
CK_CNT
Counter register F7 F8 F9 FA FB FC 00 01
UEV
Prescaler buffer 0 3
Prescaler counter 0 0 1 2 3 0 1 2 3
Figure 19-3 Counter timing diagram with prescaler division change from 1 to 4
Upcounting mode
In upcounting mode, the counter counts from 0 to the auto-reload value (content of the TIMx_ARR register),
then restarts from 0 and generates a counter overflow event. Setting the UG bit in the TIMx_EGR register
also generates an update event.
The UEV event can be disabled by software by setting the UDIS bit in the TIMx_CR1 register. This is to
avoid updating the shadow registers while writing new values in the preload registers. Then no update event
occurs until the UDIS bit has been written to 0. However, the counter restarts from 0, as well as the counter
of the prescaler (but the prescale rate does not change). In addition, if the URS bit (update request selection)
in TIMx_CR1 register is set, setting the UG bit generates an update event UEV but without setting the UIF
flag (thus no interrupt is sent). This is to avoid generating both update and capture interrupts when clearing
the counter on the capture event.
When an update event occurs, all the registers are updated and the update flag (UIF bit in TIMx_SR register)
is set (depending on the URS bit):
The auto-reload shadow register is updated with the preload value (TIMx_ARR),
The buffer of the prescaler is reloaded with the preload value (content of the TIMx_PSC register).
The following figures show some examples of the counter behavior for different clock frequencies when
TIMx_ARR=0x36.
CK_PSC
CNT_EN
CK_CNT
Counter register 31 32 33 34 35 36 00 01 02 03 04 05 06 07
Counter overflow
UEV
UIF
CK_PSC
CNT_EN
CK_CNT
Counter register 34 35 36 00 01 02 03
Counter overflow
UEV
UIF
CK_PSC
CNT_EN
CK_CNT
Counter register 35 36 00 01
Counter overflow
UEV
UIF
CK_PSC
CK_CNT
Counter register 35 36 00 01
Counter overflow
UEV
UIF
CEN
CK_CNT
Counter register 31 32 33 34 35 36 00 01 02 03 04 05 06 07
Counter overflow
UEV
UIF
CEN
CK_CNT
Counter register F0 F1 F2 F3 F4 F5 00 01 02 03 04 05 06 07
Counter overflow
UEV
UIF
Figure 19-10 shows the behavior of the control circuit and the upcounter in normal mode, without prescaler.
Interal clock
CNT_EN
UG
CNT_INIT
CK_CNT=CK_PSC
Counter register 31 32 33 34 35 36 00 01 02 03 04 05 06 07
Each Capture/Compare channel is built around a capture/compare register (including a shadow register), a
input stage for capture (with digital filter, multiplexing and prescaler) and an output stage (with comparator
and output control).
The input stage samples the corresponding Tix input to generate a filtered signal TixF. Then, an edge
detector with polarity selection generates a signal (TixFPx) which can be used as trigger input by the slave
mode controller or as the capture command. It is prescaled before the capture register (IC1PS).
TI1F_ED
11
TI1 TI1F_Falling TI1FP1 IC1 Divider IC1PS
Filter Edge 01 /1,/2,/4,/8
fDTS TI1F_Rising
downcounter detector 00
ICF[3:0] CC1P/CC1NP
TIMx_CCMR1 TIMx_CCER CC1S[1:0] CCIE
TIMx_CCMR1 TIMx_CCER
APB Bus
MCU-peripheral interface
high 8
if 16-bit
low 8
S write CCR1H
write_in_progress
Read CCR1H S read_in_progress
Capture/Compare Preload Register R write CCR1L
Read CCR1L R CC1S[1]
capture_transfer compare_transfer output
CC1S[0]
CC1S[1] mode
OC1PE OC1PE
CC1S[0] Capture/Compare Shadow Register UEV
capture from time TIMx_CCMR1
IC1PS base unit
CC1E CNT>CCR1
Counter CNT=CCR1
CC1G
TIMx_EGR
To the master
mode controller
CC1P
TIMx_CCER CC1E TIMx_CCER
OC1M[2:0]
TIMx_CCMR1
In Input capture mode, the Capture/Compare Registers (TIMx_CCRx) are used to latch the value of the
counter after a transition detected by the corresponding Icx signal. When a capture occurs, the
corresponding CCXIF flag (TIMx_SR register) is set and an interrupt can be sent if it is enabled. If a capture
occurs while the CcxIF flag was already high, then the over-capture flag CcxOF (TIMx_SR register) is set.
CcxIF can be cleared by software by writing it to ‘0’ or by reading the captured data stored in the
TIMx_CCRx register. CcxOF is cleared when you write it to ‘0’.
The following example shows how to capture the counter value in TIMx_CCR1 when TI1 input rises. To do
this, use the following procedure:
Select the active input: TIMx_CCR1 must be linked to the TI1 input, so write the CC1S bits to ‘01’ in the
TIMx_CCMR1 register. As soon as CC1S becomes different from ‘00’, the channel is configured in input
mode and the TIMx_CCR1 register becomes read-only.
Program the input filter duration you need with respect to the signal you connect to the timer (when the
input is one of the Tix (IcxF bits in the TIMx_CCMRx register). Let us imagine that, when toggling, the
input signal is not stable during at must 5 internal clock cycles. We must program a filter duration longer
than these 5 clock cycles. We can validate a transition on TI1 when 8 consecutive samples with the
new level have been detected (sampled at fDTS frequency). Then write IC1F bits to ‘0011’ in the
TIMx_CCMR1 register.
Program the input prescaler. In our example, we wish the capture to be performed at each valid
transition, so the prescaler is disabled (write IC1PS bits to ‘00’ in the TIMx_CCMR1 register).
Enable capture from the counter into the capture register by setting the CC1E bit in the TIMx_CCER
register.
If needed, enable the related interrupt request by setting the CC1IE bit in the TIMx_DIER register.
When an input capture occurs:
The TIMx_CCR1 register gets the value of the counter on the active transition.
CC1IF flag is set (interrupt flag). CC1OF is also set if at least two consecutive captures occurred
whereas the flag was not cleared.
An interrupt is generated depending on the CC1IE bit.
In order to handle the overcapture, it is recommended to read the data before the overcapture flag. This is to
avoid missing an overcapture which could happen after reading the flag and before reading the data.
Note:IC interrupt requests can be generated by software by setting the corresponding CCxG bit in the
TIMx_EGR register.
In output mode (CCxS bits = ‘00’ in the TIMx_CCMRx register), each output compare signal (OCxREF and
then Ocx/OcxN) can be forced to active or inactive level directly by software, independently of any
comparison between the output compare register and the counter.
To force an output compare signal (OCXREF/Ocx) to its active level, you just need to write ‘101’ in the OcxM
bits in the corresponding TIMx_CCMRx register. Thus OCXREF is forced high (OCxREF is always active
high) and Ocx get opposite value to CCxP polarity bit.
For example: CCxP=’0’ (Ocx active high) => Ocx is forced to high level.
The OCxREF signal can be forced low by writing the OcxM bits to ‘100’ in the TIMx_CCMRx register.
The comparison between the TIMx_CCRx shadow register and the counter is still performed and allows the
flag to be set. Interrupt requests can be sent accordingly.
This function is used to control an output waveform or to indicate when a period of time has elapsed.
When a match is found between the capture/compare register and the counter, the output compare function:
Assigns the corresponding output pin to a programmable value defined by the output compare mode
(OcxM bits in the TIMx_CCMRx register) and the output polarity (CCxP bit in the TIMx_CCER register).
The output pin can keep its level (OCXM=’000’), be set active (OcxM=’001’), be set inactive
(OcxM=’010’) or can toggle (OcxM=’011’) on match.
Sets a flag in the interrupt status register (CcxIF bit in the TIMx_SR register).
Generates an interrupt if the corresponding interrupt mask is set (CCXIE bit in the TIMx_DIER register).
The TIMx_CCRx registers can be programmed with or without preload registers using the OcxPE bit in the
TIMx_CCMRx register.
In output compare mode, the update event UEV has no effect on OCxREF and Ocx output. The timing
resolution is one count of the counter. Output compare mode can also be used to output a single pulse (in
One-pulse mode).
Procedure:
1. Select the counter clock (internal, external, prescaler).
2. Write the desired data in the TIMx_ARR and TIMx_CCRx registers.
3. Set the CcxIE bit if an interrupt request is to be generated.
4. Select the output mode. For example:
− Write OcxM = ‘011’ to toggle Ocx output pin when CNT matches CCRx
− Write OcxPE = ‘0’ to disable preload register
− Write CCxP = ‘0’ to select active high polarity
− Write CcxE = ‘1’ to enable the output
5. Enable the counter by setting the CEN bit in the TIMx_CR1 register.
The TIMx_CCRx register can be updated at any time by software to control the output waveform, provided
that the preload register is not enabled (OcxPE=’0’, else TIMx_CCRx shadow register is updated only at the
next update event UEV). An example is given in Figure 19-14.
oc1ref=OC1
Pulse Width Modulation mode allows you to generate a signal with a frequency determined by the value of
the TIMx_ARR register and a duty cycle determined by the value of the TIMx_CCRx register.
The PWM mode can be selected independently on each channel (one PWM per Ocx output) by writing ‘110’
(PWM mode 1) or ‘111’ (PWM mode 2) in the OcxM bits in the TIMx_CCMRx register. You must enable the
corresponding preload register by setting the OcxPE bit in the TIMx_CCMRx register, and eventually the
auto-reload preload register (in upcounting or center-aligned modes) by setting the ARPE bit in the
TIMx_CR1 register.
As the preload registers are transferred to the shadow registers only when an update event occurs, before
starting the counter, you have to initialize all the registers by setting the UG bit in the TIMx_EGR register.
The Ocx polarity is software programmable using the CCxP bit in the TIMx_CCER register. It can be
programmed as active high or active low. Ocx output is enabled by a combination of the CcxE, CcxNE, MOE,
OSSI and OSSR bits (TIMx_CCER and TIMx_BDTR registers).
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).
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
When the microcontroller enters debug mode (Cortex™-M0 core halted), the TIMx counter either continues
to work normally or stops, depending on DBG_TIMx_STOP configuration bit in DBG module.
The following table shows the TIM14 register map and reset values.
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
Address offset Name
0
CKD[1:0]
ARPE
UDIS
URS
CEN
TIM14_CR1
–
–
0x00
Reset x x x x x x x x x x x x x x x x x x x x x x 0 0 0 x x x x 0 0 0
CC1IE
UIE
TIM14_DIER
–
–
0x0C
Reset x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x 0 0
CC1OF
CC1IF
UIF
TIM14_SR
–
–
0x10
Reset x x x x x x x x x x x x x x x x x x x x x x 0 x x x x x x x 0 0
CC1G
UG
TIM14_EGR
–
–
0x14
Reset x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x 0 0
CC1S[1:0]
TIM14_
CC1PE
CC1FE
CCMR1 OC1M[2:0]
–
–
(output mode)
Reset x x x x x x x x x x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0
0x18
CC1S[1:0]
IC1F[3:0]
TIM14_
IC1PSC
CCMR1
–
–
[1:0]
(input mode)
Reset x x x x x x x x x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0
CC1NP
CC1P
CC1E
TIM14_CCER
–
–
0x20
Reset x x x x x x x x x x x x x x x x x x x x x x x x x x x x 0 x 0 0
TIM14_CNT CNT[15:0]
–
0x24
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIM14_PSC PSC[15:0]
–
0x28
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIM14_ARR ARR[15:0]
–
0x2C
Reset x x x x x x x x x x x x x x x x 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
TIM14_CCR1 CCR1[15:0]
–
0x34
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TI1_RMP
TIM14_OR
–
0x50
Reset x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x 0 0
31:24 —
type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
23:16 —
type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
15:8 — CKD[1:0]
type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RW RW
7:0 ARPE — URS UDIS CEN
type RW RO-0 RO-0 RO-0 RO-0 RW RW RW
which OC1 is derived. OC1REF is active high whereas OC1 active level
depends on CC1P and CC1NP bits.
000: Frozen. The comparison between the output compare register
TIMx_CCR1 and the counter TIMx_CNT has no effect on the outputs.
001: Set channel 1 to active level on match. OC1REF signal is forced high
when the counter TIMx_CNT matches the capture/compare register 1
(TIMx_CCR1).
010: Set channel 1 to inactive level on match. OC1REF signal is forced low
when the counter TIMx_CNT matches the capture/compare register 1
(TIMx_CCR1).
011: Toggle – OC1REF toggles when TIMx_CNT = TIMx_CCR1.
100: Force inactive level – OC1REF is forced low.
101: Force active level – OC1REF is forced high.
110: PWM mode 1
- In upcounting, channel 1 is active as long as TIMx_CNT<TIMx_CCR1
else inactive
In downcounting, channel 1 is inactive (OC1REF=‘0’) as long as
TIMx_CNT>TIMx_CCR1 else active (OC1REF=’1’)
111: PWM mode 2
- In upcounting, In upcounting, channel 1 is inactive as long as
TIMx_CNT<TIMx_CCR1 else active.
In downcounting, channel 1 is active as long as TIMx_CNT>TIMx_CCR1
else inactive.
Note: In PWM mode 1 or 2, the OCREF level changes only when the result
of the comparison changes or when the output compare mode switches from
“frozen” mode to “PWM” mode.
3 OC1PE Output compare 1 preload enable
0: Preload register on TIMx_CCR1 disabled. TIMx_CCR1 can be written at
anytime, the new value is taken in account immediately.
1: Preload register on TIMx_CCR1 enabled. Read/Write operations access
the preload register. TIMx_CCR1 preload value is loaded in the active
register at each update event.
2 OC1FE Output compare 1 fast enable
This bit is used to accelerate the effect of an event on the trigger in input on
the CC output.
0: CC1 behaves normally depending on counter and CCR1 values even
when the trigger is ON. The minimum delay to activate CC1 output when an
edge occurs on the trigger input is 5 clock cycles.
1: An active edge on the trigger input acts like a compare match on CC1
output. OC is then set to the compare level independently of the result of the
comparison. Delay to sample the trigger input and to activate CC1 output is
reduced to 3 clock cycles.
Note: OC1FE acts only if the channel is configured in PWM1 or PWM2
mode.
1:0 CC1S[1:0] Capture/Compare 1 selection
This bit-field defines the direction of the channel (input/output) as well as the
used input.
00: CC1 channel is configured as output.
01: CC1 channel is configured as input, IC1 is mapped on TI1.
10: Reserved
11: Reserved
Note: CC1S bits are writable only when the channel is OFF (CC1E = 0 in
TIMx_CCER).
encoder mode).
10:Reserved, do not use this configuration.
11:11: non-inverted/both edges
The circuit is sensitive to both TixFP1 rising and falling edges (capture or
trigger operations in reset, external clock or trigger mode), TixFP1 is not
inverted (in gated mode or encoder mode ).
0 CC1E Capture/Compare 1 output enable.
CC1 channel configured as output:
0: Off – OC1 is not active
1: On – OC1 signal is output on the corresponding output pin
CC1 channel configured as input:
This bit determines if a capture of the counter value can actually be done
into the input capture/compare register 1 (TIMx_CCR1) or not.
0: Capture disabled
1: Capture enabled
Table 19-1 Output control bits for complementary Ocx and OcxN channels with break feature
CcxE bit Ocx output state
0 Output Disabled(Ocx=0,Ocx_EN=0)
1 Ocx=OCxREF + Polarity, Ocx_EN=1
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.
in the TIMx_CCMR1 register (bit OC1PE). Else the preload value is copied
in the active capture/compare 1 register when an update event occurs.
The active capture/compare register contains the value to be compared to
the counter TIMx_CNT and signaled on OC1 output.
If channel CC1is configured as input:
CCR1 is the counter value transferred by the last input capture 1 event (IC1).
The TIM15/16/17 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 TIM15/16/17 timers are completely independent, and do not share any resources. They can be
synchronized together.
TI1FP2
REP register
Auto-reload register UI
Repetition
Stop,clear or up/down counter U
CK_PSC PSC CK_CNT
+/- CNT counter
prescaler
DTG registers
CC1I UEV
CC1I
TI1FP1 OC1 TIMx_CH1
TIMx_CH1 TI1 Input Filter &
TI1FP2 IC1 IC1PS OC1REF output
Edge Prescaler Capture/Compare 1 Register DTG TIMx_CH1N
control OC1N
detector TRC
CC2I UEV
CC2I
TIMx_CH2 TI2 Input Filter & TI2FP1 IC2 IC2PS OC2REF output OC2 TIMx_CH2
Edge TI2FP2 Prescaler Capture/Compare 2 Register
control
detector TRC
ETRF
TIMx_BKIN BRK Polarity BI
Selection
Notes:
Preload registers transferred
Reg
to active registers on UEV event
according to control bit
event
Internal Clock(CK_INT)
Counter Enable(CEN)
REP register
Auto-reload register UI
Repetition
Stop,clear or up counter U
CK_PSC PSC CK_CNT
+/- CNT counter
prescaler
DTG registers
CC1I UEV CC1I
OC1 TIMx_CH1
TIMx_CH1 TI1 Input Filter & TI1FP1 IC1 IC1PS OC1REF output
Edge Prescaler Capture/Compare 1 Register DTG TIMx_CH1N
control OC1N
detector
Notes:
Preload registers transferred
Reg
to active registers on UEV event
according to control bit
event
The main block of the programmable advanced-control timer is a 16-bit counter with its related auto-reload
register. The counter can count up. The counter clock can be divided by a prescaler.
The counter, the auto-reload register and the prescaler register can be written or read by software. This is
true even when the counter is running.
The time-base unit includes:
Counter register (TIMx_CNT)
Prescaler register (TIMx_PSC)
Auto-reload register (TIMx_ARR)
Repetition counter register (TIMx_RCR)
The content of the preload register are transferred into the shadow register permanently or at each update
event (UEV), depending on the auto-reload preload enable bit (ARPE) in TIMx_CR1 register. The update
event is sent when the counter reaches the overflow and if the UDIS bit equals 0 in the TIMx_CR1 register. It
can also be generated by software. The generation of the update event is described in detailed for each
configuration.
The counter is clocked by the prescaler output CK_CNT, which is enabled only when the counter enable bit
(CEN) in TIMx_CR1 register is set (refer also to the slave mode controller description to get more details on
counter enabling).
Note that the counter starts counting 1 clock cycle after setting the CEN bit in the TIMx_CR1 register.
Prescaler description
The prescaler can divide the counter clock frequency by any factor between 1 and 65536. It is based on a
16-bit counter controlled through a 16-bit register (in the TIMx_PSC register). It can be changed on the fly as
this control register is buffered. The new prescaler ratio is taken into account at the next update event.
Figure 20-3 and Figure 20-4 ive some examples of the counter behavior when the prescaler ratio is
changed on the fly:
CK_PSC
CEN
CK_CNT
Counter register F7 F8 F9 FA FB FC 00 01 02 03
UEV
Prescaler buffer 0 1
Prescaler counter 0 0 1 0 1 0 1 0 1
Figure 20-3 Counter timing diagram with prescaler division change from 1 to 2
CK_PSC
CEN
CK_CNT
Counter register F7 F8 F9 FA FB FC 00 01
UEV
Prescaler buffer 0 3
Prescaler counter 0 0 1 2 3 0 1 2 3
Figure 20-4 Counter timing diagram with prescaler division change from 1 to 4
Upcounting mode
In upcounting mode, the counter counts from 0 to the auto-reload value (content of the TIMx_ARR register),
then restarts from 0 and generates a counter overflow event.
If the repetition counter is used, the update event (UEV) is generated after upcounting is repeated for the
number of times programmed in the repetition counter register (TIMx_RCR). Else the update event is
generated at each counter overflow.
Setting the UG bit in the TIMx_EGR register (by software or by using the slave mode controller) also
generates an update event.
The UEV event can be disabled by software by setting the UDIS bit in the TIMx_CR1 register. This is to
avoid updating the shadow registers while writing new values in the preload registers. Then no update event
occurs until the UDIS bit has been written to 0. However, the counter restarts from 0, as well as the counter
of the prescaler (but the prescale rate does not change). In addition, if the URS bit (update request selection)
in TIMx_CR1 register is set, setting the UG bit generates an update event UEV but without setting the UIF
flag (thus no interrupt or DMA request is sent). This is to avoid generating both update and capture interrupts
when clearing the counter on the capture event.
When an update event occurs, all the registers are updated and the update flag (UIF bit in TIMx_SR register)
is set (depending on the URS bit):
The repetition counter is reloaded with the content of TIMx_RCR register,
The auto-reload shadow register is updated with the preload value (TIMx_ARR),
The buffer of the prescaler is reloaded with the preload value (content of the TIMx_PSC register).
The following figures show some examples of the counter behavior for different clock frequencies when
TIMx_ARR=0x36.
CK_PSC
CNT_EN
CK_CNT
Counter register 31 32 33 34 35 36 00 01 02 03 04 05 06 07
Counter overflow
UEV
UIF
CK_PSC
CNT_EN
CK_CNT
Counter register 34 35 36 00 01 02 03
Counter overflow
UEV
UIF
CK_PSC
CNT_EN
CK_CNT
Counter register 35 36 00 01
Counter overflow
UEV
UIF
CK_PSC
CK_CNT
Counter register 35 36 00 01
Counter overflow
UEV
UIF
CEN
CK_CNT
Counter register 31 32 33 34 35 36 00 01 02 03 04 05 06 07
Counter overflow
UEV
UIF
CK_PSC
CEN
CK_CNT
Counter register F0 F1 F2 F3 F4 F5 00 01 02 03 04 05 06 07
Counter overflow
UEV
UIF
Section 20.4.1: Time-base unit describes how the update event (UEV) is generated with respect to the
counter overflows/underflows. It is actually generated only when the repetition counter has reached zero.
This can be useful when generating PWM signals.
This means that data are transferred from the preload registers to the shadow registers (TIMx_ARR
auto-reload register, TIMx_PSC prescaler register, but also TIMx_CCRx capture/compare registers in
compare mode) every N counter overflows or underflows, where N is the value in the TIMx_RCR repetition
counter register.
The repetition counter is decremented at each counter overflow in upcounting mode
The repetition counter is an auto-reload type; the repetition rate is maintained as defined by the TIMx_RCR
register value. When the update event is generated by software (by setting the UG bit in TIMx_EGR register)
or by hardware through the slave mode controller, it occurs immediately whatever the value of the repetition
counter is and the repetition counter is reloaded with the content of the TIMx_RCR register.
Upcounting
TIMx_RCR=0 UEV
TIMx_RCR=1 UEV
TIMx_RCR=2 UEV
TIMx_RCR=3 UEV
TIMx_RCR=3
and UEV
re-synchronization (by SW)
Update event: Preload registers transferred to
UEV
active registers and update interrupt generated
Figure 20-11 Update rate examples depending on mode and TIMx_RCR register settings
Interal clock
CNT_EN
UG
CNT_INIT
CK_CNT=CK_PSC
Counter register 31 32 33 34 35 36 00 01 02 03 04 05 06 07
ITRx
0xx
TI1_ED
100
TRGI External
TI1FP1 clock mode 1
101
TI2F_rising
TI2 Edge 0 TI2FP2 CK_PSC
Filter TI2F_falling 1 110
Detector
ETRF
111
CK_INT Internal clock
ICF[3:0] CC2P mode
(internal clock)
TIMx_CCMR1 TIMx_CCER
SMS[2:0]
TIMx_SMCR
For example, to configure the upcounter to count in response to a rising edge on the TI2 input, use the
following procedure:
1. Configure channel 2 to detect rising edges on the TI2 input by writing CC2S = ‘01’ in the TIMx_CCMR1
register.
2. Configure the input filter duration by writing the IC2F[3:0] bits in the TIMx_CCMR1 register (if no filter is
needed, keep IC2F=0000).
3. Select rising edge polarity by writing CC2P=0 in the TIMx_CCER register.
4. Configure the timer in external clock mode 1 by writing SMS=111 in the TIMx_SMCR register.
5. Select TI2 as the trigger input source by writing TS=110 in the TIMx_SMCR register.
6. Enable the counter by writing CEN=1 in the TIMx_CR1 register.
Note:The capture prescaler is not used for triggering, so you don’t need to configure it.
When a rising edge occurs on TI2, the counter counts once and the TIF flag is set.
The delay between the rising edge on TI2 and the actual clock of the counter is due to the resynchronization
circuit on TI2 input.
TI2
CEN
CK_CNT=CK_PSC
Counter register 34 35 36 0
TIF
Write TIF=0
Each Capture/Compare channel is built around a capture/compare register (including a shadow register), a
input stage for capture (with digital filter, multiplexing and prescaler) and an output stage (with comparator
and output control).
The input stage samples the corresponding Tix input to generate a filtered signal TixF. Then, an edge
detector with polarity selection generates a signal (TixFPx) which can be used as trigger input by the slave
mode controller or as the capture command. It is prescaled before the capture register (IcxPS).
TI1F_ED
To the slave mode controller
TI1 TI1F_Rising
Filter Edge 0 TI1FP1
fDTS
downcounter detector TI1F_Falling 1
01
TI2FP1 IC1 Divider IC1PS
10
ICF[3:0] /1,/2,/4,/8
CC1P/CC1NP TRC
TIMx_CCMR1 11
(from slave
TIMx_CCER mode controller)
TI2F_Rising
(from channel 2) 0
CC1S[1:0] ICPS[1:0] CCIE
TI2F_Falling 1
(from channel 2)
TIMx_CCMR1 TIMx_CCER
APB Bus
MCU-peripheral interface
high 8
if 16-bit
low 8
S write CCR1H
write_in_progress
Read CCR1H S read_in_progress
Capture/Compare Preload Register R write CCR1L
Read CCR1L R CC1S[1]
capture_transfer compare_transfer output
CC1S[0]
CC1S[1] mode
OC1PE OC1PE
CC1S[0] Capture/Compare Shadow Register UEV
IC1PS capture from time TIMx_CCMR1
CNT>CCR1 base unit
CC1E
Counter CNT=CCR1
CC1G
TIMx_EGR
TIMx_SMCR
OCCS
To the master
mode controller
CC2P
TIMx_CCER CC2E TIMx_CCER
OC2M[2:0]
TIMx_CCMR2 MOE OSSI TIMx_BDTR
OIS2 TIMx_CR2
In compare mode, the content of the preload register is copied into the shadow register which is compared
to the counter.
In Input capture mode, the Capture/Compare Registers (TIMx_CCRx) are used to latch the value of the
counter after a transition detected by the corresponding Icx signal. When a capture occurs, the
corresponding CCXIF flag (TIMx_SR register) is set and an interrupt or a DMA request can be sent if they
are enabled. If a capture occurs while the CcxIF flag was already high, then the over-capture flag CcxOF
(TIMx_SR register) is set. CcxIF can be cleared by software by writing it to ‘0’ or by reading the captured
data stored in the TIMx_CCRx register. CcxOF is cleared when you write it to ‘0’.
The following example shows how to capture the counter value in TIMx_CCR1 when TI1 input rises. To do
this, use the following procedure:
Select the active input: TIMx_CCR1 must be linked to the TI1 input, so write the CC1S bits to 01 in the
TIMx_CCMR1 register. As soon as CC1S becomes different from 00, the channel is configured in input
and the TIMx_CCR1 register becomes read-only.
Program the input filter duration you need with respect to the signal you connect to the timer when the
input is one of the Tix (IcxF bits in the TIMx_CCMRx register). Let’s imagine that, when toggling, the
input signal is not stable during at must 5 internal clock cycles. We must program a filter duration longer
than these 5 clock cycles. We can validate a transition on TI1 when 8 consecutive samples with the
new level have been detected (sampled at f DTS frequency). Then write IC1F bits to 0011 in the
TIMx_CCMR1 register.
Program the input prescaler. In our example, we wish the capture to be performed at each valid
transition, so the prescaler is disabled (write IC1PS bits to ‘00’ in the TIMx_CCMR1 register).
Enable capture from the counter into the capture register by setting the CC1E bit in the TIMx_CCER
register.
If needed, enable the related interrupt request by setting the CC1IE bit in the TIMx_DIER register,
and/or the DMA request by setting the CC1DE bit in the TIMx_DIER register.
When an input capture occurs:
The TIMx_CCR1 register gets the value of the counter on the active transition.
CC1IF flag is set (interrupt flag). CC1OF is also set if at least two consecutive captures occurred
whereas the flag was not cleared.
An interrupt is generated depending on the CC1IE bit.
A DMA request is generated depending on the CC1DE bit.
In order to handle the overcapture, it is recommended to read the data before the overcapture flag. This is to
avoid missing an overcapture which could happen after reading the flag and before reading the data.
Note:IC interrupt and/or DMA requests can be generated by software by setting the corresponding CCxG bit
in the TIMx_EGR register.
This mode is a particular case of input capture mode. The procedure is the same except:
TI1
IC1/IC2
COUNTER
5 0 1 2 3 4 5 0
T1CCR1 5
T1CCR2 3
In output mode (CCxS bits = 00 in the TIMx_CCMRx register), each output compare signal (OCxREF and
then Ocx/OcxN) can be forced to active or inactive level directly by software, independently of any
comparison between the output compare register and the counter.
To force an output compare signal (OCXREF/Ocx) to its active level, you just need to write 101 in the OcxM
bits in the corresponding TIMx_CCMRx register. Thus OCXREF is forced high (OCxREF is always active
high) and Ocx get opposite value to CCxP polarity bit.
The OCxREF signal can be forced low by writing the OcxM bits to 100 in the TIMx_CCMRx register.
Anyway, the comparison between the TIMx_CCRx shadow register and the counter is still performed and
allows the flag to be set. Interrupt and DMA requests can be sent accordingly.
This function is used to control an output waveform or indicating when a period of time has elapsed.
When a match is found between the capture/compare register and the counter, the output compare function:
Assigns the corresponding output pin to a programmable value defined by the output compare mode
(OcxM bits in the TIMx_CCMRx register) and the output polarity (CCxP bit in the TIMx_CCER register).
The output pin can keep its level (OCXM=000), be set active (OcxM=001), be set inactive (OcxM=010)
or can toggle (OcxM=011) on match.
Sets a flag in the interrupt status register (CcxIF bit in the TIMx_SR register).
Generates an interrupt if the corresponding interrupt mask is set (CCXIE bit in the TIMx_DIER register).
Sends a DMA request if the corresponding enable bit is set (CcxDE bit in the TIMx_DIER register,
CCDS bit in the TIMx_CR2 register for the DMA request selection).
The TIMx_CCRx registers can be programmed with or without preload registers using the OcxPE bit in the
TIMx_CCMRx register.
In output compare mode, the update event UEV has no effect on OCxREF and Ocx output. The timing
resolution is one count of the counter. Output compare mode can also be used to output a single pulse (in
One-pulse mode).
Procedure:
1. Select the counter clock (internal, external, prescaler).
2. Write the desired data in the TIMx_ARR and TIMx_CCRx registers.
3. Set the CcxIE bit if an interrupt request is to be generated.
4. Select the output mode. For example:
− Write OcxM = 011 to toggle Ocx output pin when CNT matches CCRx
− Write OcxPE = 0 to disable preload register
− Write CCxP = 0 to select active high polarity
− Write CcxE = 1 to enable the output
5. Enable the counter by setting the CEN bit in the TIMx_CR1 register.
The TIMx_CCRx register can be updated at any time by software to control the output waveform, provided
that the preload register is not enabled (OcxPE=’0’, else TIMx_CCRx shadow register is updated only at the
next update event UEV). An example is given in Figure 20-20.
oc1ref=OC1
Pulse Width Modulation mode allows you to generate a signal with a frequency determined by the value of
the TIMx_ARR register and a duty cycle determined by the value of the TIMx_CCRx register.
The PWM mode can be selected independently on each channel (one PWM per Ocx output) by writing ‘110’
(PWM mode 1) or ‘111’ (PWM mode 2) in the OcxM bits in the TIMx_CCMRx register. You must enable the
corresponding preload register by setting the OcxPE bit in the TIMx_CCMRx register, and eventually the
auto-reload preload register (in upcounting or center-aligned modes) by setting the ARPE bit in the
TIMx_CR1 register.
As the preload registers are transferred to the shadow registers only when an update event occurs, before
starting the counter, you have to initialize all the registers by setting the UG bit in the TIMx_EGR register.
Ocx polarity is software programmable using the CCxP bit in the TIMx_CCER register. It can be
programmed as active high or active low. Ocx output is enabled by a combination of the CcxE, CcxNE, MOE,
OSSI and OSSR bits (TIMx_CCER and TIMx_BDTR registers).
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).
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
The TIM15/16/17 general-purpose timers can output two complementary signals and manage the
switching-off and the switching-on instants of the outputs.
This time is generally known as dead-time and you have to adjust it depending on the devices you have
connected to the outputs and their characteristics (intrinsic delays of level-shifters, delays due to power
switches...)
You can select the polarity of the outputs (main output Ocx or complementary OcxN) independently for each
output. This is done by writing to the CCxP and CCxNP bits in the TIMx_CCER register.
The complementary signals Ocx and OcxN are activated by a combination of several control bits: the CcxE
and CcxNE bits in the TIMx_CCER register and the MOE, OISx, OISxN, OSSI and OSSR bits in the
TIMx_BDTR and TIMx_CR2 registers. Refer to Table : Output control bits for complementary Ocx and OcxN
channels with break feature for more details. In particular, the dead-time is activated when switching to the
IDLE state (MOE falling down to 0).
Dead-time insertion is enabled by setting both CcxE and CcxNE bits, and the MOE bit if the break circuit is
present. There is one 10-bit dead-time generator for each channel. From a reference waveform OCxREF, it
generates 2 outputs Ocx and OcxN. If Ocx and OcxN are active high:
The Ocx output signal is the same as the reference signal except for the rising edge, which is delayed
relative to the reference rising edge.
The OcxN output signal is the opposite of the reference signal except for the rising edge, which is
delayed relative to the reference falling edge.
If the delay is greater than the width of the active output (Ocx or OcxN) then the corresponding pulse is not
generated.
The following figures show the relationships between the output signals of the dead-time generator and the
reference signal OCxREF. (we suppose CCxP=0, CCxNP=0, MOE=1, CcxE=1 and CcxNE=1 in these
examples)
OCxREF
delay
OCx
OCxN
delay
OCxREF
delay
OCx
OCxN
Figure 20-23 Dead-time waveforms with delay greater than the negative pulse
OCxREF
OCx
delay
OCxN
Figure 20-24 Dead-time waveforms with delay greater than the positive pulse
The dead-time delay is the same for each of the channels and is programmable with the DTG bits in the
TIMx_BDTR register. Refer to Section.Refer to Section 20.5.15: TIM15 break and dead-time register
(TIM15_BDTR) for delay calculation.
OcxN are enabled (CcxE=CcxNE=1) Ocx becomes active when OCxREF is high whereas OcxN is
complemented and becomes active when OCxREF is low.
When using the break function, the output enable signals and inactive levels are modified according to
additional control bits (MOE, OSSI and OSSR bits in the TIMx_BDTR register, OISx and OISxN bits in the
TIMx_CR2 register). In any case, the Ocx and OcxN outputs cannot be set both to active level at a given
time. Refer to Table : Output control bits for complementary Ocx and OcxN channels with break feature for
more details.
The break source can be either the break input pin or one of the internal break sources as shown below:
Core’s LOCKUP output
PVD output
A clock failure event is detected by Clock Security System (CSS)
When exiting from reset, the break circuit is disabled and the MOE bit is low. You can enable the break
function by setting the BKE bit in the TIMx_BDTR register. The break input polarity can be selected by
configuring the BKP bit in the same register. BKE and BKP can be modified at the same time. When the BKE
and BKP bits are written, a delay of 1 APB clock cycle is applied before the writing is effective. Consequently,
it is necessary to wait 1 APB clock period to correctly read back the bit after the write operation.
Because MOE falling edge can be asynchronous, a resynchronization circuit has been inserted between the
actual signal (acting on the outputs) and the synchronous control bit (accessed in the TIMx_BDTR register).
It results in some delays between the asynchronous and the synchronous signals. In particular, if you write
MOE to 1 whereas it was low, you must insert a delay (dummy instruction) before reading it correctly. This is
because you write the asynchronous signal and read the synchronous signal.
When a break occurs (selected level on the break input):
The MOE bit is cleared asynchronously, putting the outputs in inactive state, idle state or in reset state
(selected by the OSSI bit). This feature functions even if the MCU oscillator is off.
Each output channel is driven with the level programmed in the OISx bit in the TIMx_CR2 register as
soon as MOE=0. If OSSI=0 then the timer releases the enable output else the enable output remains
high.
When complementary outputs are used:
− The outputs are first put in reset state inactive state (depending on the polarity). This is done
asynchronously so that it works even if no clock is provided to the timer.
− If the timer clock is still present, then the dead-time generator is reactivated in order to drive the
outputs with the level programmed in the OISx and OISxN bits after a dead-time. Even in this case,
Ocx and OcxN cannot be driven to their active level together. Note that because of the
resynchronization on MOE, the dead-time duration is a bit longer than usual (around 2 ck_tim
clock cycles).
− If OSSI=0 then the timer releases the enable outputs else the enable outputs remain or become
high as soon as one of the CcxE or CcxNE bits is high.
The break status flag (BIF bit in the TIMx_SR register) is set. An interrupt can be generated if the BIE
bit in the TIMx_DIER register is set.
If the AOE bit in the TIMx_BDTR register is set, the MOE bit is automatically set again at the next
update event UEV. This can be used to perform a regulation, for instance. Else, MOE remains low until
you write it to ‘1’ again. In this case, it can be used for security and you can connect the break input to
an alarm from power drivers, thermal sensors or any security components.
Note:The break inputs is acting on level. Thus, the MOE cannot be set while the break input is active
(neither automatically nor by software). In the meantime, the status flag BIF cannot be cleared.
The break can be generated by the BRK input which has a programmable polarity and an enable bit BKE in
the TIMx_BDTR Register.
In addition to the break input and the output management, a write protection has been implemented inside
the break circuit to safeguard the application. It allows you to freeze the configuration of several parameters
(dead-time duration, Ocx/OcxN polarities and state when disabled, OcxM configurations, break enable and
polarity). You can choose from 3 levels of protection selected by the LOCK bits in the TIMx_BDTR register.
The LOCK bits can be written only once after an MCU reset.
The Figure 20-25 shows an example of behavior of the outputs in response to a break.
OCxREF
OCx
(OCxN not implemented,CCxP=0,OISx=1)
OCx
(OCxN not implemented,CCxP=0,OISx=0)
OCx
(OCxN not implemented,CCxP=1,OISx=1)
OCx
(OCxN not implemented,CCxP=1,OISx=0)
OCx
OCx
OCx
OCxN delay
(CCxE=1,CCxP=0,OISx=1,CCxNE=1,CCxNP=1,OISxN=1)
OCx
OCxN delay
(CCxE=1,CCxP=0,OISx=0,CCxNE=0,CCxNP=0,OISxN=1)
OCx
OCxN
(CCxE=1,CCxP=0,CCxNE=0,CCxNP=0,OISx=OISxN=0 or OISx=OISxN=1)
One-pulse mode (OPM) is a particular case of the previous modes. It allows the counter to be started in
response to a stimulus and to generate a pulse with a programmable length after a programmable delay.
Starting the counter can be controlled through the slave mode controller. Generating the waveform can be
done in output compare mode or PWM mode. You select One-pulse mode by setting the OPM bit in the
TIMx_CR1 register. This makes the counter stop automatically at the next update event UEV.
A pulse can be correctly generated only if the compare value is different from the counter initial value. Before
starting (when the timer is waiting for the trigger), the configuration must be:
TI2
OC1REF
OC1
TIM1_ARR
TIM1_CCR1
0 Tdelay TIME
Tpulse
The TIM15 timer can be synchronized with an external trigger in several modes: Reset mode, Gated mode
and Trigger mode.
TI1
UG
ck_cnt=ck_psc
Counter register 30 31 32 33 34 35 36 0 1 2 3 0 1 2 3
TIF
TI1
synchronization time
CEN
synchronization time
cnt_en
ck_cnt=ck_psc
Counter register 30 31 32 33 34 35 36 37 38
TIF
write TIF=0
TI2
synchorization tim e
CNT_EN
ck_cnt=ck_psc
Counter register 34 35 36 37
TIF
The TIM timers are linked together internally for timer synchronization or chaining. Refer to Section 17.3.15:
Timer synchronization for details.
When the microcontroller enters debug mode (Cortex™-M0 core halted), the TIMx counter either continues
to work normally or stops, depending on DBG_TIMx_STOP configuration bit in DBG module.
The following table shows the TIM15 register map and reset values.
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
Address offset Name
0
CKD[1:0]
ARPE
UDIS
OPM
URS
CEN
TIM15_CR1
–
–
0x00
Reset x x x x x x x x x x x x x x x x x x x x x x 0 0 0 x x x 0 0 0 0
OIS1N
CCDS
CCUS
CCPC
OIS2
OIS1
TIM15_CR2 MMS[2:0]
–
–
0x04
Reset x x x x x x x x x x x x x x x x x x x x x 1 1 1 x 0 0 0 0 0 x 0
MSM
TIM15_SMCR TS[2:0] SMS[2:0]
–
–
0x08
Reset x x x x x x x x x x x x x x x x x x x x x x x x 0 0 0 0 x 0 0 0
CC2DE
CC1DE
COMIE
CC2IE
CC1IE
UDE
TDE
UIE
BIE
TIE
TIM15_DIER
–
–
0x0C
Reset x x x x x x x x x x x x x x x x x 0 x x x 0 0 0 0 0 0 x x 0 0 0
CC2OF
CC1OF
COMIF
CC2IF
CC1IF
UIF
BIF
TIF
TIM15_SR
–
0x10
Reset x x x x x x x x x x x x x x x x x x x x x 0 0 x 0 0 0 x x 0 0 0
COMG
CC2G
CC1G
BG
TG
UG
TIM15_EGR
–
0x14
Reset x x x x x x x x x x x x x x x x x x x x x x x x 0 0 0 x x 0 0 0
CC2S[1:0]
CC1S[1:0]
TIM15
CC2PE
CC2FE
CC1PE
CC1FE
_CCMR1 OC2M[2:0] OC1M[2:0]
–
(output mode)
Reset x x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 x 0 0 0 0 0 0 0
0x18
CC2S[1:0]
CC1S[1:0]
IC2F[3:0]
IC1F[3:0]
TIM15
IC2PSC IC1PSC
_CCMR1
–
[1:0] [1:0]
(input mode)
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
CC2NP
CC1NP
CC1NE
CC2P
CC2E
CC1P
CC1E
TIM15_CCER
–
–
0x20
Reset x x x x x x x x x x x x x x x x x x x x x x x x 0 x 0 0 0 0 0 0
TIM15_CNT CNT[15:0]
–
–
0x24
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIM15_PSC PSC[15:0]
–
–
0x28
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIM15_ARR ARR[15:0]
–
–
0x2C
Reset x x x x x x x x x x x x x x x x 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
TIM15_RCR REP[7:0]
–
–
0x30
Reset x x x x x x x x x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0
TIM15_CCR1 CCR1[15:0]
–
0x34
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIM15_CCR2 CCR2[15:0]
–
0x38
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
LOCK[1:0]
OSSR
MOE
OSSI
AOE
BKP
BKE
TIM15_BDTR DTG[7:0]
–
0x44
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0x48
Reset x x x x x x x x x x x x x x x x x x x 0 0 0 0 0 x x x 0 0 0 0 0
TIM15_DMAR DMAB[15:0]
–
0x4C
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
7 NA Reserved, undefined
6:4 MMS[2:0] Master mode selection
These bits allow to select the information to be sent in master mode to slave
timers for synchronization (TRGO). The combination is as follows:
000: Reset – the UG bit from the TIMx_EGR register is used as trigger
output (TRGO). If the reset is generated by the trigger input
(SMS[2:0]=100) then the signal on TRGO is delayed compared to the
actual reset.
001: Enable – the Counter Enable signal CNT_EN is used as trigger output
(TRGO). It is useful to start several timers at the same time or to control a
window in which a slave timer is enable. The Counter Enable signal is
generated by a logic OR between CEN control bit and the trigger input when
configured in gated mode. When the Counter Enable signal is controlled by
the trigger input, there is a delay on TRGO, except if the master/slave mode
is selected (see the MSM bit description in TIMx_SMCR register).
010: Update – The update event is selected as trigger output (TRGO). For
instance a master timer can then be used as a prescaler for a slave timer.
011: Compare Pulse – The trigger output send a positive pulse when the
CC1IF flag is to be set (even if it was already high), as soon as a capture or
a compare match occurred. (TRGO).
100: Compare – OC1REF signal is used as trigger output (TRGO).
101: Compare – OC2REF signal is used as trigger output (TRGO).
110: Reserved
111: Reserved
3 CCDS Capture/compare DMA selection
0: CCx DMA request sent when CCx event occurs
1: CCx DMA requests sent when update event occurs
2 CCUS Capture/compare control update selection
0: When capture/compare control bits are preloaded (CCPC=1), they are
updated by setting the COMG bit only.
1: When capture/compare control bits are preloaded (CCPC=1), they are
updated by setting the COMG bit or when an rising edge occurs on TRGI.
Note: This bit acts only on channels that have a complementary output.
1 NA Reserved, undefined
0 CCPC Capture/compare preloaded control
0: CcxE, CcxNE, CCxP, CCxNP and OcxM bits are not preloaded
1: CcxE, CcxNE, CCxP, CCxNP and OcxM bits are preloaded, after having
been written, they are updated only when COMG bit set or rising edge detected
on TRGI, depending on the CCUS bit)
Note: This bit acts only on channels that have a complementary output.
Note: The gated mode must not be used if TI1F_ED is selected as the
trigger input (TS=’100’). Indeed, TI1F_ED outputs 1 pulse for each transition
on TI1F, whereas the gated mode checks the level of the trigger signal.
0: Off
- OC1N is not active. OC1N level is then function of MOE, OSSI, OSSR, OIS1,
OIS1N and CC1E bits.
1: On
- OC1N signal is output on the corresponding output pin depending on MOE,
OSSI, OSSR, OIS1, OIS1N and CC1E bits.
Note: On channels having a complementary output, this bit is preloaded. If the
CCPC bit is set in the TIMx_CR2 register then the CC1NE active bit takes the
new value from the preloaded bits only when a Commutation event is generated.
1 CC1P Capture/Compare 1 output polarity
CC1 channel configured as output:
0: OC1 active high
1: OC1 active low
CC1 channel configured as input:
The CC1NP/CC1P bits select the polarity of TI1FP1 and TI2FP1 for trigger or
capture operations.
00: noninverted/rising edge
circuit is sensitive to TixFP1’s rising edge (capture, trigger in reset or trigger
mode), TixFP1 is not inverted (in gated mode).
01: inverted/falling edge
circuit is sensitive to TixFP1’s falling edge (capture, trigger in reset,
or trigger mode), TixFP1 is inverted (in gated mode ).
10: reserved, do not use this configuration.
11: noninverted/both edges
The circuit is sensitive to both TixFP1 rising and falling edges((capture, trigger in
reset or trigger mode), TixFP1is not inverted (in gated mode ).
Note1: This bit is not writable as soon as LOCK level 2 or 3 has been
programmed (LOCK bits in TIMx_BDTR register).
Note2: On channels having a complementary output, this bit is preloaded. If the
CCPC bit is set in the TIMx_CR2 register then the CC1P active bit takes the new
value from the preloaded bits only when a Commutation event is generated
0 CC1E Capture/Compare 1 output enable
CC1 channel configured as output:
0: Off
- OC1 is not active. OC1 level is then function of MOE, OSSI, OSSR, OIS1,
OIS1N and CC1NE bits.
1: On
- OC1 signal is output on the corresponding output pin depending on MOE,
OSSI, OSSR, OIS1, OIS1N and CC1NE bits.
CC1 channel configured as input:
This bit determines if a capture of the counter value can actually be done into the
input capture/compare register 1 (TIMx_CCR1) or not.
0: Capture disabled.
1: Capture enabled.
Note: On channels having a complementary output, this bit is preloaded. If the
CCPC bit is set in the TIMx_CR2 register then the CC1E active bit takes the new
value from the preloaded bits only when a Commutation event is generated.
Table 20-2 Output control bits for complementary Ocx and OcxN channels with break feature
Control bits Output states(1)
MOE OSSI OSSR CcxE CcxNE Ocx output state OcxN output state
Output Disabled (not driven Output Disabled (not driven
0 0 0 by the timer) by the timer)
Ocx=0, Ocx_EN=0 OcxN=0, OcxN_EN=0
Output Disabled (not driven OCxREF + Polarity
0 0 1 by the timer) OcxN=OCxREF ^ CCxNP
Ocx=0, Ocx_EN=0 OcxN_EN=1
OCxREF + Polarity Output Disabled (not driven
0 1 0 Ocx=OCxREF ^ CCxNP by the timer)
Ocx_EN=1 OcxN=0, OcxN_EN=0
Complementary to OCxREF +
OCxREF + Polarity + deadtime
0 1 1 Polarity + deadtime
Ocx_EN=1
OcxN_EN=1
1 X
Output Disabled (not driven Output Disabled (not driven
1 0 0 by the timer) by the timer)
Ocx=CCxP, Ocx_EN=0 OcxN=CCxNP,OcxN_EN=0
Off-State (output enabled with OCxREF + Polarity
1 0 1 run mode) OcxN=OCxREF ^ CCxNP
Ocx=CCxP, Ocx_EN=1 OcxN_EN=1
OCxREF + Polarity Off-State (output enabled with run
1 1 0 Ocx=OCxREF ^ T1CCxNP mode)
Ocx_EN=1 OcxN=T1CCxNP, Ocx_EN=1
Complementary to OCxREF +
OCxREF + Polarity + deadtime
1 1 1 Polarity + deadtime
Ocx_EN=1
OcxN_EN=1
Output Disabled (not driven Output Disabled (not driven
0 0 0 by the timer) by the timer)
Ocx=CCxP, Ocx_EN=0 OcxN=CCxNP,OcxN_EN=0
0 0 1 Output Disabled (not driven by the timer)
0 1 0 Asynchronously: Ocx=CCxP, Ocx_EN=0, OcxN=CCxNP,
OcxN_EN=0
Then if the clock is present: Ocx=OISx and OcxN=OISxN after a
0 1 1
dead-time, assuming that OISx and OISxN do not correspond to OCX
0 X and OcxN both in active state.
Output Disabled (not driven Output Disabled (not driven
1 0 0 by the timer) by the timer)
Ocx=CCxP, Ocx_EN=0 OcxN=CCxNP,OcxN_EN=0
1 0 1 Off-State (output enabled with inactive state)
1 1 0 Asynchronously:Ocx=CCxP,Ocx_EN=1,OcxN=CCxNP,OcxN_EN=1
Then if the clock is present: Ocx=OISx and OcxN=OISxN
1 1 1 after a dead-time, assuming that OISx and OISxN do not
correspond to OCX and OcxN both in active state
(1) When both outputs of a channel are not used (CcxE = CcxNE = 0), the OISx, OISxN, CCxP and
1: MOE can be set by software or automatically at the next update event (if
the break input is not be active)
Note: This bit can not be modified as long as LOCK level 1 has been
programmed (LOCK bits in TIMx_BDTR register).
13 BKP Break polarity
0: Break input BRK is active low
1: Break input BRK is active high
Note: This bit can not be modified as long as LOCK level 1 has been
programmed (LOCK bits in TIMx_BDTR register).
Note: Any write operation to this bit takes a delay of 1 APB clock cycle to
become effective
12 BKE Break enable
0: Break inputs (BRK and internal break sources) disabled
1: Break inputs (BRK and internal break sources) enabled
Note: This bit cannot be modified when LOCK level 1 has been programmed
(LOCK bits in TIMx_BDTR register).
Note: Any write operation to this bit takes a delay of 1 APB clock cycle to
become effective.
11 OSSR Off-state selection for Run mode
This bit is used when MOE=1 on channels having a complementary output
which are configured as outputs.
0: When inactive, Ocx/OcxN outputs are disabled (Ocx/OcxN enable output
signal =0)
1: When inactive, Ocx/OcxN outputs are enabled with their inactive level as
soon as CcxE=1 or CcxNE=1. Then, Ocx/OcxN enable output signal=1.
Note: This bit can not be modified as soon as the LOCK level 2 has been
programmed (LOCK bits in TIMx_BDTR register).
10 OSSI Off-state selection for Idle mode
This bit is used when MOE=0 on channels configured as outputs.
0: When inactive, Ocx/OcxN outputs are disabled (Ocx/OcxN enable output
signal=0).
1: When inactive, Ocx/OcxN outputs are forced first with their idle level as
soon as CcxE=1 or CcxNE=1. Ocx/OcxN enable output signal=1)
Note: This bit can not be modified as soon as the LOCK level 2 has been
programmed (LOCK bits in TIMx_BDTR register).
9:8 LOCK Lock configuration
These bits offer a write protection against software errors.
00: LOCK OFF – No bit is write protected
01: LOCK Level 1 = DTG bits in TIMx_BDTR register, OISx and OISxN bits
in TIMx_CR2 register and BKE/BKP/AOE bits in TIMx_BDTR register can no
longer be written
10: LOCK Level 2 = LOCK Level 1 + CC Polarity bits (CCxP/CCxNP bits in
TIMx_CCER register, as long as the related channel is configured in output
through the CCxS bits) as well as OSSR and OSSI bits can no longer be
written.
11: LOCK Level 3 = LOCK Level 2 + CC Control bits (OcxM and OcxPE bits
in TIMx_CCMRx registers, as long as the related channel is configured in
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
Address offset Name
0
CKD[1:0]
ARPE
UDIS
OPM
URS
CEN
TIMx_CR1
–
–
0x00
Reset x x x x x x x x x x x x x x x x x x x x x x 0 0 0 x x x 0 0 0 0
OIS1N
CCDS
CCPC
OIS1
TIMx_CR2
–
–
0x04
Reset x x x x x x x x x x x x x x x x x x x x x x 1 1 x x x x 0 x x 0
COMIE
CC1DE
CC1IE
UDE
UIE
BIE
TIMx_DIER
–
–
0x0C
Reset x x x x x x x x x x x x x x x x x x x x x x 0 0 0 x 0 x x x 0 0
CC1OF
COMIF
CC1IF
UIF
BIF
TIMx_SR
–
–
0x10
Reset x x x x x x x x x x x x x x x x x x x x x x 0 x 0 x 0 x x x 0 0
COMG
CC1G
BG
UG
TIMx_EGR
–
–
0x14
Reset x x x x x x x x x x x x x x x x x x x x x x x x 0 x 0 x x x 0 0
CC1S[1:0]
TIMx_CCMR
CC1PE
CC1FE
1 OC1M[2:0]
–
–
(output mode)
Reset x x x x x x x x x x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0
0x18
CC2S[1:0]
CC1S[1:0]
IC2F[3:0]
IC1F[3:0]
TIMx_CCMR
IC2PSC IC1PSC
1
–
[1:0] [1:0]
(input mode)
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
CC1NP
CC1NE
CC1P
CC1E
TIMx_CCER
–
–
0x20
Reset x x x x x x x x x x x x x x x x x x x x x x x x x x x x 0 0 0 0
TIMx_CNT CNT[15:0]
–
0x24
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIMx_PSC PSC[15:0]
–
0x28
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIMx_ARR ARR[15:0]
–
0x2C
Reset x x x x x x x x x x x x x x x x 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
TIMx_RCR REP[7:0]
–
0x30
Reset x x x x x x x x x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0
TIMx_CCR1 CCR1[15:0]
–
0x34
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
LOCK[1:0]
OSSR
MOE
OSSI
AOE
BKP
BKE
TIM15_BDTR DTG[7:0]
–
0x44
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
–
0x48
Reset x x x x x x x x x x x x x x x x x x x 0 0 0 0 0 x x x 0 0 0 0 0
TIMx_DMAR – DMAB[15:0]
–
0x4C
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
the CCPC bit is set in the TIMx_CR2 register then the CC1P active bit takes
the new value from the preloaded bits only when a Commutation event is
generated.
0 CC1E Capture/Compare 1 output enable
CC1 channel configured as output:
0: Off
- OC1 is not active. OC1 level is then function of MOE, OSSI, OSSR,
OIS1, OIS1N and CC1NE bits.
1: On
- OC1 signal is output on the corresponding output pin depending on
MOE, OSSI, OSSR, OIS1, OIS1N and CC1NE bits.
CC1 channel configured as input:
This bit determines if a capture of the counter value can actually be done
into the input capture/compare register 1 (TIMx_CCR1) or not.
0: Capture disabled
1: Capture enabled
Note: On channels having a complementary output, this bit is preloaded. If
the CCPC bit is set in the TIMx_CR2 register then the CC1E active bit takes
the new value from the preloaded bits only when a Commutation event is
generated.
Table 20-3 utput control bits for complementary Ocx and OcxN channels with break feature
Control bits Output states(1)
MOE OSSI OSSR CcxE CcxNE Ocx output state OcxN output state
Output Disabled (not driven by Output Disabled (not driven by
0 0 0 the timer) the timer)
Ocx=0, Ocx_EN=0 OcxN=0, OcxN_EN=0
Output Disabled (not driven by OCxREF + Polarity
0 0 1 the timer) OcxN=OCxREF ^ CCxNP
Ocx=0, Ocx_EN=0 OcxN_EN=1
OCxREF + Polarity Output Disabled (not driven by
0 1 0 Ocx=OCxREF ^ CCxNP the timer)
Ocx_EN=1 OcxN=0, OcxN_EN=0
Complementary to OCxREF +
OCxREF + Polarity + deadtime
0 1 1 Polarity + dead-time
Ocx_EN=1
OcxN_EN=1
1 X
Output Disabled (not driven by Output Disabled (not driven by
1 0 0 the timer) the timer)
Ocx=CCxP, Ocx_EN=0 OcxN=CCxNP,OcxN_EN=0
Off-state(output enabled with Run OCxREF + Polarity
1 0 1 mode) OcxN=OCxREF ^ CCxNP
Ocx=CCxP, Ocx_EN=1 OcxN_EN=1
OCxREF + Polarity Off-state(output enabled with Run
1 1 0 Ocx=OCxREF ^ T1CCxNP mode)
Ocx_EN=1 OcxN=T1CCxNP, Ocx_EN=1
Complementary to OCxREF +
OCxREF + Polarity + deadtime
1 1 1 Polarity + dead-time
Ocx_EN=1
OcxN_EN=1
Output Disabled (not driven by Output Disabled (not driven by
0 0 0 the timer) the timer)
Ocx=CCxP, Ocx_EN=0 OcxN=CCxNP,OcxN_EN=0
0 0 1 Output Disabled (not driven by the timer)
0 1 0 Asynchronously: Ocx=CCxP, Ocx_EN=0, OcxN=CCxNP,
OcxN_EN=0
Then if the clock is present: Ocx=OISx and OcxN=OISxN after a
0 1 1
dead-time, assuming that OISx and OISxN do not correspond to OCX
0 X and OcxN both in active state.
Output Disabled (not driven by Output Disabled (not driven by
1 0 0 the timer) the timer)
Ocx=CCxP, Ocx_EN=0 OcxN=CCxNP,OcxN_EN=0
1 0 1 Off-State (output enabled with inactive state)
1 1 0 Asynchronously:Ocx=CCxP,Ocx_EN=1,OcxN=CCxNP,OcxN_EN=1
Then if the clock is present: Ocx=OISx and OcxN=OISxN
1 1 1 after a dead-time, assuming that OISx and OISxN do not
correspond to OCX and OcxN both in active state
(1) When both outputs of a channel are not used (CcxE = CcxNE = 0), the OISx, OISxN, CCxP and
Note2: Any write operation to this bit takes a delay of 1 APB clock cycle to
become effective.
11 OSSR Off-state selection for Run mode
This bit is used when MOE=1 on channels having a complementary output
which are configured as outputs.
0: When inactive, Ocx/OcxN outputs are disabled (Ocx/OcxN enable output
signal=0)
1: When inactive, Ocx/OcxN outputs are enabled with their inactive level as
soon as CcxE=1 or CcxNE=1. Then, Ocx/OcxN enable output signal=1
Note: This bit can not be modified as soon as the LOCK level 2 has been
programmed (LOCK bits in TIMx_BDTR register).
10 OSSI Off-state selection for Idle mode
This bit is used when MOE=0 on channels configured as outputs.
0: When inactive, Ocx/OcxN outputs are disabled (Ocx/OcxN enable output
signal=0)
1: When inactive, Ocx/OcxN outputs are forced first with their idle level as
soon as CcxE=1 or CcxNE=1. Ocx/OcxN enable output signal=1)
Note: This bit can not be modified as soon as the LOCK level 2 has been
programmed (LOCK bits in TIMx_BDTR register).
9:8 LOCK Lock configuration
These bits offer a write protection against software errors.
00: LOCK OFF – No bit is write protected
01: LOCK Level 1 = DTG bits in TIMx_BDTR register, OISx and OISxN bits
in TIMx_CR2 register and BKE/BKP/AOE bits in TIMx_BDTR register can no
longer be written.
10: LOCK Level 2 = LOCK Level 1 + CC Polarity bits (CCxP/CCxNP bits in
TIMx_CCER register, as long as the related channel is configured in output
through the CCxS bits) as well as OSSR and OSSI bits can no longer be
written.
11: LOCK Level 3 = LOCK Level 2 + CC Control bits (OcxM and OcxPE bits
in TIMx_CCMRx registers, as long as the related channel is configured in
output through the CCxS bits) can no longer be written.
Note: The LOCK bits can be written only once after the reset. Once the
TIMx_BDTR register has been written, their content is frozen until the
next reset.
7:0 DTG[7:0] Dead-time generator setup
This bit-field defines the duration of the dead-time inserted between the
complementary outputs. DT correspond to this duration
DTG[7:5]=0xx => DT=DTG[7:0]* tdtg with tdtg=tDTS
DTG[7:5]=10x => DT=(64+DTG[5:0]) * tdtg with tdtg= 2*tDTS
DTG[7:5]=110 => DT=(32+DTG[4:0]) * tdtg with tdtg=8* tDTS
DTG[7:5]=111 => DT=(32+DTG[4:0]) * tdtg with tdtg= 16*tDTS
Example if TDTS=125ns (8MHz), dead-time possible values are:
DTG[7:0] = 0~7Fh,0 to 15875 ns by 125 ns steps
DTG[7:0] = 80h~BFh,16 μs to 31750 ns by 250 ns steps,
DTG[7:0] = C0h~DFh,32 μs to 63 μs by 1 μs steps,
DTG[7:0] = E0h~FFh,64 μs to 126 μs by 2 μs steps
Note: This bit-field can not be modified as long as LOCK level 1, 2 or 3 has
been programmed (LOCK bits in TIMx_BDTR register).
31:24 —
23:16 —
15:8 DMAB[15:8]
type RW RW RW RW RW RW RW RW
7:0 DMAB[7:0]
type RW RW RW RW RW RW RW RW
TIM17_CH1
IR_OUT
TIM16_CH1
USART1
USART2
SYSCFG_CFGR1[7:6]
All standard IR pulse modulation modes can be obtained by programming the two timer output compare
channels.
TIM17 generates high frequency carrier signal TIM16 generates the modulation envelope.
The infrared function is output on the IR_OUT pin. The activation of this function is done through the
GPIOx_AFRx register by enabling the related alternate function bit.
22.1. Introduction
The devices feature an embedded watchdog peripheral which offers a combination of high safety level,
timing accuracy and flexibility of use. The Independent watchdog peripheral serves to detect and resolve
malfunctions due to software failure, and to trigger system reset when the counter reaches a given timeout
value.
Free-running downcounter
Clocked from an independent RC oscillator (can operate in Standby and Stop modes)
Conditional Reset
− Reset (if watchdog activated) when the downcounter value becomes less than 0
− Reset (if watchdog activated) if the downcounter is reloaded outside the window
Figure 22-1 shows the functional blocks of the independent watchdog module.
TCLK
5V ___domain
12-bit reload
value
12-bit
8-bit window
LSI
prescaler value
40KHz
12-bit IWDG_RESET
downcounter
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_KR register, the IWDG_RLR value is reloaded
in the counter and the watchdog reset is prevented. It should have at least three 40kHz LSI clock cycles
between two write operation.
The IWDG can also work as a window watchdog by setting the appropriate window in the IWDG_WINR
register.
If the reload operation is performed while the counter is greater than the value stored in the window register
(IWDG_WINR), then a reset is provided.
The default value of the IWDG_WINR is 0x0000 0FFF, so if it is not updated, the window option is disabled.
As soon as the window value is changed, a reload operation is performed in order to reset the downcounter
to the IWDG_RLR value and ease the cycle number calculation to generate the next reload.
If the DBG_IWDG_STOP in DBG module is 1, then the IWDG will be enabled automatically turn on when the
device is power up. A hardware reset will be generated if the 0x0000AAAA not been wrote into IWDG_KR
before conter count to 0 or before the down counter greater than the window value.s
IWDG register cannot be configured in Stop or Standby mode, but the counter can still run and the reset
signal can also be generated.
Write access to the IWDG_PR, IWDG_RLR and IWDG_WINR registers is protected. To modify them, you
must first write the code 0x0000 5555 in the IWDG_KR register. A write access to this register with a
different value will break the sequence and register access will be protected again. This implies that it is the
case of the reload operation(writing 0x0000 AAAA).
A status register(IWDG_SR) is available to indicate that an update of the prescaler or the down-counter
reload value or the window value is on going.
When the microcontroller enters debug mode (core halted), the IWDG counter either continues to work
normally or stops, depending on DBG_IWDG_STOP configuration bit in DBG module
Offset Register
9
8
7
6
5
4
3
2
1
0
IWDG_KR KEY[15:0]
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x00
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
IWDG_PR PR[2:0]
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x04
Reset x x x x x x x x x x x x x x x x x x x x x x x x x x x x x 0 0 0
IWDG_RLR RL[11:0]
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x08
Reset x x x x x x x x x x x x x x x x x x x x 1 1 1 1 1 1 1 1 1 1 1 1
WVU
RVU
PVU
IWDG_SR
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x0C
Reset x x x x x x x x x x x x x x x x x x x x x x x x x x x x x 0 0 0
IWDG_WINR WIN[11:0]
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x10
Reset x x x x x x x x x x x x x x x x x x x x 1 1 1 1 1 1 1 1 1 1 1 1
22.4.1. IWDG_KR
22.4.2. IWDG_PR
22.4.3. IWDG_RLR
22.4.4. IWDG_SR
22.4.5. IWDG_WINR
23.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 downcounter before the T[6] bit
becomes cleared. An MCU reset is also generated if the 7-bit downcounter value (in the control register) is
refreshed before the downcounter has reached the window register value. This implies that the counter must
be refreshed in a limited window.
The WWDG clock is prescaled from the APB 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.
If the watchdog is activated (the WDGA bit is set in the WWDG_CR register) and when the 7-bit
downcounter (T[6:0] bits) rolls over from 0x40 to 0x3F (T[6] becomes cleared), it initiates a reset. If the
software reloads the counter while the counter is greater than the value stored in the window register, then a
reset is generated.
The application program must write in the WWDG_CR register at regular intervals during normal operation
to prevent an MCU reset. This operation must occur only when the counter value is lower than the window
register value. The value to be stored in the WWDG_CR register must be between 0xFF and 0xC0.
W[6:0]
cmp=1 when
T[6:0] >[ W6:0]
WWDG_RESET cmp
Q D
Write WWDG_CR
RB pclk
WDGA T6 T[5:0]
presetn
7-bit downcounter
pclk
/4096 prescaler
The watchdog is always disabled after a reset. It is enabled by setting the WDGA bit in the WWDG_CR
register, then it cannot be disabled again except by a reset.
This downcounter is free-running, counting down even if the watchdog is disabled. When the watchdog is
enabled, the T[6] bit must be set to prevent generating an immediate reset.
The T[5:0] bits contain the number of increments which represents the time delay before the watchdog
produces a reset. The timing varies between a minimum and a maximum value due to the unknown status of
the prescaler when writing to the WWDG_CR register . The Configuration register (WWDG_CFR) contains
the high limit of the window:
To prevent a reset, the downcounter must be reloaded when its value is lower than the window register value
and greater than 0x3F. Figure 23-2 describes the window watchdog process.
Note: The T[6] bit can be used to generate a software reset (the WDGA bit is set and the T[6] bit is cleared).
The Early Wakeup Interrupt (EWI) can be used if specific safety operations or data logging must be
performed before the actual reset is generated. The EWI interrupt is enabled by setting the EWI bit in the
WWDG_CFR register. When the downcounter reaches the value 0x40, an EWI interrupt is generated and
the corresponding interrupt service routine (ISR) can be used to trigger specific actions (such as
communications or data logging), before resetting the device.
In some applications, the EWI interrupt can be used to manage a software system check and/or system
recovery/graceful degradation, without generating a WWDG reset. In this case, the corresponding interrupt
service routine (ISR) should reload the WWDG counter to avoid the WWDG reset, then trigger the required
actions.
Note: When the EWI interrupt cannot be served, e.g. due to a system lock in a higher priority task, the
WWDG reset will eventually be generated.
You can use the formula shown below to calculate the WWDG timeout.
Note: When writing to the WWDG_CR register, always write 1 in the T[6] bit to avoid generating an
immediate reset.
T[6:0] downcounter
W[6:0]
0x3F
reset
When the microcontroller enters debug mode (Cortex®-M0 core halted), the WWDG counter either continues
to work normally or stops, depending on DBG_WWDG_STOP configuration bit in DBG module.
9
8
7
6
5
4
3
2
1
0
WWDG_CR T[6:0]
WDGA
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x00
Reset x x x x x x x x x x x x x x x x x x x x x x x x 0 1 1 1 1 1 1 1
WDGTB1
WDGTB0
WWDG_CFR W[6:0]
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x04
EWI
Reset x x x x x x x x x x x x x x x x x x x x x x 0 0 0 1 1 1 1 1 1 1
EWIF
WWDG_SR
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x08
Reset x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x 0
23.4.1. WWDG_CR
23.4.2. WWDG_CFR
23.4.3. WWDG_SR
24.1. Introduction
The real-time clock (RTC) is an independent BCD timer/counter. The RTC provides a time- of-day
clock/calendar with programmable alarm interrupt.
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).
RTC_TAMP2
RTC tamper control
TAMPxF
registers
RTC_TAMP1
RTC_REFIN
LSE(32.768 Hz)
HSE/32
LSI
RTCCLK
Shadow registers
Shadow register
RTC_TR,
RTC_SSR
RTC_DR
RTC_CALIB
Output
RTC_OUT
Control
RTC_ALARM
Alarm A
= ALRAF
RTC_ALRMAR
RTC_ALRMASSR
RTC_OUT, RTC_TS and RTC_TAMP1 are mapped on the same pin (PC13).
The selection of the RTC_ALARM output is performed through the RTC_TAFCR register as follows: the
PC13VALUE bit is used to select whether the RTC_ALARM output is configured in push-pull or open drain
mode.
When PC13 is not used as RTC alternate function, it can be forced in output push-pull mode by setting the
PC13MODE bit in the RTC_TAFCR. The output data value is then given by the PC13VALUE bit. In this case,
PC13 output push-pull state and data are preserved in Standby mode.
The output mechanism follows the priority order shown in Table 24-1
When PC14 and PC15 are not used as LSE oscillator, they can be forced in output push-pull mode by setting
the PC14MODE and PC15MODE bits in the RTC_TAFCR register respectively. The output data values are
then given by PC14VALUE and PC15VALUE. In this case, the PC14 and PC15 output push-pull states and
data values are preserved in Standby mode.
The output mechanism follows the priority order shown in Table 24-2 and Table 24-3
(1)
Table 24-1 RTC pin PC13 configuration
Pin RTC_TAMP1 RTC_TS RTC_ALARM RTC_CALIB
configuration output input output output PC13MODE PC13VALUE
and function enabled enabled enabled enabled
RTC_TAMP1
1 0 Don’t care Don’t care Don’t care Don’t care
input floating
RTC_TS and
RTC_TAMP1 1 1 Don’t care Don’t care Don’t care Don’t care
input floating
RTC_TS
0 1 Don’t care Don’t care Don’t care Don’t care
input floating
RTC_ALARM
0 0 1 Don’t care Don’t care 0
output OD
RTC_ALARM
0 0 1 Don’t care Don’t care 1
output PP
RTC_CALIB
0 0 0 1 Don’t care Don’t care
output PP
Output PP PC13 output
0 0 0 0 1
forced data value
Wakeup pin or
0 0 0 0 0 Don’t care
Standard GPIO
The RTC clock source (RTCCLK) is selected through the clock controller among the LSE clock, the LSI
oscillator clock, and the HSE clock. For more information on the RTC clock source configuration, refer to
section 7 RCC
A programmable prescaler stage generates a 1 Hz clock which is used to update the calendar. To minimize
power consumption, the prescaler is split into 2 programmable prescalers (see Figure 24-1 RTC block
diagram):
A 7-bit asynchronous prescaler configured through the PREDIV_A bits of the RTC_PRER register.
A 15-bit synchronous prescaler configured through the PREDIV_S bits of the RTC_PRER register.
Note: When both prescalers are used, it is recommended to configure the asynchronous prescaler to a high
value to minimize consumption.
The asynchronous prescaler division factor is set to 128, and the synchronous division factor to 256, to
obtain an internal clock frequency of 1 Hz (ck_spre) with an LSE frequency of 32.768 kHz.
The minimum division factor is 1 and the maximum division factor is 2 22. This corresponds to a maximum
input frequency of around 4 MHz.
fCK_APRE is given by the following formula:
f RTCCLK
f CK_APRE
PREDIV_A 1
he 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_apre is given by the following formula:
f RTCCLK
f CK_SPRE
(PREDIV_S 1) (PREDIV_A 1)
The RTC calendar time and date registers are accessed through shadow registers which are synchronized
with PCLK (APB clock). They can also be accessed directly in order to avoid waiting for the synchronization
duration.
RTC_SSR for the subseconds
RTC_TR for the time
RTC_DR for the date
Every two RTCCLK periods, the current calendar value is copied into the shadow registers, and the RSF bit
of RTC_ISR register is set (see section 24.7.4: RTC initialization and status register). The copy is not
performed in Stop and Standby mode. When exiting these modes, the shadow registers are updated after up
to 2 RTCCLK periods.
When the application reads the calendar registers, it accesses the content of the shadow registers. It is
possible to make a direct access to the calendar registers by setting the 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 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.
The calendar shadow registers (RTC_SSR, RTC_TR and RTC_DR) and some bits of the RTC status
register (RTC_ISR) are reset to their default values by all available system reset sources.
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 and alternate function configuration register (RTC_TAFCR), the RTC backup registers (RTC_BKPxR),
the wakeup timer register (RTC_WUTR), the Alarm A registers (RTC_ALRMASSR/RTC_ALRMAR).
In addition, the RTC keeps on running under system reset if the reset source is different from the RTC
___domain reset one. When a RTC ___domain reset occurs, the RTC is stopped and all the RTC registers are set to
their reset values.
The RTC can be synchronized to a remote clock with a high degree of precision. After reading the
sub-second field (RTC_SSR or RTC_TSSSR), a calculation can be made of the precise offset between the
times being maintained by the remote clock and the RTC. The RTC can then be adjusted to eliminate this
offset by “shifting” its clock by a fraction of a second using RTC_SHIFTR.
RTC_SSR contains the value of the synchronous prescaler counter. This allows one to calculate the exact
time being maintained by the RTC down to a resolution of 1 / (PREDIV_S + 1) seconds. As a consequence,
the resolution can be improved by increasing the synchronous prescaler value (PREDIV_S[14:0]. The
maximum resolution allowed (30.52 μs with a 32768 Hz clock) is obtained with PREDIV_S set to 0x7FFF.
However, increasing PREDIV_S means that PREDIV_A must be decreased in order to maintain the
synchronous prescaler output at 1 Hz. In this way, the frequency of the asynchronous prescaler output
increases, which may increase the RTC dynamic consumption.
The RTC can be finely adjusted using the RTC shift control register (RTC_SHIFTR). Writing to
RTC_SHIFTR can shift (either delay or advance) the clock by up to a second with a resolution of 1 /
(PREDIV_S + 1) seconds. The shift operation consists of adding the SUBFS[14:0] value to the synchronous
prescaler counter SS[15:0]: this will delay the clock. If at the same time the ADD1S bit is set, this results in
adding one second and at the same time subtracting a fraction of second, so this will advance the clock.
As soon as a shift operation is initiated by a write to the RTC_SHIFTR register, the SHPF flag is set by
hardware to indicate that a shift operation is pending. This bit is cleared by hardware as soon as the shift
operation has completed.
Note: This synchronization feature is not compatible with the reference clock detection feature: firmware
must not write to RTC_SHIFTR when REFCKON=1.
The update of the RTC calendar can be synchronized to a reference clock, RTC_REFIN, which is usually
the mains frequency (50 or 60 Hz). The precision of the RTC_REFIN reference clock should be higher than
the 32.768 kHz LSE clock. When the RTC_REFIN 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 asynchronous prescaler which outputs the
ck_apre 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
PREDIV_S = 0x00FF
Note: RTC_REFIN clock detection is not available in Standby mode.
The RTC frequency can be digitally calibrated with a resolution of about 0.954 ppm with a range from -487.1
ppm to +488.5 ppm. The correction of the frequency is performed using series of small adjustments (adding
and/or subtracting individual RTCCLK pulses). These adjustments are fairly well distributed so that the RTC
is well calibrated even when observed over short durations of time.
The smooth digital calibration is performed during a cycle of about 220 RTCCLK pulses, or 32 seconds when
the input frequency is 32768 Hz. This cycle is maintained by a 20-bit counter, cal_cnt[19:0], clocked by
RTCCLK.
The smooth calibration register (RTC_CALR) specifies the number of RTCCLK clock cycles 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.
Setting CALM[1] to 1 causes two additional cycles to be masked
Setting CALM[2] to 1 causes four additional cycles to be masked
and so on up to CALM[8] set to 1 which causes 256 clocks to be masked.
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 (F CAL) given the input frequency (FRTCCLK) is as
follows:
20
F CAL F RTCCLK [1 (CALP 512 CALM) / (2 CALM CALP 512) ]
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.
The RTC_TAMPx input events can be configured either for edge detection, or for level detection with
filtering.
The tamper detection can be configured for the following purposes:
Generate an interrupt
Capable to wakeup from Stop and Standby modes
When the COE bit is set to 1 in the RTC_CR register, a reference clock is provided on the RTC_CALIB
device output.
If the COSEL bit in the RTC_CR register is reset and PREDIV_A = 0x7F, the RTC_CALIB frequency is
fRTCCLK/64. This corresponds to a calibration output at 512 Hz for an RTCCLK frequency at 32.768 kHz. The
RTC_CALIB duty cycle is irregular: there is a light jitter on falling edges. It is therefore recommended to use
rising edges.
When COSEL is set and “PREDIV_S+1” is a non-zero multiple of 256 (i.e: PREDIV_S[7:0] = 0xFF), the
RTC_CALIB frequency is fRTCCLK/(256 * (PREDIV_A+1)). This corresponds to a calibration output at 1 Hz
for prescaler default values (PREDIV_A = 0x7F, PREDIV_S = 0xFF), with an RTCCLK frequency at 32.768
kHz.
Note: When the RTC_CALIB or RTC_ALARM output is selected, the RTC_OUT pin is automatically
configured in output alternate function.
When COSEL is cleared, RTC_CALIB output is level 6 of asynchronous prescaler.
When COSEL is set, RTC_CALIB output is level 8 of synchronous prescaler.
The OSEL[1:0] control bits in the RTC_CR register are used to activate the alarm alternate function output
RTC_ALARM, and to select the function which is output. These functions reflect the contents of the
corresponding flags in the RTC_ISR register.
The polarity of the output is determined by the POL control bit in RTC_CR so that the opposite of the
selected flag bit is output when POL is set to 1.
Alarm alternate function output
The RTC_ALARM pin can be configured in output open drain or output push-pull using the control bit
PC13VALUE in the RTC_TAFCR 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).
When the RTC_CALIB or RTC_ALARM output is selected, the RTC_OUT pin is automatically configured in
output alternate function.
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
Offset Register
8
7
0
HT
PM
RTC_TR HU[3:0] MNT[2:0] MNU[3:0] ST[2:0] SU[3:0]
–
–
0x00 [1:0]
Reset x x x x x x x x x 0 0 0 0 0 0 0 x 0 0 0 0 0 0 0 x 0 0 0 0 0 0 0
DT
MT
RTC_DR YT[3:0] YU[3:0] WDU[2:0] MU[3:0] DU[3:0]
–
–
0x04 [1:0]
Reset x x x x x x x x 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 x x 0 0 0 0 0 1
REFCKON
BYPSHAD
TSEDGE
ALRAIE
COSEL
SUB1H
ADD1H
ALRAE
OSEL
TSIE
FMT
COE
POL
BKP
TSE
–
–
RTC_CR
0x08
Reset x x x x x x x x 0 x 0 0 0 0 0 0 0 x x 0 0 x x 0 x 0 0 0 0 x x x
RECALPF
ALRAWF
TAMP2F
TAMP1F
TSOVF
ALRAF
INITF
SHPF
INITS
INIT
TSF
RSF
RTC_ISR
–
–
0x0C
Reset x x x x x x x x x x x x x x x 0 x 0 0 0 0 x x 0 0 0 0 0 0 x x 1
RTC_PRER PREDIV_A[6:0] PREDIV_S[14:0]
–
–
0x10
Reset x x x x x x x x x 1 1 1 1 1 1 1 x 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
WDSEL
MSK4
MSK3
MSK2
MSK1
DT HT
PM
Reset 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
RTC_WPR KEY[7:0]
–
–
–
–
–
–
0x24
Reset x x x x x x x x x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0
RTC_SSR SS[15:0]
–
–
–
–
–
0x28
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ADD1S
SUBFS[14:0]
–
–
–
RTC_SHIFTR
0x2C
Reset 0 x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
HT
PM
–
RTC_TSTR
0x30 [1:0]
Reset x x x x x x x x x 0 0 0 0 0 0 0 x 0 0 0 0 0 0 0 x 0 0 0 0 0 0 0
DT
MT
–
–
–
RTC_TSDR
0x34 [1:0]
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 x x 0 0 0 0 0 0
RTC_TSSSR SS[15:0]
–
–
–
0x38
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
CALW16
CALW8
CALP
RTC_CALR CALM[8:0]
–
–
–
–
–
0x3C
Reset x x x x x x x x x x x x x x x x 0 0 0 x x x x 0 0 0 0 0 0 0 0 0
TAMPPUDIS
PC15VALUE
PC14VALUE
PC13VALUE
TAMPPRCH
TAMPFREQ
TAMP2TRG
TAMP1TRG
PC15MODE
PC14MODE
PC13MODE
TAMPFLT
TAMPTS
TAMP2E
TAMP1E
TAMPIE
[1:0]
[1:0]
[2:0]
RTC_TAFCR
–
–
–
0x40
Reset x x x x x x x x 0 0 0 0 0 0 x x 0 0 0 0 0 0 0 0 0 x x 0 0 0 0 0
MASKSS
SS[14:0]
–
–
–
–
–
RTC_ALRMASSR
0x44 [3:0]
Reset x x x x 0 0 0 0 x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
24.6.1. RTC_TR
The RTC_TR is the calendar time shadow register. This register must be written in initialization mode only.
Refer to Calendar initialization and configuration and Reading the calendar
This register is write protected. The write access procedure is described in “RTC register write protection”
Address offset: 0x00
RTC ___domain reset value: 0x0000 0000
System reset: 0x0000 0000 when BYPSHAD = 0. Not affected when BYPSHAD = 1.
24.6.2. RTC_DR
The RTC_DR is the calendar date shadow register. This register must be written in initialization mode only.
Refer to Calendar initialization and configuration and Reading the calendar
This register is write protected. The write access procedure is described in “RTC register write protection”
Address offset: 0x04
RTC ___domain reset value: 0x0000 2101
System reset: 0x0000 2101 when BYPSHAD = 0. Not affected when BYPSHAD = 1.
24.6.3. RTC_CR
These bits are used to select the flag to be routed to RTC_ALARM output
0: Output disabled
1: Alarm A output enabled
20 POL Output polarity
This bit is used to configure the polarity of RTC_ALARM output
0: The pin is high when ALRAF is asserted
1: The pin is low when ALRAF is asserted
19 COSEL Calibration output selection
When COE=1, this bit selects which signal is output on RTC_CALIB
0: Calibration output is 512 Hz
1: Calibration output is 1 Hz
These frequencies are valid for RTCCLK at 32.768 kHz and prescalers at
their default values (PREDIV_A=127 and PREDIV_S=255). Refer to section
24.3.14: calibration clock output
18 BKP Backup
This bit can be written by the user to memorize whether the daylight saving
time change has been performed or not.
17 SUB1H Subtract 1 hour (winter time change)
When this bit is set outside initialization mode, 1 hour is subtracted to the
calendar time if the current hour is not 0. This bit is always read as 0.
Setting this bit has no effect when current hour is 0.
0: No effect
1: Subtracts 1 hour to the current time. This can be used for winter time
change
16 ADD1H Add 1 hour (summer time change)
When this bit is set outside initialization mode, 1 hour is added to the
calendar time. This bit is always read as 0.
0: No effect
1: Adds 1 hour to the current time. This can be used for summer time change
15 TSIE Time-stamp interrupt enable
0: Time-stamp Interrupt disable
1: Time-stamp Interrupt enable
14:13 NA Reserved
12 ALRAIE Alarm A interrupt enable
0: Alarm A interrupt disabled
1: Alarm A interrupt enabled
11 TSE timestamp enable
0: timestamp disable
1 Alarm A enable: timestamp enable
10:9 NA Reserved
8 ALRAE Alarm A enable
0: Alarm A disabled
1: Alarm A enabled
7 NA Reserved
6 FMT Hour format
0: 24 hour/day format
1: AM/PM hour format
5 BYPSHAD Bypass the shadow registers
0: Calendar values (when reading from RTC_SSR, RTC_TR, and RTC_DR)
are taken from the shadow registers, which are updated once every two
RTCCLK cycles.
1: Calendar values (when reading from RTC_SSR, RTC_TR, and RTC_DR)
are taken directly from the calendar counters.
Note: If the frequency of the APB clock is less than seven times the
frequency of RTCCLK, BYPSHAD must be set to ‘1’
4 REFCKON RTC_REFIN reference clock detection enable (50 or 60 Hz)R
0: RTC_REFIN detection disabled
1: RTC_REFIN detection enabled
Note: PREDIV_S must be 0x00FF.
3 TSEDGE Time-stamp event active edge
0: RTC_TS input rising edge generates a time-stamp event
1: RTC_TS input falling edge generates a time-stamp event
TSE must be reset when TSEDGE is changed to avoid unwanted TSF
setting.
2:0 NA Reserved
Note: Bits 7, 6 and 4 of this register can be written in initialization mode only (RTC_ISR/INITF = 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”
24.6.4. RTC_ISR
This register is write protected (except for RTC_ISR[13:8] bits). The write access procedure is described in
“RTC register write protection”
Address offset: 0x0C
RTC ___domain reset value: 0x0000 0007
System reset: not affected except INIT, INITF, and RSF bits which are cleared to ‘0’
24.6.5. RTC_PRER
This register must be written in initialization mode only. The initialization must be performed in two separate
write accesses. Refer to Calendar initialization and configuration
This register is write protected. The write access procedure is described in “RTC register write protection”
Address offset: 0x10
RTC ___domain reset value: 0x007F 00FF
System reset: not affected
15:8 — PREDIV_S[14:8]
Type RO-0 RW RW RW RW RW RW RW
7:0 PREDIV_S[7:0]
Type RW RW RW RW RW RW RW RW
24.6.6. RTC_ALRMAR
This register can be written only when ALRAWF is set to 1 in RTC_ISR, or in initialization mode.
This register is write protected. The write access procedure is described in “RTC register write protection”
Address offset: 0x1C
RTC ___domain reset value: 0x0000 0000
System reset: not affected
24.6.7. RTC_WPR
24.6.8. RTC_SSR
24.6.9. RTC_SHIFTR
This register is write protected. The write access procedure is described in “RTC register write protection”
Address offset: 0x2C
RTC ___domain reset value: 0x0000 0000
System reset: not affected
24.6.10. RTC_TSTR
The content of this register is valid only when TSF is set to 1 in RTC_ISR. It is cleared when TSF bit is reset.
Address offset: 0x30
RTC ___domain reset value: 0x0000 0000
System reset: not affected
24.6.11. RTC_TSDR
The content of this register is valid only when TSF is set to 1 in RTC_ISR. It is cleared when TSF bit is reset.
Address offset: 0x34
RTC ___domain reset value: 0x0000 0000
System reset: not affected
24.6.12. RTC_TSSSR
The content of this register is valid only when RTC_ISR/TSF is set. It is cleared when the RTC_ISR/TSF bit
is reset.
Address offset: 0x38
RTC ___domain reset value: 0x0000 0000
System reset: not affected
24.6.13. RTC_CALR
This register is write protected. The write access procedure is described in “RTC register write protection”
Address offset: 0x3C
RTC ___domain reset value: 0x0000 0000
System reset: not affected
24.6.14. RTC_TAFCR
If TAMPFLT = 00:
0: RTC_TAMP1 input rising edge triggers a tamper detection event.
1: RTC_TAMP1 input falling edge triggers a tamper detection event.
0 TAMP1E RTC_TAMP1 input detection enable
0: RTC_TAMP1 detection disabled
1: RTC_TAMP1 detection enabled
Note: When TAMPFLT = 0, TAMP1E must be reset when TAMP1TRG is changed to avoid spuriously setting
TAMP1F.
24.6.15. RTC_ALRMASSR
This register can be written only when ALRAE is reset in RTC_CR register, or in initialization mode.
This register is write protected. The write access procedure is described in “RTC register write protection”
Address offset: 0x44
RTC ___domain reset value: 0x0000 0000
System reset: not affected
The overflow bits of the synchronous counter (bits 15) is never compared.
This bit can be different from 0 only after a shift operation.
23:15 NA Reserved
14:0 SS[14:0] Sub seconds value
This value is compared with the contents of the synchronous prescaler
counter to determine if Alarm A is to be activated. Only bits 0 up MASKSS-1
are compared.
In addition to receiving and transmitting data, this interface converts it from serial to parallel format and vice
versa. The interrupts are enabled or disabled by software. The interface is connected to the I2C bus by a
data pin (SDA) and by a clock pin (SCL). It can be connected with a standard (up to 100 kHz), Fast-mode
(up to 400 kHz) or Fast-mode Plus (up to 1 MHz) I2C bus.
This interface can also be connected to a SMBus with the data pin (SDA) and clock pin (SCL). If SMBus
feature is supported: the additional optional SMBus Alert pin (SMBA) is also available.
This manual describes the full set of features implemented in I2C1. I2C2 supports a smaller set of features,
but is otherwise identical to I2C1. The differences are listed below.
APB
APB bus
bus
PCLK Register
Register
SMBus
SMBus Alert
Alert control
control &
& status
status I2C_SMBA
Clock
Clock generation
generation Digital
Digital Analog
Analog
noise
noise noise
noise
filter
filter filter
filter GPIO
GPIO I2C_SCL
Clock
Clock stretching
stretching
Timeout
Timeout
Clock control
I2C1SW
Data control
SYSCLK Digital
Digital Analog
Analog
Shift
Shift register
register noise noise
0 noise noise
PCLK filter
filter filter
filter GPIO
GPIO I2C_SDA
1
HSI
PEC
PEC generation/check
generation/check
I2C I/Os support 20 mA output current drive for Fast-mode Plus operation. This is enabled by setting the
driving capability control bits for SCL and SDA, refer to SYSCFG_CFGR1.
The PCLK clock period TPCLK must respect the following condition:
TPCLK<4/3TSCL
with TSCL:SCL period
Caution: When the I2C kernel is clocked by PCLK. PCLK must respect the conditions for T I2CCLK.
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.
th
A9 clock pulse follows the 8 clock cycles of a byte transfer, during which the receiver must send an
acknowledge bit (ACK) to the transmitter. Refer to the following Figure 25-2 I2C bus protocol.
SCL 1 8 9
2
START
STOP
Noise filters
Before you enable the I2C peripheral by setting the PE bit in I2C_CR1 register, you 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. You 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*I2CCLK periods. This allows to suppress spikes with a programmable length of 1
to 15 I2CCLK periods.
Table 25-2 Comparison of analog vs. digital filters
Analog filter Digital filter
Pulse width of suppressed spikes Programmable length from 1 to 15
I2C peripheral clocks
Benefits Available in Stop mode Programmable length:
Drawbacks Variation vs. temperature, voltage, —
process
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.
synchronization time
SDADEL
SCL
SDA
SCLDEL
SCL
SDA
data setup
time
Caution: Changing the timing configuration is not allowed when the I2C is enabled.
I2C configuration
The I2C slave NOSTRETCH mode must also be configured before enabling the peripheral.
Changing the NOSTRETCH configuration is not allowed when the I2C is enabled
Initial settings
Clear PE bit
Configure
PRESC,SDADEL,SCLDEL,SCLH,
SCLL
Set PE bit
End
A software reset can be performed by clearing the PE bit in the I2C_CR1 register. In that case I2C lines SCL
and SDA are released. Internal states machines are reset and communication control bits, as well as status
bits come back to their reset value. The configuration registers are not impacted.
Here is the list of impacted register bits:
1. I2Cx_CR2 register: START,STOP,NACK
2. I2Cx_ISR register: BSY,TXE,TXIS,RXNE,ADDR,NACKF,TCR,TC,STOPF,BERR,ARLO,OVR
PE must be kept low during at least 3 APB clock cycles in order to perform the software reset. This is
ensured by writing the following software sequence: - Write PE=0 – Check PE=0 – Write PE=1.
The data transfer is managed through transmit and receive data registers and a shift register.
Reception
th
The SDA input fills the shift register. After the 8 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.
th th
The stretch is inserted between the 8 and 9 SCL pulse (before the Acknowledge pulse).
ACK ACK
pulse pulse
SCL SCLstretch
Shift
xx data1 xx data2
register
RXNE
read data0
read data1
Transmission
th
If the I2C_TXDR register is not empty (TXE=0), its content is copied into the shift register after the 9 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
th
done after the 9 SCL pulse.
ACK ACK
pulse pulse
SCL SCLstretch
Shift
xx xx xx
d1
d2
register
TXE
write data1
write data2
The byte counter is always used in master mode. By default it is disabled in slave mode, but it can be
enabled by software by setting the SBC (Slave Byte Control) bit in the I2Cx_CR2 register.
The number of bytes to be transferred is programmed in the NBYTES[7:0] bit field in the I2C_CR2 register. If
the number of bytes to be transferred (NBYTES) is greater than 255, or if a receiver wants to control the
acknowledge value of a received data byte, the reload mode must be selected by setting the RELOAD bit in
the I2C_CR2 register. In this mode, TCR flag is set when the number of bytes programmed in NBYTES has
been transferred, and an interrupt is generated if TCIE is set. SCL is stretched as long as TCR flag is set.
TCR is cleared by software when NBYTES is written to a non-zero value.
When the NBYTES counter is reloaded with the last number of bytes, RELOAD bit must be cleared.
Caution: The AUTOEND bit has no effect when the RELOAD bit is set.
Table 25-4 I2C configuration table
Function SBC bit RELOAD bit AUTOEND bit
Master Tx/Rx NBYTES + STOP x 0 1
Master Tx/Rx + NBYTES + RESTART x 0 0
Slave Tx/Rx+ ACK 0 x x
Slave Rx + ACK 1 1 x
When the I2C is selected by one of its enabled addresses, the ADDR interrupt status flag is set, and an
interrupt is generated if the ADDRIE bit is set.
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 I2Cx_CR1 register.
After receiving an ADDR interrupt, if several addresses are enabled you must read the ADDCODE[6:0] bits
in the I2Cx_ISR register in order to check which address matched. DIR flag must also be checked in order to
know the transfer direction.
Reload mode must be selected in order to allow byte ACK control in slave reception mode (RELOAD=1). To
get control of each byte, NBYTES must be initialized to 0x1 in the ADDR interrupt subroutine, and reloaded
to 0x1 after each received byte. When the byte is received, the TCR bit is set, stretching the SCL signal low
th th
between the 8 and 9 SCL pulses. You can read the data from the I2Cx_RXDR register, and then decide to
acknowledge it or not by configuring the ACK bit in the I2Cx_CR2 register. The SCL stretch is released by
programming NBYTES to a non-zero value: the acknowledge or not-acknowledge is sent and next byte can
be received.
NBYTES can be loaded with a value greater than 0x1, and in this case, the reception flow is continuous
during NBYTES data reception.
Note1: The SBC bit must be configured when the I2C is disabled, or when the slave is not addressed, or
when ADDR=1.
Note2. The RELOAD bit value can be changed when ADDR=1, or when TCR=1.
Note3. Slave Byte Control mode is not compatible with NOSTRETCH mode. Setting SBC when
NOSTRETCH=1 is not allowed.
Slave initialization
Initial setting
Clear OA1EN,OA2EN
Configure OA1,OA1MODE,OA1EN,
OA2,OA2MSK,OA2EN,GCEN
Configure SBC
End
Slave transmitter
A transmit interrupt status (TXIS) is generated when the I2Cx_TXDR register becomes empty. An interrupt is
generated if the TXIE bit is set in the I2Cx_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 I2Cx_ISR register and an interrupt is generated if the
NACKIE bit is set in the I2Cx_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
I2Cx_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), you can choose either to send the
content of the I2Cx_TXDR register as the first data byte, or to flush the I2Cx_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 you cannot
flush the I2Cx_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 I2Cx_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 I2Cx_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 will be
generated (the OVR flag is set). If you need a TXIS event, (Transmit Interrupt or Transmit DMA request),
you must set the TXIS bit in addition to the TXE bit, in order to generate a TXIS event.
Slave
transmission
Slave initialization
NO
ADDR=1?
YES SCL
read ADDCODE and DIR, stretched
set ADDRCF(optional set
TXE)
NO
TXIS=1?
YES
Slave
transmission
Slave initialization
NO
NO STOPF=1?
TXIS=1?
YES
YES
Optional set TXE and
Write TXDATA register TXIS
Set STOPCF
TXE
transmission
EV1:ADDR ISR, read ADDCODE,DIR,set ADDRCF
S reception
EV2:TXIS ISR, write DATA1
TXE
transmission
EV1:ADDR ISR, read ADDCODE,DIR, set ADDRCF
S reception
EV2:TXIS ISR, write DATA2
SCL stretch
EV3:TXIS ISR, ,write DATA3
TXE
SCL stretch
EV4:TXIS ISR, write DATA4(not sent)
RXNE is set in I2Cx_ISR when the I2C_RXDR is full, and generates an interrupt if RXIE is set in I2Cx_CR1.
RXNE is cleared when I2Cx_RXDR is read.
When a STOP is received and STOPIE is set in I2Cx_CR1, STOPF is set in I2Cx_ISR and an interrupt is
generated.
Slave reception
Slave initialization
NO
ADDR=1?
YES SCL
Read ADDCODE and DIR, stretched
set ADDRCF(optional set
TXE)
NO
RXNE=1?
YES
Slave reception
Slave initialization
NO
NO STOPF=1?
RXNE=1?
YES
YES
optional set TXE and TXIS
Read RXDATA register
Set STOPCF
RXNE
transmission
EV1:ADDR ISR, read ADDCODE,DIR, set ADDRCF
S reception
EV2:RXNE ISR, read DATA1
TXE
A clock synchronization mechanism is implemented in order to support multi-master environment and slave
clock stretching.
detection.
● The high level of the clock is counted using the SCLH counter, starting from the SCL high level internal
detection.
The I2C detects its own SCL low level after a T sync1 delay depending on the SCL falling edge, SCL input
noise filters (analog + digital) and SCL synchronization to the I2CxCLK clock. The I2C releases SCL to high
level once the SCLL counter reaches the value programmed in the SCLL[7:0] bits in the I2Cx_TIMINGR
register.
The I2C detects its own SCL high level after a T sync2 delay depending on the SCL rising edge, SCL input
noise filters (analog + digital) and SCL synchronization to I2CxCLK clock. The I2C ties SCL to low level once
the SCLH counter is reached reaches the value programmed in the SCLH[7:0] bits in the I2C_TIMINGR
register.
Tsync2 Tsync1
SCLH
SCL
SCLL
SCL released SCL drive low
SCLH SCLH
SCLL SCLL
SCL released
SCL driven low by SCL driven low by
another device another device
signals
TF Fall time of both - 300 - 300 - 120 - 300 ns
SDA and SCL
signals
SCLL is also used to generate the T BUF and TSU:STA timings.
SCLH is also used to generate the T HD:STA and TSU:STO timings.
You must then set the START bit in I2C_CR2 register. Changing all the above bits is not allowed when
START bit is set.
Then the master automatically sends the START condition followed by the slave address as soon as it
detects that the bus is free (BSY = 0) and after a delay of tBUF.
In case of an arbitration loss, the master automatically switches back to slave mode and can acknowledge
its own address if it is addressed as a slave.
Note: The START bit is reset by hardware when the slave address has been sent on the bus, whatever the
received acknowledge value. The START bit is also reset by hardware if an arbitration loss occurs. 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 BSY=1.
Master
initialization
Initial setting
End
DATA NA P
11110x
1
x
Slave address R
S A DATA A DATA NA P
1st 7 bits W
A TXIS event generates an interrupt if the TXIE bit is set in the I2Cx_CR1 register. The flag is cleared when
the I2Cx_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.
Master
transmission
Master initialization
NBYTES=N,AUTOEND=0(for
RESTART),AUTOEND=1(for STOP)
No
No
NACKF=1? TXIS=1?
Yes Yes
No
NBYTES
transmitted?
Yes
TC=1?
No
Figure 25-18 Transfer sequence flowchart for I2C master transmitter for N≤255 bytes
Master
transmission
Master initialization
NBYTES=0xFF,N=N-255,
RELOAD=1, set START
No
No
NACKF=1? TXIS=1?
Yes Yes
No
NBYTES
transmitted?
Yes
Yes
TC=1?
No
TCR=1?
IF
N<256;NBYTES=N;RELOAD=
0;AUTOEND=0 FOR
RESTART;
AUTOEND=1 FOR STOP
ELSE
NBYTES=0xff;N=N-255;
RELOAD=1
Figure 25-19 Transfer sequence flowchart for I2C master transmitter for N>255 bytes
TXIS TXIS
TXE
NBYTES
xx 2
TXIS TXIS TC
TXE
NBYTES
xx 2
Master receiver
th
In the case of a read transfer, the RXNE flag is set after each byte reception, after the 8 SCL pulse. An
RXNE event generates an interrupt if the RXIE bit is set in the I2Cx_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 I2Cx_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.
Master reception
Master initialization
NBYTES=N,AUTOEND=0 (FOR
RESTART);AUTOEND=1(FOR
STOP),configure slave address,
set START bit
No
RXNE=1?
Yes
Read RXDR
No
NBYTES
received?
Yes
Yes
TC=1?
Master reception
Figure 25-21 Transfer sequence flowchart for I2C master receiver for N≤255 bytes
Master reception
Master initialization
NBYTES=0xFF,N=N-255;
RELOAD=1,configure slave
address,set START bit
No
RXNE=1?
Yes
Read RXDR
No
NBYTES
received?
Yes
Yes
TC=1?
No
TCR=1?
Yes
IF N<256
NBYTES=N;N=0;RELOAD=0;
AUTOEND=0 for RESTART;
AUTOEND=1 for STOP
ELSE
NBYTES=0XFF;N=N-255
RELOAD=1
End
Figure 25-22 Transfer sequence flowchart for I2C master receiver for N >255 bytes
RXNE RXNE
RXNE RXNE TC
INIT
EV1 EV2
NBYTES
xx 2
The tables below provide examples of how to program the I2C_TIMINGR to obtain timings compliant with
the I2C specification. There is an error between configuration values and the actual synchronous values.
Table 25-6 Examples of timings settings forFI2CCLK=8M
Parameter Standard-mode Fast-mode Fast-mode Plus
10kHz 100kHz 400kHz 500kHz
PRESC 1 1 0 0
SCLL 0xC7 0x13 0x9 0x6
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.
SMBus provides a control bus for system and power management related tasks.
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 version 2.0 (http://smbus.org/specs/).
This peripheral supports the Address Resolution Protocol (ARP). The SMBus Device Default Address
(0b1100 001) is enabled by setting SMBDEN bit in I2Cx_CR1 register. The ARP commands should be
implemented by the user software.
For more details of the SMBus Address Resolution Protocol, refer to SMBus specification version 2.0
(http://smbus.org/specs/).
When this protocol is used, the device acts as a master and the host as a slave.
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 will acknowledge 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 I2Cx_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 I2Cx_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
I2Cx_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.
The peripheral embeds a hardware PEC calculator and allows to send a Not Acknowledge automatically
when the received byte does not match with the hardware calculated PEC.
Timeouts
This peripheral embeds hardware timers in order to be compliant with the 3 timeouts defined in SMBus
specification version 2.0.
Table 25-9 SMBus timeout specifications
Limits
Symbol Parameter Unit
Min Max
TTIMOUT Detect clock low timeout 25 35 ms
TLOW:SEXT Cumulative clock low extend time (slave device) - 25 ms
TLOW:MEXT Cumulative clock low extend time (master device) - 10 ms
TLOW:SEXTis the cumulative time a given slave device is allowed to extend the clock cycles in one message
from the initial START to the STOP. It is possible that, another slave device or the master will also extend the
clock causing the combined clock low extend time to be greater than T LOW:SEXTis. Therefore, this parameter
is measured with the slave device as the sole target of a full-speed master.
TLOW:MEXT is the cumulative time a master device is allowed to extend its clock cycles within each byte of a
message as defined from START-to-ACK, ACK-to-ACK, or ACK-to-STOP. It is possible that a slave device
or another master will also extend the clock causing the combined clock low time to be greater than
TLOW:MEXT on a given byte. Therefore, this parameter is measured with a full speed slave device as the sole
This timing parameter covers the condition where a master has been dynamically added to the bus and may
not have detected a state transition on the SMBCLK or SMBDAT lines. In this case, the master must wait
long enough to ensure that a transfer is not currently in progress. The peripheral supports a hardware bus
idle detection.
Start Stop
TLOW:SEXT
SMBCLK
SMBCLK
ACK ACK
This section is relevant only when SMBus feature is supported. In addition to I2C initialization, some other
specific initialization must be done in order to perform SMBus communication:
PEC calculation is enabled by setting the PECEN bit in the I2Cx_CR1 register. Then the PEC transfer is
managed with the help of a hardware byte counter: NBYTES[7:0] in the I2Cx_CR2 register. The PECEN bit
must be configured before enabling the I2C.
The PEC transfer is managed with the hardware byte counter, so the SBC bit must be set when interfacing
the SMBus in slave mode. The PEC is transferred after NBYTES-1 data have been transferred when the
PECBYTE bit is set and the RELOAD bit is cleared. If RELOAD is set, PECBYTE has no effect.
Caution: Changing the PECEN configuration is not allowed when the I2C is enabled.
Table 25-10 SMBus with PEC configuration
Mode SBC RELOAD AUTOEND PECBYTE
Master Tx/Rx NBYTES+PEC+STOP x 0 1 1
Master Tx/Rx NBYTES+PEC+RESTART x 0 0 1
Slave Tx/Rx with +PEC 1 0 x 1
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 the
● TTIMEOUT check
In order to enable theTTIMEOUT 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
I2Cx_TIMEOUTR register. If SCL is tied low for a time greater than(TIMEOUTA+1)*2048*T I2C_CLK, the
TIMEOUT flag is set in the I2Cx_ISR register.
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, you can choose the same value for the both. Then the timer is enabled by setting the TEXTEN bit
in the I2Cx_TIMEOUTR register. If the SMBus peripheral performs a cumulative SCL stretch for a time
greater than (TIMEOUTB+1)*2048*TI2C_CLK, and in the timeout interval described in Bus idle the
TIMEOUT flag is set in the I2Cx_ISR register.
Caution: Changing the TIMEOUTB configuration is not allowed when the TEXTEN bit is set.
In addition to 2C slave transfer management some additional software flowcharts are provided to support
SMBus.
Caution: The PECBYTE bit has no effect when the RELOAD bit is set.
SMBus slave
transmission
Slave transmission
No
ADDR=1?
Yes
SCL
Read ADDRCODE and set DIR stretched
bit , NBYTES=N+1, set ADDRCF
TXIS=1?
No
Yes
Write TXDATA
register
Figure 25-25 Transfer sequence flowchart for SMBus slave transmitter N bytes + PEC
Figure 25-26 Transfer bus diagrams for SMBus slave transmitter (SBC=1)
In order to check the PEC byte, the RELOAD bit must be cleared and the PECBYTE bit must be set. In this
case, after NBYTES-1 data have been received, the next received byte is compared with the internal
I2Cx_PECR register content. A NACK is automatically generated if the comparison does not match, and an
ACK is automatically generated if the comparison matches, whatever the ACK bit value. Once the PEC byte
is received, it is copied into the I2Cx_RXDR register like any other data, and the RXNE flag is set.
In the case of a PEC mismatch, the PECERR flag is set and an interrupt is generated if the ERRIE bit is set
in the I2C_CR1 register.
If no ACK software control is needed, you can program PECBYTE=1 and, in the same write operation,
program NBYTES with the number of bytes to be received in a continuous flow. After NBYTES-1 are
received, the next received byte is checked as being the PEC.
Caution: The PECBYTE bit has no effect when the RELOAD bit is set.
SMBus slave
reception
Slave initialization
No
ADDR=1?
RXNE=1? No
TCR=1?
Yes
Read RXDATA,program
NACK=0,NBYTES=1,N=N-1
No
N=1?
Yes
Read RXDATA,program
RELOAD=0,NACK=0,NBYTES=1
No
RXNE=1?
Yes
Read RXDATA
End
Figure 25-28 Bus transfer diagrams for SMBus slave receiver (SBC=1)
This section is relevant only when SMBus feature is supported. In addition to I2C master transfer
management some additional software flowcharts are provided to support SMBus.
If the SMBus master wants to send a STOP condition after the PEC, automatic end mode should be
selected (AUTOEND=1). In this case, the STOP condition automatically follows the PEC transmission.
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.
TXIS TXIS
TXE
NBYTES
xx 3
TXE
NBYTES
xx 3 N
When the SMBus master receiver wants to receive the PEC byte followed by a RESTART condition at the
end of the transfer, software mode must be selected (AUTOEND=0). The PECBYTE bit must be set and the
slave address must be programmed, before setting the START bit. In this case, after NBYTES-1 data have
been received, the next received byte is automatically checked versus the I2C_PECR register content. The
TC flag is set after the PEC byte reception, stretching the SCL line low. The RESTART condition can be
programmed in the TC interrupt subroutine.
Caution: The PECBYTE bit has no effect when the RELOAD bit is set.
S transmission
EV2:RXNE ISR, read DATA2
The following are the error conditions which may cause communication to fail.
The bus error flag is set only if the I2C is involved in the transfer as master or addressed slave (i.e not during
the address phase in slave mode).
In case of a misplaced START or RESTART detection in slave mode, the I2C enters address recognition
state like for a correct START condition.
When a bus error is detected, the BERR flag is set in the I2Cx_ISR register, and an interrupt is generated if
the ERRIE bit is set in the I2Cx_CR1 register.
Arbitration lost(ARLO)
An arbitration loss is detected when a high level is sent on the SDA line, but a low level is sampled on the
SCL rising edge.
● In master mode, arbitration loss is detected during the address phase, data phase and data
acknowledge phase. In this case, the SDA and SCL lines are released, the START control bit is cleared
by hardware and the master switches automatically to slave mode.
● In slave mode, arbitration loss is detected during data phase and data acknowledge phase. In this case,
the transfer is stopped, and the SCL and SDA lines are released.
When an arbitration loss is detected, the ARLO flag is set in the I2Cx_ISR register, and an interrupt is
generated if the ERRIE bit is set in the I2Cx_CR1 register.
When an overrun or underrun error is detected, the OVR flag is set in the I2Cx_ISR register, and an interrupt
is generated if the ERRIE bit is set in the I2Cx_CR1 register.
A PEC error is detected when the received PEC byte does not match with the I2Cx_PECR register content.
A NACK is automatically sent after the wrong PEC reception.
When a PEC error is detected, the PECERR flag is set in the I2Cx_ISR register, and an interrupt is
generated if the ERRIE bit is set in the I2Cx_CR1 register.
When a timeout violation is detected in master mode, a STOP condition is automatically sent.
When a timeout violation is detected in slave mode, SDA and SCL lines are automatically released.
When a timeout error is detected, the TIMEOUT flag is set in the I2Cx_ISR register, and an interrupt is
generated if the ERRIE bit is set in the I2Cx_CR1 register.
Alert (ALERT)
This section is relevant only when the SMBus feature is supported.
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 I2Cx_CR1 register.
● In master mode: the initialization, the slave address, direction, number of bytes and START bit are
programmed by software (the transmitted slave address cannot be transferred with DMA). When all data
are transferred using DMA, the DMA must be initialized before setting the START bit. The end of transfer
is managed with the NBYTES counter.
● In slave mode:
-With NOSTRETCH=0, when all data are transferred using DMA, the DMA must be initialized before
the address match event, or in ADDR interrupt subroutine, before clearing ADDR.
-With NOSTRETCH=1, the DMA must be initialized before the address match event.
● For instances supporting SMBus: the PEC transfer is managed with NBYTES counter.
Note: If DMA is used for transmission, the TXIE bit does not need to be enabled.
Note: If DMA is used for reception, the RXIE bit does not need to be enabled.
When the microcontroller enters debug mode (core halted), the SMBus timeout either continues to work
normally or stops, depending on the DBG_I2Cx_SMBUS_TIMEOUT configuration bits in the DBG module.
25.2.19. Interrupts
Depending on the product implementation, all these interrupts events can either share the same interrupt
vector (I2C global interrupt), or be grouped into 2 interrupt vectors (I2C event interrupt and I2C error
interrupt).
In order to enable the I2C interrupts, the following sequence is required:
1. Configure and enable the I2C IRQ channel in the NVIC.
2. Configure the I2C to generate interrupts.
TCR
TC
TCIE
TXIS
TXIE
RXNE
RXNEIE I2C interrupt
glonal interrupt
STOPF
STOPIE
ADDR
ADDRIE erro interrupt
ERRIE
NACKF
NACKIE
BERR
OVR
ARLO
TIMEOUT PECERR
ALERT
0x1C
0x0C
Rev1.3
Address offset
Reset
Reset
Reset
Reset
Reset
Reset
Reset
Reset
Reset
Reset
Reset
Name
I2C_CR2
I2C_CR1
I2Cx_ICR
I2Cx_ISR
I2C_OAR1
I2Cx_OAR2
I2Cx_TXDR
I2Cx_RXDR
I2Cx_PECR
I2Cx_TIMINGR
I2Cx_TIMEOUTR
Fremont Micro Devices
x
x
x
x
x
x
x
x
x
– – – – – TEXTEN – – – – 31
x
x
x
x
x
0x
x
x
x
x
– – – – – – – – – – 30
0
PRESC[3:0]
x
x
x
x
x
x
x
x
x
– – – – – – – – – x – 29
x
x
x
x
x
x
x
x
x
x
– – – – – – – – – – 28
x
x
x
x
x
0x
x
x
x
x
– – – – – – – – – – 27
x
x
x
x
x
x
x
x
x
– – – – – – – – PECBYTE – 26
0
0
25.3. I2C register map
x
x
x
x
x
x
x
x
x
– – – – – – – – AUTOEND – 25
0
0
x
x
x
x
x
x
x
x
x
– – – – – – – – RELOAD – 24
0
0
x
x
x
x
x
x
– – – – – – PECEN 23
0
0
0
0
0
x
x
x
x
x
x
– – – – – – ALERTEBN 22
0
0
0
0
0
TIMEOUTB[11:0] SCLDEL[3:0]
x
x
x
x
x
– – – – – x – SMBDEN 21
0
0
0
0
0
x
x
x
x
x
x
– – – – ADDCODE[6:0] – – SMBHEN 20
0
0
0
0
0
NBYTE[7:0]
x
x
x
x
x
x
– – – – – – GCEN 19
535
0
0
0
0
x
x
x
x
x
x
0x
– – – – – – – 18
0
0
0
0
SDADEL[3:0]
x
x
x
x
x
x
– – – – – – NOSTRETCH 17
0
0
0
0
x
x
x
x
x
x
0x
– – – – DIR – – SBC 16
0
0
0
0
x
x
x
x
– – – – BUSY TIMOUTEN OA2EN OA1EN NACK RXDMAEN 15
0
0
0
x
x
x
x
0x
0x
0x
0x
– – – – – – – – STOP TXDMAEN 14
0
0
x
x
x
x
x
x
0x
0
0
0
0
x
x
x
x
x
0
0
0
0
0
0
SCLH[7:0]
x
x
x
x
x
0
0
0
0
0
0
x
x
x
– – – OVRCF OVR OA1MODE RD_WRN 10
0
0
0
0
0
0
0
0
DNF[3:0]
x
x
x
– – – ARLOCF ARLO OA2MSK[2:0] 9
0
0
0
0
0
0
0
0
x
x
x
– – – BERRCF BERR 8
0
0
0
0
0
0
0
0
– TCR ERRIE 7
0
0
0
0
0
0
0
0
0
0
0
– TC TCIE 6
0
0
0
0
0
0
0
0
0
0
0
TIMEOUTA[11:0]
STOPCF STOPF STOPIE 5
0
0
0
0
0
0
0
0
0
0
0
OA1[9:0] SADD[9:0]
NACKCF NACKF OA2[7:1] NACKIE 4
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0x
– RXNE RXIE 2
0
0
0
0
0
0
0
0
0
0
x
– TXIS TXIE 1
0
0
0
0
0
0
0
0
0
x
0x
– TXE – PE 0
0
0
0
1
0
0
0
0
0
FT32F0xxx8 RM
2024-03-22
Fremont Micro Devices FT32F0xxx8 RM
st
1: The master only sends the 1 7 bits of the 10 bit address, followed by
Read direction.
11 ADD10 10-bit addressing mode (master mode)
0: The master operates in 7-bit addressing mode,
1: The master operates in 10-bit addressing mode
10 RD_WRN Transfer direction (master mode)
0: Master requests a write transfer.
1: Master requests a read transfer.
9:0 SADD Slave address,(master mode)
In 7-bit addressing mode (ADD10 = 0):
SADD[9:8] and SADD[0] bits are don’t care, SADD[7:1] should be written
with the 7-bit slave address to be sent
In 10-bit addressing mode (ADD10 = 1):
These bits should be written with bits 10 of the slave address to be sent.
7:0 SCLL[7:0]
type RW
(TIMEOUT=1).
14:13 NA Reserved, undefined
12 TIDLE Idle clock timeout detection
0: TIMEOUTA is used to detect SCL low timeout
1: TIMEOUTA is used to detect both SCL and SDA high timeout (bus idle
condition)
11:0 TIMEOUTA Bus Timeout A
The SCL low timeout condition TTIMEOUT when TIDLE=0:
TTIMOUT=(TIMEOUTA+1)*2048*TI2CCLK
The bus idle condition (both SCL and SDA high) when TIDLE=1:
TIDLE=(TIMEOUTA+1)*4*TI2CCLK
transfer.
It is cleared by software by setting the STOPCF bit.
Note: This bit is cleared by hardware when PE=0
4 NACKF This flag is set by hardware when a NACK is received after a byte transmission.
It is cleared by software by setting the NACKCF bit.
Note: This bit is cleared by hardware when PE=0.
3 ADDR Address matched (slave mode)
This bit is set by hardware as soon as the received slave address matched with
one of the enabled slave addresses. It is cleared by software by setting
ADDRCF bit.
Note: This bit is cleared by hardware when PE=0.
2 RXNE Receive data register not empty (receivers)
This bit is set by hardware when the received data is copied into the I2C_RXDR
register, and is ready to be read. It is cleared when I2C_RXDR is read.
Note: This bit is cleared by hardware when PE=0.
1 TXIS Transmit interrupt status (transmitters)
This bit is set by hardware when the I2C_TXDR register is empty and the data
to be transmitted must be written in the I2C_TXDR register. It is cleared when
the next data to be sent is written in the I2C_TXDR register.
This bit can be written to ‘1’ by software when NOSTRETCH=1 only, in order to
generate a TXIS event (interrupt if TXIE=1 or DMA request if TXDMAEN=1).
Note: This bit is cleared by hardware when PE=0.
0 TXE Transmit data register empty (transmitters)
This bit is set by hardware when the I2C_TXDR register is empty. It is cleared
when the next data to be sent is written in the I2C_TXDR register.
This bit can be written to ‘1’ by software in order to flush the transmit data
register I2C_TXDR.
Note: This bit is set by hardware when PE=0.
This manual describe the full set of features implemented in USART1, USART2supports a smaller set of
features, but is otherwise identical to USART1. The differences are listed below.
The interface connect with external device by 3 pins as shown in Figure 26-1, any USART bidirectional
communication requires a minimum of two pins: Receive data In (RX) and Transmit data Out (TX):
RX: Receive data Input. This is the serial data input. Oversampling techniques are used for data recovery by
discriminating between valid incoming data and noise.
TX: Transmit data Output. When the transmitter is disabled, the output pin returns to its I/O port
configuration. When the transmitter is enabled and nothing is to be transmitted, the TX pin is at high level. In
single-wire half- duplex communication, this I/O is used to transmit and receive the data.
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(8 or 9 bit)least significant bit first
1,1.5,2 stop bits indicating that the frame is complete
The USART interface uses a baud rate generator (12-bit integer, 4-bit demicals)
A status register(USART_ISR)
Receive and transmit data registers(USART_RDR,USART_TDR)
A baud rate register USART_BRR)
nCTS: Clear To Send blocks the data transmission at the end of the current transfer when high
nRTS: Request to send indicates that the USART is ready to receive data (when low).
APB BUS
TDR RDR
SCLK
TX SCL control
SIR/ENDEC
block
RX
GT PSC USART_CR2
USART_CR3
USART_CR1
nRTS/DE
Hardware flow
controller
nCTS
USART_CR1 USART_ISR
Interrupt control
Fclk
The word length can be selected as being either 8 or 9 bits by programming the M0 bits in the USARTx_CR1
register
8-bit character length: M0=0
9-bit character length: M0=1
In default configuration, the signal (TX or RX) is in low state during the start bit. It is in high state during the
stop bit. These values can be inverted, separately for each signal, through polarity configuration control.
An Idle character is interpreted as an entire frame of “1”s. (The number of “1” ‘s will include
the number of stop bits).
A Break character is interpreted on receiving “0”s for a frame period. At the end of the break frame, the
transmitter inserts 2 stop bits.
Transmission and reception are driven by a common baud rate generator, the clock for each is generated
when the enable bit is set respectively for the transmitter and receiver.
Start bit0 bit1 bit2 bit3 bit4 bit5 bit6 bit7 bit8 Stop
Clock
*
Break Frame
Stop Start
Idle Frame
Start
Start bit0 bit1 bit2 bit3 bit4 bit5 bit6 bit7 Stop
Clock *
Break Frame
Stop Start
Idle Frame
Start
26.2.4. Transmitter
The transmitter can send data words of either 8 or 9 bits depending on the M bits status. The Transmit
Enable bit (TE) must be set in order to activate the transmitter function. The data in the transmit shift register
is output on the TX pin and the corresponding clock pulses are output on the SCLK pin.
Character transmission
During an USART transmission, data shifts out least significant bit first (default configuration) on the TX pin.
In this mode, the USARTx_TDR register consists of a buffer (TDR) between the internal bus and the
transmit shift register. 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: 1, 1.5 and 2 stop bits.
Note1: The TE bit must be set before writing the data to be transmitted to the USARTx_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.
Note2: 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. 1 stop bit: This is the default value of number of stop bits.
2. 2 stop bits: This will be supported by normal USART, single-wire and modem modes.
A break transmission will be 10 low bits (when M= 0) or 11 low bits (when M= 1) or followed by 2 stop bits. It
is not possible to transmit long breaks (break of length greater than 10/11 low bits).
Clock
*
transmission of the last frame is complete. This is required for instance when the USART is disabled or
enters the Halt mode to avoid corrupting the last transmission.
When a transmission is taking place, a write instruction to the USARTx_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 USARTx_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 USARTx_CR1 register.
After writing the last data in the USARTx_TDR register, it is mandatory to wait for TC=1 before disabling the
USART(Figure 26-4).
IDLE
TX line
TXE flag
USART_TDR F1 F2 F3
TC flag
Break characters
Setting the SBKRQ bit transmits a break character. The break frame length depends on the M0 bit(Figure
26-2).
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.
Idle characters
Setting the TE bit drives the USART to send an idle frame before the first data frame.
26.2.5. Receiver
The USART can receive data words of either 8 or 9 bits depending on the M0 bits in the USARTx_CR1
register.
In the USART, the start bit is detected when a specific sequence of samples is recognized. This sequence is:
1110X0X0X 0X0X0X0.
IDLE
LOW_DET START
RX line
Sample clk
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
sampled
values
7/16 7/16
one-bit time
Sample Data 1 1 0 X 0 X 0 X 0 0 0 0 X X X X X X X
Falling edge
detection
Zero Zero
Character reception
During an USART reception, data shifts in least significant bit first (default configuration) through the RX pin.
In this mode, the USARTx_RDR register consists of a buffer (RDR) between the internal bus and the receive
shift register.
Procedure:
1. Program the M0 bits in USARTx_CR1 to define the word length.
2. Select the desired baud rate using the baud rate register USARTx_BRR
3. Program the number of stop bits in USARTx_CR2.
4. Enable the USART by writing the UE bit in USARTx_CR1 register to 1.
5. Select DMA enable (DMAR) in USARTx_CR3 if multibuffer communication is to take place. Configure
the DMA register as explained in multibuffer communication.
6. Set the RE bit USARTx_CR1. This enables the receiver which begins searching for a start bit.
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 USARTx_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.
The choice of the clock source must be done according to two criteria:
● Possible use of the USART in low-power mode
● Communication speed.
The communication speed range (specially the maximum communication speed) is also determined by the
clock source.
The receiver implements different user-configurable oversampling techniques for data recovery by
discriminating between valid incoming data and noise. This allows a trade-off between the maximum
communication speed and noise/clock inaccuracy immunity.
The oversampling method can be selected by programming the OVER8 bit in the USARTx_CR1 register
and can be either 16 or 8 times the baud rate clock (Figure 26-6 and Figure 26-7).
Programming the ONEBIT bit in the USARTx_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
When noise is detected in a frame: The NF bit is set. It will generate an interrupt if the EIE bit is set in the
USARTx_CR3 register. The NF bit is reset by setting NFCF bit in ICR register.
RX line
Sample clk
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
sampled
values
7/16 7/16
RX line
Sample clk
1 2 3 4 5 6 7 8
sampled values
3/8 3/8
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; An interrupt will be issued if the EIE bit is
set in the USARTx_CR3 register. The FE bit is reset by writing 1 to the FECF in the USARTx_ICR register.
The baud rate for the receiver and transmitter (Rx and Tx) are both set to the same value as programmed in
the USARTx_BRR register.
Note: The baud counters are updated to the new value in the baud registers after a write operation to
USARTx_BRR. Hence the baud rate register value should not be changed during communication. The value
of the USARTDIV configuration must be greater than or equal to 16.
USARTDIV=2*48000000/921600=104(104d=68h)
BRR[3:0]= USARTDIV[3:0]>>1 = 8h>>1=4h
BRR=0x64
Table 26-3 Error calculation for programmed baud rates at f CK = 48MHz in both cases of
oversampling by 16 or by 8
Baud rate Oversampling by 16 Oversampling by 8
S.No Desired Actual BRR Error% Actual BRR Error%
1 2.4kBps 2.4kBps 0x4E20 0 2.4kBps 0x9C40 0
2 9.6kBps 9.6kBps 0x1388 0 9.6kBps 0x2710 0
3 19.2kBps 19.2kBps 0x9C4 0 19.2kBps 0x1384 0
4 38.4kBps 38.4kBps 0x4E2 0 38.4kBps 0x9C2 0
5 57.6kBps 57.62kBps 0x341 0.03 57.59kBps 0x681 0.02
6 115.2kBps 115.11kBps 0x1A1 0.08 115.25kBps 0x340 0.04
7 230.4kBps 230.76kBps 0xD0 0.16 230.21kBps 0x1A0 0.08
8 460.8kBps 461.54kBps 0x68 0.16 461.54kBps 0xD0 0.16
9 921.6kBps 923.07kBps 0x34 0.16 923.07kBps 0x64 0.16
10 2MBps 2MBps 0x18 0 2MBps 0x30 0
11 3MBps 3MBps 0x10 0 3MBps 0x20 0
12 4Mbps NA NA NA 4MBps 0x14 0
13 5MBps NA NA NA 5052.63kBps 0x11 1.05
14 6MBps NA NA NA 6MBps 0x10 0
Note: The lower the CPU clock the lower the accuracy for a particular baud rate.
The asynchronous receiver of the USART works correctly only if the total clock system deviation is less than
the tolerance of the USART receiver. The causes which contribute to the total deviation are:
● DTRA: Deviation due to the transmitter error (which also includes the deviation of the transmitter’s local
oscillator)
● DQUANT: Error due to the baud rate quantization of the receiver
● DREC: Deviation of the receiver’s local oscillator
● DTCL: Deviation due to the transmission line (generally due to the transceivers which can introduce an
asymmetry between the low-to-high transition timing and the high-to- low transition timing).
Requirement: DTRA+DQUANT+DREC+DTCL<USART receiver’s tolerance.
The USART receiver can receive data correctly at up to the maximum tolerated deviation specified
depending on the following choices:
● 10- or 11-bit character length defined by the M bits in the USARTx_CR1 register
● Oversampling by 8 or 16 defined by the OVER8 bit in the USARTx_CR1 register
● Demicals baud rate utilization
● Use of 1 bit or 3 bits to sample the data, depending on the value of the ONEBIT bit in the USARTx_CR3
register.
Table 26-4 Tolerance of the USART receiver when BRR [3:0] = 0000
OVER8=0 OVER8=1
M0 位
ONEBIT=0 ONEBIT=1 ONEBIT=0 ONEBIT=1
0 3.75% 4.375% 2.5% 3.75%
1 3.41% 3.97% 2.27% 3.41%
Table 26-5 Tolerance of the USART receiver when BRR[3:0] is different from 0000
OVER8=0 OVER8=1
M0 位
ONEBIT=0 ONEBIT=1 ONEBIT=0 ONEBIT=1
0 3.33% 3.88% 2% 3%
1 3.03% 3.53% 1.82% 2.73%
Note: The data specified in Table 26-4 and Table 26-5 may slightly differ in the special case when the
received frames contain some Idle frames of exactly 10-bit durations when M0 bits = 0 (11-bit durations
when M00 bits = 1)
The USART is able to detect and automatically set the USARTx_BRR register value based on the reception
of one character. Automatic baud rate detection is useful under two circumstances:
● The communication speed of the system is not known in advance
● The system is using a relatively low accuracy clock source and this mechanism allows the correct baud
rate to be obtained without measuring the clock deviation.
The clock source frequency must be compatible with the expected communication speed(oversampling by
16 must be selected and baudrate between fCK/65535 and fCK/16).
Before activating the auto baud rate detection, the character must be confirmed first .They can be chosen
through the ABRMOD[1:0] field in the USARTx_CR2 register:
● Any character starting with a bit at 1. In this case the USART measures the duration of the Start bit
(falling edge to rising edge).
● Any character starting with a 10xx bit pattern. In this case, the USART measures the duration of the
st
Start and of the 1 data bit. The measurement is done falling edge to falling edge, ensuring better
accuracy in the case of slow signal slopes.
Prior to activating auto baud rate detection, the USARTx_BRR register must be initialized by writing a
non-zero baud rate value.
If the line is noisy, the correct baud rate detection cannot be guaranteed. In this case the BRR value may be
corrupted and the ABRE error flag will be set. This also happens if the communication speed is not
compatible with the automatic baud rate detection range (bit duration not between
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.
It is possible to perform multiprocessor communication with the USART (with several USARTs connected in
a network). For instance one of the USARTs can be the master, its TX output connected to the RX inputs of
the other USARTs. The others are slaves, their respective TX outputs are logically ANDed together and
connected to the RX input of the master.
In multiprocessor configurations it is often desirable that only the intended message recipient should actively
receive the full message contents, thus reducing redundant USART service overhead for all non addressed
receivers.
The non addressed devices may be placed in mute mode by means of the muting function. In order to use
the mute mode feature, the MME bit must be set in the USARTx_CR1 register.
In mute mode:
● None of the reception status bits can be set.
● All the receive interrupts are inhibited.
● The RWU bit in USARTx_ISR register is set to 1. RWU can be controlled automatically by hardware or
by software, through the MMRQ bit in the USARTx_RQR register, under certain conditions.
The USART can enter or exit from mute mode using one of two methods, depending on the WAKE bit in the
USARTx_CR1 register:
● Idle Line detection if the WAKE bit is reset
● Address Mark detection if the WAKE bit is set
The USART enters mute mode when the MMRQ bit is written to 1 and the RWU is automatically set. It
wakes up when an Idle frame is detected(Figure 26-8).
RXNE RXNE
RWU
In this mode, bytes are recognized as addresses if their MSB is a ‘1’ otherwise they are considered as data.
In an address byte, the address of the targeted receiver is put in the 4 or 7 LSBs. The choice of 7 or 4-bit
address detection is done using the ADDM7 bit. This 4- bit/7-bit word is compared by the receiver with its
own address which is programmed in the ADD bits in the USARTx_CR2 register.
Note: In 7-bit and 9-bit data modes, address detection is done on 6-bit and 8-bit addresses (ADD[5:0] and
ADD[7:0]) respectively.
The USART enters mute mode when an address character is received which does not match its
programmed address. In this case, the RWU bit is set by hardware. The RXNE flag is not set for this address
byte and no interrupt or DMA request is issued when the USART enters mute mode.
The USART exits from mute mode when an address character is received which matches the programmed
address. Then the RWU bit is cleared and subsequent bytes are received normally. The RXNE bit is set for
the address character since the RWU bit has been cleared(Figure 26-9).
RXNE RXNE
RWU
Parity control (generation of parity bit in transmission and parity checking in reception) can be enabled by
setting the PCE bit in the USARTx_CR1 register. Depending on the frame length defined by the M0 bit, the
possible USART frame formats are as listed in Table 26-6.
Even parity
The parity bit is calculated to obtain an even number of “1s” inside the frame of the 7 or 8 (depending on M0
bits values) and the parity bit. (PS =0)
Odd parity
The parity bit is calculated to obtain an odd number of “1s” inside the frame of the 7 or 8 (depending on M0
bits values) and the parity bit. (PS =1)
The synchronous mode is selected by writing the CLKEN bit in the USARTx_CR2 register to 1. In
synchronous mode, the following bit must be kept cleared:
● HDSEL bit in the USARTx_CR3 register
In this mode, the USART can be used to control bidirectional synchronous serial communications in master
mode. The SCLK pin is the output of the USART transmitter clock. No clock pulses are sent to the SCLK pin
during start bit and stop bit. Depending on the state of the LBCL bit in the USARTx_CR2 register, clock
pulses are, or are not, generated during the last valid data bit (address mark). The CPOL bit in the
USARTx_CR2 register is used to select the clock polarity, and the CPHA bit in the USARTx_CR2 register is
used to select the phase of the external clock(see Figure 26-10 and Figure 26-11).
During the Idle state, preamble and send break, the external SCLK clock is not activated. In synchronous
mode the USART transmitter works exactly like in asynchronous mode. But as SCLK is synchronized with
TX (according to CPOL and CPHA), the data on TX is synchronous.
In this mode the USART receiver works in a different manner compared to the asynchronous mode. If RE=1,
the data is sampled on SCLK (rising or falling edge, depending on CPOL and CPHA), without any
oversampling. A setup and a hold time must be respected
Note:The SCLK 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 USARTx_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.
TX line Start Bit0 Bit1 Bit2 Bit3 Bit4 Bit5 Bit6 Bit7
Capture strobe
TX line Start Bit0 Bit1 Bit2 Bit3 Bit4 Bit5 Bit6 Bit7 Bit8
RX line Bit0 Bit1 Bit2 Bit3 Bit4 Bit5 Bit6 Bit7 Bit8
Capture strobe
Single-wire half-duplex mode is selected by setting the HDSEL bit in the USARTx_CR3 register. In this
mode, the following bit must be kept cleared:
● CLKEN bit in the USARTx_CR2 register
The USART can be configured to follow a single-wire half-duplex protocol where the TX and RX lines are
internally connected. The selection between half- and full-duplex communication is made with a control bit
HDSEL in USARTx_CR3.
The USART is capable of performing continuous communication using the DMA. The DMA requests for Rx
buffer and Tx buffer are generated independently.
When the number of data transfers programmed in the DMA Controller is reached, the DMA controller
generates an interrupt on the DMA channel interrupt vector.
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(Figure 26-12)
IDLE
TX line
TXE flag
USART_TDR F1 F2 F3
Cleared by software
DMA TCIF
TC flag
Set UE, DMA WaiteTXE=1, and TC is not set TC is not set TC is not set
send 3 data write data to TDR because TXE=0 because TXE=0 because TXE=1
To map a DMA channel for USART reception, use the following procedure:
1. Write the USARTx_RDR register address in the DMA control register to configure it as the source of the
transfer. The data is moved from this address to the memory after each RXNE event.
2. Write the memory address in the DMA control register to configure it as the destination of the transfer.
The data is loaded from USARTx_RDR to this memory area after each RXNE event.
3. Configure the total number of bytes to be transferred to the DMA control register
4. Configure the channel priority in the DMA control register
5. Configure interrupt generation after half/ full transfer as required by the application.
6. Activate the channel in the DMA control register.
When the number of data transfers programmed in the DMA Controller is reached, the DMA controller
generates an interrupt on the DMA channel interrupt vector (Figure 26-13).
RX line
RXNE flag
DMA req
USART_RDR F1 F2 F3
DMA TCIF
set UE, DMA
Read F1 Read F2 Read F3
receive 3 data
It is possible to control the serial data flow between 2 devices by using the nCTS input and the nRTS
output.The Figure 26-14 shows how to connect 2 devices in this mode:
USART1 USART2
TX RX
TX circuit RX circuit
nCTS nRTS
RX TX
RTS and CTS flow control can be enabled independently by writing the RTSE and CTSE bits respectively to
1 (in the USARTx_CR3 register).
START START
nRTS
DATA0 READ
RXNE DATA1 can send RXNE
When CTSE=1, the CTSIF status bit is automatically set by hardware as soon as the nCTS input toggles. It
indicates when the receiver becomes ready or not ready for communication. An interrupt is generated if the
CTSIE bit in the USARTx_CR3 register is set. Figure 26-16 shows an example of communication with CTS
flow control enabled.
START START
end of the last stop bit, in a transmitted message, and the de-activation of the DE signal. It is programmed
using the DEDT [4:0] bit fields in the USARTx_CR1 control register. The polarity of the DE signal can be
configured using the DEP bit in the USARTx_CR3 control register.
26.2.16. Interrupts
The USART interrupt events are connected to the same interrupt vector (Figure 26.15)
● During transmission: Transmission Complete, ransmit data Register empty.
● 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
TXIE
CTSIF
CTSI
USART
E
interrupt
IDLE
IDLEIE
RXNEIE
RXNE
ORE
RXNEIE
PE
PEIE
FE
NF
ORE EIE
CMF
CMIE
RTOF
RTOIE
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
Address offset Name
7
6
3
2
0
RXNEIE
OVER8
IDLEIE
RTIOE
WAKE
TXEIE
CMIE
DEAT[4:0]
DEDT[4:0]
MME
PEIE
TCIE
PCE
M1
M0
PS
TE
RE
UE
USARTx_CR1
–
–
–
–
0x00
Reset x x x 0x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0x 0
MSBFIRST
STOP[1:0]
DATAINV
ABRMOD[1:0]
ADD[7:4]
ADD[3:0]
RTOEN
ABREN
ADDM7
CLKEN
RXINV
TXINV
SWAP
CPHA
CPOL
LBCL
USARTx_CR2
–
–
–
–
–
0x04
Reset 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0x 0 0 0 0 0 0x x x 0x x x x
ONEBIT
OVRDIS
HDSEL
CTSIE
DMAR
DMAT
DDRE
CTSE
RTSE
DEM
DEP
EIE
USARTx_CR3
–
–
–
–
–
–
0x08
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0x x x 0x 0
BRR[15:0]
USARTx_BRR
–
–
–
–
–
–
–
–
0x0C
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
RTOR[23:0]
USARTx_RTOR
–
–
–
–
–
0x14
Reset x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
RXFRQ
SBKRQ
ABRRQ
MMRQ
USARTx_RQR
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x18
Reset x x x x x x x x x x x x x x x x x x x x x x x x x x x x 0 0 0 0
CTSIF
BUSY
ABRE
RXNE
SBKF
ABRF
RTOF
IDLE
CMF
ORE
CTS
TXE
PE
TC
NF
FE
USARTx_ISR
–
–
–
–
–
0x1C
Reset x x x x x x x x x x x x x 0 0 0 0 0x x 0 0 0x 1 1 0 0 0 0 0 0
IDLECF
ORECF
RTOCF
CTSCF
CMCF
PECF
TCCF
FECF
NCF
USARTx_ICR
–
–
–
–
–
–
–
0x20
Reset x x x x x x x x x x x x x x 0x x x x x 0x 0x x 0x 0 0 0 0 0
RDR[8:0]
USARTx_RDR
–
–
–
–
–
–
–
–
–
–
–
0x24
Reset x x x x x x x x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0
TDR[8:0]
USARTx_TDR
–
–
–
–
–
–
–
–
–
0x28
Reset x x x x x x x x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0
12 M0 Word length
This bit determines the word length.
0: 8-bit data
1: 9-bit data
11 WAKE Receiver wakeup method
0: Idle line
1: Address mark
10 PCE Parity control enable
0: Parity control disabled
1: Parity control enabled
9 PS Parity selection
0: Odd parity
1: Even parity
8 PEIE PE interrupt enable
0: PE interrupt disabled
1: PE interrupt enabled
7 TXEIE TXE interrupt enable
0: TXE interrupt disabled
1: TXE interrupt enabled
6 TCIE Transmission complete interrupt enable
0: Transmission complete interrupt disabled
1: Transmission complete interrupt enabled
5 RXNEIE RXNE interrupt enable
0: RXNE interrupt disabled
1: RXNE interrupt enabled
4 IDLEIE IDLE interrupt enable
0: IDLE interrupt disabled
1: IDLE interrupt enabled
3 TE Transmitter enable
0: Transmitter is disabled
1: Transmitter is enabled
2 RE Receiver enable
0: Receiver is disabled
1: Receiver is enabled
1 NA Reserved, undefined
0 UE USART enable
0: USART disabled
1: USART enabled
received in the shift register is ready to be transferred into the RDR register
while RXNE=1. It is cleared by a software, writing 1 to the ORECF, in the
USARTx_ICR register.
0: No overrun error
1: Overrun error is detected
2 NF Noise detection flag
0: No noise is detected
1: Noise is detected
1 FE Framing error, It is cleared by software, writing 1 to the FECF bit in the
USARTx_ICR register.
0: No Framing error is detected
1: Framing error or break character is detected
0 PE Parity error
It is cleared by software, writing 1 to the PECF in the USARTx_ICR register.
0: No parity error
1: Parity error
27.1. Introduction
The SPI interface can be used to communicate with external devices using the SPI protocol. SPI mode is
selectable by software. SPI Motorola 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 SPI allows synchronous, serial communication between the MCU and external devices. Application
software can manage the communication by polling the status flag or using dedicated SPI interrupt. The
main elements of SPI and their interactions are shown in the following block diagram Figure 27-1.
GPIO
Shifter
Tx Shifter Rx Shifter
CRC
Rx Control
Communication
Controller
Tx Control TxFIFO Controller RxFIFO Controller
SPI_TXCRCR
TxFIFO RxFIFO
BUS
APB Interface
INT_CTRL NVIC
Register File
DMA DMA
Controller
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
− detect a conflict between multiple masters
See Section 24.3.4: 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.
The SPI allows the MCU to communicate using different configurations, depending on the device targeted
and the application requirements. These configurations use 2 or 3 wires (with software NSS management)
or 3 or 4 wires (with hardware NSS management). Communication is always initiated by the master.
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
Shift register Shift register
MOSI MOSI
NSS(1) NSS(1)
Vcc Slave
Master
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.
MISO(2) MISO
Shift register Shift register
MOSI MOSI(2)
NSS(1) NSS(1)
Vcc Slave
Master
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 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.
MOSI MOSI
NSS(1) NSS(1)
Vcc Slave
Master
Figure 27-4 Simplex single master/single slave application (master in transmit-only/slave in receive-only
mode)
1. The NSS pin is configured as an input in this case.
2. The input information is captured in the shift register and must be ignored in standard transmit only
mode (for example, 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
In a configuration with two or more independent slaves, the master uses GPIO pins to manage the chip
select lines for each slave. The master must select one of the slaves individually by pulling low the GPIO
connected to the slave NSS input. When this is done, a standard master and dedicated slave
communication is established.
MISO MISO
Shift register Shift register
MOSI MOSI
MISO
Shift register
MOSI
SCK
NSS
Slave 2
MISO
Shift register
MOSI
SCK
NSS
Slave 3
In slave mode, the NSS works as a standard “chip select” input and lets the slave communicate with the
master. In master mode, NSS can be used either as output or input. As an input it can prevent multimaster
bus collision, and as an output it can drive a slave select signal of a single slave.
Hardware or software slave select management can be set using the SSM bit in the SPIx_CR1 register:
Software NSS management (SSM = 1): in this configuration, slave select information is driven internally
by the SSI bit value in register SPIx_CR1. The external NSS pin is free for other application uses.
Hardware NSS management (SSM = 0): in this case, there are two possible configurations. The
configuration used depends on the NSS output configuration (SSOE bit in register SPIx_CR1).
− 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). A pulse
can be generated between continuous communications if NSS pulse mode is activated (NSSP=1).
The SPI cannot work in multimaster configuration with this NSS setting.
− 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 Input
NSS GPIO
pin logic
During SPI communication, receive and transmit operations are performed simultaneously. The serial clock
(SCK) synchronizes the shifting and sampling of the information on the data lines. The communication
format depends on the clock phase, the clock polarity and the data frame format. To be able to communicate
together, the master and slaves devices must follow the same communication format.
(falling edge if the CPOL bit is set, rising edge if the CPOL bit is reset). Data are latched on each occurrence
of this clock transition type.
The combination of CPOL (clock polarity) and CPHA (clock phase) bits selects the data capture clock edge.
Figure 27-7 shows an SPI full-duplex transfer with the four combinations of the CPHA and CPOL bits.
Note1: Prior to changing the CPOL/CPHA bits the SPI must be disabled by resetting the SPE bit.
Note2: The idle state of SCK must correspond to the polarity selected in the SPIx_CR1 register (by
pulling up SCK if CPOL=1 or pulling down SCK if CPOL=0).
CPOL=1
CPOL=0
NSS(to slave)
CPHA=1
CPOL =1
CPOL =0
CPHA=0
DS<=8 bits: data is right-aligned on byte DS>8 bits: data is right-aligned on 16 bit
Example: DS=5 bit Example: DS=14 bit
7 54 0 15 14 13 0
xxx Data frame TX xx Data frame TX
7 54 0 15 14 13 0
000 Data frame RX 00 Data frame RX
Figure 27-8 Data alignment when data length is not equal to 8-bit or 16-bit
Note:The minimum data length is 4 bits. If a data length of less than 4 bits is selected, it is forced to an 8-bit
data frame size.
The configuration procedure is almost the same for master and slave. For specific mode setups, follow the
dedicated chapters. When a standard communication is to be initialized, perform these steps:
1. Write proper GPIO registers: Configure GPIO for MOSI, MISO and SCK pins.
2. Write to the SPI_CR1 register:
a) Configure the serial clock baud rate using the BR[2:0] bits (Note: 4).
b) Configure the CPOL and CPHA bits combination to define one of the four relationships between the
data transfer and the serial clock (CPHA must be cleared in NSSP mode). (Note: 2).
c) Select simplex or half-duplex mode by configuring RXONLY or BIDIMODE and BIDIOE (RXONLY
and BIDIMODE can’t be set at the same time).
d) Configure the LSBFIRST bit to define the frame format (Note: 2).
e) Configure the CRCL and CRCEN bits if CRC is needed (while SCK clock signal is at idle state).
f) Configure SSM and SSI (Note: 2 & 3).
g) Configure the MSTR bit (in multimaster NSS configuration, avoid conflict state on NSS if master is
configured to prevent MODF error).
3. Write to SPI_CR2 register:
a) Configure the DS[3:0] bits to select the data length for the transfer.
b) Configure SSOE (Note: 1 & 2 & 3).
c) Set the FRF bit if the TI protocol is required (keep NSSP bit cleared in TI mode).
d) Set the NSSP bit if the NSS pulse mode between two data units is required (keep CHPA and
FRFbits cleared in NSSP mode).
e) Configure the FRXTH bit. The RXFIFO threshold must be aligned to the read access size for the
SPIx_DR register.
f) Initialize LDMA_TX and LDMA_RX bits if DMA is used in packed mode.
4. Write to SPI_CRCPR register: Configure the CRC polynomial if needed.
5. Write proper DMA registers: Configure DMA streams dedicated for SPI Tx and Rx in DMA registers if the
DMA streams are used.
Note:
It is recommended to enable the SPI slave before the master sends the clock. If not, undesired data
transmission might occur. The data register of the slave must already contain data to be sent before starting
communication with the master (either on the first edge of the communication clock, or before the end of the
ongoing communication if the clock signal is continuous). The SCK signal must be settled at an idle state
level corresponding to the selected polarity before the SPI slave is enabled.
The master at full duplex (or in any transmit-only mode) starts to communicate when the SPI is enabled and
TXFIFO is not empty, or with the next write to TXFIFO.
In any master receive only mode (RXONLY=1 or BIDIMODE=1 & BIDIOE=0), master starts to communicate
and the clock starts running immediately after SPI is enabled.
The BSY bit being set indicates ongoing transaction of a current data frame. When the clock signal runs
continuously, the BSY flag stays set between data frames at master but becomes low for a minimum
duration of one SPI clock at slave between each data frame transfer..
Sequence handling
A few data frames can be passed at single sequence to complete a message. When transmission is enabled,
a sequence begins and continues while any data is present in the TXFIFO of the master. The clock signal is
provided continuously by the master until TXFIFO becomes empty, then it stops waiting for additional data.
In receive-only modes, half duplex (BIDIMODE=1, BIDIOE=0) or simplex (BIDIMODE=0, RXONLY=1) the
master starts the sequence immediately when both SPI is enabled and receive-only mode is activated. The
clock signal is provided by the master and it does not stop until either SPI or 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 master or slave in SPI mode, and data from
the slave is always transacted and processed by the master even if the slave could not prepare it 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 a single slave system it is not necessary to control the slave with NSS,
but it is often better to provide the pulse here too, to synchronize the slave with the beginning of each data
sequence. NSS can be managed by both software and hardware.
When the BSY bit is set it signifies an ongoing data frame transaction. Reading BSY status together with
FTLVL[1:0] to check if a transmission is fully completed, which is necessary before the system enter HALT.
Entering HALT before transmission completed might cause the data damaged. Checking BSY status also
can used for NSS pin management. When the dedicated frame transaction is finished, the RXNE flag is
raised. The last bit is just sampled and the complete data frame is stored in the RXFIFO.
frame). Specific procedure must be followed when disabling SPI in this mode.
Data received but not read remains stored in RXFIFO when the SPI is disabled, and must be processed the
next time the SPI is enabled, before starting a new sequence. To prevent having unread data, ensure that
RXFIFO is empty when disabling the SPI, by using the correct disabling procedure, or by initializing all the
SPI registers with a software reset via the control of a specific register dedicated to peripheral reset (see the
SPIiRST bits in the RCC_APBiRSTR registers).
Standard disable procedure is based on pulling BSY status together with FTLVL[1:0] to check if a
transmission session is fully completed. This check can be done in specific cases, too, when it is necessary
to identify the end of ongoing transactions, for example:
When NSS signal is managed by software and master has to provide proper end of NSS pulse for slave
When transactions’ streams from DMA or FIFO are completed while the last data frame or CRC frame
transaction is still ongoing in the peripheral bus.
The correct disable procedure is (except when receive only mode is used):
1. Wait until FTLVL[1:0] = 00 (no more data to transmit).
2. Wait until BSY=0 (the last data frame is processed).
3. Disable the SPI (SPE=0).
4. Read data until FRLVL[1:0] = 00 (read all the received 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 data until FRLVL[1:0] = 00 (read all the received data).
Note: If packing mode is used and an odd number of data frames with a format less than or equal to 8 bits
(fitting into one byte) has to be received, FRXTH must be set when FRLVL[1:0] = 01, in order to generate the
RXNE event to read the last odd data frame
Data packing
When the data frame size fits into one byte (less than or equal to 8 bits), data packing is used automatically
when any read or write 16-bit access is performed on the SPIx_DR register. The double data frame pattern
is handled in parallel in this case. At first, the SPI operates using the pattern stored in the LSB of the
accessed word, then with the other half stored in the MSB.
Figure 27-9 provides an example of data packing mode sequence handling. Two data frames are sent after
the single 16-bit access the SPIx_DR register of the transmitter. This sequence can generate just one RXNE
event in the receiver if the RXFIFO threshold is set to 16 bits (FRXTH=0). The receiver then has to access
both data frames by a single 16-bit read of SPIx_DR as a response to this single RXNE event. The RxFIFO
threshold setting and the following read access must be always kept aligned at the receiver side, as data
can be lost if it is not in line.
A specific problem appears if an odd number of such “fit into one byte” data frames must be handled. On the
transmitter side, writing the last data frame of any odd sequence with an 8- bit access to SPIx_DR is enough.
The receiver has to change the Rx_FIFO threshold level for the last data frame received in the odd
sequence of frames in order to generate the RXNE event.
16-bit access when write to data register 16-bit access when read from data register
SPI_DR =0x040A when TXE=1 SPI_DR =0x040A when RXNE=1
SPIx_DR register.
If data packing mode is used and the number of data to transfer is not a multiple of two, the
LDMA_TX/LDMA_RX bits must be set. The SPI then considers only one data for the transmission or
reception to serve the last DMA transfer
Communication diagrams
Some typical timing schemes are explained in this section. These schemes are valid no matter if the SPI
events are handled by pulling, interrupts or DMA. For simplicity, the LSBFIRST=0, CPOL=0 and CPHA=1
setting is used as a common assumption here. No complete configuration of DMA streams is provided.
The following numbered notes are common for Figure 27-10 and Figure 27-11.
1. The slave starts to control MISO line as NSS is active and SPI is enabled, and is disconnected from the
line when one of them is released. Sufficient time must be provided for the slave to prepare data
dedicated to the master in advance before its transaction starts.
2. At the master, BSY stays active between frames if the communication (clock signal) is continuous. At the
slave, BSY signal always goes down for at least one clock cycle between data frames.
3. The TXE signal is cleared only if TXFIFO is full.
4. The DMA arbitration process starts just after the TXDMAEN bit is set. The TXE interrupt is generated
just after the TXEIE is set. As the TXE signal is at an active level, data transfers to TxFIFO start, until
TxFIFO becomes full or the DMA transfer completes.
5. If all the data to be sent can fit into TxFIFO, the DMA Tx TCIF flag can be raised even before
communication on the SPI bus starts. This flag always rises before the SPI transaction is completed.
6. The CRC value for a package is calculated continuously frame by frame. Continuously frame by frame
in the SPIx_TxCRCR and SPIx_RxCRCR registers. The CRC information is processed after the entire
data package has completed, either automatically by DMA (Tx channel must be set to the number of
data frames to be processed) or by SW (the user must handle CRCNEXT bit during the last data frame
processing).
While the CRC value calculated in SPIx_TxCRCR is simply sent out by transmitter, received CRC
information is loaded into RxFIFO and then compared with the SPIx_RxCRCR register content (CRC
error flag can be raised here if any difference). This is why the user must take care to flush this
information from the FIFO, either by software reading out all the stored content of RxFIFO, or by DMA
when the proper number of data frames is preset for Rx channel (number of data frames + number of
CRC frames) (see the settings at the example assumption).
7. In data packed mode, TxE and RxNE events are paired and each read/write access to the FIFO is 16
bits wide until the number of data frames are even. If the TxFIFO is ¾ full FTLVL status stays at FIFO full
level. That is why the last odd data frame cannot be stored before the TxFIFO becomes ½ full. This
frame is stored into TxFIFO with an 8- bit access either by software or automatically by DMA when
LDMA_TX control is set.
8. To receive the last odd data frame in packed mode, the Rx threshold must be changed to 8-bit when the
last data frame is processed, either by software setting FRXTH=1 or automatically by a DMA internal
signal when LDMA_RX is set.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
NSS
SCK
BSY
2 2
MOSI MSB DTx1 MSB DTx2 MSB DTx3
SPE
TXE 3 3
FTLVL 10 11 10 11 10 00
4
MISO DTx1 LSB DTx2 LSB DTx3 LSB
RXN
1
E
DMA or software control at Rx events DTx1 DTx2 DTx3
FRLVL 00 10 00 10 00 10 00
DMA Rx
DMA Tx TICF 5 TICF
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
NSS
SCK
BSY 2
TXE 3 3
FTLVL 10 11 10 11 10 00
RXNE
FRLVL 00 10 00 10 00 10 00
DMA Rx
DMA Tx TICF 5 TICF
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
NSS
SCK
BSY 2
SPE
TXE 3
FTLVL 10 11 10 00
4
MISO DTx1 LSB DTx2 LSB CRC LSB
1 1
RXNE
FRLVL 00 10 00 10 00 10 00
DMA Rx
DMA Tx TICF 5 TICF 6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
NSS
SCK
BSY 2
DRx12 DRx34 DRx5
MOSI 5 4 3 2 1 5 4 3 2 1 5 4 3 2 1 5 4 3 2 1 5 4 3 2 1
SPE
TXE 3 3
4
MISO 5 4 3 2 1 5 4 3 2 1 5 4 3 2 1 5 4 3 2 1 5 4 3 2 1
DMA Rx
DMA Tx TICF 5 TICF
Three status flags are provided for the application to completely monitor the state of the SPI bus.
Busy flag(BSY)
The BSY flag is set and cleared by hardware (writing to this flag has no effect).
When BSY is set, it indicates that a data transfer is in progress on the SPI (the SPI bus is busy).
The BSY flag can be used in certain modes to detect the end of a transfer so that the software can disable
the SPI or its peripheral clock before entering a HALT mode. This avoids corrupting the last transfer.
The BSY flag is also useful for preventing write collisions in a multimaster system.
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: When the next transmission can be handled immediately by the master (e.g. if the master is in
Receive-only mode or its Transmit FIFO is not empty), communication is continuous and the BSY flag
remains set to ‘1’ between transfers on the master side. Although this is not the case with a slave, it is
recommended to use always the TXE and RXNE flags (instead of the BSY flags) to handle data
transmission or reception operations.
An SPI interrupt is generated if one of the following error flags is set and interrupt is enabled by setting the
ERRIE bit.
Overrun flag(OVR)
An overrun condition occurs when data is received by a master or slave and the RXFIFO has not enough
space to store this received data. This can happen if the software or the DMA did not have enough time to
read the previously received data (stored in the RXFIFO) or when space for data storage is limited. E.g. the
RXFIFO is not available when CRC is enabled in receive only mode so in this case the reception buffer is
limited into a single data frame buffer
When an overrun condition occurs, the newly received value does not overwrite the previous one in the
RXFIFO. The newly received value is discarded and all data transmitted subsequently is lost. Clearing the
OVR bit is done by a read access to the SPI_DR register followed by a read access to the SPI_SR register.
Mode fault(MODF)
Mode fault occurs when the master device has its internal NSS signal (NSS pin in NSS hardware mode, or
SSI bit in NSS software mode) pulled low. This automatically sets the MODF bit. Master mode fault affects
the SPI interface in the following ways:
The MODF bit is set and an SPI interrupt is generated if the ERRIE bit is set.
The SPE bit is cleared. This blocks all output from the device and disables the SPI interface.
The MSTR bit is cleared, thus forcing the device into slave mode.
Use the following software sequence to clear the MODF bit:
1. Make a read or write access to the SPIx_SR register while the MODF bit is set.
2. Then write to the SPIx_CR1 register.
To avoid any multiple slave conflicts in a system comprising several MCUs, the NSS pin must be pulled high
during the MODF bit clearing sequence. The SPE and MSTR bits can be restored to their original state after
this clearing sequence. As a security, hardware does not allow the SPE and MSTR bits to be set while the
MODF bit is set. In a slave device the MODF bit cannot be set except as the result of a previous multimaster
conflict.
CRC error(CRCERR)
This flag is used to verify the validity of the value received when the CRCEN bit in the SPIx_CR1 register is
set. The CRCERR flag in the SPIx_SR register is set if the value received in the shift register does not
match the receiver SPIx_RXCRCR value. The flag is cleared by the software.
This mode is activated by the NSSP bit in the SPIx_CR2 register and it takes effect only if the SPI interface
is configured as Motorola SPI master (FRF=0) with capture on the first edge (SPIx_CR1 CPHA = 0, CPOL
setting is ignored). When activated, an NSS pulse is generated between two consecutive data frame
transfers when NSS stays at high level for the duration of one clock period at least. This mode allows the
slave to latch data. NSSP pulse mode is designed for applications with a single master-slave pair.
Figure 27-14 illustrates NSS pin management when NSSP pulse mode is enabled.
NSS
SCK
27.3.12. TI mode
NSS
g g g
r lin r lin r lin
g g e mp g g e mp g g e mp t RELEASE
tri sa tri sa tri sa
SCK
FRAM1 FRAM2
Two separate CRC calculators are implemented in order to check the reliability of transmitted and received
data. The SPI offers CRC8 or CRC16 calculation independently of the frame data length, which can be fixed
to 8-bit or 16-bit. For all the other data frame lengths, no CRC is available.
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 (parallel CRC calculation) using an odd programmable polynomial
on each bit. For transmitter, CRC calculate when the TXFIFO transfer data to shift register. For receiver, the
CRC calculate when the shift register transfer data to RXFIFO. 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.
After the CRC reception, the CRC value is stored in the RXFIFO and must be read in the SPIx_DR register
in order to clear the RXNE flag.
The peripheral registers can be accessed by half-words (16-bit) or words (32-bit). SPI_DR
in addition by can be accessed by 8-bit access.
The following table shows the SPI register map and reset values.
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
BIDIMODE15
BIDIOE 14
CRCEN 13
CRCNEXT12
11
RXONLY 10
Address offset Name
9
8
LSBFIRST7
6
5
4
3
2
1
0
MSTR
CPHA
CPOL
CRCL
SSM
SPE
SSI
SPIx_CR1 BR[2:0]
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x00
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
RXDMAEN
TXDMAEN
LDMA_RX
LDMA_TX
RXNEIE
FRXTH
ERRIE
TXEIE
NSSP
SSOE
FRF
SPIx_CR2 DS[3:0]
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x04
Reset x x x x x x x x x x x x x x x x x 0 0 0 0 1
FRLVL[1:0] 1 1 0 0 0 0 0 0 0 0
FTLVL[1:0]
CRCERR
MODF
RXNE
OVR
BSY
FRE
TXE
SPIx_SR
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x08
Reset x x x x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0x x 1 0
SPIx_DR DR[15:0]
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x0C
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
SPIx_CRCPR CRCPOLY[15:0]
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x10
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
SPIx_RXCRCR RXCRC[15:0]
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x14
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
SPIx_TXCRCR TXCRC[15:0]
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x18
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
SSP
SPIx_SSPR
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x300
Reset x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x 0
slave is not accessed, the output from the accessed slave is not corrupted.
0: Full duplex (Transmit and receive)
1: Output disabled (Receive-only mode)
9 SSM Software slave management
When the SSM bit is set, the NSS pin input is replaced with the value from
the SSI bit.
0: Software slave management disabled
1: Software slave management enabled
Note:This bit is not used in SPI TI mode.
8 SSI Internal slave select
This bit has an effect only when the SSM bit is set. The value of this bit is
forced onto the NSS pin and the I/O value of the NSS pin is ignored.
Note:This bit is not used in SPI TI mode
7 LSBFIRST Frame format
0: data is transmitted / received with the MSB first
1: data is transmitted / received with the LSB first
Note:1. This bit should not be changed when communication is ongoing.
2. This bit is not used in SPI TI mode.
6 SPE SPI enable
0: Peripheral disabled
1: Peripheral enabled
Note:When disabling the SPI, follow the procedure described in Procedure
for disabling the SPI
5:3 BR[2:0] Baud rate control
000: 𝑓𝑃𝐶𝐿𝐾 /2
001: 𝑓𝑃𝐶𝐿𝐾 /4
010: 𝑓𝑃𝐶𝐿𝐾 /8
011: 𝑓𝑃𝐶𝐿𝐾 /16
100: 𝑓𝑃𝐶𝐿𝐾 /32
101: 𝑓𝑃𝐶𝐿𝐾 /64
110: 𝑓𝑃𝐶𝐿𝐾 /128
111: 𝑓𝑃𝐶𝐿𝐾 /256
Note:These bits should not be changed when communication is ongoing.
2 MSTR Master selection
0: Slave configuration
1: Master configuration
Note:This bit should not be changed when communication is ongoing.
1 CPOL Clock polarity
0: CK to 0 when idle
1: CK to 1 when idle
Note:1.This bit should not be changed when communication is ongoing.
2.This bit is not used in SPI TI mode.
0 CPHA Clock phase
0: RXNE event is generated if the FIFO level is greater than or equal to 1/2
(16-bit)
1: RXNE event is generated if the FIFO level is greater than or equal to 1/4
(8-bit)
11:8 DS[3:0] Data size
These bits configure the data length for SPI transfers
0000: Not used
0001: Not used
0010: Not used
0011: 4-bit
0100: 5-bit
0101: 6-bit
0110: 7-bit
0111: 8-bit
1000: 9-bit
1001: 10-bit
1010: 11-bit
1011: 12-bit
1100: 13-bit
1101: 14-bit
1110: 15-bit
1111: 16-bit
If software attempts to write one of the “Not used” values, they are forced to
the value “0111”(8-bit).
7 TXEIE Tx buffer empty interrupt enable
0: TXE interrupt masked
1: TXE interrupt not masked. Used to generate an interrupt request when the
TXE flag is set.
6 RXNEIE RX buffer not empty interrupt enable
0: RXNE interrupt masked
1: RXNE interrupt not masked. Used to generate an interrupt request when
the RXNE flag is set.
5 ERRIE Error interrupt enable
This bit controls the generation of an interrupt when an error condition
occurs (CRCERR, OVR, MODF in SPI mode, FRE at TI mode).
0: Error interrupt is masked
1: Error interrupt is enabled
4 FRF Frame format
0: SPI Motorola mode
1 SPI TI mode
Note: This bit must be written only when the SPI is disabled (SPE=0).
3 NSSP NSS pulse management
This bit should be set in master to enable NSSP mode. This bit can be set or
cleared in slave.it allow the SPI to generate an NSS pulse between two
consecutive data when doing continuous transfers. In the case of a single
data transfer, it forces the NSS pin high level after the transfer. It has no
meaning if CPHA = ’1’, or FRF = ’1’.
0: No NSS pulse
1: NSS pulse generated
Note: 1. This bit must be written only when the SPI is disabled (SPE=0).
2. This bit is not used in SPI TI mode.
2 SSOE SS output enable
0: SS output is disabled in master mode and the SPI interface can work in
multimaster configuration
1: SS output is enabled in master mode and when the SPI interface is
enabled. The SPI interface cannot work in a multimaster environment.
Note: This bit is not used in SPI TI mode.
1 TXDMAEN Tx buffer DMA enable
When this bit is set, a DMA request is generated whenever the TXE flag is
set.
0: Tx buffer DMA disabled
1: Tx buffer DMA enabled
0 RXDMAEN Rx buffer DMA enable
When this bit is set, a DMA request is generated whenever the RXNE flag is
set.
0: Rx buffer DMA disabled
1: Rx buffer DMA enabled
CPU
Endp;oint Control
interface
EP0 EP1-7 IN Interrupt
control
Control Control
OUT
Common
EP reg.
Serial Interace Engine (SIE): The SIE handles isochronous mode recognized, bit stuffing/unstuffing, and
CRC generation/checking, PID received and generated. It generates headers for packets to be transmitted
and decodes the headers of received packets.
Endpoint controller: Control each endpoint transfer state and direction.Two controller state machines are
used: one for control transfers over Endpoint 0, and one for Bulk, Interrupt or isochronous transactions over
Endpoints 1 to 7. Endpoints 1 to 7 only can be IN or OUT endpoint because the USB is half-duplex
619ffective619ng
RAM Controller: The RAM controller provides an interface to a single block of synchronous single-port
RAM which is used to buffer packets between the CPU and USB.
CPU Interface: The CPU Interface allows access to the control/status registers and the FIFOs for each
endpoint, and USB read/write SRAM data through CPU interface It also generates an interrupt to the CPU
when a packet has been successfully transmitted or received, or when the core enters Suspend mode or
resumes from Suspend mode.
1K USB RAM can be configured by the USB controller or AHB bus to be accessed. If the access method
changed when the data is transfer, it might cause data loss. It needs software to ensure the access method
changing not affect the data transfer.
Each endpoint can transfer data to the master. The data store in the corresponding FIFO block. The sending
data of each endpoint shared the same block with the received data, so sending data and receiving data can
not process at the same time. It need to be ensure whether the endpoint is configured as OUT when the
data is wrote to the buffer block, otherwise it should configured as IN. Each endpoint buffer distributed in
different area of 1kB SRAM as shown below. The INMAXP/OUTMAP register of each endpoint can not
exceed each endpoint maximum value.
0x000
Endpoint0 64B
0x040
Endpoint1 64B
0x080
Endpoint2 64B
0x0c0
Endpoint3 64B
0x100
Endpoint4 128B
0x180
Endpoint5 128B
0x200
Endpoint6 256B
0x300
Endpoint7 256B
0x400
Endpoint 1-7 is double-buffered, the value of IN/OUTMAXP register should be configured less than the half
of the endpoint related memory area. For example, double-buffered will beenabled automatically when the
configured value is less than 32B (Endpoint 1 phsical storage is 64B).
Accessing the USB RAM by AHB is need to set the URAMCTL and no need to set USBEN.
Softawre can read/write USB RAM when it accessed by AHB, like SRAM.
The USB RAM can be accessed by byte (8-bit) but can be not access half words (16-bit) or words (32-bit).
The USB peripheral interfaces with the USB host, detecting token packets, handling data
transmission/reception, and processing handshake packets as required by the USB standard. Packet is the
basic unit of USB transfmission, all data is packed than transfer in the AHB.
Byte
1 0-1024 2
number
Control transferl
The host send request to the specified device by endpoint 0 incontrol transfer, then the device respond an
ACK
The OUTPKTRDY of endpoint0 is set when endpoint0 receive a data packet, then CPU read the data packet
from FIFO and operates according to the data of the packet. If endpoint0 need to send data, then should
switch the FIFO direction to IN and write the data to the FIFO, eventually the corresponding INPKTRDY will
be set. The data will be send to the host automatically when the USB controller receive the IN token packets.
The receiver will respond an ACK if it is finish receiving and calibrate correct, otherwise respond a NACK.
When the endpoint0 receive a SETUP packet in idle state, the OUTPKTRDY bit set and enter the
corresponding interrupt if it is enabled, and read the FIFO data in the interrupt, then switch the FIFO
direction (MODE bit in the INCSR register) according to the instruction. Data will not be transfer if SETUP
packet set the address and interface command.
OUT
INT
INT
INT
INT
Setup IN DATA IN DATA
STATE
OUT OUT IN
INT
INT
INT
INT
Setup
DATA DATA STATE
IN
INT
INT
Setup INT:interrupt
STATE
IDLE state
OUTPKTRDY==1? No
Yes
Decode command
Yes
Set
SERVICEDOUTPKTRDY
Set and set DATAEND
SERVICEDOUTPKTRDY
Return
No
Return
Data received
Return
Bulk transfer
Bulk transfer mode can nonperiodic exchange data with host. Configure FIFO in output mode can write data
to FIFO, then the controller receive IN token and automatically send the FIFO data to the host. After the host
finsh calibrating, it will respond an ACK. Configure FIFO in input mode, then the controller receive OUT
token and automatically receive the FIFO data to the host. After the host finsh calibrating, it will respond an
ACK, otherwise it respond a NACK.
Bulk transfer can enable duble-buffered mode. When the value of INMAXP/OUTMAXP is less than or equal
to the FIFO size, double packet buffering is enabled, two data packets can be buffered in the FIFO. If the
last packet not be read already, the new packet can be received and both packet will not loss. It can also
advanced write two packets to FIFO when it is sending.
As a packet to be sent is loaded into the FIFO, the INPKTRDY bit should be set to inform the controller
write operation is complete. After the AUTOSET bit is set, the loaded data in FIFO is equal to the INMAX,
the INPKTRDY will be set automatically. After reading the FIFO data, the SERVICEDOUTPKTRDY should
be set to clear OUTPKTRDY flag, the OUTPKTRDY will be cleared automatically after the AUTOCLEAR is
enable and finish reading the FIFO data.
Set SENDSTALL bit can disable the corresponding transfer state, when the software receive the
SENTSTALL flag, the SENTSTALL bit should be cleared. If an endpoint is no need then the SENDSTALL
should be 1. After clear the SENDSTALL bit ,the corresponding endpoint can be enable, and the
CLRDATATOG bit should be set to recover the data toggle state.
Idle Idle
NO
SENDSTALL==1?
Valid IN token?
YES
YES
Valid data(No
timeout)?
YES SENDSTALL==1?
YES
NO
INPKTRDY cleared
Interrupt generated
Interrupt generated
Idle
Idle
Figure 28-6 Endpoint data send and receive flow chart in bulk mode
Interrupt mode
Interrupt mode can periodic exchange data with host, and the operation is similar to the bulk mode. Set
FRCDATATOG bit to force the data sending state to toggle. Whether the host respond an ACK or not, the
transmission will be recognized as succedd.
Double packet buffering is enabled in this mode when the INMAXP/OUTMAXP data is not greater than the
half the FIFO size.
After the AUTOSET bit is set, the corresponding endpoint FIFO data reach the configured value of the
INMAX, INPKTRDY will be set automatically. After AUTOCLEAR is set and endpoint receive data,
OUTPKTRDY will be cleared automatically when the CPU read the OUTMAXP.
In Isochronous mode, If FIFO do not have any data (CPU do not have enough time to write data into the
FIFO), UNDERRUN bit is set when a zero length data packet is sent. If FIFO do not have enough space
(CPU do not have enough time to read data from the FIFO), the OVERRUN bit is set . when the OVERRUN
or the UNDERRUN is set ,the software should clear it by increase CPU clock frequency etc. The
OUTPKTRDY bit and the DATAERROR bit will be set in Isochronous mode when the error is received (for
example CRC calibration falied), the software also should clear it.
Idle Idle
NO
NO YES
Valid IN token?
YES
YES
Yes
NO
INPKTRDY cleared,
Interrupt generated
OUTPKTRDY set,
interrupt generated
Idle
Idle
Figure 28-7 Endpoint data send and receive flow in Isochronous mode
The controller will enter the suspwnd mode when the USB bus keep in idle time exceed than 3ms, and the
SUSPENDE is set. The corresponding interrupt event will be generated if the SUSPENDIE
When the bus detect the resume signal, the controller will exit the suspend mode. Or set the RESUME bit to
force the interface to exit suspend state.
7
6
5
4
3
2
1
0
FADDR[7:0]
USB_FADDR
0x00
Reset 0 0 0 0 0 0 0 0
ISOUPDATE
SUSPENDM
SUSPENDE
RESUME
RESET
USB_POWER
0x01
_
_
_
Reset 0x x x 0 0 0 0
EP7INF
EP6INF
EP5INF
EP4INF
EP3INF
EP2INF
EP1INF
EP0F
USB_INTRIN
0x02
EP5OUTF
EP4OUTF
EP3OUTF
EP2OUTF
EP1OUTF
USB_INTROUT
0x04 _
Reset 0 0 0 0 0 0 0x
SUSPEND
RESUME
RESET
USB_INTRUSB
0x06
SOF
_
_
_
_
Reset x x x x 0 0 0 0
EP7INIE
EP6INIE
EP5INIE
EP4INIE
EP3INE
EP2INE
EP1INE
USB_INTRINE
EP0E
0x07
Reset 1 1 1 1 1 1 1 1
EP7OUTIE 7
EP6OUTIE 6
EP5OUTIE 5
EP4OUTIE 4
EP3OUTIE 3
EP2OUTIE 2
EP1OUTIE 1
0
USB_INTROUTE
0x09
_
Reset 1 1 1 1 1 1 1 x
SUSPENDIE
RESUMEIE
RESETIE
USB_INTRUSBE
0x0b
SOFIE
_
_
_
_
Reset x x x x 0 1 1 0
USB_FRAM1 FRAME[7:0]
0x0c
USB_FRAM2
0x0d
_
_
_
_
_
Reset x x x x x 0 0 0
INDEX[2:0]
USB_INDEX
0x0e
_
_
_
_
_
Reset x x x x x 0 0 0
PDEN
PUEN
USB_PDCTRL
0x0f
_
_
_
_
_
_
Reset x x x x x x 0 0
0
Offset Register Description
INMAXP[7:0]
0x10 USB_INMAXP
Reset 0 0 0 0 0 0 0 0
SOUTPKTRDY
SSETUPEND
OUTPKTRDY
SENDSTALL
SENTSTALL
SETUPEND
INPKTRDY
DATAEND
0x11 USB_CSR0 (Endpoint0)
Reset 0 0 0 0 0 0 0 0
CLRDATATOG
SENDSTALL
SENTSTALL
UNDERRUN
FLUSHFIFO
INPKTRDY
0x11 USB_INCSR1 (Endpoint1-7)
FIFONE
Reset 0 0 0 0 0 0 0 0
FRCDATATOG
AUTOSET
_
0x12 USB_INCSR2
MODE
ISO
0x13 USB_OUTMAXP
Reset 0 0 0 0 0 0 0 0
CLRDATATOG
DATAERROR
OUTPKTRDY
SENDSTALL
SENTSTALL
FLUSHFIFO
OVERRUN
0x14 USB_OUTCSR1
FIFOF
Reset 0 0 0 0 0 0 0 0
AUTOCLEAR
0x15 USB_OUTCSR2
ISO
Reset 0 0x x x x x x
Reset 0 0 0 0 0 0 0 0
7
6
5
4
3
2
1
0
Offset Register Description
FIFO0[7:0]
0x20 USB_FIFO0
Reset x x x x x x x x
FIFO1[7:0]
0x24 USB_FIFO1
Reset x x x x x x x x
FIFO2[7:0]
0x28 USB_FIFO2
Reset x x x x x x x x
FIFO3[7:0]
0x2c USB_FIFO3
Reset x x x x x x x x
Data register
FIFO4[7:0]
0x30 USB_FIFO4
Reset x x x x x x x x
FIFO5[7:0]
0x34 USB_FIFO5
Reset x x x x x x x x
FIFO6[7:0]
0x38 USB_FIFO6
Reset x x x x x x x x
FIFO7[7:0]
0x3c USB_FIFO7
Reset x x x x x x x x
28.3.1. USB_FADDR
Bit 7 6 5 4 3 2 1 0
7:0 UPDATE FADDR[6:0]
Type RO RW
28.3.2. USB_POWER
Bit 7 6 5 4 3 2 1 0
7:0 ISOUPDATE — RESET RESUME SUSPENDM SUSPENDE
Type RW RO-0 RO-0 RO-0 RO RW RO RW
28.3.3. USB_INTRIN
Bit 7 6 5 4 3 2 1 0
7:0 EP7INF EP6INF EP5INF EP4INF EP3INF EP2INF EP1INF EP0F
Type RO RO RO RO RO RO RO RO
28.3.4. USB_INTROUT
Bit 7 6 5 4 3 2 1 0
7:0 EP7OUTF EP6OUTF EP5OUTF EP4OUTF EP3OUTF EP2OUTF EP1OUTF —
Type RO RO RO RO RO RO RO RO-0
28.3.5. USB_INTRUSB
Bit 7 6 5 4 3 2 1 0
7:0 — SOF RESET RESUME SUSPEND
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
28.3.6. USB_INTRINE
Bit 7 6 5 4 3 2 1 0
7:0 EP7INE EP6INE EP5INE EP4INE EP3INE EP2INE EP1INE EP0IE
Type RW RW RW RW RW RW RW RW
28.3.7. USB_INTROUTE
Bit 7 6 5 4 3 2 1 0
7:0 EP7OUTE EP6OUTE EP5OUTE EP4OUTE EP3OUTE EP2OUTE EP1OUTE —
Type RW RW RW RW RW RW RW RO-0
28.3.8. USB_INTRUSBE
Bit 7 6 5 4 3 2 1 0
7:0 — SOFIE RESETIE RESUMEIE SUSPENDIE
Type RO-0 RO-0 RO-0 RO-0 RW RW RW RW
28.3.9. USB_FRAM1
Bit 7 6 5 4 3 2 1 0
7:0 FRAM1[7:0]
Type RO RO RO RO RO RO RO RO
28.3.10. USB_FRAM2
Bit 7 6 5 4 3 2 1 0
7:0 — FRAM2[2:0]
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO
28.3.11. USB_INDEX
Bit 7 6 5 4 3 2 1 0
7:0 — INDEX[3:0]
Type RO-0 RO-0 RO-0 RO-0 RW
28.3.12. USB_PDCTRL
Bit 7 6 5 4 3 2 1 0
7:0 — PDEN PUEN
Type RO-0 RO-0 RO-0 RO-0 RO-0 RO-0 RW RW
28.3.13. USB_CSR0
Bit 7 6 5 4 3 2 1 0
7:0 SSETUPE SOUTPKT SENDSTAL SETUPEN DATAEND SENTSTAL INPKTR OUTPKT
ND RDY L D L DY RDY
Type W1 W1 W1 RO W1 RC_W0 RS RO
28.3.14. USB_OUTCOUNTER
Bit 7 6 5 4 3 2 1 0
7:0 OUTCOUNTER1[7:0]
Type RO RO RO RO RO RO RO RO
28.3.15. USB_INMAXP
Bit 7 6 5 4 3 2 1 0
7:0 INMAXP[7:0]
Type RW RW RW RW RW RW RW RW
28.3.16. USB_INCSR1
Bit 7 6 5 4 3 2 1 0
7:0 — CLRDAT SENTSTALL SENDSTALL FLUSHFIFO OVERRUN FIFONE INPKTRDY
ATOG
Type RO-0 W1 RC_W0 RW W1 RC_W0 RC_W0 RS
28.3.17. USB_INCSR2
Bit 7 6 5 4 3 2 1 0
7:0 AUTOSET ISO MODE — FRCDATATOG —
Type RW RW RW RO-0 RW RO-0 RO-0 RO-0
28.3.18. USB_OUTMAXP
Bit 7 6 5 4 3 2 1 0
7:0 OUTMAXP[7:0]
Type RW RW RW RW RW RW RW RW
28.3.19. USB_OUTCSR1
Bit 7 6 5 4 3 2 1 0
7:0 CLRDAT SENTSTAL SENDSTAL FLUSHFIF DATAERRO OVERRU FIFOF OUTPKTR
ATOG L L O R N DY
Type W1 RC_W0 RW W1 RO RC_W0 RO RC_W0
28.3.20. USB_OUTCSR2
Bit 7 6 5 4 3 2 1 0
7:0 AUTOCLEAR ISO —
Type RW RW RO-0 RO-0 RO-0 RO-0 RO-0 RO-0
28.3.21. USB_FIFO0
Bit 7 6 5 4 3 2 1 0
7:0 FIFO0[7:0]
Type RW
28.3.22. USB_FIFO1
Bit 7 6 5 4 3 2 1 0
7:0 FIFO1[7:0]
Type RW
28.3.23. USB_FIFO2
Bit 7 6 5 4 3 2 1 0
7:0 FIFO2[7:0]
Type RW
28.3.24. USB_FIFO3
Bit 7 6 5 4 3 2 1 0
7:0 FIFO3[7:0]
Type RW
28.3.25. USB_FIFO4
Bit 7 6 5 4 3 2 1 0
7:0 FIFO4[7:0]
Type RW
28.3.26. USB_FIFO5
Bit 7 6 5 4 3 2 1 0
7:0 FIFO5[7:0]
Type RW
28.3.27. USB_FIFO6
Bit 7 6 5 4 3 2 1 0
7:0 FIFO6[7:0]
Type RW
28.3.28. USB_FIFO7
Bit 7 6 5 4 3 2 1 0
7:0 FIFO7[7:0]
Type RW
29.1. Introduction
The touch sensing controller provides a simple solution for adding capacitive sensing functionality to any
application. Capacitive sensing technology is able to detect finger presence near an electrode which is
protected from direct touch by a dielectric. The capacitive variation introduced by the finger (or any
conductive object) is measured using a proven implementation based on a surface charge transfer
acquisition principle.
The surface charge transfer acquisition is a proven, robust and efficient way to measure a capacitance. It
uses a minimum number of external components to operate with a single ended electrode type. This
acquisition is designed around an analog I/O group which is composed of four GPIOs
VDD
VLDO Gx_LDO_MOD_SEL
00 FLOAT
Gx_VDD_EN Gx_VLDO_EN
01 VLDO
10 VDD
11 GND
Gx_GND_EN
The pulse low state of positive PWM and pulse high state of negative PWM stage is charging.
The pulse high state of positive PWM and pulse low state of negative PWM stage is charge transfers to the
sampling capacitor.
A dead time is inserted between the pulse high and low states to ensure an optimum charge transfer
acquisition sequence, the Charge transfer acquisition sequence diagram as shown below:
touch_ldo_en
vldo_en
key_en
touch_ldo_en
vldo_en
LDO always on
KEY pwm mode
cap_en
key_en
touch_ldo_en
cap_en
key_en
touch_ldo_en
vldo_en
LDO always on
KEY always on
cap_en
key_en
Vcs
VDD
Threshold=VIH
0 TIME
Frequency hopping allows to generate a variation of the charge transfer frequency. This is done to improve
the robustness of the charge transfer acquisition in noisy environments and also to reduce the induced
emission. Enable/disable by configure FHSS_MODE. This mode is control the PWM generated by TIM17
and make the period and duty cycle changing continuously, the timing as shown below:
cap_en
key_en
transfer transfer transfer transfer transfer transfer transfer transfer
+0 +0 +1 +1 +2 +2 +3 +0
Software mode
Configure TSC register to change the TSC LDO mode to control the charge-discharge process, the
procedure as shown below:,
1. Using the comparator to detect the voltage of sampling capacitor; one of the comparator input connect
to the sampling capacitor corresponding I/O, the other input connect to the output of DAC;
2. Initialize the TSC charging; Enable sampling capacitor and touchkey, set MOD_SEL=11, select GND
discharge mode to initialize the TSC discharging;
3. Enter deadtime, Disable sampling capacitor and touchkey and set MOD_SEL=00 to float touchkey.;
4. Charge the touchkey ; Enables touchkey and set MOD_SEL=01 to select VLDO voltage tocharge the
touchkey;
5. Enter deadtime again;
6. Sampling the transfer charge; Enable sampling capacitor and touchkey, the charge of touchkey will
transfer to the sampling capacitor.
7. Repeat the procedure 3~6 until the voltage of the sampling capacitor is exceed the threshold.
Hardware mode
Hardware PWM mode need to opeartewith TIM17 and the TIM17 should provide 2 complementary PWM
output with inserted deadtime. PWM waveform correspond to charge transfer sampling:
Positive PWM output pulse low and negative PWM output pulse high correspond to the charging of the
charge transfer sampling;
Positive PWM output pulse low and negative PWM output pulse low(deadtime) correspond to the
deadtime of the charge transfer sampling;
Positive PWM output pulse high and negative PWM output pulse low correspond to the transfer of the
charge transfer sampling;
The procedure of the hardware PWM mode as shown below:
1. Using the comparator to detect the voltage of sampling capacitor; one of the comparator input connect
to the sampling capacitor corresponding I/O, the other input connect to the output of DAC;
2. Initialize the TSC charging; Enable sampling capacitor and touchkey, set MOD_SEL=11, select GND
discharge mode to initialize the TSC discharging;
3. Configure TSC as hardware mode (); Enable sampling capacitor and touchkey before set PWM_EN=1;
4. Configure TIM17 to generate the required PWM output
G3_CAP_EN 26
G2_CAP_EN 25
G1_CAP_EN 24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
Offset Register
9
8
7
6
5
4
3
2
1
0
LDO_MODE
0x00
Reset X X X 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
HW_MOD_SEL
G3_PWM_SEL
G2_PWM_SEL
G1_PWM_SEL
G3_PWM_EN
G2_PWM_EN
G1_PWM_EN
G3_CG_MOD
G2_CG_MOD
G1_CG_MOD
MOD_SEL
MOD_SEL
MOD_SEL
G3_LDO_
G2_LDO_
G1_LDO_
TSC_CFGR
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
–
0x04
Reset X X X X X X X X X X X 0 0 0 0 0 X X X 0 0 0 0 0 X X X 0 0 0 0 0
29.4.1. TSC_CR
29.4.2. TSC_CFGR
30.1. Overview
The FT32F0xxx8 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 FT32F0xxx8
Support:
Serial wire
Bus matrix
System
interface
Cortex-M0
Core
Debug AP
}
SWDIO
SWCLK
SW-DP
Debug AP
Bridge DBGMCU
NVIC
DWT
BPU
MCU debug box (support for low-power modes), control over peripheral clocks
FT32F0xxx8 MCUs are available in various packages with different numbers of available pins.
Two pins of FT32F0xxx8 are used as outputs for the SW-DP as general purpose I/Os. These pins are
available on all packages.
Table 30-1 port pins
SW debug port
SWJ-DP pin name Pin assignment
Type Debug assignment
Serial Wire Data
SWDIO input/output PA13
Input/Output
SWCLK input Serial Wire Clock PA14
Once the SW I/O is released by the user software, the GPIO controller takes control of these pins, it is
necessary to ensure those pins are not configured in floating mode. The reset states of the GPIO control
registers put the I/Os in the equivalent states:
SWDIO: input pull-up
SWCLK: input pull-down
Those I/Os can be configured as normal GPIOs by software.
The FT32F0xxx8 products integrate an MCU ID code. This ID identifies the MCU part number and the die
revision. This code is accessible by the software debug port (two pins) or by the user software.
The protocol allows two banks of registers (DPACC registers and APACC registers) to be read and written
to.
Bits are transferred LSB-first on the wire.
For SWDIO bidirectional management, the line must be pulled-up on the board (100 kΩ recommended)
Each time the direction of SWDIO changes in the protocol, a turnaround time is inserted where the line is not
driven by the host nor the target. By default, this turnaround time is one bit time, however this can be
adjusted by configuring the SWCLK frequency.
Refer to the CPU 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.
Bit Name Description
0..2 ACK 001: FAULT
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.
Bit Name Description
0..31 WDATA/RDATA Write or Read data
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.
The State Machine of the SW-DP has an internal ID code which identifies the SW-DP. It follows the JEP-106
standard.
Note:Note that the SW-DP state machine is inactive until the target reads this ID code.
The SW-DP state machine is in RESET STATE either after power-on reset, or after the line is high for more
than 50 cycles.
The SW-DP state machine is in IDLE STATE if the line is low for at least two cycles after RESET state.
After RESET state, it is mandatory to first enter into an IDLE state AND to perform a READ access of the
DP-SW ID CODE register. Otherwise, the target will issue a FAULT acknowledge response on another
transactions.
Read accesses to the DP are not posted: the target response can be immediate (if ACK=OK) or can be
delayed (if ACK=WAIT).
Read accesses to the AP are posted. This means that the result of the access is returned on the next
transfer. If the next access to be done is NOT an AP access, then the DP-RDBUFF register must be read to
obtain the result.
The READOK flag of the DP-CTRL/STAT register is updated on every AP read access or RDBUFF read
request to know if the AP read access was successful.
The SW-DP implements a write buffer (for both DP or AP writes), that enables it to accept a write operation
even when other transactions are still outstanding. If the write buffer is full, the target acknowledge response
is “WAIT”. With the exception of IDCODE read or CTRL/STAT read or ABORT write which are accepted
even if the write buffer is full.
Because of the asynchronous clock domains SWCLK and HCLK, two extra SWCLK cycles are needed after
a write transaction (after the parity bit) to make the write effective internally. These cycles should be applied
while driving the line low (IDLE state),This is particularly important when writing the CTRL/STAT for a
power-up request. If the next transaction (requiring a power-up) occurs immediately, it will fail.
Core debug is accessed through the core debug registers. The Debug Fault Status Register (DFSR,
address is 0xE000ED30, each bit cleared by writing 1) provides status information about the state of the
debug event and the reason of core halted. When core is halted by debug event the corresponding bit will be
set.
Address Register Description
Debug Halting Control and Status Register(DHCSR) User can
0xE000EDF0 DHCSR enable core debug halt and step the processor by configuring
DHCSR register
Debug Core Register Selector Register(DCRSR) User can
read/write core register by configuring DCRSR when core is
0xE000EDF4 DCRSR
halted. This selects the processor register to transfer data to or
from.
Debug Core Register Data Register(DCRDR) This holds data
0xE000EDF8 DCRDR for reading and writing registers to and from the core register
selected by the DCRSR (Selector) register.
Debug Exception and Monitor Control Register(DEMCR) is
0xE000EDFC DEMCR used for interrupt control in debug mode or DWT module
enabled.
The MCU debug component helps the debugger provide support for:
Low-power modes
Clock control for timers, watchdog during a breakpoint
To enter low-power mode, the instruction WFI or WFE must be executed. The MCU implements several
low-power modes which can either deactivate the CPU clock or reduce the power of the CPU. The core
does not allow FCLK or HCLK to be turned off during a debug session. As these are required for the
debugger connection, during a debug, they must remain active. The MCU integrates special means to allow
the user to debug software in low-power modes.
For this, the debugger host must first set some debug configuration registers to change the low-power mode
behavior:
In Sleep mode: the DBG_SLEEP bit must be previously set by the debugger. This provides the same
frequency as FCLK (configured by software) for HCLK in Sleep mode.
In Stop mode, the DBG_STOP bit must be previously set by the debugger. This enables the internal RC
oscillator clock to feed FCLK and HCLK in Stop mode.
During a breakpoint, it is necessary to choose how the counter of timers and watchdog should behave:
They can continue to count inside a breakpoint. This is usually required when a PWM is controlling a
motor, for example.
They can stop to count inside a breakpoint. This is required for watchdog purposes.
This register allows the configuration of the MCU under DEBUG. This concerns:
Low-power mode support
Timer and Watchdog support
DBGMCU_CR register is mapped to AHB bus, Base address: 0x40013404.
It is asynchronously reset by the PORESET (and not the system reset). It can be written by the debugger
under system reset.
If the debugger host does not support these features, it is still possible for the user software to write to these
registers.
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0
DWTTRAP
VCATCH
HALTED
EXTERNAL
BKPT
DFSR
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
0x30
Reset x x x x x x x x x x x x x x x x x x x x x x x x x x x 0 0 0 0 0
DBGKEY[15:0]
C_MASKINTS
ST_RETIRE_ST
C_DEBUGEN
S_RESET_ST
S_LOCKUP
C_STEP
C_HALT
S_READY
S_SLEEP
S_HALT
0xF0 DHCSR
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
Reset x x x x x x 1 0 x x x x 0 0 0 0 x x x x x x x x x x x x 0 0 0 0
REGWnR
REGSEL
[4:0]
DCRSR
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
0xF4
Reset x x x x x x x x x x x x x x x 0 x x x x x x x x x x x 0 0 0 0 0
DCRDR DBGTMP[31:0]
0xF8
Reset 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
VC_CORERESET
VC_HARDERR
DWTENA
DEMCR
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
0xFC
Reset x x x x x x x 0 x x x x x x x x x x x x x 0 x x x x x x x x x 0
30.3.1. DFSR
30.3.2. DHCSR
2 C_STEP Single step control bit, 662ffective only when debug is enabled
1 C_HALT Core halted control bit, 662ffective only when debug is enabled
0 C_DEBUGEN Debug enable bit, debug is enabled when C_DEBUGEN is set
Note : DHCSR[31 :16 must be ] 0xA05F in write operation ;
DHCSR[31 :26] are reserved and DHCSR[25 :16] are correspoding status flag in read operation.
30.3.3. DCRSR
30.3.4. DCRDR
30.3.5. DEMCR
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
Offset Register
9
8
7
6
5
4
3
2
1
0
IDCODE REV_ID[15:0] DEV_ID[11:0]
_
_
_
_
0x00
Reset 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 0 0 0 1 0 0 1 0 0 0
DBG_STANDBY
DBG_STOP
DBGMCU_CR
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
0x04
Reset x x x x x x x x x x x x x x x x x x x x x x x x x x x x x 0 0 x
DBG_I2C1_SMBUS_TIMEOUT
DBG_WWDG_STOP
DBG_TIM14_STOP
DBG_IWDG_STOP
DBG_TIM7_STOP
DBG_TIM6_STOP
DBG_TIM3_STOP
DBG_RTC_STOP
DBGMCU_APB1
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
0x08 _FZ
Reset x x x x x x x x x x 0 x x x x x x x x 0 0 0 x 0 x x 0 0 x x 0 x
DBG_TIM17_STOP
DBG_TIM16_STOP
DBG_TIM15_STOP
DBG_TIM1_STOP
DBGMCU_APB2
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
0x0C _FZ _
Reset x x x x x x x x x x x x x 0 0 0 x x x x 0 x x x x x x x x x x x
30.4.1. DBGMCU_IDCODE
23:16 PARTNO[11:4]
type RO RO RO RO RO RO RO RO
15:8 PARTNO[3:0] DESIGNER[11:8]
type RO RO RO RO RO RO RO RO
7:0 DESIGNER[7:1] —
type RO RO RO RO RO RO RO RO-1
31.1. Overview
The device electronic signature is stored in the System memory area of the Flash memory module, and can
be read using the debug interface or by the CPU. It contains factory-programmed identification and
calibration data that allow the user firmware or other external devices to automatically match to the
characteristics of the FT32F0xxx8 microcontroller.
15:8 UID[47:40]
type RO RO RO RO RO RO RO RO
7:0 UID[39:32]
type RO RO RO RO RO RO RO RO
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0
Flash_size _ Flash_size[15:0]
_
_
_
_
_
_
_
_
_
_
_
_
_
_
_
0x00
Reset x x x x x x x x x x x x x x x x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
31.3.1. FLASH_SIZE
Revision History
Date Revision Description
2022-06-23 1.0 Preliminary version
2022-10-20 1.1 Correct some clerical errors
Add introduction page
Add note for turning off the HSI14 clock in the low-power mode
Correct the description of HSEDRVEN in RCC_HSECFG register
Delete the description of FLASH factory configuration
Modify the operational amplifier description
2023-01-16 1.2 Add memory address information of TS_CAL1、TS_CAL2 and VREFINT_CAL
2024-03-22 1.3 Correct some clerical errors
Update Table 3-3 Access status versus protection level and execution modes
Correct the description of HSEDRVEN in RCC_HSECFG register
Add capacitive sensing GPIOs information for ≥VerD chips