From 8add9219a794d4df666871a1ed5daab23109a84b Mon Sep 17 00:00:00 2001 From: Hake Huang Date: Mon, 30 Dec 2024 11:21:42 +0000 Subject: [PATCH] drivers: timer : cortex_m_systick MAX_TICKS protection when CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC set to 960M and CONFIG_SYS_CLOCK_TICKS_PER_SEC set to 100 the MAX_TICKS will be zero or even negative value, which is not expected. so need add a protection here downgrading the accuracy to its as high as possible also add build message to show that tickless has no effect fixes: #36766 there used to be a workaround, not a fix, either change the CONFIG_SYS_CLOCK_TICKS_PER_SEC=200 or CONFIG_PM to set the CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC to 32678 Signed-off-by: Hake Huang --- drivers/timer/cortex_m_systick.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/drivers/timer/cortex_m_systick.c b/drivers/timer/cortex_m_systick.c index 029738900ef..e2563ddd9d7 100644 --- a/drivers/timer/cortex_m_systick.c +++ b/drivers/timer/cortex_m_systick.c @@ -17,11 +17,23 @@ #define COUNTER_MAX 0x00ffffff #define TIMER_STOPPED 0xff000000 -#define CYC_PER_TICK (sys_clock_hw_cycles_per_sec() \ - / CONFIG_SYS_CLOCK_TICKS_PER_SEC) -#define MAX_TICKS ((k_ticks_t)(COUNTER_MAX / CYC_PER_TICK) - 1) + +#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) +extern unsigned int z_clock_hw_cycles_per_sec; +#define CYC_PER_TICK (z_clock_hw_cycles_per_sec/CONFIG_SYS_CLOCK_TICKS_PER_SEC) +#else +#define CYC_PER_TICK (CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC/CONFIG_SYS_CLOCK_TICKS_PER_SEC) +#endif + +/* add MAX_TICKS protection */ +#define _MAX_TICKS (int)((k_ticks_t)(COUNTER_MAX / CYC_PER_TICK) - 1) +#define MAX_TICKS ((_MAX_TICKS > 0) ? _MAX_TICKS : 1) #define MAX_CYCLES (MAX_TICKS * CYC_PER_TICK) +#if (COUNTER_MAX / CYC_PER_TICK) == 1 +#pragma message("tickless does nothing as CONFIG_SYS_CLOCK_TICKS_PER_SEC too low") +#endif + /* Minimum cycles in the future to try to program. Note that this is * NOT simply "enough cycles to get the counter read and reprogrammed * reliably" -- it becomes the minimum value of the LOAD register, and