Skip to content

Commit e346a72

Browse files
committed
drivers/adc: stm32: workaround for both L462xE and G081xB errata
Apply workaround for both L462xE errata 2.5.2 and G081xB errata 2.6.2, avoiding to have double enabling for a single read on G0 series. Make workaround for L462xE valid also for the async api. Signed-off-by: Giancarlo Stasi <[email protected]>
1 parent bdcef28 commit e346a72

File tree

1 file changed

+51
-51
lines changed

1 file changed

+51
-51
lines changed

drivers/adc/adc_stm32.c

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,40 @@ static void adc_stm32_calib(const struct device *dev)
319319
}
320320
#endif
321321

322+
static int adc_stm32_enable_and_wait_stabilisation(ADC_TypeDef *adc)
323+
{
324+
#if defined(CONFIG_SOC_SERIES_STM32L4X) || \
325+
defined(CONFIG_SOC_SERIES_STM32L5X) || \
326+
defined(CONFIG_SOC_SERIES_STM32WBX) || \
327+
defined(CONFIG_SOC_SERIES_STM32G0X) || \
328+
defined(CONFIG_SOC_SERIES_STM32G4X) || \
329+
defined(CONFIG_SOC_SERIES_STM32H7X) || \
330+
defined(CONFIG_SOC_SERIES_STM32WLX)
331+
/*
332+
* Enabling ADC modules in L4, WB, G0 and G4 series may fail if they are
333+
* still not stabilized, this will wait for a short time to ensure ADC
334+
* modules are properly enabled.
335+
*/
336+
uint32_t countTimeout = 0;
337+
338+
do {
339+
if (LL_ADC_IsEnabled(adc) == 0UL) {
340+
LL_ADC_Enable(adc);
341+
}
342+
countTimeout++;
343+
if (countTimeout == 10) {
344+
return -ETIMEDOUT;
345+
}
346+
} while (LL_ADC_IsActiveFlag_ADRDY(adc) == 0);
347+
LL_ADC_ClearFlag_ADRDY(adc);
348+
#else
349+
if (LL_ADC_IsEnabled(adc) == 0UL) {
350+
LL_ADC_Enable(adc);
351+
}
352+
#endif
353+
return 0;
354+
}
355+
322356
static int start_read(const struct device *dev,
323357
const struct adc_sequence *sequence)
324358
{
@@ -425,6 +459,12 @@ static int start_read(const struct device *dev,
425459
}
426460
#elif !defined(CONFIG_SOC_SERIES_STM32F1X) && \
427461
!defined(STM32F3X_ADC_V2_5)
462+
if (LL_ADC_IsEnabled(adc) == 0) {
463+
err = adc_stm32_enable_and_wait_stabilisation(adc);
464+
if (err) {
465+
return err;
466+
}
467+
}
428468
LL_ADC_SetResolution(adc, resolution);
429469
#endif
430470

@@ -464,7 +504,13 @@ static int start_read(const struct device *dev,
464504

465505
adc_context_start_read(&data->ctx, sequence);
466506

467-
return adc_context_wait_for_completion(&data->ctx);
507+
err = adc_context_wait_for_completion(&data->ctx);
508+
509+
if (LL_ADC_IsEnabled(adc)) {
510+
LL_ADC_Disable(adc);
511+
}
512+
513+
return err;
468514
}
469515

470516
static void adc_context_start_sampling(struct adc_context *ctx)
@@ -502,61 +548,15 @@ static void adc_stm32_isr(const struct device *dev)
502548
LOG_DBG("ISR triggered.");
503549
}
504550

505-
static int adc_stm32_enable_and_wait_stabilisation(ADC_TypeDef *adc)
506-
{
507-
#if defined(CONFIG_SOC_SERIES_STM32L4X) || \
508-
defined(CONFIG_SOC_SERIES_STM32L5X) || \
509-
defined(CONFIG_SOC_SERIES_STM32WBX) || \
510-
defined(CONFIG_SOC_SERIES_STM32G0X) || \
511-
defined(CONFIG_SOC_SERIES_STM32G4X) || \
512-
defined(CONFIG_SOC_SERIES_STM32H7X) || \
513-
defined(CONFIG_SOC_SERIES_STM32WLX)
514-
/*
515-
* Enabling ADC modules in L4, WB, G0 and G4 series may fail if they are
516-
* still not stabilized, this will wait for a short time to ensure ADC
517-
* modules are properly enabled.
518-
*/
519-
uint32_t countTimeout = 0;
520-
521-
do {
522-
if (LL_ADC_IsEnabled(adc) == 0UL) {
523-
LL_ADC_Enable(adc);
524-
}
525-
countTimeout++;
526-
if (countTimeout == 10) {
527-
return -ETIMEDOUT;
528-
}
529-
} while (LL_ADC_IsActiveFlag_ADRDY(adc) == 0);
530-
LL_ADC_ClearFlag_ADRDY(adc);
531-
#else
532-
if (LL_ADC_IsEnabled(adc) == 0UL) {
533-
LL_ADC_Enable(adc);
534-
}
535-
#endif
536-
return 0;
537-
}
538-
539551
static int adc_stm32_read(const struct device *dev,
540552
const struct adc_sequence *sequence)
541553
{
542554
struct adc_stm32_data *data = dev->data;
543-
const struct adc_stm32_cfg *config = dev->config;
544-
ADC_TypeDef *adc = (ADC_TypeDef *)config->base;
545-
int error = 0;
546-
547-
if (LL_ADC_IsEnabled(adc) == 0) {
548-
error = adc_stm32_enable_and_wait_stabilisation(adc);
549-
}
550-
551-
if (error == 0) {
552-
adc_context_lock(&data->ctx, false, NULL);
553-
error = start_read(dev, sequence);
554-
adc_context_release(&data->ctx, error);
555-
}
555+
int error;
556556

557-
if (LL_ADC_IsEnabled(adc)) {
558-
LL_ADC_Disable(adc);
559-
}
557+
adc_context_lock(&data->ctx, false, NULL);
558+
error = start_read(dev, sequence);
559+
adc_context_release(&data->ctx, error);
560560

561561
return error;
562562
}

0 commit comments

Comments
 (0)