cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 ADC DMA Sample Rate Time

gokhannsahin
Associate II

Hi everyone, 

I have some questions about using ADC. I have an analog channel and want to read it for a specified time according to my PWM signal because it has been used as a voltage sense on an inductor. However, I need more samples and have to check the changes. To do this, set two-timers, one has been used to get compare of PWM value, other trig the ADC for 100samples. Here I am confused right here. I'm not sure how to calculate the timing, also I'm afraid of getting overrun and wrong data. So, I need your help and have some questions about this application. By the way, I'm using STM32G431 which is new MCU.

  1. The datasheet says that the maximum of the clock frequency of the ADC module is 40MHz. The clock frequency of MCU is 160MHz. Then, should I set the clock prescaler of ADC as "Sync divided 4" or does it never exceed 40MHz anyway? (for example, what about in async 1?)
  2. If a new trig occurs by the timer used for trig before the adc read cycle is complete, will raw value be wrong?
  3. How should I set the trig time according to adc reading time?
16 REPLIES 16
berendi
Principal
  1. It is not guaranteed to work above 40 MHz. Read: it might work every time on your desk, and sometimes fail in the field.
  2. You'll just get the previous conversion result again. The reference manual says, "Any hardware triggers which occur while a conversion is ongoing are ignored."
  3. Try continuous conversion mode, so after each conversion a new one is started right away by the ADC itself.
Ozone
Lead

> If a new trig occurs by the timer used for trig before the adc read cycle is complete, will raw value be wrong?

I suppose the the trigger is ignored, or previous conversion is abandoned. Never tried that.

What is the sampling rate you would need ?

Several STM32F variants support a dual/triple ADC mode, with intersected triggers to achieve a higher sample rate.

Not sure about such features for the G series.

gokhannsahin
Associate II

Actually,there is no specified sample rate.

I want to get the maximum possible sample so I can get more accurate results.

I enclosed the picture of pwm and the signal to be measured, you can see.

0690X00000DBeiWQAT.jpg

> Actually,there is no specified sample rate.

> I want to get the maximum possible sample so I can get more accurate results.

On second look, this is contradictory.

You need a sampling rate/trigger related to your signal, and PWM cycle.

If your sampling point is unsynchronized (flaoting), you don't know what (when) you measure.

RomainR.
ST Employee

Hi gokhannsahin,

From STM32G474 datasheet (DS12589.pdf Rev2), the maximum ADC Clock frequency 60MHz (for single ADC operation)

Let's suppose you choose to set ADC prescaler to have ADC Clock = 40Mz, with a sample time 2.5 ADC Clock with 12-bit resolution.

You get ADC TConversion = (2.5+12.5) x 0.025µs = 0.375µs.

in this setup you are able to convert up to 950 samples (match and fit with your signal half period of 357µs)

In your case, you could use ADC in continuous mode (with Tconversion = 0.375µs) combined with DMA to convert the full period of your waveform and obtain an array of 1904 sample.

To synchronize ADC conversion with the full period, you can trigger your ADC one time at the rising edge of pwm and let ADC get the full period in continuous.

If you need less sample / period, increase ADC sample time according your need.

Best regards.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

berendi
Principal

I would just let ADC running continuously, set an analog watchdog on the signal with interrupt, and start DMA from the interrupt handler whenever it is above the range.

Should I start/stop DMA to measure only the areas shown in the picture?

If the ADC has nothing else to do, then why not?

You should start ADC with DMA one time.

__IO uint32_t pData[950] = {0,};

HAL_ADC_Start_DMA(&hadc1, (uint32_t*) pData, sizeof(pData);

The ADC is in continuous mode and triggered by the timer in charge to produce PWM wave each rising edge.

In this case you only have to start or stop the PWM TIMER to synchronize ADC+DMA conversion with your waveform.

HAL_TIM_PWM_Start();

The DMA get the request to ADC in normal mode for the amount of sample pData, which fit into your T half period you want to convert.

Then pData array will contains 950 samples. And repeat again on next PWM rising edge for a new conversion of T.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.