luat_fs_air105.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. #include "luat_ota.h"
  25. struct lfs_config mcu_flash_lfs_cfg;
  26. struct lfs LFS;
  27. #define LFS_BLOCK_DEVICE_READ_SIZE (256)
  28. #define LFS_BLOCK_DEVICE_PROG_SIZE (__FLASH_PAGE_SIZE__)
  29. #define LFS_BLOCK_DEVICE_LOOK_AHEAD (16)
  30. #define LFS_BLOCK_DEVICE_CACHE_SIZE (256)
  31. #define SCRIPT_LUADB_START_ADDR (__FLASH_BASE_ADDR__ + __CORE_FLASH_BLOCK_NUM__ * __FLASH_BLOCK_SIZE__)
  32. // 根据头文件的定义, 算出脚本区和文件系统区的绝对地址
  33. const size_t script_luadb_start_addr = SCRIPT_LUADB_START_ADDR;
  34. const size_t lfs_fs_start_addr = SCRIPT_LUADB_START_ADDR + __SCRIPT_FLASH_BLOCK_NUM__ * __FLASH_BLOCK_SIZE__ ;
  35. static int block_device_read(const struct lfs_config *cfg, lfs_block_t block,
  36. lfs_off_t off, void *buffer, lfs_size_t size)
  37. {
  38. uint32_t start_address = block * __FLASH_SECTOR_SIZE__ + off + lfs_fs_start_addr;
  39. memcpy(buffer, start_address, size);
  40. // DBG("%x, %u", start_address, size);
  41. // DBG_HexPrintf(buffer, 16);
  42. return LFS_ERR_OK;
  43. }
  44. static int block_device_prog(const struct lfs_config *cfg, lfs_block_t block,
  45. lfs_off_t off, const void *buffer, lfs_size_t size)
  46. {
  47. uint32_t start_address = block * __FLASH_SECTOR_SIZE__ + off + lfs_fs_start_addr;
  48. // DBG("%x", start_address);
  49. if (Flash_ProgramData(start_address, buffer, size, 0))
  50. {
  51. return LFS_ERR_IO;
  52. }
  53. return LFS_ERR_OK;
  54. }
  55. static int block_device_erase(const struct lfs_config *cfg, lfs_block_t block)
  56. {
  57. uint32_t start_address = block * __FLASH_SECTOR_SIZE__ + lfs_fs_start_addr;
  58. // DBG("%x", start_address);
  59. if (Flash_EraseSector(start_address, 0))
  60. {
  61. return LFS_ERR_IO;
  62. }
  63. return LFS_ERR_OK;
  64. }
  65. static int block_device_sync(const struct lfs_config *cfg)
  66. {
  67. //DBG_Trace("block_device_sync");
  68. return 0;
  69. }
  70. #define LFS_BLOCK_DEVICE_READ_SIZE (256)
  71. #define LFS_BLOCK_DEVICE_PROG_SIZE (__FLASH_PAGE_SIZE__)
  72. #define LFS_BLOCK_DEVICE_LOOK_AHEAD (16)
  73. #define LFS_BLOCK_DEVICE_CACHE_SIZE (256)
  74. #ifdef __BUILD_APP__
  75. //__attribute__ ((aligned (8))) static char lfs_cache_buff[LFS_BLOCK_DEVICE_CACHE_SIZE];
  76. __attribute__ ((aligned (8))) static char lfs_read_buff[LFS_BLOCK_DEVICE_READ_SIZE];
  77. __attribute__ ((aligned (8))) static char lfs_prog_buff[LFS_BLOCK_DEVICE_PROG_SIZE];
  78. __attribute__ ((aligned (8))) static char lfs_lookahead_buff[16];
  79. #endif
  80. void FileSystem_Init(void)
  81. {
  82. struct lfs_config *config = &mcu_flash_lfs_cfg;
  83. //DBG_INFO("ID:%02x,%02x,%02x,Size:%uKB", Ctrl->FlashID[0], Ctrl->FlashID[1], Ctrl->FlashID[2], Ctrl->Size);
  84. config->context = NULL;
  85. config->cache_size = LFS_BLOCK_DEVICE_READ_SIZE;
  86. config->prog_size = __FLASH_PAGE_SIZE__;
  87. config->block_size = __FLASH_SECTOR_SIZE__;
  88. config->read_size = LFS_BLOCK_DEVICE_READ_SIZE;
  89. config->block_cycles = 200;
  90. config->lookahead_size = LFS_BLOCK_DEVICE_LOOK_AHEAD;
  91. //config->block_count = (Ctrl->Size / 4) - __CORE_FLASH_SECTOR_NUM__ - __SCRIPT_FLASH_SECTOR_NUM__;
  92. config->block_count = 512 / 4 ;
  93. config->name_max = 63;
  94. config->read = block_device_read;
  95. config->prog = block_device_prog;
  96. config->erase = block_device_erase;
  97. config->sync = block_device_sync;
  98. //config->buffer = (void*)lfs_cache_buff;
  99. config->read_buffer = (void*)lfs_read_buff;
  100. config->prog_buffer = (void*)lfs_prog_buff;
  101. config->lookahead_buffer = (void*)lfs_lookahead_buff;
  102. /*
  103. * 正式加入luatos代码可以开启
  104. */
  105. int re = lfs_mount(&LFS, &mcu_flash_lfs_cfg);
  106. if (re) {
  107. DBG_INFO("lfs, mount fail=%d", re);
  108. re = lfs_format(&LFS, &mcu_flash_lfs_cfg);
  109. if (re) {
  110. DBG_INFO("lfs, format fail=%d", re);
  111. }
  112. else {
  113. lfs_mount(&LFS, &mcu_flash_lfs_cfg);
  114. }
  115. }
  116. }
  117. extern const struct luat_vfs_filesystem vfs_fs_lfs2;
  118. extern const struct luat_vfs_filesystem vfs_fs_luadb;
  119. #ifdef LUAT_USE_VFS_INLINE_LIB
  120. //extern const char luadb_inline[];
  121. // extern const char luadb_inline_sys[];
  122. #endif
  123. int luat_fs_init(void) {
  124. FileSystem_Init();
  125. #ifdef LUAT_USE_FS_VFS
  126. // vfs进行必要的初始化
  127. luat_vfs_init(NULL);
  128. // 注册vfs for posix 实现
  129. luat_vfs_reg(&vfs_fs_lfs2);
  130. luat_vfs_reg(&vfs_fs_luadb);
  131. // 指向3M + 512k的littefs 文件区
  132. luat_fs_conf_t conf = {
  133. .busname = (char*)&LFS,
  134. .type = "lfs2",
  135. .filesystem = "lfs2",
  136. .mount_point = "",
  137. };
  138. luat_fs_mount(&conf);
  139. #ifdef LUAT_USE_OTA
  140. //OTA检测升级
  141. luat_ota(script_luadb_start_addr);
  142. #endif
  143. // 指向3M 的脚本区, luadb格式
  144. luat_fs_conf_t conf2 = {
  145. .busname = (char*)script_luadb_start_addr,
  146. .type = "luadb",
  147. .filesystem = "luadb",
  148. .mount_point = "/luadb/",
  149. };
  150. luat_fs_mount(&conf2);
  151. // #endif
  152. #endif
  153. #ifdef LUAT_USE_LVGL
  154. luat_lv_fs_init();
  155. //lv_bmp_init();
  156. lv_png_init();
  157. lv_split_jpeg_init();
  158. #endif
  159. return 0;
  160. }
  161. #ifdef LUAT_USE_OTA
  162. int luat_flash_write(uint32_t addr, uint8_t * buf, uint32_t len){
  163. // DBG("addr %x %d", addr,len);
  164. Flash_EraseSector(addr, 0);
  165. return FLASH_ProgramPage( addr, len, buf);
  166. }
  167. #endif