luat_little_flash_lfs2.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "luat_base.h"
  2. #include "luat_fs.h"
  3. #include "luat_mem.h"
  4. #define LUAT_LOG_TAG "little_flash"
  5. #include "luat_log.h"
  6. #ifdef LUAT_USE_LITTLE_FLASH
  7. #ifdef LUAT_USE_FS_VFS
  8. #include "lfs.h"
  9. #include "little_flash.h"
  10. static size_t lf_offset = 0;
  11. // Read a block
  12. static int lf_block_device_read(const struct lfs_config *cfg, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size) {
  13. little_flash_t* flash = (little_flash_t*)cfg->context;
  14. // LLOGD("lf_block_device_read block:%d off:%d size:%d", block, off, size);
  15. return little_flash_read(flash, lf_offset + block * flash->chip_info.erase_size + off, buffer, size);
  16. // LLOGD("lf_block_device_read ret:%d", ret);
  17. // return LFS_ERR_OK;
  18. }
  19. static int lf_block_device_prog(const struct lfs_config *cfg, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size) {
  20. little_flash_t* flash = (little_flash_t*)cfg->context;
  21. // LLOGD("lf_block_device_prog block:%d off:%d size:%d", block, off, size);
  22. return little_flash_write(flash, lf_offset + block * flash->chip_info.erase_size + off, buffer, size);
  23. // LLOGD("lf_block_device_prog ret:%d", ret);
  24. // return LFS_ERR_OK;
  25. }
  26. static int lf_block_device_erase(const struct lfs_config *cfg, lfs_block_t block) {
  27. little_flash_t* flash = (little_flash_t*)cfg->context;
  28. return little_flash_erase(flash, lf_offset + block * flash->chip_info.erase_size, flash->chip_info.erase_size);
  29. // LLOGD("lf_block_device_erase ret:%d block:%d", ret, block);
  30. // return LFS_ERR_OK;
  31. }
  32. static int lf_block_device_sync(const struct lfs_config *cfg) {
  33. return LFS_ERR_OK;
  34. }
  35. #define LFS_BLOCK_DEVICE_LOOK_AHEAD (16)
  36. typedef struct LFS2 {
  37. lfs_t lfs;
  38. struct lfs_config cfg;
  39. uint8_t* lookahead_buffer[LFS_BLOCK_DEVICE_LOOK_AHEAD];
  40. uint8_t* read_buffer;
  41. uint8_t* prog_buffer;
  42. }LFS2_t;
  43. lfs_t* flash_lfs_lf(little_flash_t* flash, size_t offset, size_t maxsize) {
  44. if (flash==NULL){
  45. LLOGE("flash is null");
  46. return NULL;
  47. }
  48. LFS2_t *_lfs = luat_heap_malloc(sizeof(LFS2_t));
  49. if (_lfs == NULL)
  50. return NULL;
  51. memset(_lfs, 0, sizeof(LFS2_t));
  52. lf_offset = offset;
  53. lfs_t *lfs = &_lfs->lfs;
  54. struct lfs_config *lfs_cfg = &_lfs->cfg;
  55. lfs_cfg->context = flash,
  56. // block device operations
  57. lfs_cfg->read = lf_block_device_read;
  58. lfs_cfg->prog = lf_block_device_prog;
  59. lfs_cfg->erase = lf_block_device_erase;
  60. lfs_cfg->sync = lf_block_device_sync;
  61. // block device configuration
  62. lfs_cfg->read_size = flash->chip_info.read_size;
  63. lfs_cfg->prog_size = flash->chip_info.prog_size;
  64. lfs_cfg->block_size = flash->chip_info.erase_size;
  65. lfs_cfg->block_count = (maxsize > 0 ? maxsize : (flash->chip_info.capacity - offset)) / flash->chip_info.erase_size;
  66. lfs_cfg->block_cycles = 200;
  67. lfs_cfg->cache_size = flash->chip_info.prog_size;
  68. lfs_cfg->lookahead_size = LFS_BLOCK_DEVICE_LOOK_AHEAD;
  69. _lfs->read_buffer = luat_heap_malloc(flash->chip_info.prog_size);
  70. _lfs->prog_buffer = luat_heap_malloc(flash->chip_info.prog_size);
  71. lfs_cfg->read_buffer = _lfs->read_buffer;
  72. lfs_cfg->prog_buffer = _lfs->prog_buffer;
  73. lfs_cfg->lookahead_buffer = _lfs->lookahead_buffer;
  74. lfs_cfg->name_max = 63;
  75. lfs_cfg->file_max = 0;
  76. lfs_cfg->attr_max = 0;
  77. // LLOGD("block_size %d", lfs_cfg->block_size);
  78. // LLOGD("block_count %d", lfs_cfg->block_count);
  79. // LLOGD("capacity %d", flash->chip_info.capacity);
  80. // LLOGD("erase_size %d", flash->chip_info.erase_size);
  81. // ------
  82. int err = lfs_mount(lfs, lfs_cfg);
  83. LLOGD("lfs_mount %d",err);
  84. if (err)
  85. {
  86. err = lfs_format(lfs, lfs_cfg);
  87. LLOGD("lfs_format %d",err);
  88. if(err)
  89. goto fail;
  90. err = lfs_mount(lfs, lfs_cfg);
  91. LLOGD("lfs_mount %d",err);
  92. if(err)
  93. goto fail;
  94. }
  95. return lfs;
  96. fail :
  97. luat_heap_free(_lfs);
  98. return NULL;
  99. //------
  100. }
  101. #endif
  102. #endif