luat_fs_air105.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #include "app_interface.h"
  22. #include "luat_base.h"
  23. #include "luat_fs.h"
  24. struct lfs_config mcu_flash_lfs_cfg;
  25. struct lfs LFS;
  26. #define LFS_BLOCK_DEVICE_READ_SIZE (256)
  27. #define LFS_BLOCK_DEVICE_PROG_SIZE (__FLASH_PAGE_SIZE__)
  28. #define LFS_BLOCK_DEVICE_LOOK_AHEAD (16)
  29. #define LFS_BLOCK_DEVICE_CACHE_SIZE (256)
  30. #define SCRIPT_LUADB_START_ADDR (__FLASH_BASE_ADDR__ + __CORE_FLASH_SECTOR_NUM__ * __FLASH_SECTOR_SIZE__)
  31. // 根据头文件的定义, 算出脚本区和文件系统区的绝对地址
  32. const size_t script_luadb_start_addr = SCRIPT_LUADB_START_ADDR;
  33. const size_t lfs_fs_start_addr = SCRIPT_LUADB_START_ADDR + __SCRIPT_FLASH_SECTOR_NUM__ * __FLASH_SECTOR_SIZE__ ;
  34. static int block_device_read(const struct lfs_config *cfg, lfs_block_t block,
  35. lfs_off_t off, void *buffer, lfs_size_t size)
  36. {
  37. uint32_t start_address = block * __FLASH_SECTOR_SIZE__ + off + lfs_fs_start_addr;
  38. memcpy(buffer, start_address, size);
  39. // DBG("%x, %u", start_address, size);
  40. // DBG_HexPrintf(buffer, 16);
  41. return LFS_ERR_OK;
  42. }
  43. static int block_device_prog(const struct lfs_config *cfg, lfs_block_t block,
  44. lfs_off_t off, const void *buffer, lfs_size_t size)
  45. {
  46. uint32_t start_address = block * __FLASH_SECTOR_SIZE__ + off + lfs_fs_start_addr;
  47. // DBG("%x", start_address);
  48. if (Flash_ProgramData(start_address, buffer, size, 0))
  49. {
  50. return LFS_ERR_IO;
  51. }
  52. return LFS_ERR_OK;
  53. }
  54. static int block_device_erase(const struct lfs_config *cfg, lfs_block_t block)
  55. {
  56. uint32_t start_address = block * __FLASH_SECTOR_SIZE__ + lfs_fs_start_addr;
  57. // DBG("%x", start_address);
  58. if (Flash_EraseSector(start_address, 0))
  59. {
  60. return LFS_ERR_IO;
  61. }
  62. return LFS_ERR_OK;
  63. }
  64. static int block_device_sync(const struct lfs_config *cfg)
  65. {
  66. //DBG_Trace("block_device_sync");
  67. return 0;
  68. }
  69. #define LFS_BLOCK_DEVICE_READ_SIZE (256)
  70. #define LFS_BLOCK_DEVICE_PROG_SIZE (__FLASH_PAGE_SIZE__)
  71. #define LFS_BLOCK_DEVICE_LOOK_AHEAD (16)
  72. #define LFS_BLOCK_DEVICE_CACHE_SIZE (256)
  73. #ifdef __BUILD_APP__
  74. //__attribute__ ((aligned (8))) static char lfs_cache_buff[LFS_BLOCK_DEVICE_CACHE_SIZE];
  75. __attribute__ ((aligned (8))) static char lfs_read_buff[LFS_BLOCK_DEVICE_READ_SIZE];
  76. __attribute__ ((aligned (8))) static char lfs_prog_buff[LFS_BLOCK_DEVICE_PROG_SIZE];
  77. __attribute__ ((aligned (8))) static char lfs_lookahead_buff[16];
  78. #endif
  79. void FileSystem_Init(void)
  80. {
  81. struct lfs_config *config = &mcu_flash_lfs_cfg;
  82. //DBG_INFO("ID:%02x,%02x,%02x,Size:%uKB", Ctrl->FlashID[0], Ctrl->FlashID[1], Ctrl->FlashID[2], Ctrl->Size);
  83. config->context = NULL;
  84. config->cache_size = LFS_BLOCK_DEVICE_READ_SIZE;
  85. config->prog_size = __FLASH_PAGE_SIZE__;
  86. config->block_size = __FLASH_SECTOR_SIZE__;
  87. config->read_size = LFS_BLOCK_DEVICE_READ_SIZE;
  88. config->block_cycles = 200;
  89. config->lookahead_size = LFS_BLOCK_DEVICE_LOOK_AHEAD;
  90. //config->block_count = (Ctrl->Size / 4) - __CORE_FLASH_SECTOR_NUM__ - __SCRIPT_FLASH_SECTOR_NUM__;
  91. config->block_count = 512 / 4 ;
  92. config->name_max = 63;
  93. config->read = block_device_read;
  94. config->prog = block_device_prog;
  95. config->erase = block_device_erase;
  96. config->sync = block_device_sync;
  97. //config->buffer = (void*)lfs_cache_buff;
  98. config->read_buffer = (void*)lfs_read_buff;
  99. config->prog_buffer = (void*)lfs_prog_buff;
  100. config->lookahead_buffer = (void*)lfs_lookahead_buff;
  101. /*
  102. * 正式加入luatos代码可以开启
  103. */
  104. int re = lfs_mount(&LFS, &mcu_flash_lfs_cfg);
  105. if (re) {
  106. DBG_INFO("lfs, mount fail=%d", re);
  107. re = lfs_format(&LFS, &mcu_flash_lfs_cfg);
  108. if (re) {
  109. DBG_INFO("lfs, format fail=%d", re);
  110. }
  111. else {
  112. lfs_mount(&LFS, &mcu_flash_lfs_cfg);
  113. }
  114. }
  115. }
  116. extern const struct luat_vfs_filesystem vfs_fs_lfs2;
  117. extern const struct luat_vfs_filesystem vfs_fs_luadb;
  118. #ifdef LUAT_USE_VFS_INLINE_LIB
  119. //extern const char luadb_inline[];
  120. // extern const char luadb_inline_sys[];
  121. #endif
  122. int luat_fs_init(void) {
  123. FileSystem_Init();
  124. #ifdef LUAT_USE_FS_VFS
  125. // vfs进行必要的初始化
  126. luat_vfs_init(NULL);
  127. // 注册vfs for posix 实现
  128. luat_vfs_reg(&vfs_fs_lfs2);
  129. luat_vfs_reg(&vfs_fs_luadb);
  130. // 指向3M + 512k的littefs 文件区
  131. luat_fs_conf_t conf = {
  132. .busname = (char*)&LFS,
  133. .type = "lfs2",
  134. .filesystem = "lfs2",
  135. .mount_point = "",
  136. };
  137. luat_fs_mount(&conf);
  138. // 指向3M 的脚本区, luadb格式
  139. luat_fs_conf_t conf2 = {
  140. .busname = (char*)script_luadb_start_addr,
  141. .type = "luadb",
  142. .filesystem = "luadb",
  143. .mount_point = "/luadb/",
  144. };
  145. luat_fs_mount(&conf2);
  146. // #endif
  147. #endif
  148. #ifdef LUAT_USE_LVGL
  149. luat_lv_fs_init();
  150. //lv_bmp_init();
  151. lv_png_init();
  152. lv_split_jpeg_init();
  153. #endif
  154. return 0;
  155. }