luat_sfud_lfs2.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include "luat_base.h"
  2. #include "luat_fs.h"
  3. #define LUAT_LOG_TAG "luat.sfud"
  4. #include "luat_log.h"
  5. #ifdef LUAT_USE_SFUD
  6. #ifdef LUAT_USE_FS_VFS
  7. #include "lfs.h"
  8. #include "sfud.h"
  9. #define LFS_BLOCK_SIZE (4096)
  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_ERASE_SIZE (4096) // one sector 4KB
  14. //#define LFS_BLOCK_DEVICE_TOTOAL_SIZE (FLASH_FS_REGION_SIZE)
  15. #define LFS_BLOCK_DEVICE_LOOK_AHEAD (16)
  16. // Read a block
  17. 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) {
  18. sfud_flash* flash = (sfud_flash*)cfg->context;
  19. int re = sfud_read(flash, block * LFS_BLOCK_SIZE + off, size, buffer);
  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. int re = sfud_write(flash, block * LFS_BLOCK_SIZE + off, size, buffer);
  25. return LFS_ERR_OK;
  26. }
  27. static int sfud_block_device_erase(const struct lfs_config *cfg, lfs_block_t block) {
  28. sfud_flash* flash = (sfud_flash*)cfg->context;
  29. int re = sfud_erase(flash, block * LFS_BLOCK_SIZE, LFS_BLOCK_SIZE);
  30. return LFS_ERR_OK;
  31. }
  32. static int sfud_block_device_sync(const struct lfs_config *cfg) {
  33. return LFS_ERR_OK;
  34. }
  35. typedef struct LFS2 {
  36. lfs_t lfs;
  37. struct lfs_config cfg;
  38. uint8_t read_buffer[LFS_BLOCK_DEVICE_READ_SIZE];
  39. uint8_t prog_buffer[LFS_BLOCK_DEVICE_PROG_SIZE];
  40. uint8_t lookahead_buffer[LFS_BLOCK_DEVICE_LOOK_AHEAD];
  41. }LFS2_t;
  42. lfs_t* flash_lfs_sfud(sfud_flash* flash) {
  43. LFS2_t *_lfs = luat_heap_malloc(sizeof(LFS2_t));
  44. if (_lfs == NULL)
  45. return NULL;
  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 = sfud_block_device_read;
  51. lfs_cfg->prog = sfud_block_device_prog;
  52. lfs_cfg->erase = sfud_block_device_erase;
  53. lfs_cfg->sync = sfud_block_device_sync;
  54. // block device configuration
  55. lfs_cfg->read_size = LFS_BLOCK_DEVICE_READ_SIZE;
  56. lfs_cfg->prog_size = LFS_BLOCK_DEVICE_PROG_SIZE;
  57. lfs_cfg->block_size = flash->chip.erase_gran;
  58. lfs_cfg->block_count = flash->chip.capacity / flash->chip.erase_gran;
  59. lfs_cfg->block_cycles = 200;
  60. lfs_cfg->cache_size = LFS_BLOCK_DEVICE_CACHE_SIZE;
  61. lfs_cfg->lookahead_size = LFS_BLOCK_DEVICE_LOOK_AHEAD;
  62. lfs_cfg->read_buffer = _lfs->read_buffer;
  63. lfs_cfg->prog_buffer = _lfs->prog_buffer;
  64. lfs_cfg->lookahead_buffer = _lfs->lookahead_buffer;
  65. lfs_cfg->name_max = 63;
  66. lfs_cfg->file_max = 0;
  67. lfs_cfg->attr_max = 0;
  68. // ------
  69. int err = lfs_mount(lfs, lfs_cfg);
  70. LLOGD("lfs_mount %d",err);
  71. if (err)
  72. {
  73. err = lfs_format(lfs, lfs_cfg);
  74. LLOGD("lfs_format %d",err);
  75. if(err)
  76. goto fail;
  77. err = lfs_mount(lfs, lfs_cfg);
  78. LLOGD("lfs_mount %d",err);
  79. if(err)
  80. goto fail;
  81. }
  82. return lfs;
  83. fail :
  84. luat_heap_free(_lfs);
  85. return NULL;
  86. //------
  87. }
  88. #endif
  89. #endif