Skip to content

Commit 022d4f4

Browse files
author
Jamie Smith
authored
Fix hal-sleep-manager test on nRF52 and clean it up in general (#74)
1 parent 32a9080 commit 022d4f4

File tree

1 file changed

+28
-24
lines changed
  • hal/tests/TESTS/mbed_hal/sleep_manager

1 file changed

+28
-24
lines changed

hal/tests/TESTS/mbed_hal/sleep_manager/main.cpp

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ utest::v1::status_t testcase_setup(const Case *const source, const size_t index_
105105
#if DEVICE_LPTICKER
106106
lp_ticker_init();
107107
#endif
108+
108109
return utest::v1::greentea_case_setup_handler(source, index_of_case);
109110
}
110111

@@ -143,18 +144,25 @@ void test_sleep_auto()
143144
const ticker_irq_handler_type lp_ticker_irq_handler_org = set_lp_ticker_irq_handler(lp_ticker_isr);
144145
uint32_t us_ts1, us_ts2, lp_ts1, lp_ts2, us_diff1, us_diff2, lp_diff1, lp_diff2;
145146

146-
/* Let's avoid the Lp ticker wrap-around case */
147-
wraparound_lp_protect();
148-
uint32_t lp_wakeup_ts_raw = lp_ticker_read() + us_to_ticks(SLEEP_DURATION_US, lp_ticker_info->frequency);
149-
timestamp_t lp_wakeup_ts = overflow_protect(lp_wakeup_ts_raw, lp_ticker_info->bits);
150-
lp_ticker_set_interrupt(lp_wakeup_ts);
147+
const unsigned int sleep_duration_lp_ticks = us_to_ticks(SLEEP_DURATION_US, lp_ticker_info->frequency);
148+
const unsigned int sleep_duration_us_ticks = us_to_ticks(SLEEP_DURATION_US, us_ticker_info->frequency);
149+
150+
// Wait for hardware serial buffers to flush. This is because serial transmissions generate
151+
// interrupts on some targets, which wake us from sleep.
152+
busy_wait_ms(SERIAL_FLUSH_TIME_MS);
151153

152154
/* Some targets may need an interrupt short time after LPTIM interrupt is
153155
* set and forbid deep_sleep during that period. Let this period pass */
154156
TEST_ASSERT_TRUE(sleep_manager_can_deep_sleep_test_check());
155157

156158
sleep_manager_lock_deep_sleep();
157159

160+
/* Let's avoid the Lp ticker wrap-around case */
161+
wraparound_lp_protect();
162+
uint32_t lp_wakeup_ts_raw = lp_ticker_read() + sleep_duration_lp_ticks;
163+
timestamp_t lp_wakeup_ts = overflow_protect(lp_wakeup_ts_raw, lp_ticker_info->bits);
164+
lp_ticker_set_interrupt(lp_wakeup_ts);
165+
158166
us_ts1 = us_ticker_read();
159167
lp_ts1 = lp_ticker_read();
160168

@@ -163,23 +171,25 @@ void test_sleep_auto()
163171
us_ts2 = us_ticker_read();
164172
lp_ts2 = lp_ticker_read();
165173

166-
us_diff1 = ticks_to_us((us_ts1 <= us_ts2) ? (us_ts2 - us_ts1) : (us_ticker_mask - us_ts1 + us_ts2 + 1), us_ticker_info->frequency);
167-
lp_diff1 = ticks_to_us((lp_ts1 <= lp_ts2) ? (lp_ts2 - lp_ts1) : (lp_ticker_mask - lp_ts1 + lp_ts2 + 1), lp_ticker_info->frequency);
174+
us_diff1 = (us_ts1 <= us_ts2) ? (us_ts2 - us_ts1) : (us_ticker_mask - us_ts1 + us_ts2 + 1);
175+
lp_diff1 = (lp_ts1 <= lp_ts2) ? (lp_ts2 - lp_ts1) : (lp_ticker_mask - lp_ts1 + lp_ts2 + 1);
168176

169177
// Deep sleep locked -- ordinary sleep mode used:
170178
// * us_ticker powered ON,
171179
// * lp_ticker powered ON,
172180
// so both should increment equally.
173181

174-
// Verify us and lp tickers incremented equally, with 10% tolerance.
175-
TEST_ASSERT_UINT64_WITHIN_MESSAGE(
176-
SLEEP_DURATION_US / 10ULL, lp_diff1, us_diff1,
177-
"Deep sleep mode locked, but still used");
182+
// Verify us and lp tickers incremented the expected amount, with 10% tolerance.
183+
TEST_ASSERT_UINT64_WITHIN_MESSAGE(sleep_duration_lp_ticks / 10ULL, sleep_duration_lp_ticks, lp_diff1, "lp ticker sleep time incorrect");
184+
TEST_ASSERT_UINT64_WITHIN_MESSAGE(sleep_duration_us_ticks / 10ULL, sleep_duration_us_ticks, us_diff1, "us ticker sleep time incorrect - perhaps deep sleep mode was used?");
178185

179186
sleep_manager_unlock_deep_sleep();
180-
TEST_ASSERT_TRUE(sleep_manager_can_deep_sleep());
187+
/* Some targets may need an interrupt short time after LPTIM interrupt is
188+
* set and forbid deep_sleep during that period. Let this period pass */
189+
TEST_ASSERT_TRUE(sleep_manager_can_deep_sleep_test_check());
181190

182-
// Wait for hardware serial buffers to flush.
191+
// Wait for hardware serial buffers to flush. This is because serial transmissions generate
192+
// interrupts on some targets, which wake us from sleep.
183193
busy_wait_ms(SERIAL_FLUSH_TIME_MS);
184194

185195
/* Let's avoid the Lp ticker wrap-around case */
@@ -188,10 +198,6 @@ void test_sleep_auto()
188198
lp_wakeup_ts = overflow_protect(lp_wakeup_ts_raw, lp_ticker_info->bits);
189199
lp_ticker_set_interrupt(lp_wakeup_ts);
190200

191-
/* Some targets may need an interrupt short time after LPTIM interrupt is
192-
* set and forbid deep_sleep during that period. Let this period pass */
193-
TEST_ASSERT_TRUE(sleep_manager_can_deep_sleep_test_check());
194-
195201
us_ts1 = us_ticker_read();
196202
lp_ts1 = lp_ticker_read();
197203

@@ -200,8 +206,8 @@ void test_sleep_auto()
200206
us_ts2 = us_ticker_read();
201207
lp_ts2 = lp_ticker_read();
202208

203-
us_diff2 = ticks_to_us((us_ts1 <= us_ts2) ? (us_ts2 - us_ts1) : (us_ticker_mask - us_ts1 + us_ts2 + 1), us_ticker_info->frequency);
204-
lp_diff2 = ticks_to_us((lp_ts1 <= lp_ts2) ? (lp_ts2 - lp_ts1) : (lp_ticker_mask - lp_ts1 + lp_ts2 + 1), lp_ticker_info->frequency);
209+
us_diff2 = (us_ts1 <= us_ts2) ? (us_ts2 - us_ts1) : (us_ticker_mask - us_ts1 + us_ts2 + 1);
210+
lp_diff2 = (lp_ts1 <= lp_ts2) ? (lp_ts2 - lp_ts1) : (lp_ticker_mask - lp_ts1 + lp_ts2 + 1);
205211

206212
// Deep sleep unlocked -- deep sleep mode used:
207213
// * us_ticker powered OFF,
@@ -211,11 +217,9 @@ void test_sleep_auto()
211217
// 1. current lp_ticker increment,
212218
// 2. previous us_ticker increment (locked sleep test above)
213219

214-
// Verify that the current us_ticker increment:
215-
// 1. is at most 10% of lp_ticker increment
216-
// 2. is at most 10% of previous us_ticker increment.
217-
TEST_ASSERT_MESSAGE(us_diff2 < lp_diff2 / 10ULL, "Deep sleep mode unlocked, but not used");
218-
TEST_ASSERT_MESSAGE(us_diff2 < us_diff1 / 10ULL, "Deep sleep mode unlocked, but not used");
220+
// us ticker should not have incremented during deep sleep. It should be zero, plus some tolerance for the time to enter deep sleep.
221+
TEST_ASSERT_UINT64_WITHIN_MESSAGE(sleep_duration_us_ticks / 10ULL, 0, us_diff2, "us ticker sleep time incorrect - perhaps deep sleep mode was not used?");
222+
TEST_ASSERT_UINT64_WITHIN_MESSAGE(sleep_duration_lp_ticks / 10ULL, sleep_duration_lp_ticks, lp_diff2, "lp ticker sleep time incorrect");
219223

220224
set_us_ticker_irq_handler(us_ticker_irq_handler_org);
221225
set_lp_ticker_irq_handler(lp_ticker_irq_handler_org);

0 commit comments

Comments
 (0)