From b8d57738b80a32c012ca9498f7ce28c879f5d3aa Mon Sep 17 00:00:00 2001 From: Andrew Boie Date: Thu, 16 Feb 2017 14:12:55 -0800 Subject: [PATCH] cortex_m_systick: fix _timer_cycle_get_32() race We need to account for the interrupt happening in the middle of the calculation. Issue: ZEP-1546 Change-Id: I193534856d7521cac7ca354d3e5b65e93b984bb1 Signed-off-by: Andrew Boie --- drivers/timer/cortex_m_systick.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/drivers/timer/cortex_m_systick.c b/drivers/timer/cortex_m_systick.c index 450cdac6b75..ba4b96b5d07 100644 --- a/drivers/timer/cortex_m_systick.c +++ b/drivers/timer/cortex_m_systick.c @@ -28,7 +28,7 @@ #include /* running total of timer count */ -static uint32_t clock_accumulated_count; +static volatile uint32_t clock_accumulated_count; /* * A board support package's board.h header must provide definitions for the @@ -559,7 +559,14 @@ int _sys_clock_driver_init(struct device *device) */ uint32_t _timer_cycle_get_32(void) { - return clock_accumulated_count + (SysTick->LOAD - SysTick->VAL); + uint32_t cac, count; + + do { + cac = clock_accumulated_count; + count = SysTick->LOAD - SysTick->VAL; + } while (cac != clock_accumulated_count); + + return cac + count; } #ifdef CONFIG_SYSTEM_CLOCK_DISABLE