luat_fs_mini.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include "luat_base.h"
  2. #include "luat_fs.h"
  3. #include "luat_malloc.h"
  4. #define LUAT_LOG_TAG "fs"
  5. #include "luat_log.h"
  6. #include "dirent.h"
  7. extern const struct luat_vfs_filesystem vfs_fs_posix;
  8. extern const struct luat_vfs_filesystem vfs_fs_luadb;
  9. extern const struct luat_vfs_filesystem vfs_fs_ram;
  10. extern const struct luat_vfs_filesystem vfs_fs_lfs2;
  11. extern int cmdline_argc;
  12. extern char **cmdline_argv;
  13. extern char *luadb_ptr;
  14. // 从命令行参数构建luadb
  15. void *build_luadb_from_cmd(void);
  16. static void lvgl_fs_init(void);
  17. static void pc_lfs2_mount(void);
  18. int luat_fs_init(void)
  19. {
  20. // vfs进行必要的初始化
  21. luat_vfs_init(NULL);
  22. // 注册vfs for posix 实现
  23. luat_vfs_reg(&vfs_fs_posix);
  24. luat_vfs_reg(&vfs_fs_luadb);
  25. luat_vfs_reg(&vfs_fs_ram);
  26. luat_vfs_reg(&vfs_fs_lfs2);
  27. luat_fs_conf_t conf = {
  28. .busname = "",
  29. .type = "posix",
  30. .filesystem = "posix",
  31. .mount_point = "",
  32. };
  33. luat_fs_mount(&conf);
  34. // 挂载虚拟的/ram
  35. luat_fs_conf_t conf_ram = {
  36. .busname = "",
  37. .type = "ram",
  38. .filesystem = "ram",
  39. .mount_point = "/ram/",
  40. };
  41. luat_fs_mount(&conf_ram);
  42. // 挂载虚拟的/luadb
  43. void *ptr = luadb_ptr;
  44. if (ptr != NULL)
  45. {
  46. luat_fs_conf_t conf2 = {
  47. .busname = ptr,
  48. .type = "luadb",
  49. .filesystem = "luadb",
  50. .mount_point = "/luadb/",
  51. };
  52. luat_fs_mount(&conf2);
  53. }
  54. // 挂载/lfs2作为测试lfs文件系统的目录
  55. pc_lfs2_mount();
  56. #ifdef LUAT_USE_LVGL
  57. lvgl_fs_init();
  58. #endif
  59. return 0;
  60. }
  61. #ifdef LUAT_USE_LVGL
  62. #undef LUAT_LOG_TAG
  63. #include "luat_lvgl.h"
  64. static void lvgl_fs_init(void) {
  65. luat_lv_fs_init();
  66. #ifdef LUAT_USE_LVGL_BMP
  67. lv_bmp_init();
  68. #endif
  69. #ifdef LUAT_USE_LVGL_PNG
  70. lv_png_init();
  71. #endif
  72. #ifdef LUAT_USE_LVGL_JPG
  73. lv_split_jpeg_init();
  74. #endif
  75. }
  76. #endif
  77. #include "lfs.h"
  78. #define LUAT_LFS_MEM_SIZE (128 * 1024)
  79. #define LUAT_LFS_MEM_BLOCK_SIZE ( 4 * 1024)
  80. #define LUAT_LFS_MEM_BLOCK_COUNT (LUAT_LFS_MEM_SIZE / LUAT_LFS_MEM_BLOCK_SIZE)
  81. static char lfs_mem_buff[LUAT_LFS_MEM_SIZE];
  82. static int mem_read(const struct lfs_config *c, lfs_block_t block,
  83. lfs_off_t off, void *buffer, lfs_size_t size) {
  84. (void)c;
  85. // LLOGD("lfs内存读取 %d %d", block * LUAT_LFS_MEM_BLOCK_SIZE + off, size);
  86. memcpy(buffer, lfs_mem_buff + (block * LUAT_LFS_MEM_BLOCK_SIZE + off), size);
  87. return 0;
  88. }
  89. static int mem_prog(const struct lfs_config *c, lfs_block_t block,
  90. lfs_off_t off, const void *buffer, lfs_size_t size) {
  91. (void)c;
  92. // LLOGD("lfs内存写入 %d %d", block * LUAT_LFS_MEM_BLOCK_SIZE + off, size);
  93. memcpy(lfs_mem_buff + (block * LUAT_LFS_MEM_BLOCK_SIZE + off), buffer, size);
  94. return 0;
  95. }
  96. static int mem_erase(const struct lfs_config *c, lfs_block_t block) {
  97. (void)c;
  98. // LLOGD("lfs内存块清除 %d", block * LUAT_LFS_MEM_BLOCK_SIZE);
  99. memset(lfs_mem_buff + (block * LUAT_LFS_MEM_BLOCK_SIZE), 0xFF, LUAT_LFS_MEM_BLOCK_SIZE);
  100. return 0;
  101. }
  102. static int mem_sync(const struct lfs_config *c) {
  103. (void)c;
  104. return 0;
  105. }
  106. #define LFS_BLOCK_DEVICE_READ_SIZE (256)
  107. #define LFS_BLOCK_DEVICE_PROG_SIZE (256)
  108. #define LFS_BLOCK_DEVICE_CACHE_SIZE (256)
  109. #define LFS_BLOCK_DEVICE_ERASE_SIZE (4096) // one sector 4KB
  110. #define LFS_BLOCK_DEVICE_TOTOAL_SIZE (FLASH_FS_REGION_SIZE * 1024)
  111. #define LFS_BLOCK_DEVICE_LOOK_AHEAD (16)
  112. static char lfs_read_buf[256];
  113. static char lfs_prog_buf[256];
  114. static char lfs_lookahead_buf[LFS_BLOCK_DEVICE_LOOK_AHEAD];
  115. static lfs_t lfs_mem;
  116. const struct lfs_config cfg = {
  117. .read = mem_read,
  118. .prog = mem_prog,
  119. .erase = mem_erase,
  120. .sync = mem_sync,
  121. // block device configuration
  122. .read_size = LFS_BLOCK_DEVICE_READ_SIZE,
  123. .prog_size = LFS_BLOCK_DEVICE_PROG_SIZE,
  124. .block_size = LFS_BLOCK_DEVICE_ERASE_SIZE,
  125. .block_count = LUAT_LFS_MEM_BLOCK_COUNT,
  126. .block_cycles = 200,
  127. .cache_size = LFS_BLOCK_DEVICE_CACHE_SIZE,
  128. .lookahead_size = LFS_BLOCK_DEVICE_LOOK_AHEAD,
  129. .read_buffer = lfs_read_buf,
  130. .prog_buffer = lfs_prog_buf,
  131. .lookahead_buffer = lfs_lookahead_buf,
  132. .name_max = 63,
  133. .file_max = 0,
  134. .attr_max = 0
  135. };
  136. static void pc_lfs2_mount(void) {
  137. // LLOGD("执行/lfs2挂载");
  138. int ret = lfs_mount(&lfs_mem, &cfg);
  139. if (ret) {
  140. // LLOGD("格式化");
  141. lfs_format(&lfs_mem, &cfg);
  142. ret = lfs_mount(&lfs_mem, &cfg);
  143. if (ret) {
  144. LLOGE("挂载/lfs2/失败!!! %d", ret);
  145. return;
  146. }
  147. }
  148. luat_fs_conf_t conf = {
  149. .busname = (char*)&lfs_mem,
  150. .type = "lfs2",
  151. .filesystem = "lfs2",
  152. .mount_point = "/lfs2/",
  153. };
  154. luat_fs_mount(&conf);
  155. // LLOGD("挂载/lfs2/ 成功");
  156. }