15. Basic timer (TIM6/TIM7)

This section applies to STM32F030x8, STM32F070xB and STM32F030xC devices only. TIM7 is available only on STM32F070xB and STM32F030xC devices.

15.1 TIM6/TIM7 introduction

The basic timer TIM6 consists of a 16-bit auto-reload counter driven by a programmable prescaler.

15.2 TIM6/TIM7 main features

Figure 139. Basic timer block diagram

Figure 139. Basic timer block diagram

The block diagram illustrates the internal architecture of the basic timer TIM6/TIM7. The main components are:

Notes:

MS32645V1

Figure 139. Basic timer block diagram

15.3 TIM6/TIM7 functional description

15.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 140 and Figure 141 give some examples of the counter behavior when the prescaler ratio is changed on the fly.

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

Timing diagram for Figure 140 showing signals CK_PSC, CEN, Timerclock = CK_CNT, Counter register, Update event (UEV), Prescaler control register, Prescaler buffer, and Prescaler counter over time. It illustrates a change in prescaler division from 1 to 2.

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

The diagram shows that after writing a new value (1) to the prescaler control register, the prescaler buffer updates to 1, and the prescaler counter begins counting from 0 to 1. The timerclock (CK_CNT) frequency is halved after the update. The counter register continues to count from 00 upwards. The identifier MS31076V2 is present in the bottom right corner.

Timing diagram for Figure 140 showing signals CK_PSC, CEN, Timerclock = CK_CNT, Counter register, Update event (UEV), Prescaler control register, Prescaler buffer, and Prescaler counter over time. It illustrates a change in prescaler division from 1 to 2.

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

Timing diagram for Figure 141 showing signals CK_PSC, CEN, Timerclock = CK_CNT, Counter register, Update event (UEV), Prescaler control register, Prescaler buffer, and Prescaler counter over time. It illustrates a change in prescaler division from 1 to 4.

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

The diagram shows that after writing a new value (3) to the prescaler control register, the prescaler buffer updates to 3, and the prescaler counter begins counting from 0 to 3. The timerclock (CK_CNT) frequency is divided by 4 after the update. The counter register continues to count from 00 upwards. The identifier MS31077V2 is present in the bottom right corner.

Timing diagram for Figure 141 showing signals CK_PSC, CEN, Timerclock = CK_CNT, Counter register, Update event (UEV), Prescaler control register, Prescaler buffer, and Prescaler counter over time. It illustrates a change in prescaler division from 1 to 4.

15.3.2 Counter modes

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 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 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 written to 0, 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 142. Counter timing diagram, internal clock divided by 1

Timing diagram for a basic timer (TIM6/TIM7) showing the relationship between the prescaler clock (CK_PSC), counter enable (CNT_EN), timer clock (CK_CNT), counter register values, counter overflow, update event (UEV), and update interrupt flag (UIF).

The timing diagram illustrates the operation of a basic timer. The top signal, CK_PSC, is a periodic square wave representing the prescaler clock. Below it, CNT_EN is a signal that goes high to enable the counter. When CNT_EN is high, the Timerclock = CK_CNT signal is a square wave with a frequency that is a fraction of CK_PSC. The Counter register is shown as a sequence of values: 31, 32, 33, 34, 35, 36, 00, 01, 02, 03, 04, 05, 06, 07. The counter increments by 1 at each rising edge of CK_CNT. When the counter reaches 36 (0x24), it overflows to 00. This overflow generates a pulse on the Counter overflow signal. Simultaneously, an Update event (UEV) is generated, shown as a pulse. Finally, the Update interrupt flag (UIF) is set, shown as a signal that goes high at the overflow point.

Timing diagram for a basic timer (TIM6/TIM7) showing the relationship between the prescaler clock (CK_PSC), counter enable (CNT_EN), timer clock (CK_CNT), counter register values, counter overflow, update event (UEV), and update interrupt flag (UIF).

MS31078V2

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

Timing diagram for internal clock divided by 2. It shows CK_PSC, CNT_EN, Timerclock = CK_CNT, Counter register values (0034, 0035, 0036, 0000, 0001, 0002, 0003), Counter overflow, Update event (UEV), and Update interrupt flag (UIF) over time.

This timing diagram illustrates the operation of a basic timer with the internal clock 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) is derived from CK_PSC and has a frequency half that of CK_PSC. The Counter register shows a sequence of values: 0034, 0035, 0036, 0000, 0001, 0002, and 0003. Vertical dashed lines indicate key timing points: the first at the rising edge of CK_PSC when CNT_EN goes high; the second at the rising edge of CK_CNT following the count of 0036; and the third at the subsequent rising edge of CK_CNT. At the second dashed line, the Counter overflow, Update event (UEV), and Update interrupt flag (UIF) signals all transition from low to high. These signals remain high until the third dashed line, where they all transition back to low.

Timing diagram for internal clock divided by 2. It shows CK_PSC, CNT_EN, Timerclock = CK_CNT, Counter register values (0034, 0035, 0036, 0000, 0001, 0002, 0003), Counter overflow, Update event (UEV), and Update interrupt flag (UIF) over time.

MS31079V2

Figure 144. 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 (0035, 0036, 0000, 0001), Counter overflow, Update event (UEV), and Update interrupt flag (UIF) over time.

This timing diagram illustrates the operation of a basic timer with the internal clock divided by 4. The signals CK_PSC, CNT_EN, Counter register, Counter overflow, Update event (UEV), and Update interrupt flag (UIF) follow similar patterns to Figure 143. However, the Timerclock (CK_CNT) has a frequency one-quarter that of CK_PSC. The Counter register shows values 0035, 0036, 0000, and 0001. Vertical dashed lines indicate key timing points: the first at the rising edge of CK_PSC when CNT_EN goes high; the second at the rising edge of CK_CNT following the count of 0036; and the third at the subsequent rising edge of CK_CNT. At the second dashed line, the Counter overflow, Update event (UEV), and Update interrupt flag (UIF) signals all transition from low to high and remain high until the third dashed line, where they transition back to low.

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

MS31080V2

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

Timing diagram for Figure 145 showing CK_PSC, Timerclock = CK_CNT, Counter register (values 1F, 20, 00), Counter overflow, Update event (UEV), and Update interrupt flag (UIF).

This timing diagram illustrates the operation of a basic timer with an internal clock divided by N. The top signal is CK_PSC, a periodic square wave. Below it, Timerclock = CK_CNT is shown as a series of pulses, with a break indicating a change in frequency. The Counter register is shown in three states: 1F, 20, and 00. Vertical dashed lines indicate the relationship between the clock and the counter values. The Counter overflow signal is a pulse that occurs when the counter reaches 00. The Update event (UEV) and Update interrupt flag (UIF) are also shown as pulses that coincide with the counter overflow. The diagram is labeled MS31081V2 in the bottom right corner.

Timing diagram for Figure 145 showing CK_PSC, Timerclock = CK_CNT, Counter register (values 1F, 20, 00), Counter overflow, Update event (UEV), and Update interrupt flag (UIF).

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

Timing diagram for Figure 146 showing CK_PSC, CEN, Timerclock = CK_CNT, Counter register (values 31 to 07), Counter overflow, Update event (UEV), Update interrupt flag (UIF), and Auto-reload preload register (values FF, 36).

This timing diagram shows the counter operation when ARPE = 0 and the TIMx_ARR register is not preloaded. The signals shown are CK_PSC, CEN (Counter Enable), Timerclock = CK_CNT, Counter register, Counter overflow, Update event (UEV), Update interrupt flag (UIF), and Auto-reload preload register. The counter register values are shown as 31, 32, 33, 34, 35, 36, 00, 01, 02, 03, 04, 05, 06, 07. The Counter overflow and Update event (UEV) occur when the counter reaches 36. The Update interrupt flag (UIF) is set at this time. The Auto-reload preload register is shown with values FF and 36. A note indicates that a new value should be written in TIMx_ARR. The diagram is labeled MS31082V2 in the bottom right corner.

Timing diagram for Figure 146 showing CK_PSC, CEN, Timerclock = CK_CNT, Counter register (values 31 to 07), Counter overflow, Update event (UEV), Update interrupt flag (UIF), and Auto-reload preload register (values FF, 36).

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

Timing diagram for a basic timer showing signals 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 over time. It illustrates the sequence of events when ARPE=1 and the auto-reload register is preloaded.

The timing diagram illustrates the operation of a basic timer (TIM6/TIM7) when ARPE=1 and the auto-reload register (TIMx_ARR) is preloaded. The diagram shows the following signals and their timing relationships:

An arrow labeled "Write a new value in TIMx_ARR" points to the transition of the Auto-reload preload register at the first vertical dashed line. The diagram is identified by the code MS31083V2 in the bottom right corner.

Timing diagram for a basic timer showing signals 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 over time. It illustrates the sequence of events when ARPE=1 and the auto-reload register is preloaded.

15.3.3 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 148 shows the behavior of the control circuit and the upcounter in normal mode, without prescaler.

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

Timing diagram showing the control circuit and counter register behavior in normal mode.

The diagram illustrates the timing relationships between several signals over time. The 'Internal clock' is a continuous square wave. The 'CEN=CNT_EN' signal is initially low and transitions to high at a certain point. The 'UG' signal is initially low and transitions to high after the CEN signal goes high. The 'CNT_INIT' signal is initially high and transitions to low after the UG signal goes high. The 'Counter clock = CK_CNT = CK_PSC' signal is initially low and transitions to a square wave after the CEN signal goes high. The 'Counter register' shows a sequence of values: 31, 32, 33, 34, 35, 36, 00, 01, 02, 03, 04, 05, 06, 07. Vertical dashed lines indicate the timing of the signal transitions and counter increments.

MS31085V2

Timing diagram showing the control circuit and counter register behavior in normal mode.

15.3.4 Debug mode

When the microcontroller enters the debug mode (Cortex™-M0 core - halted), the TIMx counter either continues to work normally or stops, depending on the DBG_TIMx_STOP configuration bit in the DBG module.

15.4 TIM6/TIM7 registers

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

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

15.4.1 TIM6/TIM7 control register 1 (TIMx_CR1)

Address offset: 0x00

Reset value: 0x0000

1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.ARPERes.Res.Res.OPMURSUDISCEN
rwrwrwrwrw

Bits 15:8 Reserved, always read as 0.

Bit 7 ARPE : Auto-reload preload enable

0: TIMx_ARR register is not buffered.

1: TIMx_ARR register is buffered.

Bits 6:4 Reserved, always read as 0.

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

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 generates 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). 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: Gated 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.

15.4.2 TIM6/TIM7 DMA/Interrupt enable register (TIMx_DIER)

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.

15.4.3 TIM6/TIM7 status register (TIMx_SR)

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:

–At overflow or underflow regarding the repetition counter value and if UDIS = 0 in the TIMx_CR1 register.

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

15.4.4 TIM6/TIM7 event generation register (TIMx_EGR)

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

15.4.5 TIM6/TIM7 counter (TIMx_CNT)

Address offset: 0x24

Reset value: 0x0000

1514131211109876543210
CNT[15:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

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

15.4.6 TIM6/TIM7 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 into the active prescaler register at each update event.

15.4.7 TIM6/TIM7 auto-reload register (TIMx_ARR)

Address offset: 0x2C

Reset value: 0xFFFF

1514131211109876543210
ARR[15:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

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

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

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

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

15.4.8 TIM6/TIM7 register map

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

Table 51. TIM6/TIM7 register map and reset values

OffsetRegister313029282726252423222120191817161514131211109876543210
0x00TIMx_CR1Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.ARPERes.Res.Res.Res.OPMURSUDISCEN
Reset value00000
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.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.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.Res.UG
Reset value0
0x24TIMx_CNTRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.CNT[15:0]
Reset value000000000000000
0x28TIMx_PSCRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.PSC[15:0]
Reset value000000000000000
0x2CTIMx_ARRRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.ARR[15:0]
Reset value111111111111111

Refer to Section 2.2 on page 37 for the register boundary addresses.