Skip to content

Commit bab52fb

Browse files
gautierg-stcarlescufi
authored andcommitted
drivers: adc: clean stm32 adc calibration
Rework and clean some code around STM32 ADC calibration Signed-off-by: Guillaume Gautier <[email protected]>
1 parent 5180b6f commit bab52fb

File tree

1 file changed

+48
-47
lines changed

1 file changed

+48
-47
lines changed

drivers/adc/adc_stm32.c

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,45 @@ static void adc_stm32_start_conversion(const struct device *dev)
329329
#if !defined(CONFIG_SOC_SERIES_STM32F2X) && \
330330
!defined(CONFIG_SOC_SERIES_STM32F4X) && \
331331
!defined(CONFIG_SOC_SERIES_STM32F7X) && \
332-
!defined(CONFIG_SOC_SERIES_STM32F1X) && \
333-
!defined(STM32F3X_ADC_V2_5) && \
334332
!defined(CONFIG_SOC_SERIES_STM32L1X)
333+
334+
/* Number of ADC clock cycles to wait before of after starting calibration */
335+
#if defined(LL_ADC_DELAY_CALIB_ENABLE_ADC_CYCLES)
336+
#define ADC_DELAY_CALIB_ADC_CYCLES LL_ADC_DELAY_CALIB_ENABLE_ADC_CYCLES
337+
#elif defined(LL_ADC_DELAY_ENABLE_CALIB_ADC_CYCLES)
338+
#define ADC_DELAY_CALIB_ADC_CYCLES LL_ADC_DELAY_ENABLE_CALIB_ADC_CYCLES
339+
#elif defined(LL_ADC_DELAY_DISABLE_CALIB_ADC_CYCLES)
340+
#define ADC_DELAY_CALIB_ADC_CYCLES LL_ADC_DELAY_DISABLE_CALIB_ADC_CYCLES
341+
#endif
342+
343+
static void adc_stm32_calib_delay(const struct device *dev)
344+
{
345+
/*
346+
* Calibration of F1 and F3 (ADC1_V2_5) must start two cycles after ADON
347+
* is set.
348+
* Other ADC modules have to wait for some cycles after calibration to
349+
* be enabled.
350+
*/
351+
const struct adc_stm32_cfg *config = dev->config;
352+
const struct device *const clk = DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE);
353+
uint32_t adc_rate, wait_cycles;
354+
355+
if (clock_control_get_rate(clk,
356+
(clock_control_subsys_t) &config->pclken, &adc_rate) < 0) {
357+
LOG_ERR("ADC clock rate get error.");
358+
}
359+
360+
if (adc_rate == 0) {
361+
LOG_ERR("ADC Clock rate null");
362+
return;
363+
}
364+
wait_cycles = SystemCoreClock / adc_rate *
365+
ADC_DELAY_CALIB_ADC_CYCLES;
366+
367+
for (int i = wait_cycles; i >= 0; i--) {
368+
}
369+
}
370+
335371
static void adc_stm32_calib(const struct device *dev)
336372
{
337373
const struct adc_stm32_cfg *config =
@@ -347,6 +383,8 @@ static void adc_stm32_calib(const struct device *dev)
347383
LL_ADC_StartCalibration(adc, LL_ADC_SINGLE_ENDED);
348384
#elif defined(CONFIG_SOC_SERIES_STM32C0X) || \
349385
defined(CONFIG_SOC_SERIES_STM32F0X) || \
386+
defined(CONFIG_SOC_SERIES_STM32F1X) || \
387+
defined(STM32F3X_ADC_V2_5) || \
350388
defined(CONFIG_SOC_SERIES_STM32G0X) || \
351389
defined(CONFIG_SOC_SERIES_STM32L0X) || \
352390
defined(CONFIG_SOC_SERIES_STM32WLX)
@@ -1350,46 +1388,13 @@ static int adc_stm32_init(const struct device *dev)
13501388
#if !defined(CONFIG_SOC_SERIES_STM32F2X) && \
13511389
!defined(CONFIG_SOC_SERIES_STM32F4X) && \
13521390
!defined(CONFIG_SOC_SERIES_STM32F7X) && \
1353-
!defined(CONFIG_SOC_SERIES_STM32F1X) && \
1354-
!defined(STM32F3X_ADC_V2_5) && \
13551391
!defined(CONFIG_SOC_SERIES_STM32L1X)
1356-
/*
1357-
* Calibration of F1 and F3 (ADC1_V2_5) series has to be started
1358-
* after ADC Module is enabled.
1359-
*/
1392+
1393+
#if !defined(CONFIG_SOC_SERIES_STM32F1X) && !defined(STM32F3X_ADC_V2_5)
13601394
adc_stm32_disable(adc);
13611395
adc_stm32_calib(dev);
1362-
#endif
1363-
1364-
#if defined(CONFIG_SOC_SERIES_STM32C0X) || \
1365-
defined(CONFIG_SOC_SERIES_STM32F0X) || \
1366-
defined(STM32F3X_ADC_V1_1) || \
1367-
defined(CONFIG_SOC_SERIES_STM32L0X) || \
1368-
defined(CONFIG_SOC_SERIES_STM32L4X) || \
1369-
defined(CONFIG_SOC_SERIES_STM32L5X) || \
1370-
defined(CONFIG_SOC_SERIES_STM32WBX) || \
1371-
defined(CONFIG_SOC_SERIES_STM32G0X) || \
1372-
defined(CONFIG_SOC_SERIES_STM32G4X) || \
1373-
defined(CONFIG_SOC_SERIES_STM32H5X) || \
1374-
defined(CONFIG_SOC_SERIES_STM32H7X) || \
1375-
defined(CONFIG_SOC_SERIES_STM32U5X) || \
1376-
defined(CONFIG_SOC_SERIES_STM32WLX)
1377-
/*
1378-
* ADC modules on these series have to wait for some cycles to be
1379-
* enabled.
1380-
*/
1381-
uint32_t adc_rate, wait_cycles;
1382-
1383-
if (clock_control_get_rate(clk,
1384-
(clock_control_subsys_t) &config->pclken, &adc_rate) < 0) {
1385-
LOG_ERR("ADC clock rate get error.");
1386-
}
1387-
1388-
wait_cycles = SystemCoreClock / adc_rate *
1389-
LL_ADC_DELAY_CALIB_ENABLE_ADC_CYCLES;
1390-
1391-
for (int i = wait_cycles; i >= 0; i--) {
1392-
}
1396+
adc_stm32_calib_delay(dev);
1397+
#endif /* !defined(CONFIG_SOC_SERIES_STM32F1X) && !defined(STM32F3X_ADC_V2_5) */
13931398
#endif
13941399

13951400
err = adc_stm32_enable(adc);
@@ -1399,15 +1404,11 @@ static int adc_stm32_init(const struct device *dev)
13991404

14001405
config->irq_cfg_func();
14011406

1402-
#if defined(CONFIG_SOC_SERIES_STM32F1X) || \
1403-
defined(STM32F3X_ADC_V2_5)
1404-
/*
1405-
* Calibration of F1 and F3 (ADC1_V2_5) must starts after two cycles
1406-
* after ADON is set.
1407-
*/
1408-
LL_ADC_StartCalibration(adc);
1407+
#if defined(CONFIG_SOC_SERIES_STM32F1X) || defined(STM32F3X_ADC_V2_5)
1408+
adc_stm32_calib_delay(dev);
1409+
adc_stm32_calib(dev);
14091410
LL_ADC_REG_SetTriggerSource(adc, LL_ADC_REG_TRIG_SOFTWARE);
1410-
#endif
1411+
#endif /* !defined(CONFIG_SOC_SERIES_STM32F1X) && !defined(STM32F3X_ADC_V2_5) */
14111412

14121413
#ifdef CONFIG_SOC_SERIES_STM32H7X
14131414
/*

0 commit comments

Comments
 (0)