ql_uart.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. 
  2. /*================================================================
  3. Copyright (c) 2021, Quectel Wireless Solutions Co., Ltd. All rights reserved.
  4. Quectel Wireless Solutions Proprietary and Confidential.
  5. =================================================================*/
  6. /*=================================================================
  7. EDIT HISTORY FOR MODULE
  8. This section contains comments describing changes made to the module.
  9. Notice that changes are listed in reverse chronological order.
  10. WHEN WHO WHAT, WHERE, WHY
  11. ------------ ------- -------------------------------------------------------------------------------
  12. =================================================================*/
  13. #ifndef QL_UART_H
  14. #define QL_UART_H
  15. #include "ql_api_common.h"
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. /*===========================================================================
  20. * Macro Definition
  21. ===========================================================================*/
  22. #define QL_UART_ERRCODE_BASE (QL_COMPONENT_BSP_UART<<16)
  23. #define QL_UART_MAX_NUM 6
  24. /*===========================================================================
  25. * Enum
  26. ===========================================================================*/
  27. typedef enum
  28. {
  29. QL_UART_SUCCESS = 0,
  30. QL_UART_EXECUTE_ERR = 1|QL_UART_ERRCODE_BASE,
  31. QL_UART_MEM_ADDR_NULL_ERR,
  32. QL_UART_INVALID_PARAM_ERR,
  33. QL_UART_OPEN_REPEAT_ERR,
  34. QL_UART_NOT_OPEN_ERR = 5|QL_UART_ERRCODE_BASE,
  35. } ql_uart_errcode_e;
  36. typedef enum
  37. {
  38. QL_PORT_NONE = -1,
  39. QL_UART_PORT_1,
  40. QL_UART_PORT_2,
  41. QL_UART_PORT_3,
  42. QL_UART_PORT_4,
  43. QL_UART_PORT_5,
  44. QL_UART_PORT_6,
  45. QL_USB_PORT_AT,
  46. QL_USB_PORT_MODEM,
  47. QL_USB_PORT_NMEA,
  48. QL_USB_PORT_PRINTER = QL_USB_PORT_NMEA, //when enabled usb printer, usb nmea port will be enumerated as a printing device
  49. QL_PORT_MAX,
  50. }ql_uart_port_number_e;
  51. typedef enum
  52. {
  53. QL_FC_NONE = 0,
  54. QL_FC_HW,
  55. }ql_uart_flowctrl_e;
  56. typedef enum
  57. {
  58. QL_UART_BAUD_AUTO = 0, //自适应波特率
  59. QL_UART_BAUD_1200 = 1200,
  60. QL_UART_BAUD_2400 = 2400,
  61. QL_UART_BAUD_4800 = 4800,
  62. QL_UART_BAUD_9600 = 9600,
  63. QL_UART_BAUD_14400 = 14400,
  64. QL_UART_BAUD_19200 = 19200,
  65. QL_UART_BAUD_28800 = 28800,
  66. QL_UART_BAUD_33600 = 33600,
  67. QL_UART_BAUD_38400 = 38400,
  68. QL_UART_BAUD_57600 = 57600,
  69. QL_UART_BAUD_115200 = 115200,
  70. QL_UART_BAUD_230400 = 230400,
  71. QL_UART_BAUD_250000 = 250000,
  72. QL_UART_BAUD_460800 = 460800,
  73. QL_UART_BAUD_500000 = 500000,
  74. QL_UART_BAUD_921600 = 921600,
  75. QL_UART_BAUD_1000000 = 1000000,
  76. QL_UART_BAUD_1843200 = 1843200,
  77. QL_UART_BAUD_2000000 = 2000000,
  78. //QL_UART_BAUD_2100000 = 2100000,
  79. //QL_UART_BAUD_3686400 = 3686400, //only support uart2/3
  80. //QL_UART_BAUD_4000000 = 4000000, //only support uart2/3
  81. //QL_UART_BAUD_4468750 = 4468750 //only support uart2/3
  82. }ql_uart_baud_e;
  83. typedef enum
  84. {
  85. QL_UART_DATABIT_7 = 7,
  86. QL_UART_DATABIT_8 = 8, //8910 ARM UART hardware only support 8bit Data
  87. }ql_uart_databit_e;
  88. typedef enum
  89. {
  90. QL_UART_STOP_1 =1,
  91. QL_UART_STOP_2 =2,
  92. }ql_uart_stopbit_e;
  93. typedef enum
  94. {
  95. QL_UART_PARITY_NONE,
  96. QL_UART_PARITY_ODD,
  97. QL_UART_PARITY_EVEN,
  98. }ql_uart_paritybit_e;
  99. typedef enum
  100. {
  101. QL_UART_TX_SENDING,
  102. QL_UART_TX_COMPLETE,
  103. }ql_uart_tx_status_e;
  104. typedef enum
  105. {
  106. QL_UART_EVENT_RX_ARRIVED = (1 << 0), ///< Received new data
  107. QL_UART_EVENT_RX_OVERFLOW = (1 << 1), ///< Rx fifo overflowed
  108. QL_UART_EVENT_TX_COMPLETE = (1 << 2) ///< All data had been sent
  109. }ql_uart_event_e;
  110. /*===========================================================================
  111. * Struct
  112. ===========================================================================*/
  113. typedef struct
  114. {
  115. ql_uart_baud_e baudrate;
  116. ql_uart_databit_e data_bit;
  117. ql_uart_stopbit_e stop_bit;
  118. ql_uart_paritybit_e parity_bit;
  119. ql_uart_flowctrl_e flow_ctrl;
  120. }ql_uart_config_s;
  121. /*****************************************************************
  122. * Description: UART callback
  123. *
  124. * Parameters:
  125. * ind_type [in] 事件类型,UART RX接收数据、RX buffer溢出、TX fifo发送完成。
  126. * port [in] 串口号
  127. * size [in] 数据大小
  128. *
  129. *****************************************************************/
  130. typedef void (*ql_uart_callback)(uint32 ind_type, ql_uart_port_number_e port, uint32 size);
  131. /*===========================================================================
  132. * Functions declaration
  133. ===========================================================================*/
  134. /*****************************************************************
  135. * Function: ql_uart_set_event_mask
  136. *
  137. * Description: 设置事件掩码,设置的事件会回调通知,设置完重新打开uart口生效
  138. *
  139. * Parameters:
  140. * port [in] 串口选择。
  141. * event_mask [in] 事件掩码。
  142. *
  143. * Return:
  144. * 0 设置成功。
  145. * other 错误码。
  146. *
  147. *****************************************************************/
  148. ql_uart_errcode_e ql_uart_set_event_mask(ql_uart_port_number_e port, uint32_t event_mask);
  149. /*****************************************************************
  150. * Function: ql_uart_set_dcbconfig
  151. *
  152. * Description: 设置串口属性,设置完重新打开uart口生效
  153. *
  154. * Parameters:
  155. * port [in] 串口选择。
  156. * dcb [in] 串口属性配置结构体
  157. *
  158. * Return:
  159. * 0 设置成功。
  160. * other 错误码。
  161. *
  162. *****************************************************************/
  163. ql_uart_errcode_e ql_uart_set_dcbconfig(ql_uart_port_number_e port, ql_uart_config_s *dcb);
  164. /*****************************************************************
  165. * Function: ql_uart_get_dcbconfig
  166. *
  167. * Description: 获取串口属性
  168. *
  169. * Parameters:
  170. * port [in] 串口选择。
  171. * dcb [out] 串口属性配置结构体
  172. *
  173. * Return:
  174. * 0 获取成功。
  175. * other 错误码。
  176. *
  177. *****************************************************************/
  178. ql_uart_errcode_e ql_uart_get_dcbconfig(ql_uart_port_number_e port, ql_uart_config_s *dcb);
  179. /*****************************************************************
  180. * Function: ql_uart_open
  181. *
  182. * Description: 打开 UART 设备
  183. *
  184. * Parameters:
  185. * port [in] 串口选择。
  186. *
  187. * Return:
  188. * 0 设置成功。
  189. * other 错误码。
  190. *
  191. * Attention:如果遇到UART通信过程中TX下拉电平偏高,导致通信异常,
  192. * 可以尝试适当增加TX引脚的驱动能力,默认是2对应7.5mA。
  193. *****************************************************************/
  194. ql_uart_errcode_e ql_uart_open(ql_uart_port_number_e port);
  195. /*****************************************************************
  196. * Function: ql_uart_close
  197. *
  198. * Description: 关闭 UART 设备
  199. *
  200. * Parameters:
  201. * port [in] 串口选择。
  202. *
  203. * Return:
  204. * 0 设置成功。
  205. * other 错误码。
  206. *
  207. *****************************************************************/
  208. ql_uart_errcode_e ql_uart_close(ql_uart_port_number_e port);
  209. /*****************************************************************
  210. * Function: ql_uart_write
  211. *
  212. * Description: UART写
  213. *
  214. * Parameters:
  215. * port [in] 串口选择。
  216. * data [in] 要写入的数据
  217. * data_len [in] 要写入的数据长度
  218. *
  219. * Return:
  220. * < 0 错误码
  221. * other 写入的实际字节长度。
  222. *
  223. *****************************************************************/
  224. int ql_uart_write(ql_uart_port_number_e port, unsigned char *data, unsigned int data_len);
  225. /*****************************************************************
  226. * Function: ql_uart_read
  227. *
  228. * Description: UART读
  229. *
  230. * Parameters:
  231. * port [in] 串口选择。
  232. * data [out] 读取的数据
  233. * data_len [in] 要读取的数据长度
  234. *
  235. * Return:
  236. * < 0 错误码
  237. * other 实际读取的数据长度
  238. *
  239. *****************************************************************/
  240. int ql_uart_read(ql_uart_port_number_e port, unsigned char *data, unsigned int data_len);
  241. /*****************************************************************
  242. * Function: ql_uart_register_cb
  243. *
  244. * Description: 注册UART 事件回调函数
  245. *
  246. * Parameters:
  247. * port [in] 串口选择。
  248. * uart_cb [in] 需要注册的回调函数,若为NULL,则取消注册callback,可使用轮询读取数据
  249. *
  250. * Return:
  251. * 0 注册成功。
  252. * other 错误码。
  253. *
  254. *****************************************************************/
  255. ql_uart_errcode_e ql_uart_register_cb(ql_uart_port_number_e port, ql_uart_callback uart_cb);
  256. /*****************************************************************
  257. * Function: ql_uart_get_tx_fifo_status
  258. *
  259. * Description: 获取UART TX FIFO数据发送状态
  260. *
  261. * Parameters:
  262. * port [in] 串口选择。
  263. * tx_status [in] TX FIFO状态
  264. *
  265. * Return:
  266. * 0 获取成功
  267. * other 错误码。
  268. *
  269. *****************************************************************/
  270. ql_uart_errcode_e ql_uart_get_tx_fifo_status(ql_uart_port_number_e port, ql_uart_tx_status_e *tx_status);
  271. #ifdef CONFIG_QUEC_PROJECT_FEATURE_SLEEP
  272. /*****************************************************************
  273. * Function: ql_uart_set_sleep_delay_time
  274. *
  275. * Description: 设置UART无数据进入休眠的时间
  276. *
  277. * Parameters:
  278. * times [in] 设置的时间,单位:s。设置的最小值1s,最大值255s。
  279. *
  280. * Return:
  281. * 0 设置成功。
  282. * other 错误码。
  283. *
  284. *****************************************************************/
  285. ql_uart_errcode_e ql_uart_set_sleep_delay_time(uint8_t times);
  286. /*****************************************************************
  287. * Function: ql_uart_get_sleep_delay_time
  288. *
  289. * Description: 获取当前设置的UART无数据进入休眠的时间
  290. *
  291. * Parameters:
  292. * times [out] 获取到的时间,单位:s。
  293. *
  294. * Return:
  295. * 0 设置成功。
  296. * other 错误码。
  297. *
  298. *****************************************************************/
  299. ql_uart_errcode_e ql_uart_get_sleep_delay_time(uint8_t *times);
  300. #endif
  301. #ifdef __cplusplus
  302. } /*"C" */
  303. #endif
  304. #endif /* QL_UART_H */