2021-09-17 05:31 AM
I'm using CubeIDE 1.7.0 and a Nucleo board to initialize and start ADC1 (at default settings, more or less).
The issue is that HAL_ADC_Start() would return HAL_ERROR due to a timeout. This is probably Clock-related but I couldn't figure it out yet.
Here's some snippets (generated code, mostly) I thought might be relevant:
HAL_StatusTypeDef ret = 0;
uint32_t value = 0;
while(1) {
ret = HAL_ADC_Start(&hadc4);
ret = HAL_ADC_PollForConversion(&hadc4, 1000);
value = HAL_ADC_GetValue(&hadc4);
HAL_ADC_Stop(&hadc4);
}
static void MX_ADC1_Init(void)
{
/* USER CODE BEGIN ADC1_Init 0 */
/* USER CODE END ADC1_Init 0 */
/* USER CODE BEGIN ADC1_Init 1 */
/* USER CODE END ADC1_Init 1 */
/** Common config
*/
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
hadc1.Init.Resolution = ADC_RESOLUTION_14B;
hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc1.Init.LowPowerAutoWait = DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;
hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc1.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE;
hadc1.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR;
hadc1.Init.OversamplingMode = DISABLE;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC1_Init 2 */
/* USER CODE END ADC1_Init 2 */
}
void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
if(hadc->Instance==ADC1)
{
/* USER CODE BEGIN ADC1_MspInit 0 */
/* USER CODE END ADC1_MspInit 0 */
/** Initializes the peripherals clock
*/
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADCDAC;
PeriphClkInit.AdcDacClockSelection = RCC_ADCDACCLKSOURCE_HSI;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
/* Peripheral clock enable */
__HAL_RCC_ADC1_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
/**ADC1 GPIO Configuration
PC2 ------> ADC1_IN3
*/
GPIO_InitStruct.Pin = VBUS_SENSE_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(VBUS_SENSE_GPIO_Port, &GPIO_InitStruct);
/* USER CODE BEGIN ADC1_MspInit 1 */
/* USER CODE END ADC1_MspInit 1 */
}
void HAL_MspInit(void)
{
/* USER CODE BEGIN MspInit 0 */
/* USER CODE END MspInit 0 */
__HAL_RCC_PWR_CLK_ENABLE();
HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_3);
/* System interrupt init*/
/* USER CODE BEGIN MspInit 1 */
/* USER CODE END MspInit 1 */
}
2021-09-17 05:59 AM
> ret = HAL_ADC_Start(&hadc4);
> ...
> if (HAL_ADC_Init(&hadc1) != HAL_OK)
Your post says ADC1 but your code initializes ADC1 and uses ADC4.
If it's reporting HAL_ERROR, look at the reason for the error in hadc->ErrorCode.
2021-09-17 06:10 AM
Thanks for you answer! Sorry I posted the ADC4 example together with ADC1 initialization; my bad but the issue really is present for either ADC1 or ADC4.
I've checked hadc1.ErrorCode right now: it reads 1 => HAL_ADC_ERROR_INTERNAL.
If I step through the code, here's the spot where it times out:
while (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_RDY) == 0UL)
Hope that helps.
2021-09-20 12:37 AM
Any other tips why the ready flag doesn't get set?
I've double-checked clock settings but everything seems fine.
2021-09-20 06:31 AM
2021-09-21 12:04 AM
Thanks for mentioning this. Any chance there's a bug in the STM HAL and/or CubeIDE for the U5?
2021-09-21 01:38 AM
Using an RTOS and the task enabling the ADC is being preempted as described in https://community.st.com/s/question/0D53W00000LCTfySAH/adcenable-in-stm32l4xxhaladcc-return-with-halerror?
>Any chance there's a bug in the STM HAL and/or CubeIDE for the U5?
Or most everyone here is very unlucky.
2021-09-21 05:51 AM
2021-09-23 12:19 AM
Good hint about the preemption issue. I'm not using an RTOS though, so that won't be it.
2021-12-14 10:17 AM
Hi,
Probably it is late, however you need to enable VDDA before hand. Please refer to "10.4.4 Independent analog peripherals supply" for STM32U5
Best Regards,
Ramin