From 2d740d42e0ef58933df40540cb6338b12d27bed3 Mon Sep 17 00:00:00 2001 From: Erwan Gouriou Date: Tue, 13 Sep 2022 10:45:28 +0200 Subject: [PATCH] drivers: timer: stm32_lptim: Change clock source config check sanction A specific check is implemented lptim driver in order to ensure global platform clock/tick configuration is in line with recommendations. To respect portability principles, don't error out when a config conflict is detected but generates a warning instead. Also, since these are only recommendations, provide an option to override the check. Besides automatically override when ZTEST is enabled, as some kernel tests specifically configure tick freq to 100. Signed-off-by: Erwan Gouriou --- drivers/timer/Kconfig.stm32_lptim | 12 ++++++++++++ drivers/timer/stm32_lptim_timer.c | 20 +++++++++++--------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/drivers/timer/Kconfig.stm32_lptim b/drivers/timer/Kconfig.stm32_lptim index 5a5ea17dfa..d1e0d5cb81 100644 --- a/drivers/timer/Kconfig.stm32_lptim +++ b/drivers/timer/Kconfig.stm32_lptim @@ -44,4 +44,16 @@ config STM32_LPTIM_TIMEBASE default 0xFFFF if STM32_LPTIM_CLOCK_LSE default 0xF9FF if STM32_LPTIM_CLOCK_LSI +config STM32_LPTIM_TICK_FREQ_RATIO_OVERRIDE + bool "Override tick to freq ratio check" + default y if ZTEST + help + For LPTIM configuration, a specific tick freq is advised + depending on LPTIM input clock: + - LSI(32KHz): 4000 ticks/sec + - LSE(32678): 4096 ticks/sec + To prevent misconfigurations, a dedicated check is implemented + in the driver. + This options allows to override this check + endif # STM32_LPTIM_TIMER diff --git a/drivers/timer/stm32_lptim_timer.c b/drivers/timer/stm32_lptim_timer.c index c422391e2f..092424f672 100644 --- a/drivers/timer/stm32_lptim_timer.c +++ b/drivers/timer/stm32_lptim_timer.c @@ -74,6 +74,17 @@ static bool autoreload_ready = true; static struct k_spinlock lock; +/* For tick accuracy, a specific tick to freq ratio is expected */ +/* This check assumes LSI@32KHz or LSE@32768Hz */ +#if !defined(CONFIG_STM32_LPTIM_TICK_FREQ_RATIO_OVERRIDE) +#if (((DT_CLOCKS_CELL_BY_IDX(DT_DRV_INST(0), 1, bus) == STM32_SRC_LSI) && \ + (CONFIG_SYS_CLOCK_TICKS_PER_SEC != 4000)) || \ + ((DT_CLOCKS_CELL_BY_IDX(DT_DRV_INST(0), 1, bus) == STM32_SRC_LSE) && \ + (CONFIG_SYS_CLOCK_TICKS_PER_SEC != 4096))) +#warning Advised tick freq is 4096 for LSE / 4000 for LSI +#endif +#endif /* !CONFIG_STM32_LPTIM_TICK_FREQ_RATIO_OVERRIDE */ + static void lptim_irq_handler(const struct device *unused) { @@ -303,15 +314,6 @@ static int sys_clock_driver_init(const struct device *dev) LL_SRDAMR_GRP1_EnableAutonomousClock(LL_SRDAMR_GRP1_PERIPH_LPTIM1AMEN); #endif - /* For tick accuracy, a specific tick to freq ratio is expected */ - /* This check assumes LSI@32KHz or LSE@32768Hz */ - if (((lptim_clk[1].bus == STM32_SRC_LSI) && - (CONFIG_SYS_CLOCK_TICKS_PER_SEC != 4000)) || - ((lptim_clk[1].bus == STM32_SRC_LSE) && - (CONFIG_SYS_CLOCK_TICKS_PER_SEC != 4096))) { - return -ENOTSUP; - } - /* Enable LPTIM clock source */ err = clock_control_configure(clk_ctrl, (clock_control_subsys_t *) &lptim_clk[1],