From 105cfc88476909bed4b78e1a541d1cfc24cee7dd Mon Sep 17 00:00:00 2001 From: Adam Kondraciuk Date: Tue, 1 Jul 2025 10:26:59 +0200 Subject: [PATCH 1/2] Revert "[nrf fromlist] drivers: timer: nrf_grtc_timer: add last_count initialization" This reverts commit de4fad1be0a50716005819393910e6efc559b408. Signed-off-by: Adam Kondraciuk --- drivers/timer/nrf_grtc_timer.c | 8 -------- include/zephyr/drivers/timer/nrf_grtc_timer.h | 11 ----------- 2 files changed, 19 deletions(-) diff --git a/drivers/timer/nrf_grtc_timer.c b/drivers/timer/nrf_grtc_timer.c index 2874411de39..285e48a57fd 100644 --- a/drivers/timer/nrf_grtc_timer.c +++ b/drivers/timer/nrf_grtc_timer.c @@ -66,7 +66,6 @@ static struct k_spinlock lock; static uint64_t last_count; /* Time (SYSCOUNTER value) @last sys_clock_announce() */ static atomic_t int_mask; static uint8_t ext_channels_allocated; -static uint64_t grtc_start_value; static nrfx_grtc_channel_t system_clock_channel_data = { .handler = sys_clock_timeout_handler, .p_context = NULL, @@ -359,11 +358,6 @@ int z_nrf_grtc_timer_capture_read(int32_t chan, uint64_t *captured_time) return 0; } -uint64_t z_nrf_grtc_timer_startup_value_get(void) -{ - return grtc_start_value; -} - #if defined(CONFIG_POWEROFF) && defined(CONFIG_NRF_GRTC_START_SYSCOUNTER) int z_nrf_grtc_wakeup_prepare(uint64_t wake_time_us) { @@ -491,8 +485,6 @@ static int sys_clock_driver_init(void) } #endif /* CONFIG_NRF_GRTC_START_SYSCOUNTER */ - last_count = sys_clock_tick_get() * CYC_PER_TICK; - grtc_start_value = last_count; int_mask = NRFX_GRTC_CONFIG_ALLOWED_CC_CHANNELS_MASK; if (!IS_ENABLED(CONFIG_TICKLESS_KERNEL)) { system_timeout_set_relative(CYC_PER_TICK); diff --git a/include/zephyr/drivers/timer/nrf_grtc_timer.h b/include/zephyr/drivers/timer/nrf_grtc_timer.h index 5a51df21744..f8b69d7ddf0 100644 --- a/include/zephyr/drivers/timer/nrf_grtc_timer.h +++ b/include/zephyr/drivers/timer/nrf_grtc_timer.h @@ -189,17 +189,6 @@ int z_nrf_grtc_timer_capture_read(int32_t chan, uint64_t *captured_time); */ int z_nrf_grtc_wakeup_prepare(uint64_t wake_time_us); -/** @brief Get the GRTC counter value latched at startup. - * - * @note The GRTC timer is not cleared by software at startup, - * while the system tick starts counting from zero. - * In some cases, it may be necessary to compare the system tick - * with the GRTC value — in such situations, this offset can be useful. - * - * @return GRTC value latched during system clock initialization. - */ -uint64_t z_nrf_grtc_timer_startup_value_get(void); - /** * @brief Initialize the GRTC clock timer driver from an application- * defined function. From 3bc198928fcb5ce2308d9a933a58daf4f0b6d43f Mon Sep 17 00:00:00 2001 From: Adam Kondraciuk Date: Wed, 11 Jun 2025 13:37:19 +0200 Subject: [PATCH 2/2] [nrf fromtree] drivers: timer: nrf_grtc_timer: add last_count initialization The GRTC counter is not cleared at startup, therefore the `last_count` variable needs to be initialized accordingly. This change: - Prevents overflow of the `sys_clock_announce()` int32_t parameter - Ensures the correct uptime value, which should be reset during initialization Signed-off-by: Adam Kondraciuk (cherry picked from commit e77f942cff5516140f7db48bb42eda49522c1c7a) --- drivers/timer/nrf_grtc_timer.c | 8 ++++++++ include/zephyr/drivers/timer/nrf_grtc_timer.h | 11 +++++++++++ 2 files changed, 19 insertions(+) diff --git a/drivers/timer/nrf_grtc_timer.c b/drivers/timer/nrf_grtc_timer.c index 285e48a57fd..2809d8b3d60 100644 --- a/drivers/timer/nrf_grtc_timer.c +++ b/drivers/timer/nrf_grtc_timer.c @@ -66,6 +66,7 @@ static struct k_spinlock lock; static uint64_t last_count; /* Time (SYSCOUNTER value) @last sys_clock_announce() */ static atomic_t int_mask; static uint8_t ext_channels_allocated; +static uint64_t grtc_start_value; static nrfx_grtc_channel_t system_clock_channel_data = { .handler = sys_clock_timeout_handler, .p_context = NULL, @@ -358,6 +359,11 @@ int z_nrf_grtc_timer_capture_read(int32_t chan, uint64_t *captured_time) return 0; } +uint64_t z_nrf_grtc_timer_startup_value_get(void) +{ + return grtc_start_value; +} + #if defined(CONFIG_POWEROFF) && defined(CONFIG_NRF_GRTC_START_SYSCOUNTER) int z_nrf_grtc_wakeup_prepare(uint64_t wake_time_us) { @@ -485,6 +491,8 @@ static int sys_clock_driver_init(void) } #endif /* CONFIG_NRF_GRTC_START_SYSCOUNTER */ + last_count = (counter() / CYC_PER_TICK) * CYC_PER_TICK; + grtc_start_value = last_count; int_mask = NRFX_GRTC_CONFIG_ALLOWED_CC_CHANNELS_MASK; if (!IS_ENABLED(CONFIG_TICKLESS_KERNEL)) { system_timeout_set_relative(CYC_PER_TICK); diff --git a/include/zephyr/drivers/timer/nrf_grtc_timer.h b/include/zephyr/drivers/timer/nrf_grtc_timer.h index f8b69d7ddf0..5a51df21744 100644 --- a/include/zephyr/drivers/timer/nrf_grtc_timer.h +++ b/include/zephyr/drivers/timer/nrf_grtc_timer.h @@ -189,6 +189,17 @@ int z_nrf_grtc_timer_capture_read(int32_t chan, uint64_t *captured_time); */ int z_nrf_grtc_wakeup_prepare(uint64_t wake_time_us); +/** @brief Get the GRTC counter value latched at startup. + * + * @note The GRTC timer is not cleared by software at startup, + * while the system tick starts counting from zero. + * In some cases, it may be necessary to compare the system tick + * with the GRTC value — in such situations, this offset can be useful. + * + * @return GRTC value latched during system clock initialization. + */ +uint64_t z_nrf_grtc_timer_startup_value_get(void); + /** * @brief Initialize the GRTC clock timer driver from an application- * defined function.