Skip to content

Commit 3c7f990

Browse files
pizi-nordicnashif
authored andcommitted
kernel: Do not use sys_clock_ticks_per_sec in _ms_to_ticks()
The value of sys_clock_ticks_per_sec is obtained using simple integer division with rounding toward zero. As result using this variable in _ms_to_ticks() introduces some error. This commit eliminates sys_clock_ticks_per_sec from equation used in _ms_to_ticks() removing introduced error. Also, this commit fixes #8895. Signed-off-by: Piotr Zięcik <[email protected]>
1 parent 2a26576 commit 3c7f990

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

include/kernel.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,9 +1357,9 @@ static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
13571357

13581358
#ifdef _NEED_PRECISE_TICK_MS_CONVERSION
13591359
/* use 64-bit math to keep precision */
1360-
s64_t ms_ticks_per_sec = (s64_t)ms * sys_clock_ticks_per_sec;
1361-
1362-
return (s32_t)ceiling_fraction(ms_ticks_per_sec, MSEC_PER_SEC);
1360+
return (s32_t)ceiling_fraction(
1361+
(s64_t)ms * sys_clock_hw_cycles_per_sec,
1362+
(s64_t)MSEC_PER_SEC * sys_clock_hw_cycles_per_tick);
13631363
#else
13641364
/* simple division keeps precision */
13651365
s32_t ms_per_tick = MSEC_PER_SEC / sys_clock_ticks_per_sec;

0 commit comments

Comments
 (0)