cancel
Showing results for 
Search instead for 
Did you mean: 

Best way to configure ADC reading for speed

evanpol
Associate II

Hello,

 

I am trying to configure my STM32H723ZG to be able to read the value of a few current sensors. My main concern at the moment is the speed at which I need to read these. I'm hoping to be reading upwards of rate of 20kHz. 

My questions are about which method might be best, the standard way of reading the ADCs seems insufficient, I've seen online some methods are DMA and injected conversions. If anyone could provide some insight into what might be best and how to go about implementing them that would be great.

Additionally, I wanted to ask how to determine an appropriate sampling time of the ADC.

 

Thank you in advance for your help!

4 REPLIES 4
Aime
ST Employee

Hi @evanpol,

 

Here's a detailed explanation and solution to help you achieve fast and efficient ADC readings on your STM32H723ZG, targeting a sampling rate of around 20 kHz for multiple current sensors.

 

Challenges

  • Reading multiple ADC channels at 20 kHz means a total sampling rate of at least 20 kHz per channel.
  • Standard polling-based ADC reading is CPU-intensive and often too slow for such rates.
  • You need a method that offloads data transfer and minimizes CPU overhead.

a) DMA (Direct Memory Access) with Regular Conversions

  • How it works: ADC conversions are triggered (either by software or timer), and results are automatically transferred to memory via DMA.
  • Advantages:
    • CPU is free during conversion and data transfer.
    • Continuous and efficient data acquisition.
  • Implementation tips:
    • Configure ADC in scan mode to sample multiple channels sequentially.
    • Use a timer trigger (e.g., TIMx) to start ADC conversions at precise intervals (e.g., 50 µs for 20 kHz).
    • Configure DMA in circular mode to continuously fill a buffer.
    • Process data in the background or via DMA transfer complete interrupts.

b) Injected Conversions

  • Injected conversions are typically used for high-priority or asynchronous conversions.
  • They can be triggered independently from regular conversions.
  • Use case: When you need to sample a subset of channels at irregular intervals or higher priority.
  • For your use case (regular high-speed sampling), DMA with regular conversions is usually more efficient.

c) ADC with Timer Trigger + DMA (Best Practice)

  • Use a hardware timer to trigger ADC conversions at exactly 20 kHz.
  • ADC performs scan conversions of your sensor channels.
  • DMA transfers results to RAM without CPU intervention.
  • You get a continuous stream of samples with minimal CPU load.


How to Implement DMA + Timer Trigger ADC Sampling on STM32H7

Configure ADC:

  • Enable scan mode.
  • Select channels corresponding to your sensors.
  • Set sampling time (see next section).
  • Enable DMA request.
  • Set ADC to be triggered by timer event (e.g., TIM1 TRGO).

Configure Timer:

  • Set timer frequency to 20 kHz (period = 50 µs).
  • Configure timer to generate TRGO event on update.

Configure DMA:

  • Set DMA channel linked to ADC.
  • Configure circular mode to continuously fill buffer.
  • Set buffer size = number of channels × number of samples per batch.
  • Enable DMA interrupts if needed (e.g., half-transfer, transfer complete).

Start timer and ADC:

  • Start timer.
  • Start ADC in DMA mode.

Data Processing:

  • Process ADC data in DMA interrupt or main loop.
  • Use double buffering if needed for continuous processing.

 

Determining Appropriate ADC Sampling Time

The ADC sampling time is the time the ADC input is connected to the sampling capacitor before conversion starts. It affects:

  • Accuracy: Longer sampling time allows the capacitor to charge fully, especially for high source impedance sensors.
  • Speed: Shorter sampling time increases conversion rate but may reduce accuracy if the input impedance is high.

How to choose sampling time:

Check sensor output impedance:

  • If low (e.g., < 1 kΩ), short sampling times (e.g., 2.5 to 7.5 ADC clock cycles) are sufficient.
  • If high, increase sampling time accordingly.

Calculate total conversion time:

Tconv = Tsampling+12.5×TADCclock​​

  • For STM32H7, ADC conversion requires 12.5 ADC clock cycles plus sampling time.
  • ADC clock frequency depends on ADC prescaler and clock source.

Since you want 20 kHz per channel, even with multiple channels, this is achievable with ample margin.

 

Good luck,
Aime

Aime
ST Employee

Hi @evanpol,

 

Here's a detailed explanation and solution to help you achieve fast and efficient ADC readings on your STM32H723ZG, targeting a sampling rate of around 20 kHz for multiple current sensors.

 

Challenges

  • Reading multiple ADC channels at 20 kHz means a total sampling rate of at least 20 kHz per channel.
  • Standard polling-based ADC reading is CPU-intensive and often too slow for such rates.
  • You need a method that offloads data transfer and minimizes CPU overhead.

a) DMA (Direct Memory Access) with Regular Conversions

  • How it works: ADC conversions are triggered (either by software or timer), and results are automatically transferred to memory via DMA.
  • Advantages:
    • CPU is free during conversion and data transfer.
    • Continuous and efficient data acquisition.
  • Implementation tips:
    • Configure ADC in scan mode to sample multiple channels sequentially.
    • Use a timer trigger (e.g., TIMx) to start ADC conversions at precise intervals (e.g., 50 µs for 20 kHz).
    • Configure DMA in circular mode to continuously fill a buffer.
    • Process data in the background or via DMA transfer complete interrupts.

b) Injected Conversions

  • Injected conversions are typically used for high-priority or asynchronous conversions.
  • They can be triggered independently from regular conversions.
  • Use case: When you need to sample a subset of channels at irregular intervals or higher priority.
  • For your use case (regular high-speed sampling), DMA with regular conversions is usually more efficient.

c) ADC with Timer Trigger + DMA (Best Practice)

  • Use a hardware timer to trigger ADC conversions at exactly 20 kHz.
  • ADC performs scan conversions of your sensor channels.
  • DMA transfers results to RAM without CPU intervention.
  • You get a continuous stream of samples with minimal CPU load.


How to Implement DMA + Timer Trigger ADC Sampling on STM32H7

Configure ADC:

  • Enable scan mode.
  • Select channels corresponding to your sensors.
  • Set sampling time (see next section).
  • Enable DMA request.
  • Set ADC to be triggered by timer event (e.g., TIM1 TRGO).

Configure Timer:

  • Set timer frequency to 20 kHz (period = 50 µs).
  • Configure timer to generate TRGO event on update.

Configure DMA:

  • Set DMA channel linked to ADC.
  • Configure circular mode to continuously fill buffer.
  • Set buffer size = number of channels × number of samples per batch.
  • Enable DMA interrupts if needed (e.g., half-transfer, transfer complete).

Start timer and ADC:

  • Start timer.
  • Start ADC in DMA mode.

Data Processing:

  • Process ADC data in DMA interrupt or main loop.
  • Use double buffering if needed for continuous processing.

 

Determining Appropriate ADC Sampling Time

The ADC sampling time is the time the ADC input is connected to the sampling capacitor before conversion starts. It affects:

  • Accuracy: Longer sampling time allows the capacitor to charge fully, especially for high source impedance sensors.
  • Speed: Shorter sampling time increases conversion rate but may reduce accuracy if the input impedance is high.

How to choose sampling time:

Check sensor output impedance:

  • If low (e.g., < 1 kΩ), short sampling times (e.g., 2.5 to 7.5 ADC clock cycles) are sufficient.
  • If high, increase sampling time accordingly.

Calculate total conversion time:

Tconv = Tsampling+12.5×TADCclock​​

  • For STM32H7, ADC conversion requires 12.5 ADC clock cycles plus sampling time.
  • ADC clock frequency depends on ADC prescaler and clock source.

Since you want 20 kHz per channel, even with multiple channels, this is achievable with ample margin.

 

Good luck,
Aime

Aime
ST Employee

Hi @evanpol,

 

Here's a detailed explanation and solution to help you achieve fast and efficient ADC readings on your STM32H723ZG, targeting a sampling rate of around 20 kHz for multiple current sensors.

 

Challenges

  • Reading multiple ADC channels at 20 kHz means a total sampling rate of at least 20 kHz per channel.
  • Standard polling-based ADC reading is CPU-intensive and often too slow for such rates.
  • You need a method that offloads data transfer and minimizes CPU overhead.

a) DMA (Direct Memory Access) with Regular Conversions

  • How it works: ADC conversions are triggered (either by software or timer), and results are automatically transferred to memory via DMA.
  • Advantages:
    • CPU is free during conversion and data transfer.
    • Continuous and efficient data acquisition.
  • Implementation tips:
    • Configure ADC in scan mode to sample multiple channels sequentially.
    • Use a timer trigger (e.g., TIMx) to start ADC conversions at precise intervals (e.g., 50 µs for 20 kHz).
    • Configure DMA in circular mode to continuously fill a buffer.
    • Process data in the background or via DMA transfer complete interrupts.

b) Injected Conversions

  • Injected conversions are typically used for high-priority or asynchronous conversions.
  • They can be triggered independently from regular conversions.
  • Use case: When you need to sample a subset of channels at irregular intervals or higher priority.
  • For your use case (regular high-speed sampling), DMA with regular conversions is usually more efficient.

c) ADC with Timer Trigger + DMA (Best Practice)

  • Use a hardware timer to trigger ADC conversions at exactly 20 kHz.
  • ADC performs scan conversions of your sensor channels.
  • DMA transfers results to RAM without CPU intervention.
  • You get a continuous stream of samples with minimal CPU load.


How to Implement DMA + Timer Trigger ADC Sampling on STM32H7

Configure ADC:

  • Enable scan mode.
  • Select channels corresponding to your sensors.
  • Set sampling time (see next section).
  • Enable DMA request.
  • Set ADC to be triggered by timer event (e.g., TIM1 TRGO).

Configure Timer:

  • Set timer frequency to 20 kHz (period = 50 µs).
  • Configure timer to generate TRGO event on update.

Configure DMA:

  • Set DMA channel linked to ADC.
  • Configure circular mode to continuously fill buffer.
  • Set buffer size = number of channels × number of samples per batch.
  • Enable DMA interrupts if needed (e.g., half-transfer, transfer complete).

Start timer and ADC:

  • Start timer.
  • Start ADC in DMA mode.

Data Processing:

  • Process ADC data in DMA interrupt or main loop.
  • Use double buffering if needed for continuous processing.

 

Determining Appropriate ADC Sampling Time

The ADC sampling time is the time the ADC input is connected to the sampling capacitor before conversion starts. It affects:

  • Accuracy: Longer sampling time allows the capacitor to charge fully, especially for high source impedance sensors.
  • Speed: Shorter sampling time increases conversion rate but may reduce accuracy if the input impedance is high.

How to choose sampling time:

Check sensor output impedance:

  • If low (e.g., < 1 kΩ), short sampling times (e.g., 2.5 to 7.5 ADC clock cycles) are sufficient.
  • If high, increase sampling time accordingly.

Calculate total conversion time:

Tconv = Tsampling+12.5×TADCclock​​

  • For STM32H7, ADC conversion requires 12.5 ADC clock cycles plus sampling time.
  • ADC clock frequency depends on ADC prescaler and clock source.

Since you want 20 kHz per channel, even with multiple channels, this is achievable with ample margin.

 

Good luck,
Aime

MOBEJ
ST Employee

Hello @evanpol  , 

You can check the example for using the ADC peripheral to perform a single conversion on the internal temperature sensor and calculate the temperature in degrees Celsius at this link:

https://github.com/STMicroelectronics/STM32CubeF7/blob/master/Projects/STM32F767ZI-Nucleo/Examples_LL/ADC/ADC_TemperatureSensor/Src/main.c

This example uses the polling method and shows basic ADC configuration and reading. You can follow its structure as a starting point for your own application.

You can also check our other examples demonstrating ADC with different programming models such as DMA and interrupt-based methods in this link below : 
STM32CubeWB/Projects/P-NUCLEO-WB55.Nucleo/Examples_LL/ADC at master · STMicroelectronics/STM32CubeWB · GitHub

Br

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.