Skip to content

Commit 8add921

Browse files
hakehuangcfriedt
authored andcommitted
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 <hake.huang@oss.nxp.com>
1 parent 53374c6 commit 8add921

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

drivers/timer/cortex_m_systick.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,23 @@
1717
#define COUNTER_MAX 0x00ffffff
1818
#define TIMER_STOPPED 0xff000000
1919

20-
#define CYC_PER_TICK (sys_clock_hw_cycles_per_sec() \
21-
/ CONFIG_SYS_CLOCK_TICKS_PER_SEC)
22-
#define MAX_TICKS ((k_ticks_t)(COUNTER_MAX / CYC_PER_TICK) - 1)
20+
21+
#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME)
22+
extern unsigned int z_clock_hw_cycles_per_sec;
23+
#define CYC_PER_TICK (z_clock_hw_cycles_per_sec/CONFIG_SYS_CLOCK_TICKS_PER_SEC)
24+
#else
25+
#define CYC_PER_TICK (CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC/CONFIG_SYS_CLOCK_TICKS_PER_SEC)
26+
#endif
27+
28+
/* add MAX_TICKS protection */
29+
#define _MAX_TICKS (int)((k_ticks_t)(COUNTER_MAX / CYC_PER_TICK) - 1)
30+
#define MAX_TICKS ((_MAX_TICKS > 0) ? _MAX_TICKS : 1)
2331
#define MAX_CYCLES (MAX_TICKS * CYC_PER_TICK)
2432

33+
#if (COUNTER_MAX / CYC_PER_TICK) == 1
34+
#pragma message("tickless does nothing as CONFIG_SYS_CLOCK_TICKS_PER_SEC too low")
35+
#endif
36+
2537
/* Minimum cycles in the future to try to program. Note that this is
2638
* NOT simply "enough cycles to get the counter read and reprogrammed
2739
* reliably" -- it becomes the minimum value of the LOAD register, and

0 commit comments

Comments
 (0)