luat_uart_air101.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "luat_base.h"
  2. #include "luat_malloc.h"
  3. #include "luat_msgbus.h"
  4. #include "luat_uart.h"
  5. #define LUAT_LOG_TAG "luat.uart.101"
  6. #include "luat_log.h"
  7. #include "wm_include.h"
  8. #include "wm_uart.h"
  9. #include "wm_gpio_afsel.h"
  10. #include "stdio.h"
  11. //串口数量,编号从0开始
  12. #define MAX_DEVICE_COUNT TLS_UART_MAX
  13. //存放串口设备句柄
  14. static uint8_t serials_buff_len[MAX_DEVICE_COUNT] ={TLS_UART_RX_BUF_SIZE};
  15. extern struct tls_uart_port uart_port[TLS_UART_MAX];
  16. int luat_uart_exist(int uartid)
  17. {
  18. if (uartid < 0 || uartid >= MAX_DEVICE_COUNT)
  19. {
  20. return 0;
  21. }
  22. return 1;
  23. }
  24. static s16 uart_input_cb(u16 len, void* user_data)
  25. {
  26. int uartid = (int)user_data;
  27. //不是fifo超时回调
  28. if(uartid < 100)
  29. {
  30. //未读取长度够不够?
  31. if(CIRC_CNT(uart_port[uartid].recv.head, uart_port[uartid].recv.tail, TLS_UART_RX_BUF_SIZE)
  32. < (serials_buff_len[uartid] - 200))
  33. return 0;
  34. }
  35. else//是fifo超时回调
  36. {
  37. uartid -= 100;
  38. }
  39. rtos_msg_t msg;
  40. msg.handler = l_uart_handler;
  41. msg.ptr = NULL;
  42. msg.arg1 = uartid;
  43. msg.arg2 = CIRC_CNT(uart_port[uartid].recv.head, uart_port[uartid].recv.tail, TLS_UART_RX_BUF_SIZE);
  44. luat_msgbus_put(&msg, 1);
  45. return 0;
  46. }
  47. //串口发送完成事件回调
  48. static s16 uart_sent_cb(struct tls_uart_port *port)
  49. {
  50. tls_uart_free_tx_sent_data(port);
  51. rtos_msg_t msg;
  52. msg.handler = l_uart_handler;
  53. msg.arg1 = port->uart_no;
  54. msg.arg2 = 0;
  55. msg.ptr = NULL;
  56. luat_msgbus_put(&msg, 1);
  57. return 0;
  58. }
  59. int luat_uart_setup(luat_uart_t *uart)
  60. {
  61. int ret;
  62. tls_uart_options_t opt = {0};
  63. if (!luat_uart_exist(uart->id))
  64. {
  65. return -1;
  66. }
  67. opt.baudrate = uart->baud_rate;
  68. opt.charlength = (uart->data_bits)-5;
  69. opt.paritytype = uart->parity;
  70. opt.flow_ctrl = TLS_UART_FLOW_CTRL_NONE;
  71. opt.stopbits = (uart->stop_bits)-1;
  72. switch (uart->id)
  73. {
  74. case TLS_UART_0:
  75. wm_uart0_rx_config(WM_IO_PB_20);
  76. wm_uart0_tx_config(WM_IO_PB_19);
  77. break;
  78. case TLS_UART_1:
  79. wm_uart1_rx_config(WM_IO_PB_07);
  80. wm_uart1_tx_config(WM_IO_PB_06);
  81. break;
  82. case TLS_UART_2:
  83. wm_uart2_rx_config(WM_IO_PB_03);
  84. wm_uart2_tx_scio_config(WM_IO_PB_02);
  85. break;
  86. case TLS_UART_3:
  87. wm_uart3_rx_config(WM_IO_PB_01);
  88. wm_uart3_tx_config(WM_IO_PB_00);
  89. break;
  90. case TLS_UART_4:
  91. wm_uart4_rx_config(WM_IO_PB_05);
  92. wm_uart4_tx_config(WM_IO_PB_04);
  93. break;
  94. #ifdef AIR103
  95. case TLS_UART_5:
  96. wm_uart5_rx_config(WM_IO_PA_13);
  97. wm_uart5_tx_config(WM_IO_PA_12);
  98. break;
  99. #endif
  100. default:
  101. break;
  102. }
  103. ret = tls_uart_port_init(uart->id, &opt, 0);
  104. if (ret != WM_SUCCESS)
  105. {
  106. return ret; //初始化失败
  107. }
  108. if(uart->bufsz > TLS_UART_RX_BUF_SIZE)
  109. uart->bufsz = TLS_UART_RX_BUF_SIZE;
  110. if(uart->bufsz < 1024)
  111. uart->bufsz = 1024;
  112. serials_buff_len[uart->id] = uart->bufsz;
  113. return ret;
  114. }
  115. int luat_uart_write(int uartid, void *data, size_t length)
  116. {
  117. int ret = 0;
  118. //printf("uid:%d,data:%s,length = %d\r\n",uartid, (char *)data, length);
  119. if (!luat_uart_exist(uartid))
  120. {
  121. return 0;
  122. }
  123. ret = tls_uart_write(uartid, data,length);
  124. ret = (ret == 0) ? length : 0;
  125. return ret;
  126. }
  127. int luat_uart_read(int uartid, void *buffer, size_t length)
  128. {
  129. int ret = 0;
  130. if (!luat_uart_exist(uartid))
  131. {
  132. return 0;
  133. }
  134. ret = tls_uart_read(uartid,(u8 *) buffer,(u16)length);
  135. return ret;
  136. }
  137. int luat_uart_close(int uartid)
  138. {
  139. if (!luat_uart_exist(uartid))
  140. {
  141. return 0;
  142. }
  143. // tls_uart_port_init(uartid,NULL,0);
  144. return 0;
  145. }
  146. int luat_setup_cb(int uartid, int received, int sent)
  147. {
  148. if (!luat_uart_exist(uartid))
  149. {
  150. return -1;
  151. }
  152. if (received)
  153. {
  154. tls_uart_rx_callback_register(uartid,(s16(*)(u16, void*))uart_input_cb, (void*)uartid);
  155. }
  156. if (sent)
  157. {
  158. tls_uart_tx_callback_register(uartid, (s16(*)(struct tls_uart_port *))uart_sent_cb);
  159. }
  160. return 0;
  161. }