Triggering the STM32 ADC in the DMA mode - Page 1
Forum section: electronics / general

author: VanitarNordic
author_rank: Frequent Contributor
timestamp: January 15, 2019, 07:02:37 pm
content: I have configured the STM32 ADC in the DMA mode which fills a buffer, for example, a buffer with 100 elements (buffer)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.

author: Jeroen3
author_rank: Super Contributor
timestamp: January 15, 2019, 07:22:56 pm
content: What specific chip do you have?I think you need to concatenate some timers to do this.

author: VanitarNordic
author_rank: Frequent Contributor
timestamp: January 15, 2019, 08:07:38 pm
content: QuoteSTM32F303C8

author: MasterT
author_rank: Frequent Contributor
timestamp: January 16, 2019, 01:45:59 am
content: Download F3 package, study examples.https://www.st.com/en/embedded-software/stm32cubef3.htmlQuote

author: Jeroen3
author_rank: Super Contributor
timestamp: January 16, 2019, 07:02:36 am
content: Two options:1.Hardware timebase, software counterUse 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 restartUse 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)

author: VanitarNordic
author_rank: Frequent Contributor
timestamp: January 16, 2019, 08:51:57 am
content: My problem is that my perception of "triggering" is that an action will be triggered by something, like turning on a lamp by a switchbut 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

author: Jeroen3
author_rank: Super Contributor
timestamp: January 16, 2019, 09:53:05 am
content: 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(below image)

author: VanitarNordic
author_rank: Frequent Contributor
timestamp: January 16, 2019, 10:44:56 am
content: 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)?

author: capt bullshot
author_rank: Super Contributor
timestamp: January 16, 2019, 11:20:34 am
content: 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.

author: Jeroen3
author_rank: Super Contributor
timestamp: January 16, 2019, 11:49:20 am
content: 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 theRM0316 Reference Manual (DM00043574).

author: ogden
author_rank: Super Contributor
timestamp: January 16, 2019, 11:58:50 am
content: Quote from: Jeroen3 on January 16, 2019, 11:49:20 amDon't. DMA have DMA_CNDTRx which can count up-to 65535 transfers. Just read DMA chapter of stm32 Reference Manual.

author: VanitarNordic
author_rank: Frequent Contributor
timestamp: January 16, 2019, 12:11:01 pm
content: 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 frequencythat does not sound practicalI was thinking that I can start the whole conversion by a trigger and the ADC or DMA clock comes from the internal source.

author: ogden
author_rank: Super Contributor
timestamp: January 16, 2019, 12:53:16 pm
content: Quote from: VanitarNordic on January 16, 2019, 12:11:01 pmPlease use quoting so we do not need to guess which post you are answering toUnless 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.

author: mbless
author_rank: Regular Contributor
timestamp: January 16, 2019, 01:47:38 pm
content: 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.

author: Jeroen3
author_rank: Super Contributor
timestamp: January 16, 2019, 01:49:28 pm
content: 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.

author: VanitarNordic
author_rank: Frequent Contributor
timestamp: January 16, 2019, 01:54:11 pm
content: Quote from: Jeroen3 on January 16, 2019, 01:49:28 pmI 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.

author: ogden
author_rank: Super Contributor
timestamp: January 16, 2019, 02:12:46 pm
content: Quote from: Jeroen3 on January 16, 2019, 01:49:28 pmDMA 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.

author: VanitarNordic
author_rank: Frequent Contributor
timestamp: January 16, 2019, 02:21:48 pm
content: QuoteThat'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)

author: ogden
author_rank: Super Contributor
timestamp: January 16, 2019, 02:27:05 pm
content: Quote from: VanitarNordic on January 16, 2019, 02:21:48 pmAs always in such cases - compile source code examples and look why they work, how they differ from what you are doing.

author: Jeroen3
author_rank: Super Contributor
timestamp: January 17, 2019, 07:57:19 am
content: 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 stopstoringthe 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 triggeredby the ADCfor 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,storesDMA 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 applicationsNow, 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.

author: Gibson486
author_rank: Frequent Contributor
timestamp: January 17, 2019, 06:50:04 pm
content: 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.

author: VanitarNordic
author_rank: Frequent Contributor
timestamp: January 17, 2019, 07:32:59 pm
content: Quote from: Gibson486 on January 17, 2019, 06:50:04 pmYes, what do you use as the internal clock?

author: Gibson486
author_rank: Frequent Contributor
timestamp: January 17, 2019, 08:09:16 pm
content: Quote from: VanitarNordic on January 17, 2019, 07:32:59 pmYou set up a timer and set up the clock source as the internal clock.

author: VanitarNordic
author_rank: Frequent Contributor
timestamp: January 18, 2019, 07:02:37 am
content: QuoteI mean pulses of the trigger. How do you make that?

author: Jeroen3
author_rank: Super Contributor
timestamp: January 18, 2019, 11:48:27 am
content: Do you have any background in embedded development? I feel like you're not understanding what we're trying to say.

author: VanitarNordic
author_rank: Frequent Contributor
timestamp: January 18, 2019, 01:55:48 pm
content: Quote from: Jeroen3 on January 18, 2019, 11:48:27 amWhy do you talk like this? I asked him because he might introduce another method

author: Jeroen3
author_rank: Super Contributor
timestamp: January 18, 2019, 03:11:22 pm
content: This was not clear to me. The way it was written sounded like you were not sure how timers connect to internal clock or create triggers.The risks of online forums in a foreign language is that some implicit meanings do not transfer.