mid_littlefs_flash.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include "main.h"
  2. #if USE_LFS_RECORD==1
  3. #define USE_MID_LFS_FLASH
  4. #endif
  5. #ifdef USE_MID_LFS_FLASH
  6. #include <stdio.h>
  7. #include "obj_spi_w25qxx.h"
  8. #include "obj_hal_w25qxx.h"
  9. #include "func_spi_w25qxx.h"
  10. #include "mid_littlefs_flash.h"
  11. // lfs for W25Q128 *********************************************************/
  12. //The W25Q128BV array is organized into 65,536 programmable pages of 256-bytes each.
  13. //128Mbit ==> 16Mbyte ==> 16*1024*1024 ==> 4*1024*block==>
  14. //-------------------------------------------------------------------------//
  15. lfs_t lfs;
  16. lfs_file_t lfs_file;
  17. // configuration of the filesystem is provided by this struct
  18. #pragma pack(4) /*4字节对齐*/
  19. typedef struct lfs_sys_bufffer
  20. {
  21. uint32_t read[LFS_CACH_SIZE/4];
  22. uint32_t prog[LFS_CACH_SIZE/4];
  23. uint32_t lookahead[LFS_CACH_SIZE/4/8];
  24. }lfs_sys_buff;
  25. lfs_sys_buff lfs_sys_buf = {0};
  26. /* lfs for STM32Flash ********************************************************/
  27. lfs_t lfs_flash;
  28. lfs_file_t file_flash;
  29. #pragma pack()/*还原默认对齐*/
  30. /* Private function prototypes -----------------------------------------------*/
  31. int flash_block_read(const struct lfs_config *c, lfs_block_t block,
  32. lfs_off_t off, void *buffer, lfs_size_t size);
  33. int flash_block_prog(const struct lfs_config *c, lfs_block_t block,
  34. lfs_off_t off, const void *buffer, lfs_size_t size);
  35. int flash_block_erase(const struct lfs_config *c, lfs_block_t block);
  36. int flash_block_sync(const struct lfs_config *c);
  37. int lfs_file_open_user(lfs_t *lfs, lfs_file_t *file, const char *path, int flags) ;
  38. // configuration of the filesystem is provided by this struct
  39. const struct lfs_config cfg_flash = {
  40. // block device operations
  41. .read = flash_block_read,
  42. .prog = flash_block_prog,
  43. .erase = flash_block_erase,
  44. .sync = flash_block_sync,
  45. // block device configuration
  46. .read_size = LFS_SECT_SIZE,
  47. .prog_size = LFS_SECT_SIZE,
  48. .block_size = LFS_BLOCK_SIZE,
  49. .block_count = LFS_BLOCK_CNT,
  50. .cache_size = LFS_CACH_SIZE,
  51. .lookahead_size = LFS_CACH_SIZE/8,
  52. .block_cycles = 500,
  53. .read_buffer = lfs_sys_buf.read,
  54. .prog_buffer = lfs_sys_buf.prog,
  55. .lookahead_buffer = lfs_sys_buf.lookahead,
  56. };
  57. void mid_lfs_flash_init(void)
  58. {
  59. int err = -1;
  60. int erase_flag = 0;
  61. err = lfs_mount(&lfs_flash, &cfg_flash);
  62. while( err != LFS_ERR_OK )
  63. {
  64. if(erase_flag)
  65. {
  66. W25QXX_Erase_Chip();
  67. lfs_format(&lfs_flash, &cfg_flash);
  68. err = lfs_mount(&lfs_flash, &cfg_flash);
  69. erase_flag = 0;
  70. }
  71. osDelay(1000);
  72. }
  73. return;
  74. }
  75. int mid_lfs_flash_get_free(void)
  76. {
  77. return 0xffffff;
  78. }
  79. int mid_lfs_flash_bootcnt(void)
  80. {
  81. //while(1)
  82. {
  83. uint32_t boot_count = 0;
  84. lfs_file_open_static(&lfs_flash, &file_flash, "boot_count", LFS_O_RDWR | LFS_O_CREAT);
  85. lfs_file_read(&lfs_flash, &file_flash, &boot_count, sizeof(boot_count));
  86. // update boot count
  87. boot_count += 1;
  88. lfs_file_rewind(&lfs_flash, &file_flash); // seek the file to begin
  89. lfs_file_write(&lfs_flash, &file_flash, &boot_count, sizeof(boot_count));
  90. // remember the storage is not updated until the file is closed successfully
  91. lfs_file_close(&lfs_flash, &file_flash);
  92. // // release any resources we were using
  93. // print the boot count
  94. printf("boot_count: %d\n", boot_count);
  95. };
  96. return 0;
  97. }
  98. int mid_lfs_flash_boot_step(void)
  99. {
  100. //while(1)
  101. {
  102. uint32_t boot_count = 0;
  103. lfs_file_open_static(&lfs_flash, &file_flash, "boot_step", LFS_O_RDWR | LFS_O_CREAT);
  104. lfs_file_read(&lfs_flash, &file_flash, &boot_count, sizeof(boot_count));
  105. // update boot count
  106. boot_count += 1;
  107. lfs_file_rewind(&lfs_flash, &file_flash); // seek the file to begin
  108. lfs_file_write(&lfs_flash, &file_flash, &boot_count, sizeof(boot_count));
  109. // remember the storage is not updated until the file is closed successfully
  110. lfs_file_close(&lfs_flash, &file_flash);
  111. // // release any resources we were using
  112. // print the boot count
  113. printf("boot_count: %d\n", boot_count);
  114. };
  115. return 0;
  116. }
  117. int flash_block_read(const struct lfs_config *c, lfs_block_t block,
  118. lfs_off_t off, void *buffer, lfs_size_t size)
  119. {
  120. W25QXX_Read((uint8_t*)buffer, (uint32_t)((block * LFS_BLOCK_SIZE) + off), (uint16_t)size);
  121. return 0;
  122. }
  123. int flash_block_prog(const struct lfs_config *c, lfs_block_t block,
  124. lfs_off_t off, const void *buffer, lfs_size_t size)
  125. {
  126. //W25QXX_Write((uint8_t*)buffer, block * LFS_BLOCK_SIZE + off, size);
  127. W25QXX_Write_NoCheck((uint8_t*)buffer, block * LFS_BLOCK_SIZE + off, size);
  128. return 0;
  129. }
  130. int flash_block_erase(const struct lfs_config *c, lfs_block_t block)
  131. {
  132. W25QXX_Erase_Sector(block);
  133. return 0;
  134. }
  135. int flash_block_sync(const struct lfs_config *c)
  136. {
  137. return 0;
  138. }
  139. #if 0
  140. #pragma pack(4) /*4字节对齐*/
  141. //uint32_t file_buffer[LFS_CACH_SIZE/4];
  142. #pragma pack()/*还原默认对齐*/
  143. #else
  144. static uint8_t file_buffer[LFS_CACH_SIZE] __attribute__((aligned(4)));
  145. #endif
  146. struct lfs_file_config file_cfg = {.attr_count = 0, .attrs = 0, .buffer = file_buffer};
  147. int lfs_file_open_static(lfs_t *lfs, lfs_file_t *file, const char *path, int flags)
  148. {
  149. memset(&file_cfg,0x00,sizeof(struct lfs_file_config));
  150. file_cfg.buffer = file_buffer;
  151. memset(file_buffer,0x00,sizeof(file_buffer));
  152. memset(file,0x00,sizeof(lfs_file_t));
  153. return lfs_file_opencfg(lfs, file, path, flags, &file_cfg);
  154. }
  155. #endif //---------------USE_MID_LFS_FLASH---------------------//
  156. //------------------------the end of file-------------------------//