scuart.h 9.4 KB

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