luat_lib_sdio.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. @module sdio
  3. @summary sdio
  4. @version 1.0
  5. @date 2021.09.02
  6. @tag LUAT_USE_SDIO
  7. */
  8. #include "luat_base.h"
  9. #include "luat_sdio.h"
  10. #include "luat_malloc.h"
  11. #define SDIO_COUNT 2
  12. static luat_sdio_t sdio_t[SDIO_COUNT];
  13. /**
  14. 初始化sdio
  15. @api sdio.init(id)
  16. @int 通道id,与具体设备有关,通常从0开始,默认0
  17. @return boolean 打开结果
  18. */
  19. static int l_sdio_init(lua_State *L) {
  20. if (luat_sdio_init(luaL_optinteger(L, 1, 0)) == 0) {
  21. lua_pushboolean(L, 1);
  22. }
  23. else {
  24. lua_pushboolean(L, 0);
  25. }
  26. return 1;
  27. }
  28. /*
  29. 直接读写sd卡上的数据
  30. @api sdio.sd_read(id, offset, len)
  31. @int sdio总线id
  32. @int 偏移量,必须是512的倍数
  33. @int 长度,必须是512的倍数
  34. @return string 若读取成功,返回字符串,否则返回nil
  35. @usage
  36. -- 初始化sdio并直接读取sd卡数据
  37. sdio.init(0)
  38. local t = sdio.sd_read(0, 0, 1024)
  39. if t then
  40. --- xxx
  41. end
  42. */
  43. static int l_sdio_read(lua_State *L) {
  44. int id = luaL_checkinteger(L, 1);
  45. int offset = luaL_checkinteger(L, 2);
  46. int len = luaL_checkinteger(L, 3);
  47. char* recv_buff = luat_heap_malloc(len);
  48. if(recv_buff == NULL)
  49. return 0;
  50. int ret = luat_sdio_sd_read(id, sdio_t[id].rca, recv_buff, offset, len);
  51. if (ret > 0) {
  52. lua_pushlstring(L, recv_buff, ret);
  53. luat_heap_free(recv_buff);
  54. return 1;
  55. }
  56. luat_heap_free(recv_buff);
  57. return 0;
  58. }
  59. /*
  60. 直接写sd卡
  61. @api sdio.sd_write(id, data, offset)
  62. @int sdio总线id
  63. @string 待写入的数据,长度必须是512的倍数
  64. @int 偏移量,必须是512的倍数
  65. @return bool 若读取成功,返回true,否则返回false
  66. @usage
  67. -- 初始化sdio并直接读取sd卡数据
  68. sdio.init(0)
  69. local t = sdio.sd_write(0, data, 0)
  70. if t then
  71. --- xxx
  72. end
  73. */
  74. static int l_sdio_write(lua_State *L) {
  75. int id = luaL_checkinteger(L, 1);
  76. size_t len;
  77. const char* send_buff;
  78. send_buff = lua_tolstring(L, 2, &len);
  79. int offset = luaL_checkinteger(L, 3);
  80. int ret = luat_sdio_sd_write(id, sdio_t[id].rca, (char*)send_buff, offset, len);
  81. if (ret > 0) {
  82. lua_pushboolean(L, 1);
  83. }
  84. lua_pushboolean(L, 0);
  85. return 1;
  86. }
  87. /*
  88. 挂载SD卡, 使用FATFS文件系统
  89. @api sdio.sd_mount(id, path, auto_format)
  90. @int sdio总线id
  91. @string 挂载路径, 默认"/sd", 不允许以"/"结尾
  92. @bool 是否自动格式化,默认是true
  93. @return bool 挂载成功返回true,否则返回false
  94. @return int 底层返回的具体结果码,用于调试
  95. */
  96. static int l_sdio_sd_mount(lua_State *L) {
  97. int id = luaL_optinteger(L, 1, 0);
  98. const char* path = luaL_optstring(L, 2, "/sd");
  99. int auto_format = luaL_optinteger(L, 3, 1);
  100. int ret = luat_sdio_sd_mount(id, &sdio_t[id].rca, (char*)path, auto_format);
  101. lua_pushboolean(L, ret == 0 ? 1: 0);
  102. lua_pushinteger(L, ret);
  103. return 2;
  104. }
  105. /*
  106. 卸载SD卡(视硬件情况, 不一定支持)
  107. @api sdio.sd_umount(id, path)
  108. @int sdio总线id
  109. @string 挂载路径, 默认"/sd", 不允许以"/"结尾
  110. @return bool 挂载成功返回true,否则返回false
  111. */
  112. static int l_sdio_sd_umount(lua_State *L) {
  113. int id = luaL_checkinteger(L, 1);
  114. //int auto_format = luaL_checkinteger(L, 2);
  115. int ret = luat_sdio_sd_unmount(id, sdio_t[id].rca);
  116. lua_pushboolean(L, ret == 0 ? 1: 0);
  117. return 1;
  118. }
  119. /*
  120. 格式化SD卡
  121. @api sdio.sd_format(id)
  122. @int sdio总线id
  123. @return bool 挂载成功返回true,否则返回false
  124. */
  125. static int l_sdio_sd_format(lua_State *L) {
  126. int id = luaL_checkinteger(L, 1);
  127. //int auto_format = luaL_checkinteger(L, 2);
  128. int ret = luat_sdio_sd_format(id, sdio_t[id].rca);
  129. lua_pushboolean(L, ret == 0 ? 1: 0);
  130. return 1;
  131. }
  132. #include "rotable2.h"
  133. static const rotable_Reg_t reg_sdio[] =
  134. {
  135. { "init" , ROREG_FUNC(l_sdio_init )},
  136. { "sd_read" , ROREG_FUNC(l_sdio_read )},
  137. { "sd_write" , ROREG_FUNC(l_sdio_write)},
  138. { "sd_mount" , ROREG_FUNC(l_sdio_sd_mount)},
  139. { "sd_umount" , ROREG_FUNC(l_sdio_sd_umount)},
  140. { "sd_format" , ROREG_FUNC(l_sdio_sd_format)},
  141. { NULL, ROREG_INT(0) }
  142. };
  143. LUAMOD_API int luaopen_sdio( lua_State *L ) {
  144. luat_newlib2(L, reg_sdio);
  145. return 1;
  146. }