Bjm
Bjm

Reputation: 123

STM32 PWM first pulse width varies

I'm using Marlin on a BTT SKR MINI E3 V3.0 board (ARM Cortex M0+ STM32), and I'm trying to use a PWM signal to time a delay used to acquire images.

I do the timing by counting a number of pulses in a loop, like this:

    loop()
    {
      Attach interrupt on pwm pin
      Start pwm signal
      count rising edges in interrupt handler
      turn off pwm
    }

The problem is that I see that the first pulse sometimes varies in width (please see this video https://gemoo.com/tools/upload-video/share/581538573017690112?codeId=v6gl6nmk112Oz&card=581538569448337408)

This only occurs after the board has been idle for a while (ie. hours). I can restart the board then everything is ok.

Also the pulse width, as it can be seen in the video varies periodically, so eg. there are 5 ok pulses the 1 faulty, then 5 ok.

I'm at loss for where to look, but I think this is a software issue.

Has anybody experienced something similar?

Upvotes: 0

Views: 351

Answers (1)

Bjm
Bjm

Reputation: 123

Resetting the timer that is used to generate the pwm before starting the signal seems to fix this problem. I guess it makes sense, if the signal start at a random count. I do something like this:

TIM2->CR1 &= ~(1U); 
TIM2->CNT = 0x0000;
TIM2->SR = 0;
TIM2->CR1 |= 1U;

This is inspired by https://www.youtube.com/watch?v=K0_hfbT7vWA&t=634s

Upvotes: 0

Related Questions