drv_uart.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /* Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
  2. * All rights reserved.
  3. *
  4. * This software is supplied "AS IS" without any warranties.
  5. * RDA assumes no responsibility or liability for the use of the software,
  6. * conveys no license or title under any patent, copyright, or mask work
  7. * right to the product. RDA reserves the right to make changes in the
  8. * software without notification. RDA also make no representation or
  9. * warranty that such application will be suitable for the specified use
  10. * without further testing or modification.
  11. */
  12. #ifndef _DRV_UART_H_
  13. #define _DRV_UART_H_
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include <stddef.h>
  17. #include "drv_names.h"
  18. #include "drv_config.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. struct drvUart;
  23. /**
  24. * @brief UART struct
  25. *
  26. * Only a declare here, caller have no necessary to know
  27. * how it is implemented. And it is a anchor to identify
  28. * which uart device is to be handled.
  29. */
  30. typedef struct drvUart drvUart_t;
  31. /**
  32. * @brief UART data event
  33. */
  34. enum
  35. {
  36. DRV_UART_EVENT_RX_ARRIVED = (1 << 0), ///< Received new data
  37. DRV_UART_EVENT_RX_OVERFLOW = (1 << 1), ///< Rx fifo overflowed
  38. DRV_UART_EVENT_TX_COMPLETE = (1 << 2) ///< All data had been sent
  39. };
  40. /**
  41. * @brief UART data bits
  42. */
  43. typedef enum
  44. {
  45. DRV_UART_DATA_BITS_5 = 5,
  46. DRV_UART_DATA_BITS_6 = 6,
  47. DRV_UART_DATA_BITS_7 = 7,
  48. DRV_UART_DATA_BITS_8 = 8
  49. } drvUartDataBits_t;
  50. /**
  51. * @brief UART stop bits
  52. */
  53. typedef enum
  54. {
  55. DRV_UART_STOP_BITS_1 = 1,
  56. DRV_UART_STOP_BITS_2 = 2
  57. } drvUartStopBits_t;
  58. /**
  59. * @brief UART parity check mode
  60. */
  61. typedef enum
  62. {
  63. DRV_UART_NO_PARITY, ///< No parity check
  64. DRV_UART_ODD_PARITY, ///< Parity check is odd
  65. DRV_UART_EVEN_PARITY, ///< Parity check is even
  66. DRV_UART_SPACE_PARITY, ///< Parity check is always 0 (space)
  67. DRV_UART_MARK_PARITY ///< Parity check is always 1 (mark)
  68. } drvUartParity_t;
  69. /**
  70. * @brief function type to notify data event
  71. */
  72. typedef void (*drvUartEventCB_t)(void *param, uint32_t evt);
  73. /**
  74. * @brief UART config
  75. */
  76. typedef struct drvUartCfg
  77. {
  78. uint32_t baud; ///< baudrate, 0 for auto baud
  79. drvUartDataBits_t data_bits; ///< data bits
  80. drvUartStopBits_t stop_bits; ///< stop bits
  81. drvUartParity_t parity; ///< parity check mode
  82. bool auto_baud_lc; ///< "at" ot "AT" for auto baud. 8910 can detect both "at" and "AT". So, it will be ignored in 8910.
  83. bool cts_enable; ///< enable cts or not
  84. bool rts_enable; ///< enable rts or not
  85. size_t rx_buf_size; ///< rx buffer size
  86. size_t tx_buf_size; ///< tx buffer size
  87. uint32_t event_mask; ///< event mask, identify which event will be notified
  88. drvUartEventCB_t event_cb; ///< event notify callback function
  89. void *event_cb_ctx; ///< app private param (like a context)
  90. } drvUartCfg_t;
  91. /**
  92. * @brief UART attributes
  93. */
  94. typedef struct
  95. {
  96. uint32_t baud_max;
  97. bool data_bit5_support : 1;
  98. bool data_bit6_support : 1;
  99. bool data_bit7_support : 1;
  100. bool data_bit8_support : 1;
  101. bool stop_bit1_support : 1;
  102. bool stop_bit2_support : 1;
  103. bool parity_odd_support : 1;
  104. bool parity_even_support : 1;
  105. bool parity_mark_support : 1;
  106. bool parity_space_support : 1;
  107. } drvUartAttr_t;
  108. /**
  109. * @brief create an UART driver
  110. *
  111. * Create an UART driver, caller should provide an valid UART FOURCC device name
  112. * and an valid UART config.
  113. *
  114. * @param name FOURCC UART name (DRV_NAME_UART{1, 2, 3})
  115. * @param cfg UART config
  116. * @return
  117. * - NULL Fail to create an UART driver
  118. * - non-null Create an UART driver
  119. */
  120. drvUart_t *drvUartCreate(uint32_t name, drvUartCfg_t *cfg);
  121. /**
  122. * @brief open UART driver
  123. *
  124. * Open the UART driver, done it can send/receive data via the UART.
  125. *
  126. * When the UART is already opened, it will return true directly.
  127. *
  128. * @param uart the UART driver
  129. * @return
  130. * - true success
  131. * - false fail
  132. */
  133. bool drvUartOpen(drvUart_t *uart);
  134. /**
  135. * @brief create an UART driver for already opened device
  136. *
  137. * This is similar to \p drvUartCreate and \p drvUartOpen. And it will only
  138. * create and open already enabled device. Inside, UART configuration won't
  139. * be changed. So, UART configuration related fields will be ignored, and
  140. * will be filled with the actual value.
  141. *
  142. * This will be useful when the data already in hardware rx fifo shall be
  143. * kept.
  144. *
  145. * @param name FOURCC UART name (DRV_NAME_UART{1, 2, 3})
  146. * @param cfg UART config
  147. * @return
  148. * - created UART driver
  149. * - NULL on fail, invalid parameter or UART isn't enabled.
  150. */
  151. drvUart_t *drvUartOpenKeepConfig(uint32_t name, drvUartCfg_t *cfg);
  152. /**
  153. * @brief close UART driver
  154. *
  155. * @note All data in tx/rx buffer will be purged.
  156. *
  157. * Close the UART driver, stop all data transfer.
  158. * But do not release resource.
  159. *
  160. * @param uart the UART driver
  161. */
  162. void drvUartClose(drvUart_t *uart);
  163. /**
  164. * @brief destroy the UART driver
  165. *
  166. * @param uart the UART driver
  167. */
  168. void drvUartDestroy(drvUart_t *uart);
  169. /**
  170. * @brief send data via the UART
  171. *
  172. * @note data may not send to hardware right away
  173. *
  174. * Send data via UART, the data may not send to hardware directly
  175. * because the UART hw may be busy, and the data not sent to hw will
  176. * be cached to a software tx buffer(see, drvUartCfg_t->tx_buf_size).
  177. *
  178. * Therefore the return value include the data had been sent and the
  179. * data had been cached.
  180. *
  181. * @param uart the UART driver
  182. * @param data data buffer to be sent
  183. * @param size data buffer size
  184. * @return
  185. * - (-1) Parameter error
  186. * - OTHERS (>=0) The number of bytes actually sent (or cached)
  187. */
  188. int drvUartSend(drvUart_t *uart, const void *data, size_t size);
  189. /**
  190. * @brief send data via the UART without cache in n milliseconds
  191. *
  192. * @note data will not cached to tx fifo
  193. *
  194. * @param uart the UART driver
  195. * @param data data buffer to be sent
  196. * @param size data buffer size
  197. * @param timeout_ms timeout milliseconds
  198. * @return
  199. * - (-1) Parameter error
  200. * - OTHERS (>=0) The number of bytes actually sent
  201. */
  202. int drvUartSendAll(drvUart_t *uart, const void *data, size_t size, uint32_t timeout_ms);
  203. /**
  204. * @brief receive data from the UART
  205. *
  206. * Receive data from the UART driver. Actually caller got data
  207. * from a software rx fifo but not hw fifo directly.
  208. *
  209. * After drvUartOpen the driver will store data from UART hw rx fifo
  210. * automatically and trigger a DRV_UART_EVENT_RX_ARRIVED, app who use
  211. * this driver can call the api after receive the event.
  212. *
  213. * @param uart the UART driver
  214. * @param buf buffer to store data
  215. * @param size buffer size
  216. * @return
  217. * - (-1) Parameter error
  218. * - OTHERS (>=0) The number of bytes actually receive from UART
  219. */
  220. int drvUartReceive(drvUart_t *uart, void *buf, size_t size);
  221. /**
  222. * @brief inquire avalable bytes in rx buffer
  223. *
  224. * @param uart the UART driver
  225. * @return
  226. * - (-1) Parameter error
  227. * - OTHERS (>=0) Available size in byte
  228. */
  229. int drvUartReadAvail(drvUart_t *uart);
  230. /**
  231. * @brief inquire avalable space in tx buffer
  232. *
  233. * @param uart the UART driver
  234. * @return
  235. * - (-1) Parameter error
  236. * - OTHERS (>=0) Available size in byte
  237. */
  238. int drvUartWriteAvail(drvUart_t *uart);
  239. /**
  240. * @brief set whether UART will auto sleep
  241. *
  242. * When enabled, UART will sleep after all bytes are transfered , with
  243. * specified timeout.
  244. *
  245. * To disable auto sleep feature, set \p timeout to -1 (or other negative
  246. * values).
  247. *
  248. * It maybe different among chips, or even different UARTs of one chip
  249. * whether system will be waken up when there are UART input.
  250. *
  251. * @param uart the UART driver
  252. * @param timeout auto sleep wait time after transfer done. It can be
  253. * 0 but not recommended. Negative value to disable
  254. * auto sleep feature.
  255. */
  256. void drvUartSetAutoSleep(drvUart_t *uart, int timeout);
  257. /**
  258. * @brief get uart device name
  259. *
  260. * @param uart the UART driver
  261. * @return uart device name
  262. */
  263. uint32_t drvUartGetName(drvUart_t *uart);
  264. /**
  265. * @brief get uart configuration, will never return null
  266. *
  267. * @param uart the UART driver
  268. * @return
  269. * - NULL fail, only when UART is NULL
  270. * - otherwise the UART configuration struct point.
  271. */
  272. const drvUartCfg_t *drvUartConfig(drvUart_t *uart);
  273. /**
  274. * @brief get the UART attribute
  275. *
  276. * @param uart the UART driver
  277. * @return
  278. * - NULL fail, only when \p uart is NULL
  279. * - other the UART attribute struct address
  280. */
  281. const drvUartAttr_t *drvUartAttribute(drvUart_t *uart);
  282. /**
  283. * @brief get the UART attribute from the device name
  284. *
  285. * @param name the UART device name
  286. * @return
  287. * - NULL invalid device name
  288. * - other the UART attribute struct address
  289. */
  290. const drvUartAttr_t *drvUartAttributeFromName(uint32_t name);
  291. /**
  292. * @brief reconfig the uart
  293. *
  294. * @note caller should make sure the UART had already closed,
  295. * or it will return false
  296. * if `cfg` is NULL, it will do nothing and return true
  297. *
  298. * @param uart the UART driver
  299. * @param cfg the configuration
  300. * @return
  301. * - true sucess
  302. * - false fail
  303. */
  304. bool drvUartReconfig(drvUart_t *uart, drvUartCfg_t *cfg);
  305. /**
  306. * @brief set event mask and callback
  307. *
  308. * @param uart the UART driver
  309. * @param event_mask event mask, identify which event will be notified
  310. * @param event_cb event notify callback function
  311. * @param event_cb_ctx event callback context
  312. * @return
  313. * - true on sucess
  314. * - false on fail, invalid parameter
  315. */
  316. bool drvUartSetCallback(drvUart_t *uart, uint32_t event_mask, drvUartEventCB_t event_cb, void *event_cb_ctx);
  317. /**
  318. * @brief wait rx available, with optional timeout
  319. *
  320. * @param uart the UART driver
  321. * @param timeout wait timeout
  322. * @return
  323. * - true on success
  324. * - false on fail, invalid prameter or timeout
  325. */
  326. bool drvUartWaitRxAvail(drvUart_t *uart, unsigned timeout);
  327. /**
  328. * @brief wait tx available, with optional timeout
  329. *
  330. * @param uart the UART driver
  331. * @param timeout wait timeout
  332. * @return
  333. * - true on success
  334. * - false on fail, invalid prameter or timeout
  335. */
  336. bool drvUartWaitTxAvail(drvUart_t *uart, unsigned timeout);
  337. /**
  338. * @brief wait tx data send finished
  339. *
  340. * @param uart the UART driver
  341. * @param timeout wait time in millisecond
  342. * @return
  343. * - true data all sent
  344. * - false data residual
  345. */
  346. bool drvUartWaitTxFinish(drvUart_t *uart, uint32_t timeout);
  347. /**
  348. * @brief bind UART to stdio
  349. *
  350. * @param uart the UART driver
  351. * @return
  352. * - true on success
  353. * - false on fail
  354. */
  355. bool drvUartBindStdio(drvUart_t *uart);
  356. #ifdef __cplusplus
  357. }
  358. #endif
  359. #endif