Skip to content

Commit 84c614e

Browse files
committed
stmhal: Convert to use VFS sub-system and new ooFatFs component.
This patch makes the following configuration changes: - MICROPY_FSUSERMOUNT is disabled, removing old mounting infrastructure - MICROPY_VFS is enabled, giving new VFS sub-system - MICROPY_VFS_FAT is enabled, giving uos.VfsFat type - MICROPY_FATFS_OO is enabled, to use new ooFatFs lib, R0.12b User facing API should be almost unchanged. Most notable changes are removal of os.mkfs (use os.VfsFat.mkfs instead) and pyb.mount doesn't allow unmounting by passing None as the device.
1 parent 3242cf2 commit 84c614e

11 files changed

Lines changed: 114 additions & 421 deletions

File tree

stmhal/Makefile

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ CMSIS_DIR=cmsis
2626
HAL_DIR=hal/$(MCU_SERIES)
2727
USBDEV_DIR=usbdev
2828
#USBHOST_DIR=usbhost
29-
FATFS_DIR=lib/fatfs
29+
FATFS_DIR=lib/oofatfs
3030
DFU=../tools/dfu.py
3131
# may need to prefix dfu-util with sudo
3232
USE_PYDFU ?= 1
@@ -109,8 +109,8 @@ SRC_LIB = $(addprefix lib/,\
109109
libm/sf_erf.c \
110110
libm/wf_lgamma.c \
111111
libm/wf_tgamma.c \
112-
fatfs/ff.c \
113-
fatfs/option/ccsbcs.c \
112+
oofatfs/ff.c \
113+
oofatfs/option/unicode.c \
114114
mp-readline/readline.c \
115115
netutils/netutils.c \
116116
timeutils/timeutils.c \
@@ -159,14 +159,12 @@ SRC_C = \
159159
modutime.c \
160160
modusocket.c \
161161
modnetwork.c \
162-
import.c \
163162
extint.c \
164163
usrsw.c \
165164
rng.c \
166165
rtc.c \
167166
flash.c \
168167
storage.c \
169-
builtin_open.c \
170168
sdcard.c \
171169
fatfs_port.c \
172170
lcd.c \

stmhal/builtin_open.c

Lines changed: 0 additions & 30 deletions
This file was deleted.

stmhal/fatfs_port.c

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,10 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include "py/mphal.h"
2827
#include "py/runtime.h"
29-
#include "lib/fatfs/ff.h" /* FatFs lower layer API */
30-
#include "lib/fatfs/diskio.h" /* FatFs lower layer API */
28+
#include "lib/oofatfs/ff.h"
3129
#include "rtc.h"
3230

33-
const PARTITION VolToPart[MICROPY_FATFS_VOLUMES] = {
34-
{0, 1}, // Logical drive 0 ==> Physical drive 0, 1st partition
35-
{1, 0}, // Logical drive 1 ==> Physical drive 1 (auto detection)
36-
{2, 0}, // Logical drive 2 ==> Physical drive 2 (auto detection)
37-
{3, 0}, // Logical drive 3 ==> Physical drive 3 (auto detection)
38-
/*
39-
{0, 2}, // Logical drive 2 ==> Physical drive 0, 2nd partition
40-
{0, 3}, // Logical drive 3 ==> Physical drive 0, 3rd partition
41-
*/
42-
};
43-
4431
DWORD get_fattime(void) {
4532
rtc_init_finalise();
4633
RTC_TimeTypeDef time;

stmhal/import.c

Lines changed: 0 additions & 35 deletions
This file was deleted.

stmhal/main.c

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
#include "py/mphal.h"
3838

3939
#include "lib/utils/pyexec.h"
40-
#include "lib/fatfs/ff.h"
40+
#include "lib/oofatfs/ff.h"
41+
#include "extmod/vfs.h"
4142
#include "extmod/fsusermount.h"
4243

4344
#include "systick.h"
@@ -67,6 +68,7 @@
6768
void SystemClock_Config(void);
6869

6970
fs_user_mount_t fs_user_mount_flash;
71+
mp_vfs_mount_t mp_vfs_mount_flash;
7072

7173
void flash_error(int n) {
7274
for (int i = 0; i < n; i++) {
@@ -168,17 +170,14 @@ static const char fresh_readme_txt[] =
168170
// avoid inlining to avoid stack usage within main()
169171
MP_NOINLINE STATIC void init_flash_fs(uint reset_mode) {
170172
// init the vfs object
171-
fs_user_mount_t *vfs = &fs_user_mount_flash;
172-
vfs->str = "/flash";
173-
vfs->len = 6;
174-
vfs->flags = 0;
175-
pyb_flash_init_vfs(vfs);
176-
177-
// put the flash device in slot 0 (it will be unused at this point)
178-
MP_STATE_PORT(fs_user_mount)[0] = vfs;
173+
fs_user_mount_t *vfs_fat = &fs_user_mount_flash;
174+
vfs_fat->str = NULL;
175+
vfs_fat->len = 0;
176+
vfs_fat->flags = 0;
177+
pyb_flash_init_vfs(vfs_fat);
179178

180179
// try to mount the flash
181-
FRESULT res = f_mount(&vfs->fatfs, vfs->str, 1);
180+
FRESULT res = f_mount(&vfs_fat->fatfs);
182181

183182
if (reset_mode == 3 || res == FR_NO_FILESYSTEM) {
184183
// no filesystem, or asked to reset it, so create a fresh one
@@ -187,33 +186,33 @@ MP_NOINLINE STATIC void init_flash_fs(uint reset_mode) {
187186
led_state(PYB_LED_R2, 1);
188187
uint32_t start_tick = HAL_GetTick();
189188

190-
res = f_mkfs("/flash", 0, 0);
189+
uint8_t working_buf[_MAX_SS];
190+
res = f_mkfs(&vfs_fat->fatfs, FM_FAT, 0, working_buf, sizeof(working_buf));
191191
if (res == FR_OK) {
192192
// success creating fresh LFS
193193
} else {
194194
printf("PYB: can't create flash filesystem\n");
195-
MP_STATE_PORT(fs_user_mount)[0] = NULL;
196195
return;
197196
}
198197

199198
// set label
200-
f_setlabel("/flash/pybflash");
199+
f_setlabel(&vfs_fat->fatfs, "pybflash");
201200

202201
// create empty main.py
203202
FIL fp;
204-
f_open(&fp, "/flash/main.py", FA_WRITE | FA_CREATE_ALWAYS);
203+
f_open(&vfs_fat->fatfs, &fp, "/main.py", FA_WRITE | FA_CREATE_ALWAYS);
205204
UINT n;
206205
f_write(&fp, fresh_main_py, sizeof(fresh_main_py) - 1 /* don't count null terminator */, &n);
207206
// TODO check we could write n bytes
208207
f_close(&fp);
209208

210209
// create .inf driver file
211-
f_open(&fp, "/flash/pybcdc.inf", FA_WRITE | FA_CREATE_ALWAYS);
210+
f_open(&vfs_fat->fatfs, &fp, "/pybcdc.inf", FA_WRITE | FA_CREATE_ALWAYS);
212211
f_write(&fp, fresh_pybcdc_inf, sizeof(fresh_pybcdc_inf) - 1 /* don't count null terminator */, &n);
213212
f_close(&fp);
214213

215214
// create readme file
216-
f_open(&fp, "/flash/README.txt", FA_WRITE | FA_CREATE_ALWAYS);
215+
f_open(&vfs_fat->fatfs, &fp, "/README.txt", FA_WRITE | FA_CREATE_ALWAYS);
217216
f_write(&fp, fresh_readme_txt, sizeof(fresh_readme_txt) - 1 /* don't count null terminator */, &n);
218217
f_close(&fp);
219218

@@ -224,38 +223,33 @@ MP_NOINLINE STATIC void init_flash_fs(uint reset_mode) {
224223
// mount sucessful
225224
} else {
226225
printf("PYB: can't mount flash\n");
227-
MP_STATE_PORT(fs_user_mount)[0] = NULL;
228226
return;
229227
}
230228

229+
// mount the flash device (there should be no other devices mounted at this point)
230+
mp_vfs_mount_t *vfs = &mp_vfs_mount_flash;
231+
vfs->str = "/flash";
232+
vfs->len = 6;
233+
vfs->obj = MP_OBJ_FROM_PTR(vfs_fat);
234+
vfs->next = NULL;
235+
MP_STATE_VM(vfs_mount_table) = vfs;
236+
231237
// The current directory is used as the boot up directory.
232238
// It is set to the internal flash filesystem by default.
233-
f_chdrive("/flash");
239+
MP_STATE_PORT(vfs_cur) = vfs;
234240

235241
// Make sure we have a /flash/boot.py. Create it if needed.
236242
FILINFO fno;
237-
#if _USE_LFN
238-
fno.lfname = NULL;
239-
fno.lfsize = 0;
240-
#endif
241-
res = f_stat("/flash/boot.py", &fno);
242-
if (res == FR_OK) {
243-
if (fno.fattrib & AM_DIR) {
244-
// exists as a directory
245-
// TODO handle this case
246-
// see http://elm-chan.org/fsw/ff/img/app2.c for a "rm -rf" implementation
247-
} else {
248-
// exists as a file, good!
249-
}
250-
} else {
243+
res = f_stat(&vfs_fat->fatfs, "/boot.py", &fno);
244+
if (res != FR_OK) {
251245
// doesn't exist, create fresh file
252246

253247
// LED on to indicate creation of boot.py
254248
led_state(PYB_LED_R2, 1);
255249
uint32_t start_tick = HAL_GetTick();
256250

257251
FIL fp;
258-
f_open(&fp, "/flash/boot.py", FA_WRITE | FA_CREATE_ALWAYS);
252+
f_open(&vfs_fat->fatfs, &fp, "/flash/boot.py", FA_WRITE | FA_CREATE_ALWAYS);
259253
UINT n;
260254
f_write(&fp, fresh_boot_py, sizeof(fresh_boot_py) - 1 /* don't count null terminator */, &n);
261255
// TODO check we could write n bytes
@@ -485,24 +479,29 @@ int main(void) {
485479
// if an SD card is present then mount it on /sd/
486480
if (sdcard_is_present()) {
487481
// create vfs object
488-
fs_user_mount_t *vfs = m_new_obj_maybe(fs_user_mount_t);
489-
if (vfs == NULL) {
482+
fs_user_mount_t *vfs_fat = m_new_obj_maybe(fs_user_mount_t);
483+
mp_vfs_mount_t *vfs = m_new_obj_maybe(mp_vfs_mount_t);
484+
if (vfs == NULL || vfs_fat == NULL) {
490485
goto no_mem_for_sd;
491486
}
492-
vfs->str = "/sd";
493-
vfs->len = 3;
494-
vfs->flags = FSUSER_FREE_OBJ;
495-
sdcard_init_vfs(vfs);
496-
497-
// put the sd device in slot 1 (it will be unused at this point)
498-
MP_STATE_PORT(fs_user_mount)[1] = vfs;
487+
vfs_fat->str = NULL;
488+
vfs_fat->len = 0;
489+
vfs_fat->flags = FSUSER_FREE_OBJ;
490+
sdcard_init_vfs(vfs_fat);
499491

500-
FRESULT res = f_mount(&vfs->fatfs, vfs->str, 1);
492+
FRESULT res = f_mount(&vfs_fat->fatfs);
501493
if (res != FR_OK) {
502494
printf("PYB: can't mount SD card\n");
503-
MP_STATE_PORT(fs_user_mount)[1] = NULL;
504-
m_del_obj(fs_user_mount_t, vfs);
495+
m_del_obj(fs_user_mount_t, vfs_fat);
496+
m_del_obj(mp_vfs_mount_t, vfs);
505497
} else {
498+
// mount the sd device after the internal flash
499+
vfs->str = "/sd";
500+
vfs->len = 3;
501+
vfs->obj = MP_OBJ_FROM_PTR(vfs_fat);
502+
vfs->next = NULL;
503+
MP_STATE_VM(vfs_mount_table)->next = vfs;
504+
506505
// TODO these should go before the /flash entries in the path
507506
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_sd));
508507
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_sd_slash_lib));
@@ -520,7 +519,7 @@ int main(void) {
520519
#endif
521520
{
522521
// use SD card as current directory
523-
f_chdrive("/sd");
522+
MP_STATE_PORT(vfs_cur) = vfs;
524523
}
525524
}
526525
no_mem_for_sd:;
@@ -534,8 +533,8 @@ int main(void) {
534533
// TODO perhaps have pyb.reboot([bootpy]) function to soft-reboot and execute custom boot.py
535534
if (reset_mode == 1 || reset_mode == 3) {
536535
const char *boot_py = "boot.py";
537-
FRESULT res = f_stat(boot_py, NULL);
538-
if (res == FR_OK) {
536+
mp_import_stat_t stat = mp_import_stat(boot_py);
537+
if (stat == MP_IMPORT_STAT_FILE) {
539538
int ret = pyexec_file(boot_py);
540539
if (ret & PYEXEC_FORCED_EXIT) {
541540
goto soft_reset_exit;
@@ -597,8 +596,8 @@ int main(void) {
597596
} else {
598597
main_py = mp_obj_str_get_str(MP_STATE_PORT(pyb_config_main));
599598
}
600-
FRESULT res = f_stat(main_py, NULL);
601-
if (res == FR_OK) {
599+
mp_import_stat_t stat = mp_import_stat(main_py);
600+
if (stat == MP_IMPORT_STAT_FILE) {
602601
int ret = pyexec_file(main_py);
603602
if (ret & PYEXEC_FORCED_EXIT) {
604603
goto soft_reset_exit;

stmhal/modmachine.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
#include <stdio.h>
28+
#include <string.h>
2829

2930
#include "modmachine.h"
3031
#include "py/gc.h"
@@ -34,8 +35,9 @@
3435
#include "extmod/machine_pulse.h"
3536
#include "extmod/machine_i2c.h"
3637
#include "lib/utils/pyexec.h"
37-
#include "lib/fatfs/ff.h"
38-
#include "lib/fatfs/diskio.h"
38+
#include "lib/oofatfs/ff.h"
39+
#include "extmod/vfs.h"
40+
#include "extmod/fsusermount.h"
3941
#include "gccollect.h"
4042
#include "irq.h"
4143
#include "rng.h"
@@ -144,10 +146,16 @@ STATIC mp_obj_t machine_info(mp_uint_t n_args, const mp_obj_t *args) {
144146

145147
// free space on flash
146148
{
147-
DWORD nclst;
148-
FATFS *fatfs;
149-
f_getfree("/flash", &nclst, &fatfs);
150-
printf("LFS free: %u bytes\n", (uint)(nclst * fatfs->csize * 512));
149+
for (mp_vfs_mount_t *vfs = MP_STATE_VM(vfs_mount_table); vfs != NULL; vfs = vfs->next) {
150+
if (strncmp("/flash", vfs->str, vfs->len) == 0) {
151+
// assumes that it's a FatFs filesystem
152+
fs_user_mount_t *vfs_fat = MP_OBJ_TO_PTR(vfs->obj);
153+
DWORD nclst;
154+
f_getfree(&vfs_fat->fatfs, &nclst);
155+
printf("LFS free: %u bytes\n", (uint)(nclst * vfs_fat->fatfs.csize * 512));
156+
break;
157+
}
158+
}
151159
}
152160

153161
if (n_args == 1) {

0 commit comments

Comments
 (0)