mid_littlefs_ram.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #ifdef USE_MID_LFS_RAM
  2. #include <stdio.h>
  3. #include "lfs.h"
  4. #include "mid_littlefs_ram.h"
  5. #include "cmsis_os.h"
  6. #define LFS_SECT_SIZE 64
  7. //#define LFS_BLOCK_SIZE (LFS_SECT_SIZE*4)
  8. #define LFS_BLOCK_SIZE 64
  9. #define LFS_BLOCK_CNT 32
  10. /* lfs for W25Q128 *********************************************************/
  11. lfs_t lfs;
  12. lfs_file_t file;
  13. // configuration of the filesystem is provided by this struct
  14. typedef struct lfs_sys_bufffer
  15. {
  16. uint8_t read[LFS_BLOCK_SIZE];
  17. uint8_t prog[LFS_BLOCK_SIZE];
  18. uint32_t lookahead[LFS_BLOCK_CNT/(4*8)+1];
  19. }lfs_sys_buff;
  20. lfs_sys_buff lfs_sys_buf = {0};
  21. uint8_t ram_block[LFS_BLOCK_CNT][LFS_SECT_SIZE] = {0};
  22. /* lfs for STM32Flash ********************************************************/
  23. lfs_t lfs_Stm32_ram;
  24. lfs_file_t file_ram;
  25. // configuration of the filesystem is provided by this struct
  26. const struct lfs_config cfg_Stm32Flash = {
  27. // block device operations
  28. .read = ram_block_read,
  29. .prog = ram_block_prog,
  30. .erase = ram_block_erase,
  31. .sync = ram_block_sync,
  32. // block device configuration
  33. .read_size = LFS_SECT_SIZE,
  34. .prog_size = LFS_SECT_SIZE,
  35. .block_size = LFS_BLOCK_SIZE,
  36. .block_count = LFS_BLOCK_CNT,
  37. .cache_size = LFS_BLOCK_SIZE,
  38. .lookahead_size = sizeof(lfs_sys_buf.lookahead),
  39. .block_cycles = 500,
  40. //
  41. .read_buffer = lfs_sys_buf.read,
  42. .prog_buffer = lfs_sys_buf.prog,
  43. .lookahead_buffer = lfs_sys_buf.lookahead,
  44. };
  45. int mid_little_fs_ram_main(void)
  46. {
  47. int err = -1;
  48. err = lfs_mount(&lfs_Stm32_ram, &cfg_Stm32Flash);
  49. if( err )
  50. {
  51. lfs_format(&lfs_Stm32_ram, &cfg_Stm32Flash);
  52. lfs_mount(&lfs_Stm32_ram, &cfg_Stm32Flash);
  53. }
  54. while(1)
  55. {
  56. uint32_t boot_count = 0;
  57. lfs_file_open_user(&lfs_Stm32_ram, &file_ram, "boot_count", LFS_O_RDWR | LFS_O_CREAT);
  58. lfs_file_read(&lfs_Stm32_ram, &file_ram, &boot_count, sizeof(boot_count));
  59. // update boot count
  60. boot_count += 1;
  61. lfs_file_rewind(&lfs_Stm32_ram, &file_ram); // seek the file to begin
  62. lfs_file_write(&lfs_Stm32_ram, &file_ram, &boot_count, sizeof(boot_count));
  63. // remember the storage is not updated until the file is closed successfully
  64. lfs_file_close(&lfs_Stm32_ram, &file_ram);
  65. // // release any resources we were using
  66. // print the boot count
  67. printf("boot_count: %d\n", boot_count);
  68. osDelay(1000);
  69. }
  70. //lfs_unmount(&lfs);
  71. }
  72. int ram_block_read(const struct lfs_config *c, lfs_block_t block,
  73. lfs_off_t off, void *buffer, lfs_size_t size)
  74. {
  75. uint8_t* pSrc = (uint8_t*)&ram_block[block][0] + off;
  76. uint8_t* pDest = (uint8_t*)buffer;
  77. memcpy(pDest,pSrc,size);
  78. return 0;
  79. }
  80. int ram_block_prog(const struct lfs_config *c, lfs_block_t block,
  81. lfs_off_t off, const void *buffer, lfs_size_t size)
  82. {
  83. uint8_t* pDest = (uint8_t*)&ram_block[block][0] + off;
  84. uint8_t* pSrc = (uint8_t*)buffer;
  85. memcpy(pDest,pSrc,size);
  86. return 0;
  87. }
  88. int ram_block_erase(const struct lfs_config *c, lfs_block_t block)
  89. {
  90. uint8_t* pDest = (uint8_t*)&ram_block[block][0];
  91. memset(pDest,0xff,c->block_size);
  92. return 0;
  93. }
  94. int ram_block_sync(const struct lfs_config *c)
  95. {
  96. return 0;
  97. }
  98. // Statically allocated file buffer. Must be cache_size.
  99. static uint8_t file_buffer[LFS_BLOCK_SIZE];
  100. struct lfs_file_config file_cfg = {
  101. .buffer = file_buffer
  102. };
  103. int lfs_file_open_user(lfs_t *lfs, lfs_file_t *file, const char *path, int flags)
  104. {
  105. return lfs_file_opencfg(lfs, file, path, flags, &file_cfg);
  106. }
  107. #endif //---------------------USE_MID_LFS_RAM-----------------------//