Skip to content

Commit e743676

Browse files
VynDragondpgeorge
authored andcommitted
zephyr: Introduce Zephyr native filesystem VFS interface.
This allows using Zephyr's supported VFS and interface it to MicroPython's VFS. That makes the MicroPython-side filesystem drivers unecessary, eg you can disable LFS2 in MicroPython but still do LFS2 because it's using Zephyr's. An advantage of this is that the filesystem can be shared between Zephyr C code and MicroPython. Signed-off-by: Vdragon <mail@massdriver.space>
1 parent 53189f9 commit e743676

File tree

6 files changed

+837
-2
lines changed

6 files changed

+837
-2
lines changed

ports/zephyr/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ set(MICROPY_SOURCE_PORT
5555
uart_core.c
5656
zephyr_device.c
5757
zephyr_storage.c
58+
zephyr_filesystem.c
5859
mpthreadport.c
5960
)
6061
list(TRANSFORM MICROPY_SOURCE_PORT PREPEND ${MICROPY_PORT_DIR}/)

ports/zephyr/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,47 @@ run the following after you built an image with the previous command:
208208

209209
$ west build -t run
210210

211+
File Systems
212+
------------
213+
214+
The Zephyr Micropython port provides 2 options for handling filesystems on the device:
215+
The first is the Micropython filesystem management, which uses Micropython's filesystem code and
216+
relies on zephyr's FlashArea API, this is enabled by default when
217+
`CONFIG_FLASH` and `CONFIG_FLASH_MAP` are turned on.
218+
The second option is using Zephyr's Filesystem management:
219+
This relies on Zephyr's File System features and enables sharing access between zephyr and
220+
micropython code. Several configuration options must be enabled:
221+
222+
CONFIG_FLASH_MAP=y # Requirement for the file system subsystem
223+
CONFIG_FILE_SYSTEM=y # Enables the file system subsystem
224+
CONFIG_FILE_SYSTEM_LITTLEFS=y # Enables the littlefs support in zephyr
225+
CONFIG_FILE_SYSTEM_MKFS=y # Enables the ability to create new file systems
226+
227+
Then, a fstab must be added to the dts overlay, for example:
228+
229+
230+
fstab {
231+
compatible = "zephyr,fstab";
232+
lfs2: lfs2 {
233+
compatible = "zephyr,fstab,littlefs";
234+
mount-point = "/flash";
235+
partition = <&storage_partition>;
236+
read-size=<16>;
237+
prog-size=<4096>;
238+
cache-size=<4096>;
239+
lookahead-size=<32>;
240+
block-cycles=<4>;
241+
};
242+
};
243+
244+
It is then possible to use the FS like a normal Micropython filesystem:
245+
246+
import vfs, zephyr
247+
zfs = zephyr.FileSystem(zephyr.FileSystem.fstab()[0])
248+
vfs.mount(zfs, "/zephyr")
249+
250+
You may disable Micropython's File system code to save space:
251+
252+
CONFIG_MICROPY_VFS_FAT=n
253+
CONFIG_MICROPY_VFS_LFS1=n
254+
CONFIG_MICROPY_VFS_LFS2=n

ports/zephyr/modzephyr.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ static const mp_rom_map_elem_t mp_module_time_globals_table[] = {
8686
#ifdef CONFIG_FLASH_MAP
8787
{ MP_ROM_QSTR(MP_QSTR_FlashArea), MP_ROM_PTR(&zephyr_flash_area_type) },
8888
#endif
89+
#ifdef CONFIG_FILE_SYSTEM
90+
{ MP_ROM_QSTR(MP_QSTR_FileSystem), MP_ROM_PTR(&zephyr_filesystem_type) },
91+
#endif
8992
};
9093

9194
static MP_DEFINE_CONST_DICT(mp_module_time_globals, mp_module_time_globals_table);

ports/zephyr/modzephyr.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,8 @@ extern const mp_obj_type_t zephyr_disk_access_type;
3737
extern const mp_obj_type_t zephyr_flash_area_type;
3838
#endif
3939

40+
#ifdef CONFIG_FILE_SYSTEM
41+
extern const mp_obj_type_t zephyr_filesystem_type;
42+
#endif
43+
4044
#endif // MICROPY_INCLUDED_ZEPHYR_MODZEPHYR_H

ports/zephyr/mpconfigport.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,10 @@
8484
#endif
8585
#define MICROPY_PY_MACHINE_PWM (1)
8686
#define MICROPY_PY_MACHINE_PWM_INCLUDEFILE "ports/zephyr/machine_pwm.c"
87-
#ifdef CONFIG_NETWORKING
88-
// If we have networking, we likely want errno comfort
87+
#if defined(CONFIG_NETWORKING) || defined(CONFIG_FILE_SYSTEM)
8988
#define MICROPY_PY_ERRNO (1)
89+
#endif
90+
#ifdef CONFIG_NETWORKING
9091
#define MICROPY_PY_SOCKET (1)
9192
#endif
9293
#ifdef CONFIG_BT

0 commit comments

Comments
 (0)