Skip to content

Commit e77f942

Browse files
adamkondraciukdanieldegrasse
authored andcommitted
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>
1 parent a3d37e3 commit e77f942

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
@@ -70,6 +70,7 @@ static struct k_spinlock lock;
7070
static uint64_t last_count; /* Time (SYSCOUNTER value) @last sys_clock_announce() */
7171
static atomic_t int_mask;
7272
static uint8_t ext_channels_allocated;
73+
static uint64_t grtc_start_value;
7374
static nrfx_grtc_channel_t system_clock_channel_data = {
7475
.handler = sys_clock_timeout_handler,
7576
.p_context = NULL,
@@ -362,6 +363,11 @@ int z_nrf_grtc_timer_capture_read(int32_t chan, uint64_t *captured_time)
362363
return 0;
363364
}
364365

366+
uint64_t z_nrf_grtc_timer_startup_value_get(void)
367+
{
368+
return grtc_start_value;
369+
}
370+
365371
#if defined(CONFIG_POWEROFF) && defined(CONFIG_NRF_GRTC_START_SYSCOUNTER)
366372
int z_nrf_grtc_wakeup_prepare(uint64_t wake_time_us)
367373
{
@@ -489,6 +495,8 @@ static int sys_clock_driver_init(void)
489495
}
490496
#endif /* CONFIG_NRF_GRTC_START_SYSCOUNTER */
491497

498+
last_count = (counter() / CYC_PER_TICK) * CYC_PER_TICK;
499+
grtc_start_value = last_count;
492500
int_mask = NRFX_GRTC_CONFIG_ALLOWED_CC_CHANNELS_MASK;
493501
if (!IS_ENABLED(CONFIG_TICKLESS_KERNEL)) {
494502
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)