Преглед изворни кода

add: lfs-sdf的关联代码. 做lfs2库意义不大的样子,直接lfs-vfs就好了吧,待定

Wendal Chen пре 4 година
родитељ
комит
df93c256f8

+ 12 - 8
luat/include/luat_sfd.h

@@ -1,7 +1,19 @@
 
 #include "luat_base.h"
 
+
+
+typedef struct sdf_opts {
+    int (*initialize) (void* userdata);
+	int (*status) (void* userdata);
+	int (*read) (void* userdata, char* buff, size_t offset, size_t len);
+	int (*write) (void* userdata, const char* buff, size_t offset, size_t len);
+	int (*erase) (void* userdata, size_t offset, size_t len);
+	int (*ioctl) (void* userdata, size_t cmd, void* buff);
+}sdf_opts_t;
+
 typedef struct sfd_w25q {
+    const sdf_opts_t* opts;
     int spi_id;
     int spi_cs;
     size_t sector_size;
@@ -10,11 +22,3 @@ typedef struct sfd_w25q {
     char chip_id[8];
 } sfd_w25q_t;
 
-typedef struct sdf_opts {
-    int (*initialize) (void* userdata);
-	int (*status) (void* userdata);
-	int (*read) (void* userdata, char* buff, size_t offset, size_t len);
-	int (*write) (void* userdata, const char* buff, size_t offset, size_t len);
-	int (*erase) (void* userdata, size_t sector, size_t count);
-	int (*ioctl) (void* userdata, size_t cmd, void* buff);
-}sdf_opts_t;

+ 7 - 6
luat/modules/luat_lib_sfd.c

@@ -34,8 +34,9 @@ static int l_sfd_init(lua_State *L) {
     memset(w25q, 0, sizeof(sfd_w25q_t));
     w25q->spi_id = spi_id;
     w25q->spi_cs = spi_cs;
+    w25q->opts = &sfd_w25q_opts;
 
-    int re = sfd_w25q_opts.initialize(w25q);
+    int re = w25q->opts->initialize(w25q);
     if (re == 0) {
         return 1;
     }
@@ -55,7 +56,7 @@ end
 */
 static int l_sfd_status(lua_State *L) {
     sfd_w25q_t *w25q = (sfd_w25q_t *) lua_touserdata(L, 1);
-    lua_pushinteger(L, sfd_w25q_opts.status(w25q));
+    lua_pushinteger(L, w25q->opts->status(w25q));
     return 1;
 }
 
@@ -78,7 +79,7 @@ static int l_sfd_read(lua_State *L) {
     size_t len = luaL_checkinteger(L, 3);
     luaL_Buffer buff;
     luaL_buffinitsize(L, &buff, len);
-    sfd_w25q_opts.read(w25q, buff.b, offset, len);
+    w25q->opts->read(w25q, buff.b, offset, len);
     luaL_pushresult(&buff);
     return 1;
 }
@@ -101,7 +102,7 @@ static int l_sfd_write(lua_State *L) {
     size_t offset = luaL_checkinteger(L,2);
     size_t len = 0;
     const char* buff = luaL_checklstring(L, 3, &len);
-    int re = sfd_w25q_opts.write(w25q, buff, offset, len);
+    int re = w25q->opts->write(w25q, buff, offset, len);
     lua_pushboolean(L, re == 0 ? 1 : 0);
     return 1;
 }
@@ -121,8 +122,8 @@ end
 static int l_sfd_erase(lua_State *L) {
     sfd_w25q_t *w25q = (sfd_w25q_t *) lua_touserdata(L, 1);
     size_t offset = luaL_checkinteger(L,2);
-    size_t len = luaL_checkinteger(L, 3);
-    int re = sfd_w25q_opts.erase(w25q, offset, len);
+    size_t len = luaL_optinteger(L, 3, 4096);
+    int re = w25q->opts->erase(w25q, offset, len);
     lua_pushboolean(L, re == 0 ? 1 : 0);
     return 1;
 }

+ 1 - 1
luat/modules/luat_sfd.c

@@ -79,7 +79,7 @@ static int sfd_w25q_init (void* userdata) {
 
     // 读设备唯一id
     luat_gpio_set(w25q->spi_cs, 0);
-    const char* chip_id_cmd = {0x4B, 0x00, 0x00, 0x00, 0x00};
+    char chip_id_cmd[] = {0x4B, 0x00, 0x00, 0x00, 0x00};
     luat_spi_send(w25q->spi_id, chip_id_cmd, 5);
     luat_spi_read(w25q->spi_id, w25q->chip_id, 8);
     luat_gpio_set(w25q->spi_cs, 1);

+ 42 - 0
luat/packages/lfs/lfs_sfd.c

@@ -0,0 +1,42 @@
+
+#
+#include "luat_base.h"
+#include "luat_sfd.h"
+#include "luat_spi.h"
+#include "luat_gpio.h"
+#include "luat_malloc.h"
+
+#include "lfs.h"
+
+#define LUAT_LOG_TAG "lfs2"
+#include "luat_log.h"
+
+int lfs_sfd_read(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size) {
+    sfd_w25q_t *w25q = (sfd_w25q_t *)c->context;
+    return w25q->opts->read(w25q, buffer, block*4096+off, size);
+}
+
+    // Program a region in a block. The block must have previously
+    // been erased. Negative error codes are propogated to the user.
+    // May return LFS_ERR_CORRUPT if the block should be considered bad.
+int lfs_sfd_prog(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size) {
+    sfd_w25q_t *w25q = (sfd_w25q_t *)c->context;
+    return w25q->opts->write(w25q, buffer, block*4096+off, size);
+}
+
+    // Erase a block. A block must be erased before being programmed.
+    // The state of an erased block is undefined. Negative error codes
+    // are propogated to the user.
+    // May return LFS_ERR_CORRUPT if the block should be considered bad.
+int lfs_sfd_erase(const struct lfs_config *c, lfs_block_t block) {
+    sfd_w25q_t *w25q = (sfd_w25q_t *)c->context;
+    return w25q->opts->erase(w25q, block*4096, 4096);
+}
+
+    // Sync the state of the underlying block device. Negative error codes
+    // are propogated to the user.
+int lfs_sfd_sync(const struct lfs_config *c) {
+    sfd_w25q_t *w25q = (sfd_w25q_t *)c->context;
+    //w25q->opts->ioctl(w25q, ???);
+    return 0;
+}

+ 109 - 0
luat/packages/lfs/luat_lib_lfs2.c

@@ -0,0 +1,109 @@
+
+
+#include "luat_base.h"
+#include "luat_sfd.h"
+#include "luat_spi.h"
+#include "luat_gpio.h"
+#include "luat_malloc.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;
+}lfs2_mount_t;
+
+static lfs2_mount_t mounted[2] = {0};
+
+static int l_lfs2_mount(lua_State *L) {
+    const char* path = luaL_checkstring(L, 1);
+    sfd_w25q_t *w25q = lua_touserdata(L, 2);
+    for (size_t i = 0; i < 2; i++)
+    {
+        if (mounted[i].userdata == NULL) {
+            mounted[i].userdata = w25q;
+            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));
+
+            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 = w25q->sector_count / 16;
+            cfg->block_cycles = 200;
+            cfg->cache_size = 16;
+            cfg->lookahead_size = 256;
+
+            // cfg.read_buffer = lfs_read_buf,
+            // cfg.prog_buffer = lfs_prog_buf,
+            // cfg.lookahead_buffer = lfs_lookahead_buf,
+            cfg->name_max = 63;
+            cfg->file_max = 0;
+            cfg->attr_max = 0;
+
+            cfg->context = w25q;
+
+            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;
+}
+
+
+#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);
+  return 1;
+}

+ 10 - 0
luat/vfs/luat_vfs.c

@@ -8,7 +8,17 @@
 
 #ifdef LUAT_USE_FS_VFS
 
+#ifdef getc
 #undef getc
+#endif
+
+#ifdef feof
+#undef feof
+#endif
+
+#ifdef ferror
+#undef ferror
+#endif
 
 static luat_vfs_t vfs= {0};