Skip to content

Commit cafd03f

Browse files
[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 <adam.kondraciuk@nordicsemi.no> (cherry picked from commit e77f942cff5516140f7db48bb42eda49522c1c7a)
1 parent 982f3d5 commit cafd03f

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

drivers/timer/nrf_grtc_timer.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ static struct k_spinlock lock;
6666
static uint64_t last_count; /* Time (SYSCOUNTER value) @last sys_clock_announce() */
6767
static atomic_t int_mask;
6868
static uint8_t ext_channels_allocated;
69+
static uint64_t grtc_start_value;
6970
static nrfx_grtc_channel_t system_clock_channel_data = {
7071
.handler = sys_clock_timeout_handler,
7172
.p_context = NULL,
@@ -358,6 +359,11 @@ int z_nrf_grtc_timer_capture_read(int32_t chan, uint64_t *captured_time)
358359
return 0;
359360
}
360361

362+
uint64_t z_nrf_grtc_timer_startup_value_get(void)
363+
{
364+
return grtc_start_value;
365+
}
366+
361367
#if defined(CONFIG_POWEROFF) && defined(CONFIG_NRF_GRTC_START_SYSCOUNTER)
362368
int z_nrf_grtc_wakeup_prepare(uint64_t wake_time_us)
363369
{
@@ -485,6 +491,8 @@ static int sys_clock_driver_init(void)
485491
}
486492
#endif /* CONFIG_NRF_GRTC_START_SYSCOUNTER */
487493

494+
last_count = (counter() / CYC_PER_TICK) * CYC_PER_TICK;
495+
grtc_start_value = last_count;
488496
int_mask = NRFX_GRTC_CONFIG_ALLOWED_CC_CHANNELS_MASK;
489497
if (!IS_ENABLED(CONFIG_TICKLESS_KERNEL)) {
490498
system_timeout_set_relative(CYC_PER_TICK);

include/zephyr/drivers/timer/nrf_grtc_timer.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,17 @@ int z_nrf_grtc_timer_capture_read(int32_t chan, uint64_t *captured_time);
189189
*/
190190
int z_nrf_grtc_wakeup_prepare(uint64_t wake_time_us);
191191

192+
/** @brief Get the GRTC counter value latched at startup.
193+
*
194+
* @note The GRTC timer is not cleared by software at startup,
195+
* while the system tick starts counting from zero.
196+
* In some cases, it may be necessary to compare the system tick
197+
* with the GRTC value — in such situations, this offset can be useful.
198+
*
199+
* @return GRTC value latched during system clock initialization.
200+
*/
201+
uint64_t z_nrf_grtc_timer_startup_value_get(void);
202+
192203
/**
193204
* @brief Initialize the GRTC clock timer driver from an application-
194205
* defined function.

0 commit comments

Comments
 (0)