2020#include " mbed.h"
2121#include " unity/unity.h"
2222
23+ using namespace std ::chrono;
24+
2325#define NUM_TIMEOUTS 16
24- # define DRIFT_TEST_PERIOD_US 10000
26+ const microseconds DRIFT_TEST_PERIOD = 10ms;
2527
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;
2929
3030/* Timeouts are quite arbitrary due to large number of boards with varying level of accuracy */
3131#define LONG_DELTA_US (100000 )
@@ -41,24 +41,6 @@ void cnt_callback(volatile uint32_t *cnt)
4141 (*cnt)++;
4242}
4343
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-
6244/* * Template for tests: callback called once
6345 *
6446 * Test callback called once
@@ -77,15 +59,15 @@ void test_single_call(void)
7759 Semaphore sem (0 , 1 );
7860 T timeout;
7961
80- timeout.attach_callback (mbed::callback (sem_callback, &sem), TEST_DELAY_US );
62+ timeout.attach (mbed::callback (sem_callback, &sem), TEST_DELAY );
8163
8264 bool acquired = sem.try_acquire ();
8365 TEST_ASSERT_FALSE (acquired);
8466
85- acquired = sem.try_acquire_for (TEST_DELAY_MS + 2 );
67+ acquired = sem.try_acquire_for (TEST_DELAY + 2ms );
8668 TEST_ASSERT_TRUE (acquired);
8769
88- acquired = sem.try_acquire_for (TEST_DELAY_MS + 2 );
70+ acquired = sem.try_acquire_for (TEST_DELAY + 2ms );
8971 TEST_ASSERT_FALSE (acquired);
9072
9173 timeout.detach ();
@@ -109,13 +91,13 @@ void test_cancel(void)
10991 Semaphore sem (0 , 1 );
11092 T timeout;
11193
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 );
11395
114- bool acquired = sem.try_acquire_for (TEST_DELAY_MS );
96+ bool acquired = sem.try_acquire_for (TEST_DELAY );
11597 TEST_ASSERT_FALSE (acquired);
11698 timeout.detach ();
11799
118- acquired = sem.try_acquire_for (TEST_DELAY_MS + 2 );
100+ acquired = sem.try_acquire_for (TEST_DELAY + 2ms );
119101 TEST_ASSERT_FALSE (acquired);
120102}
121103
@@ -142,13 +124,13 @@ void test_override(void)
142124 Semaphore sem2 (0 , 1 );
143125 T timeout;
144126
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 );
146128
147- bool acquired = sem1.try_acquire_for (TEST_DELAY_MS );
129+ bool acquired = sem1.try_acquire_for (TEST_DELAY );
148130 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 );
150132
151- acquired = sem2.try_acquire_for (2 * TEST_DELAY_MS + 2 );
133+ acquired = sem2.try_acquire_for (2 * TEST_DELAY + 2ms );
152134 TEST_ASSERT_TRUE (acquired);
153135 acquired = sem1.try_acquire ();
154136 TEST_ASSERT_FALSE (acquired);
@@ -176,9 +158,9 @@ void test_multiple(void)
176158 volatile uint32_t callback_count = 0 ;
177159 T timeouts[NUM_TIMEOUTS];
178160 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 );
180162 }
181- ThisThread::sleep_for (TEST_DELAY_MS + 2 );
163+ ThisThread::sleep_for (TEST_DELAY + 2ms );
182164 TEST_ASSERT_EQUAL (NUM_TIMEOUTS, callback_count);
183165}
184166
@@ -200,7 +182,7 @@ void test_no_wait(void)
200182 for (int i = 0 ; i < 100 ; i++) {
201183 Semaphore sem (0 , 1 );
202184 T timeout;
203- timeout.attach_callback (mbed::callback (sem_callback, &sem), 0ULL );
185+ timeout.attach (mbed::callback (sem_callback, &sem), 0s );
204186 bool sem_acquired = sem.try_acquire ();
205187 TEST_ASSERT_EQUAL (true , sem_acquired);
206188 timeout.detach ();
@@ -227,11 +209,11 @@ void test_delay_accuracy(void)
227209 Timer timer;
228210
229211 timer.start ();
230- timeout.attach_callback (mbed::callback (sem_callback, &sem), delay_us);
212+ timeout.attach (mbed::callback (sem_callback, &sem), microseconds ( delay_us) );
231213
232214 sem.acquire ();
233215 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 ());
235217
236218 timeout.detach ();
237219}
@@ -262,15 +244,15 @@ void test_sleep(void)
262244
263245 sleep_manager_lock_deep_sleep ();
264246 timer.start ();
265- timeout.attach_callback (mbed::callback (sem_callback, &sem), delay_us);
247+ timeout.attach (mbed::callback (sem_callback, &sem), microseconds ( delay_us) );
266248
267249 bool deep_sleep_allowed = sleep_manager_can_deep_sleep_test_check ();
268250 TEST_ASSERT_FALSE_MESSAGE (deep_sleep_allowed, " Deep sleep should be disallowed" );
269251 sem.acquire ();
270252 timer.stop ();
271253
272254 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 ());
274256
275257 timeout.detach ();
276258}
@@ -316,17 +298,17 @@ void test_deepsleep(void)
316298 * hardware buffers are empty. However, such an API does not exist now,
317299 * so we'll use the ThisThread::sleep_for() function for now.
318300 */
319- ThisThread::sleep_for (20 );
301+ ThisThread::sleep_for (20ms );
320302
321303 timer.start ();
322- timeout.attach_callback (mbed::callback (sem_callback, &sem), delay_us);
304+ timeout.attach (mbed::callback (sem_callback, &sem), microseconds ( delay_us) );
323305
324306 bool deep_sleep_allowed = sleep_manager_can_deep_sleep_test_check ();
325307 TEST_ASSERT_TRUE_MESSAGE (deep_sleep_allowed, " Deep sleep should be allowed" );
326308 sem.acquire ();
327309 timer.stop ();
328310
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 ());
330312
331313 timeout.detach ();
332314}
@@ -336,14 +318,14 @@ void test_deepsleep(void)
336318template <typename TimeoutTesterType>
337319class TimeoutDriftTester {
338320public:
339- TimeoutDriftTester (us_timestamp_t period = 1000 ) :
321+ TimeoutDriftTester (microseconds period = 1ms ) :
340322 _callback_count (0 ), _period(period), _timeout()
341323 {
342324 }
343325
344326 void reschedule_callback (void )
345327 {
346- _timeout.attach_callback (mbed::callback (this , &TimeoutDriftTester::reschedule_callback), _period);
328+ _timeout.attach (mbed::callback (this , &TimeoutDriftTester::reschedule_callback), _period);
347329 _callback_count++;
348330 }
349331
@@ -359,7 +341,7 @@ class TimeoutDriftTester {
359341
360342private:
361343 volatile uint32_t _callback_count;
362- us_timestamp_t _period;
344+ microseconds _period;
363345 TimeoutTesterType _timeout;
364346};
365347
@@ -376,11 +358,10 @@ class TimeoutDriftTester {
376358template <typename T>
377359void test_reschedule (void )
378360{
379- volatile uint32_t callback_count = 0 ;
380- TimeoutDriftTester<T> timeout;
361+ TimeoutDriftTester<T> timeout (TEST_DELAY);
381362
382363 timeout.reschedule_callback ();
383- ThisThread::sleep_for (TEST_DELAY_MS * 5 );
364+ ThisThread::sleep_for (TEST_DELAY * 5 );
384365 TEST_ASSERT (timeout.get_callback_count () >= 3 );
385366}
386367
@@ -410,7 +391,7 @@ void test_drift(void)
410391 char _key[11 ] = { };
411392 char _value[128 ] = { };
412393 int expected_key = 1 ;
413- TimeoutDriftTester<T> timeout (DRIFT_TEST_PERIOD_US );
394+ TimeoutDriftTester<T> timeout (DRIFT_TEST_PERIOD );
414395
415396 greentea_send_kv (" timing_drift_check_start" , 0 );
416397 timeout.reschedule_callback ();
@@ -421,11 +402,11 @@ void test_drift(void)
421402 expected_key = strcmp (_key, " base_time" );
422403 } while (expected_key);
423404
424- 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 () );
425406
426407 // wait for 2nd signal from host
427408 greentea_parse_kv (_key, _value, sizeof (_key), sizeof (_value));
428- 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 () );
429410
430411 timeout.detach_callback ();
431412
0 commit comments