42. Basic timers (TIM6/TIM7)

42.1 TIM6/TIM7 introduction

The basic timers TIM6 and TIM7 consist of a 16-bit auto-reload counter driven by a programmable prescaler.

They may be used as generic timers for time base generation but they are also specifically used to drive the digital-to-analog converter (DAC). In fact, the timers are internally connected to the DAC and are able to drive it through their trigger outputs.

The timers are completely independent, and do not share any resources.

42.2 TIM6/TIM7 main features

Basic timer (TIM6/TIM7) features include:

Figure 483. Basic timer block diagram

Block diagram of a basic timer (TIM6/TIM7) showing the internal components and signal flow.

The diagram illustrates the internal architecture of a basic timer. It consists of the following components and connections:

Notes:

MS33142V1

Block diagram of a basic timer (TIM6/TIM7) showing the internal components and signal flow.

42.3 TIM6/TIM7 functional description

42.3.1 Time-base unit

The main block of the programmable timer is a 16-bit upcounter with its related auto-reload register. 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. The preload register is accessed each time an attempt is made to write or read the auto-reload register. The contents 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 the TIMx_CR1 register. The update event is sent when the counter reaches the overflow value 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 the TIMx_CR1 register is set.

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 register (in the TIMx_PSC register). It can be changed on the fly as the TIMx_PSC control register is buffered. The new prescaler ratio is taken into account at the next update event.

Figure 484 and Figure 485 give some examples of the counter behavior when the prescaler ratio is changed on the fly.

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

Timing diagram for prescaler division change from 1 to 2. It shows the relationship between CK_PSC, CEN, Timerclock, Counter register, Update event (UEV), Prescaler control register, Prescaler buffer, and Prescaler counter over time.

The diagram illustrates the timing of a prescaler division change from 1 to 2. The CK_PSC signal is the primary clock. The CEN signal is active low, starting at the beginning of the sequence. The Timerclock = CK_CNT is derived from CK_PSC. The Counter register shows values F7, F8, F9, FA, FB, FC, 00, 01, 02, 03. The Update event (UEV) is a pulse at the end of each counter rollover. The Prescaler control register is written with 0 and then 1. The Prescaler buffer is updated to 1. The Prescaler counter counts from 0 to 1, then 0 to 1, indicating a division of 2.

MS31076V2

Timing diagram for prescaler division change from 1 to 2. It shows the relationship between CK_PSC, CEN, Timerclock, Counter register, Update event (UEV), Prescaler control register, Prescaler buffer, and Prescaler counter over time.

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

Timing diagram for prescaler division change from 1 to 4. It shows the relationship between CK_PSC, CEN, Timerclock, Counter register, Update event (UEV), Prescaler control register, Prescaler buffer, and Prescaler counter over time.

The diagram illustrates the timing of a prescaler division change from 1 to 4. The CK_PSC signal is the primary clock. The CEN signal is active low, starting at the beginning of the sequence. The Timerclock = CK_CNT is derived from CK_PSC. The Counter register shows values F7, F8, F9, FA, FB, FC, 00, 01. The Update event (UEV) is a pulse at the end of each counter rollover. The Prescaler control register is written with 0 and then 3. The Prescaler buffer is updated to 3. The Prescaler counter counts from 0 to 3, then 0 to 3, indicating a division of 4.

MS31077V2

Timing diagram for prescaler division change from 1 to 4. It shows the relationship between CK_PSC, CEN, Timerclock, Counter register, Update event (UEV), Prescaler control register, Prescaler buffer, and Prescaler counter over time.

42.3.2 Counting mode

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

An update event can be generate 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 the TIMx_CR1 register. This avoids updating the shadow registers while writing new values into the preload registers. In this way, no update event occurs until the UDIS bit has been cleared, however, the counter and the prescaler counter both restart from 0 (but the prescale rate does not change). In addition, if the URS (update request selection) bit in the TIMx_CR1 register is set, setting the UG bit generates an update event UEV, but the UIF flag is not set (so no interrupt or DMA request is sent).

When an update event occurs, all the registers are updated and the update flag (UIF bit in the 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 486. Counter timing diagram, internal clock divided by 1

Timing diagram for a counter in counting mode with internal clock divided by 1. The diagram shows the relationship between CK_PSC, CNT_EN, Timerclock = CK_CNT, Counter register values, Counter overflow, Update event (UEV), and Update interrupt flag (UIF).

The diagram illustrates the timing of a counter in counting mode. The counter register counts from 31 to 00, then wraps back to 00. A counter overflow occurs when the counter reaches 00. An update event (UEV) is generated at the overflow. The update interrupt flag (UIF) is set when the update event occurs. The counter register is updated when the update event occurs. The counter register is updated when the update event occurs.

CK_PSC: Prescaler clock signal.

CNT_EN: Counter enable signal.

Timerclock = CK_CNT: Counter clock signal.

Counter register: Counter register values (31, 32, 33, 34, 35, 36, 00, 01, 02, 03, 04, 05, 06, 07).

Counter overflow: Counter overflow signal.

Update event (UEV): Update event signal.

Update interrupt flag (UIF): Update interrupt flag signal.

MS31078V2

Timing diagram for a counter in counting mode with internal clock divided by 1. The diagram 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 487. Counter timing diagram, internal clock divided by 2

Timing diagram for counter with internal clock divided by 2. Shows CK_PSC, CNT_EN, Timerclock = CK_CNT, Counter register (0034 to 0003), Counter overflow, Update event (UEV), and Update interrupt flag (UIF).

This timing diagram illustrates the operation of a counter when the internal clock is divided by 2. The CK_PSC signal is a continuous square wave. The CNT_EN signal is active low, starting high and transitioning low at the first falling edge of CK_PSC. The Timerclock = CK_CNT signal is a square wave with a period twice that of CK_PSC, starting at the second falling edge of CK_PSC. The Counter register shows values 0034, 0035, 0036, 0000, 0001, 0002, and 0003. The Counter overflow signal is a pulse at the transition from 0036 to 0000. The Update event (UEV) signal is a pulse at the same transition. The Update interrupt flag (UIF) signal is a pulse at the same transition.

SignalValue/State
CK_PSCContinuous square wave
CNT_ENActive low, starts high
Timerclock = CK_CNTSquare wave, period = 2 * CK_PSC
Counter register0034, 0035, 0036, 0000, 0001, 0002, 0003
Counter overflowPulse at 0036 to 0000 transition
Update event (UEV)Pulse at 0036 to 0000 transition
Update interrupt flag (UIF)Pulse at 0036 to 0000 transition

MS31079V2

Timing diagram for counter with internal clock divided by 2. Shows CK_PSC, CNT_EN, Timerclock = CK_CNT, Counter register (0034 to 0003), Counter overflow, Update event (UEV), and Update interrupt flag (UIF).

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

Timing diagram for counter with internal clock divided by 4. Shows CK_PSC, CNT_EN, Timerclock = CK_CNT, Counter register (0035 to 0001), Counter overflow, Update event (UEV), and Update interrupt flag (UIF).

This timing diagram illustrates the operation of a counter when the internal clock is divided by 4. The CK_PSC signal is a continuous square wave. The CNT_EN signal is active low, starting high and transitioning low at the first falling edge of CK_PSC. The Timerclock = CK_CNT signal is a square wave with a period four times that of CK_PSC, starting at the second falling edge of CK_PSC. The Counter register shows values 0035, 0036, 0000, and 0001. The Counter overflow signal is a pulse at the transition from 0036 to 0000. The Update event (UEV) signal is a pulse at the same transition. The Update interrupt flag (UIF) signal is a pulse at the same transition.

SignalValue/State
CK_PSCContinuous square wave
CNT_ENActive low, starts high
Timerclock = CK_CNTSquare wave, period = 4 * CK_PSC
Counter register0035, 0036, 0000, 0001
Counter overflowPulse at 0036 to 0000 transition
Update event (UEV)Pulse at 0036 to 0000 transition
Update interrupt flag (UIF)Pulse at 0036 to 0000 transition

MS31080V2

Timing diagram for counter with internal clock divided by 4. Shows CK_PSC, CNT_EN, Timerclock = CK_CNT, Counter register (0035 to 0001), Counter overflow, Update event (UEV), and Update interrupt flag (UIF).

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

Timing diagram for Figure 489 showing CK_PSC, Timerclock = CK_CNT, Counter register, Counter overflow, Update event (UEV), and Update interrupt flag (UIF).

This timing diagram illustrates the operation of a counter when the internal clock is divided by N. It shows the following signals over time:

MS31081V2

Timing diagram for Figure 489 showing CK_PSC, Timerclock = CK_CNT, Counter register, Counter overflow, Update event (UEV), and Update interrupt flag (UIF).

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

Timing diagram for Figure 490 showing CK_PSC, CEN, Timerclock = CK_CNT, Counter register, Counter overflow, Update event (UEV), Update interrupt flag (UIF), and Auto-reload preload register.

This timing diagram illustrates the counter operation when ARPE = 0 and the auto-reload register (TIMx_ARR) is not preloaded. It shows the following signals:

MS31082V2

Timing diagram for Figure 490 showing CK_PSC, CEN, Timerclock = CK_CNT, Counter register, Counter overflow, Update event (UEV), Update interrupt flag (UIF), and Auto-reload preload register.

Figure 491. Counter timing diagram, update event when ARPE=1 (TIMx_ARR preloaded)

Timing diagram for a counter update event when ARPE=1. The diagram shows the relationship between CK_PSC, CEN, Timerclock = CK_CNT, Counter register, Counter overflow, Update event (UEV), Update interrupt flag (UIF), Auto-reload preload register, and Auto-reload shadow register. A vertical dashed line marks the update event. The Counter register shows values F0, F1, F2, F3, F4, F5, 00, 01, 02, 03, 04, 05, 06, 07. The Auto-reload preload register shows F5 and 36. The Auto-reload shadow register shows F5 and 36. A note at the bottom indicates 'Write a new value in TIMx_ARR'.

The diagram illustrates the timing of a counter update event when ARPE=1. The counter register is preloaded with a new value (F5) before the update event. The counter overflow and update event (UEV) signals are shown as pulses. The update interrupt flag (UIF) is set after the update event. The auto-reload preload register is updated with the new value (F5) before the update event, and the auto-reload shadow register is updated with the new value (F5) after the update event. The counter register is updated with the new value (F5) after the update event. The counter overflow and update event (UEV) signals are shown as pulses. The update interrupt flag (UIF) is set after the update event. The auto-reload preload register is updated with the new value (F5) before the update event, and the auto-reload shadow register is updated with the new value (F5) after the update event.

Timing diagram for a counter update event when ARPE=1. The diagram shows the relationship between CK_PSC, CEN, Timerclock = CK_CNT, Counter register, Counter overflow, Update event (UEV), Update interrupt flag (UIF), Auto-reload preload register, and Auto-reload shadow register. A vertical dashed line marks the update event. The Counter register shows values F0, F1, F2, F3, F4, F5, 00, 01, 02, 03, 04, 05, 06, 07. The Auto-reload preload register shows F5 and 36. The Auto-reload shadow register shows F5 and 36. A note at the bottom indicates 'Write a new value in TIMx_ARR'.

42.3.3 UIF bit remapping

The IUFREMAP bit in the TIMx_CR1 register forces a continuous copy of the Update Interrupt Flag UIF into the timer counter register's bit 31 (TIMxCNT[31]). This allows to atomically read both the counter value and a potential roll-over condition signaled by the UIFCPY flag. In particular cases, it can ease the calculations by avoiding race conditions caused for instance by a processing shared between a background task (counter reading) and an interrupt (Update Interrupt).

There is no latency between the assertions of the UIF and UIFCPY flags.

42.3.4 Clock source

The counter clock is provided by the Internal clock (CK_INT) source.

The CEN (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 for UG that remains cleared automatically). As soon as the CEN bit is written to 1, the prescaler is clocked by the internal clock CK_INT.

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

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

Timing diagram for the control circuit in normal mode. It shows the relationship between the Internal clock, CEN=CNT_EN, UG, CNT_INIT, Counter clock = CK_CNT = CK_PSC, and the Counter register. The Counter register is shown as a sequence of bits from 31 to 07. The Counter clock is derived from the Internal clock. The CEN=CNT_EN signal enables the counter. The UG signal is a pulse that triggers a counter update. The CNT_INIT signal is a pulse that initializes the counter. The Counter register is updated on the rising edge of the Counter clock.

MS31085V2

Timing diagram for the control circuit in normal mode. It shows the relationship between the Internal clock, CEN=CNT_EN, UG, CNT_INIT, Counter clock = CK_CNT = CK_PSC, and the Counter register. The Counter register is shown as a sequence of bits from 31 to 07. The Counter clock is derived from the Internal clock. The CEN=CNT_EN signal enables the counter. The UG signal is a pulse that triggers a counter update. The CNT_INIT signal is a pulse that initializes the counter. The Counter register is updated on the rising edge of the Counter clock.

42.3.5 Debug mode

When the system enters debug mode (processor core halted), the TIMx counter either continues to work normally or stops, depending on TIMx bit in DBGMCU module. For more details, refer to Section 64.10.9: Microprocessor debug unit (DBGMCU).

42.4 TIM6/TIM7 registers

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

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

42.4.1 TIMx control register 1 (TIMx_CR1)(x = 6 to 7)

Address offset: 0x00

Reset value: 0x0000

1514131211109876543210
Res.Res.Res.Res.UIFRE
MAP
Res.Res.Res.ARPERes.Res.Res.OPMURSUDISCEN
rwrwrwrwrwrw

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

Bit 11 UIFREMAP: UIF status bit remapping

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

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

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

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

42.4.2 TIMx control register 2 (TIMx_CR2)(x = 6 to 7)

Address offset: 0x04

Reset value: 0x0000

1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.Res.MMS[2:0]Res.Res.Res.Res.
rwrwrw

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

Bits 6:4 MMS[2:0]: Master mode selection

These bits are used 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 a trigger output (TRGO). If 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 a 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 the TIMx_SMCR register).

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

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

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

42.4.3 TIMx DMA/Interrupt enable register (TIMx_DIER)(x = 6 to 7)

Address offset: 0x0C

Reset value: 0x0000

1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.UDERes.Res.Res.Res.Res.Res.Res.UIE
rwrw

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

Bit 8 UDE: Update DMA request enable

0: Update DMA request disabled.

1: Update DMA request enabled.

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

Bit 0 UIE: Update interrupt enable

0: Update interrupt disabled.

1: Update interrupt enabled.

42.4.4 TIMx status register (TIMx_SR)(x = 6 to 7)

Address offset: 0x10

Reset value: 0x0000

1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.UIF
rc_w0

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

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:

42.4.5 TIMx event generation register (TIMx_EGR)(x = 6 to 7)

Address offset: 0x14

Reset value: 0x0000

1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.UG
w

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

Bit 0 UG: Update generation

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

0: No action.

1: Re-initializes the timer counter and generates an update of the registers. Note that the prescaler counter is cleared too (but the prescaler ratio is not affected).

42.4.6 TIMx counter (TIMx_CNT)(x = 6 to 7)

Address offset: 0x24

Reset value: 0x0000 0000

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

Bit 31 UIFCPY: UIF Copy

This bit is a read-only copy of the UIF bit of the TIMx_ISR register. If the UIFREMAP bit in TIMx_CR1 is reset, bit 31 is reserved and read as 0.

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

Bits 15:0 CNT[15:0]: Counter value

42.4.7 TIMx prescaler (TIMx_PSC)(x = 6 to 7)

Address offset: 0x28

Reset value: 0x0000

42.4.7column_2Bit 31 Bits 15:0UIFCPY CNT[15:0]: UIF Copy: Counter valuecolumn_7column_8column_9column_10column_11column_12column_13column_14column_15RM0442
151413121110987 PSC[15:0]6543210
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
Bits 15:0PSC[15:0]: Prescaler value
151413121110987 ARR[15:0]6543210
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
Bits 15:0ARR[15:0]: Prescaler value
Refer to behavior.Section 42.3.1: Time-base unit on page 2220for more details about ARR update and

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

42.4.8 TIMx auto-reload register (TIMx_ARR)(x = 6 to 7)

Address offset: 0x2C

Reset value: 0xFFFF

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

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

Refer to Section 42.3.1: Time-base unit on page 2220 for more details about ARR update and behavior.

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

42.4.9 TIMx register map

TIMx registers are mapped as 16-bit addressable registers as described in the table below:

Table 297. TIMx register map and reset values

OffsetRegister name313029282726252423222120191817161514131211109876543210
0x00TIMx_CR1Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.UIFREMARes.Res.Res.ARPERes.Res.Res.OPMURSUDISCEN
Reset value000000
0x04TIMx_CR2Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.MMS [2:0]Res.Res.Res.Res.Res.Res.
Reset value000
0x08Reserved
0x0CTIMx_DIERRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.UDERes.Res.Res.Res.Res.Res.Res.UIE
Reset value00
0x10TIMx_SRRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.UIF
Reset value0
0x14TIMx_EGRRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.UG
Reset value0
0x18-0x20Reserved
0x24TIMx_CNTUIFCPY or Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.CNT[15:0]
Reset value00000000000
0x28TIMx_PSCRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.PSC[15:0]
Reset value0000000000
0x2CTIMx_ARRRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.ARR[15:0]
Reset value1111111111

Refer to Section 2.5 on page 153 for the register boundary addresses.