STM32 Timer Overview (AN 4013) PDF
STM32 Timer Overview (AN 4013) PDF
Application note
STM32 cross-series timer overview
Introduction
The purpose of this document is detailed hereafter:
• Present an overview of the timer peripherals in the STM32 product series detailed in
Table 1.
• Describe the various modes and specific features of the timers, such as clock sources.
• Explain how to use the available modes and features.
• Explain how to compute the time base in each configuration.
• Describe the timer synchronization sequences and the advanced features for motor
control applications, in addition to the general-purpose timer modes.
For each mode, typical configurations are presented and examples of how to use the modes
are provided.
In the rest of this document (unless otherwise specified), the term STM32xx is used to refer
to the products listed in Table 1.
Contents
1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3 Timer synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.1 Timer system link . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.2 Master configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.3 Slave configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
6 Low-power timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
6.1 Used as a wakeup timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
6.2 Pulse counter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
7 Specific applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
7.1 Infrared application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
7.2 3-phase AC and PMSM control motor . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
7.3 Six-step mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
8 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
List of tables
List of figures
1 Overview
The STM32 devices are built-in with various types of timers, with the following features for
each:
• General-purpose timers are used in any application for output compare (timing and
delay generation), one-pulse mode, input capture (for external signal frequency
measurement), sensor interface (encoder, hall sensor)...
• Advanced timers: these timers have the most features. In addition to general purpose
functions, they include several features related to motor control and digital power
conversion applications: three complementary signals with deadtime insertion,
emergency shut-down input.
• One or two channel timers: used as general-purpose timers with a limited number of
channels.
• One or two channel timers with complementary output: same as previous type, but
having a deadtime generator on one channel. This allows having complementary
signals with a time base independent from the advanced timers.
• Basic timers have no input/outputs and are used either as timebase timers or for
triggering the DAC peripheral.
• Low-power timers are simpler than general purpose timers and their advantage is the
ability to continue working in low-power modes and generate a wake-up event.
• High-resolution timers are specialized timer peripherals designed to drive power
conversion in lighting and power source applications. It is however also usable in other
fields that require very fine timing resolution. AN4885 and AN4449 are practical
examples of high-resolution timer use.
Table 2 summarizes the STM32 family timers.
Table 3 presents a general overview of timer features.
AN4013
STM32
STM32 STM32
STM32 F101 STM32
F04x F030xB
F101 /F102 F2 STM32 STM32
/F070x6 /F030x8 STM32
/F102 /F103 STM32 /F4 STM32 F30X L05X
/F03x /F05x STM32 L03x STM32 STM32
/F103 /F105 F100 (exclu- F401 /F3x8 STM32 STM32 STM32 /L06x
Timer type (exclu- /F09x F7 /L02x L1 L4
lines /F107 value ding /F411 (exclu- F37x F334 F31x /L07x
ding /F07x Series /L01x Series Series
XL lines up line /F401, /F410 ding /L08x
/F030x8 (exclu- lines
density to high /F411, /F378) lines
and ding
(xF, xG) density /F410)
/F030x) F070x6)
(x4-xE)
TIM1
TIM1(1) TIM1(1) TIM1 TIM1 TIM1 TIM1
Advanced TIM1 TIM1 TIM1 TIM1 TIM8(1) - TIM1 - - -
TIM8(1) TIM8(1) TIM8 TIM8(1) TIM8 TIM8(1)
TIM20(1)
32- TIM2 TIM2(1) TIM2 TIM2 TIM2
TIM2 TIM2 - - - TIM2 TIM2 TIM2 - - TIM5(1)
bit TIM5 TIM5 TIM5 TIM5 TIM5(1)
General TIM2 TIM2 TIM2
purpose TIM3(1) TIM3 TIM2
16- TIM3 TIM3 TIM3 TIM3 TIM3(1) TIM3 TIM3 TIM2 TIM3(1)
TIM3 TIM3 TIM4(1) TIM4 TIM3 TIM2 TIM3
bit TIM4 TIM4(1) TIM4 TIM4 TIM4(1) TIM4 TIM4 TIM3(1) TIM4(1)
TIM19(1) TIM19 TIM4
TIM5 TIM5(1) TIM5(1)
TIM6
DocID022500 Rev 6
TIM6 TIM6 TIM6(1) TIM6 TIM6 TIM6 TIM6 TIM6 TIM6 TIM6 TIM6 TIM6
Basic - TIM6(1) TIM7 -
TIM7(1) TIM7 TIM7(1) TIM7 TIM7 TIM7(1) TIM7 TIM7(1) TIM7 TIM7(1) TIM7 TIM7
TIM18
TIM10 TIM10 TIM10
TIM11 TIM13(1) TIM11 TIM10(1) TIM13 TIM11 TIM10
1 channel TIM14 TIM14 - - - - - - -
TIM13 TIM14(1) TIM13 TIM11 TIM14 TIM13 TIM11
TIM14 TIM14 TIM14
TIM9 TIM9 TIM9 TIM21 TIM21
2-channel - - - TIM12(1) TIM9 - TIM12 - - TIM9 -
TIM12 TIM12 TIM12 TIM22 TIM22(1)
2-channel with
complementary - TIM15 - - TIM15 - - TIM15 TIM15 TIM15 TIM15 - - - - TIM15
output
1-channel with
TIM16 TIM16 TIM16 TIM16 TIM16 TIM16 TIM16 TIM16
complementary - - - - - - - -
TIM17 TIM17 TIM17 TIM17 TIM17 TIM17 TIM17 TIM17(1)
output
Low-power LPTIM1
- - - - - - LPTIM1(1) - - - - LPTIM1 LPTIM1 LPTIM1 -
timer LPTIM2
High-resolution
- - - - - - - - - HRTIM - - - - - -
timer
1. Not available on all products in the line. Please check the datasheet for details.
Note: More recent versions of advanced timers present several new modes: asymmetric mode, combined mode, one retriggerable
Overview
mode, combined 3 PWM mode and a second break input, these modes are available only for STM32L4/F7 Series and
STM32F30x/F3x8 lines.
7/39
Overview AN4013
Yes (OC
1-channel 16 bit Up No 1 0 No
signal)
Yes (OC
Low-power timer 16 bit Up No 1(3) 0 No
signal)
High-resolution
16 bit Up Yes 5(3) 5 Yes Yes
timer
1. With STM32L4/F7 series and STM32F30x/F3x8 lines the advanced timers have 6 channels. The two extra channels are
however not connected to GPIO (not available as output).
2. TIM2 and TIM5 are 32-bit counter resolution some products and 16-bit in others. See Table 2 or product datasheet as
reference.
3. Low-power timer and high-resolution timer do not have channels directly comparable with channels on regular timer
peripherals. Indicated number is a “channel equivalent”.
a. For the STM32F100/101/102/103/105/107 lines, polarity selection for both edges can be achieved by using
TI1F_ED, but only for TI1 input.
Particular case
To facilitate the input capture measurement, the timer counter is reset after each rising edge
detected on the timer input channel by:
• selecting TIxFPx as the input trigger by setting the TS bits in the SMCR register
• selecting the reset mode as the slave mode by configuring the SMS bits in the SMCR
register
Using this configuration, when an edge is detected, the counter is reset and the period of the
external signal is automatically given by the value on the CCRx register. This method is
used only with channel 1 or channel 2.
In this case, the input capture prescaler (ICPSC) is not considered in the period
computation.
The period is computed as follows:
Period = CCRx /(TIMx_CLK *(PSC+1)* polarity_index(1))
The polarity index is 1 if rising or falling edge is used, and 2 if both edges are used.
Many STM32Cube firmwares package include examples in
Examples\TIM\TIM_InputCapture subfolder.
PWM mode 1
• In up-counting, channelx is active as long as CNT< CCRx, otherwise it is inactive.
• In down-counting, channelx is inactive as long as CNT> CCRx, otherwise it is active.
PWM mode 2
• In up-counting, channelx is inactive as long as CNT < CCRx, otherwise it is active.
• In down-counting, channelx is active as long as CNT > CCRx, otherwise it is inactive.
Note: Active when OCREF = 1, inactive when OCREF = 0.
To configure the timer in this mode:
1. Configure the output pin:
a) Select the output mode by writing CCS bits in CCMRx register.
b) Select the polarity by writing the CCxP bit in CCER register.
2. Select the PWM mode (PWM1 or PWM2) by writing OCxM bits in CCMRx register.
3. Program the period and the duty cycle respectively in ARR and CCRx registers.
4. Set the preload bit in CCMRx register and the ARPE bit in the CR1 register.
5. Select the counting mode:
a) PWM edge-aligned mode: the counter must be configured up-counting or down-
counting.
b) PWM center aligned mode: the counter mode must be center aligned counting
mode (CMS bits different from '00').
6. Enable the capture compare.
7. Enable the counter.
For more details on using the timer in this mode, refer to the STM32Cube F3 firmware
package examples in the Examples\TIM\TIM_PWMOutput subfolders.
reference is identical to OC1/3REF, when the counter is down counting, the output
reference is identical to OC2/4REF
• OCxM = 1111 to use the Asymmetric PWM2, in this mode the output reference has the
same behavior as in PWM2 mode. When the counter is counting up the output
reference is identical to OC1/3REF, when the counter is down counting, the output
reference is identical to OC2/4REF
The following figure resumes the asymmetric behavior versus the center aligned PWM
mode:
3:0FHQWHUDOLJQHG$55 DQG
&&5\
069
2&5HI3:0
2&5HI3:0
2&&5HI
&RPELQHG3:0
2&5HI3:0
2&5HI3:0
2&&5HI
&RPELQHG3:0
069
7ULJJHU
2&[
069
3 Timer synchronization
7,0[
7,0[B&/.
,75
,75 75&
7ULJJHULQSXWV ,75
,75
,75 75*, 75*2 7ULJJHU
7ULJJHU
RXWSXW
FRQWURO
7,)('
7,)3
7,)3
069
1
x 1 0 0 Output disabled Output disabled
OCxREF +
x 1 1 0 Off-state
Polarity
OCxREF (not OCxREF)
Motor control
x 1 1 1 + Polarity + Polarity
(sinewave)
+ Deadtime + Deadtime
0 x 0 0
0 x 0 1 Outputs
Output disable disconnected
0 x 1 0 from I/O ports
0 x 1 1
0
1 x 0 0
Off-state
1 x 0 1 All PWMs OFF
(outputs are first forced with their (low Z for safe
1 x 1 0 inactive level then forced to their stop)
idle level after the deadtime.)
1 x 1 1
Note: 1 Dead time insertion is enabled by setting both CCxE and CCxNE bits, and the MOE bit.
2 When only OCxN is enabled (CCxE=0, CCxNE=1), it is not complemented and becomes
active as soon as OCxREF is high. For example, if CCxNP=0 then OCxN=OCxRef. On the
other hand, when both OCx and OCxN are enabled (CCxE=CCxNE=1) OCx becomes
active when OCxREF is high, whereas OCxN is complemented and becomes active when
OCxREF is low.
2&5HI
2&5HI
2&5HI
2&5HI
2&5HI&RPE
2&5HI&RPE
2&5HI&RPE
069
2&5()
&+
'HOD\
&+1
'HOD\
069
The dead time parameter is computed using the DTG[7:0] bits and the dead time clock
(Tdtg).
The dead time clock is computed as follows:
Tdtg = TDTS, if DTG[7] = 0
Tdtg = 2 x TDTS, if DTG[6] = 0
Tdtg = 8 x TDTS, if DTG[5] = 0
Tdtg = 16 x TDTS, if DTG[7:5] = 111
The following table presents the priorities between the two break inputs.
ON after deadtime
Active Inactive OFF
insertion
Inactive Active OFF OFF
ON after deadtime
Active - OFF
insertion
Note: The LOCK bits can be written only once after the reset. Once the BDTR register has been
written, its content is frozen until the next reset.
7,
7,
&RXQWHU
8S 'RZQ 8S
',5ELW
069
The timer's counter is incremented or decremented for each transition on the selected input
TI1 or TI2.
7,
7,
&RXQWHU
8S 'RZQ 8S
',5ELW
069
Note: The counter in case of resolution X2 can also be incremented on the TI1 edge.
In STM32 timer encoder interface mode, the encoder mode3 corresponds to resolution X4.
In this mode, the counter counts up/down on both TI1 and TI2 edges.
The resolution X2 is selected when encoder mode 1 or mode 2 is selected, that is, the
counter counts up/down on TI2 edge depending on the TI1 level, or the counter counts
up/down on TI1 edge depending on TI2 level.
Hall sensor
The Hall sensor is a type of sensor based on Hall effect: when a conductor is placed in a
magnetic field, a voltage will be generated perpendicular to both the current and the
magnetic field.
There are four types of Hall sensor IC devices that provide a digital output: unipolar
switches, bipolar switches, omni polar switches, and latches. The main difference between
them is the output waveforms (pulse duration).
The digital Hall sensor provides a digital output in relation to the magnetic field to which it is
exposed. When the magnetic field increases and is greater than the BRP (magnetic field
release point value), the output will be ON. When the magnetic field decreases and is lower
than the BOP (magnetic field operate point value) the output will be OFF.
Figure 9 presents the output waveform of a typical Hall sensor.
%5)
6HQVRURXWSXW
069
Generally, the Hall sensor is used in the three-phase motor control. Figure 10 presents the
commutation sequence.
+DOO
+DOO
+DOO
0RWRUSRVLWLRQ
069
The slave mode controller is configured in reset mode; the slave input is TI1F_ED. Thus,
each time one of the 3 inputs toggles, the counter restarts counting from 0. This creates a
time base triggered by any change on the Hall inputs.
Channel 1 is configured in input capture mode, capture signal is TRC. The captured value,
which corresponds to the time, elapsed between 2 changes on the inputs, gives information
about motor speed.
TIM configuration in Hall sensor interface mode
1. Configure 3 timer inputs ORed to the TI1 input channel by writing the TI1S bit in
TIMx_CR2 register to '1',
2. Program the time base: write the TIMx_ARR to the max value (the counter must
cleared by the TI1 change. Set the prescaler to get a maximum counter period longer
than the time between 2 changes on the sensors,
3. Program channel 1 in capture mode (TRC selected): write the CC1S bits in the
TIMx_CCMR1 register to '01'. The user can also program the digital filter if needed.
The high-resolution timer was designed specifically to control power conversion systems in
switch mode power supplies of lighting systems.Though it really excels in this role, it can of
course be used in other applications with high requirements for timer resolution.
The HRTIM features up to 10 outputs which can be configured in various coupled and
autonomous modes using five timing units tied to a common master for synchronization
purposes. The synchronization with other timers is also facilitated. The HRTIM is strongly
tied to ADCs and fault inputs for feedback purposes.
For more information about the high-resolution timer, read the reference manual of the
particular MCU line.
Application related information can be found in “High brightness LED dimming using the
STM32F334 Discovery kit“(AN4885) and “Buck-boost converter using the STM32F334
Discovery kit” (AN4449).
For practical examples check following STM32Cube package subfolders:
• Projects\STM32F3348-Discovery\Examples\HRTIM_BasicPWM
• Projects\STM32F3348-Discovery\Examples\HRTIM_BuckBoost
• Projects\STM32F3348-Discovery\Examples\HRTIM_BuckSyncRect
• Projects\STM32F3348-Discovery\Examples\HRTIM_DualBuck
• Projects\STM32F3348-Discovery\Examples\HRTIM_LLC_HalfBridge
• Projects\STM32F3348-Discovery\Examples\HRTIM_Multiphase
• Projects\STM32F3348-Discovery\Examples\HRTIM_Snippets
• Projects\STM32F3348-Discovery\Examples\HRTIM_TM_PFC.
6 Low-power timer
The main difference and advantage of the LPTIM compared to any other timer peripheral in
STM32 family of microcontrollers is the ability to continue working even in stop mode and
the possibility to generate events waking the MCU from the stop mode. Depending on the
selected clock source, the runtime power consumption can be substantially lower compared
to a general purpose timer. While it can perform a similar job as a general purpose timer,
let’s focus on task that it is better suited to do.
7 Specific applications
8 Revision history
STMicroelectronics NV and its subsidiaries (“ST”) reserve the right to make changes, corrections, enhancements, modifications, and
improvements to ST products and/or to this document at any time without notice. Purchasers should obtain the latest relevant information on
ST products before placing orders. ST products are sold pursuant to ST’s terms and conditions of sale in place at the time of order
acknowledgement.
Purchasers are solely responsible for the choice, selection, and use of ST products and ST assumes no liability for application assistance or
the design of Purchasers’ products.
Resale of ST products with provisions different from the information set forth herein shall void any warranty granted by ST for such product.
ST and the ST logo are trademarks of ST. All other product or service names are the property of their respective owners.
Information in this document supersedes and replaces information previously supplied in any prior versions of this document.