@@ -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
@@ -43,10 +45,11 @@ extern "C" {
43
45
#define US_TICKER_OVERFLOW_DELTA2 60
44
46
45
47
#define TICKER_100_TICKS 100
48
+ #define TICKER_500_TICKS 500
46
49
47
50
#define MAX_FUNC_EXEC_TIME_US 20
48
51
#define DELTA_FUNC_EXEC_TIME_US 5
49
- #define NUM_OF_CALLS 1000
52
+ #define NUM_OF_CALLS 100
50
53
51
54
#define NUM_OF_CYCLES 100000
52
55
@@ -149,6 +152,19 @@ void wait_cycles(volatile unsigned int cycles)
149
152
while (cycles--);
150
153
}
151
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
+
152
168
/* Test that ticker_init can be called multiple times and
153
169
* ticker_init allows the ticker to keep counting and disables the ticker interrupt.
154
170
*/
@@ -419,64 +435,134 @@ void ticker_increment_test(void)
419
435
/* Test that common ticker functions complete with the required amount of time. */
420
436
void ticker_speed_test (void )
421
437
{
422
- Timer timer;
423
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 ();
443
+
444
+ /* Free function will disable the ticker. For time measurement
445
+ * we need to use other one if available.
446
+ */
447
+ #if DEVICE_LPTICKER
448
+ const ticker_info_t * lp_ticker_info = get_lp_ticker_data ()->interface ->get_info ();
449
+ bool us_ticker_test = (intf == get_us_ticker_data ()->interface );
450
+ #endif
424
451
425
452
/* ---- Test ticker_read function. ---- */
426
- timer.reset ();
427
- timer.start ();
453
+ start = us_ticker_read ();
428
454
while (counter--) {
429
455
intf->read ();
430
456
}
431
- timer. stop ();
457
+ stop = us_ticker_read ();
432
458
433
- 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)));
434
460
435
461
/* ---- Test ticker_clear_interrupt function. ---- */
436
462
counter = NUM_OF_CALLS;
437
- timer.reset ();
438
- timer.start ();
463
+ start = us_ticker_read ();
439
464
while (counter--) {
440
465
intf->clear_interrupt ();
441
466
}
442
- timer. stop ();
467
+ stop = us_ticker_read ();
443
468
444
- 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)));
445
470
446
471
/* ---- Test ticker_set_interrupt function. ---- */
447
472
counter = NUM_OF_CALLS;
448
- timer.reset ();
449
- timer.start ();
473
+ start = us_ticker_read ();
450
474
while (counter--) {
451
475
intf->set_interrupt (0 );
452
476
}
453
- timer. stop ();
477
+ stop = us_ticker_read ();
454
478
455
- 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)));
456
480
457
481
/* ---- Test fire_interrupt function. ---- */
458
482
counter = NUM_OF_CALLS;
459
- timer.reset ();
460
- timer.start ();
483
+ start = us_ticker_read ();
461
484
while (counter--) {
462
485
intf->fire_interrupt ();
463
486
}
464
- timer. stop ();
487
+ stop = us_ticker_read ();
465
488
466
- 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)));
467
490
468
491
/* ---- Test disable_interrupt function. ---- */
469
492
counter = NUM_OF_CALLS;
470
- timer.reset ();
471
- timer.start ();
493
+ start = us_ticker_read ();
472
494
while (counter--) {
473
495
intf->disable_interrupt ();
474
496
}
475
- timer.stop ();
497
+ stop = us_ticker_read ();
498
+
499
+ TEST_ASSERT (diff_us (start, stop, us_ticker_info) < (NUM_OF_CALLS * (MAX_FUNC_EXEC_TIME_US + DELTA_FUNC_EXEC_TIME_US)));
500
+
501
+ /* ---- Test free function. ---- */
502
+ #if DEVICE_LPTICKER
503
+ counter = NUM_OF_CALLS;
504
+ if (us_ticker_test) {
505
+ lp_ticker_init ();
506
+ }
507
+ start = us_ticker_test ? lp_ticker_read () : us_ticker_read ();
508
+ while (counter--) {
509
+ intf->free ();
510
+ }
511
+ stop = us_ticker_test ? lp_ticker_read () : us_ticker_read ();
512
+
513
+ TEST_ASSERT (diff_us (start, stop, us_ticker_info) < (NUM_OF_CALLS * (MAX_FUNC_EXEC_TIME_US + DELTA_FUNC_EXEC_TIME_US)));
514
+ #endif
515
+ }
516
+
517
+ /* Test that ticker_free disables ticker interrupt. */
518
+ void ticker_free_interrupt_test (void )
519
+ {
520
+ overflow_protect ();
521
+
522
+ uint32_t cycles_500_ticks = 50 ;
523
+ uint32_t reference_ticks_count = 0 ;
524
+
525
+ while (reference_ticks_count < TICKER_500_TICKS) {
526
+ cycles_500_ticks *= 2 ;
527
+ const uint32_t start = intf->read ();
528
+ wait_cycles (cycles_500_ticks);
529
+ reference_ticks_count = intf->read () - start;
530
+ }
531
+
532
+ intFlag = 0 ;
533
+
534
+ intf->set_interrupt (intf->read () + (TICKER_500_TICKS / 2 ));
535
+ intf->free ();
536
+ wait_cycles (cycles_500_ticks);
537
+ intf->init ();
538
+ TEST_ASSERT_EQUAL (0 , intFlag);
539
+ }
540
+
541
+ /* Test that ticker can be successfully re-initialized after free(). */
542
+ void ticker_init_free_test (void )
543
+ {
544
+ intf->free ();
545
+ intf->init ();
546
+
547
+ overflow_protect ();
548
+
549
+ intFlag = 0 ;
550
+
551
+ const uint32_t tick_count = intf->read ();
552
+
553
+ intf->set_interrupt (intf->read () + TICKER_INT_VAL);
554
+
555
+ while (intf->read () < (tick_count + TICKER_INT_VAL - TICKER_DELTA)) {
556
+ TEST_ASSERT_EQUAL_INT_MESSAGE (0 , intFlag, " Interrupt fired too early" );
557
+ }
558
+
559
+ while (intf->read () < (tick_count + TICKER_INT_VAL + TICKER_DELTA)) {
560
+ }
476
561
477
- TEST_ASSERT (timer. read_us () < (NUM_OF_CALLS * (MAX_FUNC_EXEC_TIME_US + DELTA_FUNC_EXEC_TIME_US)) );
562
+ TEST_ASSERT_EQUAL ( 1 , intFlag );
478
563
}
479
564
565
+
480
566
utest::v1::status_t us_ticker_setup (const Case *const source, const size_t index_of_case)
481
567
{
482
568
intf = get_us_ticker_data ()->interface ;
@@ -551,6 +637,8 @@ Case cases[] = {
551
637
Case (" Microsecond ticker overflow test" , us_ticker_setup, ticker_overflow_test, us_ticker_teardown),
552
638
Case (" Microsecond ticker increment test" , us_ticker_setup, ticker_increment_test, us_ticker_teardown),
553
639
Case (" Microsecond ticker speed test" , us_ticker_setup, ticker_speed_test, us_ticker_teardown),
640
+ Case (" Microsecond ticker free interrupt test" , us_ticker_setup, ticker_free_interrupt_test, us_ticker_teardown),
641
+ Case (" Microsecond re-init after free test" , us_ticker_setup, ticker_init_free_test, us_ticker_teardown),
554
642
#if DEVICE_LPTICKER
555
643
Case (" lp ticker init is safe to call repeatedly" , lp_ticker_setup, ticker_init_test, lp_ticker_teardown),
556
644
Case (" lp ticker info test" , lp_ticker_setup, ticker_info_test, lp_ticker_teardown),
@@ -561,6 +649,8 @@ Case cases[] = {
561
649
Case (" lp ticker overflow test" , lp_ticker_setup, ticker_overflow_test, lp_ticker_teardown),
562
650
Case (" lp ticker increment test" , lp_ticker_setup, ticker_increment_test, lp_ticker_teardown),
563
651
Case (" lp ticker speed test" , lp_ticker_setup, ticker_speed_test, lp_ticker_teardown),
652
+ Case (" lp ticker free interrupt test" , lp_ticker_setup, ticker_free_interrupt_test, lp_ticker_teardown),
653
+ Case (" lp ticker re-init after free test" , lp_ticker_setup, ticker_init_free_test, lp_ticker_teardown),
564
654
#endif
565
655
};
566
656
0 commit comments