STM32 Timer Synchronization and Trigger Modes Explained

In the previous article, STM32 Encoder Mode Explained: Read Rotary Encoders, the counter of a timer was driven by two external signals instead of the internal clock. Encoder mode is one example of the slave mode controller that most general-purpose and advanced timers contain. The same controller can also take its input from another timer, and this connection is the basis of timer synchronization. When one timer reports an event to another timer in hardware, the two counters can start, stop, and reset together without involving the CPU.

In this article, we will first look at the reasons for connecting timers and at the meaning of the master and slave roles. After that, we will go through the three common slave modes, Reset, Trigger, and Gated mode. We will then build a small example on the NUCLEO-F446RE board where TIM2 gates the PWM output of TIM3. We will also see how a timer can start an ADC conversion.

The article is an introduction to the concepts and the main settings, so it does not cover every trigger-routing detail. This article is part of the STM32 timer tutorial series.

Why Synchronize STM32 Timers

A single timer is enough for many jobs, but some applications need two or more counters that follow each other exactly. The simplest way to connect them is in software, for example by starting the second timer in the line after the first one. This works for slow signals, but the two timers then start a few clock cycles apart, and the size of that gap depends on the compiler, the interrupt activity, and the code around the function calls.

Hardware synchronization removes this uncertainty, because the trigger travels between the timers as an electrical signal that is handled in the same clock domain.

The first use case is coordinated PWM. Two PWM outputs from different timers can have the same frequency, but they do not keep a fixed phase to each other, because each timer starts counting at its own moment. For example, in an interleaved buck converter, the two phases should switch half a period apart, and a start-up offset that changes after every reset breaks that spacing. When the master timer starts or resets the slave, both counters begin from the same point, and the phase between the outputs is the same after every power-up. Phase alignment follows the same idea in a smaller form, since the slave restarts at the beginning of each master period and its edges always keep the same distance from the master edges. The STM32 PWM Mode Explained article covers the PWM settings that both timers need.

The second use is a timed sequence. A master timer can produce a compare event after a defined delay and use it to start a slave timer, so that the slave begins its pulse or its counting exactly at that moment. Chains of this kind create delays and bursts without any interrupt code, and they build on the mechanisms from STM32 Output Compare and One-Pulse Mode Explained.

The last use is triggering other peripherals. A timer event can start an ADC conversion or update a DAC at a precise time, which gives a sampling rate with almost no jitter and keeps the CPU free for other work.


STM32 Master and Slave Timers

The idea behind synchronization is that one timer sends a signal and another timer listens to it. The sending timer is called the master, and the listening timer is called the slave. These roles are not fixed properties of a timer, because a timer can be a master for one timer and a slave of another at the same time, and this allows chains of several timers. The role only depends on how the registers are configured.

Block diagram of a master timer sending TRGO through internal trigger routing as ITRx to a slave timer

Master Timer and TRGO

The master timer sends its signal through the trigger output, which is called TRGO. The master decides which internal event appears on this output, and the choice is made with the master mode selection bits (MMS) in the control register CR2. In HAL, the same choice is the field MasterOutputTrigger in the structure TIM_MasterConfigTypeDef, and CubeMX shows it as Trigger Event Selection. The table lists the sources that are typically available.

TRGO sourceHAL valueWhen TRGO is generated
ResetTIM_TRGO_RESETWhen the UG bit is set by software
EnableTIM_TRGO_ENABLEWhen the counter is enabled
UpdateTIM_TRGO_UPDATEAt every update event, for example an overflow
Compare PulseTIM_TRGO_OC1As a pulse when channel 1 sets its compare flag
Compare referenceTIM_TRGO_OC1REF to TIM_TRGO_OC4REFWhile the reference signal of the channel is high

Not every timer offers every source, because the compare sources depend on the number of channels, and the basic timers TIM6 and TIM7 only offer the first three. A basic timer can therefore act as a master, but it cannot be a slave, because it has no slave mode controller.


Slave Timer and ITR

The slave timer receives the signal on its trigger input, which is called TRGI. The slave selects the source of TRGI with the trigger selection bits (TS) in the register SMCR. The sources that connect to other timers are the internal trigger inputs ITR0 to ITR3, and we use one of these ITR inputs for synchronization between timers. After the trigger input is selected, the slave mode bits (SMS) decide what the slave does with the signal, and the next section describes these modes.


Trigger Routing Depends on the MCU

The numbers ITR0 to ITR3 do not point to the same master on every timer. Each timer has its own fixed assignment, and this assignment is documented in the reference manual in a table with a title such as “TIMx internal trigger connection”. On one timer, ITR1 can be connected to TIM2, and on another timer, the same ITR1 can be connected to TIM8. The assignments also change between STM32 families, so a configuration that works on an STM32F4 cannot be copied to an STM32G4 or an STM32H7 without checking the new table.

The image below shows the STM32F446 Timer internal trigger connections for the General Purpose Timers.

STM32F446 reference manual table of internal trigger connections ITR0 to ITR3 for the general-purpose timers

The routing table is the most important reference for every synchronization project, and the example later in this article shows how to read it.


STM32 Timer Trigger Modes

The slave mode defines what the slave counter does when the trigger input changes. The three modes in this section are the most common ones, and each of them works on the level or on the edge of the trigger input. Some other slave modes exist as well, such as the external clock mode, which uses the trigger as the clock of the counter, and the encoder modes from the previous article. Several newer families add combined modes such as Reset + Trigger, which are not covered here.

Reset Mode

In Reset mode, the rising edge of the trigger input clears the counter of the slave and also generates an update event. The prescaler counter is cleared at the same time, so the slave restarts from zero as if it had just been enabled. The counter keeps running freely between two triggers, and it can overflow on its own if the next trigger arrives late.

This mode is useful whenever the slave has to restart together with another signal. A common case is a phase-aligned PWM signal, where the master update event resets the slave at the start of every master period. The slave output then keeps the same offset from the master output for the whole run, even if the two timers use different frequencies. The mode is also useful for measuring the time since an external event, because the counter always starts from zero at the event.

Timing diagram of Reset mode where the slave counter returns to zero on every rising edge of the trigger input

Trigger Mode

In Trigger mode, the rising edge of the trigger input starts the counter of the slave. The counter has to be stopped before the trigger arrives, which means that the enable bit CEN is cleared, and the hardware sets this bit when the trigger comes. The counter then runs on its own, and later triggers have no effect while it is running. The start is controlled by the trigger, but the stop is not, so the counter keeps counting until it is stopped by software or by another condition, such as one-pulse mode.

This mode is useful for starting a timer at a defined moment relative to another event. One example is a delayed pulse, where the master generates a compare event after a set time and the slave then starts its own pulse or its own PWM output. When combined with one-pulse mode, as covered in output compare article, the slave produces exactly one pulse for every trigger.

Timing diagram of Trigger mode where the slave counter starts on the first trigger and ignores the second

Gated Mode

In Gated mode, the counter of the slave runs only while the trigger input is high. When the input goes low, the counter stops and keeps its current value, and when the input goes high again, the counter continues from that value. The counter has to be enabled by software (CEN set) before the gate has any effect, and then the gate controls the clock of the counter. The prescaler pauses together with the counter, because the gate acts on the clock that feeds both of them.

This mode is useful whenever a signal should be active for a limited time window. Typical examples are PWM bursts that are only present while a master signal is high, a pulse counter that counts events only during a measurement window, and a timer that measures how long a gate signal stays active. The most important detail is that a stopped counter is not reset, so the slave output keeps the level it had at the moment of the stop.

Timing diagram of Gated mode where the slave counter runs only while the trigger input is high

STM32 Timer Synchronization Example

In this example, we connect two timers on the NUCLEO-F446RE board, as in the earlier articles of the series. TIM2 is the master and produces a 100 Hz signal with a duty cycle of 30 percent, which stays high for 3 ms in every 10 ms. TIM3 is the slave and generates a 10 kHz PWM signal, but it runs in Gated mode, so the PWM only appears while the master signal is high. The result is a burst of PWM cycles every 10 ms, and the length of every burst is controlled by hardware. The timer clock is 90 MHz for both timers in this example, and if you use a different clock setup, the prescaler values below have to change.

Choose the Timer Pair

The first step is to find a pair of timers that are connected on the device. For the STM32F446, the reference manual RM0390 contains a table that lists the timers’ internal triggers. The table below shows the entries for the general purpose timers of this device, and the entry for TIM3 is the one that we use.

Slave timerITR0ITR1ITR2ITR3
TIM2TIM1TIM8TIM3TIM4
TIM3TIM1TIM2TIM5TIM4
TIM4TIM1TIM2TIM3TIM8

The row of TIM3 shows that TIM2 is connected to the input ITR1, so TIM2 can act as the master and TIM3 as the slave with the trigger source ITR1. CubeMX lets you check the same connection without the manual, because the Trigger Source list of the slave timer only offers the inputs that exist on that timer. If you select a different pair, repeat this lookup before you configure anything.

Internal trigger table with the TIM3 row highlighted and TIM2 marked under ITR1

Configure the Master Timer

Now we will configure the Master timer, TIM2. Set the Clock Source to Internal Clock and set Channel1 to PWM Generation CH1, which assigns the channel to the pin PA0. The synchronization itself only needs the channel to be in PWM mode, so PWM Generation No Output works as well, but the pin lets us look at the gate signal on an oscilloscope, and we use it for that reason.

In the Parameter Settings tab, set the Prescaler to 8999 and the Counter Period to 99. This will give a 10 kHz counter clock and a 100 Hz period.

CubeMX settings of TIM2 as master with PWM channel 1 and TRGO set to Output Compare (OC1REF)

Set the Pulse of channel 1 to 30, so that the reference signal stays high for 30 counts, which equals 3 ms. Open the Trigger Output (TRGO) Parameters group and set the Trigger Event Selection to Output Compare (OC1REF), which sends the reference signal of channel 1 to the slave.

The generated code in MX_TIM2_Init() contains the lines below.


Configure the Slave Timer

Select TIM3 next, keep the Clock Source on Internal Clock, and set Channel1 to PWM Generation CH1, which puts the output on the pin PA6. The Internal Clock stays selected because in Gated mode the counter still uses the internal clock, and the trigger only enables it. Below the clock source, set Slave Mode to Gated Mode and the Trigger Source to ITR1, which matches the entry that we found in the routing table.

In the Parameter Settings tab, set the Prescaler to 89 and the Counter Period to 99, which produces a 1 MHz counter clock and a 10 kHz PWM signal. Set the Pulse to 50 for a duty cycle of 50 percent.

CubeMX settings of TIM3 as slave with Gated Mode, trigger source ITR1, and PWM channel 1

The relevant part of MX_TIM3_Init() looks as follows, and the slave configuration is the call to HAL_TIM_SlaveConfigSynchro().


Start the Timers

The generated code prepares both timers but does not start them. We start the slave first, so that it is ready when the first gate signal arrives. Then we start the master, because the master is the source of the gate. In Gated mode, HAL_TIM_PWM_Start() enables the counter of the slave as usual, and the gate then decides when the counter actually runs. If you later change the slave to Trigger mode, HAL leaves the enable bit untouched on purpose, because the hardware has to set it when the trigger arrives.

The other two slave modes use the same structure. Changing SlaveMode to TIM_SLAVEMODE_RESET or TIM_SLAVEMODE_TRIGGER selects Reset mode or Trigger mode, and the trigger source stays ITR1. In Reset mode, TRGO of the master is better set to the Update source, so that the slave restarts at the start of every master period.


Check the Result

Connect an oscilloscope or a logic analyzer to PA0, which carries the gate signal of TIM2, and to PA6, which carries the PWM signal of TIM3.

The image below shows both signals captured on the logic analyzer.

Logic analyzer capture of the TIM2 gate signal on PA0 and the gated 10 kHz PWM bursts of TIM3 on PA6

The signal on PA0 is high for 3 ms in every 10 ms, and the signal on PA6 shows a 10 kHz square wave only while PA0 is high. A burst should contain close to 30 PWM cycles, and the small difference comes from the short synchronization delay between the trigger and the counter.

An important detail is visible at the end of every burst. When the gate goes low, the counter of TIM3 stops, and the output pin keeps the level that it had at that moment. In this example, the burst length is an exact multiple of the PWM period, so the output stops at the start of a cycle, but a gate time that is not a multiple of the period would freeze the output in the middle of a cycle. If the idle level matters for your circuit, choose the timing so that the gate ends at a point where the output has the correct level.


STM32 Timer Trigger for ADC

A timer can also send its TRGO signal to peripherals that are not timers, and the ADC is the most common receiver. In this arrangement, the timer works as the sample clock of the ADC, and every trigger edge starts one conversion. The conversions then happen at exactly equal intervals, which is not possible when the sampling is started from a software loop or from a timer interrupt, because the interrupt latency adds jitter. The ADC does not use the slave mode controller for this purpose, since it has its own external trigger input that lists the timer outputs it accepts.

The setup needs two settings. On the timer side, TRGO is set to the source that matches the desired sampling rate. For a fixed sample rate the Update event is the simplest choice, because it produces one trigger for every counter overflow. On the ADC side, the external trigger is set to the timer output, and the trigger edge is set to rising.

CubeMX ADC settings with the external trigger set to the Timer 2 trigger output on a rising edge

In HAL, these are the two fields shown below, where TIM2 TRGO is used as the trigger source.

The list of timer outputs that an ADC accepts depends on the device and on the ADC instance. The same principle applies to the DAC, which can update its output on a timer trigger and produce waveforms with a precise sample time. The conversion results of a timer-triggered ADC are often collected by DMA, so that no CPU time is spent per sample, and the DMA article of this series describes this combination.


Timer Synchronization Problems

Incorrect ITR and TRGO Mapping

The most frequent mistake is the selection of an ITR input that is not connected to the intended master. The configuration is accepted without any warning in this case, because every ITR input is a valid choice from the point of view of the registers. The slave then listens to a different timer or to a timer that does not run, and nothing happens. The fix is to look up the slave timer in the routing table of the reference manual and to choose the input that lists the master. The same applies to the master side, where the TRGO source has to be the event that the slave expects, for example Update for a reset and OC1REF for a gate.


Unsupported Timer Combination

Not every timer can be a master or a slave, and not every pair is connected. Basic timers can only be masters, some small general-purpose timers have no slave mode at all, and the pairs that are available differ between devices. A pair that exists on one family can be missing on another family, and the trigger source list in CubeMX is a quick way to find out. Timers on different buses can also run with different clocks, which is not an error, but it changes the frequency ratio between master and slave and has to be part of the calculation. More information on the timer groups is available in STM32 Timer Types Explained.


Unexpected Startup Phase or Counter Reset

The last group of problems concerns the state of the slave counter at the start. A slave that was used before can still hold an old counter value, so the first burst or the first period is shorter than expected. Writing zero to the counter before the start removes this effect. The Reset mode also generates an update event on every trigger, which loads the preloaded registers and can start an update interrupt or a DMA request. If two timers have to start at exactly the same clock edge, the Enable source of the master together with the Trigger mode of the slave is the right choice. The Prescaler, Counter and ARR article explains how the preload registers and update events behave, and STM32 Timer Interrupts Explained shows how to react to them.


Conclusion

Timer synchronization connects the counters of an STM32 in hardware, so that one timer can start, stop, or reset another with no software delay. The master selects an internal event as its trigger output (TRGO), the slave selects the matching internal trigger input (ITRx), and the slave mode decides how the counter reacts. Reset mode restarts the counter on every trigger, Trigger mode starts it once, and Gated mode runs it only while the trigger is high.

For a reliable setup, check the trigger routing table of your device first, confirm that both timers support the modes that you need, and make sure that the master really produces the selected event. The same trigger outputs also start ADC conversions and DAC updates, which gives precise sampling without CPU load. In the next articles, we will look at how DMA moves the data of timer-triggered peripherals without any CPU work.


Download the Project Here

Download the CubeMX project files and HAL source code โ€” all tested on real hardware.

If you find this useful, you can support us with a Donation as well.

What to Read Next

Article by Yashmiti

Yashmiti is a technical writer at STM32Guide, where she creates tutorials and guides that help embedded-systems learners and developers work confidently with STM32 microcontrollers. Her writing covers tools like STM32CubeIDE and STM32CubeMX, along with peripherals and communication protocols โ€” always with an emphasis on clear explanations and hands-on examples that take readers from concept to working project.

She's also part of the effort to build STM32Guide into an organized, easy-to-navigate resource for the STM32 community.

Leave a Comment