ald_uart.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /**
  2. *********************************************************************************
  3. *
  4. * @file ald_uart.h
  5. * @brief Header file of UART module library.
  6. *
  7. * @version V1.0
  8. * @date 22 Feb. 2023
  9. * @author AE Team
  10. * @note
  11. * Change Logs:
  12. * Date Author Notes
  13. * 22 Feb. 2023 Lisq The first version
  14. *
  15. * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
  16. *
  17. * SPDX-License-Identifier: Apache-2.0
  18. *
  19. * Licensed under the Apache License, Version 2.0 (the License); you may
  20. * not use this file except in compliance with the License.
  21. * You may obtain a copy of the License at
  22. *
  23. * www.apache.org/licenses/LICENSE-2.0
  24. *
  25. * Unless required by applicable law or agreed to in writing, software
  26. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  27. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  28. * See the License for the specific language governing permissions and
  29. * limitations under the License.
  30. **********************************************************************************
  31. */
  32. #ifndef __ALD_UART_H__
  33. #define __ALD_UART_H__
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif /* __cplusplus */
  37. #include "ald_cmu.h"
  38. #include "ald_dma.h"
  39. /** @addtogroup ES32VF2264_ALD
  40. * @{
  41. */
  42. /** @addtogroup UART
  43. * @{
  44. */
  45. /**
  46. * @defgroup UART_Public_Macros UART Public Macros
  47. * @{
  48. */
  49. #define ALD_UART_RX_ENABLE(hperh) (SET_BIT((hperh)->perh->LCON, UART_LCON_RXEN_MSK))
  50. #define ALD_UART_RX_DISABLE(hperh) (CLEAR_BIT((hperh)->perh->LCON, UART_LCON_RXEN_MSK))
  51. #define ALD_UART_TX_ENABLE(hperh) (SET_BIT((hperh)->perh->LCON, UART_LCON_TXEN_MSK))
  52. #define ALD_UART_TX_DISABLE(hperh) (CLEAR_BIT((hperh)->perh->LCON, UART_LCON_TXEN_MSK))
  53. #define ALD_UART_RX_TIMEOUT_ENABLE(hperh) (SET_BIT((hperh)->perh->RTOR, UART_RTOR_RTOEN_MSK))
  54. #define ALD_UART_RX_TIMEOUT_DISABLE(hperh) (CLEAR_BIT((hperh)->perh->RTOR, UART_RTOR_RTOEN_MSK))
  55. #define ALD_UART_MSB_FIRST_ENABLE(hperh) (SET_BIT((hperh)->perh->LCON, UART_LCON_MSB_MSK))
  56. #define ALD_UART_MSB_FIRST_DISABLE(hperh) (CLEAR_BIT((hperh)->perh->LCON, UART_LCON_MSB_MSK))
  57. #define ALD_UART_DATA_INV_ENABLE(hperh) (SET_BIT((hperh)->perh->LCON, UART_LCON_DATAINV_MSK))
  58. #define ALD_UART_DATA_INV_DISABLE(hperh) (CLEAR_BIT((hperh)->perh->LCON, UART_LCON_DATAINV_MSK))
  59. #define ALD_UART_RX_INV_ENABLE(hperh) (SET_BIT((hperh)->perh->LCON, UART_LCON_RXINV_MSK))
  60. #define ALD_UART_RX_INV_DISABLE(hperh) (CLEAR_BIT((hperh)->perh->LCON, UART_LCON_RXINV_MSK))
  61. #define ALD_UART_TX_INV_ENABLE(hperh) (SET_BIT((hperh)->perh->LCON, UART_LCON_TXINV_MSK))
  62. #define ALD_UART_TX_INV_DISABLE(hperh) (CLEAR_BIT((hperh)->perh->LCON, UART_LCON_TXINV_MSK))
  63. #define ALD_UART_TX_RX_SWAP_ENABLE(hperh) (SET_BIT((hperh)->perh->LCON, UART_LCON_SWAP_MSK))
  64. #define ALD_UART_TX_RX_SWAP_DISABLE(hperh) (CLEAR_BIT((hperh)->perh->LCON, UART_LCON_SWAP_MSK))
  65. #define ALD_UART_HDSEL_ENABLE(hperh) (SET_BIT((hperh)->perh->MCON, UART_MCON_HDEN_MSK))
  66. #define ALD_UART_HDSEL_DISABLE(hperh) (CLEAR_BIT((hperh)->perh->MCON, UART_MCON_HDEN_MSK))
  67. #define ALD_UART_LPBMOD_ENABLE(hperh) (SET_BIT((hperh)->perh->MCON, UART_MCON_LPBKEN_MSK))
  68. #define ALD_UART_LPBMOD_DISABLE(hperh) (CLEAR_BIT((hperh)->perh->MCON, UART_MCON_LPBKEN_MSK))
  69. #define ALD_UART_AUTOBR_ENABLE(hperh) (SET_BIT((hperh)->perh->MCON, UART_MCON_ABREN_MSK))
  70. #define ALD_UART_AUTOBR_DISABLE(hperh) (CLEAR_BIT((hperh)->perh->MCON, UART_MCON_ABREN_MSK))
  71. #define ALD_UART_AUTOBR_REPT(hperh) (SET_BIT((hperh)->perh->MCON, UART_MCON_ABRREPT_MSK))
  72. #define ALD_UART_GET_BRR_VALUE(hperh) (READ_REG((hperh)->perh->BRR))
  73. #define ALD_UART_SET_TIMEOUT_VALUE(x, y) (MODIFY_REG((x)->perh->RTOR, UART_RTOR_RTO_MSK, (y) << UART_RTOR_RTO_POSS))
  74. #define ALD_UART_ENABLE_SCARD_CLK(hperh) (SET_BIT((hperh)->perh->SCARD, UART_SCARD_SCLKEN_MSK))
  75. #define ALD_UART_DISABLE_SCARD_CLK(hperh) (CLEAR_BIT((hperh)->perh->SCARD, UART_SCARD_SCLKEN_MSK))
  76. #define ALD_UART_ENABLE_SCARD_NACK(hperh) (SET_BIT((hperh)->perh->SCARD, UART_SCARD_SCNACK_MSK))
  77. #define ALD_UART_DISABLE_SCARD_NACK(hperh) (CLEAR_BIT((hperh)->perh->SCARD, UART_SCARD_SCNACK_MSK))
  78. #define ALD_UART_SCARD_ENABLE(hperh) (SET_BIT((hperh)->perh->SCARD, UART_SCARD_SCEN_MSK))
  79. #define ALD_UART_SCARD_DISABLE(hperh) (CLEAR_BIT((hperh)->perh->SCARD, UART_SCARD_SCEN_MSK))
  80. /**
  81. * @}
  82. */
  83. /** @defgroup UART_Public_Types UART Public Types
  84. * @{
  85. */
  86. /**
  87. * @brief UART word length
  88. */
  89. typedef enum {
  90. ALD_UART_WORD_LENGTH_8B = 0x0U, /**< 8-bits */
  91. ALD_UART_WORD_LENGTH_7B = 0x1U, /**< 7-bits */
  92. ALD_UART_WORD_LENGTH_6B = 0x2U, /**< 6-bits */
  93. ALD_UART_WORD_LENGTH_5B = 0x3U, /**< 5-bits */
  94. } ald_uart_word_length_t;
  95. /**
  96. * @brief UART stop bits
  97. */
  98. typedef enum {
  99. ALD_UART_STOP_BITS_1 = 0x0U, /**< 1-bits */
  100. ALD_UART_STOP_BITS_2 = 0x1U, /**< 2-bits */
  101. ALD_UART_STOP_BITS_0_5 = 0x0U, /**< 0.5-bits, using smartcard mode */
  102. ALD_UART_STOP_BITS_1_5 = 0x1U, /**< 1.5-bits, using smartcard mode */
  103. } ald_uart_stop_bits_t;
  104. /**
  105. * @brief UART parity
  106. */
  107. typedef enum {
  108. ALD_UART_PARITY_NONE = 0x0U, /**< Not parity */
  109. ALD_UART_PARITY_ODD = 0x1U, /**< Odd parity */
  110. ALD_UART_PARITY_EVEN = 0x3U, /**< Even parity */
  111. } ald_uart_parity_t;
  112. /**
  113. * @brief UART mode
  114. */
  115. typedef enum {
  116. ALD_UART_MODE_UART = 0x0U, /**< UART */
  117. ALD_UART_MODE_LIN = 0x1U, /**< LIN */
  118. ALD_UART_MODE_IrDA = 0x2U, /**< IrDA */
  119. ALD_UART_MODE_RS485 = 0x3U, /**< RS485 */
  120. ALD_UART_MODE_HDSEL = 0x4U, /**< Single-wire half-duplex */
  121. ALD_UART_MODE_SCARD = 0x5U, /**< Smart card */
  122. } ald_uart_mode_t;
  123. /**
  124. * @brief UART hardware flow control
  125. */
  126. typedef enum {
  127. ALD_UART_HW_FLOW_CTL_DISABLE = 0x0U, /**< Auto-flow-control disable */
  128. ALD_UART_HW_FLOW_CTL_ENABLE = 0x1U, /**< Auto-flow-control enable */
  129. } ald_uart_hw_flow_ctl_t;
  130. /**
  131. * @brief ALD UART state
  132. */
  133. typedef enum {
  134. ALD_UART_STATE_RESET = 0x00U, /**< Peripheral is not initialized */
  135. ALD_UART_STATE_READY = 0x01U, /**< Peripheral Initialized and ready for use */
  136. ALD_UART_STATE_BUSY = 0x02U, /**< an internal process is ongoing */
  137. ALD_UART_STATE_BUSY_TX = 0x11U, /**< Data Transmission process is ongoing */
  138. ALD_UART_STATE_BUSY_RX = 0x21U, /**< Data Reception process is ongoing */
  139. ALD_UART_STATE_BUSY_TX_RX = 0x31U, /**< Data Transmission Reception process is ongoing */
  140. ALD_UART_STATE_TIMEOUT = 0x03U, /**< Timeout state */
  141. ALD_UART_STATE_ERROR = 0x04U, /**< Error */
  142. } ald_uart_state_t;
  143. /**
  144. * @brief UART error codes
  145. */
  146. typedef enum {
  147. ALD_UART_ERROR_NONE = ((uint32_t)0x00U), /**< No error */
  148. ALD_UART_ERROR_PE = ((uint32_t)0x01U), /**< Parity error */
  149. ALD_UART_ERROR_NE = ((uint32_t)0x02U), /**< Noise error */
  150. ALD_UART_ERROR_FE = ((uint32_t)0x04U), /**< frame error */
  151. ALD_UART_ERROR_ORE = ((uint32_t)0x08U), /**< Overrun error */
  152. ALD_UART_ERROR_DMA = ((uint32_t)0x10U), /**< DMA transfer error */
  153. } ald_uart_error_t;
  154. /**
  155. * @brief UART init structure definition
  156. */
  157. typedef struct {
  158. uint32_t baud; /**< Specifies the uart communication baud rate */
  159. ald_uart_word_length_t word_length; /**< Specifies the number of data bits transmitted or received in a frame */
  160. ald_uart_stop_bits_t stop_bits; /**< Specifies the number of stop bits transmitted */
  161. ald_uart_parity_t parity; /**< Specifies the parity mode */
  162. ald_uart_mode_t mode; /**< Specifies uart mode */
  163. ald_uart_hw_flow_ctl_t fctl; /**< Specifies wether the hardware flow control mode is enabled or disabled */
  164. } ald_uart_init_t;
  165. /**
  166. * @brief UART handle structure definition
  167. */
  168. typedef struct ald_uart_handle_s {
  169. UART_TypeDef *perh; /**< UART registers base address */
  170. ald_uart_init_t init; /**< UART communication parameters */
  171. uint8_t *tx_buf; /**< Pointer to UART Tx transfer Buffer */
  172. uint16_t tx_size; /**< UART Tx Transfer size */
  173. uint16_t tx_count; /**< UART Tx Transfer Counter */
  174. uint8_t *rx_buf; /**< Pointer to UART Rx transfer Buffer */
  175. uint16_t rx_size; /**< UART Rx Transfer size */
  176. uint16_t rx_count; /**< UART Rx Transfer Counter */
  177. ald_dma_handle_t hdmatx; /**< UART Tx DMA Handle parameters */
  178. ald_dma_handle_t hdmarx; /**< UART Rx DMA Handle parameters */
  179. lock_state_t lock; /**< Locking object */
  180. ald_uart_state_t state; /**< UART communication state */
  181. ald_uart_error_t err_code; /**< UART Error code */
  182. void (*tx_cplt_cbk)(struct ald_uart_handle_s *arg); /**< Tx completed callback */
  183. void (*rx_cplt_cbk)(struct ald_uart_handle_s *arg); /**< Rx completed callback */
  184. void (*error_cbk)(struct ald_uart_handle_s *arg); /**< error callback */
  185. } ald_uart_handle_t;
  186. /**
  187. * @brief UART RS485 configure structure definition
  188. */
  189. typedef struct {
  190. type_func_t normal; /**< Normal mode */
  191. type_func_t dir; /**< Auto-direction mode */
  192. type_func_t invert; /**< Address detection invert */
  193. uint8_t addr; /**< Address for compare */
  194. } ald_uart_rs485_config_t;
  195. /**
  196. * @brief Smart_card Rx/Tx handle retry time
  197. */
  198. typedef enum {
  199. ALD_SCARD_RETRY_CNT0 = 0x0U, /**< retry time 0 */
  200. ALD_SCARD_RETRY_CNT1 = 0x1U, /**< retry time 1 */
  201. ALD_SCARD_RETRY_CNT2 = 0x2U, /**< retry time 2 */
  202. ALD_SCARD_RETRY_CNT3 = 0x3U, /**< retry time 3 */
  203. ALD_SCARD_RETRY_CNT4 = 0x4U, /**< retry time 4 */
  204. ALD_SCARD_RETRY_CNT5 = 0x5U, /**< retry time 5 */
  205. ALD_SCARD_RETRY_CNT6 = 0x6U, /**< retry time 6 */
  206. ALD_SCARD_RETRY_CNT7 = 0x7U, /**< retry time 7 */
  207. } ald_scard_retry_t;
  208. /**
  209. * @brief UART Smart card configure structure definition
  210. */
  211. typedef struct {
  212. uint8_t block_len; /**< Specifies the data block length.*/
  213. uint8_t pt; /**< Specifies the protect time*/
  214. ald_scard_retry_t retry; /**< Specifies retry time.*/
  215. uint8_t clk_div; /**< Specifies the clock division.*/
  216. type_func_t clk_out; /**< Specifies the clock out */
  217. } ald_uart_scard_config_t;
  218. /**
  219. * @brief LIN detection break length
  220. */
  221. typedef enum {
  222. ALD_LIN_BREAK_LEN_10B = 0x0U, /**< 10-bit break */
  223. ALD_LIN_BREAK_LEN_11B = 0x1U, /**< 11-bit break */
  224. } ald_uart_lin_break_len_t;
  225. /**
  226. * @brief UART auto-baud mode
  227. */
  228. typedef enum {
  229. ALD_UART_ABRMOD_1_TO_0 = 0x0U, /**< Detect bit0:1, bit1:0 */
  230. ALD_UART_ABRMOD_1 = 0x1U, /**< Detect bit0:1 */
  231. ALD_UART_ABRMOD_0_TO_1 = 0x2U, /**< Detect bit0:0, bit1:1 */
  232. } ald_uart_auto_baud_mode_t;
  233. /**
  234. * @brief UART DMA Requests
  235. */
  236. typedef enum {
  237. ALD_UART_DMA_REQ_TX = 0x0U, /**< TX dma */
  238. ALD_UART_DMA_REQ_RX = 0x1U, /**< RX dma */
  239. } ald_uart_dma_req_t;
  240. /**
  241. * @brief UART status types
  242. */
  243. typedef enum {
  244. ALD_UART_STATUS_PERR = (1U << 0), /**< Parity error */
  245. ALD_UART_STATUS_FERR = (1U << 1), /**< Framing error */
  246. ALD_UART_STATUS_BKERR = (1U << 2), /**< Break error */
  247. ALD_UART_STATUS_CTSSTA = (1U << 3), /**< Clear to send status */
  248. ALD_UART_STATUS_RSBUSY = (1U << 8), /**< Receive shif register busy */
  249. ALD_UART_STATUS_RFNEMPTY = (1U << 10), /**< Receive FIFO not empty */
  250. ALD_UART_STATUS_RFOERR = (1U << 12), /**< Reveive FIFO overrun error */
  251. ALD_UART_STATUS_RFUERR = (1U << 13), /**< Receive FIFO underrun error */
  252. ALD_UART_STATUS_TSBUSY = (1U << 14), /**< Transmit shit register busy */
  253. ALD_UART_STATUS_TFEMPTY = (1U << 16), /**< Transmit FIFO empty */
  254. ALD_UART_STATUS_TFOERR = (1U << 18), /**< Transmit FIFO overrun error */
  255. } ald_uart_status_t;
  256. /**
  257. * @brief UART interrupt types
  258. */
  259. typedef enum {
  260. ALD_UART_IT_RXBERR = (1U << 0), /**< Receiver byte error */
  261. ALD_UART_IT_ABEND = (1U << 1), /**< Auto-Baud rate detection end */
  262. ALD_UART_IT_ABTO = (1U << 2), /**< Auto-Baud rate detection timeout */
  263. ALD_UART_IT_DCTS = (1U << 3), /**< Delta CTS status */
  264. ALD_UART_IT_RXTO = (1U << 4), /**< Receiver timeout */
  265. ALD_UART_IT_ADDRM = (1U << 5), /**< Addredd match */
  266. ALD_UART_IT_LINBK = (1U << 6), /**< Lin break detection */
  267. ALD_UART_IT_EOB = (1U << 7), /**< End of block */
  268. ALD_UART_IT_NOISE = (1U << 8), /**< Start bit noise detection */
  269. ALD_UART_IT_RFNEMPTY = (1U << 10), /**< Receive FIFO not empty */
  270. ALD_UART_IT_RFFULL = (1U << 11), /**< Receive FIFO full */
  271. ALD_UART_IT_RFOERR = (1U << 12), /**< Receive FIFO overrun */
  272. ALD_UART_IT_RFUERR = (1U << 13), /**< Reveive FIFO underrun */
  273. ALD_UART_IT_TBC = (1U << 14), /**< Transmit shift register empty */
  274. ALD_UART_IT_TFEMPTY = (1U << 16), /**< Transmit FIFO empty */
  275. ALD_UART_IT_TFOERR = (1U << 18), /**< Transmit FIFO overrun error */
  276. } ald_uart_it_t;
  277. /**
  278. * @brief UART flags types
  279. */
  280. typedef enum {
  281. ALD_UART_IF_RXBERR = (1U << 0), /**< Receiver byte error */
  282. ALD_UART_IF_ABEND = (1U << 1), /**< Auto-Baud rate detection end */
  283. ALD_UART_IF_ABTO = (1U << 2), /**< Auto-Baud rate detection timeout */
  284. ALD_UART_IF_DCTS = (1U << 3), /**< Delta CTS status */
  285. ALD_UART_IF_RXTO = (1U << 4), /**< Receiver timeout */
  286. ALD_UART_IF_ADDRM = (1U << 5), /**< Addredd match */
  287. ALD_UART_IF_LINBK = (1U << 6), /**< Lin break detection */
  288. ALD_UART_IF_EOB = (1U << 7), /**< End of block */
  289. ALD_UART_IF_NOISE = (1U << 8), /**< Start bit noise detection */
  290. ALD_UART_IF_RFNEMPTY = (1U << 10), /**< Receive FIFO not empty */
  291. ALD_UART_IF_RFFULL = (1U << 11), /**< Receive FIFO full */
  292. ALD_UART_IF_RFOERR = (1U << 12), /**< Receive FIFO overrun */
  293. ALD_UART_IF_RFUERR = (1U << 13), /**< Reveive FIFO underrun */
  294. ALD_UART_IF_TBC = (1U << 14), /**< Transmit shift register empty */
  295. ALD_UART_IF_TFEMPTY = (1U << 16), /**< Transmit FIFO empty */
  296. ALD_UART_IF_TFOVER = (1U << 18), /**< Transmit FIFO overrun */
  297. } ald_uart_flag_t;
  298. /**
  299. * @}
  300. */
  301. /** @defgroup UART_Private_Macros UART Private Macros
  302. * @{
  303. */
  304. #define IS_UART_ENHANCE(x)(((x) == EUART0) || \
  305. ((x) == EUART1))
  306. #define IS_UART_ALL(x) (((x) == EUART0) || \
  307. ((x) == EUART1) || \
  308. ((x) == CUART0) || \
  309. ((x) == CUART1) || \
  310. ((x) == CUART2))
  311. #define IS_UART_WORD_LENGTH(x) (((x) == ALD_UART_WORD_LENGTH_5B) || \
  312. ((x) == ALD_UART_WORD_LENGTH_6B) || \
  313. ((x) == ALD_UART_WORD_LENGTH_7B) || \
  314. ((x) == ALD_UART_WORD_LENGTH_8B))
  315. #define IS_UART_STOPBITS(x) (((x) == ALD_UART_STOP_BITS_1) || \
  316. ((x) == ALD_UART_STOP_BITS_2) || \
  317. ((x) == ALD_UART_STOP_BITS_0_5) || \
  318. ((x) == ALD_UART_STOP_BITS_1_5))
  319. #define IS_UART_PARITY(x) (((x) == ALD_UART_PARITY_NONE) || \
  320. ((x) == ALD_UART_PARITY_ODD) || \
  321. ((x) == ALD_UART_PARITY_EVEN))
  322. #define IS_UART_MODE(x) (((x) == ALD_UART_MODE_UART) || \
  323. ((x) == ALD_UART_MODE_LIN) || \
  324. ((x) == ALD_UART_MODE_IrDA) || \
  325. ((x) == ALD_UART_MODE_HDSEL) || \
  326. ((x) == ALD_UART_MODE_SCARD) || \
  327. ((x) == ALD_UART_MODE_RS485))
  328. #define IS_UART_HARDWARE_FLOW_CONTROL(x) \
  329. (((x) == ALD_UART_HW_FLOW_CTL_DISABLE) || \
  330. ((x) == ALD_UART_HW_FLOW_CTL_ENABLE))
  331. #define IS_UART_LIN_BREAK_LEN(x) (((x) == ALD_LIN_BREAK_LEN_10B) || \
  332. ((x) == ALD_LIN_BREAK_LEN_11B))
  333. #define IS_UART_AUTO_BAUD_MODE(x) (((x) == ALD_UART_ABRMOD_1_TO_0) || \
  334. ((x) == ALD_UART_ABRMOD_1) || \
  335. ((x) == ALD_UART_ABRMOD_0_TO_1))
  336. #define IS_UART_DMA_REQ(x) (((x) == ALD_UART_DMA_REQ_TX) || \
  337. ((x) == ALD_UART_DMA_REQ_RX))
  338. #define IS_UART_STATUS(x) (((x) == ALD_UART_STATUS_PERR) || \
  339. ((x) == ALD_UART_STATUS_FERR) || \
  340. ((x) == ALD_UART_STATUS_BKERR) || \
  341. ((x) == ALD_UART_STATUS_CTSSTA) || \
  342. ((x) == ALD_UART_STATUS_RSBUSY) || \
  343. ((x) == ALD_UART_STATUS_RFNEMPTY) || \
  344. ((x) == ALD_UART_STATUS_RFOERR) || \
  345. ((x) == ALD_UART_STATUS_RFUERR) || \
  346. ((x) == ALD_UART_STATUS_TSBUSY) || \
  347. ((x) == ALD_UART_STATUS_TFEMPTY) || \
  348. ((x) == ALD_UART_STATUS_TFOERR))
  349. #define IS_UART_IT(x) (((x) == ALD_UART_IT_RXBERR) || \
  350. ((x) == ALD_UART_IT_ABEND) || \
  351. ((x) == ALD_UART_IT_ABTO) || \
  352. ((x) == ALD_UART_IT_DCTS) || \
  353. ((x) == ALD_UART_IT_RXTO) || \
  354. ((x) == ALD_UART_IT_ADDRM) || \
  355. ((x) == ALD_UART_IT_LINBK) || \
  356. ((x) == ALD_UART_IT_EOB) || \
  357. ((x) == ALD_UART_IT_NOISE) || \
  358. ((x) == ALD_UART_IT_RFNEMPTY) || \
  359. ((x) == ALD_UART_IT_RFOERR) || \
  360. ((x) == ALD_UART_IT_RFUERR) || \
  361. ((x) == ALD_UART_IT_TBC) || \
  362. ((x) == ALD_UART_IT_TFEMPTY) || \
  363. ((x) == ALD_UART_IT_TFOERR))
  364. #define IS_UART_IF(x) (((x) == ALD_UART_IF_RXBERR) || \
  365. ((x) == ALD_UART_IF_ABEND) || \
  366. ((x) == ALD_UART_IF_ABTO) || \
  367. ((x) == ALD_UART_IF_DCTS) || \
  368. ((x) == ALD_UART_IF_RXTO) || \
  369. ((x) == ALD_UART_IF_ADDRM) || \
  370. ((x) == ALD_UART_IF_LINBK) || \
  371. ((x) == ALD_UART_IF_EOB) || \
  372. ((x) == ALD_UART_IF_NOISE) || \
  373. ((x) == ALD_UART_IF_RFNEMPTY) || \
  374. ((x) == ALD_UART_IF_RFOERR) || \
  375. ((x) == ALD_UART_IF_RFUERR) || \
  376. ((x) == ALD_UART_IF_TBC) || \
  377. ((x) == ALD_UART_IF_TFEMPTY) || \
  378. ((x) == ALD_UART_IF_TFOVER))
  379. #define IS_UART_SCARD_CLK(x) (((x) <= 0x1F))
  380. #define IS_UART_BAUDRATE(x) (((x) > 0) && ((x) < 0x44AA21))
  381. #define IS_UART_DATA(x) ((x) <= 0x1FF)
  382. #define ALD_UART_STATE_TX_MASK (1U << 4)
  383. #define ALD_UART_STATE_RX_MASK (1U << 5)
  384. /**
  385. * @}
  386. */
  387. /** @addtogroup UART_Public_Functions
  388. * @{
  389. */
  390. /** @addtogroup UART_Public_Functions_Group1
  391. * @{
  392. */
  393. /* Initialization functions */
  394. void ald_uart_init(ald_uart_handle_t *hperh);
  395. void ald_uart_reset(ald_uart_handle_t *hperh);
  396. void ald_uart_rs485_config(ald_uart_handle_t *hperh, ald_uart_rs485_config_t *config);
  397. void ald_uart_scard_config(ald_uart_handle_t *hperh, ald_uart_scard_config_t *config);
  398. /**
  399. * @}
  400. */
  401. /** @addtogroup UART_Public_Functions_Group2
  402. * @{
  403. */
  404. /* IO operation functions */
  405. ald_status_t ald_uart_send(ald_uart_handle_t *hperh, uint8_t *buf, uint16_t size, uint32_t timeout);
  406. ald_status_t ald_uart_recv(ald_uart_handle_t *hperh, uint8_t *buf, uint16_t size, uint32_t timeout);
  407. ald_status_t ald_uart_send_n_lock(ald_uart_handle_t *hperh, uint8_t *buf, uint16_t size, uint32_t timeout);
  408. ald_status_t ald_uart_recv_n_lock(ald_uart_handle_t *hperh, uint8_t *buf, uint16_t size, uint32_t timeout);
  409. ald_status_t ald_uart_send_by_it(ald_uart_handle_t *hperh, uint8_t *buf, uint16_t size);
  410. ald_status_t ald_uart_recv_by_it(ald_uart_handle_t *hperh, uint8_t *buf, uint16_t size);
  411. ald_status_t ald_uart_recv_frame_by_it(ald_uart_handle_t *hperh, uint8_t *buf, uint16_t size, uint32_t t_out);
  412. ald_status_t ald_uart_send_by_dma(ald_uart_handle_t *hperh, uint8_t *buf, uint16_t size, uint8_t channel);
  413. ald_status_t ald_uart_recv_by_dma(ald_uart_handle_t *hperh, uint8_t *buf, uint16_t size, uint8_t channel);
  414. ald_status_t ald_uart_dma_pause(ald_uart_handle_t *hperh);
  415. ald_status_t ald_uart_dma_resume(ald_uart_handle_t *hperh);
  416. ald_status_t ald_uart_dma_stop(ald_uart_handle_t *hperh);
  417. void ald_uart_irq_handler(ald_uart_handle_t *hperh);
  418. void ald_uart_irq_handler_fast(ald_uart_handle_t *hperh);
  419. /**
  420. * @}
  421. */
  422. /** @addtogroup UART_Public_Functions_Group3
  423. * @{
  424. */
  425. /* Peripheral Control functions */
  426. void ald_uart_interrupt_config(ald_uart_handle_t *hperh, ald_uart_it_t it, type_func_t state);
  427. void ald_uart_dma_req_config(ald_uart_handle_t *hperh, ald_uart_dma_req_t req, type_func_t state);
  428. void uart_lin_break_detect_irq(ald_uart_handle_t *hperh, type_func_t status);
  429. void ald_uart_lin_send_break(ald_uart_handle_t *hperh);
  430. void ald_uart_lin_detect_break_len_config(ald_uart_handle_t *hperh, ald_uart_lin_break_len_t len);
  431. void ald_uart_auto_baud_config(ald_uart_handle_t *hperh, ald_uart_auto_baud_mode_t mode);
  432. ald_status_t ald_uart_rs485_send_addr(ald_uart_handle_t *hperh, uint16_t addr, uint32_t timeout);
  433. it_status_t ald_uart_get_it_status(ald_uart_handle_t *hperh, ald_uart_it_t it);
  434. flag_status_t ald_uart_get_status(ald_uart_handle_t *hperh, ald_uart_status_t status);
  435. flag_status_t ald_uart_get_flag_status(ald_uart_handle_t *hperh, ald_uart_flag_t flag);
  436. flag_status_t ald_uart_get_mask_flag_status(ald_uart_handle_t *hperh, ald_uart_flag_t flag);
  437. void ald_uart_clear_flag_status(ald_uart_handle_t *hperh, ald_uart_flag_t flag);
  438. /**
  439. * @}
  440. */
  441. /** @addtogroup UART_Public_Functions_Group4
  442. * @{
  443. */
  444. /* Peripheral State and Errors functions */
  445. ald_uart_state_t ald_uart_get_state(ald_uart_handle_t *hperh);
  446. uint32_t ald_uart_get_error(ald_uart_handle_t *hperh);
  447. /**
  448. * @}
  449. */
  450. /**
  451. * @}
  452. */
  453. /**
  454. * @}
  455. */
  456. /**
  457. * @}
  458. */
  459. #ifdef __cplusplus
  460. }
  461. #endif /* __cplusplus */
  462. #endif /* __ALD_UART_H__ */