luat_uart_air105.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #include "luat_base.h"
  22. #include "luat_malloc.h"
  23. #include "luat_msgbus.h"
  24. #include "luat_uart.h"
  25. #include "app_interface.h"
  26. #define LUAT_LOG_TAG "luat.uart"
  27. #include "luat_log.h"
  28. // #define UART_RX_USE_DMA
  29. //串口数量,编号从0开始
  30. #define MAX_DEVICE_COUNT UART_MAX
  31. //存放串口设备句柄
  32. static uint8_t serials_marks[MAX_DEVICE_COUNT+1] ={0};
  33. int luat_uart_exist(int uartid){
  34. if (uartid < 1 || uartid > MAX_DEVICE_COUNT){
  35. return 0;
  36. }
  37. return 1;
  38. }
  39. static int32_t luat_uart_cb(void *pData, void *pParam){
  40. rtos_msg_t msg;
  41. uint32_t uartid = (uint32_t)pData;
  42. uint32_t State = (uint32_t)pParam;
  43. uint32_t len;
  44. // LLOGD("luat_uart_cb pData:%d pParam:%d ",uartid,State);
  45. switch (State){
  46. case UART_CB_TX_BUFFER_DONE:
  47. break;
  48. case UART_CB_TX_ALL_DONE:
  49. msg.handler = l_uart_handler;
  50. msg.arg1 = uartid;
  51. msg.arg2 = 0;
  52. msg.ptr = NULL;
  53. luat_msgbus_put(&msg, 1);
  54. break;
  55. case UART_CB_RX_BUFFER_FULL:
  56. break;
  57. case UART_CB_RX_TIMEOUT:
  58. if (serials_marks[uartid]) return 0;
  59. serials_marks[uartid] = 1;
  60. len = Uart_RxBufferRead(uartid, NULL, 0);
  61. msg.handler = l_uart_handler;
  62. msg.ptr = NULL;
  63. msg.arg1 = uartid;
  64. msg.arg2 = len;
  65. luat_msgbus_put(&msg, 1);
  66. break;
  67. case UART_CB_RX_NEW:
  68. break;
  69. case DMA_CB_DONE:
  70. break;
  71. case UART_CB_ERROR:
  72. break;
  73. case DMA_CB_ERROR:
  74. LLOGD("%x", Uart_GetLastError(uartid));
  75. break;
  76. }
  77. }
  78. static int usb_uart_handler(lua_State *L, void* ptr,int arg1) {
  79. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  80. lua_pop(L, 1);
  81. int uart_id = msg->arg1;
  82. // LLOGD("msg->arg1: %d,msg->arg2: %d",msg->arg1,msg->arg2);
  83. if (msg->arg2 == 1) {
  84. lua_getglobal(L, "sys_pub");
  85. lua_pushstring(L, "USB_UART_INC");
  86. lua_pushinteger(L, uart_id);
  87. lua_pushinteger(L, 1);
  88. lua_call(L, 3, 0);
  89. }
  90. else if(msg->arg2 == 2){
  91. lua_getglobal(L, "sys_pub");
  92. lua_pushstring(L, "USB_UART_INC");
  93. lua_pushinteger(L, uart_id);
  94. lua_pushinteger(L, 0);
  95. lua_call(L, 3, 0);
  96. }
  97. // 给rtos.recv方法返回个空数据
  98. // lua_pushinteger(L, 0);
  99. return 0;
  100. }
  101. static int32_t luat_uart_usb_cb(void *pData, void *pParam){
  102. rtos_msg_t msg = {0};
  103. uint32_t uartid = (uint32_t)pData;
  104. uint32_t State = (uint32_t)pParam;
  105. uint32_t len;
  106. // LLOGD("luat_uart_cb pData:%d pParam:%d ",uartid,State);
  107. switch (State){
  108. case UART_CB_TX_BUFFER_DONE:
  109. break;
  110. case UART_CB_TX_ALL_DONE:
  111. msg.handler = l_uart_handler;
  112. msg.arg1 = 4;
  113. msg.arg2 = 0;
  114. msg.ptr = NULL;
  115. luat_msgbus_put(&msg, 1);
  116. break;
  117. case UART_CB_RX_BUFFER_FULL:
  118. break;
  119. case UART_CB_RX_TIMEOUT:
  120. break;
  121. case UART_CB_RX_NEW:
  122. if (serials_marks[4]) return 0;
  123. serials_marks[4] = 1;
  124. len = Core_VUartRxBufferRead(VIRTUAL_UART0,NULL,0);
  125. msg.handler = l_uart_handler;
  126. msg.ptr = NULL;
  127. msg.arg1 = 4;
  128. msg.arg2 = len;
  129. luat_msgbus_put(&msg, 1);
  130. break;
  131. case DMA_CB_DONE:
  132. break;
  133. case UART_CB_ERROR:
  134. // LLOGI("usb disconnect");
  135. Core_VUartBufferTxStop(VIRTUAL_UART0);
  136. msg.handler = usb_uart_handler;
  137. msg.ptr = NULL;
  138. msg.arg1 = 4;
  139. msg.arg2 = 2;
  140. luat_msgbus_put(&msg, 1);
  141. break;
  142. case UART_CB_CONNECTED:
  143. //对方打开串口工具
  144. msg.handler = usb_uart_handler;
  145. msg.ptr = NULL;
  146. msg.arg1 = 4;
  147. msg.arg2 = 1;
  148. luat_msgbus_put(&msg, 1);
  149. break;
  150. }
  151. }
  152. int luat_uart_setup(luat_uart_t *uart){
  153. int parity = 0;
  154. if (uart->id > MAX_DEVICE_COUNT){
  155. return -1;
  156. }
  157. switch (uart->id){
  158. case UART_ID0:
  159. break;
  160. case UART_ID1:
  161. GPIO_Iomux(GPIOC_01, 3);
  162. GPIO_Iomux(GPIOC_00, 3);
  163. break;
  164. case UART_ID2:
  165. GPIO_Iomux(GPIOD_13, 0);
  166. GPIO_Iomux(GPIOD_12, 0);
  167. break;
  168. case UART_ID3:
  169. GPIO_Iomux(GPIOE_08, 2);
  170. GPIO_Iomux(GPIOE_09, 2);
  171. break;
  172. default:
  173. break;
  174. }
  175. if (uart->parity == 1)parity = UART_PARITY_ODD;
  176. else if (uart->parity == 2)parity = UART_PARITY_EVEN;
  177. int stop_bits = (uart->stop_bits)==1?UART_STOP_BIT1:UART_STOP_BIT2;
  178. if (uart->id == 4)
  179. Core_VUartInit(VIRTUAL_UART0, uart->baud_rate, 1, (uart->data_bits), parity, stop_bits, NULL);
  180. else
  181. Uart_BaseInit(uart->id, uart->baud_rate, 1, (uart->data_bits), parity, stop_bits, NULL);
  182. return 0;
  183. }
  184. int luat_uart_write(int uartid, void *data, size_t length){
  185. //printf("uid:%d,data:%s,length = %d\r\n",uartid, (char *)data, length);
  186. if (!luat_uart_exist(uartid)){
  187. return 0;
  188. }
  189. if (uartid == 4)
  190. Core_VUartBufferTx(VIRTUAL_UART0, (const uint8_t *)data, length);
  191. else
  192. Uart_BufferTx(uartid, (const uint8_t *)data, length);
  193. return length;
  194. }
  195. int luat_uart_read(int uartid, void *buffer, size_t length){
  196. int ret = 0;
  197. if (!luat_uart_exist(uartid)){
  198. return 0;
  199. }
  200. serials_marks[uartid] = 0;
  201. if (uartid == 4)
  202. ret = Core_VUartRxBufferRead(VIRTUAL_UART0, (uint8_t *)buffer,length);
  203. else
  204. ret = Uart_RxBufferRead(uartid, (uint8_t *)buffer,length);
  205. // LLOGD("uartid:%d buffer:%s ",uartid,buffer);
  206. return ret;
  207. }
  208. int luat_uart_close(int uartid){
  209. if (!luat_uart_exist(uartid)){
  210. return 0;
  211. }
  212. if (uartid == 4)
  213. Core_VUartDeInit(VIRTUAL_UART0);
  214. else
  215. Uart_DeInit(uartid);
  216. return 0;
  217. }
  218. int luat_setup_cb(int uartid, int received, int sent){
  219. if (!luat_uart_exist(uartid)){
  220. return -1;
  221. }
  222. if (uartid == 4)
  223. Core_VUartSetCb(VIRTUAL_UART0, luat_uart_usb_cb);
  224. else
  225. Uart_SetCb(uartid, luat_uart_cb);
  226. #ifndef UART_RX_USE_DMA
  227. Uart_EnableRxIrq(uartid);
  228. #endif
  229. return 0;
  230. }