luat_lib_little_flash.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. @module little_flash
  3. @summary LITTLE FLASH 软件包
  4. @version 1.0
  5. @date 2024.05.11
  6. @demo little_flash
  7. @tag LUAT_USE_LITTLE_FLASH
  8. */
  9. #include "luat_base.h"
  10. #include "luat_spi.h"
  11. #include "luat_mem.h"
  12. #define LUAT_LOG_TAG "little_flash"
  13. #include "luat_log.h"
  14. #include "little_flash.h"
  15. /*
  16. 初始化 little_flash
  17. @api lf.init(spi_device)
  18. @int userdata spi_device
  19. @return userdata 成功返回一个数据结构,否则返回nil
  20. @usage
  21. --spi_device
  22. spi_device = spi.deviceSetup(0,17,0,0,8,2000000,spi.MSB,1,0)
  23. log.info("lf.init",lf.init(spi_device))
  24. */
  25. static int luat_little_flash_init(lua_State *L){
  26. luat_spi_device_t* little_flash_spi_device = NULL;
  27. little_flash_t* lf_flash = NULL;
  28. if (lua_type(L, 1) == LUA_TUSERDATA){
  29. little_flash_spi_device = (luat_spi_device_t*)lua_touserdata(L, 1);
  30. lf_flash = luat_heap_malloc(sizeof(little_flash_t));
  31. memset(lf_flash, 0, sizeof(little_flash_t));
  32. lf_flash->spi.user_data = little_flash_spi_device;
  33. }else{
  34. LLOGW("little_flash init spi_device is nil");
  35. return 0;
  36. }
  37. little_flash_init();
  38. lf_err_t re = little_flash_device_init(lf_flash);
  39. if (re == LF_ERR_OK){
  40. lua_pushlightuserdata(L, lf_flash);
  41. return 1;
  42. }
  43. luat_heap_free(lf_flash);
  44. return 0;
  45. }
  46. #ifdef LUAT_USE_FS_VFS
  47. #include "luat_fs.h"
  48. #include "lfs.h"
  49. extern lfs_t* flash_lfs_lf(little_flash_t* flash, size_t offset, size_t maxsize);
  50. /*
  51. 挂载 little_flash lfs文件系统
  52. @api lf.mount(flash, mount_point, offset, maxsize)
  53. @userdata flash Flash 设备对象 lf.init()返回的数据结构
  54. @string mount_point 挂载目录名
  55. @int 起始偏移量,默认0
  56. @int 总大小, 默认是整个flash
  57. @return bool 成功返回true
  58. @usage
  59. log.info("lf.mount",lf.mount(little_flash_device,"/little_flash"))
  60. */
  61. static int luat_little_flash_mount(lua_State *L) {
  62. little_flash_t *flash = lua_touserdata(L, 1);
  63. if (flash == NULL) {
  64. LLOGE("little_flash mount flash is nil");
  65. return 0;
  66. }
  67. const char* mount_point = luaL_checkstring(L, 2);
  68. size_t offset = luaL_optinteger(L, 3, 0);
  69. size_t maxsize = luaL_optinteger(L, 4, 0);
  70. lfs_t* lfs = flash_lfs_lf(flash, offset, maxsize);
  71. if (lfs) {
  72. luat_fs_conf_t conf = {
  73. .busname = (char*)lfs,
  74. .type = "lfs2",
  75. .filesystem = "lfs2",
  76. .mount_point = mount_point,
  77. };
  78. int ret = luat_fs_mount(&conf);
  79. LLOGD("vfs mount %s ret %d", mount_point, ret);
  80. lua_pushboolean(L, 1);
  81. }
  82. else {
  83. lua_pushboolean(L, 0);
  84. }
  85. return 1;
  86. }
  87. #endif
  88. #include "rotable2.h"
  89. static const rotable_Reg_t reg_little_flash[] =
  90. {
  91. { "init", ROREG_FUNC(luat_little_flash_init)},
  92. #ifdef LUAT_USE_FS_VFS
  93. { "mount", ROREG_FUNC(luat_little_flash_mount)},
  94. #endif
  95. { NULL, ROREG_INT(0)}
  96. };
  97. LUAMOD_API int luaopen_little_flash( lua_State *L ) {
  98. luat_newlib2(L, reg_little_flash);
  99. return 1;
  100. }