luat_sfd_onchip.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "luat_base.h"
  2. #include "luat_sfd.h"
  3. #include "luat_malloc.h"
  4. #include "lfs.h"
  5. #ifdef LUAT_SFD_ONCHIP
  6. #if LUAT_SFD_ONCHIP
  7. int sfd_onchip_init (void* userdata);
  8. int sfd_onchip_status (void* userdata);
  9. int sfd_onchip_read (void* userdata, char* buff, size_t offset, size_t len);
  10. int sfd_onchip_write (void* userdata, const char* buff, size_t offset, size_t len);
  11. int sfd_onchip_erase (void* userdata, size_t offset, size_t len);
  12. int sfd_onchip_ioctl (void* userdata, size_t cmd, void* buff);
  13. const sdf_opts_t sfd_onchip_opts = {
  14. .initialize = sfd_onchip_init,
  15. .status = sfd_onchip_status,
  16. .read = sfd_onchip_read,
  17. .write = sfd_onchip_write,
  18. .erase = sfd_onchip_erase,
  19. .ioctl = sfd_onchip_ioctl,
  20. };
  21. #include "lfs.h"
  22. #define LFS_BLOCK_SIZE (4096)
  23. #define LFS_BLOCK_DEVICE_READ_SIZE (256)
  24. #define LFS_BLOCK_DEVICE_PROG_SIZE (256)
  25. #define LFS_BLOCK_DEVICE_CACHE_SIZE (256)
  26. #define LFS_BLOCK_DEVICE_ERASE_SIZE (4096) // one sector 4KB
  27. //#define LFS_BLOCK_DEVICE_TOTOAL_SIZE (FLASH_FS_REGION_SIZE)
  28. #define LFS_BLOCK_DEVICE_LOOK_AHEAD (16)
  29. // Read a block
  30. static int block_device_read(const struct lfs_config *cfg, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size) {
  31. sfd_onchip_t* onchip = (sfd_onchip_t*)cfg->context;
  32. sfd_onchip_read(onchip, buffer, block * LFS_BLOCK_SIZE + off, size);
  33. return LFS_ERR_OK;
  34. }
  35. 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) {
  36. sfd_onchip_t* onchip = (sfd_onchip_t*)cfg->context;
  37. sfd_onchip_write(onchip, buffer, block * LFS_BLOCK_SIZE + off, size);
  38. return LFS_ERR_OK;
  39. }
  40. static int block_device_erase(const struct lfs_config *cfg, lfs_block_t block) {
  41. sfd_onchip_t* onchip = (sfd_onchip_t*)cfg->context;
  42. sfd_onchip_erase(onchip, block * LFS_BLOCK_SIZE, LFS_BLOCK_SIZE);
  43. return LFS_ERR_OK;
  44. }
  45. static int block_device_sync(const struct lfs_config *cfg) {
  46. sfd_onchip_t* onchip = (sfd_onchip_t*)cfg->context;
  47. sfd_onchip_ioctl(onchip, 0x05, NULL);
  48. return LFS_ERR_OK;
  49. }
  50. typedef struct LFS2 {
  51. lfs_t lfs;
  52. struct lfs_config cfg;
  53. uint8_t read_buffer[LFS_BLOCK_DEVICE_READ_SIZE];
  54. uint8_t prog_buffer[LFS_BLOCK_DEVICE_PROG_SIZE];
  55. uint8_t lookahead_buffer[LFS_BLOCK_DEVICE_LOOK_AHEAD];
  56. }LFS2_t;
  57. lfs_t* onchip_lfs_sfd(sfd_onchip_t* onchip) {
  58. LFS2_t *_lfs = luat_heap_malloc(sizeof(LFS2_t));
  59. if (_lfs == NULL)
  60. return NULL;
  61. lfs_t *lfs = &_lfs->lfs;
  62. struct lfs_config *lfs_cfg = &_lfs->cfg;
  63. lfs_cfg->context = onchip,
  64. // block device operations
  65. lfs_cfg->read = block_device_read;
  66. lfs_cfg->prog = block_device_prog;
  67. lfs_cfg->erase = block_device_erase;
  68. lfs_cfg->sync = block_device_sync;
  69. // block device configuration
  70. lfs_cfg->read_size = LFS_BLOCK_DEVICE_READ_SIZE;
  71. lfs_cfg->prog_size = LFS_BLOCK_DEVICE_PROG_SIZE;
  72. lfs_cfg->block_size = LFS_BLOCK_SIZE;
  73. lfs_cfg->block_count = onchip->block_count;
  74. lfs_cfg->block_cycles = 200;
  75. lfs_cfg->cache_size = LFS_BLOCK_DEVICE_CACHE_SIZE;
  76. lfs_cfg->lookahead_size = LFS_BLOCK_DEVICE_LOOK_AHEAD;
  77. lfs_cfg->read_buffer = _lfs->read_buffer;
  78. lfs_cfg->prog_buffer = _lfs->prog_buffer;
  79. lfs_cfg->lookahead_buffer = _lfs->lookahead_buffer;
  80. lfs_cfg->name_max = 63;
  81. lfs_cfg->file_max = 0;
  82. lfs_cfg->attr_max = 0;
  83. // ------
  84. int err = lfs_mount(lfs, lfs_cfg);
  85. LLOGD("lfs_mount %d",err);
  86. if (err)
  87. {
  88. err = lfs_format(lfs, lfs_cfg);
  89. LLOGD("lfs_format %d",err);
  90. if(err)
  91. goto fail;
  92. err = lfs_mount(lfs, lfs_cfg);
  93. LLOGD("lfs_mount %d",err);
  94. if(err)
  95. goto fail;
  96. }
  97. return lfs;
  98. fail :
  99. luat_heap_free(_lfs);
  100. return NULL;
  101. //------
  102. }
  103. #endif
  104. #endif