10 #define LUAT_LOG_TAG "lfs2"
15 #ifdef LUAT_USE_FS_VFS
16 extern const struct luat_vfs_filesystem vfs_fs_lfs2
;
19 int lfs_sfd_read(const struct lfs_config
*c
, lfs_block_t block
, lfs_off_t off
, void *buffer
, lfs_size_t size
);
20 int lfs_sfd_prog(const struct lfs_config
*c
, lfs_block_t block
, lfs_off_t off
, const void *buffer
, lfs_size_t size
);
21 int lfs_sfd_erase(const struct lfs_config
*c
, lfs_block_t block
);
22 int lfs_sfd_sync(const struct lfs_config
*c
);
24 typedef struct lfs2_mount
{
29 struct lfs_config
* cfg
;
32 static lfs2_mount_t mounted
[2] = {0};
35 挂载lifftefs,当前支持spi flash 和 memory 两种
36 @api lfs2.mount(path, drv, formatOnFail)
38 @userdata 挂载所需要的额外数据, 当前仅支持sfd
39 @bool 挂载失败时是否尝试格式化,默认为false
41 local drv = sfd.init(0, 18)
43 local fs = lfs2.mount("/sfd", drv, true)
46 static int l_lfs2_mount(lua_State
*L
) {
47 const char* path
= luaL_checkstring(L
, 1);
48 sfd_drv_t
*drv
= lua_touserdata(L
, 2);
49 bool formatOnFail
= lua_isboolean(L
, 3) ? lua_toboolean(L
, 3) : false;
50 for (size_t i
= 0; i
< 2; i
++)
52 if (mounted
[i
].userdata
== NULL
) {
53 mounted
[i
].userdata
= drv
;
54 memcpy(mounted
[i
].path
, path
, strlen(path
) + 1);
57 mounted
[i
].luaref
= luaL_ref(L
, LUA_REGISTRYINDEX
);
59 mounted
[i
].fs
= luat_heap_malloc(sizeof(lfs_t
));
60 struct lfs_config
* cfg
= (struct lfs_config
*)luat_heap_malloc(sizeof(struct lfs_config
));
63 memset(cfg
, 0, sizeof(struct lfs_config
));
64 memset(mounted
[i
].fs
, 0, sizeof(lfs_t
));
66 cfg
->read
= lfs_sfd_read
;
67 cfg
->prog
= lfs_sfd_prog
;
68 cfg
->erase
= lfs_sfd_erase
;
69 cfg
->sync
= lfs_sfd_sync
;
73 cfg
->block_size
= 4096;
74 cfg
->block_count
= drv
->sector_count
/ 16;
75 cfg
->block_cycles
= 200;
76 cfg
->cache_size
= 256;
77 cfg
->lookahead_size
= 16;
79 cfg
->read_buffer
= luat_heap_malloc(256);
80 cfg
->prog_buffer
= luat_heap_malloc(256);
81 cfg
->lookahead_buffer
= luat_heap_malloc(256);
88 int ret
= lfs_mount(mounted
[i
].fs
, cfg
);
89 LLOGW("lfs_mount ret %d", ret
);
90 if (ret
< 0 && formatOnFail
) {
91 if (lfs_format(mounted
[i
].fs
, cfg
) == 0) {
92 ret
= lfs_mount(mounted
[i
].fs
, cfg
);
96 luat_heap_free(cfg
->lookahead_buffer
);
97 luat_heap_free(cfg
->prog_buffer
);
98 luat_heap_free(cfg
->read_buffer
);
100 luat_heap_free(mounted
[i
].fs
);
101 mounted
[i
].fs
= NULL
;
102 mounted
[i
].cfg
= NULL
;
103 mounted
[i
].userdata
= NULL
;
106 #ifdef LUAT_USE_FS_VFS
107 //char mount_point[16] = {0};
108 //memcpy(mount_point, "/lfs2", strlen("/lfs2"));
109 //memcpy(mount_point + strlen("/lfs2"), path, strlen(path));
110 luat_fs_conf_t conf2
= {
111 .busname
= (char*)mounted
[i
].fs
,
113 .filesystem
= "lfs2",
116 luat_fs_mount(&conf2
);
119 lua_pushboolean(L
, ret
>= 0 ? 1 : 0);
123 LLOGW("only 2 mount is allow");
129 @api lfs2.mount(path)
132 local drv = sfd.init("spi", 0, 18)
134 local fs = lfs2.mount("sfd", "/sfd", drv)
140 static int l_lfs2_mkfs(lua_State
*L
) {
141 const char* path
= luaL_checkstring(L
, 1);
142 for (size_t i
= 0; i
< 2; i
++) {
143 if (mounted
[i
].userdata
== NULL
)
145 if (!strcmp(mounted
[i
].path
, path
)) {
146 int ret
= lfs_format(mounted
[i
].fs
, mounted
[i
].cfg
);
147 LLOGD("lfs_format ret %d", ret
);
148 lua_pushboolean(L
, ret
== 0 ? 1 : 0);
152 LLOGW("not path match, ignore mkfs");
157 #include "rotable2.h"
158 static const rotable_Reg_t reg_lfs2
[] =
160 { "mount", ROREG_FUNC(l_lfs2_mount
)}, //初始化,挂载
161 // { "unmount", ROREG_FUNC(l_lfs2_unmount)}, // 取消挂载
162 { "mkfs", ROREG_FUNC(l_lfs2_mkfs
)}, // 格式化!!!
163 { NULL
, ROREG_INT(0)}
166 int luaopen_lfs2( lua_State
*L
)
168 luat_newlib2(L
, reg_lfs2
);
169 #ifdef LUAT_USE_FS_VFS
170 luat_vfs_reg(&vfs_fs_lfs2
);