cancel
Showing results for 
Search instead for 
Did you mean: 

HAL ADC multi channel in polling mode

Arthur21
Associate

Hi everyone,

 

I have a STM32F042F6P6 chip. I want to read 9 analog channels on it.

I guess there are at least 2 options: configure a scan mode with DMA, and staying in polling mode.

I decided to stay in polling mode to avoid a potential problem due to DMA config.
I defined 1 adc handle  and did channel configurations (8 first channels are gpios, the 9th is internal temp)

It seems that when calling those 2 methods repeatedly, the ADC auto-increments and read each channel starting from the lowest ID.

HAL_ADC_PollForConversion
HAL_ADC_GetValue
 

Let's say now that I want to have a method that reads only the internal temperature, how do I manage to tell to HAL_ADC_PollConversion to point on the internal temperature channel only ?

I have had an idea but it is quite radical:
- create other channel config structure, with only the Temp channel defined, and each time I call a conversion method, before that I call HAL_ADC_ConfigChannel with the good channel config structure.


Also, I have 2 questions:
- What is the rank of an ADC channel, and what is it used for ? I see sometimes people defining several channel but with the same rank, I don't understand why.
- Does someone have a breve explanation of multichannel conversion in DMA mode ? I do not find clear information for newbie in embedded systems like me.

Thank you very much,
Arthur

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

HAL_ADC_PollConversion always returns the last conversion. You need to manage internally what channel that is. Have an internal variable that increments by 1 for each conversion. Use that to know what channel is being returned.

Doing multi-channel without DMA isn't recommended as you can easily run into overruns.

On the F0, rank and channel number are the same thing. Rank 0 is channel 0, etc. On other families rank determines which order channels are converted.

See "13.5.5 Managing converted data using the DMA" in the reference manual for an explanation of DMA. Also should be examples you can follow in the CubeMX repository.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

3 REPLIES 3
TDK
Guru

HAL_ADC_PollConversion always returns the last conversion. You need to manage internally what channel that is. Have an internal variable that increments by 1 for each conversion. Use that to know what channel is being returned.

Doing multi-channel without DMA isn't recommended as you can easily run into overruns.

On the F0, rank and channel number are the same thing. Rank 0 is channel 0, etc. On other families rank determines which order channels are converted.

See "13.5.5 Managing converted data using the DMA" in the reference manual for an explanation of DMA. Also should be examples you can follow in the CubeMX repository.

If you feel a post has answered your question, please click "Accept as Solution".

@TDK wrote:  Doing multi-channel without DMA isn't recommended as you can easily run into overruns.

What do you mean by overruns in this case?  Do you mean that the calling code will not call HAL_ADC_PollForConversion() and HAL_ADC_GetValue() fast enough, and the sample will get overwritten or corrupted?

Effectively, yes. Particularly when the conversions are continuously generated and it's just a question of calling HAL_ADC_GetValue fast enough.

The various STM32 families have different ADC peripherals. Most of them do not "wait" for you to read the value before converting the next channel.

If you feel a post has answered your question, please click "Accept as Solution".