16. General-purpose timers (TIM2/TIM3)

16.1 TIM2/TIM3 introduction

The general-purpose timers consist of a 16-bit auto-reload counter driven by a programmable prescaler.

They may be used for a variety of purposes, including measuring the pulse lengths of input signals ( input capture ) or generating output waveforms ( output compare and PWM ).

Pulse lengths and waveform periods can be modulated from a few microseconds to several milliseconds using the timer prescaler and the RCC clock controller prescalers.

The timers are completely independent, and do not share any resources. They can be synchronized together as described in Section 16.3.15 .

16.2 TIM2/TIM3 main features

General-purpose TIMx timer features include:

Figure 77. General-purpose timer block diagram

General-purpose timer block diagram showing internal clock, ETR input, ITR inputs, trigger controller, slave controller, encoder interface, auto-reload register, counter, prescalers, capture/compare registers, and output controls for four channels (CH1-CH4).

The diagram illustrates the internal architecture of a general-purpose timer (TIM2/TIM3). At the top, the internal clock (CK_INT) and TIMxCLK from RCC are inputs to the Trigger controller. The ETR input is processed through a Polarity selection & edge detector & prescaler to produce ETRP, which is then filtered by an Input filter to generate ETRF. Four ITR inputs (ITR0, ITR1, ITR2, ITR3) are combined via an ITR block to produce TRC. The TI1F_ED input is also processed through an ITR block to produce TI1F_ED. These signals (ETR, TRC, TI1F_ED) are inputs to the Trigger controller. The Trigger controller outputs TRGO to other timers and DAC/ADC. It also provides Reset, enable, up, and count signals to the Slave controller mode. The Slave controller mode is connected to the Encoder interface. The Encoder interface takes TI1FP1 and TI2FP2 as inputs. The Auto-reload register is connected to the Counter (CNT) and receives Stop, clear or up/down signals. The Counter (CNT) is a +/- CNT counter that receives CK_PSC and CK_CNT signals. The CK_PSC signal is generated by a PSC prescaler that takes CK_INT as input. The Counter (CNT) is connected to four Capture/Compare registers (CC1, CC2, CC3, CC4). Each register has a Prescaler and an Output control block. The Output control blocks generate OC1, OC2, OC3, and OC4 signals, which are TIMx_CH1, TIMx_CH2, TIMx_CH3, and TIMx_CH4 respectively. Each channel (CH1-CH4) has an Input filter & edge detector that takes TI1, TI2, TI3, and TI4 as inputs. The Input filter & edge detectors also receive TI1FP1, TI1FP2, TI2FP1, TI2FP2, TI3FP3, TI3FP4, TI4FP3, and TI4FP4 signals. The Input filter & edge detectors generate IC1, IC2, IC3, and IC4 signals, which are inputs to the Capture/Compare registers. The Capture/Compare registers also receive CC1PS, CC2PS, CC3PS, and CC4PS signals from their respective Prescalers. The Capture/Compare registers generate OC1REF, OC2REF, OC3REF, and OC4REF signals. The ETRF signal is also an input to the Counter (CNT).

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

MS19673V1

General-purpose timer block diagram showing internal clock, ETR input, ITR inputs, trigger controller, slave controller, encoder interface, auto-reload register, counter, prescalers, capture/compare registers, and output controls for four channels (CH1-CH4).

16.3 TIM2/TIM3 functional description

16.3.1 Time-base unit

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

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

The time-base unit includes:

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

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

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

Prescaler description

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

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

Timing diagram for Figure 78 showing signal transitions for CK_PSC, CEN, Timerclock, Counter register, Update event, Prescaler control register, Prescaler buffer, and Prescaler counter. The counter register values are F7, F8, F9, FA, FB, FC, 00, 01, 02, 03. The prescaler control register changes from 0 to 1. The prescaler counter counts 0, 1, 0, 1, 0, 1, 0, 1.

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

Vertical dashed lines indicate the timing relationships between the signals, showing that the prescaler division change takes effect after the update event.

MS31076V2

Timing diagram for Figure 78 showing signal transitions for CK_PSC, CEN, Timerclock, Counter register, Update event, Prescaler control register, Prescaler buffer, and Prescaler counter. The counter register values are F7, F8, F9, FA, FB, FC, 00, 01, 02, 03. The prescaler control register changes from 0 to 1. The prescaler counter counts 0, 1, 0, 1, 0, 1, 0, 1.

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

Timing diagram for Figure 79 showing signal transitions for CK_PSC, CEN, Timerclock, Counter register, Update event, Prescaler control register, Prescaler buffer, and Prescaler counter. The counter register values are F7, F8, F9, FA, FB, FC, 00, 01. The prescaler control register changes from 0 to 3. The prescaler counter counts 0, 1, 2, 3, 0, 1, 2, 3.

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

Vertical dashed lines indicate the timing relationships between the signals, showing that the prescaler division change takes effect after the update event.

MS31077V2

Timing diagram for Figure 79 showing signal transitions for CK_PSC, CEN, Timerclock, Counter register, Update event, Prescaler control register, Prescaler buffer, and Prescaler counter. The counter register values are F7, F8, F9, FA, FB, FC, 00, 01. The prescaler control register changes from 0 to 3. The prescaler counter counts 0, 1, 2, 3, 0, 1, 2, 3.

16.3.2 Counter modes

Upcounting mode

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

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

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

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

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

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

Figure 80. Counter timing diagram, internal clock divided by 1. The diagram shows the relationship between the prescaler clock (CK_PSC), counter enable (CNT_EN), timer clock (Timerclock = CK_CNT), counter register values, counter overflow, update event (UEV), and update interrupt flag (UIF).

The timing diagram illustrates the operation of a timer in upcounting mode. The signals shown are:

The diagram shows that the counter increments from 31 to 36, then overflows to 00, and continues to 07. The overflow event occurs at the transition from 36 to 00. The UEV and UIF signals are shown as pulses that occur at the same time as the overflow.

MS31078V2

Figure 80. Counter timing diagram, internal clock divided by 1. The diagram shows the relationship between the prescaler clock (CK_PSC), counter enable (CNT_EN), timer clock (Timerclock = CK_CNT), counter register values, counter overflow, update event (UEV), and update interrupt flag (UIF).

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

Timing diagram for internal clock divided by 2. It shows the relationship between CK_PSC, CNT_EN, Timerclock (CK_CNT), Counter register values, Counter overflow, 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 signals shown are:

Vertical dashed lines indicate the timing relationships between the counter register values and the other signals. The overflow, UEV, and UIF signals all go high at the transition from 0036 to 0000.

MS31079V2

Timing diagram for internal clock divided by 2. It shows the relationship between CK_PSC, CNT_EN, Timerclock (CK_CNT), Counter register values, Counter overflow, Update event (UEV), and Update interrupt flag (UIF).

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

Timing diagram for internal clock divided by 4. It shows the relationship between CK_PSC, CNT_EN, Timerclock (CK_CNT), Counter register values, Counter overflow, 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 signals shown are:

Vertical dashed lines indicate the timing relationships between the counter register values and the other signals. The overflow, UEV, and UIF signals all go high at the transition from 0036 to 0000.

MS31080V2

Timing diagram for internal clock divided by 4. It shows the relationship between CK_PSC, CNT_EN, Timerclock (CK_CNT), Counter register values, Counter overflow, Update event (UEV), and Update interrupt flag (UIF).

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

Figure 83: Counter timing diagram, internal clock divided by N. The diagram shows the relationship between the prescaler clock (CK_PSC), the timer clock (CK_CNT), the counter register, counter overflow, update event (UEV), and update interrupt flag (UIF).

Figure 83 is a timing diagram showing the relationship between the prescaler clock (CK_PSC), the timer clock (CK_CNT), the counter register, counter overflow, update event (UEV), and update interrupt flag (UIF). The diagram is divided into two main sections by a vertical dashed line.

The diagram illustrates that the counter register increments by 1 on each rising edge of the timer clock (CK_CNT). When the counter reaches its maximum value (20 in this case), it overflows to 00, triggering the counter overflow, update event (UEV), and update interrupt flag (UIF) signals.

MS31081V2

Figure 83: Counter timing diagram, internal clock divided by N. The diagram shows the relationship between the prescaler clock (CK_PSC), the timer clock (CK_CNT), the counter register, counter overflow, update event (UEV), and update interrupt flag (UIF).

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

Figure 84: Counter timing diagram, Update event when ARPE=0 (TIMx_ARR not preloaded). The diagram shows the relationship between the prescaler clock (CK_PSC), the enable signal (CEN), the timer clock (CK_CNT), the counter register, counter overflow, update event (UEV), update interrupt flag (UIF), and the auto-reload preload register.

Figure 84 is a timing diagram showing the relationship between the prescaler clock (CK_PSC), the enable signal (CEN), the timer clock (CK_CNT), the counter register, counter overflow, update event (UEV), update interrupt flag (UIF), and the auto-reload preload register. The diagram is divided into two main sections by a vertical dashed line.

The diagram illustrates that the counter register increments by 1 on each rising edge of the timer clock (CK_CNT) when CEN is high. When the counter reaches its maximum value (36 in this case), it overflows to 00, triggering the counter overflow, update event (UEV), and update interrupt flag (UIF) signals. The auto-reload preload register is updated with a new value (36) when a write is performed to TIMx_ARR.

MS31082V2

Figure 84: Counter timing diagram, Update event when ARPE=0 (TIMx_ARR not preloaded). The diagram shows the relationship between the prescaler clock (CK_PSC), the enable signal (CEN), the timer clock (CK_CNT), the counter register, counter overflow, update event (UEV), update interrupt flag (UIF), and the auto-reload preload register.

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

Figure 85. Counter timing diagram, Update event when ARPE=1 (TIMx_ARR preloaded). The diagram shows the relationship between the prescaler clock (CK_PSC), counter enable (CEN), timer clock (CK_CNT), counter register values, counter overflow, update event (UEV), update interrupt flag (UIF), and auto-reload registers (preload and shadow) over time. The counter counts from F0 to F5, then overflows to 00. The update event (UEV) and UIF flag are generated at the overflow. The auto-reload preload register is updated with a new value (36) before the counter overflows. The auto-reload shadow register is updated with the new value (36) at the overflow. The diagram also shows the counter counting from 00 to 07 after the overflow.

The diagram illustrates the timing of a general-purpose timer (TIM2/TIM3) in upcounting mode with ARPE=1. The signals shown are:

An arrow indicates the action: "Write a new value in TIMx_ARR". The diagram is labeled MS31083V2.

Figure 85. Counter timing diagram, Update event when ARPE=1 (TIMx_ARR preloaded). The diagram shows the relationship between the prescaler clock (CK_PSC), counter enable (CEN), timer clock (CK_CNT), counter register values, counter overflow, update event (UEV), update interrupt flag (UIF), and auto-reload registers (preload and shadow) over time. The counter counts from F0 to F5, then overflows to 00. The update event (UEV) and UIF flag are generated at the overflow. The auto-reload preload register is updated with a new value (36) before the counter overflows. The auto-reload shadow register is updated with the new value (36) at the overflow. The diagram also shows the counter counting from 00 to 07 after the overflow.

Downcounting mode

In downcounting mode, the counter counts from the auto-reload value (content of the TIMx_ARR register) down to 0, then restarts from the auto-reload value and generates a counter underflow event.

An Update event can be 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 auto-reload value, whereas the counter of the prescaler restarts from 0 (but the prescale rate doesn't change).

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

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

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

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

Timing diagram for internal clock divided by 1. It shows the relationship between CK_PSC, CNT_EN, Timerclock (CK_CNT), Counter register values, Counter underflow (cnt_udf), Update event (UEV), and Update interrupt flag (UIF).

This timing diagram illustrates the counter behavior when the internal clock is divided by 1. The signals shown are:

Vertical dashed lines indicate the timing relationships between the clock edges and the counter value changes. The identifier MS31184V1 is present in the bottom right corner.

Timing diagram for internal clock divided by 1. It shows the relationship between CK_PSC, CNT_EN, Timerclock (CK_CNT), Counter register values, Counter underflow (cnt_udf), Update event (UEV), and Update interrupt flag (UIF).

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

Timing diagram for internal clock divided by 2. It shows the relationship between CK_PSC, CNT_EN, Timerclock (CK_CNT), Counter register values, Counter underflow (cnt_udf), Update event (UEV), and Update interrupt flag (UIF).

This timing diagram illustrates the counter behavior when the internal clock is divided by 2. The signals shown are:

Vertical dashed lines indicate the timing relationships between the clock edges and the counter value changes. The identifier MS31185V1 is present in the bottom right corner.

Timing diagram for internal clock divided by 2. It shows the relationship between CK_PSC, CNT_EN, Timerclock (CK_CNT), Counter register values, Counter underflow (cnt_udf), Update event (UEV), and Update interrupt flag (UIF).

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

Timing diagram for internal clock divided by 4. It shows CK_PSC, CNT_EN, Timerclock = CK_CNT, Counter register (values 0001, 0000, 0000, 0001), Counter underflow, Update event (UEV), and Update interrupt flag (UIF) over time.

This timing diagram illustrates the operation of a timer when the internal clock is divided by 4. The top signal, CK_PSC, is a periodic square wave. Below it, CNT_EN is a signal that goes high to enable the counter. The Timerclock = CK_CNT signal is a square wave with a frequency one-fourth that of CK_PSC. The Counter register shows a sequence of values: 0001, 0000, 0000, and 0001. The Counter underflow signal is a pulse that occurs when the counter register transitions from 0000 to 0001. The Update event (UEV) and Update interrupt flag (UIF) are also shown as pulses that coincide with the counter underflow events. The diagram is labeled MS31186V1 in the bottom right corner.

Timing diagram for internal clock divided by 4. It shows CK_PSC, CNT_EN, Timerclock = CK_CNT, Counter register (values 0001, 0000, 0000, 0001), Counter underflow, Update event (UEV), and Update interrupt flag (UIF) over time.

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

Timing diagram for internal clock divided by N. It shows CK_PSC, Timerclock = CK_CNT, Counter register (values 20, 1F, 00, 36), Counter underflow, Update event (UEV), and Update interrupt flag (UIF) over time.

This timing diagram illustrates the operation of a timer when the internal clock is divided by an arbitrary value N. The top signal, CK_PSC, is a periodic square wave. Below it, the Timerclock = CK_CNT signal is a square wave with a frequency that is N times lower than CK_PSC. The Counter register shows a sequence of values: 20, 1F, 00, and 36. The Counter underflow signal is a pulse that occurs when the counter register transitions from 00 to 36. The Update event (UEV) and Update interrupt flag (UIF) are also shown as pulses that coincide with the counter underflow events. The diagram is labeled MS31187V1 in the bottom right corner.

Timing diagram for internal clock divided by N. It shows CK_PSC, Timerclock = CK_CNT, Counter register (values 20, 1F, 00, 36), Counter underflow, Update event (UEV), and Update interrupt flag (UIF) over time.

Figure 90. Counter timing diagram, Update event when repetition counter is not used

Timing diagram for a general-purpose timer (TIM2/TIM3) showing the relationship between the prescaler clock (CK_PSC), enable signal (CEN), timer clock (CK_CNT), counter register values, counter underflow, update event (UEV), update interrupt flag (UIF), and auto-reload preload register. The counter counts from 05 down to 00, then overflows to 36 and continues counting down. An update event (UEV) is generated when the counter reaches 00. The auto-reload preload register is shown with the value FF, and a note indicates that a new value can be written in TIMx_ARR.

The timing diagram illustrates the following signals and events:

MS31188V1

Timing diagram for a general-purpose timer (TIM2/TIM3) showing the relationship between the prescaler clock (CK_PSC), enable signal (CEN), timer clock (CK_CNT), counter register values, counter underflow, update event (UEV), update interrupt flag (UIF), and auto-reload preload register. The counter counts from 05 down to 00, then overflows to 36 and continues counting down. An update event (UEV) is generated when the counter reaches 00. The auto-reload preload register is shown with the value FF, and a note indicates that a new value can be written in TIMx_ARR.

Center-aligned mode (up/down counting)

In center-aligned mode, the counter counts from 0 to the auto-reload value (content of the TIMx_ARR register) – 1, generates a counter overflow event, then counts from the auto-reload value down to 1 and generates a counter underflow event. Then it restarts counting from 0.

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

In this mode, the 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 auto-reload 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 interrupt when clearing the counter on the capture event.

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

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

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

Timing diagram for a general-purpose timer in center-aligned mode 1. The diagram shows the relationship between the prescaler clock (CK_PSC), counter enable (CEN), timer clock (CK_CNT), counter register values, counter underflow, counter overflow, update event (UEV), and update interrupt flag (UIF).

The timing diagram illustrates the operation of a timer in center-aligned mode 1. The top signal, CK_PSC, is a periodic square wave. Below it, CEN (Counter Enable) is a horizontal line that goes high to start the timer. The Timerclock = CK_CNT signal is a square wave with a frequency twice that of CK_PSC. The Counter register shows a sequence of values: 04, 03, 02, 01, 00, 01, 02, 03, 04, 05, 06, 05, 04, 03. Vertical dashed lines mark the center of each counter period. At the first dashed line (counter value 00), the Counter underflow signal goes high. At the second dashed line (counter value 06), the Counter overflow signal goes high. The Update event (UEV) signal goes high at the first dashed line and low at the second. The Update interrupt flag (UIF) goes high at the first dashed line and remains high until the second dashed line. The diagram is labeled MS31189V1 in the bottom right corner.

Timing diagram for a general-purpose timer in center-aligned mode 1. The diagram shows the relationship between the prescaler clock (CK_PSC), counter enable (CEN), timer clock (CK_CNT), counter register values, 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 16.4.1: TIMx control register 1 (TIMx_CR1) on page 415 ).

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

Timing diagram for Figure 92 showing counter operation with internal clock divided by 2. The diagram includes signals for CK_PSC, CNT_EN, Timerclock = CK_CNT, Counter register (values 0003 to 0000 and back to 0003), Counter underflow, Update event (UEV), and Update interrupt flag (UIF).

This timing diagram illustrates the operation of a timer where the internal clock is divided by 2. The top signal, CK_PSC, is a periodic square wave. Below it, CNT_EN is a high-level enable signal. The Timerclock = CK_CNT signal is a square wave with a frequency half that of CK_PSC. The Counter register shows a sequence of values: 0003, 0002, 0001, 0000, 0001, 0002, 0003. Vertical dashed lines indicate the rising edges of the timer clock. At the transition from 0000 to 0001, the Counter underflow, Update event (UEV), and Update interrupt flag (UIF) signals all go high. The UIF signal remains high until it is manually cleared. The MS31190V1 identifier is in the bottom right corner.

Timing diagram for Figure 92 showing counter operation with internal clock divided by 2. The diagram includes signals for CK_PSC, CNT_EN, Timerclock = CK_CNT, Counter register (values 0003 to 0000 and back to 0003), Counter underflow, Update event (UEV), and Update interrupt flag (UIF).

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

Timing diagram for Figure 93 showing counter operation with internal clock divided by 4 and TIMx_ARR=0x36. The diagram includes signals for CK_PSC, CNT_EN, Timerclock = CK_CNT, Counter register (values 0034 to 0036 and back to 0035), Counter overflow, Update event (UEV), and Update interrupt flag (UIF). A note indicates that in center-aligned mode 2 or 3, the counter is updated with a UIF on overflow.

This timing diagram shows the counter operation with the internal clock divided by 4 and an auto-reload register value of 0x36. The CK_PSC signal is a periodic square wave. CNT_EN is a high-level enable signal. The Timerclock = CK_CNT signal has a frequency one-quarter that of CK_PSC. The Counter register shows values 0034, 0035, 0036, and then back to 0035. Vertical dashed lines mark the rising edges of the timer clock. At the transition from 0036 to 0035, the Counter overflow, Update event (UEV), and Update interrupt flag (UIF) signals all go high. The UIF signal remains high until cleared. A note at the bottom left states: "Note: Here, center_aligned mode 2 or 3 is updated with an UIF on overflow". The MS31191V1 identifier is in the bottom right corner.

Timing diagram for Figure 93 showing counter operation with internal clock divided by 4 and TIMx_ARR=0x36. The diagram includes signals for CK_PSC, CNT_EN, Timerclock = CK_CNT, Counter register (values 0034 to 0036 and back to 0035), Counter overflow, Update event (UEV), and Update interrupt flag (UIF). A note indicates that in center-aligned mode 2 or 3, the counter is updated with a UIF on overflow.

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

Figure 94: Counter timing diagram, internal clock divided by N. The diagram shows the relationship between the prescaler clock (CK_PSC), the timer clock (Timerclock = CK_CNT), the counter register value, the counter underflow signal, the update event (UEV), and the update interrupt flag (UIF).

Figure 94 is a timing diagram showing the relationship between the prescaler clock (CK_PSC), the timer clock (Timerclock = CK_CNT), the counter register value, the counter underflow signal, the update event (UEV), and the update interrupt flag (UIF). The diagram is divided into two main sections by a vertical dashed line.

The diagram illustrates that the counter underflow, update event, and update interrupt flag are all triggered when the counter reaches 00. The counter register value is shown in hexadecimal (20, 1F, 01, 00).

MS31192V1

Figure 94: Counter timing diagram, internal clock divided by N. The diagram shows the relationship between the prescaler clock (CK_PSC), the timer clock (Timerclock = CK_CNT), the counter register value, the counter underflow signal, the update event (UEV), and the update interrupt flag (UIF).

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

Figure 95: Counter timing diagram, Update event with ARPE=1 (counter underflow). The diagram shows the relationship between the prescaler clock (CK_PSC), the counter enable (CEN), the timer clock (Timerclock = CK_CNT), the counter register value, the counter underflow signal, the update event (UEV), the update interrupt flag (UIF), the auto-reload preload register, and the auto-reload active register.

Figure 95 is a timing diagram showing the relationship between the prescaler clock (CK_PSC), the counter enable (CEN), the timer clock (Timerclock = CK_CNT), the counter register value, the counter underflow signal, the update event (UEV), the update interrupt flag (UIF), the auto-reload preload register, and the auto-reload active register. The diagram is divided into two main sections by a vertical dashed line.

The diagram illustrates that the counter underflow, update event, and update interrupt flag are all triggered when the counter reaches 00. The counter register value is shown in hexadecimal (06, 05, 04, 03, 02, 01, 00, 01, 02, 03, 04, 05, 06, 07). The auto-reload preload register and auto-reload active register are shown with values FD and 36.

MS31193V1

Figure 95: Counter timing diagram, Update event with ARPE=1 (counter underflow). The diagram shows the relationship between the prescaler clock (CK_PSC), the counter enable (CEN), the timer clock (Timerclock = CK_CNT), the counter register value, the counter underflow signal, the update event (UEV), the update interrupt flag (UIF), the auto-reload preload register, and the auto-reload active register.

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

Figure 96. Counter timing diagram, Update event with ARPE=1 (counter overflow). The diagram shows the relationship between the prescaler clock (CK_PSC), counter enable (CEN), timer clock (CK_CNT), counter register values, counter overflow, update event (UEV), update interrupt flag (UIF), auto-reload preload register, and auto-reload active register over time. The counter register counts from F7 to 2F, overflowing at FC to 36. The update event occurs at the overflow, setting the UIF flag and updating the active register from FD to 36.

The timing diagram illustrates the operation of a general-purpose timer (TIM2/TIM3) in counter mode with ARPE=1. The signals shown are:

MS31194V1

Figure 96. Counter timing diagram, Update event with ARPE=1 (counter overflow). The diagram shows the relationship between the prescaler clock (CK_PSC), counter enable (CEN), timer clock (CK_CNT), counter register values, counter overflow, update event (UEV), update interrupt flag (UIF), auto-reload preload register, and auto-reload active register over time. The counter register counts from F7 to 2F, overflowing at FC to 36. The update event occurs at the overflow, setting the UIF flag and updating the active register from FD to 36.

16.3.3 Clock selection

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

Internal clock source (CK_INT)

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

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

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

Timing diagram for Figure 97 showing Internal clock, CEN=CNT_EN, UG, CNT_INIT, Counter clock = CK_CNT = CK_PSC, and Counter register values over time.

The diagram shows the following signals and their relationship over time:

MS31085V2

Timing diagram for Figure 97 showing Internal clock, CEN=CNT_EN, UG, CNT_INIT, Counter clock = CK_CNT = CK_PSC, and Counter register values over time.

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 98. TI2 external clock connection example

Block diagram for Figure 98 showing the TI2 external clock connection example, including Filter, Edge detector, TIMx_SMCR, and Encoder mode logic.

The diagram illustrates the connection of the TI2 input to the external clock source:

MS31196V1

Block diagram for Figure 98 showing the TI2 external clock connection example, including Filter, Edge detector, TIMx_SMCR, and Encoder mode logic.

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

  1. 1. Configure channel 2 to detect rising edges on the TI2 input by writing CC2S= '01 in the TIMx_CCMR1 register.
  2. 2. Configure the input filter duration by writing the IC2F[3:0] bits in the TIMx_CCMR1 register (if no filter is needed, keep IC2F=0000).

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

  1. 3. Select rising edge polarity by writing CC2P=0 and CC2NP=0 in the TIMx_CCER register.
  2. 4. Configure the timer in external clock mode 1 by writing SMS=111 in the TIMx_SMCR register.
  3. 5. Select TI2 as the input source by writing TS=110 in the TIMx_SMCR register.
  4. 6. Enable the counter by writing CEN=1 in the TIMx_CR1 register.

For code example, refer to A.9.1: Upcounter on TI2 rising edge code example .

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

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

Figure 99. Control circuit in external clock mode 1

Timing diagram for Figure 99. Control circuit in external clock mode 1. The diagram shows five waveforms over time. 1. TI2: A digital signal that goes high, then low, then high again. 2. CNT_EN: A signal that goes high and stays high. 3. Counter clock = CK_CNT = CK_PSC: A periodic square wave that is active only when CNT_EN is high. 4. Counter register: Shows the count values 34, 35, and 36. The count increments by 1 at each rising edge of the counter clock. 5. TIF: A flag that goes high at each rising edge of the counter clock and is reset to 0 by software. Two arrows labeled 'Write TIF=0' point to the falling edges of the TIF signal. Vertical dashed lines indicate the rising edges of TI2 and the corresponding rising edges of the counter clock. The text 'MS31087V2' is in the bottom right corner.

MS31087V2

Timing diagram for Figure 99. Control circuit in external clock mode 1. The diagram shows five waveforms over time. 1. TI2: A digital signal that goes high, then low, then high again. 2. CNT_EN: A signal that goes high and stays high. 3. Counter clock = CK_CNT = CK_PSC: A periodic square wave that is active only when CNT_EN is high. 4. Counter register: Shows the count values 34, 35, and 36. The count increments by 1 at each rising edge of the counter clock. 5. TIF: A flag that goes high at each rising edge of the counter clock and is reset to 0 by software. Two arrows labeled 'Write TIF=0' point to the falling edges of the TIF signal. Vertical dashed lines indicate the rising edges of TI2 and the corresponding rising edges of the counter clock. The text 'MS31087V2' is in the bottom right corner.

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

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

Figure 100. External trigger input block

Figure 100. External trigger input block diagram. The diagram shows the signal flow from the ETR pin through various processing stages to the CK_PSC output. The stages include an ETR pin input, an ETP (External Trigger Polarity) block, a Divider (1, 2, 4, 8) block, a Filter downcounter block, and an Encoder mode block. The ETR pin is connected to the ETP block. The ETP block is controlled by the TIMx_SMCR register (ETP). The output of the ETP block is connected to the Divider block. The Divider block is controlled by the TIMx_SMCR register (ETPS[1:0]). The output of the Divider block is ETRP, which is connected to the Filter downcounter block. The Filter downcounter block is controlled by the TIMx_SMCR register (ETF[3:0]). The output of the Filter downcounter block is ETRF, which is connected to the Encoder mode block. The Encoder mode block is controlled by the TIMx_SMCR register (ECE, SMS[2:0]). The Encoder mode block also receives inputs from TI2F or TI1F (rising or falling edges), TRGI (rising edge), and CK_INT (internal clock). The output of the Encoder mode block is CK_PSC.
Figure 100. External trigger input block diagram. The diagram shows the signal flow from the ETR pin through various processing stages to the CK_PSC output. The stages include an ETR pin input, an ETP (External Trigger Polarity) block, a Divider (1, 2, 4, 8) block, a Filter downcounter block, and an Encoder mode block. The ETR pin is connected to the ETP block. The ETP block is controlled by the TIMx_SMCR register (ETP). The output of the ETP block is connected to the Divider block. The Divider block is controlled by the TIMx_SMCR register (ETPS[1:0]). The output of the Divider block is ETRP, which is connected to the Filter downcounter block. The Filter downcounter block is controlled by the TIMx_SMCR register (ETF[3:0]). The output of the Filter downcounter block is ETRF, which is connected to the Encoder mode block. The Encoder mode block is controlled by the TIMx_SMCR register (ECE, SMS[2:0]). The Encoder mode block also receives inputs from TI2F or TI1F (rising or falling edges), TRGI (rising edge), and CK_INT (internal clock). The output of the Encoder mode block is CK_PSC.

MS33116V1

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

  1. 1. As no filter is needed in this example, write ETF[3:0]=0000 in the TIMx_SMCR register.
  2. 2. Set the prescaler by writing ETPS[1:0]=01 in the TIMx_SMCR register
  3. 3. Select rising edge detection on the ETR pin by writing ETP=0 in the TIMx_SMCR register
  4. 4. Enable external clock mode 2 by writing ECE=1 in the TIMx_SMCR register.
  5. 5. Enable the counter by writing CEN=1 in the TIMx_CR1 register.

For code example, refer to A.9.2: Up counter on each 2 ETR rising edges code example .

The counter counts once each 2 ETR rising edges.

The delay between the rising edge on ETR and the actual clock of the counter is due to the resynchronization circuit on the ETRP signal.

Figure 101. Control circuit in external clock mode 2

Timing diagram for Figure 101 showing the control circuit in external clock mode 2. The diagram tracks several signals: f_CK_INT (internal clock), CNT_EN (counter enable), ETR (external trigger), ETRP (prescaled trigger), ETRF (filtered trigger), Counter clock (CK_INT = CK_PSC), and the Counter register. The Counter register increments from 34 to 35 and then to 36 on the rising edges of the Counter clock pulses, which are derived from the ETRF signal transitions synchronized with f_CK_INT.

Timing diagram showing the control circuit in external clock mode 2. The diagram illustrates the relationship between the internal clock ( \( f_{CK\_INT} \) ), counter enable (CNT_EN), external trigger (ETR), external trigger prescaler (ETRP), external trigger filter (ETRF), counter clock (Counter clock = \( CK\_INT = CK\_PSC \) ), and the counter register values (34, 35, 36). The counter register values are shown in boxes separated by 'X' marks, indicating transitions. The counter clock pulses occur following transitions in the ETRF signal, leading to increments in the counter register.

MS3311V2

Timing diagram for Figure 101 showing the control circuit in external clock mode 2. The diagram tracks several signals: f_CK_INT (internal clock), CNT_EN (counter enable), ETR (external trigger), ETRP (prescaled trigger), ETRF (filtered trigger), Counter clock (CK_INT = CK_PSC), and the Counter register. The Counter register increments from 34 to 35 and then to 36 on the rising edges of the Counter clock pulses, which are derived from the ETRF signal transitions synchronized with f_CK_INT.

16.3.4 Capture/compare channels

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

The following figure gives an overview of one Capture/Compare channel.

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

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

Figure 102: Capture/compare channel (example: channel 1 input stage) block diagram. The diagram shows the input stage for channel 1. TI1 is filtered by a downcounter (f_bts) to produce TI1F. TI1F is then processed by an edge detector to produce TI1F_Rising and TI1F_Falling signals. These signals are multiplexed with TI2F_Rising and TI2F_Falling (from channel 2) to produce TI1FP1 and TI2FP1. TI1F_ED is generated by an OR gate of TI1F_Rising and TI1F_Falling and is sent to the slave mode controller. TI1FP1 and TI2FP1 are multiplexed to produce IC1. IC1 is then divided by a divider (/1, /2, /4, /8) to produce IC1PS. Control signals include ICF[3:0] from TIMx_CCMR1, CC1P/CC1NP from TIMx_CCER, and CC1S[1:0], ICPS[1:0], and CC1E from TIMx_CCMR1 and TIMx_CCER. A TRC signal from the slave mode controller is also shown. MS33115V1
Figure 102: Capture/compare channel (example: channel 1 input stage) block diagram. The diagram shows the input stage for channel 1. TI1 is filtered by a downcounter (f_bts) to produce TI1F. TI1F is then processed by an edge detector to produce TI1F_Rising and TI1F_Falling signals. These signals are multiplexed with TI2F_Rising and TI2F_Falling (from channel 2) to produce TI1FP1 and TI2FP1. TI1F_ED is generated by an OR gate of TI1F_Rising and TI1F_Falling and is sent to the slave mode controller. TI1FP1 and TI2FP1 are multiplexed to produce IC1. IC1 is then divided by a divider (/1, /2, /4, /8) to produce IC1PS. Control signals include ICF[3:0] from TIMx_CCMR1, CC1P/CC1NP from TIMx_CCER, and CC1S[1:0], ICPS[1:0], and CC1E from TIMx_CCMR1 and TIMx_CCER. A TRC signal from the slave mode controller is also shown. MS33115V1

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

Figure 103. Capture/compare channel 1 main circuit

Figure 103: Capture/compare channel 1 main circuit block diagram. The diagram shows the main circuit for channel 1. It includes an APB Bus connected to an MCU-peripheral interface. The interface is connected to a Capture/compare preload register and a Capture/compare shadow register. The preload register is loaded from the APB Bus (high/low 16-bit) and its output is compared with the Counter value in a Comparator. The Comparator outputs CNT>CCR1 and CNT=CCR1 to the time base unit. The shadow register is loaded from the preload register via capture_transfer or compare_transfer signals. The input mode is determined by CC1S[1] and CC1S[0]. The capture/compare function is enabled by IC1PS and CC1E. The output mode is determined by CC1S[1] and CC1S[0]. The output polarity is determined by OC1PE. The output is generated by CC1G from TIMx_EGR. Control signals include Read CCR1H, Read CCR1L, write_in_progress, write CCR1H, write CCR1L, CC1S[1], CC1S[0], OC1PE, UEV (from time base unit), TIMx_CCMR1, and TIMx_EGR. MS33144V1
Figure 103: Capture/compare channel 1 main circuit block diagram. The diagram shows the main circuit for channel 1. It includes an APB Bus connected to an MCU-peripheral interface. The interface is connected to a Capture/compare preload register and a Capture/compare shadow register. The preload register is loaded from the APB Bus (high/low 16-bit) and its output is compared with the Counter value in a Comparator. The Comparator outputs CNT>CCR1 and CNT=CCR1 to the time base unit. The shadow register is loaded from the preload register via capture_transfer or compare_transfer signals. The input mode is determined by CC1S[1] and CC1S[0]. The capture/compare function is enabled by IC1PS and CC1E. The output mode is determined by CC1S[1] and CC1S[0]. The output polarity is determined by OC1PE. The output is generated by CC1G from TIMx_EGR. Control signals include Read CCR1H, Read CCR1L, write_in_progress, write CCR1H, write CCR1L, CC1S[1], CC1S[0], OC1PE, UEV (from time base unit), TIMx_CCMR1, and TIMx_EGR. MS33144V1

Figure 104. Output stage of capture/compare channel (channel 1)

Figure 104. Output stage of capture/compare channel (channel 1). The diagram shows the internal logic of the output stage. At the top, TIMx_SMCR register contains the OCCS bit. Below it, a multiplexer selects between OCREF_CLR (input 0) and ETRF (input 1) to generate the ocref_clr_int signal. This signal, along with counter comparison signals (CNT > CCR1 and CNT = CCR1), is fed into the Output mode controller. The controller also receives OC1M[2:0] from the TIMx_CCMR1 register. The controller outputs OC1REF, which is connected to the master mode controller and also to a second multiplexer. This second multiplexer selects between OC1REF (input 0) and its inverted version (input 1) based on the CC1P bit from the TIMx_CCER register. The output of this multiplexer is then passed through an Output enable circuit, which is controlled by the CC1E bit from the TIM1_CCER register, resulting in the final OC1 output.
Figure 104. Output stage of capture/compare channel (channel 1). The diagram shows the internal logic of the output stage. At the top, TIMx_SMCR register contains the OCCS bit. Below it, a multiplexer selects between OCREF_CLR (input 0) and ETRF (input 1) to generate the ocref_clr_int signal. This signal, along with counter comparison signals (CNT > CCR1 and CNT = CCR1), is fed into the Output mode controller. The controller also receives OC1M[2:0] from the TIMx_CCMR1 register. The controller outputs OC1REF, which is connected to the master mode controller and also to a second multiplexer. This second multiplexer selects between OC1REF (input 0) and its inverted version (input 1) based on the CC1P bit from the TIMx_CCER register. The output of this multiplexer is then passed through an Output enable circuit, which is controlled by the CC1E bit from the TIM1_CCER register, resulting in the final OC1 output.

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.

16.3.5 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 over-capture flag CCxOF (TIMx_SR register) is set. CCxIF can be cleared by software by writing it to 0 or by reading the captured data stored in the TIMx_CCRx register. CCxOF is cleared when it is written with 0.

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

  1. 1. Select the active input: TIMx_CCR1 must be linked to the TI1 input, so write the CC1S bits to 01 in the TIMx_CCMR1 register. As soon as CC1S becomes different from 00, the channel is configured in input and the TIMx_CCR1 register becomes read-only.
  2. 2. Program the appropriate input filter duration in relation with the signal connected to the timer (when the input is one of the TIx (ICxF bits in the TIMx_CCMRx register). Let's imagine that, when toggling, the input signal is not stable during at most 5 internal clock cycles. We must program a filter duration longer than these 5 clock cycles. We can validate a transition on TI1 when 8 consecutive samples with the new level have been

detected (sampled at \( f_{DTS} \) frequency). Then write IC1F bits to 0011 in the TIMx_CCMR1 register.

  1. 3. Select the edge of the active transition on the TI1 channel by writing the CC1P and CC1NP bits to 00 in the TIMx_CCER register (rising edge in this case).
  2. 4. Program the input prescaler. In our example, we wish the capture to be performed at each valid transition, so the prescaler is disabled (write IC1PS bits to 00 in the TIMx_CCMR1 register).
  3. 5. Enable capture from the counter into the capture register by setting the CC1E bit in the TIMx_CCER register.
  4. 6. 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.

For code example, refer to A.9.3: Input capture configuration code example .

When an input capture occurs:

For code example, refer to A.9.4: Input capture data management code example .

In order to handle the overcapture, it is recommended to read the data before the overcapture flag. This is to avoid missing an overcapture which could happen after reading the flag and before reading the data.

Note: IC interrupt and/or DMA requests can be generated by software by setting the corresponding CCxG bit in the TIMx_EGR register.

16.3.6 PWM input mode

This mode is a particular case of input capture mode. The procedure is the same except:

For example, one can measure the period (in TIMx_CCR1 register) and the duty cycle (in TIMx_CCR2 register) of the PWM applied on TI1 using the following procedure (depending on CK_INT frequency and prescaler value):

  1. 1. Select the active input for TIMx_CCR1: write the CC1S bits to 01 in the TIMx_CCMR1 register (TI1 selected).
  2. 2. Select the active polarity for 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).
  3. 3. Select the active input for TIMx_CCR2: write the CC2S bits to 10 in the TIMx_CCMR1 register (TI1 selected).
  4. 4. Select the active polarity for TI1FP2 (used for capture in TIMx_CCR2): write the CC2P bit to '1' and the CC2NP bit to '0' (active on falling edge).
  5. 5. Select the valid trigger input: write the TS bits to 101 in the TIMx_SMCR register (TI1FP1 selected).
  6. 6. Configure the slave mode controller in reset mode: write the SMS bits to 100 in the TIMx_SMCR register.
  7. 7. Enable the captures: write the CC1E and CC2E bits to '1' in the TIMx_CCER register.

For code example, refer to A.9.5: PWM input configuration code example .

Figure 105. PWM input mode timing

Timing diagram for PWM input mode showing TI1, TIMx_CNT, TIMx_CCR1, and TIMx_CCR2 signals over time. The diagram illustrates the capture of rising and falling edges to measure pulse width and period.

The timing diagram shows four horizontal lines representing signals over time. The top line is TI1, showing a PWM signal. The second line is TIMx_CNT, showing a counter that increments from 0000 to 0004, then resets to 0000 and increments again. The third line is TIMx_CCR1, which captures the value 0004 at the first rising edge. The fourth line is TIMx_CCR2, which captures the value 0002 at the first falling edge. Below the TIMx_CNT line, three annotations with arrows point to specific events: 'IC1 capture IC2 capture reset counter' points to the first rising edge, 'IC2 capture pulse width measurement' points to the first falling edge, and 'IC1 capture period measurement' points to the second rising edge. The identifier 'ai15413' is in the bottom right corner.

Timing diagram for PWM input mode showing TI1, TIMx_CNT, TIMx_CCR1, and TIMx_CCR2 signals over time. The diagram illustrates the capture of rising and falling edges to measure pulse width and period.

16.3.7 Forced output mode

In output mode (CCxS bits = 00 in the TIMx_CCMRx register), each output compare signal (OCxREF and then 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 (OCxREF/OCx) to its active level, one just needs to write 101 in the OCxM bits in the corresponding TIMx_CCMRx register. Thus OCxREF is forced high (OCxREF is always active high) and OCx get opposite value to CCxP polarity bit.

e.g.: CCxP=0 (OCx active high) => OCx is forced to high level.

OCxREF signal can be forced low by writing the OCxM bits to 100 in the TIMx_CCMRx register.

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

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

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

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

Procedure:

  1. 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, one must write OCxM=011, OCxPE=0, CCxP=0 and CCxE=1 to toggle OCx output pin when CNT matches CCRx, CCRx preload is not used, OCx is enabled and active high.
  5. 5. Enable the counter by setting the CEN bit in the TIMx_CR1 register.

For code example, refer to A.9.7: Output compare configuration code example .

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

Figure 106. Output compare mode, toggle on OC1.

Timing diagram for Output compare mode, toggle on OC1. The diagram shows three horizontal timelines: TIM1_CNT, TIM1_CCR1, and OC1REF=OC1. TIM1_CNT shows a sequence of values: 0039, 003A, 003B, followed by a gap, then B200, B201. TIM1_CCR1 shows values 003A and B201. OC1REF=OC1 is a square wave that toggles state at each match point (when TIM1_CNT equals TIM1_CCR1). Arrows indicate that writing B201h in the CC1R register updates the TIM1_CCR1 register, and that matches on CCR1 generate an interrupt if enabled.

Write B201h in the CC1R register

TIM1_CNT: 0039 | 003A | 003B | ... | B200 | B201

TIM1_CCR1: 003A | B201

OC1REF=OC1

Match detected on CCR1
Interrupt generated if enabled

MS31092V1

Timing diagram for Output compare mode, toggle on OC1. The diagram shows three horizontal timelines: TIM1_CNT, TIM1_CCR1, and OC1REF=OC1. TIM1_CNT shows a sequence of values: 0039, 003A, 003B, followed by a gap, then B200, B201. TIM1_CCR1 shows values 003A and B201. OC1REF=OC1 is a square wave that toggles state at each match point (when TIM1_CNT equals TIM1_CCR1). Arrows indicate that writing B201h in the CC1R register updates the TIM1_CCR1 register, and that matches on CCR1 generate an interrupt if enabled.

16.3.9 PWM mode

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

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

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

OCx polarity is software programmable using the CCxP bit in the TIMx_CCER register. It can be programmed as active high or active low. OCx output is enabled by 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). However, to comply with the OCREF_CLR functionality (OCREF can be cleared by an external event through the ETR signal until the next PWM period), the OCREF signal is asserted only:

This forces the PWM by software while the timer is running.

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

Upcounting configuration

Upcounting is active when the DIR bit in the TIMx_CR1 register is low. Refer to Section : Upcounting mode on page 375 .

In the following example, we consider PWM mode 1. The reference PWM signal 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 auto-reload value (in TIMx_ARR) then OCxREF is held at '1'. If the compare value is 0 then OCxREF is held at '0'. Figure 107 shows some edge-aligned PWM waveforms in an example where TIMx_ARR=8.

For code example, refer to A.9.8: Edge-aligned PWM configuration example .

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

Timing diagram showing edge-aligned PWM waveforms for different compare register (CCR) values. The top row shows the Counter register values from 0 to 8, then repeating 0 to 1. Below are four sets of waveforms for CCRx=4, CCRx=8, CCRx>8, and CCRx=0. Each set includes the OCxREF signal and the CCxIF interrupt signal. Vertical dashed lines indicate the counter values 0, 4, 8, and 0 again. For CCRx=4, OCxREF is high from counter 0 to 3 and low from 4 to 8. For CCRx=8, OCxREF is high from 0 to 7 and low at 8. For CCRx>8, OCxREF is always high. For CCRx=0, OCxREF is always low. CCxIF signals are shown as pulses when the counter matches the CCR value.

The figure is a timing diagram illustrating edge-aligned PWM waveforms for a timer with an auto-reload register (ARR) set to 8. The top row shows the Counter register values over time: 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1. Vertical dashed lines mark the counter values 0, 4, 8, and 0 again. Below the counter values, four sets of waveforms are shown for different compare register (CCR) values:

MS31093V1

Timing diagram showing edge-aligned PWM waveforms for different compare register (CCR) values. The top row shows the Counter register values from 0 to 8, then repeating 0 to 1. Below are four sets of waveforms for CCRx=4, CCRx=8, CCRx>8, and CCRx=0. Each set includes the OCxREF signal and the CCxIF interrupt signal. Vertical dashed lines indicate the counter values 0, 4, 8, and 0 again. For CCRx=4, OCxREF is high from counter 0 to 3 and low from 4 to 8. For CCRx=8, OCxREF is high from 0 to 7 and low at 8. For CCRx>8, OCxREF is always high. For CCRx=0, OCxREF is always low. CCxIF signals are shown as pulses when the counter matches the CCR value.

Downcounting configuration

Downcounting is active when DIR bit in TIMx_CR1 register is high. Refer to Section : Downcounting mode on page 378 .

In PWM mode 1, the reference signal OCxREF is low as long as TIMx_CNT > TIMx_CCRx else it becomes high. If the compare value in TIMx_CCRx is greater than the auto-reload value in TIMx_ARR, then OCxREF is held at '1. 0% PWM is not possible in this mode.

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 OCxREF/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 Section : Center-aligned mode (up/down counting) on page 381 .

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

For code example, refer to A.9.9: Center-aligned PWM configuration example .

Figure 108. 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 shows the counter register values, OCxREF signal levels, and CCxIF interrupt flags for various CMS settings.

The figure illustrates the timing of center-aligned PWM waveforms for a timer with an Auto-Reload Register (ARR) value of 8. The counter register (CNT) is shown with values from 0 to 8, then back down to 0, and then back up to 1. Vertical dashed lines indicate the positions of the Capture/Compare Register (CCR) values.

AI14681b

Timing diagram showing center-aligned PWM waveforms for different CCRx values (4, 7, 8, >8, 0) with ARR=8. The diagram shows the counter register values, OCxREF signal levels, and CCxIF interrupt flags for various CMS settings.

Hints on using center-aligned mode:

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

Figure 109. Example of one-pulse mode.

Timing diagram for one-pulse mode. The diagram shows four waveforms over time (t). 1. TI2: A positive pulse that triggers the counter. 2. OC1REF: The reference output, which is high when the counter is below CCR1 and low otherwise. 3. OC1: The output compare signal, which goes high when the counter reaches CCR1 and low when it reaches ARR. 4. Counter: A sawtooth-like waveform that starts at 0, increments in steps until it reaches ARR, then resets to 0. The time from the rising edge of TI2 to the rising edge of OC1 is labeled t_DELAY. The time from the rising edge of OC1 to its falling edge is labeled t_PULSE. The maximum counter value is labeled TIM1_ARR, and the compare value is labeled TIM1_CCR1. The diagram is labeled MS31099V1 in the bottom right corner.
Timing diagram for one-pulse mode. The diagram shows four waveforms over time (t). 1. TI2: A positive pulse that triggers the counter. 2. OC1REF: The reference output, which is high when the counter is below CCR1 and low otherwise. 3. OC1: The output compare signal, which goes high when the counter reaches CCR1 and low when it reaches ARR. 4. Counter: A sawtooth-like waveform that starts at 0, increments in steps until it reaches ARR, then resets to 0. The time from the rising edge of TI2 to the rising edge of OC1 is labeled t_DELAY. The time from the rising edge of OC1 to its falling edge is labeled t_PULSE. The maximum counter value is labeled TIM1_ARR, and the compare value is labeled TIM1_CCR1. The diagram is labeled MS31099V1 in the bottom right corner.

For example one may want to generate a positive pulse on OC1 with a length of \( t_{PULSE} \) and after a delay of \( t_{DELAY} \) as soon as a positive edge is detected on the TI2 input pin.

Let's use TI2FP2 as trigger 1:

  1. 1. Map TI2FP2 on TI2 by writing CC2S=01 in the TIMx_CCMR1 register.
  2. 2. TI2FP2 must detect a rising edge, write CC2P=0 and CC2NP='0' in the TIMx_CCER register.
  3. 3. Configure TI2FP2 as trigger for the slave mode controller (TRGI) by writing TS=110 in the TIMx_SMCR register.
  4. 4. TI2FP2 is used to start the counter by writing SMS to '110 in the TIMx_SMCR register (trigger mode).

For code example, refer to A.9.16: One-Pulse mode code example .

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

In our example, the DIR and CMS bits in the TIMx_CR1 register should be low.

Since only 1 pulse (Single mode) is needed, a 1 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 auto-reload value back to 0). When OPM bit in the TIMx_CR1 register is set to '0', so the Repetitive Mode is selected.

Particular case: OCx fast enable:

In One-pulse mode, the edge detection on TIx input set the CEN bit which enables the counter. Then the comparison between the counter and the compare value makes the output toggle. But several clock cycles are needed for these operations and it limits the minimum delay \( 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 OCxRef (and OCx) is forced in response to the stimulus, without taking in account the comparison. Its new level is the same as if a compare match had occurred. OCxFE acts only if the channel is configured in PWM1 or PWM2 mode.

For code example, refer to A.9.16: One-Pulse mode code example .

16.3.11 Clearing the OCxREF signal on an external event

  1. 1. The external trigger prescaler should 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 TIMx_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.

For code example, refer to A.9.10: ETR configuration to clear OCxREF code example .

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

Figure 110. Clearing TIMx OCxREF

Timing diagram showing the relationship between Counter (CNT), ETRF, and OCxREF signals. The Counter (CNT) is a sawtooth wave. ETRF is a pulse that goes high when the counter overflows. OCxREF (OCxCE = '0') is a signal that is high when the counter is below the compare value (CCRx) and low otherwise. OCxREF (OCxCE = '1') is a signal that is high when the counter is below the compare value (CCRx) and low otherwise. The diagram shows that OCxREF_CLR becomes high at the first overflow and remains high at the second overflow because the counter is still high (100% duty cycle).

The figure is a timing diagram illustrating the clearing of the TIMx OCxREF signal. It consists of four horizontal signal lines:

Vertical dashed lines indicate key timing points. Arrows point to specific events on the OCxREF (OCxCE = '1') line:The diagram shows that in the case of a PWM with a 100% duty cycle (where CCRx > ARR), the OCxREF signal remains high through multiple overflow events as long as the counter is still high at the overflow point.

Timing diagram showing the relationship between Counter (CNT), ETRF, and OCxREF signals. The Counter (CNT) is a sawtooth wave. ETRF is a pulse that goes high when the counter overflows. OCxREF (OCxCE = '0') is a signal that is high when the counter is below the compare value (CCRx) and low otherwise. OCxREF (OCxCE = '1') is a signal that is high when the counter is below the compare value (CCRx) and low otherwise. The diagram shows that OCxREF_CLR becomes high at the first overflow and remains high at the second overflow because the counter is still high (100% duty cycle).
  1. 1. In case of a PWM with a 100% duty cycle (if \( CCRx > ARR \) ), OCxREF is enabled again at the next counter overflow.

16.3.12 Encoder interface mode

To select Encoder Interface mode write \( SMS='001' \) in the TIMx_SMCR register if the counter is counting on TI2 edges only, \( SMS=010 \) if it is counting on TI1 edges only and \( SMS=011 \) if it is counting on both TI1 and TI2 edges.

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

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

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

In this mode, the counter is modified automatically following the speed and the direction of the incremental encoder and its content, therefore, always represents the encoder's

position. The count direction correspond to the rotation direction of the connected sensor. The table summarizes the possible combinations, assuming TI1 and TI2 do not switch at the same time.

Table 76. Counting direction versus encoder signals

Active edgeLevel on opposite signal (TI1FP1 for TI2, TI2FP2 for TI1)TI1FP1 signalTI2FP2 signal
RisingFallingRisingFalling
Counting on TI1 onlyHighDownUpNo CountNo Count
LowUpDownNo CountNo Count
Counting on TI2 onlyHighNo CountNo CountUpDown
LowNo CountNo CountDownUp
Counting on TI1 and TI2HighDownUpUpDown
LowUpDownDownUp

An external incremental 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 indicate the mechanical zero position, may be connected to an external interrupt input and trigger a counter reset.

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

For code example, refer to A.9.11: Encoder interface code example .

Figure 111. Example of counter operation in encoder interface mode

Timing diagram for Figure 111 showing TI1, TI2, and Counter signals over time. The diagram is divided into five phases: forward, jitter, backward, jitter, and forward. In the forward phases, the Counter increases (up). In the backward phases, the Counter decreases (down). The jitter phases show brief reversals in direction. The TI1 and TI2 signals are square waves that are out of phase by 90 degrees. The Counter signal is a staircase-like waveform. The diagram is labeled MS33107V1.
Timing diagram for Figure 111 showing TI1, TI2, and Counter signals over time. The diagram is divided into five phases: forward, jitter, backward, jitter, and forward. In the forward phases, the Counter increases (up). In the backward phases, the Counter decreases (down). The jitter phases show brief reversals in direction. The TI1 and TI2 signals are square waves that are out of phase by 90 degrees. The Counter signal is a staircase-like waveform. The diagram is labeled MS33107V1.

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

Figure 112. Example of encoder interface mode with TI1FP1 polarity inverted

Timing diagram for Figure 112 showing TI1, TI2, and Counter signals over time. The diagram is divided into five phases: forward, jitter, backward, jitter, and forward. In the forward phases, the Counter decreases (down). In the backward phases, the Counter increases (up). The jitter phases show brief reversals in direction. The TI1 and TI2 signals are square waves that are out of phase by 90 degrees. The Counter signal is a staircase-like waveform. The diagram is labeled MS33108V1.
Timing diagram for Figure 112 showing TI1, TI2, and Counter signals over time. The diagram is divided into five phases: forward, jitter, backward, jitter, and forward. In the forward phases, the Counter decreases (down). In the backward phases, the Counter increases (up). The jitter phases show brief reversals in direction. The TI1 and TI2 signals are square waves that are out of phase by 90 degrees. The Counter signal is a staircase-like waveform. The diagram is labeled MS33108V1.

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 generated by a Real-Time clock.

16.3.13 Timer input XOR function

The TI1S bit in the TIMx_CR2 register, allows the input filter of channel 1 to be connected to the output of a XOR gate, combining the three input pins TIMx_CH1 to TIMx_CH3.

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

16.3.14 Timers and external trigger synchronization

The TIMx Timers can be synchronized with an external trigger in several modes: Reset mode, Gated mode and Trigger mode.

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 TI1 input:

For code example, refer to A.9.12: Reset mode code example .

The counter starts counting on the internal clock, then behaves normally until TI1 rising edge. When 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 auto-reload register TIMx_ARR=0x36. The delay between the rising edge on TI1 and the actual reset of the counter is due to the resynchronization circuit on TI1 input.

Figure 113. Control circuit in reset mode

Timing diagram showing the control circuit in reset mode. The diagram illustrates the relationship between the TI1 input signal, the Update Generation (UG) signal, the Counter clock (ck_cnt = ck_psc), the Counter register values, and the Trigger Interrupt Flag (TIF) signal. The TI1 signal shows a rising edge. The UG signal is a pulse that occurs when the counter reaches the auto-reload value (0x36). The Counter clock is a periodic square wave. The Counter register shows a sequence of values: 30, 31, 32, 33, 34, 35, 36, 00, 01, 02, 03, 00, 01, 02, 03. The TIF signal is a pulse that occurs when the counter reaches the auto-reload value (0x36).

The timing diagram shows the following signals and their relationship:

The diagram illustrates that the counter starts counting from 00 and increments until it reaches the auto-reload value of 36. At this point, the UG and TIF signals pulse, and the counter resets to 00. The TI1 rising edge occurs after the counter has already reset, indicating that the reset is triggered by the counter's own overflow condition when configured in reset mode.

MS31401V2

Timing diagram showing the control circuit in reset mode. The diagram illustrates the relationship between the TI1 input signal, the Update Generation (UG) signal, the Counter clock (ck_cnt = ck_psc), the Counter register values, and the Trigger Interrupt Flag (TIF) signal. The TI1 signal shows a rising edge. The UG signal is a pulse that occurs when the counter reaches the auto-reload value (0x36). The Counter clock is a periodic square wave. The Counter register shows a sequence of values: 30, 31, 32, 33, 34, 35, 36, 00, 01, 02, 03, 00, 01, 02, 03. The TIF signal is a pulse that occurs when the counter reaches the auto-reload value (0x36).

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 TI1 input is low:

  1. 1. Configure the channel 1 to detect low levels on 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 TI1 as the input source by writing TS=101 in TIMx_SMCR register.
  3. 3. Enable the counter by writing CEN=1 in the TIMx_CR1 register (in gated mode, the counter doesn't start if CEN=0, whatever is the trigger input level).

For code example, refer to A.9.13: Gated mode code example .

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

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

Figure 114. Control circuit in gated mode

Timing diagram for Figure 114. Control circuit in gated mode. The diagram shows five waveforms over time. 1. TI1: A signal that starts high, goes low, then high again, then low again, and finally stays high. 2. cnt_en: Counter enable signal. It is low while TI1 is high. When TI1 goes low, cnt_en goes high. When TI1 goes high, cnt_en goes low. 3. Counter clock = ck_cnt = ck_psc: A periodic square wave clock. It is active (pulsing) when cnt_en is high and stops (becomes flat) when cnt_en is low. 4. Counter register: Shows the count values. It starts at 30 and increments (31, 32, 33) while TI1 is low. When TI1 goes high, the count stops at 34. When TI1 goes low again, it resumes counting (35, 36, 37, 38). 5. TIF: Timer interrupt flag. It is set (goes high) when the counter starts (when TI1 goes low) and when it stops (when TI1 goes high). Arrows labeled 'Write TIF=0' point to the falling edges of the TIF signal, indicating where it can be manually cleared.
Timing diagram for Figure 114. Control circuit in gated mode. The diagram shows five waveforms over time. 1. TI1: A signal that starts high, goes low, then high again, then low again, and finally stays high. 2. cnt_en: Counter enable signal. It is low while TI1 is high. When TI1 goes low, cnt_en goes high. When TI1 goes high, cnt_en goes low. 3. Counter clock = ck_cnt = ck_psc: A periodic square wave clock. It is active (pulsing) when cnt_en is high and stops (becomes flat) when cnt_en is low. 4. Counter register: Shows the count values. It starts at 30 and increments (31, 32, 33) while TI1 is low. When TI1 goes high, the count stops at 34. When TI1 goes low again, it resumes counting (35, 36, 37, 38). 5. TIF: Timer interrupt flag. It is set (goes high) when the counter starts (when TI1 goes low) and when it stops (when TI1 goes high). Arrows labeled 'Write TIF=0' point to the falling edges of the TIF signal, indicating where it can be manually cleared.
  1. 1. 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 TI2 input:

  1. 1. Configure the channel 2 to detect rising edges on 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 TI2 as the input source by writing TS=110 in TIMx_SMCR register.

For code example, refer to A.9.14: Trigger mode code example .

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

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

Figure 115. Control circuit in trigger mode

Timing diagram showing the control circuit in trigger mode. It displays five signals over time: TI2 (input), cnt_en (enable), Counter clock = ck_cnt = ck_psc (clock), Counter register (count), and TIF (flag). A vertical dashed line marks the rising edge of TI2. Before the edge, TI2 is high, cnt_en is low, the clock is off, the counter register shows 34, and TIF is low. Immediately after the rising edge, cnt_en goes high, the clock starts, the counter register increments to 35, 36, 37, and 38, and TIF goes high. The diagram is labeled MS31403V1 in the bottom right corner.

Timing diagram illustrating the control circuit in trigger mode. The diagram shows five signals over time, with a vertical dashed line indicating the rising edge of the TI2 input signal.

The diagram is labeled MS31403V1 in the bottom right corner.

Timing diagram showing the control circuit in trigger mode. It displays five signals over time: TI2 (input), cnt_en (enable), Counter clock = ck_cnt = ck_psc (clock), Counter register (count), and TIF (flag). A vertical dashed line marks the rising edge of TI2. Before the edge, TI2 is high, cnt_en is low, the clock is off, the counter register shows 34, and TIF is low. Immediately after the rising edge, cnt_en goes high, the clock starts, the counter register increments to 35, 36, 37, and 38, and TIF goes high. The diagram is labeled MS31403V1 in the bottom right corner.

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 ETR 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 ETR as TRGI through the TS bits of TIMx_SMCR register.

In the following example, the upcounter is incremented at each rising edge of the ETR signal as soon as a rising edge of 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 ETR 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 TI1 as the input source by writing TS=101 in TIMx_SMCR register.

For code example, refer to A.9.15: External clock mode 2 + trigger mode code example .

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

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

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

Timing diagram for Figure 116 showing signals TI1, CEN/CNT_EN, ETR, Counter clock, Counter register, and TIF over time. The counter register values 34, 35, and 36 are shown at the bottom.

The diagram shows the relationship between several signals over time. TI1 is a signal that goes high and then low. CEN/CNT_EN is a signal that goes high when TI1 goes high and stays high. ETR is a periodic square wave. Counter clock = CK_CNT = CK_PSC is a signal that is high when ETR is high and low when ETR is low. Counter register shows values 34, 35, and 36. The counter increments from 34 to 35 when ETR goes high, and from 35 to 36 when ETR goes high again. TIF is a signal that goes high when TI1 goes high and stays high.

Timing diagram for Figure 116 showing signals TI1, CEN/CNT_EN, ETR, Counter clock, Counter register, and TIF over time. The counter register values 34, 35, and 36 are shown at the bottom.

16.3.15 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 117: Master/Slave timer example presents an overview of the trigger selection and the master mode selection blocks.

Note: The clock of the slave timer 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.

Using one timer as prescaler for another timer

Figure 117. Master/Slave timer example

Block diagram for Figure 117 showing TIMx (Master) and TIMy (Slave) connected via TRGO1 and ITR1. TIMx includes a Prescaler, Counter, and Master mode control. TIMy includes an Input trigger selection, Slave mode control, CK_PSC, Prescaler, and Counter.

The diagram shows two timers, TIMx and TIMy . TIMx is configured as a Master timer. It has a Clock input, a Prescaler , a Counter , and a Master mode control block. The Master mode control block has a UEV input and a TRGO1 output. The TRGO1 output is connected to the ITR1 input of TIMy . TIMy is configured as a Slave timer. It has an Input trigger selection block, a Slave mode control block, a CK_PSC input, a Prescaler , and a Counter . The Input trigger selection block has a TS input and is connected to the ITR1 input of the Slave mode control block. The Slave mode control block has an SMS input and is connected to the CK_PSC input of the Prescaler block.

Block diagram for Figure 117 showing TIMx (Master) and TIMy (Slave) connected via TRGO1 and ITR1. TIMx includes a Prescaler, Counter, and Master mode control. TIMy includes an Input trigger selection, Slave mode control, CK_PSC, Prescaler, and Counter.

For example, Timer x can be configured to act as a prescaler for Timer y. Refer to Figure 117 . To do this, follow the sequence below:

  1. 1. Configure Timer x in master mode so that it outputs a periodic trigger signal on each update event UEV. If MMS=010 is written in the TIMx_CR2 register, a rising edge is output on TRGO1 each time an update event is generated.
  2. 2. To connect the TRGO1 output of Timer x to Timer y, Timer y must be configured in slave mode using ITR1 as internal trigger. This is selected through the TS bits in the TIMy_SMCR register (writing TS=000).
  3. 3. Then the slave mode controller must be put in external clock mode 1 (write SMS=111 in the TIMy_SMCR register). This causes Timer y to be clocked by the rising edge of the periodic Timer x trigger signal (which correspond to the timer x counter overflow).
  4. 4. Finally both timers must be enabled by setting their respective CEN bits (TIMx_CR1 register).

For code example, refer to A.9.17: Timer prescaling another timer code example .

Note: If OCx is selected on Timer x as trigger output (MMS=1xx), its rising edge is used to clock the counter of timer y.

Using one timer to enable another timer

In this example, we control the enable of Timer y with the output compare 1 of Timer x. Refer to Figure 117 for connections. Timer y counts on the divided internal clock only when OC1REF of Timer x is high. Both counter clock frequencies are divided by 3 by the prescaler compared to CK_INT ( \( f_{CK\_CNT} = f_{CK\_INT}/3 \) ).

  1. 1. Configure Timer x master mode to send its Output Compare 1 Reference (OC1REF) signal as trigger output (MMS=100 in the TIMx_CR2 register).
  2. 2. Configure the Timer x OC1REF waveform (TIMx_CCMR1 register).
  3. 3. Configure Timer y to get the input trigger from Timer x (TS=000 in the TIMy_SMCR register).
  4. 4. Configure Timer y in gated mode (SMS=101 in TIMy_SMCR register).
  5. 5. Enable Timer y by writing '1' in the CEN bit (TIMy_CR1 register).
  6. 6. Start Timer x by writing '1' in the CEN bit (TIMx_CR1 register).

For code example, refer to A.9.18: Timer enabling another timer code example .

Note: The counter 2 clock is not synchronized with counter 1, this mode only affects the Timer y counter enable signal.

Figure 118. Gating timer y with OC1REF of timer x

Timing diagram showing the relationship between CK_INT, TIMERx-OC1REF, TIMERx-CNT, TIMERy-CNT, and TIMERy-TIF signals. The diagram illustrates the gating of Timer y by the OC1REF signal of Timer x. The TIMERx-CNT register shows values FC, FD, FE, FF, 00, 01. The TIMERy-CNT register shows values 3045, 3046, 3047, 3048. The TIMERy-TIF signal is shown as a pulse that goes high when the OC1REF signal is high and low when the OC1REF signal is low. A note indicates 'Write TIF = 0'.

The timing diagram shows five signal lines over time.
1. CK_INT : A continuous square wave clock signal.
2. TIMERx-OC1REF : An output signal that is high for a specific duration.
3. TIMERx-CNT : The counter for Timer x, with values FC, FD, FE, FF, 00, 01 shown. It increments while OC1REF is high.
4. TIMERy-CNT : The counter for Timer y, with values 3045, 3046, 3047, 3048 shown. It only increments when OC1REF is high (gated mode).
5. TIMERy-TIF : The Timer y interrupt flag, which is high when OC1REF is high and low otherwise.
Vertical dashed lines mark key events. A note 'Write TIF = 0' with an arrow points to the falling edge of the TIMERy-TIF signal.

Timing diagram showing the relationship between CK_INT, TIMERx-OC1REF, TIMERx-CNT, TIMERy-CNT, and TIMERy-TIF signals. The diagram illustrates the gating of Timer y by the OC1REF signal of Timer x. The TIMERx-CNT register shows values FC, FD, FE, FF, 00, 01. The TIMERy-CNT register shows values 3045, 3046, 3047, 3048. The TIMERy-TIF signal is shown as a pulse that goes high when the OC1REF signal is high and low when the OC1REF signal is low. A note indicates 'Write TIF = 0'.

MS33137V1

In the example in Figure 118 , the Timer y counter and prescaler are not initialized before being started. So they start counting from their current value. It is possible to start from a given value by resetting both timers before starting Timer x. 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, we synchronize Timer x and Timer y. Timer x is the master and starts from 0. Timer y is the slave and starts from 0xE7. The prescaler ratio is the same for both timers. Timer y stops when Timer x is disabled by writing '0' to the CEN bit in the TIMy_CR1 register:

  1. 1. Configure Timer x master mode to send its Output Compare 1 Reference (OC1REF) signal as trigger output (MMS=100 in the TIMx_CR2 register).
  2. 2. Configure the Timer x OC1REF waveform (TIMx_CCMR1 register).
  3. 3. Configure Timer y to get the input trigger from Timer x (TS=000 in the TIMy_SMCR register).
  4. 4. Configure Timer y in gated mode (SMS=101 in TIMy_SMCR register).
  5. 5. Reset Timer x by writing '1' in UG bit (TIMx_EGR register).
  6. 6. Reset Timer y by writing '1' in UG bit (TIMy_EGR register).
  7. 7. Initialize Timer y to 0xE7 by writing '0xE7' in the timer y counter (TIMy_CNTL).
  8. 8. Enable Timer y by writing '1' in the CEN bit (TIMy_CR1 register).
  9. 9. Start Timer x by writing '1' in the CEN bit (TIMx_CR1 register).
  10. 10. Stop Timer x by writing '0' in the CEN bit (TIMx_CR1 register).

For code example, refer to A.9.19: Master and slave synchronization code example .

Figure 119. Gating timer y with Enable of timer x

Timing diagram showing the interaction between Timer x and Timer y. CK_INT is the master clock. TIMERx-CEN=CNT_EN shows the enable signal for Timer x. TIMERx-CNT_INIT shows a pulse for initialization. TIMERx-CNT shows values 75, then 00, 01, 02. TIMERy-CNT shows values AB, then 00, E7, E8, E9. TIMERy-CNT_INIT shows a pulse. TIMERy-write CNT shows a pulse corresponding to the transition to E7. TIMERy-TIF is a trigger flag that goes high at the update event and is cleared by writing TIF=0.

MS33138V1

Timing diagram showing the interaction between Timer x and Timer y. CK_INT is the master clock. TIMERx-CEN=CNT_EN shows the enable signal for Timer x. TIMERx-CNT_INIT shows a pulse for initialization. TIMERx-CNT shows values 75, then 00, 01, 02. TIMERy-CNT shows values AB, then 00, E7, E8, E9. TIMERy-CNT_INIT shows a pulse. TIMERy-write CNT shows a pulse corresponding to the transition to E7. TIMERy-TIF is a trigger flag that goes high at the update event and is cleared by writing TIF=0.

Using one timer to start another timer

In this example, we set the enable of Timer y with the update event of Timer x. Refer to Figure 117 for connections. Timer y starts counting from its current value (which can be nonzero) on the divided internal clock as soon as the update event is generated by Timer x. When Timer y receives the trigger signal its CEN bit is automatically set and the counter counts until we write '0 to the CEN bit in the TIM2_CR1 register. Both counter clock frequencies are divided by 3 by the prescaler compared to CK_INT ( \( f_{CK\_CNT} = f_{CK\_INT}/3 \) ).

  1. 1. Configure Timer x master mode to send its Update Event (UEV) as trigger output (MMS=010 in the TIMx_CR2 register).
  2. 2. Configure the Timer x period (TIMx_ARR registers).
  3. 3. Configure Timer y to get the input trigger from Timer x (TS=000 in the TIMy_SMCR register).
  4. 4. Configure Timer y in trigger mode (SMS=110 in TIMy_SMCR register).
  5. 5. Start Timer x by writing '1 in the CEN bit (TIMx_CR1 register).

Figure 120. Triggering timer y with update of timer x

Timing diagram for Figure 120 showing the relationship between two timers, x and y. The diagram shows the internal clock (CK_INT), update event (TIMERx-UEV), counter values (TIMERx-CNT, TIMERy-CNT), enable signal (TIMERy-CEN=CNT_EN), and interrupt flag (TIMERy-TIF).

Timing diagram for Figure 120. The diagram shows the relationship between two timers, x and y. The signals shown are:

MS33139V1

Timing diagram for Figure 120 showing the relationship between two timers, x and y. The diagram shows the internal clock (CK_INT), update event (TIMERx-UEV), counter values (TIMERx-CNT, TIMERy-CNT), enable signal (TIMERy-CEN=CNT_EN), and interrupt flag (TIMERy-TIF).

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

Figure 121 shows the behavior with the same configuration as in Figure 120 but in trigger mode instead of gated mode (SMS=110 in the TIMy_SMCR register).

Figure 121. Triggering timer y with Enable of timer x

Timing diagram for Figure 121 showing the relationship between two timers, x and y, in trigger mode. The diagram shows CK_INT, TIMERx-CEN=CNT_EN, TIMERx-CNT_INIT, TIMERx-CNT, TIMERy-CNT, TIMERy-CNT_INIT, TIMERy write CNT, and TIMERy-TIF.

Timing diagram for Figure 121. The diagram shows the relationship between two timers, x and y, in trigger mode. The signals shown are:

MS33140V1

Timing diagram for Figure 121 showing the relationship between two timers, x and y, in trigger mode. The diagram shows CK_INT, TIMERx-CEN=CNT_EN, TIMERx-CNT_INIT, TIMERx-CNT, TIMERy-CNT, TIMERy-CNT_INIT, TIMERy write CNT, and TIMERy-TIF.

Starting 2 timers synchronously in response to an external trigger

In this example, we set the enable of timer x when its TI1 input rises, and the enable of Timer y with the enable of Timer x. Refer to Figure 117 for connections. To ensure the counters are aligned, Timer x must be configured in Master/Slave mode (slave with respect to TI1, master with respect to Timer y):

  1. 1. Configure Timer x master mode to send its Enable as trigger output (MMS=001 in the TIMx_CR2 register).
  2. 2. Configure Timer x slave mode to get the input trigger from TI1 (TS=100 in the TIMx_SMCR register).
  3. 3. Configure Timer x in trigger mode (SMS=110 in the TIMx_SMCR register).
  4. 4. Configure the Timer x in Master/Slave mode by writing MSM=1 (TIMx_SMCR register).
  5. 5. Configure Timer y to get the input trigger from Timer x (TS=000 in the TIMy_SMCR register).
  6. 6. Configure Timer y in trigger mode (SMS=110 in the TIMy_SMCR register).

For code example, refer to A.9.20: Two timers synchronized by an external trigger code example .

When a rising edge occurs on TI1 (Timer x), both counters starts counting synchronously on the internal clock and both TIF flags are set.

Note: In this example both timers are initialized before starting (by setting their respective UG bits). Both counters starts from 0, but 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 insert a delay between CNT_EN and CK_PSC on timer x.

Figure 122. Triggering timer x and y with timer x TI1 input

Timing diagram showing the synchronization of two timers, x and y, triggered by the TI1 input of timer x. The diagram displays several signal lines over time: CK_INT (internal clock), TIMERx-TI1 (external trigger), TIMERx-CEN=CNT_EN (timer x enable), TIMERx-CK_PSC (timer x prescaler output), TIMERx-CNT (timer x counter), TIMERx-TIF (timer x interrupt flag), TIMERy-CEN=CNT_EN (timer y enable), TIMERy-CK_PSC (timer y prescaler output), TIMERy-CNT (timer y counter), and TIMERy-TIF (timer y interrupt flag). The sequence starts with CK_INT as a continuous square wave. TIMERx-TI1 is initially low and then goes high. Upon the rising edge of TIMERx-TI1, TIMERx-CEN=CNT_EN goes high, followed by a short delay before TIMERx-CK_PSC begins toggling. Simultaneously, TIMERy-CEN=CNT_EN goes high, and after another short delay, TIMERy-CK_PSC begins toggling. Both counters, TIMERx-CNT and TIMERy-CNT, start counting from 00. The counter values are shown as a sequence of boxes: 01, 02, 03, 04, 05, 06, 07, 08, 09. Finally, both TIMERx-TIF and TIMERy-TIF flags are set high.
Timing diagram showing the synchronization of two timers, x and y, triggered by the TI1 input of timer x. The diagram displays several signal lines over time: CK_INT (internal clock), TIMERx-TI1 (external trigger), TIMERx-CEN=CNT_EN (timer x enable), TIMERx-CK_PSC (timer x prescaler output), TIMERx-CNT (timer x counter), TIMERx-TIF (timer x interrupt flag), TIMERy-CEN=CNT_EN (timer y enable), TIMERy-CK_PSC (timer y prescaler output), TIMERy-CNT (timer y counter), and TIMERy-TIF (timer y interrupt flag). The sequence starts with CK_INT as a continuous square wave. TIMERx-TI1 is initially low and then goes high. Upon the rising edge of TIMERx-TI1, TIMERx-CEN=CNT_EN goes high, followed by a short delay before TIMERx-CK_PSC begins toggling. Simultaneously, TIMERy-CEN=CNT_EN goes high, and after another short delay, TIMERy-CK_PSC begins toggling. Both counters, TIMERx-CNT and TIMERy-CNT, start counting from 00. The counter values are shown as a sequence of boxes: 01, 02, 03, 04, 05, 06, 07, 08, 09. Finally, both TIMERx-TIF and TIMERy-TIF flags are set high.

MS33141V1

16.3.16 Debug mode

When the microcontroller enters debug mode (Cortex®-M0+ core - halted), the TIMx counter either continues to work normally or stops, depending on DBG_TIMx_STOP configuration bit in DBG module. For more details, refer to Section 27.9.2: Debug support for timers, watchdog and I 2 C .

16.4 TIM2/TIM3 registers

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

The 32-bit peripheral registers have to be written by words (32 bits). All other peripheral registers have to be written by half-words (16 bits) or words (32 bits). Read accesses can be done by bytes (8 bits), half-words (16 bits) or words (32 bits).

16.4.1 TIMx control register 1 (TIMx_CR1)

Address offset: 0x00

Reset value: 0x0000

1514131211109876543210
Res.Res.Res.Res.Res.Res.CKD[1:0]ARPECMSDIROPMURSUDISCEN
rwrwrwrwrwrwrwrwrwrw

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

Bits 9:8 CKD : Clock division

This bit-field indicates the division ratio between the timer clock (CK_INT) frequency and sampling clock used by the digital filters (ETR, TIx),

00: \( t_{DTS} = t_{CK\_INT} \)

01: \( t_{DTS} = 2 \times t_{CK\_INT} \)

10: \( t_{DTS} = 4 \times t_{CK\_INT} \)

11: Reserved

Bit 7 ARPE : Auto-reload preload enable

0: TIMx_ARR register is not buffered

1: TIMx_ARR register is buffered

Bits 6:5 CMS : 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

0: Counter used as upcounter

1: Counter used as downcounter

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

Bit 3 OPM : One-pulse mode

0: Counter is not stopped at update event

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

Bit 2 URS: Update request source

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

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

These events can be:

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

Bit 1 UDIS: Update disable

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

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

Buffered registers are then loaded with their preload values.

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

Bit 0 CEN: Counter enable

0: Counter disabled

1: Counter enabled

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

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

16.4.2 TIMx control register 2 (TIMx_CR2)

Address offset: 0x04

Reset value: 0x0000

1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.TI1SMMS[2:0]CCDSRes.Res.
rwrwrwrwrw

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

Bit 7 TI1S : TI1 selection

0: The TIMx_CH1 pin is connected to TI1 input

1: The TIMx_CH1, CH2 and CH3 pins are connected to the TI1 input (XOR combination)

Bits 6:4 MMS : Master mode selection

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

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

001: Enable - the Counter enable signal, CNT_EN, is used as trigger output (TRGO). It is useful to start several timers at the same time or to control a window in which a slave timer is enabled. The Counter Enable signal is generated by a logic OR between CEN control bit and the trigger input when configured in gated mode.

When the Counter Enable signal is controlled by the trigger input, there is a delay on TRGO, except if the master/slave mode is selected (see the MSM bit description in TIMx_SMCR register).

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

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

100: Compare - OC1REF signal is used as trigger output (TRGO)

101: Compare - OC2REF signal is used as trigger output (TRGO)

110: Compare - OC3REF signal is used as trigger output (TRGO)

111: Compare - OC4REF signal is used as trigger output (TRGO)

Note: The clock of the slave timer or ADC must be enabled prior to receiving 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.

16.4.3 TIMx slave mode control register (TIMx_SMCR)

Address offset: 0x08

Reset value: 0x0000

1514131211109876543210
ETPECEETPS[1:0]ETF[3:0]MSMTS[2:0]Res.SMS[2:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bit 15 ETP : External trigger polarity

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

0: ETR is noninverted, active at high level or rising edge

1: ETR 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 ETRF signal.

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

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

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

Bits 13:12 ETPS : External trigger prescaler

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

00: Prescaler OFF

01: ETRP frequency divided by 2

10: ETRP frequency divided by 4

11: ETRP frequency divided by 8

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

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

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

0001: \( f_{SAMPLING}=f_{CK\_INT} \) , N=2

0010: \( f_{SAMPLING}=f_{CK\_INT} \) , N=4

0011: \( f_{SAMPLING}=f_{CK\_INT} \) , 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 (TRGI) is delayed to allow a perfect synchronization between the current timer and its slaves (through TRGO). It is useful if we want to synchronize several timers on a single external event.

Bits 6:4 TS : Trigger selection

This bit-field selects the trigger input to be used to synchronize the counter.

000: Internal Trigger 0 (ITR0).

001: Internal Trigger 1 (ITR1).

010: Internal Trigger 2 (ITR2).

011: Reserved.

100: TI1F Edge Detector (TI1F_ED)

101: Filtered Timer Input 1 (TI1FP1)

110: Filtered Timer Input 2 (TI2FP2)

111: External Trigger input (ETRF)

See Table 77: TIM2/TIM3 internal trigger connection on page 419 for more details on ITRx meaning for each Timer.

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

Bit 3 Reserved, must be kept at '1'.

Bits 2:0 SMS : Slave mode selection

When external signals are selected the active edge of the trigger signal (TRGI) is linked to the polarity selected on the external input (see Input Control register and Control Register description).

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

001: Encoder mode 1 - Counter counts up/down on TI2FP1 edge depending on TI1FP2 level.

010: Encoder mode 2 - Counter counts up/down on TI1FP2 edge depending on TI2FP1 level.

011: Encoder mode 3 - Counter counts up/down on both TI1FP1 and TI2FP2 edges depending on the level of the other input.

100: Reset Mode - Rising edge of the selected trigger input (TRGI) reinitializes the counter and generates an update of the registers.

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

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

111: External Clock Mode 1 - Rising edges of the selected trigger (TRGI) clock the counter.

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

The clock of the slave timer must be enabled prior to receiving events from the master timer, and must not be changed on-the-fly while triggers are received from the master timer."

Table 77. TIM2/TIM3 internal trigger connection

Slave TIMITR0 (TS = 000)ITR1 (TS = 001)ITR2 (TS = 010)
TIM2TIM21TIM22TIM3
TIM3TIM2TIM22TIM21

16.4.4 TIMx DMA/Interrupt enable register (TIMx_DIER)

Address offset: 0x0C

Reset value: 0x0000

1514131211109876543210
Res.TDERes.CC4DECC3DECC2DECC1DEUDERes.TIERes.CC4IECC3IECC2IECC1IEUIE
rwrwrwrwrwrwrwrwrwrwrwrw

Bit 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, always read as 0

Bit 12 CC4DE : Capture/Compare 4 DMA request enable

0: CC4 DMA request disabled.

1: CC4 DMA request enabled.

Bit 11 CC3DE : Capture/Compare 3 DMA request enable

0: CC3 DMA request disabled.

1: CC3 DMA request enabled.

Bit 10 CC2DE : Capture/Compare 2 DMA request enable

0: CC2 DMA request disabled.

1: CC2 DMA request enabled.

Bit 9 CC1DE : Capture/Compare 1 DMA request enable

0: CC1 DMA request disabled.

1: CC1 DMA request enabled.

Bit 8 UDE : Update DMA request enable

0: Update DMA request disabled.

1: Update DMA request enabled.

Bit 7 Reserved, must be kept at reset value.

Bit 6 TIE : Trigger interrupt enable

0: Trigger interrupt disabled.

1: Trigger interrupt enabled.

Bit 5 Reserved, must be kept at reset value.

Bit 4 CC4IE : Capture/Compare 4 interrupt enable

0: CC4 interrupt disabled.

1: CC4 interrupt enabled.

Bit 3 CC3IE : Capture/Compare 3 interrupt enable

0: CC3 interrupt disabled

1: CC3 interrupt enabled

Bit 2 CC2IE : Capture/Compare 2 interrupt enable

0: CC2 interrupt disabled

1: CC2 interrupt enabled

Bit 1 CC1IE : Capture/Compare 1 interrupt enable

0: CC1 interrupt disabled

1: CC1 interrupt enabled

Bit 0 UIE : Update interrupt enable

0: Update interrupt disabled

1: Update interrupt enabled

16.4.5 TIMx status register (TIMx_SR)

Address offset: 0x10

Reset value: 0x0000

1514131211109876543210
ResResResCC4OFCC3OFCC2OFCC1OFResResTIFResCC4IFCC3IFCC2IFCC1IFUIF
rc_w0rc_w0rc_w0rc_w0rc_w0rc_w0rc_w0rc_w0rc_w0rc_w0

Bits 15: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 trigger event (active edge detected on 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

If channel CC1 is configured as output:

This flag is set by hardware when the counter matches the compare value, with some exception in center-aligned mode (refer to the CMS bits in the TIMx_CR1 register description). It is cleared by software.

0: No match

1: The content of the counter TIMx_CNT matches the content of the TIMx_CCR1 register. When the contents of TIMx_CCR1 are greater than the contents of TIMx_ARR, the CC1IF bit goes high on the counter overflow (in upcounting and up/down-counting modes) or underflow (in downcounting mode)

If channel CC1 is configured as input:

This bit is set by hardware on a capture. It is cleared by software or by reading the TIMx_CCR1 register.

0: No input capture occurred

1: The counter value has been captured in TIMx_CCR1 register (An edge has been detected on IC1 which matches the selected polarity)

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.

16.4.6 TIMx event generation register (TIMx_EGR)

Address offset: 0x14

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 (upcounting), else it takes the auto-reload value (TIMx_ARR) if DIR=1 (downcounting).

16.4.7 TIMx capture/compare mode register 1 (TIMx_CCMR1)

Address offset: 0x18

Reset value: 0x0000

The channels can be used in input (capture mode) or in output (compare mode). The direction of a channel is defined by configuring the corresponding CCxS bits. All the other bits of this register have a different function in input and in output mode. For a given bit, OCxx describes its function when the channel is configured in output, ICxx describes its function when the channel is configured in input. So one must take care that the same bit can have a different meaning for the input stage and for the output stage.

1514131211109876543210
OC2CEOC2M[2:0]OC2PEOC2FECC2S[1:0]OC1CEOC1M[2:0]OC1PEOC1FECC1S[1:0]
IC2F[3:0]IC2PSC[1:0]IC1F[3:0]IC1PSC[1:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Output compare mode

Bit 15 OC2CE : Output compare 2 clear enable

Bits 14:12 OC2M[2:0] : Output compare 2 mode

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 bit-field 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, IC2 is mapped on TI2

10: CC2 channel is configured as input, IC2 is mapped on TI1

11: CC2 channel is configured as input, IC2 is mapped on TRC. This mode is working only if an internal trigger input is selected through 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

OC1CE: Output Compare 1 Clear Enable

0: OC1Ref is not affected by the ETRF input

1: OC1Ref is cleared as soon as a High level is detected on ETRF input

Bits 6:4 OC1M : Output compare 1 mode

These bits define the behavior of the output reference signal OC1REF from which OC1 and OC1N are derived. OC1REF is active high whereas OC1 and OC1N active level depends on CC1P and CC1NP bits.

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

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

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

011: Toggle - OC1REF toggles when TIMx_CNT=TIMx_CCR1.

100: Force inactive level - OC1REF is forced low.

101: Force active level - OC1REF is forced high.

110: PWM mode 1 - In upcounting, channel 1 is active as long as TIMx_CNT<TIMx_CCR1 else inactive. In downcounting, channel 1 is inactive (OC1REF=0) as long as TIMx_CNT>TIMx_CCR1 else active (OC1REF=1).

111: PWM mode 2 - In upcounting, channel 1 is inactive as long as TIMx_CNT<TIMx_CCR1 else active. In downcounting, channel 1 is active as long as TIMx_CNT>TIMx_CCR1 else inactive.

Note: In PWM mode 1 or 2, the OCREF level changes only when the result of the comparison changes or when the output compare mode switches from “frozen” mode to “PWM” mode.

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.

Note: The PWM mode can be used without validating the preload register only in one-pulse mode (OPM bit set in TIMx_CR1 register). Else the behavior is not guaranteed.

Bit 2 OC1FE : Output compare 1 fast enable

This bit is used to accelerate the effect of an event on the trigger in input on the CC output.

0: CC1 behaves normally depending on counter and CCR1 values even when the trigger is ON. The minimum delay to activate CC1 output when an edge occurs on the trigger input is 5 clock cycles.

1: An active edge on the trigger input acts like a compare match on CC1 output. Then, OC is set to the compare level independently from the result of the comparison. Delay to sample the trigger input and to activate CC1 output is reduced to 3 clock cycles. OCFE acts only if the channel is configured in PWM1 or PWM2 mode.

Bits 1:0 CC1S : Capture/Compare 1 selection

This bit-field defines the direction of the channel (input/output) as well as the used input.

00: CC1 channel is configured as output.

01: CC1 channel is configured as input, IC1 is mapped on TI1.

10: CC1 channel is configured as input, IC1 is mapped on TI2.

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

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

Input capture mode

Bits 15:12 IC2F : Input capture 2 filter

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

Bits 9:8 CC2S : Capture/compare 2 selection

This bit-field 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, IC2 is mapped on TI2.

10: CC2 channel is configured as input, IC2 is mapped on TI1.

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

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

Bits 7:4 IC1F : Input capture 1 filter

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

0000: No filter, sampling is done at \( f_{DTS} \)1000: \( f_{SAMPLING}=f_{DTS}/8 \) , N=6
0001: \( f_{SAMPLING}=f_{CK\_INT} \) , N=21001: \( f_{SAMPLING}=f_{DTS}/8 \) , N=8
0010: \( f_{SAMPLING}=f_{CK\_INT} \) , N=41010: \( f_{SAMPLING}=f_{DTS}/16 \) , N=5
0011: \( f_{SAMPLING}=f_{CK\_INT} \) , N=81011: \( f_{SAMPLING}=f_{DTS}/16 \) , N=6
0100: \( f_{SAMPLING}=f_{DTS}/2 \) , N=61100: \( f_{SAMPLING}=f_{DTS}/16 \) , N=8
0101: \( f_{SAMPLING}=f_{DTS}/2 \) , N=81101: \( f_{SAMPLING}=f_{DTS}/32 \) , N=5
0110: \( f_{SAMPLING}=f_{DTS}/4 \) , N=61110: \( f_{SAMPLING}=f_{DTS}/32 \) , N=6
0111: \( f_{SAMPLING}=f_{DTS}/4 \) , N=81111: \( f_{SAMPLING}=f_{DTS}/32 \) , N=8

Bits 3:2 IC1PSC : Input capture 1 prescaler

This bit-field defines the ratio of the prescaler acting on CC1 input (IC1).

The prescaler is reset as soon as CC1E=0 (TIMx_CCER register).

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

01: capture is done once every 2 events

10: capture is done once every 4 events

11: capture is done once every 8 events

Bits 1:0 CC1S : Capture/Compare 1 selection

This bit-field defines the direction of the channel (input/output) as well as the used input.

00: CC1 channel is configured as output

01: CC1 channel is configured as input, IC1 is mapped on TI1

10: CC1 channel is configured as input, IC1 is mapped on TI2

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

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

16.4.8 TIMx capture/compare mode register 2 (TIMx_CCMR2)

Address offset: 0x1C

Reset value: 0x0000

Refer to the above CCMR1 register description.

1514131211109876543210
OC4CEOC4M[2:0]OC4PEOC4FECC4S[1:0]OC3CEOC3M[2:0]OC3PEOC3FECC3S[1:0]
IC4F[3:0]IC4PSC[1:0]IC3F[3:0]IC3PSC[1:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Output compare mode

Bit 15 OC4CE : Output compare 4 clear enable

Bits 14:12 OC4M : Output compare 4 mode

Bit 11 OC4PE : Output compare 4 preload enable

Bit 10 OC4FE : Output compare 4 fast enable

Bits 9:8 CC4S : Capture/Compare 4 selection

This bit-field 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, IC4 is mapped on TI4

10: CC4 channel is configured as input, IC4 is mapped on TI3

11: CC4 channel is configured as input, IC4 is mapped on TRC. This mode is working only if an internal trigger input is selected through TS bit (TIMx_SMCR register)

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

Bit 7 OC3CE : Output compare 3 clear enable

Bits 6:4 OC3M : Output compare 3 mode

Bit 3 OC3PE : Output compare 3 preload enable

Bit 2 OC3FE : Output compare 3 fast enable

Bits 1:0 CC3S : Capture/Compare 3 selection

This bit-field defines the direction of the channel (input/output) as well as the used input.

00: CC3 channel is configured as output

01: CC3 channel is configured as input, IC3 is mapped on TI3

10: CC3 channel is configured as input, IC3 is mapped on TI4

11: CC3 channel is configured as input, IC3 is mapped on TRC. This mode is working only if an internal trigger input is selected through TS bit (TIMx_SMCR register)

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

Input capture mode

Bits 15:12 IC4F : Input capture 4 filter

Bits 11:10 IC4PSC : Input capture 4 prescaler

Bits 9:8 CC4S : Capture/Compare 4 selection

This bit-field 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, IC4 is mapped on TI4

10: CC4 channel is configured as input, IC4 is mapped on TI3

11: CC4 channel is configured as input, IC4 is mapped on TRC. This mode is working only if an internal trigger input is selected through TS bit (TIMx_SMCR register)

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

Bits 7:4 IC3F : Input capture 3 filter

Bits 3:2 IC3PSC : Input capture 3 prescaler

Bits 1:0 CC3S : Capture/Compare 3 selection

This bit-field defines the direction of the channel (input/output) as well as the used input.

00: CC3 channel is configured as output

01: CC3 channel is configured as input, IC3 is mapped on TI3

10: CC3 channel is configured as input, IC3 is mapped on TI4

11: CC3 channel is configured as input, IC3 is mapped on TRC. This mode is working only if an internal trigger input is selected through TS bit (TIMx_SMCR register)

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

16.4.9 TIMx capture/compare enable register (TIMx_CCER)

Address offset: 0x20

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 TI1FP1/TI2FP1 polarity. refer to CC1P description.

Bit 2 Reserved, must be kept at reset value.

Bit 1 CC1P : Capture/Compare 1 output Polarity.

CC1 channel configured as output:

0: OC1 active high

1: OC1 active low

CC1 channel configured as input:

CC1NP/CC1P bits select TI1FP1 and TI2FP1 polarity for trigger or capture operations.

00: noninverted/rising edge

Circuit is sensitive to TIxFP1 rising edge (capture, trigger in reset, external clock or trigger mode), TIxFP1 is not inverted (trigger in gated mode, encoder mode).

01: inverted/falling edge

Circuit is sensitive to TIxFP1 falling edge (capture, trigger in reset, external clock or trigger mode), TIxFP1 is inverted (trigger in gated mode, encoder mode).

10: reserved, do not use this configuration.

11: noninverted/both edges

Circuit is sensitive to both TIxFP1 rising and falling edges (capture, trigger in reset, external clock or trigger mode), TIxFP1 is not inverted (trigger in gated mode). This configuration must not be used for encoder mode.

Bit 0 CC1E : Capture/Compare 1 output enable.

CC1 channel configured as output:

0: Off - OC1 is not active

1: On - OC1 signal is output on the corresponding output pin

CC1 channel configured as input:

This bit determines if a capture of the counter value can actually be done into the input capture/compare register 1 (TIMx_CCR1) or not.

0: Capture disabled

1: Capture enabled

Table 78. Output control bit for standard OCx channels

CCxE bitOCx output state
0Output Disabled (OCx=0, OCx_EN=0)
1OCx=OCxREF + Polarity, OCx_EN=1

Note: The state of the external I/O pins connected to the standard OCx channels depends on the OCx channel state and the GPIO registers.

16.4.10 TIMx counter (TIMx_CNT)

Address offset: 0x24

Reset value: 0x0000

1514131211109876543210
CNT[15:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 15:0 CNT[15:0] : Low counter value

16.4.11 TIMx prescaler (TIMx_PSC)

Address offset: 0x28

Reset value: 0x0000

1514131211109876543210
PSC[15:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 15:0 PSC[15:0] : Prescaler value

The counter clock frequency (CK_CNT) is equal to \( f_{CK\_PSC} / (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”).

16.4.12 TIMx auto-reload register (TIMx_ARR)

Address offset: 0x2C

Reset value: 0xFFFF FFFF

1514131211109876543210
ARR[15:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 15:0 ARR[15:0] : Low Auto-reload value

ARR is the value to be loaded in the actual auto-reload register.

Refer to the Section 16.3.1: Time-base unit on page 373 for more details about ARR update and behavior.

The counter is blocked while the auto-reload value is null.

16.4.13 TIMx capture/compare register 1 (TIMx_CCR1)

Address offset: 0x34

Reset value: 0x0000

1514131211109876543210
CCR1[15:0]
rw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/r

Bits 15:0 CCR1[15:0] : Low 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 OC1 output.

If channel CC1 is configured as input:

CCR1 is the counter value transferred by the last input capture 1 event (IC1). The TIMx_CCR1 register is read-only and cannot be programmed.

16.4.14 TIMx capture/compare register 2 (TIMx_CCR2)

Address offset: 0x38

Reset value: 0x0000

1514131211109876543210
CCR2[15:0]
rw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/r

Bits 15:0 CCR2[15:0] : Low 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 OC2 output.

If channel CC2 is configured as input:

CCR2 is the counter value transferred by the last input capture 2 event (IC2). The TIMx_CCR2 register is read-only and cannot be programmed.

16.4.15 TIMx capture/compare register 3 (TIMx_CCR3)

Address offset: 0x3C

Reset value: 0x0000

1514131211109876543210
CCR3[15:0]
rw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/r

Bits 15:0 CCR3[15:0] : Low Capture/Compare 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 signalled on OC3 output.

If channel CC3 is configured as input:

CCR3 is the counter value transferred by the last input capture 3 event (IC3). The TIMx_CCR3 register is read-only and cannot be programmed.

16.4.16 TIMx capture/compare register 4 (TIMx_CCR4)

Address offset: 0x40

Reset value: 0x0000

1514131211109876543210
CCR4[15:0]
rw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/r

Bits 15:0 CCR4[15:0] : Low Capture/Compare value

If CC4 channel is configured as output (CC4S bits):

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 signalled on OC4 output.

If CC4 channel is configured as input (CC4S bits in TIMx_CCMR4 register):

CCR4 is the counter value transferred by the last input capture 4 event (IC4). The TIMx_CCR4 register is read-only and cannot be programmed.

16.4.17 TIMx DMA control register (TIMx_DCR)

Address offset: 0x48

Reset value: 0x0000

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

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

Bits 12:8 DBL[4:0] : DMA burst length

This 5-bit vector defines the number of DMA transfers (the timer recognizes a burst transfer when a read or a write access is done to the TIMx_DMAR address).

00000: 1 transfer,

00001: 2 transfers,

00010: 3 transfers,

...

10001: 18 transfers.

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

Bits 4:0 DBA[4:0] : DMA base address

This 5-bit 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,

...

Example: Let us consider the following transfer: DBL = 7 transfers & DBA = TIMx_CR1. In this case the transfer is done to/from 7 registers starting from the TIMx_CR1 address.

16.4.18 TIMx DMA address for full transfer (TIMx_DMAR)

Address offset: 0x4C

Reset value: 0x0000

1514131211109876543210
DMAB[15:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 15:0 DMAB[15: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).

Example of how to use the DMA burst feature

In this example the timer DMA burst feature is used to update the contents of the CCRx registers (x = 2, 3, 4) 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 bit fields 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

For code example, refer to A.9.21: DMA burst feature code example .

Note: This example is for the case where every CCRx register to be updated once. If every CCRx register is to be updated twice for example, the number of data to transfer should 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.

16.4.19 TIM2 option register (TIM2_OR)

Address offset: 0x50

Reset value: 0x0000

1514131211109876543
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.TI4_RMPETR_RMP
rwrwrwrwrw

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

Bits 4:3 TI4_RMP : Internal trigger (TI4 connected to TIM2_CH4) remap

This bit is set and cleared by software.

01: TIM2 TI4 input connected to COMP2_OUT

10: TIM2 TI4 input connected to COMP1_OUT

others: TIM2 TI4 input connected to ORed GPIOs. Refer to the Alternate function mapping table in the device datasheets.

Bits 2:0 ETR_RMP : Timer2 ETR remap

This bit is set and cleared by software.

111: TIM2 ETR input is connected to COMP1_OUT

110: TIM2 ETR input is connected to COMP2_OUT

101: TIM2 ETR input is connected to LSE

011: TIM2 ETR input is connected to HSI16 when HSI16OUTEN bit is set in Clock control register (RCC_CR) (except for category 3 devices)

others: TIM2 ETR input is connected to ORed GPIOs. Refer to the Alternate function mapping table in the device datasheets

16.4.20 TIM3 option register (TIM3_OR)

Address offset: 0x50

Reset value: 0x0000

1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.TI_RMPETR_RMP
rwrwrwrwrw

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

Bit 4 TI_RMP : Timer3 remapping on PC9

This bit is set and cleared by software.

1: TIM3_CH4 selected

0: Reserved

Bit 3 TI_RMP : Timer3 remap on PB5

This bit is set and cleared by software.

1: TIM3_CH2 selected

0: TIM22_CH2 selected

Bit 2 TI_RMP : Timer3 TI remap

This bit is set and cleared by software.

1: TIM3_TI1 input is connected to PE3, PA6, PC6 or PB4

0: Reserved

Bits 1:0 ETR_RMP : Timer3 ETR remap

These bits are set and cleared by software.

others configurations: TIM3_ETR input is connected to PE2 or PD2

16.5 TIMx register map

TIMx registers are mapped as described in the table below:

Table 79. TIM2/3 register map and reset values

OffsetRegister1514131211109876543210
0x00TIMx_CR1Res.Res.Res.Res.Res.Res.CKD [1:0]ARPECMS[1:0]DIROPMURSUDISCEN
Reset value0000000000
0x04TIMx_CR2Res.Res.Res.Res.Res.Res.Res.Res.TI1SMMS[2:0]CCDSRes.Res.Res.
Reset value00000
0x08TIMx_SMCRETPECEETPS [1:0]ETF[3:0]MSMTS[2:0]Res.SMS[2:0]
Reset value000000000000000
0x0CTIMx_DIERRes.TDERes.CC4DECC3DECC2DECC1DEUDERes.TIERes.CC4IECC3IECC2IECC1IEUIE
Reset value000000000000
0x10TIMx_SRRes.Res.Res.CC4OFCC3OFCC2OFCC1OFRes.Res.TIFRes.CC4IFCC3IFCC2IFCC1IFUIF
Reset value0000000000
0x14TIMx_EGRRes.Res.Res.Res.Res.Res.Res.Res.Res.TGRes.CC4GCC3GCC2GCC1GUG
Reset value000000
0x18TIMx_CCMR1
Output Compare mode
OC2CEOC2M [2:0]OC2PEOC2FECC2S [1:0]OC1CEOC1M [2:0]OC1PEOC1FECC1S [1:0]
Reset value0000000000000000
TIMx_CCMR1
Input Capture mode
IC2F[3:0]IC2 PSC [1:0]CC2S [1:0]IC1F[3:0]IC1 PSC [1:0]CC1S [1:0]
Reset value0000000000000000
0x1CTIMx_CCMR2
Output Compare mode
OC4CEOC4M [2:0]OC4PEOC4FECC4S [1:0]OC3CEOC3M [2:0]OC3PEOC3FECC3S [1:0]
Reset value0000000000000000
TIMx_CCMR2
Input Capture mode
IC4F[3:0]IC4 PSC [1:0]CC4S [1:0]IC3F[3:0]IC3 PSC [1:0]CC3S [1:0]
Reset value0000000000000000
0x20TIMx_CCERCC4NPRes.CC4PCC4ECC3NPRes.CC3PCC3ECC2NPRes.CC2PCC2ECC1NPRes.CC1PCC1E
Reset value000000000000
0x24TIMx_CNTCNT[15:0]
Reset value0000000000000000
0x28TIMx_PSCPSC[15:0]
Reset value0000000000000000

Table 79. TIM2/3 register map and reset values (continued)

OffsetRegister1514131211109876543210
0x2CTIMx_ARRARR[15:0]
Reset value0000000000000000
0x30Res.
0x34TIMx_CCR1CCR1[15:0]
Reset value0000000000000000
0x38TIMx_CCR2CCR2[15:0]
Reset value0000000000000000
0x3CTIMx_CCR3CCR3[15:0]
Reset value0000000000000000
0x40TIMx_CCR4CCR4[15:0]
Reset value0000000000000000
0x44Res.
0x48TIMx_DCRRes.Res.Res.DBL[4:0]Res.Res.Res.DBA[4:0]
Reset value0000000000
0x4CTIMx_DMARDMAB[15:0]
Reset value0000000000000000
0x50TIM2_ORRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.TI4_RMPETR_RMP
Reset value
0x50TIM3_ORRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.TI_RMPETR_RMP
Reset value00
Refer to Section 2.2 on page 51 for the register boundary addresses.