Skip to content

Commit

Permalink
tests: drivers: timer: grtc: Fix GRTC test
Browse files Browse the repository at this point in the history
The `z_nrf_grtc_timer_get_ticks()` function converts system ticks
to GRTC ticks. It gets the current system tick to calculate an
absolute GRTC value. The same does the test function to provide
an argument to be converted. If the system tick occurs between those
`sys_clock_tick_get()` calls the `z_nrf_grtc_timer_get_ticks()` will
take into account the newer tick while the test estimate bases on
the old tick value. Due to that the maximum result error is 1 system
tick minus 1 GRTC tick which equals (`CYC_PER_TICK` - 1) for GRTC
ticks.

Signed-off-by: Adam Kondraciuk <adam.kondraciuk@nordicsemi.no>
  • Loading branch information
adamkondraciuk committed Apr 30, 2024
1 parent 88a50aa commit 5771342
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions tests/drivers/timer/nrf_grtc_timer/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,42 @@
#include <hal/nrf_grtc.h>

#define GRTC_SLEW_TICKS 10
#define NUMBER_OF_TRIES 2000
#define CYC_PER_TICK \
((uint64_t)sys_clock_hw_cycles_per_sec() / (uint64_t)CONFIG_SYS_CLOCK_TICKS_PER_SEC)

ZTEST(nrf_grtc_timer, test_get_ticks)
{
k_timeout_t t = K_MSEC(1);

uint64_t exp_ticks = z_nrf_grtc_timer_read() + t.ticks;
uint64_t exp_ticks = z_nrf_grtc_timer_read() + t.ticks * CYC_PER_TICK;
int64_t ticks;

/* Relative 1ms from now timeout converted to GRTC */
ticks = z_nrf_grtc_timer_get_ticks(t);
zassert_true((ticks >= exp_ticks) && (ticks <= (exp_ticks + GRTC_SLEW_TICKS)),
"Unexpected result %" PRId64 " (expected: %" PRId64 ")", ticks, exp_ticks);

/* Absolute timeout 1ms in the past */
t = Z_TIMEOUT_TICKS(Z_TICK_ABS(sys_clock_tick_get() - K_MSEC(1).ticks));

exp_ticks = z_nrf_grtc_timer_read() - K_MSEC(1).ticks;
ticks = z_nrf_grtc_timer_get_ticks(t);
zassert_true((ticks >= exp_ticks) && (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;
ticks = z_nrf_grtc_timer_get_ticks(t);
zassert_true((ticks >= exp_ticks) && (ticks <= (exp_ticks + GRTC_SLEW_TICKS)),
"Unexpected result %" PRId64 " (expected: %" PRId64 ")", ticks, exp_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));

exp_ticks = z_nrf_grtc_timer_read() - 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;
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);
}
}

ZTEST_SUITE(nrf_grtc_timer, NULL, NULL, NULL, NULL, NULL);

0 comments on commit 5771342

Please sign in to comment.