luat_uart_rtt.c 5.2 KB

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