Kaynağa Gözat

add: 为sfud添加文件系统支持,待测试

Wendal Chen 4 yıl önce
ebeveyn
işleme
750c1dca8b
2 değiştirilmiş dosya ile 126 ekleme ve 0 silme
  1. 23 0
      components/sfud/luat_lib_sfud.c
  2. 103 0
      components/sfud/luat_sfud_lfs2.c

+ 23 - 0
components/sfud/luat_lib_sfud.c

@@ -184,6 +184,29 @@ static int l_sfud_erase_write(lua_State *L){
     return 1;
 }
 
+#include "luat_fs.h"
+#include "lfs.h"
+lfs_t* flash_lfs_sfud(sfud_flash* flash);
+static int l_sfud_mount(lua_State *L) {
+    const sfud_flash *flash = lua_touserdata(L, 1);
+    const char* mount_point = luaL_checkstring(L, 2);
+    lfs_t* lfs = flash_lfs_sfud(flash);
+    if (lfs) {
+	    luat_fs_conf_t conf = {
+		    .busname = (char*)lfs,
+		    .type = "lfs2",
+		    .filesystem = "lfs2",
+		    .mount_point = mount_point,
+	    };
+	    luat_fs_mount(&conf);
+        lua_pushboolean(L, 1);
+    }
+    else {
+        lua_pushboolean(L, 0);
+    }
+    return 1;
+}
+
 #include "rotable.h"
 static const rotable_Reg reg_sfud[] =
 {

+ 103 - 0
components/sfud/luat_sfud_lfs2.c

@@ -0,0 +1,103 @@
+
+#include "luat_base.h"
+#include "luat_fs.h"
+
+#ifdef LUAT_USE_SFUD
+#ifdef LUAT_USE_FS_VFS
+#include "lfs.h"
+#include "sfud.h"
+#define LFS_BLOCK_SIZE (4096)
+#define LFS_BLOCK_DEVICE_READ_SIZE (256)
+#define LFS_BLOCK_DEVICE_PROG_SIZE (256)
+#define LFS_BLOCK_DEVICE_CACHE_SIZE (256)
+#define LFS_BLOCK_DEVICE_ERASE_SIZE (4096) // one sector 4KB
+//#define LFS_BLOCK_DEVICE_TOTOAL_SIZE (FLASH_FS_REGION_SIZE)
+#define LFS_BLOCK_DEVICE_LOOK_AHEAD (16)
+
+// Read a block
+static int block_device_read(const struct lfs_config *cfg, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size) {
+    sfud_flash* flash = (sfud_flash*)cfg->context;
+    sfud_read(flash, buffer, block * LFS_BLOCK_SIZE + off, size);
+    return LFS_ERR_OK;
+}
+
+static int block_device_prog(const struct lfs_config *cfg, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size) {
+    sfud_flash* flash = (sfud_flash*)cfg->context;
+    sfud_write(flash, buffer, block * LFS_BLOCK_SIZE + off, size);
+    return LFS_ERR_OK;
+}
+
+static int block_device_erase(const struct lfs_config *cfg, lfs_block_t block) {
+    sfud_flash* flash = (sfud_flash*)cfg->context;
+    sfud_erase(flash, block * LFS_BLOCK_SIZE, LFS_BLOCK_SIZE);
+    return LFS_ERR_OK;
+}
+
+static int block_device_sync(const struct lfs_config *cfg) {
+    return LFS_ERR_OK;
+}
+
+typedef struct LFS2 {
+    lfs_t lfs;
+    struct lfs_config cfg;
+    uint8_t read_buffer[LFS_BLOCK_DEVICE_READ_SIZE];
+    uint8_t prog_buffer[LFS_BLOCK_DEVICE_PROG_SIZE];
+    uint8_t lookahead_buffer[LFS_BLOCK_DEVICE_LOOK_AHEAD];
+}LFS2_t;
+
+
+lfs_t* flash_lfs_sfud(sfud_flash* flash) {
+    LFS2_t *_lfs = luat_heap_malloc(sizeof(LFS2_t));
+    if (_lfs == NULL)
+        return NULL;
+    lfs_t *lfs = &_lfs->lfs;
+    struct lfs_config *lfs_cfg = &_lfs->cfg;
+
+    lfs_cfg->context = flash,
+    // block device operations
+    lfs_cfg->read = block_device_read;
+    lfs_cfg->prog = block_device_prog;
+    lfs_cfg->erase = block_device_erase;
+    lfs_cfg->sync = block_device_sync;
+
+    // block device configuration
+    lfs_cfg->read_size = LFS_BLOCK_DEVICE_READ_SIZE;
+    lfs_cfg->prog_size = LFS_BLOCK_DEVICE_PROG_SIZE;
+    lfs_cfg->block_size = flash->chip.erase_gran;
+    lfs_cfg->block_count = flash->chip.capacity / flash->chip.erase_gran;
+    lfs_cfg->block_cycles = 200;
+    lfs_cfg->cache_size = LFS_BLOCK_DEVICE_CACHE_SIZE;
+    lfs_cfg->lookahead_size = LFS_BLOCK_DEVICE_LOOK_AHEAD;
+
+    lfs_cfg->read_buffer = _lfs->read_buffer;
+    lfs_cfg->prog_buffer = _lfs->prog_buffer;
+    lfs_cfg->lookahead_buffer = _lfs->lookahead_buffer;
+    lfs_cfg->name_max = 63;
+    lfs_cfg->file_max = 0;
+    lfs_cfg->attr_max = 0;
+
+    // ------
+    int err = lfs_mount(&lfs, &lfs_cfg);
+    LLOGD("lfs_mount %d",err);
+    if (err)
+    {
+        err = lfs_format(&lfs, &lfs_cfg);
+        LLOGD("lfs_format %d",err);
+        if(err)
+            goto fail;
+        err = lfs_mount(&lfs, &lfs_cfg);
+        LLOGD("lfs_mount %d",err);
+        if(err)
+            goto fail;
+    }
+    return lfs;
+fail :
+    luat_heap_free(_lfs);
+    return NULL;
+    //------
+}
+
+#endif
+
+#endif
+