Author Topic: Triggering the STM32 ADC in the DMA mode  (Read 14229 times)

0 Members and 1 Guest are viewing this topic.

Offline VanitarNordicTopic starter

  • Frequent Contributor
  • **
  • Posts: 277
  • Country: 00
Triggering the STM32 ADC in the DMA mode
« on: January 15, 2019, 07:02:37 pm »
I have configured the STM32 ADC in the DMA mode which fills a buffer, for example, a buffer with 100 elements (buffer[100])

I want that the ADC start the conversions only by a Timer trigger. I mean when the Timer or internal trigger happens, it should do the conversion of all 100 samples, then it must wait for the next trigger to start the conversion again.

I don't want to use interrupts because interrupts could have unknown latency or time shift, but in my case, I want to sample the input signal without any possible shift in the sampling and if I use the timer interrupt as a trigger (to start the ADC DMA), then some unwanted shifts in the conversions might happen, because the conversions start point is not a fixed number.

unwanted time shifts in the sampling should be as low as possible, even in the range of nS.

I use CubeMX.
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4068
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: Triggering the STM32 ADC in the DMA mode
« Reply #1 on: January 15, 2019, 07:22:56 pm »
What specific chip do you have?
I think you need to concatenate some timers to do this.
 

Offline VanitarNordicTopic starter

  • Frequent Contributor
  • **
  • Posts: 277
  • Country: 00
Re: Triggering the STM32 ADC in the DMA mode
« Reply #2 on: January 15, 2019, 08:07:38 pm »
Quote
What specific chip do you have?

STM32F303C8
 

Online MasterT

  • Frequent Contributor
  • **
  • Posts: 783
  • Country: ca
Re: Triggering the STM32 ADC in the DMA mode
« Reply #3 on: January 16, 2019, 01:45:59 am »
Download F3 package, study examples.
https://www.st.com/en/embedded-software/stm32cubef3.html
Quote
- coming with examples running on ST boards: STM32 Nucleo, Discovery kits and Evaluation boards
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4068
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: Triggering the STM32 ADC in the DMA mode
« Reply #4 on: January 16, 2019, 07:02:36 am »
Two options:
1. Hardware timebase, software counter
Use timer A, and count the update events at which a ADC sequence is started in the ISR. Then stop the timer after 100.
Timer A will be set with TRGO update, and must have a connection to ADCx in the Peripheral Interconnect Matrix. (see TIMx_CR2:MMS)

2. Hardware timebase, hardware counter, software restart
Use two timers. Set timer A with TRGO update and TRGI in Gated Mode. Timer B will run One-shot countdown, 100 * TIMA_ARR, with TRGO as Enable. The DMA complete ISR must reset Timer B.
Timer A needs connection to ADCn.
Timer B needs connection to Timer A.

See if you can puzzle this out. Using CubeMX might make this more difficult than it has to be. Or they've updated timer interconnects since I last used it. Let me know if this works out! (or if I made a mistake)
 
The following users thanked this post: VanitarNordic

Offline VanitarNordicTopic starter

  • Frequent Contributor
  • **
  • Posts: 277
  • Country: 00
Re: Triggering the STM32 ADC in the DMA mode
« Reply #5 on: January 16, 2019, 08:51:57 am »
My problem is that my perception of "triggering" is that an action will be triggered by something, like turning on a lamp by a switch
but in this ADC triggering case when a trigger happens, the ADC just make one conversion, although the DMA is circular. it seems that we have to make the ADC clock as the trigger, in the size of the required samples. I tried to use the repetition counter, but it is limited to 256
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4068
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: Triggering the STM32 ADC in the DMA mode
« Reply #6 on: January 16, 2019, 09:53:05 am »
You need at least one timer (timer A) to repeatedly trigger the ADC Start of Conversion Sequence 100 times. The DMA is automatically started by the ADC end-of-conversion.

Then you could use a timer B to enable timer A, hence my idea with (B) TRGO Enable and (A) TRGI Gated.
You could also set Timer B TRGI to Trigger Mode if you want the conversions to start by hardware as well.

The repetition counter inhibits the timer Update event, to delay updating of the registers. (for PWM updating)
I don't think you can use it in this case.
Quote
This means that data are transferred from the preload registers to the shadow registers every N+1 counter overflows or underflows, where N is the value in the TIMx_RCR repetition counter register.

(below image)
« Last Edit: January 16, 2019, 09:54:47 am by Jeroen3 »
 
The following users thanked this post: VanitarNordic

Offline VanitarNordicTopic starter

  • Frequent Contributor
  • **
  • Posts: 277
  • Country: 00
Re: Triggering the STM32 ADC in the DMA mode
« Reply #7 on: January 16, 2019, 10:44:56 am »
Thank you very much.

Let me be more precise. Actually, I have used a timer to generate PWM. What I want is to start ADC when a PWM pulse ends, falling edge. The TRGO options are clear in the photo.
Can I use it to trigger the ADC individually (to trigger the DMA conversion of 100 samples, not just one)?
 

Offline capt bullshot

  • Super Contributor
  • ***
  • Posts: 3033
  • Country: de
    • Mostly useless stuff, but nice to have: wunderkis.de
Re: Triggering the STM32 ADC in the DMA mode
« Reply #8 on: January 16, 2019, 11:20:34 am »
The ADC itself can be triggered to perform one single channel conversion or a sequence (multiple channels, multiplexer is set automatically) of conversions. The ADC cannot be triggered to do a number of conversions of one channel. You'd have to provide a trigger signal for each conversion.
Safety devices hinder evolution
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4068
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: Triggering the STM32 ADC in the DMA mode
« Reply #9 on: January 16, 2019, 11:49:20 am »
Technically the ADC can perform 16 conversions of one channel by setting all sequence bits to the same channel.
The samplerate is locked to the conversion time and ADC clock.

Please go read the RM0316 Reference Manual (DM00043574).
« Last Edit: January 16, 2019, 11:53:21 am by Jeroen3 »
 

Offline ogden

  • Super Contributor
  • ***
  • Posts: 3731
  • Country: lv
Re: Triggering the STM32 ADC in the DMA mode
« Reply #10 on: January 16, 2019, 11:58:50 am »
Technically the ADC can perform 16 conversions of one channel by setting all sequence bits to the same channel.

Don't. DMA have DMA_CNDTRx which can count up-to 65535 transfers. Just read DMA chapter of stm32 Reference Manual.
 

Offline VanitarNordicTopic starter

  • Frequent Contributor
  • **
  • Posts: 277
  • Country: 00
Re: Triggering the STM32 ADC in the DMA mode
« Reply #11 on: January 16, 2019, 12:11:01 pm »
so if I understand you correctly, the ADC DMA will only do one conversion, except that we trigger it in the size of required samples. for example 100 square pulse for 100 samples.
The speed of the trigger should be equal to the sampling speed. therefore if the maximum ADC clock is 14MHz to make the fastest conversions, then we have to provide pulses with this frequency

that does not sound practical  :horse:

I was thinking that I can start the whole conversion by a trigger and the ADC or DMA clock comes from the internal source.
« Last Edit: January 16, 2019, 12:41:50 pm by VanitarNordic »
 

Offline ogden

  • Super Contributor
  • ***
  • Posts: 3731
  • Country: lv
Re: Triggering the STM32 ADC in the DMA mode
« Reply #12 on: January 16, 2019, 12:53:16 pm »
so if I understand you correctly, the ADC DMA will only do one conversion, except that we trigger it in the size of required samples.

Please use quoting so we do not need to guess which post you are answering to

Unless you want max ADC sampling speed, you shall start conversions using timer. Configure DMA for 100 transfers, start timer. After you get DMA done signal, you stop conversion start timer. Don't worry if ADC will do more conversions than needed - when done, DMA will ignore those.
 

Offline mbless

  • Regular Contributor
  • *
  • Posts: 227
  • Country: 00
Re: Triggering the STM32 ADC in the DMA mode
« Reply #13 on: January 16, 2019, 01:47:38 pm »
I currently have ADC DMA setup to sample the same channel 100 times. I set the ADC to continuous conversions and DMA to normal. I then call the HAL ADC DMA function with a buffer of 100, so it will fill the buffer and stop. You have to call the ADC DMA stop function, e.g. in the DMA conversion complete callback, before you call the start function again.

I haven't tried triggering off a timer, though, so no idea if it'll work that way.
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4068
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: Triggering the STM32 ADC in the DMA mode
« Reply #14 on: January 16, 2019, 01:49:28 pm »
Using the dma counter could work. But you’re less free in samplerate.
You’ll probably still need a timer to initiate the dma, since the adc is in continous mode.
« Last Edit: January 16, 2019, 01:51:07 pm by Jeroen3 »
 

Offline VanitarNordicTopic starter

  • Frequent Contributor
  • **
  • Posts: 277
  • Country: 00
Re: Triggering the STM32 ADC in the DMA mode
« Reply #15 on: January 16, 2019, 01:54:11 pm »
Using the dma counter could work. But you’re less free in samplerate.
You’ll probably still need a timer to initiate the dma, since the adc is in continous mode.

I have tested and it doesn't work. even I had configured the DMA in the circular mode which naturally should do all conversions till the end but it just makes one conversion by the trigger pulse.
 

Offline ogden

  • Super Contributor
  • ***
  • Posts: 3731
  • Country: lv
Re: Triggering the STM32 ADC in the DMA mode
« Reply #16 on: January 16, 2019, 02:12:46 pm »
You’ll probably still need a timer to initiate the dma, since the adc is in continous mode.

DMA transfer must be initiated by ADC. ADC shall not necessarily run in the continuous mode unless sample rate requires such. DMA shall not be in circular mode when DMA counter used.
 

Offline VanitarNordicTopic starter

  • Frequent Contributor
  • **
  • Posts: 277
  • Country: 00
Re: Triggering the STM32 ADC in the DMA mode
« Reply #17 on: January 16, 2019, 02:21:48 pm »
Quote
ADC shall not necessarily run in the continuous mode

That's true, ADC should not be continuous in this case, otherwise, it does not care about the trigger and something and just continuously does the conversion. but the DMA should be circular, but it only does one conversion, except that I make several square pulses as a trigger, in size of the required samples (for example 100), and as fast as the trigger, then as fast would be the sampling (till the threshold)
 

Offline ogden

  • Super Contributor
  • ***
  • Posts: 3731
  • Country: lv
Re: Triggering the STM32 ADC in the DMA mode
« Reply #18 on: January 16, 2019, 02:27:05 pm »
Quote
ADC shall not necessarily run in the continuous mode

That's true, ADC should not be continuous in this case, otherwise, it does not care about the trigger and something and just continuously does the conversion. but the DMA should be circular, but it only does one conversion, except that I make several square pulses as a trigger, in size of the required samples (for example 100), and as fast as the trigger, then as fast would be the sampling (till the threshold)

As always in such cases - compile source code examples and look why they work, how they differ from what you are doing.
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4068
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: Triggering the STM32 ADC in the DMA mode
« Reply #19 on: January 17, 2019, 07:57:19 am »
Let's make it clear. The 100 conversions should start on the last falling edge of a PWM burst. With minimum jitter.
The minimum jitter means the sequence of 100 conversions must be started by hardware.

To do this several approaches have been described here.
One of which is using the DMA to stop storing the samples.
Meaning the ADC could be in continuous mode, and some other hardware triggered the DMA Channel Enable bit.

The ADC has few operational modes.
- One sequence.
- Continuous.
Let's forget injected for a moment here.

The DMA is triggered by the ADC for each End-Of-Conversion, any conversion, including those within a sequence.
Performing sequence conversion on the ADC, performs the amount of DMA operations as channels in the sequence.
Performing continuous conversions on the ADC, stores DMA CNDTR conversions. Or infinite when circular is enabled.
Using CNDTR as the counter to 100 is possible, low jitter was requested something still has to do either of these:
- Enable the DMA channel by hardware while the ADC is in continous. Maximum latency is the sequence time.
- Enable the ADC continuous mode by hardware. Maximum latency is ADC start delay.

Recommended reading: AN3116 Application STM32™’s ADC modes and their applications

Now, I think it is possible to perform this completely in hardware, except resetting the hardware.
- Use a timer X to detect the PWM burst, and enable a timer Y after n edges. (assuming fixed edge count*)
- This timer Y triggers the ADC sequence conversions, not once but infinitely.
- DMA counts to 100, the DMA Complete IRQ is set.
- In the ISR, reset timer X, stop and reset timer Y and reset the DMA. And do something with your data, obviously.

*now, timeout triggering can probably also be done. By connecting timer X external trigger to reset mode, with countdown mode. The update event should TRGO to timer Y to enable it. I have never tried this.
« Last Edit: January 17, 2019, 08:00:54 am by Jeroen3 »
 

Offline Gibson486

  • Frequent Contributor
  • **
  • Posts: 324
  • Country: us
Re: Triggering the STM32 ADC in the DMA mode
« Reply #20 on: January 17, 2019, 06:50:04 pm »
I did this with the DAC. You can start and stop the clock trigger on demand. What I did was when I was ready for the DAC conversions, I would start the clock that triggers the DAC (or ADC in your case). At the end of my DMA transfer, an interrupt gets generated. I use that interrupt to stop the clock, which in turn tells the DAC to stop conversions. Another way is to set up the clock to only count to 100. At the end of the cycle, an interrupt can be generated. Just one way you can try. Hopefully it is helpful.
 

Offline VanitarNordicTopic starter

  • Frequent Contributor
  • **
  • Posts: 277
  • Country: 00
Re: Triggering the STM32 ADC in the DMA mode
« Reply #21 on: January 17, 2019, 07:32:59 pm »
I did this with the DAC. You can start and stop the clock trigger on demand. What I did was when I was ready for the DAC conversions, I would start the clock that triggers the DAC (or ADC in your case). At the end of my DMA transfer, an interrupt gets generated. I use that interrupt to stop the clock, which in turn tells the DAC to stop conversions. Another way is to set up the clock to only count to 100. At the end of the cycle, an interrupt can be generated. Just one way you can try. Hopefully it is helpful.

Yes, what do you use as the internal clock?
 

Offline Gibson486

  • Frequent Contributor
  • **
  • Posts: 324
  • Country: us
Re: Triggering the STM32 ADC in the DMA mode
« Reply #22 on: January 17, 2019, 08:09:16 pm »
I did this with the DAC. You can start and stop the clock trigger on demand. What I did was when I was ready for the DAC conversions, I would start the clock that triggers the DAC (or ADC in your case). At the end of my DMA transfer, an interrupt gets generated. I use that interrupt to stop the clock, which in turn tells the DAC to stop conversions. Another way is to set up the clock to only count to 100. At the end of the cycle, an interrupt can be generated. Just one way you can try. Hopefully it is helpful.

Yes, what do you use as the internal clock?

You set up a timer and set up the clock source as the internal clock.
 

Offline VanitarNordicTopic starter

  • Frequent Contributor
  • **
  • Posts: 277
  • Country: 00
Re: Triggering the STM32 ADC in the DMA mode
« Reply #23 on: January 18, 2019, 07:02:37 am »
Quote
You set up a timer and set up the clock source as the internal clock.

I mean pulses of the trigger. How do you make that?
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4068
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: Triggering the STM32 ADC in the DMA mode
« Reply #24 on: January 18, 2019, 11:48:27 am »
Do you have any background in embedded development? I feel like you're not understanding what we're trying to say.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf