luat_sfud_lfs2.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "luat_base.h"
  2. #include "luat_fs.h"
  3. #include "luat_malloc.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_SIZE (4096)
  11. #define LFS_BLOCK_DEVICE_READ_SIZE (256)
  12. #define LFS_BLOCK_DEVICE_PROG_SIZE (256)
  13. #define LFS_BLOCK_DEVICE_CACHE_SIZE (256)
  14. #define LFS_BLOCK_DEVICE_ERASE_SIZE (4096) // one sector 4KB
  15. //#define LFS_BLOCK_DEVICE_TOTOAL_SIZE (FLASH_FS_REGION_SIZE)
  16. #define LFS_BLOCK_DEVICE_LOOK_AHEAD (16)
  17. // Read a block
  18. 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) {
  19. sfud_flash* flash = (sfud_flash*)cfg->context;
  20. int re = sfud_read(flash, block * LFS_BLOCK_SIZE + off, size, buffer);
  21. return LFS_ERR_OK;
  22. }
  23. 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) {
  24. sfud_flash* flash = (sfud_flash*)cfg->context;
  25. int re = sfud_write(flash, block * LFS_BLOCK_SIZE + off, size, buffer);
  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. int re = sfud_erase(flash, block * LFS_BLOCK_SIZE, LFS_BLOCK_SIZE);
  31. return LFS_ERR_OK;
  32. }
  33. static int sfud_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. uint8_t read_buffer[LFS_BLOCK_DEVICE_READ_SIZE];
  40. uint8_t prog_buffer[LFS_BLOCK_DEVICE_PROG_SIZE];
  41. uint8_t lookahead_buffer[LFS_BLOCK_DEVICE_LOOK_AHEAD];
  42. }LFS2_t;
  43. lfs_t* flash_lfs_sfud(sfud_flash* flash) {
  44. LFS2_t *_lfs = luat_heap_malloc(sizeof(LFS2_t));
  45. if (_lfs == NULL)
  46. return NULL;
  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 = sfud_block_device_read;
  52. lfs_cfg->prog = sfud_block_device_prog;
  53. lfs_cfg->erase = sfud_block_device_erase;
  54. lfs_cfg->sync = sfud_block_device_sync;
  55. // block device configuration
  56. lfs_cfg->read_size = LFS_BLOCK_DEVICE_READ_SIZE;
  57. lfs_cfg->prog_size = LFS_BLOCK_DEVICE_PROG_SIZE;
  58. lfs_cfg->block_size = flash->chip.erase_gran;
  59. lfs_cfg->block_count = flash->chip.capacity / flash->chip.erase_gran;
  60. lfs_cfg->block_cycles = 200;
  61. lfs_cfg->cache_size = LFS_BLOCK_DEVICE_CACHE_SIZE;
  62. lfs_cfg->lookahead_size = LFS_BLOCK_DEVICE_LOOK_AHEAD;
  63. lfs_cfg->read_buffer = _lfs->read_buffer;
  64. lfs_cfg->prog_buffer = _lfs->prog_buffer;
  65. lfs_cfg->lookahead_buffer = _lfs->lookahead_buffer;
  66. lfs_cfg->name_max = 63;
  67. lfs_cfg->file_max = 0;
  68. lfs_cfg->attr_max = 0;
  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