Skip to main content
Steffen1
Associate
September 17, 2021
Question

STM32U5 Cannot enable ADC (timeout on polling for ADC_ISR:ASRDY)

  • September 17, 2021
  • 10 replies
  • 5721 views

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 */
}
This topic has been closed for replies.

10 replies

TDK
September 17, 2021

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

"If you feel a post has answered your question, please click ""Accept as Solution""."
Steffen1
Steffen1Author
Associate
September 17, 2021

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.

Steffen1
Steffen1Author
Associate
September 20, 2021

Any other tips why the ready flag doesn't get set?

I've double-checked clock settings but everything seems fine.

TDK
September 20, 2021
You can step through the code and see where/why HAL_ADC_ERROR_INTERNAL is getting set. The STM32U5 is new and doesn't appear to have a github repository for it yet.
"If you feel a post has answered your question, please click ""Accept as Solution""."
Steffen1
Steffen1Author
Associate
September 21, 2021

Thanks for mentioning this. Any chance there's a bug in the STM HAL and/or CubeIDE for the U5?

Ramin1
Associate II
December 14, 2021

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

Steffen1
Steffen1Author
Associate
December 16, 2021

Hi Ramin,

that's great news! I'm glad you figured that out.

thanks,

Steffen