Skip to main content
skon.1
Associate III
July 31, 2020
Question

Waiting indefinitely for an ADC conversion to complete

  • July 31, 2020
  • 14 replies
  • 5269 views

Hello,

In my application I want to get the ADC voltage - waiting indefinitely until the operation completes.  Will this code work ?

while ( HAL_ADC_PollForConversion ( & hadc1 , 1000 ) == HAL_TIMEOUT );

This topic has been closed for replies.

14 replies

prain
Visitor II
July 31, 2020

No need to external while loop, HAL_ADC_PollForConversion has a while loop inside. Just use

HAL_ADC_PollForConversion ( & hadc1 , 1000 );

MCU waits at this line until EOC flag sets.

skon.1
skon.1Author
Associate III
July 31, 2020

What does the "1000" value do inside the function ? Isn't it the timeout ?

I want it to wait indefinitely - not just 1000 cycles...

prain
Visitor II
August 1, 2020

The timeout value can be a uint32_t and the unit is milliseconds not cycles.

prain
Visitor II
July 31, 2020

After conversion completed, use HAL_ADC_GetValue to read ADC value.

TDK
July 31, 2020

To wait indefinitely, use HAL_MAX_DELAY as the delay. This is #defined as 0xFFFFFFFF.

The code you linked will also work. I feel like I wrote that in a post not too long ago which I can't find.

"If you feel a post has answered your question, please click ""Accept as Solution""."
skon.1
skon.1Author
Associate III
August 1, 2020

TDK,

I get what you're saying.

0xFFFFFFFF is a very long time - and can be considered "indefinite" for any practical application.

But IMO writing :

while ( HAL_ADC_PollForConversion ( & hadc1 , 1000 ) == HAL_TIMEOUT );

Is more "logically correct" - because it's a TRUE infinite loop.

TDK
August 1, 2020
It’s a special case handled by the HAL library. It is infinite. Check the source code.
But it’s academic. If you’re waiting more than a day for the conversion to complete, something in your code is wrong.
"If you feel a post has answered your question, please click ""Accept as Solution""."
S.Ma
Principal
August 1, 2020

Sounds like unflexible mind's question. Elaborate:

  • Are you using bare metal while loop or RTOS?
  • Why your application would expect the ADC to take forever to convert an input while conversion is predictable cycle count? Do you use a trigger input signal? Which one? How about using interrupt in this case so your core can do something else rather than pure waiting.

Context helps understand if there is a better way to implement what is truly needed.

TDK
August 1, 2020

Relevant code:

HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)
{
 
 ...
 
 /* Check End of conversion flag */
 while(!(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOC)))
 {
 /* Check if timeout is disabled (set to infinite wait) */
 if(Timeout != HAL_MAX_DELAY) // <----------------------------------------------------------------
 {
 if((Timeout == 0U) || ((HAL_GetTick() - tickstart ) > Timeout))
 {
 /* Update ADC state machine to timeout */
 SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
 
 /* Process unlocked */
 __HAL_UNLOCK(hadc);
 
 return HAL_TIMEOUT;
 }
 }
 }
 
 ...
 
}

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