cancel
Showing results for 
Search instead for 
Did you mean: 

Best way to change stored waveforms, when using DMA to serve PWM timer

Hatte
Associate II

I need to dynamically change waveforms on a PWM output on an STMF030 MCU.

See it as a poor man's alternative to a signal generator using a DAC.

It will be used to generate modulated audio waveforms.

I am using DMA to lookup (a sine) waveform via a circular buffer, that is updating TIM->CCR1 to control the PWM duty Cycle. The PWM frequency is in the range 40-100kHz (10-25us period), and waveforms (duty cycle) will be stored in a 16 - 128 word lookup table, providing a modulated waveform in the 1-8kHz range.

I would like to be able to update the waveform (CCR1 table) dynamically, but I am uncertain how to best do so. I would prefer the waveform change to be timed to take place at the completion of a waveform (DMA buffer transfer), to provide a controlled (smooth) transition between the two.

Here are the options, that I have considered.

  1. Use two waveform buffers, one active and one passive. Update the passive waveform, and use the DMA TCIF (transfer complete) interrupt, to update the DMA to now use the (previously) passive buffer. This would require the interrupt to be serviced in 25us, to ensure a change before the next DMA fetch.
  2. Use two waveform buffers (as above), but rather than changing the DMA memory address to a new buffer, update the contents of the actively used buffer in two stages. The first half-buffer updated upon DMA HTIF (half -transfer) interrupt, and the other half upon DMA TCIF (transfer complete interrupt). That would make the ISR less time critical (16+ periods = 400+us), but is a little more complex, and leaves half the time to prepare the subsequent waveform. The buffer-to-buffer memory transfer could potentially be done using memory-to-memory DMA, but that may be overkill.

I would really like some advice as to options of achieving my goal of smoothly transitioning waveforms, and any suggestions are welcome.

Thanks in advance,

Morten

1 ACCEPTED SOLUTION

Accepted Solutions

> This would require the interrupt to be serviced in 25us,

Why wouldn't that be viable? Surely writing to a couple of registers on a multi-ten-MHz mcu should not last more than a few microsenconds (or even below a microsecond with a bit of planning) - unless you burn them up in some inflated "library".

> half-buffer updated upon DMA HTIF

Doesn't sound hopeless either, but that of course depends on the particular algorithm you want to employ. Benchmark.

JW

View solution in original post

1 REPLY 1

> This would require the interrupt to be serviced in 25us,

Why wouldn't that be viable? Surely writing to a couple of registers on a multi-ten-MHz mcu should not last more than a few microsenconds (or even below a microsecond with a bit of planning) - unless you burn them up in some inflated "library".

> half-buffer updated upon DMA HTIF

Doesn't sound hopeless either, but that of course depends on the particular algorithm you want to employ. Benchmark.

JW