From 7f2cb0fd227cedd0ed10c0e39ccbb0acfd482be7 Mon Sep 17 00:00:00 2001 From: Erwan Gouriou Date: Tue, 23 Aug 2022 14:15:34 +0200 Subject: [PATCH] drivers: timer: stm32: Specific handling for L0 LSI On L0 series, LSI runs at 37KHz while LPTIM driver only supports speeds up to 32768Hz (to avoid counter overflow). Consequence is a time running faster than reality (x1.13) Solution to this is the implementation of the LPTIM prescaler support. While moving driver configuration from Kconfig to DT, this case was not taken into account and the effect was LPTIM counter overflow which consequence is worse than the slightly faster timer. Reproduce the initial behavior with this piece of code that will be removed once prescaler support is available. Signed-off-by: Erwan Gouriou --- drivers/timer/stm32_lptim_timer.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/drivers/timer/stm32_lptim_timer.c b/drivers/timer/stm32_lptim_timer.c index 3476d5c7e3c..5cd05a1d99a 100644 --- a/drivers/timer/stm32_lptim_timer.c +++ b/drivers/timer/stm32_lptim_timer.c @@ -315,6 +315,19 @@ static int sys_clock_driver_init(const struct device *dev) clock_control_get_rate(clk_ctrl, (clock_control_subsys_t *) &lptim_clk[1], &lptim_clock_freq); +#if defined(CONFIG_SOC_SERIES_STM32L0X) + /* Driver only supports freqs up to 32768Hz. On L0, LSI freq is 37KHz, + * which will overflow the LPTIM counter. + * Previous LPTIM configuration using device tree was doing forcing this + * with a Kconfig default. Impact is that time is 1.13 faster than reality. + * Following lines reproduce this behavior in order not to change behavior. + * This issue will be fixed by implementation LPTIM prescaler support. + */ + if (lptim_clk[1].bus == STM32_SRC_LSI) { + lptim_clock_freq = KHZ(32); + } +#endif + /* Set LPTIM time base based on clck source freq * Time base = (2s * freq) - 1 */