| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- #include "luat_base.h"
- #include "luat_sfd.h"
- #include "luat_spi.h"
- #include "luat_gpio.h"
- #include "luat_malloc.h"
- #define LUAT_LOG_TAG "lfs2"
- #include "luat_log.h"
- #include "lfs.h"
- int lfs_sfd_read(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size);
- int lfs_sfd_prog(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size);
- int lfs_sfd_erase(const struct lfs_config *c, lfs_block_t block);
- int lfs_sfd_sync(const struct lfs_config *c);
- typedef struct lfs2_mount {
- char path[16];
- void* userdata;
- int luaref;
- lfs_t* fs;
- struct lfs_config* cfg;
- }lfs2_mount_t;
- static lfs2_mount_t mounted[2] = {0};
- /**
- 挂载lifftefs,当前支持spi flash 和 memory 两种
- @string 挂载类型, 当前支持sfd和mem两种
- @string 挂载路径
- @userdata 挂载所需要的额外数据, 当前仅支持sfd
- @usage
- local drv = sfd.init(0, 18)
- if drv then
- local fs = lfs2.mount("/sfd", drv)
- end
- */
- static int l_lfs2_mount(lua_State *L) {
- const char* path = luaL_checkstring(L, 1);
- sfd_drv_t *drv = lua_touserdata(L, 2);
- for (size_t i = 0; i < 2; i++)
- {
- if (mounted[i].userdata == NULL) {
- mounted[i].userdata = drv;
- memcpy(mounted[i].path, path, strlen(path) + 1);
-
- lua_settop(L, 2);
- mounted[i].luaref = luaL_ref(L, LUA_REGISTRYINDEX);
-
- mounted[i].fs = luat_heap_malloc(sizeof(lfs_t));
- struct lfs_config* cfg = (struct lfs_config*)luat_heap_malloc(sizeof(struct lfs_config));
- mounted[i].cfg = cfg;
- memset(cfg, 0, sizeof(struct lfs_config));
- memset(mounted[i].fs, 0, sizeof(lfs_t));
- cfg->read = lfs_sfd_read;
- cfg->prog = lfs_sfd_prog;
- cfg->erase = lfs_sfd_erase;
- cfg->sync = lfs_sfd_sync;
- cfg->read_size = 256;
- cfg->prog_size = 256;
- cfg->block_size = 4096;
- cfg->block_count = drv->sector_count / 16;
- cfg->block_cycles = 200;
- cfg->cache_size = 256;
- cfg->lookahead_size = 16;
- cfg->read_buffer = luat_heap_malloc(256);
- cfg->prog_buffer = luat_heap_malloc(256);
- cfg->lookahead_buffer = luat_heap_malloc(256);
- cfg->name_max = 63;
- cfg->file_max = 0;
- cfg->attr_max = 0;
- cfg->context = drv;
- int ret = lfs_mount(mounted[i].fs, cfg);
- if (ret)
- LLOGW("lfs_mount ret %d", ret);
- lua_pushboolean(L, ret == 0 ? 1 : 0);
- return 1;
- }
- }
- return 0;
- }
- /**
- 格式化为lifftefs
- @api lfs2.mount(path)
- @string 挂载路径
- @usage
- local drv = sfd.init("spi", 0, 18)
- if drv then
- local fs = lfs2.mount("sfd", "/sfd", drv)
- if fs then
- lfs2.mkfs(fs)
- end
- end
- */
- static int l_lfs2_mkfs(lua_State *L) {
- const char* path = luaL_checkstring(L, 1);
- for (size_t i = 0; i < 2; i++) {
- if (mounted[i].userdata == NULL)
- continue;
- if (!strcmp(mounted[i].path, path)) {
- int ret = lfs_format(mounted[i].fs, mounted[i].cfg);
- LLOGD("lfs_format ret %d", ret);
- lua_pushboolean(L, ret == 0 ? 1 : 0);
- return 1;
- }
- }
- LLOGW("not path match, ignore mkfs");
- return 0;
- }
- #include "rotable.h"
- static const rotable_Reg reg_lfs2[] =
- {
- { "mount", l_lfs2_mount, 0}, //初始化,挂载
- // { "unmount", l_lfs2_unmount, 0}, // 取消挂载
- { "mkfs", l_lfs2_mkfs, 0}, // 格式化!!!
- // //{ "test", l_lfs2_test, 0},
- // { "getfree", l_lfs2_getfree, 0}, // 获取文件系统大小,剩余空间
- // { "debug", l_lfs2_debug_mode, 0}, // 调试模式,打印更多日志
- // { "lsdir", l_lfs2_lsdir, 0}, // 列举目录下的文件,名称,大小,日期,属性
- // { "mkdir", l_lfs2_mkdir, 0}, // 列举目录下的文件,名称,大小,日期,属性
- // { "stat", l_lfs2_stat, 0}, // 查询文件信息
- // { "open", l_lfs2_open, 0}, // 打开一个文件句柄
- // { "close", l_lfs2_close, 0}, // 关闭一个文件句柄
- // { "seek", l_lfs2_seek, 0}, // 移动句柄的当前位置
- // { "truncate", l_lfs2_truncate, 0}, // 缩减文件尺寸到当前seek位置
- // { "read", l_lfs2_read, 0}, // 读取数据
- // { "write", l_lfs2_write, 0}, // 写入数据
- // { "remove", l_lfs2_remove, 0}, // 删除文件,别名方法
- // { "unlink", l_lfs2_remove, 0}, // 删除文件
- // { "rename", l_lfs2_rename, 0}, // 文件改名
- // { "readfile", l_lfs2_readfile, 0}, // 读取文件的简易方法
- // { "playmp3", l_lfs2_playmp3, 0}, // 读取文件的简易方法
- { NULL, NULL, 0 }
- };
- int luaopen_lfs2( lua_State *L )
- {
- luat_newlib(L, reg_lfs2);
- #ifdef LUAT_USE_FS_VFS
-
- #endif
- return 1;
- }
|