Skip to main content
Louis Cloete work
Associate III
October 2, 2020
Solved

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

  • October 2, 2020
  • 5 replies
  • 3170 views

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.

This topic has been closed for replies.
Best answer by Amel NASRI

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

5 replies

Amel NASRI
Amel NASRIBest answer
ST Technical Moderator
October 9, 2020

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 "Best Answer" on the reply which solved your issue or answered your question.
Louis Cloete work
Associate III
October 10, 2020

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

Nesrine.JLASSI
Visitor II
October 12, 2020

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
October 12, 2020

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.

Amel NASRI
ST Technical Moderator
October 13, 2020

This is exactly what was suggested and is being internally reviewed :)

To give better visibility on the answered topics, please click on "Best Answer" on the reply which solved your issue or answered your question.