luat_uart_air101.c 5.5 KB

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