cancel
Showing results for 
Search instead for 
Did you mean: 

STM32U5G9J ADC HAL_ADC_Start results in an error.

Jack3
Senior III

HAL_ADC_Start on STM32U5G9J results in HAL error.

Initializing the ADC

/* ADC4 init function */
void MX_ADC4_Init(void)
{

  /* USER CODE BEGIN ADC4_Init 0 */

  /* USER CODE END ADC4_Init 0 */

  ADC_ChannelConfTypeDef sConfig = {0};

  /* USER CODE BEGIN ADC4_Init 1 */

  /* USER CODE END ADC4_Init 1 */

  /** Common config
  */
  hadc4.Instance = ADC4;
  hadc4.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV256;
  hadc4.Init.Resolution = ADC_RESOLUTION_12B;
  hadc4.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc4.Init.ScanConvMode = ADC4_SCAN_DISABLE;
  hadc4.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  hadc4.Init.LowPowerAutoPowerOff = ADC_LOW_POWER_NONE;
  hadc4.Init.LowPowerAutoWait = DISABLE;
  hadc4.Init.ContinuousConvMode = ENABLE;
  hadc4.Init.NbrOfConversion = 2;
  hadc4.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc4.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc4.Init.DMAContinuousRequests = DISABLE;
  hadc4.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_LOW;
  hadc4.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  hadc4.Init.SamplingTimeCommon1 = ADC4_SAMPLETIME_1CYCLE_5;
  hadc4.Init.SamplingTimeCommon2 = ADC4_SAMPLETIME_1CYCLE_5;
  hadc4.Init.OversamplingMode = DISABLE;
  if (HAL_ADC_Init(&hadc4) != HAL_OK)
  {
    Error_Handler();
  }

  /** Configure Regular Channel
  */
  sConfig.Channel = ADC_CHANNEL_8;
  sConfig.Rank = ADC4_REGULAR_RANK_1;
  sConfig.SamplingTime = ADC4_SAMPLINGTIME_COMMON_1;
  sConfig.OffsetNumber = ADC_OFFSET_NONE;
  sConfig.Offset = 0;
  if (HAL_ADC_ConfigChannel(&hadc4, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }

  /** Configure Regular Channel
  */
  sConfig.Channel = ADC_CHANNEL_7;
  sConfig.Rank = ADC4_REGULAR_RANK_2;
  if (HAL_ADC_ConfigChannel(&hadc4, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN ADC4_Init 2 */

  /* USER CODE END ADC4_Init 2 */

}

void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  if(adcHandle->Instance==ADC4)
  {
  /* USER CODE BEGIN ADC4_MspInit 0 */

  /* USER CODE END ADC4_MspInit 0 */

  /** Initializes the peripherals clock
  */
    PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADCDAC;
    PeriphClkInit.AdcDacClockSelection = RCC_ADCDACCLKSOURCE_HCLK;
    if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
    {
      Error_Handler();
    }

    /* ADC4 clock enable */
    __HAL_RCC_ADC4_CLK_ENABLE();

    __HAL_RCC_GPIOG_CLK_ENABLE();
    /**ADC4 GPIO Configuration
    PG0     ------> ADC4_IN7
    PG1     ------> ADC4_IN8
    */
    GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);

    /* ADC4 interrupt Init */
//    HAL_NVIC_SetPriority(ADC4_IRQn, 0, 0);
//    HAL_NVIC_EnableIRQ(ADC4_IRQn);
  /* USER CODE BEGIN ADC4_MspInit 1 */

  /* USER CODE END ADC4_MspInit 1 */
  }
}

 

Reading:

  if (HAL_ADC_Start(&hadc4) != HAL_OK)
  {
    printf("HAL_ADC_Start ERROR\n");
  }
  if (HAL_ADC_PollForConversion(&hadc4, 10) == HAL_OK)
  {
    uint16_t  value = (uint16_t) HAL_ADC_GetValue(&hadc4);
    printf("Value: %d\n", (uint32_t)value);
  }
  else
  {
    printf("HAL_ADC_PollForConversion ERROR\n");
  }

 

What can be the problem?

3 REPLIES 3
TDK
Super User

Step through HAL_ADC_Start and find out what fails. No need to guess.

Use DMA to convert more than 1 channel at a time.

If you feel a post has answered your question, please click "Accept as Solution".
EXUE.2
ST Employee

there is no problem for the code you showed, but you must debug it online and check which point is error. on the other hand, did you have enable the VDDA power like below?

void HAL_MspInit(void)
{

/* USER CODE BEGIN MspInit 0 */

/* USER CODE END MspInit 0 */

__HAL_RCC_PWR_CLK_ENABLE();
HAL_PWREx_EnableVddA();

/* System interrupt init*/

/* USER CODE BEGIN MspInit 1 */
HAL_PWREx_EnableVddA();
HAL_PWREx_EnableVddIO2();
/* USER CODE END MspInit 1 */
}

Saket_Om
ST Employee

Hello @Jack3 

Please refer to the configuration on the example STM32CubeU5/Projects/NUCLEO-U575ZI-Q/Examples/ADC/ADC_SingleConversion_TriggerSW_IT at main · STMicroelectronics/STM32CubeU5

I modified it to run ADC in pooling mode, and it work fine for me. HAL_ADC_Start() don't return HAL_ERROR.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Saket_Om