luat_fs_air105.c 6.4 KB

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