2020-07-31 08:30 AM
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 );
2020-08-01 12:13 PM
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.
2020-08-01 12:21 PM
Sounds like unflexible mind's question. Elaborate:
Context helps understand if there is a better way to implement what is truly needed.
2020-08-01 12:24 PM
2020-08-01 12:30 PM
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;
}
}
}
...
}
2020-08-01 11:09 PM
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!