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>
bound-method-equality
Damien George 2020-10-28 17:19:52 +11:00
parent 368c1a0961
commit 0118c07916
5 changed files with 14 additions and 4 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)) {
}
}