ADC_Enable in stm32l4xx_hal_adc.c return with HAL_ERROR
Hi
When calling HAL_ADC_Start, ADC_Enable returns with error due to timeout.
The timeout set by the HAL is 2ms (ADC_ENABLE_TIMEOUT)
I'm using the STM32L496 with MSI @1Mhz.
I'm using the HAL with FreeRTOS, and there are several tasks running.
There is more than 1 task using the ADC but on different ports (ADC1/ADC2/ADC3) and different channels.
Once in a while during one of the measurements the timeout occurs.
Increasing the timeout solved the issue.
Any idea why this is happening.
Obviously a task context switch happens, prolonging the time this function is out of context thus making it reach the timeout.
These are the relevant lines of code:
/* Enable the ADC peripheral */
LL_ADC_Enable(hadc->Instance);
/* Wait for ADC effectively enabled */
tickstart = HAL_GetTick();
while (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_RDY) == 0UL)
{
/* If ADEN bit is set less than 4 ADC clock cycles after the ADCAL bit
has been cleared (after a calibration), ADEN bit is reset by the
calibration logic.
The workaround is to continue setting ADEN until ADRDY is becomes 1.
Additionally, ADC_ENABLE_TIMEOUT is defined to encompass this
4 ADC clock cycle duration */
/* Note: Test of ADC enabled required due to hardware constraint to */
/* not enable ADC if already enabled. */
if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
{
LL_ADC_Enable(hadc->Instance);
}
if ((HAL_GetTick() - tickstart) > ADC_ENABLE_TIMEOUT)
{
/* Update ADC state machine to error */
SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
/* Set ADC error code to ADC peripheral internal error */
SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
return HAL_ERROR;
}
}
}
