Skip to content

Commit 2fe6b98

Browse files
committed
tests-mbed_hal-common_tickers: use low level ticker API for time measuring.
In `ticker speed` test case execution time of ticker API functions is measured using Timer object. Test replaces original ticker handler for testing purposes, so test should not relay on higher level ticker based features(like Timer). Use low level ticker API for time measuring.
1 parent 6eb66cc commit 2fe6b98

File tree

1 file changed

+43
-31
lines changed

1 file changed

+43
-31
lines changed

TESTS/mbed_hal/common_tickers/main.cpp

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ extern "C" {
3333
#error [NOT_SUPPORTED] test not supported
3434
#endif
3535

36+
#define US_PER_S 1000000
37+
3638
#define FORCE_OVERFLOW_TEST (false)
3739
#define TICKER_INT_VAL 500
3840
#define TICKER_DELTA 10
@@ -47,7 +49,7 @@ extern "C" {
4749

4850
#define MAX_FUNC_EXEC_TIME_US 20
4951
#define DELTA_FUNC_EXEC_TIME_US 5
50-
#define NUM_OF_CALLS 1000
52+
#define NUM_OF_CALLS 100
5153

5254
#define NUM_OF_CYCLES 100000
5355

@@ -150,6 +152,19 @@ void wait_cycles(volatile unsigned int cycles)
150152
while (cycles--);
151153
}
152154

155+
/* Auxiliary function to determine how long ticker function are executed.
156+
* This function returns number of us between <start_ticks> and <stop_ticks>
157+
* taking into account counter roll-over, counter size and frequency.
158+
*/
159+
uint32_t diff_us(uint32_t start_ticks, uint32_t stop_ticks, const ticker_info_t * info)
160+
{
161+
uint32_t counter_mask = ((1 << info->bits) - 1);
162+
163+
uint32_t diff_ticks = ((stop_ticks - start_ticks) & counter_mask);
164+
165+
return (uint32_t) ((uint64_t) diff_ticks * US_PER_S / info->frequency);
166+
}
167+
153168
/* Test that ticker_init can be called multiple times and
154169
* ticker_init allows the ticker to keep counting and disables the ticker interrupt.
155170
*/
@@ -420,85 +435,82 @@ void ticker_increment_test(void)
420435
/* Test that common ticker functions complete with the required amount of time. */
421436
void ticker_speed_test(void)
422437
{
423-
Timer timer;
424-
#if DEVICE_LPTICKER
425-
LowPowerTimer lptimer;
426-
#endif
427438
int counter = NUM_OF_CALLS;
439+
uint32_t start;
440+
uint32_t stop;
441+
442+
const ticker_info_t * us_ticker_info = get_us_ticker_data()->interface->get_info();
428443

429444
/* Free function will disable the ticker. For time measurement
430-
* we need to use other one.
445+
* we need to use other one if available.
431446
*/
432447
#if DEVICE_LPTICKER
448+
const ticker_info_t * lp_ticker_info = get_lp_ticker_data()->interface->get_info();
433449
bool us_ticker_test = (intf == get_us_ticker_data()->interface);
434-
Timer * free_timer = us_ticker_test ? &lptimer : &timer;
435450
#endif
436451

437452
/* ---- Test ticker_read function. ---- */
438-
timer.reset();
439-
timer.start();
453+
start = us_ticker_read();
440454
while (counter--) {
441455
intf->read();
442456
}
443-
timer.stop();
457+
stop = us_ticker_read();
444458

445-
TEST_ASSERT(timer.read_us() < (NUM_OF_CALLS * (MAX_FUNC_EXEC_TIME_US + DELTA_FUNC_EXEC_TIME_US)));
459+
TEST_ASSERT(diff_us(start, stop, us_ticker_info) < (NUM_OF_CALLS * (MAX_FUNC_EXEC_TIME_US + DELTA_FUNC_EXEC_TIME_US)));
446460

447461
/* ---- Test ticker_clear_interrupt function. ---- */
448462
counter = NUM_OF_CALLS;
449-
timer.reset();
450-
timer.start();
463+
start = us_ticker_read();
451464
while (counter--) {
452465
intf->clear_interrupt();
453466
}
454-
timer.stop();
467+
stop = us_ticker_read();
455468

456-
TEST_ASSERT(timer.read_us() < (NUM_OF_CALLS * (MAX_FUNC_EXEC_TIME_US + DELTA_FUNC_EXEC_TIME_US)));
469+
TEST_ASSERT(diff_us(start, stop, us_ticker_info) < (NUM_OF_CALLS * (MAX_FUNC_EXEC_TIME_US + DELTA_FUNC_EXEC_TIME_US)));
457470

458471
/* ---- Test ticker_set_interrupt function. ---- */
459472
counter = NUM_OF_CALLS;
460-
timer.reset();
461-
timer.start();
473+
start = us_ticker_read();
462474
while (counter--) {
463475
intf->set_interrupt(0);
464476
}
465-
timer.stop();
477+
stop = us_ticker_read();
466478

467-
TEST_ASSERT(timer.read_us() < (NUM_OF_CALLS * (MAX_FUNC_EXEC_TIME_US + DELTA_FUNC_EXEC_TIME_US)));
479+
TEST_ASSERT(diff_us(start, stop, us_ticker_info) < (NUM_OF_CALLS * (MAX_FUNC_EXEC_TIME_US + DELTA_FUNC_EXEC_TIME_US)));
468480

469481
/* ---- Test fire_interrupt function. ---- */
470482
counter = NUM_OF_CALLS;
471-
timer.reset();
472-
timer.start();
483+
start = us_ticker_read();
473484
while (counter--) {
474485
intf->fire_interrupt();
475486
}
476-
timer.stop();
487+
stop = us_ticker_read();
477488

478-
TEST_ASSERT(timer.read_us() < (NUM_OF_CALLS * (MAX_FUNC_EXEC_TIME_US + DELTA_FUNC_EXEC_TIME_US)));
489+
TEST_ASSERT(diff_us(start, stop, us_ticker_info) < (NUM_OF_CALLS * (MAX_FUNC_EXEC_TIME_US + DELTA_FUNC_EXEC_TIME_US)));
479490

480491
/* ---- Test disable_interrupt function. ---- */
481492
counter = NUM_OF_CALLS;
482-
timer.reset();
483-
timer.start();
493+
start = us_ticker_read();
484494
while (counter--) {
485495
intf->disable_interrupt();
486496
}
487-
timer.stop();
497+
stop = us_ticker_read();
488498

489-
TEST_ASSERT(timer.read_us() < (NUM_OF_CALLS * (MAX_FUNC_EXEC_TIME_US + DELTA_FUNC_EXEC_TIME_US)));
499+
TEST_ASSERT(diff_us(start, stop, us_ticker_info) < (NUM_OF_CALLS * (MAX_FUNC_EXEC_TIME_US + DELTA_FUNC_EXEC_TIME_US)));
490500

491501
/* ---- Test free function. ---- */
492502
#if DEVICE_LPTICKER
493503
counter = NUM_OF_CALLS;
494-
free_timer->reset();
495-
free_timer->start();
504+
if (us_ticker_test) {
505+
lp_ticker_init();
506+
}
507+
start = us_ticker_test ? lp_ticker_read() : us_ticker_read();
496508
while (counter--) {
497509
intf->free();
498510
}
499-
free_timer->stop();
511+
stop = us_ticker_test ? lp_ticker_read() : us_ticker_read();
500512

501-
TEST_ASSERT(free_timer->read_us() < (NUM_OF_CALLS * (MAX_FUNC_EXEC_TIME_US + DELTA_FUNC_EXEC_TIME_US)));
513+
TEST_ASSERT(diff_us(start, stop, us_ticker_info) < (NUM_OF_CALLS * (MAX_FUNC_EXEC_TIME_US + DELTA_FUNC_EXEC_TIME_US)));
502514
#endif
503515
}
504516

0 commit comments

Comments
 (0)