luat_sfd_lfs.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "luat_base.h"
  2. #include "luat_sfd.h"
  3. #include "luat_mem.h"
  4. #define LUAT_LOG_TAG "sfd.lfs"
  5. #include "luat_log.h"
  6. luat_sfd_lfs_t* sfd_lfs;
  7. // Read a block
  8. static int block_device_read(const struct lfs_config *cfg, lfs_block_t block,
  9. lfs_off_t off, void *buffer, lfs_size_t size) {
  10. sfd_drv_t* drv = cfg->context;
  11. int ret = luat_sfd_read(drv, buffer, block * LFS_BLOCK_DEVICE_ERASE_SIZE + off, size);
  12. // LLOGD("sfd read %d %d %d %d", block, off, size, ret);
  13. if (ret >= 0) {
  14. // LLOGD("block_device_read return LFS_ERR_OK");
  15. return LFS_ERR_OK;
  16. }
  17. // LLOGD("block_device_read return LFS_ERR_IO");
  18. return LFS_ERR_IO;
  19. }
  20. // Program a block
  21. //
  22. // The block must have previously been erased.
  23. static int block_device_prog(const struct lfs_config *cfg, lfs_block_t block,
  24. lfs_off_t off, const void *buffer, lfs_size_t size) {
  25. sfd_drv_t* drv = cfg->context;
  26. int ret = luat_sfd_write(drv, buffer, block * LFS_BLOCK_DEVICE_ERASE_SIZE + off, size);
  27. // LLOGD("sfd write %d %d %d %d", block, off, size, ret);
  28. if (ret == size) {
  29. // LLOGD("block_device_prog return LFS_ERR_OK");
  30. return LFS_ERR_OK;
  31. }
  32. LLOGE("write fail %d ret %d", block, ret);
  33. return LFS_ERR_IO;
  34. }
  35. // Erase a block
  36. //
  37. // A block must be erased before being programmed. The
  38. // state of an erased block is undefined.
  39. static int block_device_erase(const struct lfs_config *cfg, lfs_block_t block) {
  40. sfd_drv_t* drv = cfg->context;
  41. int ret = luat_sfd_erase(drv, block * LFS_BLOCK_DEVICE_ERASE_SIZE, LFS_BLOCK_DEVICE_ERASE_SIZE);
  42. if (ret) {
  43. return LFS_ERR_OK;
  44. }
  45. // 再试一次
  46. ret = luat_sfd_erase(drv, block * LFS_BLOCK_DEVICE_ERASE_SIZE, LFS_BLOCK_DEVICE_ERASE_SIZE);
  47. if (ret == 0) {
  48. return LFS_ERR_OK;
  49. }
  50. LLOGE("erase fail %d ret %d", block, ret);
  51. return LFS_ERR_IO;
  52. }
  53. // Sync the block device
  54. static int block_device_sync(const struct lfs_config *cfg) {
  55. return 0;
  56. }
  57. int luat_sfd_lfs_init(sfd_drv_t *drv) {
  58. int ret = 0;
  59. if (sfd_lfs == NULL) {
  60. sfd_lfs = luat_heap_malloc(sizeof(luat_sfd_lfs_t));
  61. if (sfd_lfs == NULL) {
  62. LLOGE("out of memory when malloc sfd_lfs");
  63. return -1;
  64. }
  65. memset(sfd_lfs, 0, sizeof(luat_sfd_lfs_t));
  66. sfd_lfs->conf.read = block_device_read;
  67. sfd_lfs->conf.prog = block_device_prog;
  68. sfd_lfs->conf.erase = block_device_erase;
  69. sfd_lfs->conf.sync = block_device_sync;
  70. sfd_lfs->conf.attr_max = 0;
  71. sfd_lfs->conf.file_max = 4096;
  72. sfd_lfs->conf.block_count = (LFS_BLOCK_DEVICE_TOTOAL_SIZE) / LFS_BLOCK_DEVICE_ERASE_SIZE;
  73. sfd_lfs->conf.block_size = LFS_BLOCK_DEVICE_ERASE_SIZE;
  74. sfd_lfs->conf.block_cycles = 200;
  75. sfd_lfs->conf.name_max = 63;
  76. sfd_lfs->conf.read_size = LFS_BLOCK_DEVICE_CACHE_SIZE;
  77. sfd_lfs->conf.cache_size = LFS_BLOCK_DEVICE_CACHE_SIZE;
  78. sfd_lfs->conf.prog_size = LFS_BLOCK_DEVICE_PROG_SIZE;
  79. sfd_lfs->conf.cache_size = LFS_BLOCK_DEVICE_CACHE_SIZE;
  80. sfd_lfs->conf.lookahead_size = LFS_BLOCK_DEVICE_LOOK_AHEAD;
  81. sfd_lfs->conf.lookahead_buffer = sfd_lfs->lookahead_buffer;
  82. sfd_lfs->conf.prog_buffer = sfd_lfs->prog_buffer;
  83. sfd_lfs->conf.read_buffer = sfd_lfs->read_buffer;
  84. sfd_lfs->conf.context = drv;
  85. ret = lfs_mount(&sfd_lfs->lfs, &sfd_lfs->conf);
  86. if (ret != LFS_ERR_OK) {
  87. LLOGI("sfd_lfs mount ret %d, exec auto-format", ret);
  88. ret = lfs_format(&sfd_lfs->lfs, &sfd_lfs->conf);
  89. if (ret != LFS_ERR_OK) {
  90. luat_heap_free(sfd_lfs);
  91. sfd_lfs = NULL;
  92. LLOGE("sfd_lfs auto-format ret %d", ret);
  93. return ret;
  94. }
  95. ret = lfs_mount(&sfd_lfs->lfs, &sfd_lfs->conf);
  96. if (ret != LFS_ERR_OK) {
  97. luat_heap_free(sfd_lfs);
  98. sfd_lfs = NULL;
  99. LLOGE("sfd_lfs remount ret %d", ret);
  100. return ret;
  101. }
  102. }
  103. LLOGD("init ok");
  104. }
  105. return 0;
  106. }