@@ -118,18 +118,6 @@ bool common_hal_alarm_woken_from_sleep(void) {
118
118
|| cause == NRF_SLEEP_WAKEUP_TOUCHPAD );
119
119
}
120
120
121
- nrf_sleep_source_t alarm_woken_from_sleep_2 (void ) {
122
- nrf_sleep_source_t cause = _get_wakeup_cause ();
123
- if (cause == NRF_SLEEP_WAKEUP_GPIO ||
124
- cause == NRF_SLEEP_WAKEUP_TIMER ||
125
- cause == NRF_SLEEP_WAKEUP_TOUCHPAD ) {
126
- return cause ;
127
- }
128
- else {
129
- return NRF_SLEEP_WAKEUP_UNDEFINED ;
130
- }
131
- }
132
-
133
121
STATIC mp_obj_t _get_wake_alarm (size_t n_alarms , const mp_obj_t * alarms ) {
134
122
nrf_sleep_source_t cause = _get_wakeup_cause ();
135
123
switch (cause ) {
@@ -163,7 +151,7 @@ STATIC void _setup_sleep_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t
163
151
// TODO: this handles all possible types of wakeup, which is redundant with main.
164
152
// revise to extract all parts essential to enabling sleep wakeup, but leave the
165
153
// alarm/non-alarm sorting to the existing main loop.
166
- nrf_sleep_source_t system_on_idle_until_alarm (int64_t timediff_ms , uint32_t prescaler ) {
154
+ void system_on_idle_until_alarm (int64_t timediff_ms , uint32_t prescaler ) {
167
155
bool have_timeout = false;
168
156
uint64_t start_tick = 0 , end_tick = 0 ;
169
157
int64_t tickdiff ;
@@ -197,7 +185,6 @@ nrf_sleep_source_t system_on_idle_until_alarm(int64_t timediff_ms, uint32_t pres
197
185
#endif
198
186
199
187
int64_t remaining ;
200
- nrf_sleep_source_t wakeup_cause = NRF_SLEEP_WAKEUP_UNDEFINED ;
201
188
sleepmem_wakeup_event = SLEEPMEM_WAKEUP_BY_NONE ;
202
189
sleepmem_wakeup_pin = WAKEUP_PIN_UNDEF ;
203
190
@@ -210,30 +197,29 @@ nrf_sleep_source_t system_on_idle_until_alarm(int64_t timediff_ms, uint32_t pres
210
197
#endif
211
198
212
199
while (1 ) {
213
- if (mp_hal_is_interrupted ()) {
214
- WAKEUP_REASON ('I' );
215
- break ;
216
- }
200
+ if (mp_hal_is_interrupted ()) {
201
+ WAKEUP_REASON ('I' );
202
+ break ;
203
+ }
217
204
if (serial_connected () && serial_bytes_available ()) {
218
- WAKEUP_REASON ('S' );
219
- break ;
220
- }
205
+ WAKEUP_REASON ('S' );
206
+ break ;
207
+ }
221
208
RUN_BACKGROUND_TASKS ;
222
- wakeup_cause = alarm_woken_from_sleep_2 ();
223
- if (wakeup_cause != NRF_SLEEP_WAKEUP_UNDEFINED ) {
224
- WAKEUP_REASON ('0' + wakeup_cause );
225
- break ;
226
- }
227
- if (have_timeout ) {
228
- remaining = end_tick - port_get_raw_ticks (NULL );
229
- // We break a bit early so we don't risk setting the alarm before the time when we call
230
- // sleep.
231
- if (remaining < 1 ) {
232
- WAKEUP_REASON ('t' );
209
+ if (common_hal_alarm_woken_from_sleep ()) {
210
+ WAKEUP_REASON ('W' );
233
211
break ;
234
212
}
235
- port_interrupt_after_ticks (remaining );
236
- }
213
+ if (have_timeout ) {
214
+ remaining = end_tick - port_get_raw_ticks (NULL );
215
+ // We break a bit early so we don't risk setting the alarm before the time when we call
216
+ // sleep.
217
+ if (remaining < 1 ) {
218
+ WAKEUP_REASON ('t' );
219
+ break ;
220
+ }
221
+ port_interrupt_after_ticks (remaining );
222
+ }
237
223
// Idle until an interrupt happens.
238
224
port_idle_until_interrupt ();
239
225
#ifdef NRF_DEBUG_PRINT
@@ -242,16 +228,15 @@ nrf_sleep_source_t system_on_idle_until_alarm(int64_t timediff_ms, uint32_t pres
242
228
-- ct ;
243
229
}
244
230
#endif
245
- if (have_timeout ) {
246
- remaining = end_tick - port_get_raw_ticks (NULL );
247
- if (remaining <= 0 ) {
248
- wakeup_cause = NRF_SLEEP_WAKEUP_TIMER ;
249
- sleepmem_wakeup_event = SLEEPMEM_WAKEUP_BY_TIMER ;
250
- WAKEUP_REASON ( 'T' ) ;
251
- break ;
231
+ if (have_timeout ) {
232
+ remaining = end_tick - port_get_raw_ticks (NULL );
233
+ if (remaining <= 0 ) {
234
+ sleepmem_wakeup_event = SLEEPMEM_WAKEUP_BY_TIMER ;
235
+ WAKEUP_REASON ( 'T' ) ;
236
+ break ;
237
+ }
252
238
}
253
239
}
254
- }
255
240
#ifdef NRF_DEBUG_PRINT
256
241
dbg_printf ("%c\r\n" , reason );
257
242
#endif
@@ -271,12 +256,10 @@ nrf_sleep_source_t system_on_idle_until_alarm(int64_t timediff_ms, uint32_t pres
271
256
}
272
257
dbg_printf ("lapse %6.1f sec\r\n" , sec );
273
258
#endif
274
-
275
- return wakeup_cause ;
276
259
}
277
260
278
261
mp_obj_t common_hal_alarm_light_sleep_until_alarms (size_t n_alarms , const mp_obj_t * alarms ) {
279
- mp_obj_t r_obj = mp_const_none ;
262
+ mp_obj_t wake_alarm ;
280
263
alarm_time_timealarm_clear_wakeup_time ();
281
264
_setup_sleep_alarms (false, n_alarms , alarms );
282
265
@@ -285,25 +268,16 @@ mp_obj_t common_hal_alarm_light_sleep_until_alarms(size_t n_alarms, const mp_obj
285
268
#endif
286
269
287
270
int64_t timediff_ms = alarm_time_timealarm_get_wakeup_timediff_ms ();
288
- nrf_sleep_source_t cause = system_on_idle_until_alarm (timediff_ms , 0 );
289
- (void )cause ;
290
-
291
- #ifdef NRF_DEBUG_PRINT
292
- //dbg_printf("wakeup! ");
293
- print_wakeup_cause (cause );
294
- #endif
271
+ system_on_idle_until_alarm (timediff_ms , 0 );
295
272
296
273
if (mp_hal_is_interrupted ()) {
297
- #ifdef NRF_DEBUG_PRINT
298
- dbg_printf ("mp_hal_is_interrupted\r\n" );
299
- #endif
300
- r_obj = mp_const_none ;
274
+ wake_alarm = mp_const_none ;
301
275
}
302
276
else {
303
- r_obj = _get_wake_alarm (n_alarms , alarms );
277
+ wake_alarm = _get_wake_alarm (n_alarms , alarms );
304
278
}
305
279
alarm_reset ();
306
- return r_obj ;
280
+ return wake_alarm ;
307
281
}
308
282
309
283
void common_hal_alarm_set_deep_sleep_alarms (size_t n_alarms , const mp_obj_t * alarms ) {
@@ -325,14 +299,9 @@ void NORETURN common_hal_alarm_enter_deep_sleep(void) {
325
299
#ifdef NRF_DEBUG_PRINT
326
300
dbg_check_RTCprescaler (); //XXX
327
301
#endif
328
- nrf_sleep_source_t cause ;
329
- cause = system_on_idle_until_alarm (timediff_ms ,
330
- PRESCALER_VALUE_IN_DEEP_SLEEP );
331
- (void )cause ;
302
+ system_on_idle_until_alarm (timediff_ms , PRESCALER_VALUE_IN_DEEP_SLEEP );
332
303
333
304
#ifdef NRF_DEBUG_PRINT
334
- dbg_printf ("wakeup! " );
335
- print_wakeup_cause (cause );
336
305
dbg_printf ("RESET...\r\n\r\n" );
337
306
#endif
338
307
@@ -370,25 +339,9 @@ void common_hal_alarm_pretending_deep_sleep(void) {
370
339
#endif
371
340
372
341
int64_t timediff_ms = alarm_time_timealarm_get_wakeup_timediff_ms ();
373
- nrf_sleep_source_t cause = system_on_idle_until_alarm (timediff_ms , 0 );
374
- (void )cause ;
342
+ system_on_idle_until_alarm (timediff_ms , 0 );
375
343
376
- #ifdef NRF_DEBUG_PRINT
377
- dbg_printf ("wakeup! " );
378
- print_wakeup_cause (cause );
379
- #endif
380
-
381
- // alarm_reset();
382
-
383
- #if 0
384
- // if one of Alarm event occurred, reset myself
385
- if (cause == NRF_SLEEP_WAKEUP_GPIO ||
386
- cause == NRF_SLEEP_WAKEUP_TIMER ||
387
- cause == NRF_SLEEP_WAKEUP_TOUCHPAD ) {
388
- reset_cpu ();
389
- }
390
- // else, just return and go into REPL
391
- #endif
344
+ alarm_reset ();
392
345
}
393
346
394
347
void common_hal_alarm_gc_collect (void ) {
0 commit comments