nu_scuart.h 13 KB

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