diff --git a/ports/stm32/boards/stm32f0xx_hal_conf_base.h b/ports/stm32/boards/stm32f0xx_hal_conf_base.h index 9cb7761ac..faceda2f4 100644 --- a/ports/stm32/boards/stm32f0xx_hal_conf_base.h +++ b/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 diff --git a/ports/stm32/boards/stm32h7xx_hal_conf_base.h b/ports/stm32/boards/stm32h7xx_hal_conf_base.h index 5c97e2c44..9f43da403 100644 --- a/ports/stm32/boards/stm32h7xx_hal_conf_base.h +++ b/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 diff --git a/ports/stm32/boards/stm32l0xx_hal_conf_base.h b/ports/stm32/boards/stm32l0xx_hal_conf_base.h index ed524fecc..b100daaa9 100644 --- a/ports/stm32/boards/stm32l0xx_hal_conf_base.h +++ b/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 diff --git a/ports/stm32/boards/stm32wbxx_hal_conf_base.h b/ports/stm32/boards/stm32wbxx_hal_conf_base.h index 8dbc9ecea..83d07ad5b 100644 --- a/ports/stm32/boards/stm32wbxx_hal_conf_base.h +++ b/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 diff --git a/ports/stm32/machine_adc.c b/ports/stm32/machine_adc.c index f29896d37..9c20f0f95 100644 --- a/ports/stm32/machine_adc.c +++ b/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)) { } }