From cfce5afcd9dad4d30ba2343d106058d27c239aa2 Mon Sep 17 00:00:00 2001
From: Alexander Mihajlovic
Date: Wed, 5 Jan 2022 11:31:45 +0100
Subject: [PATCH] drivers: adc: stm32: Clear ADRDY before waiting
Clear ADRDY before enabling ADC to ensure the subsequent
wait for ADRDY does not stop prematurely in case ADRDY
was already set.
The "ADC on-off control" sections of the following reference manuals
were consulted. That gives at least one instance per series affected
by this change, even if not every affected MCU is covered.
- RM0438 (STM32L552xx and STM32L562xx)
- RM0351 (STM32L47xxx, STM32L48xxx, STM32L49xxx and STM32L4Axxx)
- RM0434 (STM32WB55xx and STM32WB35xx)
- RM0454 (STM32G0x0)
- RM0440 (STM32G4 Series)
- RM0399 (STM32H745/755 and STM32H747/757)
- RM0433 (STM32H742, STM32H743/753 and STM32H750)
- RM0453 (STM32WL5x)
Signed-off-by: Alexander Mihajlovic
---
drivers/adc/adc_stm32.c | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/drivers/adc/adc_stm32.c b/drivers/adc/adc_stm32.c
index ac311460e9b..a6b59de6195 100644
--- a/drivers/adc/adc_stm32.c
+++ b/drivers/adc/adc_stm32.c
@@ -412,11 +412,6 @@ static void adc_stm32_oversampling(ADC_TypeDef *adc, uint8_t ratio, uint32_t shi
*/
static int adc_stm32_enable(ADC_TypeDef *adc)
{
- if (LL_ADC_IsEnabled(adc) == 1UL) {
- return 0;
- }
- LL_ADC_Enable(adc);
-
#if defined(CONFIG_SOC_SERIES_STM32L4X) || \
defined(CONFIG_SOC_SERIES_STM32L5X) || \
defined(CONFIG_SOC_SERIES_STM32WBX) || \
@@ -424,6 +419,14 @@ static int adc_stm32_enable(ADC_TypeDef *adc)
defined(CONFIG_SOC_SERIES_STM32G4X) || \
defined(CONFIG_SOC_SERIES_STM32H7X) || \
defined(CONFIG_SOC_SERIES_STM32WLX)
+
+ if (LL_ADC_IsEnabled(adc) == 1UL) {
+ return 0;
+ }
+
+ LL_ADC_ClearFlag_ADRDY(adc);
+ LL_ADC_Enable(adc);
+
/*
* Enabling ADC modules in L4, WB, G0 and G4 series may fail if they are
* still not stabilized, this will wait for a short time to ensure ADC
@@ -440,6 +443,8 @@ static int adc_stm32_enable(ADC_TypeDef *adc)
}
}
}
+#else
+ LL_ADC_Enable(adc);
#endif
return 0;