|
30 | 30 | #include "py/runtime.h"
|
31 | 31 | #include "py/stackctrl.h"
|
32 | 32 | #include "py/gc.h"
|
| 33 | +#include "py/mperrno.h" |
33 | 34 | #include "py/mphal.h"
|
34 | 35 | #include "lib/mp-readline/readline.h"
|
35 | 36 | #include "lib/utils/pyexec.h"
|
|
81 | 82 | STATIC pyb_thread_t pyb_thread_main;
|
82 | 83 | #endif
|
83 | 84 |
|
84 |
| -#if MICROPY_HW_ENABLE_STORAGE |
85 |
| -STATIC fs_user_mount_t fs_user_mount_flash; |
86 |
| -#endif |
87 |
| - |
88 | 85 | #if defined(MICROPY_HW_UART_REPL)
|
89 | 86 | #ifndef MICROPY_HW_UART_REPL_RXBUF
|
90 | 87 | #define MICROPY_HW_UART_REPL_RXBUF (260)
|
@@ -159,65 +156,51 @@ STATIC mp_obj_t pyb_main(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_a
|
159 | 156 | MP_DEFINE_CONST_FUN_OBJ_KW(pyb_main_obj, 1, pyb_main);
|
160 | 157 |
|
161 | 158 | #if MICROPY_HW_ENABLE_STORAGE
|
| 159 | +STATIC int vfs_mount_and_chdir(mp_obj_t bdev, mp_obj_t mount_point) { |
| 160 | + nlr_buf_t nlr; |
| 161 | + mp_int_t ret = -MP_EIO; |
| 162 | + if (nlr_push(&nlr) == 0) { |
| 163 | + mp_obj_t args[] = { bdev, mount_point }; |
| 164 | + mp_vfs_mount(2, args, (mp_map_t*)&mp_const_empty_map); |
| 165 | + mp_vfs_chdir(mount_point); |
| 166 | + ret = 0; // success |
| 167 | + nlr_pop(); |
| 168 | + } else { |
| 169 | + mp_obj_base_t *exc = nlr.ret_val; |
| 170 | + if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(exc->type), MP_OBJ_FROM_PTR(&mp_type_OSError))) { |
| 171 | + mp_obj_t v = mp_obj_exception_get_value(MP_OBJ_FROM_PTR(exc)); |
| 172 | + mp_obj_get_int_maybe(v, &ret); // get errno value |
| 173 | + ret = -ret; |
| 174 | + } |
| 175 | + } |
| 176 | + return ret; |
| 177 | +} |
| 178 | + |
162 | 179 | // avoid inlining to avoid stack usage within main()
|
163 | 180 | MP_NOINLINE STATIC bool init_flash_fs(uint reset_mode) {
|
164 |
| - // init the vfs object |
165 |
| - fs_user_mount_t *vfs_fat = &fs_user_mount_flash; |
166 |
| - vfs_fat->blockdev.flags = 0; |
167 |
| - pyb_flash_init_vfs(vfs_fat); |
168 |
| - |
169 |
| - // try to mount the flash |
170 |
| - FRESULT res = f_mount(&vfs_fat->fatfs); |
| 181 | + if (reset_mode == 3) { |
| 182 | + // Asked by user to reset filesystem |
| 183 | + factory_reset_create_filesystem(); |
| 184 | + } |
171 | 185 |
|
172 |
| - if (reset_mode == 3 || res == FR_NO_FILESYSTEM) { |
173 |
| - // no filesystem, or asked to reset it, so create a fresh one |
| 186 | + // Try to mount the flash on "/flash" and chdir to it for the boot-up directory. |
| 187 | + mp_obj_t bdev = MP_OBJ_FROM_
E413
PTR(&pyb_flash_obj); |
| 188 | + mp_obj_t mount_point = MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash); |
| 189 | + int ret = vfs_mount_and_chdir(bdev, mount_point); |
174 | 190 |
|
175 |
| - // LED on to indicate creation of LFS |
176 |
| - led_state(PYB_LED_GREEN, 1); |
177 |
| - uint32_t start_tick = HAL_GetTick(); |
178 |
| - |
179 |
| - uint8_t working_buf[FF_MAX_SS]; |
180 |
| - res = f_mkfs(&vfs_fat->fatfs, FM_FAT, 0, working_buf, sizeof(working_buf)); |
181 |
| - if (res == FR_OK) { |
182 |
| - // success creating fresh LFS |
183 |
| - } else { |
184 |
| - printf("MPY: can't create flash filesystem\n"); |
185 |
| - return false; |
| 191 | + if (ret == -MP_ENODEV && reset_mode != 3) { |
| 192 | + // No filesystem (and didn't already create one), try to create a fresh one |
| 193 | + ret = factory_reset_create_filesystem(); |
| 194 | + if (ret == 0) { |
| 195 | + ret = vfs_mount_and_chdir(bdev, mount_point); |
186 | 196 | }
|
| 197 | + } |
187 | 198 |
|
188 |
| - // set label |
189 |
| - f_setlabel(&vfs_fat->fatfs, MICROPY_HW_FLASH_FS_LABEL); |
190 |
| - |
191 |
| - // populate the filesystem with factory files |
192 |
| - factory_reset_make_files(&vfs_fat->fatfs); |
193 |
| - |
194 |
| - // keep LED on for at least 200ms |
195 |
| - systick_wait_at_least(start_tick, 200); |
196 |
| - led_state(PYB_LED_GREEN, 0); |
197 |
| - } else if (res == FR_OK) { |
198 |
| - // mount sucessful |
199 |
| - } else { |
200 |
| - fail: |
| 199 | + if (ret != 0) { |
201 | 200 | printf("MPY: can't mount flash\n");
|
202 | 201 | return false;
|
203 | 202 | }
|
204 | 203 |
|
205 |
| - // mount the flash device (there should be no other devices mounted at this point) |
206 |
| - // we allocate this structure on the heap because vfs->next is a root pointer |
207 |
| - mp_vfs_mount_t *vfs = m_new_obj_maybe(mp_vfs_mount_t); |
208 |
| - if (vfs == NULL) { |
209 |
| - goto fail; |
210 |
| - } |
211 |
| - vfs->str = "/flash"; |
212 |
| - vfs->len = 6; |
213 |
| - vfs->obj = MP_OBJ_FROM_PTR(vfs_fat); |
214 |
| - vfs->next = NULL; |
215 |
| - MP_STATE_VM(vfs_mount_table) = vfs; |
216 |
| - |
217 |
| - // The current directory is used as the boot up directory. |
218 |
| - // It is set to the internal flash filesystem by default. |
219 |
| - MP_STATE_PORT(vfs_cur) = vfs; |
220 |
| - |
221 | 204 | return true;
|
222 | 205 | }
|
223 | 206 | #endif
|
@@ -616,7 +599,7 @@ void stm32_main(uint32_t reset_mode) {
|
616 | 599 | // if an SD card is present then mount it on /sd/
|
617 | 600 | if (sdcard_is_present()) {
|
618 | 601 | // if there is a file in the flash called "SKIPSD", then we don't mount the SD card
|
619 |
| - if (!mounted_flash || f_stat(&fs_user_mount_flash.fatfs, "/SKIPSD", NULL) != FR_OK) { |
| 602 | + if (!mounted_flash || mp_vfs_import_stat("SKIPSD") == MP_IMPORT_STAT_FILE) { |
620 | 603 | mounted_sdcard = init_sdcard_fs();
|
621 | 604 | }
|
622 | 605 | }
|
|
0 commit comments