@@ -333,11 +333,26 @@ int main(void) {
333333 pyb_stdio_uart = NULL ;
334334#endif
335335
336- readline_init ();
337- pin_init ();
338- extint_init ();
336+ // Initialise low-level sub-systems. Here we need to very basic things like
337+ // zeroing out memory and resetting any of the sub-systems. Following this
338+ // we can run Python scripts (eg boot.py), but anything that is configurable
339+ // by boot.py must be set after boot.py is run.
340+
341+ readline_init0 ();
342+ pin_init0 ();
343+ extint_init0 ();
344+ timer_init0 ();
345+
346+ #if MICROPY_HW_ENABLE_RNG
347+ rng_init0 ();
348+ #endif
339349
340- // local filesystem init
350+ i2c_init0 ();
351+ spi_init0 ();
352+ pyb_usb_init0 ();
353+
354+ // Initialise the local flash filesystem.
355+ // Create it if needed, and mount in on /flash.
341356 {
342357 // try to mount the flash
343358 FRESULT res = f_mount (& fatfs0 , "/flash" , 1 );
@@ -383,7 +398,11 @@ int main(void) {
383398 }
384399 }
385400
386- // make sure we have a /flash/boot.py
401+ // The current directory is used as the boot up directory.
402+ // It is set to the internal flash filesystem by default.
403+ f_chdrive ("/flash" );
404+
405+ // Make sure we have a /flash/boot.py. Create it if needed.
387406 {
388407 FILINFO fno ;
389408#if _USE_LFN
@@ -419,9 +438,6 @@ int main(void) {
419438 }
420439 }
421440
422- // root device defaults to internal flash filesystem
423- f_chdrive ("/flash" );
424-
425441#if defined(USE_DEVICE_MODE )
426442 usb_storage_medium_t usb_medium = USB_STORAGE_MEDIUM_FLASH ;
427443#endif
@@ -433,7 +449,7 @@ int main(void) {
433449 if (res != FR_OK ) {
434450 printf ("[SD] could not mount SD card\n" );
435451 } else {
436- // use SD card as root device
452+ // use SD card as current directory
437453 f_chdrive ("/sd" );
438454
439455 // TODO these should go before the /flash entries in the path
@@ -448,9 +464,6 @@ int main(void) {
448464 }
449465 }
450466 }
451- #else
452- // Get rid of compiler warning if no SDCARD is configured.
453- (void )first_soft_reset ;
454467#endif
455468
456469 // run boot.py, if it exists
@@ -469,6 +482,10 @@ int main(void) {
469482 led_state (3 , 0 );
470483 led_state (4 , 0 );
471484
485+ // Now we initialise sub-systems that need configuration from boot.py,
486+ // or whose initialisation can be safely deferred until after running
487+ // boot.py.
488+
472489#if defined(USE_HOST_MODE )
473490 // USB host
474491 pyb_usb_host_init ();
@@ -487,15 +504,6 @@ int main(void) {
487504 }
488505#endif
489506
490- timer_init0 ();
491-
492- #if MICROPY_HW_ENABLE_RNG
493- rng_init0 ();
494- #endif
495-
496- i2c_init0 ();
497- spi_init0 ();
498-
499507#if MICROPY_HW_HAS_MMA7660
500508 // MMA accel: init and reset
501509 accel_init ();
@@ -511,7 +519,15 @@ int main(void) {
511519 dac_init ();
512520#endif
513521
514- // now that everything is initialised, run main script
522+ #if MICROPY_HW_ENABLE_CC3K
523+ // wifi using the CC3000 driver
524+ pyb_wlan_init ();
525+ pyb_wlan_start ();
526+ #endif
527+
528+ // At this point everything is fully configured and initialised.
529+
530+ // Run the main script from the current directory.
515531 if (reset_mode == 1 && pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL ) {
516532 const char * main_py ;
517533 if (pyb_config_main == MP_OBJ_NULL ) {
@@ -527,14 +543,8 @@ int main(void) {
527543 }
528544 }
529545
530- #if MICROPY_HW_ENABLE_CC3K
531- // wifi using the CC3000 driver
532- pyb_wlan_init ();
533- pyb_wlan_start ();
534- #endif
535-
536- // enter REPL
537- // REPL mode can change, or it can request a soft reset
546+ // Main script is finished, so now go into REPL mode.
547+ // The REPL mode can change, or it can request a soft reset.
538548 for (;;) {
539549 if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL ) {
540550 if (pyexec_raw_repl () != 0 ) {
@@ -547,6 +557,8 @@ int main(void) {
547557 }
548558 }
549559
560+ // soft reset
561+
550562 printf ("PYB: sync filesystems\n" );
551563 storage_flush ();
552564
0 commit comments