@@ -301,7 +301,8 @@ STATIC void cleanup_after_vm(supervisor_allocation *heap, mp_obj_t exception) {
301
301
302
302
STATIC void print_code_py_status_message (safe_mode_t safe_mode ) {
303
303
if (autoreload_is_enabled ()) {
304
- serial_write_compressed (translate ("Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.\n" ));
304
+ serial_write_compressed (
305
+ translate ("Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.\n" ));
305
306
} else {
306
307
serial_write_compressed (translate ("Auto-reload is off.\n" ));
307
308
}
@@ -401,7 +402,8 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
401
402
// the options because it can be treated like any other reason-for-stickiness bit. The
402
403
// source is different though: it comes from the options that will apply to the next run,
403
404
// while the rest of next_code_options is what applied to this run.
404
- if (next_code_allocation != NULL && (((next_code_info_t * )next_code_allocation -> ptr )-> options & SUPERVISOR_NEXT_CODE_OPT_NEWLY_SET )) {
405
+ if (next_code_allocation != NULL &&
406
+ (((next_code_info_t * )next_code_allocation -> ptr )-> options & SUPERVISOR_NEXT_CODE_OPT_NEWLY_SET )) {
405
407
next_code_options |= SUPERVISOR_NEXT_CODE_OPT_NEWLY_SET ;
406
408
}
407
409
@@ -527,28 +529,38 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
527
529
// Sleep until our next interrupt.
528
530
#if CIRCUITPY_ALARM
529
531
if (result .return_code & PYEXEC_DEEP_SLEEP ) {
530
- // Make sure we have been awake long enough for USB to connect (enumeration delay).
531
- int64_t connecting_delay_ticks = CIRCUITPY_USB_CONNECTED_SLEEP_DELAY * 1024 - port_get_raw_ticks ( NULL ) ;
532
- // Until it's safe to decide whether we're real/fake sleeping
532
+ const bool awoke_from_true_deep_sleep =
533
+ common_hal_mcu_processor_get_reset_reason () == RESET_REASON_DEEP_SLEEP_ALARM ;
534
+
533
535
if (fake_sleeping ) {
534
536
// This waits until a pretend deep sleep alarm occurs. They are set
535
537
// during common_hal_alarm_set_deep_sleep_alarms. On some platforms
536
538
// it may also return due to another interrupt, that's why we check
537
539
// for deep sleep alarms above. If it wasn't a deep sleep alarm,
538
540
// then we'll idle here again.
539
541
common_hal_alarm_pretending_deep_sleep ();
540
- } else if (connecting_delay_ticks < 0 ) {
541
- // Entering deep sleep (may be fake or real.)
542
+ }
543
+ // The first time we go into a deep sleep, make sure we have been awake long enough
544
+ // for USB to connect (enumeration delay), or for the BLE workflow to start.
545
+ // We wait CIRCUITPY_WORKFLOW_CONNECTION_SLEEP_DELAY seconds after a restart.
546
+ // But if we woke up from a real deep sleep, don't wait for connection. The user will need to
547
+ // do a hard reset to get out of the real deep sleep.
548
+ else if (awoke_from_true_deep_sleep ||
549
+ port_get_raw_ticks (NULL ) > CIRCUITPY_WORKFLOW_CONNECTION_SLEEP_DELAY * 1024 ) {
550
+ // OK to start sleeping, real or fake.
542
551
status_led_deinit ();
543
552
deinit_rxtx_leds ();
544
553
board_deinit ();
545
- if (!supervisor_workflow_active ()) {
554
+
555
+ // Continue with true deep sleep even if workflow is available.
556
+ if (awoke_from_true_deep_sleep || !supervisor_workflow_active ()) {
546
557
// Enter true deep sleep. When we wake up we'll be back at the
547
558
// top of main(), not in this loop.
548
559
common_hal_alarm_enter_deep_sleep ();
549
560
// Does not return.
550
561
} else {
551
- serial_write_compressed (translate ("Pretending to deep sleep until alarm, CTRL-C or file write.\n" ));
562
+ serial_write_compressed (
563
+ translate ("Pretending to deep sleep until alarm, CTRL-C or file write.\n" ));
552
564
fake_sleeping = true;
553
565
}
554
566
} else {
0 commit comments