Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: drivers: timer: nrf_grtc: Fix test_get_ticks #83999

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
27 changes: 23 additions & 4 deletions tests/drivers/timer/nrf_grtc_timer/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,37 @@ ZTEST(nrf_grtc_timer, test_get_ticks)

for (uint32_t i = 0; i < NUMBER_OF_TRIES; i++) {
/* Absolute timeout 1ms in the past */
t = Z_TIMEOUT_TICKS(Z_TICK_ABS(sys_clock_tick_get() - K_MSEC(1).ticks));
uint64_t curr_tick;
uint64_t curr_grtc_tick;
uint64_t curr_tick2;

exp_ticks = z_nrf_grtc_timer_read() - K_MSEC(1).ticks * CYC_PER_TICK;
do {
/* GRTC and system tick must be read during single system tick. */
curr_tick = sys_clock_tick_get();
curr_grtc_tick = z_nrf_grtc_timer_read();
curr_tick2 = sys_clock_tick_get();
} while (curr_tick != curr_tick2);

t = Z_TIMEOUT_TICKS(Z_TICK_ABS(curr_tick - K_MSEC(1).ticks));

exp_ticks = curr_grtc_tick - K_MSEC(1).ticks * CYC_PER_TICK;
ticks = z_nrf_grtc_timer_get_ticks(t);

zassert_true((ticks >= (exp_ticks - CYC_PER_TICK + 1)) &&
(ticks <= (exp_ticks + GRTC_SLEW_TICKS)),
"Unexpected result %" PRId64 " (expected: %" PRId64 ")", ticks,
exp_ticks);

/* Absolute timeout 10ms in the future */
t = Z_TIMEOUT_TICKS(Z_TICK_ABS(sys_clock_tick_get() + K_MSEC(10).ticks));
exp_ticks = z_nrf_grtc_timer_read() + K_MSEC(10).ticks * CYC_PER_TICK;
do {
/* GRTC and system tick must be read during single system tick. */
curr_tick = sys_clock_tick_get();
curr_grtc_tick = z_nrf_grtc_timer_read();
curr_tick2 = sys_clock_tick_get();
} while (curr_tick != curr_tick2);

t = Z_TIMEOUT_TICKS(Z_TICK_ABS(curr_tick + K_MSEC(10).ticks));
exp_ticks = curr_grtc_tick + K_MSEC(10).ticks * CYC_PER_TICK;
ticks = z_nrf_grtc_timer_get_ticks(t);
zassert_true((ticks >= (exp_ticks - CYC_PER_TICK + 1)) &&
(ticks <= (exp_ticks + GRTC_SLEW_TICKS)),
Expand Down
Loading