@@ -86,11 +86,6 @@ void init_flash_fs(void) {
8686 if (res == FR_NO_FILESYSTEM ) {
8787 // no filesystem so create a fresh one
8888
89- // We are before USB initializes so temporarily undo the USB_WRITEABLE
90- // requirement.
91- bool usb_writeable = (vfs -> flags & FSUSER_USB_WRITEABLE ) > 0 ;
92- vfs -> flags &= ~FSUSER_USB_WRITEABLE ;
93-
9489 res = f_mkfs ("/flash" , 0 , 0 );
9590 // Flush the new file system to make sure its repaired immediately.
9691 flash_flush ();
@@ -101,10 +96,6 @@ void init_flash_fs(void) {
10196
10297 // set label
10398 f_setlabel ("CIRCUITPY" );
104-
105- if (usb_writeable ) {
106- vfs -> flags |= FSUSER_USB_WRITEABLE ;
107- }
10899 } else if (res != FR_OK ) {
109100 MP_STATE_PORT (fs_user_mount )[0 ] = NULL ;
110101 return ;
@@ -251,21 +242,13 @@ bool start_mp(void) {
251242 mp_hal_stdout_tx_str ("Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.\r\n" );
252243 }
253244 #endif
254-
255- new_status_color (BOOT_RUNNING );
256- pyexec_result_t result ;
257- bool found_boot = maybe_run ("settings.txt" , & result ) ||
258- maybe_run ("settings.py" , & result ) ||
259- maybe_run ("boot.py" , & result ) ||
260- maybe_run ("boot.txt" , & result );
261245 bool found_main = false;
262- if (!found_boot || !(result .return_code & PYEXEC_FORCED_EXIT )) {
263- new_status_color (MAIN_RUNNING );
264- found_main = maybe_run ("code.txt" , & result ) ||
265- maybe_run ("code.py" , & result ) ||
266- maybe_run ("main.py" , & result ) ||
267- maybe_run ("main.txt" , & result );
268- }
246+ pyexec_result_t result ;
247+ new_status_color (MAIN_RUNNING );
248+ found_main = maybe_run ("code.txt" , & result ) ||
249+ maybe_run ("code.py" , & result ) ||
250+ maybe_run ("main.py" , & result ) ||
251+ maybe_run ("main.txt" , & result );
269252 reset_status_led ();
270253
271254 if (result .return_code & PYEXEC_FORCED_EXIT ) {
@@ -520,37 +503,73 @@ int main(void) {
520503 // as current dir.
521504 init_flash_fs ();
522505
506+ // Reset everything and prep MicroPython to run boot.py.
507+ reset_samd21 ();
508+ reset_mp ();
509+
510+ // Run boot before initing USB and capture output in a file.
511+ new_status_color (BOOT_RUNNING );
512+ #ifdef CIRCUITPY_BOOT_OUTPUT_FILE
513+ FIL file_pointer ;
514+ boot_output_file = & file_pointer ;
515+ FRESULT result = f_open (boot_output_file , CIRCUITPY_BOOT_OUTPUT_FILE , FA_WRITE | FA_CREATE_ALWAYS );
516+ if (result != FR_OK ) {
517+ while (true) {}
518+ }
519+ #endif
520+
521+ // TODO(tannewt): Re-add support for flashing boot error output.
522+ bool found_boot = maybe_run ("settings.txt" , NULL ) ||
523+ maybe_run ("settings.py" , NULL ) ||
524+ maybe_run ("boot.py" , NULL ) ||
525+ maybe_run ("boot.txt" , NULL );
526+ (void ) found_boot ;
527+
528+ #ifdef CIRCUITPY_BOOT_OUTPUT_FILE
529+ f_close (boot_output_file );
530+ boot_output_file = NULL ;
531+ #endif
532+
533+ // Turn off local writing in favor of USB writing prior to initializing USB.
534+ flash_set_usb_writeable (true);
535+
523536 usb_hid_init ();
524537
525538 // Start USB after getting everything going.
526539 #ifdef USB_REPL
527540 udc_start ();
528541 #endif
529542
543+ // Reset to remove any state that boot.py setup. It should only be used to
544+ // change internal state thats not in the heap.
545+ reset_samd21 ();
546+ reset_mp ();
530547
531- // Main script is finished, so now go into REPL mode.
532- // The REPL mode can change, or it can request a reload.
548+ // Boot script is finished, so now go into REPL/main mode.
533549 int exit_code = PYEXEC_FORCED_EXIT ;
534550 bool skip_repl = true;
535551 bool first_run = true;
536552 for (;;) {
537553 if (!skip_repl ) {
554+ // The REPL mode can change, or it can request a reload.
538555 autoreload_disable ();
539556 new_status_color (REPL_RUNNING );
540557 if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL ) {
541558 exit_code = pyexec_raw_repl ();
542559 } else {
543560 exit_code = pyexec_friendly_repl ();
544561 }
562+ reset_samd21 ();
563+ reset_mp ();
545564 }
546565 if (exit_code == PYEXEC_FORCED_EXIT ) {
547566 if (!first_run ) {
548567 mp_hal_stdout_tx_str ("soft reboot\r\n" );
549568 }
550- reset_samd21 ();
551- reset_mp ();
552569 first_run = false;
553570 skip_repl = start_mp ();
571+ reset_samd21 ();
572+ reset_mp ();
554573 } else if (exit_code != 0 ) {
555574 break ;
556575 }
0 commit comments