Skip to content

Commit 228ff93

Browse files
mprseCruz Monrreal II
authored andcommitted
tests-mbed_drivers-lp_timer: change delay method
The test sometimes fails on NRF51_DK (test case: "Test: LowPowerTimer - time measurement 1 ms.") in morph tests. The test verifies if LowPowerTimer class correctly counts elapsed time. Sometimes we got measured ~1600 us for delay 1000 us (delta 550 us). The delay is performed using `wait_us()` function which for delays greater than or equal to 1 ms (our case) calls `Thread::wait((uint32_t)ms);`. This causes rescheduling and potentially can put board into sleep (deep sleep mode is disabled by `wait_us()`). For our test purposes we don't need rescheduling/sleep since this actions takes extra time and have influence on the time measurement accuracy. The solution is to implement function for delay which is based on busy loop and uses us ticker. It has been verified that this solves the problem. With this fix when measurement of 1 ms is repeated 1000 times we got usually measured time equal to ~1080 us, and sometimes ~1300us (checked that this is caused by systick interrupt handling). Since this is test for drivers layer and the results are acceptable I decided to not disabling systick in the test).
1 parent 21b8490 commit 228ff93

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

TESTS/mbed_drivers/lp_timer/main.cpp

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ extern uint32_t SystemCoreClock;
5555
#define DELTA_MS(delay_ms) (1 + ((delay_ms) * US_PER_MSEC / 20 / US_PER_MSEC))
5656
#define DELTA_S(delay_ms) (0.000500f + (((float)(delay_ms)) / MSEC_PER_SEC / 20))
5757

58+
void busy_wait_us(int us)
59+
{
60+
const ticker_data_t *const ticker = get_us_ticker_data();
61+
uint32_t start = ticker_read(ticker);
62+
while ((ticker_read(ticker) - start) < (uint32_t)us);
63+
}
64+
65+
void busy_wait_ms(int ms)
66+
{
67+
busy_wait_us(ms * US_PER_MSEC);
68+
}
69+
5870
/* This test verifies if low power timer is stopped after
5971
* creation.
6072
*
@@ -74,7 +86,7 @@ void test_lptimer_creation()
7486

7587
/* Wait 10 ms.
7688
* After that operation timer read routines should still return 0. */
77-
wait_ms(10);
89+
busy_wait_ms(10);
7890

7991
/* Check results. */
8092
TEST_ASSERT_EQUAL_FLOAT(0, lp_timer.read());
@@ -102,7 +114,7 @@ void test_lptimer_time_accumulation()
102114
lp_timer.start();
103115

104116
/* Wait 10 ms. */
105-
wait_ms(10);
117+
busy_wait_ms(10);
106118

107119
/* Stop the timer. */
108120
lp_timer.stop();
@@ -116,15 +128,15 @@ void test_lptimer_time_accumulation()
116128
/* Wait 50 ms - this is done to show that time elapsed when
117129
* the timer is stopped does not have influence on the
118130
* timer counted time. */
119-
wait_ms(50);
131+
busy_wait_ms(50);
120132

121133
/* ------ */
122134

123135
/* Start the timer. */
124136
lp_timer.start();
125137

126138
/* Wait 20 ms. */
127-
wait_ms(20);
139+
busy_wait_ms(20);
128140

129141
/* Stop the timer. */
130142
lp_timer.stop();
@@ -145,7 +157,7 @@ void test_lptimer_time_accumulation()
145157
lp_timer.start();
146158

147159
/* Wait 30 ms. */
148-
wait_ms(30);
160+
busy_wait_ms(30);
149161

150162
/* Stop the timer. */
151163
lp_timer.stop();
@@ -159,15 +171,15 @@ void test_lptimer_time_accumulation()
159171
/* Wait 50 ms - this is done to show that time elapsed when
160172
* the timer is stopped does not have influence on the
161173
* timer time. */
162-
wait_ms(50);
174+
busy_wait_ms(50);
163175

164176
/* ------ */
165177

166178
/* Start the timer. */
167179
lp_timer.start();
168180

169181
/* Wait 1 sec. */
170-
wait_ms(1000);
182+
busy_wait_ms(1000);
171183

172184
/* Stop the timer. */
173185
lp_timer.stop();
@@ -196,7 +208,7 @@ void test_lptimer_reset()
196208
lp_timer.start();
197209

198210
/* Wait 10 ms. */
199-
wait_ms(10);
211+
busy_wait_ms(10);
200212

201213
/* Stop the timer. */
202214
lp_timer.stop();
@@ -214,7 +226,7 @@ void test_lptimer_reset()
214226
lp_timer.start();
215227

216228
/* Wait 20 ms. */
217-
wait_ms(20);
229+
busy_wait_ms(20);
218230

219231
/* Stop the timer. */
220232
lp_timer.stop();
@@ -241,13 +253,13 @@ void test_lptimer_start_started_timer()
241253
lp_timer.start();
242254

243255
/* Wait 10 ms. */
244-
wait_ms(10);
256+
busy_wait_ms(10);
245257

246258
/* Now start timer again. */
247259
lp_timer.start();
248260

249261
/* Wait 20 ms. */
250-
wait_ms(20);
262+
busy_wait_ms(20);
251263

252264
/* Stop the timer. */
253265
lp_timer.stop();
@@ -274,7 +286,7 @@ void test_lptimer_float_operator()
274286
lp_timer.start();
275287

276288
/* Wait 10 ms. */
277-
wait_ms(10);
289+
busy_wait_ms(10);
278290

279291
/* Stop the timer. */
280292
lp_timer.stop();
@@ -302,7 +314,7 @@ void test_lptimer_time_measurement()
302314
lp_timer.start();
303315

304316
/* Wait <wait_val_us> us. */
305-
wait_us(wait_val_us);
317+
busy_wait_us(wait_val_us);
306318

307319
/* Stop the timer. */
308320
lp_timer.stop();

0 commit comments

Comments
 (0)