luat_lib_uart.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. @module uart
  3. @summary 串口操作库
  4. @version 1.0
  5. @date 2020.03.30
  6. @demo uart
  7. @video https://www.bilibili.com/video/BV1er4y1p75y
  8. */
  9. #include "luat_base.h"
  10. #include "luat_uart.h"
  11. #include "luat_malloc.h"
  12. #include "luat_msgbus.h"
  13. #include "luat_fs.h"
  14. #include "string.h"
  15. #include "luat_zbuff.h"
  16. #define LUAT_LOG_TAG "uart"
  17. #include "luat_log.h"
  18. #define MAX_DEVICE_COUNT 10
  19. typedef struct luat_uart_cb {
  20. int received;//回调函数
  21. int sent;//回调函数
  22. } luat_uart_cb_t;
  23. static luat_uart_cb_t uart_cbs[MAX_DEVICE_COUNT];
  24. int l_uart_handler(lua_State *L, void* ptr) {
  25. //LLOGD("l_uart_handler");
  26. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  27. lua_pop(L, 1);
  28. int uart_id = msg->arg1;
  29. if (!luat_uart_exist(uart_id)) {
  30. //LLOGW("not exist uart id=%ld but event fired?!", uart_id);
  31. return 0;
  32. }
  33. // sent event
  34. if (msg->arg2 == 0) {
  35. //LLOGD("uart%ld sent callback", uart_id);
  36. if (uart_cbs[uart_id].sent) {
  37. lua_geti(L, LUA_REGISTRYINDEX, uart_cbs[uart_id].sent);
  38. if (lua_isfunction(L, -1)) {
  39. lua_pushinteger(L, uart_id);
  40. lua_call(L, 1, 0);
  41. }
  42. }
  43. }
  44. else {
  45. if (uart_cbs[uart_id].received) {
  46. lua_geti(L, LUA_REGISTRYINDEX, uart_cbs[uart_id].received);
  47. if (lua_isfunction(L, -1)) {
  48. lua_pushinteger(L, uart_id);
  49. lua_pushinteger(L, msg->arg2);
  50. lua_call(L, 2, 0);
  51. }
  52. else {
  53. //LLOGD("uart%ld received callback not function", uart_id);
  54. }
  55. }
  56. else {
  57. //LLOGD("uart%ld no received callback", uart_id);
  58. }
  59. }
  60. // 给rtos.recv方法返回个空数据
  61. lua_pushinteger(L, 0);
  62. return 1;
  63. }
  64. /*
  65. 配置串口参数
  66. @api uart.setup(id, baud_rate, data_bits, stop_bits, partiy, bit_order, buff_size)
  67. @int 串口id, uart0写0, uart1写1, 如此类推, 最大值取决于设备
  68. @int 波特率, 默认115200,可选择波特率表:{2000000,921600,460800,230400,115200,57600,38400,19200,9600,4800,2400}
  69. @int 数据位,默认为8, 可选 7/8
  70. @int 停止位,默认为1, 可选 0/1, 有部分特殊芯片会支持1.5,使用2代表.
  71. @int 校验位,可选 uart.None/uart.Even/uart.Odd
  72. @int 大小端,默认小端 uart.LSB, 可选 uart.MSB
  73. @int 缓冲区大小,默认值1024
  74. @int 485模式下的转换GPIO, 默认值0xffffffff
  75. @int 485模式下的rx方向GPIO的电平, 默认值0
  76. @int 485模式下tx向rx转换的延迟时间,默认值12bit的时间,单位us
  77. @return int 成功返回0,失败返回其他值
  78. @usage
  79. -- 最常用115200 8N1
  80. uart.setup(1, 115200, 8, 1, uart.NONE)
  81. -- 可以简写为 uart.setup(1)
  82. */
  83. static int l_uart_setup(lua_State *L)
  84. {
  85. lua_Number stop_bits = luaL_optnumber(L, 4, 1);
  86. luat_uart_t uart_config = {
  87. .id = luaL_checkinteger(L, 1),
  88. .baud_rate = luaL_optinteger(L, 2, 115200),
  89. .data_bits = luaL_optinteger(L, 3, 8),
  90. .parity = luaL_optinteger(L, 5, LUAT_PARITY_NONE),
  91. .bit_order = luaL_optinteger(L, 6, LUAT_BIT_ORDER_LSB),
  92. .bufsz = luaL_optinteger(L, 7, 1024),
  93. .pin485 = luaL_optinteger(L, 8, 0xffffffff),
  94. .rx_level = luaL_optinteger(L, 9, 0),
  95. };
  96. if(stop_bits == 0.5)
  97. uart_config.stop_bits = LUAT_0_5_STOP_BITS;
  98. else if(stop_bits == 1.5)
  99. uart_config.stop_bits = LUAT_1_5_STOP_BITS;
  100. else
  101. uart_config.stop_bits = (uint8_t)stop_bits;
  102. uart_config.delay = luaL_optinteger(L, 10, 12000000/uart_config.baud_rate);
  103. int result = luat_uart_setup(&uart_config);
  104. lua_pushinteger(L, result);
  105. return 1;
  106. }
  107. /*
  108. 写串口
  109. @api uart.write(id, data)
  110. @int 串口id, uart0写0, uart1写1
  111. @string/zbuff 待写入的数据,如果是zbuff会从指针起始位置开始读
  112. @int 可选,要发送的数据长度,默认全发
  113. @return int 成功的数据长度
  114. @usage
  115. -- 写入可见字符串
  116. uart.write(1, "rdy\r\n")
  117. -- 写入十六进制的数据串
  118. uart.write(1, string.char(0x55,0xAA,0x4B,0x03,0x86))
  119. */
  120. static int l_uart_write(lua_State *L)
  121. {
  122. size_t len;
  123. const char *buf;
  124. uint8_t id = luaL_checkinteger(L, 1);
  125. if(lua_isuserdata(L, 2))
  126. {
  127. luat_zbuff_t *buff = ((luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
  128. len = buff->len - buff->cursor;
  129. buf = (const char *)(buff->addr + buff->cursor);
  130. }
  131. else
  132. {
  133. buf = lua_tolstring(L, 2, &len);//取出字符串数据
  134. }
  135. if(lua_isinteger(L, 3))
  136. {
  137. size_t l = luaL_checkinteger(L, 3);
  138. if(len > l)
  139. len = l;
  140. }
  141. int result = luat_uart_write(id, (char*)buf, len);
  142. lua_pushinteger(L, result);
  143. return 1;
  144. }
  145. /*
  146. buff形式写串口,等同于c语言uart_tx(uart_id, &buff[start], len);
  147. @api uart.tx(id, buff, start, len)
  148. @int 串口id, uart0写0, uart1写1
  149. @zbuff 待写入的数据,如果是zbuff会从指针起始位置开始读
  150. @int 可选,要发送的数据起始位置,默认为0
  151. @int 可选,要发送的数据长度,默认为zbuff内有效数据,最大值不超过zbuff的最大空间
  152. @return int 成功的数据长度
  153. @usage
  154. uart.tx(1, buf)
  155. */
  156. static int l_uart_tx(lua_State *L)
  157. {
  158. size_t start, len;
  159. // const char *buf;
  160. luat_zbuff_t *buff;
  161. uint8_t id = luaL_checkinteger(L, 1);
  162. if(lua_isuserdata(L, 2))
  163. {
  164. buff = ((luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
  165. }
  166. else
  167. {
  168. lua_pushinteger(L, 0);
  169. return 1;
  170. }
  171. start = luaL_optinteger(L, 3, 0);
  172. len = luaL_optinteger(L, 4, buff->used);
  173. if (start >= buff->len)
  174. {
  175. lua_pushinteger(L, 0);
  176. return 1;
  177. }
  178. if ((start + len)>= buff->len)
  179. {
  180. len = buff->len - start;
  181. }
  182. int result = luat_uart_write(id, buff->addr + start, len);
  183. lua_pushinteger(L, result);
  184. return 1;
  185. }
  186. /*
  187. 读串口
  188. @api uart.read(id, len)
  189. @int 串口id, uart0写0, uart1写1
  190. @int 读取长度
  191. @file/zbuff 可选:文件句柄或zbuff对象
  192. @return string 读取到的数据 / 传入zbuff时,返回读到的长度,并把zbuff指针后移
  193. @usage
  194. uart.read(1, 16)
  195. */
  196. static int l_uart_read(lua_State *L)
  197. {
  198. uint8_t id = luaL_checkinteger(L, 1);
  199. uint32_t length = luaL_optinteger(L, 2, 1024);
  200. if(lua_isuserdata(L, 3)){//zbuff对象特殊处理
  201. luat_zbuff_t *buff = ((luat_zbuff_t *)luaL_checkudata(L, 3, LUAT_ZBUFF_TYPE));
  202. uint8_t* recv = buff->addr+buff->cursor;
  203. if(length > buff->len - buff->cursor)
  204. length = buff->len - buff->cursor;
  205. int result = luat_uart_read(id, recv, length);
  206. if(result < 0)
  207. result = 0;
  208. buff->cursor += result;
  209. lua_pushinteger(L, result);
  210. return 1;
  211. }
  212. if (length < 512)
  213. length = 512;
  214. uint8_t* recv = luat_heap_malloc(length);
  215. if (recv == NULL) {
  216. LLOGE("system is out of memory!!!");
  217. lua_pushstring(L, "");
  218. return 1;
  219. }
  220. uint32_t read_length = 0;
  221. while(read_length < length)//循环读完
  222. {
  223. int result = luat_uart_read(id, (void*)(recv + read_length), length - read_length);
  224. if (result > 0) {
  225. read_length += result;
  226. }
  227. else
  228. {
  229. break;
  230. }
  231. }
  232. if(read_length > 0)
  233. {
  234. if (lua_isinteger(L, 3)) {
  235. uint32_t fd = luaL_checkinteger(L, 3);
  236. luat_fs_fwrite(recv, 1, read_length, (FILE*)fd);
  237. }
  238. else {
  239. lua_pushlstring(L, (const char*)recv, read_length);
  240. }
  241. }
  242. else
  243. {
  244. lua_pushstring(L, "");
  245. }
  246. luat_heap_free(recv);
  247. return 1;
  248. }
  249. /*
  250. buff形式读串口,一次读出全部数据存入buff中,如果buff空间不够会自动扩展,目前只有air105支持这个操作
  251. @api uart.rx(id, buff)
  252. @int 串口id, uart0写0, uart1写1
  253. @zbuff zbuff对象
  254. @return int 返回读到的长度,并把zbuff指针后移
  255. @usage
  256. uart.rx(1, buff)
  257. */
  258. static int l_uart_rx(lua_State *L)
  259. {
  260. uint8_t id = luaL_checkinteger(L, 1);
  261. if(lua_isuserdata(L, 2)){//zbuff对象特殊处理
  262. luat_zbuff_t *buff = ((luat_zbuff_t *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
  263. int result = luat_uart_read(id, NULL, 0); //读出当前缓存的长度,目前只有105支持这个操作
  264. if (result > (buff->len - buff->used))
  265. {
  266. __zbuff_resize(buff, buff->len + result);
  267. }
  268. luat_uart_read(id, buff->addr + buff->used, result);
  269. lua_pushinteger(L, result);
  270. buff->used += result;
  271. return 1;
  272. }
  273. else
  274. {
  275. lua_pushinteger(L, 0);
  276. return 1;
  277. }
  278. return 1;
  279. }
  280. /*
  281. 读串口Rx缓存中剩余数据量,目前只有air105支持这个操作
  282. @api uart.rx_size(id)
  283. @int 串口id, uart0写0, uart1写1
  284. @return int 返回读到的长度
  285. @usage
  286. local size = uart.rx_size(1)
  287. */
  288. static int l_uart_rx_size(lua_State *L)
  289. {
  290. uint8_t id = luaL_checkinteger(L, 1);
  291. lua_pushinteger(L, luat_uart_read(id, NULL, 0));//读出当前缓存的长度,目前只有105支持这个操作
  292. return 1;
  293. }
  294. /*
  295. 关闭串口
  296. @api uart.close(id)
  297. @int 串口id, uart0写0, uart1写1
  298. @return nil 无返回值
  299. @usage
  300. uart.close(1)
  301. */
  302. static int l_uart_close(lua_State *L)
  303. {
  304. uint8_t result = luat_uart_close(luaL_checkinteger(L, 1));
  305. lua_pushinteger(L, result);
  306. return 1;
  307. }
  308. /*
  309. 注册串口事件回调
  310. @api uart.on(id, event, func)
  311. @int 串口id, uart0写0, uart1写1
  312. @string 事件名称
  313. @function 回调方法
  314. @return nil 无返回值
  315. @usage
  316. uart.on(1, "receive", function(id, len)
  317. local data = uart.read(id, len)
  318. log.info("uart", id, len, data)
  319. end)
  320. */
  321. static int l_uart_on(lua_State *L) {
  322. int uart_id = luaL_checkinteger(L, 1);
  323. if (!luat_uart_exist(uart_id)) {
  324. lua_pushliteral(L, "no such uart id");
  325. return 1;
  326. }
  327. const char* event = luaL_checkstring(L, 2);
  328. if (!strcmp("receive", event) || !strcmp("recv", event)) {
  329. if (uart_cbs[uart_id].received != 0) {
  330. luaL_unref(L, LUA_REGISTRYINDEX, uart_cbs[uart_id].received);
  331. uart_cbs[uart_id].received = 0;
  332. }
  333. if (lua_isfunction(L, 3)) {
  334. lua_pushvalue(L, 3);
  335. uart_cbs[uart_id].received = luaL_ref(L, LUA_REGISTRYINDEX);
  336. }
  337. }
  338. else if (!strcmp("sent", event)) {
  339. if (uart_cbs[uart_id].sent != 0) {
  340. luaL_unref(L, LUA_REGISTRYINDEX, uart_cbs[uart_id].sent);
  341. uart_cbs[uart_id].sent = 0;
  342. }
  343. if (lua_isfunction(L, 3)) {
  344. lua_pushvalue(L, 3);
  345. uart_cbs[uart_id].sent = luaL_ref(L, LUA_REGISTRYINDEX);
  346. }
  347. }
  348. luat_setup_cb(uart_id, uart_cbs[uart_id].received, uart_cbs[uart_id].sent);
  349. return 0;
  350. }
  351. /*
  352. 等待485模式下TX完成,mcu不支持串口发送移位寄存器空或者类似中断时才需要,在sent事件回调后使用
  353. @api uart.wait485(id)
  354. @int 串口id, uart0写0, uart1写1
  355. @return int 等待了多少次循环才等到tx完成,用于粗劣的观察delay时间是否足够,返回不为0说明还需要放大delay
  356. */
  357. static int l_uart_wait485_tx_done(lua_State *L) {
  358. int uart_id = luaL_checkinteger(L, 1);
  359. if (!luat_uart_exist(uart_id)) {
  360. lua_pushinteger(L, 0);
  361. return 1;
  362. }
  363. #ifdef LUAT__UART_TX_NEED_WAIT_DONE
  364. lua_pushinteger(L, luat_uart_wait_485_tx_done(uart_id));
  365. #else
  366. lua_pushinteger(L, 0);
  367. #endif
  368. return 1;
  369. }
  370. /*
  371. 检查串口号是否存在
  372. @api uart.exist(id)
  373. @int 串口id, uart0写0, uart1写1, 如此类推
  374. @return bool 存在返回true
  375. */
  376. static int l_uart_exist(lua_State *L)
  377. {
  378. lua_pushboolean(L, luat_uart_exist(luaL_checkinteger(L,1)));
  379. return 1;
  380. }
  381. /*
  382. 获取可用串口号列表,当前仅限win32
  383. @api uart.list(max)
  384. @int 可选,默认256,最多获取多少个串口
  385. @return table 获取到的可用串口号列表
  386. */
  387. #ifdef LUAT_FORCE_WIN32
  388. static int l_uart_list(lua_State *L)
  389. {
  390. size_t len = luaL_optinteger(L,1,256);
  391. lua_newtable(L);//返回用的table
  392. uint8_t* buff = (uint8_t*)luat_heap_malloc(len);
  393. if (!buff)
  394. return 1;
  395. int rlen = luat_uart_list(buff, len);
  396. for(int i = 0;i<rlen;i++)
  397. {
  398. lua_pushinteger(L,i+1);
  399. lua_pushinteger(L,buff[i]);
  400. lua_settable(L,-3);
  401. }
  402. luat_heap_free(buff);
  403. return 1;
  404. }
  405. #endif
  406. #include "rotable2.h"
  407. static const rotable_Reg_t reg_uart[] =
  408. {
  409. { "setup", ROREG_FUNC(l_uart_setup)},
  410. { "close", ROREG_FUNC(l_uart_close)},
  411. { "write", ROREG_FUNC(l_uart_write)},
  412. { "read", ROREG_FUNC(l_uart_read)},
  413. { "on", ROREG_FUNC(l_uart_on)},
  414. { "wait485", ROREG_FUNC(l_uart_wait485_tx_done)},
  415. { "exist", ROREG_FUNC(l_uart_exist)},
  416. #ifdef LUAT_FORCE_WIN32
  417. { "list", ROREG_FUNC(l_uart_list)},
  418. #endif
  419. //校验位
  420. { "Odd", ROREG_INT(LUAT_PARITY_ODD)},
  421. { "Even", ROREG_INT(LUAT_PARITY_EVEN)},
  422. { "None", ROREG_INT(LUAT_PARITY_NONE)},
  423. //@const ODD number 奇校验
  424. { "ODD", ROREG_INT(LUAT_PARITY_ODD)},
  425. //@const EVEN number 偶校验
  426. { "EVEN", ROREG_INT(LUAT_PARITY_EVEN)},
  427. //@const NONE number 无校验
  428. { "NONE", ROREG_INT(LUAT_PARITY_NONE)},
  429. //高低位顺序
  430. //@const LSB number 小端模式
  431. { "LSB", ROREG_INT(LUAT_BIT_ORDER_LSB)},
  432. //@const MSB number 大端模式
  433. { "MSB", ROREG_INT(LUAT_BIT_ORDER_MSB)},
  434. { "tx", ROREG_FUNC(l_uart_tx)},
  435. { "rx", ROREG_FUNC(l_uart_rx)},
  436. { "rx_size", ROREG_FUNC(l_uart_rx_size)},
  437. //@const VUART_0 number 虚拟串口0
  438. { "VUART_0", ROREG_INT(LUAT_VUART_ID_0)},
  439. { NULL, ROREG_INT(0) }
  440. };
  441. LUAMOD_API int luaopen_uart(lua_State *L)
  442. {
  443. luat_newlib2(L, reg_uart);
  444. return 1;
  445. }