Introduction to STM32 Timers

Keywords: Embedded systems, ARM, STM32F4, Timers, Counter

In the world of embedded systems the most important parameter is time. It’s the time that qualifies a systems from simple blinky led to complex inter-systems communication. Its time which defines how fast/slow a job can be done on target processor. Obviously what’s the purpose of time if you can’t count it? This where timers comes into action. Timers provide the facility to measure time relatively from one reference point to another. This relative time measure enables us to generate an event or a specific time interval (like signal) or conversely measure an event time. This makes timers one of the crucial part of Embedded Systems.

The basic idea behind timers is that its logic is provided with some clock and upon each clock it increments/ decrements a counter register value. At the end the counted number of cycles is multiplied with input clock Time period to get the total time elapsed.

Total Time: Counter Value * 1/Frequency

Figure-1: Timer General Operation

A term that usually confuses newbies is the difference between a Timer and a Counter. As a matter of fact there is no such thing as Timers…!!! We only have counters in embedded systems. The source of confusion is that both these terms are NOT so often used INTERFACHANGABLY. The basic functions of both is to count time based events. A minor difference that is usually considered to differentiate timers and counters is frequency source. While in case of timers the input events (clock pulses) have same frequency i.e. clock to timers is fixed; and when the timer is feed from some external events which may or may not of fixed time intervals, the timer is known be as Counter (Counting events). But the basic idea behind both of them is same – They just count events. That’s it.

STM32 Timers:

Based on features/functionalities STM32 Microcontrollers Timers are categorized in 3 main types.

  1. Basic Timers
  2. General Purpose Timers
  3. Advanced Timers

Almost all STM32Fxxx devices come with General purpose timers and Advanced timers with optional availability of Basic Timers which is covered by ARM Systick timer itself.

Basic Timers:

As the name suggests, its a timer with basic functionality of counting and works independently of any I/O interaction. Its just connected to some on-chip frequency source and simply counts time. This type of timer can’t capture external events or produce output signals like PWM etc.

General-Purpose Timers:

This type of timers are available on almost every STM32Fxxx device. These timers are like the Basic timers with one exception that they can also perform I/O operation/interaction like generating PWM on a pin or capturing an event from external source for comparison purpose.

Advanced timers:

These type of timers inherent all the features of General-Purpose timers with add-ons of few advance features that generate SPECIAL signals for some time critical devices like Power Electronics, Motor Control etc.

There is another special purpose timer usually available on embedded SOCs known as watchdog timer. This is a special purpose timer which prevents processor from getting stuck at a single point in software for more than a specified allowed time. The watchdog timer is kicked (reset) in various points in software flow. If software gets stuck in some infinite loop and watchdog timer gets fired (counted to specified value), the watchdog will reset the processor similar to Power ON Reset.

Figure-2: STM32 Timers Types

Example: The STM32F405xx and STM32F407xx devices include two advanced-control timers, eight general-purpose timers, two basic timers and two watchdog timers.

The following table summarize their features.

Table-1: STM32F4xx Timers

STM32 Timer Internals:

Let’s discuss briefly what’s going on under the surface. In this section we will try to dig down a little more into working anatomy of STM32 Timers. I will try to keep things as simple as possible, for more in-depth information, refer to respective SoC datasheet.

Shadow and Preload Register:

In STM32 Timers, certain registers have their duplicate/alias registers. The actual registers are called Preload registers and the duplicate/alias ones are called shadow registers. The shadow registers are not accessible/visible to software while the actual registers can be programmed by software. The point of duplication is to copy and lock the value from actual register to shadow register and then comparison is made with the shadow registers to prevent undesirable results. In other words the Preload registers are used by Software and Shadow registers are used by Hardware.

Following are the registers in STM32 microcontrollers that have their duplicate registers.

  • Pre-scaler Register (TIMx_PSC)
  • Auto-reload Register (TIMx_ARR)
  • Repetition Counter Register (TIMx_RCR)

STM32 Timer Events:

Update Event (UEV): This event causes the shadow registers values to be updated from preload registers (ARR, PSC, CCRx) for possible new values. This event is fired whenever the Counter register overflows/underflows or when the UG bit is set manually by software in TIMx_EGR register.

STM32 Timers implementations consists of the following main blocks.

  1. The clock-subsystem
  2. Time-base Unit (The Timer Kernel)
  3. Input Stage and Signal Conditioning
  4. Output Stage

1. Time-Base Unit (The Timer Kernel):

This is the hearth of STM32 Timers. As a matter of fact this is the actual Timer. It is the base logic where all the counting occurs. The Time-Base Unit basically consists of the following main registers.

  1. Auto-Reload Register: This contains the actual value with which the Timer counted value is compared. There are actually two auto-reload registers. The one that is accessible to software and the one that is shadow of this register. The shadow register is not accessible to software and the value from actual auto-reload register is shifted to shadow register for comparison.
  2. Prescaler Register: This register is used to cut-down/divide the input clock frequency to timer by a constant factor. For example if 100Mhz is the input clock to Timers logic before prescaler and the prescaler register is loaded with 1 (actual divider: prescaler register value + 1) then the frequency that will appear at timer input will be 50Mhz.
  3. Counter Register: This is the actual register that contains the value counted by the timer at any given time.
  4. Repetition Register: This register is used to configure STM32 timers not to generate interrupt/event every time the counter register counts to auto reload shadow-register. The event is generated only when a number (REP register value) of count cycles are completed.
  5. Capture/compare Registers: As the name suggests these register captures input from input channels for comparison and/or produce output after comparison on output channels.
Figure-3: STM32F4xx Timers Time Base

2. The clock-subsystem:

The clock subsystem also known as clock and trigger controller is main clock providing unit that multiplex multiple selectable clock sources on single Timer input line. Following are the main clock sources that feed clock and trigger controller.

  1. Internal Clock: This is the clock source that is feed from RCC STM32 Unit. This clock source comes from main system clock of STM32 MCU Figure-4(1).
  2. ITRx: These are input lines that can be connected to the output of other on-chip timers in master/slave architecture. In this configuration, one timer, called Master, provides clock and other control signals while the receiving timer, called Slave, actually perform the counting operation Figure-4(2).
  3. TIMx_ETR: This line connects timers with external PIN to count external events like counting external pulses Figure-4(3).
  4. TIxFPx: These lines also connect timer to external clock source like TIMx_ETR with the only exception that the input clock from the external pins are first passed through some intermediate logic (discussed later) for edge detection/digital filtration to remove spurious events Figure-4(4).
Figure-4: STM32F4xx Timers clock Sources

3. Input Stage and Signal Conditioning:

The input stage and signal conditioning provide the facility intermediate process like digital filtering etc. before the signal is feed to timer.

Figure-4: STM32F4xx Timers Input Stage

4. Output Compare:

The output stage is used to generate some timing signals like PWM etc. for off-chip peripherals.

Figure-5: STM32F4xx Timers Output Compare

The whole STM32 timers units are interconnected as follow:

Figure-6: STM32F4xx Timers Logic


[1] – Reference manual-RM0090

75 thoughts on “Introduction to STM32 Timers”

Leave a Reply

Your email address will not be published. Required fields are marked *