tests: drivers: timer: grtc: Fix GRTC test

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>
This commit is contained in:
Adam Kondraciuk 2024-02-01 09:46:02 +01:00 committed by Henrik Brix Andersen
commit 2cd2d5fef3
3 changed files with 26 additions and 14 deletions

View file

@ -54,7 +54,7 @@ config NRF_GRTC_TIMER_AUTO_KEEP_ALIVE
bool
default y if NRF_GRTC_START_SYSCOUNTER
help
This feature prevents the SYSCOUNTER to sleep when any core is in
This feature prevents the SYSCOUNTER from sleeping when any core is in
active state.
endif # NRF_GRTC_TIMER

View file

@ -8,12 +8,15 @@
#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 */
@ -21,20 +24,28 @@ ZTEST(nrf_grtc_timer, test_get_ticks)
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));
k_msleep(1);
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);
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));
/* 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);
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);

View file

@ -4,5 +4,6 @@ tests:
platform_allow:
- nrf54l15pdk/nrf54l15/cpuapp
- nrf54l15pdk/nrf54l15/cpuflpr
- nrf54l15bsim/nrf54l15/cpuapp
- nrf54h20dk/nrf54h20/cpuapp
- nrf54h20dk/nrf54h20/cpurad