Um0306 PDF
Um0306 PDF
Reference manual
STM32F101xx and STM32F103xx
advanced ARM-based 32-bit MCUs
Introduction
This Reference Manual targets application developers. It provides complete information on
how to use the STM32F101xx and ST32M103xx microcontroller memory and peripherals.
The STM32F101xx and ST32M103xx will be referred to as STM32F10x throughout the
document.
The STM32F10x is a family of microcontrollers with different memory sizes, packages and
peripherals.
For ordering information, mechanical and electrical device characteristics please refer to the
STM32F101xx and ST32M103xx datasheets.
For information on programming, erasing and protection of the internal Flash memory
please refer to the STM32F10x Flash Programming Manual.
For information on the ARM Cortex-M3 core, please refer to the Cortex-M3TM Technical
Reference Manual.
Related documents
Available from www.arm.com:
■ Cortex-M3TM Technical Reference Manual
www.BDTIC.com/ST
Contents UM0306
Contents
1 Documentation conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.1 List of abbreviations for registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2/519
www.BDTIC.com/ST
UM0306 Contents
3/519
www.BDTIC.com/ST
Contents UM0306
4/519
www.BDTIC.com/ST
UM0306 Contents
5/519
www.BDTIC.com/ST
Contents UM0306
6/519
www.BDTIC.com/ST
UM0306 Contents
7/519
www.BDTIC.com/ST
Contents UM0306
8/519
www.BDTIC.com/ST
UM0306 Contents
9/519
www.BDTIC.com/ST
Contents UM0306
10/519
www.BDTIC.com/ST
UM0306 Contents
11/519
www.BDTIC.com/ST
Contents UM0306
12/519
www.BDTIC.com/ST
UM0306 Contents
13/519
www.BDTIC.com/ST
Contents UM0306
14/519
www.BDTIC.com/ST
UM0306 Contents
15/519
www.BDTIC.com/ST
Contents UM0306
16/519
www.BDTIC.com/ST
UM0306 List of tables
List of tables
17/519
www.BDTIC.com/ST
List of tables UM0306
18/519
www.BDTIC.com/ST
UM0306 List of figures
List of figures
19/519
www.BDTIC.com/ST
List of figures UM0306
20/519
www.BDTIC.com/ST
UM0306 List of figures
21/519
www.BDTIC.com/ST
List of figures UM0306
22/519
www.BDTIC.com/ST
UM0306 Documentation conventions
1 Documentation conventions
write-only (w) Software can only write to this bit. Reading the bit returns the reset value.
read-clear (rc) The software can only read or clear this bit.
Software can read as well as clear this bit by writing 1. Writing ‘0’ has no
read/clear (rc_w1)
effect on the bit value.
Software can read as well as clear this bit by writing 0. Writing ‘1’ has no
read/clear (rc_w0)
effect on the bit value.
Software can read as well as set this bit. Writing ‘0’ has no effect on the bit
read/set (rs)
value.
toggle (t) The software can only toggle this bit by writing ‘1’. Writing ‘0’ has no effect.
23/519
www.BDTIC.com/ST
Memory and bus architecture UM0306
SRAM
DMA Request
ICode bus
This bus connects the Instruction bus of the Cortex-M3 core to the Flash memory instruction
interface. Prefetching is performed on this bus.
DCode bus
This bus connects the DCode bus (literal load and debug access) of the Cortex-M3 core to
the Flash memory Data interface.
System bus
This bus connects the system bus of the Cortex-M3 core (peripherals bus) to a BusMatrix
which manages the arbitration between the core and the DMA.
24/519
www.BDTIC.com/ST
UM0306 Memory and bus architecture
DMA bus
This bus connects the AHB master interface of the DMA to the BusMatrix which manages
the access of CPU DCode 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. The BusMatrix is composed of
three masters (CPU DCode, System bus and DMA bus) and three slaves (FLITF, SRAM,
and AHB2APB bridges).
AHB peripherals are connected on system bus through a BusMatrix to allow DMA access.
25/519
www.BDTIC.com/ST
Memory and bus architecture UM0306
reserved 1K
6 0x4001 3C00
USART1 1K
0x4001 3800
0xC000 0000 reserved 1K
0x4001 3400
SPI1 1K
0x4001 3000
TIM1 1K
0x4001 2C00
5 ADC2 1K
0x4001 2800
ADC1 1K
0x4001 2400
0xA000 0000
reserved 2K
0x4001 1C00
Port E 1K
4 0x1FFF FFFF 0x4001 1800
Port D 1K
reserved
0x1FFF F9FF 0x4001 1400
Port C 1K
0x8000 0000
0x4001 1000
OPTION BYTES
Port B 1K
0x1FFF F800 0x4001 0C00
Port A 1K
0x4001 0800
EXTI 1K
3 SYSTEMMEMORY 0x4001 0400
AFIO 1K
0x4001 0000
0x1FFF F000
0x6000 0000 reserved 35K
0x4000 7400
PWR 1K
0x4000 7000
2 BKP 1K
0x4000 6C00
reserved 1K
reserved 0x4000 6800
0x4000 0000 PERIPHERALS bxCAN 1K
0x4000 6400
USB SRAM 256 x 16-bit 1K
0x4000 6000
USB Registers 1K
0x4000 5C00
1 I2C2 1K
0x4000 5800
I2C1 1K
SRAM 0x4000 5400
0x2000 0000
0x0801 FFFF
reserved 2K
0x4000 4C00
USART3 1K
0 FLASH
0x4000 4800
USART2 1K
0x4000 4400
0x0000 0000
CODE 0x0800 0000 reserved 2K
0x4000 3C00
SPI2 1K
0x4000 3800
reserved 1K
0x4000 3400
IWDG 1K
0x4000 3000
WWDG 1K
Reserved 0x4000 2C00
RTC 1K
0x4000 2800
reserved 7K
0x4000 0C00
TIM4 1K
0x4000 0800
TIM3 1K
0x4000 0400
TIM2 1K
0x4000 0000
26/519
www.BDTIC.com/ST
UM0306 Memory and bus architecture
27/519
www.BDTIC.com/ST
Memory and bus architecture UM0306
28/519
www.BDTIC.com/ST
UM0306 Memory and bus architecture
29/519
www.BDTIC.com/ST
Memory and bus architecture UM0306
Note: For further information on the Flash memory registers, please refer to the STM32F10x Flash
Programming manual.
30/519
www.BDTIC.com/ST
UM0306 Memory and bus architecture
31/519
www.BDTIC.com/ST
Memory and bus architecture UM0306
This aliases the physical memory associated with each boot mode to Block 000 (boot
memory). The values on the BOOT pins are latched on the 4th rising edge of SYSCLK after
a Reset. It is up to the user to set the BOOT1 and BOOT0 pins after Reset to select the
required boot mode.
The BOOT pins are also re-sampled when exiting from STANDBY mode. Consequently they
must be kept in the required Boot mode configuration in STANDBY mode.
Even when aliased in the boot memory space, the related memory (Flash memory or
SRAM) is still accessible at its original memory space.
After this startup delay has elapsed, the CPU starts code execution from the boot memory,
located at the bottom of the memory address space starting from 0x0000_0000h.
32/519
www.BDTIC.com/ST
UM0306 Power control (PWR)
VDDA domain
(from 0V VREF-
up to VDDA) VREF+ A/D converter
Temp. sensor
VDDA Reset block
(VDD) PLL
VSSA
I/O Ring
VSS Core
(3.3V) STANDBY circuitry Memories
VDD (Wake-up logic, digital
IWDG) peripherals
Voltage Regulator
Backup domain
LSE crystal 32K osc
(VDD) VBAT
BKP registers
RCC BDCR register
RTC
33/519
www.BDTIC.com/ST
Power control (PWR) UM0306
34/519
www.BDTIC.com/ST
UM0306 Power control (PWR)
POR
40 mV
hysteresis
PDR
Temporization
tRSTTEMPO
Reset
100 mV
PVD Threshold hysteresis
PVD Output
35/519
www.BDTIC.com/ST
Power control (PWR) UM0306
36/519
www.BDTIC.com/ST
UM0306 Power control (PWR)
37/519
www.BDTIC.com/ST
Power control (PWR) UM0306
38/519
www.BDTIC.com/ST
UM0306 Power control (PWR)
Note: To enter STOP mode, all EXTI Line pending bits (in Pending register
(EXTI_PR)) and RTC Alarm flag must be reset. Otherwise, the STOP mode
entry procedure is ignored and program execution continues.
If WFI was used for entry:
Any EXTI Line configured in Interrupt mode (the corresponding EXTI
Interrupt vector must be enabled in the NVIC). Refer to Table 27: Vector
Mode Exit table on page 98
If WFE was used for entry:
Any EXTI Line configured in event mode. Refer to Section 6.2.3: Wake-
up event management on page 102
Wake-up latency None
39/519
www.BDTIC.com/ST
Power control (PWR) UM0306
Debug mode
The debug connection is lost if the application puts the MCU in STOP or STANDBY mode
while the debug features are used. This is due to the fact that the Cortex-M3 core is no
longer clocked.
However, the STM32F10x/ST32M103xx integrate special capabilities that allow the user to
perform software debugging in low-power modes. For more details, refer to section
Section 20.15.1: Debug support for low-power modes.
40/519
www.BDTIC.com/ST
UM0306 Power control (PWR)
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw rw rw rw rw rc_w1 rc_w1 rw rw
41/519
www.BDTIC.com/ST
Power control (PWR) UM0306
42/519
www.BDTIC.com/ST
UM0306 Power control (PWR)
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw r r r
43/519
www.BDTIC.com/ST
Power control (PWR) UM0306
9
8
7
6
5
4
3
2
1
0
CWUF
PDDS
PVDE
CSBF
LPDS
DBP
PWR_CR PLS[2:0]
000h Reserved
Reset Value 0 0 0 0 0 0 0 0 0
EWUP
PVDO
WUF
SBF
PWR_CSR
004h Reserved Reserved
Reset Value 0 0 0 0
44/519
www.BDTIC.com/ST
UM0306 Reset and clock control (RCC)
4.1 Reset
There are three types of reset, defined as system Reset, power Reset and backup domain
Reset.
Software Reset
The SYSRESETREQ bit in Cortex-M3 Application Interrupt and Reset Control Register
must be set to force a software Reset on the device. Refer to the Cortex-M3 technical
reference manual for more details.
45/519
www.BDTIC.com/ST
Reset and clock control (RCC) UM0306
VDD
RON
EXTERNAL SYSTEM NRESET
RESET Filter
NRST
WWDG Reset
PULSE
IWDG Reset
GENERATOR POR/PDR Reset
(min 20µs) Software Reset
Low-power management Reset
The Backup domain has two specific resets that affect only the Backup domain (see
Figure 3)
4.2 Clocks
Three different clock sources can be used to drive the system clock (SYSCLK):
● HSI oscillator clock
● HSE oscillator clock
● PLL clock
The devices have the following two secondary clock sources:
● 32 kHz Low Speed Internal RC (LSI RC) which drives the Independent Watchdog and
optionally the RTC used for Auto Wake-up from STOP/STANDBY mode.
● 32.768 kHz Low Speed External crystal (LSE crystal) which optionally drives the Real-
Time Clock (RTCCLK)
46/519
www.BDTIC.com/ST
UM0306 Reset and clock control (RCC)
Each clock source can be switched on or off independently when it is not used, to optimize
power consumption.
8 MHz
HSI RC HSI USBCLK
USB 48 MHz
Prescaler to USB interface
/2 /1, 1.5
HCLK
72 MHz max to AHB bus, core,
Clock memory and DMA
Enable (3 bits)
/8 to Cortex System timer
PLLSRC SW
PLLMUL FCLK Cortex
HSI free running clock
..., x16 SYSCLK AHB APB1
36 MHz max PCLK1
x2, x3, x4 PLLCLK 72 MHz
Prescaler Prescaler
to APB1
PLL max /1, 2 ..512 /1, 2, 4, 8, 16 peripherals
Peripheral Clock
HSE
Enable (13 bits)
to TIM2, 3
TIM2, 3, 4 and 4
CSS x1, 2 Multiplier TIMXCLK
Peripheral Clock
Enable (3 bits)
PLLXTPRE APB2
72 MHz max PCLK2
Prescaler
OSC_OUT to APB2
4-16 MHz /1, 2, 4, 8, 16 peripherals
Peripheral Clock
HSE OSC Enable (11 bits)
OSC_IN /2
TIM1 Timer to TIM1
x1, 2 Multiplier TIM1CLK
Peripheral Clock
/128 Enable (1 bit)
ADC to ADC
OSC32_IN to RTC Prescaler
LSE OSC LSE ADCCLK
32.768 kHz RTCCLK /2, 4, 6, 8
OS32_OUT
RTCSEL[1:0]
Several prescalers allow the configuration of the AHB frequency, the high speed APB
(APB2) and the low speed APB (APB1) domains. The AHB and the APB2 domains
maximum frequency is 72 MHz. The APB1 domains maximum allowed frequency is 36
MHz. The RCC feeds the Cortex System Timer (SysTick) external clock with the AHB clock
divided by 8. The SysTick can work either with this clock or with the Cortex clock (AHB),
configurable in the SysTick Control and Status Register. The ADCs are clocked by the clock
of the High Speed domain (APB2) divided by 2, 4, 6 or 8.
47/519
www.BDTIC.com/ST
Reset and clock control (RCC) UM0306
The timer clock frequencies are twice the frequency of the APB domain which they are
connected to. Nevertheless, if the APB prescaler is 1, the clock frequency of the timer is the
same as the frequency of the APB domain which it is connected to.
FCLK acts as Cortex-M3 free running clock. For more details refer to the ARM Cortex-M3
Technical Reference Manual.
OSC_OUT
(HiZ)
EXTERNAL
SOURCE
Crystal/Ceramic Resonators
OSC_IN OSC_OUT
CL1 CL2
LOAD
CAPACITORS
48/519
www.BDTIC.com/ST
UM0306 Reset and clock control (RCC)
Calibration
RC oscillator frequencies can vary from one chip to another due to manufacturing process
variations, this is why each device is factory calibrated by ST for 1% accuracy at TA=25°C.
After Reset, the factory calibration value is loaded in the HSICAL[7:0] bits in the Clock
control register (RCC_CR).
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 Clock control register (RCC_CR).
The HSIRDY flag in the Clock control register (RCC_CR) 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 Clock control register
(RCC_CR).
The HSI signal can also be used as a backup source (Auxiliary clock) if the HSE crystal
oscillator fails. Refer to Section 4.2.7: Clock security system (CSS) on page 51.
4.2.3 PLL
The internal PLL can be used to multiply the HSI RC output or HSE crystal output clock
frequency. Refer to Figure 7 and Clock control register (RCC_CR).
The PLL configuration (selection of HSI oscillator divided by 2 or HSE oscillator for PLL
input clock, and multiplication factor) must be done before enabling the PLL. Once the PLL
enabled, these parameters cannot be changed.
An interrupt can be generated when the PLL is ready if enabled in the Clock interrupt
register (RCC_CIR).
49/519
www.BDTIC.com/ST
Reset and clock control (RCC) UM0306
If the USB interface is used in the application, the PLL must be programmed to output 48 or
72 MHz. This is needed to provide a 48 MHz USBCLK.
50/519
www.BDTIC.com/ST
UM0306 Reset and clock control (RCC)
51/519
www.BDTIC.com/ST
Reset and clock control (RCC) UM0306
programmed in alternate function mode. One of 4 clock signals can be selected as the MCO
clock.
● SYSCLK
● HSI
● HSE
● PLL clock divided by 2
The selection is controlled by the MCO[2:0] bits of the Clock configuration register
(RCC_CFGR).
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
r rw rw rw r rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
HSI
HSI CAL[7:0] HSI TRIM[4:0] Res. HSION
RDY
r r r r r r r r rw rw rw rw rw r rw
52/519
www.BDTIC.com/ST
UM0306 Reset and clock control (RCC)
53/519
www.BDTIC.com/ST
Reset and clock control (RCC) UM0306
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw rw rw rw rw rw rw rw rw rw rw rw r r rw rw
54/519
www.BDTIC.com/ST
UM0306 Reset and clock control (RCC)
55/519
www.BDTIC.com/ST
Reset and clock control (RCC) UM0306
56/519
www.BDTIC.com/ST
UM0306 Reset and clock control (RCC)
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
w w w w w w
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
PLL HSE HSI LSE LSI PLL HSE HSI LSE LSI
Reserved CSSF Reserved
RDYIE RDYIE RDYIE RDYIE RDYIE RDYF RDYF RDYF RDYF RDYF
rw rw rw rw rw r r r r r r
57/519
www.BDTIC.com/ST
Reset and clock control (RCC) UM0306
58/519
www.BDTIC.com/ST
UM0306 Reset and clock control (RCC)
59/519
www.BDTIC.com/ST
Reset and clock control (RCC) UM0306
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
USART
SPI1 TIM1 ADC2 ADC1 IOPE IOPD IOPC IOPB IOPA AFIO
Res. 1 Res. Reserved Res.
RST RST RST RST RST RST RST RST RST RST
RST
rw rw rw rw rw rw rw rw rw rw rw
60/519
www.BDTIC.com/ST
UM0306 Reset and clock control (RCC)
61/519
www.BDTIC.com/ST
Reset and clock control (RCC) UM0306
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
USART USART
PWR BKP CAN USB I2C2 I2C1
Reserved Res. Res. Reserved 3 2 Res.
RST RST RST RST RST RST
RST RST
rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw rw rw rw rw
62/519
www.BDTIC.com/ST
UM0306 Reset and clock control (RCC)
63/519
www.BDTIC.com/ST
Reset and clock control (RCC) UM0306
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw rw rw
64/519
www.BDTIC.com/ST
UM0306 Reset and clock control (RCC)
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
USAR
SPI1 TIM1 ADC2 ADC1 IOPE IOPD IOPC IOPB IOPA AFIO
Res; T1 Res; Reserved Res.
EN EN EN EN EN EN EN EN EN EN
EN
rw rw rw rw rw rw rw rw rw rw rw
65/519
www.BDTIC.com/ST
Reset and clock control (RCC) UM0306
66/519
www.BDTIC.com/ST
UM0306 Reset and clock control (RCC)
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw rw rw rw rw
67/519
www.BDTIC.com/ST
Reset and clock control (RCC) UM0306
68/519
www.BDTIC.com/ST
UM0306 Reset and clock control (RCC)
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved BDRST
rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw rw rw rw r rw
69/519
www.BDTIC.com/ST
Reset and clock control (RCC) UM0306
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
LSI
Reserved LSION
RDY
r rw
70/519
www.BDTIC.com/ST
UM0306 Reset and clock control (RCC)
71/519
www.BDTIC.com/ST
4.4
024h
020h
008h
018h
014h
010h
004h
000h
01Ch
00Ch
72/519
Offset
Table 10.
RCC_CR
RCC_CIR
RCC_CSR
Register
Reset Value
Reset Value
Reset Value
Reset Value
Reset Value
Reset Value
Reset Value
Reset Value
Reset Value
Reset Value
RCC_CFGR
RCC_BDCR
RCC_AHBENR
RCC_APB1ENR
RCC_APB2ENR
RCC_APB2RSTR
RCC_APB1RSTR
0
LPWRSTF 31
0
WWDGRSTF 30
0
IWDGRSTF
Reserved
Reserved
29
0
0
0
SFTRSTF PWREN PWRRST
Reserved
28
Reserved
1
0
0
Reset and clock control (RCC)
Reserved
1
0
PINRSTF Reserved Reserved 26
0
0
0
0
0
0
0
24
0
0
0
USBEN USBRST CSSC Reserved
Reserved
23
0
0
0
Reserved
Reserved
Reserved
22
0
0
0
I2C1EN I2C1RST 21
Reserved
0
0
PLLRDYC 20
Reserved Reserved
RCC - register map and reset values
0
0
0
HSERDYC CSSON 19
0
0
0
0
0
18
0
0
0
0
0
Reserved
17
0
0
0
0
0
0
0
RTCEN 15
[1:0]
PRE
ADC
0
0
0
0
0
0
Reserved Reserved 13
Reserved Reserved
0
0
0
0
0
Reserved
PPRE2
0
0
0
0
0
0
0
Reserved
11
HSICAL[7:0]
0
0
0
0
0
0
0
0
0
0
0
SEL
[1:0]
RTC
LSIRDYIE
0
0
0
0
Reserved Reserved
8
0
0
1
CSSF 7
www.BDTIC.com/ST
0
0
0
0
IOPEEN IOPERST 6
Reserved
Reserved
Reserved
0
0
0
0
IOPDEN IOPDRST 5
HPRE[3:0]
0
1
0
0
0
0
Reserved
4
HSITRIM[4:0]
0
0
0
0
0
0
0
0
0
1
0
0
0
SWS
0
0
0
0
0
1
LSIRDYF
0
0
0
0
0
0
0
0
1
0
UM0306 General purpose and alternate function I/O (GPIO and AFIO)
73/519
www.BDTIC.com/ST
General purpose and alternate function I/O (GPIO and AFIO) UM0306
Analog Input
TO ON-CHIP VDD_IO
PERIPHERAL Alternate Function Input
00 Reserved
01 Max. output speed 10 MHz
10 Max. output speed 2 MHz
11 Max. output speed 50 MHz
74/519
www.BDTIC.com/ST
UM0306 General purpose and alternate function I/O (GPIO and AFIO)
75/519
www.BDTIC.com/ST
General purpose and alternate function I/O (GPIO and AFIO) UM0306
VDD_IO
INPUT DATA REGISTER
ON/OFF PULL
ON
READ UP
VDD_IO
BIT SET/RESET REGISTERS
DIODE
WRITE VSS
INPUT DRIVER I/O PIN
OUTPUT DRIVER
PROTECTION
DIODE
VSS
READ/WRITE
76/519
www.BDTIC.com/ST
UM0306 General purpose and alternate function I/O (GPIO and AFIO)
ON
READ
VDD_IO
BIT SET/RESET REGISTERS
TTL SCHMITT
TRIGGER PROTECTION
DIODE
OUTPUT DATA REGISTER
WRITE
INPUT DRIVER I/O PIN
77/519
www.BDTIC.com/ST
General purpose and alternate function I/O (GPIO and AFIO) UM0306
A set of Alternate Function I/O registers allow you to remap some alternate functions to
different pins. Refer to
READ VDD_IO
BIT SET/RESET REGISTERS
TTL SCHMITT
PROTECTION
TRIGGER
DIODE
OUTPUT DATA REGISTER
78/519
www.BDTIC.com/ST
UM0306 General purpose and alternate function I/O (GPIO and AFIO)
Analog Input
TO ON-CHIP
PERIPHERAL
TTL SCHMITT
PROTECTION
OUTPUT DATA REGISTER TRIGGER
DIODE
WRITE
INPUT DRIVER I/O PIN
PROTECTION
DIODE
VSS
READ/WRITE
FROM ON-CHIP
PERIPHERAL
79/519
www.BDTIC.com/ST
General purpose and alternate function I/O (GPIO and AFIO) UM0306
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
80/519
www.BDTIC.com/ST
UM0306 General purpose and alternate function I/O (GPIO and AFIO)
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
81/519
www.BDTIC.com/ST
General purpose and alternate function I/O (GPIO and AFIO) UM0306
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
IDR15 IDR14 IDR13 IDR12 IDR11 IDR10 IDR9 IDR8 IDR7 IDR6 IDR5 IDR4 IDR3 IDR2 IDR1 IDR0
r r r r r r r r r r r r r r r r
82/519
www.BDTIC.com/ST
UM0306 General purpose and alternate function I/O (GPIO and AFIO)
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
ODR15 ODR14 ODR13 ODR12 ODR11 ODR10 ODR9 ODR8 ODR7 ODR6 ODR5 ODR4 ODR3 ODR2 ODR1 ODR0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
83/519
www.BDTIC.com/ST
General purpose and alternate function I/O (GPIO and AFIO) UM0306
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
BR15 BR14 BR13 BR12 BR11 BR10 BR9 BR8 BR7 BR6 BR5 BR4 BR3 BR2 BR1 BR0
w w w w w w w w w w w w w w w w
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
BS15 BS14 BS13 BS12 BS11 BS10 BS9 BS8 BS7 BS6 BS5 BS4 BS3 BS2 BS1 BS0
w w w w w w w w w w w w w w w w
84/519
www.BDTIC.com/ST
UM0306 General purpose and alternate function I/O (GPIO and AFIO)
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
BR15 BR14 BR13 BR12 BR11 BR10 BR9 BR8 BR7 BR6 BR5 BR4 BR3 BR2 BR1 BR0
w w w w w w w w w w w w w w w w
85/519
www.BDTIC.com/ST
General purpose and alternate function I/O (GPIO and AFIO) UM0306
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved LCKK
rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
LCK15 LCK14 LCK13 LCK12 LCK11 LCK10 LCK9 LCK8 LCK7 LCK6 LCK5 LCK4 LCK3 LCK2 LCK1 LCK0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
86/519
www.BDTIC.com/ST
UM0306 General purpose and alternate function I/O (GPIO and AFIO)
To optimize the number of free GPIOs during debugging, this mapping can be configured in
different ways by programming the SWJ_CFG[1:0] bits in the AF remap and debug I/O
configuration register (AFIO_MAPR). Refer to Table 15
87/519
www.BDTIC.com/ST
General purpose and alternate function I/O (GPIO and AFIO) UM0306
88/519
www.BDTIC.com/ST
UM0306 General purpose and alternate function I/O (GPIO and AFIO)
89/519
www.BDTIC.com/ST
General purpose and alternate function I/O (GPIO and AFIO) UM0306
90/519
www.BDTIC.com/ST
UM0306 General purpose and alternate function I/O (GPIO and AFIO)
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw rw rw rw rw rw rw rw
Bits 31:18
EVOE Event Output Enable
Bit 7 Set and cleared by software. When set the EVENTOUT Cortex output is
connected to the I/O selected by the PORT[2:0] and PIN[3:0] bits.
PORT[2:0]: Port selection
Set and cleared by software. Select the port used to output the Cortex
EVENTOUT signal.
000: PA selected
Bits 6:4
001: PB selected
010: PC selected
011: PD selected
100: PE selected
PIN[3:0] Pin selection (x = A .. E)
Set and cleared by software. Select the pin used to output the Cortex
EVENTOUT signal.
0000: Px0 selected
Bits 3:0 0001: Px1 selected
0010: Px2 selected
0011: Px3 selected
...
1111: Px15 selected
91/519
www.BDTIC.com/ST
General purpose and alternate function I/O (GPIO and AFIO) UM0306
92/519
www.BDTIC.com/ST
UM0306 General purpose and alternate function I/O (GPIO and AFIO)
93/519
www.BDTIC.com/ST
General purpose and alternate function I/O (GPIO and AFIO) UM0306
94/519
www.BDTIC.com/ST
UM0306 General purpose and alternate function I/O (GPIO and AFIO)
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
95/519
www.BDTIC.com/ST
General purpose and alternate function I/O (GPIO and AFIO) UM0306
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
96/519
www.BDTIC.com/ST
UM0306 General purpose and alternate function I/O (GPIO and AFIO)
9
8
7
6
5
4
3
2
1
0
CNF MODE CNF MODE CNF MODE CNF MODE CNF MODE CNF MODE CNF MODE CNF MODE
GPIOx_CRL 7 7 6 6 5 5 4 4 3 3 2 2 1 1 0 0
00h
[1:0] [1:0] [1:0] [1:0] [1:0] [1:0] [1:0] [1:0] [1:0] [1:0] [1:0] [1:0] [1:0] [1:0] [1:0] [1:0]
Reset Value 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0
CNF MODE CNF MODE CNF MODE CNF MODE CNF MODE CNF MODE CNF MODE CNF MODE
GPIOx_CRH 15 15 14 14 13 13 12 12 11 11 10 10 9 9 8 8
04h
[1:0] [1:0] [1:0] [1:0] [1:0] [1:0] [1:0] [1:0] [1:0] [1:0] [1:0] [1:0] [1:0] [1:0] [1:0] [1:0]
Reset Value 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0
GPIOx_IDR IDR[15:0]l
08h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
GPIOx_ODR ODR[15:0]
0Ch Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
GPIOx_BRR BR[15:0]
14h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
LCKK
GPIOx_LCKR LCK[15:0]
18h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Offset Register
9
8
7
6
5
4
3
2
1
0
EVOE
USART2_REMAP
USART1_REMAP
CAN_REMAP[1]
CAN_REMAP[0]
TIM4_REMPAP
PD01_REMAP
SPI1_REMAP
I2C1_REMAP
SWJ_CFG[2]
SWJ_CFG[1]
SWJ_CFG[0]
AFIO_MAPR
04h Reserved Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
97/519
www.BDTIC.com/ST
Interrupts and events UM0306
Priority
Type of
Acronym Description Address
priority
- - - Reserved 0x0000_0000
-3 fixed Reset Reset 0x0000_0004
Non maskable interrupt. The RCC
-2 fixed NMI Clock Security System (CSS) is linked 0x0000_0008
to the NMI vector.
-1 fixed HardFault All class of fault 0x0000_000C
0 settable MemManage Memory management 0x0000_0010
1 settable BusFault Pre-fetch fault, memory access fault 0x0000_0014
2 settable UsageFault Undefined instruction or illegal state 0x0000_0018
0x0000_001C -
- - - Reserved
0x0000_002B
System service call via SWI
3 settable SVCall 0x0000_002C
instruction
4 settable Debug Monitor Debug Monitor 0x0000_0030
- - - Reserved 0x0000_0034
5 settable PendSV Pendable request for system service 0x0000_0038
98/519
www.BDTIC.com/ST
UM0306 Interrupts and events
Position
Priority
Type of
Acronym Description Address
priority
99/519
www.BDTIC.com/ST
Interrupts and events UM0306
Position
Priority
Type of
Acronym Description Address
priority
100/519
www.BDTIC.com/ST
UM0306 Interrupts and events
19 19 19 19 19
To NVIC Interrupt 19 19 19 19 19
Controller
.
19
EVENT
MASK
REGISTER
101/519
www.BDTIC.com/ST
Interrupts and events UM0306
102/519
www.BDTIC.com/ST
UM0306 Interrupts and events
PA0
PB0
EXTI0
PC0
PD0
PE0
PA1
PB1
EXTI1
PC1
PD1
PE1
PA15
PB15
EXTI15
PC15
PD15
PE15
103/519
www.BDTIC.com/ST
Interrupts and events UM0306
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
MR15 MR14 MR13 MR12 MR11 MR10 MR9 MR8 MR7 MR6 MR5 MR4 MR3 MR2 MR1 MR0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
MR15 MR14 MR13 MR12 MR11 MR10 MR9 MR8 MR7 MR6 MR5 MR4 MR3 MR2 MR1 MR0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
104/519
www.BDTIC.com/ST
UM0306 Interrupts and events
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
TR15 TR14 TR13 TR12 TR11 TR10 TR9 TR8 TR7 TR6 TR5 TR4 TR3 TR2 TR1 TR0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Note: The external wake-up lines are edge triggered, no glitches must be generated on these
lines.
If a rising edge on external interrupt line occurs during writing of EXTI_RTSR register, the
pending bit will not be set.
Rising and Falling edge triggers can be set for the same interrupt line. In this configuration,
both generate a trigger condition.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
TR15 TR14 TR13 TR12 TR11 TR10 TR9 TR8 TR7 TR6 TR5 TR4 TR3 TR2 TR1 TR0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Note: The external wake-up lines are edge triggered, no glitches must be generated on these
lines.
If a falling edge on external interrupt line occurs during writing of EXTI_FTSR register, the
pending bit will not be set.
Rising and Falling edge triggers can be set for the same interrupt line. In this configuration,
both generate a trigger condition.
105/519
www.BDTIC.com/ST
Interrupts and events UM0306
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
SWIER SWIER SWIER SWIER SWIER SWIER SWIER SWIER SWIER SWIER SWIER SWIER SWIER SWIER SWIER SWIER
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
106/519
www.BDTIC.com/ST
UM0306 Interrupts and events
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
PR15 PR14 PR13 PR12 PR11 PR10 PR9 PR8 PR7 PR6 PR5 PR4 PR3 PR2 PR1 PR0
rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1 rc_w1
Table 28. External interrupt/event controller 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
Offset Register
9
8
7
6
5
4
3
2
1
0
EXTI_IMR MR[18:0]
00h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
EXTI_EMR MR[18:0]
04h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
EXTI_RTSR TR[18:0]
08h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
EXTI_FTSR TR[18:0]
0Ch Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
EXTI_SWIER SWIER[18:0]
10h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
EXTI_PR PR[18:0]
14h Reserved
Reset Value x x x x x x x x x x x x x x x x x x x
107/519
www.BDTIC.com/ST
DMA controller (DMA) UM0306
7.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 7 channels, each dedicated to managing memory access requests
from one or more peripherals. It has an arbiter for handling the priority between DMA
requests.
108/519
www.BDTIC.com/ST
UM0306 DMA controller (DMA)
SRAM
Ch.7 USART2
USART1 USART3 TIM2
SPI2 TIM3
SPI1 TIM4
Arbiter I2C1
ADC1 I2C2
TIM1
AHB Slave
DMA Request
109/519
www.BDTIC.com/ST
DMA controller (DMA) UM0306
7.3.2 Arbiter
The arbiter manages the channel requests based on their priority and launches the
peripheral/memory access sequences.
The priorities are managed in two stages:
● Software: each channel priority can be configured in the DMA_CCRx register. There
are four levels:
– Very high priority
– High priority
– Medium priority
– Low priority
● Hardware: if 2 requests have the same software priority level, the channel with the
lowest number will get priority versus the channel with the highest number. For
example, channel 2 gets priority over channel 4.
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 will be the one programmed in the DMA_CPARx/DMA_CMARx registers.
If the channel is configured in non-circular mode, no DMA requests are served after the end
of the transfer (i.e. once the number of data to be transferred reaches zero).
110/519
www.BDTIC.com/ST
UM0306 DMA controller (DMA)
1. Set the peripheral register address in the DMA_CPARx register. The data will be
moved from/ to this address to/ from the memory after the peripheral event.
2. Set the memory address in the DMA_CMARx register. The data will be written to or
read from this memory after the peripheral event.
3. Configure the total number of data to be transferred in the DMA_CNDTRx register.
After each peripheral event, this value will be decremented.
4. Configure the channel priority using the PL[1:0] bits in the DMA_CCRx register
5. Configure data transfer direction, circular mode, peripheral & memory incremented
mode, peripheral & memory data size, and interrupt after half and/or full transfer in the
DMA_CCRx register
6. Activate the channel by setting the ENABLE bit in the DMA_CCRx register.
As soon as the channel is enabled, it can serve any DMA request from the peripheral
connected on the channel.
Once half of the bytes are transferred, the Half-Transfer Flag (HTIF) is set and an interrupt is
generated if the Half-Transfer Interrupt Enable bit (HTIE) is set. At the end of the transfer,
the Transfer Complete Flag (TCIF) is set and an interrupt is generated if the Transfer
Complete Interrupt Enable bit (TCIE) is set.
Circular mode
Circular mode is available to handle circular buffers and continuous data flows (e.g. ADC
scan mode). This feature can be enabled using the CIRC bit in the DMA_CCRx register.
When circular mode is activated, the number of data to be transferred is automatically
reloaded with the initial value programmed during the channel configuration phase, and the
DMA requests continue to be served.
Memory-to-memory mode
The DMA channels can also work without being triggered by a request from a peripheral.
This mode is called Memory to Memory mode.
If the MEM2MEM bit in the DMA_CCRx register is set, then the channel initiates transfers as
soon as it is enabled by software by setting the Enable bit (EN) in the DMA_CCRx register.
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.
111/519
www.BDTIC.com/ST
DMA controller (DMA) UM0306
Channel 1 EN bit
USART3_TX
TIM1_CH1 HW REQUEST 2
CHANNEL 2
TIM2_UP
TIM3_CH3 SW TRIGGER (MEM2MEM bit)
SPI1_RX
Channel 2 EN bit
USART3_RX
TIM1_CH2 HW REQUEST 3
CHANNEL 3
TIM3_CH4
TIM3_UP
SW TRIGGER (MEM2MEM bit)
SPI1_TX
internal
USART1_TX Channel 3 EN bit
TIM1_CH4 DMA
TIM1_TRIG HW REQUEST 4 REQUEST
CHANNEL 4
TIM1_COM
TIM4_CH2
SW TRIGGER (MEM2MEM bit)
SPI2_RX
I2C2_TX
Channel 4 EN bit
USART1_RX
TIM1_UP
HW REQUEST 5
SPI2_TX CHANNEL 5
TIM2_CH1
TIM4_CH3 SW TRIGGER (MEM2MEM bit)
I2C2_RX
Channel 5 EN bit
USART2_RX
TIM1_CH3 HW REQUEST 6
CHANNEL 6
TIM3_CH1
TIM3_TRIG SW TRIGGER (MEM2MEM bit)
I2C1_TX
Channel 6 EN bit
USART2_TX HW REQUEST 7
TIM2_CH2 CHANNEL 7
TIM2_CH4 LOW PRIORITY
SW TRIGGER (MEM2MEM bit)
TIM4_UP
I2C1_RX
Channel 7 EN bit
112/519
www.BDTIC.com/ST
UM0306 DMA controller (DMA)
ADC ADC1
TIM1_CH4
TIM1 TIM1_CH1 TIM1_CH2 TIM1_TRIG TIM1_UP TIM1_CH3
TIM1_COM
TIM2_CH2
TIM2 TIM2_CH3 TIM2_UP TIM2_CH1
TIM2_CH4
TIM3_CH4 TIM3_CH1
TIM3 TIM3_CH3
TIM3_UP TIM3_TRIG
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved TEIF7 HTIF7 TCIF7 GIF7 TEIF6 HTIF6 TCIF6 GIF6 TEIF5 HTIF5 TCIF5 GIF5
r r r r r r r r r r r r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
TEIF4 HTIF4 TCIF4 GIF4 TEIF3 HTIF3 TCIF3 GIF3 TEIF2 HTIF2 TCIF2 GIF2 TEIF1 HTIF1 TCIF1 GIF1
r r r r r r r r r r r r r r r r
113/519
www.BDTIC.com/ST
DMA controller (DMA) UM0306
114/519
www.BDTIC.com/ST
UM0306 DMA controller (DMA)
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
CTEIF CHTIF CTCIF CGIF CTEIF CHTIF CTCIF CGIF CTEIF CHTIF CTCIF CGIF
Reserved
7 7 7 7 6 6 6 6 5 5 5 5
rw rw rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CTEIF CHTIF CTCIF CGIF CTEIF CHTIF CTCIF CGIF CTEIF CHTIF CTCIF CGIF CTEIF CHTIF CTCIF CGIF
4 4 4 4 3 3 3 3 2 2 2 2 1 1 1 1
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
115/519
www.BDTIC.com/ST
DMA controller (DMA) UM0306
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
MEM2
Res. PL[1:0] MSIZE[1:0] PSIZE[1:0] MINC PINC CIRC DIR TEIE HTIE TCIE EN
MEM
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
116/519
www.BDTIC.com/ST
UM0306 DMA controller (DMA)
117/519
www.BDTIC.com/ST
DMA controller (DMA) UM0306
9
8
7
6
5
4
3
2
1
0
HTIF7
HTIF6
HTIF5
HTIF4
HTIF3
HTIF2
HTIF1
TCIF7
TCIF6
TCIF5
TCIF4
TCIF3
TCIF2
TCIF1
TEIF7
TEIF6
TEIF5
TEIF4
TEIF3
TEIF2
TEIF1
GIF7
GIF6
GIF5
GIF4
GIF3
GIF2
GIF1
DMA_ISR
000h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
CHTIF7
CHTIF6
CHTIF5
CTCIF7
CHTIF4
CTCIF6
CHTIF3
CTCIF5
CHTIF2
CTCIF4
CHTIF1
CTCIF3
CTCIF2
CTCIF1
CTEIF7
CTEIF6
CTEIF5
CTEIF4
CTEIF3
CTEIF2
CTEIF1
CGIF7
CGIF6
CGIF5
CGIF4
CGIF3
CGIF2
CGIF1
DMA_IFCR
004h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
MEM2MEM
M
MINC
CIRC
PINC
HTIE
TCIE
PL PSIZE
TEIE
DIR
EN
DMA_CCR1 SIZE
008h Reserved [1:0] [1:0]
[1:0]
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CNDTR1 NDT[15:0]
00Ch Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CPAR1 PA[31:0]
010h
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CMAR1 MA[31:0]
014h
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
018h Reserved
MEM2MEM
M
MINC
CIRC
PINC
HTIE
TCIE
PL PSIZE
TEIE
DIR
EN
DMA_CCR2 SIZE
01Ch Reserved [1:0] [1:0]
[1:0]
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CNDTR2 NDT[15:0]
020h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
118/519
www.BDTIC.com/ST
UM0306 DMA controller (DMA)
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
DMA_CPAR2 PA[31:0]
024h
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CMAR2 MA[31:0]
028h
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
02Ch Reserved
MEM2MEM
M
MINC
CIRC
PINC
HTIE
TCIE
PL PSIZE
TEIE
DIR
EN
DMA_CCR3 SIZE
030h Reserved [1:0] [1:0]
[1:0]
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CNDTR3 NDT[15:0]
034h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CPAR3 PA[31:0]
038h
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CMAR3 MA[31:0]
03Ch
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
040h Reserved
MEM2MEM
M
MINC
CIRC
PINC
HTIE
TCIE
PL PSIZE
TEIE
DIR
EN
DMA_CCR4 SIZE
044h Reserved [1:0] [1:0]
[1:0]
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CNDTR4 NDT[15:0]
048h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CPAR4 PA[31:0]
04Ch
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CMAR4 MA[31:0]
050h
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
054h Reserved
MEM2MEM
M
MINC
CIRC
PINC
HTIE
TCIE
PL PSIZE
TEIE
DIR
EN
DMA_CCR5 SIZE
058h Reserved [1:0] [1:0]
[1:0]
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CNDTR5 NDT[15:0]
05Ch Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CPAR5 PA[31:0]
060h
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CMAR5 MA[31:0]
064h
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
068h Reserved
119/519
www.BDTIC.com/ST
DMA controller (DMA) UM0306
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
MEM2MEM
M
MINC
CIRC
PINC
HTIE
TCIE
PL PSIZE
TEIE
DIR
EN
DMA_CCR6 SIZE
06Ch Reserved [1:0] [1:0]
[1:0]
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CNDTR6 NDT[15:0]
070h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CPAR6 PA[31:0]
074h
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CMAR6 MA[31:0]
078h
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
07Ch Reserved
MEM2MEM
M
MINC
CIRC
PINC
HTIE
TCIE
PL PSIZE
TEIE
DIR
EN
DMA_CCR7 SIZE
080h Reserved [1:0] [1:0]
[1:0]
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CNDTR7 NDT[15:0]
084h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CPAR7 PA[31:0]
088h
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DMA_CMAR7 MA[31:0]
08Ch
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
090h Reserved
120/519
www.BDTIC.com/ST
UM0306 Real-Time Clock (RTC)
8.1 Introduction
The Real-Time Clock is an independent timer. The RTC provides a set of continuously-
running counters which can be used, with suitable software, to provide a clock-calendar
function. The counter values can be written to set the current time/date of the system.
8.3.1 Overview
The RTC consists of two main units (see Figure 18 on page 122). The first one (APB1
Interface) is used to interface with the APB1 bus. This unit also contains a set of 16-bit
registers accessible from the APB1 bus in read or write mode (for more information refer to
Section 8.4: RTC register description on page 125). The APB1 interface is clocked by the
APB1 bus clock in order to interface with the APB1 bus.
The other unit (RTC Core) consists of a chain of programmable counters made of two main
blocks. The first block is the RTC prescaler block, which generates the RTC time base
TR_CLK that can be programmed to have a period of up to 1 second. It includes a 20-bit
programmable divider (RTC Prescaler). Every TR_CLK period, the RTC generates an
interrupt (Second Interrupt) if it is enabled in the RTC_CR register. The second block is a
32-bit programmable counter that can be initialized to the current system time. The system
time is incremented at the TR_CLK rate and compared with a programmable date (stored in
the RTC_ALR register) in order to generate an alarm interrupt, if enabled in the RTC_CR
control register.
121/519
www.BDTIC.com/ST
Real-Time Clock (RTC) UM0306
APB1 bus
PCLK1
RTCCLK
Backup domain
RTC_CR
RTC_PRL
RTC_Second
SECF
Reload 32-bit programmable
counter SECIE
TR_CLK RTC_Overflow
RTC_DIV RTC_CNT OWF
rising OWIE
edge RTC_Alarm
RTC prescaler = ALRF
ALRIE
RTC_ALR
not powered in STANDBY
powered in STANDBY
NVIC INTERRUPT
powered in STANDBY CONTROLLER
122/519
www.BDTIC.com/ST
UM0306 Real-Time Clock (RTC)
123/519
www.BDTIC.com/ST
Real-Time Clock (RTC) UM0306
Configuration procedure:
1. Poll RTOFF, wait until its value goes to ‘1’
2. Set the CNF bit to enter configuration mode
3. Write to one or more RTC registers
4. Clear the CNF bit to exit configuration mode
5. Poll RTOFF, wait until its value goes to ‘1’ to check the end of the write operation.
The write operation only executes when the CNF bit is cleared; it takes at least three
RTCCLK cycles to complete.
Figure 19. RTC second and alarm waveform example with PR=0003, ALARM=00004
RTCCLK
RTC_PR 0002 0001 0000 0003 0002 0001 0000 0003 0002 0001 0000 0003 0002 0001 0000 0003 0002 0001 0000 0003 0002 0001 0000 0003
RTC_Second
RTC_ALARM
1 RTCCLK
124/519
www.BDTIC.com/ST
UM0306 Real-Time Clock (RTC)
RTC_PR 0002 0001 0000 0003 0002 0001 0000 0003 0002 0001 0000 0003 0002 0001 0000 0003 0002 0001 0000 0003 0002 0001 0000 0003
RTC_Second
RTC_Overflow
1 RTCCLK
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw rw rw
These bits are used to mask interrupt requests. Note that at reset all interrupts are disabled,
so it is possible to write to the RTC registers to ensure that no interrupt requests are pending
after initialization. It is not possible to write to the RTC_CRH register when the peripheral is
completing a previous write operation (flagged by RTOFF=0, see Section 8.3.4 on page
124).
The RTC functions are controlled by this control register. Some bits must be written using a
specific configuration procedure (see Configuration procedure:).
125/519
www.BDTIC.com/ST
Real-Time Clock (RTC) UM0306
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
126/519
www.BDTIC.com/ST
UM0306 Real-Time Clock (RTC)
The functions of the RTC are controlled by this control register. It is not possible to write to
the RTC_CR register while the peripheral is completing a previous write operation (flagged
by RTOFF=0, see Section 8.3.4 on page 124).
Note: 1 Any flag remains pending until the appropriate RTC_CR request bit is reset by software,
indicating that the interrupt request has been granted.
2 At reset the interrupts are disabled, no interrupt requests are pending and it is possible to
write to the RTC registers.
3 The OWF, ALRF, SECF and RSF bits are not updated when the APB1 clock is not running.
4 The OWF, ALRF, SECF and RSF bits can only be set by hardware and only cleared by
5 If ALRF = 1 and ALRIE = 1, the RTC global interrupt is enabled. If EXTI Line 17 is also
enabled through the EXTI Controller, both the RTC global interrupt and the RTC Alarm
interrupt are enabled.
6 If ALRF = 1, the RTC Alarm interrupt is enabled if EXTI Line 17 is enabled through the EXTI
Controller in interrupt mode. When the EXTI Line 17 is enabled in event mode, a pulse is
generated on this line (no RTC Alarm interrupt generation).
127/519
www.BDTIC.com/ST
Real-Time Clock (RTC) UM0306
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved PRL[19:16]
w w w w
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
PRL[15:0]
w w w w w w w w w w w w w w w w
Note: If the input clock frequency (fRTCCLK) is 32.768 kHz, write 7FFFh in this register to get a
signal period of 1 second.
128/519
www.BDTIC.com/ST
UM0306 Real-Time Clock (RTC)
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved RTC_DIV[19:16]
r r r r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
RTC_DIV[15:0]
r r r r r r r r r r r r r r r r
129/519
www.BDTIC.com/ST
Real-Time Clock (RTC) UM0306
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
RTC_CNT[31:16]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
RTC_CNT[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
130/519
www.BDTIC.com/ST
UM0306 Real-Time Clock (RTC)
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
RTC_ALR[31:16]
w w w w w w w w w w w w w w w w
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
RTC_ALR[15:0]
w w w w w w w w w w w w w w w w
131/519
www.BDTIC.com/ST
Real-Time Clock (RTC) UM0306
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 SECIE
ALRIE
OWIE
RTC_CRH
000h Reserved
Reset Value 0 0 0
RTOFF
SECF
ALRF
OWF
CNF
RSF
RTC_CRL
004h Reserved
Reset Value 1 0 0 0 0 0
RTC_PRLH PRL[19:16]
008h Reserved
Reset Value 0 0 0 0
RTC_PRLL PRL[15:0]
00Ch Reserved
Reset Value 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
RTC_DIVH DIV[31:16]
010h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
RTC_DIVL DIV[15:0]
014h Reserved
Reset Value 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
RTC_CNTL CNT[15:0]
01Ch Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
RTC_ALRH ALR[31:16]
020h Reserved
Reset Value 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
RTC_ALRL ALR[15:0]
024h Reserved
Reset Value 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
132/519
www.BDTIC.com/ST
UM0306 Backup registers (BKP)
9.1 Introduction
The backup registers are ten 16-bit registers for storing 20 bytes of user application data.
They are implemented in the backup domain that remains powered on by VBAT when the
VDD power is switched off. They are not reset when the device wakes up from STANDBY
mode or by a system reset or power reset.
In addition, the BKP control registers are used to manage the Tamper detection feature and
RTC calibration.
After reset, the access to Backup registers and RTC is disabled and the Backup domain is
protected against possible parasitic write access.
The DBP bit must be set in the Power control register (PWR_CR) to enable access to the
Backup registers and RTC.
9.2 Features
● Ten 16-bit data registers.
● Status/control register for managing the anti-Tamper feature
● Calibration register for storing the RTC calibration value
133/519
www.BDTIC.com/ST
Backup registers (BKP) UM0306
D[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
rw rw rw rw rw rw rw rw
134/519
www.BDTIC.com/ST
UM0306 Backup registers (BKP)
rw rw
Note: Setting the TPAL and TPE bits at the same time is always safe, however resetting both at
the same time can generate a spurious Tamper event. For this reason it is recommended to
change the TPAL bit only when the TPE bit is reset.
r r rw w w
135/519
www.BDTIC.com/ST
Backup registers (BKP) UM0306
136/519
www.BDTIC.com/ST
UM0306 Backup registers (BKP)
9
8
7
6
5
4
3
2
1
0
00h Reserved
BKP_DR1 D[15:0]
04h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
BKP_DR2 D[15:0]
08h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
BKP_DR3 D[15:0]
0Ch Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
BKP_DR4 D[15:0]
10h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
BKP_DR5 D[15:0]
14h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
BKP_DR6 D[15:0]
18h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
BKP_DR7 D[15:0]
1Ch Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
BKP_DR8 D[15:0]
20h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
BKP_DR9 D[15:0]
24h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
BKP_DR10 D[15:0]
28h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
CCO
BKP_RTCCR CAL[6:0]
2C Reserved
Reset Value 0 0 0 0 0 0 0 0
TPAL
TPE
BKP_CR
30h Reserved
Reset Value 0 0
TPIE
CTE
TEF
CTI
TIF
BKP_CSR
34h Reserved Reserved
Reset Value 0 0 0 0 0
137/519
www.BDTIC.com/ST
Independent watchdog (IWDG) UM0306
The STM32F10x has two embedded watchdog peripherals which offer a combination of
high safety level, timing accuracy and flexibility of use. Both Watchdog peripherals
(Independent and Window) serve to detect and resolve malfunctions due to software failure,
and triggering an interrupt or system reset when the counter reaches a given time-out value.
The Independent Watchdog (IWDG) is clocked by its own dedicated low-speed clock (32
kHz) and thus stays active even if the main clock fails. The Window Watchdog (WWDG)
clock is prescaled from the APB1 clock and has a configurable time-window that can be
programmed to detect abnormally late or early application behavior.
The IWDG is best suited to applications which require the watchdog to run as a totally
independent process outside the main application, but have lower timing accuracy
constraints. The WWDG is best suited to applications which require the watchdog to react
within an accurate timing window.
For further information on the Window Watchdog, refer to Section 11 on page 145.
10.1 Introduction
Figure 21 shows the functional blocks of the independent Watchdog module.
When the independent watchdog is started by writing the value CCCCh in the Key Register
(IWDG_KR), the counter starts counting down from the reset value of FFFh. When it
reaches the end of count value (000h) a reset signal is generated (IWDG RESET).
Whenever the key value AAAAh is written in the IWDG_KR register, the IWDG_RLR value is
re-loaded in the counter and the watchdog reset is prevented.
138/519
www.BDTIC.com/ST
UM0306 Independent watchdog (IWDG)
Note: The watchdog function is implemented in the VDD voltage domain that is still functional in
STOP and STANDBY modes.
/4 0 0.125 ms 512.5 ms
/8 1 0.25 ms 1025 ms
/16 2 0.50 ms 2050 ms
/32 3 1 ms 4100 ms
/64 4 2 ms 8200 ms
/128 5 4 ms 16400 ms
/256 6 (or 7) 8 ms 32800 ms
Note: These timings are given for a 32 kHz clock but the microcontroller’s internal RC frequency
can vary from 30 to 90 kHz. Moreover, given an exact RC oscillator frequency, the exact
timings still depend on the phasing of the APB interface clock versus the RC oscillator 32
kHz clock so that there is always a full RC period of uncertainty.
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
KEY[15:0]
w w w w w w w w w w w w w w w w
139/519
www.BDTIC.com/ST
Independent watchdog (IWDG) UM0306
140/519
www.BDTIC.com/ST
UM0306 Independent watchdog (IWDG)
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved PR[2:0]
rw rw rw
141/519
www.BDTIC.com/ST
Independent watchdog (IWDG) UM0306
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved RL[11:0]
rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
r r
142/519
www.BDTIC.com/ST
UM0306 Independent watchdog (IWDG)
Note: If several reload values or prescaler values are used by application, it is mandatory to wait
until RVU bit is reset before changing the reload value and to wait until PVU bit is reset
before changing the prescaler value. However, after updating the prescaler and/or the
reload value it is not necessary to wait until RVU or PVU is reset before continuing code
execution (even in case of low-power mode entry, the write operation is taken into account
and will complete)
143/519
www.BDTIC.com/ST
Independent watchdog (IWDG) UM0306
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
IWDG_KR KEY[15:0]
00h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
IWDG_PR PR[2:0]
04h Reserved
Reset Value 0 0 0
IWDG_RLR RL[11:0]
08h Reserved
Reset Value 1 1 1 1 1 1 1 1 1 1 1 1
RVU
PVU
IWDG_SR
0Ch Reserved
Reset Value 0 0
144/519
www.BDTIC.com/ST
UM0306 Window watchdog (WWDG)
11.1 Introduction
The Window Watchdog is used to detect the occurrence of a software fault, usually
generated by external interference or by unforeseen logical conditions, which causes the
application program to abandon its normal sequence. The Watchdog circuit generates an
MCU reset on expiry of a programmed time period, unless the program refreshes the
contents of the downcounter before the T6 bit becomes cleared. An MCU reset is also
generated if the 7-bit downcounter value (in the control register) is refreshed before the
downcounter has reached the window register value. This implies that the counter must be
refreshed in a limited window.
- W6 W5 W4 W3 W2 W1 W0
comparator
= 1 when
T6:0 > W6:0 CMP
Write WWDG_CR
WATCHDOG CONTROL REGISTER (WWDG_CR)
WDGA T6 T5 T4 T3 T2 T1 T0
6-BIT DOWNCOUNTER (CNT)
PCLK1
(from RCC clock controller)
WDG PRESCALER
(WDGTB)
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
145/519
www.BDTIC.com/ST
Window watchdog (WWDG) UM0306
value is lower than the window register value. The value to be stored in the WWDG_CR
register must be between FFh and C0h:
● Enabling the watchdog:
he watchdog is always disabled after a reset. It is enabled by setting the WDGA bit in
the WWDG_CR register, then it cannot be disabled again except by a reset.
● Controlling the downcounter:
This downcounter is free-running: It counts down even if the watchdog is disabled.
When the watchdog is enabled, the T6 bit must be set to prevent generating an
immediate reset.
The T[5:0] bits contain the number of increments which represents the time delay
before the watchdog produces a reset. The timing varies between a minimum and a
maximum value due to the unknown status of the prescaler when writing to the
WWDG_CR register (see Figure 23).
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 3Fh. Figure 23 describes the window watchdog
process.
Note: The T6 bit can be used to generate a software reset (the WDGA bit is set and the T6 bit is
cleared).
146/519
www.BDTIC.com/ST
UM0306 Window watchdog (WWDG)
W[6:0]
3Fh
time
Refresh not allowed Refresh Window
T6 bit
Reset
where:
TWWDG: WWDG timeout
TPCLK1: APB1 Clock period measured in ms
0 113 µs 7.28 ms
1 227 µs 14.56 ms
2 455 µs 29.12 ms
3 910 µs 58.25 ms
147/519
www.BDTIC.com/ST
Window watchdog (WWDG) UM0306
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved WDGA T6 T5 T4 T3 T2 T1 T0
rs rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
WDG WDG
Reserved EWI W6 W5 W4 W3 W2 W1 W0
TB1 TB0
rs rw rw rw rw rw rw rw rw rw
148/519
www.BDTIC.com/ST
UM0306 Window watchdog (WWDG)
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved EWIF
rc_w0
149/519
www.BDTIC.com/ST
Window watchdog (WWDG) UM0306
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
WDGA
WWDG_CR T[6:0]
00h Reserved
Reset Value 0 1 1 1 1 1 1 1
WDGTB1
WDGTB0
EWI
WWDG_CFR W[6:0]
04h Reserved
Reset Value 0 0 0 1 1 1 1 1 1 1
EWIF
WWDG_SR
08h Reserved
Reset Value 0
150/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
12.1 Introduction
The Advanced Control Timer (TIM1) 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,
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 12.4.20.
151/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
TI1F_ED
TI1FP1 Encoder
TI2FP2 Interface
REP Register
UI
U AutoReload Register
Repetition
counter U
Stop, Clear or Up/Down
ETRF
BRK BI
TIM1_BKIN Polarity Selection
Notes:
Reg Preload registers transferred
to active registers on U event
according to control bit
event
152/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
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 TIM1_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 25 and Figure 26 give some examples of the counter behavior when the prescaler
ratio is changed on the fly:
153/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
Figure 25. Counter timing diagram with prescaler division change from 1 to 2
CK_PSC
CNT_EN
COUNTER REGISTER F7 F8 F9 FA FB FC 00 01 02 03
PRESCALER BUFFER 0 1
PRESCALER COUNTER 0 0 1 0 1 0 1 0 1
Figure 26. Counter timing diagram with prescaler division change from 1 to 4
CK_PSC
CNT_EN
COUNTER REGISTER F7 F8 F9 FA FB FC 00 01
PRESCALER BUFFER 0 3
PRESCALER COUNTER 0 0 1 2 3 0 1 2 3
154/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
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 doesn’t change). In addition, if the URS bit (update request selection) in
TIM1_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
TIM1_SR register) is set (depending on the URS bit):
The repetition counter is reloaded with the content of TIM1_RCR register,
The auto-reload shadow register is updated with the preload value (TIM1_ARR),
The buffer of the prescaler is reloaded with the preload value (content of the TIM1_PSC
register).
The following figures show some examples of the counter behavior for different clock
frequencies when TIM1_ARR=0x36.
CK_PSC
CNT_EN
COUNTER REGISTER 31 32 33 34 35 36 00 01 02 03 04 05 06 07
COUNTER OVERFLOW
CK_PSC
CNT_EN
COUNTER OVERFLOW
155/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
CK_PSC
CNT_EN
COUNTER OVERFLOW
CK_PSC
COUNTER REGISTER 1F 20 00
COUNTER OVERFLOW
Figure 31. Counter timing diagram, Update event when ARPE=0 (TIM1_ARR not
preloaded)
CK_PSC
CNT_EN
COUNTER REGISTER 31 32 33 34 35 36 00 01 02 03 04 05 06 07
COUNTER OVERFLOW
AUTO-RELOAD REGISTER FF 36
156/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
Figure 32. Counter timing diagram, Update event when ARPE=1 (TIM1_ARR
preloaded)
CK_PSC
CNT_EN
COUNTER REGISTER F0 F1 F2 F3 F4 F5 00 01 02 03 04 05 06 07
COUNTER OVERFLOW
Down-counting mode
In down-counting mode, the counter counts from the auto-reload value (content of the
TIM1_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 up-counting is
repeated for the number of times programmed in the repetition counter register
(TIM1_RCR). Else the update event is generated at each counter overflow.
Setting the UG bit in the TIM1_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 TIM1_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 TIM1_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
TIM1_SR register) is set (depending on the URS bit):
● The repetition counter is reloaded with the content of TIM1_RCR register,
● The auto-reload active register is updated with the preload value (content of the
TIM1_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 TIM1_ARR=0x36.
157/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
CK_PSC
CNT_EN
COUNTER REGISTER 05 04 03 02 01 00 36 35 34 33 32 31 30 2F
CK_PSC
CNT_EN
COUNTER UNDERFLOW
CK_PSC
CNT_EN
COUNTER UNDERFLOW
158/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
CK_PSC
COUNTER REGISTER 20 1F 00 36
COUNTER UNDERFLOW
Figure 37. Counter timing diagram, Update event when repetition counter is not
used
CK_PSC
CNT_EN
COUNTER REGISTER 05 04 03 02 01 00 36 35 34 33 32 31 30 2F
COUNTER UNDERFLOW
AUTO-RELOAD REGISTER FF 36
159/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
In addition, if the URS bit (update request selection) in TIM1_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
TIM1_SR register) is set (depending on the URS bit):
The repetition counter is reloaded with the content of TIM1_RCR register,
The auto-reload active register is updated with the preload value (content of the TIM1_ARR
register). Note that if the update source is a counter overflow, the auto-reload is updated
before the counter is reloaded, so that the next period is the expected one (the counter is
loaded with the new value).
The following figures show some examples of the counter behavior for different clock
frequencies.
CK_PSC
CNT_EN
COUNTER REGISTER 04 03 02 01 00 01 02 03 04 05 06 05 04 03
COUNTER UNDERFLOW
COUNTER UNDERFLOW
CK_PSC
CNT_EN
COUNTER UNDERFLOW
160/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
CK_PSC
CNT_EN
COUNTER OVERFLOW
CK_PSC
COUNTER REGISTER 20 1F 01 00
COUNTER UNDERFLOW
Figure 42. Counter timing diagram, Update event with ARPE=1 (counter underflow)
CK_PSC
CNT_EN
COUNTER REGISTER 06 05 04 03 02 01 00 01 02 03 04 05 06 07
COUNTER UNDERFLOW
161/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
Figure 43. Counter timing diagram, Update event with ARPE=1 (counter overflow)
CK_PSC
CNT_EN
COUNTER REGISTER F7 F8 F9 FA FB FC 36 35 34 33 32 31 30 2F
COUNTER OVERFLOW
162/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
Figure 44. Update rate examples depending on mode and TIM1_RCR register
settings
Up-Counting Down-Counting
Counter
TIM1_CNT
TIM1_RCR = 0 UEV
TIM1_RCR = 1 UEV
TIM1_RCR = 2 UEV
TIM1_RCR = 3 UEV
TIM1_RCR = 3
and
re-synchronization
UEV
(by SW) (by SW) (by SW)
UEV Update Event: Preload registers transferred to active registers and update interrupt generated
Update Event if the repetition down-counter underflow occurs when the counter is equal to
to the auto-reload value.
163/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
Internal clock
CEN=CNT_EN
UG
CNT_INIT
COUNTER REGISTER 31 32 33 34 35 36 00 01 02 03 04 05 06 07
ti2f or
or
ti1f or encoder
ITR1 001 mode
ti1f_ed 100
ti1fp1 trgi external clock
ti2f_rising 0 101 mode 1 ck_psc
TI2 Filter Edge ti2fp2 110
Detector ti2f_falling etrf external clock
1 etrf 111 mode 2
ICF[3:0] CC2P CK_INT internal clock
TIM1_CCMR1 TIM1_CCER mode
(internal clock)
ECE SMS[2:0]
TIM1_SMCR
164/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
For example, to configure the up-counter 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
TIM1_CCMR1 register.
2. Configure the input filter duration by writing the IC2F[3:0] bits in the TIM1_CCMR1
register (if no filter is needed, keep IC2F=0000).
3. Select rising edge polarity by writing CC2P=0 in the TIM1_CCER register.
4. Configure the timer in external clock mode 1 by writing SMS=111 in the TIM1_SMCR
register.
5. Select TI2 as the trigger input source by writing TS=110 in the TIM1_SMCR register.
6. Enable the counter by writing CEN=1 in the TIM1_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
CNT_EN
COUNTER REGISTER 34 35 36
TIF
Write TIF=0
ti2f or
or
ti1f or encoder
mode
165/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
For example, to configure the up-counter 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 TIM1_SMCR register.
2. Set the prescaler by writing ETPS[1:0]=01 in the TIM1_SMCR register
3. Select rising edge detection on the ETR pin by writing ETP=0 in the TIM1_SMCR
register
4. Enable external clock mode 2 by writing ECE=1 in the TIM1_SMCR register.
5. Enable the counter by writing CEN=1 in the TIM1_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
ETR
ETRP
ETRF
COUNTER REGISTER 34 35 36
166/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
TI1F_ED
to the slave mode controller
TI1F_rising 0
TI1 TI1F
filter Edge TI1FP1
01
fDTS down-counter Detector TI1F_falling
1
TI2FP1 IC1 divider IC1PS
10
/1, /2, /4, /8
ICF[3:0] CC1P TRC
11
TIM1_CCMR1 TIM1_CCER (from slave mode
controller)
TI2F_rising 0
(from channel 2)
CC1S[1:0] ICPS[1:0] CC1E
TI2F_falling
1
(from channel 2) TIM1_CCMR1 TIM1_CCER
The output stage generates an intermediate waveform which is then used for reference:
OCxRef (active high). The polarity acts at the end of the chain.
APB Bus
MCU-PERIPHERAL INTERFACE
(if 16-bit)
8 8
high
low
S write CCR1H
read CCR1H S write_in_progress
read_in_progress
Capture/Compare Preload Register write CCR1L
read CCR1L R
R CC1S[1]
output
capture_transfer compare_transfer mode
CC1S[0]
CC1S[1] input
mode OC1PE
Capture/Compare Shadow Register OC1PE
CC1S[0] UEV
TIM1_CCMR1
comparator (from time
ic1ps capture base unit)
CC1E CNT>CCR1
Counter
CNT=CCR1
CC1G
TIM1_EGR
167/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
ETR 0
Output OC1
‘0’ Enable
x0 1 Circuit
10
oc1_dt CC1P
CNT>CCR1 11
Output Mode oc1ref Dead-Time TIM1_CCER
CNT=CCR1 Controller Generator
oc1n_dt
11
10 0 OC1N
Output
‘0’ 0x Enable
1 Circuit
OC1CE OC1M[2:0] DTG[7:0] CC1NE CC1E CC1NP MOE OSSI OSSR TIM1_BDTR
TIM1_CCMR1 TIM1_BDTR TIM1_CCER TIM1_CCER
CC1P
CNT > CCR1
Output Mode oc1ref TIM1_CCER
CNT = CCR1 Controller
CC1E TIM1_CCER
The capture/compare block is made of one preload register and one shadow register. Write
and read always access the preload register.
In capture mode, captures are actually done in the shadow register, which is copied into the
preload register.
In compare mode, the content of the preload register is copied into the shadow register
which is compared to the counter.
168/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
The following example shows how to capture the counter value in TIM1_CCR1 when TI1
input rises. To do this, use the following procedure:
● Select the active input: TIM1_CCR1 must be linked to the TI1 input, so write the CC1S
bits to 01 in the TIM1_CCMR1 register. As soon as CC1S becomes different from 00,
the channel is configured in input and the TIM1_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 TIM1_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
TIM1_CCMR1 register.
● Select the edge of the active transition on the TI1 channel by writing CC1P bit to 0 in
the TIM1_CCER register (rising edge in this case).
● Program the input prescaler. In our example, we wish the capture to be performed at
each valid transition, so the prescaler is disabled (write IC1PS bits to ‘00’ in the
TIM1_CCMR1 register).
● Enable capture from the counter into the capture register by setting the CC1E bit in the
TIM1_CCER register.
● If needed, enable the related interrupt request by setting the CC1IE bit in the
TIM1_DIER register, and/or the DMA request by setting the CC1DE bit in the
TIM1_DIER register.
When an input capture occurs:
● The TIM1_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 TIM1_EGR register.
169/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
For example, you can measure the period (in TIM1_CCR1 register) and the duty cycle (in
TIM1_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 TIM1_CCR1: write the CC1S bits to 01 in the TIM1_CCMR1
register (TI1 selected).
● Select the active polarity for TI1FP1 (used both for capture in TIM1_CCR1 and counter
clear): write the CC1P bit to ‘0’ (active on rising edge).
● Select the active input for TIM1_CCR2: write the CC2S bits to 10 in the TIM1_CCMR1
register (TI1 selected).
● Select the active polarity for TI1FP2 (used for capture in TIM1_CCR2): write the CC2P
bit to ‘1’ (active on falling edge).
● Select the valid trigger input: write the TS bits to 101 in the TIM1_SMCR register
(TI1FP1 selected).
● Configure the slave mode controller in reset mode: write the SMS bits to 100 in the
TIM1_SMCR register.
● Enable the captures: write the CC1E and CC2E bits to ‘1’ in the TIM1_CCER register.
TI1
TIM1_CCR1 0004
TIM1_CCR2 0002
170/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
171/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
oc1ref=OC1
172/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
‘1’. If the compare value is 0 then OCxRef is held at ‘0’. Figure 56 shows some edge-aligned
PWM waveforms in an example where TIM1_ARR=8.
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
Down-counting configuration
Down-counting is active when DIR bit in TIM1_CR1 register is high. Refer to the Down-
counting mode on page 157
In PWM mode 1, the reference signal OCxRef is low as long as TIM1_CNT>TIM1_CCRx
else it becomes high. If the compare value in TIM1_CCRx is greater than the auto-reload
value in TIM1_ARR, then OCxREF is held at ‘1’. 0% PWM is not possible in this mode.
173/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
COUNTER REGISTER 0 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 0 1
ocxref
CCRx=4
CCxIF
ocxref
CCRx=7
CCxIF
ocxref ‘1’
CCRx>=8
CCxIF
ocxref ‘0’
CCRx=0
CCxIF
174/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
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
OCx
delay
OCxN
delay
Figure 59. Dead-Time waveforms with delay greater than the negative pulse.
OCxREF
OCx
delay
OCxN
Figure 60. Dead-Time waveforms with delay greater than the positive pulse.
OCxREF
OCx
OCxN
delay
The dead-time delay is the same for each of the channels and is programmable with the
DTG bits in the TIM1_BDTR register. Refer to Section 12.5.18: Break and dead-time
register (TIM1_BDTR) on page 216 for delay calculation.
175/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
176/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
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 TIM1_SR register) is set. An interrupt can be
generated if the BIE bit in the TIM1_DIER register is set. A DMA request can be sent if
the BDE bit in the TIM1_DIER register is set.
● If the AOE bit in the TIM1_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 TIM1_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 TIM1_BDTR register. Refer to
Section 12.5.18: Break and dead-time register (TIM1_BDTR) on page 216. The LOCK bits
can be written only once after an MCU reset.
The Figure 61 shows an example of behavior of the outputs in response to a break.
177/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
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=0, CCxNE=0, CCxNP=0, OISxN=1)
OCx
OCxN delay
(CCxE=1, CCxP=0, OISx=1, CCxNE=0, CCxNP=0, OISxN=0)
OCx
OCxN
(CCxE=1, CCxP=0, CCxNE=0, CCxNP=0, OISx=OISxN=0 or OISx=OISxN=1)
178/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
(CCRx)
counter (CNT)
ETRF
OCxREF
(OCxCE=’0’)
OCxREF
(OCxCE=’1’)
OCREF_CLR OCREF_CLR
becomes high still high
179/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
(CCRx)
counter (CNT)
OCxREF
Write COM to 1
COM event
180/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
TI2
OC1Ref
OC1
TIM1_ARR
COUNTER
TIM1_CCR1
0
tDELAY t
tPULSE
For example you may want to generate a positive pulse on OC1 with a length of tPULSE and
after a delay of tDELAY as soon as a positive edge is detected on the TI2 input pin.
Let’s use TI2FP2 as trigger 1:
● Map TI2FP2 to TI2 by writing CC2S=’01’ in the TIM1_CCMR1 register.
● TI2FP2 must detect a rising edge, write CC2P=’0’ in the TIM1_CCER register.
● Configure TI2FP2 as trigger for the slave mode controller (TRGI) by writing TS=’110’ in
the TIM1_SMCR register.
● TI2FP2 is used to start the counter by writing SMS to ‘110’ in the TIM1_SMCR register
(trigger mode).
181/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
The OPM waveform is defined by writing the compare registers (taking into account the
clock frequency and the counter prescaler).
● The tDELAY is defined by the value written in the TIM1_CCR1 register.
● The tPULSE is defined by the difference between the auto-reload value and the compare
value (TIM1_ARR - TIM1_CCR1).
● Let’s say you want to build a waveform with a transition from ‘0’ to ‘1’ when a compare
match occurs and a transition from ‘1’ to ‘0’ when the counter reaches the auto-reload
value. To do this you enable PWM mode 2 by writing OC1M=111 in the TIM1_CCMR1
register. You can optionally enable the preload registers by writing OC1PE=’1’ in the
TIM1_CCMR1 register and ARPE in the TIM1_CR1 register. In this case you have to
write the compare value in the TIM1_CCR1 register, the auto-reload value in the
TIM1_ARR register, generate an update by setting the UG bit and wait for external
trigger event on TI2. CC1P is written to ‘0’ in this example.
In our example, the DIR and CMS bits in the TIM1_CR1 register should be low.
You only want 1 pulse, so you write ‘1’ in the OPM bit in the TIM1_CR1 register to stop the
counter at the next update event (when the counter rolls over from the auto-reload value
back to 0).
Particular case: OCx fast enable:
In One Pulse Mode, the edge detection on TIx input set the CEN bit which enables the
counter. Then the comparison between the counter and the compare value makes the
output toggle. But several clock cycles are needed for these operations and it limits the
minimum delay tDELAY min we can get.
If you want to output a waveform with the minimum delay, you can set the OCxFE bit in the
TIM1_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.
182/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
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.
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.
The Figure 65 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).
183/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
TI1
TI2
COUNTER
up down up
Figure 66 gives an example of counter behavior when TI1FP1 polarity is inverted (same
configuration as above except CC1P=’1’).
Figure 66. Example of encoder interface mode with TI1FP1 polarity inverted.
forward jitter backward jitter forward
TI1
TI2
COUNTER
down up down
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.
184/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
185/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
TIH1
TIH2
TIH3
Interfacing Timer
counter (CNT)
(CCR2)
TRGO=OC2REF
COM
OC1
OC1N
TIM1 Timer
OC2
OC2N
OC3
OC3N
186/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
TI1
UG
COUNTER REGISTER 30 31 32 33 34 35 36 00 01 02 03 00 01 02 03
TIF
187/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
TI1
cnt_en
COUNTER REGISTER 30 31 32 33 34 35 36 37 38
TIF
Write TIF=0
188/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
TI2
cnt_en
COUNTER REGISTER 34 35 36 37 38
TIF
189/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
1. Configure the external trigger input circuit by programming the TIM1_SMCR register as
follows:
– ETF = 0000: no filter
– ETPS=00: prescaler disabled
– ETP=0: detection of rising edges on ETR and ECE=1 to enable the external clock
mode 2.
2. Configure the channel 1 as follows, to detect rising edges on TI:
– IC1F=0000: no filter.
– The capture prescaler is not used for triggering and does not need to be
configured.
– CC1S=01in TIM1_CCMR1 register to select only the input capture source
– CC1P=0 in TIM1_CCER register to validate the polarity (and detect rising edge
only).
3. Configure the timer in trigger mode by writing SMS=110 in TIM1_SMCR register.
Select TI1 as the input source by writing TS=101 in TIM1_SMCR register.
A rising edge on TI1 enables the counter and sets the TIF flag. The counter then counts on
ETR rising edges.
The delay between the rising edge of the ETR signal and the actual reset of the counter is
due to the resynchronization circuit on ETRP input.
TI1
CEN/CNT_EN
ETR
COUNTER REGISTER 34 35 36
TIF
190/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
rw rw rw rw rw rw rw rw rw rw
191/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
192/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
Res. OIS4 OIS3N OIS3 OIS2N OIS2 OIS1N OIS1 TI1S MMS[2:0] CCDS CCUS Res. CCPC
rw rw rw rw rw rw rw rw rw rw rw rw rw rw
193/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
194/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bit 15 This bit selects whether ETR or ETR is used for trigger operations
0: ETR is non-inverted, active at high level or rising edge.
1: ETR is inverted, active at low level or falling edge.
ECE: External clock enable.
This bit enables External clock mode 2.
0: External clock mode 2 disabled.
1: External clock mode 2 enabled. The counter is clocked by any active edge on the
ETRF signal.
Bit 14 Note 1: Setting the ECE bit has the same effect as selecting external clock mode 1
with TRGI connected to ETRF (SMS=111 and TS=111).
Note 2: It is possible to simultaneously use external clock mode 2 with the following
slave modes: reset mode, gated mode and trigger mode. Nevertheless, TRGI must not
be connected to ETRF in this case (TS bits must not be 111).
Note 3: If external clock mode 1 and external clock mode 2 are enabled at the same
time, the external clock input is ETRF.
ETPS[1:0]: External trigger prescaler.
External trigger signal ETRP frequency must be at most 1/4 of TIM1CLK frequency.
A prescaler can be enabled to reduce ETRP frequency. It is useful when inputting
fast external clocks.
Bits 13:12
00: Prescaler OFF.
01: ETRP frequency divided by 2.
10: ETRP frequency divided by 4.
11: ETRP frequency divided by 8.
195/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
196/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
197/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
198/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
199/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
200/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
201/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
w w w w w w w w
202/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
203/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
204/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
205/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
206/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
207/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
208/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
209/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
210/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
Table 37. Output control bits for complementary OCx and OCxN channels with
break feature
Control bits Output states
MOE OSSI OSSR CCxE CCxNE
OCx Output State OCxN Output State
bit bit bit bit bit
Output Disabled (not Output Disabled (not
0 0 0 driven by the timer) driven by the timer)
OCx=CCxP, OCx_EN=0 OCxN=CCxNP, OCxN_EN=0
Complementary to
OCREF + Polarity +
OCREF (not OCREF) +
0 1 1 dead-time
Polarity + dead-time
OCx_EN=1
OCxN_EN=1
Off-State (output
enabled with inactive OCxREF + Polarity
1 0 1 OCxN=OCxREF xor CCxNP,
state)
OCxN_EN=1
OCx=CCxP, OCx_EN=1
Off-State (output
OCxREF + Polarity
enabled with inactive
1 1 0 OCx=OCxREF xor CCxP,
state)
OCx_EN=1
OCxN=CCxNP, OCxN_EN=1
Complementary to
OCREF + Polarity +
OCREF (not OCREF) +
1 1 1 dead-time
Polarity + dead-time
OCx_EN=1
OCxN_EN=1
0 0 0
Output Disabled (not driven by the timer)
0 0 1
Asynchronously: OCx=CCxP, OCx_EN=0, OCxN=CCxNP,
0 1 0 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 don’t correspond to
OCX and OCxN both to active state.
0 1 X 0 0
1 0 1 Off-State (output enabled with inactive state)
Asynchronously: OCx=CCxP, OCx_EN=1, OCxN=CCxNP,
1 1 0 OCxN_EN=1
Then if the clock is present: OCx=OISx and OCxN=OISxN after a
1 1 1 dead-time, assuming that OISx and OISxN don’t correspond to
OCX and OCxN both to active state
Note: The state of the external I/O pins connected to the complementary OCx and
OCxN channels depends on the OCx and OCxN channel state and the GPIO and
AFIO registers.
211/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
CNT[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
PSC[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
ARR[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
212/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
Reserved REP[7:0]
rw rw rw rw rw rw rw rw
CCR1[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
213/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
CCR2[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
CCR3[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
214/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
CCR4[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
215/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
216/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
Note: As the bits AOE, BKP, BKE, OSSI, OSSR and DTG[7:0] can be write-locked depending on
the LOCK configuration, it can be necessary to configure all of them during the first write
access to the TIM1_BDTR register.
217/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
rw rw rw rw rw rw rw rw rw rw
DMAB[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
218/519
www.BDTIC.com/ST
UM0306 Advanced control timer (TIM1)
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
ARPE
UDIS
OPM
CKD CMS
CEN
URS
DIR
TIM1_CR1
00h Reserved [1:0] [1:0]
Reset Value 0 0 0 0 0 0 0 0 0 0
Reserved
OIS3N
OIS2N
OIS1N
CCPC
CCDS
CCUS
OIS4
OIS3
OIS2
OIS1
TI1S
TIM1_CR2 MMS[2:0]
04h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0
CC3IE Reserved
MSM
ETPS
ECE
ETP
TIM1_SMCR ETF[3:0] TS[2:0] SMS[2:0]
08h Reserved [1:0]
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
COMDE
CC4DE
CC3DE
CC2DE
CC1DE
COMIE
CC4IE
CC2IE
CC1IE
UDE
TDE
UIE
BIE
TIE
TIM1_DIER
0Ch Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
CC4OF
CC3OF
CC2OF
CC1OF
COMIF
Reserved
CC4IF
CC3IF
CC2IF
CC1IF
UIF
BIF
TIF
TIM1_SR
10h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0
CC4G
CC3G
CC2G
CC1G
COM
UG
BG
TG
TIM1_EGR
14h Reserved
Reset Value 0 0 0 0 0 0 0 0
TIM1_CCMR1
OC2CE
OC1CE
OC2PE
OC1PE
OC2FE
OC1FE
OC2M CC2S OC1M CC1S
Output Compare
Reserved [2:0] [1:0] [2:0] [1:0]
mode
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
18h
TIM1_CCMR1 IC2 IC1
CC2S CC1S
Input Capture IC2F[3:0] PSC IC1F[3:0] PSC
Reserved [1:0] [1:0]
mode [1:0] [1:0]
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIM1_CCMR2 OC3CE
OC4PE
OC3PE
OC4FE
OC3FE
O24CE
CC2NP
CC2NE
CC1NP
CC1NE
CC4P
CC4E
CC3P
CC3E
CC2P
CC2E
CC1P
CC1E
TIM1_CCER
20h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIM1_CNT CNT[15:0]
24h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIM1_PSC PSC[15:0]
28h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIM1_ARR ARR[15:0]
2Ch Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIM1_RCR REP[7:0]
30h Reserved
Reset Value 0 0 0 0 0 0 0 0
TIM1_CCR1 CCR1[15:0]
34h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
219/519
www.BDTIC.com/ST
Advanced control timer (TIM1) UM0306
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
TIM1_CCR2 CCR2[15:0]
38h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIM1_CCR3 CCR3[15:0]
3Ch Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIM1_CCR4 CCR4[15:0]
40h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
OSSR
OSSI
MOE
LOCK
AOE
BKP
BKE
TIM1_BDTR DT[7:0]
44h Reserved [1:0]
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIM1_DMAR DMAB[15:0]
4Ch Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
220/519
www.BDTIC.com/ST
UM0306 General purpose timer (TIMx)
13.1 Introduction
The General purpose timers consist of a 16-bit auto-reload counter driven by a
programmable prescaler.
They may be used for a variety of purposes, including measuring the pulse lengths of input
signals (input capture) or generating output waveforms (output compare and PWM).
Pulse lengths and waveform periods can be modulated from a few microseconds to several
milliseconds using the timer prescaler and the RCC clock controller prescalers.
The timers are completely independent, and do not share any resources. They can be
synchronized together as described in Section 13.4.15.
221/519
www.BDTIC.com/ST
General purpose timer (TIMx) UM0306
TI1FP1 Encoder
TI2FP2 Interface
U AutoReload Register UI
Notes:
Reg Preload registers transferred
to active registers on U event
according to control bit
event
222/519
www.BDTIC.com/ST
UM0306 General purpose timer (TIMx)
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 in account at the next update event.
Figure 73 and Figure 74 give some examples of the counter behavior when the prescaler
ratio is changed on the fly:
Figure 73. Counter timing diagram with prescaler division change from 1 to 2
CK_PSC
CNT_EN
COUNTER REGISTER F7 F8 F9 FA FB FC 00 01 02 03
PRESCALER BUFFER 0 1
PRESCALER COUNTER 0 0 1 0 1 0 1 0 1
223/519
www.BDTIC.com/ST
General purpose timer (TIMx) UM0306
Figure 74. Counter timing diagram with prescaler division change from 1 to 4
CK_PSC
CNT_EN
COUNTER REGISTER F7 F8 F9 FA FB FC 00 01
PRESCALER BUFFER 0 3
PRESCALER COUNTER 0 0 1 2 3 0 1 2 3
224/519
www.BDTIC.com/ST
UM0306 General purpose timer (TIMx)
CK_INT
CNT_EN
COUNTER REGISTER 31 32 33 34 35 36 00 01 02 03 04 05 06 07
COUNTER OVERFLOW
CK_INT
CNT_EN
COUNTER OVERFLOW
CK_INT
CNT_EN
COUNTER OVERFLOW
225/519
www.BDTIC.com/ST
General purpose timer (TIMx) UM0306
CK_INT
COUNTER REGISTER 1F 20 00
COUNTER OVERFLOW
Figure 79. Counter timing diagram, Update event when ARPE=0 (TIMx_ARR not
preloaded)
CK_INT
CNT_EN
COUNTER REGISTER 31 32 33 34 35 36 00 01 02 03 04 05 06 07
COUNTER OVERFLOW
AUTO-RELOAD REGISTER FF 36
226/519
www.BDTIC.com/ST
UM0306 General purpose timer (TIMx)
Figure 80. Counter timing diagram, Update event when ARPE=1 (TIMx_ARR
preloaded)
CK_PSC
CNT_EN
COUNTER REGISTER F0 F1 F2 F3 F4 F5 00 01 02 03 04 05 06 07
COUNTER OVERFLOW
Down-counting mode
In down-counting 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 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.
227/519
www.BDTIC.com/ST
General purpose timer (TIMx) UM0306
CK_INT
CNT_EN
COUNTER REGISTER 05 04 03 02 01 00 36 35 34 33 32 31 30 2F
CK_INT
CNT_EN
COUNTER UNDERFLOW
CK_INT
CNT_EN
COUNTER UNDERFLOW
228/519
www.BDTIC.com/ST
UM0306 General purpose timer (TIMx)
CK_INT
COUNTER REGISTER 20 1F 00 36
COUNTER UNDERFLOW
Figure 85. Counter timing diagram, Update event when repetition counter is not
used
CK_INT
CNT_EN
COUNTER REGISTER 05 04 03 02 01 00 36 35 34 33 32 31 30 2F
COUNTER UNDERFLOW
AUTO-RELOAD REGISTER FF 36
229/519
www.BDTIC.com/ST
General purpose timer (TIMx) UM0306
In addition, if the URS bit (update request selection) in TIMx_CR1 register is set, setting the
UG bit generates an update event UEV but without setting the UIF flag (thus no interrupt or
DMA request is sent). This is to avoid generating both update and capture interrupt when
clearing the counter on the capture event.
When an update event occurs, all the registers are updated and the update flag (UIF bit in
TIMx_SR register) is set (depending on the URS bit):
The auto-reload active register is updated with the preload value (content of the TIMx_ARR
register). Note that if the update source is a counter overflow, the auto-reload is updated
before the counter is reloaded, so that the next period is the expected one (the counter is
loaded with the new value).
The following figures show some examples of the counter behavior for different clock
frequencies.
CK_INT
CNT_EN
COUNTER REGISTER 04 03 02 01 00 01 02 03 04 05 06 05 04 03
COUNTER UNDERFLOW
COUNTER UNDERFLOW
CK_INT
CNT_EN
COUNTER UNDERFLOW
230/519
www.BDTIC.com/ST
UM0306 General purpose timer (TIMx)
CK_INT
CNT_EN
CK_INT
COUNTER REGISTER 20 1F 01 00
COUNTER UNDERFLOW
Figure 90. Counter timing diagram, Update event with ARPE=1 (counter underflow)
CK_INT
CNT_EN
COUNTER REGISTER 06 05 04 03 02 01 00 01 02 03 04 05 06 07
COUNTER UNDERFLOW
231/519
www.BDTIC.com/ST
General purpose timer (TIMx) UM0306
Figure 91. Counter timing diagram, Update event with ARPE=1 (counter overflow)
CK_INT
CNT_EN
COUNTER REGISTER F7 F8 F9 FA FB FC 36 35 34 33 32 31 30 2F
COUNTER OVERFLOW
232/519
www.BDTIC.com/ST
UM0306 General purpose timer (TIMx)
CK_INT
CEN=CNT_EN
UG
CNT_INIT
COUNTER REGISTER 31 32 33 34 35 36 00 01 02 03 04 05 06 07
ti2f or
or
ti1f or encoder
ITR1 001 mode
ti1f_ed 100
ti1fp1 trgi external clock
ti2f_rising 0 101 mode 1 CK_PSC
TI2 Filter Edge ti2fp2 110
Detector ti2f_falling etrf external clock
1 etrf 111 mode 2
ICF[3:0] CC2P CK_INT internal clock
TIMx_CCMR1 TIMx_CCER mode
(internal clock)
ECE SMS[2:0]
TIMx_SMCR
For example, to configure the up-counter to count in response to a rising edge on the TI2
input, use the following procedure:
For example, to configure the up-counter to count in response to a rising edge on the TI2
input, use the following procedure:
1. Configure channel 2 to detect rising edges on the TI2 input by writing CC2S= ‘01’ in the
TIMx_CCMR1 register.
2. Configure the input filter duration by writing the IC2F[3:0] bits in the TIMx_CCMR1
register (if no filter is needed, keep IC2F=0000).
Note: The capture prescaler is not used for triggering, so you don’t need to configure it.
3. Select rising edge polarity by writing CC2P=0 in the TIMx_CCER register.
4. Configure the timer in external clock mode 1 by writing SMS=111 in the TIMx_SMCR
register.
5. Select TI2 as the input source by writing TS=110 in the TIMx_SMCR register.
6. Enable the counter by writing CEN=1 in the TIMx_CR1 register.
When a rising edge occurs on TI2, the counter counts once and the TIF flag is set.
233/519
www.BDTIC.com/ST
General purpose timer (TIMx) UM0306
The delay between the rising edge on TI2 and the actual clock of the counter is due to the
resynchronization circuit on TI2 input.
TI2
CNT_EN
COUNTER REGISTER 34 35 36
TIF
Write TIF=0
ti2f or
or
ti1f or encoder
mode
For example, to configure the up-counter 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.
234/519
www.BDTIC.com/ST
UM0306 General purpose timer (TIMx)
fMASTER
CNT_EN
ETR
ETRP
ETRF
COUNTER REGISTER 34 35 36
TI1F_ED
to the slave mode controller
TI1F_rising 0
TI1 TI1F
filter Edge TI1FP1
01
fDTS down-counter Detector TI1F_falling
1
TI2FP1 IC1 divider IC1PS
10
/1, /2, /4, /8
ICF[3:0] CC1P TRC
11
TIMx_CCMR1 TIMx_CCER (from slave mode
controller)
TI2F_rising 0
(from channel 2)
CC1S[1:0] ICPS[1:0] CC1E
TI2F_falling
1
(from channel 2) TIMx_CCMR1 TIMx_CCER
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.
235/519
www.BDTIC.com/ST
General purpose timer (TIMx) UM0306
APB Bus
MCU-PERIPHERAL INTERFACE
(if 16-bit)
8 8
high
low
S write CCR1H
read CCR1H S write_in_progress
read_in_progress
Capture/Compare Preload Register write CCR1L
read CCR1L R
R CC1S[1]
output
capture_transfer compare_transfer mode
CC1S[0]
CC1S[1] input
mode OC1PE
Capture/Compare Shadow Register OC1PE
CC1S[0] UEV
TIMx_CCMR1
comparator (from time
ic1ps capture base unit)
CC1E CNT>CCR1
Counter
CNT=CCR1
CC1G
TIMx_EGR
CC1P
CNT > CCR1
Output Mode oc1ref TIMx_CCER
CNT = CCR1 Controller
CC1E TIMx_CCER
OC1M[2:0]
TIMx_CCMR1
The capture/compare block is made of one preload register and one shadow register. Write
and read always access the preload register.
In capture mode, captures are actually done in the shadow register, which is copied into the
preload register.
In compare mode, the content of the preload register is copied into the shadow register
which is compared to the counter.
236/519
www.BDTIC.com/ST
UM0306 General purpose timer (TIMx)
cleared by software by writing it to ‘0’ or by reading the captured data stored in the
TIMx_CCRx register (when you read the low byte in case of 16-bit 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.
● Select the edge of the active transition on the TI1 channel by writing CC1P bit to 0 in
the TIMx_CCER register (rising edge in this case).
● Program the input prescaler. In our example, we wish the capture to be performed at
each valid transition, so the prescaler is disabled (write IC1PS bits to 00 in the
TIMx_CCMR1 register).
● Enable capture from the counter into the capture register by setting the CC1E bit in the
TIMx_CCER register.
● If needed, enable the related interrupt request by setting the CC1IE bit in the
TIMx_DIER register, and/or the DMA request by setting the CC1DE bit in the
TIMx_DIER register.
When an input capture occurs:
● The TIMx_CCR1 register gets the value of the counter on the active transition.
● CC1IF flag is set (interrupt flag). CC1OF is also set if at least two consecutive captures
occurred whereas the flag was not cleared.
● An interrupt is generated depending on the CC1IE bit.
● A DMA request is generated depending on the CC1DE bit.
In order to handle the overcapture, it is recommended to read the data before the
overcapture flag. This is to avoid missing an overcapture which could happen after reading
the flag and before reading the data.
Note: IC interrupt and/or DMA requests can be generated by software by setting the
corresponding CCxG bit in the TIMx_EGR register.
237/519
www.BDTIC.com/ST
General purpose timer (TIMx) UM0306
For example, you can measure the period (in TIMx_CCR1 register) and the duty cycle (in
TIMx_CCR2 register) of the PWM applied on TI1 using the following procedure (depending
on CK_INT frequency and prescaler value):
● Select the active input for TIMx_CCR1: write the CC1S bits to 01 in the TIMx_CCMR1
register (TI1 selected).
● Select the active polarity for TI1FP1 (used both for capture in TIMx_CCR1 and counter
clear): write the CC1P bit to ‘0’ (active on rising edge).
● Select the active input for TIMx_CCR2: write the CC2S bits to 10 in the TIMx_CCMR1
register (TI1 selected).
● Select the active polarity for TI1FP2 (used for capture in TIMx_CCR2): write the CC2P
bit to ‘1’ (active on falling edge).
● Select the valid trigger input: write the TS bits to 101 in the TIMx_SMCR register
(TI1FP1 selected).
● Configure the slave mode controller in reset mode: write the SMS bits to 100 in the
TIMx_SMCR register.
● Enable the captures: write the CC1E and CC2E bits to ‘1’ in the TIMx_CCER register.
TI1
TIMx_CCR1 0004
TIMx_CCR2 0002
238/519
www.BDTIC.com/ST
UM0306 General purpose timer (TIMx)
239/519
www.BDTIC.com/ST
General purpose timer (TIMx) UM0306
oc1ref=OC1
240/519
www.BDTIC.com/ST
UM0306 General purpose timer (TIMx)
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
Down-counting configuration
Down-counting is active when DIR bit in TIMx_CR1 register is high. Refer to Down-counting
mode on page 227
In PWM mode 1, the reference signal ocxref is low as long as TIMx_CNT>TIMx_CCRx else
it becomes high. If the compare value in TIMx_CCRx is greater than the auto-reload value in
TIMx_ARR, then ocxref is held at ‘1’. 0% PWM is not possible in this mode.
241/519
www.BDTIC.com/ST
General purpose timer (TIMx) UM0306
COUNTER REGISTER 0 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 0 1
ocxref
CCRx=4
CCxIF
ocxref
CCRx=7
CCxIF
ocxref ‘1’
CCRx>=8
CCxIF
ocxref ‘0’
CCRx=0
CCxIF
242/519
www.BDTIC.com/ST
UM0306 General purpose timer (TIMx)
TI2
OC1Ref
OC1
TIM1_ARR
COUNTER
TIM1_CCR1
0
tDELAY
tPULSE t
For example you may want to generate a positive pulse on OC1 with a length of tPULSE and
after a delay of tDELAY as soon as a positive edge is detected on the TI2 input pin.
Let’s use TI2FP2 as trigger 1:
● Map TI2FP2 on TI2 by writing IC2S=’01’ in the TIMx_CCMR1 register.
● TI2FP2 must detect a rising edge, write CC2P=’0’ in the TIMx_CCER register.
● Configure TI2FP2 as trigger for the slave mode controller (TRGI) by writing TS=’110’ in
the TIMx_SMCR register.
● TI2FP2 is used to start the counter by writing SMS to ‘110’ in the TIMx_SMCR register
(trigger mode).
The OPM waveform is defined by writing the compare registers (taking into account the
clock frequency and the counter prescaler).
● The tDELAY is defined by the value written in the TIMx_CCR1 register.
● The tPULSE is defined by the difference between the auto-reload value and the compare
value (TIMx_ARR - TIMx_CCR1).
● Let’s say you want to build a waveform with a transition from ‘0’ to ‘1’ when a compare
match occurs and a transition from ‘1’ to ‘0’ when the counter reaches the auto-reload
value. To do this you enable PWM mode 2 by writing OC1M=111 in the TIMx_CCMR1
register. You can optionally enable the preload registers by writing OC1PE=’1’ in the
TIMx_CCMR1 register and ARPE in the TIMx_CR1 register. In this case you have to
write the compare value in the TIMx_CCR1 register, the auto-reload value in the
TIMx_ARR register, generate an update by setting the UG bit and wait for external
trigger event on TI2. CC1P is written to ‘0’ in this example.
In our example, the DIR and CMS bits in the TIMx_CR1 register should be low.
You only want 1 pulse, so you write ‘1’ in the OPM bit in the TIMx_CR1 register to stop the
counter at the next update event (when the counter rolls over from the auto-reload value
back to 0).
Particular case: OCx fast enable:
In One Pulse Mode, the edge detection on TIx input set the CEN bit which enables the
counter. Then the comparison between the counter and the compare value makes the
243/519
www.BDTIC.com/ST
General purpose timer (TIMx) UM0306
output toggle. But several clock cycles are needed for these operations and it limits the
minimum delay tDELAY min we can get.
If you want to output a waveform with the minimum delay, you can set the OCxFE bit in the
TIMx_CCMRx register. Then OCxRef (and OCx) is forced in response to the stimulus,
without taking in account the comparison. Its new level is the same as if a compare match
had occurred. OCxFE acts only if the channel is configured in PWM1 or PWM2 mode.
This function can be only used in output compare mode and PWM mode. It does not work in
forced mode.
For example, the OCxREF signal can be connected to the output of a comparator to be used
for current handling. In this case, the ETR must be configured as follow:
1. The External Trigger Prescaler should be kept off: bits ETPS[1:0] of the TIM1_SMCR
register set to ‘00’.
2. The external clock mode 2 must be disabled: bit ECE of the TIM1_SMCR register set to
‘0’.
3. The External Trigger Polarity (ETP) and the External Trigger Filter (ETF) can be
configured according to the user needs.
Figure 105 shows the behavior of the OCxREF signal when the ETRF Input becomes High,
for both values of the enable bit OCxCE. In this example, the timer TIMx is programmed in
PWM mode.
(CCRx)
counter (CNT)
ETRF
OCxREF
(OCxCE=’0’)
OCxREF
(OCxCE=’1’)
OCREF_CLR OCREF_CLR
becomes high still high
244/519
www.BDTIC.com/ST
UM0306 General purpose timer (TIMx)
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.
The Figure 106 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
245/519
www.BDTIC.com/ST
General purpose timer (TIMx) UM0306
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, IC1FP1 mapped on TI1).
● CC2S=’01’ (TIMx_CCMR2 register, IC2FP2 mapped on TI2).
● CC1P=’0’ (TIMx_CCER register, IC1FP1 non-inverted, IC1FP1=TI1).
● CC2P=’0’ (TIMx_CCER register, IC2FP2 non-inverted, IC2FP2=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
Figure 107 gives an example of counter behavior when IC1FP1 polarity is inverted (same
configuration as above except CC1P=’1’).
Figure 107. Example of encoder interface mode with IC1FP1 polarity inverted.
forward jitter backward jitter forward
TI1
TI2
COUNTER
down up down
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
246/519
www.BDTIC.com/ST
UM0306 General purpose timer (TIMx)
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.
247/519
www.BDTIC.com/ST
General purpose timer (TIMx) UM0306
TI1
UG
COUNTER REGISTER 30 31 32 33 34 35 36 00 01 02 03 00 01 02 03
TIF
248/519
www.BDTIC.com/ST
UM0306 General purpose timer (TIMx)
TI1
cnt_en
COUNTER REGISTER 30 31 32 33 34 35 36 37 38
TIF
Write TIF=0
249/519
www.BDTIC.com/ST
General purpose timer (TIMx) UM0306
TI2
cnt_en
COUNTER REGISTER 34 35 36 37 38
TIF
250/519
www.BDTIC.com/ST
UM0306 General purpose timer (TIMx)
TI1
CEN/CNT_EN
ETR
COUNTER REGISTER 34 35 36
TIF
251/519
www.BDTIC.com/ST
General purpose timer (TIMx) UM0306
selection blocks.
TIMER 1 TIMER 2
INPUT
TRIGGER
SELECTION
For example, you can configure Timer 1 to act as a prescaler for Timer 2. Refer to
Figure 112. To do this:
● Configure Timer 1 in master mode so that it outputs a periodic trigger signal on each
update event UEV. If you write MMS=010 in the TIM1_CR2 register, a rising edge is
output on TRGO1 each time an update event is generated.
● To connect the TRGO1 output of Timer 1 to Timer 2, Timer 2 must be configured in
slave mode using ITR1 as internal trigger. You select this through the TS bits in the
TIM2_SMCR register (writing TS=001).
● Then you put the slave mode controller in external clock mode 1 (write SMS=111 in the
TIM2_SMCR register). This causes Timer 2 to be clocked by the rising edge of the
periodic Timer 1 trigger signal (which correspond to the timer 1 counter overflow).
● Finally both timers must be enabled by setting their respective CEN bits (TIMx_CR1
register).
Note: If OCx is selected on Timer 1 as trigger output (MMS=1xx), its rising edge is used to clock
the counter of timer 2.
252/519
www.BDTIC.com/ST
UM0306 General purpose timer (TIMx)
CK_INT
TIMER1-OC1REF
TIMER1-CNT FC FD FE FF 00 01
TIMER 2-TIF
Write TIF=0
In the example in Figure 113, the Timer 2 counter and prescaler are not initialized before
being started. So they start counting from their current value. It is possible to start from a
given value by resetting both timers before starting Timer 1. You can then write any value
you want in the timer counters. The timers can easily be reset by software using the UG bit
in the TIMx_EGR registers.
In the next example, we synchronize Timer 1 and Timer 2. Timer 1 is the master and starts
from 0. Timer 2 is the slave and starts from 0xE7. The prescaler ratio is the same for both
timers. Timer 2 stops when Timer 1 is disabled by writing ‘0’ to the CEN bit in the TIM1_CR1
register:
● Configure Timer 1 master mode to send its Output Compare 1 Reference (OC1REF)
signal as trigger output (MMS=100 in the TIM1_CR2 register).
● Configure the Timer 1 OC1REF waveform (TIM1_CCMR1 register).
● Configure Timer 2 to get the input trigger from Timer 1 (TS=001 in the TIM2_SMCR
register).
● Configure Timer 2 in gated mode (SMS=101 in TIM2_SMCR register).
● Reset Timer 1 by writing ‘1’ in UG bit (TIM1_EGR register).
● Reset Timer 2 by writing ‘1’ in UG bit (TIM2_EGR register).
● Initialize Timer 2 to 0xE7 by writing ‘0xE7’ in the timer 2 counter (TIM2_CNTL).
● Enable Timer 2 by writing ‘1’ in the CEN bit (TIM2_CR1 register).
● Start Timer 1 by writing ‘1’ in the CEN bit (TIM1_CR1 register).
● Stop Timer 1 by writing ‘0’ in the CEN bit (TIM1_CR1 register).
253/519
www.BDTIC.com/ST
General purpose timer (TIMx) UM0306
CK_INT
TIMER1-CEN=cnt_en
TIMER1-cnt_init
TIMER1-CNT 75 00 01 02
TIMER2-CNT AB 00 E7 E8 E9
TIMER2-cnt_init
TIMER2
write CNT
TIMER 2-TIF
Write TIF=0
CK_INT
TIMER1-UEV
TIMER1-CNT FD FE FF 00 01 02
TIMER2-CNT 45 46 47 48
TIMER2-CEN=cnt_en
TIMER 2-TIF
Write TIF=0
254/519
www.BDTIC.com/ST
UM0306 General purpose timer (TIMx)
As in the previous example, you can initialize both counters before starting counting.
Figure 116 shows the behavior with the same configuration as in Figure 115 but in trigger
mode instead of gated mode (SMS=110 in the TIM2_SMCR register).
CK_INT
TIMER1-CEN=cnt_en
TIMER1-cnt_init
TIMER1-CNT 75 00 01 02
TIMER2-CNT CD 00 E7 E8 E9 EA
TIMER2-cnt_init
TIMER2
write CNT
TIMER 2-TIF
Write TIF=0
255/519
www.BDTIC.com/ST
General purpose timer (TIMx) UM0306
counters are aligned, Timer 1 must be configured in Master/Slave mode (slave with respect
to TI1, master with respect to Timer 2):
● Configure Timer 1 master mode to send its Enable as trigger output (MMS=001 in the
TIM1_CR2 register).
● Configure Timer 1 slave mode to get the input trigger from TI1 (TS=100 in the
TIM1_SMCR register).
● Configure Timer 1 in trigger mode (SMS=110 in the TIM1_SMCR register).
● Configure the Timer 1 in Master/Slave mode by writing MSM=’1’ (TIM1_SMCR
register).
● Configure Timer 2 to get the input trigger from Timer 1 (TS=001 in the TIM2_SMCR
register).
● Configure Timer 2 in trigger mode (SMS=110 in the TIM2_SMCR register).
When a rising edge occurs on TI1 (Timer 1), both counters starts counting synchronously on
the internal clock and both TIF flags are set.
Note: In this example both timers are initialized before starting (by setting their respective UG
bits). Both counters starts from 0, but you can easily insert an offset between them by
writing any of the counter registers (TIMx_CNT). You can see that the master/slave mode
insert a delay between CNT_EN and CK_PSC on timer 1.
CK_INT
TIMER 1-TI1
TIMER1-CEN=cnt_en
TIMER 1-ck_psc
TIMER1-CNT 00 01 02 03 04 05 06 07 08 09
TIMER1-TIF
TIMER2-CEN=cnt_en
TIMER 2-ck_psc
TIMER2-CNT 00 01 02 03 04 05 06 07 08 09
TIMER2-TIF
256/519
www.BDTIC.com/ST
UM0306 General purpose timer (TIMx)
rw rw rw rw rw rw rw rw rw rw
257/519
www.BDTIC.com/ST
General purpose timer (TIMx) UM0306
258/519
www.BDTIC.com/ST
UM0306 General purpose timer (TIMx)
rw rw rw rw rw
259/519
www.BDTIC.com/ST
General purpose timer (TIMx) UM0306
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Bit 15 This bit selects whether ETR or ETR is used for trigger operations
0: ETR is non-inverted, active at high level or rising edge.
1: ETR is inverted, active at low level or falling edge.
ECE: External Clock enable.
This bit enables External clock mode 2.
0: External clock mode 2 disabled.
1: External clock mode 2 enabled. The counter is clocked by any active edge on the
ETRF signal.
Bit 14 Note 1: Setting the ECE bit has the same effect as selecting external clock mode 1 with
TRGI connected to ETRF (SMS=111 and TS=111).
Note 2: It is possible to simultaneously use external clock mode 2 with the following
slave modes: reset mode, gated mode and trigger mode. Nevertheless, TRGI must not
be connected to ETRF in this case (TS bits must not be 111).
Note 3: If external clock mode 1 and external clock mode 2 are enabled at the same
time, the external clock input is ETRF.
ETPS: External Trigger Prescaler.
External trigger signal ETRP frequency must be at most 1/4 of CK_INT frequency. A
prescaler can be enabled to reduce ETRP frequency. It is useful when inputting fast
external clocks.
Bits 13:12
00: Prescaler OFF.
01: ETRP frequency divided by 2.
10: ETRP frequency divided by 4.
11: ETRP frequency divided by 8.
260/519
www.BDTIC.com/ST
UM0306 General purpose timer (TIMx)
261/519
www.BDTIC.com/ST
General purpose timer (TIMx) UM0306
262/519
www.BDTIC.com/ST
UM0306 General purpose timer (TIMx)
263/519
www.BDTIC.com/ST
General purpose timer (TIMx) UM0306
264/519
www.BDTIC.com/ST
UM0306 General purpose timer (TIMx)
265/519
www.BDTIC.com/ST
General purpose timer (TIMx) UM0306
266/519
www.BDTIC.com/ST
UM0306 General purpose timer (TIMx)
w w w w w w
267/519
www.BDTIC.com/ST
General purpose timer (TIMx) UM0306
268/519
www.BDTIC.com/ST
UM0306 General purpose timer (TIMx)
269/519
www.BDTIC.com/ST
General purpose timer (TIMx) UM0306
270/519
www.BDTIC.com/ST
UM0306 General purpose timer (TIMx)
271/519
www.BDTIC.com/ST
General purpose timer (TIMx) UM0306
272/519
www.BDTIC.com/ST
UM0306 General purpose timer (TIMx)
273/519
www.BDTIC.com/ST
General purpose timer (TIMx) UM0306
Reserved CC4P CC4E Reserved CC3P CC3E Reserved CC2P CC2E Reserved CC1P CC1E
rw rw rw rw rw rw rw rw
274/519
www.BDTIC.com/ST
UM0306 General purpose timer (TIMx)
Note: The state of the external I/O pins connected to the standard OCx channels depends on the
OCx channel state and the GPIO and AFIO registers.
CNT[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
PSC[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
275/519
www.BDTIC.com/ST
General purpose timer (TIMx) UM0306
ARR[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
CCR1[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
276/519
www.BDTIC.com/ST
UM0306 General purpose timer (TIMx)
CCR2[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
CCR3[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
277/519
www.BDTIC.com/ST
General purpose timer (TIMx) UM0306
CCR4[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
278/519
www.BDTIC.com/ST
UM0306 General purpose timer (TIMx)
rw rw rw rw rw rw rw rw rw rw
DMAB[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
279/519
www.BDTIC.com/ST
General purpose timer (TIMx) UM0306
9
8
7
6
5
4
3
2
1
0
ARPE
UDIS
OPM
CKD CMS
CEN
URS
DIR
TIMx_CR1
00h Reserved [1:0] [1:0]
Reset Value 0 0 0 0 0 0 0 0 0 0
CCDS
TI1S
TIMx_CR2 MMS[2:0]
04h Reserved Reserved
Reset Value 0 0 0 0 0
CC3IE Reserved
MSM
ETPS
ECE
ETP
TIMx_SMCR ETF[3:0] TS[2:0] SMS[2:0]
08h Reserved [1:0]
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
CC4DE
CC3DE
CC2DE
CC1DE
Reserved
Reserved
CC2IE
CC1IE
UDE
TDE
UIE
TIE
TIMx_DIER
0Ch Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0
CC4OF
CC3OF
CC2OF
CC1OF
Reserved
CC4IF
CC3IF
CC2IF
CC1IF
UIF
TIF
TIMx_SR
10h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0
CC4G
CC3G
CC2G
CC1G
UG
TG
TIMx_EGR
14h Reserved
Reset Value 0 0 0 0 0 0
TIMx_CCMR1
OC2CE
OC1CE
OC2PE
OC1PE
OC2FE
OC1FE
OC2M CC2S OC1M CC1S
Output Compare
Reserved [2:0] [1:0] [2:0] [1:0]
mode
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
18h
TIMx_CCMR1 IC2 IC1
CC2S CC1S
Input Capture IC2F[3:0] PSC IC1F[3:0] PSC
Reserved [1:0] [1:0]
mode [1:0] [1:0]
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIMx_CCMR2
OC3CE
OC4PE
OC3PE
OC4FE
OC3FE
O24CE
Reserved
Reserved
CC4P
CC4E
CC3P
CC3E
CC2P
CC2E
CC1P
CC1E
TIMx_CCER
20h Reserved
Reset Value 0 0 0 0 0 0 0 0
TIMx_CNT CNT[15:0]
24h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIMx_PSC PSC[15:0]
28h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIMx_ARR ARR[15:0]
2Ch Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
30h Reserved
TIMx_CCR1 CCR1[15:0]
34h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
280/519
www.BDTIC.com/ST
UM0306 General purpose timer (TIMx)
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
TIMx_CCR2 CCR2[15:0]
38h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIMx_CCR3 CCR3[15:0]
3Ch Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
TIMx_CCR4 CCR4[15:0]
40h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
44h Reserved
TIMx_DMAR DMAB[15:0]
4Ch Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
281/519
www.BDTIC.com/ST
Controller area network (bxCAN) UM0306
14.1 Introduction
The Basic Extended CAN peripheral, named bxCAN, interfaces the CAN network. It
supports the CAN protocols version 2.0A and B. It has been designed to manage a high
number of incoming messages efficiently with a minimum CPU load. It also meets the
priority requirements for transmit messages.
For safety-critical applications, the CAN controller provides all hardware functions for
supporting the CAN Time Triggered Communication option.
282/519
www.BDTIC.com/ST
UM0306 Controller area network (bxCAN)
CAN node 2
CAN node n
MCU
Application
CAN
Controller
CAN CAN
Rx Tx
CAN
Transceiver
CAN CAN
High Low
CAN Bus
14.3.3 Tx mailboxes
Three transmit mailboxes are provided to the software for setting up messages. The
transmission Scheduler decides which mailbox has to be transmitted first.
283/519
www.BDTIC.com/ST
Controller area network (bxCAN) UM0306
Filter Config.
SLEEP
SLAK= 1
INAK = 0
N RQ SL
.I EE
C P
YN .I
NR
.S SL Q
P K EE
EE . AC P
.A
CK
SL .I
E EP NR
SL Q
.A
CK
NORMAL INRQ . ACK INITIALIZATION
SLAK= 0 SLAK= 0
INAK = 0 INAK = 1
INRQ . SYNC . SLEEP
Note: 1 ACK = The wait state during which hardware confirms a request by setting the INAK or
SLAK bits in the CAN_MSR register
2 SYNC = The state during which bxCAN waits until the CAN bus is idle, meaning 11
consecutive recessive bits have been monitored on CANRX
284/519
www.BDTIC.com/ST
UM0306 Controller area network (bxCAN)
285/519
www.BDTIC.com/ST
Controller area network (bxCAN) UM0306
bxCAN
Tx Rx
=1
CANTX CANRX
286/519
www.BDTIC.com/ST
UM0306 Controller area network (bxCAN)
bxCAN
Tx Rx
CANTX CANRX
This mode is provided for self-test functions. To be independent of external events, the CAN
Core ignores acknowledge errors (no dominant bit sampled in the acknowledge slot of a
data / remote frame) in Loop Back Mode. In this mode, the bxCAN performs an internal
feedback from its Tx output to its Rx input. The actual value of the CANRX input pin is
disregarded by the bxCAN. The transmitted messages can be monitored on the CANTX pin.
bxCAN
Tx Rx
=1
CANTX CANRX
287/519
www.BDTIC.com/ST
Controller area network (bxCAN) UM0306
empty state, the software no longer has write access to the mailbox registers. Immediately
after the TXRQ bit has been set, the mailbox enters pending state and waits to become the
highest priority mailbox, see Transmit Priority. As soon as the mailbox has the highest
priority it will be scheduled for transmission. The transmission of the message of the
scheduled mailbox will start (enter transmit state) when the CAN bus becomes idle. Once
the mailbox has been successfully transmitted, it will become empty again. The hardware
indicates a successful transmission by setting the RQCP and TXOK bits in the CAN_TSR
register.
If the transmission fails, the cause is indicated by the ALST bit in the CAN_TSR register in
case of an Arbitration Lost, and/or the TERR bit, in case of transmission error detection.
Transmit priority
By identifier:
When more than one transmit mailbox is pending, the transmission order is given by the
identifier of the message stored in the mailbox. The message with the lowest identifier value
has the highest priority according to the arbitration of the CAN protocol. If the identifier
values are equal, the lower mailbox number will be scheduled first.
By transmit request order:
The transmit mailboxes can be configured as a transmit FIFO by setting the TXFP bit in the
CAN_MCR register. In this mode the priority order is given by the transmit request order.
This mode is very useful for segmented transmission.
Abort
A transmission request can be aborted by the user setting the ABRQ bit in the CAN_TSR
register. In pending or scheduled state, the mailbox is aborted immediately. An abort
request while the mailbox is in transmit state can have two results. If the mailbox is
transmitted successfully the mailbox becomes empty with the TXOK bit set in the
CAN_TSR register. If the transmission fails, the mailbox becomes scheduled, the
transmission is aborted and becomes empty with TXOK cleared. In all cases the mailbox
will become empty again at least at the end of the current transmission.
288/519
www.BDTIC.com/ST
UM0306 Controller area network (bxCAN)
EMPTY
RQCP=X
TXOK=X
TXRQ=1
TME = 1
PENDING
RQCP=0 Mailbox has
TXOK=0 highest priority
ABRQ=1
TME = 0
EMPTY
Transmit succeeded
RQCP=1
TXOK=1
TME = 1
Valid message
A received message is considered as valid when it has been received correctly according to
the CAN protocol (no error until the last but one bit of the EOF field) and It passed through
the identifier filtering successfully, see Section 14.5.4: Identifier filtering.
289/519
www.BDTIC.com/ST
Controller area network (bxCAN) UM0306
PENDING_1
Release FMP=0x01
Mailbox FOVR=0
PENDING_2
FMP=0x10
FOVR=0
PENDING_3
FMP=0x11 Valid Message
FOVR=0 Received
OVERRUN
Release FMP=0x11
Mailbox FOVR=1
RFOM=1
Valid Message
Received
FIFO management
Starting from the empty state, the first valid message received is stored in the FIFO which
becomes pending_1. The hardware signals the event setting the FMP[1:0] bits in the CRFR
register to the value 01b. The message is available in the FIFO output mailbox. The software
reads out the mailbox content and releases it by setting the RFOM bit in the CRFR register.
The FIFO becomes empty again. If a new valid message has been received in the
meantime, the FIFO stays in pending_1 state and the new message is available in the
output mailbox.
If the application does not release the mailbox, the next valid message will be stored in the
FIFO which enters pending_2 state (FMP[1:0] = 10b). The storage process is repeated for
the next valid message putting the FIFO into pending_3 state (FMP[1:0] = 11b). At this
point, the software must release the output mailbox by setting the RFOM bit, so that a
mailbox is free to store the next valid message. Otherwise the next valid message received
will cause a loss of message.
Refer also to Section 14.5.5: Message storage
Overrun
Once the FIFO is in pending_3 state (i.e. the three mailboxes are full) the next valid
message reception will lead to an overrun and a message will be lost. The hardware
290/519
www.BDTIC.com/ST
UM0306 Controller area network (bxCAN)
signals the overrun condition by setting the FOVR bit in the CRFR register. Which message
is lost depends on the configuration of the FIFO:
● If the FIFO lock function is disabled (RFLM bit in the CAN_MCR register cleared) the
last message stored in the FIFO will be overwritten by the new incoming message. In
this case the latest messages will be always available to the application.
● If the FIFO lock function is enabled (RFLM bit in the CAN_MCR register set) the most
recent message will be discarded and the software will have the three oldest messages
in the FIFO available.
Scalable width
To optimize and adapt the filters to the application needs, each filter bank can be scaled
independently. Depending on the filter scale a filter bank provides:
● One 32-bit filter for the STDID[10:0], EXTID[17:0], IDE and RTR bits.
● Two 16-bit filters for the STDID[10:0], RTR, IDE and EXTID[17:15] bits.
Refer to Figure 126.
Furthermore, the filters can be configured in mask mode or in identifier list mode.
Mask mode
In mask mode the identifier registers are associated with mask registers specifying which
bits of the identifier are handled as “must match” or as “don’t care”.
291/519
www.BDTIC.com/ST
Controller area network (bxCAN) UM0306
identifiers. All bits of the incoming identifier must match the bits specified in the filter
registers.
n
Mask CAN_FxR0[31:24] CAN_FxR0[23:16]
ID CAN_FxR1[15:8] CAN_FxR1[7:0]
n+1
Mask CAN_FxR1[31:24] CAN_FxR1[23:16]
FSCx = 0
292/519
www.BDTIC.com/ST
UM0306 Controller area network (bxCAN)
2
1 ID Mask (32-bit) 2 4 ID List (32-bit) 3
3
4 Deactivated 4
3 ID List (16-bit) 7
5 ID Mask (16-bit) 5
6
Deactivated 7 6
5 8 ID Mask (16-bit)
ID List (32-bit) 8 7
8
9 Deactivated 9
6 ID Mask (16-bit) 10 10
10 ID List (16-bit)
11
11 12
9 ID List (32-bit) 11 ID List (32-bit)
12 13
ID=Identifier
293/519
www.BDTIC.com/ST
Controller area network (bxCAN) UM0306
Filter bank
Num Receive FIFO
Identifier 0
0
Identifier 1 Message
Identifier List
Identifier 5
Identifier & Mask
1
Identifier
Mask 2 Filter number stored in the
FMI
Filter Match Index field
within the CAN_RDTxR
Identifier register
4 3
Mask
No Match
Found
Message Discarded
The example above shows the filtering principle of the bxCAN. On reception of a message,
the identifier is compared first with the filters configured in identifier list mode. If there is a
match, the message is stored in the associated FIFO and the index of the matching filter is
stored in the Filter Match Index. As shown in the example, the identifier matches with
Identifier #2 thus the message content and FMI 2 is stored in the FIFO.
If there is no match, the incoming identifier is then compared with the filters configured in
mask mode.
If the identifier does not match any of the identifiers configured in the filters, the message is
discarded by hardware without disturbing the software.
294/519
www.BDTIC.com/ST
UM0306 Controller area network (bxCAN)
Transmit mailbox
The software sets up the message to be transmitted in an empty transmit mailbox. The
status of the transmission is indicated by hardware in the CAN_TSR register.
0 CAN_TIxR
4 CAN_TDTxR
8 CAN_TDLxR
12 CAN_TDHxR
Receive mailbox
When a message has been received, it is available to the software in the FIFO output
mailbox. Once the software has handled the message (e.g. read it) the software must
release the FIFO output mailbox by means of the RFOM bit in the CRFR register to make
the next incoming message available. The filter match index is stored in the MFMI field of
the CAN_RDTxR register. The 16-bit time stamp value is stored in the TIME[15:0] field of
CAN_RDTxR.
0 CAN_RIxR
4 CAN_RDTxR
8 CAN_RDLxR
12 CAN_RDHxR
295/519
www.BDTIC.com/ST
Controller area network (bxCAN) UM0306
ERROR ACTIVE
ERROR PASSIVE
BUS OFF
Bus-Off recovery
The Bus-Off state is reached when TEC is greater than 255, this state is indicated by BOFF
bit in CAN_ESR register. In Bus-Off state, the bxCAN is no longer able to transmit and
receive messages.
Depending on the ABOM bit in the CAN_MCR register bxCAN will recover from Bus-Off
(become error active again) either automatically or on software request. But in both cases
the bxCAN has to wait at least for the recovery sequence specified in the CAN standard
(128 occurrences of 11 consecutive recessive bits monitored on CANRX).
If ABOM is set, the bxCAN will start the recovering sequence automatically after it has
entered Bus-Off state.
If ABOM is cleared, the software must initiate the recovering sequence by requesting
bxCAN to enter and to leave initialization mode.
Note: In initialization mode, bxCAN does not monitor the CANRX signal, therefore it cannot
complete the recovery sequence. To recover, bxCAN must be in normal mode.
296/519
www.BDTIC.com/ST
UM0306 Controller area network (bxCAN)
Its operation may be explained simply by splitting nominal bit time into three segments as
follows:
● Synchronization segment (SYNC_SEG): a bit change is expected to occur within this
time segment. It has a fixed length of one time quantum (1 x tCAN).
● Bit segment 1 (BS1): defines the location of the sample point. It includes the
PROP_SEG and PHASE_SEG1 of the CAN standard. Its duration is programmable
between 1 and 16 time quanta but may be automatically lengthened to compensate for
positive phase drifts due to differences in the frequency of the various nodes of the
network.
● Bit segment 2 (BS2): defines the location of the transmit point. It represents the
PHASE_SEG2 of the CAN standard. Its duration is programmable between 1 and 8
time quanta but may also be automatically shortened to compensate for negative
phase drifts.
The resynchronization Jump Width (SJW) defines an upper bound to the amount of
lengthening or shortening of the bit segments. It is programmable between 1 and 4 time
quanta.
A valid edge is defined as the first transition in a bit time from dominant to recessive bus
level provided the controller itself does not send a recessive bit.
If a valid edge is detected in BS1 instead of SYNC_SEG, BS1 is extended by up to SJW so
that the sample point is delayed.
Conversely, if a valid edge is detected in BS2 instead of SYNC_SEG, BS2 is shortened by
up to SJW so that the transmit point is moved earlier.
As a safeguard against programming errors, the configuration of the Bit Timing Register
(CAN_BTR) is only possible while the device is in STANDBY mode.
Note: For a detailed description of the CAN bit timing and resynchronization mechanism, please
refer to the ISO 11898 standard.
1 x tq tBS1 tBS2
297/519
www.BDTIC.com/ST
Controller area network (bxCAN) UM0306
IDE
r0
RTR
SOF
ACK
Inter-Frame Space
Inter-Frame Space Data Frame (Extended Identifier) or Overload Frame
64 + 8 * N
Std Arbitr. Field Ext Arbitr. Field Ctrl Field Data Field CRC Field Ack Field
2
12 20 6 8*N 16 7
r1
RTR
IDE
r0
SRR
SOF
ACK
Inter-Frame Space
Inter-Frame Space Remote Frame or Overload Frame
44
Arbitration Field Ctrl Field CRC Field Ack Field
2
12 6 16 7
ACK
Data Frame or Inter-Frame Space
Remote Frame Error Frame or Overload Frame Notes:
Error Flag Echo Error Delimiter • 0 <= N <= 8
Flag
6 ≤6 8 • SOF = Start Of Frame
• ID = Identifier
• RTR = Remote Transmission Request
Data Frame or • IDE = Identifier Extension Bit
Any Frame Inter-Frame Space Remote Frame • r0 = Reserved Bit
Suspend • DLC = Data Length Code
Intermission Transmission Bus Idle
3 • CRC = Cyclic Redundancy Code
8
• Error flag: 6 dominant bits if node is error
14.6 Interrupts
Four interrupt vectors are dedicated to bxCAN. Each interrupt source can be independently
enabled or disabled by means of the CAN Interrupt Enable Register (CAN_IER).
298/519
www.BDTIC.com/ST
UM0306 Controller area network (bxCAN)
FMPIE0
FMP0
& FIFO 0
INTERRUPT
FFIE0
CAN_RF0R FULL0
& +
FOVIE0
FOVR0
&
FMPIE1
FMP1
& FIFO 1
INTERRUPT
FFIE1
CAN_RF1R FULL1
& +
FOVIE1
FOVR1
&
ERRIE
EWGIE
EWGF &
EPVIE
CAN_ESR EPVF & &
BOFIE
+
ERRI
BOFF & CAN_MSR STATUS CHANGE
ERROR
LECIE
1≤LEC≤6 & INTERRUPT
WKUIE
WKUI
&
CAN_MSR
SLKIE
SLAKI
&
299/519
www.BDTIC.com/ST
Controller area network (bxCAN) UM0306
● The error and status change interrupt can be generated by the following events:
– Error condition, for more details on error conditions please refer to the CAN Error
Status register (CAN_ESR).
– Wake-up condition, SOF monitored on the CAN Rx signal.
– Entry into SLEEP mode.
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved DBF
rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
RE
Reserved TTCM ABOM AWUM NART RFLM TXFP SLEEP INRQ
SET
rs rw rw rw rw rw rw rw rw
300/519
www.BDTIC.com/ST
UM0306 Controller area network (bxCAN)
301/519
www.BDTIC.com/ST
Controller area network (bxCAN) UM0306
302/519
www.BDTIC.com/ST
UM0306 Controller area network (bxCAN)
Reserved
rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved RX SAMP RXM TXM Reserved SLAKI WKUI ERRI SLAK INAK
303/519
www.BDTIC.com/ST
Controller area network (bxCAN) UM0306
304/519
www.BDTIC.com/ST
UM0306 Controller area network (bxCAN)
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
305/519
www.BDTIC.com/ST
Controller area network (bxCAN) UM0306
306/519
www.BDTIC.com/ST
UM0306 Controller area network (bxCAN)
307/519
www.BDTIC.com/ST
Controller area network (bxCAN) UM0306
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rs rc_w1 rc_w1 r r
308/519
www.BDTIC.com/ST
UM0306 Controller area network (bxCAN)
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rs rc_w1 rc_w1 r r
309/519
www.BDTIC.com/ST
Controller area network (bxCAN) UM0306
rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw rw rw rw rw rw rw rw rw rw rw rw
310/519
www.BDTIC.com/ST
UM0306 Controller area network (bxCAN)
311/519
www.BDTIC.com/ST
Controller area network (bxCAN) UM0306
REC[7:0] TEC[7:0]
r r r r r r r r r r r r r r r r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw rw rw r r r
312/519
www.BDTIC.com/ST
UM0306 Controller area network (bxCAN)
rw rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw rw rw rw rw rw rw rw rw rw rw
313/519
www.BDTIC.com/ST
Controller area network (bxCAN) UM0306
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
STID[10:0] EXID[17:13]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
314/519
www.BDTIC.com/ST
UM0306 Controller area network (bxCAN)
Mailbox data length control and time stamp register (CAN_TDTxR) (x=0..2)
All bits of this register are write protected when the mailbox is not in empty state.
Address Offsets: 184h, 194h, 1A4h
Reset Value: xxh
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
TIME[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw rw rw rw rw
315/519
www.BDTIC.com/ST
Controller area network (bxCAN) UM0306
DATA3[7:0] DATA2[7:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
DATA1[7:0] DATA0[7:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
DATA7[7:0] DATA6[7:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
DATA5[7:0] DATA4[7:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
316/519
www.BDTIC.com/ST
UM0306 Controller area network (bxCAN)
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
STID[10:0] EXID[17:13]
r r r r r r r r r r r r r r r r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
r r r r r r r r r r r r r r r
317/519
www.BDTIC.com/ST
Controller area network (bxCAN) UM0306
Receive FIFO mailbox data length control and time stamp register
(CAN_RDTxR) (x=0..1)
Address Offsets: 1B4h, 1C4h
Reset Value: xxh
Note: All RX registers are write protected.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
TIME[15:0]
r r r r r r r r r r r r r r r r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
r r r r r r r r r r r r
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
DATA3[7:0] DATA2[7:0]
r r r r r r r r r r r r r r r r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
DATA1[7:0] DATA0[7:0]
r r r r r r r r r r r r r r r r
318/519
www.BDTIC.com/ST
UM0306 Controller area network (bxCAN)
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
DATA7[7:0] DATA6[7:0]
r r r r r r r r r r r r r r r r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
DATA5[7:0] DATA4[7:0]
r r r r r r r r r r r r r r r r
319/519
www.BDTIC.com/ST
Controller area network (bxCAN) UM0306
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved FINIT
rw
320/519
www.BDTIC.com/ST
UM0306 Controller area network (bxCAN)
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved FBM13 FBM12 FBM11 FBM10 FBM9 FBM8 FBM7 FBM6 FBM5 FBM4 FBM3 FBM2 FBM1 FBM0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Note: Please refer to Figure 126: Filter bank scale configuration - register organization on
page 292
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved FSC13 FSC12 FSC11 FSC10 FSC9 FSC8 FSC7 FSC6 FSC5 FSC4 FSC3 FSC2 FSC1 FSC0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw
Note: Please refer to Figure 126: Filter bank scale configuration - register organization on
page 292
321/519
www.BDTIC.com/ST
Controller area network (bxCAN) UM0306
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved FFA13 FFA12 FFA11 FFA10 FFA9 FFA8 FFA7 FFA6 FFA5 FFA4 FFA3 FFA2 FFA1 FFA0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw
322/519
www.BDTIC.com/ST
UM0306 Controller area network (bxCAN)
Note: There are 14 filter banks, x=0..13. Each filter bank x is composed of two 32-bit registers,
CAN_FxR[1:0].
This register can only be modified when the FACTx bit of the CAN_FAxR register is cleared
or when the FINIT bit of the CAN_FMR register is set.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
FB31 FB30 FB29 FB28 FB27 FB26 FB25 FB24 FB23 FB22 FB21 FB20 FB19 FB18 FB17 FB16
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
FB15 FB14 FB13 FB12 FB11 FB10 FB9 FB8 FB7 FB6 FB5 FB4 FB3 FB2 FB1 FB0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
In all configurations:
FB[31:0] Filter Bits
Identifier
Each bit of the register specifies the level of the corresponding bit of the expected
identifier.
0: Dominant bit is expected
1: Recessive bit is expected
Bits 31:0
Mask
Each bit of the register specifies whether the bit of the associated identifier
register must match with the corresponding bit of the expected identifier or not.
0: Don’t care, the bit is not used for the comparison
1: Must match, the bit of the incoming identifier must have the same level has
specified in the corresponding identifier register of the filter.
Note: Depending on the scale and mode configuration of the filter the function of each register can
differ. For the filter mapping, functions description and mask registers association, refer to
Section 14.5.4: Identifier filtering on page 291.
A Mask/Identifier register in mask mode has the same bit mapping as in identifier list
mode.
For the register mapping/addresses of the filter banks please refer to the Table 44 on
page 324.
323/519
www.BDTIC.com/ST
Controller area network (bxCAN) UM0306
9
8
7
6
5
4
3
2
1
0
SLEEP
AWUM
ABOM
TTCM
RFLM
NART
TXFP
INRQ
CAN_MCR
000h Reserved
Reset Value 0 0 0 0 0 0 1 0
SLAKI
SAMP
WKUI
SLAK
ERRI
INAK
RXM
TXM
RX
CAN_MSR
004h Reserved Reserved
RQCP2
RQCP1
RQCP0
ABRQ2
ABRQ1
ABRQ0
TERR2
TXOK2
TERR1
TXOK1
TERR0
TXOK0
ALST2
ALST1
ALST0
CAN_TSR LOW[2:0] TME[2:0]
008h Reserved Reserved Reserved
Reset Value 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
FMP0[1:0]
RFOM0
FOVR0
FULL0
Reserved
CAN_RF0R
00Ch Reserved
Reset Value 0 0 0 0 0
FMP1[1:0]
RFOM1
FOVR1
FULL1
Reserved
CAN_RF1R
010h Reserved
Reset Value 0 0 0 0 0
FMPIE1
FMPIE0
FOVIE1
FOVIE0
EWGIE
WKUIE
ERRIE
TMEIE
BOFIE
Reserved
EPVIE
LECIE
SLKIE
FFIE1
FFIE0
CAN_IER
014h Reserved Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0
LEC[2:0]
EWGF
Reserved
BOFF
EPVF
CAN_ESR REC[7:0] TEC[7:0]
018h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
SJW[1:0]
Reserved
LBKM
CLK8
SILM
Reset Value 0 0 0 0 0 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0
020h-17Fh Reserved
TXRQ
RTR
IDE
Reset Value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x 0
TGT
Reset Value x x x x x x x x x x x x x x x x x x x x x
Reset Value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
Reset Value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
TXRQ
RTR
IDE
Reset Value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x 0
324/519
www.BDTIC.com/ST
UM0306 Controller area network (bxCAN)
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
TGT
CAN_TDT1R TIME[15:0] DLC[3:0]
194h Reserved Reserved
Reset Value x x x x x x x x x x x x x x x x x x x x x
Reset Value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
Reset Value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
TXRQ
RTR
IDE
CAN_TI2R STID[10:0] EXID[17:0]
1A0h
Reset Value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x 0
TGT
CAN_TDT2R TIME[15:0] DLC[3:0]
1A4h Reserved Reserved
Reset Value x x x x x x x x x x x x x x x x x x x x x
Reset Value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
Reset Value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
Reserved
RTR
IDE
CAN_RI0R STID[10:0] EXID[17:0]
1B0h
Reset Value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
Reset Value x x x x x x x x x x x x x x x x x x x x x x x x x x x x
Reset Value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
Reset Value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
Reserved
RTR
IDE
Reset Value x x x x x x x x x x x x x x x x x x x x x x x x x x x x
Reset Value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
Reset Value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
1D0h-1FFh Reserved
325/519
www.BDTIC.com/ST
Controller area network (bxCAN) UM0306
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 FINIT
CAN_FMR
200h Reserved
Reset Value 1
CAN_FM0R FBM[13:0]
204h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0
208h Reserved
CAN_FS0R FSC[13:0]
20Ch Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0
210h Reserved
CAN_FFA0R FFA[13:0]
214h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0
218h Reserved
CAN_FA0R FACT[13:0]
21Ch Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0
220h Reserved
224-23Fh Reserved
CAN_F0R0 FB[31:0]
240h
Reset Value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
CAN_F0R1 FB[31:0]
244h
Reset Value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
CAN_F1R0 FB[31:0]
248h
Reset Value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
CAN_F1R1 FB[31:0]
24Ch
Reset Value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
. . .
. . .
. . .
. . .
CAN_F13R0 FB[31:0]
2A8h
Reset Value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
CAN_F13R1 FB[31:0]
2ACh
Reset Value x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
326/519
www.BDTIC.com/ST
UM0306 Inter-integrated circuit (I2C) interface
15.1 Introduction
I2C (Inter-Integrated Circuit) Bus Interface serves as an interface between the
microcontroller and the serial I2C bus. It provides multi-master capability, and controls all I2C
bus-specific sequencing, protocol, arbitration and timing. It supports standard and fast
speed modes. It is also SMBus 2.0 compatible.
It may be used for a variety of purposes, including CRC generation and verification, SMBus
(System Management Bus) and PMBus (Power Management Bus).
Depending on specific device implementation DMA capability can be available for reduced
CPU overload.
327/519
www.BDTIC.com/ST
Inter-integrated circuit (I2C) interface UM0306
Mode selection
The interface can operate in one of the four following modes:
● Slave transmitter
● Slave receiver
● Master transmitter
● Master receiver
By default, it operates in slave mode. The interface automatically switches from slave to
master, after it generates a START condition and from master to slave, if an arbitration loss
or a STOP generation occurs, allowing Multi-Master capability.
Communication flow
In Master mode, the I2C interface initiates a data transfer and generates the clock signal. A
serial data transfer always begins with a start condition and ends with a stop condition. Both
start and stop conditions are generated in master mode by software.
In Slave mode, the interface is capable of recognizing its own addresses (7 or 10-bit), and
the General Call address. The General Call address detection may be enabled or disabled
by software.
Data and addresses are transferred as 8-bit bytes, MSB first. The first byte(s) following the
start condition contain the address (one in 7-bit mode, two in 10-bit mode). The address is
always transmitted in Master mode.
A 9th clock pulse follows the 8 clock cycles of a byte transfer, during which the receiver must
send an acknowledge bit to the transmitter. Refer to the following figure.
328/519
www.BDTIC.com/ST
UM0306 Inter-integrated circuit (I2C) interface
SDA
MSB ACK
SCL
1 2 8 9
START STOP
CONDITION CONDITION
Acknowledge may be enabled or disabled by software. The I2C interface addresses (dual
addressing 7-bit/ 10-bit and/or general call address) can be selected by software.
The Block Diagram of the I2C interface is shown in Figure 134.
329/519
www.BDTIC.com/ST
Inter-integrated circuit (I2C) interface UM0306
DATA REGISTER
DATA
SDA CONTROL DATA SHIFT REGISTER
CLOCK CONTROL
REGISTER (CCR)
CONTROL REGISTERS
(CR1&CR2)
CONTROL
STATUS REGISTERS LOGIC
(SR1&SR2)
SMBALERT
Note: SMBALERT is an optional signal in SMBus mode. This signal is not applicable if
SMBus is disabled.
330/519
www.BDTIC.com/ST
UM0306 Inter-integrated circuit (I2C) interface
331/519
www.BDTIC.com/ST
Inter-integrated circuit (I2C) interface UM0306
Slave transmitter
Following the address reception and after clearing ADDR, the slave sends bytes from the
DR register to the SDA line via the internal shift register.
The slave stretches SCL low until ADDR is cleared and DR filled with the data to be sent
(see Figure 135 Transfer sequencing EV1 EV3).
When the acknowledge pulse is received:
● The TxE bit is set by hardware with an interrupt if the ITEVFEN and the ITBUFEN bits
are set.
If TxE is set and a data was not written in the DR register before the end of the last data
transmission, the BTF bit is set and the interface waits for a write in the DR register,
stretching SCL low.
332/519
www.BDTIC.com/ST
UM0306 Inter-integrated circuit (I2C) interface
Slave receiver
Following the address reception and after clearing ADDR, the slave receives bytes from the
SDA line into the DR register via the internal shift register. After each byte the interface
generates in sequence:
● An acknowledge pulse if the ACK bit is set
● The RxNE bit is set by hardware and an interrupt is generated if the ITEVFEN and
ITBUFEN bit is set.
If RxNE is set and the data in the DR register is not read before the end of the last data
reception, the BTF bit is set and the interface waits for a read to the DR register, stretching
SCL low (see Figure 136 Transfer sequencing).
333/519
www.BDTIC.com/ST
Inter-integrated circuit (I2C) interface UM0306
Start condition
Setting the START bit while the BUSY bit is cleared causes the interface to generate a Start
condition and switch to Master mode (M/SL bit set).
Once the Start condition is sent:
● The SB bit is set by hardware and an interrupt is generated if the ITEVFEN bit is set.
Then the master waits for a read of the SR1 register followed by a write in the DR register
with the Slave address (see Figure 137 & Figure 138 Transfer sequencing EV5).
334/519
www.BDTIC.com/ST
UM0306 Inter-integrated circuit (I2C) interface
The master can decide to enter Transmitter or Receiver mode depending on the LSB of the
slave address sent.
● In 7-bit addressing mode,
– To enter Transmitter mode, a master sends the slave address with LSB reset.
– To enter Receiver mode, a master sends the slave address with LSB set.
● In 10-bit addressing mode,
– To enter Transmitter mode, a master sends the header (11110xx0) and then the
slave address with LSB reset, (where xx denotes the two most significant bits of
the address).
– To enter Receiver mode, a master sends the header (11110xx0) and then the
slave address with LSB reset. Then it should send a repeated Start condition
followed by the header (11110xx1), (where xx denotes the two most significant bits
of the address).
The TRA bit indicates whether the master is in Receiver or Transmitter mode.
Master transmitter
Following the address transmission and after clearing ADDR, the master sends bytes from
the DR register to the SDA line via the internal shift register.
The master waits until TxE is cleared, (see Figure 137 Transfer sequencing EV8).
When the acknowledge pulse is received:
● The TxE bit is set by hardware and an interrupt is generated if the ITEVFEN and
ITBUFEN bits are set.
If TxE is set and a data byte was not written in the DR register before the end of the last data
transmission, BTF is set and the interface waits until BTF is cleared.
335/519
www.BDTIC.com/ST
Inter-integrated circuit (I2C) interface UM0306
EV5: SB=1, cleared by reading SR1 register followed by writing DR register with Address.
EV6: ADDR=1, cleared by reading SR1 register followed by reading SR2.
EV8_1: TxE=1 shift register empty
EV8: TxE=1 cleared by writing DR register.
EV8_2: TxE=1, BTF = 1 cleared by HW by stop condition
EV9: ADD10=1, cleared by reading SR1 register followed by writing DR register.
336/519
www.BDTIC.com/ST
UM0306 Inter-integrated circuit (I2C) interface
Master receiver
Following the address transmission and after clearing ADDR, the I2C interface enters
Master Receiver mode. In this mode the interface receives bytes from the SDA line into the
DR register via the internal shift register. After each byte the interface generates in
sequence:
● An acknowledge pulse if the ACK bit is set
● The RxNE bit is set and an interrupt is generated if the ITEVFEN and ITBUFEN bits are
set (see Figure 138 Transfer sequencing EV7).
If the RxNE bit is set and the data in the DR register is not read before the end of the last
data reception, the BTF bit is set by hardware and the interface waits for a read in the DR
register.
337/519
www.BDTIC.com/ST
Inter-integrated circuit (I2C) interface UM0306
338/519
www.BDTIC.com/ST
UM0306 Inter-integrated circuit (I2C) interface
15.4.5 SMBus
Introduction
The System Management Bus (SMBus) is a two-wire interface through which various
devices can communicate with each other and with the rest of the system. It is based on I2C
principles of operation. SMBus provides a control bus for system and power management
related tasks. A system may use SMBus to pass messages to and from devices instead of
toggling individual control lines.
The System Management Bus Specification refers to three types of devices. A slave is a
device that is receiving or responding to a command. A master is a device that issues
commands, generates the clocks, and terminates the transfer. A host is a specialized master
that provides the main interface to the system's CPU. A host must be a master-slave and
must support the SMBus host notify protocol. Only one host is allowed in a system.
339/519
www.BDTIC.com/ST
Inter-integrated circuit (I2C) interface UM0306
Device identification
Any device that exists on the System Management Bus as a slave has a unique address
called the Slave Address. For the list of reserved slave addresses, refer to the SMBus
specification ver. 2.0 (http://smbus.org/specs/).
Bus protocols
The SMBus specification supports up to 9 bus protocols. For more details of these protocols
and SMBus address types, refer to SMBus specification ver. 2.0 (http://smbus.org/specs/).
These protocols should be implemented by the user software.
340/519
www.BDTIC.com/ST
UM0306 Inter-integrated circuit (I2C) interface
A slave-only device can signal the host through SMBALERT that it wants to talk by setting
ALERT bit in I2C_CR1 register. The host processes the interrupt and simultaneously
accesses all SMBALERT devices through the Alert Response Address (known as ARA
having a value 0001 100X). Only the device(s) which pulled SMBALERT low will
acknowledge the Alert Response Address. This status is identified using SMBALERT Status
flag in I2C_SR1 register. The host performs a modified Receive Byte operation. The 7 bit
device address provided by the slave transmit device is placed in the 7 most significant bits
of the byte. The eighth bit can be a zero or one.
If more than one device pulls SMBALERT low, the highest priority (lowest address) device
will win communication rights via standard arbitration during the slave address transfer. After
acknowledging the slave address the device must disengage its SMBALERT pull-down. If
the host still sees SMBALERT low when the message transfer is complete, it knows to read
the ARA again.
A host which does not implement the SMBALERT signal may periodically access the ARA.
For more details on SMBus Alert mode, refer to SMBus specification ver. 2.0
(http://smbus.org/specs/).
Time-out error
There are differences in the timing specifications between I2C and SMBus.
SMBus defines a clock low time-out, TIMEOUT of 35 ms. Also SMBus specifies TLOW:
SEXT as the cumulative clock low extend time for a slave device. SMBus specifies TLOW:
MEXT as the cumulative clock low extend time for a master device. For more details on
these time-outs, refer to SMBus specification ver. 2.0 (http://smbus.org/specs/).
The status flag Timeout or Tlow Error in I2C_SR1 shows the status of this feature.
341/519
www.BDTIC.com/ST
Inter-integrated circuit (I2C) interface UM0306
corresponding DMA channel is reached, the DMA controller sends an End of Transfer EOT
signal to the I2C interface and generates a Transfer Complete interrupt if enabled:
● Master Transmitter: In the interrupt routine after the EOT interrupt, disable DMA
requests then wait for a BTF event before programming the STOP condition.
● Master Receiver: The DMA controller sends a hardware signal EOT_1 corresponding
to the (number of bytes -1). If, in the I2C_CR2 register, the LAST bit is set, I2C
automatically sends a NACK after the next byte following EOT_1. The user can
generate a Stop condition in the DMA Transfer Complete interrupt routine if enabled.
Note: Please refer to the product specs for availability DMA controller. If DMA is not available in
the product, the user should use I2C as explained in section 1.4. In the I2C ISR, the user can
clear TxE/ RxNE flags to achieve continuous communication.
342/519
www.BDTIC.com/ST
UM0306 Inter-integrated circuit (I2C) interface
343/519
www.BDTIC.com/ST
Inter-integrated circuit (I2C) interface UM0306
Note: 1 SB, ADDR, ADD10, STOPF, BTF, RxNE and TxE are logically ORed on the same interrupt
channel.
2 BERR, ARLO, AF, OVR, PECERR, TIMEOUT and SMBALERT are logically ORed on the
same interrupt channel.
344/519
www.BDTIC.com/ST
UM0306 Inter-integrated circuit (I2C) interface
ITEVFEN
SB
ADDR
ADD10
STOPF
it_event
BTF
TxE
ITBUFEN
RxNE
ITERREN
BERR
ARLO
it_error
AF
OVR
PECERR
TIMEOUT
SMBAlert
SW NO EN EN SMB SM
Res. ALERT PEC POS ACK STOP START ENGC Res. PE
RST STRETCH PEC ARP TYPE BUS
rw rw rw rw rw rw rw rw rw rw rw rw rw rw
345/519
www.BDTIC.com/ST
Inter-integrated circuit (I2C) interface UM0306
Note:
In Master mode, the BTF bit of the I2C_SR1 register must be cleared when
STOP is requested.
346/519
www.BDTIC.com/ST
UM0306 Inter-integrated circuit (I2C) interface
347/519
www.BDTIC.com/ST
Inter-integrated circuit (I2C) interface UM0306
rw rw rw rw rw rw rw rw rw rw rw
348/519
www.BDTIC.com/ST
UM0306 Inter-integrated circuit (I2C) interface
ADD
Res. Reserved ADD[9:8] ADD[7:1] ADD0
MODE
rw rw rw rw rw rw rw rw rw rw rw rw
rw rw rw rw rw rw rw rw
349/519
www.BDTIC.com/ST
Inter-integrated circuit (I2C) interface UM0306
Reserved DR[7:0]
rw rw rw rw rw rw rw rw
350/519
www.BDTIC.com/ST
UM0306 Inter-integrated circuit (I2C) interface
rc rc rc rc rc rc rc r r r r r r r
351/519
www.BDTIC.com/ST
Inter-integrated circuit (I2C) interface UM0306
352/519
www.BDTIC.com/ST
UM0306 Inter-integrated circuit (I2C) interface
353/519
www.BDTIC.com/ST
Inter-integrated circuit (I2C) interface UM0306
SMB
SMB GEN
PEC[7:0] DUALF DEF Res. TRA BUSY MSL
HOST CALL
AULT
r r r r r r r r r r r r r r r
354/519
www.BDTIC.com/ST
UM0306 Inter-integrated circuit (I2C) interface
355/519
www.BDTIC.com/ST
Inter-integrated circuit (I2C) interface UM0306
rw rw rw rw rw rw rw rw rw rw rw rw rw rw
356/519
www.BDTIC.com/ST
UM0306 Inter-integrated circuit (I2C) interface
Reserved TRISE[5:0]
rw rw rw rw rw rw
357/519
www.BDTIC.com/ST
Inter-integrated circuit (I2C) interface UM0306
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
NOSTRETCH
SMBTYPE
SWRST
SMBUS
ENARP
ENPEC
ALERT
START
Reserved
Reserved
ENGC
STOP
POS
PEC
ACK
PE
I2C_CR1
00h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ITERREN
ITBUFEN
ITEVTEN
DMAEN
Reserved
LAST
I2C_CR2 FREQ[5:0]
04h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0
ADDMODE
Reserved
ADD0
I2C_OAR1 ADD[9:8] ADD[7:1]
08h Reserved Reserved
Reset Value 0 1 0 0 0 0 0 0 0 0 0 0
ENDUAL
I2C_OAR2 ADD2[7:1]
0Ch Reserved
Reset Value 0 0 0 0 0 0 0 0
I2C_DR DR[7:0]
10h Reserved
Reset Value 0 0 0 0 0 0 0 0
SMBALERT
TIMEOUT
PECERR
STOPF
ADD10
Reserved
Reserved
ADDR
BERR
ARLO
RxNE
OVR
BTF
TxE
SB
AF
I2C_SR1
14h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0
SMBDEFAULT
SMBHOST
GENCALL
DUALF
Reserved
BUSY
MSL
TRA
I2C_SR2 PEC[7:0]
18h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Reserved
DUTY
F/S
I2C_CCR CCR[11:0]
1Ch Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0
I2C_TRISE TRISE[5:0]
20h Reserved
Reset Value 0 0 0 0 1 0
358/519
www.BDTIC.com/ST
UM0306 Serial peripheral interface (SPI)
16.1 Introduction
The Serial Peripheral Interface (SPI) allows half/ full-duplex, synchronous, serial
communication with external devices. The interface can be configured as the master and in
this case it provides the communication clock (SCK) to the external slave device. The
interface is also capable of operating in multi-master configuration.
It may be used for a variety of purposes, including Simplex synchronous transfers on 2 lines
with a possible bidirectional data line or reliable communication using CRC checking.
359/519
www.BDTIC.com/ST
Serial peripheral interface (SPI) UM0306
READ
RX BUFFER
SPI_CR2
MOSI
TXE RXNE ERR TXDM RXDM
0 0 SSOE AEN AEN
SHIFT REGISTER IE IE IE
MISO
LsbFirst SPI_SR
CRC
BSY OVR MOD ERR 0 0 TXE RXNE
TX BUFFER F
WRITE
0
COMMUNICATION
CONTROL 1
SCK
BR[2:0]
BAUD RATE GENERATOR
NSS
360/519
www.BDTIC.com/ST
UM0306 Serial peripheral interface (SPI)
MASTER SLAVE
MSBit LSBit MSBit LSBit
MISO MISO
8-BIT SHIFT REGISTER 8-BIT SHIFT REGISTER
MOSI MOSI
SPI
SCK SCK
CLOCK
GENERATOR
NSS NSS
VDD
Not used if NSS is managed
by software
Note: Here, the NSS pin is configured as input
The MOSI pins are connected together and the MISO pins are connected together. In this
way data is transferred serially between master and slave (most significant bit first).
The communication is always initiated by the master. When the master device transmits
data to a slave device via MOSI pin, the slave device responds the MISO pin. This implies
full duplex communication with both data out and data in synchronized with the same clock
signal (which is provided by the master device via the SCK pin).
SSI bit 1
NSS Internal
NSS external pin 0
361/519
www.BDTIC.com/ST
Serial peripheral interface (SPI) UM0306
If CPHA (clock phase) bit is set, the second edge on the SCK pin (falling edge if the CPOL
bit is reset, rising edge if the CPOL bit is set) is the MSBit capture strobe. Data is latched on
the occurrence of the first clock transition. If CPHA bit is reset, the first edge on the SCK pin
(falling edge if CPOL bit is set, rising edge if CPOL bit is reset) is the MSBit capture strobe.
Data is latched on the occurrence of the second clock transition.
The combination of the CPOL clock polarity and CPHA (clock phase) bits selects the data
capture clock edge.
Figure 143, shows an SPI transfer with the four combinations of the CPHA and CPOL bits.
The diagram may be interpreted as a master or slave timing diagram where the SCK pin, the
MISO pin, the MOSI pin are directly connected between the master and the slave device.
Note: 1 Prior to changing the CPOL/CPHA bits the SPI must be disabled by resetting the SPE bit.
2 Master and slave must be programmed with the same timing mode.
3 The idle state of SCK must correspond to the polarity selected in the SPI_CR1 register (by
pulling up SCK if CPOL=1 or pulling down SCK if CPOL=0).
4 The Data Frame Format (8- or 16-bit) is selected through the DFF bit in SPI_CR1 register,
and determines the data length during transmission/reception.
362/519
www.BDTIC.com/ST
UM0306 Serial peripheral interface (SPI)
CPHA =1
CPOL = 1
CPOL = 0
MISO LSBit
MSBit
(from master)
8 or 16 bits depending on Data Frame Format (see SPI_CR1)
MOSI
MSBit LSBit
(from slave)
NSS
(to slave)
CAPTURE STROBE
CPHA =0
CPOL = 1
CPOL = 0
NSS
(to slave)
CAPTURE STROBE
Note: These timings are shown with the LSBFIRST bit reset in the SPI_CR1 register.
363/519
www.BDTIC.com/ST
Serial peripheral interface (SPI) UM0306
Procedure
1. Set the DFF bit to define 8- or 16-bit data frame format
2. Select the CPOL and CPHA bits to define one of the four relationships between the
data transfer and the serial clock (see Figure 143). For correct data transfer, the CPOL
and CPHA bits must be configured the same way in the slave device and the master
device.
3. The frame format (MSB-first or LSB-first depending on the value of the LSBFIRST bit in
the SPI_CR1 register) must be same as the master device.
4. In Hardware mode (refer to Slave select (NSS) pin management on page 361), the
NSS pin must be connected to a low level signal during the complete byte transmit
sequence. In Software mode, set the SSM bit and clear the SSI bit in the SPI_CR1
register.
5. Clear the MSTR bit and set the SPE bit (both in the SPI_CR1 register) to assign the
pins to alternate functions.
In this configuration the MOSI pin is a data input and the MISO pin is a data output.
Transmit sequence
The data byte is parallel loaded into the Tx buffer during a write cycle.
The transmit sequence begins when the slave device receives the clock signal and the most
significant bit of the data on its MOSI pin. The remaining bits (the 7 bits in 8-bit data frame
format, and the 15 bits in 16-bit data frame format) are loaded into the shift-register. The
TXE flag in the SPI_SR register is set on the transfer of data from the Tx Buffer to the shift
register and an interrupt will be generated if TXEIE bit in the SPI_CR2 register is set.
For the receiver, when data transfer is complete:
● The Data in shift register is transferred to Rx Buffer and the RXNE flag (SPI_SR
register) is set
● An Interrupt is generated if the RXEIE bit is set in the SPI_CR2 register.
After the last sampling clock edge the RXNE bit is set, a copy of the data byte received in
the shift register is moved to the Rx buffer. When the SPI_DR register is read, the SPI
peripheral returns this buffered value.
Clearing of the RXNE bit is performed by reading the SPI_DR register.
Procedure
1. Select the BR[2:0] bits to define the serial clock baud rate (see SPI_CR1 register).
2. Select the CPOL and CPHA bits to define one of the four relationships between the
data transfer and the serial clock (see Figure 143).
3. Set the DFF bit to define 8- or 16-bit data frame format
4. Configure the LSBFIRST bit in the SPI_CR1 register to define the frame format
5. If the NSS pin is required in input mode, in Hardware mode, connect the NSS pin to a
high level signal during the complete byte transmit sequence. In software mode, set the
364/519
www.BDTIC.com/ST
UM0306 Serial peripheral interface (SPI)
Transmit sequence
The transmit sequence begins when a byte is written in the Tx Buffer.
The data byte is parallel loaded into the shift register (from the internal bus) during first bit
transmission and then shifted out serially to the MOSI pin MSB first or LSB first depending
on the LSBFIRST bit in the SPI_CR1 register. The TXE flag will be set on the transfer of
data from the Tx Buffer to the shift register and an interrupt will be generated if TXEIE bit in
the SPI_CR2 register is set.
For the receiver, when data transfer is complete
● The Data in shift register is transferred to RX Buffer and the RXNE flag is set.
● An Interrupt is generated if the RXEIE bit is set in the SPI_CR2 register
At the last sampling clock edge the RXNE bit is set, a copy of the data byte received in the
shift register is moved to the Rx buffer. When the SPI_DR register is read, the SPI
peripheral returns this buffered value.
Clearing the RXNE bit is performed by reading the SPI_DR register.
A continuous transmit stream can be maintained if the next data to be transmitted is put in
the Tx buffer once the transmission is started. Note that TXE flag should be ‘1’ before an
attempt to write the Tx buffer.
Receive-only mode
To start the communication in receive-only mode, it is necessary to enable the SPI. In the
master mode, the communication starts immediately and will stop when the SPE bit is reset
365/519
www.BDTIC.com/ST
Serial peripheral interface (SPI) UM0306
and the current reception terminates. In slave mode, the SPI will continue to receive as long
as the NSS is pulled down (or the SSI bit is reset) and the SCK is running.
Note: The SPI can be used in Tx-only mode when the RXONLY bit in the SPI_CR1 register is
reset, the RX pin (MISO in master or MOSI in slave) can be used as GPIO. In this case,
when the data register is read, it does not contain the received value.
Busy flag
This flag indicates the state of the communication layer of the SPI. When it is set, it indicates
that the SPI is busy communicating and/or there is a valid data byte in the Tx buffer waiting
to be transmitted. The purpose of this flag is to indicate if there is any communication
ongoing on the SPI bus or not. This flag will be set as soon as:
1. Data is written in the SPI_DR register in master mode
2. The SCK clock is present in slave mode
The BUSY flag will reset as soon as a byte is transmitted/ received. This flag is set and reset
by hardware. This flag can be monitored to avoid write collision errors. Writing to this flag
has no effect. This flag has meaning only when the SPE bit is set.
366/519
www.BDTIC.com/ST
UM0306 Serial peripheral interface (SPI)
367/519
www.BDTIC.com/ST
Serial peripheral interface (SPI) UM0306
At the end of data and CRC transfers, the flag CRCERR of SPI-SR is set if corruption occurs
during the transfer
Overrun condition
An overrun condition occurs when the master device has sent data bytes and the slave
device has not cleared the RXNE bit resulting from the previous data byte transmitted.
When an overrun condition occurs:
● OVR bit is set and an interrupt is generated if the ERRIE bit is set.
In this case, the receiver buffer contents will not be updated with the newly received data
from the master device. A read to the SPI_DR register returns this byte. All other
subsequently transmitted bytes are lost.
Clearing the OVR bit is done by a read of the SPI_DR register followed by a read access to
the SPI_SR register.
368/519
www.BDTIC.com/ST
UM0306 Serial peripheral interface (SPI)
CRC error
This flag is used to verify the validity of the value received when the CRCEN bit in the
SPI_CR1 register is set. In full-duplex mode, the CRCERR flag in the SPI_SR register is set
if the value received in the shift register (after transmission of the transmitter SPI_TXCRCR
value) does not match the receiver SPI_RXCRCR value.
16.3.9 Interrupts
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
369/519
www.BDTIC.com/ST
Serial peripheral interface (SPI) UM0306
370/519
www.BDTIC.com/ST
UM0306 Serial peripheral interface (SPI)
371/519
www.BDTIC.com/ST
Serial peripheral interface (SPI) UM0306
rw rw rw rw rw rw
372/519
www.BDTIC.com/ST
UM0306 Serial peripheral interface (SPI)
CRC
Reserved BSY OVR MODF Reserved TXE RXNE
ERR
r rc rc rc r r
373/519
www.BDTIC.com/ST
Serial peripheral interface (SPI) UM0306
DR[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
CRCPOLY[15:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
RxCRC[15:0]
r r r r r r r r r r r r r r r r
374/519
www.BDTIC.com/ST
UM0306 Serial peripheral interface (SPI)
TxCRC[15:0]
r r r r r r r r r r r r r r r r
375/519
www.BDTIC.com/ST
Serial peripheral interface (SPI) UM0306
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
BIDIMODE
CRCNEXT
LSBFIRST
CRCEN
RXOnly
BIDIOE
MSTR
CPHA
CPOL
SSM
SPE
DFF
SSI
SPI_CR1 BR [2:0]
00h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
RXDMAEN
TXDMAEN
RXNEIE
ERRIE
Reserved
TXEIE
SSOE
SPI_CR2
04h Reserved
Reset Value 0 0 0 0 0 0
CRCERR
Reserved
MODF
RXNE
OVR
BSY
TXE
SPI_SR
08h Reserved
Reset Value 0 0 0 0 1 0
SPI_DR DR[15:0]
0Ch Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
SPI_CRCPR CRCPOLY[15:0]
10h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
SPI_RXCRCR RxCRC[15:0]
14h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
SPI_TXCRCR TxCRC[15:0]
18h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
376/519
www.BDTIC.com/ST
UM0306 Universal synchronous asynchronous receiver transmitter (USART)
17.1 Introduction
The Universal Synchronous Asynchronous Receiver Transmitter (USART) offers a flexible
means of full-duplex data exchange with external equipment requiring an industry standard
NRZ asynchronous serial data format. The USART offers a very wide range of baud rates
using a fractional baud rate generator.
It supports synchronous one-way communication and half-duplex single wire
communication. It also supports the LIN (Local Interconnection Network), Smartcard
Protocol and IrDA (Infrared Data Association) SIR ENDEC specifications, and modem
operations (CTS/RTS). It allows multi-processor communication.
377/519
www.BDTIC.com/ST
Universal synchronous asynchronous receiver transmitter (USART) UM0306
High speed data communication is possible by using the DMA for multi-buffer
configuration.Main features
● Full duplex, asynchronous communications
● NRZ standard format (Mark/Space)
● Fractional baud rate generator systems
– A common programmable transmit and receive baud rates up to 4.5 MBits/s
● Programmable data word length (8 or 9 bits)
● Configurable stop bits - support for 1 or 2 stop bits
● LIN Master Synchronous Break send capability and LIN slave break detection
capability
– 13-bit break generation and 10/11 bit break detection when USART is hardware
configured for LIN
● Transmitter clock output for synchronous transmission
● IrDA SIR Encoder Decoder
– Support for 3/16 bit duration for normal mode
● Smartcard Emulation Capability
– The Smartcard interface supports the asynchronous protocol Smartcards as
defined in ISO 7816-3 standards
– 0.5, 1.5 Stop Bits for Smartcard operation
● Single wire Half Duplex Communication
● Configurable Multi-Buffer communication using DMA (Direct Memory Access)
– Buffering of Received/Transmitted bytes in Reserved SRAM using centralized
DMA
● Separate enable bits for Transmitter and Receiver
● Transfer detection flags:
– Receive buffer full
– Transmit buffer empty
– End of Transmission flags
● Parity control:
– Transmits parity bit
– Checks parity of received data byte
● Four error detection flags:
– Overrun error
– Noise error
– Frame error
– Parity error
● Ten interrupt sources with flags:
– CTS changes
– LIN break detection
– Transmit data register empty
– Transmission complete
– Receive data register full
– Idle line received
378/519
www.BDTIC.com/ST
UM0306 Universal synchronous asynchronous receiver transmitter (USART)
– Overrun error
– Framing error
– Noise error
– Parity error
● Multi-Processor communication - enter into mute mode if address match does not
occur
● Wake up from mute mode (by idle line detection or address mark detection)
● Two receiver wake-up modes:
– Address bit (MSB)
– Idle line
379/519
www.BDTIC.com/ST
Universal synchronous asynchronous receiver transmitter (USART) UM0306
380/519
www.BDTIC.com/ST
UM0306 Universal synchronous asynchronous receiver transmitter (USART)
PWDATA PRDATA
Write Read (DATA REGISTER) DR
RX IrDA
SIR
SW_RX ENDEC Receive Shift Register
Transmit Shift Register
BLOCK
IRDA_OUT
IRDA_IN GTPR
GT PSC SCLK CONTROL SCLK
CR3 CR2
DMAT DMAR SCEN NACK HD IRLP IREN LINE STOP[1:0] CKEN CPOL CPHA LBCL
CR2 CR1
USART Address UE M WAKE PCE PS PEIE
nRTS Hardware
flow
nCTS controller
WAKE RECEIVER
TRANSMIT UP RECEIVER CLOCK
CONTROL UNIT CONTROL
CR1 SR
TXEIE TCIE RXNE
IE
IDLE TE RE RWU SBK CTS LBD TXE TC RXNE IDLE ORE NE FE PE
IE
USART
INTERRUPT
CONTROL
USART_BRR
TE TRANSMITTER RATE
TRANSMITTER
CONTROL
CLOCK
/16 /DIV
BRR (Mantissa)
15 0
fCPU
RECEIVER RATE
RE CONTROL
CONVENTIONAL BAUD RATE GENERATOR
381/519
www.BDTIC.com/ST
Universal synchronous asynchronous receiver transmitter (USART) UM0306
Start
Idle Frame Bit
Start
Idle Frame Bit
382/519
www.BDTIC.com/ST
UM0306 Universal synchronous asynchronous receiver transmitter (USART)
17.2.3 Transmitter
The transmitter can send data words of either 8 or 9 bits depending on the M bit status.
When the transmit enable bit (TE) is set, 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 on the TX pin. In this
mode, the USART_DR register consists of a buffer (TDR) between the internal bus and the
transmit shift register (see Figure 144).
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.
Note: 1 The TE bit should not be reset during transmission of data. Resetting the TE bit during the
transmission will corrupt the data on the TX pin as the baud rate counters will get frozen.
The current data being transmitted will be lost.
2 An idle frame will be sent after the TE bit is enabled.
383/519
www.BDTIC.com/ST
Universal synchronous asynchronous receiver transmitter (USART) UM0306
a) 1 Stop Bit
Possible Next Data Frame
Parity
Data Frame
Bit Next
Start Start
Bit Bit0 Bit1 Bit2 Bit3 Bit4 Bit5 Bit6 Bit7 Bit
1 1/2 stop bits
b) 1 1/2 stop Bits
Possible Next Data Frame
Parity
Data Frame
Bit Next
Start 2 Stop Start
Bit Bit0 Bit1 Bit2 Bit3 Bit4 Bit5 Bit6 Bit7 Bits Bit
Procedure:
1. Enable the USART by writing the UE bit in USART_CR1 register to 1.
2. Program the M bit in USART_CR1 to define the word length.
3. Program the number of stop bits in USART_CR2.
4. Select DMA enable (DMAT) in USART_CR3 if Multi buffer Communication is to take
place. Configure the DMA register as explained in multi-buffer communication.
5. Set the TE bit in USART_CR1 to send an idle frame as first transmission.
6. Select the desired baud rate using the USART_BRR register.
7. Write the data to send in the USART_DR register (this clears the TXE bit). Repeat this
for each data to be transmitted in case of single buffer.
384/519
www.BDTIC.com/ST
UM0306 Universal synchronous asynchronous receiver transmitter (USART)
When no transmission is taking place, a write instruction to the USART_DR register places
the data directly in the shift register, the data transmission starts, and the TXE bit is
immediately set.
When a frame transmission is complete (after the stop bit) the TC bit is set and an interrupt
is generated if the TCIE is set in the USART_CR1 register.
Clearing the TC bit is performed by the following software sequence:
1. A read to the USART_SR register
2. A write to the USART_DR register
Note: The TC bit can also be cleared by writing a ‘0’ to it. This clearing sequence is recommended
only for Multi-buffer communication.
Break characters
Setting the SBK bit transmits a break character. The break frame length depends on the M
bit (see Figure 145).
If the SBK bit is set to ‘1’ a break character is sent on the TX line after completing the current
character transmission. This bit is reset by hardware when the break character is completed
(during the stop bit of the break character). The USART inserts a logic 1 bit at the end of the
last break frame to guarantee the recognition of the start bit of the next frame.
Note: If the software resets the SBK bit before the commencement of break transmission, the
break character will not be transmitted. For two consecutive breaks, the SBK bit should be
set after the stop bit of the previous break.
Idle characters
Setting the TE bit drives the USART to send an idle frame before the first data frame.
17.2.4 Receiver
The USART can receive data words of either 8 or 9 bits depending on the M bit in the
USART_CR1 register.
Character reception
During an USART reception, data shifts in least significant bit first through the RX pin. In this
mode, the USART_DR register consists of a buffer (RDR) between the internal bus and the
received shift register.
Procedure:
1. Enable the USART by writing the UE bit in USART_CR1 register to 1.
2. Program the M bit in USART_CR1 to define the word length.
3. Program the number of stop bits in USART_CR2.
4. Select DMA enable (DMAT) in USART2_CR3 if Multi-buffer Communication is to take
place. Configure the DMA register as explained in multi-buffer communication. STEP 3
5. Select the desired baud rate using the baud rate register USART_BRR
6. Set the RE bit USART_CR1. This enables the receiver which begins searching for a
start bit.
385/519
www.BDTIC.com/ST
Universal synchronous asynchronous receiver transmitter (USART) UM0306
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 a data received character
plus an interrupt if the IDLEIE bit is set.
Overrun error
An overrun error occurs when a character is received when RXNE has not been reset. Data
can not be transferred from the shift register to the RDR register until the RXNE bit is
cleared.
The RXNE flag is set after every byte received. An overrun error occurs if RXNE flag is set
when the next data is received or the previous DMA request has not been serviced. When
an overrun error occurs:
● The ORE bit is set.
● The RDR content will not be lost. The previous data is available when a read to
USART_DR 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 both the EIE and DMAR bits
are set.
● The ORE bit is reset by a read to the USART_SR register followed by a USART_DR
register read operation.
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. It may also occur when the
386/519
www.BDTIC.com/ST
UM0306 Universal synchronous asynchronous receiver transmitter (USART)
new data is received during the reading sequence (between the USART_SR register read
access and the USART_DR read access).
Noise error
Over-sampling techniques are used (except in synchronous mode) for data recovery by
discriminating between valid incoming data and noise.
RX LINE
sampled values
Sample
clock 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
6/16
7/16 7/16
One bit time
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.
387/519
www.BDTIC.com/ST
Universal synchronous asynchronous receiver transmitter (USART) UM0306
USARTDIV is an unsigned fixed point number. The 12-bit mantissa is coded on the
USART_BRR register.
388/519
www.BDTIC.com/ST
UM0306 Universal synchronous asynchronous receiver transmitter (USART)
Example 2:
To program USARTDIV = 25.62d,
This leads to:
DIV_Fraction = 16*0.62d = 9.92d, nearest real number 10d = Ah
DIV_Mantissa = mantissa (25.620d) = 25d = 19h
Then, BRR = 19Ah
Example 3:
To program USARTDIV = 50.99d
This leads to:
DIV_Fraction = 16*0.99d = 15.84d => nearest real number, 16d = 10h
DIV_Mantissa = mantissa (50.990d) = 50d = 32h
Note: The Baud Counters will be updated with the new value of the Baud Registers after a write to
BRR. Hence the Baud Register value should not be changed during a transaction.
389/519
www.BDTIC.com/ST
Universal synchronous asynchronous receiver transmitter (USART) UM0306
Note: The lower the CPU clock the lower will be the accuracy for a particular Baud rate. The upper
limit of the achievable baud rate can be fixed with this data.
390/519
www.BDTIC.com/ST
UM0306 Universal synchronous asynchronous receiver transmitter (USART)
RXNE RXNE
RX IDLE Addr=0 Data 1 Data 2 IDLE Addr=1 Data 3 Data 4 Addr=2 Data 5
RWU written to 1
(RXNE was cleared)
391/519
www.BDTIC.com/ST
Universal synchronous asynchronous receiver transmitter (USART) UM0306
Legends: SB: Start Bit, STB: Stop Bit, PB: Parity Bit
Note: In case of wake up by an address mark, the MSB bit of the data is taken into account and
not the parity bit
Even parity: the parity bit is calculated to obtain an even number of “1s” inside the frame
made of the 7 or 8 LSB bits (depending on whether M is equal to 0 or 1) and the parity bit.
Ex: data=00110101; 4 bits set => parity bit will be 0 if even parity is selected (PS bit in
USART_CR1 = 0).
Odd parity: the parity bit is calculated to obtain an odd number of “1s” inside the frame
made of the 7 or 8 LSB bits (depending on whether M is equal to 0 or 1) and the parity bit.
Ex: data=00110101; 4 bits set => parity bit will be 1 if odd parity is selected (PS bit in
USART_CR1 = 1).
Transmission mode: If the PCE bit is set in USART_CR1, then the MSB bit of the data
written in the data register is transmitted but is changed by the parity bit (even number of
“1s” if even parity is selected (PS=0) or an odd number of “1s” if odd parity is selected
(PS=1)). If the parity check fails, the PE flag is set in the USART_SR register and an
interrupt is generated if PEIE is set in the USART_CR1 register.
LIN transmission
The same procedure explained in Section 17.2.3 has to be applied for LIN Master
transmission than for normal USART transmission with the following differences:
● Clear the M bit to configure 8-bit word length.
● Set the LINEN bit to enter LIN mode. In this case, setting the SBK bit sends 13 ‘0’ bits
as a break character. Then a bit of value ‘1’ is sent to allow the next start detection.
LIN reception
When the LIN mode is enabled, the break detection circuit is activated. The detection is
totally independent from the normal USART receiver. A break can be detected whenever it
occurs, during idle state or during a frame.
When the receiver is enabled (RE=1 in USART_CR1), the circuit looks at the RX input for a
start signal. The method for detecting start bits is the same when searching break
392/519
www.BDTIC.com/ST
UM0306 Universal synchronous asynchronous receiver transmitter (USART)
characters or data. After a start bit has been detected, the circuit samples the next bits
exactly like for the data (on the 8th, 9th and 10th samples). If 10 (when the LBDL = 0 in
USART_CR2) or 11 (when LBDL=1 in USART_CR2) consecutive bits are detected as ‘0’,
and are followed by a delimiter character, the LBD flag is set in USART_SR. If the LBDIE
bit=1, an interrupt is generated. Before validating the break, the delimiter is checked for as it
signifies that the RX line has returned to a high level.
If a ‘1’ is sampled before the 10 or 11 have occurred, the break detection circuit cancels the
current detection and searches for a start bit again.
If the LIN mode is disabled (LINEN=0), the receiver continues working as normal USART,
without taking into account the break detection.
If the LIN mode is enabled (LINEN=1), as soon as a framing error occurs (i.e. stop bit
detected at ‘0’, which will be the case for any break frame), the receiver stops until the break
detection circuit receives either a ‘1’, if the break word was not complete, or a delimiter
character if a break has been detected.
The behavior of the break detector state machine and the break flag is shown on the
Figure 150: Break detection in LIN mode (11-bit break length - LBDL bit is set) on page 394.
Examples of break frames are given on Figure 151: Break detection in LIN mode vs Framing
error detection on page 395.
393/519
www.BDTIC.com/ST
Universal synchronous asynchronous receiver transmitter (USART) UM0306
Figure 150. Break detection in LIN mode (11-bit break length - LBDL bit is set)
Case 1: break signal not long enough => break discarded, LBD is not set
Capture Strobe
Break State machine Idle Bit0 Bit1 Bit2 Bit3 Bit4 Bit5 Bit6 Bit7 Bit8 Bit9 Bit10 Idle
Read Samples 0 0 0 0 0 0 0 0 0 0 1
Case 2: break signal just long enough => break detected, LBD is set
Capture Strobe
delimiter is immediate
Break State machine Idle Bit0 Bit1 Bit2 Bit3 Bit4 Bit5 Bit6 Bit7 Bit8 Bit9 B10 Idle
Read Samples 0 0 0 0 0 0 0 0 0 0 0
LBD
Case 3: break signal long enough => break detected, LBD is set
Capture Strobe
Break State machine Idle Bit0 Bit1 Bit2 Bit3 Bit4 Bit5 Bit6 Bit7 Bit8 Bit9 Bit10 wait delimiter Idle
Read Samples 0 0 0 0 0 0 0 0 0 0 0
LBD
394/519
www.BDTIC.com/ST
UM0306 Universal synchronous asynchronous receiver transmitter (USART)
RXNE / FE
LBD
RXNE / FE
LBD
395/519
www.BDTIC.com/ST
Universal synchronous asynchronous receiver transmitter (USART) UM0306
has been written). This means that it is not possible to receive a synchronous data without
transmitting data.
2 The LBCL, CPOL and CPHA bits have to be selected when both the transmitter and the
receiver are disabled (TE=RE=0) to ensure that the clock pulses function correctly. These
bits should not be changed while the transmitter or the receiver is enabled.
3 It is advised that TE and RE are set in the same instruction in order to minimize the setup
and the hold time of the receiver.
4 The USART supports master mode only: it cannot receive or send data related to an input
clock (SCLK is always an output).
RX Data out
TX Data in
SCLK Clock
Data on TX 0 1 2 3 4 5 6 7
(from master)
Start LSB MSB Stop
Data on RX 0 1 2 3 4 5 6 7
(from slave)
LSB MSB
*
Capture Strobe
396/519
www.BDTIC.com/ST
UM0306 Universal synchronous asynchronous receiver transmitter (USART)
Data on TX 0 1 2 3 4 5 6 7 8
(from master)
Start LSB MSB Stop
Data on RX 0 1 2 3 4 5 6 7 8
(from slave)
LSB MSB
*
Capture Strobe
tSETUP tHOLD
Note: The function of SCLK is different in Smartcard mode. Refer to the Smartcard mode chapter
for more details.
397/519
www.BDTIC.com/ST
Universal synchronous asynchronous receiver transmitter (USART) UM0306
arbiter, for instance). In particular, the transmission is never blocked by hardware and
continue to occur as soon as a data is written in the data register while the TE bit is set.
17.2.11 Smartcard
The smartcard mode is selected by setting the SCEN bit in the USART_CR3 register. In
smartcard mode, the following bits must be kept cleared:
● LINEN bit in the USART_CR2 register,
● HDSEL and IREN bits in the USART_CR3 register.
Moreover, the CLKEN bit may be set in order to provide a clock to the smartcard.
The Smartcard interface is designed to support asynchronous protocol Smartcards as
defined in the ISO7816-3 standard. USART should be configured as:
● 8 bits plus parity: where M=1 and PCE=1 in the USART_CR1 register and either:
– 0.5 stop bits when receiving: where STOP=’01’ in the USART_CR2 register
– 1.5 stop bits when transmitting: where STOP=’11’ in the USART_CR2 register.
Figure 156 shows examples of what can be seen on the data line with and without parity
error.
S 0 1 2 3 4 5 6 7 P
Start
bit
S 0 1 2 3 4 5 6 7 P
When connected to a smartcard, the TX output of the USART drives a bidirectional line that
the smartcard also drives into. To do so, SW_RX must be connected on the same I/O than
TX at product level. The Transmitter output enable TX_EN is asserted during the
transmission of the start bit and the data byte, and is deasserted during the stop bit (weak
pull up), so that the receive can drive the line in case of a parity error. If TX_EN is not used,
TX is driven at high level during the stop bit: Thus the receiver can drive the line as long as
TX is configured in open-drain.
Smartcard is a single wire half duplex communication protocol.
● Transmission of data from the transmit shift register is guaranteed to be delayed by a
minimum of 1/2 baud clock. In normal operation a full transmit shift register will start
shifting on the next baud clock edge. In Smartcard mode this transmission is further
delayed by a guaranteed 1/2 baud clock.
● If a parity error is detected during reception of a frame programmed with a 1/2 stop bit
period, the transmit line is pulled low for a baud clock period after the completion of the
receive frame, i.e. at the end of the 1/2 stop bit period. This is to indicate to the
Smartcard that the data transmitted to USART has not been correctly received. This
398/519
www.BDTIC.com/ST
UM0306 Universal synchronous asynchronous receiver transmitter (USART)
NACK signal (pulling transmit line low for 1 baud clock) will cause a framing error on the
transmitter side (configured with 1.5 stop bits). The application can handle re-sending
of data according to the protocol. A parity error is ‘NACK’ed by the receiver if the NACK
control bit is set, otherwise a NACK is not transmitted.
● The assertion of the TC flag can be delayed by programming the Guard Time register.
In normal operation, TC is asserted when the transmit shift register is empty and no
further transmit requests are outstanding. In Smartcard mode an empty transmit shift
register triggers the guard time counter to count up to the programmed value in the
Guard Time register. TC is forced low during this time. When the guard time counter
reaches the programmed value TC is asserted high.
● The de-assertion of TC flag is unaffected by Smartcard mode.
● If a framing error is detected on the transmitter end (due to a NACK from the receiver),
the NACK will not be detected as a start bit by the receive block of the transmitter.
According to the ISO protocol, the duration of the received NACK can be 1 or 2 baud
clock periods.
● On the receiver side, if a parity error is detected and a NACK is transmitted the receiver
will not detect the NACK as a start bit.
Note: 1 A break character is not significant in Smartcard mode. A 00h data with a framing error will
be treated as data and not as a break.
2 No IDLE frame is transmitted when toggling the TE bit. The IDLE frame (as defined for the
other configurations) is not defined by the ISO protocol.
Figure 157 details how the NACK signal is sampled by the USART. In this example the
USART is transmitting a data and is configured with 1.5 stop bits. The receiver part of the
USART is enabled in order to check the integrity of the data and the NACK signal.
Figure 157. Parity error detection using the 1.5 stop bits
sampling at sampling at
8th, 9th, 10th 16th, 17th, 18th
sampling at sampling at
8th, 9th, 10th 8th, 9th, 10th
The USART can provide a clock to the smartcard through the SCLK output. In smartcard
mode, SCLK is not associated to the communication but is simply derived from the internal
peripheral input clock through a 5-bit prescaler. The division ratio is configured in the
prescaler register USART_GTPR. SCLK frequency can be programmed from fCK/2 to
fCK/62, where fCK is the peripheral input clock.
399/519
www.BDTIC.com/ST
Universal synchronous asynchronous receiver transmitter (USART) UM0306
400/519
www.BDTIC.com/ST
UM0306 Universal synchronous asynchronous receiver transmitter (USART)
Receiver:
Receiving in low-power mode is similar to receiving in normal mode. For glitch detection the
USART should discard pulses of duration shorter than 1/PSC. A valid low is accepted only if
its duration is greater than 2 periods of the IrDA low-power Baud clock (PSC value in
USART_GTPR).
Note: 1 A pulse of width less than two and greater than one PSC period(s) may or may not be
rejected.
2 The receiver set up time should be managed by software. The IrDA physical layer
specification specifies a minimum of 10 ms delay between transmission and reception (IrDA
is a half duplex protocol).
TX
OR USART_TX
SIR
Transmit IrDA_OUT
SIREN Encoder
USART
SIR
RX Receive IrDA_IN
Decoder
USART_RX
IrDA_OUT
3/16
IrDA_IN
RX 0 1
0 1 0 0 0 1 1
1
401/519
www.BDTIC.com/ST
Universal synchronous asynchronous receiver transmitter (USART) UM0306
17.2.4. In the USART2_SR register, you can clear the TXE/ RXNE flags to achieve
continuous communication.
402/519
www.BDTIC.com/ST
UM0306 Universal synchronous asynchronous receiver transmitter (USART)
USART 1 USART 2
TX RX
RX TX
RTS and CTS flow control can be enabled independently by writing respectively RTSE and
CTSE bits to 1 (in the USART_CR3 register).
nRTS
403/519
www.BDTIC.com/ST
Universal synchronous asynchronous receiver transmitter (USART) UM0306
transmitted (assuming that a data is to be transmitted, in other words, if TXE=0), else the
transmission does not occur. When nCTS is deasserted during a transmission, the current
transmission is completed before the transmitter stops.
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 USART_CR3 register is set. The figure
below shows an example of communication with CTS flow control enabled.
nCTS
404/519
www.BDTIC.com/ST
UM0306 Universal synchronous asynchronous receiver transmitter (USART)
The USART interrupt events are connected to the same interrupt vector (see Figure 163).
● During transmission: Transmission Complete, Clear to Send or Transmit Data Register
empty interrupt.
● While receiving: Idle Line detection, Overrun error, Receive Data register not empty,
Parity error, LIN break detection, Noise Flag (only in multi buffer communication) and
Framing Error (only in multi buffer communication).
These events generate an interrupt if the corresponding Enable Control Bit is set.
TC
TCIE
TXE
TXEIE
CTS
CTSIE
USART
IDLE
IDLEIE interrupt
RXNEIE
ORE
RXNEIE
RXNE
PE
PEIE
LBD
LBDIE
FE
NE
OVR EIE
DMAR
405/519
www.BDTIC.com/ST
Universal synchronous asynchronous receiver transmitter (USART) UM0306
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rc rc r rc r r r r r r
406/519
www.BDTIC.com/ST
UM0306 Universal synchronous asynchronous receiver transmitter (USART)
407/519
www.BDTIC.com/ST
Universal synchronous asynchronous receiver transmitter (USART) UM0306
408/519
www.BDTIC.com/ST
UM0306 Universal synchronous asynchronous receiver transmitter (USART)
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved DR[8:0]
rw rw rw rw rw rw rw rw rw
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
DIV_Mantissa[11:0] DIV_Fraction[3:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
409/519
www.BDTIC.com/ST
Universal synchronous asynchronous receiver transmitter (USART) UM0306
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
RXNE
Reserved UE M WAKE PCE PS PEIE TXEIE TCIE IDLEIE TE RE RWU SBK
IE
rw rw rw rw rw rw rw rw rw rw rw rw rw rw
410/519
www.BDTIC.com/ST
UM0306 Universal synchronous asynchronous receiver transmitter (USART)
411/519
www.BDTIC.com/ST
Universal synchronous asynchronous receiver transmitter (USART) UM0306
412/519
www.BDTIC.com/ST
UM0306 Universal synchronous asynchronous receiver transmitter (USART)
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CLK
Res. LINEN STOP[1:0] CPOL CPHA LBCL Res. LBDIE LBDL Res. ADD[3:0]
EN
rw rw rw rw rw rw rw rw rw rw rw rw rw rw
413/519
www.BDTIC.com/ST
Universal synchronous asynchronous receiver transmitter (USART) UM0306
Note: These 3 bits (CPOL, CPHA, LBCL) should not be written while the transmitter is enabled.
414/519
www.BDTIC.com/ST
UM0306 Universal synchronous asynchronous receiver transmitter (USART)
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
HD
Reserved CTSIE CTSE RTSE DMAT DMAR SCEN NACK IRLP IREN EIE
SEL
rw rw rw rw rw rw rw rw rw rw rw
415/519
www.BDTIC.com/ST
Universal synchronous asynchronous receiver transmitter (USART) UM0306
416/519
www.BDTIC.com/ST
UM0306 Universal synchronous asynchronous receiver transmitter (USART)
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
GT[7:0] PSC[7:0]
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
417/519
www.BDTIC.com/ST
Universal synchronous asynchronous receiver transmitter (USART) UM0306
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
RXNE
IDLE
ORE
CTS
TXE
LBD
NE
TC
PE
FE
USART_SR
00h Reserved
Reset Value 0 0 1 1 0 0 0 0 0 0
USART_DR DR[8:0]
04h Reserved
Reset Value 0 0 0 0 0 0 0 0 0
DIV_Fraction
USART_BRR DIV_Mantissa[15:4]
08h Reserved [3:0]
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
RXNEIE
IDLEIE
WAKE
TXEIE
RWU
PEIE
TCIE
PCE
SBK
UE
RE
PS
TE
USART_CR1
M
0Ch Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0
CLKEN
Reserved
Reserved
LINEN
LBDIE
CPHA
CPOL
LBCL
LBDL
STOP
USART_CR2 ADD[3:0]
10h Reserved [1:0]
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0
HDSEL
CTSIE
DMAR
SCEN
NACK
DMAT
CTSE
RTSE
IREN
IRLP
EIE
USART_CR3
14h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
418/519
www.BDTIC.com/ST
UM0306 USB full speed device interface (USB)
18.1 Introduction
The USB Peripheral implements an interface between a full-speed USB 2.0 bus and the
APB1 bus.
USB suspend/resume are supported which allows to stop the device clocks for low-power
consumption.
419/519
www.BDTIC.com/ST
USB full speed device interface (USB) UM0306
D+ D-
USB
Control
RX-TX Clock
registers & logic
Suspend Recovery
Timer Control
Endpoint Interrupt
Selection registers & logic
S.I.E.
Packet
Buffer Endpoint Endpoint
Interface Registers Registers
Packet
Register Interrupt
Arbiter Buffer
Mapper Mapper
Memory
APB1 wrapper
APB1 Interface
PCLK1 APB1 bus IRQs to NVIC
420/519
www.BDTIC.com/ST
UM0306 USB full speed device interface (USB)
When a token for a valid function/endpoint pair is recognized by the USB Peripheral, the
related data transfer (if required and if the endpoint is configured) takes place. The data
buffered by the USB Peripheral is loaded in an internal 16 bit register and memory access to
the dedicated buffer is performed. When all the data has been transferred, if needed, the
proper handshake packet over the USB is generated or expected according to the direction
of the transfer.
At the end of the transaction, an endpoint-specific interrupt is generated, reading status
registers and/or using different interrupt response routines. The microcontroller can
determine:
● Which endpoint has to be served
● Which type of transaction took place, if errors occurred (bit stuffing, format, CRC,
protocol, missing ACK, over/underrun, etc.)
USB Peripheral
Special support is offered to Isochronous transfers and high throughput bulk transfers,
implementing a double buffer usage, which allows to always have an available buffer for the
USB Peripheral while the microcontroller uses the other one.
The unit can be placed in low-power mode (SUSPEND mode), by writing in the control
register, whenever required. At this time, all static power dissipation is avoided, and the USB
clock can be slowed down or stopped. The detection of activity at the USB inputs, while in
low-power mode, wakes the device up asynchronously. A special interrupt source can be
connected directly to a wake-up line to allow the system to immediately restart the normal
clock generation and/or support direct clock start/stop.
421/519
www.BDTIC.com/ST
USB full speed device interface (USB) UM0306
endpoints* in any combination. For example the USB Peripheral can be programmed to
have 4 doublebuffer endpoints and 8 single-buffer/mono-directional endpoints.
● Control Registers: These are the registers containing information about the status of
the whole USB Peripheral and used to force some USB events, such as resume and
power-down.
● Interrupt Registers: These contain the Interrupt masks and a record of the events. They
can be used to inquire an interrupt reason, the interrupt status or to clear the status of a
pending interrupt.
Note: * Endpoint 0 is always used for control transfer in single-buffer mode.
The USB Peripheral is connected to the APB1 bus through an APB1 interface, containing
the following blocks:
● Packet Memory: This is the local memory that physically contains the Packet Buffers. It
can be used by the Packet Buffer interface, which creates the data structure and can be
accessed directly by the application software. The size of the Packet Memory is 512
bytes, structured as 256 words by 16 bits.
● Arbiter: This block accepts memory requests coming from the APB1 bus and from the
USB interface. It resolves the conflicts by giving priority to APB1 accesses, while
always reserving half of the memory bandwidth to complete all USB transfers. This
time-duplex scheme implements a virtual dual-port SRAM that allows memory access,
while an USB transaction is happening. Multi-word APB1 transfers of any length are
also allowed by this scheme.
● Register Mapper: This block collects the various byte-wide and bit-wide registers of the
USB Peripheral in a structured 16-bit wide word set addressed by the APB1.
● Interrupt Mapper: This block is used to select how the possible USB events can
generate interrupts and map them to IRQ lines of the NVIC.
● APB1 Wrapper: This provides an interface to the APB1 for the memory and register. It
also maps the whole USB Peripheral in the APB1 address space.
422/519
www.BDTIC.com/ST
UM0306 USB full speed device interface (USB)
As a first step application software needs to activate register macrocell clock and de-assert
macrocell specific reset signal using related control bits provided by device clock
management logic.
After that the analog part of the device related to the USB transceiver must be switched on
using the PDWN bit in CNTR register which requires a special handling. This bit is intended
to switch on the internal voltage references supplying the port transceiver. Since this circuits
have a defined startup time, during which the behavior of USB transceiver is not defined, it is
necessary to wait this time, after having set the PDWN bit in CNTR register, then the reset
condition on the USB part can be removed (clearing of FRES bit in CNTR register) and the
ISTR register can be cleared, removing any spurious pending interrupt, before enabling any
other macrocell operation.
As a last step the USB specific 48 MHz clock needs to be activated, using the related
control bits provided by device clock management logic.
At system reset, the microcontroller must initialize all required registers and the packet
buffer description table, to make the USB Peripheral able to properly generate interrupts and
data transfers. All registers not specific to any endpoint must be initialized according to the
needs of application software (choice of enabled interrupts, chosen address of packet
buffers, etc.). Then the process continues as for the USB reset case (see further
paragraph).
423/519
www.BDTIC.com/ST
USB full speed device interface (USB) UM0306
Note: Due to USB data rate and packet memory interface requirements, the APB1 clock frequency
must be greater than 8 MHz to avoid data overrun/underrun problems.
Each endpoint is associated with two packet buffers (usually one for transmission and the
other one for reception). Buffers can be placed anywhere inside the packet memory
because their location and size is specified in a buffer description table, which is also
located in the packet memory at the address indicated by the register. Each table entry is
associated to an endpoint register and it is composed of four 16-bit words so that table start
address must always be aligned to an 8-byte boundary (the lowest three bits of register are
always “000”). Buffer descriptor table entries are described in the Section 18.6.3: Buffer
descriptor table. If an endpoint is unidirectional and it is neither an Isochronous nor a
double-buffered bulk, only one packet buffer is required (the one related to the supported
transfer direction). Other table locations related to unsupported transfer directions or unused
endpoints, are available to the user. isochronous and double-buffered bulk endpoints have
special handling of packet buffers (Refer to Section 18.5.4: Isochronous transfers and
Section 18.5.3: Double-buffered endpoints respectively). The relationship between buffer
description table entries and packet buffer areas is depicted in Figure 165.
424/519
www.BDTIC.com/ST
UM0306 USB full speed device interface (USB)
Figure 165. Packet buffer areas with examples of buffer description table locations
Buffer for
double-buffered
IN Endpoint 3
Each packet buffer is used either during reception or transmission starting from the bottom.
The USB Peripheral will never change the contents of memory locations adjacent to the
allocated memory buffers; if a packet bigger than the allocated buffer length is received
(buffer overrun condition) the data will be copied to the memory only up to the last available
location.
425/519
www.BDTIC.com/ST
USB full speed device interface (USB) UM0306
Endpoint initialization
The first step to initialize an endpoint is to write appropriate values to the
ADDRn_TX/ADDRn_RX registers so that the USB Peripheral finds the data to be
transmitted already available and the data to be received can be buffered. The EP_TYPE
bits in the register must be set according to the endpoint type, eventually using the
EP_KIND bit to enable any special required feature. On the transmit side, the endpoint must
be enabled using the STAT_TX bits in the register and COUNTn_TX must be initialized. For
reception, STAT_RX bits must be set to enable reception and COUNTn_RX must be written
with the allocated buffer size using the BL_SIZE and NUM_BLOCK fields. Unidirectional
endpoints, except Isochronous and double-buffered bulk endpoints, need to initialize only
bits and registers related to the supported direction. Once the transmission and/or reception
are enabled, register and locations ADDRn_TX/ADDRn_RX, COUNTn_TX/COUNTn_RX
(respectively), should not be modified by the application software, as the hardware can
change their value on the fly. When the data transfer operation is completed, notified by a
CTR interrupt event, they can be accessed again to re-enable a new operation.
426/519
www.BDTIC.com/ST
UM0306 USB full speed device interface (USB)
427/519
www.BDTIC.com/ST
USB full speed device interface (USB) UM0306
Control transfers
Control transfers are made of a SETUP transaction, followed by zero or more data stages,
all of the same direction, followed by a status stage (a zero-byte transfer in the opposite
direction). SETUP transactions are handled by control endpoints only and are very similar to
OUT ones (data reception) except that the values of DTOG_TX and DTOG_RX bits of the
addressed endpoint registers are set to 1 and 0 respectively, to initialize the control transfer,
and both STAT_TX and STAT_RX are set to ‘10’ (NAK) to let software decide if subsequent
transactions must be IN or OUT depending on the SETUP contents. A control endpoint must
check SETUP bit in the register at each CTR_RX event to distinguish normal OUT
transactions from SETUP ones. A USB device can determine the number and direction of
data stages by interpreting the data transferred in the SETUP stage, and is required to
STALL the transaction in the case of errors. To do so, at all data stages before the last, the
unused direction should be set to STALL, so that, if the host reverses the transfer direction
too soon, it gets a STALL as a status stage. While enabling the last data stage, the opposite
direction should be set to NAK, so that, if the host reverses the transfer direction (to perform
the status stage) immediately, it is kept waiting for the completion of the control operation. If
the control operation completes successfully, the software will change NAK to VALID,
otherwise to STALL. At the same time, if the status stage will be an OUT, the STATUS_OUT
(EP_KIND in the register) bit should be set, so that an error is generated if a status
transaction is performed with not-zero data. When the status transaction is serviced, the
application clears the STATUS_OUT bit and sets STAT_RX to VALID (to accept a new
command) and STAT_TX to NAK (to delay a possible status stage immediately following the
next setup).
Since the USB specification states that a SETUP packet cannot be answered with a
handshake different from ACK, eventually aborting a previously issued command to start the
new one, the USB logic doesn’t allow a control endpoint to answer with a NAK or STALL
packet to a SETUP token received from the host.
When the STAT_RX bits are set to ‘01’ (STALL) or ‘10’ (NAK) and a SETUP token is
received, the USB accepts the data, performing the required data transfers and sends back
an ACK handshake. If that endpoint has a previously issued CTR_RX request not yet
acknowledged by the application (i.e. CTR_RX bit is still set from a previously completed
reception), the USB discards the SETUP transaction and does not answer with any
handshake packet regardless of its state, simulating a reception error and forcing the host to
send the SETUP token again. This is done to avoid losing the notification of a SETUP
transaction addressed to the same endpoint immediately following the transaction, which
triggered the CTR_RX interrupt.
428/519
www.BDTIC.com/ST
UM0306 USB full speed device interface (USB)
429/519
www.BDTIC.com/ST
USB full speed device interface (USB) UM0306
The memory buffer which is currently being used by the USB Peripheral is defined by DTOG
buffer flag, while the buffer currently in use by application software is identified by SW_BUF
buffer flag. The relationship between the buffer flag value and the used packet buffer is the
same in both cases, and it is listed in the following table.
430/519
www.BDTIC.com/ST
UM0306 USB full speed device interface (USB)
be masked as ‘10’ (NAK) when a buffer conflict between the USB Peripheral and the
application software is detected (this condition is identified by DTOG and SW_BUF having
the same value, see Table 56 on page 430). The application software responds to the CTR
event notification by clearing the interrupt flag and starting any required handling of the
completed transaction. When the application packet buffer usage is over, the software
toggles the SW_BUF bit, writing ‘1’ to it, to notify the USB Peripheral about the availability of
that buffer. In this way, the number of NAKed transactions is limited only by the application
elaboration time of a transaction data: if the elaboration time is shorter than the time
required to complete a transaction on the USB bus, no re-transmissions due to flow control
will take place and the actual transfer rate will be limited only by the host PC.
The application software can always override the special flow control implemented for
double-buffered bulk endpoints, writing an explicit status different from ‘11’ (Valid) into the
STAT bit pair of the related register. In this case, the USB Peripheral will always use the
programmed endpoint status, regardless of the buffer usage condition.
431/519
www.BDTIC.com/ST
USB full speed device interface (USB) UM0306
432/519
www.BDTIC.com/ST
UM0306 USB full speed device interface (USB)
reception is disabled to avoid any further SUSP interrupts being issued while the USB
is suspended.
2. Remove or reduce any static power consumption in blocks different from the USB
Peripheral.
3. Set LP_MODE bit in register to 1 to remove static power consumption in the analog
USB transceivers but keeping them able to detect resume activity.
4. Optionally turn off external oscillator and device PLL to stop any activity inside the
device.
When an USB event occurs while the device is in SUSPEND mode, the RESUME procedure
must be invoked to restore nominal clocks and regain normal USB behavior. Particular care
must be taken to insure that this process does not take more than 10mS when the wakening
event is an USB reset sequence (See “Universal Serial Bus Specification” for more details).
The start of a resume or reset sequence, while the USB Peripheral is suspended, clears the
LP_MODE bit in register asynchronously. Even if this event can trigger an WKUP interrupt
if enabled, the use of an interrupt response routine must be carefully evaluated because of
the long latency due to system clock restart; to have the shorter latency before re-activating
the nominal clock it is suggested to put the resume procedure just after the end of the
suspend one, so its code is immediately executed as soon as the system clock restarts. To
prevent ESD discharges or any other kind of noise from waking-up the system (the exit from
suspend mode is an asynchronous event), a suitable analog filter on data line status is
activated during suspend; the filter width is about 70ns.
The following is a list of actions a resume procedure should address:
1. Optionally turn on external oscillator and/or device PLL.
2. Clear FSUSP bit of register.
3. If the resume triggering event has to be identified, bits RXDP and RXDM in the register
can be used according to Table 58, which also lists the intended software action in all
the cases. If required, the end of resume or reset sequence can be detected monitoring
the status of the above mentioned bits by checking when they reach the “10”
configuration, which represent the Idle bus state; moreover at the end of a reset
sequence the RESET bit in register is set to 1, issuing an interrupt if enabled, which
should be handled as usual.
A device may require to exit from suspend mode as an answer to particular events not
directly related to the USB protocol (e.g. a mouse movement wakes up the whole system).
In this case, the resume sequence can be started by setting the RESUME bit in the register
to ‘1’ and resetting it to 0 after an interval between 1mS and 15mS (this interval can be
timed using ESOF interrupts, occurring with a 1mS period when the system clock is running
at nominal frequency). Once the RESUME bit is clear, the resume sequence will be
433/519
www.BDTIC.com/ST
USB full speed device interface (USB) UM0306
completed by the host PC and its end can be monitored again using the RXDP and RXDM
bits in the register.
Note: The RESUME bit must be anyway used only after the USB Peripheral has been put in
suspend mode, setting the FSUSP bit in register to 1.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw rw rw rw rw rw rw rw rw rw rw rw rw
434/519
www.BDTIC.com/ST
UM0306 USB full speed device interface (USB)
435/519
www.BDTIC.com/ST
USB full speed device interface (USB) UM0306
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
PMA
CTR ERR WKUP SUSP RESET SOF ESOF DIR EP_ID[3:0]
OVR
r rc rc rc rc rc rc rc r r r r r
This register contains the status of all the interrupt sources allowing application software to
determine, which events caused an interrupt request.
The upper part of this register contains single bits, each of them representing a specific
event. These bits are set by the hardware when the related event occurs; if the
corresponding bit in the register is set, a generic interrupt request is generated. The
interrupt routine, examining each bit, will perform all necessary actions, and finally it will
clear the serviced bits. If any of them is not cleared, the interrupt is considered to be still
pending, and the interrupt line will be kept high again. If several bits are set simultaneously,
only a single interrupt will be generated.
Endpoint transaction completion can be handled in a different way to reduce interrupt
response latency. The CTR bit is set by the hardware as soon as an endpoint successfully
completes a transaction, generating a generic interrupt request if the corresponding bit in is
set. An endpoint dedicated interrupt condition is activated independently from the CTRM bit
in the register. Both interrupt conditions remain active until software clears the pending bit in
the corresponding register (the CTR bit is actually a read only bit). USB PeripheralFor
endpoint-related interrupts, the software can use the Direction of Transaction (DIR) and
EP_ID read-only bits to identify, which endpoint made the last interrupt request and called
the corresponding interrupt service routine.
The user can choose the relative priority of simultaneously pending events by specifying the
order in which software checks bits in an interrupt service routine. Only the bits related to
events, which are serviced, are cleared. At the end of the service routine, another interrupt
will be requested, to service the remaining conditions.
To avoid spurious clearing of some bits, it is recommended to clear them with a load
instruction where all bits which must not be altered are written with 1, and all bits to be
cleared are written with ‘0’ (these bits can only be cleared by software). Read-modify-write
cycles should be avoided because between the read and the write operations another bit
could be set by the hardware and the next write will clear it before the microprocessor has
the time to serve the event.
436/519
www.BDTIC.com/ST
UM0306 USB full speed device interface (USB)
437/519
www.BDTIC.com/ST
USB full speed device interface (USB) UM0306
438/519
www.BDTIC.com/ST
UM0306 USB full speed device interface (USB)
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
r r r r r r r r r r r r r r r r
439/519
www.BDTIC.com/ST
USB full speed device interface (USB) UM0306
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
BTABLE[15:3] Reserved
rw rw rw rw rw rw rw rw rw rw rw rw rw
440/519
www.BDTIC.com/ST
UM0306 USB full speed device interface (USB)
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
r-c t t t r rw rw rw r-c t t t rw rw rw rw
They are also reset when an USB reset is received from the USB bus or forced through bit
FRES in the CTLR register, except the CTR_RX and CTR_TX bits, which are kept
unchanged to avoid missing a correct packet notification immediately followed by an USB
reset event. Each endpoint has its register where n is the endpoint identifier.
Read-modify-write cycles on these registers should be avoided because between the read
and the write operations some bits could be set by the hardware and the next write would
modify them before the CPU has the time to detect the change. For this purpose, all bits
affected by this problem have an ‘invariant’ value that must be used whenever their
modification is not required. It is recommended to modify these registers with a load
instruction where all the bits, which can be modified only by the hardware, are written with
their ‘invariant’ value.
441/519
www.BDTIC.com/ST
USB full speed device interface (USB) UM0306
442/519
www.BDTIC.com/ST
UM0306 USB full speed device interface (USB)
443/519
www.BDTIC.com/ST
USB full speed device interface (USB) UM0306
444/519
www.BDTIC.com/ST
UM0306 USB full speed device interface (USB)
00 BULK
01 CONTROL
10 ISO
11 INTERRUPT
00 BULK DBL_BUF
01 CONTROL STATUS_OUT
10 ISO Not used
11 INTERRUPT Not used
445/519
www.BDTIC.com/ST
USB full speed device interface (USB) UM0306
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
ADDRn_TX[15:1] -
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw -
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
- COUNTn_TX[9:0]
rw rw rw rw rw rw rw rw rw rw
These bits are not used since packet size is limited by USB specifications to 1023 bytes.
Bits 15:10
Their value is not considered by the USB Peripheral.
COUNTn_TX[9:0]: Transmission Byte Count
Bits 9:0 These bits contain the number of bytes to be transmitted by the endpoint associated
with the register at the next IN token addressed to it.
Double-buffered and Isochronous IN Endpoints have two
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
- COUNTn_TX_1[9:0]
- - - - - - rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
- COUNTn_TX_0[9:0]
- - - - - - rw rw rw rw rw rw rw rw rw rw
446/519
www.BDTIC.com/ST
UM0306 USB full speed device interface (USB)
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
ADDRn_RX[15:1] -
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw -
447/519
www.BDTIC.com/ST
USB full speed device interface (USB) UM0306
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw rw rw rw rw rw r r r r r r r r r r
This table location is used to store two different values, both required during packet
reception. The most significant bits contains the definition of allocated buffer size, to allow
buffer overflow detection, while the least significant part of this location is written back by the
USB Peripheral at the end of reception to give the actual number of received bytes. Due to
the restrictions on the number of available bits, buffer size is represented using the number
of allocated memory blocks, where block size can be selected to choose the trade-off
between fine-granularity/small-buffer and coarse-granularity/large-buffer. The size of
allocated buffer is a part of the endpoint descriptor and it is normally defined during the
enumeration process according to its maxPacketSize parameter value (See “Universal
Serial Bus Specification”).
BLSIZE
NUM_BLOCK_1[4:0] COUNTn_RX_1[9:0]
_1
rw rw rw rw rw rw r r r r r r r r r r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
BLSIZE
NUM_BLOCK_0[4:0] COUNTn_RX_0[9:0]
_0
rw rw rw rw rw rw r r r r r r r r r r
448/519
www.BDTIC.com/ST
UM0306 USB full speed device interface (USB)
449/519
www.BDTIC.com/ST
USB full speed device interface (USB) UM0306
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
DTOG_RX
DTOG_TX
EP_KIND
CTR_RX
CTR_TX
STAT_ EP STAT_
SETUP
USB_EP0R RX TYPE TX EA[3:0]
00h Reserved
[1:0] [1:0] [1:0]
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DTOG_RX
DTOG_TX
EP_KIND
CTR_RX
CTR_TX
STAT_ EP STAT_
SETUP
USB_EP1R RX TYPE TX EA[3:0]
04h Reserved
[1:0] [1:0] [1:0]
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DTOG_RX
DTOG_TX
EP_KIND
CTR_RX
CTR_TX
STAT_ EP STAT_
SETUP
USB_EP2R RX TYPE TX EA[3:0]
08h Reserved
[1:0] [1:0] [1:0]
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DTOG_RX
DTOG_TX
EP_KIND
CTR_RX
CTR_TX
STAT_ EP STAT_
SETUP
USB_EP3R RX TYPE TX EA[3:0]
0Ch Reserved
[1:0] [1:0] [1:0]
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DTOG_RX
DTOG_TX
EP_KIND
CTR_RX
CTR_TX
STAT_ EP STAT_
SETUP
USB_EP4R RX TYPE TX EA[3:0]
10h Reserved
[1:0] [1:0] [1:0]
Reset Value 0 0
DTOG_RX 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DTOG_TX
EP_KIND
CTR_RX
CTR_TX
STAT_ EP STAT_
SETUP
USB_EP5R RX TYPE TX EA[3:0]
14h Reserved
[1:0] [1:0] [1:0]
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DTOG_RX
DTOG_TX
EP_KIND
CTR_RX
CTR_TX
STAT_ EP STAT_
SETUP
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DTOG_RX
DTOG_TX
EP_KIND
CTR_RX
CTR_TX
STAT_ EP STAT_
SETUP
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
20h-3Fh Reserved
PMAOVRM
RESUME
LPMODE
RESETM
WKUPM
ESOFM
SUSPM
FSUSP
PDWN
ERRM
CTRM
SOFM
FRES
40h Reserved Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 1 1
PMAOVR
RESET
WKUP
ESOF
SUSP
ERR
SOF
CTR
DIR
EP_ID[3:0]
44h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0
RXDM
RXDP
LSOF
LCK
FN[10:0]
48h Reserved [1:0]
Reset Value 0 0 0 0 0 x x x x x x x x x x x
EF ADD[6:0]
4Ch Reserved Reserved
Reset Value 0 0 0 0 0 0 0 0
BTABLE[15:3]
50h Reserved Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0
450/519
www.BDTIC.com/ST
UM0306 Analog/digital converter (ADC)
19.1 Introduction
The 12-bit ADC is a successive approximation Analog to Digital Converter. It has up to 18
multiplexed channels allowing it measure signals from 16 external and two internal sources.
A/D Conversion of the various channels can be performed in single, continuous, scan or
discontinuous mode. The result of the ADC is stored in a left-aligned or right-aligned 16-bit
data register.
The Analog Watchdog feature allows the application to detect if the input voltage goes
outside the user-defined high or low thresholds.
451/519
www.BDTIC.com/ST
Analog/digital converter (ADC) UM0306
Interrupt
Flags enable bits
End of Conversion
EOC EOCIE
End of Injected Conversion ADC Interrupt to NVIC
JEOC JEOCIE
Analog Watchdog Event
AWD AWDIE
ANALOG WATCHDOG
Compare Result
High Threshold (12 bits)
Low Threshold (12 bits)
Address/data bus
INJECTED DATA REGISTERS
VREF+ (4 x 16 bits)
VREF-
REGULAR DATA REGISTER
VDDA (16 bits)
VSSA
ANALOG DMA request
MUX
ADC_IN0
ADC_IN1
GPIO up to 4 INJECTED
ADCCLK
Ports CHANNELS ANALOG TO DIGITAL
up to 16 CONVERTER
REGULAR
ADC_IN15 CHANNELS
Temp. sensor
VREFINT
JEXTSEL[2:0] bits
TIM1_TRGO
TIM1_CH4 JEXTRIG
TIM2_TRGO bit
TIM2_CH1
Start trigger
TIM3_CH4 EXTRIG
TIM4_TRGO (injected group)
EXTI_15 bit
EXTSEL[2:0] bits
TIM1_CH1
TIM1_CH2 Start trigger
TIM1_CH3 (regular group)
TIM2_CH2
TIM3_TRGO
TIM4_CH4
EXTI_11
452/519
www.BDTIC.com/ST
UM0306 Analog/digital converter (ADC)
453/519
www.BDTIC.com/ST
Analog/digital converter (ADC) UM0306
The total number of conversions in the injected group must be written in the L[1:0] bits
in the ADC_JSQR register.
If the ADC_SQRx or ADC_JSQR registers are modified during a conversion, the current
conversion is reset and a new start pulse is sent to the ADC to convert the new chosen
group.
454/519
www.BDTIC.com/ST
UM0306 Analog/digital converter (ADC)
ADC_CLK
SET ADON
Analog voltage
455/519
www.BDTIC.com/ST
Analog/digital converter (ADC) UM0306
None x 0 0
All injected channels 0 0 1
All regular channels 0 1 0
All regular and injected channels 0 1 1
(1)
Single injected channel 1 0 1
(1)
Single regular channel 1 1 0
Single (1) regular or injected channel 1 1 1
1. Selected by AWDCH[4:0] bits
456/519
www.BDTIC.com/ST
UM0306 Analog/digital converter (ADC)
Auto-injection
If the JAUTO bit is set, then the injected group channels are automatically converted after
the regular group channels. This can be used to convert a sequence of up to 20 conversions
programmed in the ADC_SQRx and ADC_JSQR registers.
In this mode, external trigger on injected channels must be disabled.
If the CONT bit is also set in addition to the JAUTO bit, regular channels followed by injected
channels are continuously converted.
Note: It is not possible to use both auto-injected and discontinuous modes simultaneously.
Inj. event
Reset ADC
SOC
max latency
457/519
www.BDTIC.com/ST
Analog/digital converter (ADC) UM0306
Example:
n = 3, channels to be converted = 0, 1, 2, 3, 6, 7, 9, 10
1st trigger: sequence converted 0, 1, 2
2nd trigger: sequence converted 3, 6, 7
3rd trigger: sequence converted 9, 10 and an EOC event generated
4th trigger: sequence converted 0, 1, 2
Note: When a regular group is converted in discontinuous mode, no rollover will occur.
When all sub groups are converted, the next trigger starts conversion of the first sub-group.
In the example above, the 4th trigger reconverts the 1st sub-group channels 0, 1 and 2.
Injected group
This mode is enabled by setting the JDISCEN bit in the ADC_CR1 register. It can be used to
convert the sequence selected in the ADC_JSQR register, channel by channel, after an
external trigger event.
When an external trigger occurs, it starts the next channel conversions selected in the
ADC_JSQR registers until all the conversions in the sequence are done. The total sequence
length is defined by the JL[1:0] bits in the ADC_JSQR register.
Example:
n = 1, channels to be converted = 1, 2, 3
1st trigger: channel 1 converted
2nd trigger: channel 2 converted
3rd trigger: channel 3 converted and EOC and JEOC events generated
4th trigger: channel 1
Note: 1 When all injected channels are converted, the next trigger starts the conversion of the first
injected channel. In the example above, the 4th trigger reconverts the 1st injected channel
1.
2 It is not possible to use both auto-injected and discontinuous modes simultaneously.
3 The user must avoid setting discontinuous mode for both regular and injected groups
together. Discontinuous mode must be enabled only for one group conversion.
458/519
www.BDTIC.com/ST
UM0306 Analog/digital converter (ADC)
19.5 Calibration
The ADC has an built-in self calibration mode. Calibration significantly reduces accuracy
errors due to internal capacitor bank variations. During calibration, an error-correction code
(digital word) is calculated for each capacitor, and during all subsequent conversions, the
error contribution of each capacitor is removed using this code.
Calibration is started by setting the CAL bit in the ADC_CR2 register. Once calibration is
over, the CAL bit is reset by hardware and normal conversion can be performed. It is
recommended to calibrate the ADC once at power-on. The calibration codes are stored in
the ADC_DR as soon as the calibration phase ends.
Note: 1 It is recommended to perform a calibration after each power-up.
2 Before starting a calibration the ADC must have been in power-off state (ADON bit = ‘0’) for
at least two ADC clock cycles.
CLK
459/519
www.BDTIC.com/ST
Analog/digital converter (ADC) UM0306
Regular group
0 0 0 0 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
Regular group
D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0 0 0 0 0
460/519
www.BDTIC.com/ST
UM0306 Analog/digital converter (ADC)
One of these events can be generated by setting a bit in a register (SWSTART and
JSWSTART in ADC_CR2).
A regular group conversion can be interrupted by a injected trigger.
461/519
www.BDTIC.com/ST
Analog/digital converter (ADC) UM0306
462/519
www.BDTIC.com/ST
UM0306 Analog/digital converter (ADC)
REGULAR
CHANNELS ADC2 (SLAVE)
INJECTED
CHANNELS
internal triggers
Address/data bus
REGULAR DATA REGISTER
(16 bits)*
ADC_IN0
REGULAR
ADC_IN1 CHANNELS
GPIO
Ports
INJECTED
CHANNELS
ADC_IN15
Temp. sensor
VREFINT DUAL MODE
CONTROL
EXTI_15
Start trigger mux
(injected group)
Note: External triggers are present on ADC2 but are not shown for the purposes of this diagram
* In some dual ADC modes, the ADC1 data register (ADC1_DR) contains both ADC1 and ADC2 regular converted
data over the entire 32 bits.
463/519
www.BDTIC.com/ST
Analog/digital converter (ADC) UM0306
Sampling
Conversion
ADC2 CH0 CH1 CH2 CH3
ADC1 CH3 CH2 CH1 CH0
Sampling
Conversion
ADC1 CH0 CH1 CH2 CH3 ... CH15
ADC2 CH15 CH14 CH13 CH12 ... CH0
464/519
www.BDTIC.com/ST
UM0306 Analog/digital converter (ADC)
Sampling
End of conversion on ADC2
Conversion
ADC2 CH0 ... CH0
ADC1 CH0 ... CH0
Trigger
End of conversion on ADC1
7 ADCCLK
cycles
465/519
www.BDTIC.com/ST
Analog/digital converter (ADC) UM0306
14 ADCCLK
cycles
28 ADCCLK
cycles
ADC1 ...
ADC2
If the injected discontinuous mode is enabled for both ADC1 and ADC2:
● When the 1st trigger occurs, the first injected channel in ADC1 is converted.
● When the 2nd trigger arrives, the first injected channel in ADC2 are converted
● and so on....
A JEOC interrupt, if enabled, is generated after all injected group channels of ADC1 are
converted.
A JEOC interrupt, if enabled, is generated after all injected group channels of ADC2 are
converted.
If another external trigger occurs after all injected group channels have been converted then
the alternate trigger process restarts.
466/519
www.BDTIC.com/ST
UM0306 Analog/digital converter (ADC)
Figure 179. Alternate trigger: 4 injected channels (each ADC) in discontinuous model
1st trigger 3rd trigger 5th trigger 7th trigger Sampling
JEOC on ADC1 Conversion
ADC1
ADC2
JEOC on ADC2
ADC1 inj
CH0
2nd trig
If a trigger occurs during an injected conversion that has interrupted a regular conversion, it
will be ignored. Figure 181 shows the behavior in this case (2nd trig is ignored).
467/519
www.BDTIC.com/ST
Analog/digital converter (ADC) UM0306
Figure 182. Interleaved single channel with injected sequence CH11, CH12
Sampling
ADC1 CH0 CH0 CH0 Conversion
ADC2 CH0 CH0 CH0
CH11 CH12
Trigger
CH12 CH11
CH0 CH0
CH0 CH0
Main features
● Supported Temperature Range: -40 to 125 degrees
● Precision: +/- 1.5° C
468/519
www.BDTIC.com/ST
UM0306 Analog/digital converter (ADC)
TEMPERATURE VSENSE
SENSOR ADC_IN16
Address/data bus
converted data
ADC
VREFINT
INTERNAL
ADC_IN17
POWER
BLOCK
19.12 Interrupts
An interrupt can be produced on end of conversion for regular and injected groups and
when the Analog Watchdog status bit is set. Separate interrupt enable bits are available for
flexibility.
469/519
www.BDTIC.com/ST
Analog/digital converter (ADC) UM0306
Two other two flags are present in the ADC_SR register, but there is no interrupt associated
with them:
● JSTRT (Start of conversion for injected group channels)
● STRT (Start of conversion for regular group channels
470/519
www.BDTIC.com/ST
UM0306 Analog/digital converter (ADC)
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw rw rw rw rw
471/519
www.BDTIC.com/ST
Analog/digital converter (ADC) UM0306
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
AWD AWD
Reserved Reserved DUALMOD[3:0]
EN ENJ
rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
472/519
www.BDTIC.com/ST
UM0306 Analog/digital converter (ADC)
473/519
www.BDTIC.com/ST
Analog/digital converter (ADC) UM0306
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
TS SW SW EXT
Reserved EXTSEL[2:0] Res.
VREFE START STARTJ TRIG
rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
JEXTT RST
JEXTSEL[2:0] ALIGN Reserved DMA Reserved CAL CONT ADON
RIG CAL
rw rw rw rw rw rw rw rw rw rw
474/519
www.BDTIC.com/ST
UM0306 Analog/digital converter (ADC)
475/519
www.BDTIC.com/ST
Analog/digital converter (ADC) UM0306
476/519
www.BDTIC.com/ST
UM0306 Analog/digital converter (ADC)
rw rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
SMP
SMP14[2:0] SMP13[2:0] SMP12[2:0] SMP11[2:0] SMP10[2:0]
15_0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
477/519
www.BDTIC.com/ST
Analog/digital converter (ADC) UM0306
rw rw rw rw rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
SMP
SMP4[2:0] SMP3[2:0] SMP2[2:0] SMP1[2:0] SMP0[2:0]
5_0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
478/519
www.BDTIC.com/ST
UM0306 Analog/digital converter (ADC)
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved JOFFSETx[11:0]
rw rw rw rw rw rw rw rw rw rw rw rw
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved HT[11:0]
rw rw rw rw rw rw rw rw rw rw rw rw
479/519
www.BDTIC.com/ST
Analog/digital converter (ADC) UM0306
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved LT[11:0]
rw rw rw rw rw rw rw rw rw rw rw rw
480/519
www.BDTIC.com/ST
UM0306 Analog/digital converter (ADC)
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
SQ16_
SQ15[4:0] SQ14[4:0] SQ13[4:0]
0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
481/519
www.BDTIC.com/ST
Analog/digital converter (ADC) UM0306
rw rw rw rw rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
SQ10_
SQ9[4:0] SQ8[4:0] SQ7[4:0]
0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
rw rw rw rw rw rw rw rw rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
482/519
www.BDTIC.com/ST
UM0306 Analog/digital converter (ADC)
rw rw rw rw rw rw
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
JSQ4_0
rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw rw
483/519
www.BDTIC.com/ST
Analog/digital converter (ADC) UM0306
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
JDATA[15:0]
r r r r r r r r r r r r r r r r
ADC2DATA[15:0]
r r r r r r r r r r r r r r r r
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
DATA[15:0]
r r r r r r r r r r r r r r r r
484/519
www.BDTIC.com/ST
UM0306 Analog/digital converter (ADC)
9
8
7
6
5
4
3
2
1
0
JSTRT
JEOC
STRT
AWD
EOC
ADC_SR
00h Reserved
Reset Value 0 0 0 0 0
AWD SGL
JDISCEN
JAWDEN
JEOC IE
DISCEN
AWDEN
AWDIE
JAUTO
EOCIE
Reserved
SCAN
DUALMOD DISC
ADC_CR1 AWDCH[4:0]
04h Reserved [3:0] NUM [2:0]
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
JSWSTART
JEXTTRIG
SWSTART
TSVREFE
EXTTRIG
RSTCAL
ALIGN
Reserved
Reserved
ADON
CONT
EXTSEL JEXTSEL
DMA
CAL
ADC_CR2
08h Reserved [2:0] [2:0] Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ADC_JOFR1 JOFFSET1[11:0]
14h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0
ADC_JOFR2 JOFFSET2[11:0]
18h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0
ADC_JOFR3 JOFFSET3[11:0]
1Ch Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0
ADC_JOFR4 JOFFSET4[11:0]
20h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0
ADC_HTR HT[11:0]
24h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0
ADC_LTR LT[11:0]
28h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0
485/519
www.BDTIC.com/ST
Analog/digital converter (ADC) UM0306
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
ADC_JDR1 JDATA[15:0]
3Ch Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ADC_JDR2 JDATA[15:0]
40h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ADC_JDR3 JDATA[15:0]
44h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ADC_JDR4 JDATA[15:0]
48h Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
486/519
www.BDTIC.com/ST
UM0306 Debug support (DBG)
20.1 Overview
The STM32F10x is built around a Cortex-M3 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
STM32F10x MCU.
Two interfaces for debug are available:
● Serial wire
● JTAG debug port
Cortex-M3 Data
System
Core interface
JTMS/
SWDIO External Private
TRACESWO
Peripheral Bus (PPB)
JTDI Trace Port
Bridge TPIU TRACECK
JTDO/
TRACESWO SWJ-DP AHB-AP
TRACED[3:0]
JNTRST Internal Private NVIC
Peripheral Bus (PPB)
JTCK/
SWCLK
DWT DBGMCU
FPB
ITM
Note: The debug features embedded in the Cortex-M3 core are a subset of the ARM CoreSight
Design Kit.
487/519
www.BDTIC.com/ST
Debug support (DBG) UM0306
The ARM Cortex-M3 core provides integrated on-chip debug support. It is comprised of:
● SWJ-DP: Serial wire / JTAG debug port
● AHP-AP: AHB access port
● ITM: Instrumentation trace macrocell
● FPB: Flash patch breakpoint
● DWT: Data watchpoint trigger
● TPUI: Trace port unit interface (available on larger packages, where the corresponding
pins are mapped)
It also includes debug features dedicated to STM32F10x:
● Flexible debug pinout assignment
● MCU debug box (support for low-power modes, control over peripheral clocks, etc.)
Note: For further information on debug functionality supported by the ARM Cortex-M3 core, refer
to the Cortex-M3 r1p1 Technical Reference Manual (TRM) and to the CoreSight Design Kit
r1p0 TRM.
488/519
www.BDTIC.com/ST
UM0306 Debug support (DBG)
In the SWJ-DP, the two JTAG pins of the SW-DP are multiplexed with some of the five JTAG
pins of the JTAG-DP.
TRACESWO (asynchronous trace)
SWJ-DP
JTDO TDO
TDO
TDI
JTDI TDI
nTRST
JNTRST nTRST
JTAG-DP
TCK
TMS
nPOTRST
From
SWD/JTAG power-on
select nPOTRST reset
DBGRESETn
SWDITMS
DBGDI
JTMS/SWDIO
SWDO
DBGDO
SW-DP
SWDOEN
DBGDOEN
SWCLKTCK
JTCK/SWCLK DBGCLK
The figure above shows that the asynchronous TRACE output (TRACESWO) is multiplexed
with TDO. This means that the asynchronous trace can only be used with SW-DP, not JTAG-
DP.
489/519
www.BDTIC.com/ST
Debug support (DBG) UM0306
490/519
www.BDTIC.com/ST
UM0306 Debug support (DBG)
Note: When the APB bridge write buffer is full, it takes one extra APB cycle when writing the
REMAP_AF register. This is because the deactivation of the JTAGSW pins is done in two
cycles to guarantee a clean level on the nTRST and TCK input signals of the core.
● Cycle 1: the JTAGSW input signals to the core are tied to 1 or 0 (to 1 for nTRST, TDI
and TMS, to 0 for TCK)
● Cycle 2: the GPI/O controller takes the control signals of the SWJTAG I/O pins (like
controls of direction, pull-up/down, Schmitt trigger activation, etc.).
491/519
www.BDTIC.com/ST
Debug support (DBG) UM0306
20.4.4 Using serial wire and releasing the unused debug pins as GPIOs
To use the serial wire DP to release some GPIOs, the user software must set
SWJ_CFG=010 just after reset. This release PA15, PB3 and PB4 which now become
available as GPIOs.
When debugging, the host performs the following actions:
● Under system RESET, all SWJ pins are assigned (JTAG-DP + SW-DP)
● Under system RESET, the debugger host sends the JTAG sequence to switch from the
JTAG-DP to the SW-DP.
● Still under system RESET, the debugger sets a breakpoint on vector reset
● The System Reset is released and the Core halts.
● All the debug communications from this point are done using the SW-DP. The other
JTAG pins can then be reassigned as GPIOs by the user software.
Note: For user software designs, note that:
To release the debug pins, remember that they will be first configured either in input-pull-up
(nTRST, TMS, TDI) or pull-down (TCK) or output tristate (TDO) for a certain duration after
reset until the instant when the user software releases the pins.
When debug pins (JTAG or SW or TRACE) are mapped, changing the corresponding I/O pin
configuration in the IOPORT controller has no effect.
492/519
www.BDTIC.com/ST
UM0306 Debug support (DBG)
STM32F10x
JNTRST
JTMS
SW-DP
Selected
DBGMCU_IDCODE
Address: 0xE0042000
Only 32-bits access supported
Read only = 0xXXXXX410 where X is undefined
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Reserved DEV_ID
r r r r r r r r r r r r
493/519
www.BDTIC.com/ST
Debug support (DBG) UM0306
20.7 This code is accessible by the JTAG Debug Port (4 to 5 pins) or by the SW Debug Port (two
pins) or by the user software.JTAG debug port
A standard JTAG state machine is implemented with a 4-bit Instruction Register (IR) and five
Data Registers (for full details, refer to the Cortex-M3 r1p1 Technical Reference Manual
(TRM):
BYPASS
1111
[1 bit]
IDCODE ID CODE
1110
[32 bits] 0x3BA00477 (ARM Cortex-M3 r1p1 ID Code)
Debug Port Access Register
This initiates a debug port and allows access to a debug port register.
– When transferring data IN:
Bits 34:3 = DATA[31:0] = 32-bit data to transfer for a write request
Bits 2:1 = A[3:2] = 2-bit address of a debug port register.
Bit 0 = RnW = Read request (1) or write request (0).
DPACC – When transferring data OUT:
1010
[35 bits] Bits 34:3 = DATA[31:0] = 32-bit data which is read following a read
request
Bits 2:0 = ACK[2:0] = 3-bit Acknowledge:
010 = OK/FAULT
001 = WAIT
OTHER = reserved
Refer to Table 72 for a description of the A(3:2) bits
494/519
www.BDTIC.com/ST
UM0306 Debug support (DBG)
Table 72. 32-bit debug port registers addressed through the shifted value A[3:2]
Address A(3:2) value Description
0x0 00 Reserved
DP CTRL/STAT register. Used to:
– Request a system or debug power-up
0x4 01 – Configure the transfer operation for AP accesses
– Control the pushed compare and pushed verify operations.
– Read some status flags (overrun, power-up acknowledges)
DP SELECT register: Used to select the current access port and the
active 4-words register window.
– Bits 31:24: APSEL: select the current AP
0x8 10 – Bits 23:8: reserved
– Bits 7:4: APBANKSEL: select the active 4-words register window on the
current AP
– Bits 3:0: reserved
DP RDBUFF register: Used to allow the debugger to get the final result
0xC 11 after a sequence of operations (without requesting new JTAG-DP
operation)
495/519
www.BDTIC.com/ST
Debug support (DBG) UM0306
Refer to the Cortex-M3 r1p1 TRM for a detailed description of DPACC and APACC registers.
The packet request is always followed by the turnaround time (default 1 bit) where neither
the host nor target drive the line.
496/519
www.BDTIC.com/ST
UM0306 Debug support (DBG)
001: FAULT
0..2 ACK 010: WAIT
100: OK
The ACK Response must be followed by a turnaround time only if it is a READ transaction or
if a WAIT or FAULT acknowledge has been received.
WDATA or
0..31 Write or Read data
RDATA
32 Parity Single parity of the 32 data bits
The DATA transfer must be followed by a turnaround time only if it is a READ transaction.
497/519
www.BDTIC.com/ST
Debug support (DBG) UM0306
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.
498/519
www.BDTIC.com/ST
UM0306 Debug support (DBG)
There are many AP Registers (see AHB-AP) addressed as the combination of:
● The shifted value A[3:2]
● The current value of the DP SELECT register
20.9 AHB-AP (AHB Access Port) - valid for both JTAG-DP or SW-
DP
Features:
● System access is independent of the processor status.
● Either SW-DP or JTAG-DP accesses AHB-AP.
● The AHB-AP is an AHB master into the Bus Matrix. Consequently, it can access all the
data buses (Dcode Bus, System Bus, internal and external PPB bus) but the ICode
bus.
● Bitband transactions are supported.
● AHB-AP transactions bypass the FPB.
The address of the 32-bits AHP-AP resisters are 6-bits wide (up to 64 words or 256 bytes)
and consists of:
a) Bits [8:4] = the bits[7:4] APBANKSEL of the DP SELECT register
b) Bits [3:2] = the 2 address bits of A(3:2) of the 35-bit packet request for SW-DP.
The AHB-AP of the Cortex-M3 includes 9 x 32-bits registers:
Address
Register Name Notes
offset
499/519
www.BDTIC.com/ST
Debug support (DBG) UM0306
It consists of 4 registers:
Register Description
Note: Important: these registers are not reset by a system reset. They are only reset by a power-
on reset.
Refer to the Cortex-M3 r1p1 TRM for further details.
To Halt on reset, it is necessary to:
● enable the bit0 (VC_CORRESET) of the Debug and Exception Monitor Control
Register
● enable the bit0 (C_DEBUGEN) of the Debug Halting Control and Status Register.
500/519
www.BDTIC.com/ST
UM0306 Debug support (DBG)
501/519
www.BDTIC.com/ST
Debug support (DBG) UM0306
The packets emitted by the ITM are output to the TPIU (Trace Port Interface Unit). The
formatter of the TPIU adds some extra packets (refer to TPIU) and then output the complete
packets sequence to the debugger host.
The bit TRCEN of the Debug Exception and Monitor Control Register must be enabled
before you program or use the ITM.
502/519
www.BDTIC.com/ST
UM0306 Debug support (DBG)
Example of configuration
To output a simple value to the TPIU:
● Configure the TPIU and enable the I/IO_TRACEN to assign TRACE I/Os in the MCU
● Write 0xC5ACCE55 to the ITM Lock Access Register to unlock the write access to the
ITM registers
● Write 0x00010005 to the ITM Trace Control Register to enable the ITM with Sync
enabled and an ATB ID different from 0x00
● Write 0x1 to the ITM Trace Enable Register to enable the Stimulus Port 0
● Write 0x1 to the ITM Trace Privilege Register to unmask stimulus ports 7:0
● Write the value to output in the Stimulus Port Register 0: this can be done by software
(using a printf function)
503/519
www.BDTIC.com/ST
Debug support (DBG) UM0306
For this, the debugger host must first set some debug configuration registers to change the
low-power mode behavior:
● In SLEEP mode, DBG_SLEEP bit of DBGMCU_CR register must be previously set by
the debugger. This will feed HCLK with the same clock that is provided to FCLK
(system clock previously configured by the software).
● In STOP mode, the bit DBG_STOP must be previously set by the debugger. This will
enable the internal RC oscillator clock to feed FCLK and HCLK in STOP mode.
DBGMCU_CR
Address: 0xE0042004
Only 32-bits access supported
POR Reset: 0x00000000 (not reset by system reset)
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
Reserved
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
DBG_
DBG_ DBG_ DBG_ DBG_ DBG_ DBG_ TRACE_ TRACE DBG_
WWDG DBG_ DBG_
Res. CAN_ TIM4_ TIM3_ TIM2_ TIM1_ IWDG MODE _ Reserved STAND
_ STOP SLEEP
STOP STOP STOP STOP STOP STOP [1:0] IOEN BY
STOP
rw rw rw rw rw rw rw rw rw rw rw rw rw
504/519
www.BDTIC.com/ST
UM0306 Debug support (DBG)
505/519
www.BDTIC.com/ST
Debug support (DBG) UM0306
20.16.1 Introduction
The TPIU acts as a bridge between the on-chip trace data from the ITM.
The output data stream encapsulates the trace source ID, that is then captured by a Trace
Port Analyzer (TPA).
The core embeds a simple TPIU, especially designed for low-cost debug (consisting of a
special version of the CoreSight TPIU).
The TPIU only supports ITM debug trace which is a limited trace as it only outputs
information coming from the ITM.
506/519
www.BDTIC.com/ST
UM0306 Debug support (DBG)
TPIU
TRACECLKIN
TRACECK
TRACESWO
507/519
www.BDTIC.com/ST
Debug support (DBG) UM0306
● Synchronous mode
The synchronous mode requires from 2 to 6 extra pins depending on the data trace
size and is only available in the larger packages. In addition it is available in JTAG mode
and in Serial Wire mode and provides better bandwidth output capabilities than
asynchronous trace.
Trace synchronous mode
STM32F10x pin
TPUI pin name
assignment
Type Description
508/519
www.BDTIC.com/ST
UM0306 Debug support (DBG)
DBGMCU_CR
TRACE I/O pin assigned
register
MODE[1:0]
TRACE_ Pins assigned for: PB3 /
TRACE_
IOEN PE2 / PE3 / PE4 / PE5 / PE6 /
JTDO/
TRACE TRACE TRACE TRACE TRACE
TRACES
CK D[0] D[1] D[2] D[3]
WO
Note: By default, the TRACECLKIN input clock of the TPIU is tied to GND. It is assigned to HCLK
two clock cycles after the bit TRACE_IOEN has been set.
The debugger must then program the Trace Mode by writing the bits PROTOCOL[1:0] of the
SPP_R (Selected Pin Protocol) register of the TPIU.
● PROTOCOL=00: Trace Port Mode (synchronous)
● PROTOCOL=01 or 10: Serial Wire (Manchester or NRZ) Mode (asynchronous mode).
Default state is 01
It then also configures the trace port size by writing the bits [3:0] of the CPSPS_R (Current
Sync Port Size Register) of the TPIU:
● 0x1 for 1 pin (default state)
● 0x2 for 2 pins
● 0x4 for 4 pins
509/519
www.BDTIC.com/ST
Debug support (DBG) UM0306
Note: Refer to the ARM CoreSight Architecture Specification v1.0 (ARM IHI 0029B) for further
information
Use of the formatter for STM32F10x MCU
For STM32F10x MCU, there is only one TRACE source (the ITM). But the formatter can not
be disabled and must be used in bypass mode because the TRACECTL pin is not assigned.
This way, the Trace Port Analyzer can decode part of the formatter protocol to determine the
position of the trigger.
510/519
www.BDTIC.com/ST
UM0306 Debug support (DBG)
Here, TRACECLKIN is driven internally and is connected to HCLK only when TRACE is
used.
Note: In this synchronous mode, it is not required to provide a stable clock frequency.
The TRACE I/Os (including TRACECK) are driven by the rising edge of TRACLKIN (equal to
HCLK). Consequently, the output frequency of TRACECK is equal to HCLK/2.
511/519
www.BDTIC.com/ST
Debug support (DBG) UM0306
512/519
www.BDTIC.com/ST
UM0306 Debug support (DBG)
9
8
7
6
5
4
3
2
1
0
0xE0042000
DBGMCU_
DEV_ID
IDCODE Reserved
Reset Value 0 1 0 0 0 0 0 1 0 0 0 0
DBG_WWDG_STOP
DBG_IWDG_STOP
DBG_TIM4_STOP
DBG_TIM3_STOP
DBG_TIM2_STOP
DBG_TIM1_STOP
DBG_CAN_STOP
DBG_STANDBY
TRACE_MODE
TRACE_IOEN
DBG_SLEEP
DBG_STOP
0xE0042004
Reserved
[1:0]
DBGMCU_CR
Reserved
Reset Value 0 0 0 0 0 0 0 0 0 0 0 0 0
513/519
www.BDTIC.com/ST
Revision history UM0306
21 Revision history
514/514
www.BDTIC.com/ST
UM0306 Index
Index
A
ADC_CR1 . . . . . . . . . . . . . . . . . . . . . . . . . . .472
ADC_CR2 . . . . . . . . . . . . . . . . . . . . . . . . . . .474
ADC_DR . . . . . . . . . . . . . . . . . . . . . . . . . . . .484
ADC_HTR . . . . . . . . . . . . . . . . . . . . . . . . . . .479
ADC_JDRx . . . . . . . . . . . . . . . . . . . . . . . . . . .484
ADC_JOFRx . . . . . . . . . . . . . . . . . . . . . . . . .479
ADC_JSQR . . . . . . . . . . . . . . . . . . . . . . . . . .483
ADC_LTR . . . . . . . . . . . . . . . . . . . . . . . . . . . .480
ADC_SMPR1 . . . . . . . . . . . . . . . . . . . . . . . . .477
ADC_SMPR2 . . . . . . . . . . . . . . . . . . . . . . . . .478
ADC_SQR1 . . . . . . . . . . . . . . . . . . . . . . . . . .481
ADC_SQR2 . . . . . . . . . . . . . . . . . . . . . . . . . .482
ADC_SQR3 . . . . . . . . . . . . . . . . . . . . . . . . . .482
ADC_SR . . . . . . . . . . . . . . . . . . . . . . . . . . . . .471
AFIO_EVCR . . . . . . . . . . . . . . . . . . . . . . . . . . .91
AFIO_EXTICR1 . . . . . . . . . . . . . . . . . . . . . . . .95
AFIO_EXTICR2 . . . . . . . . . . . . . . . . . . . . . . . .95
AFIO_EXTICR3 . . . . . . . . . . . . . . . . . . . . . . . .96
AFIO_EXTICR4 . . . . . . . . . . . . . . . . . . . . . . . .96
AFIO_MAPR . . . . . . . . . . . . . . . . . . . . . . . . . .92
B
BKP_CR . . . . . . . . . . . . . . . . . . . . . . . . . . . . .135
BKP_CSR . . . . . . . . . . . . . . . . . . . . . . . . . . .135
BKP_DRx . . . . . . . . . . . . . . . . . . . . . . . . . . . .134
BKP_RTCCR . . . . . . . . . . . . . . . . . . . . . . . . .134
C
CAN_BTR . . . . . . . . . . . . . . . . . . . . . . . . . . .313
CAN_ESR . . . . . . . . . . . . . . . . . . . . . . . . . . .312
CAN_FA0R . . . . . . . . . . . . . . . . . . . . . . . . . .322
CAN_FFA0R . . . . . . . . . . . . . . . . . . . . . . . . .322
CAN_FM0R . . . . . . . . . . . . . . . . . . . . . . . . . .321
CAN_FMR . . . . . . . . . . . . . . . . . . . . . . . . . . .320
CAN_FS0R . . . . . . . . . . . . . . . . . . . . . . . . . .321
CAN_FxR . . . . . . . . . . . . . . . . . . . . . . . . . . . .322
CAN_IER . . . . . . . . . . . . . . . . . . . . . . . . . . . .310
CAN_MCR . . . . . . . . . . . . . . . . . . . . . . . . . . .300
CAN_MSR . . . . . . . . . . . . . . . . . . . . . . . . . . .303
CAN_RDHxR . . . . . . . . . . . . . . . . . . . . . . . . .319
CAN_RDLxR . . . . . . . . . . . . . . . . . . . . . . . . .318
CAN_RDTxR . . . . . . . . . . . . . . . . . . . . . . . . .318
CAN_RF0R . . . . . . . . . . . . . . . . . . . . . . . . . .308
CAN_RF1R . . . . . . . . . . . . . . . . . . . . . . . . . .309
CAN_RIxR . . . . . . . . . . . . . . . . . . . . . . . . . . .317
515/519
www.BDTIC.com/ST
Index UM0306
CAN_TDHxR . . . . . . . . . . . . . . . . . . . . . . . . .316
CAN_TDLxR . . . . . . . . . . . . . . . . . . . . . . . . .315
CAN_TDTxR . . . . . . . . . . . . . . . . . . . . . . . . .315
CAN_TIxR . . . . . . . . . . . . . . . . . . . . . . . . . . .314
CAN_TSR . . . . . . . . . . . . . . . . . . . . . . . . . . .305
D
DBGMCU_CR . . . . . . . . . . . . . . . . . . . . . . . .504
DBGMCU_IDCODE . . . . . . . . . . . . . . . . . . . .493
DMA_CMARx . . . . . . . . . . . . . . . . . . . . . . . . .118
DMA_CNDTRx . . . . . . . . . . . . . . . . . . . . . . . .117
DMA_CPARx . . . . . . . . . . . . . . . . . . . . . . . . .118
DMA_IFCR . . . . . . . . . . . . . . . . . . . . . . . . . . .115
DMA_ISR . . . . . . . . . . . . . . . . . . . . . . . . . . . .113
E
EXTI_EMR . . . . . . . . . . . . . . . . . . . . . . . . . . .104
EXTI_FTSR . . . . . . . . . . . . . . . . . . . . . . . . . .105
EXTI_IMR . . . . . . . . . . . . . . . . . . . . . . . . . . . .104
EXTI_PR . . . . . . . . . . . . . . . . . . . . . . . . . . . .107
EXTI_RTSR . . . . . . . . . . . . . . . . . . . . . . . . . .105
EXTI_SWIER . . . . . . . . . . . . . . . . . . . . . . . . .106
G
GPIOx_BRR . . . . . . . . . . . . . . . . . . . . . . . . . . .85
GPIOx_BSRR . . . . . . . . . . . . . . . . . . . . . . . . .84
GPIOx_CRH . . . . . . . . . . . . . . . . . . . . . . . . . . .81
GPIOx_CRL . . . . . . . . . . . . . . . . . . . . . . . . . . .80
GPIOx_IDR . . . . . . . . . . . . . . . . . . . . . . . . . . .82
GPIOx_LCKR . . . . . . . . . . . . . . . . . . . . . . . . . .86
GPIOx_ODR . . . . . . . . . . . . . . . . . . . . . . . . . .83
I
I2C_CCR . . . . . . . . . . . . . . . . . . . . . . . . . . . .356
I2C_CR1 . . . . . . . . . . . . . . . . . . . . . . . . . . . .345
I2C_CR2 . . . . . . . . . . . . . . . . . . . . . . . . . . . .348
I2C_DR . . . . . . . . . . . . . . . . . . . . . . . . . . . . .350
I2C_OAR1 . . . . . . . . . . . . . . . . . . . . . . . . . . .349
I2C_OAR2 . . . . . . . . . . . . . . . . . . . . . . . . . . .349
I2C_SR1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .351
I2C_SR2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .354
I2C_TRISE . . . . . . . . . . . . . . . . . . . . . . . . . . .357
IWDG_KR . . . . . . . . . . . . . . . . . . . . . . . . . . .139
IWDG_PR . . . . . . . . . . . . . . . . . . . . . . . . . . .141
IWDG_RLR . . . . . . . . . . . . . . . . . . . . . . . . . .142
IWDG_SR . . . . . . . . . . . . . . . . . . . . . . . . . . .142
516/519
www.BDTIC.com/ST
UM0306 Index
P
PWR_CR . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41
PWR_CSR . . . . . . . . . . . . . . . . . . . . . . . . . . . .43
R
RCC_AHBENR . . . . . . . . . . . . . . . . . . . . . . . .64
RCC_APB1ENR . . . . . . . . . . . . . . . . . . . . . . . .67
RCC_APB1RSTR . . . . . . . . . . . . . . . . . . . . . .62
RCC_APB2ENR . . . . . . . . . . . . . . . . . . . . . . . .65
RCC_APB2RSTR . . . . . . . . . . . . . . . . . . . . . .60
RCC_BDCR . . . . . . . . . . . . . . . . . . . . . . . . . . .69
RCC_CFGR . . . . . . . . . . . . . . . . . . . . . . . . . . .54
RCC_CIR . . . . . . . . . . . . . . . . . . . . . . . . . . . . .57
RCC_CR . . . . . . . . . . . . . . . . . . . . . . . . . . . . .52
RCC_CSR . . . . . . . . . . . . . . . . . . . . . . . . . . . .70
RTC_ALRH . . . . . . . . . . . . . . . . . . . . . . . . . .131
RTC_ALRL . . . . . . . . . . . . . . . . . . . . . . . . . . .131
RTC_CNTH . . . . . . . . . . . . . . . . . . . . . . . . . .130
RTC_CNTL . . . . . . . . . . . . . . . . . . . . . . . . . .130
RTC_CRH . . . . . . . . . . . . . . . . . . . . . . . . . . .125
RTC_CRL . . . . . . . . . . . . . . . . . . . . . . . . . . . .126
RTC_DIVH . . . . . . . . . . . . . . . . . . . . . . . . . . .129
RTC_DIVL . . . . . . . . . . . . . . . . . . . . . . . . . . .129
RTC_PRLH . . . . . . . . . . . . . . . . . . . . . . . . . .128
RTC_PRLL . . . . . . . . . . . . . . . . . . . . . . . . . . .128
S
SPI_CR1 . . . . . . . . . . . . . . . . . . . . . . . . . . . .369
SPI_CR2 . . . . . . . . . . . . . . . . . . . . . . . . . . . .372
SPI_CRCPR . . . . . . . . . . . . . . . . . . . . . . . . . .374
SPI_DR . . . . . . . . . . . . . . . . . . . . . . . . . . . . .374
SPI_RXCRCR . . . . . . . . . . . . . . . . . . . . . . . .374
SPI_SR . . . . . . . . . . . . . . . . . . . . . . . . . . . . .373
SPI_TXCRCR . . . . . . . . . . . . . . . . . . . . . . . .375
T
TIM1_ARR . . . . . . . . . . . . . . . . . . . . . . . . . . .212
TIM1_BDTR . . . . . . . . . . . . . . . . . . . . . . . . . .216
TIM1_CCER . . . . . . . . . . . . . . . . . . . . . . . . . .209
TIM1_CCMR1 . . . . . . . . . . . . . . . . . . . . . . . .204
TIM1_CCMR2 . . . . . . . . . . . . . . . . . . . . . . . .208
TIM1_CCR1 . . . . . . . . . . . . . . . . . . . . . . . . . .213
TIM1_CCR2 . . . . . . . . . . . . . . . . . . . . . . . . . .214
TIM1_CCR3 . . . . . . . . . . . . . . . . . . . . . . . . . .214
TIM1_CCR4 . . . . . . . . . . . . . . . . . . . . . . . . . .215
TIM1_CNT . . . . . . . . . . . . . . . . . . . . . . . . . . .212
TIM1_CR1 . . . . . . . . . . . . . . . . . . . . . . . . . . .191
TIM1_CR2 . . . . . . . . . . . . . . . . . . . . . . . . . . .193
TIM1_DCR . . . . . . . . . . . . . . . . . . . . . . . . . . .218
517/519
www.BDTIC.com/ST
Index UM0306
TIM1_DIER . . . . . . . . . . . . . . . . . . . . . . . . . .198
TIM1_DMAR . . . . . . . . . . . . . . . . . . . . . . . . .218
TIM1_EGR . . . . . . . . . . . . . . . . . . . . . . . . . . .202
TIM1_PSC . . . . . . . . . . . . . . . . . . . . . . . . . . .212
TIM1_RCR . . . . . . . . . . . . . . . . . . . . . . . . . . .213
TIM1_SMCR . . . . . . . . . . . . . . . . . . . . . . . . .195
TIM1_SR . . . . . . . . . . . . . . . . . . . . . . . . . . . .200
TIMx_ARR . . . . . . . . . . . . . . . . . . . . . . . . . . .276
TIMx_CCER . . . . . . . . . . . . . . . . . . . . . . . . . .274
TIMx_CCMR1 . . . . . . . . . . . . . . . . . . . . . . . .268
TIMx_CCMR2 . . . . . . . . . . . . . . . . . . . . . . . .272
TIMx_CCR1 . . . . . . . . . . . . . . . . . . . . . . . . . .276
TIMx_CCR2 . . . . . . . . . . . . . . . . . . . . . . . . . .277
TIMx_CCR3 . . . . . . . . . . . . . . . . . . . . . . . . . .277
TIMx_CCR4 . . . . . . . . . . . . . . . . . . . . . . . . . .278
TIMx_CNT . . . . . . . . . . . . . . . . . . . . . . . . . . .275
TIMx_CR1 . . . . . . . . . . . . . . . . . . . . . . . . . . .257
TIMx_CR2 . . . . . . . . . . . . . . . . . . . . . . . . . . .259
TIMx_DCR . . . . . . . . . . . . . . . . . . . . . . . . . . .279
TIMx_DIER . . . . . . . . . . . . . . . . . . . . . . . . . . .263
TIMx_DMAR . . . . . . . . . . . . . . . . . . . . . . . . . .279
TIMx_EGR . . . . . . . . . . . . . . . . . . . . . . . . . . .267
TIMx_PSC . . . . . . . . . . . . . . . . . . . . . . . . . . .275
TIMx_SMCR . . . . . . . . . . . . . . . . . . . . . . . . . .260
TIMx_SR . . . . . . . . . . . . . . . . . . . . . . . . . . . .265
U
USART_BRR . . . . . . . . . . . . . . . . . . . . . . . . .409
USART_CR1 . . . . . . . . . . . . . . . . . . . . . . . . .410
USART_CR2 . . . . . . . . . . . . . . . . . . . . . . . . .413
USART_CR3 . . . . . . . . . . . . . . . . . . . . . . . . .415
USART_DR . . . . . . . . . . . . . . . . . . . . . . . . . .409
USART_GTPR . . . . . . . . . . . . . . . . . . . . . . . .417
USART_SR . . . . . . . . . . . . . . . . . . . . . . . . . .406
W
WWDG_CFR . . . . . . . . . . . . . . . . . . . . . . . . .148
WWDG_CR . . . . . . . . . . . . . . . . . . . . . . . . . .148
WWDG_SR . . . . . . . . . . . . . . . . . . . . . . . . . .149
518/519
www.BDTIC.com/ST
UM0306
Information in this document is provided solely in connection with ST products. STMicroelectronics NV and its subsidiaries (“ST”) reserve the
right to make changes, corrections, modifications or improvements, to this document, and the products and services described herein at any
time, without notice.
All ST products are sold pursuant to ST’s terms and conditions of sale.
Purchasers are solely responsible for the choice, selection and use of the ST products and services described herein, and ST assumes no
liability whatsoever relating to the choice, selection or use of the ST products and services described herein.
No license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted under this document. If any part of this
document refers to any third party products or services it shall not be deemed a license grant by ST for the use of such third party products
or services, or any intellectual property contained therein or considered as a warranty covering the use in any manner whatsoever of such
third party products or services or any intellectual property contained therein.
UNLESS OTHERWISE SET FORTH IN ST’S TERMS AND CONDITIONS OF SALE ST DISCLAIMS ANY EXPRESS OR IMPLIED
WARRANTY WITH RESPECT TO THE USE AND/OR SALE OF ST PRODUCTS INCLUDING WITHOUT LIMITATION IMPLIED
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE (AND THEIR EQUIVALENTS UNDER THE LAWS
OF ANY JURISDICTION), OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT.
UNLESS EXPRESSLY APPROVED IN WRITING BY AN AUTHORIZED ST REPRESENTATIVE, ST PRODUCTS ARE NOT
RECOMMENDED, AUTHORIZED OR WARRANTED FOR USE IN MILITARY, AIR CRAFT, SPACE, LIFE SAVING, OR LIFE SUSTAINING
APPLICATIONS, NOR IN PRODUCTS OR SYSTEMS WHERE FAILURE OR MALFUNCTION MAY RESULT IN PERSONAL INJURY,
DEATH, OR SEVERE PROPERTY OR ENVIRONMENTAL DAMAGE. ST PRODUCTS WHICH ARE NOT SPECIFIED AS "AUTOMOTIVE
GRADE" MAY ONLY BE USED IN AUTOMOTIVE APPLICATIONS AT USER’S OWN RISK.
Resale of ST products with provisions different from the statements and/or technical features set forth in this document shall immediately void
any warranty granted by ST for the ST product or service described herein and shall not create or extend in any manner whatsoever, any
liability of ST.
Information in this document supersedes and replaces all information previously supplied.
The ST logo is a registered trademark of STMicroelectronics. All other names are the property of their respective owners.
519/519
www.BDTIC.com/ST