Skip to content

Commit 3fcd8c4

Browse files
author
Filip Jagodzinski
committed
Tests: SleepManager: Fix sleep_auto test
Use us & lp tickers directly, without the common ticker layer.
1 parent 9e34f76 commit 3fcd8c4

File tree

1 file changed

+74
-50
lines changed

1 file changed

+74
-50
lines changed

TESTS/mbed_hal/sleep_manager/main.cpp

Lines changed: 74 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
#include <limits.h>
2020
#include "mbed.h"
2121
#include "mbed_lp_ticker_wrapper.h"
22+
#include "../sleep/sleep_test_utils.h"
2223
#include "sleep_manager_api_tests.h"
2324

2425
#if !DEVICE_SLEEP
2526
#error [NOT_SUPPORTED] test not supported
2627
#endif
2728

28-
#define SLEEP_DURATION_US 100000ULL
29-
#define SERIAL_FLUSH_TIME_MS 20
29+
#define SLEEP_DURATION_US 20000ULL
3030
#define DEEP_SLEEP_TEST_CHECK_WAIT_US 2000
3131
#define DEEP_SLEEP_TEST_CHECK_WAIT_DELTA_US 500
3232

@@ -110,9 +110,38 @@ void test_lock_gt_ushrt_max()
110110

111111
#if DEVICE_LPTICKER
112112
#if DEVICE_USTICKER
113-
void wakeup_callback(volatile int *wakeup_flag)
113+
utest::v1::status_t testcase_setup(const Case * const source, const size_t index_of_case)
114114
{
115-
(*wakeup_flag)++;
115+
// Suspend the RTOS kernel scheduler to prevent interference with duration of sleep.
116+
osKernelSuspend();
117+
#if DEVICE_LPTICKER
118+
ticker_suspend(get_lp_ticker_data());
119+
#if (LPTICKER_DELAY_TICKS > 0)
120+
// Suspend the low power ticker wrapper to prevent interference with deep sleep lock.
121+
lp_ticker_wrapper_suspend();
122+
#endif
123+
#endif
124+
ticker_suspend(get_us_ticker_data());
125+
// Make sure HAL tickers are initialized.
126+
us_ticker_init();
127+
#if DEVICE_LPTICKER
128+
lp_ticker_init();
129+
#endif
130+
return utest::v1::greentea_case_setup_handler(source, index_of_case);
131+
}
132+
133+
utest::v1::status_t testcase_teardown(const Case * const source, const size_t passed, const size_t failed,
134+
const utest::v1::failure_t failure)
135+
{
136+
ticker_resume(get_us_ticker_data());
137+
#if DEVICE_LPTICKER
138+
#if (LPTICKER_DELAY_TICKS > 0)
139+
lp_ticker_wrapper_resume();
140+
#endif
141+
ticker_resume(get_lp_ticker_data());
142+
#endif
143+
osKernelResume(0);
144+
return utest::v1::greentea_case_teardown_handler(source, passed, failed, failure);
116145
}
117146

118147
/* This test is based on the fact that the high-speed clocks are turned off
@@ -124,22 +153,26 @@ void wakeup_callback(volatile int *wakeup_flag)
124153
*/
125154
void test_sleep_auto()
126155
{
127-
const ticker_data_t *const us_ticker = get_us_ticker_data();
128-
const ticker_data_t *const lp_ticker = get_lp_ticker_data();
129-
us_timestamp_t us_ts, lp_ts, us_diff1, us_diff2, lp_diff1, lp_diff2;
130-
LowPowerTimeout lp_timeout;
156+
const ticker_info_t *us_ticker_info = get_us_ticker_data()->interface->get_info();
157+
const unsigned us_ticker_mask = ((1 << us_ticker_info->bits) - 1);
158+
const ticker_irq_handler_type us_ticker_irq_handler_org = set_us_ticker_irq_handler(us_ticker_isr);
159+
const ticker_info_t *lp_ticker_info = get_lp_ticker_data()->interface->get_info();
160+
const unsigned lp_ticker_mask = ((1 << lp_ticker_info->bits) - 1);
161+
const ticker_irq_handler_type lp_ticker_irq_handler_org = set_lp_ticker_irq_handler(lp_ticker_isr);
162+
us_timestamp_t us_ts1, us_ts2, lp_ts1, lp_ts2, us_diff1, us_diff2, lp_diff1, lp_diff2;
131163

132164
sleep_manager_lock_deep_sleep();
133-
volatile int wakeup_flag = 0;
134-
lp_timeout.attach_us(mbed::callback(wakeup_callback, &wakeup_flag), SLEEP_DURATION_US);
135-
us_ts = ticker_read_us(us_ticker);
136-
lp_ts = ticker_read_us(lp_ticker);
137-
138-
while (wakeup_flag == 0) {
139-
sleep_manager_sleep_auto();
140-
}
141-
us_diff1 = ticker_read_us(us_ticker) - us_ts;
142-
lp_diff1 = ticker_read_us(lp_ticker) - lp_ts;
165+
uint32_t lp_wakeup_ts_raw = lp_ticker_read() + us_to_ticks(SLEEP_DURATION_US, lp_ticker_info->frequency);
166+
timestamp_t lp_wakeup_ts = overflow_protect(lp_wakeup_ts_raw, lp_ticker_info->bits);
167+
lp_ticker_set_interrupt(lp_wakeup_ts);
168+
us_ts1 = ticks_to_us(us_ticker_read(), us_ticker_info->frequency);
169+
lp_ts1 = ticks_to_us(lp_ticker_read(), lp_ticker_info->frequency);
170+
171+
sleep_manager_sleep_auto();
172+
us_ts2 = ticks_to_us(us_ticker_read(), us_ticker_info->frequency);
173+
us_diff1 = (us_ts1 <= us_ts2) ? (us_ts2 - us_ts1) : (us_ticker_mask - us_ts1 + us_ts2 + 1);
174+
lp_ts2 = ticks_to_us(lp_ticker_read(), lp_ticker_info->frequency);
175+
lp_diff1 = (lp_ts1 <= lp_ts2) ? (lp_ts2 - lp_ts1) : (lp_ticker_mask - lp_ts1 + lp_ts2 + 1);
143176

144177
// Deep sleep locked -- ordinary sleep mode used:
145178
// * us_ticker powered ON,
@@ -155,18 +188,19 @@ void test_sleep_auto()
155188
TEST_ASSERT_TRUE(sleep_manager_can_deep_sleep());
156189

157190
// Wait for hardware serial buffers to flush.
158-
wait_ms(SERIAL_FLUSH_TIME_MS);
191+
busy_wait_ms(SERIAL_FLUSH_TIME_MS);
159192

160-
wakeup_flag = 0;
161-
lp_timeout.attach_us(mbed::callback(wakeup_callback, &wakeup_flag), SLEEP_DURATION_US);
162-
us_ts = ticker_read_us(us_ticker);
163-
lp_ts = ticker_read_us(lp_ticker);
193+
lp_wakeup_ts_raw = lp_ticker_read() + us_to_ticks(SLEEP_DURATION_US, lp_ticker_info->frequency);
194+
lp_wakeup_ts = overflow_protect(lp_wakeup_ts_raw, lp_ticker_info->bits);
195+
lp_ticker_set_interrupt(lp_wakeup_ts);
196+
us_ts1 = ticks_to_us(us_ticker_read(), us_ticker_info->frequency);
197+
lp_ts1 = ticks_to_us(lp_ticker_read(), lp_ticker_info->frequency);
164198

165-
while (wakeup_flag == 0) {
166-
sleep_manager_sleep_auto();
167-
}
168-
us_diff2 = ticker_read_us(us_ticker) - us_ts;
169-
lp_diff2 = ticker_read_us(lp_ticker) - lp_ts;
199+
sleep_manager_sleep_auto();
200+
us_ts2 = ticks_to_us(us_ticker_read(), us_ticker_info->frequency);
201+
us_diff2 = (us_ts1 <= us_ts2) ? (us_ts2 - us_ts1) : (us_ticker_mask - us_ts1 + us_ts2 + 1);
202+
lp_ts2 = ticks_to_us(lp_ticker_read(), lp_ticker_info->frequency);
203+
lp_diff2 = (lp_ts1 <= lp_ts2) ? (lp_ts2 - lp_ts1) : (lp_ticker_mask - lp_ts1 + lp_ts2 + 1);
170204

171205
// Deep sleep unlocked -- deep sleep mode used:
172206
// * us_ticker powered OFF,
@@ -181,11 +215,18 @@ void test_sleep_auto()
181215
// 2. is at most 10% of previous us_ticker increment.
182216
TEST_ASSERT_MESSAGE(us_diff2 < lp_diff2 / 10ULL, "Deep sleep mode unlocked, but not used");
183217
TEST_ASSERT_MESSAGE(us_diff2 < us_diff1 / 10ULL, "Deep sleep mode unlocked, but not used");
218+
219+
set_us_ticker_irq_handler(us_ticker_irq_handler_org);
220+
set_lp_ticker_irq_handler(lp_ticker_irq_handler_org);
184221
}
185222
#endif
186223

187224
void test_lock_unlock_test_check()
188225
{
226+
// Make sure HAL tickers are initialized.
227+
ticker_read(get_us_ticker_data());
228+
ticker_read(get_lp_ticker_data());
229+
189230
// Use LowPowerTimer instead of Timer to prevent deep sleep lock.
190231
LowPowerTimer lp_timer;
191232
us_timestamp_t exec_time_unlocked, exec_time_locked;
@@ -233,43 +274,26 @@ void test_lock_unlock_test_check()
233274
utest::v1::status_t testsuite_setup(const size_t number_of_cases)
234275
{
235276
GREENTEA_SETUP(10, "default_auto");
236-
// Suspend the RTOS kernel scheduler to prevent interference with duration of sleep.
237-
osKernelSuspend();
238-
#if DEVICE_LPTICKER && (LPTICKER_DELAY_TICKS > 0)
239-
// Suspend the low power ticker wrapper to prevent interference with deep sleep lock.
240-
lp_ticker_wrapper_suspend();
241-
#endif
242-
#if DEVICE_LPTICKER && DEVICE_USTICKER
243-
// Make sure HAL tickers are initialized.
244-
ticker_read(get_us_ticker_data());
245-
ticker_read(get_lp_ticker_data());
246-
#endif
247277
return utest::v1::greentea_test_setup_handler(number_of_cases);
248278
}
249279

250-
void testsuite_teardown(const size_t passed, const size_t failed, const utest::v1::failure_t failure)
251-
{
252-
#if DEVICE_LPTICKER && (LPTICKER_DELAY_TICKS > 0)
253-
lp_ticker_wrapper_resume();
254-
#endif
255-
osKernelResume(0);
256-
utest::v1::greentea_test_teardown_handler(passed, failed, failure);
257-
}
258-
259280
Case cases[] = {
260281
Case("deep sleep lock/unlock", test_lock_unlock),
261282
Case("deep sleep unbalanced unlock", test_lone_unlock),
262283
Case("deep sleep locked USHRT_MAX times", test_lock_eq_ushrt_max),
263284
Case("deep sleep locked more than USHRT_MAX times", test_lock_gt_ushrt_max),
264285
#if DEVICE_LPTICKER
265286
#if DEVICE_USTICKER
266-
Case("sleep_auto calls sleep/deep sleep based on lock", test_sleep_auto),
287+
Case("sleep_auto calls sleep/deep sleep based on lock",
288+
(utest::v1::case_setup_handler_t) testcase_setup,
289+
test_sleep_auto,
290+
(utest::v1::case_teardown_handler_t) testcase_teardown),
267291
#endif
268292
Case("deep sleep lock/unlock test_check", test_lock_unlock_test_check),
269293
#endif
270294
};
271295

272-
Specification specification(testsuite_setup, cases, testsuite_teardown);
296+
Specification specification(testsuite_setup, cases);
273297

274298
int main()
275299
{

0 commit comments

Comments
 (0)