@@ -33,6 +33,8 @@ extern "C" {
33
33
#error [NOT_SUPPORTED] test not supported
34
34
#endif
35
35
36
+ #define US_PER_S 1000000
37
+
36
38
#define FORCE_OVERFLOW_TEST (false )
37
39
#define TICKER_INT_VAL 500
38
40
#define TICKER_DELTA 10
@@ -47,7 +49,7 @@ extern "C" {
47
49
48
50
#define MAX_FUNC_EXEC_TIME_US 20
49
51
#define DELTA_FUNC_EXEC_TIME_US 5
50
- #define NUM_OF_CALLS 1000
52
+ #define NUM_OF_CALLS 100
51
53
52
54
#define NUM_OF_CYCLES 100000
53
55
@@ -150,6 +152,19 @@ void wait_cycles(volatile unsigned int cycles)
150
152
while (cycles--);
151
153
}
152
154
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
+
153
168
/* Test that ticker_init can be called multiple times and
154
169
* ticker_init allows the ticker to keep counting and disables the ticker interrupt.
155
170
*/
@@ -420,85 +435,82 @@ void ticker_increment_test(void)
420
435
/* Test that common ticker functions complete with the required amount of time. */
421
436
void ticker_speed_test (void )
422
437
{
423
- Timer timer;
424
- #if DEVICE_LPTICKER
425
- LowPowerTimer lptimer;
426
- #endif
427
438
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 ();
428
443
429
444
/* 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 .
431
446
*/
432
447
#if DEVICE_LPTICKER
448
+ const ticker_info_t * lp_ticker_info = get_lp_ticker_data ()->interface ->get_info ();
433
449
bool us_ticker_test = (intf == get_us_ticker_data ()->interface );
434
- Timer * free_timer = us_ticker_test ? &lptimer : &timer;
435
450
#endif
436
451
437
452
/* ---- Test ticker_read function. ---- */
438
- timer.reset ();
439
- timer.start ();
453
+ start = us_ticker_read ();
440
454
while (counter--) {
441
455
intf->read ();
442
456
}
443
- timer. stop ();
457
+ stop = us_ticker_read ();
444
458
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)));
446
460
447
461
/* ---- Test ticker_clear_interrupt function. ---- */
448
462
counter = NUM_OF_CALLS;
449
- timer.reset ();
450
- timer.start ();
463
+ start = us_ticker_read ();
451
464
while (counter--) {
452
465
intf->clear_interrupt ();
453
466
}
454
- timer. stop ();
467
+ stop = us_ticker_read ();
455
468
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)));
457
470
458
471
/* ---- Test ticker_set_interrupt function. ---- */
459
472
counter = NUM_OF_CALLS;
460
- timer.reset ();
461
- timer.start ();
473
+ start = us_ticker_read ();
462
474
while (counter--) {
463
475
intf->set_interrupt (0 );
464
476
}
465
- timer. stop ();
477
+ stop = us_ticker_read ();
466
478
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)));
468
480
469
481
/* ---- Test fire_interrupt function. ---- */
470
482
counter = NUM_OF_CALLS;
471
- timer.reset ();
472
- timer.start ();
483
+ start = us_ticker_read ();
473
484
while (counter--) {
474
485
intf->fire_interrupt ();
475
486
}
476
- timer. stop ();
487
+ stop = us_ticker_read ();
477
488
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)));
479
490
480
491
/* ---- Test disable_interrupt function. ---- */
481
492
counter = NUM_OF_CALLS;
482
- timer.reset ();
483
- timer.start ();
493
+ start = us_ticker_read ();
484
494
while (counter--) {
485
495
intf->disable_interrupt ();
486
496
}
487
- timer. stop ();
497
+ stop = us_ticker_read ();
488
498
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)));
490
500
491
501
/* ---- Test free function. ---- */
492
502
#if DEVICE_LPTICKER
493
503
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 ();
496
508
while (counter--) {
497
509
intf->free ();
498
510
}
499
- free_timer-> stop ();
511
+ stop = us_ticker_test ? lp_ticker_read () : us_ticker_read ();
500
512
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)));
502
514
#endif
503
515
}
504
516
0 commit comments