diff options
| author | Damien George <damien.p.george@gmail.com> | 2014-07-31 23:44:04 +0100 |
|---|---|---|
| committer | Damien George <damien.p.george@gmail.com> | 2014-07-31 23:44:04 +0100 |
| commit | 65dd7bc13d2083a7db5a974bf6480b1652a35c82 (patch) | |
| tree | af18c5f8ad6c33a33d7a57e8d495b1890655f15b /stmhal/main.c | |
| parent | 5aac6aa445d343074b64bb7fa3ddc9184b0c65be (diff) | |
stmhal: Change 0:/ and 1:/ to /flash and /sd; add CWD support.
Some important changes to the way the file system is structured on the
pyboard:
1. 0: and 1: drive names are now replaced with POSIX inspired
directories, namely /flash and /sd.
2. Filesystem now supports the notion of a current working directory.
Supports the standard Python way of manipulating it: os.chdir and
os.getcwd.
3. On boot up, current directory is /flash if no SD inserted, else /sd
if SD inserted. Then runs boot.py and main.py from the current dir.
This is the same as the old behaviour, but is much more consistent and
flexible (eg you can os.chdir in boot.py to change where main.py is run
from).
4. sys.path (for import) is now set to '' (current dir), plus /flash
and /flash/lib, and then /sd and /sd/lib if SD inserted. This, along
with CWD, means that import now works properly. You can import a file
from the current directory.
5. os.listdir is fixed to return just the basename, not the full path.
See issue #537 for background and discussion.
Diffstat (limited to 'stmhal/main.c')
| -rw-r--r-- | stmhal/main.c | 58 |
1 files changed, 28 insertions, 30 deletions
diff --git a/stmhal/main.c b/stmhal/main.c index 2cc463462..183f11b7a 100644 --- a/stmhal/main.c +++ b/stmhal/main.c @@ -332,8 +332,9 @@ soft_reset: qstr_init(); mp_init(); mp_obj_list_init(mp_sys_path, 0); - mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_0_colon__slash_)); - mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_0_colon__slash_lib)); + mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script) + mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash)); + mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash_slash_lib)); mp_obj_list_init(mp_sys_argv, 0); readline_init(); @@ -344,7 +345,7 @@ soft_reset: // local filesystem init { // try to mount the flash - FRESULT res = f_mount(&fatfs0, "0:", 1); + FRESULT res = f_mount(&fatfs0, "/flash", 1); if (reset_mode == 3 || res == FR_NO_FILESYSTEM) { // no filesystem, or asked to reset it, so create a fresh one @@ -352,7 +353,7 @@ soft_reset: led_state(PYB_LED_R2, 1); uint32_t start_tick = HAL_GetTick(); - res = f_mkfs("0:", 0, 0); + res = f_mkfs("/flash", 0, 0); if (res == FR_OK) { // success creating fresh LFS } else { @@ -361,19 +362,19 @@ soft_reset: // create empty main.py FIL fp; - f_open(&fp, "0:/main.py", FA_WRITE | FA_CREATE_ALWAYS); + f_open(&fp, "/flash/main.py", FA_WRITE | FA_CREATE_ALWAYS); UINT n; f_write(&fp, fresh_main_py, sizeof(fresh_main_py) - 1 /* don't count null terminator */, &n); // TODO check we could write n bytes f_close(&fp); // create .inf driver file - f_open(&fp, "0:/pybcdc.inf", FA_WRITE | FA_CREATE_ALWAYS); + f_open(&fp, "/flash/pybcdc.inf", FA_WRITE | FA_CREATE_ALWAYS); f_write(&fp, fresh_pybcdc_inf, sizeof(fresh_pybcdc_inf) - 1 /* don't count null terminator */, &n); f_close(&fp); // create readme file - f_open(&fp, "0:/README.txt", FA_WRITE | FA_CREATE_ALWAYS); + f_open(&fp, "/flash/README.txt", FA_WRITE | FA_CREATE_ALWAYS); f_write(&fp, fresh_readme_txt, sizeof(fresh_readme_txt) - 1 /* don't count null terminator */, &n); f_close(&fp); @@ -387,14 +388,14 @@ soft_reset: } } - // make sure we have a 0:/boot.py + // make sure we have a /flash/boot.py { FILINFO fno; #if _USE_LFN fno.lfname = NULL; fno.lfsize = 0; #endif - FRESULT res = f_stat("0:/boot.py", &fno); + FRESULT res = f_stat("/flash/boot.py", &fno); if (res == FR_OK) { if (fno.fattrib & AM_DIR) { // exists as a directory @@ -411,7 +412,7 @@ soft_reset: uint32_t start_tick = HAL_GetTick(); FIL fp; - f_open(&fp, "0:/boot.py", FA_WRITE | FA_CREATE_ALWAYS); + f_open(&fp, "/flash/boot.py", FA_WRITE | FA_CREATE_ALWAYS); UINT n; f_write(&fp, fresh_boot_py, sizeof(fresh_boot_py) - 1 /* don't count null terminator */, &n); // TODO check we could write n bytes @@ -424,21 +425,25 @@ soft_reset: } // root device defaults to internal flash filesystem - uint root_device = 0; + f_chdrive("/flash"); #if defined(USE_DEVICE_MODE) usb_storage_medium_t usb_medium = USB_STORAGE_MEDIUM_FLASH; #endif #if MICROPY_HW_HAS_SDCARD - // if an SD card is present then mount it on 1:/ + // if an SD card is present then mount it on /sd/ if (reset_mode == 1 && sdcard_is_present()) { - FRESULT res = f_mount(&fatfs1, "1:", 1); + FRESULT res = f_mount(&fatfs1, "/sd", 1); if (res != FR_OK) { printf("[SD] could not mount SD card\n"); } else { // use SD card as root device - root_device = 1; + f_chdrive("/sd"); + + // TODO these should go before the /flash entries in the path + mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_sd)); + mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_sd_slash_lib)); if (first_soft_reset) { // use SD card as medium for the USB MSD @@ -453,17 +458,12 @@ soft_reset: (void)first_soft_reset; #endif - // run <root>:/boot.py, if it exists + // run boot.py, if it exists if (reset_mode == 1) { - const char *boot_file; - if (root_device == 0) { - boot_file = "0:/boot.py"; - } else { - boot_file = "1:/boot.py"; - } - FRESULT res = f_stat(boot_file, NULL); + const char *boot_py = "boot.py"; + FRESULT res = f_stat(boot_py, NULL); if (res == FR_OK) { - if (!pyexec_file(boot_file)) { + if (!pyexec_file(boot_py)) { flash_error(4); } } @@ -518,20 +518,18 @@ soft_reset: // now that everything is initialised, run main script if (reset_mode == 1 && pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) { - vstr_t *vstr = vstr_new(); - vstr_printf(vstr, "%d:/", root_device); + const char *main_py; if (pyb_config_main == MP_OBJ_NULL) { - vstr_add_str(vstr, "main.py"); + main_py = "main.py"; } else { - vstr_add_str(vstr, mp_obj_str_get_str(pyb_config_main)); + main_py = mp_obj_str_get_str(pyb_config_main); } - FRESULT res = f_stat(vstr_str(vstr), NULL); + FRESULT res = f_stat(main_py, NULL); if (res == FR_OK) { - if (!pyexec_file(vstr_str(vstr))) { + if (!pyexec_file(main_py)) { flash_error(3); } } - vstr_free(vstr); } #if MICROPY_HW_ENABLE_CC3K |
