@@ -105,6 +105,7 @@ utest::v1::status_t testcase_setup(const Case *const source, const size_t index_
105
105
#if DEVICE_LPTICKER
106
106
lp_ticker_init ();
107
107
#endif
108
+
108
109
return utest::v1::greentea_case_setup_handler (source, index_of_case);
109
110
}
110
111
@@ -143,18 +144,25 @@ void test_sleep_auto()
143
144
const ticker_irq_handler_type lp_ticker_irq_handler_org = set_lp_ticker_irq_handler (lp_ticker_isr);
144
145
uint32_t us_ts1, us_ts2, lp_ts1, lp_ts2, us_diff1, us_diff2, lp_diff1, lp_diff2;
145
146
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);
151
153
152
154
/* Some targets may need an interrupt short time after LPTIM interrupt is
153
155
* set and forbid deep_sleep during that period. Let this period pass */
154
156
TEST_ASSERT_TRUE (sleep_manager_can_deep_sleep_test_check ());
155
157
156
158
sleep_manager_lock_deep_sleep ();
157
159
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
+
158
166
us_ts1 = us_ticker_read ();
159
167
lp_ts1 = lp_ticker_read ();
160
168
@@ -163,23 +171,25 @@ void test_sleep_auto()
163
171
us_ts2 = us_ticker_read ();
164
172
lp_ts2 = lp_ticker_read ();
165
173
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 );
168
176
169
177
// Deep sleep locked -- ordinary sleep mode used:
170
178
// * us_ticker powered ON,
171
179
// * lp_ticker powered ON,
172
180
// so both should increment equally.
173
181
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?" );
178
185
179
186
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 ());
181
190
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.
183
193
busy_wait_ms (SERIAL_FLUSH_TIME_MS);
184
194
185
195
/* Let's avoid the Lp ticker wrap-around case */
@@ -188,10 +198,6 @@ void test_sleep_auto()
188
198
lp_wakeup_ts = overflow_protect (lp_wakeup_ts_raw, lp_ticker_info->bits );
189
199
lp_ticker_set_interrupt (lp_wakeup_ts);
190
200
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
-
195
201
us_ts1 = us_ticker_read ();
196
202
lp_ts1 = lp_ticker_read ();
197
203
@@ -200,8 +206,8 @@ void test_sleep_auto()
200
206
us_ts2 = us_ticker_read ();
201
207
lp_ts2 = lp_ticker_read ();
202
208
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 );
205
211
206
212
// Deep sleep unlocked -- deep sleep mode used:
207
213
// * us_ticker powered OFF,
@@ -211,11 +217,9 @@ void test_sleep_auto()
211
217
// 1. current lp_ticker increment,
212
218
// 2. previous us_ticker increment (locked sleep test above)
213
219
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" );
219
223
220
224
set_us_ticker_irq_handler (us_ticker_irq_handler_org);
221
225
set_lp_ticker_irq_handler (lp_ticker_irq_handler_org);
0 commit comments