cancel
Showing results for 
Search instead for 
Did you mean: 

Having to configure each time reading the adc value

WDashti
Associate

Hello

I am using stm32h750vb. I want to use an interrupt to read the adc value, but in the interrupt and dam mode the adc unit does not work.

In polling mode, it shows the correct value only once after the microcontroller reset and then a constant value.

Only displays the correct value if I use the following function to read the adc value. Using this method causes the microcontroller to heat up in a short time.

uint16_t adc_read_value(void){
	static uint16_t Value = 0;
	
	HAL_ADC_Init(&hadc1);
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
HAL_ADCEx_Calibration_Start(&hadc1, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED);
	HAL_ADC_Start(&hadc1);
	HAL_ADC_PollForConversion(&hadc1, 100);
  Value = HAL_ADC_GetValue(&hadc1);
  HAL_ADC_DeInit(&hadc1);
 
	return Value;
}

I have used the adc unit several times in the stm32f1 and stm32l1 series but have not encountered such a problem.

I used adc1 and adc2 and several bases but the problem was not solved.

using stm32cubemx 6.5 and keil​

static void MX_ADC1_Init(void)
{
  /* USER CODE BEGIN ADC1_Init 0 */
	hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  /* 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.Resolution = ADC_RESOLUTION_16B;
  hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  hadc1.Init.LowPowerAutoWait = DISABLE;
  hadc1.Init.ContinuousConvMode = ENABLE;
  hadc1.Init.NbrOfConversion = 1;
  hadc1.Init.DiscontinuousConvMode = DISABLE;
  hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc1.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR;
  hadc1.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
  hadc1.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE;
  hadc1.Init.OversamplingMode = DISABLE;
  if (HAL_ADC_Init(&hadc1) != HAL_OK)
  {
    Error_Handler();
  }
 
  /** Configure the ADC multi-mode
  */
  multimode.Mode = ADC_MODE_INDEPENDENT;
  if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK)
  {
    Error_Handler();
  }
 
  /** Configure Regular Channel
  */
  sConfig.Channel = ADC_CHANNEL_17;
  sConfig.Rank = ADC_REGULAR_RANK_1;
  sConfig.SamplingTime = ADC_SAMPLETIME_64CYCLES_5;
  sConfig.SingleDiff = ADC_SINGLE_ENDED;
  sConfig.OffsetNumber = ADC_OFFSET_NONE;
  sConfig.Offset = 0;
  sConfig.OffsetSignedSaturation = DISABLE;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN ADC1_Init 2 */
  /* USER CODE END ADC1_Init 2 */
}

1 ACCEPTED SOLUTION

Accepted Solutions
Sara BEN HADJ YAHYA
ST Employee

Hello @WDashti​ ,

Thanks for your feedback and sorry for the late reply,

As @Pavel A.​  mentioned, there is a known issue with ADC clock in v6.5.0 which affects the code generation.

When generating a code, the initialization of peripherals clock (the following code) is not generated in HAL_ADC_MspInit(ADC_HandleTypeDef* hadc) in the generated stm32h7xx_hal_msp.c file

    PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_ADC;
    PeriphClkInitStruct.PLL2.PLL2M = 4;
    PeriphClkInitStruct.PLL2.PLL2N = 9;
    PeriphClkInitStruct.PLL2.PLL2P = 4;
    PeriphClkInitStruct.PLL2.PLL2Q = 2;
    PeriphClkInitStruct.PLL2.PLL2R = 2;
    PeriphClkInitStruct.PLL2.PLL2RGE = RCC_PLL2VCIRANGE_3;
    PeriphClkInitStruct.PLL2.PLL2VCOSEL = RCC_PLL2VCOMEDIUM;
    PeriphClkInitStruct.PLL2.PLL2FRACN = 3072.0;
    PeriphClkInitStruct.AdcClockSelection = RCC_ADCCLKSOURCE_PLL2;
    if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
    {
      Error_Handler();
    }

As a workaround for this issue, you can add the missing part within the first user section in HAL_ADC_MspInit() function.

  /* USER CODE BEGIN ADC1_MspInit 0 */
 
  /* USER CODE END ADC1_MspInit 0 */

PS: The clock config can be changed manually according to your application (PLL settings,..).

I will let you know once the fix is done.

I hope this helps :smiling_face_with_smiling_eyes: ,

If this is not your issue, could you please share your ioc file for further check?

If your issue is solved, please close this post by clicking the "Select as Best" button. This will help other members of the community find this response more quickly :)

Sara.

View solution in original post

7 REPLIES 7
MM..1
Chief III

Maybe you start and dont stop

hadc1.Init.ContinuousConvMode = ENABLE;

too MX Init call for you hal init , then in your func dont call it. In firmware folder exist examples projects for DMA/IT ...

Pavel A.
Evangelist III

There is a nasty Cube bug in ADC clock configuration.

If your init code is affected, will have to wait for fix.

WDashti
Associate

The same problem exists with stm32cubemx 5.6 and stm32h7 package 1.7.

Sara BEN HADJ YAHYA
ST Employee

Hello @WDashti​ ,

Thanks for your feedback and sorry for the late reply,

As @Pavel A.​  mentioned, there is a known issue with ADC clock in v6.5.0 which affects the code generation.

When generating a code, the initialization of peripherals clock (the following code) is not generated in HAL_ADC_MspInit(ADC_HandleTypeDef* hadc) in the generated stm32h7xx_hal_msp.c file

    PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_ADC;
    PeriphClkInitStruct.PLL2.PLL2M = 4;
    PeriphClkInitStruct.PLL2.PLL2N = 9;
    PeriphClkInitStruct.PLL2.PLL2P = 4;
    PeriphClkInitStruct.PLL2.PLL2Q = 2;
    PeriphClkInitStruct.PLL2.PLL2R = 2;
    PeriphClkInitStruct.PLL2.PLL2RGE = RCC_PLL2VCIRANGE_3;
    PeriphClkInitStruct.PLL2.PLL2VCOSEL = RCC_PLL2VCOMEDIUM;
    PeriphClkInitStruct.PLL2.PLL2FRACN = 3072.0;
    PeriphClkInitStruct.AdcClockSelection = RCC_ADCCLKSOURCE_PLL2;
    if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
    {
      Error_Handler();
    }

As a workaround for this issue, you can add the missing part within the first user section in HAL_ADC_MspInit() function.

  /* USER CODE BEGIN ADC1_MspInit 0 */
 
  /* USER CODE END ADC1_MspInit 0 */

PS: The clock config can be changed manually according to your application (PLL settings,..).

I will let you know once the fix is done.

I hope this helps :smiling_face_with_smiling_eyes: ,

If this is not your issue, could you please share your ioc file for further check?

If your issue is solved, please close this post by clicking the "Select as Best" button. This will help other members of the community find this response more quickly :)

Sara.

xsong.1
Associate

0693W00000Nqnq7QAB.png0693W00000Nqnq2QAB.pngMCU:STM32H743IIT6

STM32CubeMX:V6.5.0

"Pinout & Cofiguration" -->adc enable, but "Clock Configuration" ADC disabled.

Same project,STM32CubeMx6.4.0 is OK,but STM32CubeMx6.5.0 is wrong .so It is a Bug.

Hello @xsong.1​ ,

Yes you are right it is a bug, it is actually a known issue and it will be fixed soon. I will keep you posted.

The workaround for this issue is mentioned in my previous comment.

Sorry for the inconveniences,

Sara.

Sara BEN HADJ YAHYA
ST Employee

Hello @xsong.1​ , @WDashti​, @Pavel A.​ , @MM..1​ 

This issue is fixed in STM32CubeMX latest release.

V6.6.0 is now available under this Link.

Thanks for your contribution.

Sara.