@@ -261,6 +261,85 @@ MP_NOINLINE STATIC void init_flash_fs(uint reset_mode) {
261261 }
262262}
263263
264+ STATIC void init_sdcard_fs (bool first_soft_reset ) {
265+ bool first_part = true;
266+ for (int part_num = 1 ; part_num <= 4 ; ++ part_num ) {
267+ // create vfs object
268+ fs_user_mount_t * vfs_fat = m_new_obj_maybe (fs_user_mount_t );
269+ mp_vfs_mount_t * vfs = m_new_obj_maybe (mp_vfs_mount_t );
270+ if (vfs == NULL || vfs_fat == NULL ) {
271+ break ;
272+ }
273+ vfs_fat -> str = NULL ;
274+ vfs_fat -> len = 0 ;
275+ vfs_fat -> flags = FSUSER_FREE_OBJ ;
276+ sdcard_init_vfs (vfs_fat , part_num );
277+
278+ // try to mount the partition
279+ FRESULT res = f_mount (& vfs_fat -> fatfs );
280+
281+ if (res != FR_OK ) {
282+ // couldn't mount
283+ m_del_obj (fs_user_mount_t , vfs_fat );
284+ m_del_obj (mp_vfs_mount_t , vfs );
285+ } else {
286+ // mounted via FatFs, now mount the SD partition in the VFS
287+ if (first_part ) {
288+ // the first available partition is traditionally called "sd" for simplicity
289+ vfs -> str = "/sd" ;
290+ vfs -> len = 3 ;
291+ } else {
292+ // subsequent partitions are numbered by their index in the partition table
293+ if (part_num == 2 ) {
294+ vfs -> str = "/sd2" ;
295+ } else if (part_num == 2 ) {
296+ vfs -> str = "/sd3" ;
297+ } else {
298+ vfs -> str = "/sd4" ;
299+ }
300+ vfs -> len = 4 ;
301+ }
302+ vfs -> obj = MP_OBJ_FROM_PTR (vfs_fat );
303+ vfs -> next = NULL ;
304+ for (mp_vfs_mount_t * * m = & MP_STATE_VM (vfs_mount_table );; m = & (* m )-> next ) {
305+ if (* m == NULL ) {
306+ * m = vfs ;
307+ break ;
308+ }
309+ }
310+
311+ if (first_part ) {
312+ // TODO these should go before the /flash entries in the path
313+ mp_obj_list_append (mp_sys_path , MP_OBJ_NEW_QSTR (MP_QSTR__slash_sd ));
314+ mp_obj_list_append (mp_sys_path , MP_OBJ_NEW_QSTR (MP_QSTR__slash_sd_slash_lib ));
315+ }
316+
317+ if (first_soft_reset ) {
318+ // use SD card as medium for the USB MSD
319+ #if defined(USE_DEVICE_MODE )
320+ pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_SDCARD ;
321+ #endif
322+ }
323+
324+ #if defined(USE_DEVICE_MODE )
325+ // only use SD card as current directory if that's what the USB medium is
326+ if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_SDCARD )
327+ #endif
328+ {
329+ if (first_part ) {
330+ // use SD card as current directory
331+ MP_STATE_PORT (vfs_cur ) = vfs ;
332+ }
333+ }
334+ first_part = false;
335+ }
336+ }
337+
338+ if (first_part ) {
339+ printf ("PYB: can't mount SD card\n" );
340+ }
341+ }
342+
264343STATIC uint update_reset_mode (uint reset_mode ) {
265344#if MICROPY_HW_HAS_SWITCH
266345 if (switch_get ()) {
@@ -478,51 +557,7 @@ int main(void) {
478557#if MICROPY_HW_HAS_SDCARD
479558 // if an SD card is present then mount it on /sd/
480559 if (sdcard_is_present ()) {
481- // create vfs object
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 ) {
485- goto no_mem_for_sd ;
486- }
487- vfs_fat -> str = NULL ;
488- vfs_fat -> len = 0 ;
489- vfs_fat -> flags = FSUSER_FREE_OBJ ;
490- sdcard_init_vfs (vfs_fat );
491-
492- FRESULT res = f_mount (& vfs_fat -> fatfs );
493- if (res != FR_OK ) {
494- printf ("PYB: can't mount SD card\n" );
495- m_del_obj (fs_user_mount_t , vfs_fat );
496- m_del_obj (mp_vfs_mount_t , vfs );
497- } 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-
505- // TODO these should go before the /flash entries in the path
506- mp_obj_list_append (mp_sys_path , MP_OBJ_NEW_QSTR (MP_QSTR__slash_sd ));
507- mp_obj_list_append (mp_sys_path , MP_OBJ_NEW_QSTR (MP_QSTR__slash_sd_slash_lib ));
508-
509- if (first_soft_reset ) {
510- // use SD card as medium for the USB MSD
511- #if defined(USE_DEVICE_MODE )
512- pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_SDCARD ;
513- #endif
514- }
515-
516- #if defined(USE_DEVICE_MODE )
517- // only use SD card as current directory if that's what the USB medium is
518- if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_SDCARD )
519- #endif
520- {
521- // use SD card as current directory
522- MP_STATE_PORT (vfs_cur ) = vfs ;
523- }
524- }
525- no_mem_for_sd :;
560+ init_sdcard_fs (first_soft_reset );
526561 }
527562#endif
528563
0 commit comments