Skip to content

Commit

Permalink
drivers: timer: nrf_grtc_timer: Optimize z_nrf_grtc_timer_get_ticks
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
nordic-krch committed Jan 17, 2025
1 parent bba72e7 commit a55b494
Showing 1 changed file with 7 additions and 18 deletions.
25 changes: 7 additions & 18 deletions drivers/timer/nrf_grtc_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
((uint64_t)sys_clock_hw_cycles_per_sec() / (uint64_t)CONFIG_SYS_CLOCK_TICKS_PER_SEC)

#define COUNTER_SPAN (GRTC_SYSCOUNTERL_VALUE_Msk | ((uint64_t)GRTC_SYSCOUNTERH_VALUE_Msk << 32))
#define MAX_ABS_TICKS (COUNTER_SPAN / CYC_PER_TICK)

#define MAX_TICKS \
(((COUNTER_SPAN / CYC_PER_TICK) > INT_MAX) ? INT_MAX : (COUNTER_SPAN / CYC_PER_TICK))

Expand Down Expand Up @@ -286,31 +288,18 @@ void z_nrf_grtc_timer_abort(int32_t chan)

uint64_t z_nrf_grtc_timer_get_ticks(k_timeout_t t)
{
uint64_t curr_time;
int64_t curr_tick;
int64_t result;
int64_t abs_ticks;
int64_t grtc_ticks;

curr_time = counter();
curr_tick = sys_clock_tick_get();
int64_t abs_ticks = Z_TICK_ABS(t.ticks);

grtc_ticks = t.ticks * CYC_PER_TICK;
abs_ticks = Z_TICK_ABS(t.ticks);
if (abs_ticks < 0) {
int64_t grtc_ticks = t.ticks * CYC_PER_TICK;

/* relative timeout */
return (grtc_ticks > (int64_t)COUNTER_SPAN) ?
-EINVAL : (curr_time + grtc_ticks);
-EINVAL : (counter() + grtc_ticks);
}

/* absolute timeout */
result = (abs_ticks - curr_tick) * CYC_PER_TICK;

if (result > (int64_t)COUNTER_SPAN) {
return -EINVAL;
}

return curr_time + result;
return (abs_ticks > MAX_ABS_TICKS) ? -EINVAL : (abs_ticks * CYC_PER_TICK);
}

int z_nrf_grtc_timer_capture_prepare(int32_t chan)
Expand Down

0 comments on commit a55b494

Please sign in to comment.