Skip to content

Commit 8a213fe

Browse files
committed
lp_us_tickers test - fix count_ticks() function to give the same results on all compilers
count_ticks() function counts ticker ticks elapsed during execution of N cycles of empty while loop. In current version N value (cycles) is given as volatile paramater in order to disable possible compiler optimalisation. There was a problem with measured time on different compilers and additionally results on ARM compiler were unexpected (the difference beetween measured elapsed ticks for the same number of cycles was to large). This might be caused by the memory access in order to store updated variable in memory. To fix this issue given numer of cycles has been stored into register and register is decremented (no memory access). With this fix count_ticks(NUM_OF_CYCLES, 1) call returns 2500 +/-1 for us ticker ticks using each compiler (K64F).
1 parent 46d4d6d commit 8a213fe

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

TESTS/mbed_hal/lp_us_tickers/main.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ unsigned int ticker_overflow_delta;
4949

5050
/* Auxiliary function to count ticker ticks elapsed during execution of N cycles of empty while loop.
5151
* Parameter <step> is used to disable compiler optimisation. */
52-
uint32_t count_ticks(volatile uint32_t cycles, uint32_t step)
52+
uint32_t count_ticks(uint32_t cycles, uint32_t step)
5353
{
54+
register uint32_t reg_cycles = cycles;
55+
5456
core_util_critical_section_enter();
5557

5658
const uint32_t start = intf->read();

0 commit comments

Comments
 (0)