luat_uart_air101.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. if (uart_port[port->uart_no].rs480.rs485_param_bit.is_485used){
  58. luat_timer_mdelay(port->uart_cb_len);
  59. port->uart_cb_len = 0;
  60. if (uart_port[port->uart_no].rs480.rs485_param_bit.wait_time)
  61. luat_timer_us_delay(uart_port[port->uart_no].rs480.rs485_param_bit.wait_time);
  62. luat_gpio_set(uart_port[port->uart_no].rs480.rs485_pin, uart_port[port->uart_no].rs480.rs485_param_bit.rx_level);
  63. }
  64. return 0;
  65. }
  66. int luat_uart_setup(luat_uart_t *uart)
  67. {
  68. int ret;
  69. tls_uart_options_t opt = {0};
  70. if (!luat_uart_exist(uart->id))
  71. {
  72. return -1;
  73. }
  74. opt.baudrate = uart->baud_rate;
  75. opt.charlength = (uart->data_bits)-5;
  76. opt.paritytype = uart->parity;
  77. opt.flow_ctrl = TLS_UART_FLOW_CTRL_NONE;
  78. opt.stopbits = (uart->stop_bits)-1;
  79. switch (uart->id)
  80. {
  81. case TLS_UART_0:
  82. wm_uart0_rx_config(WM_IO_PB_20);
  83. wm_uart0_tx_config(WM_IO_PB_19);
  84. break;
  85. case TLS_UART_1:
  86. wm_uart1_rx_config(WM_IO_PB_07);
  87. wm_uart1_tx_config(WM_IO_PB_06);
  88. break;
  89. case TLS_UART_2:
  90. wm_uart2_rx_config(WM_IO_PB_03);
  91. wm_uart2_tx_scio_config(WM_IO_PB_02);
  92. break;
  93. case TLS_UART_3:
  94. wm_uart3_rx_config(WM_IO_PB_01);
  95. wm_uart3_tx_config(WM_IO_PB_00);
  96. break;
  97. case TLS_UART_4:
  98. wm_uart4_rx_config(WM_IO_PB_05);
  99. wm_uart4_tx_config(WM_IO_PB_04);
  100. break;
  101. #ifdef AIR103
  102. case TLS_UART_5:
  103. wm_uart5_rx_config(WM_IO_PA_13);
  104. wm_uart5_tx_config(WM_IO_PA_12);
  105. break;
  106. #endif
  107. default:
  108. break;
  109. }
  110. ret = tls_uart_port_init(uart->id, &opt, 0);
  111. if (ret != WM_SUCCESS)
  112. {
  113. return ret; //初始化失败
  114. }
  115. if(uart->bufsz > TLS_UART_RX_BUF_SIZE)
  116. uart->bufsz = TLS_UART_RX_BUF_SIZE;
  117. if(uart->bufsz < 1024)
  118. uart->bufsz = 1024;
  119. serials_buff_len[uart->id] = uart->bufsz;
  120. uart_port[uart->id].rs480.rs485_param_bit.is_485used = (uart->pin485 > WM_IO_PB_31)?0:1;
  121. uart_port[uart->id].rs480.rs485_pin = uart->pin485;
  122. uart_port[uart->id].rs480.rs485_param_bit.rx_level = uart->rx_level;
  123. uart_port[uart->id].rs480.rs485_param_bit.wait_time = uart->delay;
  124. if (uart_port[uart->id].rs480.rs485_param_bit.is_485used){
  125. luat_gpio_mode(uart_port[uart->id].rs480.rs485_pin, 0, 0, uart_port[uart->id].rs480.rs485_param_bit.rx_level);
  126. }
  127. return ret;
  128. }
  129. int luat_uart_write(int uartid, void *data, size_t length)
  130. {
  131. int ret = 0;
  132. //printf("uid:%d,data:%s,length = %d\r\n",uartid, (char *)data, length);
  133. if (!luat_uart_exist(uartid))return 0;
  134. if (uart_port[uartid].rs480.rs485_param_bit.is_485used) luat_gpio_set(uart_port[uartid].rs480.rs485_pin, !uart_port[uartid].rs480.rs485_param_bit.rx_level);
  135. ret = tls_uart_write(uartid, data,length);
  136. uart_port[uartid].uart_cb_len = length;
  137. ret = (ret == 0) ? length : 0;
  138. return ret;
  139. }
  140. int luat_uart_read(int uartid, void *buffer, size_t length)
  141. {
  142. int ret = 0;
  143. if (!luat_uart_exist(uartid))
  144. {
  145. return 0;
  146. }
  147. ret = tls_uart_read(uartid,(u8 *) buffer,(u16)length);
  148. return ret;
  149. }
  150. int luat_uart_close(int uartid)
  151. {
  152. if (!luat_uart_exist(uartid))return 0;
  153. uart_port[uartid].rs480.rs485_param_bit.is_485used = 0;
  154. // tls_uart_port_init(uartid,NULL,0);
  155. return 0;
  156. }
  157. int luat_setup_cb(int uartid, int received, int sent)
  158. {
  159. if (!luat_uart_exist(uartid))
  160. {
  161. return -1;
  162. }
  163. if (received)
  164. {
  165. tls_uart_rx_callback_register(uartid,(s16(*)(u16, void*))uart_input_cb, (void*)uartid);
  166. }
  167. if (sent)
  168. {
  169. tls_uart_tx_sent_callback_register(uartid, (s16(*)(struct tls_uart_port *))uart_sent_cb);
  170. }
  171. return 0;
  172. }
  173. int luat_uart_wait_485_tx_done(int uartid){
  174. int cnt = 0;
  175. if (luat_uart_exist(uartid)){
  176. if (uart_port[uartid].rs480.rs485_param_bit.is_485used){
  177. if (uart_port[uartid].rs480.rs485_param_bit.wait_time)
  178. luat_timer_us_delay(uart_port[uartid].rs480.rs485_param_bit.wait_time);
  179. luat_gpio_set(uart_port[uartid].rs480.rs485_pin, uart_port[uartid].rs480.rs485_param_bit.rx_level);
  180. }
  181. }
  182. return cnt;
  183. }