cancel
Showing results for 
Search instead for 
Did you mean: 

HAL with CubeMX ADC2 deinit causes ADC1 to read vrefint channel incorrectly.

Louis Cloete work
Associate III

I've had some issues to calculate the correct VREF+ voltage for the ADC on an STM32L476RGT6 MCU. After some investigation, I isolated the cause to be that I called `HAL_ADC_DeInit()` on ADC2 after I used it during setup before the main loop. If I just remove the call to `HAL_ADC_DeInit(&hadc2);`, ADC1 reads the vrefint channel correctly.

Any ideas? I can just keep ADC2 initialized, but the application will need to consume as little power as possible, since it will be powered by a battery and solar panel. Is deinitializing the ADC actually going to save power, or doesn't it matter?

Attached are the main.c and stm32l4xx_hal_msp.c files for my minimal reproduction project. If I comment the call to `HAL_ADC_DeInit(&hadc2)` on line 132 of main.c, vrefint is read correctly. If I just add that line back and change nothing else, vrefint reads 2-3x too low.

1 ACCEPTED SOLUTION

Accepted Solutions
Amel NASRI
ST Employee

Hi @Community member​ ,

HAL_ADC_DeInit calls HAL_ADC_MspDeInit. In this later, the call of __HAL_RCC_ADC_CLK_DISABLE will reset all ADCs as they are using the same clock.

As a workaround, you can call MX_ADC1_Init() after HAL_ADC_DeInit(&hadc2).

As your purpose is to minimize power consumption, you can put ADC2 in Deep-power-down by writing 1 to DEEPPWD inADC_CR register, instead of calling HAL_ADC_DeInit.

For farther recommendations on how to optimize power consumption for your application, have a look to AN4746.

-Amel

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.

View solution in original post

5 REPLIES 5
Amel NASRI
ST Employee

Hi @Community member​ ,

HAL_ADC_DeInit calls HAL_ADC_MspDeInit. In this later, the call of __HAL_RCC_ADC_CLK_DISABLE will reset all ADCs as they are using the same clock.

As a workaround, you can call MX_ADC1_Init() after HAL_ADC_DeInit(&hadc2).

As your purpose is to minimize power consumption, you can put ADC2 in Deep-power-down by writing 1 to DEEPPWD inADC_CR register, instead of calling HAL_ADC_DeInit.

For farther recommendations on how to optimize power consumption for your application, have a look to AN4746.

-Amel

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.

Louis Cloete work
Associate III

Thanks @Amel NASRI​ for the explanation. Of course disabling the clock will mess with things! I'll take a look at AN4746.

Hello @Community member​ 

Thanks for your post.

Feedback raised internally to be reviewed and treated accordingly to the priority. We will give you an update as soon as possible.

Thanks for your contribution!

Best regards,

Nesrine

Louis Cloete work
Associate III

Thanks! The behaviour I'd expect would be that the HAL only disables the clock if all ADCs are deinitialized, not when any ADC is deinitialized.

This is exactly what was suggested and is being internally reviewed ��

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.