Commit 5b85a86c authored by Paul Sokolovsky's avatar Paul Sokolovsky
Browse files

extmod/fsusermount: Introduce separate mkfs() function.

Per the previously discussed plan. mount() still stays backward-compatible,
and new mkfs() is rought and takes more args than needed. But is a step
in a forward direction.
parent a2e5e4c3
...@@ -32,7 +32,7 @@ ...@@ -32,7 +32,7 @@
#include "lib/fatfs/ff.h" #include "lib/fatfs/ff.h"
#include "fsusermount.h" #include "fsusermount.h"
STATIC mp_obj_t pyb_mount(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { STATIC mp_obj_t fatfs_mount_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args, bool mkfs) {
static const mp_arg_t allowed_args[] = { static const mp_arg_t allowed_args[] = {
{ MP_QSTR_readonly, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} }, { MP_QSTR_readonly, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
{ MP_QSTR_mkfs, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} }, { MP_QSTR_mkfs, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
...@@ -85,15 +85,28 @@ STATIC mp_obj_t pyb_mount(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t * ...@@ -85,15 +85,28 @@ STATIC mp_obj_t pyb_mount(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *
vfs->writeblocks[0] = MP_OBJ_NULL; vfs->writeblocks[0] = MP_OBJ_NULL;
} }
// mount the block device // mount the block device (if mkfs, only pre-mount)
FRESULT res = f_mount(&vfs->fatfs, vfs->str, 1); FRESULT res = f_mount(&vfs->fatfs, vfs->str, !mkfs);
// check the result // check the result
if (res == FR_OK) { if (res == FR_OK) {
if (mkfs) {
goto mkfs;
}
} else if (res == FR_NO_FILESYSTEM && args[1].u_bool) { } else if (res == FR_NO_FILESYSTEM && args[1].u_bool) {
mkfs:
res = f_mkfs(vfs->str, 1, 0); res = f_mkfs(vfs->str, 1, 0);
if (res != FR_OK) { if (res != FR_OK) {
mkfs_error:
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't mkfs")); nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't mkfs"));
} }
if (mkfs) {
// If requested to only mkfs, unmount pre-mounted device
res = f_mount(NULL, vfs->str, 0);
if (res != FR_OK) {
goto mkfs_error;
}
MP_STATE_PORT(fs_user_mount) = NULL;
}
} else { } else {
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't mount")); nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't mount"));
} }
...@@ -112,6 +125,15 @@ STATIC mp_obj_t pyb_mount(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t * ...@@ -112,6 +125,15 @@ STATIC mp_obj_t pyb_mount(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *
} }
return mp_const_none; return mp_const_none;
} }
MP_DEFINE_CONST_FUN_OBJ_KW(pyb_mount_obj, 2, pyb_mount);
STATIC mp_obj_t fatfs_mount(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
return fatfs_mount_mkfs(n_args, pos_args, kw_args, false);
}
MP_DEFINE_CONST_FUN_OBJ_KW(fsuser_mount_obj, 2, fatfs_mount);
STATIC mp_obj_t fatfs_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
return fatfs_mount_mkfs(n_args, pos_args, kw_args, true);
}
MP_DEFINE_CONST_FUN_OBJ_KW(fsuser_mkfs_obj, 2, fatfs_mkfs);
#endif // MICROPY_FSUSERMOUNT #endif // MICROPY_FSUSERMOUNT
...@@ -34,4 +34,5 @@ typedef struct _fs_user_mount_t { ...@@ -34,4 +34,5 @@ typedef struct _fs_user_mount_t {
FATFS fatfs; FATFS fatfs;
} fs_user_mount_t; } fs_user_mount_t;
MP_DECLARE_CONST_FUN_OBJ(pyb_mount_obj); MP_DECLARE_CONST_FUN_OBJ(fsuser_mount_obj);
MP_DECLARE_CONST_FUN_OBJ(fsuser_mkfs_obj);
...@@ -61,6 +61,7 @@ ...@@ -61,6 +61,7 @@
#include "usb.h" #include "usb.h"
#include "portmodules.h" #include "portmodules.h"
#include "modmachine.h" #include "modmachine.h"
#include "extmod/fsusermount.h"
/// \function millis() /// \function millis()
/// Returns the number of milliseconds since the board was last reset. /// Returns the number of milliseconds since the board was last reset.
...@@ -164,7 +165,7 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = { ...@@ -164,7 +165,7 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_delay), (mp_obj_t)&time_sleep_ms_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_delay), (mp_obj_t)&time_sleep_ms_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_udelay), (mp_obj_t)&time_sleep_us_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_udelay), (mp_obj_t)&time_sleep_us_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_sync), (mp_obj_t)&mod_os_sync_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_sync), (mp_obj_t)&mod_os_sync_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_mount), (mp_obj_t)&pyb_mount_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_mount), (mp_obj_t)&fsuser_mount_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_Timer), (mp_obj_t)&pyb_timer_type }, { MP_OBJ_NEW_QSTR(MP_QSTR_Timer), (mp_obj_t)&pyb_timer_type },
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment