luat_little_flash_lfs2.c 3.6 KB

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