Skip to content

Commit c9ecc9b

Browse files
mprse0xc0170
authored andcommitted
lp_us_tickers test: provide minor fixes after review
- count_ticks: fix counter overflow handling, - count_ticks: use reg_cycles variable in while loop, - increment test: reduce number of cycles for slow cores if measure process needs to be repeated (difference is greater than 1).
1 parent 48cae05 commit c9ecc9b

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

TESTS/mbed_hal/common_tickers/main.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040

4141
#define NUM_OF_CYCLES 100000
4242

43+
#define US_TICKER_OV_LIMIT 20000
44+
#define LP_TICKER_OV_LIMIT 4000
45+
4346
using namespace utest::v1;
4447

4548
volatile int intFlag = 0;
@@ -59,7 +62,7 @@ uint32_t count_ticks(uint32_t cycles, uint32_t step)
5962

6063
const uint32_t start = intf->read();
6164

62-
while (cycles -= step) {
65+
while (reg_cycles -= step) {
6366
/* Just wait. */
6467
}
6568

@@ -68,7 +71,7 @@ uint32_t count_ticks(uint32_t cycles, uint32_t step)
6871
core_util_critical_section_exit();
6972

7073
/* Handle overflow - overflow protection may not work in this case. */
71-
uint32_t diff = (start <= stop) ? (stop - start) : (uint32_t)(max_count - start + stop);
74+
uint32_t diff = (start <= stop) ? (stop - start) : (uint32_t)(max_count - start + stop + 1);
7275

7376
return (diff);
7477
}
@@ -338,22 +341,27 @@ void ticker_increment_test(void)
338341

339342
TEST_ASSERT_UINT32_WITHIN(1, next_tick_count, base_tick_count);
340343
} else { // high frequency tickers
341-
const uint32_t base_tick_count = count_ticks(NUM_OF_CYCLES, 1);
344+
345+
uint32_t num_of_cycles = NUM_OF_CYCLES;
346+
347+
const uint32_t base_tick_count = count_ticks(num_of_cycles, 1);
342348
uint32_t next_tick_count = base_tick_count;
343349
uint32_t inc_val = 0;
344350

345351
while (inc_val < 100) {
346-
next_tick_count = count_ticks(NUM_OF_CYCLES + inc_val, 1);
352+
next_tick_count = count_ticks(num_of_cycles + inc_val, 1);
347353

348354
if (next_tick_count == base_tick_count) {
349355
/* Same tick count, so increase num of cycles. */
350356
inc_val++;
351357
} else {
352358
/* It is possible that the difference between base and next
353359
* tick count on some platforms is greater that 1, in this case we need
354-
* to repeat counting with the same number of cycles.
360+
* to repeat counting with the reduced number of cycles (for slower boards).
355361
* In cases if difference is exactly 1 we can exit the loop.
356362
*/
363+
num_of_cycles /= 2;
364+
357365
if (next_tick_count - base_tick_count == 1 ||
358366
base_tick_count - next_tick_count == 1) {
359367
break;

0 commit comments

Comments
 (0)