Search topics...
Timers & PWMTimer Synchronizationfoundational

How can you synchronize multiple timers, and why would you need to?

0 upvotes
Practice with AISoon
Study the fundamentals first — Timers & PWM topic page

STM32 timers can be interconnected using the internal master/slave trigger system. A master timer outputs a trigger signal (TRGO — Trigger Output) on configurable events: counter enable, overflow (update), compare match, or output compare pulse. A slave timer receives this trigger through internal trigger inputs (ITR0 through ITR3, each hard-wired to a specific master timer) and responds according to its configured slave mode — it can start, stop, reset, gate, or clock its counter based on the trigger.

Common use cases include: (1) Cascading timers for extended counting range — a master timer's overflow event clocks a slave timer, creating a 32-bit counter from two 16-bit timers. This is essential when TIM2/TIM5 (the 32-bit timers) are unavailable. (2) Synchronized multi-phase PWM — all PWM channels across multiple timers start counting at exactly the same instant. This is critical for 3-phase motor control where the phase relationships between U, V, and W PWM signals must be exact. Even a few clock cycles of skew between timer starts can cause current imbalance and torque ripple. (3) Timer-triggered ADC sampling — a timer TRGO triggers ADC conversions at precise intervals with zero CPU intervention and zero jitter, far more accurate than software-triggered sampling. (4) Gated counting — one timer enables another only during a measurement window, useful for frequency counting applications.

The internal trigger routing (which ITR connects to which timer) is fixed in silicon and varies between STM32 families. Getting the mapping wrong is a common bug — always consult the reference manual's "TIMx internal trigger connection" table. For example, on STM32F4, TIM1's TRGO is connected to TIM2's ITR0, TIM3's ITR0, and TIM4's ITR0 — but TIM8's TRGO connects to different ITR inputs on each slave. There is no way to change these mappings in software; if the required trigger path does not exist, you must use a different timer combination or route the trigger through a GPIO pin and use external trigger mode.

Source: Timers & PWM Q&A