hosal_uart.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * Copyright (c) 2016-2022 Bouffalolab.
  3. *
  4. * This file is part of
  5. * *** Bouffalolab Software Dev Kit ***
  6. * (see www.bouffalolab.com).
  7. *
  8. * Redistribution and use in source and binary forms, with or without modification,
  9. * are permitted provided that the following conditions are met:
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * 3. Neither the name of Bouffalo Lab nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  25. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  26. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef __HOSAL_UART_H__
  31. #define __HOSAL_UART_H__
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. /** @addtogroup hosal_uart UART
  36. * HOSAL UART API
  37. *
  38. * @{
  39. */
  40. #include <stdio.h>
  41. #include <stdint.h>
  42. #include "hosal_dma.h"
  43. #define HOSAL_UART_AUTOBAUD_0X55 1 /**< @brief UART auto baudrate detection using codeword 0x55 */
  44. #define HOSAL_UART_AUTOBAUD_STARTBIT 2 /**< @brief UART auto baudrate detection using start bit */
  45. #define HOSAL_UART_TX_CALLBACK 1 /**< @brief UART tx idle interrupt callback */
  46. #define HOSAL_UART_RX_CALLBACK 2 /**< @brief UART rx complete callback */
  47. #define HOSAL_UART_TX_DMA_CALLBACK 3 /**< @brief UART tx DMA trans complete callback */
  48. #define HOSAL_UART_RX_DMA_CALLBACK 4 /**< @brief UART rx DMA trans complete callback */
  49. #define HOSAL_UART_BAUD_SET 1 /**< @brief UART baud set */
  50. #define HOSAL_UART_BAUD_GET 2 /**< @brief UART baud get */
  51. #define HOSAL_UART_DATA_WIDTH_SET 3 /**< @brief UART data width set */
  52. #define HOSAL_UART_DATA_WIDTH_GET 4 /**< @brief UART data width get */
  53. #define HOSAL_UART_STOP_BITS_SET 5 /**< @brief UART stop bits set */
  54. #define HOSAL_UART_STOP_BITS_GET 6 /**< @brief UART stop bits get */
  55. #define HOSAL_UART_FLOWMODE_SET 7 /**< @brief UART flow mode set */
  56. #define HOSAL_UART_FLOWSTAT_GET 8 /**< @brief UART flow state get */
  57. #define HOSAL_UART_PARITY_SET 9 /**< @brief UART flow mode set */
  58. #define HOSAL_UART_PARITY_GET 10 /**< @brief UART flow state get */
  59. #define HOSAL_UART_MODE_SET 11 /**< @brief UART mode set */
  60. #define HOSAL_UART_MODE_GET 12 /**< @brief UART mode get */
  61. #define HOSAL_UART_FREE_TXFIFO_GET 13 /**< @brief UART free tx fifo get */
  62. #define HOSAL_UART_FREE_RXFIFO_GET 14 /**< @brief UART free rx fifo get */
  63. #define HOSAL_UART_FLUSH 15 /**< @brief Wait for the send to complete */
  64. #define HOSAL_UART_TX_TRIGGER_ON 16 /**< @brief UART TX trigger on */
  65. #define HOSAL_UART_TX_TRIGGER_OFF 17 /**< @brief UART TX trigger off */
  66. #define HOSAL_UART_DMA_TX_START 18 /**< @brief UART DMA TX start trans */
  67. #define HOSAL_UART_DMA_RX_START 19 /**< @brief UART DMA RX start trans */
  68. /**
  69. * @brief hosal uart callback
  70. *
  71. * @param[in] p_arg Set the custom parameters specified when the callback function is set
  72. *
  73. * @return
  74. * - 0 : on success
  75. * - EIO : if an error occurred with any step
  76. */
  77. typedef int (*hosal_uart_callback_t)(void *p_arg);
  78. /**
  79. * @brief UART data width
  80. */
  81. typedef enum {
  82. HOSAL_DATA_WIDTH_5BIT,
  83. HOSAL_DATA_WIDTH_6BIT,
  84. HOSAL_DATA_WIDTH_7BIT,
  85. HOSAL_DATA_WIDTH_8BIT,
  86. HOSAL_DATA_WIDTH_9BIT
  87. } hosal_uart_data_width_t;
  88. /**
  89. * @brief UART stop bits
  90. */
  91. typedef enum {
  92. HOSAL_STOP_BITS_1 = 1,
  93. HOSAL_STOP_BITS_2 = 3
  94. } hosal_uart_stop_bits_t;
  95. /**
  96. * @brief UART flow control
  97. */
  98. typedef enum {
  99. HOSAL_FLOW_CONTROL_DISABLED,
  100. HOSAL_FLOW_CONTROL_RTS,
  101. HOSAL_FLOW_CONTROL_CTS,
  102. HOSAL_FLOW_CONTROL_CTS_RTS
  103. } hosal_uart_flow_control_t;
  104. /**
  105. * @brief UART parity
  106. */
  107. typedef enum {
  108. HOSAL_NO_PARITY,
  109. HOSAL_ODD_PARITY,
  110. HOSAL_EVEN_PARITY
  111. } hosal_uart_parity_t;
  112. /**
  113. * @brief UART mode
  114. */
  115. typedef enum {
  116. HOSAL_UART_MODE_POLL, /**< @brief UART poll mode (default mode) */
  117. HOSAL_UART_MODE_INT_TX, /**< @brief UART TX int mode */
  118. HOSAL_UART_MODE_INT_RX, /**< @brief UART RX int mode */
  119. HOSAL_UART_MODE_INT, /**< @brief UART TX and RX int mode */
  120. } hosal_uart_mode_t;
  121. /**
  122. * @brief UART DMA configuration
  123. */
  124. typedef struct {
  125. /**
  126. * @brief UART DMA trans buffer
  127. * When the transmission is TX, the address is the src address
  128. * When the transmission is RX, the address is the dest address
  129. */
  130. uint8_t *dma_buf;
  131. uint32_t dma_buf_size; /**< @brief UART DMA trans buffer size */
  132. } hosal_uart_dma_cfg_t;
  133. /**
  134. * @brief UART configuration
  135. */
  136. typedef struct {
  137. uint8_t uart_id; /**< @brief UART id */
  138. uint8_t tx_pin; /**< @brief UART tx pin */
  139. uint8_t rx_pin; /**< @brief UART rx pin */
  140. uint8_t cts_pin; /**< @brief UART cts pin */
  141. uint8_t rts_pin; /**< @brief UART rts pin */
  142. uint32_t baud_rate; /**< @brief UART baud rate */
  143. hosal_uart_data_width_t data_width; /**< @brief UART data width */
  144. hosal_uart_parity_t parity; /**< @brief UART parity bit */
  145. hosal_uart_stop_bits_t stop_bits; /**< @brief UART stop btis */
  146. hosal_uart_flow_control_t flow_control; /**< @brief UART flow control */
  147. hosal_uart_mode_t mode; /**< @brief UART int or pull mode */
  148. } hosal_uart_config_t;
  149. /**
  150. * @brief UART device type
  151. */
  152. typedef struct {
  153. uint8_t port; /**< @brief UART port */
  154. hosal_uart_config_t config; /**< @brief UART config */
  155. hosal_uart_callback_t tx_cb; /**< @brief UART tx callback */
  156. void *p_txarg; /**< @brief UART tx callback arg */
  157. hosal_uart_callback_t rx_cb; /**< @brief UART rx callback */
  158. void *p_rxarg; /**< @brief UART rx callback arg */
  159. hosal_uart_callback_t txdma_cb; /**< @brief UART tx dma callback */
  160. void *p_txdma_arg; /**< @brief UART tx dma callback arg */
  161. hosal_uart_callback_t rxdma_cb; /**< @brief UART rx dma callback */
  162. void *p_rxdma_arg; /**< @brief UART rx dma callback arg */
  163. hosal_dma_chan_t dma_tx_chan; /**< @brief UART dma tx channel */
  164. hosal_dma_chan_t dma_rx_chan; /**< @brief UART dma rx channel */
  165. void *priv; /**< @brief UART private data */
  166. } hosal_uart_dev_t;
  167. /**
  168. * @brief define a UART default config
  169. *
  170. * @param[in] cfg config define
  171. * @param[in] id uart id
  172. * @param[in] tx uart tx pin
  173. * @param[in] rx uart rx pin
  174. * @param[in] baud uart baud
  175. *
  176. */
  177. #define HOSAL_UART_CFG_DECL(cfg, id, tx, rx, baud) \
  178. hosal_uart_config_t cfg = { \
  179. .uart_id = id, \
  180. .tx_pin = tx, \
  181. .rx_pin = rx, \
  182. .cts_pin = 255, \
  183. .rts_pin = 255, \
  184. .baud_rate = baud, \
  185. .data_width = HOSAL_DATA_WIDTH_8BIT, \
  186. .parity = HOSAL_NO_PARITY, \
  187. .stop_bits = HOSAL_STOP_BITS_1, \
  188. .mode = HOSAL_UART_MODE_POLL, \
  189. };
  190. /**
  191. * @brief define a UART device
  192. *
  193. * @param[in] dev uart device
  194. * @param[in] id uart id
  195. * @param[in] tx uart tx pin
  196. * @param[in] rx uart rx pin
  197. * @param[in] baud uart baud
  198. */
  199. #define HOSAL_UART_DEV_DECL(dev, id, tx, rx, baud) \
  200. hosal_uart_dev_t dev = { \
  201. .config = { \
  202. .uart_id = id, \
  203. .tx_pin = tx, \
  204. .rx_pin = rx, \
  205. .cts_pin = 255, \
  206. .rts_pin = 255, \
  207. .baud_rate = baud, \
  208. .data_width = HOSAL_DATA_WIDTH_8BIT, \
  209. .parity = HOSAL_NO_PARITY, \
  210. .stop_bits = HOSAL_STOP_BITS_1, \
  211. .mode = HOSAL_UART_MODE_POLL, \
  212. }, \
  213. };
  214. /**
  215. * @brief Get auto baudrate on a UART interface
  216. *
  217. * @param[in] uart the UART interface
  218. * @param[in] mode auto baudrate detection mode(codeword 0x55 or start bit)
  219. *
  220. * @return
  221. * - = 0 on success
  222. * - EIO if an error occurred with any step
  223. */
  224. int hosal_uart_abr_get(hosal_uart_dev_t *uart, uint8_t mode);
  225. /**
  226. * @brief Initialises a UART interface
  227. *
  228. * @param[in] uart the interface which should be initialised
  229. *
  230. * @return
  231. * - 0 on success
  232. * - EIO if an error occurred with any step
  233. */
  234. int hosal_uart_init(hosal_uart_dev_t *uart);
  235. /**
  236. * @brief Poll transmit data on a UART interface
  237. *
  238. * @param[in] uart the UART interface
  239. * @param[in] txbuf pointer to the start of data
  240. * @param[in] size number of bytes to transmit
  241. *
  242. * @return
  243. * - > 0 on success
  244. * - EIO if an error occurred with any step
  245. */
  246. int hosal_uart_send(hosal_uart_dev_t *uart, const void *txbuf, uint32_t size);
  247. /**
  248. * @brief Poll receive data on a UART interface
  249. *
  250. * @param[in] uart the UART interface
  251. * @param[out] rxbuf pointer to the buffer which will store incoming data
  252. * @param[in] expect_size expect number of bytes to receive
  253. *
  254. * @return
  255. * - > 0 number of bytes to receive
  256. * - EIO if an error occurred with any step
  257. */
  258. int hosal_uart_receive(hosal_uart_dev_t *uart, void *data, uint32_t expect_size);
  259. /**
  260. * @brief hal uart ioctl
  261. *
  262. * @param[in] uart the UART interface
  263. * @param[in] ctl Control request
  264. * - HOSAL_UART_BAUD_SET : baud set, p_arg is baud
  265. * - HOSAL_UART_BAUD_GET : baud get, p_arg is baud's pointer
  266. * - HOSAL_UART_DATA_WIDTH_SET : set data width,p_arg is hosal_uart_data_width_t
  267. * - HOSAL_UART_DATA_WIDTH_GET : get data width,p_arg is hosal_uart_data_width_t's pointer
  268. * - HOSAL_UART_STOP_BITS_SET : set stop bits, p_arg is hosal_uart_stop_bits_t
  269. * - HOSAL_UART_STOP_BITS_GET : get stop bits, p_arg is hosal_uart_stop_bits_t's pointer
  270. * - HOSAL_UART_PARITY_SET : set parity, p_arg is hosal_uart_parity_t
  271. * - HOSAL_UART_PARITY_GET : get parity, p_arg is hosal_uart_parity_t's pointer
  272. * - HOSAL_UART_MODE_SET : UART mode set, p_arg is hosal_uart_mode_t
  273. * - HOSAL_UART_MODE_GET : UART mode get, p_arg is hosal_uart_mode_t's pointer
  274. * - HOSAL_UART_FLOWMODE_SET : UART flow mode set, p_arg is hosal_uart_flow_control_t
  275. * - HOSAL_UART_FLOWSTAT_GET : UART flow state get, p_arg is hosal_uart_flow_control_t's pointer
  276. * - HOSAL_UART_FREE_TXFIFO_GET : get uart free tx fifo size (bytes)
  277. * - HOSAL_UART_FREE_RXFIFO_GET : get uart free rx fifo size (bytes)
  278. * - HOSAL_UART_FLUSH : Wait for the send to complete
  279. * - HOSAL_UART_TX_TRIGGER_ON : UART TX trigger on
  280. * - HOSAL_UART_TX_TRIGGER_OFF : UART TX trigger off
  281. * - HOSAL_UART_DMA_CONFIG : p_arg is hosal_uart_dma_cfg_t's pointer
  282. * - HOSAL_UART_DMA_TX_START : UART DMA TX start trans p_arg is hosal_uart_dma_cfg_t's pointer
  283. * - HOSAL_UART_DMA_RX_START : UART DMA RX start trans p_arg is hosal_uart_dma_cfg_t's pointer
  284. *
  285. * @param[in,out] p_arg parameter
  286. *
  287. * @return
  288. * - 0 on success
  289. * - EIO if an error occurred with any step
  290. */
  291. int hosal_uart_ioctl (hosal_uart_dev_t *uart, int ctl, void *p_arg);
  292. /**
  293. * @brief hal uart callback set
  294. *
  295. * @param[in] uart the UART interface
  296. * @param[in] callback_type callback type
  297. * - HOSAL_UART_TX_CALLBACK
  298. * - HOSAL_UART_RX_CALLBACK
  299. * - HOSAL_UART_TX_DMA_CALLBACK
  300. * - HOSAL_UART_RX_DMA_CALLBACK
  301. * @param[in] pfn_callback callback function
  302. * @param[in] arg callback function parameter
  303. *
  304. * @return
  305. * - 0 on success
  306. * - EIO if an error occurred with any step
  307. */
  308. int hosal_uart_callback_set (hosal_uart_dev_t *uart,
  309. int callback_type,
  310. hosal_uart_callback_t pfn_callback,
  311. void *arg);
  312. /**
  313. * @brief Deinitialises a UART interface
  314. *
  315. * @param[in] uart the interface which should be deinitialised
  316. *
  317. * @return
  318. * - 0 on success
  319. * - EIO if an error occurred with any step
  320. */
  321. int hosal_uart_finalize(hosal_uart_dev_t *uart);
  322. /** @} */
  323. #ifdef __cplusplus
  324. }
  325. #endif
  326. #endif /* __HOSAL_UART_H__ */
  327. /* end of file */