Skip to content

Commit 3667ee1

Browse files
committed
stmhal: On boot, mount all available partitions of the SD card.
The first partition is mounted as "/sd" and subsequent partitions are mounted as "/sd<part_num>". This is backwards compatible with the previous behaviour, which just mounted the first partition on "/sd". At this point, only FatFs filesystems are mounted.
1 parent 326343f commit 3667ee1

3 files changed

Lines changed: 83 additions & 48 deletions

File tree

stmhal/main.c

Lines changed: 80 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
264343
STATIC 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

stmhal/sdcard.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,11 +443,11 @@ const mp_obj_type_t pyb_sdcard_type = {
443443
.locals_dict = (mp_obj_t)&pyb_sdcard_locals_dict,
444444
};
445445

446-
void sdcard_init_vfs(fs_user_mount_t *vfs) {
446+
void sdcard_init_vfs(fs_user_mount_t *vfs, int part) {
447447
vfs->base.type = &mp_fat_vfs_type;
448448
vfs->flags |= FSUSER_NATIVE | FSUSER_HAVE_IOCTL;
449449
vfs->fatfs.drv = vfs;
450-
vfs->fatfs.part = 0; // autodetect partition
450+
vfs->fatfs.part = part;
451451
vfs->readblocks[0] = (mp_obj_t)&pyb_sdcard_readblocks_obj;
452452
vfs->readblocks[1] = (mp_obj_t)&pyb_sdcard_obj;
453453
vfs->readblocks[2] = (mp_obj_t)sdcard_read_blocks; // native version

stmhal/sdcard.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ extern const struct _mp_obj_type_t pyb_sdcard_type;
4141
extern const struct _mp_obj_base_t pyb_sdcard_obj;
4242

4343
struct _fs_user_mount_t;
44-
void sdcard_init_vfs(struct _fs_user_mount_t *vfs);
44+
void sdcard_init_vfs(struct _fs_user_mount_t *vfs, int part);

0 commit comments

Comments
 (0)