lfs_port.c 5.6 KB

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