Skip to content

Commit 4d7167c

Browse files
mprse0xc0170
authored andcommitted
lp_us_tickers test: fix increment test for slow boards
Increment test proves that ticker counter is incremented by one. This is done indirectly for high frequency counters where it is impossible to read 'value' and 'value + 1' in two successive ticker reads. This check is done indirectly by counting ticker ticks elapsed during execution of N cycles of empty while loop. Unfortunately on slow boards with fast tickers like NRF51_DK(16 MHz CPU/1MHz hf ticker) it is possible that for the same N cycles measured number of elapsed ticks in two successive calls is greater than 1. This patch provides fix for such case - measure operation is repeated with the same number of cycles.
1 parent 6daa7fc commit 4d7167c

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

TESTS/mbed_hal/common_tickers/main.cpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ volatile int intFlag = 0;
4646
const ticker_interface_t* intf;
4747
unsigned int ticker_overflow_delta;
4848

49-
5049
/* Auxiliary function to count ticker ticks elapsed during execution of N cycles of empty while loop.
5150
* Parameter <step> is used to disable compiler optimisation. */
5251
uint32_t count_ticks(uint32_t cycles, uint32_t step)
5352
{
5453
register uint32_t reg_cycles = cycles;
5554

55+
const ticker_info_t* p_ticker_info = intf->get_info();
56+
const uint32_t max_count = ((1 << p_ticker_info->bits) - 1);
57+
5658
core_util_critical_section_enter();
5759

5860
const uint32_t start = intf->read();
@@ -65,7 +67,10 @@ uint32_t count_ticks(uint32_t cycles, uint32_t step)
6567

6668
core_util_critical_section_exit();
6769

68-
return (stop - start);
70+
/* Handle overflow - overflow protection may not work in this case. */
71+
uint32_t diff = (start <= stop) ? (stop - start) : (uint32_t)(max_count - start + stop);
72+
73+
return (diff);
6974
}
7075

7176
void ticker_event_handler_stub(const ticker_data_t * const ticker)
@@ -302,9 +307,23 @@ void ticker_increment_test(void)
302307
uint32_t next_tick_count = base_tick_count;
303308
uint32_t inc_val = 0;
304309

305-
while (next_tick_count == base_tick_count) {
310+
while (inc_val < 100) {
306311
next_tick_count = count_ticks(NUM_OF_CYCLES + inc_val, 1);
307-
inc_val++;
312+
313+
if (next_tick_count == base_tick_count) {
314+
/* Same tick count, so increase num of cycles. */
315+
inc_val++;
316+
} else {
317+
/* It is possible that the difference between base and next
318+
* tick count on some platforms is greater that 1, in this case we need
319+
* to repeat counting with the same number of cycles.
320+
* In cases if difference is exactly 1 we can exit the loop.
321+
*/
322+
if (next_tick_count - base_tick_count == 1 ||
323+
base_tick_count - next_tick_count == 1) {
324+
break;
325+
}
326+
}
308327
}
309328

310329
/* Since we are here we know that next_tick_count != base_tick_count.

0 commit comments

Comments
 (0)