lfs_port.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include "lfs_port.h"
  5. #include "lfs_util.h"
  6. #include "luat_base.h"
  7. #define LUAT_LOG_TAG "luat.fs.port"
  8. #include "luat_log.h"
  9. #include "wm_include.h"
  10. #include "lfs.h"
  11. #include "wm_flash_map.h"
  12. #include "wm_internal_flash.h"
  13. #include "luat_timer.h"
  14. extern uint32_t lfs_addr;
  15. extern uint32_t lfs_size_kb;
  16. #ifdef FLASH_FS_REGION_SIZE
  17. #undef FLASH_FS_REGION_SIZE
  18. #endif
  19. #ifdef LFS_START_ADDR
  20. #undef LFS_START_ADDR
  21. #endif
  22. #define FLASH_FS_REGION_SIZE lfs_size_kb
  23. #define LFS_START_ADDR lfs_addr
  24. /***************************************************
  25. *************** MACRO ******************
  26. ***************************************************/
  27. #define LFS_BLOCK_DEVICE_READ_SIZE (256)
  28. #define LFS_BLOCK_DEVICE_PROG_SIZE (256)
  29. #define LFS_BLOCK_DEVICE_CACHE_SIZE (256)
  30. #define LFS_BLOCK_DEVICE_ERASE_SIZE (4096) // one sector 4KB
  31. #define LFS_BLOCK_DEVICE_TOTOAL_SIZE (FLASH_FS_REGION_SIZE * 1024)
  32. #define LFS_BLOCK_DEVICE_LOOK_AHEAD (16)
  33. /***************************************************
  34. ******* FUNCTION FORWARD DECLARTION ********
  35. ***************************************************/
  36. // Read a block
  37. static int block_device_read(const struct lfs_config *cfg, lfs_block_t block,
  38. lfs_off_t off, void *buffer, lfs_size_t size);
  39. // Program a block
  40. //
  41. // The block must have previously been erased.
  42. static int block_device_prog(const struct lfs_config *cfg, lfs_block_t block,
  43. lfs_off_t off, const void *buffer, lfs_size_t size);
  44. // Erase a block
  45. //
  46. // A block must be erased before being programmed. The
  47. // state of an erased block is undefined.
  48. static int block_device_erase(const struct lfs_config *cfg, lfs_block_t block);
  49. // Sync the block device
  50. static int block_device_sync(const struct lfs_config *cfg);
  51. // utility functions for traversals
  52. static int lfs_statfs_count(void *p, lfs_block_t b);
  53. /***************************************************
  54. *************** GLOBAL VARIABLE *****************
  55. ***************************************************/
  56. #ifdef LFS_THREAD_SAFE_MUTEX
  57. static osMutexId_t lfs_mutex;
  58. #endif
  59. static char lfs_read_buf[256];
  60. static char lfs_prog_buf[256];
  61. // static __ALIGNED(4) char lfs_lookahead_buf[LFS_BLOCK_DEVICE_LOOK_AHEAD];
  62. static char __attribute__((aligned(4))) lfs_lookahead_buf[LFS_BLOCK_DEVICE_LOOK_AHEAD];
  63. // configuration of the filesystem is provided by this struct
  64. struct lfs_config lfs_cfg =
  65. {
  66. .context = NULL,
  67. // block device operations
  68. .read = block_device_read,
  69. .prog = block_device_prog,
  70. .erase = block_device_erase,
  71. .sync = block_device_sync,
  72. // block device configuration
  73. .read_size = LFS_BLOCK_DEVICE_READ_SIZE,
  74. .prog_size = LFS_BLOCK_DEVICE_PROG_SIZE,
  75. .block_size = LFS_BLOCK_DEVICE_ERASE_SIZE,
  76. //.block_count = LFS_BLOCK_DEVICE_TOTOAL_SIZE / LFS_BLOCK_DEVICE_ERASE_SIZE,
  77. .block_cycles = 200,
  78. .cache_size = LFS_BLOCK_DEVICE_CACHE_SIZE,
  79. .lookahead_size = LFS_BLOCK_DEVICE_LOOK_AHEAD,
  80. .read_buffer = lfs_read_buf,
  81. .prog_buffer = lfs_prog_buf,
  82. .lookahead_buffer = lfs_lookahead_buf,
  83. .name_max = 63,
  84. .file_max = 0,
  85. .attr_max = 0
  86. };
  87. lfs_t lfs;
  88. /***************************************************
  89. ******* INTERANL FUNCTION ********
  90. ***************************************************/
  91. static int block_device_read(const struct lfs_config *cfg, lfs_block_t block,
  92. lfs_off_t off, void *buffer, lfs_size_t size)
  93. {
  94. int ret;
  95. //LLOGD("block_device_read ,block = %d, off = %d, size = %d",block, off, size);
  96. ret = tls_fls_read(block * 4096 + off + LFS_START_ADDR, (u8 *)buffer, size);
  97. //LLOGD("block_device_read return val : %d",ret);
  98. if (ret != TLS_FLS_STATUS_OK)
  99. {
  100. return -1;
  101. }
  102. return 0;
  103. }
  104. static int block_device_prog(const struct lfs_config *cfg, lfs_block_t block,
  105. lfs_off_t off, const void *buffer, lfs_size_t size)
  106. {
  107. int ret;
  108. //LLOGD("block_device_prog ,block = %d, off = %d, size = %d",block, off, size);
  109. ret = tls_fls_write(block * 4096 + off + LFS_START_ADDR, (u8 *)buffer, size);
  110. //LLOGD("block_device_prog return val : %d",ret);
  111. if (ret != TLS_FLS_STATUS_OK)
  112. {
  113. return -1;
  114. }
  115. return 0;
  116. }
  117. static int block_device_erase(const struct lfs_config *cfg, lfs_block_t block)
  118. {
  119. int ret;
  120. ret = tls_fls_erase((block * 4096 + LFS_START_ADDR) / INSIDE_FLS_SECTOR_SIZE);
  121. if (ret != TLS_FLS_STATUS_OK)
  122. {
  123. return -1;
  124. }
  125. return 0;
  126. }
  127. static int block_device_sync(const struct lfs_config *cfg)
  128. {
  129. return 0;
  130. }
  131. static int lfs_statfs_count(void *p, lfs_block_t b)
  132. {
  133. *(lfs_size_t *)p += 1;
  134. return 0;
  135. }
  136. // Initialize
  137. int LFS_Init(void)
  138. {
  139. //luat_timer_mdelay(1000);
  140. //LLOGD("lfs addr %08X", LFS_START_ADDR);
  141. // LLOGD("lfs addr2 %08X", lfs_addr);
  142. // LLOGD("lfs addr3 %p", &lfs_addr);
  143. // LLOGD("lfs block_count %d", LFS_BLOCK_DEVICE_TOTOAL_SIZE / LFS_BLOCK_DEVICE_ERASE_SIZE);
  144. //printf("------------LFS_Init------------\r\n");
  145. // mount the filesystem
  146. lfs_cfg.block_count = LFS_BLOCK_DEVICE_TOTOAL_SIZE / LFS_BLOCK_DEVICE_ERASE_SIZE;
  147. int err = lfs_mount(&lfs, &lfs_cfg);
  148. //printf("lfs_mount %d\r\n",err);
  149. // reformat if we can't mount the filesystem
  150. // this should only happen on the first boot
  151. if (err)
  152. {
  153. luat_timer_mdelay(1000);
  154. LLOGI("lfs mount fail ret=%d , exec format", err);
  155. err = lfs_format(&lfs, &lfs_cfg);
  156. //printf("lfs_format %d\r\n",err);
  157. if(err) {
  158. LLOGW("lfs format fail ret=%d", err);
  159. return err;
  160. }
  161. err = lfs_mount(&lfs, &lfs_cfg);
  162. //printf("lfs_mount %d\r\n",err);
  163. if(err)
  164. return err;
  165. }
  166. return 0;
  167. }