Skip to content

Commit 80dfd65

Browse files
committed
stmhal/main: Put /sd directory before /flash in sys.path.
If the SD card is mounted then its libraries (ie those that are imported) should override any in /flash.
1 parent 3667ee1 commit 80dfd65

1 file changed

Lines changed: 22 additions & 14 deletions

File tree

stmhal/main.c

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ static const char fresh_readme_txt[] =
168168
;
169169

170170
// avoid inlining to avoid stack usage within main()
171-
MP_NOINLINE STATIC void init_flash_fs(uint reset_mode) {
171+
MP_NOINLINE STATIC bool init_flash_fs(uint reset_mode) {
172172
// init the vfs object
173173
fs_user_mount_t *vfs_fat = &fs_user_mount_flash;
174174
vfs_fat->str = NULL;
@@ -192,7 +192,7 @@ MP_NOINLINE STATIC void init_flash_fs(uint reset_mode) {
192192
// success creating fresh LFS
193193
} else {
194194
printf("PYB: can't create flash filesystem\n");
195-
return;
195+
return false;
196196
}
197197

198198
// set label
@@ -223,7 +223,7 @@ MP_NOINLINE STATIC void init_flash_fs(uint reset_mode) {
223223
// mount sucessful
224224
} else {
225225
printf("PYB: can't mount flash\n");
226-
return;
226+
return false;
227227
}
228228

229229
// mount the flash device (there should be no other devices mounted at this point)
@@ -259,9 +259,11 @@ MP_NOINLINE STATIC void init_flash_fs(uint reset_mode) {
259259
sys_tick_wait_at_least(start_tick, 200);
260260
led_state(PYB_LED_GREEN, 0);
261261
}
262+
263+
return true;
262264
}
263265

264-
STATIC void init_sdcard_fs(bool first_soft_reset) {
266+
STATIC bool init_sdcard_fs(bool first_soft_reset) {
265267
bool first_part = true;
266268
for (int part_num = 1; part_num <= 4; ++part_num) {
267269
// create vfs object
@@ -308,12 +310,6 @@ STATIC void init_sdcard_fs(bool first_soft_reset) {
308310
}
309311
}
310312

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-
317313
if (first_soft_reset) {
318314
// use SD card as medium for the USB MSD
319315
#if defined(USE_DEVICE_MODE)
@@ -337,6 +333,9 @@ STATIC void init_sdcard_fs(bool first_soft_reset) {
337333

338334
if (first_part) {
339335
printf("PYB: can't mount SD card\n");
336+
return false;
337+
} else {
338+
return true;
340339
}
341340
}
342341

@@ -508,8 +507,6 @@ int main(void) {
508507
mp_init();
509508
mp_obj_list_init(mp_sys_path, 0);
510509
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
511-
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash));
512-
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash_slash_lib));
513510
mp_obj_list_init(mp_sys_argv, 0);
514511

515512
// Initialise low-level sub-systems. Here we need to very basic things like
@@ -552,15 +549,26 @@ int main(void) {
552549

553550
// Initialise the local flash filesystem.
554551
// Create it if needed, mount in on /flash, and set it as current dir.
555-
init_flash_fs(reset_mode);
552+
bool mounted_flash = init_flash_fs(reset_mode);
556553

554+
bool mounted_sdcard = false;
557555
#if MICROPY_HW_HAS_SDCARD
558556
// if an SD card is present then mount it on /sd/
559557
if (sdcard_is_present()) {
560-
init_sdcard_fs(first_soft_reset);
558+
mounted_sdcard = init_sdcard_fs(first_soft_reset);
561559
}
562560
#endif
563561

562+
// set sys.path based on mounted filesystems (/sd is first so it can override /flash)
563+
if (mounted_sdcard) {
564+
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_sd));
565+
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_sd_slash_lib));
566+
}
567+
if (mounted_flash) {
568+
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash));
569+
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash_slash_lib));
570+
}
571+
564572
// reset config variables; they should be set by boot.py
565573
MP_STATE_PORT(pyb_config_main) = MP_OBJ_NULL;
566574

0 commit comments

Comments
 (0)