nu_usci_uart.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /**************************************************************************//**
  2. * @file nu_usci_uart.h
  3. * @version V3.00
  4. * @brief M480 series USCI UART (UUART) driver header file
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. * @copyright (C) 2016-2020 Nuvoton Technology Corp. All rights reserved.
  8. *****************************************************************************/
  9. #ifndef __NU_USCI_UART_H__
  10. #define __NU_USCI_UART_H__
  11. #ifdef __cplusplus
  12. extern "C"
  13. {
  14. #endif
  15. /** @addtogroup Standard_Driver Standard Driver
  16. @{
  17. */
  18. /** @addtogroup USCI_UART_Driver USCI_UART Driver
  19. @{
  20. */
  21. /** @addtogroup USCI_UART_EXPORTED_CONSTANTS USCI_UART Exported Constants
  22. @{
  23. */
  24. /*---------------------------------------------------------------------------------------------------------*/
  25. /* UUART_LINECTL constants definitions */
  26. /*---------------------------------------------------------------------------------------------------------*/
  27. #define UUART_WORD_LEN_6 (6ul << UUART_LINECTL_DWIDTH_Pos) /*!< UUART_LINECTL setting to set UART word length to 6 bits \hideinitializer */
  28. #define UUART_WORD_LEN_7 (7ul << UUART_LINECTL_DWIDTH_Pos) /*!< UUART_LINECTL setting to set UART word length to 7 bits \hideinitializer */
  29. #define UUART_WORD_LEN_8 (8ul << UUART_LINECTL_DWIDTH_Pos) /*!< UUART_LINECTL setting to set UART word length to 8 bits \hideinitializer */
  30. #define UUART_WORD_LEN_9 (9ul << UUART_LINECTL_DWIDTH_Pos) /*!< UUART_LINECTL setting to set UART word length to 9 bits \hideinitializer */
  31. /*---------------------------------------------------------------------------------------------------------*/
  32. /* UUART_PROTCTL constants definitions */
  33. /*---------------------------------------------------------------------------------------------------------*/
  34. #define UUART_PARITY_NONE (0x0ul << UUART_PROTCTL_PARITYEN_Pos) /*!< UUART_PROTCTL setting to set UART as no parity \hideinitializer */
  35. #define UUART_PARITY_ODD (0x1ul << UUART_PROTCTL_PARITYEN_Pos) /*!< UUART_PROTCTL setting to set UART as odd parity \hideinitializer */
  36. #define UUART_PARITY_EVEN (0x3ul << UUART_PROTCTL_PARITYEN_Pos) /*!< UUART_PROTCTL setting to set UART as even parity \hideinitializer */
  37. #define UUART_STOP_BIT_1 (0x0ul) /*!< UUART_PROTCTL setting for one stop bit \hideinitializer */
  38. #define UUART_STOP_BIT_2 (0x1ul) /*!< UUART_PROTCTL setting for two stop bit \hideinitializer */
  39. /*---------------------------------------------------------------------------------------------------------*/
  40. /* USCI UART interrupt mask definitions */
  41. /*---------------------------------------------------------------------------------------------------------*/
  42. #define UUART_ABR_INT_MASK (0x002ul) /*!< Auto-baud rate interrupt mask \hideinitializer */
  43. #define UUART_RLS_INT_MASK (0x004ul) /*!< Receive line status interrupt mask \hideinitializer */
  44. #define UUART_BUF_RXOV_INT_MASK (0x008ul) /*!< Buffer RX overrun interrupt mask \hideinitializer */
  45. #define UUART_TXST_INT_MASK (0x010ul) /*!< TX start interrupt mask \hideinitializer */
  46. #define UUART_TXEND_INT_MASK (0x020ul) /*!< Tx end interrupt mask \hideinitializer */
  47. #define UUART_RXST_INT_MASK (0x040ul) /*!< RX start interrupt mask \hideinitializer */
  48. #define UUART_RXEND_INT_MASK (0x080ul) /*!< RX end interrupt mask \hideinitializer */
  49. /*@}*/ /* end of group USCI_UART_EXPORTED_CONSTANTS */
  50. /** @addtogroup USCI_UART_EXPORTED_FUNCTIONS USCI_UART Exported Functions
  51. @{
  52. */
  53. /**
  54. * @brief Write USCI_UART data
  55. *
  56. * @param[in] uuart The pointer of the specified USCI_UART module
  57. * @param[in] u8Data Data byte to transmit.
  58. *
  59. * @return None
  60. *
  61. * @details This macro write Data to Tx data register.
  62. * \hideinitializer
  63. */
  64. #define UUART_WRITE(uuart, u8Data) ((uuart)->TXDAT = (u8Data))
  65. /**
  66. * @brief Read USCI_UART data
  67. *
  68. * @param[in] uuart The pointer of the specified USCI_UART module
  69. *
  70. * @return The oldest data byte in RX buffer.
  71. *
  72. * @details This macro read Rx data register.
  73. * \hideinitializer
  74. */
  75. #define UUART_READ(uuart) ((uuart)->RXDAT)
  76. /**
  77. * @brief Get Tx empty
  78. *
  79. * @param[in] uuart The pointer of the specified USCI_UART module
  80. *
  81. * @retval 0 Tx buffer is not empty
  82. * @retval >=1 Tx buffer is empty
  83. *
  84. * @details This macro get Transmitter buffer empty register value.
  85. * \hideinitializer
  86. */
  87. #define UUART_GET_TX_EMPTY(uuart) ((uuart)->BUFSTS & UUART_BUFSTS_TXEMPTY_Msk)
  88. /**
  89. * @brief Get Rx empty
  90. *
  91. * @param[in] uuart The pointer of the specified USCI_UART module
  92. *
  93. * @retval 0 Rx buffer is not empty
  94. * @retval >=1 Rx buffer is empty
  95. *
  96. * @details This macro get Receiver buffer empty register value.
  97. * \hideinitializer
  98. */
  99. #define UUART_GET_RX_EMPTY(uuart) ((uuart)->BUFSTS & UUART_BUFSTS_RXEMPTY_Msk)
  100. /**
  101. * @brief Check specified usci_uart port transmission is over.
  102. *
  103. * @param[in] uuart The pointer of the specified USCI_UART module
  104. *
  105. * @retval 0 Tx transmission is not over
  106. * @retval 1 Tx transmission is over
  107. *
  108. * @details This macro return Transmitter Empty Flag register bit value. \n
  109. * It indicates if specified usci_uart port transmission is over nor not.
  110. * \hideinitializer
  111. */
  112. #define UUART_IS_TX_EMPTY(uuart) (((uuart)->BUFSTS & UUART_BUFSTS_TXEMPTY_Msk) >> UUART_BUFSTS_TXEMPTY_Pos)
  113. /**
  114. * @brief Check specified usci_uart port receiver is empty.
  115. *
  116. * @param[in] uuart The pointer of the specified USCI_UART module
  117. *
  118. * @retval 0 Rx receiver is not empty
  119. * @retval 1 Rx receiver is empty
  120. *
  121. * @details This macro return Receive Empty Flag register bit value. \n
  122. * It indicates if specified usci_uart port receiver is empty nor not.
  123. * \hideinitializer
  124. */
  125. #define UUART_IS_RX_EMPTY(uuart) (((uuart)->BUFSTS & UUART_BUFSTS_RXEMPTY_Msk) >> UUART_BUFSTS_RXEMPTY_Pos)
  126. /**
  127. * @brief Wait specified usci_uart port transmission is over
  128. *
  129. * @param[in] uuart The pointer of the specified USCI_UART module
  130. *
  131. * @return None
  132. *
  133. * @details This macro wait specified usci_uart port transmission is over.
  134. * \hideinitializer
  135. */
  136. #define UUART_WAIT_TX_EMPTY(uuart) while(!((((uuart)->BUFSTS) & UUART_BUFSTS_TXEMPTY_Msk) >> UUART_BUFSTS_TXEMPTY_Pos))
  137. /**
  138. * @brief Check TX buffer is full or not
  139. *
  140. * @param[in] uuart The pointer of the specified USCI_UART module
  141. *
  142. * @retval 1 TX buffer is full
  143. * @retval 0 TX buffer is not full
  144. *
  145. * @details This macro check TX buffer is full or not.
  146. * \hideinitializer
  147. */
  148. #define UUART_IS_TX_FULL(uuart) (((uuart)->BUFSTS & UUART_BUFSTS_TXFULL_Msk)>>UUART_BUFSTS_TXFULL_Pos)
  149. /**
  150. * @brief Check RX buffer is full or not
  151. *
  152. * @param[in] uuart The pointer of the specified USCI_UART module
  153. *
  154. * @retval 1 RX buffer is full
  155. * @retval 0 RX buffer is not full
  156. *
  157. * @details This macro check RX buffer is full or not.
  158. * \hideinitializer
  159. */
  160. #define UUART_IS_RX_FULL(uuart) (((uuart)->BUFSTS & UUART_BUFSTS_RXFULL_Msk)>>UUART_BUFSTS_RXFULL_Pos)
  161. /**
  162. * @brief Get Tx full register value
  163. *
  164. * @param[in] uuart The pointer of the specified USCI_UART module
  165. *
  166. * @retval 0 Tx buffer is not full.
  167. * @retval >=1 Tx buffer is full.
  168. *
  169. * @details This macro get Tx full register value.
  170. * \hideinitializer
  171. */
  172. #define UUART_GET_TX_FULL(uuart) ((uuart)->BUFSTS & UUART_BUFSTS_TXFULL_Msk)
  173. /**
  174. * @brief Get Rx full register value
  175. *
  176. * @param[in] uuart The pointer of the specified USCI_UART module
  177. *
  178. * @retval 0 Rx buffer is not full.
  179. * @retval >=1 Rx buffer is full.
  180. *
  181. * @details This macro get Rx full register value.
  182. * \hideinitializer
  183. */
  184. #define UUART_GET_RX_FULL(uuart) ((uuart)->BUFSTS & UUART_BUFSTS_RXFULL_Msk)
  185. /**
  186. * @brief Enable specified USCI_UART protocol interrupt
  187. *
  188. * @param[in] uuart The pointer of the specified USCI_UART module
  189. * @param[in] u32IntSel Interrupt type select
  190. * - \ref UUART_PROTIEN_RLSIEN_Msk : Rx Line status interrupt
  191. * - \ref UUART_PROTIEN_ABRIEN_Msk : Auto-baud rate interrupt
  192. *
  193. * @return None
  194. *
  195. * @details This macro enable specified USCI_UART protocol interrupt.
  196. * \hideinitializer
  197. */
  198. #define UUART_ENABLE_PROT_INT(uuart, u32IntSel) ((uuart)->PROTIEN |= (u32IntSel))
  199. /**
  200. * @brief Disable specified USCI_UART protocol interrupt
  201. *
  202. * @param[in] uuart The pointer of the specified USCI_UART module
  203. * @param[in] u32IntSel Interrupt type select
  204. * - \ref UUART_PROTIEN_RLSIEN_Msk : Rx Line status interrupt
  205. * - \ref UUART_PROTIEN_ABRIEN_Msk : Auto-baud rate interrupt
  206. *
  207. * @return None
  208. *
  209. * @details This macro disable specified USCI_UART protocol interrupt.
  210. * \hideinitializer
  211. */
  212. #define UUART_DISABLE_PROT_INT(uuart, u32IntSel) ((uuart)->PROTIEN &= ~(u32IntSel))
  213. /**
  214. * @brief Enable specified USCI_UART buffer interrupt
  215. *
  216. * @param[in] uuart The pointer of the specified USCI_UART module
  217. * @param[in] u32IntSel Interrupt type select
  218. * - \ref UUART_BUFCTL_RXOVIEN_Msk : Receive buffer overrun error interrupt
  219. *
  220. * @return None
  221. *
  222. * @details This macro enable specified USCI_UART buffer interrupt.
  223. * \hideinitializer
  224. */
  225. #define UUART_ENABLE_BUF_INT(uuart, u32IntSel) ((uuart)->BUFCTL |= (u32IntSel))
  226. /**
  227. * @brief Disable specified USCI_UART buffer interrupt
  228. *
  229. * @param[in] uuart The pointer of the specified USCI_UART module
  230. * @param[in] u32IntSel Interrupt type select
  231. * - \ref UUART_BUFCTL_RXOVIEN_Msk : Receive buffer overrun error interrupt
  232. *
  233. * @return None
  234. *
  235. * @details This macro disable specified USCI_UART buffer interrupt.
  236. * \hideinitializer
  237. */
  238. #define UUART_DISABLE_BUF_INT(uuart, u32IntSel) ((uuart)->BUFCTL &= ~ (u32IntSel))
  239. /**
  240. * @brief Enable specified USCI_UART transfer interrupt
  241. *
  242. * @param[in] uuart The pointer of the specified USCI_UART module
  243. * @param[in] u32IntSel Interrupt type select
  244. * - \ref UUART_INTEN_RXENDIEN_Msk : Receive end interrupt
  245. * - \ref UUART_INTEN_RXSTIEN_Msk : Receive start interrupt
  246. * - \ref UUART_INTEN_TXENDIEN_Msk : Transmit end interrupt
  247. * - \ref UUART_INTEN_TXSTIEN_Msk : Transmit start interrupt
  248. *
  249. * @return None
  250. *
  251. * @details This macro enable specified USCI_UART transfer interrupt.
  252. * \hideinitializer
  253. */
  254. #define UUART_ENABLE_TRANS_INT(uuart, u32IntSel) ((uuart)->INTEN |= (u32IntSel))
  255. /**
  256. * @brief Disable specified USCI_UART transfer interrupt
  257. *
  258. * @param[in] uuart The pointer of the specified USCI_UART module
  259. * @param[in] u32IntSel Interrupt type select
  260. * - \ref UUART_INTEN_RXENDIEN_Msk : Receive end interrupt
  261. * - \ref UUART_INTEN_RXSTIEN_Msk : Receive start interrupt
  262. * - \ref UUART_INTEN_TXENDIEN_Msk : Transmit end interrupt
  263. * - \ref UUART_INTEN_TXSTIEN_Msk : Transmit start interrupt
  264. *
  265. * @return None
  266. *
  267. * @details This macro disable specified USCI_UART transfer interrupt.
  268. * \hideinitializer
  269. */
  270. #define UUART_DISABLE_TRANS_INT(uuart, u32IntSel) ((uuart)->INTEN &= ~(u32IntSel))
  271. /**
  272. * @brief Get protocol interrupt flag/status
  273. *
  274. * @param[in] uuart The pointer of the specified USCI_UART module
  275. *
  276. * @return The interrupt flag/status of protocol status register.
  277. *
  278. * @details This macro get protocol status register value.
  279. * \hideinitializer
  280. */
  281. #define UUART_GET_PROT_STATUS(uuart) ((uuart)->PROTSTS)
  282. /**
  283. * @brief Clear specified protocol interrupt flag
  284. *
  285. * @param[in] uuart The pointer of the specified USCI_UART module
  286. * @param[in] u32IntTypeFlag Interrupt Type Flag, should be
  287. * - \ref UUART_PROTSTS_ABERRSTS_Msk : Auto-baud Rate Error Interrupt Indicator
  288. * - \ref UUART_PROTSTS_ABRDETIF_Msk : Auto-baud Rate Detected Interrupt Flag
  289. * - \ref UUART_PROTSTS_BREAK_Msk : Break Flag
  290. * - \ref UUART_PROTSTS_FRMERR_Msk : Framing Error Flag
  291. * - \ref UUART_PROTSTS_PARITYERR_Msk : Parity Error Flag
  292. * - \ref UUART_PROTSTS_RXENDIF_Msk : Receive End Interrupt Flag
  293. * - \ref UUART_PROTSTS_RXSTIF_Msk : Receive Start Interrupt Flag
  294. * - \ref UUART_PROTSTS_TXENDIF_Msk : Transmit End Interrupt Flag
  295. * - \ref UUART_PROTSTS_TXSTIF_Msk : Transmit Start Interrupt Flag
  296. *
  297. * @return None
  298. *
  299. * @details This macro clear specified protocol interrupt flag.
  300. * \hideinitializer
  301. */
  302. #define UUART_CLR_PROT_INT_FLAG(uuart,u32IntTypeFlag) ((uuart)->PROTSTS = (u32IntTypeFlag))
  303. /**
  304. * @brief Get transmit/receive buffer interrupt flag/status
  305. *
  306. * @param[in] uuart The pointer of the specified USCI_UART module
  307. *
  308. * @return The interrupt flag/status of buffer status register.
  309. *
  310. * @details This macro get buffer status register value.
  311. * \hideinitializer
  312. */
  313. #define UUART_GET_BUF_STATUS(uuart) ((uuart)->BUFSTS)
  314. /**
  315. * @brief Clear specified buffer interrupt flag
  316. *
  317. * @param[in] uuart The pointer of the specified USCI_UART module
  318. * @param[in] u32IntTypeFlag Interrupt Type Flag, should be
  319. * - \ref UUART_BUFSTS_RXOVIF_Msk : Receive Buffer Over-run Error Interrupt Indicator
  320. *
  321. * @return None
  322. *
  323. * @details This macro clear specified buffer interrupt flag.
  324. * \hideinitializer
  325. */
  326. #define UUART_CLR_BUF_INT_FLAG(uuart,u32IntTypeFlag) ((uuart)->BUFSTS = (u32IntTypeFlag))
  327. /**
  328. * @brief Get wakeup flag
  329. *
  330. * @param[in] uuart The pointer of the specified USCI_UART module
  331. *
  332. * @retval 0 Chip did not wake up from power-down mode.
  333. * @retval 1 Chip waked up from power-down mode.
  334. *
  335. * @details This macro get wakeup flag.
  336. * \hideinitializer
  337. */
  338. #define UUART_GET_WAKEUP_FLAG(uuart) ((uuart)->WKSTS & UUART_WKSTS_WKF_Msk ? 1: 0 )
  339. /**
  340. * @brief Clear wakeup flag
  341. *
  342. * @param[in] uuart The pointer of the specified USCI_UART module
  343. *
  344. * @return None
  345. *
  346. * @details This macro clear wakeup flag.
  347. * \hideinitializer
  348. */
  349. #define UUART_CLR_WAKEUP_FLAG(uuart) ((uuart)->WKSTS = UUART_WKSTS_WKF_Msk)
  350. /**
  351. * @brief Trigger RX PDMA function.
  352. *
  353. * @param[in] uuart The pointer of the specified USCI_UART module.
  354. *
  355. * @return None.
  356. *
  357. * @details Set RXPDMAEN bit of UUART_PDMACTL register to enable RX PDMA transfer function.
  358. * \hideinitializer
  359. */
  360. #define UUART_TRIGGER_RX_PDMA(uuart) ((uuart)->PDMACTL |= UUART_PDMACTL_RXPDMAEN_Msk|UUART_PDMACTL_PDMAEN_Msk)
  361. /**
  362. * @brief Trigger TX PDMA function.
  363. *
  364. * @param[in] uuart The pointer of the specified USCI_UART module.
  365. *
  366. * @return None.
  367. *
  368. * @details Set TXPDMAEN bit of UUART_PDMACTL register to enable TX PDMA transfer function.
  369. * \hideinitializer
  370. */
  371. #define UUART_TRIGGER_TX_PDMA(uuart) ((uuart)->PDMACTL |= UUART_PDMACTL_TXPDMAEN_Msk|UUART_PDMACTL_PDMAEN_Msk)
  372. /**
  373. * @brief Disable RX PDMA transfer.
  374. *
  375. * @param[in] uuart The pointer of the specified USCI_UART module.
  376. *
  377. * @return None.
  378. *
  379. * @details Clear RXPDMAEN bit of UUART_PDMACTL register to disable RX PDMA transfer function.
  380. * \hideinitializer
  381. */
  382. #define UUART_DISABLE_RX_PDMA(uuart) ( (uuart)->PDMACTL &= ~UUART_PDMACTL_RXPDMAEN_Msk )
  383. /**
  384. * @brief Disable TX PDMA transfer.
  385. *
  386. * @param[in] uuart The pointer of the specified USCI_UART module.
  387. *
  388. * @return None.
  389. *
  390. * @details Clear TXPDMAEN bit of UUART_PDMACTL register to disable TX PDMA transfer function.
  391. * \hideinitializer
  392. */
  393. #define UUART_DISABLE_TX_PDMA(uuart) ( (uuart)->PDMACTL &= ~UUART_PDMACTL_TXPDMAEN_Msk )
  394. /**
  395. * @brief Enable specified USCI_UART PDMA function
  396. *
  397. * @param[in] uuart The pointer of the specified USCI_UART module
  398. * @param[in] u32FuncSel Combination of following functions
  399. * - \ref UUART_PDMACTL_TXPDMAEN_Msk
  400. * - \ref UUART_PDMACTL_RXPDMAEN_Msk
  401. * - \ref UUART_PDMACTL_PDMAEN_Msk
  402. *
  403. * @return None
  404. *
  405. * \hideinitializer
  406. */
  407. #define UUART_PDMA_ENABLE(uuart, u32FuncSel) ((uuart)->PDMACTL |= (u32FuncSel))
  408. /**
  409. * @brief Disable specified USCI_UART PDMA function
  410. *
  411. * @param[in] uuart The pointer of the specified USCI_UART module
  412. * @param[in] u32FuncSel Combination of following functions
  413. * - \ref UUART_PDMACTL_TXPDMAEN_Msk
  414. * - \ref UUART_PDMACTL_RXPDMAEN_Msk
  415. * - \ref UUART_PDMACTL_PDMAEN_Msk
  416. *
  417. * @return None
  418. *
  419. * \hideinitializer
  420. */
  421. #define UUART_PDMA_DISABLE(uuart, u32FuncSel) ((uuart)->PDMACTL &= ~(u32FuncSel))
  422. void UUART_ClearIntFlag(UUART_T* uuart, uint32_t u32Mask);
  423. uint32_t UUART_GetIntFlag(UUART_T* uuart, uint32_t u32Mask);
  424. void UUART_Close(UUART_T* uuart);
  425. void UUART_DisableInt(UUART_T* uuart, uint32_t u32Mask);
  426. void UUART_EnableInt(UUART_T* uuart, uint32_t u32Mask);
  427. uint32_t UUART_Open(UUART_T* uuart, uint32_t u32baudrate);
  428. uint32_t UUART_Read(UUART_T* uuart, uint8_t pu8RxBuf[], uint32_t u32ReadBytes);
  429. uint32_t UUART_SetLine_Config(UUART_T* uuart, uint32_t u32baudrate, uint32_t u32data_width, uint32_t u32parity, uint32_t u32stop_bits);
  430. uint32_t UUART_Write(UUART_T* uuart, uint8_t pu8TxBuf[], uint32_t u32WriteBytes);
  431. void UUART_EnableWakeup(UUART_T* uuart, uint32_t u32WakeupMode);
  432. void UUART_DisableWakeup(UUART_T* uuart);
  433. void UUART_EnableFlowCtrl(UUART_T* uuart);
  434. void UUART_DisableFlowCtrl(UUART_T* uuart);
  435. /*@}*/ /* end of group USCI_UART_EXPORTED_FUNCTIONS */
  436. /*@}*/ /* end of group USCI_UART_Driver */
  437. /*@}*/ /* end of group Standard_Driver */
  438. #ifdef __cplusplus
  439. }
  440. #endif
  441. #endif /* __NU_USCI_UART_H__ */
  442. /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/