mirror of https://github.com/ARMmbed/mbed-os.git
				
				
				
			tests-mbed_drivers-lp_timer: modify tolerance to percentage form
Represent tolerance value as it is done in `tests-mbed_drivers_timer`. Use 5% tolerance for lp ticker.pull/7009/head
							parent
							
								
									a0a07d9725
								
							
						
					
					
						commit
						06c6db5119
					
				| 
						 | 
					@ -35,26 +35,21 @@ extern uint32_t SystemCoreClock;
 | 
				
			||||||
 * timer we need to adjust delta.
 | 
					 * timer we need to adjust delta.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Macro to define delta based on CPU clock frequency.
 | 
					 /*
 | 
				
			||||||
 | 
					 * Define tolerance as follows:
 | 
				
			||||||
 | 
					 * tolerance = 500 us + 5% of measured time
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * Note that some extra time is counted by the timer.
 | 
					 * e.g.
 | 
				
			||||||
 * Additional time is caused by the function calls and
 | 
					 * 1 ms delay: tolerance = 550 us
 | 
				
			||||||
 * additional operations performed by wait and
 | 
					 * 10 ms delay: tolerance = 1000 us
 | 
				
			||||||
 * stop functions before in fact timer is stopped. This may
 | 
					 * 100 ms delay: tolerance = 5500 us
 | 
				
			||||||
 * add additional time to the counted result.
 | 
					 * 1000 ms delay: tolerance = 50500 us
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * To take in to account this extra time we introduce DELTA
 | 
					 *  */
 | 
				
			||||||
 * value based on CPU clock (speed):
 | 
					#define DELTA_US(delay_ms) (500 + delay_ms * US_PER_MSEC / 20)
 | 
				
			||||||
 *   DELTA = TOLERANCE_FACTOR / SystemCoreClock * US_FACTOR
 | 
					#define DELTA_MS(delay_ms) (1 + (delay_ms * US_PER_MSEC / 20 / US_PER_MSEC))
 | 
				
			||||||
 *
 | 
					#define DELTA_S(delay_ms) (0.000500f + (((float)delay_ms) / MSEC_PER_SEC / 20))
 | 
				
			||||||
 *   e.g.
 | 
					
 | 
				
			||||||
 *   For K64F          DELTA = (80000 / 120000000) * 1000000 = 666[us]
 | 
					 | 
				
			||||||
 *   For NUCLEO_F070RB DELTA = (80000 /  48000000) * 1000000 = 1666[us]
 | 
					 | 
				
			||||||
 *   For NRF51_DK      DELTA = (80000 /  16000000) * 1000000 = 5000[us]
 | 
					 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 * As low power timer cannot be too much accurate, this DELTA should not be more precise than 500us,
 | 
					 | 
				
			||||||
 * which corresponds to a maximum CPU clock around 130MHz
 | 
					 | 
				
			||||||
 */
 | 
					 | 
				
			||||||
#define US_PER_SEC       1000000
 | 
					#define US_PER_SEC       1000000
 | 
				
			||||||
#define US_PER_MSEC      1000
 | 
					#define US_PER_MSEC      1000
 | 
				
			||||||
#define TOLERANCE_FACTOR 80000.0f
 | 
					#define TOLERANCE_FACTOR 80000.0f
 | 
				
			||||||
| 
						 | 
					@ -122,10 +117,10 @@ void test_lptimer_time_accumulation()
 | 
				
			||||||
    lp_timer.stop();
 | 
					    lp_timer.stop();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* Check results - totally 10 ms have elapsed. */
 | 
					    /* Check results - totally 10 ms have elapsed. */
 | 
				
			||||||
    TEST_ASSERT_FLOAT_WITHIN(DELTA_S(1), 0.010f, lp_timer.read());
 | 
					    TEST_ASSERT_FLOAT_WITHIN(DELTA_S(10), 0.010f, lp_timer.read());
 | 
				
			||||||
    TEST_ASSERT_INT32_WITHIN(DELTA_MS(1), 10, lp_timer.read_ms());
 | 
					    TEST_ASSERT_INT32_WITHIN(DELTA_MS(10), 10, lp_timer.read_ms());
 | 
				
			||||||
    TEST_ASSERT_INT32_WITHIN(DELTA_US(1), 10000, lp_timer.read_us());
 | 
					    TEST_ASSERT_INT32_WITHIN(DELTA_US(10), 10000, lp_timer.read_us());
 | 
				
			||||||
    TEST_ASSERT_UINT64_WITHIN(DELTA_US(1), 10000, lp_timer.read_high_resolution_us());
 | 
					    TEST_ASSERT_UINT64_WITHIN(DELTA_US(10), 10000, lp_timer.read_high_resolution_us());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* Wait 50 ms - this is done to show that time elapsed when
 | 
					    /* Wait 50 ms - this is done to show that time elapsed when
 | 
				
			||||||
     * the timer is stopped does not have influence on the
 | 
					     * the timer is stopped does not have influence on the
 | 
				
			||||||
| 
						 | 
					@ -144,10 +139,10 @@ void test_lptimer_time_accumulation()
 | 
				
			||||||
    lp_timer.stop();
 | 
					    lp_timer.stop();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* Check results - totally 30 ms have elapsed. */
 | 
					    /* Check results - totally 30 ms have elapsed. */
 | 
				
			||||||
    TEST_ASSERT_FLOAT_WITHIN(DELTA_S(2), 0.030f, lp_timer.read());
 | 
					    TEST_ASSERT_FLOAT_WITHIN(DELTA_S(30), 0.030f, lp_timer.read());
 | 
				
			||||||
    TEST_ASSERT_INT32_WITHIN(DELTA_MS(2), 30, lp_timer.read_ms());
 | 
					    TEST_ASSERT_INT32_WITHIN(DELTA_MS(30), 30, lp_timer.read_ms());
 | 
				
			||||||
    TEST_ASSERT_INT32_WITHIN(DELTA_US(2), 30000, lp_timer.read_us());
 | 
					    TEST_ASSERT_INT32_WITHIN(DELTA_US(30), 30000, lp_timer.read_us());
 | 
				
			||||||
    TEST_ASSERT_UINT64_WITHIN(DELTA_US(2), 30000, lp_timer.read_high_resolution_us());
 | 
					    TEST_ASSERT_UINT64_WITHIN(DELTA_US(30), 30000, lp_timer.read_high_resolution_us());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* Wait 50 ms - this is done to show that time elapsed when
 | 
					    /* Wait 50 ms - this is done to show that time elapsed when
 | 
				
			||||||
     * the timer is stopped does not have influence on the
 | 
					     * the timer is stopped does not have influence on the
 | 
				
			||||||
| 
						 | 
					@ -165,10 +160,10 @@ void test_lptimer_time_accumulation()
 | 
				
			||||||
    lp_timer.stop();
 | 
					    lp_timer.stop();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* Check results - totally 60 ms have elapsed. */
 | 
					    /* Check results - totally 60 ms have elapsed. */
 | 
				
			||||||
    TEST_ASSERT_FLOAT_WITHIN(DELTA_S(3), 0.060f, lp_timer.read());
 | 
					    TEST_ASSERT_FLOAT_WITHIN(DELTA_S(60), 0.060f, lp_timer.read());
 | 
				
			||||||
    TEST_ASSERT_INT32_WITHIN(DELTA_MS(3), 60, lp_timer.read_ms());
 | 
					    TEST_ASSERT_INT32_WITHIN(DELTA_MS(60), 60, lp_timer.read_ms());
 | 
				
			||||||
    TEST_ASSERT_INT32_WITHIN(DELTA_US(3), 60000, lp_timer.read_us());
 | 
					    TEST_ASSERT_INT32_WITHIN(DELTA_US(60), 60000, lp_timer.read_us());
 | 
				
			||||||
    TEST_ASSERT_UINT64_WITHIN(DELTA_US(3), 60000, lp_timer.read_high_resolution_us());
 | 
					    TEST_ASSERT_UINT64_WITHIN(DELTA_US(60), 60000, lp_timer.read_high_resolution_us());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* Wait 50 ms - this is done to show that time elapsed when
 | 
					    /* Wait 50 ms - this is done to show that time elapsed when
 | 
				
			||||||
     * the timer is stopped does not have influence on the
 | 
					     * the timer is stopped does not have influence on the
 | 
				
			||||||
| 
						 | 
					@ -187,10 +182,10 @@ void test_lptimer_time_accumulation()
 | 
				
			||||||
    lp_timer.stop();
 | 
					    lp_timer.stop();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* Check results - totally 1060 ms have elapsed. */
 | 
					    /* Check results - totally 1060 ms have elapsed. */
 | 
				
			||||||
    TEST_ASSERT_FLOAT_WITHIN(DELTA_S(4), 1.060f, lp_timer.read());
 | 
					    TEST_ASSERT_FLOAT_WITHIN(DELTA_S(1060), 1.060f, lp_timer.read());
 | 
				
			||||||
    TEST_ASSERT_INT32_WITHIN(DELTA_MS(4), 1060, lp_timer.read_ms());
 | 
					    TEST_ASSERT_INT32_WITHIN(DELTA_MS(1060), 1060, lp_timer.read_ms());
 | 
				
			||||||
    TEST_ASSERT_INT32_WITHIN(DELTA_US(4), 1060000, lp_timer.read_us());
 | 
					    TEST_ASSERT_INT32_WITHIN(DELTA_US(1060), 1060000, lp_timer.read_us());
 | 
				
			||||||
    TEST_ASSERT_UINT64_WITHIN(DELTA_US(4), 1060000, lp_timer.read_high_resolution_us());
 | 
					    TEST_ASSERT_UINT64_WITHIN(DELTA_US(1060), 1060000, lp_timer.read_high_resolution_us());
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* This test verifies if reset() function resets the
 | 
					/* This test verifies if reset() function resets the
 | 
				
			||||||
| 
						 | 
					@ -216,10 +211,10 @@ void test_lptimer_reset()
 | 
				
			||||||
    lp_timer.stop();
 | 
					    lp_timer.stop();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* Check results - totally 10 ms elapsed. */
 | 
					    /* Check results - totally 10 ms elapsed. */
 | 
				
			||||||
    TEST_ASSERT_FLOAT_WITHIN(DELTA_S(1), 0.010f, lp_timer.read());
 | 
					    TEST_ASSERT_FLOAT_WITHIN(DELTA_S(10), 0.010f, lp_timer.read());
 | 
				
			||||||
    TEST_ASSERT_INT32_WITHIN(DELTA_MS(1), 10, lp_timer.read_ms());
 | 
					    TEST_ASSERT_INT32_WITHIN(DELTA_MS(10), 10, lp_timer.read_ms());
 | 
				
			||||||
    TEST_ASSERT_INT32_WITHIN(DELTA_US(1), 10000, lp_timer.read_us());
 | 
					    TEST_ASSERT_INT32_WITHIN(DELTA_US(10), 10000, lp_timer.read_us());
 | 
				
			||||||
    TEST_ASSERT_UINT64_WITHIN(DELTA_US(1), 10000, lp_timer.read_high_resolution_us());
 | 
					    TEST_ASSERT_UINT64_WITHIN(DELTA_US(10), 10000, lp_timer.read_high_resolution_us());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* Reset the timer - previous measured time should be lost now. */
 | 
					    /* Reset the timer - previous measured time should be lost now. */
 | 
				
			||||||
    lp_timer.reset();
 | 
					    lp_timer.reset();
 | 
				
			||||||
| 
						 | 
					@ -234,10 +229,10 @@ void test_lptimer_reset()
 | 
				
			||||||
    lp_timer.stop();
 | 
					    lp_timer.stop();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* Check results - 20 ms elapsed since the reset. */
 | 
					    /* Check results - 20 ms elapsed since the reset. */
 | 
				
			||||||
    TEST_ASSERT_FLOAT_WITHIN(DELTA_S(1), 0.020f, lp_timer.read());
 | 
					    TEST_ASSERT_FLOAT_WITHIN(DELTA_S(20), 0.020f, lp_timer.read());
 | 
				
			||||||
    TEST_ASSERT_INT32_WITHIN(DELTA_MS(1), 20, lp_timer.read_ms());
 | 
					    TEST_ASSERT_INT32_WITHIN(DELTA_MS(20), 20, lp_timer.read_ms());
 | 
				
			||||||
    TEST_ASSERT_INT32_WITHIN(DELTA_US(1), 20000, lp_timer.read_us());
 | 
					    TEST_ASSERT_INT32_WITHIN(DELTA_US(20), 20000, lp_timer.read_us());
 | 
				
			||||||
    TEST_ASSERT_UINT64_WITHIN(DELTA_US(1), 20000, lp_timer.read_high_resolution_us());
 | 
					    TEST_ASSERT_UINT64_WITHIN(DELTA_US(20), 20000, lp_timer.read_high_resolution_us());
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* This test verifies if calling start() for already
 | 
					/* This test verifies if calling start() for already
 | 
				
			||||||
| 
						 | 
					@ -267,10 +262,10 @@ void test_lptimer_start_started_timer()
 | 
				
			||||||
    lp_timer.stop();
 | 
					    lp_timer.stop();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* Check results - 30 ms have elapsed since the first start. */
 | 
					    /* Check results - 30 ms have elapsed since the first start. */
 | 
				
			||||||
    TEST_ASSERT_FLOAT_WITHIN(DELTA_S(2), 0.030f, lp_timer.read());
 | 
					    TEST_ASSERT_FLOAT_WITHIN(DELTA_S(30), 0.030f, lp_timer.read());
 | 
				
			||||||
    TEST_ASSERT_INT32_WITHIN(DELTA_MS(2), 30, lp_timer.read_ms());
 | 
					    TEST_ASSERT_INT32_WITHIN(DELTA_MS(30), 30, lp_timer.read_ms());
 | 
				
			||||||
    TEST_ASSERT_INT32_WITHIN(DELTA_US(2), 30000, lp_timer.read_us());
 | 
					    TEST_ASSERT_INT32_WITHIN(DELTA_US(30), 30000, lp_timer.read_us());
 | 
				
			||||||
    TEST_ASSERT_UINT64_WITHIN(DELTA_US(2), 30000, lp_timer.read_high_resolution_us());
 | 
					    TEST_ASSERT_UINT64_WITHIN(DELTA_US(30), 30000, lp_timer.read_high_resolution_us());
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* This test verifies low power timer float operator.
 | 
					/* This test verifies low power timer float operator.
 | 
				
			||||||
| 
						 | 
					@ -294,7 +289,7 @@ void test_lptimer_float_operator()
 | 
				
			||||||
    lp_timer.stop();
 | 
					    lp_timer.stop();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* Check result - 10 ms elapsed. */
 | 
					    /* Check result - 10 ms elapsed. */
 | 
				
			||||||
    TEST_ASSERT_FLOAT_WITHIN(DELTA_S(1), 0.010f, (float )(lp_timer));
 | 
					    TEST_ASSERT_FLOAT_WITHIN(DELTA_S(10), 0.010f, (float )(lp_timer));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* This test verifies if time counted by the low power timer is
 | 
					/* This test verifies if time counted by the low power timer is
 | 
				
			||||||
| 
						 | 
					@ -310,6 +305,8 @@ void test_lptimer_time_measurement()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    LowPowerTimer lp_timer;
 | 
					    LowPowerTimer lp_timer;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const int delta_ms = (wait_val_us / US_PER_MSEC);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* Start the timer. */
 | 
					    /* Start the timer. */
 | 
				
			||||||
    lp_timer.start();
 | 
					    lp_timer.start();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -320,10 +317,10 @@ void test_lptimer_time_measurement()
 | 
				
			||||||
    lp_timer.stop();
 | 
					    lp_timer.stop();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* Check results - wait_val_us us have elapsed. */
 | 
					    /* Check results - wait_val_us us have elapsed. */
 | 
				
			||||||
    TEST_ASSERT_FLOAT_WITHIN(DELTA_S(1), (float )wait_val_us / 1000000, lp_timer.read());
 | 
					    TEST_ASSERT_FLOAT_WITHIN(DELTA_S(delta_ms), (float )wait_val_us / 1000000, lp_timer.read());
 | 
				
			||||||
    TEST_ASSERT_INT32_WITHIN(DELTA_MS(1), wait_val_us / 1000, lp_timer.read_ms());
 | 
					    TEST_ASSERT_INT32_WITHIN(DELTA_MS(delta_ms), wait_val_us / 1000, lp_timer.read_ms());
 | 
				
			||||||
    TEST_ASSERT_INT32_WITHIN(DELTA_US(1), wait_val_us, lp_timer.read_us());
 | 
					    TEST_ASSERT_INT32_WITHIN(DELTA_US(delta_ms), wait_val_us, lp_timer.read_us());
 | 
				
			||||||
    TEST_ASSERT_UINT64_WITHIN(DELTA_US(1), wait_val_us, lp_timer.read_high_resolution_us());
 | 
					    TEST_ASSERT_UINT64_WITHIN(DELTA_US(delta_ms), wait_val_us, lp_timer.read_high_resolution_us());
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
utest::v1::status_t test_setup(const size_t number_of_cases)
 | 
					utest::v1::status_t test_setup(const size_t number_of_cases)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue