nrf/main: Add auto mount and auto format hook for internal flash FS.

This commit is contained in:
Glenn Ruben Bakke 2020-07-23 21:21:58 +02:00 committed by Damien George
parent 6ff3a2afef
commit b0fd4372c4

View File

@ -76,6 +76,13 @@
#include "usb_cdc.h"
#endif
#if MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE
#include "extmod/vfs_fat.h"
#include "lib/oofatfs/ff.h"
#include "extmod/vfs.h"
#include "flashbdev.h"
#endif
void do_str(const char *src, mp_parse_input_kind_t input_kind) {
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
if (lex == NULL) {
@ -99,6 +106,28 @@ void do_str(const char *src, mp_parse_input_kind_t input_kind) {
extern uint32_t _heap_start;
extern uint32_t _heap_end;
#if MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE
STATIC int vfs_mount_and_chdir(mp_obj_t bdev, mp_obj_t mount_point) {
nlr_buf_t nlr;
mp_int_t ret = -MP_EIO;
if (nlr_push(&nlr) == 0) {
mp_obj_t args[] = { bdev, mount_point };
mp_vfs_mount(2, args, (mp_map_t *)&mp_const_empty_map);
mp_vfs_chdir(mount_point);
ret = 0; // success
nlr_pop();
} else {
mp_obj_base_t *exc = nlr.ret_val;
if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(exc->type), MP_OBJ_FROM_PTR(&mp_type_OSError))) {
mp_obj_t v = mp_obj_exception_get_value(MP_OBJ_FROM_PTR(exc));
mp_obj_get_int_maybe(v, &ret); // get errno value
ret = -ret;
}
}
return ret;
}
#endif
int main(int argc, char **argv) {
@ -169,6 +198,23 @@ soft_reset:
pin_init0();
#if MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE
flashbdev_init();
// Try to mount the flash on "/flash" and chdir to it for the boot-up directory.
mp_obj_t mount_point = MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash);
int ret = vfs_mount_and_chdir((mp_obj_t)&nrf_flash_obj, mount_point);
if ((ret == -MP_ENODEV) || (ret == -MP_EIO)) {
pyexec_frozen_module("_mkfs.py"); // Frozen script for formatting flash filesystem.
ret = vfs_mount_and_chdir((mp_obj_t)&nrf_flash_obj, mount_point);
}
if (ret != 0) {
printf("MPY: can't mount flash\n");
}
#endif
#if MICROPY_MBFS
microbit_filesystem_init();
#endif