Skip to content

Commit a099505

Browse files
committed
extmod: Add VFS littlefs bindings.
Both LFS1 and LFS2 are supported at the same time.
1 parent 98beea9 commit a099505

File tree

6 files changed

+837
-0
lines changed

6 files changed

+837
-0
lines changed

extmod/extmod.mk

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@
33
# this sets the config file for FatFs
44
CFLAGS_MOD += -DFFCONF_H=\"lib/oofatfs/ffconf.h\"
55

6+
################################################################################
7+
# VFS littlefs
8+
9+
ifeq ($(MICROPY_VFS_LFS),1)
10+
CFLAGS_MOD += -DMICROPY_VFS_LFS=1
11+
CFLAGS_MOD += -DLFS1_NO_MALLOC -DLFS1_NO_DEBUG -DLFS1_NO_WARN -DLFS1_NO_ERROR -DLFS1_NO_ASSERT
12+
CFLAGS_MOD += -DLFS2_NO_MALLOC -DLFS2_NO_DEBUG -DLFS2_NO_WARN -DLFS2_NO_ERROR -DLFS2_NO_ASSERT
13+
LITTLEFS_DIR = lib/littlefs
14+
SRC_MOD += $(addprefix $(LITTLEFS_DIR)/,\
15+
lfs1.c \
16+
lfs1_util.c \
17+
lfs2.c \
18+
lfs2_util.c \
19+
)
20+
endif
21+
622
################################################################################
723
# ussl
824

extmod/vfs_lfs.c

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2019 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/runtime.h"
28+
#include "extmod/vfs.h"
29+
#include "extmod/vfs_lfs.h"
30+
31+
#if MICROPY_VFS && MICROPY_VFS_LFS
32+
33+
enum { LFS_MAKE_ARG_bdev, LFS_MAKE_ARG_readsize, LFS_MAKE_ARG_progsize, LFS_MAKE_ARG_lookahead };
34+
35+
static const mp_arg_t lfs_make_allowed_args[] = {
36+
{ MP_QSTR_, MP_ARG_OBJ },
37+
{ MP_QSTR_readsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 32} },
38+
{ MP_QSTR_progsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 32} },
39+
{ MP_QSTR_lookahead, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 32} },
40+
};
41+
42+
#include "lib/littlefs/lfs1.h"
43+
44+
#define LFS_BUILD_VERSION (1)
45+
#define LFSx_MACRO(s) LFS1 ## s
46+
#define LFSx_API(s) lfs1_ ## s
47+
#define MP_VFS_LFSx(s) mp_vfs_lfs1_ ## s
48+
#define MP_OBJ_VFS_LFSx mp_obj_vfs_lfs1_t
49+
#define MP_OBJ_VFS_LFSx_FILE mp_obj_vfs_lfs1_file_t
50+
#define MP_TYPE_VFS_LFSx mp_type_vfs_lfs1
51+
#define MP_TYPE_VFS_LFSx_(s) mp_type_vfs_lfs1 ## s
52+
53+
typedef struct _mp_obj_vfs_lfs1_t {
54+
mp_obj_base_t base;
55+
mp_vfs_blockdev_t blockdev;
56+
vstr_t cur_dir;
57+
struct lfs1_config config;
58+
lfs1_t lfs;
59+
} mp_obj_vfs_lfs1_t;
60+
61+
typedef struct _mp_obj_vfs_lfs1_file_t {
62+
mp_obj_base_t base;
63+
mp_obj_vfs_lfs1_t *vfs;
64+
lfs1_file_t file;
65+
struct lfs1_file_config cfg;
66+
uint8_t file_buffer[0];
67+
} mp_obj_vfs_lfs1_file_t;
68+
69+
const char *mp_vfs_lfs1_make_path(mp_obj_vfs_lfs1_t *self, mp_obj_t path_in);
70+
mp_obj_t mp_vfs_lfs1_file_open(mp_obj_t self_in, mp_obj_t path_in, mp_obj_t mode_in);
71+
72+
#include "extmod/vfs_lfsx.c"
73+
#include "extmod/vfs_lfsx_file.c"
74+
75+
#undef LFS_BUILD_VERSION
76+
#undef LFSx_MACRO
77+
#undef LFSx_API
78+
#undef MP_VFS_LFSx
79+
#undef MP_OBJ_VFS_LFSx
80+
#undef MP_OBJ_VFS_LFSx_FILE
81+
#undef MP_TYPE_VFS_LFSx
82+
#undef MP_TYPE_VFS_LFSx_
83+
84+
#include "lib/littlefs/lfs2.h"
85+
86+
#define LFS_BUILD_VERSION (2)
87+
#define LFSx_MACRO(s) LFS2 ## s
88+
#define LFSx_API(s) lfs2_ ## s
89+
#define MP_VFS_LFSx(s) mp_vfs_lfs2_ ## s
90+
#define MP_OBJ_VFS_LFSx mp_obj_vfs_lfs2_t
91+
#define MP_OBJ_VFS_LFSx_FILE mp_obj_vfs_lfs2_file_t
92+
#define MP_TYPE_VFS_LFSx mp_type_vfs_lfs2
93+
#define MP_TYPE_VFS_LFSx_(s) mp_type_vfs_lfs2 ## s
94+
95+
typedef struct _mp_obj_vfs_lfs2_t {
96+
mp_obj_base_t base;
97+
mp_vfs_blockdev_t blockdev;
98+
vstr_t cur_dir;
99+
struct lfs2_config config;
100+
lfs2_t lfs;
101+
} mp_obj_vfs_lfs2_t;
102+
103+
typedef struct _mp_obj_vfs_lfs2_file_t {
104+
mp_obj_base_t base;
105+
mp_obj_vfs_lfs2_t *vfs;
106+
lfs2_file_t file;
107+
struct lfs2_file_config cfg;
108+
uint8_t file_buffer[0];
109+
} mp_obj_vfs_lfs2_file_t;
110+
111+
const char *mp_vfs_lfs2_make_path(mp_obj_vfs_lfs2_t *self, mp_obj_t path_in);
112+
mp_obj_t mp_vfs_lfs2_file_open(mp_obj_t self_in, mp_obj_t path_in, mp_obj_t mode_in);
113+
114+
#include "extmod/vfs_lfsx.c"
115+
#include "extmod/vfs_lfsx_file.c"
116+
117+
#endif // MICROPY_VFS && MICROPY_VFS_LFS

extmod/vfs_lfs.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2019 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
#ifndef MICROPY_INCLUDED_EXTMOD_VFS_LFS_H
27+
#define MICROPY_INCLUDED_EXTMOD_VFS_LFS_H
28+
29+
#include "py/obj.h"
30+
31+
extern const mp_obj_type_t mp_type_vfs_lfs1;
32+
extern const mp_obj_type_t mp_type_vfs_lfs1_fileio;
33+
extern const mp_obj_type_t mp_type_vfs_lfs1_textio;
34+
35+
extern const mp_obj_type_t mp_type_vfs_lfs2;
36+
extern const mp_obj_type_t mp_type_vfs_lfs2_fileio;
37+
extern const mp_obj_type_t mp_type_vfs_lfs2_textio;
38+
39+
#endif // MICROPY_INCLUDED_EXTMOD_VFS_LFS_H

0 commit comments

Comments
 (0)