16 #ifndef MBED_TIMEOUT_TESTS_H 17 #define MBED_TIMEOUT_TESTS_H 20 #include "unity/unity.h" 22 #define NUM_TIMEOUTS 16 23 #define DRIFT_TEST_PERIOD_US 10000 25 const float TEST_DELAY_S = 0.01;
26 const uint32_t TEST_DELAY_MS = 1000.0F * TEST_DELAY_S;
30 #define LONG_DELTA_US (100000) 31 #define SHORT_DELTA_US (2000) 33 void sem_callback(Semaphore *sem)
38 void cnt_callback(
volatile uint32_t *cnt)
43 template<
typename TimeoutType>
48 TimeoutType::attach(func, (
float) delay_us / 1000000.0f);
52 template<
typename TimeoutType>
57 TimeoutType::attach_us(func, delay_us);
74 void test_single_call(
void)
79 timeout.attach_callback(
mbed::callback(sem_callback, &sem), TEST_DELAY_US);
81 bool acquired = sem.try_acquire();
82 TEST_ASSERT_FALSE(acquired);
84 acquired = sem.try_acquire_for(TEST_DELAY_MS + 2);
85 TEST_ASSERT_TRUE(acquired);
87 acquired = sem.try_acquire_for(TEST_DELAY_MS + 2);
88 TEST_ASSERT_FALSE(acquired);
106 void test_cancel(
void)
111 timeout.attach_callback(
mbed::callback(sem_callback, &sem), 2.0f * TEST_DELAY_US);
113 bool acquired = sem.try_acquire_for(TEST_DELAY_MS);
114 TEST_ASSERT_FALSE(acquired);
117 acquired = sem.try_acquire_for(TEST_DELAY_MS + 2);
118 TEST_ASSERT_FALSE(acquired);
138 void test_override(
void)
140 Semaphore sem1(0, 1);
141 Semaphore sem2(0, 1);
144 timeout.attach_callback(
mbed::callback(sem_callback, &sem1), 2.0f * TEST_DELAY_US);
146 bool acquired = sem1.try_acquire_for(TEST_DELAY_MS);
147 TEST_ASSERT_FALSE(acquired);
148 timeout.attach_callback(
mbed::callback(sem_callback, &sem2), 2.0f * TEST_DELAY_US);
150 acquired = sem2.try_acquire_for(2 * TEST_DELAY_MS + 2);
151 TEST_ASSERT_TRUE(acquired);
152 acquired = sem1.try_acquire();
153 TEST_ASSERT_FALSE(acquired);
173 void test_multiple(
void)
175 volatile uint32_t callback_count = 0;
176 T timeouts[NUM_TIMEOUTS];
177 for (
size_t i = 0; i < NUM_TIMEOUTS; i++) {
178 timeouts[i].attach_callback(
mbed::callback(cnt_callback, &callback_count), TEST_DELAY_US);
180 ThisThread::sleep_for(TEST_DELAY_MS + 2);
181 TEST_ASSERT_EQUAL(NUM_TIMEOUTS, callback_count);
197 void test_no_wait(
void)
199 for (
int i = 0; i < 100; i++) {
202 timeout.attach_callback(
mbed::callback(sem_callback, &sem), 0ULL);
203 int32_t sem_slots = sem.wait(0);
204 TEST_ASSERT_EQUAL(1, sem_slots);
221 template<
typename T, us_timestamp_t delay_us, us_timestamp_t delta_us>
222 void test_delay_accuracy(
void)
229 timeout.attach_callback(
mbed::callback(sem_callback, &sem), delay_us);
233 TEST_ASSERT_UINT64_WITHIN(delta_us, delay_us, timer.read_high_resolution_us());
255 template<
typename T, us_timestamp_t delay_us, us_timestamp_t delta_us>
256 void test_sleep(
void)
262 sleep_manager_lock_deep_sleep();
264 timeout.attach_callback(
mbed::callback(sem_callback, &sem), delay_us);
267 TEST_ASSERT_FALSE_MESSAGE(deep_sleep_allowed,
"Deep sleep should be disallowed");
271 sleep_manager_unlock_deep_sleep();
272 TEST_ASSERT_UINT64_WITHIN(delta_us, delay_us, timer.read_high_resolution_us());
294 template<
typename T, us_timestamp_t delay_us, us_timestamp_t delta_us>
295 void test_deepsleep(
void)
321 timeout.attach_callback(
mbed::callback(sem_callback, &sem), delay_us);
324 TEST_ASSERT_TRUE_MESSAGE(deep_sleep_allowed,
"Deep sleep should be allowed");
328 TEST_ASSERT_UINT64_WITHIN(delta_us, delay_us, timer.read_high_resolution_us());
335 template<
typename TimeoutTesterType>
339 _callback_count(0), _period(period), _timeout()
343 void reschedule_callback(
void)
345 _timeout.attach_callback(
mbed::callback(
this, &TimeoutDriftTester::reschedule_callback), _period);
349 void detach_callback(
void)
354 uint32_t get_callback_count(
void)
356 return _callback_count;
360 volatile uint32_t _callback_count;
362 TimeoutTesterType _timeout;
386 void test_drift(
void)
389 char _value[128] = { };
390 int expected_key = 1;
393 greentea_send_kv(
"timing_drift_check_start", 0);
394 timeout.reschedule_callback();
398 greentea_parse_kv(_key, _value,
sizeof(_key),
sizeof(_value));
399 expected_key = strcmp(_key,
"base_time");
400 }
while (expected_key);
402 greentea_send_kv(_key, timeout.get_callback_count() * DRIFT_TEST_PERIOD_US);
405 greentea_parse_kv(_key, _value,
sizeof(_key),
sizeof(_value));
406 greentea_send_kv(_key, timeout.get_callback_count() * DRIFT_TEST_PERIOD_US);
408 timeout.detach_callback();
411 greentea_parse_kv(_key, _value,
sizeof(_key),
sizeof(_value));
413 TEST_ASSERT_EQUAL_STRING_MESSAGE(
"pass", _key,
"Host script reported a failure");
uint64_t us_timestamp_t
A us timestamp stored in a 64 bit integer.
bool sleep_manager_can_deep_sleep_test_check(void)
Check if the target can deep sleep within a period of time.
Callback class based on template specialization.