18
18
#include " unity/unity.h"
19
19
#include " greentea-client/test_env.h"
20
20
#include < limits.h>
21
+ #include < cinttypes>
21
22
#include " mbed.h"
22
23
#include " mbed_lp_ticker_wrapper.h"
23
24
#include " hal/us_ticker_api.h"
31
32
#define SLEEP_DURATION_US 50000ULL
32
33
33
34
// Tolerance for extra sleep time in the deep sleep test.
35
+ // This accounts for the time that the processor spends going to sleep and waking up.
36
+ // The hal_deepsleep() docs specify this to be less than 10ms
34
37
// Current leader is the MIMXRT105x, which takes almost 5ms to enter/exit deep sleep.
35
38
#define DEEP_SLEEP_TOLERANCE_US 5000ULL
36
39
@@ -147,7 +150,7 @@ void test_sleep_auto()
147
150
const ticker_info_t *lp_ticker_info = get_lp_ticker_data ()->interface ->get_info ();
148
151
const unsigned lp_ticker_mask = ((1 << lp_ticker_info->bits ) - 1 );
149
152
const ticker_irq_handler_type lp_ticker_irq_handler_org = set_lp_ticker_irq_handler (lp_ticker_isr);
150
- uint32_t us_ts1, us_ts2, lp_ts1, lp_ts2, us_diff1, us_diff2, lp_diff1, lp_diff2;
153
+ uint32_t us_diff1, us_diff2, lp_diff1, lp_diff2;
151
154
152
155
const unsigned int sleep_duration_lp_ticks = us_to_ticks (SLEEP_DURATION_US, lp_ticker_info->frequency );
153
156
const unsigned int sleep_duration_us_ticks = us_to_ticks (SLEEP_DURATION_US, us_ticker_info->frequency );
@@ -156,28 +159,28 @@ void test_sleep_auto()
156
159
// interrupts on some targets, which wake us from sleep.
157
160
busy_wait_ms (SERIAL_FLUSH_TIME_MS);
158
161
159
- /* Some targets may need an interrupt short time after LPTIM interrupt is
160
- * set and forbid deep_sleep during that period. Let this period pass */
161
- TEST_ASSERT_TRUE (sleep_manager_can_deep_sleep_test_check ());
162
-
163
- sleep_manager_lock_deep_sleep ();
164
-
165
162
/* Let's avoid the Lp ticker wrap-around case */
166
163
wraparound_lp_protect ();
167
- uint32_t lp_wakeup_ts_raw = lp_ticker_read () + sleep_duration_lp_ticks;
164
+
165
+ uint32_t start_lp_time = lp_ticker_read ();
166
+ uint32_t start_us_time = us_ticker_read ();
167
+ uint32_t lp_wakeup_ts_raw = start_lp_time + sleep_duration_lp_ticks;
168
168
timestamp_t lp_wakeup_ts = overflow_protect (lp_wakeup_ts_raw, lp_ticker_info->bits );
169
169
lp_ticker_set_interrupt (lp_wakeup_ts);
170
170
171
- us_ts1 = us_ticker_read ();
172
- lp_ts1 = lp_ticker_read ();
171
+ /* Some targets may need an interrupt short time after LPTIM interrupt is
172
+ * set and forbid deep_sleep during that period. Let this period pass */
173
+ TEST_ASSERT_TRUE (sleep_manager_can_deep_sleep_test_check ());
174
+
175
+ sleep_manager_lock_deep_sleep ();
173
176
174
177
sleep_manager_sleep_auto ();
175
178
176
- us_ts2 = us_ticker_read ();
177
- lp_ts2 = lp_ticker_read ();
179
+ uint32_t end_us_time = us_ticker_read ();
180
+ uint32_t end_lp_time = lp_ticker_read ();
178
181
179
- us_diff1 = (us_ts1 <= us_ts2 ) ? (us_ts2 - us_ts1 ) : (us_ticker_mask - us_ts1 + us_ts2 + 1 );
180
- lp_diff1 = (lp_ts1 <= lp_ts2 ) ? (lp_ts2 - lp_ts1 ) : (lp_ticker_mask - lp_ts1 + lp_ts2 + 1 );
182
+ us_diff1 = (start_us_time <= end_us_time ) ? (end_us_time - start_us_time ) : (us_ticker_mask - start_us_time + end_us_time + 1 );
183
+ lp_diff1 = (start_lp_time <= end_lp_time ) ? (end_lp_time - start_lp_time ) : (lp_ticker_mask - start_lp_time + end_lp_time + 1 );
181
184
182
185
// Deep sleep locked -- ordinary sleep mode used:
183
186
// * us_ticker powered ON,
@@ -189,30 +192,30 @@ void test_sleep_auto()
189
192
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?" );
190
193
191
194
sleep_manager_unlock_deep_sleep ();
192
- /* Some targets may need an interrupt short time after LPTIM interrupt is
193
- * set and forbid deep_sleep during that period. Let this period pass */
194
- TEST_ASSERT_TRUE (sleep_manager_can_deep_sleep_test_check ());
195
195
196
196
// Wait for hardware serial buffers to flush. This is because serial transmissions generate
197
197
// interrupts on some targets, which wake us from sleep.
198
198
busy_wait_ms (SERIAL_FLUSH_TIME_MS);
199
199
200
200
/* Let's avoid the Lp ticker wrap-around case */
201
201
wraparound_lp_protect ();
202
- lp_wakeup_ts_raw = lp_ticker_read () + us_to_ticks (SLEEP_DURATION_US, lp_ticker_info->frequency );
202
+ start_lp_time = lp_ticker_read ();
203
+ start_us_time = us_ticker_read ();
204
+ lp_wakeup_ts_raw = start_lp_time + us_to_ticks (SLEEP_DURATION_US, lp_ticker_info->frequency );
203
205
lp_wakeup_ts = overflow_protect (lp_wakeup_ts_raw, lp_ticker_info->bits );
204
206
lp_ticker_set_interrupt (lp_wakeup_ts);
205
207
206
- us_ts1 = us_ticker_read ();
207
- lp_ts1 = lp_ticker_read ();
208
+ /* Some targets may need an interrupt short time after LPTIM interrupt is
209
+ * set and forbid deep_sleep during that period. Let this period pass */
210
+ TEST_ASSERT_TRUE (sleep_manager_can_deep_sleep_test_check ());
208
211
209
212
sleep_manager_sleep_auto ();
210
213
211
- us_ts2 = us_ticker_read ();
212
- lp_ts2 = lp_ticker_read ();
214
+ end_us_time = us_ticker_read ();
215
+ end_lp_time = lp_ticker_read ();
213
216
214
- us_diff2 = (us_ts1 <= us_ts2 ) ? (us_ts2 - us_ts1 ) : (us_ticker_mask - us_ts1 + us_ts2 + 1 );
215
- lp_diff2 = (lp_ts1 <= lp_ts2 ) ? (lp_ts2 - lp_ts1 ) : (lp_ticker_mask - lp_ts1 + lp_ts2 + 1 );
217
+ us_diff2 = (start_us_time <= end_us_time ) ? (end_us_time - start_us_time ) : (us_ticker_mask - start_us_time + end_us_time + 1 );
218
+ lp_diff2 = (start_lp_time <= end_lp_time ) ? (end_lp_time - start_lp_time ) : (lp_ticker_mask - start_lp_time + end_lp_time + 1 );
216
219
217
220
// Deep sleep unlocked -- deep sleep mode used:
218
221
// * us_ticker powered OFF,
@@ -225,6 +228,7 @@ void test_sleep_auto()
225
228
const unsigned int deepsleep_tolerance_lp_ticks = us_to_ticks (DEEP_SLEEP_TOLERANCE_US, lp_ticker_info->frequency );
226
229
const unsigned int deepsleep_tolerance_us_ticks = us_to_ticks (DEEP_SLEEP_TOLERANCE_US, us_ticker_info->frequency );
227
230
231
+
228
232
// us ticker should not have incremented during deep sleep. It should be zero, plus some tolerance for the time to enter deep sleep.
229
233
TEST_ASSERT_UINT64_WITHIN_MESSAGE (deepsleep_tolerance_us_ticks, 0 , us_diff2, " us ticker sleep time incorrect - perhaps deep sleep mode was not used?" );
230
234
TEST_ASSERT_UINT64_WITHIN_MESSAGE (deepsleep_tolerance_lp_ticks, sleep_duration_lp_ticks, lp_diff2, " lp ticker sleep time incorrect" );
0 commit comments