From 549358255f0a546ee131b8f2414c139864d264ec Mon Sep 17 00:00:00 2001 From: Roman Dobrodii Date: Thu, 11 May 2023 18:02:18 +0200 Subject: [PATCH] drivers/timer/gecko_burtc: allow queries prior to timer initialization Before BURTC is initialized, clock to its APB CSR file is stopped, so any attempt to read BURTC regs results in BusFault. However, many parts of Zephyr may call sys_clock getters even before sys_clock driver itself is initialized. This change adds support for that: sys_clock_elapsed() and sys_clock_cycle_get_32() simply return 0 if BURTC is not init yet. Signed-off-by: Roman Dobrodii --- drivers/timer/gecko_burtc_timer.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) 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);