STM32 center-aligned PWM: when TIM1 triggers the ADC
Trace STM32H743 TIM1 channel 4 through TRGO to injected ADC conversions, and understand why ServoHub samples near the PWM valley.
In ServoHub, an STM32H743 timer schedules the phase-current ADC conversions near the valley of a center-aligned PWM counter. The route is concrete: TIM1 channel 4 → OC4REF → TRGO → injected ADC trigger. Here are the configuration lines that establish that route, and the limits of what the timing diagram tells us.
Source excerpts refer to ServoHub 94fc13d, main.c. Timing and ripple examples come from episode 7a.
The problem
The companion current-conversion walkthrough, episode 7b, uses a reading of 32990 on phase U at 1197.4 ms, which works out to 0.2505 A. But phase current isn't flat inside one PWM period; it ripples. In the course's model of this motor (48 V bus, 0.19 mH per phase) the ripple is 0.28 A peak to peak, on a reading of 0.25 A. Sample at a random instant and you could be off by 0.14 A either way.
So the question: at what instant was 32990 taken, and why that one?
The answer: channel 4, compare value 1
TIM1 counts center-aligned with a period of 6000 at 240 MHz: up to 6000, back down to 0. That's 12000 ticks, 50 µs, 20 kHz. The three phases use channels 1–3. Channel 4 drives no pin. It exists only to fire the ADCs:
// embedded_project/Y01/Core/Src/main.c
999: htim1.Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED1;
1000: htim1.Init.Period = 6000;
1017: sMasterConfig.MasterOutputTrigger = TIM_TRGO_OC4REF;
1031: sConfigOC.OCMode = TIM_OCMODE_PWM2;
1050: sConfigOC.Pulse = 1; // then HAL_TIM_PWM_ConfigChannel(..., TIM_CHANNEL_4)
...
402: sConfigInjected.ExternalTrigInjecConv = ADC_EXTERNALTRIGINJEC_T1_TRGO;
403: sConfigInjected.ExternalTrigInjecConvEdge = ADC_EXTERNALTRIGINJECCONV_EDGE_RISING;
In PWM mode 2, OC4REF rises when the counter, counting up, reaches 1. That's one tick (4.17 ns) after the valley. TRGO routes that event to the injected groups of ADC1, ADC2 and ADC3. All three are configured to use the same trigger source.
The 4.17 ns figure describes the timer event, not a measured ADC sampling or conversion latency. ADC clock synchronization, the acquisition interval, and conversion time belong to the ADC timing path. The source separately configures InjectedSamplingTime = ADC_SAMPLETIME_2CYCLES_5 for each ADC. Sharing a trigger should not be read as proof of zero analog sampling skew or a conversion completed 4.17 ns after the valley. See the ADC and TIM chapters of ST's STM32H743 reference manual, RM0433.
TIM1 channel 4 at compare value 1: OC4REF rises 4.17 ns after the valley and goes out as TRGO. (ServoHub ep07a)
Why the valley works
- At the valley the counter is below every phase compare value, so all three lower switches conduct and no switch changes state anywhere near that instant. With compare value 3095, U stays low for 12.9 µs on each side.
- In the course's modeled operating point, the valley reading is close to the period-average current: the modeled difference is 0.02 mA. Symmetric PWM timing helps choose a useful sampling point, but symmetry alone does not prove that any current waveform equals its average at that point. This result depends on the motor model and operating point. It does not establish that a real drive needs no filtering, calibration, or compensation for sensing delays.
Modeled ripple at the illustrated operating point: the valley value is approximately the period average, with a modeled difference of 0.02 mA. This is not an oscilloscope measurement of the board. (ServoHub ep07a)
A practical reading checklist
When checking another STM32 FOC drive, follow the entire chain rather than copying CCR4 = 1 in isolation:
- Confirm the actual timer clock, counter mode, and period.
- Check which compare signal becomes TRGO and which trigger edge the ADC accepts.
- Inspect each ADC's clock and sampling-time configuration.
- Check the switching window and the current-sense amplifier's settling requirements at the operating duty cycle.
- Validate the complete sensing path on hardware before treating a model's ideal sampling instant as a measured result.
Continue with the execution trace
The episode works through it in six stops: the clock tree from a 16 MHz crystal, why only ADC1 raises the interrupt, why the duty cap is 90% (and the 2.5 µs window that cap protects), what dead time does and doesn't delay, a comparison in bsp_set_phase_voltage that never fires, and when a newly written CCR value actually takes effect (preload means the new value lands at the next peak, 25 µs after the sample).
▶ Watch the full walkthrough: ServoHub episode 7a: When the Sample Is Taken