Skip to content
Merged
99 changes: 48 additions & 51 deletions TESTS/mbed_drivers/lp_timer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,21 @@ extern uint32_t SystemCoreClock;
* 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.
* Additional time is caused by the function calls and
* additional operations performed by wait and
* stop functions before in fact timer is stopped. This may
* add additional time to the counted result.
* e.g.
* 1 ms delay: tolerance = 550 us
* 10 ms delay: tolerance = 1000 us
* 100 ms delay: tolerance = 5500 us
* 1000 ms delay: tolerance = 50500 us
*
* To take in to account this extra time we introduce DELTA
* value based on CPU clock (speed):
* DELTA = TOLERANCE_FACTOR / SystemCoreClock * US_FACTOR
*
* 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 DELTA_US(delay_ms) (500 + delay_ms * US_PER_MSEC / 20)
#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))

#define US_PER_SEC 1000000
#define US_PER_MSEC 1000
#define TOLERANCE_FACTOR 80000.0f
Expand Down Expand Up @@ -122,10 +117,10 @@ void test_lptimer_time_accumulation()
lp_timer.stop();

/* Check results - totally 10 ms have elapsed. */
TEST_ASSERT_FLOAT_WITHIN(DELTA_S(1), 0.010f, lp_timer.read());
TEST_ASSERT_INT32_WITHIN(DELTA_MS(1), 10, lp_timer.read_ms());
TEST_ASSERT_INT32_WITHIN(DELTA_US(1), 10000, lp_timer.read_us());
TEST_ASSERT_UINT64_WITHIN(DELTA_US(1), 10000, lp_timer.read_high_resolution_us());
TEST_ASSERT_FLOAT_WITHIN(DELTA_S(10), 0.010f, lp_timer.read());
TEST_ASSERT_INT32_WITHIN(DELTA_MS(10), 10, lp_timer.read_ms());
TEST_ASSERT_INT32_WITHIN(DELTA_US(10), 10000, lp_timer.read_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
* the timer is stopped does not have influence on the
Expand All @@ -144,10 +139,10 @@ void test_lptimer_time_accumulation()
lp_timer.stop();

/* Check results - totally 30 ms have elapsed. */
TEST_ASSERT_FLOAT_WITHIN(DELTA_S(2), 0.030f, lp_timer.read());
TEST_ASSERT_INT32_WITHIN(DELTA_MS(2), 30, lp_timer.read_ms());
TEST_ASSERT_INT32_WITHIN(DELTA_US(2), 30000, lp_timer.read_us());
TEST_ASSERT_UINT64_WITHIN(DELTA_US(2), 30000, lp_timer.read_high_resolution_us());
TEST_ASSERT_FLOAT_WITHIN(DELTA_S(30), 0.030f, lp_timer.read());
TEST_ASSERT_INT32_WITHIN(DELTA_MS(30), 30, lp_timer.read_ms());
TEST_ASSERT_INT32_WITHIN(DELTA_US(30), 30000, lp_timer.read_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
* the timer is stopped does not have influence on the
Expand All @@ -165,10 +160,10 @@ void test_lptimer_time_accumulation()
lp_timer.stop();

/* Check results - totally 60 ms have elapsed. */
TEST_ASSERT_FLOAT_WITHIN(DELTA_S(3), 0.060f, lp_timer.read());
TEST_ASSERT_INT32_WITHIN(DELTA_MS(3), 60, lp_timer.read_ms());
TEST_ASSERT_INT32_WITHIN(DELTA_US(3), 60000, lp_timer.read_us());
TEST_ASSERT_UINT64_WITHIN(DELTA_US(3), 60000, lp_timer.read_high_resolution_us());
TEST_ASSERT_FLOAT_WITHIN(DELTA_S(60), 0.060f, lp_timer.read());
TEST_ASSERT_INT32_WITHIN(DELTA_MS(60), 60, lp_timer.read_ms());
TEST_ASSERT_INT32_WITHIN(DELTA_US(60), 60000, lp_timer.read_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
* the timer is stopped does not have influence on the
Expand All @@ -187,10 +182,10 @@ void test_lptimer_time_accumulation()
lp_timer.stop();

/* Check results - totally 1060 ms have elapsed. */
TEST_ASSERT_FLOAT_WITHIN(DELTA_S(4), 1.060f, lp_timer.read());
TEST_ASSERT_INT32_WITHIN(DELTA_MS(4), 1060, lp_timer.read_ms());
TEST_ASSERT_INT32_WITHIN(DELTA_US(4), 1060000, lp_timer.read_us());
TEST_ASSERT_UINT64_WITHIN(DELTA_US(4), 1060000, lp_timer.read_high_resolution_us());
TEST_ASSERT_FLOAT_WITHIN(DELTA_S(1060), 1.060f, lp_timer.read());
TEST_ASSERT_INT32_WITHIN(DELTA_MS(1060), 1060, lp_timer.read_ms());
TEST_ASSERT_INT32_WITHIN(DELTA_US(1060), 1060000, lp_timer.read_us());
TEST_ASSERT_UINT64_WITHIN(DELTA_US(1060), 1060000, lp_timer.read_high_resolution_us());
}

/* This test verifies if reset() function resets the
Expand All @@ -216,10 +211,10 @@ void test_lptimer_reset()
lp_timer.stop();

/* Check results - totally 10 ms elapsed. */
TEST_ASSERT_FLOAT_WITHIN(DELTA_S(1), 0.010f, lp_timer.read());
TEST_ASSERT_INT32_WITHIN(DELTA_MS(1), 10, lp_timer.read_ms());
TEST_ASSERT_INT32_WITHIN(DELTA_US(1), 10000, lp_timer.read_us());
TEST_ASSERT_UINT64_WITHIN(DELTA_US(1), 10000, lp_timer.read_high_resolution_us());
TEST_ASSERT_FLOAT_WITHIN(DELTA_S(10), 0.010f, lp_timer.read());
TEST_ASSERT_INT32_WITHIN(DELTA_MS(10), 10, lp_timer.read_ms());
TEST_ASSERT_INT32_WITHIN(DELTA_US(10), 10000, lp_timer.read_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. */
lp_timer.reset();
Expand All @@ -234,10 +229,10 @@ void test_lptimer_reset()
lp_timer.stop();

/* Check results - 20 ms elapsed since the reset. */
TEST_ASSERT_FLOAT_WITHIN(DELTA_S(1), 0.020f, lp_timer.read());
TEST_ASSERT_INT32_WITHIN(DELTA_MS(1), 20, lp_timer.read_ms());
TEST_ASSERT_INT32_WITHIN(DELTA_US(1), 20000, lp_timer.read_us());
TEST_ASSERT_UINT64_WITHIN(DELTA_US(1), 20000, lp_timer.read_high_resolution_us());
TEST_ASSERT_FLOAT_WITHIN(DELTA_S(20), 0.020f, lp_timer.read());
TEST_ASSERT_INT32_WITHIN(DELTA_MS(20), 20, lp_timer.read_ms());
TEST_ASSERT_INT32_WITHIN(DELTA_US(20), 20000, lp_timer.read_us());
TEST_ASSERT_UINT64_WITHIN(DELTA_US(20), 20000, lp_timer.read_high_resolution_us());
}

/* This test verifies if calling start() for already
Expand Down Expand Up @@ -267,10 +262,10 @@ void test_lptimer_start_started_timer()
lp_timer.stop();

/* Check results - 30 ms have elapsed since the first start. */
TEST_ASSERT_FLOAT_WITHIN(DELTA_S(2), 0.030f, lp_timer.read());
TEST_ASSERT_INT32_WITHIN(DELTA_MS(2), 30, lp_timer.read_ms());
TEST_ASSERT_INT32_WITHIN(DELTA_US(2), 30000, lp_timer.read_us());
TEST_ASSERT_UINT64_WITHIN(DELTA_US(2), 30000, lp_timer.read_high_resolution_us());
TEST_ASSERT_FLOAT_WITHIN(DELTA_S(30), 0.030f, lp_timer.read());
TEST_ASSERT_INT32_WITHIN(DELTA_MS(30), 30, lp_timer.read_ms());
TEST_ASSERT_INT32_WITHIN(DELTA_US(30), 30000, lp_timer.read_us());
TEST_ASSERT_UINT64_WITHIN(DELTA_US(30), 30000, lp_timer.read_high_resolution_us());
}

/* This test verifies low power timer float operator.
Expand All @@ -294,7 +289,7 @@ void test_lptimer_float_operator()
lp_timer.stop();

/* 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
Expand All @@ -310,6 +305,8 @@ void test_lptimer_time_measurement()
{
LowPowerTimer lp_timer;

const int delta_ms = (wait_val_us / US_PER_MSEC);

/* Start the timer. */
lp_timer.start();

Expand All @@ -320,10 +317,10 @@ void test_lptimer_time_measurement()
lp_timer.stop();

/* 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_INT32_WITHIN(DELTA_MS(1), wait_val_us / 1000, lp_timer.read_ms());
TEST_ASSERT_INT32_WITHIN(DELTA_US(1), 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_FLOAT_WITHIN(DELTA_S(delta_ms), (float )wait_val_us / 1000000, lp_timer.read());
TEST_ASSERT_INT32_WITHIN(DELTA_MS(delta_ms), wait_val_us / 1000, lp_timer.read_ms());
TEST_ASSERT_INT32_WITHIN(DELTA_US(delta_ms), wait_val_us, lp_timer.read_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)
Expand Down
5 changes: 3 additions & 2 deletions TESTS/mbed_drivers/timerevent/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ using namespace utest::v1;
#endif

#define TEST_DELAY_US 50000ULL
#define DELTA 2

class TestTimerEvent: public TimerEvent {
private:
Expand Down Expand Up @@ -125,7 +126,7 @@ void test_insert(void) {
int32_t sem_slots = tte.sem_wait(0);
TEST_ASSERT_EQUAL(0, sem_slots);

sem_slots = tte.sem_wait(TEST_DELAY_US / 1000 + 1);
sem_slots = tte.sem_wait(TEST_DELAY_US / 1000 + DELTA);
TEST_ASSERT_EQUAL(1, sem_slots);

tte.remove();
Expand Down Expand Up @@ -154,7 +155,7 @@ void test_remove(void) {
TEST_ASSERT_EQUAL(0, sem_slots);
tte.remove();

sem_slots = tte.sem_wait(TEST_DELAY_US * 2 / 1000 + 1);
sem_slots = tte.sem_wait(TEST_DELAY_US * 2 / 1000 + DELTA);
TEST_ASSERT_EQUAL(0, sem_slots);
}

Expand Down
7 changes: 6 additions & 1 deletion TESTS/mbed_hal/common_tickers/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,23 +345,28 @@ void ticker_increment_test(void)

uint32_t num_of_cycles = NUM_OF_CYCLES;

const uint32_t base_tick_count = count_ticks(num_of_cycles, 1);
uint32_t base_tick_count = count_ticks(num_of_cycles, 1);
uint32_t next_tick_count = base_tick_count;
uint32_t inc_val = 0;

while (inc_val < 100) {

next_tick_count = count_ticks(num_of_cycles + inc_val, 1);

if (next_tick_count == base_tick_count) {

/* Same tick count, so increase num of cycles. */
inc_val++;
} else {

/* It is possible that the difference between base and next
* tick count on some platforms is greater that 1, in this case we need
* to repeat counting with the reduced number of cycles (for slower boards).
* In cases if difference is exactly 1 we can exit the loop.
*/
num_of_cycles /= 2;
inc_val = 0;
base_tick_count = count_ticks(num_of_cycles, 1);

if (next_tick_count - base_tick_count == 1 ||
base_tick_count - next_tick_count == 1) {
Expand Down
19 changes: 8 additions & 11 deletions TESTS/mbed_hal/flash/functional_tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@ using namespace utest::v1;

#define TEST_CYCLES 10000000

#ifdef TARGET_NRF52
/* The increased tolerance is to account for the imprecise timers on the NRF52. */
#define ALLOWED_DRIFT_PPM (1000000/50000) //5.0%
#else
#define ALLOWED_DRIFT_PPM (1000000/5000) //0.5%
#endif

/*
return values to be checked are documented at:
Expand Down Expand Up @@ -103,16 +98,18 @@ MBED_NOINLINE
static int time_cpu_cycles(uint32_t cycles)
{
Timer timer;
timer.start();

int timer_start = timer.read_us();
core_util_critical_section_enter();

uint32_t delay = cycles;
delay_loop(delay);
int timer_end = timer.read_us();
timer.start();

delay_loop(cycles);

timer.stop();
return timer_end - timer_start;

core_util_critical_section_exit();

return timer.read_us();
}

void flash_init_test()
Expand Down
Loading