nu_scuart.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /**************************************************************************//**
  2. * @file nu_scuart.h
  3. * @version V1.00
  4. * @brief M480 Smartcard UART mode (SCUART) 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_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. * @param[in] sc The base address of smartcard module.
  41. * @param[in] u8Data Data byte to transmit
  42. * @return None
  43. * \hideinitializer
  44. */
  45. #define SCUART_WRITE(sc, u8Data) ((sc)->DAT = (u8Data))
  46. /**
  47. * @brief Get TX FIFO empty flag status from register
  48. * @param[in] sc The base address of smartcard module
  49. * @return Transmit FIFO empty status
  50. * @retval 0 Transmit FIFO is not empty
  51. * @retval SC_STATUS_TXEMPTY_Msk Transmit FIFO is empty
  52. * \hideinitializer
  53. */
  54. #define SCUART_GET_TX_EMPTY(sc) ((sc)->STATUS & SC_STATUS_TXEMPTY_Msk)
  55. /**
  56. * @brief Get TX FIFO full flag status from register
  57. * @param[in] sc The base address of smartcard module
  58. * @return Transmit FIFO full status
  59. * @retval 0 Transmit FIFO is not full
  60. * @retval SC_STATUS_TXFULL_Msk Transmit FIFO is full
  61. * \hideinitializer
  62. */
  63. #define SCUART_GET_TX_FULL(sc) ((sc)->STATUS & SC_STATUS_TXFULL_Msk)
  64. /**
  65. * @brief Wait specified smartcard port transmission complete
  66. * @param[in] sc The base address of smartcard module
  67. * @return None
  68. * @note This Macro blocks until transmit complete.
  69. * \hideinitializer
  70. */
  71. #define SCUART_WAIT_TX_EMPTY(sc) while((sc)->STATUS & SC_STATUS_TXACT_Msk)
  72. /**
  73. * @brief Check specified smartcard port transmit FIFO is full or not
  74. * @param[in] sc The base address of smartcard module
  75. * @return Transmit FIFO full status
  76. * @retval 0 Transmit FIFO is not full
  77. * @retval 1 Transmit FIFO is full
  78. * \hideinitializer
  79. */
  80. #define SCUART_IS_TX_FULL(sc) ((sc)->STATUS & SC_STATUS_TXFULL_Msk ? 1 : 0)
  81. /**
  82. * @brief Check specified smartcard port transmission is over
  83. * @param[in] sc The base address of smartcard module
  84. * @return Transmit complete status
  85. * @retval 0 Transmit is not complete
  86. * @retval 1 Transmit complete
  87. * \hideinitializer
  88. */
  89. #define SCUART_IS_TX_EMPTY(sc) ((sc)->STATUS & SC_STATUS_TXACT_Msk ? 0 : 1)
  90. /**
  91. * @brief Check specified Smartcard port Transmission Status
  92. * @param[in] sc The pointer of smartcard module.
  93. * @retval 0 Transmit is completed
  94. * @retval 1 Transmit is active
  95. * @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.
  96. * \hideinitializer
  97. */
  98. #define SCUART_IS_TX_ACTIVE(sc) (((sc)->STATUS & SC_STATUS_TXACT_Msk)? 1 : 0)
  99. /* RX Macros */
  100. /**
  101. * @brief Read Rx data register
  102. * @param[in] sc The base address of smartcard module
  103. * @return The oldest data byte in RX FIFO
  104. * \hideinitializer
  105. */
  106. #define SCUART_READ(sc) ((sc)->DAT)
  107. /**
  108. * @brief Get RX FIFO empty flag status from register
  109. * @param[in] sc The base address of smartcard module
  110. * @return Receive FIFO empty status
  111. * @retval 0 Receive FIFO is not empty
  112. * @retval SC_STATUS_RXEMPTY_Msk Receive FIFO is empty
  113. * \hideinitializer
  114. */
  115. #define SCUART_GET_RX_EMPTY(sc) ((sc)->STATUS & SC_STATUS_RXEMPTY_Msk)
  116. /**
  117. * @brief Get RX FIFO full flag status from register
  118. * @param[in] sc The base address of smartcard module
  119. * @return Receive FIFO full status
  120. * @retval 0 Receive FIFO is not full
  121. * @retval SC_STATUS_RXFULLF_Msk Receive FIFO is full
  122. * \hideinitializer
  123. */
  124. #define SCUART_GET_RX_FULL(sc) ((sc)->STATUS & SC_STATUS_RXFULL_Msk)
  125. /**
  126. * @brief Check if receive data number in FIFO reach FIFO trigger level or not
  127. * @param[in] sc The base address of smartcard module
  128. * @return Receive FIFO data status
  129. * @retval 0 The number of bytes in receive FIFO is less than trigger level
  130. * @retval 1 The number of bytes in receive FIFO equals or larger than trigger level
  131. * @note If receive trigger level is \b not 1 byte, this macro return 0 does not necessary indicates there is \b no data in FIFO
  132. * \hideinitializer
  133. */
  134. #define SCUART_IS_RX_READY(sc) ((sc)->INTSTS & SC_INTSTS_RDAIF_Msk ? 1 : 0)
  135. /**
  136. * @brief Check specified smartcard port receive FIFO is full or not
  137. * @param[in] sc The base address of smartcard module
  138. * @return Receive FIFO full status
  139. * @retval 0 Receive FIFO is not full
  140. * @retval 1 Receive FIFO is full
  141. * \hideinitializer
  142. */
  143. #define SCUART_IS_RX_FULL(sc) ((sc)->STATUS & SC_STATUS_RXFULL_Msk ? 1 : 0)
  144. /* Interrupt Macros */
  145. /**
  146. * @brief Enable specified interrupts
  147. * @param[in] sc The base address of smartcard module
  148. * @param[in] u32Mask Interrupt masks to enable, a combination of following bits
  149. * - \ref SC_INTEN_RXTOIEN_Msk
  150. * - \ref SC_INTEN_TERRIEN_Msk
  151. * - \ref SC_INTEN_TBEIEN_Msk
  152. * - \ref SC_INTEN_RDAIEN_Msk
  153. * @return None
  154. * \hideinitializer
  155. */
  156. #define SCUART_ENABLE_INT(sc, u32Mask) ((sc)->INTEN |= (u32Mask))
  157. /**
  158. * @brief Disable specified interrupts
  159. * @param[in] sc The base address of smartcard module
  160. * @param[in] u32Mask Interrupt masks to disable, a combination of following bits
  161. * - \ref SC_INTEN_RXTOIEN_Msk
  162. * - \ref SC_INTEN_TERRIEN_Msk
  163. * - \ref SC_INTEN_TBEIEN_Msk
  164. * - \ref SC_INTEN_RDAIEN_Msk
  165. * @return None
  166. * \hideinitializer
  167. */
  168. #define SCUART_DISABLE_INT(sc, u32Mask) ((sc)->INTEN &= ~(u32Mask))
  169. /**
  170. * @brief Get specified interrupt flag/status
  171. * @param[in] sc The base address of smartcard module
  172. * @param[in] u32Type Interrupt flag/status to check, could be one of following value
  173. * - \ref SC_INTSTS_RXTOIF_Msk
  174. * - \ref SC_INTSTS_TERRIF_Msk
  175. * - \ref SC_INTSTS_TBEIF_Msk
  176. * - \ref SC_INTSTS_RDAIF_Msk
  177. * @return The status of specified interrupt
  178. * @retval 0 Specified interrupt does not happened
  179. * @retval 1 Specified interrupt happened
  180. * \hideinitializer
  181. */
  182. #define SCUART_GET_INT_FLAG(sc, u32Type) ((sc)->INTSTS & (u32Type) ? 1 : 0)
  183. /**
  184. * @brief Clear specified interrupt flag/status
  185. * @param[in] sc The base address of smartcard module
  186. * @param[in] u32Type Interrupt flag/status to clear, could be the combination of following values
  187. * - \ref SC_INTSTS_RXTOIF_Msk
  188. * - \ref SC_INTSTS_TERRIF_Msk
  189. * - \ref SC_INTSTS_TBEIF_Msk
  190. * @return None
  191. * \hideinitializer
  192. */
  193. #define SCUART_CLR_INT_FLAG(sc, u32Type) ((sc)->INTSTS = (u32Type))
  194. /**
  195. * @brief Get receive error flag/status
  196. * @param[in] sc The base address of smartcard module
  197. * @return Current receive error status, could one of following errors:
  198. * @retval SC_STATUS_PEF_Msk Parity error
  199. * @retval SC_STATUS_FEF_Msk Frame error
  200. * @retval SC_STATUS_BEF_Msk Break error
  201. * \hideinitializer
  202. */
  203. #define SCUART_GET_ERR_FLAG(sc) ((sc)->STATUS & (SC_STATUS_PEF_Msk | SC_STATUS_FEF_Msk | SC_STATUS_BEF_Msk))
  204. /**
  205. * @brief Clear specified receive error flag/status
  206. * @param[in] sc The base address of smartcard module
  207. * @param[in] u32Mask Receive error flag/status to clear, combination following values
  208. * - \ref SC_STATUS_PEF_Msk
  209. * - \ref SC_STATUS_FEF_Msk
  210. * - \ref SC_STATUS_BEF_Msk
  211. * @return None
  212. * \hideinitializer
  213. */
  214. #define SCUART_CLR_ERR_FLAG(sc, u32Mask) ((sc)->STATUS = (u32Mask))
  215. void SCUART_Close(SC_T* sc);
  216. uint32_t SCUART_Open(SC_T* sc, uint32_t u32baudrate);
  217. uint32_t SCUART_Read(SC_T* sc, uint8_t pu8RxBuf[], uint32_t u32ReadBytes);
  218. uint32_t SCUART_SetLineConfig(SC_T* sc, uint32_t u32Baudrate, uint32_t u32DataWidth, uint32_t u32Parity, uint32_t u32StopBits);
  219. void SCUART_SetTimeoutCnt(SC_T* sc, uint32_t u32TOC);
  220. void SCUART_Write(SC_T* sc,uint8_t pu8TxBuf[], uint32_t u32WriteBytes);
  221. /*@}*/ /* end of group SCUART_EXPORTED_FUNCTIONS */
  222. /*@}*/ /* end of group SCUART_Driver */
  223. /*@}*/ /* end of group Standard_Driver */
  224. #ifdef __cplusplus
  225. }
  226. #endif
  227. #endif /* __NU_SCUART_H__ */
  228. /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/