20
20
#include " mbed.h"
21
21
#include " unity/unity.h"
22
22
23
+ using namespace std ::chrono;
24
+
23
25
#define NUM_TIMEOUTS 16
24
- # define DRIFT_TEST_PERIOD_US 10000
26
+ const microseconds DRIFT_TEST_PERIOD = 10ms;
25
27
26
- const float TEST_DELAY_S = 0.01 ;
27
- const uint32_t TEST_DELAY_MS = 1000 .0F * TEST_DELAY_S;
28
- const us_timestamp_t TEST_DELAY_US = 1000000 .0F * TEST_DELAY_S;
28
+ const milliseconds TEST_DELAY = 10ms;
29
29
30
30
/* Timeouts are quite arbitrary due to large number of boards with varying level of accuracy */
31
31
#define LONG_DELTA_US (100000 )
@@ -41,24 +41,6 @@ void cnt_callback(volatile uint32_t *cnt)
41
41
(*cnt)++;
42
42
}
43
43
44
- template <typename TimeoutType>
45
- class AttachTester : public TimeoutType {
46
- public:
47
- void attach_callback (Callback<void ()> func, us_timestamp_t delay_us)
48
- {
49
- TimeoutType::attach (func, (float ) delay_us / 1000000 .0f );
50
- }
51
- };
52
-
53
- template <typename TimeoutType>
54
- class AttachUSTester : public TimeoutType {
55
- public:
56
- void attach_callback (Callback<void ()> func, us_timestamp_t delay_us)
57
- {
58
- TimeoutType::attach_us (func, delay_us);
59
- }
60
- };
61
-
62
44
/* * Template for tests: callback called once
63
45
*
64
46
* Test callback called once
@@ -77,15 +59,15 @@ void test_single_call(void)
77
59
Semaphore sem (0 , 1 );
78
60
T timeout;
79
61
80
- timeout.attach_callback (mbed::callback (sem_callback, &sem), TEST_DELAY_US );
62
+ timeout.attach (mbed::callback (sem_callback, &sem), TEST_DELAY );
81
63
82
64
bool acquired = sem.try_acquire ();
83
65
TEST_ASSERT_FALSE (acquired);
84
66
85
- acquired = sem.try_acquire_for (TEST_DELAY_MS + 2 );
67
+ acquired = sem.try_acquire_for (TEST_DELAY + 2ms );
86
68
TEST_ASSERT_TRUE (acquired);
87
69
88
- acquired = sem.try_acquire_for (TEST_DELAY_MS + 2 );
70
+ acquired = sem.try_acquire_for (TEST_DELAY + 2ms );
89
71
TEST_ASSERT_FALSE (acquired);
90
72
91
73
timeout.detach ();
@@ -109,13 +91,13 @@ void test_cancel(void)
109
91
Semaphore sem (0 , 1 );
110
92
T timeout;
111
93
112
- timeout.attach_callback (mbed::callback (sem_callback, &sem), 2 . 0f * TEST_DELAY_US );
94
+ timeout.attach (mbed::callback (sem_callback, &sem), 2 * TEST_DELAY );
113
95
114
- bool acquired = sem.try_acquire_for (TEST_DELAY_MS );
96
+ bool acquired = sem.try_acquire_for (TEST_DELAY );
115
97
TEST_ASSERT_FALSE (acquired);
116
98
timeout.detach ();
117
99
118
- acquired = sem.try_acquire_for (TEST_DELAY_MS + 2 );
100
+ acquired = sem.try_acquire_for (TEST_DELAY + 2ms );
119
101
TEST_ASSERT_FALSE (acquired);
120
102
}
121
103
@@ -142,13 +124,13 @@ void test_override(void)
142
124
Semaphore sem2 (0 , 1 );
143
125
T timeout;
144
126
145
- timeout.attach_callback (mbed::callback (sem_callback, &sem1), 2 . 0f * TEST_DELAY_US );
127
+ timeout.attach (mbed::callback (sem_callback, &sem1), 2 * TEST_DELAY );
146
128
147
- bool acquired = sem1.try_acquire_for (TEST_DELAY_MS );
129
+ bool acquired = sem1.try_acquire_for (TEST_DELAY );
148
130
TEST_ASSERT_FALSE (acquired);
149
- timeout.attach_callback (mbed::callback (sem_callback, &sem2), 2 . 0f * TEST_DELAY_US );
131
+ timeout.attach (mbed::callback (sem_callback, &sem2), 2 * TEST_DELAY );
150
132
151
- acquired = sem2.try_acquire_for (2 * TEST_DELAY_MS + 2 );
133
+ acquired = sem2.try_acquire_for (2 * TEST_DELAY + 2ms );
152
134
TEST_ASSERT_TRUE (acquired);
153
135
acquired = sem1.try_acquire ();
154
136
TEST_ASSERT_FALSE (acquired);
@@ -176,9 +158,9 @@ void test_multiple(void)
176
158
volatile uint32_t callback_count = 0 ;
177
159
T timeouts[NUM_TIMEOUTS];
178
160
for (size_t i = 0 ; i < NUM_TIMEOUTS; i++) {
179
- timeouts[i].attach_callback (mbed::callback (cnt_callback, &callback_count), TEST_DELAY_US );
161
+ timeouts[i].attach (mbed::callback (cnt_callback, &callback_count), TEST_DELAY );
180
162
}
181
- ThisThread::sleep_for (TEST_DELAY_MS + 2 );
163
+ ThisThread::sleep_for (TEST_DELAY + 2ms );
182
164
TEST_ASSERT_EQUAL (NUM_TIMEOUTS, callback_count);
183
165
}
184
166
@@ -200,7 +182,7 @@ void test_no_wait(void)
200
182
for (int i = 0 ; i < 100 ; i++) {
201
183
Semaphore sem (0 , 1 );
202
184
T timeout;
203
- timeout.attach_callback (mbed::callback (sem_callback, &sem), 0ULL );
185
+ timeout.attach (mbed::callback (sem_callback, &sem), 0s );
204
186
bool sem_acquired = sem.try_acquire ();
205
187
TEST_ASSERT_EQUAL (true , sem_acquired);
206
188
timeout.detach ();
@@ -227,11 +209,11 @@ void test_delay_accuracy(void)
227
209
Timer timer;
228
210
229
211
timer.start ();
230
- timeout.attach_callback (mbed::callback (sem_callback, &sem), delay_us);
212
+ timeout.attach (mbed::callback (sem_callback, &sem), microseconds ( delay_us) );
231
213
232
214
sem.acquire ();
233
215
timer.stop ();
234
- TEST_ASSERT_UINT64_WITHIN (delta_us, delay_us, timer.read_high_resolution_us ());
216
+ TEST_ASSERT_UINT64_WITHIN (delta_us, delay_us, timer.elapsed_time (). count ());
235
217
236
218
timeout.detach ();
237
219
}
@@ -262,15 +244,15 @@ void test_sleep(void)
262
244
263
245
sleep_manager_lock_deep_sleep ();
264
246
timer.start ();
265
- timeout.attach_callback (mbed::callback (sem_callback, &sem), delay_us);
247
+ timeout.attach (mbed::callback (sem_callback, &sem), microseconds ( delay_us) );
266
248
267
249
bool deep_sleep_allowed = sleep_manager_can_deep_sleep_test_check ();
268
250
TEST_ASSERT_FALSE_MESSAGE (deep_sleep_allowed, " Deep sleep should be disallowed" );
269
251
sem.acquire ();
270
252
timer.stop ();
271
253
272
254
sleep_manager_unlock_deep_sleep ();
273
- TEST_ASSERT_UINT64_WITHIN (delta_us, delay_us, timer.read_high_resolution_us ());
255
+ TEST_ASSERT_UINT64_WITHIN (delta_us, delay_us, timer.elapsed_time (). count ());
274
256
275
257
timeout.detach ();
276
258
}
@@ -316,17 +298,17 @@ void test_deepsleep(void)
316
298
* hardware buffers are empty. However, such an API does not exist now,
317
299
* so we'll use the ThisThread::sleep_for() function for now.
318
300
*/
319
- ThisThread::sleep_for (20 );
301
+ ThisThread::sleep_for (20ms );
320
302
321
303
timer.start ();
322
- timeout.attach_callback (mbed::callback (sem_callback, &sem), delay_us);
304
+ timeout.attach (mbed::callback (sem_callback, &sem), microseconds ( delay_us) );
323
305
324
306
bool deep_sleep_allowed = sleep_manager_can_deep_sleep_test_check ();
325
307
TEST_ASSERT_TRUE_MESSAGE (deep_sleep_allowed, " Deep sleep should be allowed" );
326
308
sem.acquire ();
327
309
timer.stop ();
328
310
329
- TEST_ASSERT_UINT64_WITHIN (delta_us, delay_us, timer.read_high_resolution_us ());
311
+ TEST_ASSERT_UINT64_WITHIN (delta_us, delay_us, timer.elapsed_time (). count ());
330
312
331
313
timeout.detach ();
332
314
}
@@ -336,14 +318,14 @@ void test_deepsleep(void)
336
318
template <typename TimeoutTesterType>
337
319
class TimeoutDriftTester {
338
320
public:
339
- TimeoutDriftTester (us_timestamp_t period = 1000 ) :
321
+ TimeoutDriftTester (microseconds period = 1ms ) :
340
322
_callback_count (0 ), _period(period), _timeout()
341
323
{
342
324
}
343
325
344
326
void reschedule_callback (void )
345
327
{
346
- _timeout.attach_callback (mbed::callback (this , &TimeoutDriftTester::reschedule_callback), _period);
328
+ _timeout.attach (mbed::callback (this , &TimeoutDriftTester::reschedule_callback), _period);
347
329
_callback_count++;
348
330
}
349
331
@@ -359,10 +341,30 @@ class TimeoutDriftTester {
359
341
360
342
private:
361
343
volatile uint32_t _callback_count;
362
- us_timestamp_t _period;
344
+ microseconds _period;
363
345
TimeoutTesterType _timeout;
364
346
};
365
347
348
+ /* * Template for tests: rescheduling
349
+ *
350
+ * Given a Timeout object
351
+ * When a callback is attached with that reattaches itself, with @a attach()
352
+ * Then the callback is called repeatedly
353
+ *
354
+ * Given a Timeout object
355
+ * When a callback is attached with that reattaches itself, with @a attach_us()
356
+ * Then the callback is called repeatedly
357
+ */
358
+ template <typename T>
359
+ void test_reschedule (void )
360
+ {
361
+ TimeoutDriftTester<T> timeout (TEST_DELAY);
362
+
363
+ timeout.reschedule_callback ();
364
+ ThisThread::sleep_for (TEST_DELAY * 5 );
365
+ TEST_ASSERT (timeout.get_callback_count () >= 3 );
366
+ }
367
+
366
368
/* * Template for tests: accuracy of timeout delay scheduled repeatedly
367
369
*
368
370
* Test time drift -- device part
@@ -389,7 +391,7 @@ void test_drift(void)
389
391
char _key[11 ] = { };
390
392
char _value[128 ] = { };
391
393
int expected_key = 1 ;
392
- TimeoutDriftTester<T> timeout (DRIFT_TEST_PERIOD_US );
394
+ TimeoutDriftTester<T> timeout (DRIFT_TEST_PERIOD );
393
395
394
396
greentea_send_kv (" timing_drift_check_start" , 0 );
395
397
timeout.reschedule_callback ();
@@ -400,11 +402,11 @@ void test_drift(void)
400
402
expected_key = strcmp (_key, " base_time" );
401
403
} while (expected_key);
402
404
403
- greentea_send_kv (_key, timeout.get_callback_count () * DRIFT_TEST_PERIOD_US );
405
+ greentea_send_kv (_key, timeout.get_callback_count () * DRIFT_TEST_PERIOD. count () );
404
406
405
407
// wait for 2nd signal from host
406
408
greentea_parse_kv (_key, _value, sizeof (_key), sizeof (_value));
407
- greentea_send_kv (_key, timeout.get_callback_count () * DRIFT_TEST_PERIOD_US );
409
+ greentea_send_kv (_key, timeout.get_callback_count () * DRIFT_TEST_PERIOD. count () );
408
410
409
411
timeout.detach_callback ();
410
412
0 commit comments