32. General-purpose timers(TIM2/TIM3/TIM4/TIM5)

32.1 TIM2/TIM3/TIM4/TIM5 introduction

The general-purpose timers consist of a 16-bit or 32-bit autoreload counter driven by a programmable prescaler.

They can 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 32.4.27: Timer synchronization.

32.2 TIM2/TIM3/TIM4/TIM5 main features

General-purpose TIMx timer features include:

32.3 TIM2/TIM3/TIM4/TIM5 implementation

Table 306. Timer instance implementation on STM32C5

Timer instanceSTM32C53x/C54xSTM32C55x/C56xSTM32C59x/C5Ax
TIM2XXX
TIM3--X
TIM4--X
TIM5XXX

Table 307. STM32C5 general purpose timers

Timer instanceTIM2TIM3TIM4TIM5
Resolution32-bit32-bit32-bit32-bit
OCREF clear selectionYesYesYesYes
Sourcestim_etr
tim_ocref_clr
tim_etr
tim_ocref_clr
tim_etr
tim_ocref_clr
tim_etr
tim_ocref_clr

32.4 TIM2/TIM3/TIM4/TIM5 functional description

32.4.1 Block diagram

Figure 325. General-purpose timer block diagram

Figure 325: General-purpose timer block diagram showing internal connections, slave controller, encoder interface, PSC prescaler, CNT counter, Auto-reload register, Capture/Compare registers 1 to 4, input filter/edge detector, and output control logic.

Notes:

MSv75880V1

Figure 325: General-purpose timer block diagram showing internal connections, slave controller, encoder interface, PSC prescaler, CNT counter, Auto-reload register, Capture/Compare registers 1 to 4, input filter/edge detector, and output control logic.

1. This feature is not available on all timers, refer to Section 32.3: TIM2/TIM3/TIM4/TIM5 implementation.

32.4.2 TIM2/TIM3/TIM4/TIM5 pins and internal signals

Table 308 and Table 309 in this section summarize the TIM inputs and outputs.

Table 308. TIM input/output pins

Pin nameSignal typeDescription
TIM_CH1
TIM_CH2
TIM_CH3
TIM_CH4
Input/outputTimer multi-purpose channels.
Each channel be used for capture, compare, or PWM.
TIM_CH1 and TIM_CH2 can also be used as external clock (below 1/4 of the tim_ker_ck clock) , external trigger and quadrature encoder inputs.
TIM_CH1, TIM_CH2 and TIM_CH3 can be used to interface with digital hall effect sensors.
TIM_ETRInputExternal trigger input. This input can be used as external trigger or as external clock source. This input can receive a clock with a frequency higher than the tim_ker_ck if the tim_etr_in prescaler is used.

Table 309. TIM internal input/output signals

Internal signal nameSignal typeDescription
tim_ti1_in[15:0]
tim_ti2_in[15:0]
tim_ti3_in[15:0]
tim_ti4_in[15:0]
InputInternal timer inputs bus. The tim_ti1_in[15:0] and tim_ti2_in[15:0] inputs can be used for capture or as external clock (below 1/4 of the tim_ker_ck clock) and for quadrature encoder signals.
tim_etr[31:0]InputExternal trigger internal input bus. These inputs can be used as trigger, external clock or for hardware cycle-by-cycle pulse width control. These inputs can receive clock with a frequency higher than the tim_ker_ck if the tim_etr_in prescaler is used.
tim_itr[27:0]InputInternal trigger input bus. These inputs can be used for the slave mode controller or as a input clock (below 1/4 of the tim_ker_ck clock).
tim_trgoOutputInternal trigger output. This trigger can trigger other on-chip peripherals.
tim_ocref_clr[15:0]InputTimer tim_ocref_clr input bus. These inputs can be used to clear the tim_ocxref signals, typically for hardware cycle-by-cycle pulse width control.
tim_pclkInputTimer APB clock.
tim_ker_ckInputTimer kernel clock
Table 309. TIM internal input/output signals (continued)
Internal signal nameSignal typeDescription
tim_itOutputGlobal Timer interrupt, gathering capture/compare, update and break trigger requests.
tim_cc1_dma
tim_cc2_dma
tim_cc3_dma
tim_cc4_dma
OutputTimer capture/compare [4:1] dma requests.
tim_upd_dmaOutputTimer update dma request.
tim_trgi_dmaOutputTimer trigger dma request.

Tables below list the sources connected to the tim_ti[4:1] input multiplexers.

Table 310. Interconnect to the tim_ti1 input multiplexer
tim_ti1 inputsSources
TIM2TIM3TIM4TIM5
tim_ti1_in0TIM2_CH1TIM3_CH1TIM4_CH1TIM5_CH1
tim_ti1_in1comp1_out
tim_ti1_in2comp2_out(1)/
eth_ptp_pps_o(2)
eth_ptp_pps_oReserved
tim_ti1_in3LSIReserved
tim_ti1_in4LSEReserved
tim_ti1_in5rtc_wut_trgReserved
tim_ti1_in6TIM5_CH1Reserved
tim_ti1_in7fdcan1_rxeof_evtfdcan2_rxeof_evtReserved
tim_ti1_in[15:8]Reserved

1. comp2_out is only available on STM32C53x/542 devices.

2. eth_ptp_pps_o is only available on STM32C59x/5A3 devices.

Table 311. Interconnect to the tim_ti2 input multiplexer
tim_ti2 inputsSources
TIM2TIM3TIM4TIM5
tim_ti2_in0TIM2_CH2TIM3_CH2TIM4_CH2TIM5_CH2
tim_ti2_in1Reserved
tim_ti2_in2Reserved
tim_ti2_in3hse_1M_ckReserved
tim_ti2_in4MCO1Reserved
tim_ti2_in5MCO2Reserved
Table 311. Interconnect to the tim_ti2 input multiplexer (continued)
tim_ti2 inputsSources
TIM2TIM3TIM4TIM5
tim_ti2_in6Reserved
tim_ti2_in7fdcan1_txeof_evtfdcan2_txeof_evtReserved
tim_ti2_in[15:8]Reserved
Table 312. Interconnect to the tim_ti3 input multiplexer
tim_ti3 inputsSources
TIM2TIM3TIM4TIM5
tim_ti3_in0TIM2_CH3TIM3_CH3TIM4_CH3TIM5_CH3
tim_ti3_in1Reserved
tim_ti3_in[6:2]Reserved
tim_ti3_in7fdcan2_rxeof_evtReserved
tim_ti3_in[15:8]Reserved
Table 313. Interconnect to the tim_ti4 input multiplexer
tim_ti4 inputsSources
TIM2TIM3TIM4TIM5
tim_ti4_in0TIM2_CH4TIM3_CH4TIM4_CH4TIM5_CH4
tim_ti4_in1comp1_outReserved
tim_ti4_in2comp2_outReserved
tim_ti4_in[6:3]Reserved
tim_ti4_in7fdcan2_txeof_evtReserved
tim_ti4_in[15:8]Reserved

The table below lists the internal sources connected to the tim_etr input multiplexer.

Table 314. TIMx internal trigger connection
TIMxTIM2TIM3TIM4TIM5
tim_itr0tim1_trgo
tim_itr1tim1_trgo2
tim_itr2Reservedtim2_trgo
tim_itr3tim3_trgoReservedtim3_trgo
tim_itr4tim4_trgoReservedtim4_trgo
tim_itr5tim5_trgoReserved

Table 314. TIMx internal trigger connection (continued)

TIMxTIM2TIM3TIM4TIM5
tim_itr6tim8_trgo
tim_itr7tim8_trgo2
tim_itr8tim12_trgo
tim_itr9tim15_trgo
tim_itr10tim16_oc1
tim_itr11tim17_oc1
tim_itr[27:12]Reserved

The table below lists the internal sources connected to the tim_etr input multiplexer.

Table 315. Interconnect to the tim_etr input multiplexer

Timer external trigger input signalTimer external trigger signal assignment
TIM2TIM3TIM4TIM5
tim_etr0TIM2_ETRTIM3_ETRTIM4_ETRTIM5_ETR
tim_etr1comp1_out
tim_etr2comp2_outReserved
tim_etr3adc1_awd1adc3_awd1
tim_etr4adc1_awd2adc3_awd2
tim_etr5adc1_awd3adc3_awd3
tim_etr6LSEReserved
tim_etr7MCO1Reserved
tim_etr8ReservedTIM2_ETR
tim_etr9TIM3_ETRReservedTIM3_ETR
tim_etr10TIM4_ETRReservedTIM4_ETR
tim_etr11TIM5_ETRReserved
tim_etr[31:12]Reserved

The table below lists the internal sources connected to the tim_ocref_clr input multiplexer.

Table 316. Interconnect to the tim_ocref_clr input multiplexer

Timer tim_ocref_clr signalTimer tim_ocref_clr signals assignment
TIM2TIM3TIM4TIM5
tim_ocref_clr0comp1_out
tim_ocref_clr1comp2_outReserved
tim_ocref_clr[15:2]Reserved

32.4.3 Time-base unit

The main block of the programmable timer is a 16-bit/32-bit counter with its related autoreload register. The counter can count up, down or both up and down. The counter clock can be divided by a prescaler.

The counter, the autoreload register and the prescaler register can be written or read by software. This is true even when the counter is running.

The time-base unit includes:

The autoreload register is preloaded. Writing to or reading from the autoreload register accesses the preload register. The content of the preload register is transferred into the shadow register permanently or at each update event (UEV), depending on the autoreload preload enable bit (ARPE) in TIM_CR1 register. The update event is sent when the counter reaches the overflow (or underflow when down-counting) and if the UDIS bit equals 0 in the TIM_CR1 register. It can also be generated by software. The generation of the update event is described in detail for each configuration.

Note: An update overrun status bit (UOVRF in TIM_SR register) indicates that an update request occurred while an update interrupt is pending (update interrupt flag set). This bit is useful when debugging closed loop applications, to indicate that:

- the update interrupt service routine is too long with respect to the interval between consecutive update events

- the update interrupt service routine was served too late with respect to the subsequent update event

The counter is clocked by the prescaler output tim_cnt_ck, which is enabled only when the counter enable bit (CEN) in TIM_CR1 register is set (refer also to the slave mode controller description to get more details on counter enabling).

Note: The counter starts counting 1 clock cycle after setting the CEN bit in the TIM_CR1 register.

Prescaler description

The prescaler can divide the counter clock frequency by any factor between 1 and 65536. It is based on a 16-bit counter controlled through a 16-bit/32-bit register (in the TIM_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 326 and Figure 327 give some examples of the counter behavior when the prescaler ratio is changed on the fly:

Figure 326. Counter timing diagram with prescaler division change from 1 to 2

Timing diagram for Figure 326 showing the transition of prescaler division from 1 to 2. It includes waveforms for tim_psc_ck, CEN, tim_cnt_ck, Counter register, Update event (UEV), Prescaler control register, Prescaler buffer, and Prescaler counter.

The diagram illustrates the timing of a prescaler division change from 1 to 2. The tim_psc_ck signal is the prescaler clock. The CEN signal is active low, starting low and then going high. The tim_cnt_ck signal is the counter clock, which is derived from tim_psc_ck after prescaling. The Counter register shows the counter value increasing from F7 to FC, then resetting to 00 and continuing to 03. The Update event (UEV) signal is a pulse that occurs when the counter reaches its maximum value (FC). The Prescaler control register shows a write operation from 0 to 1. The Prescaler buffer shows the new value (1) being loaded. The Prescaler counter shows the prescaler counter value increasing from 0 to 1, then resetting to 0 and continuing to 1. A note indicates "Write a new value in TIM_PSC" with an arrow pointing to the transition in the Prescaler control register.

MSv80061V1

Timing diagram for Figure 326 showing the transition of prescaler division from 1 to 2. It includes waveforms for tim_psc_ck, CEN, tim_cnt_ck, Counter register, Update event (UEV), Prescaler control register, Prescaler buffer, and Prescaler counter.

Figure 327. Counter timing diagram with prescaler division change from 1 to 4

Timing diagram for Figure 327 showing the transition of prescaler division from 1 to 4. It includes waveforms for tim_psc_ck, CEN, tim_cnt_ck, Counter register, Update event (UEV), Prescaler control register, Prescaler buffer, and Prescaler counter.

The diagram illustrates the timing of a prescaler division change from 1 to 4. The tim_psc_ck signal is the prescaler clock. The CEN signal is active low, starting low and then going high. The tim_cnt_ck signal is the counter clock, which is derived from tim_psc_ck after prescaling. The Counter register shows the counter value increasing from F7 to FC, then resetting to 00 and continuing to 01. The Update event (UEV) signal is a pulse that occurs when the counter reaches its maximum value (FC). The Prescaler control register shows a write operation from 0 to 3. The Prescaler buffer shows the new value (3) being loaded. The Prescaler counter shows the prescaler counter value increasing from 0 to 3, then resetting to 0 and continuing to 3. A note indicates "Write a new value in TIM_PSC" with an arrow pointing to the transition in the Prescaler control register.

MSv80062V1

Timing diagram for Figure 327 showing the transition of prescaler division from 1 to 4. It includes waveforms for tim_psc_ck, CEN, tim_cnt_ck, Counter register, Update event (UEV), Prescaler control register, Prescaler buffer, and Prescaler counter.

32.4.4 Counter modes

Up-counting mode

In up-counting mode, the counter counts from 0 to the autoreload value (content of the TIM_ARR register), then restarts from 0 and generates a counter overflow event.

An update event can be generated at each counter overflow or by setting the UG bit in the TIM_EGR register (by software or by using the slave mode controller).

The UEV event can be disabled by software by setting the UDIS bit in TIM_CR1 register. This is to avoid updating the shadow registers while writing new values in the preload registers. Then no update event occurs until the UDIS bit has been written to 0. However, the counter restarts from 0, as well as the counter of the prescaler (but the prescale rate does not change). In addition, if the URS bit (update request selection) in TIM_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 TIM_SR register) is set (depending on the URS bit):

The following figures show some examples of the counter behavior for different clock frequencies when TIM_ARR = 0x36.

Figure 328. Counter timing diagram, internal clock divided by 1

Timing diagram for counter up-counting mode showing the relationship between prescaler clock, counter clock, counter register values, counter overflow, update event, and update interrupt flag.

The diagram illustrates the timing of various signals during an up-counting operation. The counter register is shown incrementing from 31 to 36, then wrapping around to 00 and continuing up to 07. A counter overflow occurs at the transition from 36 to 00, which also triggers an update event (UEV) and sets the update interrupt flag (UIF). The prescaler clock (tim_psc_ck) and counter clock (tim_cnt_ck) are shown as square waves, with the counter clock starting after the CEN signal goes high.

SignalState / Value
tim_psc_ckContinuous square wave
CENHigh (enables counter)
tim_cnt_ckSquare wave starting after CEN goes high
Counter register31, 32, 33, 34, 35, 36, 00, 01, 02, 03, 04, 05, 06, 07
Counter overflowPulse at transition from 36 to 00
Update event (UEV)Pulse at transition from 36 to 00
Update interrupt flag (UIF)High after transition from 36 to 00

MSv50997V1

Timing diagram for counter up-counting mode showing the relationship between prescaler clock, counter clock, counter register values, counter overflow, update event, and update interrupt flag.

Figure 329. Counter timing diagram, internal clock divided by 2

Timing diagram for counter with internal clock divided by 2. It shows the relationship between tim_psc_ck, CEN, tim_cnt_ck, Counter register values (0034 to 0003), Counter overflow, Update event (UEV), and Update interrupt flag (UIF).

This timing diagram illustrates the operation of a counter when the internal clock is divided by 2. The tim_psc_ck signal is a continuous square wave. The CEN signal is a low-to-high transition that enables the counter. The tim_cnt_ck signal is a square wave with a period twice that of tim_psc_ck. The Counter register shows the sequence of values: 0034, 0035, 0036, 0000, 0001, 0002, 0003. A Counter overflow pulse occurs on the rising edge of tim_cnt_ck when the counter transitions from 0036 to 0000. This overflow triggers an Update event (UEV) pulse. The Update interrupt flag (UIF) is set high following the UEV.

MSv62300V1

Timing diagram for counter with internal clock divided by 2. It shows the relationship between tim_psc_ck, CEN, tim_cnt_ck, Counter register values (0034 to 0003), Counter overflow, Update event (UEV), and Update interrupt flag (UIF).

Figure 330. Counter timing diagram, internal clock divided by 4

Timing diagram for counter with internal clock divided by 4. It shows the relationship between tim_psc_ck, CEN, tim_cnt_ck, Counter register values (0035 to 0001), Counter overflow, Update event (UEV), and Update interrupt flag (UIF).

This timing diagram illustrates the operation of a counter when the internal clock is divided by 4. The tim_psc_ck signal is a continuous square wave. The CEN signal is a low-to-high transition that enables the counter. The tim_cnt_ck signal is a square wave with a period four times that of tim_psc_ck. The Counter register shows the sequence of values: 0035, 0036, 0000, 0001. A Counter overflow pulse occurs on the rising edge of tim_cnt_ck when the counter transitions from 0036 to 0000. This overflow triggers an Update event (UEV) pulse. The Update interrupt flag (UIF) is set high following the UEV.

MSv62301V1

Timing diagram for counter with internal clock divided by 4. It shows the relationship between tim_psc_ck, CEN, tim_cnt_ck, Counter register values (0035 to 0001), Counter overflow, Update event (UEV), and Update interrupt flag (UIF).

Figure 331. Counter timing diagram, internal clock divided by N

Timing diagram for Figure 331 showing tim_psc_ck, tim_cnt_ck, Counter register, Counter overflow, Update event (UEV), and Update interrupt flag (UIF).

This timing diagram illustrates the operation of a counter when the internal clock is divided by N. It shows the relationship between the prescaler clock (tim_psc_ck), the counter clock (tim_cnt_ck), the counter register value, and the resulting overflow and update events. The counter register is shown with values 1F, 20, and 00. The counter overflow occurs when the counter reaches its maximum value and wraps around. The update event (UEV) is generated upon overflow, and the update interrupt flag (UIF) is set accordingly.

MSv62302V1

Timing diagram for Figure 331 showing tim_psc_ck, tim_cnt_ck, Counter register, Counter overflow, Update event (UEV), and Update interrupt flag (UIF).

Figure 332. Counter timing diagram, Update event when ARPE = 0 (TIM_ARR not preloaded)

Timing diagram for Figure 332 showing tim_psc_ck, CEN, tim_cnt_ck, Counter register, Counter overflow, Update event (UEV), Update interrupt flag (UIF), and Auto-reload preload register.

This timing diagram shows the counter behavior when the auto-reload preload enable (ARPE) is 0, meaning the TIM_ARR register is not preloaded. It includes signals for the prescaler clock (tim_psc_ck), the counter enable (CEN), the counter clock (tim_cnt_ck), the counter register values (31 through 07), the counter overflow, the update event (UEV), the update interrupt flag (UIF), and the auto-reload preload register (FF to 36). A note indicates 'Write a new value in TIM_ARR' at the point where the preload register value changes from FF to 36.

MSv80063V1

Timing diagram for Figure 332 showing tim_psc_ck, CEN, tim_cnt_ck, Counter register, Counter overflow, Update event (UEV), Update interrupt flag (UIF), and Auto-reload preload register.

Figure 333. Counter timing diagram, Update event when ARPE = 1 (TIM_ARR preloaded)

Timing diagram for counter update event when ARPE = 1 (TIM_ARR preloaded).

The diagram illustrates the timing of various signals during a counter update event when ARPE = 1 (TIM_ARR preloaded). The signals shown are:

A vertical dashed line indicates the point where the counter reaches 00. A horizontal arrow points to the transition of the Auto-reload preload register from F5 to 36. The text "Write a new value in TIM_ARR" is located at the bottom left of the diagram area. The reference "MSv80064V1" is at the bottom right.

Timing diagram for counter update event when ARPE = 1 (TIM_ARR preloaded).

Down-counting mode

In down-counting mode, the counter counts from the autoreload value (content of the TIM_ARR register) down to 0, then restarts from the autoreload value and generates a counter underflow event.

An update event can be generated at each counter underflow or by setting the UG bit in the TIM_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 TIM_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 autoreload value, whereas the counter of the prescaler restarts from 0 (but the prescale rate does not change).

In addition, if the URS bit (update request selection) in TIM_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 TIM_SR register) is set (depending on the URS bit):

The following figures show some examples of the counter behavior for different clock frequencies when TIM_ARR = 0x36.

Figure 334. Counter timing diagram, internal clock divided by 1

Timing diagram showing the relationship between tim_psc_ck, CEN, tim_cnt_ck, Counter register values, Counter underflow (cnt_udf), Update event (UEV), and Update interrupt flag (UIF).

The diagram illustrates the timing of a counter relative to its prescaler clock and control signals. The counter register counts down from 05 to 00, then reloads with 36 and counts down to 2F. A counter underflow occurs when the counter reaches 00. An update event (UEV) is generated at the start of the next prescaler cycle after the underflow. The update interrupt flag (UIF) is set at the same time as the UEV.

SignalValue / State
Counter register05, 04, 03, 02, 01, 00, 36, 35, 34, 33, 32, 31, 30, 2F
Counter underflow (cnt_udf)Active low pulse at 00
Update event (UEV)Active low pulse after underflow
Update interrupt flag (UIF)Active low pulse after underflow

MSv62305V1

Timing diagram showing the relationship between tim_psc_ck, CEN, tim_cnt_ck, Counter register values, Counter underflow (cnt_udf), Update event (UEV), and Update interrupt flag (UIF).

Figure 335. Counter timing diagram, internal clock divided by 2

Timing diagram for counter with internal clock divided by 2. Shows tim_psc_ck, CEN, tim_cnt_ck, Counter register (0002 to 0033), Counter underflow, Update event (UEV), and Update interrupt flag (UIF).

This timing diagram illustrates the operation of a counter when the internal clock is divided by 2. The prescaler clock (tim_psc_ck) is a continuous square wave. The counter enable (CEN) signal is active low; it starts high, then transitions to low, and remains low for the duration of the counter operation shown. The counter clock (tim_cnt_ck) is derived from tim_psc_ck and is active low. The counter register shows the sequence of values: 0002, 0001, 0000, 0036, 0035, 0034, and 0033. A counter underflow occurs when the counter reaches 0000 and wraps around to 0036. This event triggers an update event (UEV), which in turn sets the update interrupt flag (UIF). The UIF is cleared after the update event is processed.

MSv62306V1

Timing diagram for counter with internal clock divided by 2. Shows tim_psc_ck, CEN, tim_cnt_ck, Counter register (0002 to 0033), Counter underflow, Update event (UEV), and Update interrupt flag (UIF).

Figure 336. Counter timing diagram, internal clock divided by 4

Timing diagram for counter with internal clock divided by 4. Shows tim_psc_ck, CEN, tim_cnt_ck, Counter register (0001 to 0001), Counter underflow, Update event (UEV), and Update interrupt flag (UIF).

This timing diagram illustrates the operation of a counter when the internal clock is divided by 4. The prescaler clock (tim_psc_ck) is a continuous square wave. The counter enable (CEN) signal is active low; it starts high, then transitions to low, and remains low. The counter clock (tim_cnt_ck) is derived from tim_psc_ck and is active low. The counter register shows the sequence of values: 0001, 0000, 0000, and 0001. A counter underflow occurs when the counter reaches 0000 and wraps around to 0001. This event triggers an update event (UEV), which in turn sets the update interrupt flag (UIF). The UIF is cleared after the update event is processed.

MSv62307V1

Timing diagram for counter with internal clock divided by 4. Shows tim_psc_ck, CEN, tim_cnt_ck, Counter register (0001 to 0001), Counter underflow, Update event (UEV), and Update interrupt flag (UIF).

Figure 337. Counter timing diagram, internal clock divided by N

Figure 337. Counter timing diagram, internal clock divided by N

Diagram signals and values:

Reference: MSv62308V1

Figure 337. Counter timing diagram, internal clock divided by N

Figure 338. Counter timing diagram, Update event

Figure 338. Counter timing diagram, Update event

Diagram signals and values:

Reference: MSv80065V1

Figure 338. Counter timing diagram, Update event

Center-aligned mode (up/down-counting)

In center-aligned mode, the counter counts from 0 to the autoreload value (content of the TIM_ARR register) – 1, generates a counter overflow event, then counts from the

autoreload value down to 1 and generates a counter underflow event. Then it restarts counting from 0.

Center-aligned mode is active when the CMS bits in TIM_CR1 register are not equal to 00. The output compare interrupt flag of channels configured in output is set when: the counter counts down (center aligned mode 1, CMS = 01), the counter counts up (center aligned mode 2, CMS = 10) the counter counts up and down (center aligned mode 3, CMS = 11).

In this mode, the direction bit (DIR from TIM_CR1 register) cannot be written. It is updated by hardware and gives the current direction of the counter.

The update event can be generated at each counter overflow and at each counter underflow or by setting the UG bit in the TIM_EGR register (by software or by using the slave mode controller) also generates an update event. In this case, the counter restarts counting from 0, as well as the counter of the prescaler.

The UEV update event can be disabled by software by setting the UDIS bit in TIM_CR1 register. This is to avoid updating the shadow registers while writing new values in the preload registers. Then no update event occurs until the UDIS bit has been written to 0. However, the counter continues counting up and down, based on the current autoreload value.

In addition, if the URS bit (update request selection) in TIM_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 TIM_SR register) is set (depending on the URS bit):

The following figures show some examples of the counter behavior for different clock frequencies.

Figure 339. Counter timing diagram, internal clock divided by 1, TIM_ARR = 0x6

Timing diagram for Figure 339 showing counter operation with internal clock divided by 1 and TIM_ARR = 0x6. The diagram includes waveforms for tim_psc_ck, CEN, tim_cnt_ck, Counter register, Counter underflow, Counter overflow, Update event (UEV), and Update interrupt flag (UIF). The counter register shows values 04, 03, 02, 01, 00, 01, 02, 03, 04, 05, 06, 05, 04, 03. Counter underflow occurs at 00, and counter overflow occurs at 06. Update events (UEV) occur at 00 and 06. The Update interrupt flag (UIF) is set after the first update event at 00.

MSv62310V1

Timing diagram for Figure 339 showing counter operation with internal clock divided by 1 and TIM_ARR = 0x6. The diagram includes waveforms for tim_psc_ck, CEN, tim_cnt_ck, Counter register, Counter underflow, Counter overflow, Update event (UEV), and Update interrupt flag (UIF). The counter register shows values 04, 03, 02, 01, 00, 01, 02, 03, 04, 05, 06, 05, 04, 03. Counter underflow occurs at 00, and counter overflow occurs at 06. Update events (UEV) occur at 00 and 06. The Update interrupt flag (UIF) is set after the first update event at 00.
  1. 1. Here, center-aligned mode 1 is used (for more details refer to Section 32.5.1: TIM control register 1 (TIM_CR1)).

Figure 340. Counter timing diagram, internal clock divided by 2

Timing diagram for Figure 340 showing counter operation with internal clock divided by 2. The diagram includes waveforms for tim_psc_ck, CEN, tim_cnt_ck, Counter register, Counter underflow, Update event (UEV), and Update interrupt flag (UIF). The counter register shows values 0003, 0002, 0001, 0000, 0001, 0002, 0003. Counter underflow occurs at 0000. Update events (UEV) occur at 0000. The Update interrupt flag (UIF) is set after the update event at 0000.

MSv62311V1

Timing diagram for Figure 340 showing counter operation with internal clock divided by 2. The diagram includes waveforms for tim_psc_ck, CEN, tim_cnt_ck, Counter register, Counter underflow, Update event (UEV), and Update interrupt flag (UIF). The counter register shows values 0003, 0002, 0001, 0000, 0001, 0002, 0003. Counter underflow occurs at 0000. Update events (UEV) occur at 0000. The Update interrupt flag (UIF) is set after the update event at 0000.

Figure 341. Counter timing diagram, internal clock divided by 4, TIM_ARR = 0x36

Timing diagram for Figure 341 showing tim_psc_ck, CEN, tim_cnt_ck, Counter register, Counter overflow, Update event (UEV), and Update interrupt flag (UIF) signals. The counter register values are 0034, 0035, 0036, and 0035. A note at the bottom states: 'Note: Here, center_aligned mode 2 or 3 is updated with an UIF on overflow.' and the reference 'MSv62312V1' is in the bottom right corner.

Figure 341 is a timing diagram for a counter with an internal clock divided by 4. The diagram shows the following signals over time:

Note: Here, center_aligned mode 2 or 3 is updated with an UIF on overflow.

MSv62312V1

Timing diagram for Figure 341 showing tim_psc_ck, CEN, tim_cnt_ck, Counter register, Counter overflow, Update event (UEV), and Update interrupt flag (UIF) signals. The counter register values are 0034, 0035, 0036, and 0035. A note at the bottom states: 'Note: Here, center_aligned mode 2 or 3 is updated with an UIF on overflow.' and the reference 'MSv62312V1' is in the bottom right corner.
  1. 1. Center-aligned mode 2 or 3 is used with a UIF on overflow.

Figure 342. Counter timing diagram, internal clock divided by N

Timing diagram for Figure 342 showing tim_psc_ck, tim_cnt_ck, Counter register, Counter underflow, Update event (UEV), and Update interrupt flag (UIF) signals. The counter register values are 20, 1F, 01, and 00. The reference 'MSv62313V1' is in the bottom right corner.

Figure 342 is a timing diagram for a counter with an internal clock divided by N. The diagram shows the following signals over time:

MSv62313V1

Timing diagram for Figure 342 showing tim_psc_ck, tim_cnt_ck, Counter register, Counter underflow, Update event (UEV), and Update interrupt flag (UIF) signals. The counter register values are 20, 1F, 01, and 00. The reference 'MSv62313V1' is in the bottom right corner.

Figure 343. Counter timing diagram, Update event with ARPE = 1 (counter underflow)

Timing diagram for counter underflow with ARPE = 1. It shows the relationship between tim_psc_ck, CEN, tim_cnt_ck, Counter register, Counter underflow, Update event (UEV), Update interrupt flag (UIF), Auto-reload preload register, and Auto-reload active register. The counter register counts from 06 to 00, then wraps to 01. A write to TIM_ARR occurs during the count. The diagram shows the timing of the underflow event and the update event relative to the clock signals and register values.

The diagram illustrates the timing of an update event during a counter underflow when ARPE is set to 1. The counter register counts down from 06 to 00, then wraps to 01. The auto-reload preload register is updated with a new value (FD) before the underflow. The counter underflow occurs at the end of the 00 count, followed by the update event (UEV) and the update interrupt flag (UIF). The auto-reload active register is updated with the new value (FD) after the update event.

SignalValue/State
tim_psc_ckClock signal
CENHigh
tim_cnt_ckClock signal
Counter register06, 05, 04, 03, 02, 01, 00, 01, 02, 03, 04, 05, 06, 07
Counter underflowPulse at end of 00 count
Update event (UEV)Pulse after underflow
Update interrupt flag (UIF)High after UEV
Auto-reload preload registerFD, 36
Auto-reload active registerFD, 36

MSv80066V1

Timing diagram for counter underflow with ARPE = 1. It shows the relationship between tim_psc_ck, CEN, tim_cnt_ck, Counter register, Counter underflow, Update event (UEV), Update interrupt flag (UIF), Auto-reload preload register, and Auto-reload active register. The counter register counts from 06 to 00, then wraps to 01. A write to TIM_ARR occurs during the count. The diagram shows the timing of the underflow event and the update event relative to the clock signals and register values.

Figure 344. Counter timing diagram, Update event with ARPE = 1 (counter overflow)

Figure 344: Counter timing diagram showing the relationship between tim_psc_ck, CEN, tim_cnt_ck, Counter register, Counter overflow, Update event (UEV), Update interrupt flag (UIF), Auto-reload preload register, and Auto-reload active register during a counter overflow event.

The diagram illustrates the timing of a counter overflow event when ARPE = 1. It shows the following signals and registers over time:

MSv80067V1

Figure 344: Counter timing diagram showing the relationship between tim_psc_ck, CEN, tim_cnt_ck, Counter register, Counter overflow, Update event (UEV), Update interrupt flag (UIF), Auto-reload preload register, and Auto-reload active register during a counter overflow event.

32.4.5 External trigger input

The timer features an external trigger input tim_etr_in. It can be used as:

Figure 345 below describes the tim_etr_in input conditioning. The input polarity is defined with the ETP bit in TIM_SMCR register. The trigger frequency can be decreased with the prescalers programmed by the ETPS[1:0] and SETPS[3:0] bitfields and digitally filtered as per the ETF[3:0] bitfield. The resulting signal (tim_etr) is available for three purposes: as an external clock, to condition the output (typically to reset a PWM output for a current limitation), and as a trigger for the slave mode controller.

Figure 345. External trigger input block

Figure 345: External trigger input block diagram showing the signal flow from TIM_ETR through a multiplexer, prescaler, digital filter, and synchronous prescaler to the final output tim_etr.

The diagram shows the internal signal conditioning for the external trigger input. The components and their connections are:

MSv80068V1

Figure 345: External trigger input block diagram showing the signal flow from TIM_ETR through a multiplexer, prescaler, digital filter, and synchronous prescaler to the final output tim_etr.

The external trigger input block features 2 prescalers with different functionalities:

Note: When tim_etr is used as PWM reset input for cycle-by-cycle current regulation (see Section 32.4.16), SETPS[3:0] must be set to 0 (no prescaling).

Note: The SETPS[3:0] bitfield is always preloaded: a new value will become active only once an update event has occurred.

The tim_etr_in input comes from multiple sources: input pins (default configuration), or internal sources. The selection is done with the ETRSEL[4:0] bitfield in the TIM_AF1 register.

Refer to Section 32.4.2: TIM2/TIM3/TIM4/TIM5 pins and internal signals for the list of sources connected to the etr_in input in the product.

32.4.6 Clock selection

The counter clock can be provided by the following clock sources:

Internal clock source (tim_ker_ck)

If the slave mode controller is disabled (SMS = 000 in the TIM_SMCR register), then the CEN, DIR(in the TIM_CR1 register), and UG bits (in the TIM_EGR register) are actual control bits and can be changed only by software (except UG which remains cleared automatically). As soon as the CEN bit is written to 1, the prescaler is clocked by the internal clock tim_ker_ck.

Figure 346 shows the behavior of the control circuit and the upcounter in normal mode, without prescaler.

Figure 346. Control circuit in normal mode, internal clock divided by 1

Timing diagram for Figure 346: Control circuit in normal mode, internal clock divided by 1. Signals shown: tim_ker_ck, CEN, UG, counter initialization (internal), tim_cnt_ck, tim_psc_ck, Counter register with transitions 31, 32, 33, 34, 35, 36, 00, 01, 02, 03, 04, 05, 06, 07.

MSv62317V2

Timing diagram for Figure 346: Control circuit in normal mode, internal clock divided by 1. Signals shown: tim_ker_ck, CEN, UG, counter initialization (internal), tim_cnt_ck, tim_psc_ck, Counter register with transitions 31, 32, 33, 34, 35, 36, 00, 01, 02, 03, 04, 05, 06, 07.

External clock source mode 1

This mode is selected when SMS = 111 in the TIM_SMCR register. The counter can count at each rising or falling edge on a selected input.

Figure 347. tim_ti2 external clock connection example

Block diagram for Figure 347: tim_ti2 external clock connection example showing TIM_TISEL (TI2SEL[3:0]), input selection to Filter (ICF[3:0], TIM_CCMR1), Edge detector to 2:1 mux controlled by CC2P (TIM_CCER), yielding tim_ti2fp2 into multiplexer TIM_SMCR (TS[4:0]), providing modes including Encoder mode, External clock mode 1, External clock mode 2, and Internal clock mode, to output tim_psc_ck.

MSv80069V1

Block diagram for Figure 347: tim_ti2 external clock connection example showing TIM_TISEL (TI2SEL[3:0]), input selection to Filter (ICF[3:0], TIM_CCMR1), Edge detector to 2:1 mux controlled by CC2P (TIM_CCER), yielding tim_ti2fp2 into multiplexer TIM_SMCR (TS[4:0]), providing modes including Encoder mode, External clock mode 1, External clock mode 2, and Internal clock mode, to output tim_psc_ck.

For example, to configure the upcounter to count in response to a rising edge on the tim_ti2 input, use the following procedure:

  1. 1. Select the proper tim_ti2_in[15:0] source (internal or external) with the TI2SEL[3:0] bits in the TIM_TISEL register.
  2. 2. Configure channel 2 to detect rising edges on the tim_ti2 input by writing CC2S= 01 in the TIM_CCMR1 register.
  3. 3. Configure the input filter duration by writing the IC2F[3:0] bits in the TIM_CCMR1 register (if no filter is needed, keep IC2F = 0000).

Note: The capture prescaler is not used for triggering, so it does not need to be configured.

  1. 4. Select rising edge polarity by writing CC2P = 0 and CC2NP = 0 in the TIM_CCER register.
  2. 5. Configure the timer in external clock mode 1 by writing SMS = 111 in the TIM_SMCR register.
  3. 6. Select tim_ti2 as the input source by writing TS = 00110 in the TIM_SMCR register.
  4. 7. Enable the counter by writing CEN = 1 in the TIM_CR1 register.

When a rising edge occurs on tim_ti2, the counter counts once and the TIF flag is set.

The delay between the rising edge on tim_ti2 and the actual clock of the counter is due to the resynchronization circuit on tim_ti2 input.

Figure 348. Control circuit in external clock mode 1

Timing diagram for Figure 348 showing the control circuit in external clock mode 1. The diagram includes five signals: tim_ti2, CEN, tim_cnt_ck, tim_psc_ck, Counter register, and TIF. tim_ti2 is a square wave. CEN is a high signal. tim_cnt_ck and tim_psc_ck are pulses that occur on the rising edges of tim_ti2. The Counter register shows values 34, 35, and 36, incrementing on each rising edge of tim_ti2. The TIF flag is a pulse that occurs on each rising edge of tim_ti2. Arrows indicate that TIF is cleared by writing TIF=0.

The diagram illustrates the timing relationship between the external clock input (tim_ti2), the counter enable signal (CEN), the counter clock (tim_cnt_ck, tim_psc_ck), the counter register value, and the timer interrupt flag (TIF). The counter register increments on each rising edge of tim_ti2, provided CEN is high. The TIF flag is set on each rising edge of tim_ti2 and can be cleared by writing TIF=0.

Timing diagram for Figure 348 showing the control circuit in external clock mode 1. The diagram includes five signals: tim_ti2, CEN, tim_cnt_ck, tim_psc_ck, Counter register, and TIF. tim_ti2 is a square wave. CEN is a high signal. tim_cnt_ck and tim_psc_ck are pulses that occur on the rising edges of tim_ti2. The Counter register shows values 34, 35, and 36, incrementing on each rising edge of tim_ti2. The TIF flag is a pulse that occurs on each rising edge of tim_ti2. Arrows indicate that TIF is cleared by writing TIF=0.

External clock source mode 2

This mode is selected by writing ECE = 1 in the TIM_SMCR register.

The counter can count at each rising or falling edge on the external trigger input tim_etr_in.

Figure 349 gives an overview of the external trigger input block.

Figure 349. External trigger input block External trigger input block diagram showing signal flow from TIM_ETR and tim_etr[31:1] via TIM_AF1[18:14] multiplexer to tim_etr_in, polarity selection with ETP, asynchronous prescaler, digital filter, synchronous prescaler, and multiplexer routing to tim_psc_ck through various modes (Encoder mode, External clock mode 1, External clock mode 2, Internal clock mode) configured by TIM_SMCR.

MSv80070V1

External trigger input block diagram showing signal flow from TIM_ETR and tim_etr[31:1] via TIM_AF1[18:14] multiplexer to tim_etr_in, polarity selection with ETP, asynchronous prescaler, digital filter, synchronous prescaler, and multiplexer routing to tim_psc_ck through various modes (Encoder mode, External clock mode 1, External clock mode 2, Internal clock mode) configured by TIM_SMCR.
  1. 1. Refer to Section 32.4.2: TIM2/TIM3/TIM4/TIM5 pins and internal signals

For example, to configure the upcounter to count each two rising edges on tim_etr_in, use the following procedure:

  1. 1. Select the proper tim_etr_in source (internal or external) with the ETRSEL[4:0] bits in the TIM_AF1 register.
  2. 2. As no filter is needed in this example, write ETF[3:0] = 0000 in the TIM_SMCR register.
  3. 3. Set the prescaler by writing ETPS[1:0] = 01 and SETPS[3:0] = 0 in the TIM_SMCR register.
  4. 4. Select rising edge detection on the tim_etr_in by writing ETP = 0 in the TIM_SMCR register.
  5. 5. Enable external clock mode 2 by writing ECE = 1 in the TIM_SMCR register.
  6. 6. Enable the counter by writing CEN = 1 in the TIM_CR1 register.

The counter counts once each two tim_etr_in rising edges.

The delay between the rising edge on tim_etr_in and the actual clock of the counter is due to the resynchronization circuit on the tim_etrp signal. As a consequence, the maximum frequency that can be correctly captured by the counter is at most ¼ of TIMxCLK frequency. When the ETRP signal is faster, the user must apply a division of the external signal by a proper ETPS prescaler setting.

Figure 350. Control circuit in external clock mode 2

Timing diagram for Control circuit in external clock mode 2 showing tim_ker_ck, CEN, tim_etr_in, tim_etrp, tim_etrf, tim_cnt_ck / tim_psc_ck, and Counter register values changing from 34 to 35 to 36.
Timing diagram for Control circuit in external clock mode 2 showing tim_ker_ck, CEN, tim_etr_in, tim_etrp, tim_etrf, tim_cnt_ck / tim_psc_ck, and Counter register values changing from 34 to 35 to 36.

MSv62321V1

32.4.7 Capture/compare channels

Each capture/compare channel is built around a capture/compare register (including a shadow register), an input stage for capture (with digital filter, multiplexing and prescaler) and an output stage (with comparator and output control).

The following figure gives an overview of one capture/compare channel.

The input stage samples the corresponding tim_tix input to generate a filtered signal tim_tixf. Then, an edge detector with polarity selection generates a signal (tim_tixfpy) which can be used as trigger input by the slave mode controller or as the capture command. It is prescaled before the capture register (ICxPS).

Figure 351. Capture/compare channel (example: channel 1 input stage)

Block diagram of capture/compare channel 1 input stage showing TIM_CH1 input multiplexer controlled by TIM_TISEL TI1SEL[3:0], Filter downcounter controlled by fDTS and ICF[3:0], Edge detector, polarity selection multiplexers controlled by CC1P/CC1NP, signal routing to slave mode controller, and prescaler divider /1, /2, /4, /8 generating tim_ic1f.
Block diagram of capture/compare channel 1 input stage showing TIM_CH1 input multiplexer controlled by TIM_TISEL TI1SEL[3:0], Filter downcounter controlled by fDTS and ICF[3:0], Edge detector, polarity selection multiplexers controlled by CC1P/CC1NP, signal routing to slave mode controller, and prescaler divider /1, /2, /4, /8 generating tim_ic1f.

MSv80071V1

The output stage generates an intermediate waveform which is then used for reference: tim_ocxref (active high). The polarity acts at the end of the chain.

Figure 352. Capture/compare channel 1 main circuit

Block diagram of Capture/compare channel 1 main circuit. It shows the interaction between the APB Bus, MCU-peripheral interface, Capture/compare preload register, compare shadow register, Counter, and Comparator. Input mode signals include CC1S[1], CC1S[0], IC1PS, CC1E, CC1G, and TIM_EGR. Output mode signals include CC1S[1], CC1S[0], OC1PE, and UEV (from time base unit). The Comparator outputs CNT > CCR1 and CNT = CCR1 signals. The diagram is labeled MSv80072V1.
Block diagram of Capture/compare channel 1 main circuit. It shows the interaction between the APB Bus, MCU-peripheral interface, Capture/compare preload register, compare shadow register, Counter, and Comparator. Input mode signals include CC1S[1], CC1S[0], IC1PS, CC1E, CC1G, and TIM_EGR. Output mode signals include CC1S[1], CC1S[0], OC1PE, and UEV (from time base unit). The Comparator outputs CNT > CCR1 and CNT = CCR1 signals. The diagram is labeled MSv80072V1.

Figure 353. Output stage of capture/compare channel (channel 1, idem ch.2, 3 and 4)

Block diagram of the output stage of capture/compare channel. It shows the flow from TIM_SMCR (OCCS), TIM_CCMR1 (OC1CE, OC1M[4:0]), and TIM_CCER (CC1E, CC1P) through an Output mode controller, Output selector, and Output enable circuit to produce the tim_oc1 output. It also shows connections to the master mode controller and intermediate signals like tim_oc1refc and tim_oc1ref. The diagram is labeled MSv80073V1.
Block diagram of the output stage of capture/compare channel. It shows the flow from TIM_SMCR (OCCS), TIM_CCMR1 (OC1CE, OC1M[4:0]), and TIM_CCER (CC1E, CC1P) through an Output mode controller, Output selector, and Output enable circuit to produce the tim_oc1 output. It also shows connections to the master mode controller and intermediate signals like tim_oc1refc and tim_oc1ref. The diagram is labeled MSv80073V1.

1. Available on some instances only. If not available, tim_etrf is directly connected to tim_ocref_clr_int.

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.

32.4.8 Input capture mode

In input capture mode, the capture/compare registers (TIM_CCRx) are used to latch the value of the counter after a transition detected by the corresponding ICx signal. When a capture occurs, the corresponding CCXIF flag (TIM_SR register) is set and an interrupt or a DMA request can be sent if they are enabled. If a capture occurs while the CCXIF flag was already high, then the overcapture flag CCxOF (TIM_SR register) is set. CCXIF can be cleared by software by writing it to 0 or by reading the captured data stored in the TIM_CCRx register. CCxOF is cleared when it is written with 0.

The following example shows how to capture the counter value in TIM_CCR1 when tim_ti1 input rises. To do this, use the following procedure:

  1. 1. Select the proper tim_tix_in[15:0] source (internal or external) with the TI1SEL[3:0] bits in the TIM_TISEL register.
  2. 2. Select the active input: TIM_CCR1 must be linked to the tim_ti1 input, so write the CC1S bits to 01 in the TIM_CCMR1 register. As soon as CC1S becomes different from 00, the channel is configured in input and the TIM_CCR1 register becomes read-only.
  3. 3. Program the needed input filter duration in relation with the signal connected to the timer (when the input is one of the tim_tix (ICxF bits in the TIM_CCMRx register). Let's imagine that, when toggling, the input signal is not stable during at most five internal clock cycles. We must program a filter duration longer than these five clock cycles. We can validate a transition on tim_ti1 when eight consecutive samples with the new level have been detected (sampled at \(f_{DTS}\) frequency). Then write IC1F bits to 0011 in the TIM_CCMR1 register.
  4. 4. Select the edge of the active transition on the tim_ti1 channel by writing the CC1P and CC1NP bits to 000 in the TIM_CCER register (rising edge in this case).
  5. 5. Program the input prescaler. In this example, the capture is to be performed at each valid transition, so the prescaler is disabled (write IC1PS bits to 00 in the TIM_CCMR1 register).
  6. 6. Enable capture from the counter into the capture register by setting the CC1E bit in the TIM_CCER register.
  7. 7. If needed, enable the related interrupt request by setting the CC1IE bit in the TIM_DIER register, and/or the DMA request by setting the CC1DE bit in the TIM_DIER register.

When an input capture occurs:

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 may 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 TIM_EGR register.

32.4.9 PWM input mode

This mode is used to measure both the period and the duty cycle of a PWM signal connected to single tim_tix input:

This mode is a particular case of input capture mode. The set-up procedure is similar with the following differences:

The period and the pulse width of a PWM signal applied on tim_ti1 can be measured using the following procedure:

  1. 1. Select the proper tim_tix_in[15:0] source (internal or external) with the TI1SEL[3:0] bits in the TIM_TISEL register.
  2. 2. Select the active input for TIM_CCR1: write the CC1S bits to 01 in the TIM_CCMR1 register (tim_ti1 selected).
  3. 3. Select the active polarity for tim_ti1fp1 (used both for capture in TIM_CCR1 and counter clear): write the CC1P to 0 and the CC1NP bit to 0 (active on rising edge).
  4. 4. Select the active input for TIM_CCR2: write the CC2S bits to 10 in the TIM_CCMR1 register (tim_ti1 selected).
  5. 5. Select the active polarity for tim_ti1fp2 (used for capture in TIM_CCR2): write the CC2P bit to 1 and the CC2NP bit to 0 (active on falling edge).
  6. 6. Select the valid trigger input: write the TS bits to 00101 in the TIM_SMCR register (tim_ti1fp1 selected).
  7. 7. Configure the slave mode controller in reset mode: write the SMS bits to 100 in the TIM_SMCR register.
  8. 8. Enable the captures: write the CC1E and CC2E bits to 1 in the TIM_CCER register.

Figure 354. PWM input mode timing

Timing diagram for PWM input mode showing the relationship between the input signal (tim_ti1), the counter (TIM_CNT), and the compare registers (TIM_CCR1 and TIM_CCR2). The diagram illustrates three events: 1. IC1 capture, IC2 capture, and reset counter, which occurs when the counter reaches 0000. 2. IC2 capture pulse width measurement, which occurs when the counter reaches 0003. 3. IC1 capture pulse width measurement, which occurs when the counter reaches 0004. The input signal (tim_ti1) is shown as a square wave. The counter (TIM_CNT) is shown as a sequence of values: 0004, 0000, 0001, 0002, 0003, 0004, 0000. The compare registers (TIM_CCR1 and TIM_CCR2) are shown as constant values: 0004 and 0002 respectively. Arrows indicate the timing of the events relative to the counter values.

MSv80074V1

Timing diagram for PWM input mode showing the relationship between the input signal (tim_ti1), the counter (TIM_CNT), and the compare registers (TIM_CCR1 and TIM_CCR2). The diagram illustrates three events: 1. IC1 capture, IC2 capture, and reset counter, which occurs when the counter reaches 0000. 2. IC2 capture pulse width measurement, which occurs when the counter reaches 0003. 3. IC1 capture pulse width measurement, which occurs when the counter reaches 0004. The input signal (tim_ti1) is shown as a square wave. The counter (TIM_CNT) is shown as a sequence of values: 0004, 0000, 0001, 0002, 0003, 0004, 0000. The compare registers (TIM_CCR1 and TIM_CCR2) are shown as constant values: 0004 and 0002 respectively. Arrows indicate the timing of the events relative to the counter values.
  1. 1. The PWM input mode can be used only with the TIM_CH1/TIM_CH2 signals due to the fact that only tim_ti1fp1 and tim_ti2fp2 are connected to the slave mode controller.

32.4.10 Forced output mode

In output mode (CCxS bits = 00 in the TIM_CCMRx register), each output compare signal (tim_ocxref and then tim_ocx) can be forced to active or inactive level directly by software, independently of any comparison between the output compare register and the counter.

To force an output compare signal (tim_ocxref/tim_ocx) to its active level, the user just needs to write 00101 in the OCxM bits in the corresponding TIM_CCMRx register. Thus tim_ocxref is forced high (tim_ocxref is always active high) and tim_ocx get opposite value to CCxP polarity bit.

For example: CCxP = 0 (tim_ocx active high) => tim_ocx is forced to high level.

tim_ocxref signal can be forced low by writing the OCxM bits to 00100 in the TIM_CCMRx register.

Anyway, the comparison between the TIM_CCRx shadow register and the counter is still performed and allows the flag to be set. Interrupt and DMA requests can be sent accordingly. This is described in the output compare mode section.

32.4.11 Output compare mode

This function is used to control an output waveform or indicating when a period of time has elapsed.

When a match is found between the capture/compare register and the counter, the output compare function:

set active (OCxM = 00001), be set inactive (OCxM = 00010) or can toggle (OCxM = 00011) on match.

The TIM_CCRx registers can be programmed with or without preload registers using the OCxPE bit in the TIM_CCMRx register.

In output compare mode, the update event UEV has no effect on tim_ocxref and tim_ocx output. The timing resolution is one count of the counter. Output compare mode can also be used to output a single pulse (in one-pulse mode).

Procedure

  1. 1. Select the counter clock (internal, external, prescaler).
  2. 2. Write the desired data in the TIM_ARR and TIM_CCRx registers.
  3. 3. Set the CCxIE and/or CCxDE bits if an interrupt and/or a DMA request is to be generated.
  4. 4. Select the output mode. For example:
    1. a) Write OCxM = 00011 to toggle tim_ocx output pin when CNT matches CCRx.
    2. b) Write OCxPE = 0 to disable preload register.
    3. c) Write CCxP = 0 to select active high polarity.
    4. d) Write CCxE = 1 to enable the output.
  5. 5. Enable the counter by setting the CEN bit in the TIM_CR1 register.

The TIM_CCRx register can be updated at any time by software to control the output waveform, provided that the preload register is not enabled (OCxPE = 0, else TIM_CCRx shadow register is updated only at the next update event UEV). An example is given in Figure 355.

Figure 355. Output compare mode, toggle on tim_oc1

Timing diagram for output compare mode, toggle on tim_oc1. It shows three signals: CNT (counter), CCR1 (compare register), and tim_oc1ref = tim_oc1 (output signal). The CNT signal has values 0039, 003A, 003B, followed by a dashed line, then B200, and B201. The CCR1 signal has a value of 003A and then B201. The tim_oc1ref = tim_oc1 signal is low until the match at B201, then toggles high. A note indicates 'Write B201h in the CC1R register' pointing to the CCR1 register update. Another note indicates 'Match detected on CCR1 Interrupt generated if enabled' pointing to the match point. The diagram is labeled MSv62326V1.

Write B201h in the CC1R register

CNT: 0039, 003A, 003B, ..., B200, B201

CCR1: 003A, ..., B201

tim_oc1ref = tim_oc1: [Low] → [High] (at B201)

Match detected on CCR1
Interrupt generated if enabled

MSv62326V1

Timing diagram for output compare mode, toggle on tim_oc1. It shows three signals: CNT (counter), CCR1 (compare register), and tim_oc1ref = tim_oc1 (output signal). The CNT signal has values 0039, 003A, 003B, followed by a dashed line, then B200, and B201. The CCR1 signal has a value of 003A and then B201. The tim_oc1ref = tim_oc1 signal is low until the match at B201, then toggles high. A note indicates 'Write B201h in the CC1R register' pointing to the CCR1 register update. Another note indicates 'Match detected on CCR1 Interrupt generated if enabled' pointing to the match point. The diagram is labeled MSv62326V1.

32.4.12 PWM mode

Pulse width modulation mode is used to generate a signal with a frequency determined by the value of the TIM_ARR register and a duty cycle determined by the value of the TIM_CCRx register.

The PWM mode can be selected independently on each channel (one PWM per tim_ocx output) by writing 00110 (PWM mode 1) or 00111 (PWM mode 2) in the OCxM bits in the TIM_CCMRx register. The corresponding preload register must be enabled by setting the OCxPE bit in the TIM_CCMRx register, and eventually the autoreload preload register (in up-counting or center-aligned modes) by setting the ARPE bit in the TIM_CR1 register.

As the preload registers are transferred to the shadow registers only when an update event occurs, before starting the counter, all registers must be initialized by setting the UG bit in the TIM_EGR register.

tim_ocx polarity is software programmable using the CCxP bit in the TIM_CCER register. It can be programmed as active high or active low. tim_ocx output is enabled by the CCxE bit in the TIM_CCER register. Refer to the TIM_CCERx register description for more details.

In PWM mode (1 or 2), TIM_CNT and TIM_CCRx are always compared to determine whether TIM_CCRx ≤ TIM_CNT or TIM_CNT ≤ TIM_CCRx (depending on the direction of the counter). The tim_ocref_clr can be cleared by an external event through the tim_etr_in or the tim_oceref_clr signals. In this case the tim_ocref_clr signal is asserted only:

The timer is able to generate PWM in edge-aligned mode or center-aligned mode depending on the CMS bits in the TIM_CR1 register.

PWM edge-aligned mode

In the following example, we consider PWM mode 1. The reference PWM signal tim_ocxref is high as long as TIM_CNT < TIM_CCRx else it becomes low. If the compare value in TIM_CCRx is greater than the autoreload value (in TIM_ARR) then tim_ocxref is held at 1. If the compare value is 0 then tim_ocxref is held at 0. Figure 356 shows some edge-aligned PWM waveforms in an example where TIM_ARR = 8.

Figure 356. Edge-aligned PWM waveforms (ARR = 8)

Timing diagram for edge-aligned PWM waveforms with ARR = 8. The diagram shows the Counter register counting from 0 to 8 and then wrapping to 0. Three cases are shown: CCRx=4, CCRx=8, and CCRx>8. For CCRx=4, tim_ocxref is high from counter 0 to 3 and low from 4 to 8. For CCRx=8, tim_ocxref is high from 0 to 7 and low at 8. For CCRx>8, tim_ocxref is held high at '1'. For CCRx=0, tim_ocxref is held low at '0'. CCxIF flags are shown as pulses at the compare events.

MSv62327V1

Timing diagram for edge-aligned PWM waveforms with ARR = 8. The diagram shows the Counter register counting from 0 to 8 and then wrapping to 0. Three cases are shown: CCRx=4, CCRx=8, and CCRx>8. For CCRx=4, tim_ocxref is high from counter 0 to 3 and low from 4 to 8. For CCRx=8, tim_ocxref is high from 0 to 7 and low at 8. For CCRx>8, tim_ocxref is held high at '1'. For CCRx=0, tim_ocxref is held low at '0'. CCxIF flags are shown as pulses at the compare events.

Down-counting configuration

In PWM mode 1, the reference signal tim_ocxref is low as long as TIM_CNT > TIM_CCRx else it becomes high. If the compare value in TIM_CCRx is greater than the autoreload value in TIM_ARR, then tim_ocxref is held at 100%. PWM is not possible in this mode.

PWM center-aligned mode

Center-aligned mode is active when the CMS bits in TIM_CR1 register are different from 00 (all the remaining configurations having the same effect on the tim_ocxref/tim_ocx signals). The compare flag is set when the counter counts up, when it counts down or both when it counts up and down depending on the CMS bits configuration. The direction bit (DIR) in the

TIM_CR1 register is updated by hardware and must not be changed by software. Refer to Center-aligned mode (up/down-counting).

Figure 357 shows some center-aligned PWM waveforms in an example where:

Figure 357. Center-aligned PWM waveforms (ARR = 8)

Timing diagram for center-aligned PWM waveforms with ARR = 8. The diagram shows the Counter register counting up from 0 to 8 and then down from 8 to 0. It illustrates the tim_ocxref and CCxIF signals for various CCRx values (4, 7, 8, >8, 0) and CMS settings (01, 10, 11).

The diagram illustrates the timing of center-aligned PWM waveforms for a timer with an auto-reload register (ARR) value of 8. The counter register counts up from 0 to 8 and then counts down from 8 to 0, completing one full cycle. The diagram shows the relationship between the counter register, the output compare reference (tim_ocxref), and the output compare interrupt flag (CCxIF) for different CCRx values and CMS settings.

MSv62328V2

Timing diagram for center-aligned PWM waveforms with ARR = 8. The diagram shows the Counter register counting up from 0 to 8 and then down from 8 to 0. It illustrates the tim_ocxref and CCxIF signals for various CCRx values (4, 7, 8, >8, 0) and CMS settings (01, 10, 11).

Hints on using center-aligned mode:

Dithering mode

The PWM mode effective resolution can be increased by enabling the dithering mode, using the DITHER bit in the TIM_CR1 register. This applies to both the CCR (for duty cycle resolution increase) and ARR (for PWM frequency resolution increase).

The operating principle is to have the actual CCR (or ARR) value slightly changed (adding or not one timer clock period) over 16 consecutive PWM periods, with predefined patterns. This allows a 16-fold resolution increase, considering the average duty cycle or PWM period. Figure 358 presents the dithering principle applied to four consecutive PWM cycles.

Figure 358. Dithering principle

Figure 358: Dithering principle. The diagram shows five rows of PWM waveforms over four consecutive cycles. The first row is labeled 'Average duty cycle' and shows a period of 7 units and a high time of 5 units, resulting in DC = 7/5. The subsequent rows show dithering patterns for DC = (7+1/4)/5, DC = (7+1/2)/5, DC = (7+3/4)/5, and DC = 8/5. Shaded regions indicate the dithering effect. A '1 clock cycle' marker is shown at the bottom.

Figure 358 illustrates the dithering principle for PWM duty cycle resolution. It shows five rows of PWM waveforms over four consecutive cycles. The first row is labeled 'Average duty cycle' and shows a period of 7 units and a high time of 5 units, resulting in DC = 7/5. The subsequent rows show dithering patterns for DC = (7+1/4)/5, DC = (7+1/2)/5, DC = (7+3/4)/5, and DC = 8/5. Shaded regions indicate the dithering effect. A '1 clock cycle' marker is shown at the bottom.

Figure 358: Dithering principle. The diagram shows five rows of PWM waveforms over four consecutive cycles. The first row is labeled 'Average duty cycle' and shows a period of 7 units and a high time of 5 units, resulting in DC = 7/5. The subsequent rows show dithering patterns for DC = (7+1/4)/5, DC = (7+1/2)/5, DC = (7+3/4)/5, and DC = 8/5. Shaded regions indicate the dithering effect. A '1 clock cycle' marker is shown at the bottom.

When the dithering mode is enabled, the register coding is changed as following (see Figure 359 for example):

Note: The following sequence must be followed when resetting the DITHEN bit:

  1. 1. CEN and ARPE bits must be reset.
  2. 2. The DITHEN bit must be reset.
  3. 3. The CCIF flags must be cleared.
  4. 4. The CEN bit can be set (eventually with ARPE = 1).

Figure 359. Data format and register coding in dithering mode

Figure 359: Data format and register coding in dithering mode. The diagram shows three register formats: 32-bit, 16-bit, and an example. The 32-bit format has a 28-bit integer part (b31 to b4) and a 4-bit fractional part (b3 to b0). The 16-bit format has a reserved field (b31 to b16), a 16-bit integer part (b15 to b0), and a 4-bit fractional part (b3 to b0). The example shows a 32-bit register with a base compare value of 20 and an additional 6 cycles spread over 16 periods.

Register format in dithering mode (32-bit)

b31b0
MSB: 28-bits, integer partLSB: 4-bits fractional part

Register format in dithering mode (16-bit)

b31b19b0
ReservedMSB: 16-bits, integer partLSB: 4-bits fractional part

Example

b19b0
326
206

Base compare value is 20 during 16 periods

Additional 6 cycles are spread over the 16 periods

MSv50911V1

Figure 359: Data format and register coding in dithering mode. The diagram shows three register formats: 32-bit, 16-bit, and an example. The 32-bit format has a 28-bit integer part (b31 to b4) and a 4-bit fractional part (b3 to b0). The 16-bit format has a reserved field (b31 to b16), a 16-bit integer part (b15 to b0), and a 4-bit fractional part (b3 to b0). The example shows a 32-bit register with a base compare value of 20 and an additional 6 cycles spread over 16 periods.

The minimum frequency is given by the following formula:

\[\text{Resolution} = \frac{F_{\text{Tim}}}{F_{\text{pwm}}} \Rightarrow F_{\text{pwmMin}} = \frac{F_{\text{Tim}}}{\text{Max}_{\text{Resolution}}}\]

\[\text{Dithering mode disabled: } F_{\text{pwmMin}} = \frac{F_{\text{Tim}}}{65536}\]

\[\text{Dithering mode (16-bit timer): } F_{\text{pwmMin}} = \frac{F_{\text{Tim}}}{65535 + \frac{15}{16}}\]

\[\text{Dithering mode (32-bit timer): } F_{\text{pwmMin}} = \frac{F_{\text{Tim}}}{268435454 + \frac{15}{16}}\]

Note: For 16-bit timers, the maximum TIM_ARR and TIM_CCRy values are limited to 0xFFFEF in dithering mode (corresponds to 65534 for the integer part and 15 for the dithered part). For 32-bit timers, the maximum TIM_ARR and TIM_CCRy values are limited to

0xFFFFFFFFEF in dithering mode (corresponds to 264435454 for the integer part and 15 for the dithered part).

As shown on Figure 360 and Figure 361, the dithering mode is used to increase the PWM resolution.

Figure 360. PWM resolution vs frequency (16-bit mode)

Graph showing PWM resolution vs frequency in 16-bit mode. The y-axis is 'PWM resolution' with dashed guide lines at 20-bit and 16-bit. The x-axis is 'PWM frequency' starting at FPWM min. Two downward sloping curves are displayed: 'Dithering' starting at 20-bit and 'No Dithering' starting at 16-bit.

MSv47464V2

Graph showing PWM resolution vs frequency in 16-bit mode. The y-axis is 'PWM resolution' with dashed guide lines at 20-bit and 16-bit. The x-axis is 'PWM frequency' starting at FPWM min. Two downward sloping curves are displayed: 'Dithering' starting at 20-bit and 'No Dithering' starting at 16-bit.

Figure 361. PWM resolution vs frequency (32-bit mode)

Graph showing PWM resolution vs frequency in 32-bit mode. The y-axis is 'PWM Resolution' marked at 32-bit. The x-axis is 'PWM frequency' marked at 'F(cnt) min No dithering' and 'F(cnt) min with dithering'. Two downward sloping curves are displayed: 'No Dithering' starting at F(cnt) min No dithering and 'Dithering' starting at F(cnt) min with dithering.

MSv50912V1

Graph showing PWM resolution vs frequency in 32-bit mode. The y-axis is 'PWM Resolution' marked at 32-bit. The x-axis is 'PWM frequency' marked at 'F(cnt) min No dithering' and 'F(cnt) min with dithering'. Two downward sloping curves are displayed: 'No Dithering' starting at F(cnt) min No dithering and 'Dithering' starting at F(cnt) min with dithering.

The duty cycle and/or period changes are spread over 16 consecutive periods, as described in Figure 362.

Figure 362. PWM dithering pattern

Figure 362. PWM dithering pattern timing diagram showing Counter period 1 through 16, CCR1 value 322 with Compare1 sequence (21, 20, 20, 20, 20, 20, 20, 20, 21, 20, 20, 20, 20, 20, 20, 20), CCR2 value 326 with Compare2 sequence (21, 20, 21, 20, 21, 20, 20, 20, 21, 20, 21, 20, 21, 20, 20, 20), CCR3 value 334 with Compare3 sequence (21, 21, 21, 21, 21, 21, 21, 20, 21, 21, 21, 21, 21, 21, 21, 20), CCR4 value 336 with Compare4 sequence (21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21), ARR value 643 with Auto-Reload value sequence (41, 40, 40, 40, 41, 40, 40, 40, 41, 40, 40, 40, 40, 40, 40, 40).

MSv45755V1

Figure 362. PWM dithering pattern timing diagram showing Counter period 1 through 16, CCR1 value 322 with Compare1 sequence (21, 20, 20, 20, 20, 20, 20, 20, 21, 20, 20, 20, 20, 20, 20, 20), CCR2 value 326 with Compare2 sequence (21, 20, 21, 20, 21, 20, 20, 20, 21, 20, 21, 20, 21, 20, 20, 20), CCR3 value 334 with Compare3 sequence (21, 21, 21, 21, 21, 21, 21, 20, 21, 21, 21, 21, 21, 21, 21, 20), CCR4 value 336 with Compare4 sequence (21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21), ARR value 643 with Auto-Reload value sequence (41, 40, 40, 40, 41, 40, 40, 40, 41, 40, 40, 40, 40, 40, 40, 40).

MSv45755V1

The autoreload and compare values increments are spread following specific patterns described in Table 317. The dithering sequence is done to have increments distributed as evenly as possible and minimize the overall ripple.

Table 317. CCR and ARR register change dithering pattern

LSB valuePWM period
12345678910111213141516
0000----------------
0001+1---------------
0010+1-------+1-------
0011+1---+1---+1-------
0100+1---+1---+1---+1---
0101+1-+1-+1---+1---+1---
0110+1-+1-+1---+1-+1-+1---
0111+1-+1-+1-+1-+1-+1-+1---
1000+1-+1-+1-+1-+1-+1-+1-+1-
1001+1+1+1-+1-+1-+1-+1-+1-+1-
1010+1+1+1-+1-+1-+1+1+1-+1-+1-
1011+1+1+1-+1+1+1-+1+1+1-+1-+1-
1100+1+1+1-+1+1+1-+1+1+1-+1+1+1-
1101+1+1+1+1+1+1+1-+1+1+1-+1+1+1-
1110+1+1+1+1+1+1+1-+1+1+1+1+1+1+1-
1111+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1-

The dithering mode is also available in center-aligned PWM mode (CMS bits in TIM_CR1 register are not equal to 00). In this case, the dithering pattern is applied over eight consecutive PWM periods, considering the up and down-counting phases as shown in Figure 363.

Figure 363. Dithering effect on duty cycle in center-aligned PWM mode

Figure 363: Dithering effect on duty cycle in center-aligned PWM mode. The figure shows three diagrams illustrating the dithering effect on the duty cycle in center-aligned PWM mode. Each diagram shows a triangular carrier wave and a PWM signal. The first diagram, 'No dithering', shows a standard PWM signal. The second diagram, 'Dithering up', shows the PWM signal shifted upwards, increasing the duty cycle. The third diagram, 'Dithering down', shows the PWM signal shifted downwards, decreasing the duty cycle. The diagrams are labeled 'No dithering', 'Dithering up', and 'Dithering down' respectively. The reference code 'MSv50904V1' is located at the bottom right of the figure area.
Figure 363: Dithering effect on duty cycle in center-aligned PWM mode. The figure shows three diagrams illustrating the dithering effect on the duty cycle in center-aligned PWM mode. Each diagram shows a triangular carrier wave and a PWM signal. The first diagram, 'No dithering', shows a standard PWM signal. The second diagram, 'Dithering up', shows the PWM signal shifted upwards, increasing the duty cycle. The third diagram, 'Dithering down', shows the PWM signal shifted downwards, decreasing the duty cycle. The diagrams are labeled 'No dithering', 'Dithering up', and 'Dithering down' respectively. The reference code 'MSv50904V1' is located at the bottom right of the figure area.

Table 318 shows how the dithering pattern is added in center-aligned PWM mode.

Table 318. CCR register change dithering pattern in center-aligned PWM mode

LSB valuePWM period
12345678
UpDnUpDnUpDnUpDnUpDnUpDnUpDnUpDn
0000----------------
0001+1---------------
0010+1-------+1-------
0011+1---+1---+1-------
0100+1---+1---+1---+1---
0101+1-+1-+1---+1---+1---
0110+1-+1-+1---+1-+1-+1---
0111+1-+1-+1-+1-+1-+1-+1---
1000+1-+1-+1-+1-+1-+1-+1-+1-
1001+1+1+1-+1-+1-+1-+1-+1-+1-
1010+1+1+1-+1-+1-+1+1+1-+1-+1-
1011+1+1+1-+1+1+1-+1+1+1-+1-+1-
1100+1+1+1-+1+1+1-+1+1+1-+1+1+1-
1101+1+1+1+1+1+1+1-+1+1+1-+1+1+1-
1110+1+1+1+1+1+1+1-+1+1+1+1+1+1+1-
1111+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1-

32.4.13 Asymmetric PWM mode

Asymmetric mode allows two center-aligned PWM signals to be generated with a programmable phase shift. While the frequency is determined by the value of the TIM_ARR register, the duty cycle and the phase-shift are determined by a pair of TIM_CCRx registers. One register controls the PWM during up-counting, the second during down-counting, so that PWM is adjusted every half PWM cycle, for instance:

Figure 364 shows an example of signals that can be generated using asymmetric PWM mode (channels 1 to 4 are configured in asymmetric PWM mode 2).

Figure 364. Generation of two phase-shifted PWM signals with 50% duty cycle Timing diagram showing the generation of two phase-shifted PWM signals with 50% duty cycle. The diagram includes a counter register sequence (0, 1, 2, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1) and two PWM signals: tim_oc1refc and tim_oc3refc. The counter register is a sequence of boxes labeled 0 to 8, then 7 to 1, then 0 to 1. The tim_oc1refc signal is high from counter value 0 to 7, and low from 8 to 1. The tim_oc3refc signal is high from counter value 3 to 7, and low from 8 to 2. The diagram is labeled MSv62329V1.
Timing diagram showing the generation of two phase-shifted PWM signals with 50% duty cycle. The diagram includes a counter register sequence (0, 1, 2, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1) and two PWM signals: tim_oc1refc and tim_oc3refc. The counter register is a sequence of boxes labeled 0 to 8, then 7 to 1, then 0 to 1. The tim_oc1refc signal is high from counter value 0 to 7, and low from 8 to 1. The tim_oc3refc signal is high from counter value 3 to 7, and low from 8 to 2. The diagram is labeled MSv62329V1.

Multiple asymmetric PWM modes are available to combine different pairs of TIM_CCRx registers and generate the required waveform, as listed in Table 319. The selection is done with the OCxM[4:0] bitfield in TIM_CCMRx registers. For each pair of TIM_CCRx registers, two options are given (e.g. Asymmetric PWM mode 1 and 2), one being in PWM mode 1 (channel active as long as the counter is below the TIM_CCRx value), the other one in PWM mode 2 (channel active when the counter is above the TIM_CCRx value).

For the asymmetric PWM modes 1 and 2, it is mandatory to have the same PWM mode for both channels. For instance, it is mandatory to have OC1M[4:0] = OC2M[4:0] = 01110 for the asymmetric PWM mode 1 on oc1refc.

For the asymmetric PWM modes 7 to 10, it is mandatory to have opposite PWM mode for both channels (one being in PWM mode 1 and the other in PWM mode 2). Refer to Table 319 for configuration.

Note: The OCxM[4:0] bitfield is split into two parts for compatibility reasons, the most significant bits are not contiguous with the three least significant ones.

When a given channel is used as asymmetric PWM channel, its secondary channel can also be used. For instance, if an tim_oc1refc signal is generated on channel 1 (Asymmetric PWM mode 1), it is possible to output either the regular tim_oc2ref signal on channel 2, or an tim_oc2refc signal resulting from asymmetric PWM mode 2.

Table 319. Asymmetric PWM modes(1)
Output compare modeOCxM[4:0]oc1refcoc2refcoc3refcoc4refcPWM Mode(2)
Asymmetric PWM mode 101110oc1ref (up-counting)oc3ref (up-counting)1
Asymmetric PWM mode 201111oc2ref (down-counting)oc4ref (down-counting)2
Asymmetric PWM mode 710110--oc3refc = 1 (up-counting)
oc3ref OR oc4ref (down-counting)
-1(3)
Asymmetric PWM mode 810111--oc3refc = 0 (up-counting)
oc3ref AND oc4ref (down-counting)
-2(4)
Asymmetric PWM mode 911000--oc3ref OR oc4ref (up-counting)
oc3refc = 1 (down-counting)
-1(3)
Asymmetric PWM mode 1011001--oc3ref AND oc4ref (up-counting)
oc3refc = 0 (down-counting)
-2(4)

1. Asymmetric PWM mode 7 to 10 are only available on OC3M.

  1. 2. PWM mode 1: channel active as long as the counter is below the TIM_CCRx value. PWM mode 2: channel active when the counter is above the TIM_CCRx value. Unless explicitly mentioned, the PWM mode must be identical for the two channels used for the asymmetric mode.
  2. 3. oc4ref must be programmed in PWM mode 1.
  3. 4. oc4ref must be programmed in PWM mode 2.

32.4.14 Combined PWM mode

Combined PWM mode allows two edge or center-aligned PWM signals to be generated with programmable delay and phase shift between respective pulses. While the frequency is determined by the value of the TIM_ARR register, the duty cycle and delay are determined by the two TIM_CCRx registers. The resulting signals, tim_ocxrefc, are made of an OR or AND logical combination of two reference PWMs,

Table 320 summarizes different combinations of PWM combined mode:

Table 320. Combined PWM mode overview

Output compare modeOCxM[4:0]oc1refcoc2refcoc3refcoc4refcPWM mode(1)
Combined PWM mode 101100oc1ref OR oc2refoc3ref OR oc4ref1
Combined PWM mode 201101oc1ref AND oc2refoc3ref AND oc4ref2
  1. 1. PWM mode 1: channel active as long as the counter is below the TIM_CCRx value). PWM mode 2: channel active when the counter is above the TIM_CCRx value.

Combined PWM mode can be selected independently on two channels (one tim_ocx output per pair of CCR registers) by writing 01100 (combined PWM mode 1) or 01101 (combined PWM mode 2) in the OCxM bits in the TIM_CCMRx register.

When a given channel is used as combined PWM channel, its secondary channel must be configured in the opposite PWM mode (for instance, one in PWM mode 1 and the other in PWM mode 2).

Note: The OCxM[4:0] bitfield is split into two parts for compatibility reasons, the most significant bits are not contiguous with the three least significant ones.

Figure 365 shows an example of signals that can be generated using combined PWM mode, obtained with the following configuration:

Figure 365. Combined PWM mode on channels 1 and 3

Timing diagram for Combined PWM mode on channels 1 and 3. The top part shows Combined PWM mode 1: tim_oc1refc = tim_oc1ref OR tim_oc2ref. It includes waveforms for CCR2, CCR1, tim_oc1ref, tim_oc2ref, and tim_oc1refc. The bottom part shows Combined PWM mode 2: tim_oc3refc = tim_oc3ref AND tim_oc4ref. It includes waveforms for CCR4, CCR3, tim_oc3ref, tim_oc4ref, and tim_oc3refc. Both parts show a sawtooth carrier wave and vertical dashed lines indicating timing relationships.

Combined PWM mode 1: \(tim\_oc1refc = tim\_oc1ref \text{ OR } tim\_oc2ref\)

Combined PWM mode 2: \(tim\_oc3refc = tim\_oc3ref \text{ AND } tim\_oc4ref\)

MSv74198V1

Timing diagram for Combined PWM mode on channels 1 and 3. The top part shows Combined PWM mode 1: tim_oc1refc = tim_oc1ref OR tim_oc2ref. It includes waveforms for CCR2, CCR1, tim_oc1ref, tim_oc2ref, and tim_oc1refc. The bottom part shows Combined PWM mode 2: tim_oc3refc = tim_oc3ref AND tim_oc4ref. It includes waveforms for CCR4, CCR3, tim_oc3ref, tim_oc4ref, and tim_oc3refc. Both parts show a sawtooth carrier wave and vertical dashed lines indicating timing relationships.

32.4.15 Clock dividers for dead-time generators and digital filters

The digital filters (for tim_etr_in, tim_tix) are using a DTS_ck clock (with a \(t_{DTS}\) period), derived from the tim_ker_ck clock.

A prescaler can decrease the frequency to extend the DTS_ck clock period programmed with the CKD[1:0] bitfield in the TIM_CR1 register. A second prescaler can decrease the resulting DTS_ck clock to extend the digital filters range, with the CKD2[3:0] bitfield in the TIM_CR1 register.

Figure 366. Clock prescalers for dead-time and digital filters

Figure 366: Clock prescalers for dead-time and digital filters. The diagram shows two prescalers. The first prescaler takes tim_ker_ck (t_tim_ker_ck) as input and is controlled by CKD[1:0] in TIM_CR1, outputting DTS_ck (t_DTS). The second prescaler takes DTS_ck as input and is controlled by CKD2[3:0] in TIM_CR1, outputting DTS2_ck (t_DTS2). DTS2_ck is then used by Digital filters (tim_etr_in, tim_tix).

MSv80075V1

Figure 366: Clock prescalers for dead-time and digital filters. The diagram shows two prescalers. The first prescaler takes tim_ker_ck (t_tim_ker_ck) as input and is controlled by CKD[1:0] in TIM_CR1, outputting DTS_ck (t_DTS). The second prescaler takes DTS_ck as input and is controlled by CKD2[3:0] in TIM_CR1, outputting DTS2_ck (t_DTS2). DTS2_ck is then used by Digital filters (tim_etr_in, tim_tix).

32.4.16 Clearing the tim_ocxref signal on an external event

The tim_ocxref signal of a given channel can be cleared when a high level is applied on the tim_ocref_clr_int input (OCxCE enable bit in the corresponding TIM_CCMRx register set to 1). tim_ocxref remains low until the next transition to the active state, on the following PWM cycle. This function can only be used in output compare and PWM modes. It must not be used in forced mode.

The tim_ocref_clr_int source depends on the OCREf clear selection feature implementation, refer to Section 32.3: TIM2/TIM3/TIM4/TIM5 implementation.

If the OCREf clear selection feature is implemented, the tim_ocref_clr_int can be selected between the tim_ocref_clr input and the tim_etr input (tim_etr_in after the filter) by configuring the OCCS bit in the TIM_SMCR register. The tim_ocref_clr input can be selected among several tim_ocref_clr[15:0] inputs, using the OCRSEL[3:0] bitfield in the TIM_AF2 register, as shown in Figure 367.

Figure 367. OCREf_CLR input selection multiplexer

Figure 367: OCREf_CLR input selection multiplexer. The diagram shows a multiplexer structure. On the left, a multiplexer selects from tim_ocref_clr0 to tim_ocref_clr15 inputs based on OCRSEL[3:0] in TIM_AF2, outputting tim_ocref_clr. On the right, another multiplexer selects between tim_ocref_clr and tim_etr inputs based on OCCS in TIM_SMCR, outputting tim_ocref_clr_int.

MSv80076V1

Figure 367: OCREf_CLR input selection multiplexer. The diagram shows a multiplexer structure. On the left, a multiplexer selects from tim_ocref_clr0 to tim_ocref_clr15 inputs based on OCRSEL[3:0] in TIM_AF2, outputting tim_ocref_clr. On the right, another multiplexer selects between tim_ocref_clr and tim_etr inputs based on OCCS in TIM_SMCR, outputting tim_ocref_clr_int.

If the OCREf clear selection feature is not implemented, the tim_ocref_clr_int input is directly connected to the tim_etr input.

For example, the tim_ocref_clr_int signal can be connected to the output of a comparator to be used for current handling. In this case, tim_etr_in must be configured as follows:

  1. 1. The external trigger prescaler must be kept off: bits ETPS[1:0] in the TIM_SMCR register are cleared to 00.
  2. 2. The external clock mode 2 must be disabled: bit ECE in the TIM_SMCR register is cleared to 0.
  3. 3. The external trigger polarity (ETP) and the external trigger filter (ETF) can be configured according to the application's needs.

Figure 368 shows the behavior of the tim_ocxref signal when the tim_etr input becomes high, for both values of the OCxCE enable bit. In this example, the timer TIMx is programmed in PWM mode.

Figure 368. Clearing TIMx tim_ocxref

Timing diagram showing the behavior of tim_ocxref and tim_etr signals relative to the counter (CNT) and CCRx threshold. The diagram illustrates two cases: OCxCE = '0' and OCxCE = '1'. In the OCxCE = '0' case, tim_ocxref is cleared when tim_etr goes high. In the OCxCE = '1' case, tim_ocxref is cleared when tim_etr goes high, but it remains high until the next counter overflow. Arrows indicate the points where tim_ocref_clr_int becomes high and remains high.

The diagram shows four signals over time: Counter (CNT), tim_etr, tim_ocxref(OCxCE = '0'), and tim_ocxref(OCxCE = '1'). The Counter (CNT) is a sawtooth wave that resets at the CCRx threshold. The tim_etr signal is a pulse that goes high for a duration. The tim_ocxref(OCxCE = '0') signal is high when the counter is below CCRx and low otherwise. The tim_ocxref(OCxCE = '1') signal is high when the counter is below CCRx and low otherwise, but it remains high until the next counter overflow. Arrows indicate the points where tim_ocref_clr_int becomes high and remains high.

Timing diagram showing the behavior of tim_ocxref and tim_etr signals relative to the counter (CNT) and CCRx threshold. The diagram illustrates two cases: OCxCE = '0' and OCxCE = '1'. In the OCxCE = '0' case, tim_ocxref is cleared when tim_etr goes high. In the OCxCE = '1' case, tim_ocxref is cleared when tim_etr goes high, but it remains high until the next counter overflow. Arrows indicate the points where tim_ocref_clr_int becomes high and remains high.

Note: In case of a PWM with a 100% duty cycle (if CCRx > ARR), tim_ocxref is enabled again at the next counter overflow.

This feature is also supported in combined and asymmetric modes. In this case, it is mandatory to enable the OCxCE bit for the channel. The OCxCE bit of the complementary channel (for example: OC2CE= X for oc2ref in Figure 369) is ignored. Figure 369 to Figure 371 describe the output waveforms for specific use cases when the ocref_clr_int signal is asserted.

Figure 369. Asymmetric PWM mode 1

Timing diagram for Asymmetric PWM mode 1 showing CC1, CC2, oc1ref, oc2ref, and oc1refc signals.

The diagram illustrates the timing of various signals in Asymmetric PWM mode 1. At the top, two triangular carrier waves, CC1 and CC2, are shown with dashed horizontal lines indicating their peak levels. Below these are the reference signals: oc1ref and oc2ref. The output signal oc1refc is shown for two cases: (OC1CE = OC2CE = 0) and (OC1CE = 1, OC2CE = X). In the first case, the output is high when the carrier is below the lower of the two reference levels. In the second case, the output is high when the carrier is below the higher reference level, with a small pulse labeled 'ocref_clr_int' occurring at the end of each high period. The reference MSv76623V1 is located in the bottom right corner.

Timing diagram for Asymmetric PWM mode 1 showing CC1, CC2, oc1ref, oc2ref, and oc1refc signals.

Figure 370. Asymmetric PWM mode 2

Timing diagram for Asymmetric PWM mode 2 showing CC1, CC2, oc1ref, oc2ref, and oc1refc signals.

The diagram illustrates the timing of various signals in Asymmetric PWM mode 2. It features the same triangular carrier waves CC1 and CC2 at the top. The reference signals oc1ref and oc2ref are shown below. The output signal oc1refc is shown for two cases: (OC1CE = OC2CE = 0) and (OC1CE = 1, OC2CE = X). In this mode, the output is high when the carrier is above the lower reference level. The 'ocref_clr_int' pulse is shown at the end of each high period for the (OC1CE = 1, OC2CE = X) case. The reference MSv76624V1 is located in the bottom right corner.

Timing diagram for Asymmetric PWM mode 2 showing CC1, CC2, oc1ref, oc2ref, and oc1refc signals.

Figure 371. Combined PWM mode 1

Timing diagram for Combined PWM mode 1 showing counter reset, CC1 and CC2 ramps, and various output signals (oc1ref, oc2ref, oc1refc) under different OC1CE and OC2CE configurations.

The diagram illustrates the timing relationships for Combined PWM mode 1. It shows a counter ramping up and resetting. Two compare values, CC1 and CC2, are shown as horizontal dashed lines. The output signals are:

MSv76626V2

Timing diagram for Combined PWM mode 1 showing counter reset, CC1 and CC2 ramps, and various output signals (oc1ref, oc2ref, oc1refc) under different OC1CE and OC2CE configurations.

Conditions of use

This feature supports cycle-by-cycle current control loops, typically with a comparator shortening the PWM on-time when a current threshold is exceeded.

It cannot be used in the following modes, where the OCxCE bit must be cleared:

32.4.17 One-pulse mode

One-pulse mode (OPM) is a particular case of the previous modes. It allows the counter to be started in response to a stimulus and to generate a pulse with a programmable length after a programmable delay.

Starting the counter can be controlled through the slave mode controller. Generating the waveform can be done in output compare mode or PWM mode. One-pulse mode is selected by setting the OPM bit in the TIM_CR1 register. This makes the counter stop automatically at the next update event UEV.

A pulse can be correctly generated only if the compare value is different from the counter initial value. Before starting (when the timer is waiting for the trigger), the configuration must be:

CNT < CCRx ≤ ARR (in particular, 0 < CCRx).

Figure 372. Example of one-pulse mode

Timing diagram for one-pulse mode showing the relationship between tim_ti2, tim_oc1ref, tim_oc1, and the Counter. The diagram illustrates a positive edge on tim_ti2 triggering a delay period t_DELAY, after which a pulse of duration t_PULSE is generated on tim_oc1. The Counter ramps up from 0 to TIM_ARR, with a compare match at TIM_CCR1 marking the end of the delay period.

The diagram shows the following signals over time t:

Vertical dashed lines indicate the timing of the \(t_{DELAY}\) and \(t_{PULSE}\) intervals. The label MSv80077V1 is present in the bottom right corner of the diagram area.

Timing diagram for one-pulse mode showing the relationship between tim_ti2, tim_oc1ref, tim_oc1, and the Counter. The diagram illustrates a positive edge on tim_ti2 triggering a delay period t_DELAY, after which a pulse of duration t_PULSE is generated on tim_oc1. The Counter ramps up from 0 to TIM_ARR, with a compare match at TIM_CCR1 marking the end of the delay period.

For example if the user wants to generate a positive pulse on tim_oc1 with a length of \(t_{PULSE}\) and after a delay of \(t_{DELAY}\) as soon as a positive edge is detected on the tim_ti2 input pin.

Use tim_ti2fp2 as trigger 1:

  1. 1. Select the proper tim_ti2_in[15:0] source (internal or external) with the TI2SEL[3:0] bits in the TIM_TISEL register.
  2. 2. Map tim_ti2fp2 on tim_ti2 by writing CC2S = 01 in the TIM_CCMR1 register.
  3. 3. tim_ti2fp2 must detect a rising edge, write CC2P = 0 and CC2NP = 0 in the TIM_CCER register.
  4. 4. Configure tim_ti2fp2 as trigger for the slave mode controller (tim_trgi) by writing TS = 00110 in the TIM_SMCR register.
  5. 5. tim_ti2fp2 is used to start the counter by writing SMS to 110 in the TIM_SMCR register (trigger mode).

The OPM waveform is defined by writing the compare registers (taking into account the clock frequency and the counter prescaler).

In this example, the DIR and CMS bits in the TIM_CR1 register must be low.

Since only one pulse (single mode) is needed, a one must be written in the OPM bit in the TIM_CR1 register to stop the counter at the next update event (when the counter rolls over from the autoreload value back to 0). When OPM bit in the TIM_CR1 register is set to 0, so the repetitive mode is selected.

Particular case: tim_ocx fast enable:

In one-pulse mode, the edge detection on tim_tix input set the CEN bit which enables the counter. Then the comparison between the counter and the compare value makes the output toggle. But several clock cycles are needed for these operations and it limits the minimum delay \(t_{DELAY}\) min we can get.

If one wants to output a waveform with the minimum delay, the OCxFE bit can be set in the TIM_CCMRx register. Then tim_ocxref (and tim_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.

32.4.18 Retriggerable one-pulse mode

This mode allows the counter to be started in response to a stimulus and to generate a pulse with a programmable length, but with the following differences with non-retriggerable one-pulse mode described in Section 32.4.17:

The timer must be in slave mode, with the bits SMS[4:0] = 01000 (combined reset + trigger mode) in the TIM_SMCR register, and the OCxM[4:0] bits set to 01000 or 01001 for retriggerable OPM mode 1 or 2.

If the timer is configured in up-counting mode, the corresponding CCRx must be set to 0 (the ARR register sets the pulse length). If the timer is configured in down-counting mode CCRx must be above or equal to ARR.

Note: In retriggerable one-pulse mode, the CCxIF flag is not significant.

The OCxM[4:0] and SMS[4:0] bitfields are split into two parts for compatibility reasons, the most significant bit is not contiguous with the three least significant ones.

This mode must not be used with center-aligned PWM modes. It is mandatory to have CMS[1:0] = 00 in TIM_CR1.

Figure 373. Retriggerable one-pulse mode

Timing diagram for Retriggerable one-pulse mode showing tim_trgi, Counter, and tim_ocx signals.

The diagram illustrates the operation of the retriggerable one-pulse mode. It shows three signals over time: tim_trgi (trigger input), Counter (timer counter), and tim_ocx (output compare signal).

MSv62345V2

Timing diagram for Retriggerable one-pulse mode showing tim_trgi, Counter, and tim_ocx signals.

32.4.19 Pulse on compare mode

A pulse can be generated upon compare match event. A signal with a programmable pulse width generated when the counter value equals a given compare value, for debugging or synchronization purposes.

This mode is available for any slave mode selection, including encoder modes, in edge and center aligned counting modes. It is solely available for channel 3 and channel 4. The pulse generator is unique and is shared by the two channels, as shown on Figure 374.

Figure 374. Pulse generator circuitry

Block diagram of the pulse generator circuitry for TIM3 and TIM4 channels.

The diagram illustrates the pulse generator circuitry shared between channels 3 and 4. It shows the following components and connections:

MSv62346V1

Block diagram of the pulse generator circuitry for TIM3 and TIM4 channels.

Figure 375 shows how the pulse is generated for edge-aligned and encoder operating modes.

Figure 375. Pulse generation on compare event, for edge-aligned and encoder modes

Figure 375: Pulse generation on compare event, for edge-aligned and encoder modes. The figure consists of two timing diagrams. The top diagram shows edge-aligned mode with a counter ramping up, a CMP3 threshold, and a tim_ocx output pulse. A re-trigger event is shown where a new trigger occurs while the pulse is ongoing, resulting in an 'Extended pulsewidth due to re-trigger'. The bottom diagram shows encoder mode with a counter ramping up and then down, and a tim_ocx output pulse. A re-trigger event is shown where a new trigger occurs while the pulse is ongoing, resulting in an extended pulsewidth.

MSv62347V1

Figure 375: Pulse generation on compare event, for edge-aligned and encoder modes. The figure consists of two timing diagrams. The top diagram shows edge-aligned mode with a counter ramping up, a CMP3 threshold, and a tim_ocx output pulse. A re-trigger event is shown where a new trigger occurs while the pulse is ongoing, resulting in an 'Extended pulsewidth due to re-trigger'. The bottom diagram shows encoder mode with a counter ramping up and then down, and a tim_ocx output pulse. A re-trigger event is shown where a new trigger occurs while the pulse is ongoing, resulting in an extended pulsewidth.

This output compare mode is selected using the OC3M[3:0] and OC4M[3:0] bitfields in TIM_CCMR2 register.

The pulse width is programmed using the PW[7:0] bitfield in the register, using a specific clock prescaled according to PWPRSC[2:0] bits, as follows:

\[t_{PW} = PW[7:0] \times t_{PWG}\]

\[\text{where } t_{PWG} = (2^{(PWPRSC[2:0])}) \times t_{tim\_ker\_ck}\]

Gives the resolution and maximum values depending on the prescaler value.

The pulse is retriggerable: a new trigger while the pulse is ongoing, causes the pulse to be extended.

Note: If the two channels are enabled simultaneously, the pulses are issued independently as long as the trigger on one channel is not overlapping the pulse generated on the concurrent output. On the opposite, if the two triggers are overlapping, the pulse width related to the first arriving trigger is extended (because of the retrigger), while the pulse width of the last arriving trigger is correct (as shown on Figure 376).

Figure 376. Extended pulse width in case of concurrent triggers

Timing diagram showing extended pulse width in case of concurrent triggers. It displays four signals: Trigger CMP3, Trigger CMP4, tim_oc3, and tim_oc4. Trigger CMP3 and Trigger CMP4 are shown as vertical dashed lines with downward arrows. tim_oc3 is a square wave that is high when Trigger CMP3 is active. tim_oc4 is a square wave that is high when Trigger CMP4 is active. A dashed box highlights a portion of the tim_oc3 high pulse, with an arrow pointing to it and the text 'Extended pulsewidth due to overlapping CMP4 trigger'. The diagram is labeled MSv62348V1 in the bottom right corner.
Timing diagram showing extended pulse width in case of concurrent triggers. It displays four signals: Trigger CMP3, Trigger CMP4, tim_oc3, and tim_oc4. Trigger CMP3 and Trigger CMP4 are shown as vertical dashed lines with downward arrows. tim_oc3 is a square wave that is high when Trigger CMP3 is active. tim_oc4 is a square wave that is high when Trigger CMP4 is active. A dashed box highlights a portion of the tim_oc3 high pulse, with an arrow pointing to it and the text 'Extended pulsewidth due to overlapping CMP4 trigger'. The diagram is labeled MSv62348V1 in the bottom right corner.

32.4.20 Encoder interface mode

Quadrature encoder

To select encoder interface mode write SMS = 0001 in the TIM_SMCR register if the counter is counting on tim_ti1 edges only, SMS = 0010 if it is counting on tim_ti2 edges only and SMS = 0011 if it is counting on both tim_ti1 and tim_ti2 edges.

Select the tim_ti1 and tim_ti2 polarity by programming the CC1P and CC2P bits in the TIM_CCER register. CC1NP and CC2NP must be kept cleared. When needed, the input filter can be programmed as well.

The two inputs tim_ti1 and tim_ti2 are used to interface to an incremental encoder. Refer to Table 321. The counter is clocked by each valid transition on tim_ti1fp1 or tim_ti2fp2 (tim_ti1 and tim_ti2 after input filter and polarity selection, tim_ti1fp1 = tim_ti1 if not filtered and not inverted, tim_ti2fp2 = tim_ti2 if not filtered and not inverted) assuming that it is enabled (CEN bit in TIM_CR1 register written to 1). The sequence of transitions of the two inputs is evaluated and generates count pulses as well as the direction signal. Depending on the sequence the counter counts up or down, the DIR bit in the TIM_CR1 register is modified by hardware accordingly. The DIR bit is calculated at each transition on any input (tim_ti1 or tim_ti2), whatever the counter is counting on tim_ti1 only, tim_ti2 only or both tim_ti1 and tim_ti2.

Encoder interface mode acts simply as an external clock with direction selection. This means that the counter just counts continuously between 0 and the autoreload value in the TIM_ARR register (0 to ARR or ARR down to 0 depending on the direction). So the TIM_ARR must be configured before starting. In the same way, the capture, compare, prescaler, trigger output features continue to work as normal. Encoder mode and external clock mode 2 are not compatible and must not be selected together.

In this mode, the counter is modified automatically following the speed and the direction of the quadrature encoder and its content, therefore, always represents the encoder's position. The count direction corresponds to the rotation direction of the connected sensor. The table summarizes the possible combinations, assuming tim_ti1 and tim_ti2 do not switch at the same time.

Table 321. Counting direction versus encoder signals(CC1P = CC2P = 0)

Active edgeSMS[4:0]Level on opposite signal
(tim_ti1fp1 for tim_ti2,
tim_ti2fp2 for tim_ti1)
tim_ti1fp1 signaltim_ti2fp2 signal
RisingFallingRisingFalling
Counting on tim_ti1 only
x1 mode
01110HighDownUpNo countNo count
LowNo countNo countNo countNo count
Counting on tim_ti2 only
x1 mode
01111HighNo countNo countUpDown
LowNo countNo countNo countNo count
Counting on tim_ti1 only
x2 mode
00001HighDownUpNo countNo count
LowUpDownNo countDown
Counting on tim_ti2 only
x2 mode
00010HighNo countNo countUpDown
LowNo countNo countDownUp
Counting on tim_ti1 and
tim_ti2
x4 mode
00011HighDownUpUpDown
LowUpDownDownUp

A quadrature 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 indicates the mechanical zero position, can be connected to the external trigger input and trigger a counter reset.

Figure 377 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:

Figure 377. Example of counter operation in encoder interface mode

Timing diagram for Figure 377 showing tim_ti1, tim_ti2, and Counter signals across forward, jitter, backward, and forward phases. The counter increments (up) during the first forward phase and the final forward phase, and decrements (down) during the backward phase.

The diagram shows three signals over time: tim_ti1, tim_ti2, and Counter. The timeline is divided into five segments: forward, jitter, backward, jitter, and forward. In the first forward segment, tim_ti1 is high and tim_ti2 is low, causing the Counter to increment (labeled up). In the jitter segment, both signals are high. In the backward segment, tim_ti1 is low and tim_ti2 is high, causing the Counter to decrement (labeled down). In the second jitter segment, both signals are low. In the final forward segment, tim_ti1 is high and tim_ti2 is low, causing the Counter to increment again (labeled up). The label MSv62349V1 is in the bottom right corner.

Timing diagram for Figure 377 showing tim_ti1, tim_ti2, and Counter signals across forward, jitter, backward, and forward phases. The counter increments (up) during the first forward phase and the final forward phase, and decrements (down) during the backward phase.

Figure 378 gives an example of counter behavior when tim_ti1fp1 polarity is inverted (same configuration as above except CC1P = 1).

Figure 378. Example of encoder interface mode with tim_ti1fp1 polarity inverted

Timing diagram for Figure 378 showing tim_ti1, tim_ti2, and Counter signals with inverted polarity for tim_ti1fp1. The counter decrements (down) during the first forward phase and increments (up) during the backward phase.

The diagram shows the same signals and phases as Figure 377, but with tim_ti1fp1 polarity inverted. In the first forward segment, tim_ti1 is high and tim_ti2 is low, causing the Counter to decrement (labeled down). In the jitter segment, both signals are high. In the backward segment, tim_ti1 is low and tim_ti2 is high, causing the Counter to increment (labeled up). In the second jitter segment, both signals are low. In the final forward segment, tim_ti1 is high and tim_ti2 is low, causing the Counter to decrement again (labeled down). The label MSv62350V1 is in the bottom right corner.

Timing diagram for Figure 378 showing tim_ti1, tim_ti2, and Counter signals with inverted polarity for tim_ti1fp1. The counter decrements (down) during the first forward phase and increments (up) during the backward phase.

Figure 379 shows the timer counter value during a speed reversal, for various counting modes.

Figure 379. Quadrature encoder counting modes

Timing diagram for Quadrature encoder counting modes showing tim_ti1, tim_ti2, DIR bit, Counter x4, Counter x2, and Counter x1 signals.

The diagram illustrates the counter values for three different counting modes (x4, x2, and x1) during a speed reversal. The signals shown are:

MSv62351V1

Timing diagram for Quadrature encoder counting modes showing tim_ti1, tim_ti2, DIR bit, Counter x4, Counter x2, and Counter x1 signals.

The timer, when configured in encoder interface mode provides information on the sensor's current position. Dynamic information can be obtained (speed, acceleration, deceleration) by measuring the period between two encoder events using a second timer configured in capture mode. The output of the encoder which indicates the mechanical zero can be used for this purpose. Depending on the time between two events, the counter can also be read at regular times. This can be done by latching the counter value into a third input capture register if available (then the capture signal must be periodic and can be generated by another timer). When available, it is also possible to read its value through a DMA request.

The IUFREMAP bit in the TIM_CR1 register forces a continuous copy of the update interrupt flag (UIF) into the timer counter register's bit 31 (TIM_CNT[31]). This allows both the counter value and a potential roll-over condition signaled by the UIFCPY flag to be read in an atomic way. It eases the calculation of angular speed by avoiding race conditions caused, for instance, by a processing shared between a background task (counter reading) and an interrupt (update interrupt).

There is no latency between the UIF and UIFCPY flag assertions.

In 32-bit timer implementations, when the IUFREMAP bit is set, bit 31 of the counter is overwritten by the UIFCPY flag upon read access (the counter's most significant bit is only accessible in write mode).

Clock plus direction encoder mode

In addition to the quadrature encoder mode, the timer offers support for other types of encoders.

In the "clock plus direction" mode shown on Figure 380, the clock is provided on a single line, on tim_ti2, while the direction is forced using the tim_ti1 input.

This mode is enabled with the SMS[4:0] bitfield in the TIM_SMCR register, as following:

The polarity of the direction signal on tim_ti1 is set with the CC1P bit: 0 corresponds to positive polarity (up-counting when tim_ti1 is high and down-counting when tim_ti1 is low) and CC1P = 1 corresponds to negative polarity (up-counting when tim_ti1 is low).

Figure 380. Direction plus clock encoder mode

Timing diagram for Direction plus clock encoder mode showing tim_ti1, tim_ti2, and counter values for x2 and x1 modes.

The diagram illustrates the Direction plus clock encoder mode. It shows two input signals, tim_ti1 and tim_ti2, and the resulting counter values for two modes: x2 mode and x1 mode.

MSv62352V1

Timing diagram for Direction plus clock encoder mode showing tim_ti1, tim_ti2, and counter values for x2 and x1 modes.

Directional clock encoder mode

In the “directional clock” mode on Figure 381, the clocks are provided on two lines, with a single one at once, depending on the direction, so as to have one up-counting clock line and one down-counting clock line.

This mode is enabled with the SMS[4:0] bitfield in the TIM_SMCR register, as following:

Figure 381. Directional clock encoder mode (CC1P = CC2P = 0)

Timing diagram for Figure 381 showing tim_ti1, tim_ti2, DIR bit, Counter x2 mode, and Counter x1 mode. The counter counts up from 6 to 11 and then down to 5 as tim_ti1 transitions from low to high. DIR bit is low during the up-count and high during the down-count.

Figure 381 is a timing diagram for the Directional clock encoder mode when CC1P = CC2P = 0. It shows the relationship between two input signals, tim_ti1 and tim_ti2, a DIR bit, and the resulting counter values in x2 and x1 modes. The counter starts at 6 and counts up to 11 as tim_ti1 transitions from low to high. The DIR bit is low during the up-count and high during the down-count. The counter values are shown in a sequence of boxes: 6, 7, 8, 9, 10, 11, 10, 9, 8, 7, 6, 5. The counter x2 mode shows the full sequence of values, while the counter x1 mode shows only the values 6, 7, 8, 7, 6, 5. The diagram is labeled MSv62353V1.

Timing diagram for Figure 381 showing tim_ti1, tim_ti2, DIR bit, Counter x2 mode, and Counter x1 mode. The counter counts up from 6 to 11 and then down to 5 as tim_ti1 transitions from low to high. DIR bit is low during the up-count and high during the down-count.

Figure 382. Directional clock encoder mode (CC1P = CC2P = 1)

Timing diagram for Figure 382 showing tim_ti1, tim_ti2, DIR bit, Counter x2 mode, and Counter x1 mode. The counter counts up from 6 to 11 and then down to 5 as tim_ti1 transitions from high to low. DIR bit is high during the up-count and low during the down-count.

Figure 382 is a timing diagram for the Directional clock encoder mode when CC1P = CC2P = 1. It shows the relationship between two input signals, tim_ti1 and tim_ti2, a DIR bit, and the resulting counter values in x2 and x1 modes. The counter starts at 6 and counts up to 11 as tim_ti1 transitions from high to low. The DIR bit is high during the up-count and low during the down-count. The counter values are shown in a sequence of boxes: 6, 7, 8, 9, 10, 11, 10, 9, 8, 7, 6, 5. The counter x2 mode shows the full sequence of values, while the counter x1 mode shows only the values 7, 8, 9, 8, 7, 6. The diagram is labeled MSv62354V1.

Timing diagram for Figure 382 showing tim_ti1, tim_ti2, DIR bit, Counter x2 mode, and Counter x1 mode. The counter counts up from 6 to 11 and then down to 5 as tim_ti1 transitions from high to low. DIR bit is high during the up-count and low during the down-count.

Table 322 details how the directional clock mode operates, for any input transition.

Table 322. Counting direction versus encoder signals and polarity settings

Directional clock modeSMS[4:0]Level on opposite signal
(tim_ti1fp1 for tim_ti2,
tim_ti2fp2 for tim_ti1)
tim_ti1fp1 signaltim_ti2fp2 signal
RisingFallingRisingFalling
x2 mode
CCxP = 0
01100HighDownDownUpUp
LowNo countNo countNo countNo count
x2 mode
CCxP = 1
01100HighNo countNo countNo countNo count
LowDownDownUpUp
x1 mode
CCxP = 0
01101HighNo countDownNo countUp
LowNo countNo countNo countNo count
x1 mode
CCxP = 1
01101HighNo countNo countNo countNo count
LowDownNo countUpNo count

Index input

The counter can be reset by an index signal coming from the encoder, indicating an absolute reference position. The index signal must be connected to the tim_etr_in input. It can be filtered using the digital input filter.

The index functionality is enabled with the IE bit in the TIM_ECR register. The IE bit must be set only in encoder mode, when the SMS[4:0] bitfield has the following values: 00001, 00010, 00011, 01010, 01011, 01100, 01101, 01110, 01111.

Available encoders are proposed with several options for index pulse conditioning, as per Figure 383:

Figure 383. Index gating options

Figure 383: Index gating options. This timing diagram shows five signal traces: Channel A, Channel B, Gated A & B, Gated A, and Ungated. Channel A and B are square waves. Gated A & B is a pulse that occurs only when both Channel A and Channel B are high. Gated A is a pulse that occurs only when Channel A is high, regardless of Channel B. Ungated is a continuous square wave. Vertical dashed lines indicate the timing of the index signal relative to the encoder signals. The label 'MSv45765V1' is in the bottom right corner.
Figure 383: Index gating options. This timing diagram shows five signal traces: Channel A, Channel B, Gated A & B, Gated A, and Ungated. Channel A and B are square waves. Gated A & B is a pulse that occurs only when both Channel A and Channel B are high. Gated A is a pulse that occurs only when Channel A is high, regardless of Channel B. Ungated is a continuous square wave. Vertical dashed lines indicate the timing of the index signal relative to the encoder signals. The label 'MSv45765V1' is in the bottom right corner.

The circuitry tolerates jitter on index signal, whatever the gating mode, as shown on Figure 384.

In ungated mode, the signal must be strictly below two encoder periods. If the pulse width is greater or equal to two encoder period, the counter is reset multiple times.

Figure 384. Jittered Index signals

Figure 384: Jittered Index signals. This timing diagram shows the same five signal traces as Figure 383, but with jittered index signals. The Gated A & B, Gated A, and Ungated signals show multiple overlapping pulses (hatched areas) due to jitter. A horizontal double-headed arrow at the bottom indicates the 'Max pulsewidth ungated mode'. The label 'MSv45766V1' is in the bottom right corner.
Figure 384: Jittered Index signals. This timing diagram shows the same five signal traces as Figure 383, but with jittered index signals. The Gated A & B, Gated A, and Ungated signals show multiple overlapping pulses (hatched areas) due to jitter. A horizontal double-headed arrow at the bottom indicates the 'Max pulsewidth ungated mode'. The label 'MSv45766V1' is in the bottom right corner.

The timer supports the three gating options identically, without any specific programming needed. It is only necessary to define on which encoder state (for example channel A and channel B state combination) the index must be synchronized, using the IPOS[1:0] bitfield in the TIM_ECR register.

The index detection event acts differently depending on counting direction to ensure symmetrical operation during speed reversal:

This allows the index to be generated on the very same mechanical angular position whatever the counting direction. Figure 385 shows at which position is the index generated, for a simplistic example (an encoder providing four edges par mechanical rotation).

Figure 385. Index generation for IPOS[1:0] = 11

State transition diagram for index generation for IPOS[1:0] = 11 showing 4 states: AB = 00 State 1, AB = 01 State 2, AB = 11 State 3, AB = 10 State 4. Rotor angles: 0°, 90°, 180°, 270°. Up-counting and Down-counting directions shown. An arrow pointing to Rotor angle = 180° / AB = 11 State 3 notes: 'The index event is always generated here'. Diagram code MSv45767V1.
State transition diagram for index generation for IPOS[1:0] = 11 showing 4 states: AB = 00 State 1, AB = 01 State 2, AB = 11 State 3, AB = 10 State 4. Rotor angles: 0°, 90°, 180°, 270°. Up-counting and Down-counting directions shown. An arrow pointing to Rotor angle = 180° / AB = 11 State 3 notes: 'The index event is always generated here'. Diagram code MSv45767V1.

Figure 386 presents waveforms and corresponding values for IPOS[1:0] = 11. It shows that the instant at which the counter value is forced is automatically adjusted depending on the counting direction:

An interrupt can be issued upon index detection event.

The arrows are indicating on which transition is the index event interrupt generated.

Figure 386. Counter reading with index gated on channel A (IPOS[1:0] = 11)

Waveforms for Channel A, Channel B, Index, DIR bit, and Counter sequence showing counter reset/reload behavior gated on channel A for IPOS[1:0] = 11. MSv45768V1.
Waveforms for Channel A, Channel B, Index, DIR bit, and Counter sequence showing counter reset/reload behavior gated on channel A for IPOS[1:0] = 11. MSv45768V1.

Figure 387 presents waveforms and corresponding values for the ungated mode. The arrows are indicating on which transition is the index event generated.

Figure 387. Counter reading with index ungated (IPOS[1:0] = 00)

Timing diagram for Figure 387 showing Channel A, Channel B, Index, DIR bit, and Counter transitions. The counter increments on Channel B falling edges and decrements on Channel B rising edges, with Index pulses gating the transitions.

The diagram shows the relationship between Channel A, Channel B, Index, DIR bit, and Counter. Channel A is a square wave. Channel B is a square wave phase-shifted from Channel A. The Index signal consists of periodic pulses. The DIR bit is low for the first half of the sequence and high for the second half. The Counter is shown as a sequence of 8-bit values. Arrows indicate that counter transitions occur on the falling edges of Channel B, and these transitions are only registered when an Index pulse is active.

Timing diagram for Figure 387 showing Channel A, Channel B, Index, DIR bit, and Counter transitions. The counter increments on Channel B falling edges and decrements on Channel B rising edges, with Index pulses gating the transitions.

Figure 388 shows how the gated on A & B mode is handled, for various pulse alignment scenarios. The arrows are indicating on which transition is the index event generated.

Figure 388. Counter reading with index gated on channel A and B

Timing diagram for Figure 388 showing Channel A, Channel B, Index, DIR bit, and Counter transitions. The counter increments on Channel A falling edges and decrements on Channel A rising edges, with Index pulses gating the transitions.

The diagram shows the relationship between Channel A, Channel B, Index, DIR bit, and Counter. Channel A is a square wave. Channel B is a square wave phase-shifted from Channel A. The Index signal consists of periodic pulses. The DIR bit is low for the first half of the sequence and high for the second half. The Counter is shown as a sequence of 8-bit values. Arrows indicate that counter transitions occur on the falling edges of Channel A, and these transitions are only registered when an Index pulse is active.

Timing diagram for Figure 388 showing Channel A, Channel B, Index, DIR bit, and Counter transitions. The counter increments on Channel A falling edges and decrements on Channel A rising edges, with Index pulses gating the transitions.

Figure 389 and Figure 390 detail the case where the subsequent index pulse may be narrower than one quarter of the encoder clock period.

Figure 389. Encoder mode behavior in case of narrow index pulse (IPOS[1:0] = 11)

Timing diagrams for encoder mode behavior with narrow index pulses, showing Channel A, Channel B, Index, DIR bit, and Counter states for leading and delayed transitions.

The figure consists of two timing diagrams, one above the other, illustrating encoder mode behavior with a narrow index pulse. Both diagrams share the same set of signals: Channel A, Channel B, Index, DIR bit, and Counter.

Top Diagram: Index leading state transition

Bottom Diagram: Index delayed versus state transition

MSv45771V1

Timing diagrams for encoder mode behavior with narrow index pulses, showing Channel A, Channel B, Index, DIR bit, and Counter states for leading and delayed transitions.

Figure 390. Counter reset Narrow index pulse (closer view, ARR = 0x07)

Timing diagram for counter reset narrow index pulse showing Channel A, Channel B, Index, DIR bit, and Counter states across two cycles.

The diagram illustrates the timing relationship between Channel A, Channel B, Index, DIR bit, and Counter for a counter reset narrow index pulse. It is divided into two identical sections, each showing a sequence of events over several counter cycles.

Top Section:

Bottom Section:

Vertical dashed lines indicate the boundaries of the counter cycles. The Index and DIR bit pulses are narrow and occur specifically during counter cycle 0.

MSv45772V1

Timing diagram for counter reset narrow index pulse showing Channel A, Channel B, Index, DIR bit, and Counter states across two cycles.

Figure 391 shows how the index is managed in x1 and x2 modes.

Timing diagram for Figure 391 showing Channel A, Channel B, Index, DIR bit, Counter x2, and Counter x1 signals. The diagram illustrates the index behavior in x1 and x2 modes when IPOS[1:0] = 01. Channel A and B are square waves. The Index signal is a pulse. The DIR bit is a control signal. Counter x2 and Counter x1 show the counting sequence with index resets.

Figure 391. Index behavior in x1 and x2 mode (IPOS[1:0] = 01)

AB = IPOS[1:0] = 01

Channel A

Channel B

Index

DIR bit

Counter x2: 10, 11, 0, 1, 2, 1, 0, 11, 10, 9, 8

Counter x1: 5, 6, 7, 0, 1, 3

MSv45773V1

Timing diagram for Figure 391 showing Channel A, Channel B, Index, DIR bit, Counter x2, and Counter x1 signals. The diagram illustrates the index behavior in x1 and x2 modes when IPOS[1:0] = 01. Channel A and B are square waves. The Index signal is a pulse. The DIR bit is a control signal. Counter x2 and Counter x1 show the counting sequence with index resets.

Directional index sensitivity

The IDIR[1:0] bitfield in the TIM_ECR register allows the index to be active only in a selected counting direction.

Figure 392 shows the relationship between index and counter reset events, depending on IDIR[1:0] value.

Timing diagram for Figure 392 showing DIR bit, Counter, Index input, and Counter reset for different IDIR[1:0] values. The diagram illustrates the relationship between index and counter reset events depending on the IDIR[1:0] value. The DIR bit controls UP-counting and Down-counting. The Counter signal shows the counting sequence. The Index input is a pulse. The Counter reset signal shows the reset events for IDIR[1:0]=00, IDIR[1:0]=01, and IDIR[1:0]=10.

Figure 392. Directional index sensitivity

DIR bit: UP-counting, Down-counting

Counter

Index input

Counter reset

IDIR[1:0]=00

IDIR[1:0]=01

IDIR[1:0]=10

MSv45774V1

Timing diagram for Figure 392 showing DIR bit, Counter, Index input, and Counter reset for different IDIR[1:0] values. The diagram illustrates the relationship between index and counter reset events depending on the IDIR[1:0] value. The DIR bit controls UP-counting and Down-counting. The Counter signal shows the counting sequence. The Index input is a pulse. The Counter reset signal shows the reset events for IDIR[1:0]=00, IDIR[1:0]=01, and IDIR[1:0]=10.

Special first index event management

The FIDX bit in the TIM_ECR register allows the index to be taken only once, as shown on Figure 393. Once the first index has arrived, any subsequent index is ignored. If needed, the circuitry can be rearmed by writing the FIDX bit to 0 and setting it again to 1.

Figure 393. Counter reset as function of FIDX bit setting

Timing diagram for Figure 393 showing Counter, Index input, and Counter reset for FIDX = 0 and FIDX = 1.

The diagram illustrates the behavior of the counter based on the FIDX bit setting. It consists of four horizontal signal traces:

MSv45775V1

Timing diagram for Figure 393 showing Counter, Index input, and Counter reset for FIDX = 0 and FIDX = 1.

Index blanking

The index event can be blanked using the tim_ti3 or tim_ti4 inputs. During the blanking window, the index events are no longer resetting the counter, as shown on Figure 394.

This mode is enabled using the IBLK[1:0] bitfield in the TIM_ECR register, as following:

Figure 394. Index blanking

Timing diagram for Figure 394 showing Counter, Index input, Blanking signal TI3, and Counter reset for IBLK[1:0] = 00 and IBLK[1:0] = 01.

The diagram illustrates index blanking functionality. It consists of five horizontal signal traces:

MSv45776V1

Timing diagram for Figure 394 showing Counter, Index input, Blanking signal TI3, and Counter reset for IBLK[1:0] = 00 and IBLK[1:0] = 01.

Index management in nonquadrature mode

Figure 395 and Figure 396 detail how the index is managed in directional clock mode and clock plus direction mode, when the SMS[4:0] bitfield is equal to 01010, 01011, 01100, 01101.

For both of these modes, the index sensitivity is set with the IPOS[0] bit as following:

The IPOS[1] bit is not-significant.

Figure 395. Index behavior in clock + direction mode, IPOS[0] = 1

Figure 395. Index behavior in clock + direction mode, IPOS[0] = 1: Waveforms showing Direction (TI1), Clock (TI2), Index, Counter x2 mode (values 7, 0, 1, 2, 3, 4, 3, 2, 7, 6, 5), and Counter x1 mode (values 7, 0, 1, 2, 1, 7).

MSv45777V1

Figure 395. Index behavior in clock + direction mode, IPOS[0] = 1: Waveforms showing Direction (TI1), Clock (TI2), Index, Counter x2 mode (values 7, 0, 1, 2, 3, 4, 3, 2, 7, 6, 5), and Counter x1 mode (values 7, 0, 1, 2, 1, 7).

Figure 396. Index behavior in directional clock mode, IPOS[0] = 1

Figure 396. Index behavior in directional clock mode, IPOS[0] = 1: Waveforms showing Clock Down (TI1), Clock Up (TI2), DIR bit, Counter x2 mode (values 9, 0, 1, 2, 3, 4, 3, 2, 1, 0, 9, 8), and Counter x1 mode (values 9, 0, 1, 2, 1, 0, 9).

MSv45778V1

Figure 396. Index behavior in directional clock mode, IPOS[0] = 1: Waveforms showing Clock Down (TI1), Clock Up (TI2), DIR bit, Counter x2 mode (values 9, 0, 1, 2, 3, 4, 3, 2, 1, 0, 9, 8), and Counter x1 mode (values 9, 0, 1, 2, 1, 0, 9).

Encoder error management

For encoder configurations where two quadrature signals are available, it is possible to detect transition errors. The reading on the two inputs corresponds to a 2-bit gray code which can be represented as a state diagram, on Figure 397. A single bit is expected to change at once. An erroneous transition sets the TERRF interrupt flag in the TIM_SR status

register. A transition error interrupt is generated if the TERRIE bit is set in the TIM_DIER register.

Figure 397. State diagram for quadrature encoded signals

State diagram for quadrature encoded signals showing four states (00, 01, 10, 11) and their transitions. Correct transitions are shown with solid double-headed arrows between adjacent states (00↔01, 01↔11, 11↔10, 10↔00). Erroneous transitions are shown with dashed double-headed arrows between diagonal states (00↔11, 01↔10).

Legend:
↔ Correct transitions
↔ Erroneous transitions

MSv45779V1

State diagram for quadrature encoded signals showing four states (00, 01, 10, 11) and their transitions. Correct transitions are shown with solid double-headed arrows between adjacent states (00↔01, 01↔11, 11↔10, 10↔00). Erroneous transitions are shown with dashed double-headed arrows between diagonal states (00↔11, 01↔10).

For encoder having an index signal, it is possible to detect abnormal operation resulting in an excess of pulses per revolution. An encoder with N pulses per revolution provides 4xN counts per revolution. The index signal resets the counter every 4xN clock periods.

If the counter value is incremented from TIM_ARR to 0 or decremented from 0 to TIM_ARR value without any index event, this is reported as an index position error.

The overflow threshold is programmed using the TIM_ARR register. A 1000 lines encoder results in a counter value being between 0 and 3999 (in 4x reading mode). The overflow detection threshold must be programmed by setting TIM_ARR = 3999+1 = 4000.

The error assertion is delayed to the transition 0 to 1 when in up-counting. This is to cope with narrow index pulses in gated A and B mode, as shown on Figure 398.

Figure 398. Up-counting encoder error detection

Timing diagram showing up-counting encoder error detection for two scenarios. The top scenario shows an 'Abort (index detection)' occurring at the transition from counter value 7 to 0. The bottom scenario shows an 'Error detected' at the transition from 7 to 0 and an 'Error asserted' at the transition from 0 to 1. Both scenarios show Channel A, Channel B, Index, IERRF, and Counter signals.

The figure consists of two separate timing diagrams, one above the other, illustrating encoder error detection during up-counting. Both diagrams share the same set of signals: Channel A, Channel B, Index, IERRF, and Counter.

Top Diagram: Shows Channel A and Channel B as square waves. Channel B is high when Channel A is low, and Channel B is low when Channel A is high. The Index signal is a narrow pulse that occurs during the transition from counter value 7 to 0. The IERRF signal remains low throughout. The Counter signal shows values 5, 6, 7, 0, 1, 2, 3 in sequence. An arrow labeled "Error detected" points to the transition from 7 to 0. Another arrow labeled "Abort (index detection)" points to the Index pulse.

Bottom Diagram: Shows the same Channel A and Channel B signals. The Index signal is a narrow pulse that occurs during the transition from counter value 0 to 1. The IERRF signal is low until the transition from 0 to 1, at which point it transitions high. The Counter signal shows values 5, 6, 7, 0, 1, 2, 3 in sequence. An arrow labeled "Error detected" points to the transition from 7 to 0. Another arrow labeled "Error asserted" points to the transition from 0 to 1.

MSv45780V1

Timing diagram showing up-counting encoder error detection for two scenarios. The top scenario shows an 'Abort (index detection)' occurring at the transition from counter value 7 to 0. The bottom scenario shows an 'Error detected' at the transition from 7 to 0 and an 'Error asserted' at the transition from 0 to 1. Both scenarios show Channel A, Channel B, Index, IERRF, and Counter signals.

In down-counting mode, the detection is conditioned by a preliminary transition from 1 to 0. This is to cope with narrow index pulses in gated A and B mode, as shown on Figure 399, to avoid any false error detection in case the encoder dithers between TIM_ARR and 0 immediately after the index detection.

Figure 399. Down-counting encode error detection

Figure 399. Down-counting encode error detection

MSv80078V1

Figure 399. Down-counting encode error detection

An index error sets the IERRF interrupt flag in the TIM_SR status register. An index error interrupt is generated if the IERRIE bit is set in the TIM_DIER register.

Functional encoder interrupts

The following interrupts are also available in encoder mode

Slave mode selection preload for run-time encoder mode update

It can be necessary to switch from one encoder mode to another during run-time. This is typically done at high-speed to decrease the update interrupt rate, by switching from x4 to x2 to x1 mode, as shown on Figure 400.

For this purpose, the SMS[4:0] bit can be preloaded. This is enabled by setting the SMSPE enable bit in the TIM_SMCR register. The trigger for the transfer from SMS[4:0] preload to active value can be selected with the SMSPS bit in the TIM_SMCR register.

Figure 400. Encoder mode change with preload transferred on update (SMSPS = 0)

Timing diagram showing encoder mode change with preload transferred on update (SMSPS = 0). The diagram illustrates the transition from x4 mode to x2 mode to x1 mode. It shows the Update event, Preload value, and Active value for the SMS register. The Preload value is updated before the Update event, and the Active value is updated after the Update event.

The diagram illustrates the encoder mode change process. It shows three modes: x4 mode, x2 mode, and x1 mode. The Update event is shown as a series of pulses. The Preload value is shown as a sequence of SMS values: SMS = 0011, SMS = 0001, and SMS = 1110. The Active value is shown as a sequence of SMS values: SMS = 0011, SMS = 0001, and SMS = 1110. The Preload value is updated before the Update event, and the Active value is updated after the Update event.

ModePreload valueActive value
x4 modeSMS = 0011SMS = 0011
x2 modeSMS = 0001SMS = 0001
x1 modeSMS = 1110SMS = 1110

MSv45781V1

Timing diagram showing encoder mode change with preload transferred on update (SMSPS = 0). The diagram illustrates the transition from x4 mode to x2 mode to x1 mode. It shows the Update event, Preload value, and Active value for the SMS register. The Preload value is updated before the Update event, and the Active value is updated after the Update event.

Encoder clock output

The encoder mode operating principle is not perfectly suited for high-resolution velocity measurements, at low speed, as it requires a relatively long integration time to have a sufficient number of clock edges and a precise measurement.

At low speed, a better solution is to do an edge-to-edge clock period measurement. This can be achieved using a slave timer. The timer can output the encoder clock information on the tim_trgo output. The slave timer can then perform a period measurement and provide velocity information for each and every encoder clock edge.

This mode is enabled by setting the MMS[3:0] bitfield to 1000, in the TIM_CR2 register. It is valid for the following SMS[4:0] values: 00001, 00010, 00011, 01010, 01011, 01100, 01101, 01110, 01111. Any other SMS[4:0] code is not allowed and may lead to unexpected behavior.

32.4.21 Encoder with built-in debouncer

This mode is intended to support noisy digital potentiometers and knobs, providing quadrature encoded signals with potentially long bouncing periods after each transition.

The operating principle consists of working on the first valid transition and discard any other until a second valid transition is detected on the alternate encoder signal.

The Figure 401 below represents the x2 operating mode. It must be noticed that a transition on input 1 is ignored during direction reversal, if there's no edge on input 2.

The Figure 402 below represents the x4 operating mode. In this case, a transition is always ignored during a direction reversal or when the encoder dithers between two positions.

These two modes are enabled by writing SMS[4:0] = 10000 or 10001 in TIM_SMCR register.

The encoder with built-in debouncer modes are not suitable for tracking a position or an angle in motor control applications. The transitions ignored during direction reversal will lead to a position/angle error that will build-up over time.

Figure 401. Encoder with built-in debouncer, x2 mode

Timing diagram for x2 mode showing Input 1, Input 2, Debounced signals, Direction, and Counter x2 values.

The diagram illustrates the x2 operating mode. It shows two scenarios. In the first scenario, Input 1 transitions from high to low while Input 2 is high, causing the Direction to flip from high to low and the Counter x2 to increment from 1 to 2. In the second scenario, Input 1 transitions from low to high while Input 2 is low, causing the Direction to flip from low to high and the Counter x2 to decrement from 2 to 1. The 'Debounced' signals show the filtered versions of the inputs, and the 'Direction' signal indicates the current direction of counting.

Timing diagram for x2 mode showing Input 1, Input 2, Debounced signals, Direction, and Counter x2 values.

MSv75806V1

Figure 402. Encoder with built-in debouncer, x4 mode

Timing diagram for x4 mode showing Input 1, Input 2, Debounced signals, Direction, and Counter x4 values.

The diagram illustrates the x4 operating mode. It shows a sequence of transitions. Input 1 transitions from high to low while Input 2 is high, causing the Direction to flip from high to low and the Counter x4 to increment from 1 to 2. Then, Input 2 transitions from high to low while Input 1 is low, causing the Direction to flip from low to high and the Counter x4 to increment from 2 to 3. Finally, Input 1 transitions from low to high while Input 2 is high, causing the Direction to flip from high to low and the Counter x4 to decrement from 3 to 2. The 'Debounced' signals show the filtered versions of the inputs, and the 'Direction' signal indicates the current direction of counting.

Timing diagram for x4 mode showing Input 1, Input 2, Debounced signals, Direction, and Counter x4 values.

MSv75807V2

32.4.22 Direction bit output

It is possible to output a direction signal out of the timer, on the tim_oc3 and tim_oc4 output signals (copy of the DIR bit in the TIM_CR1 register). This is achieved by setting the OC3M[3:0] or the OC4M[3:0] bitfield to 1011 in the TIM_CCMR2 register.

This feature can be used for monitoring the counting direction (or rotation direction) in encoder mode, or to have a signal indicating the up/down phases in center-aligned PWM mode.

32.4.23 Clock drift measurement

It is possible to measure the drift between two clock sources using the directional clock encoder mode. This is useful for audio applications typically, when one need to compute the drift between two streams.

To do so, a signal representing the rate of a first audio stream must be connected to tim_ti1 and a signal representing the rate of a second audio stream must be connected to tim_ti2. The signals representing the audio rates are generally the SAIx_FS_A, SAIx_FS_B, SPIx_WS or spdifrx_frame_sync(this list of signals is provided as example and depends on the audio peripheral availability for a given product line, as detailed on the product datasheet).

In this case, the clocks are present on both tim_ti1 and tim2 simultaneously, as shown on Figure 403, and the counter value indicates the drift.

Let's define \(F_{tim\_ti1}\) and \(F_{tim\_ti2}\) the frequencies of the signals on tim_ti1 and tim_ti2. The counter value will change as following:

Figure 403. Clock drift measure using directional clock mode

Figure 403: Clock drift measure using directional clock mode. The diagram shows three waveforms over time: tim_ti1, tim_ti2, and Counter value. The time axis is divided into three regions: F_tim_ti1 > F_tim_ti2, F_tim_ti1 = F_tim_ti2, and F_tim_ti1 < F_tim_ti2. In the first region, tim_ti1 has a higher frequency than tim_ti2, and the counter value increases from 0x0000 to 0x0002. In the second region, the frequencies are equal, and the counter value dithers between 0x0000 and 0x0001. In the third region, tim_ti2 has a higher frequency, and the counter value decreases from 0x0000 to 0xFFFF and then to 0xFFFE.

The figure illustrates the clock drift measurement using directional clock mode. It shows three waveforms over time: tim_ti1, tim_ti2, and the Counter value. The time axis is divided into three regions based on the relative frequencies of the input signals:

The counter value is shown on a vertical axis with labels: 0x0002, 0x0001, 0x0000, 0xFFFF, and 0xFFFE. The counter value is 0x0000 at the start of the diagram and increases to 0x0002 in the first region. In the second region, it dithers between 0x0000 and 0x0001. In the third region, it decreases to 0xFFFF and then to 0xFFFE.

MSv67593V1

Figure 403: Clock drift measure using directional clock mode. The diagram shows three waveforms over time: tim_ti1, tim_ti2, and Counter value. The time axis is divided into three regions: F_tim_ti1 > F_tim_ti2, F_tim_ti1 = F_tim_ti2, and F_tim_ti1 < F_tim_ti2. In the first region, tim_ti1 has a higher frequency than tim_ti2, and the counter value increases from 0x0000 to 0x0002. In the second region, the frequencies are equal, and the counter value dithers between 0x0000 and 0x0001. In the third region, tim_ti2 has a higher frequency, and the counter value decreases from 0x0000 to 0xFFFF and then to 0xFFFE.

32.4.24 UIF bit remapping

The IUFREMAP bit in the TIM_CR1 register forces a continuous copy of the update interrupt flag (UIF) into bit 31 of the timer counter register's bit 31 (TIM_CNT[31]). This is used to atomically read both the counter value and a potential roll-over condition signaled by the UIFCPY flag. It eases the calculation of angular speed by avoiding race conditions caused, for instance, by a processing shared between a background task (counter reading) and an interrupt (update interrupt).

There is no latency between the UIF and UIFCPY flag assertions.

In 32-bit timer implementations, when the IUFREMAP bit is set, bit 31 of the counter is overwritten by the UIFCPY flag upon read access (the counter's most significant bit is only accessible in write mode).

32.4.25 Timer input XOR function

The TI1S bit in the TIM_CR2 register, allows the input filter of channel 1 to be connected to the output of an XOR gate, combining the three input pins tim_ti1, tim_ti2 and tim_ti3.

The XOR output can be used with all the timer input functions such as trigger or input capture.

An example of this feature used to interface Hall sensors is given in Section 31.3.34: Interfacing with Hall sensors.

It is convenient to measure the interval between edges on two input signals, as per Figure 404.

Figure 404. Measuring time interval between edges on three signals

Timing diagram showing three input signals (tim_ti1, tim_ti2, tim_ti3), their XOR combination, and the resulting TIMx Counter ramp. Vertical dashed lines mark the edges of the input signals, which are captured by the counter.

The diagram illustrates the measurement of time intervals between edges on three signals (tim_ti1, tim_ti2, and tim_ti3) using an XOR gate and a timer counter. The signals are shown as square waves. The XOR output is a square wave that is high whenever an odd number of inputs are high. The TIMx Counter is a ramp signal that increments on each rising edge of the XOR output. Vertical dashed lines indicate the timing of the edges on the input signals, showing how they are captured by the counter.

MSv75854V1

Timing diagram showing three input signals (tim_ti1, tim_ti2, tim_ti3), their XOR combination, and the resulting TIMx Counter ramp. Vertical dashed lines mark the edges of the input signals, which are captured by the counter.

The XORPS control bit selects the position of the XOR gate, as shown on the Figure 405 and Figure 406 below, when the TI1S bit is set.

Figure 405. XOR gate connection with TI1S = 1 and XORPS = 0

Block diagram of XOR gate connection with TI1S = 1 and XORPS = 0 showing inputs tim_ti1, tim_ti2, tim_ti3, and tim_ti4 connected to Filter blocks and logic controlled by TIM_CR2 (TI1INV, TI2INV, TI3INV) outputting tim_ti1f_ed, with filter outputs producing tim_ti1fp1/2 through tim_ti4fp1/2 and status bits TI1FS through TI4FS in TIM_SR.

MSv80079V1

Block diagram of XOR gate connection with TI1S = 1 and XORPS = 0 showing inputs tim_ti1, tim_ti2, tim_ti3, and tim_ti4 connected to Filter blocks and logic controlled by TIM_CR2 (TI1INV, TI2INV, TI3INV) outputting tim_ti1f_ed, with filter outputs producing tim_ti1fp1/2 through tim_ti4fp1/2 and status bits TI1FS through TI4FS in TIM_SR.

Figure 406. XOR gate connection with TI1S = 1 and XORPS = 1

Block diagram of XOR gate connection with TI1S = 1 and XORPS = 1 showing inputs tim_ti1, tim_ti2, tim_ti3, and tim_ti3 passing through Filters, with an Edge Detector after the first filter feeding into the XOR logic with TIM_CR2 bits TI1INV, TI2INV, TI3INV generating tim_ti1f_ed, and TIM_SR status bits TI1FS, TI2FS, TI3FS, TI4FS.

MSv80080V1

Block diagram of XOR gate connection with TI1S = 1 and XORPS = 1 showing inputs tim_ti1, tim_ti2, tim_ti3, and tim_ti3 passing through Filters, with an Edge Detector after the first filter feeding into the XOR logic with TIM_CR2 bits TI1INV, TI2INV, TI3INV generating tim_ti1f_ed, and TIM_SR status bits TI1FS, TI2FS, TI3FS, TI4FS.

Three control bits T1INV, T2INV and T3INV in the TIM_CR2 register can invert the tim_in1, tim_in2 and tim_in3 signals prior entering the XOR gate.

The status of the tim_in1, tim_in2, tim_in3 and tim_in4 signals can be software polled using the TI1FS, TI2FS, TI3FS and TI4FS bits in the TIM_SR register (tim_ti4 is not represented on figures above and do not enter into the XOR gate).

32.4.26 Timers and external trigger synchronization

The TIMx timers can be synchronized with an external trigger in several modes: reset mode, gated mode, trigger mode, reset + trigger and gated + reset modes.

Slave mode: reset mode

The counter and its prescaler can be reinitialized in response to an event on a trigger input. Moreover, if the URS bit from the TIM_CR1 register is low, an update event UEV is generated. Then all the preloaded registers (TIM_ARR, TIM_CCRx) are updated.

In the following example, the upcounter is cleared in response to a rising edge on tim_ti1 input:

  1. 1. Configure the channel 1 to detect rising edges on tim_ti1. Configure the input filter duration (in this example, we do not need any filter, so we keep IC1F = 0000). The capture prescaler is not used for triggering, so it does not need to be configured. The CC1S bits select the input capture source only, CC1S = 01 in the TIM_CCMR1 register. Write CC1P = 0 and CC1NP = 0 in TIM_CCER register to validate the polarity (and detect rising edges only).
  2. 2. Configure the timer in reset mode by writing SMS = 100 in TIM_SMCR register. Select tim_ti1 as the input source by writing TS = 00101 in TIM_SMCR register.
  3. 3. Start the counter by writing CEN = 1 in the TIM_CR1 register.

The counter starts counting on the internal clock, then behaves normally until tim_ti1 rising edge. When tim_ti1 rises, the counter is cleared and restarts from 0. In the meantime, the trigger flag is set (TIF bit in the TIM_SR register) and an interrupt request, or a DMA request can be sent if enabled (depending on the TIE and TDE bits in TIM_DIER register).

The following figure shows this behavior when the autoreload register TIM_ARR = 0x36. The delay between the rising edge on tim_ti1 and the actual reset of the counter is due to the resynchronization circuit on tim_ti1 input.

Figure 407. Control circuit in reset mode

Timing diagram for Figure 407 showing the control circuit in reset mode. The diagram includes five signals: tim_ti1 (input trigger), Counter reset and update (output status), tim_cnt_ck, tim_psc_ck (counter clock), Counter register (showing values 30-36 and 00-03), and TIF (trigger flag). The counter register shows a sequence of values: 30, 31, 32, 33, 34, 35, 36, 00, 01, 02, 03, 00, 01, 02, 03. The counter register is cleared to 00 when tim_ti1 rises. The TIF flag is set to high when tim_ti1 rises. The counter clock is a square wave. The counter reset and update signal is high when the counter is reset and updated.

MSv62361V1

Timing diagram for Figure 407 showing the control circuit in reset mode. The diagram includes five signals: tim_ti1 (input trigger), Counter reset and update (output status), tim_cnt_ck, tim_psc_ck (counter clock), Counter register (showing values 30-36 and 00-03), and TIF (trigger flag). The counter register shows a sequence of values: 30, 31, 32, 33, 34, 35, 36, 00, 01, 02, 03, 00, 01, 02, 03. The counter register is cleared to 00 when tim_ti1 rises. The TIF flag is set to high when tim_ti1 rises. The counter clock is a square wave. The counter reset and update signal is high when the counter is reset and updated.

Slave mode: gated mode

The counter can be enabled depending on the level of a selected input.

In the following example, the upcounter counts only when tim_ti1 input is low:

  1. 1. Configure the channel 1 to detect low levels on tim_ti1. Configure the input filter duration (in this example, we do not need any filter, so we keep IC1F = 0000). The
  1. capture prescaler is not used for triggering, so it does not need to be configured. The CC1S bits select the input capture source only, CC1S = 01 in TIM_CCMR1 register. Write CC1P = 1 and CC1NP = 0 in TIM_CCER register to validate the polarity (and detect low level only).
  2. Configure the timer in gated mode by writing SMS = 101 in TIM_SMCR register. Select tim_ti1 as the input source by writing TS = 00101 in TIM_SMCR register.
  3. Enable the counter by writing CEN = 1 in the TIM_CR1 register (in gated mode, the counter does not start if CEN = 0, whatever is the trigger input level).

The counter starts counting on the internal clock as long as tim_ti1 is low and stops as soon as tim_ti1 becomes high. The TIF flag in the TIM_SR register is set both when the counter starts or stops.

The delay between the rising edge on tim_ti1 and the actual stop of the counter is due to the resynchronization circuit on tim_ti1 input.

Figure 408. Control circuit in gated mode

Timing diagram for Figure 408: Control circuit in gated mode. The diagram shows five signals over time: tim_ti1, Counter enable, tim_cnt_ck, tim_psc_ck, Counter register, and TIF. tim_ti1 is a square wave that goes high and then low. Counter enable is a pulse that goes high when tim_ti1 goes low and low when tim_ti1 goes high. tim_cnt_ck and tim_psc_ck are square waves that are active (low) only when Counter enable is high. The Counter register shows bits 30-38, with bit 34 being the carry flag. TIF is a pulse that goes high when the counter starts or stops. Arrows indicate that writing TIF = 0 clears the flag.

MSv62362V1

Timing diagram for Figure 408: Control circuit in gated mode. The diagram shows five signals over time: tim_ti1, Counter enable, tim_cnt_ck, tim_psc_ck, Counter register, and TIF. tim_ti1 is a square wave that goes high and then low. Counter enable is a pulse that goes high when tim_ti1 goes low and low when tim_ti1 goes high. tim_cnt_ck and tim_psc_ck are square waves that are active (low) only when Counter enable is high. The Counter register shows bits 30-38, with bit 34 being the carry flag. TIF is a pulse that goes high when the counter starts or stops. Arrows indicate that writing TIF = 0 clears the flag.

Note: The configuration “CCxP = CCxNP = 1” (detection of both rising and falling edges) does not have any effect in gated mode because gated mode acts on a level and not on an edge.

Slave mode: trigger mode

The counter can start in response to an event on a selected input.

In the following example, the upcounter starts in response to a rising edge on tim_ti2 input:

  1. Configure the channel 2 to detect rising edges on tim_ti2. Configure the input filter duration (in this example, we do not need any filter, so we keep IC2F = 0000). The capture prescaler is not used for triggering, so it does not need to be configured. CC2S bits are selecting the input capture source only, CC2S = 01 in TIM_CCMR1 register. Write CC2P = 1 and CC2NP = 0 in TIM_CCER register to validate the polarity (and detect low level only).
  2. Configure the timer in trigger mode by writing SMS = 110 in TIM_SMCR register. Select tim_ti2 as the input source by writing TS = 00110 in TIM_SMCR register.

When a rising edge occurs on tim_ti2, the counter starts counting on the internal clock and the TIF flag is set.

The delay between the rising edge on tim_ti2 and the actual start of the counter is due to the resynchronization circuit on tim_ti2 input.

Figure 409. Control circuit in trigger mode

Timing diagram for Figure 409: Control circuit in trigger mode. The diagram shows five signals over time: tim_ti2, Counter enable, tim_cnt_ck, tim_psc_ck, Counter register, and TIF. A vertical dashed line marks a trigger event. tim_ti2 is a pulse that triggers the event. Counter enable goes high at the trigger. tim_cnt_ck and tim_psc_ck are clock signals that start after the trigger. The Counter register shows a value of 34 before the trigger and then increments through 35, 36, 37, and 38. TIF goes high after the counter reaches 38.

MSv62363V1

Timing diagram for Figure 409: Control circuit in trigger mode. The diagram shows five signals over time: tim_ti2, Counter enable, tim_cnt_ck, tim_psc_ck, Counter register, and TIF. A vertical dashed line marks a trigger event. tim_ti2 is a pulse that triggers the event. Counter enable goes high at the trigger. tim_cnt_ck and tim_psc_ck are clock signals that start after the trigger. The Counter register shows a value of 34 before the trigger and then increments through 35, 36, 37, and 38. TIF goes high after the counter reaches 38.

Slave mode selection preload for run-time encoder mode update

The SMS[4:0] bit can be preloaded. This is enabled by setting the SMSPE enable bit in the TIM_SMCR register. The trigger for the transfer from SMS[4:0] preload to active value is the update event (UEV) occurring when the counter overflows.

Slave mode – combined reset + trigger mode

In this case, a rising edge of the selected trigger input (tim_trgi) reinitializes the counter, generates an update of the registers, and starts the counter.

This mode is used for one-pulse mode.

Slave mode – combined gated + reset mode

The counter clock is enabled when the trigger input (tim_trgi) is high. The counter stops and is reset as soon as the trigger becomes low. Both start and stop of the counter are controlled.

This mode is used to detect out-of-range PWM signal (duty cycle exceeding a maximum expected value).

Slave mode – external clock mode 2 + trigger mode

The external clock mode 2 can be used in addition to another slave mode (except external clock mode 1 and encoder mode). In this case, the tim_etr_in signal is used as external clock input, and another input can be selected as trigger input when operating in reset mode, gated mode, or trigger mode. It is recommended not to select tim_etr_in as tim_trgi through the TS bits of TIM_SMCR register.

In the following example, the upcounter is incremented at each rising edge of the tim_etr_in signal as soon as a rising edge of tim_ti1 occurs:

  1. 1. Configure the external trigger input circuit by programming the TIM_SMCR register as follows:
    • – ETF = 0000: no filter.
    • – ETPS = 00: prescaler disabled.
    • – ETP = 0: detection of rising edges on tim_etr_in and ECE = 1 to enable the external clock mode 2.
  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 TIM_CCMR1 register to select only the input capture source.
    • – CC1P = 0 and CC1NP = 0 in TIM_CCER register to validate the polarity (and detect rising edge only).
  3. 3. Configure the timer in trigger mode by writing SMS = 110 in TIM_SMCR register. Select tim_ti1 as the input source by writing TS = 00101 in TIM_SMCR register.

A rising edge on tim_ti1 enables the counter and sets the TIF flag. The counter then counts on tim_etr_in rising edges.

The delay between the rising edge of the tim_etr_in signal and the actual reset of the counter is due to the resynchronization circuit on tim_etrp input.

Figure 410. Control circuit in external clock mode 2 + trigger mode

Timing diagram for Figure 410 showing the control circuit in external clock mode 2 + trigger mode. The diagram includes six signals: tim_ti1, Counter enable, ETR, tim_cnt_ck, tim_psc_ck, Counter register, and TIF. tim_ti1 is a pulse that triggers the Counter enable. Counter enable is a pulse that enables the counter. ETR is a periodic square wave. tim_cnt_ck and tim_psc_ck are clock signals. Counter register shows values 34, 35, and 36. TIF is a flag that is set on the rising edge of tim_ti1.

The diagram illustrates the timing relationship between various signals in external clock mode 2 + trigger mode. The signals shown are:

MSv62364V1

Timing diagram for Figure 410 showing the control circuit in external clock mode 2 + trigger mode. The diagram includes six signals: tim_ti1, Counter enable, ETR, tim_cnt_ck, tim_psc_ck, Counter register, and TIF. tim_ti1 is a pulse that triggers the Counter enable. Counter enable is a pulse that enables the counter. ETR is a periodic square wave. tim_cnt_ck and tim_psc_ck are clock signals. Counter register shows values 34, 35, and 36. TIF is a flag that is set on the rising edge of tim_ti1.

32.4.27 Timer synchronization

The TIMx timers are linked together internally for timer synchronization or chaining. When one timer is configured in master mode, it can reset, start, stop, or clock the counter of another timer configured in slave mode.

Figure 411 and Figure 412 show examples of master/slave timer connections.

Figure 411. Master/Slave timer example

Figure 411: Master/Slave timer example diagram. The diagram shows two timer blocks: TIM_mstr (Master) and TIM_slv (Slave). In TIM_mstr, a Clock input goes to a Prescaler, which feeds a Counter. The Counter output goes to a Master mode control block. The Master mode control block has an MMS input and a UEV input. Its output is tim_trgo. In TIM_slv, the tim_trgo signal from TIM_mstr goes to an Input trigger selection block. This block has a TS input and its output is tim_itr. The tim_itr signal goes to a Slave mode control block, which has an SMS input. The Slave mode control block output is CK_PSC, which goes to a Prescaler, which feeds a Counter.

MSv62375V1

Figure 411: Master/Slave timer example diagram. The diagram shows two timer blocks: TIM_mstr (Master) and TIM_slv (Slave). In TIM_mstr, a Clock input goes to a Prescaler, which feeds a Counter. The Counter output goes to a Master mode control block. The Master mode control block has an MMS input and a UEV input. Its output is tim_trgo. In TIM_slv, the tim_trgo signal from TIM_mstr goes to an Input trigger selection block. This block has a TS input and its output is tim_itr. The tim_itr signal goes to a Slave mode control block, which has an SMS input. The Slave mode control block output is CK_PSC, which goes to a Prescaler, which feeds a Counter.

Figure 412. Master/slave connection example with 1 channel only timers

Figure 412: Master/slave connection example with 1 channel only timers diagram. The diagram shows two timer blocks: TIM_mstr (Master) and TIM_slv (Slave). In TIM_mstr, a Clock input goes to a Prescaler, which feeds a Counter. The Counter output goes to a Compare 1 block. The Compare 1 block output goes to an Output control block. The Output control block has a TIM_CH1 output and its main output is tim_oc1. In TIM_slv, the tim_oc1 signal from TIM_mstr goes to an Input trigger selection block. This block has a TS input and its output is tim_itr. The tim_itr signal goes to a Slave mode control block, which has an SMS input. The Slave mode control block output is CK_PSC, which goes to a Prescaler, which feeds a Counter.

MSv65225V1

Figure 412: Master/slave connection example with 1 channel only timers diagram. The diagram shows two timer blocks: TIM_mstr (Master) and TIM_slv (Slave). In TIM_mstr, a Clock input goes to a Prescaler, which feeds a Counter. The Counter output goes to a Compare 1 block. The Compare 1 block output goes to an Output control block. The Output control block has a TIM_CH1 output and its main output is tim_oc1. In TIM_slv, the tim_oc1 signal from TIM_mstr goes to an Input trigger selection block. This block has a TS input and its output is tim_itr. The tim_itr signal goes to a Slave mode control block, which has an SMS input. The Slave mode control block output is CK_PSC, which goes to a Prescaler, which feeds a Counter.

Note: The timers with one channel only (see Figure 412) do not feature a master mode. However, the tim_oc1 output signal can serve as trigger for slave timer (see TIMx internal trigger connection table in Section 32.4.2: TIM2/TIM3/TIM4/TIM5 pins and internal signals). The tim_oc1 signal pulse width must be programmed to be at least two clock cycles of the destination timer, to make sure the slave timer detects the trigger. For instance, if the destination timer tim_ker_ck clock is four times slower than the source timer, the OC1 pulse width must be eight clock cycles.

Using one timer as prescaler for another timer

For example, TIM_mstr can be configured to act as a prescaler for TIM_slv. Refer to Figure 411. To do this:

  1. 1. Configure TIM_mstr in master mode so that it outputs a periodic trigger signal on each update event UEV. If MMS = 010 is written in the TIM_mstr_CR2 register, a rising edge is output on tim_trgo each time an update event is generated.
  2. 2. To connect the tim_trgo output of TIM_mstr to TIM_slv, TIM_slv must be configured in slave mode using ITR2 as internal trigger. This is selected through the TS bits in the TIM_slv_SMCR register (writing TS = 00010).
  3. 3. Then the slave mode controller must be put in external clock mode 1 (write SMS = 111 in the TIM_slv_SMCR register). This causes TIM_slv to be clocked by the rising edge

of the periodic TIM_mstr trigger signal (which correspond to the TIM_mstr counter overflow).

  1. Finally both timers must be enabled by setting their respective CEN bits (TIM_CR1 register).

Note: If tim_ocx is selected on TIM_mstr as the trigger output (MMS = 1xx), its rising edge is used to clock the counter of TIM_slv.

Using one timer to enable another timer

In this example, we control the enable of TIM_slv with the output compare 1 of TIM_mstr. Refer to Figure 411 for connections. TIM_slv counts on the divided internal clock only when tim_oc1ref of TIM_mstr is high. Both counter clock frequencies are divided by 3 by the prescaler compared to tim_ker_ck (\(f_{tim\_cnt\_ck} = f_{tim\_ker\_ck}/3\)).

  1. Configure TIM_mstr master mode to send its output compare 1 reference (tim_oc1ref) signal as trigger output (MMS = 100 in the TIM_mstr_CR2 register).
  2. Configure the TIM_mstr tim_oc1ref waveform (TIM_mstr_CCMR1 register).
  3. Configure TIM_slv to get the input trigger from TIM_mstr (TS = 00010 in the TIM_slv_SMCR register).
  4. Configure TIM_slv in gated mode (SMS = 101 in TIM_slv_SMCR register).
  5. Enable TIM_slv by writing 1 in the CEN bit (TIM_slv_CR1 register).
  6. Start TIM_mstr by writing 1 in the CEN bit (TIM_mstr_CR1 register).

Note: The slave timer counter clock is not synchronized with the master timer counter clock, this mode only affects the TIM_slv counter enable signal.

Figure 413. Gating TIM_slv with tim_oc1ref of TIM_mstr

Timing diagram showing the relationship between tim_ker_ck, TIM_mst_oc1ref, tim_mstr_CNT, tim_slv_CNT, and tim_slv TIF bit. The diagram illustrates how the slave timer (TIM_slv) is gated by the master timer's output compare 1 reference signal (TIM_mst_oc1ref).

The diagram shows the following signals over time:

MSv62376V1

Timing diagram showing the relationship between tim_ker_ck, TIM_mst_oc1ref, tim_mstr_CNT, tim_slv_CNT, and tim_slv TIF bit. The diagram illustrates how the slave timer (TIM_slv) is gated by the master timer's output compare 1 reference signal (TIM_mst_oc1ref).

In the example in Figure 413, the TIM_slv 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 TIM_mstr. Then any value can be written in the timer counters. The timers can easily be reset by software using the UG bit in the TIM_EGR registers.

In the next example (refer to Figure 414), we synchronize TIM_mstr and TIM_slv. TIM_mstr is the master and starts from 0. TIM_slv is the slave and starts from 0xE7. The prescaler ratio is the same for both timers. TIM_slv stops when TIM_mstr is disabled by writing 0 to the CEN bit in the TIM_mstr_CR1 register:

  1. 1. Configure TIM_mstr master mode to send its output compare 1 reference (tim_oc1ref) signal as trigger output (MMS = 100 in the TIM_mstr_CR2 register).
  2. 2. Configure the TIM_mstr tim_oc1ref waveform (TIM_mstr_CCMR1 register).
  3. 3. Configure TIM_slv to get the input trigger from TIM_mstr (TS = 00010 in the TIM_slv_SMCR register).
  4. 4. Configure TIM_slv in gated mode (SMS = 101 in TIM_slv_SMCR register).
  5. 5. Reset TIM_mstr by writing 1 in UG bit (TIM_mstr_EGR register).
  6. 6. Reset TIM_slv by writing 1 in UG bit (TIM_slv_EGR register).
  7. 7. Initialize TIM_slv to 0xE7 by writing 0xE7 in the TIM_slv counter (TIM_slv_CNT).
  8. 8. Enable TIM_slv by writing 1 in the CEN bit (TIM_slv_CR1 register).
  9. 9. Start TIM_mstr by writing 1 in the CEN bit (TIM_mstr_CR1 register).
  10. 10. Stop TIM_mstr by writing 0 in the CEN bit (TIM_mstr_CR1 register).

Figure 414. Gating TIM_slv with Enable of TIM_mstr

Timing diagram for Figure 414 showing the gating of TIM_slv with the enable of TIM_mstr. The diagram includes signals for tim_ker_ck, TIM_mstr counter enable (CEN bit), tim_mstr_CNT reset, tim_mstr_CNT, tim_slv_CNT, tim_slv_CNT reset, tim_slv_CNT write, and tim_slv TIF bit. The CEN bit for TIM_mstr is high during the first two intervals and low during the last two. The CEN bit for TIM_slv is high during the first two intervals and low during the last two. The tim_mstr_CNT values are 75, 00, 01, 02. The tim_slv_CNT values are AB, 00, E7, E8, E9. The tim_slv_CNT write signal is high during the third interval. The tim_slv TIF bit is high during the third interval and low during the fourth interval. An arrow points to the transition of the tim_slv TIF bit with the label 'Write TIF = 0'. The diagram is labeled MSv62377V1.
Timing diagram for Figure 414 showing the gating of TIM_slv with the enable of TIM_mstr. The diagram includes signals for tim_ker_ck, TIM_mstr counter enable (CEN bit), tim_mstr_CNT reset, tim_mstr_CNT, tim_slv_CNT, tim_slv_CNT reset, tim_slv_CNT write, and tim_slv TIF bit. The CEN bit for TIM_mstr is high during the first two intervals and low during the last two. The CEN bit for TIM_slv is high during the first two intervals and low during the last two. The tim_mstr_CNT values are 75, 00, 01, 02. The tim_slv_CNT values are AB, 00, E7, E8, E9. The tim_slv_CNT write signal is high during the third interval. The tim_slv TIF bit is high during the third interval and low during the fourth interval. An arrow points to the transition of the tim_slv TIF bit with the label 'Write TIF = 0'. The diagram is labeled MSv62377V1.

Using one timer to start another timer

In this example, we set the enable of TIM_slv with the update event of TIM_mstr. Refer to Figure 411 for connections. TIM_slv starts counting from its current value (which can be nonzero) on the divided internal clock as soon as the update event is generated by TIM_mstr. When TIM_slv receives the trigger signal its CEN bit is automatically set and the counter counts until we write 0 to the CEN bit in the TIM_slv_CR1 register. Both counter clock frequencies are divided by 3 by the prescaler compared to tim_ker_ck (\(f_{tim\_cnt\_ck} = f_{tim\_ker\_ck}/3\)).

  1. 1. Configure TIM_mstr master mode to send its update event (UEV) as trigger output (MMS = 010 in the TIM_mstr_CR2 register).
  2. 2. Configure the TIM_mstr period (TIM_mstr_ARR registers).
  3. 3. Configure TIM_slv to get the input trigger from TIM_mstr (TS = 00010 in the TIM_slv_SMCR register).
  4. 4. Configure TIM_slv in trigger mode (SMS = 110 in TIM_slv_SMCR register).
  5. 5. Start TIM_mstr by writing 1 in the CEN bit (TIM_mstr_CR1 register).
Figure 415. Triggering TIM_slv with update of TIM_mstr Timing diagram for Figure 415 showing the triggering of TIM_slv with the update of TIM_mstr. The diagram includes signals for tim_ker_ck, tim_mstr UEV event, tim_mst_CNT, tim_slv_CNT, TIM_slv counter enable (CEN bit), and tim_slv TIF bit. The tim_mst_CNT counts FD, FE, FF, then 00, 01, 02. The tim_slv_CNT holds 45 then counts 46, 47, 48 upon the UEV event. TIM_slv counter enable (CEN bit) transitions high. The tim_slv TIF bit is asserted high and cleared via Write TIF = 0.
Timing diagram for Figure 415 showing the triggering of TIM_slv with the update of TIM_mstr. The diagram includes signals for tim_ker_ck, tim_mstr UEV event, tim_mst_CNT, tim_slv_CNT, TIM_slv counter enable (CEN bit), and tim_slv TIF bit. The tim_mst_CNT counts FD, FE, FF, then 00, 01, 02. The tim_slv_CNT holds 45 then counts 46, 47, 48 upon the UEV event. TIM_slv counter enable (CEN bit) transitions high. The tim_slv TIF bit is asserted high and cleared via Write TIF = 0.

As in the previous example, both counters can be initialized before starting counting.

Figure 416 shows the behavior with the same configuration as in Figure 415 but in trigger mode (SMS = 110 in the TIM_slv_SMCR register) instead of gated mode.

Figure 416. Triggering TIM_slv with Enable of TIM_mstr Timing diagram for Figure 416 showing the triggering of TIM_slv with Enable of TIM_mstr. Signals shown: tim_ker_ck, TIM_mst counter enable (CEN bit), tim_mstr_CNT reset, tim_mstr_CNT (values 75, 00, 01, 02), tim_slv_CNT (values CD, 00, E7, E8, E9, EA), tim_slv_CNT reset, Tim_slv_CNT write, and tim_slv TIF bit. TIM_mst counter enable goes high, triggering counter starts and tim_slv TIF bit assertion, followed by Write TIF = 0.
Timing diagram for Figure 416 showing the triggering of TIM_slv with Enable of TIM_mstr. Signals shown: tim_ker_ck, TIM_mst counter enable (CEN bit), tim_mstr_CNT reset, tim_mstr_CNT (values 75, 00, 01, 02), tim_slv_CNT (values CD, 00, E7, E8, E9, EA), tim_slv_CNT reset, Tim_slv_CNT write, and tim_slv TIF bit. TIM_mst counter enable goes high, triggering counter starts and tim_slv TIF bit assertion, followed by Write TIF = 0.

Starting two timers synchronously in response to an external trigger

In this example, we set the enable of TIM_mstr when its tim_ti1 input rises, and the enable of TIM_slv with the enable of TIM_mstr. Refer to Figure 411 for connections. To ensure the counters are aligned, TIM_mstr must be configured in master/slave mode (slave with respect to tim_ti1, master with respect to TIM_slv):

  1. 1. Configure TIM_mstr master mode to send its enable as trigger output (MMS = 001 in the TIM_mstr_CR2 register).
  2. 2. Configure TIM_mstr slave mode to get the input trigger from tim_ti1 (TS = 00100 in the TIM_mstr_SMCR register).
  3. 3. Configure TIM_mstr in trigger mode (SMS = 110 in the TIM_mstr_SMCR register).
  4. 4. Configure the TIM_mstr in master/slave mode by writing MSM = 1 (TIM_mstr_SMCR register).
  5. 5. Configure TIM_slv to get the input trigger from TIM_mstr (TS = 00000 in the TIM_slv_SMCR register).
  6. 6. Configure TIM_slv in trigger mode (SMS = 110 in the TIM_slv_SMCR register).

When a rising edge occurs on tim_ti1 (TIM_mstr), both counters start 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 an offset can easily be inserted between them by writing any of the counter registers (TIM_CNT). One can see that the master/slave mode inserts a delay between CNT_EN and CK_PSC on TIM_mstr.

Figure 417. Triggering TIM_mstr and TIM_slv with TIM_mstr tim_ti1 input

Timing diagram showing the triggering of TIM_mstr and TIM_slv with TIM_mstr tim_ti1 input. The diagram includes signals for tim_ker_ck, tim_mstr_ti1, TIM_mst counter enable (CEN bit), tim_mstr_psc_ck, tim_mstr_CNT, tim_mstr TIF bit, TIM_slv counter enable (CEN bit), tim_slv_psc_ck, tim_slv_CNT, and tim_slv TIF bit. The counters start at 00 and increment synchronously after the trigger event.

The timing diagram illustrates the synchronization of two timers, TIM_mstr and TIM_slv, triggered by an external signal tim_mstr_ti1. The diagram shows the following signals over time:

The diagram shows that both timers start counting synchronously after the tim_mstr_ti1 trigger, with a slight delay between the CEN bits of the two timers.

Timing diagram showing the triggering of TIM_mstr and TIM_slv with TIM_mstr tim_ti1 input. The diagram includes signals for tim_ker_ck, tim_mstr_ti1, TIM_mst counter enable (CEN bit), tim_mstr_psc_ck, tim_mstr_CNT, tim_mstr TIF bit, TIM_slv counter enable (CEN bit), tim_slv_psc_ck, tim_slv_CNT, and tim_slv TIF bit. The counters start at 00 and increment synchronously after the trigger event.

Note: The clock of the slave peripherals (such as timer, ADC) receiving the tim_trgo signal must be enabled prior to receive events from the master timer, and the clock frequency (prescaler) must not be changed on-the-fly while triggers are received from the master timer.

32.4.28 ADC triggers

The timer can generate an ADC triggering event with various internal signals, such as reset, enable or compare events.

Note: The clock of the slave peripherals (such as timer, ADC) receiving the tim_trgo signal must be enabled prior to receive events from the master timer, and the clock frequency (prescaler) must not be changed on-the-fly while triggers are received from the master timer.

32.4.29 ADC synchronization

The timer operation can be synchronized to the ADC clock to trigger jitter-free ADC sampling. This function is enabled using the ADSYNC bit in the TIM_CR2 register.

This feature is useful when the timers and the ADCs are operating with semisynchronous clocks (clocks derived from a same source with integer ratio tim_ker_ck/ad_ker_ck), for instance adc_ker_ck = 75 MHz and tim_ker_ck = 150 MHz or 300 MHz.

ADSYNC must also be set when both peripherals are operating at the same frequency from the same clock source, when jitter-free operation is needed.

ADSYNC must not be set and jitter-free operation is not supported in the following cases:

When ADSYNC = 1, the timer operation is slightly changed: the counter enable and counter reset events are aligned to the adc_ker_ck ADC clock, to avoid any phase shift due to clocks enable in the RCC.

Jitter-free operation is guaranteed only when one of the two requirements below is met (depending on the selected trigger source):

  1. 1. The counter period must be a multiple of the ADC clock period.

\[(TIM\_PSC + 1) \times (TIM\_ARR + 1) \times T_{tim\_ker\_ck} = n \times T_{adc\_ker\_ck}\]

  1. 2. The compare value must be a multiple of the ADC clock period.

\[(TIM\_PSC + 1) \times TIM\_CMPy \times T_{tim\_ker\_ck} = m \times T_{adc\_ker\_ck}\]

Note: If none of the two above requirements is met, the trigger is still generated, but the latency is not constant and varies with the timer and ADC clocks phase shift.

Programming guidelines

The ADC synchronization feature must not be modified during run-time, once the counter is enabled and once the ADC has been configured for receiving triggers from the timer.

It is mandatory to follow the procedure below to use the ADC synchronization:

  1. 1. Enable the destination ADC clock.
  2. 2. Configure the timer and set the ADSYNC bit.
  3. 3. Configure the ADC and enable it (using ADSTART and/or JADSTART bits).
  4. 4. Start the timer (with the CEN counter enable bit).

32.4.30 DMA burst mode

The TIMx timers have the capability to generate multiple DMA requests upon a single event. The main purpose is to be able to reprogram part of the timer multiple times without software overhead, but it can also be used to read several registers in a row, at regular intervals.

The DMA controller destination is unique and must point to the virtual register TIM_DMAR. On a given timer event, the timer launches a sequence of DMA requests (burst). Each write into the TIM_DMAR register is actually redirected to one of the timer registers.

The DBL[4:0] bits in the TIM_DCR register set the DMA burst length. The timer recognizes a burst transfer when a read or a write access is done to the TIM_DMAR address), i.e. the number of transfers (either in half-words or in bytes).

The DBA[4:0] bits in the TIM_DCR registers define the DMA base address for DMA transfers (when read/write accesses are done through the TIM_DMAR address). DBA is defined as an offset starting from the address of the TIM_CR1 register:

Example:

00000: TIM_CR1

00001: TIM_CR2

00010: TIM_SMCR

The DBSS[3:0] bits in the TIM_DCR register defines the interrupt source that triggers the DMA burst transfers (see Section 32.5.23: TIM DMA control register (TIM_DCR) for details).

As an example, the timer DMA burst feature is used to update the contents of the CCRx registers (x = 2, 3, 4) upon an update event, with the DMA transferring half words into the CCRx registers.

This is done in the following steps:

  1. 1. Configure the corresponding DMA channel as follows:
    • – DMA channel peripheral address is the DMAR register address.
    • – DMA channel memory address is the address of the buffer in the RAM containing the data to be transferred by DMA into CCRx registers.
    • – Number of data to transfer = 3 (See note below).
    • – Circular mode disabled.
  2. 2. Configure the DCR register by configuring the DBA and DBL bitfields as follows: DBL = 3, DBA = 0xE and DBSS = 1.
  3. 3. Enable the TIMx update DMA request (set the UDE bit in the DIER register).
  4. 4. Enable TIMx.
  5. 5. Enable the DMA channel.

This example is for the case where every CCRx register has to be updated once. If every CCRx register is to be updated twice for example, the number of data to transfer must be 6. Let's take the example of a buffer in the RAM containing data1, data2, data3, data4, data5, and data6. The data is transferred to the CCRx registers as follows: on the first update DMA request, data1 is transferred to CCR2, data2 is transferred to CCR3, data3 is transferred to CCR4 and on the second update DMA request, data4 is transferred to CCR2, data5 is transferred to CCR3, and data6 is transferred to CCR4.

Note: A null value can be written to the reserved registers.

32.4.31 TIM2/TIM3/TIM4/TIM5 DMA requests

The TIM2/TIM3/TIM4/TIM5 can generate a DMA requests, as shown in Table 323.

Table 323. DMA request

DMA request signalDMA requestEnable control bit
tim_upd_dmaUpdateUDE
tim_cc1_dmaCapture/compare 1CC1DE
tim_cc2_dmaCapture/compare 2CC2DE
tim_cc3_dmaCapture/compare 3CC3DE
tim_cc4_dmaCapture/compare 4CC4DE
tim_trgi_dmaTriggerTDE

Note: Some timer's DMA requests may not be connected to the DMA controller. Refer to the DMA section(s) for more details.

32.4.32 Debug mode

When the microcontroller enters debug mode (Cortex-M33 core halted), the TIMx counter can either continue to work normally or stops.

The behavior in debug mode can be programmed with a dedicated configuration bit per timer in the Debug support (DBG) module.

For more details, refer to section Debug support (DBG).

32.4.33 TIM2/TIM3/TIM4/TIM5 low-power modes

Table 324. Effect of low-power modes on TIM2/TIM3/TIM4/TIM5

ModeDescription
SleepNo effect, peripheral is active. The interrupts can cause the device to exit from Sleep mode.
StopThe timer operation is stopped and the register content is kept. No interrupt can be generated.
StandbyThe timer is powered-down and must be reinitialized after exiting the Standby mode.

32.4.34 TIM2/TIM3/TIM4/TIM5 interrupts

The TIM2/TIM3/TIM4/TIM5 can generate multiple interrupts, as shown in Table 325.

Table 325. Interrupt requests

Interrupt acronymInterrupt eventEvent flagEnable control bitInterrupt clear methodExit from Sleep modeExit from Stop and Standby mode
TIM_UPDUpdateUIFUIEwrite 0 in UIFYesNo
TIM_CCCapture/compare 1CC1IFCC1IEwrite 0 in CC1IFYesNo
Capture/compare 2CC2IFCC2IEwrite 0 in CC2IFYesNo
Capture/compare 3CC3IFCC3IEwrite 0 in CC3IFYesNo
Capture/compare 4CC4IFCC4IEwrite 0 in CC4IFYesNo
TIM_TRGITriggerTIFTIEwrite 0 in TIFYesNo
TIM_DIR
_IDX
IndexIDXFIDXIEwrite 0 in IDXFYesNo
DirectionDIRFDIRIEwrite 0 in DIRFYesNo
TIM_IERRIndex ErrorIERRFIERRIEwrite 0 in IERRFYesNo
TIM_TERRTransition ErrorTERRFTERRIEwrite 0 in TERRFYesNo

32.5 TIM2/TIM3/TIM4/TIM5 registers

Refer to Section 1.2 for a list of abbreviations used in register descriptions.

The peripheral registers can be accessed by half-words (16-bit) or words (32-bit).

32.5.1 TIM control register 1 (TIM_CR1)

Address offset: 0x000

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.CKD2[3:0]Res.Res.Res.Res.Res.Res.Res.Res.
rwrwrwrw
1514131211109876543210
Res.Res.Res.DITH ENUIFRE MAPRes.CKD[1:0]ARPECMS[1:0]DIROPMURSUDISCEN
rwrwrwrwrwrwrwrwrwrwrwrw

Bits 31:28 Reserved, must be kept at reset value.

Bits 27:24 CKD2[3:0]: Clock division 2

This bit field indicates the division between sampling clock (tDTS) used by the dead-time generators and the digital filters (tim_etr_in, tim_tix).

0000: No Division

0001: tDTS2 = 4*tDTS

0010: tDTS2 = 16*tDTS

0011: tDTS2 = 64*tDTS

0100: tDTS2 = 256*tDTS

0101: tDTS2 = 1024*tDTS

0110: tDTS2 = 4096*tDTS

0111: tDTS2 = 16384*tDTS

1000: tDTS2 = 65536*tDTS

1001: tDTS2 = 262144*tDTS

Others: Reserved, do not program this value

Bits 23:13 Reserved, must be kept at reset value.

Bit 12 DITHEN: Dithering Enable

0: Dithering disabled

1: Dithering enabled

Note: The DITHEN bit can only be modified when CEN bit is reset.

Bit 11 UIFREMAP: UIF status bit remapping

0: No remapping. UIF status bit is not copied to TIM_CNT register bit 31.

1: Remapping enabled. UIF status bit is copied to TIM_CNT register bit 31.

Bit 10 Reserved, must be kept at reset value.

Bits 9:8 CKD[1:0]: Clock division

This bitfield indicates the division ratio between the timer clock (tim_ker_ck) frequency and sampling clock used by the digital filters (tim_etr_in, tim_tix),

00: \(t_{DTS} = t_{tim\_ker\_ck}\)

01: \(t_{DTS} = 2 \times t_{tim\_ker\_ck}\)

10: \(t_{DTS} = 4 \times t_{tim\_ker\_ck}\)

11: \(t_{DTS} = 8 \times t_{tim\_ker\_ck}\)

Bit 7 ARPE: Autoreload preload enable

0: TIM_ARR register is not buffered

1: TIM_ARR register is buffered

Bits 6:5 CMS[1:0]: Center-aligned mode selection

00: Edge-aligned mode. The counter counts up or down depending on the direction bit (DIR).

01: Center-aligned mode 1. The counter counts up and down alternatively. Output compare interrupt flags of channels configured in output (CCxS = 00 in TIM_CCMRx register) are set only when the counter is counting down.

10: Center-aligned mode 2. The counter counts up and down alternatively. Output compare interrupt flags of channels configured in output (CCxS = 00 in TIM_CCMRx register) are set only when the counter is counting up.

11: Center-aligned mode 3. The counter counts up and down alternatively. Output compare interrupt flags of channels configured in output (CCxS = 00 in TIM_CCMRx register) are set both when the counter is counting up or down.

Note: It is not allowed to switch from edge-aligned mode to center-aligned mode as long as the counter is enabled (CEN = 1)

Bit 4 DIR: Direction

0: Counter used as upcounter

1: Counter used as downcounter

Note: This bit is read only when the timer is configured in center-aligned mode or encoder mode.

Bit 3 OPM: One-pulse mode

0: Counter is not stopped at update event

1: Counter stops counting at the next update event (clearing the bit CEN)

Bit 2 URS: Update request source

This bit is set and cleared by software to select the UEV event sources.

0: Any of the following events generate an update interrupt or DMA request if enabled.

These events can be:

1: Only counter overflow/underflow generates an update interrupt or DMA request if enabled.

Bit 1 UDIS: Update disable

This bit is set and cleared by software to enable/disable UEV event generation.

0: UEV enabled. The Update (UEV) event is generated by one of the following events:

Buffered registers are then loaded with their preload values.

1: UEV disabled. The Update event is not generated, shadow registers keep their value (ARR, PSC, CCRx). However the counter and the prescaler are reinitialized if the UG bit is set or if a hardware reset is received from the slave mode controller.

Bit 0 CEN: Counter enable

0: Counter disabled

1: Counter enabled

Note: External clock, gated mode and encoder mode can work only if the CEN bit has been previously set by software. However trigger mode can set the CEN bit automatically by hardware.

CEN is cleared automatically in one-pulse mode, when an update event occurs.

32.5.2 TIM control register 2 (TIM_CR2)

Address offset: 0x004

Reset value: 0x0000 0000

31302928272625242322212019181716
TI3INVTI2INVTI1INVAD
SYNC
XORPSRes.MMS[3]Res.Res.Res.Res.Res.Res.Res.Res.Res.
rwrwrwrwrwrw
1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.TI1SMMS[2:0]CCDSRes.Res.Res.
rwrwrwrwrw

Bits 25, 6, 5, 4 MMS[3:0]: Master mode selection

These bits are used to select the information to be sent in master mode to slave timers for synchronization (tim_trgo). The combination is as follows:

0000: Reset - the UG bit from the TIM_EGR register is used as trigger output (tim_trgo). If the reset is generated by the trigger input (slave mode controller configured in reset mode) then the signal on tim_trgo is delayed compared to the actual reset.

0001: Enable - the Counter enable signal, CNT_EN, is used as trigger output (tim_trgo). It is useful to start several timers at the same time or to control a window in which a slave timer is enabled. The Counter Enable signal is generated by a logic AND between CEN control bit and the trigger input when configured in gated mode. When the Counter Enable signal is controlled by the trigger input, there is a delay on tim_trgo, except if the master/slave mode is selected (see the MSM bit description in TIM_SMCR register).

0010: Update - The update event is selected as trigger output (tim_trgo). For instance a master timer can then be used as a prescaler for a slave timer.

0011: Compare pulse - The trigger output send a positive pulse when the CC1IF flag is to be set (even if it was already high), as soon as a capture or a compare match occurred (tim_trgo).

0100: Compare - tim_oc1refc signal is used as trigger output (tim_trgo)

0101: Compare - tim_oc2refc signal is used as trigger output (tim_trgo)

0110: Compare - tim_oc3refc signal is used as trigger output (tim_trgo)

0111: Compare - tim_oc4refc signal is used as trigger output (tim_trgo)

1000: Encoder clock output - The encoder clock signal is used as trigger output (tim_trgo). This code is valid for the following SMS[4:0] values: 00001, 00010, 00011, 01010, 01011, 01100, 01101, 01110, 01111. Any other SMS[4:0] code is not allowed and may lead to unexpected behavior.

Others: Reserved

Note: The clock of the slave timer or ADC must be enabled prior to receive events from the master timer, and must not be changed on-the-fly while triggers are received from the master timer.

Bit 3 CCDS: Capture/compare DMA selection

0: CCx DMA request sent when CCx event occurs

1: CCx DMA requests sent when update event occurs

Bits 2:0 Reserved, must be kept at reset value.

32.5.3 TIM slave mode control register (TIM_SMCR)

Address offset: 0x008

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.SETPS[3:0]SMSPSSMSPERes.Res.TS[4:3]Res.Res.SMS[4:3]
rwrwrwrwrwrwrwrwrwrw
1514131211109876543210
ETPECEETPS[1:0]ETF[3:0]MSMTS[2:0]OCCSSMS[2:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 31:30 Reserved, must be kept at reset value.

Bits 29:26 SETPS[3:0]: Synchronous external trigger prescaler

The linear synchronous prescaler divides the frequency of the tim_etr signal, by a factor (SETPS[3:0] + 1).

Bit 25 SMSPS: SMS preload source

This bit selects whether the events that triggers the SMS[4:0] bitfield transfer from preload to active

0: The transfer is triggered by the Timer's Update event

1: The transfer is triggered by the Index event

Bit 24 SMSPE: SMS preload enable

This bit selects whether the SMS[4:0] bitfield is preloaded

0: SMS[4:0] bitfield is not preloaded

1: SMS[4:0] preload is enabled

Bits 23:22 Reserved, must be kept at reset value.

Bits 19:18 Reserved, must be kept at reset value.

Bit 15 ETP: External trigger polarity

This bit selects whether tim_etr_in or tim_etr_in is used for trigger operations

0: tim_etr_in is non-inverted, active at high level or rising edge

1: tim_etr_in is inverted, active at low level or falling edge

Bit 14 ECE: External clock enable

This bit enables external clock mode 2.

0: External clock mode 2 disabled

1: External clock mode 2 enabled. The counter is clocked by any active edge on the tim_etr signal.

Note: Setting the ECE bit has the same effect as selecting external clock mode 1 with tim_trgi connected to tim_etr (SMS = 111 and TS = 00111).

It is possible to simultaneously use external clock mode 2 with the following slave modes: reset mode, gated mode and trigger mode. Nevertheless, tim_trgi must not be connected to tim_etr in this case (TS bits must not be 00111).

If external clock mode 1 and external clock mode 2 are enabled at the same time, the external clock input is tim_etr.

Bits 13:12 ETPS[1:0]: External trigger prescaler

External trigger signal tim_etrp frequency must be at most 1/4 of tim_ker_ck frequency. A prescaler can be enabled to reduce tim_etrp frequency. It is useful when inputting fast external clocks on tim_etr_in.

00: Prescaler OFF

01: tim_etrp frequency divided by 2

10: tim_etrp frequency divided by 4

11: tim_etrp frequency divided by 8

Bits 11:8 ETF[3:0]: External trigger filter

This bitfield then defines the frequency used to sample tim_etrp signal and the length of the digital filter applied to tim_etrp. The digital filter is made of an event counter in which N consecutive events are needed to validate a transition on the output:

0000: No filter, sampling is done at \(f_{DTS}\)

0001: \(f_{SAMPLING} = f_{tim\_ker\_ck}\), N = 2

0010: \(f_{SAMPLING} = f_{tim\_ker\_ck}\), N = 4

0011: \(f_{SAMPLING} = f_{tim\_ker\_ck}\), N = 8

0100: \(f_{SAMPLING} = f_{DTS}/2\), N = 6

0101: \(f_{SAMPLING} = f_{DTS}/2\), N = 8

0110: \(f_{SAMPLING} = f_{DTS}/4\), N = 6

0111: \(f_{SAMPLING} = f_{DTS}/4\), N = 8

1000: \(f_{SAMPLING} = f_{DTS}/8\), N = 6

1001: \(f_{SAMPLING} = f_{DTS}/8\), N = 8

1010: \(f_{SAMPLING} = f_{DTS}/16\), N = 5

1011: \(f_{SAMPLING} = f_{DTS}/16\), N = 6

1100: \(f_{SAMPLING} = f_{DTS}/16\), N = 8

1101: \(f_{SAMPLING} = f_{DTS}/32\), N = 5

1110: \(f_{SAMPLING} = f_{DTS}/32\), N = 6

1111: \(f_{SAMPLING} = f_{DTS}/32\), N = 8

Bit 7 MSM: Master/slave mode

0: No action

1: The effect of an event on the trigger input (tim_trgi) is delayed to allow a perfect synchronization between the current timer and its slaves (through tim_trgo). It is useful if we want to synchronize several timers on a single external event.

Bits 21, 20, 6, 5, 4 TS[4:0]: Trigger selection

This bitfield selects the trigger input to be used to synchronize the counter.

00000: Internal trigger 0 (tim_itr0)
00001: Internal trigger 1 (tim_itr1)
00010: Internal trigger 2 (tim_itr2)
00011: Internal trigger 3 (tim_itr3)
00100: tim_ti1 edge detector (tim_ti1f_ed)
00101: Filtered timer input 1 (tim_ti1fp1)
00110: Filtered timer input 2 (tim_ti2fp2)
00111: External trigger input (tim_etr)
01000: Internal trigger 4 (tim_itr4)
01001: Internal trigger 5 (tim_itr5)
01010: Internal trigger 6 (tim_itr6)
01011: Internal trigger 7 (tim_itr7)
01100: Internal trigger 8 (tim_itr8)
01101: Internal trigger 9 (tim_itr9)
01110: Internal trigger 10 (tim_itr10)
01111: Internal trigger 11 (tim_itr11)
10000: Internal trigger 12 (tim_itr12)
10001: Internal trigger 13 (tim_itr13)
10010: Internal trigger 14 (tim_itr14)
10011: Internal trigger 15 (tim_itr15)
10100: Internal trigger 16 (tim_itr16)
10101: Internal trigger 17 (tim_itr17)
10110: Internal trigger 18 (tim_itr18)
10111: Internal trigger 19 (tim_itr19)
11000: Internal trigger 20 (tim_itr20)
11001: Internal trigger 21 (tim_itr21)
11010: Internal trigger 22 (tim_itr22)
11011: Internal trigger 23 (tim_itr23)
11100: Internal trigger 24 (tim_itr24)
11101: Internal trigger 25 (tim_itr25)
11110: Internal trigger 26 (tim_itr26)
11111: Internal trigger 27 (tim_itr27)

See Section 32.4.2: TIM2/TIM3/TIM4/TIM5 pins and internal signals for product specific implementation details.

Note: These bits must be changed only when they are not used (for example when SMS = 000) to avoid wrong edge detections at the transition.

Bit 3 OCCS: OCREF clear selection

This bit is used to select the OCREF clear source

0: tim_ocref_clr_int is connected to the tim_ocref_clr input
1: tim_ocref_clr_int is connected to tim_etr

Note: If the OCREF clear selection feature is not supported, this bit is reserved and forced by hardware to 0. Section 32.3: TIM2/TIM3/TIM4/TIM5 implementation.

Bits 17:16, 2:0 SMS[4:0]: Slave mode selection

When external signals are selected the active edge of the trigger signal (tim_trgi) is linked to the polarity selected on the external input (refer to ETP bit in TIM_SMCR for tim_etr_in and CCxP/CCxNP bits in TIM_CCER register for tim_ti1fp1 and tim_ti2fp2).

00000: Slave mode disabled - if CEN = 1 then the prescaler is clocked directly by the internal clock.

00001: Encoder mode 1 - Counter counts up/down on tim_ti1fp1 edge depending on tim_ti2fp2 level.

00010: Encoder mode 2 - Counter counts up/down on tim_ti2fp2 edge depending on tim_ti1fp1 level.

00011: Encoder mode 3 - Counter counts up/down on both tim_ti1fp1 and tim_ti2fp2 edges depending on the level of the other input.

00100: Reset mode - Rising edge of the selected trigger input (tim_trgi) reinitializes the counter and generates an update of the registers.

00101: Gated mode - The counter clock is enabled when the trigger input (tim_trgi) is high. The counter stops (but is not reset) as soon as the trigger becomes low. Both start and stop of the counter are controlled.

00110: Trigger mode - The counter starts at a rising edge of the trigger tim_trgi (but it is not reset). Only the start of the counter is controlled.

00111: External clock mode 1 - Rising edges of the selected trigger (tim_trgi) clock the counter.

01000: Combined reset + trigger mode - Rising edge of the selected trigger input (tim_trgi) reinitializes the counter, generates an update of the registers and starts the counter.

01001: Combined gated + reset mode - The counter clock is enabled when the trigger input (tim_trgi) is high. The counter stops and is reset as soon as the trigger becomes low. Both start and stop of the counter are controlled.

01010: Encoder mode: Clock plus direction, x2 mode.

01011: Encoder mode: Clock plus direction, x1 mode, tim_ti2fp2 edge sensitivity is set by CC2P.

01100: Encoder mode: Directional clock, x2 mode.

01101: Encoder mode: Directional clock, x1 mode, tim_ti1fp1 and tim_ti2fp2 edge sensitivity is set by CC1P and CC2P.

01110: Quadrature encoder mode: x1 mode, counting on tim_ti1fp1 edges only, edge sensitivity is set by CC1P.

01111: Quadrature encoder mode: x1 mode, counting on tim_ti2fp2 edges only, edge sensitivity is set by CC2P.

10000: Quadrature encoder with built-in debouncer: x2 mode.

10001: Quadrature encoder with built-in debouncer: x4 mode.

Note: The gated mode must not be used if tim_ti1f_ed is selected as the trigger input (TS = 00100). Indeed, tim_ti1f_ed outputs 1 pulse for each transition on tim_ti1f, whereas the gated mode checks the level of the trigger signal.

Note: The clock of the slave peripherals (such as timer, ADC) receiving the tim_trgo signal must be enabled prior to receive events from the master timer, and the clock frequency (prescaler) must not be changed on-the-fly while triggers are received from the master timer.

32.5.4 TIM DMA/interrupt enable register (TIM_DIER)

Address offset: 0x00C

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.TERR
IE
IERR
IE
DIRIEIDXIERes.Res.Res.Res.
rwrwrwrw
1514131211109876543210
Res.TDERes.CC4DECC3DECC2DECC1DEUDERes.TIERes.CC4IECC3IECC2IECC1IEUIE
rwrwrwrwrwrwrwrwrwrwrwrw

Bits 31:24 Reserved, must be kept at reset value.

Bit 23 TERRIE: Transition error interrupt enable

0: Transition error interrupt disabled

1: Transition error interrupt enabled

Bit 22 IERRIE: Index error interrupt enable

0: Index error interrupt disabled

1: Index error interrupt enabled

Bit 21 DIRIE: Direction change interrupt enable

0: Direction change interrupt disabled

1: Direction change interrupt enabled

Bit 20 IDXIE: Index interrupt enable

0: Index interrupt disabled

1: Index interrupt enabled

Bits 19:15 Reserved, must be kept at reset value.

Bit 14 TDE: Trigger DMA request enable

0: Trigger DMA request disabled.

1: Trigger DMA request enabled.

Bit 13 Reserved, must be kept at reset value.

Bit 12 CC4DE: Capture/compare 4 DMA request enable

0: CC4 DMA request disabled.

1: CC4 DMA request enabled.

Bit 11 CC3DE: Capture/compare 3 DMA request enable

0: CC3 DMA request disabled.

1: CC3 DMA request enabled.

Bit 10 CC2DE: Capture/compare 2 DMA request enable

0: CC2 DMA request disabled.

1: CC2 DMA request enabled.

Bit 9 CC1DE: Capture/compare 1 DMA request enable

0: CC1 DMA request disabled.

1: CC1 DMA request enabled.

Bit 8 UDE: Update DMA request enable

0: Update DMA request disabled.

1: Update DMA request enabled.

Bit 7 Reserved, must be kept at reset value.

Bit 6 TIE: Trigger interrupt enable
0: Trigger interrupt disabled.
1: Trigger interrupt enabled.

Bit 5 Reserved, must be kept at reset value.

Bit 4 CC4IE: Capture/compare 4 interrupt enable
0: CC4 interrupt disabled.
1: CC4 interrupt enabled.

Bit 3 CC3IE: Capture/compare 3 interrupt enable
0: CC3 interrupt disabled.
1: CC3 interrupt enabled.

Bit 2 CC2IE: Capture/compare 2 interrupt enable
0: CC2 interrupt disabled.
1: CC2 interrupt enabled.

Bit 1 CC1IE: Capture/compare 1 interrupt enable
0: CC1 interrupt disabled.
1: CC1 interrupt enabled.

Bit 0 UIE: Update interrupt enable
0: Update interrupt disabled.
1: Update interrupt enabled.

32.5.5 TIM status register (TIM_SR)

Address offset: 0x010

Reset value: 0x0000 0000

31302928272625242322212019181716
TI4FSTI3FSTI2FSTI1FSRes.Res.Res.UIOVR
F
TERRFIERRFDIRFIDXFRes.Res.Res.Res.
rrrrrc_w0rc_w0rc_w0rc_w0rc_w0
1514131211109876543210
Res.Res.Res.CC4OFCC3OFCC2OFCC1OFRes.Res.TIFRes.CC4IFCC3IFCC2IFCC1IFUIF
rc_w0rc_w0rc_w0rc_w0rc_w0rc_w0rc_w0rc_w0rc_w0rc_w0

Bit 2 CC2IF: Capture/compare 2 interrupt flag

Refer to CC1IF description

Bit 1 CC1IF: Capture/compare 1 interrupt flag

This flag is set by hardware. It is cleared by software (input capture or output compare mode) or by reading the TIM_CCR1 register (input capture mode only).

0: No compare match / No input capture occurred

1: A compare match or an input capture occurred

If channel CC1 is configured as output: this flag is set when the content of the counter TIM_CNT matches the content of the TIM_CCR1 register. When the content of TIM_CCR1 is greater than the content of TIM_ARR, the CC1IF bit goes high on the counter overflow (in up-counting and up/down-counting modes) or underflow (in down-counting mode). There are three possible options for flag setting in center-aligned mode, refer to the CMS bits in the TIM_CR1 register for the full description.

If channel CC1 is configured as input: this bit is set when counter value has been captured in TIM_CCR1 register (an edge has been detected on IC1, as per the edge sensitivity defined with the CC1P and CC1NP bits setting, in TIM_CCER).

Bit 0 UIF: Update interrupt flag

This bit is set by hardware on an update event. It is cleared by software.

0: No update occurred

1: Update interrupt pending. This bit is set by hardware when the registers are updated:

At overflow or underflow and if UDIS = 0 in the TIM_CR1 register.

When CNT is reinitialized by software using the UG bit in TIM_EGR register, if URS = 0 and UDIS = 0 in the TIM_CR1 register.

When CNT is reinitialized by a trigger event (refer to the synchro control register description), if URS = 0 and UDIS = 0 in the TIM_CR1 register.

32.5.6 TIM event generation register (TIM_EGR)

Address offset: 0x014

Reset value: 0x0000

1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.Res.TGRes.CC4GCC3GCC2GCC1GUG
wwwwww

Bits 15:7 Reserved, must be kept at reset value.

Bit 6 TG: Trigger generation

This bit is set by software in order to generate an event, it is automatically cleared by hardware.

0: No action

1: The TIF flag is set in TIM_SR register. Related interrupt or DMA transfer can occur if enabled.

Bit 5 Reserved, must be kept at reset value.

Bit 4 CC4G: Capture/compare 4 generation

Refer to CC1G description

Bit 3 CC3G: Capture/compare 3 generation

Refer to CC1G description

Bit 2 CC2G: Capture/compare 2 generation

Refer to CC1G description

Bit 1 CC1G: Capture/compare 1 generation

This bit is set by software in order to generate an event, it is automatically cleared by hardware.

0: No action

1: A capture/compare event is generated on channel 1:

If channel CC1 is configured as output:

CC1IF flag is set, Corresponding interrupt or DMA request is sent if enabled.

If channel CC1 is configured as input:

The current value of the counter is captured in TIM_CCR1 register. The CC1IF flag is set, the corresponding interrupt or DMA request is sent if enabled. The CC1OF flag is set if the CC1IF flag was already high.

Bit 0 UG: Update generation

This bit can be set by software, it is automatically cleared by hardware.

0: No action

1: Re-initialize the counter and generates an update of the registers. Note that the prescaler counter is cleared too (anyway the prescaler ratio is not affected). The counter is cleared if the center-aligned mode is selected or if DIR = 0 (up-counting), else it takes the autoreload value (TIM_ARR) if DIR = 1 (down-counting).

32.5.7 TIM capture/compare mode register 1 (TIM_CCMR1)

Address offset: 0x018

Reset value: 0x0000 0000

The same register can be used for input capture mode (this section) or for output compare mode (next section). The direction of a channel is defined by configuring the corresponding CCxS bits. All the other bits of this register have a different function for input capture and for output compare modes. It is possible to combine both modes independently (for example channel 1 in input capture mode and channel 2 in output compare mode).

RM0522 32.5.7column_2Bit 2 Bit 1 Bit 0CC2G CC1G UGhardware. 0: No action 0: No actioncolumn_6column_7column_8::column_11column_12column_13column_14column_15column_16
31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
IC2F[3:0]IC2PSC[1:0]CC2S[1:0]IC1F[3:0]IC1PSC[1:0]CC1S[1:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
Bits 15:12IC2F[3:0]: Input capture 2 filter
Bits 11:10IC2PSC[1:0]:Input capture 2 prescaler

Input capture mode

Bits 31:16 Reserved, must be kept at reset value.

Bits 15:12 IC2F[3:0]: Input capture 2 filter

Bits 11:10 IC2PSC[1:0]: Input capture 2 prescaler

Bits 9:8 CC2S[1:0]: Capture/compare 2 selection

This bitfield defines the direction of the channel (input/output) as well as the used input.

00: CC2 channel is configured as output.

01: CC2 channel is configured as input, tim_ic2 is mapped on tim_ti2.

10: CC2 channel is configured as input, tim_ic2 is mapped on tim_ti1.

11: CC2 channel is configured as input, tim_ic2 is mapped on tim_trc. This mode is working only if an internal trigger input is selected through TS bit (TIM_SMCR register)

Note: CC2S bits are writable only when the channel is OFF (CC2E = 0 in TIM_CCER).

Bits 7:4 IC1F[3:0]: Input capture 1 filter

This bitfield defines the frequency used to sample tim_ti1 input and the length of the digital filter applied to tim_ti1. The digital filter is made of an event counter in which N consecutive events are needed to validate a transition on the output:

0000: No filter, sampling is done at \(f_{DTS}\)

0001: \(f_{SAMPLING} = f_{tim\_ker\_ck}\), N = 2

0010: \(f_{SAMPLING} = f_{tim\_ker\_ck}\), N = 4

0011: \(f_{SAMPLING} = f_{tim\_ker\_ck}\), N = 8

0100: \(f_{SAMPLING} = f_{DTS}/2\), N = 6

0101: \(f_{SAMPLING} = f_{DTS}/2\), N = 8

0110: \(f_{SAMPLING} = f_{DTS}/4\), N = 6

0111: \(f_{SAMPLING} = f_{DTS}/4\), N = 8

1000: \(f_{SAMPLING} = f_{DTS}/8\), N = 6

1001: \(f_{SAMPLING} = f_{DTS}/8\), N = 8

1010: \(f_{SAMPLING} = f_{DTS}/16\), N = 5

1011: \(f_{SAMPLING} = f_{DTS}/16\), N = 6

1100: \(f_{SAMPLING} = f_{DTS}/16\), N = 8

1101: \(f_{SAMPLING} = f_{DTS}/32\), N = 5

1110: \(f_{SAMPLING} = f_{DTS}/32\), N = 6

1111: \(f_{SAMPLING} = f_{DTS}/32\), N = 8

Bits 3:2 IC1PSC[1:0]: Input capture 1 prescaler

This bitfield defines the ratio of the prescaler acting on CC1 input (tim_ic1). The prescaler is reset as soon as CC1E = 0 (TIM_CCER register).

00: no prescaler, capture is done each time an edge is detected on the capture input

01: capture is done once every 2 events

10: capture is done once every 4 events

11: capture is done once every 8 events

Bits 1:0 CC1S[1:0]: Capture/compare 1 selection

This bitfield defines the direction of the channel (input/output) as well as the used input.

00: CC1 channel is configured as output

01: CC1 channel is configured as input, tim_ic1 is mapped on tim_ti1

10: CC1 channel is configured as input, tim_ic1 is mapped on tim_ti2

11: CC1 channel is configured as input, tim_ic1 is mapped on tim_trc. This mode is working only if an internal trigger input is selected through TS bit (TIM_SMCR register)

Note: CC1S bits are writable only when the channel is OFF (CC1E = 0 in TIM_CCER).

32.5.8 TIM capture/compare mode register 1 [alternate] (TIM_CCMR1)

Address offset: 0x018

Reset value: 0x0000 0000

The same register can be used for output compare mode (this section) or for input capture mode (previous section). The direction of a channel is defined by configuring the corresponding CCxS bits. All the other bits of this register have a different function for input capture and for output compare modes. It is possible to combine both modes independently (for example channel 1 in input capture mode and channel 2 in output compare mode).

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.OC2M[4:3]Res.Res.Res.Res.Res.Res.OC1M[4:3]
rwrwrwrw
1514131211109876543210
OC2CEOC2M[2:0]OC2PEOC2FECC2S[1:0]OC1CEOC1M[2:0]OC1PEOC1FECC1S[1:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Output compare mode

Bits 31:26 Reserved, must be kept at reset value.

Bits 23:18 Reserved, must be kept at reset value.

Bit 15 OC2CE: Output compare 2 clear enable

0: tim_oc2ref is not affected by the tim_ocref_clr_int signal

1: tim_oc2ref is cleared as soon as a high level is detected on tim_ocref_clr_int signal (tim_ocref_clr input or tim_etrif input)

Note: This mode is not available for all PWM modes. Refer to Section 32.4.16: Clearing the tim_ocxref signal on an external event for the list of forbidden configurations.

Bits 25:24, 14:12 OC2M[4:0]: Output compare 2 mode

Refer to OC1M description on bits 17:16, 6:4.

Bit 11 OC2PE: Output compare 2 preload enable

Bit 10 OC2FE: Output compare 2 fast enable

Bits 9:8 CC2S[1:0]: Capture/compare 2 selection

This bitfield defines the direction of the channel (input/output) as well as the used input.

00: CC2 channel is configured as output

01: CC2 channel is configured as input, tim_ic2 is mapped on tim_ti2

10: CC2 channel is configured as input, tim_ic2 is mapped on tim_ti1

11: CC2 channel is configured as input, tim_ic2 is mapped on tim_trc. This mode is working only if an internal trigger input is selected through the TS bit (TIM_SMCR register)

Note: CC2S bits are writable only when the channel is OFF (CC2E = 0 in TIM_CCER).

Bit 7 OC1CE: Output compare 1 clear enable

0: tim_oc1ref is not affected by the tim_ocref_clr_int input

1: tim_oc1ref is cleared as soon as a High level is detected on tim_ocref_clr_int input

Note: This mode is not available for all PWM modes. Refer to Section 32.4.16: Clearing the tim_ocxref signal on an external event for the list of forbidden configurations.

Bits 17:16, 6:4 OC1M[4:0]: Output compare 1 mode

These bits define the behavior of the output reference signal tim_oc1ref from which tim_oc1 is derived. tim_oc1ref is active high whereas tim_oc1 active level depends on CC1P bit.

00000: Frozen - The comparison between the output compare register TIM_CCR1 and the counter TIM_CNT has no effect on the outputs. This mode can be used when the timer serves as a software timebase. When the frozen mode is enabled during timer operation, the output keeps the state (active or inactive) it had before entering the frozen state.

00001: Set channel 1 to active level on match. tim_oc1ref signal is forced high when the counter TIM_CNT matches the capture/compare register 1 (TIM_CCR1).

00010: Set channel 1 to inactive level on match. tim_oc1ref signal is forced low when the counter TIM_CNT matches the capture/compare register 1 (TIM_CCR1).

00011: Toggle - tim_oc1ref toggles when TIM_CNT= TIM_CCR1.

00100: Force inactive level - tim_oc1ref is forced low.

00101: Force active level - tim_oc1ref is forced high.

00110: PWM mode 1 - In up-counting, channel 1 is active as long as TIM_CNT< TIM_CCR1 else inactive. In down-counting, channel 1 is inactive (tim_oc1ref= 0) as long as TIM_CNT> TIM_CCR1 else active (tim_oc1ref= 1).

00111: PWM mode 2 - In up-counting, channel 1 is inactive as long as TIM_CNT< TIM_CCR1 else active. In down-counting, channel 1 is active as long as TIM_CNT> TIM_CCR1 else inactive.

01000: Retriggerable OPM mode 1 - In up-counting mode, the channel is active until a trigger event is detected (on tim_trgi signal). Then, a comparison is performed as in PWM mode 1 and the channel becomes inactive again at the next update. In down-counting mode, the channel is inactive until a trigger event is detected (on tim_trgi signal). Then, a comparison is performed as in PWM mode 1 and the channel becomes inactive again at the next update.

01001: Retriggerable OPM mode 2 - In up-counting mode, the channel is inactive until a trigger event is detected (on tim_trgi signal). Then, a comparison is performed as in PWM mode 2 and the channel becomes inactive again at the next update. In down-counting mode, the channel is active until a trigger event is detected (on tim_trgi signal). Then, a comparison is performed as in PWM mode 1 and the channel becomes active again at the next update.

01010: Reserved.

01011: Reserved.

01100-01101: Combined PWM mode 1 and 2 - Refer to Table 320: Combined PWM mode overview for details.

01110-01111: Asymmetric PWM mode 1 and 2 - Refer to Table 319: Asymmetric PWM modes for details.

Others: Reserved.

Note: In PWM mode, the OCREF level changes when the result of the comparison changes, when the output compare mode switches from “frozen” mode to “PWM” mode and when the output compare mode switches from “force active/inactive” mode to “PWM” mode.

Bit 3 OC1PE: Output compare 1 preload enable

0: Preload register on TIM_CCR1 disabled. TIM_CCR1 can be written at anytime, the new value is taken in account immediately.
1: Preload register on TIM_CCR1 enabled. Read/Write operations access the preload register. TIM_CCR1 preload value is loaded in the active register at each update event.

Bit 2 OC1FE: Output compare 1 fast enable

This bit decreases the latency between a trigger event and a transition on the timer output. It must be used in one-pulse mode (OPM bit set in TIM_CR1 register), to have the output pulse starting as soon as possible after the starting trigger.
0: CC1 behaves normally depending on counter and CCR1 values even when the trigger is ON. The minimum delay to activate CC1 output when an edge occurs on the trigger input is 5 clock cycles.
1: An active edge on the trigger input acts like a compare match on CC1 output. Then, OC is set to the compare level independently from the result of the comparison. Delay to sample the trigger input and to activate CC1 output is reduced to three clock cycles. OCFE acts only if the channel is configured in PWM1 or PWM2 mode.

Bits 1:0 CC1S[1:0]: Capture/compare 1 selection

This bitfield defines the direction of the channel (input/output) as well as the used input.
00: CC1 channel is configured as output.
01: CC1 channel is configured as input, tim_ic1 is mapped on tim_ti1.
10: CC1 channel is configured as input, tim_ic1 is mapped on tim_ti2.
11: CC1 channel is configured as input, tim_ic1 is mapped on tim_trc. This mode is working only if an internal trigger input is selected through TS bit (TIM_SMCR register)

Note: CC1S bits are writable only when the channel is OFF (CC1E = 0 in TIM_CCER).

32.5.9 TIM capture/compare mode register 2 (TIM_CCMR2)

Address offset: 0x01C

Reset value: 0x0000 0000

The same register can be used for input capture mode (this section) or for output compare mode (next section). The direction of a channel is defined by configuring the corresponding CCxS bits. All the other bits of this register have a different function for input capture and for output compare modes. It is possible to combine both modes independently (for example channel 1 in input capture mode and channel 2 in output compare mode).

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
IC4F[3:0]IC4PSC[1:0]CC4S[1:0]IC3F[3:0]IC3PSC[1:0]CC3S[1:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Input capture mode

Bits 31:16 Reserved, must be kept at reset value.

Bits 15:12 IC4F[3:0]: Input capture 4 filter

Bits 11:10 IC4PSC[1:0]: Input capture 4 prescaler

ST logo
ST logo

Bits 9:8 CC4S[1:0]: Capture/compare 4 selection

This bitfield defines the direction of the channel (input/output) as well as the used input.

00: CC4 channel is configured as output

01: CC4 channel is configured as input, tim_ic4 is mapped on tim_ti4

10: CC4 channel is configured as input, tim_ic4 is mapped on tim_ti3

11: CC4 channel is configured as input, tim_ic4 is mapped on tim_trc. This mode is working only if an internal trigger input is selected through TS bit (TIM_SMCR register)

Note: CC4S bits are writable only when the channel is OFF (CC4E = 0 in TIM_CCER).

Bits 7:4 IC3F[3:0]: Input capture 3 filter

Bits 3:2 IC3PSC[1:0]: Input capture 3 prescaler

Bits 1:0 CC3S[1:0]: Capture/compare 3 selection

This bitfield defines the direction of the channel (input/output) as well as the used input.

00: CC3 channel is configured as output

01: CC3 channel is configured as input, tim_ic3 is mapped on tim_ti3

10: CC3 channel is configured as input, tim_ic3 is mapped on tim_ti4

11: CC3 channel is configured as input, tim_ic3 is mapped on tim_trc. This mode is working only if an internal trigger input is selected through TS bit (TIM_SMCR register)

Note: CC3S bits are writable only when the channel is OFF (CC3E = 0 in TIM_CCER).

32.5.10 TIM capture/compare mode register 2 [alternate] (TIM_CCMR2)

Address offset: 0x01C

Reset value: 0x0000 0000

The same register can be used for output compare mode (this section) or for input capture mode (previous section). The direction of a channel is defined by configuring the corresponding CCxS bits. All the other bits of this register have a different function for input capture and for output compare modes. It is possible to combine both modes independently (for example channel 1 in input capture mode and channel 2 in output compare mode).

column_1column_2Bits 9:8 Bits 7:4 Bits 3:2 Bits 1:0CC4S[1:0] IC3F[3:0] CC3S[1:0]IC3PSC[1:0]column_6column_7column_8column_9column_10column_11column_12column_13column_14column_15RM0522
31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.OC4M[4:3]Res.Res.Res.Res.Res.Res.OC3M[4:3]
rwrwrwrw
1514131211109876543210
OC4CEOC4M[2:0]OC4PE OC4FECC4S[1:0]OC3CEOC3M[2:0]OC3PE OC3FECC3S[1:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
Bit 15OC4CE: Output compare 4 clear enable
Bits 25:24, 14:12OC4M[4:0]: Output compare 4 mode

Output compare mode

Bits 31:26 Reserved, must be kept at reset value.

Bits 23:18 Reserved, must be kept at reset value.

Bit 15 OC4CE: Output compare 4 clear enable

0: tim_oc4ref is not affected by the tim_ocref_clr_int signal

1: tim_oc4ref is cleared as soon as a high level is detected on tim_ocref_clr_int signal (tim_ocref_clr input or tim_etr input)

Note: This mode is not available for all PWM modes. Refer to Section 32.4.16: Clearing the tim_ocxref signal on an external event for the list of forbidden configurations.

Bits 25:24, 14:12 OC4M[4:0]: Output compare 4 mode

Refer to OC3M[4:0].

Bit 11 OC4PE: Output compare 4 preload enable

Bit 10 OC4FE: Output compare 4 fast enable

Bits 9:8 CC4S[1:0]: Capture/compare 4 selection

This bitfield defines the direction of the channel (input/output) as well as the used input.

00: CC4 channel is configured as output

01: CC4 channel is configured as input, tim_ic4 is mapped on tim_ti4

10: CC4 channel is configured as input, tim_ic4 is mapped on tim_ti3

11: CC4 channel is configured as input, tim_ic4 is mapped on tim_trc. This mode is working only if an internal trigger input is selected through TS bit (TIM_SMCR register)

Note: CC4S bits are writable only when the channel is OFF (CC4E = 0 in TIM_CCER).

Bit 7 OC3CE: Output compare 3 clear enable

0: tim_oc3ref is not affected by the tim_ocref_clr_int signal

1: tim_oc3ref is cleared as soon as a high level is detected on tim_ocref_clr_int signal (tim_ocref_clr input or tim_etrif input)

Note: This mode is not available for all PWM modes. Refer to Section 32.4.16: Clearing the tim_ocxref signal on an external event for the list of forbidden configurations.

Bits 17:16, 6:4 OC3M[4:0]: Output compare 3 mode

These bits define the behavior of the output reference signal tim_oc3ref from which tim_oc3 and tim_oc3n are derived. tim_oc3ref is active high whereas tim_oc3 and tim_oc3n active level depends on CC3P and CC3NP bits.

Others: Reserved

Note: These bits can not be modified as long as LOCK level 3 has been programmed (LOCK bits in TIM_BDTR register) and CC1S = 00 (the channel is configured in output).

Note: In PWM mode, the OCREF level changes only when the result of the comparison changes or when the output compare mode switches from “frozen” mode to “PWM” mode.

On channels having a complementary output, this bitfield is preloaded. If the CCPC bit is set in the TIM_CR2 register then the OC3M active bits take the new value from the preloaded bits only when a COM event is generated.

32.5.11 TIM capture/compare enable register (TIM_CCER)

Address offset: 0x020

Reset value: 0x0000

1514131211109876543210
CC4NPRes.CC4PCC4ECC3NPRes.CC3PCC3ECC2NPRes.CC2PCC2ECC1NPRes.CC1PCC1E
rwrwrwrwrwrwrwrwrwrwrwrw

Bit 3 CC1NP: Capture/compare 1 output Polarity.

CC1 channel configured as output: CC1NP must be kept cleared in this case.

CC1 channel configured as input: This bit is used in conjunction with CC1P to define tim_ti1fp1/tim_ti2fp1 polarity. refer to CC1P description.

Bit 2 Reserved, must be kept at reset value.

Bit 1 CC1P: Capture/compare 1 output Polarity.

0: OC1 active high (output mode) / Edge sensitivity selection (input mode, see below)

1: OC1 active low (output mode) / Edge sensitivity selection (input mode, see below)

When CC1 channel is configured as input, both CC1NP/CC1P bits select the active polarity of TI1FP1 and TI2FP1 for trigger or capture operations.

CC1NP = 0, CC1P = 0:non-inverted/rising edge. The circuit is sensitive to TIxFP1 rising edge (capture or trigger operations in reset, external clock or trigger mode), TIxFP1 is not inverted (trigger operation in gated mode or encoder mode).

CC1NP = 0, CC1P = 1:inverted/falling edge. The circuit is sensitive to TIxFP1 falling edge (capture or trigger operations in reset, external clock or trigger mode), TIxFP1 is inverted (trigger operation in gated mode or encoder mode).

CC1NP = 1, CC1P = 1:non-inverted/both edges. The circuit is sensitive to both TIxFP1 rising and falling edges (capture or trigger operations in reset, external clock or trigger mode), TIxFP1 is not inverted (trigger operation in gated mode). This configuration must not be used in encoder mode.

CC1NP = 1, CC1P = 0:this configuration is reserved, it must not be used.

Bit 0 CC1E: Capture/compare 1 output enable.

0: Capture mode disabled / OC1 is not active

1: Capture mode enabled / OC1 signal is output on the corresponding output pin

Table 326. Output control bit for standard tim_ocx channels

CCxE bittim_ocx output state
0Output disabled (not driven by the timer: Hi-Z)
1Output enabled (tim_ocx = tim_ocxref + Polarity)

Note: The state of the external IO pins connected to the standard tim_ocx channels depends only on the GPIO registers when CCxE = 0.

32.5.12 TIM counter (TIM_CNT)

Address offset: 0x024

Reset value: 0x0000 0000

31302928272625242322212019181716
UIF
CPY_
CNT
[31]
CNT[30:16]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
CNT[15:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bit 31 UIFCPY_CNT[31]: Value depends on UIFREMAP in TIM_CR1.

If UIFREMAP = 0

CNT[31]: Most significant bit of counter value

If UIFREMAP = 1

UIFCPY: UIF Copy

This bit is a read-only copy of the UIF bit of the TIM_ISR register

Bits 30:0 CNT[30:0]: Least significant part of counter value

Non-dithering mode (DITHEN = 0)

The register holds the counter value.

Dithering mode (DITHEN = 1)

The register holds the non-dithered part in CNT[30:0]. The fractional part is not available.

32.5.13 TIM prescaler (TIM_PSC)

Address offset: 0x028

Reset value: 0x0000

1514131211109876543210
PSC[15:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 15:0 PSC[15:0]: Prescaler value

The counter clock frequency tim_cnt_ck is equal to \(f_{tim\_psc\_ck} / (PSC[15:0] + 1)\).

PSC contains the value to be loaded in the active prescaler register at each update event (including when the counter is cleared through UG bit of TIM_EGR register or through trigger controller when configured in “reset mode”).

32.5.14 TIM autoreload register (TIM_ARR)

Address offset: 0x02C

Reset value: 0xFFFF FFFF

31302928272625242322212019181716
ARR[31:16]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
ARR[15:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 31:0 ARR[31:0]: Autoreload value

ARR is the value to be loaded in the actual autoreload register.

Refer to the Section 32.4.3: Time-base unit for more details about ARR update and behavior.

The counter is blocked while the autoreload value is null.

Non-dithering mode (DITHER = 0)

The register holds the autoreload value.

Dithering mode (DITHER = 1)

The register holds the integer part in ARR[31:4]. The ARR[3:0] bitfield contains the dithered part.

32.5.15 TIM capture/compare register 1 (TIM_CCR1)

Address offset: 0x034

Reset value: 0x0000 0000

31302928272625242322212019181716
CCR1[31:16]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
CCR1[15:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 31:0 CCR1[31:0]: Capture/compare 1 value

If channel CC1 is configured as output:

CCR1 is the value to be loaded in the actual capture/compare 1 register (preload value). It is loaded permanently if the preload feature is not selected in the TIM_CCMR1 register (bit OC1PE). Else the preload value is copied in the active capture/compare 1 register when an update event occurs.

The active capture/compare register contains the value to be compared to the counter TIM_CNT and signaled on tim_oc1 output.

Non-dithering mode (DITHEN = 0)

The register holds the compare value.

Dithering mode (DITHEN = 1)

The register holds the integer part in CCR1[31:4]. The CCR1[3:0] bitfield contains the dithered part.

If channel CC1 is configured as input:

CCR1 is the counter value transferred by the last input capture 1 event (tim_ic1). The TIM_CCR1 register is read-only and cannot be programmed.

Non-dithering mode (DITHEN = 0)

The register holds the capture value.

Dithering mode (DITHEN = 1)

The register holds the capture in CCR1[31:0]. The CCR1[3:0] bits are reset.

32.5.16 TIM capture/compare register 2 (TIM_CCR2)

Address offset: 0x038

Reset value: 0x0000 0000

31302928272625242322212019181716
CCR2[31:16]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
CCR2[15:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 31:0 CCR2[31:0]: Capture/compare 2 value

If channel CC2 is configured as output:

CCR2 is the value to be loaded in the actual capture/compare 2 register (preload value). It is loaded permanently if the preload feature is not selected in the TIM_CCMR2 register (bit OC2PE). Else the preload value is copied in the active capture/compare 2 register when an update event occurs.

The active capture/compare register contains the value to be compared to the counter TIM_CNT and signaled on tim_oc2 output.

Non-dithering mode (DITHEN = 0)

The register holds the compare value.

Dithering mode (DITHEN = 1)

The register holds the integer part in CCR2[31:4]. The CCR2[3:0] bitfield contains the dithered part.

If channel CC2 is configured as input:

CCR2 is the counter value transferred by the last input capture 2 event (tim_ic2). The TIM_CCR2 register is read-only and cannot be programmed.

Non-dithering mode (DITHEN = 0)

The register holds the capture value.

Dithering mode (DITHEN = 1)

The register holds the capture in CCR2[31:0]. The CCR2[3:0] bits are reset.

32.5.17 TIM capture/compare register 3 (TIM_CCR3)

Address offset: 0x03C

Reset value: 0x0000 0000

31302928272625242322212019181716
CCR3[31:16]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
CCR3[15:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 31:0 CCR3[31:0]: Capture/compare 3 value

If channel CC3 is configured as output:

CCR3 is the value to be loaded in the actual capture/compare 3 register (preload value). It is loaded permanently if the preload feature is not selected in the TIM_CCMR3 register (bit OC3PE). Else the preload value is copied in the active capture/compare 3 register when an update event occurs.

The active capture/compare register contains the value to be compared to the counter TIM_CNT and signaled on tim_oc3 output.

Non-dithering mode (DITHEN = 0)

The register holds the compare value.

Dithering mode (DITHEN = 1)

The register holds the integer part in CCR3[31:4]. The CCR3[3:0] bitfield contains the dithered part.

If channel CC3 is configured as input:

CCR3 is the counter value transferred by the last input capture 3 event (tim_ic3). The TIM_CCR3 register is read-only and cannot be programmed.

Non-dithering mode (DITHEN = 0)

The register holds the capture value.

Dithering mode (DITHEN = 1)

The register holds the capture in CCR3[31:0]. The CCR3[3:0] bits are reset.

32.5.18 TIM capture/compare register 4 (TIM_CCR4)

Address offset: 0x040

Reset value: 0x0000 0000

31302928272625242322212019181716
CCR4[31:16]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
CCR4[15:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 31:0 CCR4[31:0]: Capture/compare 4 value

If channel CC4 is configured as output:

CCR4 is the value to be loaded in the actual capture/compare 4 register (preload value). It is loaded permanently if the preload feature is not selected in the TIM_CCMR4 register (bit OC4PE). Else the preload value is copied in the active capture/compare 4 register when an update event occurs.

The active capture/compare register contains the value to be compared to the counter TIM_CNT and signaled on tim_oc4 output.

Non-dithering mode (DITHEN = 0)

The register holds the compare value.

Dithering mode (DITHEN = 1)

The register holds the integer part in CCR4[31:4]. The CCR4[3:0] bitfield contains the dithered part.

If channel CC4 is configured as input:

CCR4 is the counter value transferred by the last input capture 4 event (tim_ic4). The TIM_CCR4 register is read-only and cannot be programmed.

Non-dithering mode (DITHEN = 0)

The register holds the capture value.

Dithering mode (DITHEN = 1)

The register holds the capture in CCR4[31:0]. The CCR4[3:0] bits are reset.

32.5.19 TIM timer encoder control register (TIM_ECR)

Address offset: 0x058

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.PWPRSC[2:0]PW[7:0]
rwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.IPOS[1:0]FIDXIBLK[1:0]IDIR[1:0]IE
rwrwrwrwrwrwrwrw

Bits 31:27 Reserved, must be kept at reset value.

Bits 26:24 PWPRSC[2:0]: Pulse width prescaler

This bitfield sets the clock prescaler for the pulse generator, as following:

\[t_{PWG} = (2^{(PWPRSC[2:0])}) \times t_{tim\_ker\_ck}\]

Bits 23:16 PW[7:0]: Pulse width

This bitfield defines the pulse duration, as following:

\[t_{PW} = PW[7:0] \times t_{PWG}\]

Bits 15:8 Reserved, must be kept at reset value.

Bits 7:6 IPOS[1:0]: Index positioning

In quadrature encoder mode (SMS[4:0] = 00001, 00010, 00011, 01110, 01111), this bit indicates in which AB input configuration the Index event resets the counter.

In directional clock mode or clock plus direction mode (SMS[4:0] = 01010, 01011, 01100, 01101), these bits indicates on which level the Index event resets the counter. In bidirectional clock mode, this applies for both clock inputs.

Note: IPOS[1] bit is not significant

Bit 5 FIDX: First index

This bit indicates if the first index only is taken into account

Bits 4:3 IBLK[1:0]: Index blanking

This bit indicates if the Index event is conditioned by the tim_ti3 input

Bits 2:1 IDIR[1:0]: Index direction

This bit indicates in which direction the Index event resets the counter.

Bit 0 IE: Index enable

This bit indicates if the Index event resets the counter.

32.5.20 TIM timer input selection register (TIM_TISEL)

Address offset: 0x05C

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.TI4SEL[3:0]Res.Res.Res.Res.TI3SEL[3:0]
rwrwrwrwrwrwrwrw
1514131211109876543210
Res.Res.Res.Res.TI2SEL[3:0]Res.Res.Res.Res.TI1SEL[3:0]
rwrwrwrwrwrwrwrw

Bits 31:28 Reserved, must be kept at reset value.

32.5.21 TIM alternate function register 1 (TIM_AF1)

Address offset: 0x060

Reset value: 0x0000 0000

column_1column_2Bits 27:24 Bits 19:16 Bits 11:8... ... ...TI4SEL[3:0] 0001: tim_ti4_in1 1111: tim_ti4_in15 Refer to implementation. TI3SEL[3:0] 0001: tim_ti3_in1 1111: tim_ti3_in15 Refer to implementation. TI2SEL[3:0] 0001: tim_ti2_in1 1111: tim_ti2_in15 Refer to implementation.column_6column_7column_8column_9column_10column_11column_12column_13column_14column_15RM0522
Bits 3:0TI1SEL[3:0]: Selects tim_ti1[15:0] input
Refer to1111: tim_ti1_in15 Section 32.4.2: TIM2/TIM3/TIM4/TIM5 pins and internal signals implementation.for product specific
31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.ETRSEL[4:2]
rwrwrw
1514131211109876543210
ETRSEL[1:0] rwRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.

Bits 31:19 Reserved, must be kept at reset value.

Bits 18:14 ETRSEL[4:0]: etr_in source selection

These bits select the etr_in input source.

00000: tim_etr0: TIM_ETR input

00001: tim_etr1

...

11111: tim_etr31

Refer to Section 32.4.2: TIM2/TIM3/TIM4/TIM5 pins and internal signals for product specific implementation.

Bits 13:0 Reserved, must be kept at reset value.

32.5.22 TIM alternate function register 2 (TIM_AF2)

Address offset: 0x064

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.OCRSEL[3:0]
rwrwrwrw
1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.

Bits 31:20 Reserved, must be kept at reset value.

Bits 19:16 OCRSEL[3:0]: ocref_clr source selection

These bits select the ocref_clr input source.

0000: tim_ocref_clr0

0001: tim_ocref_clr1

...

0111: tim_ocref_clr15

Refer to Section 32.4.2: TIM2/TIM3/TIM4/TIM5 pins and internal signals for product specific implementation.

Bits 15:0 Reserved, must be kept at reset value.

32.5.23 TIM DMA control register (TIM_DCR)

Address offset: 0x3DC

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.DBSS[3:0]
rwrwrwrw
1514131211109876543210
Res.Res.Res.DBL[4:0]Res.Res.Res.DBA[4:0]
rwrwrwrwrwrwrwrwrwrw

Bits 31:20 Reserved, must be kept at reset value.

Bits 19:16 DBSS[3:0]: DMA burst source selection

This bitfield defines the interrupt source that triggers the DMA burst transfers (the timer recognizes a burst transfer when a read or a write access is done to the TIM_DMAR address).

0000: Reserved
0001: Update
0010: CC1
0011: CC2
0100: CC3
0101: CC4
0110: COM
0111: Trigger
Others: reserved

Bits 15:13 Reserved, must be kept at reset value.

ST logo
ST logo

Bits 12:8 DBL[4:0]: DMA burst length

This 5-bit vector defines the length of DMA transfers (the timer recognizes a burst transfer when a read or a write access is done to the TIM_DMAR address), i.e. the number of transfers. Transfers can be in half-words or in bytes (see example below).

00000: 1 transfer
00001: 2 transfers
00010: 3 transfers
...
11111: 32 transfers

Example: Let us consider the following transfer: DBL = 6 bytes & DBA = TIM2_CR1.

–If DBL = 6 bytes and DBA = TIM2_CR1 represents the address of the byte to be transferred, the address of the transfer is given by the following equation:
(TIM_CR1 address) + DBA + (DMA index), where DMA index = DBL

In this example, 7 bytes are added to (TIM_CR1 address) + DBA, which gives us the address from/to which the data are copied. In this case, the transfer is done to 7 registers starting from the following address: (TIM_CR1 address) + DBA

According to the configuration of the DMA Data Size, several cases may occur:

Bits 7:5 Reserved, must be kept at reset value.

Bits 4:0 DBA[4:0]: DMA base address

This 5-bits vector defines the base-address for DMA transfers (when read/write access are done through the TIM_DMAR address). DBA is defined as an offset starting from the address of the TIM_CR1 register.

Example:

00000: TIM_CR1,
00001: TIM_CR2,
00010: TIM_SMCR,
...

32.5.24 TIM DMA address for full transfer (TIM_DMAR)

Address offset: 0x3E0

Reset value: 0x0000 0000

31302928272625242322212019181716
DMAB[31:16]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
DMAB[15:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 31:0 DMAB[31:0]: DMA register for burst accesses

A read or write operation to the DMAR register accesses the register located at the address
(TIM_CR1 address) + (DBA + DMA index) x 4

where TIM_CR1 address is the address of the control register 1, DBA is the DMA base address configured in TIM_DCR register, DMA index is automatically controlled by the DMA transfer, and ranges from 0 to DBL (DBL configured in TIM_DCR).

32.5.25 TIM2/TIM3/TIM4/TIM5 register map

TIM2/TIM3/TIM4/TIM5 registers are mapped as described in the table below.

Table 327. TIM2/TIM3/TIM4/TIM5 register map and reset values

OffsetRegister name313029282726252423222120191817161514131211109876543210
0x000TIM_CR1ResResResResCKD2[3:0]ResResResResResResResResResResResDITHENUIFREMAResCKD [1:0]ARPECMS [1:0]DIROPMURSUDISCEN
Reset value0000000000000000
0x004TIM_CR2TI3INVTI2INVTI1INVADSYNCXORPSResMMS[3]ResResResResResResResResResResResResResResResResResTI1SMMS[2:0]CCDSResResResRes
Reset value000000000000
0x008TIM_SMCRResResSETPS[3:0]SMSPSSMSPResResTS [4:3]ResResSMS [4:3]ETPECEETPS [1:0]ETF[3:0]MSMTS[2:0]OCCSSMS[2:0]
Reset value0000000000000000000000000
0x00CTIM_DIERResResResResResResResResTERRIEIERRIEDIRIEIDXIEResResResResResTDEResCC4DECC3DECC2DECC1DEUDEBIETIEResCC4IECC3IECC2IECC1IEUIE
Reset value00000000000000000
0x010TIM_SRTI4FSTI3FSTI2FSTI1FSResResResResUIOVRFTERRFIERRFDIRFIDXFResResResResResResCC4OFCC3OFCC2OFCC1OFResResResTIFResCC4IFCC3IFCC2IFCC1IFUIF
Reset value0000000000000000000
0x014TIM_EGRResResResResResResResResResResResResResResResResResResResResResResResResTGResCC4GCC3GCC2GCC1GUG
Reset value0000000
0x018TIM_CCMR1
Input capture mode
ResResResResResResResResResResResResResResResResIC2F[3:0]IC2 PSC [1:0]CC2S [1:0]IC1F[3:0]IC1 PSC [1:0]CC1S [1:0]
Reset value0000000000000000
TIM_CCMR1
Output compare mode
ResResResResResResOC2M[4:3]ResResResResResResResOC1M[4:3]OC2CEOC2M [2:0]OC2PEOC2FECC2S [1:0]OC1CEOC1M [2:0]OC1PEOC1FECC1S [1:0]
Reset value0000000000000000000

Table 327. TIM2/TIM3/TIM4/TIM5 register map and reset values (continued)

OffsetRegister name313029282726252423222120191817161514131211109876543210
0x01CTIM_CCMR2
Input capture mode
ResResResResResResResResResResResResResResResResIC4F[3:0]IC4PSC[1:0]CC4S[1:0]IC3F[3:0]IC3PSC[1:0]CC3S[1:0]
Reset value000000000000000
TIM_CCMR2
Output compare mode
ResResResResResResOC4M[4:3]ResResResResResResResOC3M[4:3]O24CEOC4M[2:0]OC4PEOC4FECC4S[1:0]OC3CEOC3M[2:0]OC3PEOC3FECC3S[1:0]
Reset value0000000000000000000
0x020TIM_CCERResResResResResResResResResResResResResResResResCC4NPResCC4PCC4ECC3NPResCC3PCC3ECC2NPResCC2PCC2ECC1NPResCC1PCC1E
Reset value000000000000
0x024TIM_CNTUIFCPY_CNT[31]CNT[30:16]
(CNT[31:16] on 32-bit timers only)
CNT[15:0]
Reset value0000000000000000000000000000000
0x028TIM_PSCResResResResResResResResResResResResResResResResPSC[15:0]
Reset value000000000000000
0x02CTIM_ARRARR[31:0]
Reset value1111111111111111111111111111111
0x030Reserved
0x034TIM_CCR1CCR1[31:20]
(32-bit timers only)
CCR1[19:0]
Reset value0000000000000000000000000000000
0x038TIM_CCR2CCR2[31:20]
(32-bit timers only)
CCR2[19:0]
Reset value0000000000000000000000000000000
0x03CTIM_CCR3CCR3[31:20]
(32-bit timers only)
CCR3[19:0]
Reset value0000000000000000000000000000000
0x040TIM_CCR4CCR4[31:20]
(32-bit timers only)
CCR4[19:0]
Reset value0000000000000000000000000000000
0x044..0x054Reserved
0x058TIM_ECRResResResResResPWPRSC[2:0]PW[7:0]ResResResResResResResResResIPOS[1:0]FIDXIBLK[1:0]IDIR[1:0]IE
Reset value000000000000000000
0x05CTIM_TISELResResResResTI4SEL[3:0]ResResResResTI3SEL[3:0]ResResResResTI2SEL[3:0]ResResResResTI1SEL[3:0]
Reset value0000000000000000
0x060TIM_AF1ResResResResResResResResResResResResETRSEL[4:0]ResResResResResResResResResResResResResResRes
Reset value00000

Table 327. TIM2/TIM3/TIM4/TIM5 register map and reset values (continued)

OffsetRegister name313029282726252423222120191817161514131211109876543210
0x064TIM_AF2ResResResResResResResResResResResResOCRSEL[3:0]ResResResResResResResResResResResResResResRes
Reset value0000
0x068..0x3D8ReservedRes.
0x3DCTIM_DCRResResResResResResResResResResResResDBSS[3:0]ResResResDBL[4:0]ResResResDBA[4:0]
Reset value0000000000000
0x3E0TIM_DMARDMAB[31:0]
Reset value0000000000000000000000000000000

Refer to Section 2.2: Memory organization for the register boundary addresses.