cancel
Showing results for 
Search instead for 
Did you mean: 

How to read a single channel each time with ADC DMA

engenharia2
Associate II
Posted on March 08, 2016 at 20:06

Hi, I need to read 3 ADC channels with DMA on STM32F042 device (CH0, CH1, CH2), but I want to read 500 samples of the CH0, then reconfigure the ADC to read 500 samples of CH1 and finally reconfigure to read CH2. I already know how to do this in sequence, that is, read CH0, CH1, CH2, then CH0, CH1, CH2 again until complete 500 samples as can seen here: https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Java/Flat.aspx?RootFolder=https%3a%2f%2fmy%2est%2ecom%2fpublic%2fSTe2ecommunities%2fmcu%2fLists%2fSTM32Java%2fADC%20DMA%20HAL%20Libraries%20STM32F0&FolderCTID=0x01200200770978C69A1141439FE559EB459D758000F9A0E3A95BA69146A17C2E80209ADC21&currentviews=40

Unfortunately I also haven't more SRAM to create a 1500 positions buffer too. Can anyone help me?

Obs: I'm using STM32CubeMX v4.13.0 and Firmware Package for Family STM32F0 v1.5.0

#no-hablo-hal
2 REPLIES 2
Posted on March 08, 2016 at 21:29

You'd have to reconfigure the ADC after each burst. I'd probably think about using a TIM with a repetition counter, with a 250 count, firing it in 'one shot' mode twice, with the DMA in a circular mode.

Alternatively you could free run the DMA/ADC, and tear both down every 500 samples and reconfigure.

Doing either in HAL/Cube looks to be man-days of fun.

Another might be do always do 3 channels, and decimate/sieve out the data you want every 300 or 375 samples. This will require less blunt force trauma to the hardware.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
engenharia2
Associate II
Posted on March 10, 2016 at 18:38

Thanks for answering clive1. I did this way and it's working for me.

void readAdcDma(TYPEDEF_ADC_CH channel) {
ADC_ChannelConfTypeDef sConfig;
HAL_ADC_Stop_DMA( & ADC_Handle);
while (HAL_ADC_DeInit( & hadc) != HAL_OK);
HAL_ADC_Init( & hadc);
if (channel == ADC_CH0) {
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5;
HAL_ADC_ConfigChannel( & hadc, & sConfig);
} else if (channel == ADC_CH1) {
sConfig.Channel = ADC_CHANNEL_1;
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5;
HAL_ADC_ConfigChannel( & hadc, & sConfig);
} else if (channel == ADC_CH2) {
sConfig.Channel = ADC_CHANNEL_2;
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5;
HAL_ADC_ConfigChannel( & hadc, & sConfig);
}
HAL_ADC_Start_DMA( & ADC_Handle, pADC, ADC_BUFFER_SIZE);
HAL_ADC_Start( & ADC_Handle);
//Wait for DMA copy data...
HAL_Delay(20);
}