summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2020-10-28 17:19:52 +1100
committerDamien George <damien@micropython.org>2020-10-29 11:09:43 +1100
commit0118c07916c24a6ccb6dbd0ea904312f01798b40 (patch)
treec9fd772d732d0cb7969204c43f0dd02f9adfd6f7
parent368c1a09611f2a139c0e401eeb4359f9cc2a7c57 (diff)
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>
-rw-r--r--ports/stm32/boards/stm32f0xx_hal_conf_base.h1
-rw-r--r--ports/stm32/boards/stm32h7xx_hal_conf_base.h1
-rw-r--r--ports/stm32/boards/stm32l0xx_hal_conf_base.h1
-rw-r--r--ports/stm32/boards/stm32wbxx_hal_conf_base.h1
-rw-r--r--ports/stm32/machine_adc.c14
5 files changed, 14 insertions, 4 deletions
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)) {
}
}