Skip to content

Commit 5b85a86

Browse files
committed
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.
1 parent a2e5e4c commit 5b85a86

3 files changed

Lines changed: 30 additions & 6 deletions

File tree

extmod/fsusermount.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include "lib/fatfs/ff.h"
3333
#include "fsusermount.h"
3434

35-
STATIC mp_obj_t pyb_mount(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
35+
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) {
3636
static const mp_arg_t allowed_args[] = {
3737
{ MP_QSTR_readonly, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
3838
{ 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 *
8585
vfs->writeblocks[0] = MP_OBJ_NULL;
8686
}
8787

88-
// mount the block device
89-
FRESULT res = f_mount(&vfs->fatfs, vfs->str, 1);
88+
// mount the block device (if mkfs, only pre-mount)
89+
FRESULT res = f_mount(&vfs->fatfs, vfs->str, !mkfs);
9090
// check the result
9191
if (res == FR_OK) {
92+
if (mkfs) {
93+
goto mkfs;
94+
}
9295
} else if (res == FR_NO_FILESYSTEM && args[1].u_bool) {
96+
mkfs:
9397
res = f_mkfs(vfs->str, 1, 0);
9498
if (res != FR_OK) {
99+
mkfs_error:
95100
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't mkfs"));
96101
}
102+
if (mkfs) {
103+
// If requested to only mkfs, unmount pre-mounted device
104+
res = f_mount(NULL, vfs->str, 0);
105+
if (res != FR_OK) {
106+
goto mkfs_error;
107+
}
108+
MP_STATE_PORT(fs_user_mount) = NULL;
109+
}
97110
} else {
98111
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't mount"));
99112
}
@@ -112,6 +125,15 @@ STATIC mp_obj_t pyb_mount(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *
112125
}
113126
return mp_const_none;
114127
}
115-
MP_DEFINE_CONST_FUN_OBJ_KW(pyb_mount_obj, 2, pyb_mount);
128+
129+
STATIC mp_obj_t fatfs_mount(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
130+
return fatfs_mount_mkfs(n_args, pos_args, kw_args, false);
131+
}
132+
MP_DEFINE_CONST_FUN_OBJ_KW(fsuser_mount_obj, 2, fatfs_mount);
133+
134+
STATIC mp_obj_t fatfs_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
135+
return fatfs_mount_mkfs(n_args, pos_args, kw_args, true);
136+
}
137+
MP_DEFINE_CONST_FUN_OBJ_KW(fsuser_mkfs_obj, 2, fatfs_mkfs);
116138

117139
#endif // MICROPY_FSUSERMOUNT

extmod/fsusermount.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ typedef struct _fs_user_mount_t {
3434
FATFS fatfs;
3535
} fs_user_mount_t;
3636

37-
MP_DECLARE_CONST_FUN_OBJ(pyb_mount_obj);
37+
MP_DECLARE_CONST_FUN_OBJ(fsuser_mount_obj);
38+
MP_DECLARE_CONST_FUN_OBJ(fsuser_mkfs_obj);

stmhal/modpyb.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
#include "usb.h"
6262
#include "portmodules.h"
6363
#include "modmachine.h"
64+
#include "extmod/fsusermount.h"
6465

6566
/// \function millis()
6667
/// 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[] = {
164165
{ MP_OBJ_NEW_QSTR(MP_QSTR_delay), (mp_obj_t)&time_sleep_ms_obj },
165166
{ MP_OBJ_NEW_QSTR(MP_QSTR_udelay), (mp_obj_t)&time_sleep_us_obj },
166167
{ MP_OBJ_NEW_QSTR(MP_QSTR_sync), (mp_obj_t)&mod_os_sync_obj },
167-
{ MP_OBJ_NEW_QSTR(MP_QSTR_mount), (mp_obj_t)&pyb_mount_obj },
168+
{ MP_OBJ_NEW_QSTR(MP_QSTR_mount), (mp_obj_t)&fsuser_mount_obj },
168169

169170
{ MP_OBJ_NEW_QSTR(MP_QSTR_Timer), (mp_obj_t)&pyb_timer_type },
170171

0 commit comments

Comments
 (0)