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

30.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 30.4.23: Timer synchronization .

30.2 TIM2/TIM3/TIM4/TIM5 main features

General-purpose TIMx timer features include:

30.3 TIM2/TIM3/TIM4/TIM5 implementation

Table 284. STM32G4 series general purpose timers
Timer instanceTIM2TIM3TIM4TIM5
Resolution32-bit16-bit16-bit32-bit
OCREF clear selectionYesYesNoNo
Sourcestim_etrfrf
tim_ocref_clr[7:0]
tim_etrfrf
tim_ocref_clr[7:0]
tim_etrfrf
-
tim_etrfrf
-

30.4 TIM2/TIM3/TIM4/TIM5 functional description

30.4.1 Block diagram

Figure 385. General-purpose timer block diagram

Figure 385. General-purpose timer block diagram. This is a complex block diagram of a general-purpose timer (TIM2/TIM3/TIM4/TIM5). It shows the internal architecture including the counter (CNT), prescalers (PSC), capture/compare registers (CC1R, CC2R, CC3R, CC4R), and various input/output channels (CH1-CH4). The diagram includes external pins like tim_ker_ck, tim_pclk, TIM_ETR, tim_etr[15:1], tim_itr[15:0], tim_itt, tim_cc1_dma, tim_cc2_dma, tim_cc3_dma, tim_cc4_dma, tim_upd_dma, tim_trgi_dma, TIM_CH1, tim_ti1_in[15:1], TIM_CH2, tim_ti2_in[15:1], TIM_CH3, tim_ti3_in[15:1], TIM_CH4, tim_ti4_in[15:1], and tim_ocef_clr[7:0]. Internal components include an XOR gate, input filters and edge detectors, prescalers, capture/compare registers, an auto-reload register, a slave controller mode, an encoder interface, and output controls. The diagram also shows signal paths for events, interrupts, and DMA outputs.

Notes:
Reg Preload registers transferred to active registers on U event according to control bit
Event
Interrupt & DMA output

MSV62373V6

Figure 385. General-purpose timer block diagram. This is a complex block diagram of a general-purpose timer (TIM2/TIM3/TIM4/TIM5). It shows the internal architecture including the counter (CNT), prescalers (PSC), capture/compare registers (CC1R, CC2R, CC3R, CC4R), and various input/output channels (CH1-CH4). The diagram includes external pins like tim_ker_ck, tim_pclk, TIM_ETR, tim_etr[15:1], tim_itr[15:0], tim_itt, tim_cc1_dma, tim_cc2_dma, tim_cc3_dma, tim_cc4_dma, tim_upd_dma, tim_trgi_dma, TIM_CH1, tim_ti1_in[15:1], TIM_CH2, tim_ti2_in[15:1], TIM_CH3, tim_ti3_in[15:1], TIM_CH4, tim_ti4_in[15:1], and tim_ocef_clr[7:0]. Internal components include an XOR gate, input filters and edge detectors, prescalers, capture/compare registers, an auto-reload register, a slave controller mode, an encoder interface, and output controls. The diagram also shows signal paths for events, interrupts, and DMA outputs.
  1. 1. This feature is not available on all timers, refer to Section 30.3: TIM2/TIM3/TIM4/TIM5 implementation .

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

Table 285 and Table 286 in this section summarize the TIM inputs and outputs.

Table 285. 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 286. 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[15: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[15: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[7: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 286. 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.

Table 287, Table 288, Table 289 and Table 290 are listing the sources connected to the tim_tif[4:1] input multiplexers.

Table 287. Interconnect to the tim_ti1 input multiplexer

tim_ti1 inputsSources
TIM2TIM3TIM4TIM5
tim_ti1_in0TIM2_CH1TIM3_CH1TIM4_CH1TIM5_CH1
tim_ti1_in1comp1_outcomp1_outcomp1_outLSI
tim_ti1_in2comp2_outcomp2_outcomp2_outLSE
tim_ti1_in3comp3_outcomp3_outcomp3_outRTC wake-up
tim_ti1_in4comp4_outcomp4_outcomp4_outcomp1_out
tim_ti1_in5comp5_outcomp5_outcomp5_outcomp2_out
tim_ti1_in6Reservedcomp6_outcomp6_outcomp3_out
tim_ti1_in7comp7_outcomp7_outcomp4_out
tim_ti1_in8ReservedReservedcomp5_out
tim_ti1_in9comp6_out
tim_ti1_in10comp7_out
tim_ti1_in[15:11]Reserved

Table 288. Interconnect to the tim_ti2 input multiplexer

tim_ti2 inputsSources
TIM2TIM3TIM4TIM5
tim_ti2_in0TIM2_CH2TIM3_CH2TIM4_CH2TIM5_CH2
tim_ti2_in1comp1_outcomp1_outcomp1_outcomp1_out
tim_ti2_in2comp2_outcomp2_outcomp2_outcomp2_out
tim_ti2_in3comp3_outcomp3_outcomp3_outcomp3_out

Table 288. Interconnect to the tim_ti2 input multiplexer (continued)

tim_ti2 inputsSources
TIM2TIM3TIM4TIM5
tim_ti2_in4comp4_outcomp4_outcomp4_outcomp4_out
tim_ti2_in5comp6_outcomp5_outcomp5_outcomp5_out
tim_ti2_in6Reservedcomp6_outcomp6_outcomp6_out
tim_ti2_in7comp7_outcomp7_outcomp7_out
tim_ti2_in[15:8]Reserved

Table 289. Interconnect to the tim_ti3 input multiplexer

tim_ti3 inputsSources
TIM2TIM3TIM4TIM5
tim_ti3_in0TIM2_CH3TIM3_CH3TIM4_CH3TIM5_CH3
tim_ti3_in1comp4_outcomp3_outcomp5_outReserved
tim_ti3_in[15:2]Reserved

Table 290. Interconnect to the tim_ti4 input multiplexer

tim_ti4 inputsSources
TIM2TIM3TIM4TIM5
tim_ti4_in0TIM2_CH4TIM3_CH4TIM4_CH4TIM5_CH4
tim_ti4_in1comp1_outReservedcomp6_outReserved
tim_ti4_in2comp2_outReserved
tim_ti4_in[15:3]Reserved

Table 291 lists the internal sources connected to the tim_itr input multiplexer.

Table 291. TIMx internal trigger connection

TIMxTIM2TIM3TIM4TIM5
tim_itr0tim1_trgotim1_trgotim1_trgotim1_trgo
tim_itr1Reservedtim2_trgotim2_trgotim2_trgo
tim_itr2tim3_trgoReservedtim3_trgotim3_trgo
tim_itr3tim4_trgotim4_trgoReservedtim4_trgo
tim_itr4tim5_trgotim5_trgotim5_trgoReserved
tim_itr5tim8_trgotim8_trgotim8_trgotim8_trgo
tim_itr6tim15_trgotim15_trgotim15_trgotim15_trgo
tim_itr7tim16_oc1tim16_oc1tim16_oc1tim16_oc1
Table 291. TIMx internal trigger connection (continued)
TIMxTIM2TIM3TIM4TIM5
tim_itr8tim17_oc1tim17_oc1tim17_oc1tim17_oc1
tim_itr9tim20_trgotim20_trgotim20_trgotim20_trgo
tim_itr10hrtim_out_sync2hrtim_out_sync2hrtim_out_sync2hrtim_out_sync2
tim_itr11USB SOF SYNCReservedReservedReserved
tim_itr[15:12]Reserved

Table 292 lists the internal sources connected to the tim_etr input multiplexer.

Table 292. Interconnect to the tim_etr input multiplexer
Timer external trigger input signalTimer external trigger signals assignment
TIM2TIM3TIM4TIM5
tim_etr0TIM2_ETRTIM3_ETRTIM4_ETRTIM5_ETR
tim_etr1comp1_outcomp1_outcomp1_outcomp1_out
tim_etr2comp2_outcomp2_outcomp2_outcomp2_out
tim_etr3comp3_outcomp3_outcomp3_outcomp3_out
tim_etr4comp4_outcomp4_outcomp4_outcomp4_out
tim_etr5comp5_outcomp5_outcomp5_outcomp5_out
tim_etr6comp6_outcomp6_outcomp6_outcomp6_out
tim_etr7comp7_outcomp7_outcomp7_outcomp7_out
tim_etr8TIM3_ETRTIM2_ETRTIM3_ETRTIM2_ETR
tim_etr9TIM4_ETRTIM4_ETRTIM5_ETRTIM3_ETR
tim_etr10TIM5_ETRReservedReservedReserved
tim_etr11LSEadc2_awa1
tim_etr12Reservedadc2_awa2
tim_etr13adc2_awa3
tim_etr[15:14]Reserved

Table 293 lists the internal sources connected to the tim_ocref_clr input multiplexer.

Table 293. Interconnect to the tim_ocref_clr input multiplexer

Timer
tim_ocref_clr
signal
Timer tim_ocref_clr signals assignment
TIM2TIM3TIM4TIM5
tim_ocref_clr0comp1_outcomp1_outReservedReserved
tim_ocref_clr1comp2_outcomp2_out
tim_ocref_clr2comp3_outcomp3_out
tim_ocref_clr3comp4_outcomp4_out
tim_ocref_clr4comp5_outcomp5_out
tim_ocref_clr5comp6_outcomp6_out
tim_ocref_clr6comp7_outcomp7_out
tim_ocref_clr7Reserved

30.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 TIMx_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 TIMx_CR1 register. It can also be generated by software. The generation of the update event is described in detail for each configuration.

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

Note that the actual counter enable signal CNT_EN is set one clock cycle after CEN.

Prescaler description

The prescaler can divide the counter clock frequency by any factor between 1 and 65536. It is based on a 16-bit counter controlled through a 16-bit/32-bit register (in the TIMx_PSC register). It can be changed on the fly as this control register is buffered. The new prescaler ratio is taken into account at the next update event.

Figure 386 and Figure 387 give some examples of the counter behavior when the prescaler ratio is changed on the fly:

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

Timing diagram showing the effect of changing the prescaler division ratio from 1 to 2 on a timer counter. The diagram includes signals for tim_psc_ck, CEN, tim_cnt_ck, Counter register, Update event (UEV), Prescaler control register, Prescaler buffer, and Prescaler counter. It shows the counter register values (F7 to 03) and the prescaler counter values (0 to 1) before and after the division change.

The timing diagram illustrates the behavior of a timer when the prescaler division ratio is changed from 1 to 2. The signals shown are:

The diagram shows that the counter register continues to count up even after the prescaler division ratio is changed. The Update event (UEV) occurs when the counter rolls over, and at that point, the prescaler buffer is updated with the new division ratio. The prescaler counter then begins to count with the new division ratio.

MSv50998V1

Timing diagram showing the effect of changing the prescaler division ratio from 1 to 2 on a timer counter. The diagram includes signals for tim_psc_ck, CEN, tim_cnt_ck, Counter register, Update event (UEV), Prescaler control register, Prescaler buffer, and Prescaler counter. It shows the counter register values (F7 to 03) and the prescaler counter values (0 to 1) before and after the division change.

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

Figure 387. Counter timing diagram with prescaler division change from 1 to 4. The diagram shows the relationship between tim_psc_ck, CEN, tim_cnt_ck, Counter register, Update event (UEV), Prescaler control register, Prescaler buffer, and Prescaler counter over time. The counter register values are F7, F8, F9, FA, FB, FC, 00, 01. The prescaler control register values are 0, 3. The prescaler buffer values are 0, 3. The prescaler counter values are 0, 0, 1, 2, 3, 0, 1, 2, 3.

The timing diagram illustrates the behavior of a general-purpose timer when the prescaler division is changed from 1 to 4. The signals shown are:

The diagram shows that the prescaler division change takes effect after the current prescaler count reaches its maximum value (3 in this case). The counter register values are F7, F8, F9, FA, FB, FC, 00, 01. The prescaler control register values are 0, 3. The prescaler buffer values are 0, 3. The prescaler counter values are 0, 0, 1, 2, 3, 0, 1, 2, 3. The text 'MSv5099V1' is visible in the bottom right corner.

Figure 387. Counter timing diagram with prescaler division change from 1 to 4. The diagram shows the relationship between tim_psc_ck, CEN, tim_cnt_ck, Counter register, Update event (UEV), Prescaler control register, Prescaler buffer, and Prescaler counter over time. The counter register values are F7, F8, F9, FA, FB, FC, 00, 01. The prescaler control register values are 0, 3. The prescaler buffer values are 0, 3. The prescaler counter values are 0, 0, 1, 2, 3, 0, 1, 2, 3.

30.4.4 Counter modes

Up-counting mode

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

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

The UEV event can be disabled by software by setting the UDIS bit in TIMx_CR1 register. This is to avoid updating the shadow registers while writing new values in the preload registers. Then no update event occurs until the UDIS bit has been written to 0. However, the counter restarts from 0, as well as the counter of the prescaler (but the prescale rate does not change). In addition, if the URS bit (update request selection) in TIMx_CR1 register is set, setting the UG bit generates an update event UEV but without setting the UIF flag (thus no interrupt or DMA request is sent). This is to avoid generating both update and capture interrupts when clearing the counter on the capture event.

When an update event occurs, all the registers are updated and the update flag (UIF bit in TIMx_SR register) is set (depending on the URS bit):

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

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

Timing diagram for internal clock divided by 1

Timing diagram showing the relationship between the prescaler clock (tim_psc_ck), counter enable (CEN), counter clock (tim_cnt_ck), counter register values, counter overflow, update event (UEV), and update interrupt flag (UIF) when the internal clock is divided by 1.

The diagram illustrates the following signals and their timing:

MSv50997V1

Timing diagram for internal clock divided by 1

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

Timing diagram for internal clock divided by 2

Timing diagram showing the relationship between the prescaler clock (tim_psc_ck), counter enable (CEN), counter clock (tim_cnt_ck), counter register values, counter overflow, update event (UEV), and update interrupt flag (UIF) when the internal clock is divided by 2.

The diagram illustrates the following signals and their timing:

MSv62300V1

Timing diagram for internal clock divided by 2

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

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

This timing diagram illustrates the operation of a timer when the internal clock is divided by 4. The top signal, tim_psc_ck , is a high-frequency square wave. Below it, the CEN (Counter Enable) signal is shown as a horizontal line that goes high to enable counting. The tim_cnt_ck signal is a lower-frequency square wave, which is the prescaled version of tim_psc_ck . The Counter register shows a sequence of values: 0035, 0036, 0000, and 0001. The transition from 0036 to 0000 represents a counter overflow. The Counter overflow signal is a pulse that goes high when the counter reaches 0000. The Update event (UEV) is a pulse that goes high at the same time as the counter overflow. The Update interrupt flag (UIF) is a signal that goes high when the update event occurs. Vertical dashed lines indicate the timing relationships between the signals.

MSv62301V1

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

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

Timing diagram for internal clock divided by N. It shows the relationship between tim_psc_ck, tim_cnt_ck, Counter register values (1F, 20, 00), Counter overflow, Update event (UEV), and Update interrupt flag (UIF).

This timing diagram illustrates the operation of a timer when the internal clock is divided by an arbitrary value N. The top signal, tim_psc_ck , is a high-frequency square wave. Below it, the tim_cnt_ck signal is a lower-frequency square wave, which is the prescaled version of tim_psc_ck . The Counter register shows a sequence of values: 1F, 20, and 00. The transition from 20 to 00 represents a counter overflow. The Counter overflow signal is a pulse that goes high when the counter reaches 00. The Update event (UEV) is a pulse that goes high at the same time as the counter overflow. The Update interrupt flag (UIF) is a signal that goes high when the update event occurs. Vertical dashed lines indicate the timing relationships between the signals.

MSv62302V1

Timing diagram for internal clock divided by N. It shows the relationship between tim_psc_ck, tim_cnt_ck, Counter register values (1F, 20, 00), Counter overflow, Update event (UEV), and Update interrupt flag (UIF).

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

Timing diagram showing the relationship between tim_psc_ck, CEN, tim_cnt_ck, Counter register, Counter overflow, Update event (UEV), Update interrupt flag (UIF), and Auto-reload preload register. The diagram illustrates the counter sequence from 31 to 07, the overflow at 36, and the update event triggered by the overflow. The auto-reload preload register is shown changing from FF to 36.

The timing diagram illustrates the operation of a general-purpose timer. The signals shown are:

MSV62303V1

Timing diagram showing the relationship between tim_psc_ck, CEN, tim_cnt_ck, Counter register, Counter overflow, Update event (UEV), Update interrupt flag (UIF), and Auto-reload preload register. The diagram illustrates the counter sequence from 31 to 07, the overflow at 36, and the update event triggered by the overflow. The auto-reload preload register is shown changing from FF to 36.

Figure 393. Counter timing diagram, Update event when ARPE = 1 (TIMx_ARR preloaded)

Figure 393. Counter timing diagram, Update event when ARPE = 1 (TIMx_ARR preloaded). The diagram shows the relationship between the prescaler clock (tim_psc_ck), counter enable (CEN), counter clock (tim_cnt_ck), counter register values, counter overflow, update event (UEV), update interrupt flag (UIF), auto-reload preload register, and auto-reload shadow register. The counter counts from F0 to F5, then overflows to 00. The update event (UEV) is generated at the overflow. The auto-reload preload register is updated with F5, and the auto-reload shadow register is updated with 36. A note indicates that a new value is written in TIMx_ARR.

The timing diagram illustrates the operation of a general-purpose timer in up-counting mode with ARPE = 1. The signals shown are:

Write a new value in TIMx_ARR

MSV62304V1

Figure 393. Counter timing diagram, Update event when ARPE = 1 (TIMx_ARR preloaded). The diagram shows the relationship between the prescaler clock (tim_psc_ck), counter enable (CEN), counter clock (tim_cnt_ck), counter register values, counter overflow, update event (UEV), update interrupt flag (UIF), auto-reload preload register, and auto-reload shadow register. The counter counts from F0 to F5, then overflows to 00. The update event (UEV) is generated at the overflow. The auto-reload preload register is updated with F5, and the auto-reload shadow register is updated with 36. A note indicates that a new value is written in TIMx_ARR.

Down-counting mode

In down-counting mode, the counter counts from the autoreload value (content of the TIMx_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 TIMx_EGR register (by software or by using the slave mode controller)

The UEV update event can be disabled by software by setting the UDIS bit in TIMx_CR1 register. This is to avoid updating the shadow registers while writing new values in the preload registers. Then no update event occurs until UDIS bit has been written to 0. However, the counter restarts from the current 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 TIMx_CR1 register is set, setting the UG bit generates an update event UEV but without setting the UIF flag (thus no interrupt or DMA request is sent). This is to avoid generating both update and capture interrupts when clearing the counter on the capture event.

When an update event occurs, all the registers are updated and the update flag (UIF bit in TIMx_SR register) is set (depending on the URS bit):

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

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

Counter timing diagram showing the relationship between prescaler clock, counter enable, counter register values, underflow, update event, and interrupt flag.

The timing diagram illustrates the operation of a timer counter. The top signal, tim_psc_ck , is a periodic square wave representing the prescaler clock. Below it, CEN (Counter Enable) is a horizontal line that goes high to enable the counter. The tim_cnt_ck signal is a square wave that starts when CEN goes high, representing the counter clock. The Counter register shows a sequence of values: 05, 04, 03, 02, 01, 00, 36, 35, 34, 33, 32, 31, 30, 2F. The counter counts down from 05 to 00, then reloads to 36 and continues counting down. The Counter underflow (cnt_udf) signal is a pulse that goes high when the counter reaches 00. The Update event (UEV) signal is a pulse that goes high when the counter reaches 00. The Update interrupt flag (UIF) signal is a pulse that goes high when the counter reaches 00. Vertical dashed lines indicate the timing relationships between the signals.

MSv62305V1

Counter timing diagram showing the relationship between prescaler clock, counter enable, counter register values, underflow, update event, and interrupt flag.

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

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

This timing diagram illustrates the operation of a timer with its internal clock divided by 2. The top signal, tim_psc_ck , is a high-frequency square wave. Below it, the CEN (Counter Enable) signal is shown as a horizontal line that goes high to enable the counter. The tim_cnt_ck signal is a square wave with a frequency half that of tim_psc_ck . The Counter register displays a sequence of values: 0002, 0001, 0000, 0036, 0035, 0034, 0033. The Counter underflow signal pulses high when the counter reaches 0000. The Update event (UEV) and Update interrupt flag (UIF) signals also pulse high at the underflow point. Vertical dashed lines indicate the timing relationships between the signals. The diagram is labeled MSv62306V1 in the bottom right corner.

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

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

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

This timing diagram illustrates the operation of a timer with its internal clock divided by 4. The tim_psc_ck signal is a high-frequency square wave. The CEN signal is shown as a horizontal line that goes high to enable the counter. The tim_cnt_ck signal is a square wave with a frequency one-quarter that of tim_psc_ck . The Counter register displays a sequence of values: 0001, 0000, 0000, 0001. The Counter underflow signal pulses high when the counter reaches 0000. The Update event (UEV) and Update interrupt flag (UIF) signals also pulse high at the underflow point. Vertical dashed lines indicate the timing relationships between the signals. The diagram is labeled MSv62307V1 in the bottom right corner.

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

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

Timing diagram for internal clock divided by N. It shows the relationship between tim_psc_ck, tim_cnt_ck, Counter register values, Counter underflow, Update event (UEV), and Update interrupt flag (UIF).

This timing diagram illustrates the operation of a timer when the internal clock is divided by N. The top signal, tim_psc_ck , is a periodic square wave. Below it, tim_cnt_ck is a lower-frequency square wave derived from tim_psc_ck . The Counter register shows a sequence of values: 20, 1F, 00, 36. A counter underflow event occurs when the counter transitions from 1F to 00. This underflow triggers a pulse on the Counter underflow line, an Update event (UEV) , and sets the Update interrupt flag (UIF) . The diagram is labeled MSv62308V1.

Timing diagram for internal clock divided by N. It shows the relationship between tim_psc_ck, tim_cnt_ck, Counter register values, Counter underflow, Update event (UEV), and Update interrupt flag (UIF).

Figure 398. Counter timing diagram, Update event

Timing diagram showing the Update event. It includes signals for tim_pasc_ck, CEN, tim_cnt_ck, Counter register, Counter underflow, Update event (UEV), Update interrupt flag (UIF), and Auto-reload preload register. It shows the counter counting down from 05 to 00, then reloading to 36.

This timing diagram shows the timer's behavior during an update event. The tim_pasc_ck signal is a high-frequency clock. The CEN (Counter Enable) signal is asserted. The tim_cnt_ck signal is derived from tim_pasc_ck . The Counter register counts down from 05 to 00, then overflows to 36. This overflow triggers a pulse on the Counter underflow line, an Update event (UEV) , and sets the Update interrupt flag (UIF) . Simultaneously, the Auto-reload preload register is updated from FF to 36. A note indicates that a new value can be written in the TIMx_ARR register. The diagram is labeled MSv62309V1.

Timing diagram showing the Update event. It includes signals for tim_pasc_ck, CEN, tim_cnt_ck, Counter register, Counter underflow, Update event (UEV), Update interrupt flag (UIF), and Auto-reload preload register. It shows the counter counting down from 05 to 00, then reloading to 36.

Center-aligned mode (up/down-counting)

In center-aligned mode, the counter counts from 0 to the autoreload value (content of the TIMx_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 TIMx_CR1 register are not equal to 00. The output compare interrupt flag of channels configured in output is set when: the counter counts down (Center aligned mode 1, CMS = 01), the counter counts up (Center aligned mode 2, CMS = 10) the counter counts up and down (Center aligned mode 3, CMS = 11).

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

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

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

In addition, if the URS bit (update request selection) in TIMx_CR1 register is set, setting the UG bit generates an update event UEV but without setting the UIF flag (thus no interrupt or DMA request is sent). This is to avoid generating both update and capture interrupts when clearing the counter on the capture event.

When an update event occurs, all the registers are updated and the update flag (UIF bit in TIMx_SR register) is set (depending on the URS bit):

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

Figure 399. Counter timing diagram, internal clock divided by 1, TIMx_ARR = 0x6

Timing diagram for Figure 399 showing signals: tim_psc_ck, CEN, tim_cnt_ck, Counter register (values 04 to 06), Counter underflow, Counter overflow, Update event (UEV), and Update interrupt flag (UIF).

This timing diagram illustrates the operation of a general-purpose timer in center-aligned mode 1 with an internal clock divided by 1 and an auto-reload register (TIMx_ARR) set to 0x6. The diagram shows the following signals over time:

Vertical dashed lines indicate the timing relationships between the counter register values and the generated signals. The diagram is labeled MSV62310V1.

Timing diagram for Figure 399 showing signals: tim_psc_ck, CEN, tim_cnt_ck, Counter register (values 04 to 06), Counter underflow, Counter overflow, Update event (UEV), and Update interrupt flag (UIF).
  1. 1. Here, center-aligned mode 1 is used (for more details refer to Section 30.5.1: TIMx control register 1 (TIMx_CR1)(x = 2 to 5) ).

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

Timing diagram for Figure 400 showing signals: tim_psc_ck, CEN, tim_cnt_ck, Counter register (values 0003 to 0000), Counter underflow, Update event (UEV), and Update interrupt flag (UIF).

This timing diagram illustrates the operation of a general-purpose timer in center-aligned mode 1 with an internal clock divided by 2 and an auto-reload register (TIMx_ARR) set to 0x6. The diagram shows the following signals over time:

Vertical dashed lines indicate the timing relationships between the counter register values and the generated signals. The diagram is labeled MSV62311V1.

Timing diagram for Figure 400 showing signals: tim_psc_ck, CEN, tim_cnt_ck, Counter register (values 0003 to 0000), Counter underflow, Update event (UEV), and Update interrupt flag (UIF).

Figure 401. Counter timing diagram, internal clock divided by 4, TIMx_ARR = 0x36

Timing diagram for Figure 401 showing signals: tim_psc_ck, CEN, tim_cnt_ck, Counter register (values 0034, 0035, 0036, 0035), Counter overflow, Update event (UEV), and Update interrupt flag (UIF).

This timing diagram illustrates the operation of a general-purpose timer in center-aligned mode. The top signal, tim_psc_ck , is a high-frequency square wave. Below it, the CEN (Counter Enable) signal is shown as a high-level pulse. The tim_cnt_ck signal is a lower-frequency square wave derived from the prescaler clock. The Counter register shows a sequence of values: 0034, 0035, 0036, and 0035. A Counter overflow pulse occurs when the counter reaches 0036 and rolls over to 0035. This overflow triggers an Update event (UEV) and sets the Update interrupt flag (UIF) . Vertical dashed lines indicate the timing relationships between the counter register values and the overflow/UEV/UIF signals.

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

MSV62312V1

Timing diagram for Figure 401 showing signals: tim_psc_ck, CEN, tim_cnt_ck, Counter register (values 0034, 0035, 0036, 0035), Counter overflow, Update event (UEV), and Update interrupt flag (UIF).

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

Timing diagram for Figure 402 showing signals: tim_psc_ck, tim_cnt_ck, Counter register (values 20, 1F, 01, 00), Counter underflow, Update event (UEV), and Update interrupt flag (UIF).

This timing diagram shows the timer's behavior when the internal clock is divided by N. The tim_psc_ck signal is shown with a break in its waveform. The tim_cnt_ck signal is also shown with a break. The Counter register displays values 20, 1F, 01, and 00. A Counter underflow occurs as the counter rolls over from 01 to 00. This underflow triggers an Update event (UEV) and sets the Update interrupt flag (UIF) . Vertical dashed lines mark the timing of the underflow relative to the counter register values.

MSV62313V1

Timing diagram for Figure 402 showing signals: tim_psc_ck, tim_cnt_ck, Counter register (values 20, 1F, 01, 00), Counter underflow, Update event (UEV), and Update interrupt flag (UIF).

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

Timing diagram showing 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 diagram illustrates the counter underflow and update event sequence when ARPE = 1.

The timing diagram illustrates the operation of a general-purpose timer when the Auto-reload Preload Enable (ARPE) bit is set to 1. The signals shown are:

Vertical dashed lines indicate key timing points: the first line marks the start of the sequence, and the second line marks the counter underflow and update event. The text 'MSV62314V1' is visible in the bottom right corner of the diagram area.

Timing diagram showing 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 diagram illustrates the counter underflow and update event sequence when ARPE = 1.

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

Figure 404. Counter timing diagram, Update event with ARPE = 1 (counter overflow). The diagram shows the relationship between the prescaler clock (tim_psc_ck), the counter enable (CEN), the counter clock (tim_cnt_ck), the counter register values, the counter overflow signal, the update event (UEV), the update interrupt flag (UIF), the auto-reload preload register, and the auto-reload active register. The counter register values are shown in hexadecimal: F7, F8, F9, FA, FB, FC, 36, 35, 34, 33, 32, 31, 30, 2F. The auto-reload preload register is set to FD, and the auto-reload active register is set to 36. The update event (UEV) is generated when the counter overflows from FC to 36. The update interrupt flag (UIF) is set when the counter overflows. The auto-reload preload register is updated when a new value is written in TIMx_ARR. The auto-reload active register is updated when the counter overflows.

The timing diagram illustrates the operation of a general-purpose timer counter. The top signal, tim_psc_ck , is a periodic clock. The CEN (Counter Enable) signal is shown as a high-level signal. The tim_cnt_ck (Counter Clock) is derived from tim_psc_ck when CEN is high. The Counter register shows a sequence of values: F7, F8, F9, FA, FB, FC, 36, 35, 34, 33, 32, 31, 30, 2F. A Counter overflow occurs when the counter reaches FC and rolls over to 36. This overflow generates an Update event (UEV) and sets the Update interrupt flag (UIF) . The Auto-reload preload register contains the value FD, which is written to TIMx_ARR . The Auto-reload active register contains the value 36, which is updated from the preload register upon overflow. The diagram is labeled MSv62315V1.

Figure 404. Counter timing diagram, Update event with ARPE = 1 (counter overflow). The diagram shows the relationship between the prescaler clock (tim_psc_ck), the counter enable (CEN), the counter clock (tim_cnt_ck), the counter register values, the counter overflow signal, the update event (UEV), the update interrupt flag (UIF), the auto-reload preload register, and the auto-reload active register. The counter register values are shown in hexadecimal: F7, F8, F9, FA, FB, FC, 36, 35, 34, 33, 32, 31, 30, 2F. The auto-reload preload register is set to FD, and the auto-reload active register is set to 36. The update event (UEV) is generated when the counter overflows from FC to 36. The update interrupt flag (UIF) is set when the counter overflows. The auto-reload preload register is updated when a new value is written in TIMx_ARR. The auto-reload active register is updated when the counter overflows.

30.4.5 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 TIMx_SMCR register), then the CEN , DIR (in the TIMx_CR1 register), and UG bits (in the TIMx_EGR register) are actual control bits and can be changed only by software (except 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 405 shows the behavior of the control circuit and the upcounter in normal mode, without prescaler.

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

Timing diagram for Figure 405 showing control signals and counter register values over time. The signals shown are tim_ker_ck (internal clock), CEN (counter enable), UG (update generation), counter initialization (internal), tim_cnt_ck and tim_psc_ck (counter and prescaler clock), and the Counter register values. The counter register values shown are 31, 32, 33, 34, 35, 36, 00, 01, 02, 03, 04, 05, 06, 07. The diagram is labeled MSv62317V2.
Timing diagram for Figure 405 showing control signals and counter register values over time. The signals shown are tim_ker_ck (internal clock), CEN (counter enable), UG (update generation), counter initialization (internal), tim_cnt_ck and tim_psc_ck (counter and prescaler clock), and the Counter register values. The counter register values shown are 31, 32, 33, 34, 35, 36, 00, 01, 02, 03, 04, 05, 06, 07. The diagram is labeled MSv62317V2.

External clock source mode 1

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

Figure 406. tim_ti2 external clock connection example

Block diagram for Figure 406 showing the connection of tim_ti2 external clock source. It includes blocks for TIMx_TISEL (TI2SEL[3:0]), TIM_CH2 (tim_ti2_in[15:0]), Filter (ICF[3:0]), Edge detector (tim_ti2f_rising, tim_ti2f_falling), CC2P (TIMx_CCER), TIMx_SMCR (TS[4:0]), Encoder mode, External clock mode 1, External clock mode 2, Internal clock mode, ECE, SMS[2:0], and tim_psc_ck. The diagram is labeled MSv62318V3.
Block diagram for Figure 406 showing the connection of tim_ti2 external clock source. It includes blocks for TIMx_TISEL (TI2SEL[3:0]), TIM_CH2 (tim_ti2_in[15:0]), Filter (ICF[3:0]), Edge detector (tim_ti2f_rising, tim_ti2f_falling), CC2P (TIMx_CCER), TIMx_SMCR (TS[4:0]), Encoder mode, External clock mode 1, External clock mode 2, Internal clock mode, ECE, SMS[2:0], and tim_psc_ck. The diagram is labeled MSv62318V3.
  1. 1. Codes ranging from 01000 to 11111: tim_itr[15:0].

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 TIMx_TISEL register.
  2. 2. Configure channel 2 to detect rising edges on the tim_ti2 input by writing CC2S= 01 in the TIMx_CCMR1 register.
  3. 3. Configure the input filter duration by writing the IC2F[3:0] bits in the TIMx_CCMR1 register (if no filter is needed, keep IC2F = 0000).

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

  1. 4. Select rising edge polarity by writing CC2P = 0 and CC2NP = 0 in the TIMx_CCER register.
  2. 5. Configure the timer in external clock mode 1 by writing SMS = 111 in the TIMx_SMCR register.
  3. 6. Select tim_ti2 as the input source by writing TS = 00110 in the TIMx_SMCR register.
  4. 7. Enable the counter by writing CEN = 1 in the TIMx_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 407. Control circuit in external clock mode 1

Timing diagram for external clock mode 1 showing the relationship between tim_ti2 input, CEN signal, counter register values, and TIF flag.

The diagram illustrates the timing for external clock mode 1. It shows five horizontal signal lines over time. The top line, 'tim_ti2', shows a periodic square wave. The second line, 'CEN', is a signal that goes high and stays high. The third line, 'tim_cnt_ck, tim_psc_ck', shows a series of narrow pulses that occur at the rising edges of the 'tim_ti2' signal. The fourth line, 'Counter register', shows the count values 34, 35, and 36, which increment at each rising edge of 'tim_ti2'. The bottom line, 'TIF', shows a pulse that goes high at each rising edge of 'tim_ti2' and returns to zero when 'Write TIF=0' is indicated by an arrow. The diagram is labeled 'MSv62319V1' in the bottom right corner.

Timing diagram for external clock mode 1 showing the relationship between tim_ti2 input, CEN signal, counter register values, and TIF flag.

External clock source mode 2

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

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

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

Figure 408. External trigger input block

Figure 408: External trigger input block diagram. It shows the signal path from TIM_ETR and TIMx_AF1[17:14] through a multiplexer to tim_etr_in. This signal goes through an edge detector (ETP), a divider (/1, /2, /4, /8 controlled by ETPS[1:0]), and a filter downcounter (controlled by ETF[3:0]) to become tim_etrf. A final multiplexer selects between Encoder mode, External clock mode 1 (tim_trgi), External clock mode 2 (tim_etrf), and Internal clock mode (tim_ker_ck) to produce tim_psc_ck. Control bits ECE and SMS[2:0] from TIMx_SMCR are shown at the bottom right.

The diagram illustrates the external trigger input block. The input signals TIM_ETR (tim_etr0) and tim_etr[15:1] are multiplexed with TIMx_AF1[17:14] to produce tim_etr_in . This signal passes through an edge polarity selector (ETP), a prescaler divider (/1, /2, /4, /8 controlled by ETPS[1:0] ), and a digital filter downcounter (controlled by ETF[3:0] ) to generate tim_etrf . A final selection stage, controlled by ECE and SMS[2:0] bits in the TIMx_SMCR register, chooses the clock source for tim_psc_ck from options including Encoder mode, External clock mode 1 ( tim_trgi ), External clock mode 2 ( tim_etrf ), and Internal clock mode ( tim_ker_ck ).

Figure 408: External trigger input block diagram. It shows the signal path from TIM_ETR and TIMx_AF1[17:14] through a multiplexer to tim_etr_in. This signal goes through an edge detector (ETP), a divider (/1, /2, /4, /8 controlled by ETPS[1:0]), and a filter downcounter (controlled by ETF[3:0]) to become tim_etrf. A final multiplexer selects between Encoder mode, External clock mode 1 (tim_trgi), External clock mode 2 (tim_etrf), and Internal clock mode (tim_ker_ck) to produce tim_psc_ck. Control bits ECE and SMS[2:0] from TIMx_SMCR are shown at the bottom right.

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[3:0] bits in the TIMx_AF1 register.
  2. 2. As no filter is needed in this example, write ETF[3:0] = 0000 in the TIMx_SMCR register.
  3. 3. Set the prescaler by writing ETPS[1:0] = 01 in the TIMx_SMCR register.
  4. 4. Select rising edge detection on the tim_etr_in by writing ETP = 0 in the TIMx_SMCR register.
  5. 5. Enable external clock mode 2 by writing ECE = 1 in the TIMx_SMCR register.
  6. 6. Enable the counter by writing CEN = 1 in the TIMx_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 1/4 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 409. Control circuit in external clock mode 2

Timing diagram for Figure 409 showing signals tim_ker_ck, CEN, tim_etr_in, tim_etrp, tim_etr, tim_cnt_ck, tim_psc_ck, and Counter register values 34, 35, 36 over time.

The timing diagram shows the relationship between several signals over time. The top signal, tim_ker_ck , is a periodic square wave. Below it, CEN (Capture/Compare Enable) is a high-level signal. tim_etr_in and tim_etrp are external trigger signals. tim_etr is the internal trigger signal. The bottom two signals, tim_cnt_ck and tim_psc_ck , are the counter and prescaler clock signals, respectively. The Counter register shows values 34, 35, and 36. Vertical dashed lines indicate clock edges. The counter increments from 34 to 35 and then to 36, synchronized with the falling edges of tim_cnt_ck .

Timing diagram for Figure 409 showing signals tim_ker_ck, CEN, tim_etr_in, tim_etrp, tim_etr, tim_cnt_ck, tim_psc_ck, and Counter register values 34, 35, 36 over time.

30.4.6 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 410. Capture/compare channel (example: channel 1 input stage)

Block diagram of the capture/compare channel input stage for channel 1.

The block diagram illustrates the input stage of a capture/compare channel. It starts with TIM_CH1 and tim_ti1_in[15:1] inputs. A multiplexer selects between tim_ti1_in0 and tim_ti1_in[15:1] based on TIMx_TISEL and TI1SEL[3:0] settings. The selected signal passes through a Filter downcounter (controlled by ICF[3:0] and TIMx_CCMR1 ) to produce tim_ti1f . This signal then enters an Edge detector (controlled by CC1P/CC1NP and TIMx_CCER ), which generates tim_ti1f_rising and tim_ti1f_falling signals. These signals are combined into tim_ti1f_ed and sent to the slave mode controller. Another multiplexer selects between tim_ti1f_rising , tim_ti1f_falling , tim_ti2fp1 (from channel 2), and tim_trc (from slave mode controller) based on CC1S[1:0] and ICPS[1:0] settings. The selected signal passes through a Divider (with options /1, /2, /4, /8) controlled by CC1E and TIMx_CCER to produce the final output tim_ic1f .

Block diagram of the capture/compare channel input stage for channel 1.

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 411. Capture/compare channel 1 main circuit

Figure 411: Capture/compare channel 1 main circuit diagram. The diagram shows the internal logic of the capture/compare channel. At the top, an APB Bus connects to an MCU-peripheral interface. Below this, a 16/32-bit Capture/compare preload register and a compare shadow register are shown. A Counter is connected to these registers. On the left, the 'Input mode' logic includes OR gates for CC1S[1] and CC1S[0], and IC1PS and CC1E signals. On the right, the 'Output mode' logic includes OR gates for CC1S[1] and CC1S[0], and OC1PE and UEV (from time base unit) signals. A Comparator block compares CNT > CCR1 and CNT = CCR1 signals. The diagram is labeled MSv63030V1.
Figure 411: Capture/compare channel 1 main circuit diagram. The diagram shows the internal logic of the capture/compare channel. At the top, an APB Bus connects to an MCU-peripheral interface. Below this, a 16/32-bit Capture/compare preload register and a compare shadow register are shown. A Counter is connected to these registers. On the left, the 'Input mode' logic includes OR gates for CC1S[1] and CC1S[0], and IC1PS and CC1E signals. On the right, the 'Output mode' logic includes OR gates for CC1S[1] and CC1S[0], and OC1PE and UEV (from time base unit) signals. A Comparator block compares CNT > CCR1 and CNT = CCR1 signals. The diagram is labeled MSv63030V1.

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

Figure 412: Output stage of capture/compare channel diagram. This diagram details the output stage logic. It starts with TIMx_SMCR (OCCS(1)) and a multiplexer for tim_ocref_clr (inputs: 0 for tim_ocref_clr, 1 for tim_etr). The output of this mux is tim_ocref. Below this, a multiplexer for tim_ocref_clr_int (inputs: 0 for CNT > CCR1, 1 for CNT = CCR1) is shown. The tim_ocref signal goes to an Output mode controller and an Output selector. The Output mode controller also receives tim_oc2ref and OC1CE and OC1M[3:0] from TIMx_CCMR1. The Output selector also receives inputs from the Output mode controller and a '0' input. The output of the Output selector is tim_oc1refc, which goes to the master mode controller and a multiplexer for CC1E (inputs: 0 for '0', 1 for CC1E from TIMx_CCER). The output of this mux is tim_oc1, which goes to an Output enable circuit. The Output enable circuit also receives CC1P from TIMx_CCER and CC1E from TIMx_CCER. The diagram is labeled MSv62374V2.
Figure 412: Output stage of capture/compare channel diagram. This diagram details the output stage logic. It starts with TIMx_SMCR (OCCS(1)) and a multiplexer for tim_ocref_clr (inputs: 0 for tim_ocref_clr, 1 for tim_etr). The output of this mux is tim_ocref. Below this, a multiplexer for tim_ocref_clr_int (inputs: 0 for CNT > CCR1, 1 for CNT = CCR1) is shown. The tim_ocref signal goes to an Output mode controller and an Output selector. The Output mode controller also receives tim_oc2ref and OC1CE and OC1M[3:0] from TIMx_CCMR1. The Output selector also receives inputs from the Output mode controller and a '0' input. The output of the Output selector is tim_oc1refc, which goes to the master mode controller and a multiplexer for CC1E (inputs: 0 for '0', 1 for CC1E from TIMx_CCER). The output of this mux is tim_oc1, which goes to an Output enable circuit. The Output enable circuit also receives CC1P from TIMx_CCER and CC1E from TIMx_CCER. The diagram is labeled MSv62374V2.

1. Available on some instances only. If not available, tim_etr 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.

30.4.7 Input capture mode

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

The following example shows how to capture the counter value in TIMx_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 TIMx_TISEL register.
  2. 2. Select the active input: TIMx_CCR1 must be linked to the tim_ti1 input, so write the CC1S bits to 01 in the TIMx_CCMR1 register. As soon as CC1S becomes different from 00, the channel is configured in input and the TIMx_CCR1 register becomes read-only.
  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 TIMx_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 TIMx_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 TIMx_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 TIMx_CCMR1 register).
  6. 6. Enable capture from the counter into the capture register by setting the CC1E bit in the TIMx_CCER register.
  7. 7. If needed, enable the related interrupt request by setting the CC1IE bit in the TIMx_DIER register, and/or the DMA request by setting the CC1DE bit in the TIMx_DIER register.

When an input capture occurs:

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 TIMx_EGR register.

30.4.8 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 TIMx_TISEL register.
  2. 2. Select the active input for TIMx_CCR1 : write the CC1S bits to 01 in the TIMx_CCMR1 register ( tim_ti1 selected).
  3. 3. Select the active polarity for tim_ti1fp1 (used both for capture in TIMx_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 TIMx_CCR2 : write the CC2S bits to 10 in the TIMx_CCMR1 register ( tim_ti1 selected).
  5. 5. Select the active polarity for tim_ti1fp2 (used for capture in TIMx_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 TIMx_SMCR register ( tim_ti1fp1 selected).
  7. 7. Configure the slave mode controller in reset mode: write the SMS bits to 100 in the TIMx_SMCR register.
  8. 8. Enable the captures: write the CC1E and CC2E bits to 1 in the TIMx_CCER register.

Figure 413. PWM input mode timing

Timing diagram for PWM input mode showing the relationship between the input signal (tim_ti1), the counter (TIMx_CNT), and the capture/compare registers (TIMx_CCR1 and TIMx_CCR2).

The diagram illustrates the timing for PWM input mode. The top signal, tim_ti1 , is a PWM signal. Below it, the TIMx_CNT counter values are shown in a sequence: 0004, 0000, 0001, 0002, 0003, 0004, 0000. The TIMx_CCR1 register is set to 0004, and the TIMx_CCR2 register is set to 0002. Three capture events are indicated by arrows pointing from the tim_ti1 signal to the counter values:

The diagram is labeled with the identifier MSV62325V1 in the bottom right corner.

Timing diagram for PWM input mode showing the relationship between the input signal (tim_ti1), the counter (TIMx_CNT), and the capture/compare registers (TIMx_CCR1 and TIMx_CCR2).
  1. 1. The PWM input mode can be used only with the TIMx_CH1/TIMx_CH2 signals due to the fact that only tim_ti1fp1 and tim_ti2fp2 are connected to the slave mode controller.

30.4.9 Forced output mode

In output mode (CCxS bits = 00 in the TIMx_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 101 in the OCxM bits in the corresponding TIMx_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 100 in the TIMx_CCMRx register.

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

30.4.10 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:

active (OCxM = 001), be set inactive (OCxM = 010) or can toggle (OCxM = 011) on match.

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

In output compare mode, the update event UEV has no effect on 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 TIMx_ARR and TIMx_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 = 0011 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 TIMx_CR1 register.

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

Figure 414. Output compare mode, toggle on tim_oc1

Timing diagram for Figure 414: Output compare mode, toggle on tim_oc1. The diagram shows three horizontal rows: CNT, CCR1, and tim_oc1ref = tim_oc1. CNT (Counter) values shown are 0039, 003A, 003B, followed by a break, then B200, B201. CCR1 (Capture/Compare Register 1) starts at 003A. A match occurs when CNT=003A, causing tim_oc1ref to toggle high. An annotation shows 'Write B201h in the CC1R register' occurring after the first match, which updates CCR1 to B201. A second match occurs when CNT reaches B201, causing tim_oc1ref to toggle low. Arrows point from these match points to the text 'Match detected on CCR1 Interrupt generated if enabled'.
Timing diagram for Figure 414: Output compare mode, toggle on tim_oc1. The diagram shows three horizontal rows: CNT, CCR1, and tim_oc1ref = tim_oc1. CNT (Counter) values shown are 0039, 003A, 003B, followed by a break, then B200, B201. CCR1 (Capture/Compare Register 1) starts at 003A. A match occurs when CNT=003A, causing tim_oc1ref to toggle high. An annotation shows 'Write B201h in the CC1R register' occurring after the first match, which updates CCR1 to B201. A second match occurs when CNT reaches B201, causing tim_oc1ref to toggle low. Arrows point from these match points to the text 'Match detected on CCR1 Interrupt generated if enabled'.

30.4.11 PWM mode

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

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

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

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

In PWM mode (1 or 2), TIMx_CNT and TIMx_CCRx are always compared to determine whether \( TIMx\_CCRx \leq TIMx\_CNT \) or \( TIMx\_CNT \leq TIMx\_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_ocref_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 TIMx_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 TIMx_CNT < TIMx_CCRx else it becomes low. If the compare value in TIMx_CCRx is greater than the autoreload value (in TIMx_ARR) then tim_ocxref is held at 1. If the compare value is 0 then tim_ocxref is held at 0. Figure 415 shows some edge-aligned PWM waveforms in an example where TIMx_ARR = 8.

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

Timing diagram showing edge-aligned PWM waveforms for different compare register (CCR) values. The counter register (CNT) counts from 0 to 8 and then reloads to 0. The diagram shows four cases: CCRx=4, CCRx=8, CCRx>8, and CCRx=0. For each case, the tim_ocxref signal and the CCxIF flag are shown. For CCRx=4, tim_ocxref is high from CNT=0 to CNT=3 and low from CNT=4 to CNT=8. For CCRx=8, tim_ocxref is high from CNT=0 to CNT=7 and low at CNT=8. For CCRx>8, tim_ocxref is always high. For CCRx=0, tim_ocxref is always low.

The figure illustrates the relationship between the counter register (CNT), the compare register (CCR), the output compare reference signal (tim_ocxref), and the compare flag (CCxIF) for edge-aligned PWM mode. The counter register (CNT) is shown as a sequence of values: 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1. Vertical dashed lines indicate the clock edges. The diagram is divided into four sections based on the CCRx value:

MSV62327V1

Timing diagram showing edge-aligned PWM waveforms for different compare register (CCR) values. The counter register (CNT) counts from 0 to 8 and then reloads to 0. The diagram shows four cases: CCRx=4, CCRx=8, CCRx>8, and CCRx=0. For each case, the tim_ocxref signal and the CCxIF flag are shown. For CCRx=4, tim_ocxref is high from CNT=0 to CNT=3 and low from CNT=4 to CNT=8. For CCRx=8, tim_ocxref is high from CNT=0 to CNT=7 and low at CNT=8. For CCRx>8, tim_ocxref is always high. For CCRx=0, tim_ocxref is always low.

Down-counting configuration

In PWM mode 1, the reference signal tim_ocxref is low as long as TIMx_CNT > TIMx_CCRx else it becomes high. If the compare value in TIMx_CCRx is greater than the autoreload value in TIMx_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 TIMx_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 TIMx_CR1 register is updated by hardware and must not be changed by software. Refer to Center-aligned mode (up/down-counting) .

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

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

Timing diagram showing center-aligned PWM waveforms for different CCRx values (4, 7, 8, >8, 0) with ARR=8. The diagram includes counter register values, tim_ocref signals, and CCxIF flag status for CMS=01, 10, and 11.

The figure illustrates the relationship between the counter register values and the resulting PWM waveforms for different capture/compare register (CCR) settings. The counter register values are shown at the top, cycling from 0 to 8 and back down to 0. The 'tim_ocref' signal is shown for various CCRx values: CCRx=4, CCRx=7, CCRx=8, CCRx>8, and CCRx=0. The 'CCxIF' flag status is indicated for different Capture/Compare Mode (CMS) settings (CMS=01, CMS=10, CMS=11). Arrows indicate the points where the counter values match the CCRx values, triggering the flag. The diagram shows that for CCRx=4, the flag is set when the counter counts down from 4 to 3. For CCRx=7, the flag is set when the counter counts down from 7 to 6. For CCRx=8, the flag is set when the counter counts down from 8 to 7. For CCRx>8, the flag is set when the counter counts down from 8 to 7. For CCRx=0, the flag is set when the counter counts down from 1 to 0.

Timing diagram showing center-aligned PWM waveforms for different CCRx values (4, 7, 8, >8, 0) with ARR=8. The diagram includes counter register values, tim_ocref signals, and CCxIF flag status for CMS=01, 10, and 11.

Hints on using center-aligned mode:

in the TIMx_CR1 register. Moreover, the DIR and CMS bits must not be changed at the same time by the software.

Dithering mode

The PWM mode effective resolution can be increased by enabling the dithering mode, using the DITHEN bit in the TIMx_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 417 presents the dithering principle applied to four consecutive PWM cycles.

Figure 417. Dithering principle

Figure 417: Dithering principle diagram showing five PWM waveforms over four consecutive cycles. The top waveform has a duty cycle DC = 7/5. The subsequent waveforms show duty cycles DC = (7+1/4)/5, DC = (7+1/2)/5, DC = (7+3/4)/5, and DC = 8/5. The diagram illustrates how the duty cycle is increased by adding one clock cycle at specific intervals. The average duty cycle is indicated as 7/5. A scale bar shows 7 and 5 clock cycles. A label '1 clock cycle' is present at the bottom. The diagram is labeled MSV45752V1.

The figure shows five PWM waveforms over four consecutive cycles. The top waveform has a duty cycle \( DC = 7/5 \) . The subsequent waveforms show duty cycles \( DC = (7+1/4)/5 \) , \( DC = (7+1/2)/5 \) , \( DC = (7+3/4)/5 \) , and \( DC = 8/5 \) . The diagram illustrates how the duty cycle is increased by adding one clock cycle at specific intervals. The average duty cycle is indicated as \( 7/5 \) . A scale bar shows 7 and 5 clock cycles. A label '1 clock cycle' is present at the bottom. The diagram is labeled MSV45752V1.

Figure 417: Dithering principle diagram showing five PWM waveforms over four consecutive cycles. The top waveform has a duty cycle DC = 7/5. The subsequent waveforms show duty cycles DC = (7+1/4)/5, DC = (7+1/2)/5, DC = (7+3/4)/5, and DC = 8/5. The diagram illustrates how the duty cycle is increased by adding one clock cycle at specific intervals. The average duty cycle is indicated as 7/5. A scale bar shows 7 and 5 clock cycles. A label '1 clock cycle' is present at the bottom. The diagram is labeled MSV45752V1.

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

Note: The ARR and CCR values will be updated automatically if the DITHEN bit is set/reset (for instance, if ARR= 0x05 with DITHEN = 0, it will be updated to ARR = 0x50 with DITHEN = 1).

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

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

Figure 418. Data format and register coding in dithering mode

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

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{MaxResolution}} \]

\[ \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 TIMx_ARR and TIMxCCRy values are limited to 0xFFFFF in dithering mode (corresponds to 65534 for the integer part and 15 for the dithered part). For 32-bit timers, the maximum TIMx_ARR and TIMxCCRy values are limited to 0xFFFFFFFF in dithering mode (corresponds to 264435454 for the integer part and 15 for the dithered part).

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

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

Figure 419: PWM resolution vs frequency (16-bit mode). A graph showing PWM resolution on the y-axis (20-bit and 16-bit) against PWM frequency on the x-axis. Two curves are shown: 'Dithering' and 'No Dithering'. The 'Dithering' curve starts at 20-bit resolution and decreases as frequency increases. The 'No Dithering' curve starts at 16-bit resolution and decreases as frequency increases. A vertical dashed line marks F_pwm_min on the x-axis.

The graph shows PWM resolution on the y-axis with markers for 16-bit and 20-bit. The x-axis represents PWM frequency, with a specific point marked as \( F_{\text{pwm min}} \) . Two curves are plotted: 'Dithering', which starts at 20-bit resolution, and 'No Dithering', which starts at 16-bit resolution. Both curves show a downward trend as frequency increases. A vertical dashed line at \( F_{\text{pwm min}} \) indicates the minimum frequency for the 'No Dithering' mode. The 'Dithering' mode maintains a higher resolution across the frequency range compared to the 'No Dithering' mode. MSV47464V2

Figure 419: PWM resolution vs frequency (16-bit mode). A graph showing PWM resolution on the y-axis (20-bit and 16-bit) against PWM frequency on the x-axis. Two curves are shown: 'Dithering' and 'No Dithering'. The 'Dithering' curve starts at 20-bit resolution and decreases as frequency increases. The 'No Dithering' curve starts at 16-bit resolution and decreases as frequency increases. A vertical dashed line marks F_pwm_min on the x-axis.

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

Figure 420: PWM resolution vs frequency (32-bit mode). A graph showing PWM Resolution on the y-axis (32-bit) against PWM frequency on the x-axis. Two curves are shown: 'No Dithering' and 'Dithering'. The 'No Dithering' curve starts at 32-bit resolution and decreases as frequency increases. The 'Dithering' curve starts at 32-bit resolution and decreases as frequency increases. Two vertical dashed lines mark F(cnt) min No dithering and F(cnt) min with dithering on the x-axis.

The graph shows PWM Resolution on the y-axis with a marker for 32-bit. The x-axis represents PWM frequency, with two specific points marked: \( F(\text{cnt}) \text{ min No dithering} \) and \( F(\text{cnt}) \text{ min with dithering} \) . Two curves are plotted: 'No Dithering', which starts at 32-bit resolution, and 'Dithering', which also starts at 32-bit resolution. Both curves show a downward trend as frequency increases. The 'Dithering' mode allows for a higher minimum frequency while maintaining the 32-bit resolution compared to the 'No Dithering' mode. MSV50912V1

Figure 420: PWM resolution vs frequency (32-bit mode). A graph showing PWM Resolution on the y-axis (32-bit) against PWM frequency on the x-axis. Two curves are shown: 'No Dithering' and 'Dithering'. The 'No Dithering' curve starts at 32-bit resolution and decreases as frequency increases. The 'Dithering' curve starts at 32-bit resolution and decreases as frequency increases. Two vertical dashed lines mark F(cnt) min No dithering and F(cnt) min with dithering on the x-axis.

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

Figure 421. PWM dithering pattern

Figure 421. PWM dithering pattern. A diagram showing the relationship between Counter period, CCR1-4 values, ARR value, and Auto-Reload value over 16 counter periods. The Counter period increases linearly from 1 to 16. CCR1 value is constant at 322. Compare1 value alternates between 21 and 20. CCR2 value is constant at 326. Compare2 value alternates between 21 and 20. CCR3 value is constant at 334. Compare3 value alternates between 21 and 20. CCR4 value is constant at 336. Compare4 value alternates between 21 and 20. ARR value is constant at 643. Auto-Reload value alternates between 41 and 40.
Parameter12345678910111213141516
Counter period12345678910111213141516
CCR1 value322
Compare1 value21202020202020202120202020202020
CCR2 value326
Compare2 value21202120212020202120212021202020
CCR3 value334
Compare3 value21212121212121202121212121212120
CCR4 value336
Compare4 value21212121212121212121212121212121
ARR value643
Auto-Reload value41404040414040404140404040404040

MSV45755V1

Figure 421. PWM dithering pattern. A diagram showing the relationship between Counter period, CCR1-4 values, ARR value, and Auto-Reload value over 16 counter periods. The Counter period increases linearly from 1 to 16. CCR1 value is constant at 322. Compare1 value alternates between 21 and 20. CCR2 value is constant at 326. Compare2 value alternates between 21 and 20. CCR3 value is constant at 334. Compare3 value alternates between 21 and 20. CCR4 value is constant at 336. Compare4 value alternates between 21 and 20. ARR value is constant at 643. Auto-Reload value alternates between 41 and 40.

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

Table 294. 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 TIMx_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 422.

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

Figure 422 shows three diagrams illustrating the dithering effect on duty cycle in center-aligned PWM mode. The first diagram, labeled 'No dithering', shows a standard PWM signal with a constant duty cycle. The second diagram, labeled 'Dithering up', shows the PWM signal with a higher duty cycle during the up-counting phase and a lower duty cycle during the down-counting phase. The third diagram, labeled 'Dithering down', shows the PWM signal with a lower duty cycle during the up-counting phase and a higher duty cycle during the down-counting phase. The diagrams illustrate how the dithering pattern affects the overall duty cycle over eight consecutive PWM periods.

MSv50904V1

Figure 422 shows three diagrams illustrating the dithering effect on duty cycle in center-aligned PWM mode. The first diagram, labeled 'No dithering', shows a standard PWM signal with a constant duty cycle. The second diagram, labeled 'Dithering up', shows the PWM signal with a higher duty cycle during the up-counting phase and a lower duty cycle during the down-counting phase. The third diagram, labeled 'Dithering down', shows the PWM signal with a lower duty cycle during the up-counting phase and a higher duty cycle during the down-counting phase. The diagrams illustrate how the dithering pattern affects the overall duty cycle over eight consecutive PWM periods.

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

Table 295. 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-

30.4.12 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 TIMx_ARR register, the duty cycle and the phase-shift are determined by a pair of TIMx_CCRx registers. One register controls the PWM during up-counting, the second during down-counting, so that PWM is adjusted every half PWM cycle:

Asymmetric PWM mode can be selected independently on two channels (one tim_ocx output per pair of CCR registers) by writing 1110 (Asymmetric PWM mode 1) or 1111 (Asymmetric PWM mode 2) in the OCxM bits in the TIMx_CCMRx register.

Note: The OCxM[3:0] bitfield is split into two parts for compatibility reasons, the most significant bit is 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 tim_oc2ref signal on channel 2, or an tim_oc2refc signal resulting from asymmetric PWM mode 2.

Figure 423 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 423. Generation of two phase-shifted PWM signals with 50% duty cycle

Timing diagram showing two phase-shifted PWM signals (tim_oc1refc and tim_oc3refc) generated using asymmetric PWM mode 2. The counter register values are shown at the top, ranging from 0 to 8, then 7 down to 0, and then 1. The signals are generated based on the counter values and the CCR registers (CCR1=0, CCR2=8 for tim_oc1refc; CCR3=3, CCR4=5 for tim_oc3refc).

The diagram illustrates the timing for two PWM signals, tim_oc1refc and tim_oc3refc , generated using asymmetric PWM mode 2. The top row shows the 'Counter register' values over time: 0, 1, 2, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1. Vertical dashed lines mark the comparison points for the PWM signals. For tim_oc1refc (CCR1=0, CCR2=8), the signal is high from counter value 0 to 8 and low from 8 back to 0. For tim_oc3refc (CCR3=3, CCR4=5), the signal is high from counter value 3 to 5 and low from 5 back to 3. Both signals have a 50% duty cycle. The diagram is labeled MSV62329V1.

Timing diagram showing two phase-shifted PWM signals (tim_oc1refc and tim_oc3refc) generated using asymmetric PWM mode 2. The counter register values are shown at the top, ranging from 0 to 8, then 7 down to 0, and then 1. The signals are generated based on the counter values and the CCR registers (CCR1=0, CCR2=8 for tim_oc1refc; CCR3=3, CCR4=5 for tim_oc3refc).

30.4.13 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 TIMx_ARR register, the duty cycle and delay are determined by the two TIMx_CCRx registers. The resulting signals, tim_ocxrefc , are made of an OR or AND logical combination of two reference PWMs:

Combined PWM mode can be selected independently on two channels (one tim_ocx output per pair of CCR registers) by writing 1100 (Combined PWM mode 1) or 1101 (Combined PWM mode 2) in the OCxM bits in the TIMx_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 Combined PWM mode 1 and the other in Combined PWM mode 2).

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

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

Figure 424. Combined PWM mode on channels 1 and 3

Timing diagrams for combined PWM mode on channels 1 and 3. The top diagram shows the AND combination of tim_oc1ref and tim_oc2ref to produce tim_oc1refc. The bottom diagram shows the OR combination of tim1_oc1ref and tim1_oc2ref to produce tim1_oc1refc. Both diagrams include waveforms for CCR1, CCR2, the individual reference signals, and the combined reference signal, with a sawtooth timer waveform at the top.

Timing diagram illustrating Combined PWM mode on channels 1 and 3. The top diagram shows the relationship between CCR2, CCR1, tim_oc1ref, tim_oc2ref, and tim_oc1refc. The bottom diagram shows the relationship between CCR2, CCR1, tim_oc1ref, tim_oc2ref, and tim1_oc1refc. Both diagrams include a sawtooth waveform representing the timer count.

tim_oc1refc = tim_oc1ref AND tim_oc2ref

tim1_oc1refc = tim1_oc1ref OR tim1_oc2ref

MSv62330V1

Timing diagrams for combined PWM mode on channels 1 and 3. The top diagram shows the AND combination of tim_oc1ref and tim_oc2ref to produce tim_oc1refc. The bottom diagram shows the OR combination of tim1_oc1ref and tim1_oc2ref to produce tim1_oc1refc. Both diagrams include waveforms for CCR1, CCR2, the individual reference signals, and the combined reference signal, with a sawtooth timer waveform at the top.

30.4.14 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 TIMx_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 does not work in Forced mode.

The tim_ocref_clr_int source depends on the OCREF clear selection feature implementation, refer to Section 30.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 TIMx_SMCR register. The tim_ocref_clr input can be selected among several tim_ocref_clr[7:0] inputs, using the OCRSEL[2:0] bitfield in the TIMx_AF2 register, as shown in Figure 425 .

Figure 425. OCREF_CLR input selection multiplexer

Figure 425: OCREF_CLR input selection multiplexer diagram. The diagram shows two multiplexers. The first multiplexer has inputs tim_ocref_clr0 through tim_ocref_clr7 and is controlled by the TIMx_AF2 register's OCRSEL[2:0] bits. Its output is tim_ocref_clr. The second multiplexer has inputs tim_ocref_clr and tim_etrf and is controlled by the TIMx_SMCR register's OCCS bits. Its output is tim_ocref_clr_int. The diagram is labeled MSv62341V2.
Figure 425: OCREF_CLR input selection multiplexer diagram. The diagram shows two multiplexers. The first multiplexer has inputs tim_ocref_clr0 through tim_ocref_clr7 and is controlled by the TIMx_AF2 register's OCRSEL[2:0] bits. Its output is tim_ocref_clr. The second multiplexer has inputs tim_ocref_clr and tim_etrf and is controlled by the TIMx_SMCR register's OCCS bits. Its output is tim_ocref_clr_int. The diagram is labeled MSv62341V2.

If the OCREF clear selection feature is not implemented, the tim_ocref_clr_int input is directly connected to the tim_etrf 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 TIMx_SMCR register are cleared to 00.
  2. 2. The external clock mode 2 must be disabled: bit ECE in the TIM1_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 426 shows the behavior of the tim_ocref signal when the tim_etrf input becomes high, for both values of the OCxCE enable bit. In this example, the timer TIMx is programmed in PWM mode.

Figure 426. Clearing TIMx tim_ocref

Figure 426: Timing diagram showing the behavior of the tim_ocref signal. The top waveform is the Counter (CNT) output, which is a sawtooth wave. The second waveform is the tim_etrf input, which is a pulse. The third waveform is the tim_ocref signal when OCxCE = '0'. The fourth waveform is the tim_ocref signal when OCxCE = '1'. Arrows indicate that when tim_etrf goes high, the tim_ocref signal becomes high if OCxCE = '0', and remains high if OCxCE = '1'. The diagram is labeled MSv62342V1.
Figure 426: Timing diagram showing the behavior of the tim_ocref signal. The top waveform is the Counter (CNT) output, which is a sawtooth wave. The second waveform is the tim_etrf input, which is a pulse. The third waveform is the tim_ocref signal when OCxCE = '0'. The fourth waveform is the tim_ocref signal when OCxCE = '1'. Arrows indicate that when tim_etrf goes high, the tim_ocref signal becomes high if OCxCE = '0', and remains high if OCxCE = '1'. The diagram is labeled MSv62342V1.

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

30.4.15 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 TIMx_CR1 register. This makes the counter stop automatically at the next update event UEV.

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

\( CNT < CCRx \leq ARR \) (in particular, \( 0 < CCRx \) ).

Figure 427. Example of One-pulse mode

Timing diagram for One-pulse mode showing the relationship between input signals, output signals, and the counter value over time.

The diagram illustrates the timing for One-pulse mode. It features four horizontal signal lines at the top: tim_ti2 (trigger input), tim_oc1ref (output compare reference), tim_oc1 (output compare 1), and a Counter value graph. The tim_ti2 signal shows a single positive pulse. The tim_oc1ref signal transitions from low to high when the counter reaches TIMx_CCR1 and stays high until the counter reaches TIMx_ARR . The tim_oc1 signal is shown as a pulse that is high during the delay and low during the pulse duration. The Counter graph shows a staircase-like increase from 0 to TIMx_ARR . The time interval from the rising edge of tim_ti2 to the start of the counter increase is labeled t DELAY . The duration of the counter increase until it reaches TIMx_ARR is labeled t PULSE . The diagram is labeled with 'MSV62344V1' in the bottom right corner.

Timing diagram for One-pulse mode showing the relationship between input signals, output signals, and the counter value over time.

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 TIMx_TISEL register.
  2. 2. Map tim_ti2fp2 on tim_ti2 by writing CC2S = 01 in the TIMx_CCMR1 register.
  3. 3. tim_ti2fp2 must detect a rising edge, write CC2P = 0 and CC2NP = 0 in the TIMx_CCER register.
  4. 4. Configure tim_ti2fp2 as trigger for the slave mode controller ( tim_trgi ) by writing TS = 00110 in the TIMx_SMCR register.
  5. 5. tim_ti2fp2 is used to start the counter by writing SMS to 110 in the TIMx_SMCR register (trigger mode).

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

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

Since only one pulse (Single mode) is needed, a one must be written in the OPM bit in the TIMx_CR1 register to stop the counter at the next update event (when the counter rolls over from the autoreload value back to 0). When OPM bit in the TIMx_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_{\text{DELAY min}} \) we can get.

If one wants to output a waveform with the minimum delay, the OCxFE bit can be set in the TIMx_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.

30.4.16 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 30.4.15 :

The timer must be in Slave mode, with the bits SMS[3:0] = 1000 (Combined Reset + trigger mode) in the TIMx_SMCR register, and the OCxM[3:0] bits set to 1000 or 1001 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[3:0] and SMS[3: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 TIMx_CR1.

Figure 428. Retriggerable one-pulse mode

Timing diagram for Retriggerable one-pulse mode showing three waveforms: tim_trgi, Counter, and tim_ocx. The diagram illustrates how the output pulse (tim_ocx) is generated and retriggered based on the trigger signal (tim_trgi) and the counter value.

The figure is a timing diagram with three horizontal axes. The top axis, labeled 'tim_trgi', shows three positive pulses. The middle axis, labeled 'Counter', shows a sawtooth waveform representing the timer's counter value. The counter increases linearly from a minimum value to a maximum value (determined by the ARR register) and then resets to the minimum. The bottom axis, labeled 'tim_ocx', shows the output pulse. The output pulse is initially high when the counter starts at its minimum value. It remains high until the counter reaches a value specified in the CCRx register, at which point it goes low. The output pulse is retriggered (goes high again) whenever a new rising edge is detected on the tim_trgi signal while the counter is still counting up. The diagram shows two such retriggering events. Vertical dashed lines indicate the timing relationships between the trigger pulses, the counter's start and reset points, and the output pulse transitions. A small label 'MSv62345V2' is visible in the bottom right corner of the diagram area.

Timing diagram for Retriggerable one-pulse mode showing three waveforms: tim_trgi, Counter, and tim_ocx. The diagram illustrates how the output pulse (tim_ocx) is generated and retriggered based on the trigger signal (tim_trgi) and the counter value.

30.4.17 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 429 .

Figure 429. Pulse generator circuitry

Schematic diagram of the pulse generator circuitry. It shows two 'Enable' blocks receiving 'CCR3 match' and 'CCR4 match' signals, controlled by OC3M = 1010 and OC4M = 1010 respectively. These enable signals are OR-ed and fed into a 'Pulse generator' block. The pulse generator also receives 'PWPRSC [2:0]' and 'PW[7:0]' inputs. The output of the pulse generator is connected to two 'R/S' flip-flops. The top flip-flop's 'Set' input is connected to the pulse generator output, and its 'Reset' input is connected to a reset signal. Its 'Q' output is AND-ed with another signal to produce 'tim_oc3'. The bottom flip-flop has its 'Set' input connected to the pulse generator output and its 'Reset' input connected to a reset signal. Its 'Q' output is AND-ed to produce 'tim_oc4'. A small schematic symbol for a reset signal is shown next to the top flip-flop. The diagram is labeled MSv62346V1.
Schematic diagram of the pulse generator circuitry. It shows two 'Enable' blocks receiving 'CCR3 match' and 'CCR4 match' signals, controlled by OC3M = 1010 and OC4M = 1010 respectively. These enable signals are OR-ed and fed into a 'Pulse generator' block. The pulse generator also receives 'PWPRSC [2:0]' and 'PW[7:0]' inputs. The output of the pulse generator is connected to two 'R/S' flip-flops. The top flip-flop's 'Set' input is connected to the pulse generator output, and its 'Reset' input is connected to a reset signal. Its 'Q' output is AND-ed with another signal to produce 'tim_oc3'. The bottom flip-flop has its 'Set' input connected to the pulse generator output and its 'Reset' input connected to a reset signal. Its 'Q' output is AND-ed to produce 'tim_oc4'. A small schematic symbol for a reset signal is shown next to the top flip-flop. The diagram is labeled MSv62346V1.

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

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

Timing diagrams for pulse generation. The top diagram shows 'Counter' (a sawtooth wave), 'CMP3' (a constant reference level), 'Triggers' (edges from the counter), and 'tim_ocx' (output pulses). It illustrates an 'Extended pulsewidth due to re-trigger' when a new trigger occurs while the output is still high. The bottom diagram shows the same signals but with a different counter/CMP3 relationship, resulting in shorter pulses. The diagram is labeled MSv62347V1.
Timing diagrams for pulse generation. The top diagram shows 'Counter' (a sawtooth wave), 'CMP3' (a constant reference level), 'Triggers' (edges from the counter), and 'tim_ocx' (output pulses). It illustrates an 'Extended pulsewidth due to re-trigger' when a new trigger occurs while the output is still high. The bottom diagram shows the same signals but with a different counter/CMP3 relationship, resulting in shorter pulses. The diagram is labeled MSv62347V1.

This output compare mode is selected using the OC3M[3:0] and OC4M[3:0] bitfields in TIMx_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 431).

Figure 431. Extended pulse width in case of concurrent triggers

Timing diagram showing two trigger signals, Trigger CMP3 and Trigger CMP4, and two output signals, tim_oc3 and tim_oc4. Trigger CMP3 is a periodic signal with falling edges. Trigger CMP4 is a periodic signal with falling edges. tim_oc3 is a pulse-width modulated signal that is retriggerable. tim_oc4 is a pulse-width modulated signal that is not retriggerable. The diagram shows that when Trigger CMP4 falls while tim_oc3 is still high, the pulse width of tim_oc3 is extended. The extended pulse width is indicated by a dashed line and a label 'Extended pulsedwidth due to overlapping CMP4 trigger'. The diagram is labeled MSV62348V1.
Timing diagram showing two trigger signals, Trigger CMP3 and Trigger CMP4, and two output signals, tim_oc3 and tim_oc4. Trigger CMP3 is a periodic signal with falling edges. Trigger CMP4 is a periodic signal with falling edges. tim_oc3 is a pulse-width modulated signal that is retriggerable. tim_oc4 is a pulse-width modulated signal that is not retriggerable. The diagram shows that when Trigger CMP4 falls while tim_oc3 is still high, the pulse width of tim_oc3 is extended. The extended pulse width is indicated by a dashed line and a label 'Extended pulsedwidth due to overlapping CMP4 trigger'. The diagram is labeled MSV62348V1.

30.4.18 Encoder interface mode

Quadrature encoder

To select Encoder interface mode write SMS = 0001 in the TIMx_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 TIMx_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 296 . 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 TIMx_CR1 register written to 1). The sequence of transitions of the two inputs is evaluated and generates count pulses as well as the direction signal. Depending on the sequence the counter counts up or down, the DIR bit in the TIMx_CR1 register is modified by hardware accordingly. The DIR bit is calculated at each transition on any input (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 TIMx_ARR register (0 to ARR or ARR down to 0 depending on the direction). So the TIMx_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 296. Counting direction versus encoder signals(CC1P = CC2P = 0)

Active edgeSMS[3: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 mode1110HighDownUpNo countNo count
LowNo countNo countNo countNo count
Counting on tim_ti2 only x1 mode1111HighNo countNo countUpDown
LowNo countNo countNo countNo count
Counting on tim_ti1 only x2 mode0001HighDownUpNo countNo count
LowUpDownNo countDown
Counting on tim_ti2 only x2 mode0010HighNo countNo countUpDown
LowNo countNo countDownUp
Counting on tim_ti1 and tim_ti2 x4 mode0011HighDownUpUpDown
LowUpDownDownUp

A quadrature encoder can be connected directly to the MCU without external interface logic. However, comparators are normally 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 432 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 432. Example of counter operation in encoder interface mode

Timing diagram for Figure 432 showing counter operation in encoder interface mode. The diagram displays three waveforms over time: tim_ti1, tim_ti2, and Counter. The tim_ti1 and tim_ti2 signals are square waves. The Counter waveform is a staircase-like signal that increases (up) during the 'forward' phase and decreases (down) during the 'backward' phase. The phases are labeled: forward, jitter, backward, jitter, forward. The counter value increases in the first 'forward' phase, decreases in the 'backward' phase, and increases again in the final 'forward' phase. The 'jitter' phases show rapid, irregular changes in the input signals. The diagram is labeled MSv62349V1.
Timing diagram for Figure 432 showing counter operation in encoder interface mode. The diagram displays three waveforms over time: tim_ti1, tim_ti2, and Counter. The tim_ti1 and tim_ti2 signals are square waves. The Counter waveform is a staircase-like signal that increases (up) during the 'forward' phase and decreases (down) during the 'backward' phase. The phases are labeled: forward, jitter, backward, jitter, forward. The counter value increases in the first 'forward' phase, decreases in the 'backward' phase, and increases again in the final 'forward' phase. The 'jitter' phases show rapid, irregular changes in the input signals. The diagram is labeled MSv62349V1.

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

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

Timing diagram for Figure 433 showing counter operation in encoder interface mode with tim_ti1fp1 polarity inverted. The diagram displays three waveforms over time: tim_ti1, tim_ti2, and Counter. The tim_ti1 and tim_ti2 signals are square waves. The Counter waveform is a staircase-like signal that decreases (down) during the 'forward' phase and increases (up) during the 'backward' phase. The phases are labeled: forward, jitter, backward, jitter, forward. The counter value decreases in the first 'forward' phase, increases in the 'backward' phase, and decreases again in the final 'forward' phase. The 'jitter' phases show rapid, irregular changes in the input signals. The diagram is labeled MSv62350V1.
Timing diagram for Figure 433 showing counter operation in encoder interface mode with tim_ti1fp1 polarity inverted. The diagram displays three waveforms over time: tim_ti1, tim_ti2, and Counter. The tim_ti1 and tim_ti2 signals are square waves. The Counter waveform is a staircase-like signal that decreases (down) during the 'forward' phase and increases (up) during the 'backward' phase. The phases are labeled: forward, jitter, backward, jitter, forward. The counter value decreases in the first 'forward' phase, increases in the 'backward' phase, and decreases again in the final 'forward' phase. The 'jitter' phases show rapid, irregular changes in the input signals. The diagram is labeled MSv62350V1.

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

Figure 434. Quadrature encoder counting modes

Timing diagram showing quadrature encoder counting modes (x1, x2, x4) for a speed reversal. The diagram displays four waveforms: tim_ti1 (square wave), tim_ti2 (square wave), DIR bit (logic level), and three counter values (Counter x4, Counter x2, Counter x1). The counters show values increasing then decreasing as the direction bit changes.

The figure is a timing diagram illustrating quadrature encoder counting modes during a speed reversal. It consists of four horizontal waveforms:

Vertical dashed lines indicate the timing relationship between the encoder signals and the counter updates. The reversal occurs when the DIR bit changes state. The diagram is labeled MSV62351V1 in the bottom right corner.

Timing diagram showing quadrature encoder counting modes (x1, x2, x4) for a speed reversal. The diagram displays four waveforms: tim_ti1 (square wave), tim_ti2 (square wave), DIR bit (logic level), and three counter values (Counter x4, Counter x2, Counter x1). The counters show values increasing then decreasing as the direction bit changes.

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 TIMx_CR1 register forces a continuous copy of the update interrupt flag (UIF) into the timer counter register's bit 31 (TIMxCNT[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 435, 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[3:0] bitfield in the TIMx_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 435. Direction plus clock encoder mode

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

The diagram illustrates the relationship between two input signals, tim_ti1 and tim_ti2, and the resulting counter values in two different modes. tim_ti1 is a direction signal that is high for the first half of the diagram and low for the second half. tim_ti2 is a clock signal that toggles between high and low. The 'Counter x2 mode' shows the counter values (6, 7, 8, 9, 10, 11, 10, 9, 8, 7, 6) which increase while tim_ti1 is high and decrease while tim_ti1 is low, updating on both rising and falling edges of tim_ti2. The 'Counter x1 mode' shows the counter values (6, 7, 8, 9, 8, 7) which also increase while tim_ti1 is high and decrease while tim_ti1 is low, but only update on the rising edges of tim_ti2. A vertical dashed line separates the high and low phases of tim_ti1. The identifier MSv62352V1 is present in the bottom right corner.

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

Directional clock encoder mode

In the “directional clock” mode on Figure 436 , 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[3:0] bitfield in the TIMx_SMCR register, as following:

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

Timing diagram for Figure 436 showing tim_ti1, tim_ti2, DIR bit, Counter x2 mode, and Counter x1 mode waveforms and counter values.

This timing diagram illustrates the directional clock encoder mode for CC1P = CC2P = 0. It shows five horizontal timelines over time. The first timeline, 'tim_ti1', shows a series of pulses. The second timeline, 'tim_ti2', shows a square wave. The third timeline, 'DIR bit', is a single bit signal that goes high when the counter is counting up and low when it is counting down. The fourth timeline, 'Counter x2 mode', shows the counter values: 6, 7, 8, 9, 10, 11, 10, 9, 8, 7, 6, 5. The fifth timeline, 'Counter x1 mode', shows the counter values: 6, 7, 8, 7, 6, 5. Vertical dashed lines indicate the clock edges used for counting. The counter counts up when tim_ti1 is high and tim_ti2 is falling, and counts down when tim_ti1 is low and tim_ti2 is falling. The DIR bit is set to 1 for counting up and 0 for counting down.

MSV62353V1

Timing diagram for Figure 436 showing tim_ti1, tim_ti2, DIR bit, Counter x2 mode, and Counter x1 mode waveforms and counter values.

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

Timing diagram for Figure 437 showing tim_ti1, tim_ti2, DIR bit, Counter x2 mode, and Counter x1 mode waveforms and counter values.

This timing diagram illustrates the directional clock encoder mode for CC1P = CC2P = 1. It shows five horizontal timelines over time. The first timeline, 'tim_ti1', shows a series of pulses. The second timeline, 'tim_ti2', shows a square wave. The third timeline, 'DIR bit', is a single bit signal that goes high when the counter is counting up and low when it is counting down. The fourth timeline, 'Counter x2 mode', shows the counter values: 6, 7, 8, 9, 10, 11, 10, 9, 8, 7, 6, 5. The fifth timeline, 'Counter x1 mode', shows the counter values: 7, 8, 9, 8, 7, 6. Vertical dashed lines indicate the clock edges used for counting. The counter counts up when tim_ti1 is low and tim_ti2 is falling, and counts down when tim_ti1 is high and tim_ti2 is falling. The DIR bit is set to 1 for counting up and 0 for counting down.

MSV62354V1

Timing diagram for Figure 437 showing tim_ti1, tim_ti2, DIR bit, Counter x2 mode, and Counter x1 mode waveforms and counter values.

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

Table 297. Counting direction versus encoder signals and polarity settings

Directional clock modeSMS[3: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
1100HighDownDownUpUp
LowNo countNo countNo countNo count
x2 mode
CCxP = 1
1100HighNo countNo countNo countNo count
LowDownDownUpUp
x1 mode
CCxP = 0
1101HighNo countDownNo countUp
LowNo countNo countNo countNo count
x1 mode
CCxP = 1
1101HighNo 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 TIMx_ECR register. The IE bit must be set only in encoder mode, when the SMS[3:0] bitfield has the following values: 0001, 0010, 011, 1010, 1011, 1100, 1101, 1110, 1111.

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

Figure 438. Index gating options

Timing diagram showing five signal traces: Channel A, Channel B, Gated A & B, Gated A, and Ungated. Channel A and B are square waves. Channel B has a rising edge marked with an arrow. Gated A & B, Gated A, and Ungated show pulses that are active only when Channel B is high. The diagram is labeled MSv45765V1.
Timing diagram showing five signal traces: Channel A, Channel B, Gated A & B, Gated A, and Ungated. Channel A and B are square waves. Channel B has a rising edge marked with an arrow. Gated A & B, Gated A, and Ungated show pulses that are active only when Channel B is high. The diagram is labeled MSv45765V1.

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

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 439. Jittered Index signals

Timing diagram showing five signal traces: Channel A, Channel B, Gated A & B, Gated A, and Ungated. Channel A and B are square waves. Channel B has a rising edge marked with an arrow. Gated A & B, Gated A, and Ungated show pulses with jittered edges. The Ungated trace has two shaded regions. A double-headed arrow at the bottom indicates the 'Max pulsewidth ungated mode'. The diagram is labeled MSv45766V1.
Timing diagram showing five signal traces: Channel A, Channel B, Gated A & B, Gated A, and Ungated. Channel A and B are square waves. Channel B has a rising edge marked with an arrow. Gated A & B, Gated A, and Ungated show pulses with jittered edges. The Ungated trace has two shaded regions. A double-headed arrow at the bottom indicates the 'Max pulsewidth ungated mode'. The diagram is labeled MSv45766V1.

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 TIMx_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 440 shows at which position is the index generated, for a simplistic example (an encoder providing four edges per mechanical rotation).

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

Figure 440: State transition diagram for index generation. It shows four states: State 1 (AB = 00), State 2 (AB = 01), State 3 (AB = 11), and State 4 (AB = 10). Transitions are labeled with rotor angles: 0°, 90°, 180°, and 270°. Up-counting and down-counting directions are indicated. An arrow points to the transition from State 3 to State 4, labeled 'The index event is always generated here'.

The diagram illustrates the state transitions for an encoder with IPOS[1:0] = 11. The states are defined by the AB bits: State 1 (00), State 2 (01), State 3 (11), and State 4 (10). Transitions occur based on rotor angle changes: 0° (State 1 to State 2), 90° (State 2 to State 3), 180° (State 3 to State 4), and 270° (State 4 to State 1). The index event is generated during the transition from State 3 to State 4. Up-counting and down-counting directions are shown in the center of the cycle.

Figure 440: State transition diagram for index generation. It shows four states: State 1 (AB = 00), State 2 (AB = 01), State 3 (AB = 11), and State 4 (AB = 10). Transitions are labeled with rotor angles: 0°, 90°, 180°, and 270°. Up-counting and down-counting directions are indicated. An arrow points to the transition from State 3 to State 4, labeled 'The index event is always generated here'.

Figure 441 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 441. Counter reading with index gated on channel A (IPOS[1:0] = 11)

Figure 441: Timing diagram showing Channel A, Channel B, Index, DIR bit, and Counter waveforms. The counter sequence is 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1. Arrows indicate index events at specific transitions.

The timing diagram shows the relationship between Channel A, Channel B, Index, DIR bit, and the Counter. The counter sequence is: 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1. The counter is reset to 0 when the encoder state is 11 (ChA=1, ChB=1) during up-counting (DIR=0). The index event is generated when the encoder state transitions from 11 to 10 (State 3 to State 4) during up-counting. Arrows indicate the specific transitions where the index event is generated.

Figure 441: Timing diagram showing Channel A, Channel B, Index, DIR bit, and Counter waveforms. The counter sequence is 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1. Arrows indicate index events at specific transitions.

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

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

Timing diagram for Figure 442 showing counter reading with index ungated. The diagram displays five waveforms: Channel A, Channel B, Index, DIR bit, and Counter. Channel A and B are square waves. The Index signal is a pulse that goes high when Channel A is high and Channel B is low. The DIR bit is a constant high level. The Counter shows a sequence of values: 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7. Arrows indicate the transitions of Channel A and B that correspond to the counter increments and decrements. The diagram is labeled MSv45769V1.
Timing diagram for Figure 442 showing counter reading with index ungated. The diagram displays five waveforms: Channel A, Channel B, Index, DIR bit, and Counter. Channel A and B are square waves. The Index signal is a pulse that goes high when Channel A is high and Channel B is low. The DIR bit is a constant high level. The Counter shows a sequence of values: 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7. Arrows indicate the transitions of Channel A and B that correspond to the counter increments and decrements. The diagram is labeled MSv45769V1.

Figure 443 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 443. Counter reading with index gated on channel A and B

Timing diagram for Figure 443 showing counter reading with index gated on channel A and B. The diagram displays five waveforms: Channel A, Channel B, Index, DIR bit, and Counter. Channel A and B are square waves. The Index signal is gated by Channel A and B, going high only when both are high. The DIR bit is a constant high level. The Counter shows a sequence of values: 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1. Arrows indicate the transitions of Channel A and B that correspond to the counter increments and decrements. The diagram is labeled MSv45770V1.
Timing diagram for Figure 443 showing counter reading with index gated on channel A and B. The diagram displays five waveforms: Channel A, Channel B, Index, DIR bit, and Counter. Channel A and B are square waves. The Index signal is gated by Channel A and B, going high only when both are high. The DIR bit is a constant high level. The Counter shows a sequence of values: 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1. Arrows indicate the transitions of Channel A and B that correspond to the counter increments and decrements. The diagram is labeled MSv45770V1.

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

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

Timing diagrams showing encoder mode behavior with a narrow index pulse. Two scenarios are shown: 'Index leading state transition' and 'Index delayed versus state transition'. Each shows Channel A, Channel B, Index, DIR bit, and Counter signals. The counter increments from 5 to 7, resets to 0 on the index pulse, continues to 6, then the DIR bit changes and it decrements back through 0 to 1.

Index leading state transition


Index delayed versus state transition

MSv45771V1

Timing diagrams showing encoder mode behavior with a narrow index pulse. Two scenarios are shown: 'Index leading state transition' and 'Index delayed versus state transition'. Each shows Channel A, Channel B, Index, DIR bit, and Counter signals. The counter increments from 5 to 7, resets to 0 on the index pulse, continues to 6, then the DIR bit changes and it decrements back through 0 to 1.

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

Timing diagram showing two instances of counter reset by a narrow index pulse. The diagram includes waveforms for Channel A, Channel B, Index, DIR bit, and Counter. In the first instance, the counter counts 5, 6, 7, then resets to 0 upon a narrow Index pulse. In the second instance, it counts 4, 5, 6, then resets to 0. Channel A and B show typical PWM-like signals. The DIR bit remains constant throughout.

The figure displays two timing diagrams illustrating the counter reset behavior triggered by a narrow index pulse. Both diagrams show the following signals over time:

Vertical dashed lines indicate the timing relationships between the signals. The counter reset occurs immediately upon the rising edge of the narrow Index pulse. The identifier MSv45772V1 is present in the bottom right corner of the diagram area.

Timing diagram showing two instances of counter reset by a narrow index pulse. The diagram includes waveforms for Channel A, Channel B, Index, DIR bit, and Counter. In the first instance, the counter counts 5, 6, 7, then resets to 0 upon a narrow Index pulse. In the second instance, it counts 4, 5, 6, then resets to 0. Channel A and B show typical PWM-like signals. The DIR bit remains constant throughout.

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

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

Timing diagram showing the relationship between Channel A, Channel B, Index, DIR bit, Counter x2, and Counter x1. 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 high when Channel A is high and Channel B is low. The DIR bit is high when the Index signal is high. Counter x2 counts from 10 to 8, and Counter x1 counts from 5 to 3. Arrows indicate the relationship between the signals and the counter values.

The diagram shows the following signals and their relationship:

Arrows indicate that the Index signal triggers a reset for both counters. The DIR bit is set when the Index signal is high.

Timing diagram showing the relationship between Channel A, Channel B, Index, DIR bit, Counter x2, and Counter x1. 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 high when Channel A is high and Channel B is low. The DIR bit is high when the Index signal is high. Counter x2 counts from 10 to 8, and Counter x1 counts from 5 to 3. Arrows indicate the relationship between the signals and the counter values.

Directional index sensitivity

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

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

Note:

Figure 447. Directional index sensitivity

Timing diagram for Figure 447 showing DIR bit, Counter, Index input, and Counter reset signals for different IDIR[1:0] settings.

Figure 447 is a timing diagram illustrating the directional index sensitivity of a timer. The diagram is divided into two main sections: UP-counting and Down-counting, controlled by the DIR bit. The signals shown are:

MSv45774V1

Timing diagram for Figure 447 showing DIR bit, Counter, Index input, and Counter reset signals for different IDIR[1:0] settings.

Special first index event management

The FIDX bit in the TIMx_ECR register allows the index to be taken only once, as shown on Figure 448. 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.

Note: When FIDX = 1, the index can be issued twice (IDXF flag set) if the direction changes at position 0 (index active).

Figure 448. Counter reset as function of FIDX bit setting

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

Figure 448 is a timing diagram illustrating the special first index event management. The signals shown are:

MSv45774V1

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

Index management in nonquadrature mode

Figure 449 and Figure 450 detail how the index is managed in directional clock mode and clock plus direction mode, when the SMS[3:0] bitfield is equal to 1010, 1011, 1100, 1101.

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 449. Index behavior in clock + direction mode, IPOS[0] = 1

Timing diagram for Figure 449 showing index behavior in clock + direction mode with IPOS[0] = 1. The diagram includes four waveforms: Direction (TI1), Clock (TI2), Index, and Counter values. The Counter is shown in two modes: Counter x2 mode (counting 7, 0, 1, 2, 3, 4, 3, 2, 7, 6, 5) and Counter x1 mode (counting 7, 0, 1, 2, 1, 7). Arrows indicate that the index pulse is detected on the rising edge of the clock when the direction is high.

The diagram illustrates the relationship between Direction (TI1), Clock (TI2), Index, and the Counter in two modes. In Counter x2 mode, the counter values are 7, 0, 1, 2, 3, 4, 3, 2, 7, 6, 5. In Counter x1 mode, the counter values are 7, 0, 1, 2, 1, 7. The Index signal is shown as a pulse. Arrows indicate that the index pulse is detected on the rising edge of the clock (TI2) when the direction (TI1) is high. The diagram is labeled MSv45777V1.

Timing diagram for Figure 449 showing index behavior in clock + direction mode with IPOS[0] = 1. The diagram includes four waveforms: Direction (TI1), Clock (TI2), Index, and Counter values. The Counter is shown in two modes: Counter x2 mode (counting 7, 0, 1, 2, 3, 4, 3, 2, 7, 6, 5) and Counter x1 mode (counting 7, 0, 1, 2, 1, 7). Arrows indicate that the index pulse is detected on the rising edge of the clock when the direction is high.

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

Timing diagram for Figure 450 showing index behavior in directional clock mode with IPOS[0] = 1. The diagram includes four waveforms: Clock Down (TI1), Clock Up (TI2), DIR bit, and Counter values. The Counter is shown in two modes: Counter x2 mode (counting 9, 0, 1, 2, 3, 4, 3, 2, 1, 0, 9, 8) and Counter x1 mode (counting 9, 0, 1, 2, 1, 0, 9). Arrows indicate that the index pulse is detected on the rising edge of the clock when the DIR bit is high.

The diagram illustrates the relationship between Clock Down (TI1), Clock Up (TI2), DIR bit, and the Counter in two modes. In Counter x2 mode, the counter values are 9, 0, 1, 2, 3, 4, 3, 2, 1, 0, 9, 8. In Counter x1 mode, the counter values are 9, 0, 1, 2, 1, 0, 9. The Index signal is shown as a pulse. Arrows indicate that the index pulse is detected on the rising edge of the clock (TI2) when the DIR bit is high. The diagram is labeled MSv45778V1.

Timing diagram for Figure 450 showing index behavior in directional clock mode with IPOS[0] = 1. The diagram includes four waveforms: Clock Down (TI1), Clock Up (TI2), DIR bit, and Counter values. The Counter is shown in two modes: Counter x2 mode (counting 9, 0, 1, 2, 3, 4, 3, 2, 1, 0, 9, 8) and Counter x1 mode (counting 9, 0, 1, 2, 1, 0, 9). Arrows indicate that the index pulse is detected on the rising edge of the clock when the DIR bit is high.

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 451 . A single bit is expected to change at once. An erroneous transition sets the TERRF interrupt flag in the TIMx_SR status register. A transition error interrupt is generated if the TERRIE bit is set in the TIMx_DIER register.

Figure 451. State diagram for quadrature encoded signals

State diagram for quadrature encoded signals showing four states (00, 01, 10, 11) and their transitions.

The diagram illustrates the state transitions for quadrature encoded signals. It features four circular states arranged in a square: 00 (top-left), 01 (top-right), 10 (bottom-left), and 11 (bottom-right). Solid double-headed arrows represent correct transitions between adjacent states: 00 to 01, 01 to 11, 11 to 10, and 10 to 00. Dashed double-headed arrows represent erroneous transitions between diagonal states: 00 to 11 and 10 to 01. A legend at the bottom left shows a solid double-headed arrow labeled 'Correct transitions' and a dashed double-headed arrow labeled 'Erroneous transitions'. The identifier MSv45779V1 is located in the bottom right corner of the diagram area.

State diagram for quadrature encoded signals showing four states (00, 01, 10, 11) and their transitions.

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 TIMx_ARR to 0 or decremented from 0 to TIMx_ARR value without any index event, this is reported as an index position error.

The overflow threshold is programmed using the TIMx_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 TIMx_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 452 .

Figure 452. Up-counting encoder error detection

Timing diagram for up-counting encoder error detection showing two scenarios: one where an error is detected and another where an abort is triggered by an index pulse.

The figure illustrates two timing scenarios for up-counting encoder error detection. Both scenarios show the relationship between Channel A, Channel B, Index, IERRF, and the Counter.

Top Scenario:

Bottom Scenario:

MSV45780V1

Timing diagram for up-counting encoder error detection showing two scenarios: one where an error is detected and another where an abort is triggered by an index pulse.

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 453 , to avoid any false error detection in case the encoder dithers between TIMx_ARR and 0 immediately after the index detection.

Figure 453. Down-counting encode error detection

Timing diagram for down-counting encode error detection. The diagram is split into two horizontal sections. The top section shows Channel A, Channel B, Index, IERRF, and Counter signals. Channel A is high, then low. Channel B is high, then low, with a falling edge at the first transition. Index is a narrow pulse. IERRF is low. Counter values are 2, 1, 0, 7, 0, 7, 6, 5. Annotations: 'No error: transition from 0 to TIMx_ARR following an index' (at 0 to 7) and 'No error: transition from 0 to TIMx_ARR without index, but not following a transition from 1 to 0' (at 0 to 7). The bottom section shows the same signals but with a different sequence: Counter values are 2, 1, 0, 7, 6, 5, 4. Annotations: 'Error detected' (at 0 to 7) and 'Error asserted' (at 7 to 6). A small note 'MSV47416V1' is in the bottom right corner.
Timing diagram for down-counting encode error detection. The diagram is split into two horizontal sections. The top section shows Channel A, Channel B, Index, IERRF, and Counter signals. Channel A is high, then low. Channel B is high, then low, with a falling edge at the first transition. Index is a narrow pulse. IERRF is low. Counter values are 2, 1, 0, 7, 0, 7, 6, 5. Annotations: 'No error: transition from 0 to TIMx_ARR following an index' (at 0 to 7) and 'No error: transition from 0 to TIMx_ARR without index, but not following a transition from 1 to 0' (at 0 to 7). The bottom section shows the same signals but with a different sequence: Counter values are 2, 1, 0, 7, 6, 5, 4. Annotations: 'Error detected' (at 0 to 7) and 'Error asserted' (at 7 to 6). A small note 'MSV47416V1' is in the bottom right corner.

An index error sets the IERRF interrupt flag in the TIMx_SR status register. An index error interrupt is generated if the IERRIE bit is set in the TIMx_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 or x1 mode, as shown on Figure 454.

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

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

Timing diagram showing encoder mode change from x4 to x2 to x1 mode. The top row shows the encoder clock signal. The second row shows 'Update event' pulses. The third row shows 'Preload value' for SMS[3:0] (0011, 0001, 1110). The bottom row shows 'Active value' for SMS[3:0] (0011, 0001, 1110). Arrows indicate that the active value is updated to match the preload value upon an update event. The diagram is labeled MSV45781V1.
Timing diagram showing encoder mode change from x4 to x2 to x1 mode. The top row shows the encoder clock signal. The second row shows 'Update event' pulses. The third row shows 'Preload value' for SMS[3:0] (0011, 0001, 1110). The bottom row shows 'Active value' for SMS[3:0] (0011, 0001, 1110). Arrows indicate that the active value is updated to match the preload value upon an update event. The diagram is labeled MSV45781V1.

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 TIMx_CR2 register. It is valid for the following SMS[3:0] values: 0001, 0010, 0011, 1010, 1011, 1100, 1101, 1110, 1111. Any other SMS[3:0] code is not allowed and may lead to unexpected behavior.

30.4.19 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 TIMx_CR1 register). This is achieved by setting the OC3M[3:0] or the OC4M[3:0] bitfield to 1011 in the TIMx_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.

30.4.20 UIF bit remapping

The IUFREMAP bit in the TIMx_CR1 register forces a continuous copy of the update interrupt flag (UIF) into bit 31 of the timer counter register's bit 31 (TIMxCNT[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).

30.4.21 Timer input XOR function

The TI1S bit in the TIM1xx_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 29.3.29: Interfacing with Hall sensors .

30.4.22 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 TIMx_CR1 register is low, an update event UEV is generated. Then all the preloaded registers (TIMx_ARR, TIMx_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 TIMx_CCMR1 register. Write CC1P = 0 and CC1NP = 0 in TIMx_CCER register to validate the polarity (and detect rising edges only).
  2. 2. Configure the timer in reset mode by writing SMS = 100 in TIMx_SMCR register. Select tim_ti1 as the input source by writing TS = 00101 in TIMx_SMCR register.
  3. 3. Start the counter by writing CEN = 1 in the TIMx_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 TIMx_SR register) and an interrupt request, or a DMA request can be sent if enabled (depending on the TIE and TDE bits in TIMx_DIER register).

The following figure shows this behavior when the autoreload register TIMx_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 455. Control circuit in reset mode

Timing diagram for Figure 455. Control circuit in reset mode. The diagram shows five signals over time: tim_ti1 (input), UG (update generation), tim_cnt_ck, tim_psc_ck (clocks), Counter register (values 30 to 36, then 00 to 03), and TIF (interrupt flag). The counter resets from 36 to 00 upon a rising edge of tim_ti1, with a delay indicated by vertical dashed lines. The TIF flag is set during this reset delay. MSV62361V1 is noted in the bottom right.
Timing diagram for Figure 455. Control circuit in reset mode. The diagram shows five signals over time: tim_ti1 (input), UG (update generation), tim_cnt_ck, tim_psc_ck (clocks), Counter register (values 30 to 36, then 00 to 03), and TIF (interrupt flag). The counter resets from 36 to 00 upon a rising edge of tim_ti1, with a delay indicated by vertical dashed lines. The TIF flag is set during this reset delay. MSV62361V1 is noted in the bottom right.

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 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 TIMx_CCMR1 register. Write CC1P = 1 and CC1NP = 0 in TIMx_CCER register to validate the polarity (and detect low level only).
  2. 2. Configure the timer in gated mode by writing SMS = 101 in TIMx_SMCR register. Select tim_ti1 as the input source by writing TS = 00101 in TIMx_SMCR register.
  3. 3. Enable the counter by writing CEN = 1 in the TIMx_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 TIMx_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 456. Control circuit in gated mode

Timing diagram for Figure 456: Control circuit in gated mode. It shows the relationship between tim_ti1, CEN, clock signals, the counter register, and the TIF flag. The counter pauses at 34 when tim_ti1 is low and resumes at 35 when it goes high again. TIF is set on the falling edge of tim_ti1.
Timing diagram for Figure 456: Control circuit in gated mode. It shows the relationship between tim_ti1, CEN, clock signals, the counter register, and the TIF flag. The counter pauses at 34 when tim_ti1 is low and resumes at 35 when it goes high again. TIF is set on the falling edge of tim_ti1.

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. 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 TIMx_CCMR1 register. Write CC2P = 1 and CC2NP = 0 in TIMx_CCER register to validate the polarity (and detect low level only).
  2. 2. Configure the timer in trigger mode by writing SMS = 110 in TIMx_SMCR register. Select tim_ti2 as the input source by writing TS = 00110 in TIMx_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 457. Control circuit in trigger mode

Timing diagram for Figure 457: Control circuit in trigger mode. It shows tim_ti2 triggering the CEN signal and the counter starting from value 34 to 35, 36, 37, 38 upon a rising edge. TIF is set when the trigger occurs.
Timing diagram for Figure 457: Control circuit in trigger mode. It shows tim_ti2 triggering the CEN signal and the counter starting from value 34 to 35, 36, 37, 38 upon a rising edge. TIF is set when the trigger occurs.

Slave mode selection preload for run-time encoder mode update

The SMS[3:0] bit can be preloaded. This is enabled by setting the SMSPE enable bit in the TIMx_SMCR register. The trigger for the transfer from SMS[3: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 TIMx_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 TIMx_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 = 01 in TIMx_CCMR1 register to select only the input capture source.
    • – CC1P = 0 and CC1NP = 0 in TIMx_CCER register to validate the polarity (and detect rising edge only).
  3. 3. Configure the timer in trigger mode by writing SMS = 110 in TIMx_SMCR register. Select tim_ti1 as the input source by writing TS = 00101 in TIMx_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 458. Control circuit in external clock mode 2 + trigger mode

Timing diagram for Figure 458 showing signals tim_ti1, CEN, ETR, tim_cnt_ck, tim_psc_ck, Counter register, and TIF over time. The counter register values 34, 35, and 36 are shown. MSV62364V1

Timing diagram showing the relationship between several signals over time. The signals are:

The diagram is labeled MSV62364V1.

Timing diagram for Figure 458 showing signals tim_ti1, CEN, ETR, tim_cnt_ck, tim_psc_ck, Counter register, and TIF over time. The counter register values 34, 35, and 36 are shown. MSV62364V1

30.4.23 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 459 and Figure 460 show examples of master/slave timer connections.

Figure 459. Master/Slave timer example

Block diagram for Figure 459 showing TIM_mstr and TIM_slv timer blocks. TIM_mstr includes Clock, Prescaler, Counter, MMS, and tim_trgo. TIM_slv includes TS, Input trigger selection, tim_itr, SMS, CK_PSC, Prescaler, and Counter. MSV62375V1

Block diagram illustrating Master/Slave timer synchronization. The diagram is divided into two main blocks: TIM_mstr (Master Timer) and TIM_slv (Slave Timer).

The diagram is labeled MSV62375V1.

Block diagram for Figure 459 showing TIM_mstr and TIM_slv timer blocks. TIM_mstr includes Clock, Prescaler, Counter, MMS, and tim_trgo. TIM_slv includes TS, Input trigger selection, tim_itr, SMS, CK_PSC, Prescaler, and Counter. MSV62375V1

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

Figure 460: Master/slave connection example with 1 channel only timers. The diagram shows two timer blocks, TIM_mstr and TIM_slv. TIM_mstr (Master) has a Clock input connected to a Prescaler, which is connected to a Counter. The Counter is connected to a Compare 1 block, which is connected to an Output control block. The Output control block has two outputs: tim_oc1 and TIM_CH1. TIM_slv (Slave) has an Input trigger selection block connected to a TS (Trigger Selection) block. The TS block is connected to a Slave mode control block. The Slave mode control block has an output labeled CK_PSC, which is connected to a Prescaler, which is connected to a Counter. The Slave mode control block also has an input labeled SMS. The tim_oc1 output of TIM_mstr is connected to the TS input of TIM_slv. The diagram is labeled MSV65225V1.
Figure 460: Master/slave connection example with 1 channel only timers. The diagram shows two timer blocks, TIM_mstr and TIM_slv. TIM_mstr (Master) has a Clock input connected to a Prescaler, which is connected to a Counter. The Counter is connected to a Compare 1 block, which is connected to an Output control block. The Output control block has two outputs: tim_oc1 and TIM_CH1. TIM_slv (Slave) has an Input trigger selection block connected to a TS (Trigger Selection) block. The TS block is connected to a Slave mode control block. The Slave mode control block has an output labeled CK_PSC, which is connected to a Prescaler, which is connected to a Counter. The Slave mode control block also has an input labeled SMS. The tim_oc1 output of TIM_mstr is connected to the TS input of TIM_slv. The diagram is labeled MSV65225V1.

Note: The timers with one channel only (see Figure 460) 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 30.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 459. 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).
  4. 4. Finally both timers must be enabled by setting their respective CEN bits (TIMx_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 459 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_{\text{tim\_cnt\_ck}} = f_{\text{tim\_ker\_ck}}/3 \) ).

  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. Enable TIM_slv by writing 1 in the CEN bit (TIM_slv_CR1 register).
  6. 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 461. Gating TIM_slv with tim_oc1ref of TIM_mstr

Timing diagram showing the relationship between master and slave timer signals. The diagram includes five horizontal lines: tim_ker_ck (clock), TIM_mst_oc1ref (master output), tim_mstr_CNT (master counter), tim_slv_CNT (slave counter), and tim_slv_TIF bit (slave interrupt flag). Vertical dashed lines mark key events. The master counter (tim_mstr_CNT) shows values FC, FD, FE, FF, 00, 01. The slave counter (tim_slv_CNT) shows values 3045, 3046, 3047, 3048. The slave TIF bit is set when the slave counter overflows. An arrow points to the TIF bit with the text 'Write TIF = 0'.

The timing diagram illustrates the operation of TIM_mstr and TIM_slv. The top signal, tim_ker_ck, is a periodic clock. Below it, TIM_mst_oc1ref is a pulse-width modulated signal. The third line, tim_mstr_CNT, shows the master counter values: FC, FD, FE, FF, 00, 01. The fourth line, tim_slv_CNT, shows the slave counter values: 3045, 3046, 3047, 3048. The bottom line, tim_slv_TIF bit, shows the interrupt flag status. Vertical dashed lines indicate synchronization points. An arrow points to the TIF bit with the text 'Write TIF = 0'. The diagram is labeled MSv62376V1 in the bottom right corner.

Timing diagram showing the relationship between master and slave timer signals. The diagram includes five horizontal lines: tim_ker_ck (clock), TIM_mst_oc1ref (master output), tim_mstr_CNT (master counter), tim_slv_CNT (slave counter), and tim_slv_TIF bit (slave interrupt flag). Vertical dashed lines mark key events. The master counter (tim_mstr_CNT) shows values FC, FD, FE, FF, 00, 01. The slave counter (tim_slv_CNT) shows values 3045, 3046, 3047, 3048. The slave TIF bit is set when the slave counter overflows. An arrow points to the TIF bit with the text 'Write TIF = 0'.

In the example in Figure 461 , 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 TIMx_EGR registers.

In the next example (refer to Figure 462 ), 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 462. Gating TIM_slv with Enable of TIM_mstr

Timing diagram showing the relationship between TIM_mstr and TIM_slv counters and control signals. The diagram includes signals for tim_ker_ck, TIM_mstr counter enable (CEN bit), tim_mstr_CNT, tim_slv_CNT, tim_slv_CNT reset, tim_slv_CNT write, and tim_slv TIF bit. The master counter (tim_mstr_CNT) starts at 75, resets to 00, and then counts up to 01 and 02. The slave counter (tim_slv_CNT) starts at AB, resets to 00, and then counts up to E7, E8, and E9. The slave counter is enabled by the master counter's CEN bit. The slave TIF bit is set when the slave counter reaches its maximum value (E9) and is cleared by writing 0.

The timing diagram illustrates the synchronization of two timers, TIM_mstr and TIM_slv. The top signal is the kernel clock (tim_ker_ck), shown as a periodic square wave. Below it is the TIM_mstr counter enable (CEN bit), which is initially low, goes high at the third clock edge, and returns low at the seventh clock edge. The tim_mstr_CNT signal shows the master counter's value: it starts at 75, is reset to 00 at the third clock edge, and then increments to 01 and 02. The tim_slv_CNT signal shows the slave counter's value: it starts at AB, is reset to 00 at the third clock edge, and then increments to E7, E8, and E9. The tim_slv_CNT reset signal is high at the third clock edge. The tim_slv_CNT write signal is high at the fourth clock edge. The tim_slv TIF bit is high at the seventh clock edge and is cleared by writing 0, as indicated by the arrow labeled 'Write TIF = 0'.

Timing diagram showing the relationship between TIM_mstr and TIM_slv counters and control signals. The diagram includes signals for tim_ker_ck, TIM_mstr counter enable (CEN bit), tim_mstr_CNT, tim_slv_CNT, tim_slv_CNT reset, tim_slv_CNT write, and tim_slv TIF bit. The master counter (tim_mstr_CNT) starts at 75, resets to 00, and then counts up to 01 and 02. The slave counter (tim_slv_CNT) starts at AB, resets to 00, and then counts up to E7, E8, and E9. The slave counter is enabled by the master counter's CEN bit. The slave TIF bit is set when the slave counter reaches its maximum value (E9) and is cleared by writing 0.

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 459 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_{\text{tim\_cnt\_ck}} = f_{\text{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 463. Triggering TIM_slv with update of TIM_mstr Timing diagram for Figure 463 showing the relationship between 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 diagram shows the master counter (tim_mst_CNT) counting from FD to 02, triggering the slave counter (tim_slv_CNT) which counts from 45 to 48. The slave counter enable (CEN bit) is set high, and the slave TIF bit is set low.

Timing diagram for Figure 463. The diagram shows the relationship between several signals over time. The top signal is tim_ker_ck , a periodic clock. Below it is the tim_mstr_UEV event , which is a pulse that goes high when the master counter tim_mst_CNT reaches 00. The tim_mst_CNT signal shows a sequence of values: FD, FE, FF, 00, 01, 02. The tim_slv_CNT signal shows a sequence of values: 45, 46, 47, 48. The TIM_slv counter enable (CEN bit) is shown as a signal that goes high at the same time as the tim_mstr_UEV event . The tim_slv_TIF bit is shown as a signal that goes low at the same time as the TIM_slv counter enable (CEN bit) . An arrow points to the tim_slv_TIF bit with the text "Write TIF = 0". The diagram is labeled MSv62378V1.

Timing diagram for Figure 463 showing the relationship between 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 diagram shows the master counter (tim_mst_CNT) counting from FD to 02, triggering the slave counter (tim_slv_CNT) which counts from 45 to 48. The slave counter enable (CEN bit) is set high, and the slave TIF bit is set low.

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

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

Figure 464. Triggering TIM_slv with Enable of TIM_mstr Timing diagram for Figure 464 showing the relationship between tim_ker_ck, TIM_mst 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 diagram shows the master counter (tim_mst_CNT) being reset to 75 and then counting from 00 to 02. The slave counter (tim_slv_CNT) is reset to CD and then counting from 00 to EA. The slave counter enable (CEN bit) is set high, and the slave TIF bit is set low.

Timing diagram for Figure 464. The diagram shows the relationship between several signals over time. The top signal is tim_ker_ck , a periodic clock. Below it is the TIM_mst counter enable (CEN bit) , which is a signal that goes high at the beginning of the sequence. The tim_mstr_CNT reset signal is shown as a pulse that goes high at the beginning of the sequence. The tim_mstr_CNT signal shows a sequence of values: 75, 00, 01, 02. The tim_slv_CNT signal shows a sequence of values: CD, 00, E7, E8, E9, EA. The tim_slv_CNT reset signal is shown as a pulse that goes high at the beginning of the sequence. The Tim_slv_CNT write signal is shown as a pulse that goes high at the beginning of the sequence. The tim_slv_TIF bit is shown as a signal that goes low at the same time as the TIM_mst counter enable (CEN bit) . An arrow points to the tim_slv_TIF bit with the text "Write TIF = 0". The diagram is labeled MSv62379V1.

Timing diagram for Figure 464 showing the relationship between tim_ker_ck, TIM_mst 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 diagram shows the master counter (tim_mst_CNT) being reset to 75 and then counting from 00 to 02. The slave counter (tim_slv_CNT) is reset to CD and then counting from 00 to EA. The slave counter enable (CEN bit) is set high, and the slave TIF bit is set low.

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 459 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 (TIMx_CNT). One can see that the master/slave mode inserts a delay between CNT_EN and CK_PSC on TIM_mstr.

Figure 465. Triggering TIM_mstr and TIM_slv with TIM_mstr tim_ti1 input

Timing diagram showing the relationship between various timer signals. The signals are: tim_ker_ck (system clock), tim_mstr_ti1 (external trigger), TIM_mstr counter enable (CEN bit), tim_mstr_psc_ck (master prescaler clock), tim_mstr_CNT (master counter), tim_mstr TIF bit, TIM_slv counter enable (CEN bit), tim_slv_psc_ck (slave prescaler clock), tim_slv_CNT (slave counter), and tim_slv TIF bit. The diagram shows that when tim_mstr_ti1 rises, the TIM_mstr CEN bit goes high, which then enables the slave timer. The master counter starts counting on the next rising edge of its prescaler clock, and the slave counter starts counting on the next rising edge of its prescaler clock. The slave counter's count is offset from the master counter's count by one prescaler clock cycle.
Timing diagram showing the relationship between various timer signals. The signals are: tim_ker_ck (system clock), tim_mstr_ti1 (external trigger), TIM_mstr counter enable (CEN bit), tim_mstr_psc_ck (master prescaler clock), tim_mstr_CNT (master counter), tim_mstr TIF bit, TIM_slv counter enable (CEN bit), tim_slv_psc_ck (slave prescaler clock), tim_slv_CNT (slave counter), and tim_slv TIF bit. The diagram shows that when tim_mstr_ti1 rises, the TIM_mstr CEN bit goes high, which then enables the slave timer. The master counter starts counting on the next rising edge of its prescaler clock, and the slave counter starts counting on the next rising edge of its prescaler clock. The slave counter's count is offset from the master counter's count by one prescaler clock cycle.

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.

30.4.24 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.

30.4.25 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 TIMx_DMAR. On a given timer event, the timer launches a sequence of DMA requests (burst). Each write into the TIMx_DMAR register is actually redirected to one of the timer registers.

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

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

Example:

00000: TIMx_CR1

00001: TIMx_CR2

00010: TIMx_SMCR

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 transfers, DBA = 0xE.
  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.

30.4.26 TIM2/TIM3/TIM4/TIM5 DMA requests

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

Table 298. DMA request

DMA request signalDMA acronymDMA requestEnable control bit
tim_upd_dmaTIM_UPUpdateUDE
tim_cc1_dmaTIM_CH1Capture/compare 1CC1DE
tim_cc2_dmaTIM_CH2Capture/compare 2CC2DE
tim_cc3_dmaTIM_CH3Capture/compare 3CC3DE
tim_cc4_dmaTIM_CH4Capture/compare 4CC4DE
tim_trgi_dmaTIM_TRIGTriggerTDE

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

30.4.27 Debug mode

When the microcontroller enters debug mode (Cortex ® -M4 with FPU 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).

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

Table 299. 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.

30.4.29 TIM2/TIM3/TIM4/TIM5 interrupts

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

Table 300. Interrupt requests

Interrupt acronymInterrupt eventEvent flagEnable control bitInterrupt clear methodExit from Sleep modeExit from Stop and Standby mode
TIM_UPUpdateUIFUIEwrite 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_TRGTriggerTIFTIEwrite 0 in TIFYesNo
TIM_DIR
_IDX
IndexIDXFIDXIEwrite 0 in IDXFYesNo
DirectionDIRFDIRIEwrite 0 in DIRFYesNo
TIM_IERRIndex ErrorIERRFIERRIEwrite 0 in IERRFYesNo
TIM_TERTransition ErrorTERRFTERRIEwrite 0 in TERRFYesNo

30.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).

30.5.1 TIMx control register 1 (TIMx_CR1)(x = 2 to 5)

Address offset: 0x000

Reset value: 0x0000

1514131211109876543210
Res.Res.Res.DITH
EN
UIFRE
MAP
Res.CKD[1:0]ARPECMS[1:0]DIROPMURSUDISCEN
rwrwrwrwrwrwrwrwrwrw

Bits 15: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 TIMx_CNT register bit 31.

1: Remapping enabled. UIF status bit is copied to TIMx_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: Reserved

Bit 7 ARPE : Autoreload preload enable

0: TIMx_ARR register is not buffered

1: TIMx_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 TIMx_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 TIMx_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 TIMx_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

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

Bit 3 OPM : One-pulse modeBit 2 URS : Update request source

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

These events can be:

Bit 1 UDIS : Update disable

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

Buffered registers are then loaded with their preload values.

Bit 0 CEN : Counter enable

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.

30.5.2 TIMx control register 2 (TIMx_CR2)(x = 2 to 5)

Address offset: 0x004

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.MMS[3]Res.Res.Res.Res.Res.Res.Res.Res.Res.
rw
1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.TI1SMMS[2:0]CCDSRes.Res.Res.
rwrwrwrwrw

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

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

Bit 7 TI1S : tim_ti1 selection

0: The tim_ti1_in[15:0] multiplexer output is to tim_ti1 input

1: The tim_ti1_in[15:0], tim_ti2_in[15:0] and tim_ti3_in[15:0] multiplexers outputs are XORed and connected to the tim_ti1 input. See also Section 29.3.29: Interfacing with Hall sensors .

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 TIMx_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 TIMx_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[3:0] values: 0001, 0010, 0011, 1010, 1011, 1100, 1101, 1110, 1111. Any other SMS[3: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.

30.5.3 TIMx slave mode control register (TIMx_SMCR)(x = 2 to 5)

Address offset: 0x008

Reset value: 0x0000 0000

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

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

Bit 25 SMSPS : SMS preload source

This bit selects whether the events that triggers the SMS[3: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[3:0] bitfield is preloaded

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

1: SMS[3:0] preload is enabled

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

Bits 19:17 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.

Others: Reserved

See Section 30.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

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

Bits 16, 2, 1, 0 SMS[3: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 TIMx_SMCR for tim_etr_in and CCxP/CCxNP bits in TIMx_CCER register for tim_ti1fp1 and tim_ti2fp2).

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

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

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

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

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

0101: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.

0110: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.

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

1000: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.

1001: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.

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

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

1100:Encoder mode: Directional clock, x2 mode.

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

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

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

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.

30.5.4 TIMx DMA/Interrupt enable register (TIMx_DIER)(x = 2 to 5)

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.

  1. Bit 7 Reserved, must be kept at reset value.
  2. Bit 6 TIE : Trigger interrupt enable
    0: Trigger interrupt disabled.
    1: Trigger interrupt enabled.
  3. Bit 5 Reserved, must be kept at reset value.
  4. Bit 4 CC4IE : Capture/Compare 4 interrupt enable
    0: CC4 interrupt disabled.
    1: CC4 interrupt enabled.
  5. Bit 3 CC3IE : Capture/Compare 3 interrupt enable
    0: CC3 interrupt disabled.
    1: CC3 interrupt enabled.
  6. Bit 2 CC2IE : Capture/Compare 2 interrupt enable
    0: CC2 interrupt disabled.
    1: CC2 interrupt enabled.
  7. Bit 1 CC1IE : Capture/Compare 1 interrupt enable
    0: CC1 interrupt disabled.
    1: CC1 interrupt enabled.
  8. Bit 0 UIE : Update interrupt enable
    0: Update interrupt disabled.
    1: Update interrupt enabled.

30.5.5 TIMx status register (TIMx_SR)(x = 2 to 5)

Address offset: 0x010

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.TERRFIERRFDIRFIDXFRes.Res.Res.Res.
rc_w0rc_w0rc_w0rc_w0
1514131211109876543210
Res.Res.Res.CC4OFCC3OFCC2OFCC1OFRes.Res.TIFRes.CC4IFCC3IFCC2IFCC1IFUIF
rc_w0rc_w0rc_w0rc_w0rc_w0rc_w0rc_w0rc_w0rc_w0rc_w0

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

Bit 23 TERRF : Transition error interrupt flag

This flag is set by hardware when a transition error is detected in encoder mode. It is cleared by software by writing it to 0.

0: No encoder transition error has been detected.

1: An encoder transition error has been detected

Bit 22 IERRF : Index error interrupt flag

This flag is set by hardware when an index error is detected. It is cleared by software by writing it to 0.

0: No index error has been detected.

1: An index error has been detected

Bit 21 DIRF : Direction change interrupt flag

This flag is set by hardware when the direction changes in encoder mode (DIR bit value in TIMx_CR is changing). It is cleared by software by writing it to 0.

0: No direction change

1: Direction change

Bit 20 IDXF : Index interrupt flag

This flag is set by hardware when an index event is detected. It is cleared by software by writing it to 0.

0: No index event occurred.

1: An index event has occurred

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

Bit 12 CC4OF : Capture/Compare 4 overcapture flag

refer to CC1OF description

Bit 11 CC3OF : Capture/Compare 3 overcapture flag

refer to CC1OF description

Bit 10 CC2OF : Capture/compare 2 overcapture flag

refer to CC1OF description

Bit 9 CC1OF : Capture/Compare 1 overcapture flag

This flag is set by hardware only when the corresponding channel is configured in input capture mode. It is cleared by software by writing it to 0.

0: No overcapture has been detected.

1: The counter value has been captured in TIMx_CCR1 register while CC1IF flag was already set

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

Bit 6 TIF : Trigger interrupt flag

This flag is set by hardware on the TRG trigger event (active edge detected on tim_trgi input) when the slave mode controller is enabled in all modes but gated mode. It is set when the counter starts or stops when gated mode is selected. It is cleared by software.

0: No trigger event occurred.

1: Trigger interrupt pending.

Bit 5 Reserved, must be kept at reset value.

Bit 4 CC4IF : Capture/Compare 4 interrupt flag

Refer to CC1IF description

Bit 3 CC3IF : Capture/Compare 3 interrupt flag

Refer to CC1IF description

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 TIMx_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 TIMx_CNT matches the content of the TIMx_CCR1 register. When the content of TIMx_CCR1 is greater than the content of TIMx_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 TIMx_CR1 register for the full description.

If channel CC1 is configured as input: this bit is set when counter value has been captured in TIMx_CCR1 register (an edge has been detected on IC1, as per the edge sensitivity defined with the CC1P and CC1NP bits setting, in TIMx_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 TIMx_CR1 register.

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

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

30.5.6 TIMx event generation register (TIMx_EGR)(x = 2 to 5)

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 TIMx_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 TIMx_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 (TIMx_ARR) if DIR = 1 (down-counting).

30.5.7 TIMx capture/compare mode register 1 (TIMx_CCMR1)(x = 2 to 5)

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).

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

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 (TIMx_SMCR register)

Note: CC2S bits are writable only when the channel is OFF (CC2E = 0 in TIMx_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 (TIMx_CCER register).

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

01: capture is done once every 2 events

10: capture is done once every 4 events

11: capture is done once every 8 events

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 (TIMx_SMCR register)

Note: CC1S bits are writable only when the channel is OFF (CC1E = 0 in TIMx_CCER).

30.5.8 TIMx capture/compare mode register 1 [alternate] (TIMx_CCMR1)(x = 2 to 5)

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.Res.OC2M
[3]
Res.Res.Res.Res.Res.Res.Res.OC1M
[3]
1514131211109876543210
OC2CEOC2M[2:0]OC2PEOC2FECC2S[1:0]OC1CEOC1M[2:0]OC1PEOC1FECC1S[1:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Output compare mode

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

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

Bit 15 OC2CE : Output compare 2 clear enable

Bits 24, 14:12 OC2M[3:0] : Output compare 2 mode
refer to OC1M description on bits 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 (TIMx_SMCR register)

Note: CC2S bits are writable only when the channel is OFF (CC2E = 0 in TIMx_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

Bits 16, 6:4 OC1M[3: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.

0000: Frozen - The comparison between the output compare register TIMx_CCR1 and the counter TIMx_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.

0001: Set channel 1 to active level on match. tim_oc1ref signal is forced high when the counter TIMx_CNT matches the capture/compare register 1 ( TIMx_CCR1 ).

0010: Set channel 1 to inactive level on match. tim_oc1ref signal is forced low when the counter TIMx_CNT matches the capture/compare register 1 ( TIMx_CCR1 ).

0011: Toggle - tim_oc1ref toggles when TIMx_CNT = TIMx_CCR1 .

0100: Force inactive level - tim_oc1ref is forced low.

0101: Force active level - tim_oc1ref is forced high.

0110: PWM mode 1 - In up-counting, channel 1 is active as long as TIMx_CNT < TIMx_CCR1 else inactive. In down-counting, channel 1 is inactive ( tim_oc1ref = 0 ) as long as TIMx_CNT > TIMx_CCR1 else active ( tim_oc1ref = 1 ).

0111: PWM mode 2 - In up-counting, channel 1 is inactive as long as TIMx_CNT < TIMx_CCR1 else active. In down-counting, channel 1 is active as long as TIMx_CNT > TIMx_CCR1 else inactive.

1000: 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.

1001: 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.

1010: Reserved.

1011: Reserved.

1100: Combined PWM mode 1 - tim_oc1ref has the same behavior as in PWM mode 1. tim_oc1refc is the logical OR between tim_oc1ref and tim_oc2ref .

1101: Combined PWM mode 2 - tim_oc1ref has the same behavior as in PWM mode 2. tim_oc1refc is the logical AND between tim_oc1ref and tim_oc2ref .

1110: Asymmetric PWM mode 1 - tim_oc1ref has the same behavior as in PWM mode 1. tim_oc1refc outputs tim_oc1ref when the counter is counting up, tim_oc2ref when it is counting down.

1111: Asymmetric PWM mode 2 - tim_oc1ref has the same behavior as in PWM mode 2. tim_oc1refc outputs tim_oc1ref when the counter is counting up, tim_oc2ref when it is counting down.

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 TIMx_CCR1 disabled. TIMx_CCR1 can be written at anytime, the new value is taken in account immediately.

1: Preload register on TIMx_CCR1 enabled. Read/Write operations access the preload register. TIMx_CCR1 preload value is loaded in the active register at each update event.

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 TIMx_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 (TIMx_SMCR register)

Note: CC1S bits are writable only when the channel is OFF (CC1E = 0 in TIMx_CCER).

30.5.9 TIMx capture/compare mode register 2 (TIMx_CCMR2)(x = 2 to 5)

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
ResResResResResResResResResResResResResResResRes
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

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 (TIMx_SMCR register)

Note: CC4S bits are writable only when the channel is OFF (CC4E = 0 in TIMx_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 (TIMx_SMCR register)

Note: CC3S bits are writable only when the channel is OFF (CC3E = 0 in TIMx_CCER).

30.5.10 TIMx capture/compare mode register 2 [alternate]
(TIMx_CCMR2)(x = 2 to 5)

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).

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.OC4M
[3]
Res.Res.Res.Res.Res.Res.Res.OC3M
[3]
rwrw
1514131211109876543210
OC4CEOC4M[2:0]OC4PEOC4FECC4S[1:0]OC3CEOC3M[2:0]OC3PEOC3FECC3S[1:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Output compare mode

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

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

Bit 15 OC4CE : Output compare 4 clear enable

Bits 24, 14:12 OC4M[3:0] : Output compare 4 mode

Refer to OC3M[3: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 (TIMx_SMCR register)

Note: CC4S bits are writable only when the channel is OFF (CC4E = 0 in TIMx_CCER).

Bit 7 OC3CE : Output compare 3 clear enable

Bits 16, 6:4 OC3M[3: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.

0000:Frozen - The comparison between the output compare register TIMx_CCR3 and the counter TIMx_CNT has no effect on the outputs.(this mode is used to generate a timing base).

0001:Set channel 3 to active level on match. tim_oc3ref signal is forced high when the counter TIMx_CNT matches the capture/compare register 3 ( TIMx_CCR3 ).

0010:Set channel 3 to inactive level on match. tim_oc3ref signal is forced low when the counter TIMx_CNT matches the capture/compare register 3 ( TIMx_CCR3 ).

0011:Toggle - tim_oc3ref toggles when TIMx_CNT = TIMx_CCR3 .

0100:Force inactive level - tim_oc3ref is forced low.

0101:Force active level - tim_oc3ref is forced high.

0110:PWM mode 1 - In up-counting, channel 3 is active as long as TIMx_CNT < TIMx_CCR3 else inactive. In down-counting, channel 3 is inactive ( tim_oc3ref = 0 ) as long as TIMx_CNT > TIMx_CCR3 else active ( tim_oc3ref = 1 ).

0111:PWM mode 2 - In up-counting, channel 3 is inactive as long as TIMx_CNT < TIMx_CCR3 else active. In down-counting, channel 3 is active as long as TIMx_CNT > TIMx_CCR3 else inactive.

1000: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 channels becomes active 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 channels becomes inactive again at the next update.

1001: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 channels 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 channels becomes active again at the next update.

1010:Pulse on compare: a pulse is generated on tim_oc3ref upon CCR3 match event, as per PWPRSC[2:0] and PW[7:0] bitfields programming in TIMx_ECR .

1011:Direction output. The tim_oc3ref signal is overridden by a copy of the DIR bit.

1100:Combined PWM mode 1 - tim_oc3ref has the same behavior as in PWM mode 1. tim_oc3refc is the logical OR between tim_oc3ref and tim_oc4ref .

1101: Combined PWM mode 2 - tim_oc3ref has the same behavior as in PWM mode 2. tim_oc3refc is the logical AND between tim_oc3ref and tim_oc4ref .

1110:Asymmetric PWM mode 1 - tim_oc3ref has the same behavior as in PWM mode 1. tim_oc3refc outputs tim_oc3ref when the counter is counting up, tim_oc4ref when it is counting down.

1111:Asymmetric PWM mode 2 - tim_oc3ref has the same behavior as in PWM mode 2. tim_oc3refc outputs tim_oc3ref when the counter is counting up, tim_oc4ref when it is counting down.

Note: These bits can not be modified as long as LOCK level 3 has been programmed (LOCK bits in TIMx_BDTR register) and CC1S = 00 (the channel is configured in output).

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 TIMx_CR2 register then the OC3M active bits take the new value from the preloaded bits only when a COM event is generated.

Bit 3 OC3PE : Output compare 3 preload enable

Bit 2 OC3FE : Output compare 3 fast enable

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 (TIMx_SMCR register)

Note: CC3S bits are writable only when the channel is OFF (CC3E = 0 in TIMx_CCER).

30.5.11 TIMx capture/compare enable register (TIMx_CCER)(x = 2 to 5)

Address offset: 0x020

Reset value: 0x0000

1514131211109876543210
CC4NPRes.CC4PCC4ECC3NPRes.CC3PCC3ECC2NPRes.CC2PCC2ECC1NPRes.CC1PCC1E
rwrwrwrwrwrwrwrwrwrwrwrw

Bit 15 CC4NP : Capture/Compare 4 output Polarity.

Refer to CC1NP description

Bit 14 Reserved, must be kept at reset value.

Bit 13 CC4P : Capture/Compare 4 output Polarity.

Refer to CC1P description

Bit 12 CC4E : Capture/Compare 4 output enable.

refer to CC1E description

Bit 11 CC3NP : Capture/Compare 3 output Polarity.

Refer to CC1NP description

Bit 10 Reserved, must be kept at reset value.

Bit 9 CC3P : Capture/Compare 3 output Polarity.

Refer to CC1P description

Bit 8 CC3E : Capture/Compare 3 output enable.

Refer to CC1E description

Bit 7 CC2NP : Capture/Compare 2 output Polarity.

Refer to CC1NP description

Bit 6 Reserved, must be kept at reset value.

Bit 5 CC2P : Capture/Compare 2 output Polarity.

refer to CC1P description

Bit 4 CC2E : Capture/Compare 2 output enable.

Refer to CC1E description

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 301. 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.

30.5.12 TIMx counter (TIMx_CNT)(x = 3, 4)

Address offset: 0x024

Reset value: 0x0000 0000

31302928272625242322212019181716
UIF
CPY
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
rw
1514131211109876543210
CNT[15:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bit 31 UIFCPY : Value depends on IUFREMAP in TIMx_CR1.

If IUFREMAP = 0

Reserved

If IUFREMAP = 1

UIFCPY : UIF Copy

This bit is a read-only copy of the UIF bit of the TIMx_ISR register

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

Bits 15:0 CNT[15:0] : 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[15:0]. The fractional part is not available.

30.5.13 TIMx counter (TIMx_CNT)(x = 2, 5)

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 IUFREMAP in TIMx_CR1.

If IUFREMAP = 0

CNT[31] : Most significant bit of counter value

If IUFREMAP = 1

UIFCPY : UIF Copy

This bit is a read-only copy of the UIF bit of the TIMx_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.

30.5.14 TIMx prescaler (TIMx_PSC)(x = 2 to 5)

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 TIMx_EGR register or through trigger controller when configured in “reset mode”).

30.5.15 TIMx autoreload register (TIMx_ARR)(x = 3, 4)

Address offset: 0x02C

Reset value: 0x0000 FFFF

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.ARR[19:16]
rwrwrwrw
1514131211109876543210
ARR[15:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

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

Bits 19:0 ARR[19:0] : Low autoreload value

ARR is the value to be loaded in the actual autoreload register.

Refer to the Section 30.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 (DITHEN = 0)

The register holds the autoreload value.

Dithering mode (DITHEN = 1)

The register holds the integer part in ARR[19:4]. The ARR[3:0] bitfield contains the dithered part.

30.5.16 TIMx autoreload register (TIMx_ARR)(x = 2, 5)

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 30.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 (DITHEN = 0)

The register holds the autoreload value.

Dithering mode (DITHEN = 1)

The register holds the integer part in ARR[31:4]. The ARR[3:0] bitfield contains the dithered part.

30.5.17 TIMx capture/compare register 1 (TIMx_CCR1)(x = 3, 4)

Address offset: 0x034

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.CCR1[19:16]
rwrwrwrw
1514131211109876543210
CCR1[15:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

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

Bits 19:0 CCR1[19: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 TIMx_CCMR1 register (bit OC1PE). Else the preload value is copied in the active capture/compare 1 register when an update event occurs.

The active capture/compare register contains the value to be compared to the counter TIMx_CNT and signaled on tim_oc1 output.

Non-dithering mode (DITHEN = 0)

The register holds the compare value in CCR1[15:0]. The CCR1[19:16] bits are reset.

Dithering mode (DITHEN = 1)

The register holds the integer part in CCR1[19: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 TIMx_CCR1 register is read-only and cannot be programmed.

Non-dithering mode (DITHEN = 0)

The CCR1[15:0] bits hold the capture value. The CCR1[19:16] bits are reserved.

Dithering mode (DITHEN = 1)

The register holds the capture in CCR1[19:0]. The CCR1[3:0] bits are reset.

30.5.18 TIMx capture/compare register 1 (TIMx_CCR1)(x = 2, 5)

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 TIMx_CCMR1 register (bit OC1PE). Else the preload value is copied in the active capture/compare 1 register when an update event occurs.

The active capture/compare register contains the value to be compared to the counter TIMx_CNT and signaled on 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 TIMx_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.

30.5.19 TIMx capture/compare register 2 (TIMx_CCR2)(x = 3, 4)

Address offset: 0x038

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.CCR2[19:16]
rwrwrwrw
1514131211109876543210
CCR2[15:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

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

Bits 19:0 CCR2[19:0] : Capture/compare 1 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 TIMx_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 TIMx_CNT and signaled on tim_oc2 output.

Non-dithering mode (DITHEN = 0)

The register holds the compare value in CCR2[15:0]. The CCR2[19:16] bits are reset.

Dithering mode (DITHEN = 1)

The register holds the integer part in CCR2[19: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 TIMx_CCR2 register is read-only and cannot be programmed.

Non-dithering mode (DITHEN = 0)

The CCR2[15:0] bits hold the capture value. The CCR2[19:16] bits are reserved.

Dithering mode (DITHEN = 1)

The register holds the capture in CCR2[19:0]. The CCR2[3:0] bits are reset.

30.5.20 TIMx capture/compare register 2 (TIMx_CCR2)(x = 2, 5)

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 TIMx_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 TIMx_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 TIMx_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.

30.5.21 TIMx capture/compare register 3 (TIMx_CCR3)(x = 3, 4)

Address offset: 0x03C

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.CCR3[19:16]
rwrwrwrw
1514131211109876543210
CCR3[15:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

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

Bits 19:0 CCR3[19: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 TIMx_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 TIMx_CNT and signaled on tim_oc3 output.

Non-dithering mode (DITHEN = 0)

The register holds the compare value in CCR3[15:0]. The CCR3[19:16] bits are reset.

Dithering mode (DITHEN = 1)

The register holds the integer part in CCR3[19: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 TIMx_CCR3 register is read-only and cannot be programmed.

Non-dithering mode (DITHEN = 0)

The CCR3[15:0] bits hold the capture value. The CCR3[19:16] bits are reserved.

Dithering mode (DITHEN = 1)

The register holds the capture in CCR3[19:0]. The CCR3[3:0] bits are reset.

30.5.22 TIMx capture/compare register 3 (TIMx_CCR3)(x = 2, 5)

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 TIMx_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 TIMx_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 TIMx_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.

30.5.23 TIMx capture/compare register 4 (TIMx_CCR4)(x = 3, 4)

Address offset: 0x040

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.CCR4[19:16]
rwrwrwrw
1514131211109876543210
CCR4[15:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

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

Bits 19:0 CCR4[19: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 TIMx_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 TIMx_CNT and signaled on tim_oc4 output.

Non-dithering mode (DITHEN = 0)

The register holds the compare value in CCR4[15:0]. The CCR4[19:16] bits are reset.

Dithering mode (DITHEN = 1)

The register holds the integer part in CCR4[19: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 TIMx_CCR4 register is read-only and cannot be programmed.

Non-dithering mode (DITHEN = 0)

The CCR4[15:0] bits hold the capture value. The CCR4[19:16] bits are reserved.

Dithering mode (DITHEN = 1)

The register holds the capture in CCR4[19:0]. The CCR4[3:0] bits are reset.

30.5.24 TIMx capture/compare register 4 (TIMx_CCR4)(x = 2, 5)

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 TIMx_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 TIMx_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 TIMx_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.

30.5.25 TIMx timer encoder control register (TIMx_ECR)(x = 2 to 5)

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]FIDXRes.Res.IDIR[1:0]IE
rwrwrwrwrwrwrw

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[3:0] = 0001, 0010, 0011, 1110, 1111), this bit indicates in which AB input configuration the Index event resets the counter.

In directional clock mode or clock plus direction mode (SMS[3:0] = 1010, 1011, 1100, 1101), 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 Reserved, must be kept at reset value.

Bits 2:1 IDIR[1:0] : Index direction

This bit indicates in which direction the Index event resets the counter.

Note: The IDR[1:0] bitfield must be written when IE bit is reset (index disabled).

Bit 0 IE : Index enable

This bit indicates if the Index event resets the counter.

30.5.26 TIMx timer input selection register (TIMx_TISEL)(x = 2 to 5)

Address offset: 0x05C

Reset value: 0x0000 0000

31302928272625242322212019181716
ResResResResTI4SEL[3:0]ResResResResTI3SEL[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.

Bits 27:24 TI4SEL[3:0] : Selects tim_ti4[15:0] input

0000: tim_ti4_in0: TIMx_CH4

0001: tim_ti4_in1

...

1111: tim_ti4_in15

Refer to Section 30.4.2: TIM2/TIM3/TIM4/TIM5 pins and internal signals for product specific implementation.

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

Bits 19:16 TI3SEL[3:0] : Selects tim_ti3[15:0] input

0000: tim_ti3_in0: TIMx_CH3

0001: tim_ti3_in1

...

1111: tim_ti3_in15

Refer to Section 30.4.2: TIM2/TIM3/TIM4/TIM5 pins and internal signals for product specific implementation.

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

Bits 11:8 TI2SEL[3:0] : Selects tim_ti2[15:0] input

0000: tim_ti2_in0: TIMx_CH2

0001: tim_ti2_in1

...

1111: tim_ti2_in15

Refer to Section 30.4.2: TIM2/TIM3/TIM4/TIM5 pins and internal signals for product specific implementation.

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

Bits 3:0 TI1SEL[3:0] : Selects tim_ti1[15:0] input

0000: tim_ti1_in0: TIMx_CH1

0001: tim_ti1_in1

...

1111: tim_ti1_in15

Refer to Section 30.4.2: TIM2/TIM3/TIM4/TIM5 pins and internal signals for product specific implementation.

30.5.27 TIMx alternate function register 1 (TIMx_AF1)(x = 2 to 5)

Address offset: 0x060

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.ETRSEL[3:2]
rwrw
1514131211109876543210
ETRSEL[1:0]Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
rwrw

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

Bits 17:14 ETRSEL[3:0] : etr_in source selection

These bits select the etr_in input source.

0000: tim_etr0: TIMx_ETR input

0001: tim_etr1

...

1111: tim_etr15

Refer to Section 30.4.2: TIM2/TIM3/TIM4/TIM5 pins and internal signals for product specific implementation.

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

30.5.28 TIMx alternate function register 2 (TIMx_AF2)(x = 2 to 5)

Address offset: 0x064

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.OCRSEL[2:0]
rwrwrw
1514131211109876543210
Res.Res.Res.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:16 OCRSEL[2:0] : ocref_clr source selection

These bits select the ocref_clr input source.

000: tim_ocref_clr0

001: tim_ocref_clr1

...

111: tim_ocref_clr7

Refer to Section 30.4.2: TIM2/TIM3/TIM4/TIM5 pins and internal signals for product specific implementation.

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

30.5.29 TIMx DMA control register (TIMx_DCR)(x = 2 to 5)

Address offset: 0x3DC

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
Res.Res.Res.DBL[4:0]Res.Res.Res.DBA[4:0]
rwrwrwrwrwrwrwrwrwrw

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

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 TIMx_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

...

11010: 26 transfers

Example: Let us consider the following transfer: DBL = 7 bytes & DBA = TIM2_CR1.

–If DBL = 7 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:

(TIMx_CR1 address) + DBA + (DMA index), where DMA index = DBL

In this example, 7 bytes are added to (TIMx_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: (TIMx_CR1 address) + DBA

According to the configuration of the DMA Data Size, several cases may occur:

–If the DMA Data Size is configured in half-words, 16-bit data are transferred to each of the 7 registers.

–If the DMA Data Size is configured in bytes, the data are also transferred to 7 registers: the first register contains the first MSB byte, the second register, the first LSB byte and so on. So with the transfer Timer, one also has to specify the size of data transferred by DMA.

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 TIMx_DMAR address). DBA is defined as an offset starting from the address of the TIMx_CR1 register.

Example:

00000: TIMx_CR1,

00001: TIMx_CR2,

00010: TIMx_SMCR,

...

30.5.30 TIMx DMA address for full transfer (TIMx_DMAR)(x = 2 to 5)

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

\[ (\text{TIMx\_CR1 address}) + (\text{DBA} + \text{DMA index}) \times 4 \]

where TIMx_CR1 address is the address of the control register 1, DBA is the DMA base address configured in TIMx_DCR register, DMA index is automatically controlled by the DMA transfer, and ranges from 0 to DBL (DBL configured in TIMx_DCR).

30.5.31 TIMx register map

TIMx registers are mapped as described in the table below.

Table 302. TIM2/TIM3/TIM4/TIM5 register map and reset values

OffsetRegister name313029282726252423222120191817161514131211109876543210
0x000TIMx_CR1Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.DITHENUIFREMARes.CKD[1:0]ARPECMS[1:0]DIROPMURSUDISCEN
Reset value000000000000
0x004TIMx_CR2Res.Res.Res.Res.Res.Res.MMS[3]Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.TI1SMMS[2:0]CCDSRes.Res.Res.
Reset value000000
0x008TIMx_SMCRRes.Res.Res.Res.Res.Res.SMSPSSMSPERes.Res.TS[4:3]Res.Res.Res.Res.SMS[3]ETPECEETPS[1:0]ETF[3:0]MSMTS[2:0]Res.SMS[2:0]
Reset value000000000000000000000
0x00CTIMx_DIERRes.Res.Res.Res.Res.Res.Res.TERRIEIERRIEDIRIEIDXIERes.Res.Res.Res.Res.Res.Res.TDECC4DECC3DECC2DECC1DEUDERes.TIERes.CC4IECC3IECC2IECC1IEUIE
Reset value0000000000000000
0x010TIMx_SRRes.Res.Res.Res.Res.Res.Res.TERRFIERRFDIRFIDXFRes.Res.Res.Res.Res.Res.Res.Res.CC4OFCC3OFCC2OFCC1OFRes.Res.TIFRes.CC4IFCC3IFCC2IFCC1IFUIF
Reset value00000000000000
0x014TIMx_EGRRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.TGRes.CC4GCC3GCC2GCC1GUG
Reset value000000

Table 302. TIM2/TIM3/TIM4/TIM5 register map and reset values (continued)

OffsetRegister name313029282726252423222120191817161514131211109876543210
0x018TIMx_CCMR1
Input Capture mode
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.IC2F[3:0]IC2
PSC
[1:0]
CC2S
[1:0]
IC1F[3:0]IC1
PSC
[1:0]
CC1S
[1:0]
Reset value0000000000000000
TIMx_CCMR1
Output Compare mode
Res.Res.Res.Res.Res.Res.Res.OC2M[3]Res.Res.Res.Res.Res.Res.Res.OC1M[3]OC2CEOC2M
[2:0]
OC2PEOC2FECC2S
[1:0]
OC1CEOC1M
[2:0]
OC1PEOC1FECC1S
[1:0]
Reset value000000000000000000
0x01CTIMx_CCMR2
Input Capture mode
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.IC4F[3:0]IC4
PSC
[1:0]
CC4S
[1:0]
IC3F[3:0]IC3
PSC
[1:0]
CC3S
[1:0]
Reset value0000000000000000
TIMx_CCMR2
Output Compare mode
Res.Res.Res.Res.Res.Res.Res.OC4M[3]Res.Res.Res.Res.Res.Res.Res.OC3M[3]OC4CEOC4M
[2:0]
OC4PEOC4FECC4S
[1:0]
OC3CEOC3M
[2:0]
OC3PEOC3FECC3S
[1:0]
Reset value000000000000000000
0x020TIMx_CCERRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.CC4NPRes.CC4PCC4ECC3NPRes.CC3PCC3ECC2NPRes.CC2PCC2ECC1NPRes.CC1PCC1E
Reset value000000000000
0x024TIMx_CNTUIFCPYCNT[30:16]
(CNT[31:16] on 32-bit timers only)
CNT[15:0]
Reset value00000000000000000000000000000000
0x028TIMx_PSCRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.PSC[15:0]
Reset value0000000000000000
0x02CTIMx_ARR
(x = 3, 4)
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.ARR[19:0]
Reset value00001111111111111111
0x02CTIMx_ARR
(x = 2, 5)
ARR[31:0]
Reset value11111111111111111111111111111111
0x030Reserved
0x034TIMx_CCR1CCR1[31:20]
(32-bit timers only)
CCR1[19:0]
Reset value00000000000000000000000000000000
0x038TIMx_CCR2CCR2[31:20]
(32-bit timers only)
CCR2[19:0]
Reset value00000000000000000000000000000000
0x03CTIMx_CCR3CCR3[31:20]
(32-bit timers only)
CCR3[19:0]
Reset value00000000000000000000000000000000
0x040TIMx_CCR4CCR4[31:20]
(32-bit timers only)
CCR4[19:0]
Reset value00000000000000000000000000000000

Table 302. TIM2/TIM3/TIM4/TIM5 register map and reset values (continued)

OffsetRegister name313029282726252423222120191817161514131211109876543210
0x044..0x054ReservedRes.
0x058TIMx_ECRResResResResResPWPRSC [2:0]PW[7:0]ResResResResResResResResResResResIPOS [1:0]FIDXResResIDIR [1:0]IE
Reset value0 0 00 0 0 0 0 0 0 00 000 00 00
0x05CTIMx_TISELResResResResResResResResResResResResResResResResResResResResResResResResResResResResResResResRes
Reset value0 0 0 00 0 0 00
0x060TIMx_AF1ResResResResResResResResResResResResResResResResResResResResResResResResResResResResResResResRes
Reset value0 0 0 0
0x064TIMx_AF2ResResResResResResResResResResResResResResResResResResResResResResResResResResResResResResResRes
Reset value0 0 0
0x068..0x3D8ReservedRes.
0x3DCTIMx_DCRResResResResResResResResResResResResResResResResResResResResResResResResResResResResResResResRes
Reset value0 0 0 0 0
0x3E0TIMx_DMARDMAB[31:0]
Reset value00000000000000000000000000000000

Refer to Section 2.2: Memory organization for the register boundary addresses.