cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 stopping and restarting PWM output has residual repetition count

Gavin Baker
Associate III
Posted on June 15, 2018 at 05:37

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:0690X0000060Bg2QAE.png

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:

0690X0000060AMlQAM.png0690X0000060BhPQAU.png

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

#stm32 #pwm #timing #waveform #clock #timer
1 ACCEPTED SOLUTION

Accepted Solutions
Posted on June 15, 2018 at 09:25

0690X0000060LHQQA2.png

JW

View solution in original post

2 REPLIES 2
Posted on June 15, 2018 at 09:25

0690X0000060LHQQA2.png

JW

Posted on June 15, 2018 at 10:24

Jan,

I added the following code before restarting the timer PWM outputs and it worked:

TIM1->EGR |= (1 << 0); // Bit 0: UG

I was too focused on trying to modify/reset the state, and I didn't realise you could trigger timer events in software in this way. Very useful to know.

Thanks so much! Cheers -

  :: Gavin