luat_uart_rtt.c 5.3 KB

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