From 2b050c1951aaa29f9fc6f08d9ba7245d012dad44 Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Mon, 17 Aug 2026 05:17:41 +0200 Subject: [PATCH] bsps/microblaze: Fix the benchmark timer The timer is loaded with a value derived from the clock frequency and the tick interval, but the read used the literal reload value of one BSP variant and the frequency of the build time option. On a variant with another frequency every read returned the same wrong value and tmck never finished. Record the reload value and the frequency used by the initialization. Update #5699. Assisted-by: Claude:claude-opus-5 claude-code Signed-off-by: Sebastian Huber --- bsps/microblaze/shared/btimer/btimer.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/bsps/microblaze/shared/btimer/btimer.c b/bsps/microblaze/shared/btimer/btimer.c index 1595eec96d..3b4cfec4d7 100644 --- a/bsps/microblaze/shared/btimer/btimer.c +++ b/bsps/microblaze/shared/btimer/btimer.c @@ -35,6 +35,13 @@ static volatile Microblaze_Timer *mblaze_timer; bool benchmark_timer_find_average_overhead; +/* The value loaded into the down counter by benchmark_timer_initialize() */ +static uint32_t mblaze_timer_reload_value; + +/* The counter frequency in Hz used by benchmark_timer_initialize() */ +static uint32_t mblaze_timer_frequency_in_hz = + BSP_MICROBLAZE_FPGA_TIMER_FREQUENCY; + void benchmark_timer_initialize ( void ) { mblaze_timer = (volatile Microblaze_Timer *) try_get_prop_from_device_tree ( @@ -63,6 +70,9 @@ void benchmark_timer_initialize ( void ) uint32_t counter_ticks_per_clock_tick = ( counter_frequency_in_hz * us_per_tick ) / 1000000; + mblaze_timer_frequency_in_hz = counter_frequency_in_hz; + mblaze_timer_reload_value = counter_ticks_per_clock_tick; + /* Set a reset value for the timer counter */ mblaze_timer->tlr0 = counter_ticks_per_clock_tick; uint32_t control_status_reg = mblaze_timer->tcsr0; @@ -92,9 +102,15 @@ benchmark_timer_t benchmark_timer_read ( void ) uint32_t ticks; uint32_t total; - ticks = 1000000 - mblaze_timer->tcr0; + /* + * The counter runs down from the value loaded by + * benchmark_timer_initialize(), so the elapsed counter ticks are the + * difference to it. Both the reload value and the frequency depend on the + * BSP variant and on the configured clock tick interval. + */ + ticks = mblaze_timer_reload_value - mblaze_timer->tcr0; - total = ticks / ( BSP_MICROBLAZE_FPGA_TIMER_FREQUENCY / 1000000 ); + total = ticks / ( mblaze_timer_frequency_in_hz / 1000000 ); if ( benchmark_timer_find_average_overhead == true ) return total; /* in XXX microsecond units */