nu_scuart.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /**************************************************************************//**
  2. * @file nu_scuart.h
  3. * @version V3.00
  4. * @brief Smartcard UART mode (SCUART) driver header file
  5. *
  6. * @copyright SPDX-License-Identifier: Apache-2.0
  7. * @copyright Copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
  8. *****************************************************************************/
  9. #ifndef __NU_SCUART_H__
  10. #define __NU_SCUART_H__
  11. #ifdef __cplusplus
  12. extern "C"
  13. {
  14. #endif
  15. /** @addtogroup Standard_Driver Standard Driver
  16. @{
  17. */
  18. /** @addtogroup SCUART_Driver SCUART Driver
  19. @{
  20. */
  21. /** @addtogroup SCUART_EXPORTED_CONSTANTS SCUART Exported Constants
  22. @{
  23. */
  24. #define SCUART_CHAR_LEN_5 (0x3UL << SC_UARTCTL_WLS_Pos) /*!< Set SCUART word length to 5 bits \hideinitializer */
  25. #define SCUART_CHAR_LEN_6 (0x2UL << SC_UARTCTL_WLS_Pos) /*!< Set SCUART word length to 6 bits \hideinitializer */
  26. #define SCUART_CHAR_LEN_7 (0x1UL << SC_UARTCTL_WLS_Pos) /*!< Set SCUART word length to 7 bits \hideinitializer */
  27. #define SCUART_CHAR_LEN_8 (0UL) /*!< Set SCUART word length to 8 bits \hideinitializer */
  28. #define SCUART_PARITY_NONE (SC_UARTCTL_PBOFF_Msk) /*!< Set SCUART transfer with no parity \hideinitializer */
  29. #define SCUART_PARITY_ODD (SC_UARTCTL_OPE_Msk) /*!< Set SCUART transfer with odd parity \hideinitializer */
  30. #define SCUART_PARITY_EVEN (0UL) /*!< Set SCUART transfer with even parity \hideinitializer */
  31. #define SCUART_STOP_BIT_1 (SC_CTL_NSB_Msk) /*!< Set SCUART transfer with one stop bit \hideinitializer */
  32. #define SCUART_STOP_BIT_2 (0UL) /*!< Set SCUART transfer with two stop bits \hideinitializer */
  33. /**@}*/ /* end of group SCUART_EXPORTED_CONSTANTS */
  34. /** @addtogroup SCUART_EXPORTED_FUNCTIONS SCUART Exported Functions
  35. @{
  36. */
  37. /* TX Macros */
  38. /**
  39. * @brief Write Data to Tx data register
  40. *
  41. * @param[in] sc The pointer of smartcard module.
  42. * @param[in] u8Data Data byte to transmit.
  43. *
  44. * @return None
  45. *
  46. * @details By writing data to DAT register, the SC will send out an 8-bit data.
  47. * \hideinitializer
  48. */
  49. #define SCUART_WRITE(sc, u8Data) ((sc)->DAT = (u8Data))
  50. /**
  51. * @brief Get Tx FIFO empty flag status from register
  52. *
  53. * @param[in] sc The pointer of smartcard module.
  54. *
  55. * @return Transmit FIFO empty status
  56. * @retval 0 Transmit FIFO is not empty
  57. * @retval SC_STATUS_TXEMPTY_Msk Transmit FIFO is empty
  58. *
  59. * @details When the last byte of Tx buffer has been transferred to Transmitter Shift Register, hardware sets TXEMPTY (SC_STATUS[9]) high.
  60. * It will be cleared when writing data into DAT (SC_DAT[7:0]).
  61. * \hideinitializer
  62. */
  63. #define SCUART_GET_TX_EMPTY(sc) ((sc)->STATUS & SC_STATUS_TXEMPTY_Msk)
  64. /**
  65. * @brief Get Tx FIFO full flag status from register
  66. *
  67. * @param[in] sc The pointer of smartcard module.
  68. *
  69. * @return Transmit FIFO full status
  70. * @retval 0 Transmit FIFO is not full
  71. * @retval SC_STATUS_TXFULL_Msk Transmit FIFO is full
  72. *
  73. * @details TXFULL (SC_STATUS[10]) is set when Tx buffer counts equals to 4, otherwise is cleared by hardware.
  74. * \hideinitializer
  75. */
  76. #define SCUART_GET_TX_FULL(sc) ((sc)->STATUS & SC_STATUS_TXFULL_Msk)
  77. /**
  78. * @brief Wait specified smartcard port transmission complete
  79. *
  80. * @param[in] sc The pointer of smartcard module.
  81. *
  82. * @return None
  83. *
  84. * @details TXACT (SC_STATUS[31]) is cleared automatically when Tx transfer is finished or the last byte transmission has completed.
  85. *
  86. * @note This macro blocks until transmit complete.
  87. * \hideinitializer
  88. */
  89. #define SCUART_WAIT_TX_EMPTY(sc) while(((sc)->STATUS & SC_STATUS_TXACT_Msk) == SC_STATUS_TXACT_Msk)
  90. /**
  91. * @brief Check specified smartcard port transmit FIFO is full or not
  92. *
  93. * @param[in] sc The pointer of smartcard module.
  94. *
  95. * @return Transmit FIFO full status
  96. * @retval 0 Transmit FIFO is not full
  97. * @retval 1 Transmit FIFO is full
  98. *
  99. * @details TXFULL (SC_STATUS[10]) indicates Tx buffer full or not.
  100. * This bit is set when Tx buffer counts equals to 4, otherwise is cleared by hardware.
  101. * \hideinitializer
  102. */
  103. #define SCUART_IS_TX_FULL(sc) (((sc)->STATUS & SC_STATUS_TXFULL_Msk)? 1 : 0)
  104. /**
  105. * @brief Check specified smartcard port transmission is over
  106. *
  107. * @param[in] sc The pointer of smartcard module.
  108. *
  109. * @return Transmit complete status
  110. * @retval 0 Transmit is not complete
  111. * @retval 1 Transmit complete
  112. *
  113. * @details TXACT (SC_STATUS[31]) indicates Tx Transmit is complete or not.
  114. * \hideinitializer
  115. */
  116. #define SCUART_IS_TX_EMPTY(sc) (((sc)->STATUS & SC_STATUS_TXACT_Msk)? 0 : 1)
  117. /**
  118. * @brief Check specified smartcard port transmit FIFO empty status
  119. *
  120. * @param[in] sc The pointer of smartcard module.
  121. *
  122. * @return Transmit FIFO empty status
  123. * @retval 0 Transmit FIFO is not empty
  124. * @retval 1 Transmit FIFO is empty
  125. *
  126. * @details TXEMPTY (SC_STATUS[9]) is set by hardware when the last byte of Tx buffer has been transferred to Transmitter Shift Register.
  127. * \hideinitializer
  128. */
  129. #define SCUART_IS_TX_FIFO_EMPTY(sc) (((sc)->STATUS & SC_STATUS_TXEMPTY_Msk)? 1 : 0)
  130. /**
  131. * @brief Check specified Smartcard port Transmission Status
  132. *
  133. * @param[in] sc The pointer of smartcard module.
  134. *
  135. * @retval 0 Transmit is completed
  136. * @retval 1 Transmit is active
  137. *
  138. * @details TXACT (SC_STATUS[31]) is set by hardware when Tx transfer is in active and the STOP bit of the last byte has been transmitted.
  139. * \hideinitializer
  140. */
  141. #define SCUART_IS_TX_ACTIVE(sc) (((sc)->STATUS & SC_STATUS_TXACT_Msk)? 1 : 0)
  142. /* RX Macros */
  143. /**
  144. * @brief Read Rx data register
  145. *
  146. * @param[in] sc The pointer of smartcard module.
  147. *
  148. * @return The oldest data byte in RX FIFO
  149. *
  150. * @details By reading DAT register, the SC will return an 8-bit received data.
  151. * \hideinitializer
  152. */
  153. #define SCUART_READ(sc) ((sc)->DAT)
  154. /**
  155. * @brief Get Rx FIFO empty flag status from register
  156. *
  157. * @param[in] sc The pointer of smartcard module.
  158. *
  159. * @return Receive FIFO empty status
  160. * @retval 0 Receive FIFO is not empty
  161. * @retval SC_STATUS_RXEMPTY_Msk Receive FIFO is empty
  162. *
  163. * @details When the last byte of Rx buffer has been read by CPU, hardware sets RXEMPTY (SC_STATUS[1]) high.
  164. * It will be cleared when SC receives any new data.
  165. * \hideinitializer
  166. */
  167. #define SCUART_GET_RX_EMPTY(sc) ((sc)->STATUS & SC_STATUS_RXEMPTY_Msk)
  168. /**
  169. * @brief Get Rx FIFO full flag status from register
  170. *
  171. * @param[in] sc The pointer of smartcard module.
  172. *
  173. * @return Receive FIFO full status
  174. * @retval 0 Receive FIFO is not full
  175. * @retval SC_STATUS_TXFULL_Msk Receive FIFO is full
  176. *
  177. * @details RXFULL (SC_STATUS[2]) is set when Rx buffer counts equals to 4, otherwise it is cleared by hardware.
  178. * \hideinitializer
  179. */
  180. #define SCUART_GET_RX_FULL(sc) ((sc)->STATUS & SC_STATUS_RXFULL_Msk)
  181. /**
  182. * @brief Check if receive data number in FIFO reach FIFO trigger level or not
  183. *
  184. * @param[in] sc The pointer of smartcard module.
  185. *
  186. * @return Receive FIFO data status
  187. * @retval 0 The number of bytes in receive FIFO is less than trigger level
  188. * @retval 1 The number of bytes in receive FIFO equals or larger than trigger level
  189. *
  190. * @details RDAIF (SC_INTSTS[0]) is used for received data reaching trigger level RXTRGLV (SC_CTL[7:6]) interrupt status flag.
  191. *
  192. * @note If receive trigger level is \b not 1 byte, this macro return 0 does not necessary indicates there is no data in FIFO.
  193. * \hideinitializer
  194. */
  195. #define SCUART_IS_RX_READY(sc) (((sc)->INTSTS & SC_INTSTS_RDAIF_Msk)? 1 : 0)
  196. /**
  197. * @brief Check specified smartcard port receive FIFO is full or not
  198. *
  199. * @param[in] sc The pointer of smartcard module.
  200. *
  201. * @return Receive FIFO full status
  202. * @retval 0 Receive FIFO is not full
  203. * @retval 1 Receive FIFO is full
  204. *
  205. * @details RXFULLF( SC_STATUS[2]) is set when Rx buffer counts equals to 4, otherwise it is cleared by hardware.
  206. * \hideinitializer
  207. */
  208. #define SCUART_IS_RX_FULL(sc) (((sc)->STATUS & SC_STATUS_RXFULL_Msk)? 1 : 0)
  209. /* Interrupt Macros */
  210. /**
  211. * @brief Enable specified interrupts
  212. *
  213. * @param[in] sc The pointer of smartcard module.
  214. * @param[in] u32Mask Interrupt masks to enable, a combination of following bits,
  215. * - \ref SC_INTEN_RXTOIEN_Msk
  216. * - \ref SC_INTEN_TERRIEN_Msk
  217. * - \ref SC_INTEN_TBEIEN_Msk
  218. * - \ref SC_INTEN_RDAIEN_Msk
  219. *
  220. * @return None
  221. *
  222. * @details The macro is used to enable receiver buffer time-out interrupt, transfer error interrupt,
  223. * transmit buffer empty interrupt or receive data reach trigger level interrupt.
  224. * \hideinitializer
  225. */
  226. #define SCUART_ENABLE_INT(sc, u32Mask) ((sc)->INTEN |= (u32Mask))
  227. /**
  228. * @brief Disable specified interrupts
  229. *
  230. * @param[in] sc The pointer of smartcard module.
  231. * @param[in] u32Mask Interrupt masks to disable, a combination of following bits,
  232. * - \ref SC_INTEN_RXTOIEN_Msk
  233. * - \ref SC_INTEN_TERRIEN_Msk
  234. * - \ref SC_INTEN_TBEIEN_Msk
  235. * - \ref SC_INTEN_RDAIEN_Msk
  236. *
  237. * @return None
  238. *
  239. * @details The macro is used to disable receiver buffer time-out interrupt, transfer error interrupt,
  240. * transmit buffer empty interrupt or receive data reach trigger level interrupt.
  241. * \hideinitializer
  242. */
  243. #define SCUART_DISABLE_INT(sc, u32Mask) ((sc)->INTEN &= ~(u32Mask))
  244. /**
  245. * @brief Get specified interrupt flag/status
  246. *
  247. * @param[in] sc The pointer of smartcard module.
  248. * @param[in] u32Type Interrupt flag/status to check, could be one of following value
  249. * - \ref SC_INTSTS_RXTOIF_Msk
  250. * - \ref SC_INTSTS_TERRIF_Msk
  251. * - \ref SC_INTSTS_TBEIF_Msk
  252. * - \ref SC_INTSTS_RDAIF_Msk
  253. *
  254. * @return The status of specified interrupt
  255. * @retval 0 Specified interrupt does not happened
  256. * @retval 1 Specified interrupt happened
  257. *
  258. * @details The macro is used to get receiver buffer time-out interrupt status, transfer error interrupt status,
  259. * transmit buffer empty interrupt status or receive data reach interrupt status.
  260. * \hideinitializer
  261. */
  262. #define SCUART_GET_INT_FLAG(sc, u32Type) (((sc)->INTSTS & (u32Type))? 1 : 0)
  263. /**
  264. * @brief Clear specified interrupt flag/status
  265. *
  266. * @param[in] sc The pointer of smartcard module.
  267. * @param[in] u32Type Interrupt flag/status to clear, only \ref SC_INTSTS_TERRIF_Msk valid for this macro.
  268. *
  269. * @return None
  270. *
  271. * @details The macro is used to clear transfer error interrupt flag.
  272. * \hideinitializer
  273. */
  274. #define SCUART_CLR_INT_FLAG(sc, u32Type) ((sc)->INTSTS = (u32Type))
  275. /**
  276. * @brief Get receive error flag/status
  277. *
  278. * @param[in] sc The pointer of smartcard module.
  279. *
  280. * @return Current receive error status, could one of following errors:
  281. * @retval SC_STATUS_PEF_Msk Parity error
  282. * @retval SC_STATUS_FEF_Msk Frame error
  283. * @retval SC_STATUS_BEF_Msk Break error
  284. *
  285. * @details The macro is used to get receiver parity error status, frame error status or break error status.
  286. * \hideinitializer
  287. */
  288. #define SCUART_GET_ERR_FLAG(sc) ((sc)->STATUS & (SC_STATUS_PEF_Msk | SC_STATUS_FEF_Msk | SC_STATUS_BEF_Msk))
  289. /**
  290. * @brief Clear specified receive error flag/status
  291. *
  292. * @param[in] sc The pointer of smartcard module.
  293. * @param[in] u32Mask Receive error flag/status to clear, combination following values
  294. * - \ref SC_STATUS_PEF_Msk
  295. * - \ref SC_STATUS_FEF_Msk
  296. * - \ref SC_STATUS_BEF_Msk
  297. *
  298. * @return None
  299. *
  300. * @details The macro is used to clear receiver parity error flag, frame error flag or break error flag.
  301. * \hideinitializer
  302. */
  303. #define SCUART_CLR_ERR_FLAG(sc, u32Mask) ((sc)->STATUS = (u32Mask))
  304. void SCUART_Close(SC_T* sc);
  305. uint32_t SCUART_Open(SC_T* sc, uint32_t u32Baudrate);
  306. uint32_t SCUART_Read(SC_T* sc, uint8_t pu8RxBuf[], uint32_t u32ReadBytes);
  307. uint32_t SCUART_SetLineConfig(SC_T* sc, uint32_t u32Baudrate, uint32_t u32DataWidth, uint32_t u32Parity, uint32_t u32StopBits);
  308. void SCUART_SetTimeoutCnt(SC_T* sc, uint32_t u32TOC);
  309. void SCUART_Write(SC_T* sc, uint8_t pu8TxBuf[], uint32_t u32WriteBytes);
  310. /**@}*/ /* end of group SCUART_EXPORTED_FUNCTIONS */
  311. /**@}*/ /* end of group SCUART_Driver */
  312. /**@}*/ /* end of group Standard_Driver */
  313. #ifdef __cplusplus
  314. }
  315. #endif
  316. #endif /* __NU_SCUART_H__ */