cancel
Showing results for 
Search instead for 
Did you mean: 

STM32G071: TIM1+PWM+DMA 2nd pulse looks strange

Mischa
Associate II

Hi,

I'm trying to talk to WS2812 RGB Leds. I'm using TIM1 CH3 PWM Output with DMA. Everything looks almost fine. I'm always sending the same data using:

HAL_TIM_PWM_Start_DMA (&htim1, TIM_CHANNEL_3, (uint32_t *) pwmData, (24 * NUM_OF_LEDS) + 50);

Here pwmData sets a duty cycle of 33 % (20 of period 60, 48 MHz) for the first 13 cycles. In between I wait for 500 ms.

But time to time (~1 out of 3) the second pulse behaves strange. Only the second pulse seems to randomly jump approx. from duty cycle 20 ... 80 %. All remaining pulses are correct.

Thanks for any hint!

4 REPLIES 4

I don't use Cube and don't know how exactly Cube does this thing, but under certain circumstances (CCR preload enabled), the first two pulses may actually be result of the last two values in buffer from the previous run.

Try using deliberately different duty cycles in the first few cycles, like 30% in first, 40% in second etc., and observe.

JW

With timer you have to manage some complications with proper DMA request source, preloads etc. Consider using SPI instead timer. SPI can simply generate pulse train that you need on MOSI line (let SCK free). Imagine sending byte with bitrate 8Mb/s bit sequence:

0b11100000 generates pulse width 3*1/8=375ns and space 625ns

0b11111000 generates pulse width 5*1/8=625ns and space 375ns

Moreover by variating SPI bitrate and message bit length (up to 16bits) you can sending multiple WS2812 bits in one byte/word. It can be realised much more easier then handling timers.

Nice trick with the SPI, @Michal Dudka​ !

JW

Mischa
Associate II

@Michal Dudka​  I like that idea using SPI, too. Thanks!