cancel
Showing results for 
Search instead for 
Did you mean: 

Most effective way to generate waveform?

Need to generate a repeating waveform with external SPI 16-bit DAC.

Using a table of values in memory, repeatedly loop through table outputting values to DAC.

Delay between outputs is constant. No glitches at repeat!

DMA only (no interrupts).

What's the most effective way to do this with ST32F427?

Thanks,

Best Regards, Dave

1 ACCEPTED SOLUTION

Accepted Solutions

This turns out to be much easier than I thought (I missed 'DMA circular mode' first go around).

One timer to:

  • trigger DMA on overflow (sets update frequency)
  • generate CS signal of correct length to bracket SPI transfer (unbelievably, lame STM32F SPI module fails to do this correctly)

DMA:

  • output from sample array in memory to SPIx->DR
  • run in circular mode (NDTR is number of samples in waveform table)
  • 16-bit transfers for this DAC
  • triggered by above timer; each timer trigger does 1 transfer (not NDTR transfers as manual might lead you to believe)

Scope picture below (top trace is CS, bottom 2 traces are SPI clock and MOSI):

0690X000006DsNsQAK.png

View solution in original post

11 REPLIES 11

Pattern buffer with TIM driving DMA to GPIO->BSRR

SPI on the STM32 is a bit limited due to the brain damaged implementation of chip select. On could certainly loop DMA data to SPI->DR, but the word level synchronization at the target device is problematic.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

@Community member​ - That's not going to work driving an SPI, and doesn't address how to loop?

One way to do this is with a DMA stream writing SPI:

  • DMA source address from table auto-increment
  • triggered by primary continuous repeating timer.
  • NTR is length of table (? does this do 1 xfer per timer trigger ?)

Primary timer uses additional channels to generate:

  • CS (normally low, raised briefly prior LDAC) (? can lame SPI module generate this ?)
  • LDAC strobe if required by DAC

2nd timer is used to reset system at time after table cycle by using a DMA2 stream to:

  • reset SPI DMA control (source address to start of table, NTR)

This seems really complicated; is there a simpler way to do it?

Thanks in advance,

Best Regards, Dave

I could generate any set of bit patterns I want at very high speed, I could certainly drive an SPI attached device at speeds matching the peripheral implementation, and loop the patterns.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
S.Ma
Principal

What SPI DAC device is considered here? Without knowing how to update the external DAC, it's difficult to "optimise". Also, why not using internal DAC if available. Voltage?

OK, I think I understand. Requires 3 transfers per SPI bit (clock low, set data bit, clock high) plus transfers for CS, so ~50 32-bit words for each 16-bit value to go to the DAC, right?

It always helps a discussion's quality if facts are stated. What DAC, what sample rate/frequencies, what data length, how many channels, which STM32, etc.

For audio-rate signals (i.e. 20Hz-20kHz, no DC-correctness required), look at I2S and respective I2S DACs.

TI mode in SPI module provides framing, too, the DAC has then to be chosen to fit this mode.

I'm not familiar with the DACs offerings, but I'd be willing to bet that there are serial DACs out there which don't require explicit framing, i.e. which automatically convert upon each Nth shifted bit.

If DAC has been already chosen and it requires different framing, besides the fairly straighforward timer-triggered-DMA-to-GPIO as Clive suggests, you can also look at generating whatever clocks waveforms you need using chained timers, and feed them back to SPI in slave mode.

You may also want to look at the SAI module, it's pretty flexible in its settings.

> No glitches at repeat!

Why would there be glitches, if DMA is in circular mode?

JW

@S.Ma​ - We need 16 bits of real accuracy, not 12 bits of marketing accuracy, hence the external DAC (also higher voltage to minimize noise). Standard 16-bit SPI xfer to set DAC (LDAC strobe optional).

Thanks @Community member​ - Faster than audio, waveform is 62kHz. Hadn't thought to see if there are I2S DACs that run fast enough. Perhaps circular mode with NDTR = table length does what I need (possibly requiring timer to generate CS as well as trigger DMA)?