diff --git a/drivers/timer/gecko_burtc_timer.c b/drivers/timer/gecko_burtc_timer.c index 5553dd0f33e..babbae41bd4 100644 --- a/drivers/timer/gecko_burtc_timer.c +++ b/drivers/timer/gecko_burtc_timer.c @@ -69,6 +69,8 @@ static atomic_t g_last_count; /* Spinlock to sync between Compare ISR and update of Compare register */ static struct k_spinlock g_lock; +/* Set to true when timer is initialized */ +static atomic_t g_init = ATOMIC_INIT(0); static void burtc_isr(const void *arg) { @@ -161,8 +163,10 @@ uint32_t sys_clock_elapsed(void) { if (!IS_ENABLED(CONFIG_TICKLESS_KERNEL)) { return 0; - } else { + } else if (atomic_get(&g_init)) { return (BURTC_CounterGet() - g_last_count) / g_cyc_per_tick; + } else { + return 0; } } @@ -172,7 +176,11 @@ uint32_t sys_clock_cycle_get_32(void) * a value of some 32-bit hw_cycles counter which counts with * z_clock_hw_cycles_per_sec frequency */ - return BURTC_CounterGet(); + if (atomic_get(&g_init)) { + return BURTC_CounterGet(); + } else { + return 0; + } } static int burtc_init(void) @@ -214,6 +222,7 @@ static int burtc_init(void) init.clkDiv = 1; init.start = false; BURTC_Init(&init); + atomic_set(&g_init, 1); /* Enable compare match interrupt */ BURTC_IntClear(BURTC_IF_COMP);