AnsweredAssumed Answered

STM32 stopping and restarting PWM output has residual repetition count

Question asked by Gavin Baker on Jun 15, 2018
Latest reply on Jun 15, 2018 by Gavin Baker

Working on an STM32F103, using PWM to generate waveforms. The timer is in One Pulse mode and uses the Repetition Count Register to clock out the required signals. Based on user selection, I am stopping and starting the output using HAL_TIM_PWM_Stop_IT()/HAL_TIM_PWM_Start_IT(). There are multiple timers in a master-slave configuration, and interrupts are being used to interface with a DAC, so there are many things that must remain in sync.

 

As expected, stopping and re-starting the timer does not affect the timer state, notably the counter CNT. That's fine as I can simply zero this register before restarting, to ensure the output is in sync. The problem is that the timer is stopped at some arbitrary point in time at which the repetition counter is at some arbitrary value, and when the output is restarted, the repetition counter appears to resume from this arbitrary value, thus introducing a short delay or phase shift in the output, proportional to the remaining count. So the first run of the output after powerup is always correct and in sync:

 

 

This shows all the outputs going from off to on, as expected.

 

However, second and subsequent times we stop and start the PWM output, we see the output starts but then stops unexpectedly leaving a variable length gap right after, then resumes and continues correctly:

 

 

Each time the device stops and restarts, the gap is of a variable length. I am manually resetting the TIMx_CNT register before enabling the PWM output. The varying size of the gap, which appears to have a maximum size, is completely consistent with the repetition count not starting from 0.

 

In the reference manual (RM0008) in Section 14.4.13 describing TMx_RCR, it mentions REP_CNT, and "Figure 52: Advanced control timer block diagram" has boxes showing the "REP Register" and "Repetition Counter", but I can't find any more info on the subject, or any way to access this in the register map. This appears to be the *actual* running repetition count which would be reset by the value from RCR.

 

How can we clear REP_CNT to avoid these glitches? Any insights appreciated! Thanks -

 

  :: Gavin

Outcomes