Browse Source

stm32/machine_adc: Fix ADC auto-calibration to run when ADC not enabled.

Prior to this commit, the ADC calibration code was never executing because
ADVREGEN bit was set making the CR register always non-zero.

This commit changes the logic so that ADC calibration is always run when
the ADC is disabled and an ADC channel is initialised.  It also uses the LL
API functions to do the calibration, to make sure it is done correctly on
each MCU variant.

Signed-off-by: Damien George <damien@micropython.org>
pull/6575/head
Damien George 4 years ago
parent
commit
0118c07916
  1. 1
      ports/stm32/boards/stm32f0xx_hal_conf_base.h
  2. 1
      ports/stm32/boards/stm32h7xx_hal_conf_base.h
  3. 1
      ports/stm32/boards/stm32l0xx_hal_conf_base.h
  4. 1
      ports/stm32/boards/stm32wbxx_hal_conf_base.h
  5. 14
      ports/stm32/machine_adc.c

1
ports/stm32/boards/stm32f0xx_hal_conf_base.h

@ -47,6 +47,7 @@
#include "stm32f0xx_hal_uart.h"
#include "stm32f0xx_hal_usart.h"
#include "stm32f0xx_hal_wwdg.h"
#include "stm32f0xx_ll_adc.h"
// Enable various HAL modules
#define HAL_MODULE_ENABLED

1
ports/stm32/boards/stm32h7xx_hal_conf_base.h

@ -53,6 +53,7 @@
#include "stm32h7xx_hal_uart.h"
#include "stm32h7xx_hal_usart.h"
#include "stm32h7xx_hal_wwdg.h"
#include "stm32h7xx_ll_adc.h"
// Enable various HAL modules
#define HAL_ADC_MODULE_ENABLED

1
ports/stm32/boards/stm32l0xx_hal_conf_base.h

@ -46,6 +46,7 @@
#include "stm32l0xx_hal_uart.h"
#include "stm32l0xx_hal_usart.h"
#include "stm32l0xx_hal_wwdg.h"
#include "stm32l0xx_ll_adc.h"
// Enable various HAL modules
#define HAL_MODULE_ENABLED

1
ports/stm32/boards/stm32wbxx_hal_conf_base.h

@ -41,6 +41,7 @@
#include "stm32wbxx_hal_tim.h"
#include "stm32wbxx_hal_uart.h"
#include "stm32wbxx_hal_usart.h"
#include "stm32wbxx_ll_adc.h"
// Enable various HAL modules
#define HAL_MODULE_ENABLED

14
ports/stm32/machine_adc.c

@ -156,10 +156,16 @@ STATIC void adc_config(ADC_TypeDef *adc, uint32_t bits) {
#endif
#if ADC_V2
if (adc->CR == 0) {
// ADC hasn't been enabled so calibrate it
adc->CR |= ADC_CR_ADCAL;
while (adc->CR & ADC_CR_ADCAL) {
if (!(adc->CR & ADC_CR_ADEN)) {
// ADC isn't enabled so calibrate it now
#if defined(STM32F0) || defined(STM32L0)
LL_ADC_StartCalibration(adc);
#elif defined(STM32L4) || defined(STM32WB)
LL_ADC_StartCalibration(adc, LL_ADC_SINGLE_ENDED);
#else
LL_ADC_StartCalibration(adc, LL_ADC_CALIB_OFFSET_LINEARITY, LL_ADC_SINGLE_ENDED);
#endif
while (LL_ADC_IsCalibrationOnGoing(adc)) {
}
}

Loading…
Cancel
Save