luat_lib_sdio.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. @module sdio
  3. @summary sdio
  4. @version 1.0
  5. @date 2021.09.02
  6. @tag LUAT_USE_SDIO
  7. @usage
  8. -- 本sdio库挂载tf卡到文件系统功能已经被fatfs的sdio模式取代
  9. -- 本sdio库仅保留直接读写TF卡的函数
  10. -- 例如 使用 sdio 0 挂载TF卡
  11. fatfs.mount(fatfs.SDIO, "/sd", 0)
  12. -- 挂载完成后, 使用 io 库的相关函数来操作就行
  13. local f = io.open("/sd/abc.txt")
  14. */
  15. #include "luat_base.h"
  16. #include "luat_sdio.h"
  17. #include "luat_mem.h"
  18. #define SDIO_COUNT 2
  19. static luat_sdio_t sdio_t[SDIO_COUNT];
  20. /**
  21. 初始化sdio
  22. @api sdio.init(id)
  23. @int 通道id,与具体设备有关,通常从0开始,默认0
  24. @return boolean 打开结果
  25. */
  26. static int l_sdio_init(lua_State *L) {
  27. if (luat_sdio_init(luaL_optinteger(L, 1, 0)) == 0) {
  28. lua_pushboolean(L, 1);
  29. }
  30. else {
  31. lua_pushboolean(L, 0);
  32. }
  33. return 1;
  34. }
  35. /*
  36. 直接读写sd卡上的数据
  37. @api sdio.sd_read(id, offset, len)
  38. @int sdio总线id
  39. @int 偏移量,必须是512的倍数
  40. @int 长度,必须是512的倍数
  41. @return string 若读取成功,返回字符串,否则返回nil
  42. @usage
  43. -- 初始化sdio并直接读取sd卡数据
  44. sdio.init(0)
  45. local t = sdio.sd_read(0, 0, 1024)
  46. if t then
  47. --- xxx
  48. end
  49. */
  50. static int l_sdio_read(lua_State *L) {
  51. int id = luaL_checkinteger(L, 1);
  52. int offset = luaL_checkinteger(L, 2);
  53. int len = luaL_checkinteger(L, 3);
  54. char* recv_buff = luat_heap_malloc(len);
  55. if(recv_buff == NULL)
  56. return 0;
  57. int ret = luat_sdio_sd_read(id, sdio_t[id].rca, recv_buff, offset, len);
  58. if (ret > 0) {
  59. lua_pushlstring(L, recv_buff, ret);
  60. luat_heap_free(recv_buff);
  61. return 1;
  62. }
  63. luat_heap_free(recv_buff);
  64. return 0;
  65. }
  66. /*
  67. 直接写sd卡
  68. @api sdio.sd_write(id, data, offset)
  69. @int sdio总线id
  70. @string 待写入的数据,长度必须是512的倍数
  71. @int 偏移量,必须是512的倍数
  72. @return bool 若读取成功,返回true,否则返回false
  73. @usage
  74. -- 初始化sdio并直接读取sd卡数据
  75. sdio.init(0)
  76. local t = sdio.sd_write(0, data, 0)
  77. if t then
  78. --- xxx
  79. end
  80. */
  81. static int l_sdio_write(lua_State *L) {
  82. int id = luaL_checkinteger(L, 1);
  83. size_t len;
  84. const char* send_buff;
  85. send_buff = lua_tolstring(L, 2, &len);
  86. int offset = luaL_checkinteger(L, 3);
  87. int ret = luat_sdio_sd_write(id, sdio_t[id].rca, (char*)send_buff, offset, len);
  88. if (ret > 0) {
  89. lua_pushboolean(L, 1);
  90. }
  91. lua_pushboolean(L, 0);
  92. return 1;
  93. }
  94. #include "rotable2.h"
  95. static const rotable_Reg_t reg_sdio[] =
  96. {
  97. { "init" , ROREG_FUNC(l_sdio_init )},
  98. { "sd_read" , ROREG_FUNC(l_sdio_read )},
  99. { "sd_write" , ROREG_FUNC(l_sdio_write)},
  100. // { "sd_mount" , ROREG_FUNC(l_sdio_sd_mount)},
  101. // { "sd_umount" , ROREG_FUNC(l_sdio_sd_umount)},
  102. // { "sd_format" , ROREG_FUNC(l_sdio_sd_format)},
  103. { NULL, ROREG_INT(0) }
  104. };
  105. LUAMOD_API int luaopen_sdio( lua_State *L ) {
  106. luat_newlib2(L, reg_sdio);
  107. return 1;
  108. }