luat_sfud_lfs2.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "luat_base.h"
  2. #include "luat_fs.h"
  3. #ifdef LUAT_USE_SFUD
  4. #ifdef LUAT_USE_FS_VFS
  5. #include "lfs.h"
  6. #include "sfud.h"
  7. #define LFS_BLOCK_SIZE (4096)
  8. #define LFS_BLOCK_DEVICE_READ_SIZE (256)
  9. #define LFS_BLOCK_DEVICE_PROG_SIZE (256)
  10. #define LFS_BLOCK_DEVICE_CACHE_SIZE (256)
  11. #define LFS_BLOCK_DEVICE_ERASE_SIZE (4096) // one sector 4KB
  12. //#define LFS_BLOCK_DEVICE_TOTOAL_SIZE (FLASH_FS_REGION_SIZE)
  13. #define LFS_BLOCK_DEVICE_LOOK_AHEAD (16)
  14. // Read a block
  15. static int block_device_read(const struct lfs_config *cfg, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size) {
  16. sfud_flash* flash = (sfud_flash*)cfg->context;
  17. sfud_read(flash, buffer, block * LFS_BLOCK_SIZE + off, size);
  18. return LFS_ERR_OK;
  19. }
  20. static int block_device_prog(const struct lfs_config *cfg, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size) {
  21. sfud_flash* flash = (sfud_flash*)cfg->context;
  22. sfud_write(flash, buffer, block * LFS_BLOCK_SIZE + off, size);
  23. return LFS_ERR_OK;
  24. }
  25. static int block_device_erase(const struct lfs_config *cfg, lfs_block_t block) {
  26. sfud_flash* flash = (sfud_flash*)cfg->context;
  27. sfud_erase(flash, block * LFS_BLOCK_SIZE, LFS_BLOCK_SIZE);
  28. return LFS_ERR_OK;
  29. }
  30. static int 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. uint8_t read_buffer[LFS_BLOCK_DEVICE_READ_SIZE];
  37. uint8_t prog_buffer[LFS_BLOCK_DEVICE_PROG_SIZE];
  38. uint8_t lookahead_buffer[LFS_BLOCK_DEVICE_LOOK_AHEAD];
  39. }LFS2_t;
  40. lfs_t* flash_lfs_sfud(sfud_flash* flash) {
  41. LFS2_t *_lfs = luat_heap_malloc(sizeof(LFS2_t));
  42. if (_lfs == NULL)
  43. return NULL;
  44. lfs_t *lfs = &_lfs->lfs;
  45. struct lfs_config *lfs_cfg = &_lfs->cfg;
  46. lfs_cfg->context = flash,
  47. // block device operations
  48. lfs_cfg->read = block_device_read;
  49. lfs_cfg->prog = block_device_prog;
  50. lfs_cfg->erase = block_device_erase;
  51. lfs_cfg->sync = block_device_sync;
  52. // block device configuration
  53. lfs_cfg->read_size = LFS_BLOCK_DEVICE_READ_SIZE;
  54. lfs_cfg->prog_size = LFS_BLOCK_DEVICE_PROG_SIZE;
  55. lfs_cfg->block_size = flash->chip.erase_gran;
  56. lfs_cfg->block_count = flash->chip.capacity / flash->chip.erase_gran;
  57. lfs_cfg->block_cycles = 200;
  58. lfs_cfg->cache_size = LFS_BLOCK_DEVICE_CACHE_SIZE;
  59. lfs_cfg->lookahead_size = LFS_BLOCK_DEVICE_LOOK_AHEAD;
  60. lfs_cfg->read_buffer = _lfs->read_buffer;
  61. lfs_cfg->prog_buffer = _lfs->prog_buffer;
  62. lfs_cfg->lookahead_buffer = _lfs->lookahead_buffer;
  63. lfs_cfg->name_max = 63;
  64. lfs_cfg->file_max = 0;
  65. lfs_cfg->attr_max = 0;
  66. // ------
  67. int err = lfs_mount(&lfs, &lfs_cfg);
  68. LLOGD("lfs_mount %d",err);
  69. if (err)
  70. {
  71. err = lfs_format(&lfs, &lfs_cfg);
  72. LLOGD("lfs_format %d",err);
  73. if(err)
  74. goto fail;
  75. err = lfs_mount(&lfs, &lfs_cfg);
  76. LLOGD("lfs_mount %d",err);
  77. if(err)
  78. goto fail;
  79. }
  80. return lfs;
  81. fail :
  82. luat_heap_free(_lfs);
  83. return NULL;
  84. //------
  85. }
  86. #endif
  87. #endif