Skip to content

Commit f9ecd48

Browse files
committed
esp8266: Change to use new generic VFS sub-system.
The VFS sub-system supports mounting of an arbitrary number of devices (limited only by available RAM). The internal flash is now mounted at "/flash".
1 parent f1e0414 commit f9ecd48

7 files changed

Lines changed: 39 additions & 108 deletions

File tree

esp8266/main.c

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ STATIC void mp_reset(void) {
4949
mp_init();
5050
mp_obj_list_init(mp_sys_path, 0);
5151
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
52-
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib));
53-
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_));
52+
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash_slash_lib));
53+
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash));
5454
mp_obj_list_init(mp_sys_argv, 0);
5555
MP_STATE_PORT(term_obj) = MP_OBJ_NULL;
5656
MP_STATE_PORT(dupterm_arr_obj) = MP_OBJ_NULL;
@@ -109,34 +109,23 @@ void user_init(void) {
109109
system_init_done_cb(init_done);
110110
}
111111

112-
mp_import_stat_t fat_vfs_import_stat(const char *path);
113-
114-
#if !MICROPY_VFS_FAT
112+
#if !MICROPY_VFS
115113
mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
116114
return NULL;
117115
}
118-
#endif
119116

120117
mp_import_stat_t mp_import_stat(const char *path) {
121-
#if MICROPY_VFS_FAT
122-
return fat_vfs_import_stat(path);
123-
#else
124118
(void)path;
125119
return MP_IMPORT_STAT_NO_EXIST;
126-
#endif
127120
}
128121

129-
mp_obj_t vfs_proxy_call(qstr method_name, mp_uint_t n_args, const mp_obj_t *args);
130122
mp_obj_t mp_builtin_open(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) {
131-
#if MICROPY_VFS_FAT
132-
// TODO: Handle kwargs!
133-
return vfs_proxy_call(MP_QSTR_open, n_args, args);
134-
#else
135123
return mp_const_none;
136-
#endif
137124
}
138125
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open);
139126

127+
#endif
128+
140129
void MP_FASTCODE(nlr_jump_fail)(void *val) {
141130
printf("NLR jump failed\n");
142131
for (;;) {

esp8266/modules/_boot.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
try:
77
if bdev:
8-
vfs = uos.VfsFat(bdev, "")
8+
vfs = uos.VfsFat(bdev)
9+
uos.mount(vfs, '/flash')
10+
uos.chdir('/flash')
911
except OSError:
1012
import inisetup
1113
vfs = inisetup.setup()

esp8266/moduos.c

Lines changed: 12 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,10 @@
2626

2727
#include <string.h>
2828

29-
#include "py/mpconfig.h"
30-
#include "py/nlr.h"
31-
#include "py/obj.h"
3229
#include "py/objtuple.h"
3330
#include "py/objstr.h"
34-
#include "py/runtime.h"
35-
#include "py/mperrno.h"
3631
#include "extmod/misc.h"
32+
#include "extmod/vfs.h"
3733
#include "extmod/vfs_fat.h"
3834
#include "genhdr/mpversion.h"
3935
#include "esp_mphal.h"
@@ -70,74 +66,6 @@ STATIC mp_obj_t os_uname(void) {
7066
}
7167
STATIC MP_DEFINE_CONST_FUN_OBJ_0(os_uname_obj, os_uname);
7268

73-
#if MICROPY_VFS_FAT
74-
mp_obj_t vfs_proxy_call(qstr method_name, mp_uint_t n_args, const mp_obj_t *args) {
75-
if (MP_STATE_PORT(fs_user_mount)[0] == NULL) {
76-
mp_raise_OSError(MP_ENODEV);
77-
}
78-
79-
mp_obj_t meth[n_args + 2];
80-
mp_load_method(MP_STATE_PORT(fs_user_mount)[0], method_name, meth);
81-
if (args != NULL) {
82-
memcpy(meth + 2, args, n_args * sizeof(*args));
83-
}
84-
return mp_call_method_n_kw(n_args, 0, meth);
85-
}
86-
87-
STATIC mp_obj_t os_listdir(mp_uint_t n_args, const mp_obj_t *args) {
88-
return vfs_proxy_call(MP_QSTR_listdir, n_args, args);
89-
}
90-
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(os_listdir_obj, 0, 1, os_listdir);
91-
92-
STATIC mp_obj_t os_mkdir(mp_obj_t path_in) {
93-
return vfs_proxy_call(MP_QSTR_mkdir, 1, &path_in);
94-
}
95-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_mkdir_obj, os_mkdir);
96-
97-
STATIC mp_obj_t os_rmdir(mp_obj_t path_in) {
98-
return vfs_proxy_call(MP_QSTR_rmdir, 1, &path_in);
99-
}
100-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_rmdir_obj, os_rmdir);
101-
102-
STATIC mp_obj_t os_chdir(mp_obj_t path_in) {
103-
return vfs_proxy_call(MP_QSTR_chdir, 1, &path_in);
104-
}
105-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_chdir_obj, os_chdir);
106-
107-
STATIC mp_obj_t os_getcwd(void) {
108-
return vfs_proxy_call(MP_QSTR_getcwd, 0, NULL);
109-
}
110-
STATIC MP_DEFINE_CONST_FUN_OBJ_0(os_getcwd_obj, os_getcwd);
111-
112-
STATIC mp_obj_t os_remove(mp_obj_t path_in) {
113-
return vfs_proxy_call(MP_QSTR_remove, 1, &path_in);
114-
}
115-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_remove_obj, os_remove);
116-
117-
STATIC mp_obj_t os_rename(mp_obj_t path_old, mp_obj_t path_new) {
118-
mp_obj_t args[2];
119-
args[0] = path_old;
120-
args[1] = path_new;
121-
return vfs_proxy_call(MP_QSTR_rename, 2, args);
122-
}
123-
STATIC MP_DEFINE_CONST_FUN_OBJ_2(os_rename_obj, os_rename);
124-
125-
STATIC mp_obj_t os_stat(mp_obj_t path_in) {
126-
return vfs_proxy_call(MP_QSTR_stat, 1, &path_in);
127-
}
128-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_stat_obj, os_stat);
129-
130-
STATIC mp_obj_t os_statvfs(mp_obj_t path_in) {
131-
return vfs_proxy_call(MP_QSTR_statvfs, 1, &path_in);
132-
}
133-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_statvfs_obj, os_statvfs);
134-
135-
STATIC mp_obj_t os_umount(void) {
136-
return vfs_proxy_call(MP_QSTR_umount, 0, NULL);
137-
}
138-
STATIC MP_DEFINE_CONST_FUN_OBJ_0(os_umount_obj, os_umount);
139-
#endif
140-
14169
STATIC mp_obj_t os_urandom(mp_obj_t num) {
14270
mp_int_t n = mp_obj_get_int(num);
14371
vstr_t vstr;
@@ -166,16 +94,17 @@ STATIC const mp_rom_map_elem_t os_module_globals_table[] = {
16694
#endif
16795
#if MICROPY_VFS_FAT
16896
{ MP_ROM_QSTR(MP_QSTR_VfsFat), MP_ROM_PTR(&mp_fat_vfs_type) },
169-
{ MP_ROM_QSTR(MP_QSTR_listdir), MP_ROM_PTR(&os_listdir_obj) },
170-
{ MP_ROM_QSTR(MP_QSTR_mkdir), MP_ROM_PTR(&os_mkdir_obj) },
171-
{ MP_ROM_QSTR(MP_QSTR_rmdir), MP_ROM_PTR(&os_rmdir_obj) },
172-
{ MP_ROM_QSTR(MP_QSTR_chdir), MP_ROM_PTR(&os_chdir_obj) },
173-
{ MP_ROM_QSTR(MP_QSTR_getcwd), MP_ROM_PTR(&os_getcwd_obj) },
174-
{ MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&os_remove_obj) },
175-
{ MP_ROM_QSTR(MP_QSTR_rename), MP_ROM_PTR(&os_rename_obj) },
176-
{ MP_ROM_QSTR(MP_QSTR_stat), MP_ROM_PTR(&os_stat_obj) },
177-
{ MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&os_statvfs_obj) },
178-
{ MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&os_umount_obj) },
97+
{ MP_ROM_QSTR(MP_QSTR_listdir), MP_ROM_PTR(&mp_vfs_listdir_obj) },
98+
{ MP_ROM_QSTR(MP_QSTR_mkdir), MP_ROM_PTR(&mp_vfs_mkdir_obj) },
99+
{ MP_ROM_QSTR(MP_QSTR_rmdir), MP_ROM_PTR(&mp_vfs_rmdir_obj) },
100+
{ MP_ROM_QSTR(MP_QSTR_chdir), MP_ROM_PTR(&mp_vfs_chdir_obj) },
101+
{ MP_ROM_QSTR(MP_QSTR_getcwd), MP_ROM_PTR(&mp_vfs_getcwd_obj) },
102+
{ MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&mp_vfs_remove_obj) },
103+
{ MP_ROM_QSTR(MP_QSTR_rename), MP_ROM_PTR(&mp_vfs_rename_obj) },
104+
{ MP_ROM_QSTR(MP_QSTR_stat), MP_ROM_PTR(&mp_vfs_stat_obj) },
105+
{ MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&mp_vfs_statvfs_obj) },
106+
{ MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&mp_vfs_mount_obj) },
107+
{ MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&mp_vfs_umount_obj) },
179108
#endif
180109
};
181110

esp8266/mpconfigport.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#define MICROPY_MEM_STATS (0)
1616
#define MICROPY_DEBUG_PRINTERS (1)
1717
#define MICROPY_DEBUG_PRINTER_DEST mp_debug_print
18-
#define MICROPY_READER_FATFS (MICROPY_VFS_FAT)
18+
#define MICROPY_READER_VFS (MICROPY_VFS)
1919
#define MICROPY_ENABLE_GC (1)
2020
#define MICROPY_STACK_CHECK (1)
2121
#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1)
@@ -94,13 +94,13 @@
9494
#define MICROPY_MODULE_FROZEN_LEXER mp_lexer_new_from_str32
9595
#define MICROPY_QSTR_EXTRA_POOL mp_qstr_frozen_const_pool
9696

97+
#define MICROPY_VFS (1)
9798
#define MICROPY_FATFS_OO (1)
9899
#define MICROPY_FATFS_ENABLE_LFN (1)
99100
#define MICROPY_FATFS_RPATH (2)
100101
#define MICROPY_FATFS_VOLUMES (2)
101102
#define MICROPY_FATFS_MAX_SS (4096)
102103
#define MICROPY_FATFS_LFN_CODE_PAGE (437) /* 1=SFN/ANSI 437=LFN/U.S.(OEM) */
103-
#define MICROPY_FSUSERMOUNT (1)
104104
#define MICROPY_VFS_FAT (1)
105105
#define MICROPY_ESP8266_APA102 (1)
106106
#define MICROPY_ESP8266_NEOPIXEL (1)
@@ -141,6 +141,11 @@ void *esp_native_code_commit(void*, size_t);
141141
#define mp_type_fileio fatfs_type_fileio
142142
#define mp_type_textio fatfs_type_textio
143143

144+
// use vfs's functions for import stat and builtin open
145+
#define mp_import_stat mp_vfs_import_stat
146+
#define mp_builtin_open mp_vfs_open
147+
#define mp_builtin_open_obj mp_vfs_open_obj
148+
144149
// extra built in names to add to the global namespace
145150
#define MICROPY_PORT_BUILTINS \
146151
{ MP_OBJ_NEW_QSTR(MP_QSTR_input), (mp_obj_t)&mp_builtin_input_obj }, \

esp8266/mpconfigport_512k.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#undef MICROPY_EMIT_INLINE_XTENSA
66
#define MICROPY_EMIT_INLINE_XTENSA (0)
77

8-
#undef MICROPY_FSUSERMOUNT
9-
#define MICROPY_FSUSERMOUNT (0)
8+
#undef MICROPY_VFS
9+
#define MICROPY_VFS (0)
1010
#undef MICROPY_VFS_FAT
1111
#define MICROPY_VFS_FAT (0)
1212

@@ -25,3 +25,7 @@
2525

2626
#undef MICROPY_PY_FRAMEBUF
2727
#define MICROPY_PY_FRAMEBUF (0)
28+
29+
#undef mp_import_stat
30+
#undef mp_builtin_open
31+
#undef mp_builtin_open_obj

esp8266/qstrdefsport.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@
2727
// qstrs specific to this port, only needed if they aren't auto-generated
2828

2929
// Entries for sys.path
30-
Q(/)
31-
Q(/lib)
30+
Q(/flash)
31+
Q(/flash/lib)

esp8266/scripts/inisetup.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ def setup():
3737
print("Performing initial setup")
3838
wifi()
3939
uos.VfsFat.mkfs(bdev)
40-
vfs = uos.VfsFat(bdev, "")
41-
with open("/boot.py", "w") as f:
40+
vfs = uos.VfsFat(bdev)
41+
uos.mount(vfs, '/flash')
42+
uos.chdir('/flash')
43+
with open("boot.py", "w") as f:
4244
f.write("""\
4345
# This file is executed on every boot (including wake-boot from deepsleep)
4446
#import esp

0 commit comments

Comments
 (0)