cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4xxx - Multi-channel ADC (regular or injected conversion)?

kenneth er
Associate II
Posted on February 10, 2018 at 11:39

Hi all,

Just wanted to check with the collective wisdom on this forum. I'm planning to do 3-channel ADC periodic sampling at 10kHz sampling rate. After reading up on the internet resources, two ways appeared to be the most reliable:

  1. Multi-channel scan regular conversion with DMA
    1. I wanted to do a simple polling routine but seemed like the normal polling method might not work that well for multi-channel as some other forum posts have suggested that some of the channels' value might get swapped at a certain interval. So this is a non-option for me. *Not too sure why it was swapped but the solution seemed to be going the DMA route since MCU operation might not be fast enough for the read.*
  2. Multi-channel injected conversion with polling
    1. Seeing that STM32F4 has 4 individual data registers to hold each channel converted reading. I have noticed that this might be one solution if injected conversion is available for your MCU.

I'm planning to go for injected conversion for its seeming ease of implementation using the HAL. However, I would like to check with you guys if this is a recommended solution? Thanks in advance~

#injected-conversion #stm32f4-adc
6 REPLIES 6
S.Ma
Principal
Posted on February 10, 2018 at 12:04

Injected channels are the 'high priority' channels to be converted. The ADC results are accessible on specific register and enable future min/max thresholds. They also have their own trigger pin vs normal channels.

It's easier to debug: Even with breakpoint and SW paused, you can see the registers change step by step.

AvaTar
Lead
Posted on February 10, 2018 at 15:31

I'm still not sure what the 'injected channels' are actually good for, use-case wise.

IMHO the suggested multichannel regular conversion (scan mode) is the way to go.

And 10kHz are not a very demanding sample rate.

I wanted to do a simple polling routine but seemed like the normal polling method might not work that well for multi-channel as some other forum posts have suggested that some of the channels' value might get swapped at a certain interval. So this is a non-option for me.

Because polling is strongly discouraged, even by ST. The ADC peripheral has one result register for all channel, so keeping track of the sequence manually is a second-best idea.

Better setup DMA for 3 channels, and enable the DMA TC interrupt (Transmission Complete).

This way, you get an interrupt when the DMA has copied one set of converted input values (3 in your case) to the supplied buffer address.

Posted on February 10, 2018 at 16:38

Yup, that's what I thought as well. I will definitely try it out. Thanks~

Posted on February 10, 2018 at 16:47

My understanding with the injected channel is for aperiodic sampling while having an on-going periodic sampling since injected has higher priority. I might be wrong but that how I have used injected channel with other MCU, e.g. PSoC. 

The only reason I'm planning to use the injected channels for STM32F4 is that only they have dedicated data registers while the regular channels share one as you have mentioned. Logistically, it will be easier to manage individual data registers. 

Regardless, my gut feeling is also to use regular scanning but with interrupt since my MCU should be relatively free in my intended application.

Posted on February 10, 2018 at 18:38

DMA can keep loading the 3 values into an array in the background, you can read/poll the array when you need the most current values. Have a TIM trigger the ADC at a convenient periodic rate.

Samples get skewed when the ADC isn't serviced promptly, or DMA is stopped/started. When taking a few dozen cycles to do the conversion the ADC needs a lot of attention.

If you want to poll and only have 3 channels, consider using ADC1, ADC2 and ADC3 to do a single channel each.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on February 11, 2018 at 05:21

That's an interesting proposal. However, my worry is on the power consumption for running 3 ADC simultaneously. Not sure about STM32 but on other MCU, ADC can be pretty power hungry for a low power device like the one I'm working on. Regardless, this would probably be perfect for simultaneous measurement without scanning delay between channel sweep.

However, you are right on the DMA. It will probably be the better way forward with enough flexibility for me to increase the sampling rate if needed.