luat_lib_lfs2.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include "luat_base.h"
  2. #include "luat_sfd.h"
  3. #include "luat_spi.h"
  4. #include "luat_gpio.h"
  5. #include "luat_malloc.h"
  6. #define LUAT_LOG_TAG "lfs2"
  7. #include "luat_log.h"
  8. #include "lfs.h"
  9. int lfs_sfd_read(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size);
  10. int lfs_sfd_prog(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size);
  11. int lfs_sfd_erase(const struct lfs_config *c, lfs_block_t block);
  12. int lfs_sfd_sync(const struct lfs_config *c);
  13. typedef struct lfs2_mount {
  14. char path[16];
  15. void* userdata;
  16. int luaref;
  17. lfs_t* fs;
  18. struct lfs_config* cfg;
  19. }lfs2_mount_t;
  20. static lfs2_mount_t mounted[2] = {0};
  21. /**
  22. 挂载lifftefs,当前支持spi flash 和 memory 两种
  23. @string 挂载类型, 当前支持sfd和mem两种
  24. @string 挂载路径
  25. @userdata 挂载所需要的额外数据, 当前仅支持sfd
  26. @usage
  27. local drv = sfd.init(0, 18)
  28. if drv then
  29. local fs = lfs2.mount("/sfd", drv)
  30. end
  31. */
  32. static int l_lfs2_mount(lua_State *L) {
  33. const char* path = luaL_checkstring(L, 1);
  34. sfd_drv_t *drv = lua_touserdata(L, 2);
  35. for (size_t i = 0; i < 2; i++)
  36. {
  37. if (mounted[i].userdata == NULL) {
  38. mounted[i].userdata = drv;
  39. memcpy(mounted[i].path, path, strlen(path) + 1);
  40. lua_settop(L, 2);
  41. mounted[i].luaref = luaL_ref(L, LUA_REGISTRYINDEX);
  42. mounted[i].fs = luat_heap_malloc(sizeof(lfs_t));
  43. struct lfs_config* cfg = (struct lfs_config*)luat_heap_malloc(sizeof(struct lfs_config));
  44. mounted[i].cfg = cfg;
  45. memset(cfg, 0, sizeof(struct lfs_config));
  46. memset(mounted[i].fs, 0, sizeof(lfs_t));
  47. cfg->read = lfs_sfd_read;
  48. cfg->prog = lfs_sfd_prog;
  49. cfg->erase = lfs_sfd_erase;
  50. cfg->sync = lfs_sfd_sync;
  51. cfg->read_size = 256;
  52. cfg->prog_size = 256;
  53. cfg->block_size = 4096;
  54. cfg->block_count = drv->sector_count / 16;
  55. cfg->block_cycles = 200;
  56. cfg->cache_size = 256;
  57. cfg->lookahead_size = 16;
  58. cfg->read_buffer = luat_heap_malloc(256);
  59. cfg->prog_buffer = luat_heap_malloc(256);
  60. cfg->lookahead_buffer = luat_heap_malloc(256);
  61. cfg->name_max = 63;
  62. cfg->file_max = 0;
  63. cfg->attr_max = 0;
  64. cfg->context = drv;
  65. int ret = lfs_mount(mounted[i].fs, cfg);
  66. if (ret)
  67. LLOGW("lfs_mount ret %d", ret);
  68. lua_pushboolean(L, ret == 0 ? 1 : 0);
  69. return 1;
  70. }
  71. }
  72. return 0;
  73. }
  74. /**
  75. 格式化为lifftefs
  76. @api lfs2.mount(path)
  77. @string 挂载路径
  78. @usage
  79. local drv = sfd.init("spi", 0, 18)
  80. if drv then
  81. local fs = lfs2.mount("sfd", "/sfd", drv)
  82. if fs then
  83. lfs2.mkfs(fs)
  84. end
  85. end
  86. */
  87. static int l_lfs2_mkfs(lua_State *L) {
  88. const char* path = luaL_checkstring(L, 1);
  89. for (size_t i = 0; i < 2; i++) {
  90. if (mounted[i].userdata == NULL)
  91. continue;
  92. if (!strcmp(mounted[i].path, path)) {
  93. int ret = lfs_format(mounted[i].fs, mounted[i].cfg);
  94. LLOGD("lfs_format ret %d", ret);
  95. lua_pushboolean(L, ret == 0 ? 1 : 0);
  96. return 1;
  97. }
  98. }
  99. LLOGW("not path match, ignore mkfs");
  100. return 0;
  101. }
  102. #include "rotable.h"
  103. static const rotable_Reg reg_lfs2[] =
  104. {
  105. { "mount", l_lfs2_mount, 0}, //初始化,挂载
  106. // { "unmount", l_lfs2_unmount, 0}, // 取消挂载
  107. { "mkfs", l_lfs2_mkfs, 0}, // 格式化!!!
  108. // //{ "test", l_lfs2_test, 0},
  109. // { "getfree", l_lfs2_getfree, 0}, // 获取文件系统大小,剩余空间
  110. // { "debug", l_lfs2_debug_mode, 0}, // 调试模式,打印更多日志
  111. // { "lsdir", l_lfs2_lsdir, 0}, // 列举目录下的文件,名称,大小,日期,属性
  112. // { "mkdir", l_lfs2_mkdir, 0}, // 列举目录下的文件,名称,大小,日期,属性
  113. // { "stat", l_lfs2_stat, 0}, // 查询文件信息
  114. // { "open", l_lfs2_open, 0}, // 打开一个文件句柄
  115. // { "close", l_lfs2_close, 0}, // 关闭一个文件句柄
  116. // { "seek", l_lfs2_seek, 0}, // 移动句柄的当前位置
  117. // { "truncate", l_lfs2_truncate, 0}, // 缩减文件尺寸到当前seek位置
  118. // { "read", l_lfs2_read, 0}, // 读取数据
  119. // { "write", l_lfs2_write, 0}, // 写入数据
  120. // { "remove", l_lfs2_remove, 0}, // 删除文件,别名方法
  121. // { "unlink", l_lfs2_remove, 0}, // 删除文件
  122. // { "rename", l_lfs2_rename, 0}, // 文件改名
  123. // { "readfile", l_lfs2_readfile, 0}, // 读取文件的简易方法
  124. // { "playmp3", l_lfs2_playmp3, 0}, // 读取文件的简易方法
  125. { NULL, NULL, 0 }
  126. };
  127. int luaopen_lfs2( lua_State *L )
  128. {
  129. luat_newlib(L, reg_lfs2);
  130. #ifdef LUAT_USE_FS_VFS
  131. #endif
  132. return 1;
  133. }