@@ -124,7 +124,6 @@ static void reset_devices(void) {
124
124
}
125
125
126
126
STATIC void start_mp (supervisor_allocation * heap , bool first_run ) {
127
- autoreload_reset ();
128
127
supervisor_workflow_reset ();
129
128
130
129
// Stack limit should be less than real stack size, so we have a chance
@@ -336,7 +335,13 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
336
335
// Collects stickiness bits that apply in the current situation.
337
336
uint8_t next_code_stickiness_situation = SUPERVISOR_NEXT_CODE_OPT_NEWLY_SET ;
338
337
338
+ // Do the filesystem flush check before reload in case another write comes
339
+ // in while we're doing the flush.
339
340
if (safe_mode == NO_SAFE_MODE ) {
341
+ stack_resize ();
342
+ filesystem_flush ();
343
+ }
344
+ if (safe_mode == NO_SAFE_MODE && !autoreload_pending ()) {
340
345
static const char * const supported_filenames [] = STRING_LIST (
341
346
"code.txt" , "code.py" , "main.py" , "main.txt" );
342
347
#if CIRCUITPY_FULL_BUILD
@@ -345,8 +350,6 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
345
350
"main.txt.py" , "main.py.txt" , "main.txt.txt" ,"main.py.py" );
346
351
#endif
347
352
348
- stack_resize ();
349
- filesystem_flush ();
350
353
supervisor_allocation * heap = allocate_remaining_memory ();
351
354
352
355
// Prepare the VM state. Includes an alarm check/reset for sleep.
@@ -390,22 +393,7 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
390
393
// Print done before resetting everything so that we get the message over
391
394
// BLE before it is reset and we have a delay before reconnect.
392
395
if ((result .return_code & PYEXEC_RELOAD ) && supervisor_get_run_reason () == RUN_REASON_AUTO_RELOAD ) {
393
- serial_write_compressed (translate ("\nCode stopped by auto-reload.\n" ));
394
-
395
- // Wait for autoreload interval before reloading
396
- uint64_t start_ticks = 0 ;
397
- do {
398
- // Start waiting, or restart interval if another reload request was initiated
399
- // while we were waiting.
400
- if (reload_requested ) {
401
- reload_requested = false;
402
- start_ticks = supervisor_ticks_ms64 ();
403
- }
404
- RUN_BACKGROUND_TASKS ;
405
- } while (supervisor_ticks_ms64 () - start_ticks < CIRCUITPY_AUTORELOAD_DELAY_MS );
406
-
407
- // Restore request for use below.
408
- reload_requested = true;
396
+ serial_write_compressed (translate ("\nCode stopped by auto-reload. Reloading soon.\n" ));
409
397
} else {
410
398
serial_write_compressed (translate ("\nCode done running.\n" ));
411
399
}
@@ -425,8 +413,6 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
425
413
426
414
if (result .return_code & PYEXEC_RELOAD ) {
427
415
next_code_stickiness_situation |= SUPERVISOR_NEXT_CODE_OPT_STICKY_ON_RELOAD ;
428
- skip_repl = true;
429
- skip_wait = true;
430
416
} else if (result .return_code == 0 ) {
431
417
next_code_stickiness_situation |= SUPERVISOR_NEXT_CODE_OPT_STICKY_ON_SUCCESS ;
432
418
if (next_code_options & SUPERVISOR_NEXT_CODE_OPT_RELOAD_ON_SUCCESS ) {
@@ -484,22 +470,27 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
484
470
size_t total_time = blink_time + LED_SLEEP_TIME_MS ;
485
471
#endif
486
472
473
+ // This loop is waits after code completes. It waits for fake sleeps to
474
+ // finish, user input or autoreloads.
487
475
#if CIRCUITPY_ALARM
488
476
bool fake_sleeping = false;
489
477
#endif
490
478
while (!skip_wait ) {
491
479
RUN_BACKGROUND_TASKS ;
492
480
493
481
// If a reload was requested by the supervisor or autoreload, return.
494
- if (reload_requested ) {
482
+ if (autoreload_ready () ) {
495
483
next_code_stickiness_situation |= SUPERVISOR_NEXT_CODE_OPT_STICKY_ON_RELOAD ;
496
484
// Should the STICKY_ON_SUCCESS and STICKY_ON_ERROR bits be cleared in
497
485
// next_code_stickiness_situation? I can see arguments either way, but I'm deciding
498
486
// "no" for now, mainly because it's a bit less code. At this point, we have both a
499
487
// success or error and a reload, so let's have both of the respective options take
500
488
// effect (in OR combination).
501
- reload_requested = false;
502
489
skip_repl = true;
490
+ // We're kicking off the autoreload process so reset now. If any
491
+ // other reloads trigger after this, then we'll want another wait
492
+ // period.
493
+ autoreload_reset ();
503
494
break ;
504
495
}
505
496
@@ -526,7 +517,7 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
526
517
#endif
527
518
528
519
// If messages haven't been printed yet, print them
529
- if (!printed_press_any_key && serial_connected ()) {
520
+ if (!printed_press_any_key && serial_connected () && ! autoreload_pending () ) {
530
521
if (!serial_connected_at_start ) {
531
522
print_code_py_status_message (safe_mode );
532
523
}
0 commit comments