scuart.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /**************************************************************************//**
  2. * @file sc.h
  3. * @version V3.00
  4. * $Revision: 7 $
  5. * $Date: 15/08/11 10:26a $
  6. * @brief M451 series Smartcard UART mode (SCUART) driver header file
  7. *
  8. * @note
  9. * Copyright (C) 2013~2015 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 Standard_Driver Standard Driver
  18. @{
  19. */
  20. /** @addtogroup SCUART_Driver SCUART Driver
  21. @{
  22. */
  23. /** @addtogroup 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 */
  27. #define SCUART_CHAR_LEN_6 (0x2ul << SC_UARTCTL_WLS_Pos) /*!< Set SCUART word length to 6 bits */
  28. #define SCUART_CHAR_LEN_7 (0x1ul << SC_UARTCTL_WLS_Pos) /*!< Set SCUART word length to 7 bits */
  29. #define SCUART_CHAR_LEN_8 (0) /*!< Set SCUART word length to 8 bits */
  30. #define SCUART_PARITY_NONE (SC_UARTCTL_PBOFF_Msk) /*!< Set SCUART transfer with no parity */
  31. #define SCUART_PARITY_ODD (SC_UARTCTL_OPE_Msk) /*!< Set SCUART transfer with odd parity */
  32. #define SCUART_PARITY_EVEN (0) /*!< Set SCUART transfer with even parity */
  33. #define SCUART_STOP_BIT_1 (SC_CTL_NSB_Msk) /*!< Set SCUART transfer with one stop bit */
  34. #define SCUART_STOP_BIT_2 (0) /*!< Set SCUART transfer with two stop bits */
  35. /*@}*/ /* end of group SCUART_EXPORTED_CONSTANTS */
  36. /** @addtogroup 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 pointer of smartcard module.
  43. * @param[in] u8Data Data byte to transmit.
  44. * @return None
  45. * @details By writing data to DAT register, the SC will send out an 8-bit data.
  46. * \hideinitializer
  47. */
  48. #define SCUART_WRITE(sc, u8Data) ((sc)-> DAT = (u8Data))
  49. /**
  50. * @brief Get TX FIFO empty flag status from register.
  51. * @param[in] sc The pointer of smartcard module.
  52. * @return Transmit FIFO empty status.
  53. * @retval 0 Transmit FIFO is not empty.
  54. * @retval SC_STATUS_TXEMPTY_Msk Transmit FIFO is empty.
  55. * @details When the last byte of TX buffer has been transferred to Transmitter Shift Register, hardware sets TXEMPTY bit (SC_STATUS[9]) high.
  56. * It will be cleared when writing data into DAT (SC_DAT[7:0]).
  57. * \hideinitializer
  58. */
  59. #define SCUART_GET_TX_EMPTY(sc) ((sc)->STATUS & SC_STATUS_TXEMPTY_Msk)
  60. /**
  61. * @brief Get TX FIFO full flag status from register.
  62. * @param[in] sc The pointer of smartcard module.
  63. * @retval 0 Transmit FIFO is not full.
  64. * @retval SC_STATUS_TXFULL_Msk Transmit FIFO is full.
  65. * @details TXFULL(SC_STATUS[10]) is set when TX pointer is equal to 4, otherwise is cleared by hardware.
  66. * \hideinitializer
  67. */
  68. #define SCUART_GET_TX_FULL(sc) ((sc)->STATUS & SC_STATUS_TXFULL_Msk)
  69. /**
  70. * @brief Wait specified smartcard port transmission complete.
  71. * @param[in] sc The pointer of smartcard module.
  72. * @return None
  73. * @details TXACT (SC_STATUS[31]) is cleared automatically when TX transfer is finished or the last byte transmission has completed.
  74. * @note This macro blocks until transmit complete.
  75. * \hideinitializer
  76. */
  77. #define SCUART_WAIT_TX_EMPTY(sc) while((sc)->STATUS & SC_STATUS_TXACT_Msk)
  78. /**
  79. * @brief Check specified smartcard port transmit FIFO is full or not.
  80. * @param[in] sc The pointer of smartcard module.
  81. * @retval 0 Transmit FIFO is not full.
  82. * @retval 1 Transmit FIFO is full.
  83. * @details TXFULL(SC_STATUS[10]) indicates TX buffer full or not.
  84. * This is set when TX pointer is equal to 4, otherwise is cleared by hardware.
  85. * \hideinitializer
  86. */
  87. #define SCUART_IS_TX_FULL(sc) ((sc)->STATUS & SC_STATUS_TXFULL_Msk ? 1 : 0)
  88. /**
  89. * @brief Check specified smartcard port transmission is over.
  90. * @param[in] sc The pointer of smartcard module.
  91. * @retval 0 Transmit is not complete.
  92. * @retval 1 Transmit complete.
  93. * @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.
  94. * \hideinitializer
  95. */
  96. #define SCUART_IS_TX_EMPTY(sc) ((sc)->STATUS & SC_STATUS_TXACT_Msk ? 0 : 1)
  97. /* RX Macros */
  98. /**
  99. * @brief Read Rx data register.
  100. * @param[in] sc The pointer of smartcard module.
  101. * @return The oldest data byte in RX FIFO.
  102. * @details By reading DAT register, the SC will return an 8-bit received data.
  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 pointer of smartcard module.
  109. * @retval 0 Receive FIFO is not empty.
  110. * @retval SC_STATUS_RXEMPTY_Msk Receive FIFO is empty.
  111. * @details When the last byte of Rx buffer has been read by CPU, hardware sets RXEMPTY(SC_STATUS[1]) high.
  112. * It will be cleared when SC receives any new data.
  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 pointer of smartcard module.
  119. * @retval 0 Receive FIFO is not full.
  120. * @retval SC_STATUS_TXFULL_Msk Receive FIFO is full.
  121. * @details RXFULLF(SC_STATUS[2]) is set when RX pointer is equal to 4, otherwise it is cleared by hardware.
  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 pointer of smartcard module.
  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. * @details RDAIF(SC_INTSTS[0]) is used for received data reaching trigger level RXTRGLV (SC_CTL[7:6]) interrupt status flag.
  131. * @note If receive trigger level is \b not 1 byte, this macro return 0 does not necessary indicates there is 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 pointer of smartcard module.
  138. * @retval 0 Receive FIFO is not full.
  139. * @retval 1 Receive FIFO is full.
  140. * @details RXFULLF(SC_STATUS[2]) is set when RX pointer is equal to 4, otherwise it is cleared by hardware.
  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 pointer of smartcard module.
  148. * @param[in] u32Mask Interrupt masks to enable, a combination of following bits.
  149. * - \ref SC_INTEN_RXTOIF_Msk
  150. * - \ref SC_INTEN_TERRIEN_Msk
  151. * - \ref SC_INTEN_TBEIEN_Msk
  152. * - \ref SC_INTEN_RDAIEN_Msk
  153. * @return None
  154. * @details The macro is used to enable receiver buffer time-out interrupt, transfer error interrupt,
  155. * transmit buffer empty interrupt or receive data reach trigger level interrupt.
  156. * \hideinitializer
  157. */
  158. #define SCUART_ENABLE_INT(sc, u32Mask) ((sc)->INTEN |= (u32Mask))
  159. /**
  160. * @brief Disable specified interrupts.
  161. * @param[in] sc The pointer of smartcard module.
  162. * @param[in] u32Mask Interrupt masks to disable, a combination of following bits.
  163. * - \ref SC_INTEN_RXTOIF_Msk
  164. * - \ref SC_INTEN_TERRIEN_Msk
  165. * - \ref SC_INTEN_TBEIEN_Msk
  166. * - \ref SC_INTEN_RDAIEN_Msk
  167. * @return None
  168. * @details The macro is used to disable receiver buffer time-out interrupt, transfer error interrupt,
  169. * transmit buffer empty interrupt or receive data reach trigger level interrupt.
  170. * \hideinitializer
  171. */
  172. #define SCUART_DISABLE_INT(sc, u32Mask) ((sc)->INTEN &= ~(u32Mask))
  173. /**
  174. * @brief Get specified interrupt flag/status.
  175. * @param[in] sc The pointer of smartcard module.
  176. * @param[in] u32Type Interrupt flag/status to check, could be one of following value:
  177. * - \ref SC_INTSTS_RBTOIF_Msk
  178. * - \ref SC_INTSTS_TERRIF_Msk
  179. * - \ref SC_INTSTS_TBEIF_Msk
  180. * - \ref SC_INTSTS_RDAIF_Msk
  181. * @return The status of specified interrupt.
  182. * @retval 0 Specified interrupt does not happened.
  183. * @retval 1 Specified interrupt happened.
  184. * @details The macro is used to get receiver buffer time-out interrupt status, transfer error interrupt status,
  185. * transmit buffer empty interrupt status or receive data reach interrupt status.
  186. * \hideinitializer
  187. */
  188. #define SCUART_GET_INT_FLAG(sc, u32Type) ((sc)->INTSTS & (u32Type) ? 1 : 0)
  189. /**
  190. * @brief Clear specified interrupt flag/status.
  191. * @param[in] sc The pointer of smartcard module.
  192. * @param[in] u32Type Interrupt flag/status to clear, could be the combination of following values:
  193. * - \ref SC_INTSTS_RBTOIF_Msk
  194. * - \ref SC_INTSTS_TERRIF_Msk
  195. * - \ref SC_INTSTS_TBEIF_Msk
  196. * @return None
  197. * @details The macro is used to clear receiver buffer time-out interrupt flag, transfer error interrupt flag or
  198. * transmit buffer empty interrupt flag.
  199. * \hideinitializer
  200. */
  201. #define SCUART_CLR_INT_FLAG(sc, u32Type) ((sc)->INTSTS = (u32Type))
  202. /**
  203. * @brief Get receive error flag/status.
  204. * @param[in] sc The pointer of smartcard module.
  205. * @return Current receive error status, could one of following errors:
  206. * @retval SC_STATUS_PEF_Msk Parity error.
  207. * @retval SC_STATUS_FEF_Msk Frame error.
  208. * @retval SC_STATUS_BEF_Msk Break error.
  209. * @details The macro is used to get receiver parity error status, receiver frame error status or
  210. * receiver break error status.
  211. * \hideinitializer
  212. */
  213. #define SCUART_GET_ERR_FLAG(sc) ((sc)->STATUS & (SC_STATUS_PEF_Msk | SC_STATUS_FEF_Msk | SC_STATUS_BEF_Msk))
  214. /**
  215. * @brief Clear specified receive error flag/status.
  216. * @param[in] sc The pointer of smartcard module.
  217. * @param[in] u32Mask Receive error flag/status to clear, combination following values:
  218. * - \ref SC_STATUS_PEF_Msk
  219. * - \ref SC_STATUS_FEF_Msk
  220. * - \ref SC_STATUS_BEF_Msk
  221. * @return None
  222. * @details The macro is used to clear receiver parity error flag, receiver frame error flag or
  223. * receiver break error flag.
  224. * \hideinitializer
  225. */
  226. #define SCUART_CLR_ERR_FLAG(sc, u32Mask) ((sc)->STATUS = (u32Mask))
  227. void SCUART_Close(SC_T* sc);
  228. uint32_t SCUART_Open(SC_T* sc, uint32_t u32baudrate);
  229. uint32_t SCUART_Read(SC_T* sc, uint8_t *pu8RxBuf, uint32_t u32ReadBytes);
  230. uint32_t SCUART_SetLineConfig(SC_T* sc, uint32_t u32Baudrate, uint32_t u32DataWidth, uint32_t u32Parity, uint32_t u32StopBits);
  231. void SCUART_SetTimeoutCnt(SC_T* sc, uint32_t u32TOC);
  232. void SCUART_Write(SC_T* sc, uint8_t *pu8TxBuf, uint32_t u32WriteBytes);
  233. /*@}*/ /* end of group SCUART_EXPORTED_FUNCTIONS */
  234. /*@}*/ /* end of group SCUART_Driver */
  235. /*@}*/ /* end of group Standard_Driver */
  236. #ifdef __cplusplus
  237. }
  238. #endif
  239. #endif //__SCUART_H__
  240. /*** (C) COPYRIGHT 2013~2015 Nuvoton Technology Corp. ***/