scuart.h 9.1 KB

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