luat_uart.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "luat_base.h"
  2. #include "luat_malloc.h"
  3. #include "luat_msgbus.h"
  4. #include "luat_uart.h"
  5. #include "luat_log.h"
  6. #include "stdio.h"
  7. #include "pico/stdlib.h"
  8. #include "hardware/uart.h"
  9. //串口数量,编号从0开始
  10. #define MAX_DEVICE_COUNT 2
  11. //存放串口设备句柄
  12. static uint8_t serials_marks[MAX_DEVICE_COUNT] ={0};
  13. int luat_uart_exist(int uartid)
  14. {
  15. if (uartid < 0 || uartid >= MAX_DEVICE_COUNT)
  16. {
  17. return 0;
  18. }
  19. return 1;
  20. }
  21. int uart_input_cb(uint16_t len, void *uartid)
  22. {
  23. if (serials_marks[(int)uartid])
  24. {
  25. // 前一个回调都还没读呢
  26. return 0;
  27. }
  28. serials_marks[(int)uartid] = 1;
  29. rtos_msg_t msg;
  30. msg.handler = l_uart_handler;
  31. msg.ptr = NULL;
  32. msg.arg1 = (int)uartid;
  33. msg.arg2 = len;
  34. luat_msgbus_put(&msg, 1);
  35. return 0;
  36. }
  37. //串口发送完成事件回调
  38. // int uart_sent_cb(struct tls_uart_port *port)
  39. // {
  40. // rtos_msg_t msg;
  41. // msg.handler = l_uart_handler;
  42. // msg.arg1 = port->uart_no;
  43. // msg.arg2 = 0;
  44. // msg.ptr = NULL;
  45. // luat_msgbus_put(&msg, 1);
  46. // return 0;
  47. // }
  48. int luat_uart_setup(luat_uart_t *uart)
  49. {
  50. int ret;
  51. // tls_uart_options_t opt;
  52. if (uart->id > MAX_DEVICE_COUNT)
  53. {
  54. return -1;
  55. }
  56. switch (uart->id)
  57. {
  58. case 0:
  59. uart_init(uart0, 115200);
  60. gpio_set_function(0, GPIO_FUNC_UART);
  61. gpio_set_function(1, GPIO_FUNC_UART);
  62. uart_set_baudrate(uart0, uart->baud_rate);
  63. uart_set_hw_flow(uart0, false, false);
  64. uart_set_format(uart0, uart->data_bits, uart->stop_bits, uart->parity);
  65. uart_set_fifo_enabled(uart0, false);
  66. break;
  67. case 1:
  68. uart_init(uart1, 115200);
  69. gpio_set_function(4, GPIO_FUNC_UART);
  70. gpio_set_function(5, GPIO_FUNC_UART);
  71. uart_set_baudrate(uart1, uart->baud_rate);
  72. uart_set_hw_flow(uart1, false, false);
  73. uart_set_format(uart1, uart->data_bits, uart->stop_bits, uart->parity);
  74. uart_set_fifo_enabled(uart1, false);
  75. break;
  76. default:
  77. break;
  78. }
  79. return 0;
  80. }
  81. int luat_uart_write(int uartid, void *data, size_t length)
  82. {
  83. int ret;
  84. // printf("uid:%d,data:%s,length = %d\r\n",uartid, (char *)data, length);
  85. if (!luat_uart_exist(uartid))
  86. {
  87. }
  88. // ret = tls_uart_write(uartid, data,length);
  89. if (uartid == 0)
  90. uart_puts(uart0, data);
  91. else if(uartid == 1)
  92. uart_puts(uart1, data);
  93. return ret;
  94. }
  95. int luat_uart_read(int uartid, void *buffer, size_t length)
  96. {
  97. int ret;
  98. if (!luat_uart_exist(uartid))
  99. {
  100. return -1;
  101. }
  102. serials_marks[uartid] = 0;
  103. // ret = tls_uart_read(uartid,(u8 *) buffer,(u16)length);
  104. // return ret;
  105. }
  106. int luat_uart_close(int uartid)
  107. {
  108. if (!luat_uart_exist(uartid))
  109. {
  110. return 0;
  111. }
  112. if (uartid == 0)
  113. uart_deinit(uart0);
  114. else if(uartid == 1)
  115. uart_deinit(uart1);
  116. return 0;
  117. }
  118. int luat_setup_cb(int uartid, int received, int sent)
  119. {
  120. if (!luat_uart_exist(uartid))
  121. {
  122. return -1;
  123. }
  124. if (received)
  125. {
  126. // tls_uart_rx_callback_register(uartid,(s16(*)(u16, void*))uart_input_cb, &uartid);
  127. }
  128. if (sent)
  129. {
  130. // tls_uart_tx_callback_register(uartid, (s16(*)(struct tls_uart_port *))uart_sent_cb);
  131. }
  132. return 0;
  133. }