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

0 Members and 1 Guest are viewing this topic.

Offline VanitarNordic

  • Frequent Contributor
  • **
  • Posts: 260
  • Country: ir
Triggering the STM32 ADC in the DMA mode
« on: January 16, 2019, 06:02:37 am »
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: 3045
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: Triggering the STM32 ADC in the DMA mode
« Reply #1 on: January 16, 2019, 06:22:56 am »
What specific chip do you have?
I think you need to concatenate some timers to do this.
 

Offline VanitarNordic

  • Frequent Contributor
  • **
  • Posts: 260
  • Country: ir
Re: Triggering the STM32 ADC in the DMA mode
« Reply #2 on: January 16, 2019, 07:07:38 am »
Quote
What specific chip do you have?

STM32F303C8
 

Offline MasterT

  • Frequent Contributor
  • **
  • Posts: 315
  • Country: ca
Re: Triggering the STM32 ADC in the DMA mode
« Reply #3 on: January 16, 2019, 12:45:59 pm »
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: 3045
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: Triggering the STM32 ADC in the DMA mode
« Reply #4 on: January 16, 2019, 06:02:36 pm »
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 VanitarNordic

  • Frequent Contributor
  • **
  • Posts: 260
  • Country: ir
Re: Triggering the STM32 ADC in the DMA mode
« Reply #5 on: January 16, 2019, 07:51:57 pm »
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: 3045
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: Triggering the STM32 ADC in the DMA mode
« Reply #6 on: January 16, 2019, 08:53:05 pm »
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, 08:54:47 pm by Jeroen3 »
 
The following users thanked this post: VanitarNordic

Offline VanitarNordic

  • Frequent Contributor
  • **
  • Posts: 260
  • Country: ir
Re: Triggering the STM32 ADC in the DMA mode
« Reply #7 on: January 16, 2019, 09:44:56 pm »
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: 1365
  • 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, 10:20:34 pm »
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: 3045
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: Triggering the STM32 ADC in the DMA mode
« Reply #9 on: January 16, 2019, 10:49:20 pm »
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, 10:53:21 pm by Jeroen3 »
 

Offline ogden

  • Super Contributor
  • ***
  • Posts: 1685
  • Country: lv
Re: Triggering the STM32 ADC in the DMA mode
« Reply #10 on: January 16, 2019, 10:58:50 pm »
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 VanitarNordic

  • Frequent Contributor
  • **
  • Posts: 260
  • Country: ir
Re: Triggering the STM32 ADC in the DMA mode
« Reply #11 on: January 16, 2019, 11: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, 11:41:50 pm by VanitarNordic »
 

Offline ogden

  • Super Contributor
  • ***
  • Posts: 1685
  • Country: lv
Re: Triggering the STM32 ADC in the DMA mode
« Reply #12 on: January 16, 2019, 11: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: 161
  • Country: 00
Re: Triggering the STM32 ADC in the DMA mode
« Reply #13 on: January 17, 2019, 12:47:38 am »
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: 3045
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: Triggering the STM32 ADC in the DMA mode
« Reply #14 on: January 17, 2019, 12:49:28 am »
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 17, 2019, 12:51:07 am by Jeroen3 »
 

Offline VanitarNordic

  • Frequent Contributor
  • **
  • Posts: 260
  • Country: ir
Re: Triggering the STM32 ADC in the DMA mode
« Reply #15 on: January 17, 2019, 12:54:11 am »
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: 1685
  • Country: lv
Re: Triggering the STM32 ADC in the DMA mode
« Reply #16 on: January 17, 2019, 01:12:46 am »
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 VanitarNordic

  • Frequent Contributor
  • **
  • Posts: 260
  • Country: ir
Re: Triggering the STM32 ADC in the DMA mode
« Reply #17 on: January 17, 2019, 01:21:48 am »
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: 1685
  • Country: lv
Re: Triggering the STM32 ADC in the DMA mode
« Reply #18 on: January 17, 2019, 01:27:05 am »
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: 3045
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: Triggering the STM32 ADC in the DMA mode
« Reply #19 on: January 17, 2019, 06:57:19 pm »
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, 07:00:54 pm by Jeroen3 »
 

Offline Gibson486

  • Regular Contributor
  • *
  • Posts: 225
  • Country: us
Re: Triggering the STM32 ADC in the DMA mode
« Reply #20 on: January 18, 2019, 05:50:04 am »
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 VanitarNordic

  • Frequent Contributor
  • **
  • Posts: 260
  • Country: ir
Re: Triggering the STM32 ADC in the DMA mode
« Reply #21 on: January 18, 2019, 06:32:59 am »
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

  • Regular Contributor
  • *
  • Posts: 225
  • Country: us
Re: Triggering the STM32 ADC in the DMA mode
« Reply #22 on: January 18, 2019, 07:09:16 am »
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 VanitarNordic

  • Frequent Contributor
  • **
  • Posts: 260
  • Country: ir
Re: Triggering the STM32 ADC in the DMA mode
« Reply #23 on: January 18, 2019, 06:02:37 pm »
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: 3045
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: Triggering the STM32 ADC in the DMA mode
« Reply #24 on: January 18, 2019, 10:48:27 pm »
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