luat_lib_uart.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. @module uart
  3. @summary 串口操作库
  4. @version 1.0
  5. @date 2020.03.30
  6. */
  7. #include "luat_base.h"
  8. #include "luat_uart.h"
  9. #include "luat_malloc.h"
  10. #include "luat_msgbus.h"
  11. #include "luat_fs.h"
  12. #include "string.h"
  13. #include "luat_zbuff.h"
  14. #define LUAT_LOG_TAG "uart"
  15. #include "luat_log.h"
  16. #define MAX_DEVICE_COUNT 10
  17. typedef struct luat_uart_cb {
  18. int received;//回调函数
  19. int sent;//回调函数
  20. } luat_uart_cb_t;
  21. static luat_uart_cb_t uart_cbs[MAX_DEVICE_COUNT];
  22. int l_uart_handler(lua_State *L, void* ptr) {
  23. //LLOGD("l_uart_handler");
  24. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  25. lua_pop(L, 1);
  26. int uart_id = msg->arg1;
  27. if (!luat_uart_exist(uart_id)) {
  28. //LLOGW("not exist uart id=%ld but event fired?!", uart_id);
  29. return 0;
  30. }
  31. // sent event
  32. if (msg->arg2 == 0) {
  33. //LLOGD("uart%ld sent callback", uart_id);
  34. if (uart_cbs[uart_id].sent) {
  35. lua_geti(L, LUA_REGISTRYINDEX, uart_cbs[uart_id].sent);
  36. if (lua_isfunction(L, -1)) {
  37. lua_pushinteger(L, uart_id);
  38. lua_call(L, 1, 0);
  39. }
  40. }
  41. }
  42. else {
  43. if (uart_cbs[uart_id].received) {
  44. lua_geti(L, LUA_REGISTRYINDEX, uart_cbs[uart_id].received);
  45. if (lua_isfunction(L, -1)) {
  46. lua_pushinteger(L, uart_id);
  47. lua_pushinteger(L, msg->arg2);
  48. lua_call(L, 2, 0);
  49. }
  50. else {
  51. //LLOGD("uart%ld received callback not function", uart_id);
  52. }
  53. }
  54. else {
  55. //LLOGD("uart%ld no received callback", uart_id);
  56. }
  57. }
  58. // 给rtos.recv方法返回个空数据
  59. lua_pushinteger(L, 0);
  60. return 1;
  61. }
  62. /*
  63. 配置串口参数
  64. @api uart.setup(id, baud_rate, data_bits, stop_bits, partiy, bit_order, buff_size)
  65. @int 串口id, uart0写0, uart1写1, 如此类推, 最大值取决于设备
  66. @int 波特率, 默认115200
  67. @int 数据位,默认为8
  68. @int 停止位,默认为1
  69. @int 校验位,可选 uart.None/uart.Even/uart.Odd
  70. @int 大小端,默认小端 uart.LSB, 可选 uart.MSB
  71. @int 缓冲区大小,默认值1024
  72. @int 485模式下的转换GPIO, 默认值0xffffffff
  73. @int 485模式下的rx方向GPIO的电平, 默认值0
  74. @int 485模式下tx向rx转换的延迟时间,默认值12bit的时间,单位us
  75. @return int 成功返回0,失败返回其他值
  76. @usage
  77. -- 最常用115200 8N1
  78. uart.setup(1, 115200, 8, 1, uart.NONE)
  79. -- 可以简写为 uart.setup(1)
  80. */
  81. static int l_uart_setup(lua_State *L)
  82. {
  83. lua_Number stop_bits = luaL_optnumber(L, 4, 1);
  84. luat_uart_t uart_config = {
  85. .id = luaL_checkinteger(L, 1),
  86. .baud_rate = luaL_optinteger(L, 2, 115200),
  87. .data_bits = luaL_optinteger(L, 3, 8),
  88. .parity = luaL_optinteger(L, 5, LUAT_PARITY_NONE),
  89. .bit_order = luaL_optinteger(L, 6, LUAT_BIT_ORDER_LSB),
  90. .bufsz = luaL_optinteger(L, 7, 1024),
  91. .pin485 = luaL_optinteger(L, 8, 0xffffffff),
  92. .rx_level = luaL_optinteger(L, 9, 0),
  93. };
  94. if(stop_bits == 0.5)
  95. uart_config.stop_bits = LUAT_0_5_STOP_BITS;
  96. else if(stop_bits == 1.5)
  97. uart_config.stop_bits = LUAT_1_5_STOP_BITS;
  98. else
  99. uart_config.stop_bits = (uint8_t)stop_bits;
  100. uart_config.delay = luaL_optinteger(L, 10, 12000000/uart_config.baud_rate);
  101. int result = luat_uart_setup(&uart_config);
  102. lua_pushinteger(L, result);
  103. return 1;
  104. }
  105. /*
  106. 写串口
  107. @api uart.write(id, data)
  108. @int 串口id, uart0写0, uart1写1
  109. @string/zbuff 待写入的数据,如果是zbuff会从指针起始位置开始读
  110. @int 可选,要发送的数据长度,默认全发
  111. @return int 成功的数据长度
  112. @usage
  113. uart.write(1, "rdy\r\n")
  114. */
  115. static int l_uart_write(lua_State *L)
  116. {
  117. size_t len;
  118. const char *buf;
  119. uint8_t id = luaL_checkinteger(L, 1);
  120. if(lua_isuserdata(L, 2))
  121. {
  122. luat_zbuff_t *buff = ((luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
  123. len = buff->len - buff->cursor;
  124. buf = (const char *)(buff->addr + buff->cursor);
  125. }
  126. else
  127. {
  128. buf = lua_tolstring(L, 2, &len);//取出字符串数据
  129. }
  130. if(lua_isinteger(L, 3))
  131. {
  132. size_t l = luaL_checkinteger(L, 3);
  133. if(len > l)
  134. len = l;
  135. }
  136. int result = luat_uart_write(id, (char*)buf, len);
  137. lua_pushinteger(L, result);
  138. return 1;
  139. }
  140. /*
  141. 读串口
  142. @api uart.read(id, len)
  143. @int 串口id, uart0写0, uart1写1
  144. @int 读取长度
  145. @file/zbuff 可选:文件句柄或zbuff对象
  146. @return string 读取到的数据 / 传入zbuff时,返回读到的长度,并把zbuff指针后移
  147. @usage
  148. uart.read(1, 16)
  149. */
  150. static int l_uart_read(lua_State *L)
  151. {
  152. uint8_t id = luaL_checkinteger(L, 1);
  153. uint32_t length = luaL_optinteger(L, 2, 1024);
  154. if(lua_isuserdata(L, 3)){//zbuff对象特殊处理
  155. luat_zbuff_t *buff = ((luat_zbuff_t *)luaL_checkudata(L, 3, LUAT_ZBUFF_TYPE));
  156. uint8_t* recv = buff->addr+buff->cursor;
  157. if(length > buff->len - buff->cursor)
  158. length = buff->len - buff->cursor;
  159. int result = luat_uart_read(id, recv, length);
  160. if(result < 0)
  161. result = 0;
  162. buff->cursor += result;
  163. lua_pushinteger(L, result);
  164. return 1;
  165. }
  166. if (length < 512)
  167. length = 512;
  168. uint8_t* recv = luat_heap_malloc(length);
  169. if (recv == NULL) {
  170. LLOGE("system is out of memory!!!");
  171. lua_pushstring(L, "");
  172. return 1;
  173. }
  174. uint32_t read_length = 0;
  175. while(read_length < length)//循环读完
  176. {
  177. int result = luat_uart_read(id, (void*)(recv + read_length), length - read_length);
  178. if (result > 0) {
  179. read_length += result;
  180. }
  181. else
  182. {
  183. break;
  184. }
  185. }
  186. if(read_length > 0)
  187. {
  188. if (lua_isinteger(L, 3)) {
  189. uint32_t fd = luaL_checkinteger(L, 3);
  190. luat_fs_fwrite(recv, 1, read_length, (FILE*)fd);
  191. }
  192. else {
  193. lua_pushlstring(L, (const char*)recv, read_length);
  194. }
  195. }
  196. else
  197. {
  198. lua_pushstring(L, "");
  199. }
  200. luat_heap_free(recv);
  201. return 1;
  202. }
  203. /*
  204. 关闭串口
  205. @api uart.close(id)
  206. @int 串口id, uart0写0, uart1写1
  207. @return nil 无返回值
  208. @usage
  209. uart.close(1)
  210. */
  211. static int l_uart_close(lua_State *L)
  212. {
  213. uint8_t result = luat_uart_close(luaL_checkinteger(L, 1));
  214. lua_pushinteger(L, result);
  215. return 1;
  216. }
  217. /*
  218. 注册串口事件回调
  219. @api uart.on(id, event, func)
  220. @int 串口id, uart0写0, uart1写1
  221. @string 事件名称
  222. @function 回调方法
  223. @return nil 无返回值
  224. @usage
  225. uart.on(1, "receive", function(id, len)
  226. local data = uart.read(id, len)
  227. log.info("uart", id, len, data)
  228. end)
  229. */
  230. static int l_uart_on(lua_State *L) {
  231. int uart_id = luaL_checkinteger(L, 1);
  232. if (!luat_uart_exist(uart_id)) {
  233. lua_pushliteral(L, "no such uart id");
  234. return 1;
  235. }
  236. const char* event = luaL_checkstring(L, 2);
  237. if (!strcmp("receive", event) || !strcmp("recv", event)) {
  238. if (uart_cbs[uart_id].received != 0) {
  239. luaL_unref(L, LUA_REGISTRYINDEX, uart_cbs[uart_id].received);
  240. uart_cbs[uart_id].received = 0;
  241. }
  242. if (lua_isfunction(L, 3)) {
  243. lua_pushvalue(L, 3);
  244. uart_cbs[uart_id].received = luaL_ref(L, LUA_REGISTRYINDEX);
  245. }
  246. }
  247. else if (!strcmp("sent", event)) {
  248. if (uart_cbs[uart_id].sent != 0) {
  249. luaL_unref(L, LUA_REGISTRYINDEX, uart_cbs[uart_id].sent);
  250. uart_cbs[uart_id].sent = 0;
  251. }
  252. if (lua_isfunction(L, 3)) {
  253. lua_pushvalue(L, 3);
  254. uart_cbs[uart_id].sent = luaL_ref(L, LUA_REGISTRYINDEX);
  255. }
  256. }
  257. luat_setup_cb(uart_id, uart_cbs[uart_id].received, uart_cbs[uart_id].sent);
  258. return 0;
  259. }
  260. #include "rotable.h"
  261. static const rotable_Reg reg_uart[] =
  262. {
  263. { "setup", l_uart_setup,0},
  264. { "close", l_uart_close,0},
  265. { "write", l_uart_write,0},
  266. { "read", l_uart_read,0},
  267. { "on", l_uart_on, 0},
  268. //校验位
  269. { "Odd", NULL, LUAT_PARITY_ODD},
  270. { "Even", NULL, LUAT_PARITY_EVEN},
  271. { "None", NULL, LUAT_PARITY_NONE},
  272. { "ODD", NULL, LUAT_PARITY_ODD},
  273. { "EVEN", NULL, LUAT_PARITY_EVEN},
  274. { "NONE", NULL, LUAT_PARITY_NONE},
  275. //高低位顺序
  276. { "LSB", NULL, LUAT_BIT_ORDER_LSB},
  277. { "MSB", NULL, LUAT_BIT_ORDER_MSB},
  278. { NULL, NULL , 0}
  279. };
  280. LUAMOD_API int luaopen_uart(lua_State *L)
  281. {
  282. luat_newlib(L, reg_uart);
  283. return 1;
  284. }