17 #ifndef MBED_TIMEOUT_TESTS_H 18 #define MBED_TIMEOUT_TESTS_H 21 #include "unity/unity.h" 25 #define NUM_TIMEOUTS 16 26 const microseconds DRIFT_TEST_PERIOD = 10ms;
28 const milliseconds TEST_DELAY = 10ms;
31 #define LONG_DELTA_US (100000) 32 #define SHORT_DELTA_US (2000) 34 void sem_callback(Semaphore *sem)
39 void cnt_callback(
volatile uint32_t *cnt)
57 void test_single_call(
void)
64 bool acquired = sem.try_acquire();
65 TEST_ASSERT_FALSE(acquired);
67 acquired = sem.try_acquire_for(TEST_DELAY + 2ms);
68 TEST_ASSERT_TRUE(acquired);
70 acquired = sem.try_acquire_for(TEST_DELAY + 2ms);
71 TEST_ASSERT_FALSE(acquired);
89 void test_cancel(
void)
94 timeout.attach(
mbed::callback(sem_callback, &sem), 2 * TEST_DELAY);
96 bool acquired = sem.try_acquire_for(TEST_DELAY);
97 TEST_ASSERT_FALSE(acquired);
100 acquired = sem.try_acquire_for(TEST_DELAY + 2ms);
101 TEST_ASSERT_FALSE(acquired);
121 void test_override(
void)
123 Semaphore sem1(0, 1);
124 Semaphore sem2(0, 1);
127 timeout.attach(
mbed::callback(sem_callback, &sem1), 2 * TEST_DELAY);
129 bool acquired = sem1.try_acquire_for(TEST_DELAY);
130 TEST_ASSERT_FALSE(acquired);
131 timeout.attach(
mbed::callback(sem_callback, &sem2), 2 * TEST_DELAY);
133 acquired = sem2.try_acquire_for(2 * TEST_DELAY + 2ms);
134 TEST_ASSERT_TRUE(acquired);
135 acquired = sem1.try_acquire();
136 TEST_ASSERT_FALSE(acquired);
156 void test_multiple(
void)
158 volatile uint32_t callback_count = 0;
159 T timeouts[NUM_TIMEOUTS];
160 for (
size_t i = 0; i < NUM_TIMEOUTS; i++) {
161 timeouts[i].attach(
mbed::callback(cnt_callback, &callback_count), TEST_DELAY);
163 ThisThread::sleep_for(TEST_DELAY + 2ms);
164 TEST_ASSERT_EQUAL(NUM_TIMEOUTS, callback_count);
180 void test_no_wait(
void)
182 for (
int i = 0; i < 100; i++) {
186 bool sem_acquired = sem.try_acquire();
187 TEST_ASSERT_EQUAL(
true, sem_acquired);
204 template<
typename T, us_timestamp_t delay_us, us_timestamp_t delta_us>
205 void test_delay_accuracy(
void)
212 timeout.attach(
mbed::callback(sem_callback, &sem), microseconds(delay_us));
216 TEST_ASSERT_UINT64_WITHIN(delta_us, delay_us, timer.elapsed_time().count());
238 template<
typename T, us_timestamp_t delay_us, us_timestamp_t delta_us>
239 void test_sleep(
void)
245 sleep_manager_lock_deep_sleep();
247 timeout.attach(
mbed::callback(sem_callback, &sem), microseconds(delay_us));
250 TEST_ASSERT_FALSE_MESSAGE(deep_sleep_allowed,
"Deep sleep should be disallowed");
254 sleep_manager_unlock_deep_sleep();
255 TEST_ASSERT_UINT64_WITHIN(delta_us, delay_us, timer.elapsed_time().count());
277 template<
typename T, us_timestamp_t delay_us, us_timestamp_t delta_us>
278 void test_deepsleep(
void)
301 ThisThread::sleep_for(20ms);
304 timeout.attach(
mbed::callback(sem_callback, &sem), microseconds(delay_us));
307 TEST_ASSERT_TRUE_MESSAGE(deep_sleep_allowed,
"Deep sleep should be allowed");
311 TEST_ASSERT_UINT64_WITHIN(delta_us, delay_us, timer.elapsed_time().count());
318 template<
typename TimeoutTesterType>
322 _callback_count(0), _period(period), _timeout()
326 void reschedule_callback(
void)
328 _timeout.attach(
mbed::callback(
this, &TimeoutDriftTester::reschedule_callback), _period);
332 void detach_callback(
void)
337 uint32_t get_callback_count(
void)
339 return _callback_count;
343 volatile uint32_t _callback_count;
344 microseconds _period;
345 TimeoutTesterType _timeout;
359 void test_reschedule(
void)
363 timeout.reschedule_callback();
364 ThisThread::sleep_for(TEST_DELAY * 5);
365 TEST_ASSERT(timeout.get_callback_count() >= 3);
389 void test_drift(
void)
392 char _value[128] = { };
393 int expected_key = 1;
396 greentea_send_kv(
"timing_drift_check_start", 0);
397 timeout.reschedule_callback();
401 greentea_parse_kv(_key, _value,
sizeof(_key),
sizeof(_value));
402 expected_key = strcmp(_key,
"base_time");
403 }
while (expected_key);
405 greentea_send_kv(_key, timeout.get_callback_count() * DRIFT_TEST_PERIOD.count());
408 greentea_parse_kv(_key, _value,
sizeof(_key),
sizeof(_value));
409 greentea_send_kv(_key, timeout.get_callback_count() * DRIFT_TEST_PERIOD.count());
411 timeout.detach_callback();
414 greentea_parse_kv(_key, _value,
sizeof(_key),
sizeof(_value));
416 TEST_ASSERT_EQUAL_STRING_MESSAGE(
"pass", _key,
"Host script reported a failure");
bool sleep_manager_can_deep_sleep_test_check(void)
Check if the target can deep sleep within a period of time.