@@ -132,7 +132,7 @@ static void reset_devices(void) {
132
132
#endif
133
133
}
134
134
135
- STATIC void start_mp (supervisor_allocation * heap , bool first_run ) {
135
+ STATIC void start_mp (supervisor_allocation * heap ) {
136
136
supervisor_workflow_reset ();
137
137
138
138
// 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) {
176
176
mp_obj_list_append (mp_sys_path , MP_OBJ_NEW_QSTR (MP_QSTR__slash_lib ));
177
177
178
178
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
187
179
}
188
180
189
181
STATIC void stop_mp (void ) {
@@ -373,7 +365,7 @@ STATIC void print_code_py_status_message(safe_mode_t safe_mode) {
373
365
}
374
366
}
375
367
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 ) {
377
369
bool serial_connected_at_start = serial_connected ();
378
370
bool printed_safe_mode_message = false;
379
371
#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
409
401
410
402
supervisor_allocation * heap = allocate_remaining_memory ();
411
403
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 );
414
406
415
407
#if CIRCUITPY_USB
416
408
usb_setup_with_vm ();
@@ -755,8 +747,7 @@ STATIC void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
755
747
756
748
supervisor_allocation * heap = allocate_remaining_memory ();
757
749
758
- // true means this is the first set of VM's after a hard reset.
759
- start_mp (heap , true);
750
+ start_mp (heap );
760
751
761
752
#if CIRCUITPY_USB
762
753
// 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) {
853
844
#endif
854
845
}
855
846
856
- STATIC int run_repl (bool first_run ) {
847
+ STATIC int run_repl (void ) {
857
848
int exit_code = PYEXEC_FORCED_EXIT ;
858
849
stack_resize ();
859
850
filesystem_flush ();
860
851
supervisor_allocation * heap = allocate_remaining_memory ();
861
- start_mp (heap , first_run );
852
+ start_mp (heap );
862
853
863
854
#if CIRCUITPY_USB
864
855
usb_setup_with_vm ();
@@ -968,6 +959,15 @@ int __attribute__((used)) main(void) {
968
959
safe_mode = NO_CIRCUITPY ;
969
960
}
970
961
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
+
971
971
// Reset everything and prep MicroPython to run boot.py.
972
972
reset_port ();
973
973
// Port-independent devices, like CIRCUITPY_BLEIO_HCI.
@@ -1001,31 +1001,32 @@ int __attribute__((used)) main(void) {
1001
1001
// Boot script is finished, so now go into REPL or run code.py.
1002
1002
int exit_code = PYEXEC_FORCED_EXIT ;
1003
1003
bool skip_repl = true;
1004
- bool first_run = true;
1005
- bool simulate_reset ;
1004
+ bool simulate_reset = true;
1006
1005
for (;;) {
1007
- simulate_reset = false;
1008
1006
if (!skip_repl ) {
1009
- exit_code = run_repl (first_run );
1007
+ exit_code = run_repl ();
1010
1008
supervisor_set_run_reason (RUN_REASON_REPL_RELOAD );
1011
1009
}
1012
1010
if (exit_code == PYEXEC_FORCED_EXIT ) {
1013
- if (!first_run ) {
1011
+ if (!simulate_reset ) {
1014
1012
serial_write_compressed (translate ("soft reboot\n" ));
1015
1013
}
1016
1014
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 );
1018
1019
} else {
1019
1020
skip_repl = false;
1020
1021
}
1021
1022
} else if (exit_code != 0 ) {
1022
1023
break ;
1023
1024
}
1024
1025
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
1029
1030
}
1030
1031
mp_deinit ();
1031
1032
return 0 ;
0 commit comments