cancel
Showing results for 
Search instead for 
Did you mean: 

Fast switching of ADC configuration on STM32

Michael Bauer
Associate II

I am using a Nucleo-32 board to sample 2 ADC channels. I am using the Cube MX and Cube L4 libraries. I am sampling CH1 for a fixed number of samples using DMA. I am using the callback HAL_ADC_ConvCpltCallback to change the ADC configuration to sample CH2. Here is the code:

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)

//void ADC_DMAConvCplt(ADC_HandleTypeDef* hadc)

{

 static uint16_t i;

 static float * aInd;

 static float * aRes;

 static uint32_t bChg;

  

 //reconfigure to read ADC CH2

 //Begin with Ch 1 (Reference). Note: Callback will switch to Ch 2

 HAL_ADC_Stop_DMA(&hadc1);

 HAL_ADC_ConfigChannel(&hadc1, &SenConfig);

 HAL_ADC_Start_DMA(&hadc1,(uint32_t *)aADCxConvertedData, ADC_PERIOD_SAMPLE_CNT);

  

 ...

}

The desired configuration is preset in SenConfig so it is not computed here and thus increases the speed. I basically need to change the ADC channel number, all other parameters are the same. I don't know if there is a better way that I could configure DMA to reconfigure the ADC? If you have any idea then I would be interested.  

I need to compare CH1 and 2 data to a known sine wave. The number of samples is chosen so that the first sample of CH1 can be compared to the first sample of CH2. So I need to know if I miss a sample so that I can compensate for that in my comparison algorithm. The problem I am having is that I observe the very first sample of CH2 data is containing what looks like a continuation of sampling CH1. I'm guessing that using this callback may mean one or more ADC conversions + DMA transfers has occured before I can make this switch. After the first sample it looks like CH2 data. Also, from the data it looks like maybe there is a loss of one sample as well. Possibly stopping and starting the DMA?  

If anyone has any thoughts to help me understand why my data looks like this I appreciate it. Also, if you have a better way to perform the switching to avoid the problems altogther that would be great. Thanks so much.

Note: I am using the STM32L432 processor which has only one ADC. So Dual Regular Simultaneous mode for the ADC is not possible with one ADC. I am not using the Multichannel (scan) mode as I am not really sure how far offset the CH2 scan would be from CH1 and how to compare that to my reference sine wave. So I am using Single Channel Discontinuous converstion mode triggered by DMA and attempting to switch ADC channels.

2 REPLIES 2

>>I am not really sure how far offset the CH2 scan would be from CH1

Well it's a synchronous machine, there are direct and consistent relationships between the input clock, prescaler, sample time, and bit conversion width.

If you can't compute it from the data in reference manual, you can feed the same sine to both channel and observe the phase shift in the time domain.

Is the measurement here going to be a continuous? ie reference sine always present, always sampled?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

You could also toggle an GPIO in a DMA TC interrupt, and scope to determine the periodictiy

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..