luat_lib_little_flash.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. local 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. lf_flash->spi.user_data = little_flash_spi_device;
  32. }else{
  33. LLOGW("little_flash init spi_device is nil");
  34. return 0;
  35. }
  36. little_flash_init();
  37. int re = little_flash_device_init(lf_flash);
  38. lua_pushlightuserdata(L, lf_flash);
  39. return 1;
  40. }
  41. #ifdef LUAT_USE_FS_VFS
  42. #include "luat_fs.h"
  43. #include "lfs.h"
  44. extern lfs_t* flash_lfs_lf(little_flash_t* flash, size_t offset, size_t maxsize);
  45. /*
  46. 挂载 little_flash lfs文件系统
  47. @api lf.mount(flash, mount_point, offset, maxsize)
  48. @userdata flash Flash 设备对象 lf.init()返回的数据结构
  49. @string mount_point 挂载目录名
  50. @int 起始偏移量,默认0
  51. @int 总大小, 默认是整个flash
  52. @return bool 成功返回true
  53. @usage
  54. log.info("lf.mount",lf.mount(little_flash_device,"/little_flash"))
  55. */
  56. static int luat_little_flash_mount(lua_State *L) {
  57. const little_flash_t *flash = lua_touserdata(L, 1);
  58. const char* mount_point = luaL_checkstring(L, 2);
  59. size_t offset = luaL_optinteger(L, 3, 0);
  60. size_t maxsize = luaL_optinteger(L, 4, 0);
  61. lfs_t* lfs = flash_lfs_lf(flash, offset, maxsize);
  62. if (lfs) {
  63. luat_fs_conf_t conf = {
  64. .busname = (char*)lfs,
  65. .type = "lfs2",
  66. .filesystem = "lfs2",
  67. .mount_point = mount_point,
  68. };
  69. int ret = luat_fs_mount(&conf);
  70. LLOGD("vfs mount %s ret %d", mount_point, ret);
  71. lua_pushboolean(L, 1);
  72. }
  73. else {
  74. lua_pushboolean(L, 0);
  75. }
  76. return 1;
  77. }
  78. #endif
  79. #include "rotable2.h"
  80. static const rotable_Reg_t reg_little_flash[] =
  81. {
  82. { "init", ROREG_FUNC(luat_little_flash_init)},
  83. #ifdef LUAT_USE_FS_VFS
  84. { "mount", ROREG_FUNC(luat_little_flash_mount)},
  85. #endif
  86. { NULL, ROREG_INT(0)}
  87. };
  88. LUAMOD_API int luaopen_little_flash( lua_State *L ) {
  89. luat_newlib2(L, reg_little_flash);
  90. return 1;
  91. }