cancel
Showing results for 
Search instead for 
Did you mean: 

Waiting indefinitely for an ADC conversion to complete

skon.1
Senior

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 );

14 REPLIES 14
skon.1
Senior

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.

S.Ma
Principal

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.

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".
TDK
Guru

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".

​ADC conversion time is in micro and milli second order. If conversion does not complete after 1 seconds, it will not complete ever. It means that there is a problem in conversion and you should restart the conversion with correct parameters. You can not see infinite wait for such thing in any professional code, because timeout concept is made for you to prevent infinite wait!