luat_little_flash_lfs2.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. int ret = little_flash_read(flash, lf_offset + block * flash->chip_info.erase_size + off, buffer, size);
  15. // LLOGD("lf_block_device_read ret %d", ret);
  16. return LFS_ERR_OK;
  17. }
  18. 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) {
  19. little_flash_t* flash = (little_flash_t*)cfg->context;
  20. int ret = little_flash_write(flash, lf_offset + block * flash->chip_info.erase_size + off, buffer, size);
  21. // LLOGD("lf_block_device_prog ret %d", ret);
  22. return LFS_ERR_OK;
  23. }
  24. static int lf_block_device_erase(const struct lfs_config *cfg, lfs_block_t block) {
  25. little_flash_t* flash = (little_flash_t*)cfg->context;
  26. int ret = little_flash_erase(flash, lf_offset + block * flash->chip_info.erase_size, flash->chip_info.erase_size);
  27. // LLOGD("lf_block_device_erase ret %d", ret);
  28. return LFS_ERR_OK;
  29. }
  30. static int lf_block_device_sync(const struct lfs_config *cfg) {
  31. return LFS_ERR_OK;
  32. }
  33. typedef struct LFS2 {
  34. lfs_t lfs;
  35. struct lfs_config cfg;
  36. }LFS2_t;
  37. lfs_t* flash_lfs_lf(little_flash_t* flash, size_t offset, size_t maxsize) {
  38. if (flash==NULL){
  39. LLOGE("flash is null");
  40. return NULL;
  41. }
  42. LFS2_t *_lfs = luat_heap_malloc(sizeof(LFS2_t));
  43. if (_lfs == NULL)
  44. return NULL;
  45. memset(_lfs, 0, sizeof(LFS2_t));
  46. lf_offset = offset;
  47. lfs_t *lfs = &_lfs->lfs;
  48. struct lfs_config *lfs_cfg = &_lfs->cfg;
  49. lfs_cfg->context = flash,
  50. // block device operations
  51. lfs_cfg->read = lf_block_device_read;
  52. lfs_cfg->prog = lf_block_device_prog;
  53. lfs_cfg->erase = lf_block_device_erase;
  54. lfs_cfg->sync = lf_block_device_sync;
  55. // block device configuration
  56. lfs_cfg->read_size = flash->chip_info.read_size;
  57. lfs_cfg->prog_size = flash->chip_info.prog_size;
  58. lfs_cfg->block_size = flash->chip_info.erase_size;
  59. lfs_cfg->block_count = (maxsize > 0 ? maxsize : (flash->chip_info.capacity - offset)) / flash->chip_info.erase_size;
  60. lfs_cfg->block_cycles = 400;
  61. lfs_cfg->cache_size = flash->chip_info.prog_size;
  62. lfs_cfg->lookahead_size = flash->chip_info.prog_size;
  63. lfs_cfg->name_max = 63;
  64. lfs_cfg->file_max = 0;
  65. lfs_cfg->attr_max = 0;
  66. // LLOGD("block_size %d", lfs_cfg->block_size);
  67. // LLOGD("block_count %d", lfs_cfg->block_count);
  68. // LLOGD("capacity %d", flash->chip_info.capacity);
  69. // LLOGD("erase_size %d", flash->chip_info.erase_size);
  70. // ------
  71. int err = lfs_mount(lfs, lfs_cfg);
  72. // LLOGD("lfs_mount %d",err);
  73. if (err)
  74. {
  75. err = lfs_format(lfs, lfs_cfg);
  76. LLOGD("lfs_format %d",err);
  77. if(err)
  78. goto fail;
  79. err = lfs_mount(lfs, lfs_cfg);
  80. LLOGD("lfs_mount %d",err);
  81. if(err)
  82. goto fail;
  83. }
  84. return lfs;
  85. fail :
  86. luat_heap_free(_lfs);
  87. return NULL;
  88. //------
  89. }
  90. #endif
  91. #endif