Skip to content

Commit 314c3b7

Browse files
[nrf fromlist] 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 Upstream PR #: 91432 Signed-off-by: Adam Kondraciuk <[email protected]>
1 parent bcf554e commit 314c3b7

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

drivers/timer/nrf_grtc_timer.c

Lines changed: 8 additions & 1 deletion
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,
@@ -295,7 +296,6 @@ uint64_t z_nrf_grtc_timer_get_ticks(k_timeout_t t)
295296

296297
if (Z_IS_TIMEOUT_RELATIVE(t)) {
297298
int64_t grtc_ticks = t.ticks * CYC_PER_TICK;
298-
299299
return (grtc_ticks > (int64_t)COUNTER_SPAN) ?
300300
-EINVAL : (counter() + grtc_ticks);
301301
}
@@ -358,6 +358,11 @@ int z_nrf_grtc_timer_capture_read(int32_t chan, uint64_t *captured_time)
358358
return 0;
359359
}
360360

361+
uint64_t z_nrf_grtc_timer_startup_value_get(void)
362+
{
363+
return grtc_start_value;
364+
}
365+
361366
#if defined(CONFIG_POWEROFF) && defined(CONFIG_NRF_GRTC_START_SYSCOUNTER)
362367
int z_nrf_grtc_wakeup_prepare(uint64_t wake_time_us)
363368
{
@@ -485,6 +490,8 @@ static int sys_clock_driver_init(void)
485490
}
486491
#endif /* CONFIG_NRF_GRTC_START_SYSCOUNTER */
487492

493+
last_count = counter();
494+
grtc_start_value = last_count;
488495
int_mask = NRFX_GRTC_CONFIG_ALLOWED_CC_CHANNELS_MASK;
489496
if (!IS_ENABLED(CONFIG_TICKLESS_KERNEL)) {
490497
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)