@@ -132,7 +132,7 @@ static void reset_devices(void) {
132132 #endif
133133}
134134
135- STATIC void start_mp (supervisor_allocation * heap , bool first_run ) {
135+ STATIC void start_mp (supervisor_allocation * heap ) {
136136 supervisor_workflow_reset ();
137137
138138 // Stack limit should be less than real stack size, so we have a chance
@@ -176,14 +176,6 @@ STATIC void start_mp(supervisor_allocation *heap, bool first_run) {
176176 mp_obj_list_append (mp_sys_path , MP_OBJ_NEW_QSTR (MP_QSTR__slash_lib ));
177177
178178 mp_obj_list_init ((mp_obj_list_t * )mp_sys_argv , 0 );
179-
180- #if CIRCUITPY_ALARM
181- // Record which alarm woke us up, if any. An object may be created so the heap must be functional.
182- // There is no alarm if this is not the first time code.py or the REPL has been run.
183- shared_alarm_save_wake_alarm (first_run ? common_hal_alarm_create_wake_alarm () : mp_const_none );
184- // Reset alarm module only after we retrieved the wakeup alarm.
185- alarm_reset ();
186- #endif
187179}
188180
189181STATIC void stop_mp (void ) {
@@ -373,7 +365,7 @@ STATIC void print_code_py_status_message(safe_mode_t safe_mode) {
373365 }
374366}
375367
376- STATIC bool run_code_py (safe_mode_t safe_mode , bool first_run , bool * simulate_reset ) {
368+ STATIC bool run_code_py (safe_mode_t safe_mode , bool * simulate_reset ) {
377369 bool serial_connected_at_start = serial_connected ();
378370 bool printed_safe_mode_message = false;
379371 #if CIRCUITPY_AUTORELOAD_DELAY_MS > 0
@@ -409,8 +401,8 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
409401
410402 supervisor_allocation * heap = allocate_remaining_memory ();
411403
412- // Prepare the VM state. Includes an alarm check/reset for sleep.
413- start_mp (heap , first_run );
404+ // Prepare the VM state.
405+ start_mp (heap );
414406
415407 #if CIRCUITPY_USB
416408 usb_setup_with_vm ();
@@ -755,8 +747,7 @@ STATIC void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
755747
756748 supervisor_allocation * heap = allocate_remaining_memory ();
757749
758- // true means this is the first set of VM's after a hard reset.
759- start_mp (heap , true);
750+ start_mp (heap );
760751
761752 #if CIRCUITPY_USB
762753 // Set up default USB values after boot.py VM starts but before running boot.py.
@@ -853,12 +844,12 @@ STATIC void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
853844 #endif
854845}
855846
856- STATIC int run_repl (bool first_run ) {
847+ STATIC int run_repl (void ) {
857848 int exit_code = PYEXEC_FORCED_EXIT ;
858849 stack_resize ();
859850 filesystem_flush ();
860851 supervisor_allocation * heap = allocate_remaining_memory ();
861- start_mp (heap , first_run );
852+ start_mp (heap );
862853
863854 #if CIRCUITPY_USB
864855 usb_setup_with_vm ();
@@ -968,6 +959,15 @@ int __attribute__((used)) main(void) {
968959 safe_mode = NO_CIRCUITPY ;
969960 }
970961
962+ #if CIRCUITPY_ALARM
963+ // Record which alarm woke us up, if any.
964+ // common_hal_alarm_record_wake_alarm() should return a static, non-heap object
965+ shared_alarm_save_wake_alarm (common_hal_alarm_record_wake_alarm ());
966+ // Then reset the alarm system. It's not reset in reset_port(), because that's also called
967+ // on VM teardown, which would clear any alarm setup.
968+ alarm_reset ();
969+ #endif
970+
971971 // Reset everything and prep MicroPython to run boot.py.
972972 reset_port ();
973973 // Port-independent devices, like CIRCUITPY_BLEIO_HCI.
@@ -1001,31 +1001,32 @@ int __attribute__((used)) main(void) {
10011001 // Boot script is finished, so now go into REPL or run code.py.
10021002 int exit_code = PYEXEC_FORCED_EXIT ;
10031003 bool skip_repl = true;
1004- bool first_run = true;
1005- bool simulate_reset ;
1004+ bool simulate_reset = true;
10061005 for (;;) {
1007- simulate_reset = false;
10081006 if (!skip_repl ) {
1009- exit_code = run_repl (first_run );
1007+ exit_code = run_repl ();
10101008 supervisor_set_run_reason (RUN_REASON_REPL_RELOAD );
10111009 }
10121010 if (exit_code == PYEXEC_FORCED_EXIT ) {
1013- if (!first_run ) {
1011+ if (!simulate_reset ) {
10141012 serial_write_compressed (translate ("soft reboot\n" ));
10151013 }
10161014 if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL ) {
1017- skip_repl = run_code_py (safe_mode , first_run , & simulate_reset );
1015+ // If code.py did a fake deep sleep, pretend that we
1016+ // are running code.py for the first time after a hard
1017+ // reset. This will preserve any alarm information.
1018+ skip_repl = run_code_py (safe_mode , & simulate_reset );
10181019 } else {
10191020 skip_repl = false;
10201021 }
10211022 } else if (exit_code != 0 ) {
10221023 break ;
10231024 }
10241025
1025- // Either the REPL or code.py has run and finished.
1026- // If code.py did a fake deep sleep, pretend that we are running code.py for
1027- // the first time after a hard reset. This will preserve any alarm information.
1028- first_run = simulate_reset ;
1026+ #if CIRCUITPY_ALARM
1027+ shared_alarm_save_wake_alarm ( simulate_reset ? common_hal_alarm_record_wake_alarm () : mp_const_none );
1028+ alarm_reset ();
1029+ #endif
10291030 }
10301031 mp_deinit ();
10311032 return 0 ;
0 commit comments