@@ -30,35 +30,24 @@ using namespace utest::v1;
30
30
31
31
extern uint32_t SystemCoreClock;
32
32
33
- /* Macro to define delta based on CPU clock frequency.
34
- *
35
- * Note that some extra time is counted by the timer.
36
- * Additional time is caused by the function calls and
37
- * additional operations performed by wait and
38
- * stop functions before in fact timer is stopped. This may
39
- * add additional time to the counted result.
40
- *
41
- * To take in to account this extra time we introduce DELTA
42
- * value based on CPU clock (speed):
43
- * DELTA = TOLERANCE_FACTOR / SystemCoreClock * US_FACTOR
44
- *
45
- * e.g.
46
- * For K64F DELTA = (30000 / 120000000) * 1000000 = 250[us]
47
- * For NUCLEO_F070RB DELTA = (30000 / 48000000) * 1000000 = 625[us]
48
- * For NRF51_DK DELTA = (30000 / 16000000) * 1000000 = 1875[us]
49
- */
50
33
#define US_PER_SEC 1000000
51
34
#define US_PER_MSEC 1000
52
- #define TOLERANCE_FACTOR 30000 .0f
53
- #define US_FACTOR 1000000 .0f
35
+ #define MSEC_PER_SEC 1000
54
36
55
- static const int delta_sys_clk_us = ((int ) (TOLERANCE_FACTOR / (float )SystemCoreClock * US_FACTOR));
56
-
57
- /* When test performs time measurement using Timer in sequence, then measurement error accumulates
58
- * in the successive attempts. */
59
- #define DELTA_US (i ) (delta_sys_clk_us * i)
60
- #define DELTA_S (i ) ((float )delta_sys_clk_us * i / US_PER_SEC)
61
- #define DELTA_MS (i ) (1 + ( (i * delta_sys_clk_us) / US_PER_MSEC))
37
+ /*
38
+ * Define tolerance as follows:
39
+ * tolerance = 500 us + 2% of measured time
40
+ *
41
+ * e.g.
42
+ * 1 ms delay: tolerance = 520 us
43
+ * 10 ms delay: tolerance = 700 us
44
+ * 100 ms delay: tolerance = 2500 us
45
+ * 1000 ms delay: tolerance = 20500 us
46
+ *
47
+ * */
48
+ #define DELTA_US (delay_ms ) (500 + delay_ms * US_PER_MSEC / 50 )
49
+ #define DELTA_MS (delay_ms ) (1 + (delay_ms * US_PER_MSEC / 50 / US_PER_MSEC))
50
+ #define DELTA_S (delay_ms ) (0 .000500f + (((float )delay_ms) / MSEC_PER_SEC / 50 ))
62
51
63
52
#define TICKER_FREQ_1MHZ 1000000
64
53
#define TICKER_BITS 32
@@ -417,10 +406,10 @@ void test_timer_time_accumulation_os_ticker()
417
406
p_timer->stop ();
418
407
419
408
/* Check results - totally 10 ms have elapsed. */
420
- TEST_ASSERT_FLOAT_WITHIN (DELTA_S (1 ), 0 .010f , p_timer->read ());
421
- TEST_ASSERT_INT32_WITHIN (DELTA_MS (1 ), 10 , p_timer->read_ms ());
422
- TEST_ASSERT_INT32_WITHIN (DELTA_US (1 ), 10000 , p_timer->read_us ());
423
- TEST_ASSERT_UINT64_WITHIN (DELTA_US (1 ), 10000 , p_timer->read_high_resolution_us ());
409
+ TEST_ASSERT_FLOAT_WITHIN (DELTA_S (10 ), 0 .010f , p_timer->read ());
410
+ TEST_ASSERT_INT32_WITHIN (DELTA_MS (10 ), 10 , p_timer->read_ms ());
411
+ TEST_ASSERT_INT32_WITHIN (DELTA_US (10 ), 10000 , p_timer->read_us ());
412
+ TEST_ASSERT_UINT64_WITHIN (DELTA_US (10 ), 10000 , p_timer->read_high_resolution_us ());
424
413
425
414
/* Wait 50 ms - this is done to show that time elapsed when
426
415
* the timer is stopped does not have influence on the
@@ -439,10 +428,10 @@ void test_timer_time_accumulation_os_ticker()
439
428
p_timer->stop ();
440
429
441
430
/* Check results - totally 30 ms have elapsed. */
442
- TEST_ASSERT_FLOAT_WITHIN (DELTA_S (2 ), 0 .030f , p_timer->read ());
443
- TEST_ASSERT_INT32_WITHIN (DELTA_MS (2 ), 30 , p_timer->read_ms ());
444
- TEST_ASSERT_INT32_WITHIN (DELTA_US (2 ), 30000 , p_timer->read_us ());
445
- TEST_ASSERT_UINT64_WITHIN (DELTA_US (2 ), 30000 , p_timer->read_high_resolution_us ());
431
+ TEST_ASSERT_FLOAT_WITHIN (DELTA_S (30 ), 0 .030f , p_timer->read ());
432
+ TEST_ASSERT_INT32_WITHIN (DELTA_MS (30 ), 30 , p_timer->read_ms ());
433
+ TEST_ASSERT_INT32_WITHIN (DELTA_US (30 ), 30000 , p_timer->read_us ());
434
+ TEST_ASSERT_UINT64_WITHIN (DELTA_US (30 ), 30000 , p_timer->read_high_resolution_us ());
446
435
447
436
/* Wait 50 ms - this is done to show that time elapsed when
448
437
* the timer is stopped does not have influence on the
@@ -460,10 +449,10 @@ void test_timer_time_accumulation_os_ticker()
460
449
p_timer->stop ();
461
450
462
451
/* Check results - totally 60 ms have elapsed. */
463
- TEST_ASSERT_FLOAT_WITHIN (DELTA_S (3 ), 0 .060f , p_timer->read ());
464
- TEST_ASSERT_INT32_WITHIN (DELTA_MS (3 ), 60 , p_timer->read_ms ());
465
- TEST_ASSERT_INT32_WITHIN (DELTA_US (3 ), 60000 , p_timer->read_us ());
466
- TEST_ASSERT_UINT64_WITHIN (DELTA_US (3 ), 60000 , p_timer->read_high_resolution_us ());
452
+ TEST_ASSERT_FLOAT_WITHIN (DELTA_S (60 ), 0 .060f , p_timer->read ());
453
+ TEST_ASSERT_INT32_WITHIN (DELTA_MS (60 ), 60 , p_timer->read_ms ());
454
+ TEST_ASSERT_INT32_WITHIN (DELTA_US (60 ), 60000 , p_timer->read_us ());
455
+ TEST_ASSERT_UINT64_WITHIN (DELTA_US (60 ), 60000 , p_timer->read_high_resolution_us ());
467
456
468
457
/* Wait 50 ms - this is done to show that time elapsed when
469
458
* the timer is stopped does not have influence on the
@@ -482,10 +471,10 @@ void test_timer_time_accumulation_os_ticker()
482
471
p_timer->stop ();
483
472
484
473
/* Check results - totally 1060 ms have elapsed. */
485
- TEST_ASSERT_FLOAT_WITHIN (DELTA_S (4 ), 1 .060f , p_timer->read ());
486
- TEST_ASSERT_INT32_WITHIN (DELTA_MS (4 ), 1060 , p_timer->read_ms ());
487
- TEST_ASSERT_INT32_WITHIN (DELTA_US (4 ), 1060000 , p_timer->read_us ());
488
- TEST_ASSERT_UINT64_WITHIN (DELTA_US (4 ), 1060000 , p_timer->read_high_resolution_us ());
474
+ TEST_ASSERT_FLOAT_WITHIN (DELTA_S (1060 ), 1 .060f , p_timer->read ());
475
+ TEST_ASSERT_INT32_WITHIN (DELTA_MS (1060 ), 1060 , p_timer->read_ms ());
476
+ TEST_ASSERT_INT32_WITHIN (DELTA_US (1060 ), 1060000 , p_timer->read_us ());
477
+ TEST_ASSERT_UINT64_WITHIN (DELTA_US (1060 ), 1060000 , p_timer->read_high_resolution_us ());
489
478
}
490
479
491
480
/* This test verifies if reset() function resets the timer
@@ -511,10 +500,10 @@ void test_timer_reset_os_ticker()
511
500
p_timer->stop ();
512
501
513
502
/* Check results - totally 10 ms elapsed. */
514
- TEST_ASSERT_FLOAT_WITHIN (DELTA_S (1 ), 0 .010f , p_timer->read ());
515
- TEST_ASSERT_INT32_WITHIN (DELTA_MS (1 ), 10 , p_timer->read_ms ());
516
- TEST_ASSERT_INT32_WITHIN (DELTA_US (1 ), 10000 , p_timer->read_us ());
517
- TEST_ASSERT_UINT64_WITHIN (DELTA_US (1 ), 10000 , p_timer->read_high_resolution_us ());
503
+ TEST_ASSERT_FLOAT_WITHIN (DELTA_S (10 ), 0 .010f , p_timer->read ());
504
+ TEST_ASSERT_INT32_WITHIN (DELTA_MS (10 ), 10 , p_timer->read_ms ());
505
+ TEST_ASSERT_INT32_WITHIN (DELTA_US (10 ), 10000 , p_timer->read_us ());
506
+ TEST_ASSERT_UINT64_WITHIN (DELTA_US (10 ), 10000 , p_timer->read_high_resolution_us ());
518
507
519
508
/* Reset the timer - previous measured time should be lost now. */
520
509
p_timer->reset ();
@@ -529,10 +518,10 @@ void test_timer_reset_os_ticker()
529
518
p_timer->stop ();
530
519
531
520
/* Check results - 20 ms elapsed since the reset. */
532
- TEST_ASSERT_FLOAT_WITHIN (DELTA_S (1 ), 0 .020f , p_timer->read ());
533
- TEST_ASSERT_INT32_WITHIN (DELTA_MS (1 ), 20 , p_timer->read_ms ());
534
- TEST_ASSERT_INT32_WITHIN (DELTA_US (1 ), 20000 , p_timer->read_us ());
535
- TEST_ASSERT_UINT64_WITHIN (DELTA_US (1 ), 20000 , p_timer->read_high_resolution_us ());
521
+ TEST_ASSERT_FLOAT_WITHIN (DELTA_S (20 ), 0 .020f , p_timer->read ());
522
+ TEST_ASSERT_INT32_WITHIN (DELTA_MS (20 ), 20 , p_timer->read_ms ());
523
+ TEST_ASSERT_INT32_WITHIN (DELTA_US (20 ), 20000 , p_timer->read_us ());
524
+ TEST_ASSERT_UINT64_WITHIN (DELTA_US (20 ), 20000 , p_timer->read_high_resolution_us ());
536
525
}
537
526
538
527
/* This test verifies if reset() function resets the timer
@@ -613,10 +602,10 @@ void test_timer_start_started_timer_os_ticker()
613
602
p_timer->stop ();
614
603
615
604
/* Check results - 30 ms have elapsed since the first start. */
616
- TEST_ASSERT_FLOAT_WITHIN (DELTA_S (2 ), 0 .030f , p_timer->read ());
617
- TEST_ASSERT_INT32_WITHIN (DELTA_MS (2 ), 30 , p_timer->read_ms ());
618
- TEST_ASSERT_INT32_WITHIN (DELTA_US (2 ), 30000 , p_timer->read_us ());
619
- TEST_ASSERT_UINT64_WITHIN (DELTA_US (2 ), 30000 , p_timer->read_high_resolution_us ());
605
+ TEST_ASSERT_FLOAT_WITHIN (DELTA_S (30 ), 0 .030f , p_timer->read ());
606
+ TEST_ASSERT_INT32_WITHIN (DELTA_MS (30 ), 30 , p_timer->read_ms ());
607
+ TEST_ASSERT_INT32_WITHIN (DELTA_US (30 ), 30000 , p_timer->read_us ());
608
+ TEST_ASSERT_UINT64_WITHIN (DELTA_US (30 ), 30000 , p_timer->read_high_resolution_us ());
620
609
}
621
610
622
611
/* This test verifies if calling start() for already
@@ -650,10 +639,10 @@ void test_timer_start_started_timer_user_ticker()
650
639
p_timer->stop ();
651
640
652
641
/* Check results - 30 ms have elapsed since the first start. */
653
- TEST_ASSERT_FLOAT_WITHIN (DELTA_S (2 ), 0 .030f , p_timer->read ());
654
- TEST_ASSERT_INT32_WITHIN (DELTA_MS (2 ), 30 , p_timer->read_ms ());
655
- TEST_ASSERT_INT32_WITHIN (DELTA_US (2 ), 30000 , p_timer->read_us ());
656
- TEST_ASSERT_UINT64_WITHIN (DELTA_US (2 ), 30000 , p_timer->read_high_resolution_us ());
642
+ TEST_ASSERT_FLOAT_WITHIN (DELTA_S (30 ), 0 .030f , p_timer->read ());
643
+ TEST_ASSERT_INT32_WITHIN (DELTA_MS (30 ), 30 , p_timer->read_ms ());
644
+ TEST_ASSERT_INT32_WITHIN (DELTA_US (30 ), 30000 , p_timer->read_us ());
645
+ TEST_ASSERT_UINT64_WITHIN (DELTA_US (30 ), 30000 , p_timer->read_high_resolution_us ());
657
646
}
658
647
659
648
/* This test verifies Timer float operator.
@@ -677,7 +666,7 @@ void test_timer_float_operator_os_ticker()
677
666
p_timer->stop ();
678
667
679
668
/* Check result - 10 ms elapsed. */
680
- TEST_ASSERT_FLOAT_WITHIN (DELTA_S (1 ), 0 .010f , (float )(*p_timer));
669
+ TEST_ASSERT_FLOAT_WITHIN (DELTA_S (10 ), 0 .010f , (float )(*p_timer));
681
670
}
682
671
683
672
/* This test verifies Timer float operator.
@@ -732,10 +721,10 @@ void test_timer_time_measurement()
732
721
p_timer->stop ();
733
722
734
723
/* Check results. */
735
- TEST_ASSERT_FLOAT_WITHIN (DELTA_S (1 ), (float )wait_val_us / 1000000 , p_timer->read ());
736
- TEST_ASSERT_INT32_WITHIN (DELTA_MS (1 ), wait_val_us / 1000 , p_timer->read_ms ());
737
- TEST_ASSERT_INT32_WITHIN (DELTA_US (1 ), wait_val_us, p_timer->read_us ());
738
- TEST_ASSERT_UINT64_WITHIN (DELTA_US (1 ), wait_val_us, p_timer->read_high_resolution_us ());
724
+ TEST_ASSERT_FLOAT_WITHIN (DELTA_S (wait_val_us / US_PER_MSEC ), (float )wait_val_us / US_PER_SEC , p_timer->read ());
725
+ TEST_ASSERT_INT32_WITHIN (DELTA_MS (wait_val_us / US_PER_MSEC ), wait_val_us / US_PER_MSEC , p_timer->read_ms ());
726
+ TEST_ASSERT_INT32_WITHIN (DELTA_US (wait_val_us / US_PER_MSEC ), wait_val_us, p_timer->read_us ());
727
+ TEST_ASSERT_UINT64_WITHIN (DELTA_US (wait_val_us / US_PER_MSEC ), wait_val_us, p_timer->read_high_resolution_us ());
739
728
}
740
729
741
730
utest::v1::status_t test_setup (const size_t number_of_cases) {
0 commit comments