Skip to content

Commit 40ab30f

Browse files
nordic-krchkartben
authored andcommitted
drivers: timer: nrf_grtc_timer: Optimize z_nrf_grtc_timer_get_ticks
Converting absolute system ticks to GRTC ticks is simple and algorithm can be simplified. Legacy algorithm was copied from nrf_rtc_timer which back then was working on 32 bits only. Signed-off-by: Krzysztof Chruściński <[email protected]>
1 parent 830a22e commit 40ab30f

File tree

1 file changed

+7
-18
lines changed

1 file changed

+7
-18
lines changed

drivers/timer/nrf_grtc_timer.c

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
((uint64_t)sys_clock_hw_cycles_per_sec() / (uint64_t)CONFIG_SYS_CLOCK_TICKS_PER_SEC)
4848

4949
#define COUNTER_SPAN (GRTC_SYSCOUNTERL_VALUE_Msk | ((uint64_t)GRTC_SYSCOUNTERH_VALUE_Msk << 32))
50+
#define MAX_ABS_TICKS (COUNTER_SPAN / CYC_PER_TICK)
51+
5052
#define MAX_TICKS \
5153
(((COUNTER_SPAN / CYC_PER_TICK) > INT_MAX) ? INT_MAX : (COUNTER_SPAN / CYC_PER_TICK))
5254

@@ -289,30 +291,17 @@ void z_nrf_grtc_timer_abort(int32_t chan)
289291

290292
uint64_t z_nrf_grtc_timer_get_ticks(k_timeout_t t)
291293
{
292-
uint64_t curr_time;
293-
int64_t curr_tick;
294-
int64_t result;
295-
int64_t abs_ticks;
296-
int64_t grtc_ticks;
297-
298-
curr_time = counter();
299-
curr_tick = sys_clock_tick_get();
294+
int64_t abs_ticks = Z_TICK_ABS(t.ticks);
300295

301-
grtc_ticks = t.ticks * CYC_PER_TICK;
302-
abs_ticks = Z_TICK_ABS(t.ticks);
303296
if (Z_IS_TIMEOUT_RELATIVE(t)) {
297+
int64_t grtc_ticks = t.ticks * CYC_PER_TICK;
298+
304299
return (grtc_ticks > (int64_t)COUNTER_SPAN) ?
305-
-EINVAL : (curr_time + grtc_ticks);
300+
-EINVAL : (counter() + grtc_ticks);
306301
}
307302

308303
/* absolute timeout */
309-
result = (abs_ticks - curr_tick) * CYC_PER_TICK;
310-
311-
if (result > (int64_t)COUNTER_SPAN) {
312-
return -EINVAL;
313-
}
314-
315-
return curr_time + result;
304+
return (abs_ticks > MAX_ABS_TICKS) ? -EINVAL : (abs_ticks * CYC_PER_TICK);
316305
}
317306

318307
int z_nrf_grtc_timer_capture_prepare(int32_t chan)

0 commit comments

Comments
 (0)