From bba72e7c1714bed2b9ce671b8fccb892d7c815bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Chru=C5=9Bci=C5=84ski?= Date: Wed, 15 Jan 2025 09:39:18 +0100 Subject: [PATCH 1/2] tests: drivers: timer: nrf_grtc: Fix test_get_ticks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Test was occasionally failing when two types of current time reading occurred at different system tick. In that case they were of by one system tick and test was failing. Signed-off-by: Krzysztof Chruściński --- tests/drivers/timer/nrf_grtc_timer/src/main.c | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/tests/drivers/timer/nrf_grtc_timer/src/main.c b/tests/drivers/timer/nrf_grtc_timer/src/main.c index 2fff9802d4936f..f53188841bdb05 100644 --- a/tests/drivers/timer/nrf_grtc_timer/src/main.c +++ b/tests/drivers/timer/nrf_grtc_timer/src/main.c @@ -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)), From a55b494dc8190290a2c8f7a203f5865ee63dd51d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Chru=C5=9Bci=C5=84ski?= Date: Wed, 15 Jan 2025 11:08:31 +0100 Subject: [PATCH 2/2] drivers: timer: nrf_grtc_timer: Optimize z_nrf_grtc_timer_get_ticks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- drivers/timer/nrf_grtc_timer.c | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/drivers/timer/nrf_grtc_timer.c b/drivers/timer/nrf_grtc_timer.c index ee3f35e09d0c78..5e4237c584cd2c 100644 --- a/drivers/timer/nrf_grtc_timer.c +++ b/drivers/timer/nrf_grtc_timer.c @@ -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)) @@ -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)