summaryrefslogtreecommitdiffstatshomepage
path: root/stmhal/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'stmhal/main.c')
-rw-r--r--stmhal/main.c49
1 files changed, 40 insertions, 9 deletions
diff --git a/stmhal/main.c b/stmhal/main.c
index 882de6e1a4..02dcc76174 100644
--- a/stmhal/main.c
+++ b/stmhal/main.c
@@ -38,6 +38,7 @@
#include "lib/utils/pyexec.h"
#include "lib/fatfs/ff.h"
+#include "extmod/fsusermount.h"
#include "systick.h"
#include "pendsv.h"
@@ -64,10 +65,7 @@
void SystemClock_Config(void);
-static FATFS fatfs0;
-#if MICROPY_HW_HAS_SDCARD
-static FATFS fatfs1;
-#endif
+fs_user_mount_t fs_user_mount_flash;
void flash_error(int n) {
for (int i = 0; i < n; i++) {
@@ -169,8 +167,18 @@ static const char fresh_readme_txt[] =
// we don't make this function static because it needs a lot of stack and we
// want it to be executed without using stack within main() function
void init_flash_fs(uint reset_mode) {
+ // init the vfs object
+ fs_user_mount_t *vfs = &fs_user_mount_flash;
+ vfs->str = "/flash";
+ vfs->len = 6;
+ vfs->flags = 0;
+ pyb_flash_init_vfs(vfs);
+
+ // put the flash device in slot 0 (it will be unused at this point)
+ MP_STATE_PORT(fs_user_mount)[0] = vfs;
+
// try to mount the flash
- FRESULT res = f_mount(&fatfs0, "/flash", 1);
+ FRESULT res = f_mount(&vfs->fatfs, vfs->str, 1);
if (reset_mode == 3 || res == FR_NO_FILESYSTEM) {
// no filesystem, or asked to reset it, so create a fresh one
@@ -183,7 +191,9 @@ void init_flash_fs(uint reset_mode) {
if (res == FR_OK) {
// success creating fresh LFS
} else {
- __fatal_error("could not create LFS");
+ printf("PYB: can't create flash filesystem\n");
+ MP_STATE_PORT(fs_user_mount)[0] = NULL;
+ return;
}
// set label
@@ -213,7 +223,9 @@ void init_flash_fs(uint reset_mode) {
} else if (res == FR_OK) {
// mount sucessful
} else {
- __fatal_error("could not access LFS");
+ printf("PYB: can't mount flash\n");
+ MP_STATE_PORT(fs_user_mount)[0] = NULL;
+ return;
}
// The current directory is used as the boot up directory.
@@ -448,6 +460,9 @@ soft_reset:
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);
+ // zero out the pointers to the mounted devices
+ memset(MP_STATE_PORT(fs_user_mount), 0, sizeof(MP_STATE_PORT(fs_user_mount)));
+
// Initialise low-level sub-systems. Here we need to very basic things like
// zeroing out memory and resetting any of the sub-systems. Following this
// we can run Python scripts (eg boot.py), but anything that is configurable
@@ -493,9 +508,24 @@ soft_reset:
#if MICROPY_HW_HAS_SDCARD
// if an SD card is present then mount it on /sd/
if (sdcard_is_present()) {
- FRESULT res = f_mount(&fatfs1, "/sd", 1);
+ // create vfs object
+ fs_user_mount_t *vfs = m_new_obj_maybe(fs_user_mount_t);
+ if (vfs == NULL) {
+ goto no_mem_for_sd;
+ }
+ vfs->str = "/sd";
+ vfs->len = 3;
+ vfs->flags = FSUSER_FREE_OBJ;
+ sdcard_init_vfs(vfs);
+
+ // put the sd device in slot 1 (it will be unused at this point)
+ MP_STATE_PORT(fs_user_mount)[1] = vfs;
+
+ FRESULT res = f_mount(&vfs->fatfs, vfs->str, 1);
if (res != FR_OK) {
- printf("[SD] could not mount SD card\n");
+ printf("PYB: can't mount SD card\n");
+ MP_STATE_PORT(fs_user_mount)[1] = NULL;
+ m_del_obj(fs_user_mount_t, vfs);
} else {
// 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));
@@ -517,6 +547,7 @@ soft_reset:
f_chdrive("/sd");
}
}
+ no_mem_for_sd:;
}
#endif