!204 update script/libs/tsl2561.lua.
[LuatOS.git] / components / lfs / luat_lib_lfs2.c
blobe5c19b41d1fcae420eb765fcf636461b591200a0
3 #include "luat_base.h"
4 #include "luat_sfd.h"
5 #include "luat_spi.h"
6 #include "luat_gpio.h"
7 #include "luat_mem.h"
8 #include "luat_fs.h"
10 #define LUAT_LOG_TAG "lfs2"
11 #include "luat_log.h"
13 #include "lfs.h"
15 #ifdef LUAT_USE_FS_VFS
16 extern const struct luat_vfs_filesystem vfs_fs_lfs2;
17 #endif
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 {
25 char path[16];
26 void* userdata;
27 int luaref;
28 lfs_t* fs;
29 struct lfs_config* cfg;
30 }lfs2_mount_t;
32 static lfs2_mount_t mounted[2] = {0};
34 /**
35 挂载lifftefs,当前支持spi flash 和 memory 两种
36 @api lfs2.mount(path, drv, formatOnFail)
37 @string 挂载路径
38 @userdata 挂载所需要的额外数据, 当前仅支持sfd
39 @bool 挂载失败时是否尝试格式化,默认为false
40 @usage
41 local drv = sfd.init(0, 18)
42 if drv then
43 local fs = lfs2.mount("/sfd", drv, true)
44 end
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);
56 lua_settop(L, 2);
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));
61 mounted[i].cfg = cfg;
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;
71 cfg->read_size = 256;
72 cfg->prog_size = 256;
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);
82 cfg->name_max = 63;
83 cfg->file_max = 0;
84 cfg->attr_max = 0;
86 cfg->context = drv;
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);
95 if (ret < 0) {
96 luat_heap_free(cfg->lookahead_buffer);
97 luat_heap_free(cfg->prog_buffer);
98 luat_heap_free(cfg->read_buffer);
99 luat_heap_free(cfg);
100 luat_heap_free(mounted[i].fs);
101 mounted[i].fs = NULL;
102 mounted[i].cfg = NULL;
103 mounted[i].userdata = NULL;
105 else {
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,
112 .type = "lfs2",
113 .filesystem = "lfs2",
114 .mount_point = path,
116 luat_fs_mount(&conf2);
117 #endif
119 lua_pushboolean(L, ret >= 0 ? 1 : 0);
120 return 1;
123 LLOGW("only 2 mount is allow");
124 return 0;
128 格式化为lifftefs
129 @api lfs2.mount(path)
130 @string 挂载路径
131 @usage
132 local drv = sfd.init("spi", 0, 18)
133 if drv then
134 local fs = lfs2.mount("sfd", "/sfd", drv)
135 if fs then
136 lfs2.mkfs(fs)
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)
144 continue;
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);
149 return 1;
152 LLOGW("not path match, ignore mkfs");
153 return 0;
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);
171 #endif
172 return 1;