luat_uart_rtt.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 "rtthread.h"
  7. #include <rtdevice.h>
  8. #include "drivers/serial.h"
  9. #define DBG_TAG "rtt.uart"
  10. #define DBG_LVL DBG_WARNING
  11. #include <rtdbg.h>
  12. //串口数量,编号从0开始
  13. #define MAX_DEVICE_COUNT 10
  14. //存放串口设备句柄
  15. static rt_device_t serials[MAX_DEVICE_COUNT];
  16. static uint8_t serials_marks[MAX_DEVICE_COUNT];
  17. static int luat_uart_rtt_init() {
  18. char name[8];
  19. name[0] = 'u';
  20. name[1] = 'a';
  21. name[2] = 'r';
  22. name[3] = 't';
  23. name[5] = 0;
  24. // 搜索uart0, uart1, ....
  25. for (size_t i = 0; i < MAX_DEVICE_COUNT; i++)
  26. {
  27. name[4] = '0' + i;
  28. serials[i] = rt_device_find(name);
  29. LOG_I("uart device dev=0x%08X uart.id=%ld", serials[i], i);
  30. }
  31. }
  32. INIT_COMPONENT_EXPORT(luat_uart_rtt_init);
  33. static int get_uart_id(rt_device_t dev) {
  34. int i;
  35. for(i=0;i<MAX_DEVICE_COUNT;i++)
  36. {
  37. if (serials[i] == dev) {
  38. //LOG_I("uart device dev->id=%d uart.id=%ld", dev->device_id, i);
  39. return i;
  40. }
  41. }
  42. LOG_W("not uart device dev=0x%08X", dev);
  43. return -1;
  44. }
  45. int luat_uart_exist(int uartid) {
  46. if (uartid < 0 || uartid >= MAX_DEVICE_COUNT) {
  47. return 0;
  48. }
  49. return serials[uartid] ? 1 : 0;
  50. }
  51. //接收数据回调
  52. static rt_err_t uart_input_cb(rt_device_t dev, rt_size_t size)
  53. {
  54. int uart_id = get_uart_id(dev);
  55. LOG_I("uart receive rtt cb, id=%ld", uart_id);
  56. if (uart_id < 0) {
  57. return RT_EOK;
  58. }
  59. if (serials_marks[uart_id]) {
  60. // 前一个回调都还没读呢
  61. return RT_EOK;
  62. }
  63. serials_marks[uart_id] = 1;
  64. rtos_msg_t msg;
  65. msg.handler = l_uart_handler;
  66. msg.ptr = RT_NULL;
  67. msg.arg1 = uart_id;
  68. msg.arg2 = size;
  69. luat_msgbus_put(&msg, 1);
  70. return RT_EOK;
  71. }
  72. //串口发送完成事件回调
  73. static rt_err_t uart_sent_cb(rt_device_t dev, void *buffer)
  74. {
  75. int uart_id = get_uart_id(dev);
  76. LOG_I("uart sent rtt cb, id=%ld", uart_id);
  77. if (uart_id < 0) {
  78. return RT_EOK;
  79. }
  80. rtos_msg_t msg;
  81. msg.handler = l_uart_handler;
  82. msg.arg1 = uart_id;
  83. msg.arg2 = 0;
  84. msg.ptr = buffer;
  85. luat_msgbus_put(&msg, 1);
  86. return RT_EOK;
  87. }
  88. int luat_uart_setup(luat_uart_t* uart)
  89. {
  90. if(uart->id > MAX_DEVICE_COUNT)
  91. {
  92. return -1;
  93. }
  94. rt_device_t dev = serials[uart->id];
  95. if (dev == RT_NULL) {
  96. return -2;
  97. }
  98. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  99. config.baud_rate = uart->baud_rate;
  100. config.data_bits = uart->data_bits;
  101. config.stop_bits = uart->stop_bits - 1;
  102. //config.bufsz = uart->bufsz;
  103. config.bufsz = 512;
  104. config.parity = uart->parity;
  105. config.bit_order = uart->bit_order;
  106. rt_device_control(dev, RT_DEVICE_CTRL_CONFIG, &config);
  107. rt_err_t re = rt_device_open(dev, RT_DEVICE_FLAG_INT_RX);
  108. if(re != RT_EOK)
  109. return re;//失败了
  110. return re;
  111. }
  112. #ifdef FALSE123
  113. #include "wm_uart.h"
  114. int tls_uart_dma_write(char *buf, u16 writesize, void (*cmpl_callback) (void *p), u16 uart_no);
  115. #endif
  116. int luat_uart_write(int uartid, void* data, size_t length)
  117. {
  118. if(!luat_uart_exist(uartid)) {
  119. LOG_W("uart id=%d not exist", uartid);
  120. return -1;
  121. }
  122. if (serials[uartid]->open_flag == 0) {
  123. LOG_W("uart id=%d is closed", uartid);
  124. return -1;
  125. }
  126. if (uartid == 1)
  127. {
  128. #ifdef FALSE123
  129. int re = tls_uart_dma_write(data, length, RT_NULL, 1);
  130. LOG_I("tls_uart_dma_write re=%d", re);
  131. return 0;
  132. #endif
  133. }
  134. int re = rt_device_write(serials[uartid], 0, data, length);
  135. LOG_I("luat_uart_write id=%ld re=%ld length=%ld", uartid, re, length);
  136. return re;
  137. }
  138. int luat_uart_read(int uartid, void* buffer, size_t length)
  139. {
  140. if(!luat_uart_exist(uartid)) {
  141. LOG_W("uart id=%d not exist", uartid);
  142. return -1;
  143. }
  144. if (serials[uartid]->open_flag == 0) {
  145. LOG_W("uart id=%d is closed", uartid);
  146. return -1;
  147. }
  148. serials_marks[uartid] = 0;
  149. int re = rt_device_read(serials[uartid], -1, buffer, length);
  150. return re;
  151. }
  152. int luat_uart_close(int uartid)
  153. {
  154. if(!luat_uart_exist(uartid)) {
  155. LOG_W("uart id=%d not exist", uartid);
  156. return 0;
  157. }
  158. int re = rt_device_close(serials[uartid]);
  159. return re;
  160. }
  161. int luat_setup_cb(int uartid, int received, int sent) {
  162. if (!luat_uart_exist(uartid)) {
  163. LOG_W("uart id=%d not exist", uartid);
  164. return -1;
  165. }
  166. if (received) {
  167. LOG_I("uart id=%d set rx_indicate", uartid);
  168. rt_device_set_rx_indicate(serials[uartid], uart_input_cb);
  169. }
  170. else {
  171. LOG_I("uart id=%d unset rx_indicate", uartid);
  172. rt_device_set_rx_indicate(serials[uartid], RT_NULL);
  173. }
  174. if (sent) {
  175. LOG_I("uart id=%d set tx_complete", uartid);
  176. rt_device_set_tx_complete(serials[uartid], uart_sent_cb);
  177. }
  178. else {
  179. LOG_I("uart id=%d unset tx_complete", uartid);
  180. rt_device_set_tx_complete(serials[uartid], RT_NULL);
  181. }
  182. return 0;
  183. }