uart_demo.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*================================================================
  2. Copyright (c) 2021, Quectel Wireless Solutions Co., Ltd. All rights reserved.
  3. Quectel Wireless Solutions Proprietary and Confidential.
  4. =================================================================*/
  5. /*=================================================================
  6. EDIT HISTORY FOR MODULE
  7. This section contains comments describing changes made to the module.
  8. Notice that changes are listed in reverse chronological order.
  9. WHEN WHO WHAT, WHERE, WHY
  10. ------------ ------- -------------------------------------------------------------------------------
  11. =================================================================*/
  12. /*===========================================================================
  13. * include files
  14. ===========================================================================*/
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "ql_api_osi.h"
  19. #include "ql_log.h"
  20. #include "ql_uart.h"
  21. #include "ql_gpio.h"
  22. #include "ql_pin_cfg.h"
  23. #include "ql_usb.h"
  24. /*===========================================================================
  25. *Definition
  26. ===========================================================================*/
  27. #define QL_UART_DEMO_LOG_LEVEL QL_LOG_LEVEL_INFO
  28. #define QL_UART_DEMO_LOG(msg, ...) QL_LOG(QL_UART_DEMO_LOG_LEVEL, "ql_uart_demo", msg, ##__VA_ARGS__)
  29. #define QL_UART_TASK_STACK_SIZE 4096
  30. #define QL_UART_TASK_PRIO APP_PRIORITY_NORMAL
  31. #define QL_UART_TASK_EVENT_CNT 5
  32. #define QL_UART_RX_BUFF_SIZE 2048
  33. #define QL_UART_TX_BUFF_SIZE 2048
  34. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  35. #define QL_USB_PRINTER_ENABLE 0
  36. #define QL_CUR_UART_PORT QL_UART_PORT_3
  37. #define QL_CUR_UART_TX_PIN QL_UART3_TX_PIN
  38. #define QL_CUR_UART_TX_FUNC QL_UART3_TX_FUNC
  39. #define QL_CUR_UART_RX_PIN QL_UART3_RX_PIN
  40. #define QL_CUR_UART_RX_FUNC QL_UART3_RX_FUNC
  41. /*===========================================================================
  42. * Variate
  43. ===========================================================================*/
  44. /*===========================================================================
  45. * Functions
  46. ===========================================================================*/
  47. void ql_uart_notify_cb(unsigned int ind_type, ql_uart_port_number_e port, unsigned int size)
  48. {
  49. unsigned char *recv_buff = calloc(1, QL_UART_RX_BUFF_SIZE+1);
  50. unsigned int real_size = 0;
  51. int read_len = 0;
  52. QL_UART_DEMO_LOG("UART port %d receive ind type:0x%x, receive data size:%d", port, ind_type, size);
  53. switch(ind_type)
  54. {
  55. case QUEC_UART_RX_OVERFLOW_IND: //rx buffer overflow
  56. case QUEC_UART_RX_RECV_DATA_IND:
  57. {
  58. while(size > 0)
  59. {
  60. memset(recv_buff, 0, QL_UART_RX_BUFF_SIZE+1);
  61. real_size= MIN(size, QL_UART_RX_BUFF_SIZE);
  62. read_len = ql_uart_read(port, recv_buff, real_size);
  63. QL_UART_DEMO_LOG("read_len=%d, recv_data=%s", read_len, recv_buff);
  64. if((read_len > 0) && (size >= read_len))
  65. {
  66. size -= read_len;
  67. }
  68. else
  69. {
  70. break;
  71. }
  72. }
  73. break;
  74. }
  75. case QUEC_UART_TX_FIFO_COMPLETE_IND:
  76. {
  77. QL_UART_DEMO_LOG("tx fifo complete");
  78. break;
  79. }
  80. }
  81. free(recv_buff);
  82. recv_buff = NULL;
  83. }
  84. static void ql_uart_demo_thread(void *param)
  85. {
  86. int ret = 0;
  87. QlOSStatus err = 0;
  88. ql_uart_config_s uart_cfg = {0};
  89. int write_len = 0;
  90. ql_uart_tx_status_e tx_status;
  91. unsigned char data[] = "hello uart demo\r\n";
  92. /***********************************************************
  93. Note start:
  94. 1.If the BAUD rate is QL UART BAUD_AUTO,a string of 'at'should be sent at least once to identify the baud rate.
  95. 2.Once the baud rate is identified, it cannot be changed unless restarted.
  96. ************************************************************/
  97. uart_cfg.baudrate = QL_UART_BAUD_115200;
  98. uart_cfg.flow_ctrl = QL_FC_NONE;
  99. uart_cfg.data_bit = QL_UART_DATABIT_8;
  100. uart_cfg.stop_bit = QL_UART_STOP_1;
  101. uart_cfg.parity_bit = QL_UART_PARITY_NONE;
  102. ret = ql_uart_set_dcbconfig(QL_CUR_UART_PORT, &uart_cfg);
  103. QL_UART_DEMO_LOG("ret: 0x%x", ret);
  104. if(QL_UART_SUCCESS != ret)
  105. {
  106. goto exit;
  107. }
  108. /***********************************************************
  109. Note start:
  110. 1. If QL_UART_PORT_1 is selected for use, there is no need to set TX and RX pin and function
  111. 2. According to the QuecOpen GPIO table, user should select the correct PIN to set function
  112. 3. CTS and RTS pins (UART2 and UART3) also need to be initialized if hardware flow control function is required
  113. ************************************************************/
  114. #if 1
  115. ret = ql_pin_set_func(QL_CUR_UART_TX_PIN, QL_CUR_UART_TX_FUNC);
  116. if(QL_GPIO_SUCCESS != ret)
  117. {
  118. goto exit;
  119. }
  120. ret = ql_pin_set_func(QL_CUR_UART_RX_PIN, QL_CUR_UART_RX_FUNC);
  121. if(QL_GPIO_SUCCESS != ret)
  122. {
  123. goto exit;
  124. }
  125. #endif
  126. /*Note end*/
  127. ret = ql_uart_open(QL_CUR_UART_PORT);
  128. QL_UART_DEMO_LOG("ret: 0x%x", ret);
  129. #if QL_USB_PRINTER_ENABLE
  130. ret = ql_uart_open(QL_USB_PORT_PRINTER);
  131. QL_UART_DEMO_LOG("ret: 0x%x", ret);
  132. #endif
  133. if(QL_UART_SUCCESS == ret)
  134. {
  135. ret = ql_uart_register_cb(QL_CUR_UART_PORT, ql_uart_notify_cb);
  136. QL_UART_DEMO_LOG("ret: 0x%x", ret);
  137. #if QL_USB_PRINTER_ENABLE
  138. ret = ql_uart_register_cb(QL_USB_PORT_PRINTER, ql_uart_notify_cb);
  139. QL_UART_DEMO_LOG("ret: 0x%x", ret);
  140. #endif
  141. memset(&uart_cfg, 0, sizeof(ql_uart_config_s));
  142. ret = ql_uart_get_dcbconfig(QL_CUR_UART_PORT, &uart_cfg);
  143. QL_UART_DEMO_LOG("ret: 0x%x, baudrate=%d, flow_ctrl=%d, data_bit=%d, stop_bit=%d, parity_bit=%d",
  144. ret, uart_cfg.baudrate, uart_cfg.flow_ctrl, uart_cfg.data_bit, uart_cfg.stop_bit, uart_cfg.parity_bit);
  145. while(1)
  146. {
  147. write_len = ql_uart_write(QL_CUR_UART_PORT, data, strlen((char *)data));
  148. QL_UART_DEMO_LOG("write_len:%d", write_len);
  149. ql_uart_get_tx_fifo_status(QL_CUR_UART_PORT, &tx_status);
  150. QL_UART_DEMO_LOG("tx_status:%d", tx_status);
  151. ql_rtos_task_sleep_ms(20000);
  152. }
  153. }
  154. exit:
  155. err = ql_rtos_task_delete(NULL);
  156. if(err != QL_OSI_SUCCESS)
  157. {
  158. QL_UART_DEMO_LOG("task deleted failed");
  159. }
  160. }
  161. void ql_uart_app_init(void)
  162. {
  163. QlOSStatus err = 0;
  164. ql_task_t uart_task = NULL;
  165. #if QL_USB_PRINTER_ENABLE
  166. /*
  167. 1. 重启生效,使能USB打印功能后,USB NMEA口将会被枚举为usb打印设备,用户可在重启后使用ql_uart_open, ql_uart_register_cb,
  168. ql_uart_write等函数,以QL_USB_PORT_PRINTER为参数来从usb打印设备中读取和写入数据;使能后usb NMEA口将不会被枚举
  169. 2. 如果开启了UAC功能,则不能使用usb打印设备
  170. */
  171. ql_usb_set_enum_mode(QL_USB_ENUM_USBNET_COM_PRINTER);
  172. #endif
  173. err = ql_rtos_task_create(&uart_task, QL_UART_TASK_STACK_SIZE, QL_UART_TASK_PRIO, "QUARTDEMO", ql_uart_demo_thread, NULL, QL_UART_TASK_EVENT_CNT);
  174. if (err != QL_OSI_SUCCESS)
  175. {
  176. QL_UART_DEMO_LOG("demo task created failed");
  177. return;
  178. }
  179. }