luat_sfud_lfs2.c 3.6 KB

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