3737#include "py/mphal.h"
3838
3939#include "lib/utils/pyexec.h"
40- #include "lib/fatfs/ff.h"
40+ #include "lib/oofatfs/ff.h"
41+ #include "extmod/vfs.h"
4142#include "extmod/fsusermount.h"
4243
4344#include "systick.h"
6768void SystemClock_Config (void );
6869
6970fs_user_mount_t fs_user_mount_flash ;
71+ mp_vfs_mount_t mp_vfs_mount_flash ;
7072
7173void flash_error (int n ) {
7274 for (int i = 0 ; i < n ; i ++ ) {
@@ -168,17 +170,14 @@ static const char fresh_readme_txt[] =
168170// avoid inlining to avoid stack usage within main()
169171MP_NOINLINE STATIC void init_flash_fs (uint reset_mode ) {
170172 // init the vfs object
171- fs_user_mount_t * vfs = & fs_user_mount_flash ;
172- vfs -> str = "/flash" ;
173- vfs -> len = 6 ;
174- vfs -> flags = 0 ;
175- pyb_flash_init_vfs (vfs );
176-
177- // put the flash device in slot 0 (it will be unused at this point)
178- MP_STATE_PORT (fs_user_mount )[0 ] = vfs ;
173+ fs_user_mount_t * vfs_fat = & fs_user_mount_flash ;
174+ vfs_fat -> str = NULL ;
175+ vfs_fat -> len = 0 ;
176+ vfs_fat -> flags = 0 ;
177+ pyb_flash_init_vfs (vfs_fat );
179178
180179 // try to mount the flash
181- FRESULT res = f_mount (& vfs -> fatfs , vfs -> str , 1 );
180+ FRESULT res = f_mount (& vfs_fat -> fatfs );
182181
183182 if (reset_mode == 3 || res == FR_NO_FILESYSTEM ) {
184183 // no filesystem, or asked to reset it, so create a fresh one
@@ -187,33 +186,33 @@ MP_NOINLINE STATIC void init_flash_fs(uint reset_mode) {
187186 led_state (PYB_LED_R2 , 1 );
188187 uint32_t start_tick = HAL_GetTick ();
189188
190- res = f_mkfs ("/flash" , 0 , 0 );
189+ uint8_t working_buf [_MAX_SS ];
190+ res = f_mkfs (& vfs_fat -> fatfs , FM_FAT , 0 , working_buf , sizeof (working_buf ));
191191 if (res == FR_OK ) {
192192 // success creating fresh LFS
193193 } else {
194194 printf ("PYB: can't create flash filesystem\n" );
195- MP_STATE_PORT (fs_user_mount )[0 ] = NULL ;
196195 return ;
197196 }
198197
199198 // set label
200- f_setlabel ("/flash/ pybflash" );
199+ f_setlabel (& vfs_fat -> fatfs , " pybflash" );
201200
202201 // create empty main.py
203202 FIL fp ;
204- f_open (& fp , "/flash /main.py" , FA_WRITE | FA_CREATE_ALWAYS );
203+ f_open (& vfs_fat -> fatfs , & fp , "/main.py" , FA_WRITE | FA_CREATE_ALWAYS );
205204 UINT n ;
206205 f_write (& fp , fresh_main_py , sizeof (fresh_main_py ) - 1 /* don't count null terminator */ , & n );
207206 // TODO check we could write n bytes
208207 f_close (& fp );
209208
210209 // create .inf driver file
211- f_open (& fp , "/flash /pybcdc.inf" , FA_WRITE | FA_CREATE_ALWAYS );
210+ f_open (& vfs_fat -> fatfs , & fp , "/pybcdc.inf" , FA_WRITE | FA_CREATE_ALWAYS );
212211 f_write (& fp , fresh_pybcdc_inf , sizeof (fresh_pybcdc_inf ) - 1 /* don't count null terminator */ , & n );
213212 f_close (& fp );
214213
215214 // create readme file
216- f_open (& fp , "/flash /README.txt" , FA_WRITE | FA_CREATE_ALWAYS );
215+ f_open (& vfs_fat -> fatfs , & fp , "/README.txt" , FA_WRITE | FA_CREATE_ALWAYS );
217216 f_write (& fp , fresh_readme_txt , sizeof (fresh_readme_txt ) - 1 /* don't count null terminator */ , & n );
218217 f_close (& fp );
219218
@@ -224,38 +223,33 @@ MP_NOINLINE STATIC void init_flash_fs(uint reset_mode) {
224223 // mount sucessful
225224 } else {
226225 printf ("PYB: can't mount flash\n" );
227- MP_STATE_PORT (fs_user_mount )[0 ] = NULL ;
228226 return ;
229227 }
230228
229+ // mount the flash device (there should be no other devices mounted at this point)
230+ mp_vfs_mount_t * vfs = & mp_vfs_mount_flash ;
231+ vfs -> str = "/flash" ;
232+ vfs -> len = 6 ;
233+ vfs -> obj = MP_OBJ_FROM_PTR (vfs_fat );
234+ vfs -> next = NULL ;
235+ MP_STATE_VM (vfs_mount_table ) = vfs ;
236+
231237 // The current directory is used as the boot up directory.
232238 // It is set to the internal flash filesystem by default.
233- f_chdrive ( "/flash" ) ;
239+ MP_STATE_PORT ( vfs_cur ) = vfs ;
234240
235241 // Make sure we have a /flash/boot.py. Create it if needed.
236242 FILINFO fno ;
237- #if _USE_LFN
238- fno .lfname = NULL ;
239- fno .lfsize = 0 ;
240- #endif
241- res = f_stat ("/flash/boot.py" , & fno );
242- if (res == FR_OK ) {
243- if (fno .fattrib & AM_DIR ) {
244- // exists as a directory
245- // TODO handle this case
246- // see http://elm-chan.org/fsw/ff/img/app2.c for a "rm -rf" implementation
247- } else {
248- // exists as a file, good!
249- }
250- } else {
243+ res = f_stat (& vfs_fat -> fatfs , "/boot.py" , & fno );
244+ if (res != FR_OK ) {
251245 // doesn't exist, create fresh file
252246
253247 // LED on to indicate creation of boot.py
254248 led_state (PYB_LED_R2 , 1 );
255249 uint32_t start_tick = HAL_GetTick ();
256250
257251 FIL fp ;
258- f_open (& fp , "/flash/boot.py" , FA_WRITE | FA_CREATE_ALWAYS );
252+ f_open (& vfs_fat -> fatfs , & fp , "/flash/boot.py" , FA_WRITE | FA_CREATE_ALWAYS );
259253 UINT n ;
260254 f_write (& fp , fresh_boot_py , sizeof (fresh_boot_py ) - 1 /* don't count null terminator */ , & n );
261255 // TODO check we could write n bytes
@@ -485,24 +479,29 @@ int main(void) {
485479 // if an SD card is present then mount it on /sd/
486480 if (sdcard_is_present ()) {
487481 // create vfs object
488- fs_user_mount_t * vfs = m_new_obj_maybe (fs_user_mount_t );
489- if (vfs == NULL ) {
482+ fs_user_mount_t * vfs_fat = m_new_obj_maybe (fs_user_mount_t );
483+ mp_vfs_mount_t * vfs = m_new_obj_maybe (mp_vfs_mount_t );
484+ if (vfs == NULL || vfs_fat == NULL ) {
490485 goto no_mem_for_sd ;
491486 }
492- vfs -> str = "/sd" ;
493- vfs -> len = 3 ;
494- vfs -> flags = FSUSER_FREE_OBJ ;
495- sdcard_init_vfs (vfs );
496-
497- // put the sd device in slot 1 (it will be unused at this point)
498- MP_STATE_PORT (fs_user_mount )[1 ] = vfs ;
487+ vfs_fat -> str = NULL ;
488+ vfs_fat -> len = 0 ;
489+ vfs_fat -> flags = FSUSER_FREE_OBJ ;
490+ sdcard_init_vfs (vfs_fat );
499491
500- FRESULT res = f_mount (& vfs -> fatfs , vfs -> str , 1 );
492+ FRESULT res = f_mount (& vfs_fat -> fatfs );
501493 if (res != FR_OK ) {
502494 printf ("PYB: can't mount SD card\n" );
503- MP_STATE_PORT ( fs_user_mount )[ 1 ] = NULL ;
504- m_del_obj (fs_user_mount_t , vfs );
495+ m_del_obj ( fs_user_mount_t , vfs_fat ) ;
496+ m_del_obj (mp_vfs_mount_t , vfs );
505497 } else {
498+ // mount the sd device after the internal flash
499+ vfs -> str = "/sd" ;
500+ vfs -> len = 3 ;
501+ vfs -> obj = MP_OBJ_FROM_PTR (vfs_fat );
502+ vfs -> next = NULL ;
503+ MP_STATE_VM (vfs_mount_table )-> next = vfs ;
504+
506505 // TODO these should go before the /flash entries in the path
507506 mp_obj_list_append (mp_sys_path , MP_OBJ_NEW_QSTR (MP_QSTR__slash_sd ));
508507 mp_obj_list_append (mp_sys_path , MP_OBJ_NEW_QSTR (MP_QSTR__slash_sd_slash_lib ));
@@ -520,7 +519,7 @@ int main(void) {
520519 #endif
521520 {
522521 // use SD card as current directory
523- f_chdrive ( "/sd" ) ;
522+ MP_STATE_PORT ( vfs_cur ) = vfs ;
524523 }
525524 }
526525 no_mem_for_sd :;
@@ -534,8 +533,8 @@ int main(void) {
534533 // TODO perhaps have pyb.reboot([bootpy]) function to soft-reboot and execute custom boot.py
535534 if (reset_mode == 1 || reset_mode == 3 ) {
536535 const char * boot_py = "boot.py" ;
537- FRESULT res = f_stat (boot_py , NULL );
538- if (res == FR_OK ) {
536+ mp_import_stat_t stat = mp_import_stat (boot_py );
537+ if (stat == MP_IMPORT_STAT_FILE ) {
539538 int ret = pyexec_file (boot_py );
540539 if (ret & PYEXEC_FORCED_EXIT ) {
541540 goto soft_reset_exit ;
@@ -597,8 +596,8 @@ int main(void) {
597596 } else {
598597 main_py = mp_obj_str_get_str (MP_STATE_PORT (pyb_config_main ));
599598 }
600- FRESULT res = f_stat (main_py , NULL );
601- if (res == FR_OK ) {
599+ mp_import_stat_t stat = mp_import_stat (main_py );
600+ if (stat == MP_IMPORT_STAT_FILE ) {
602601 int ret = pyexec_file (main_py );
603602 if (ret & PYEXEC_FORCED_EXIT ) {
604603 goto soft_reset_exit ;
0 commit comments