nu_sc.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /**************************************************************************//**
  2. * @file nu_sc.h
  3. * @version V3.00
  4. * @brief Smartcard(SC) 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_SC_H__
  10. #define __NU_SC_H__
  11. #ifdef __cplusplus
  12. extern "C"
  13. {
  14. #endif
  15. /** @addtogroup Standard_Driver Standard Driver
  16. @{
  17. */
  18. /** @addtogroup SC_Driver SC Driver
  19. @{
  20. */
  21. /** @addtogroup SC_EXPORTED_CONSTANTS SC Exported Constants
  22. @{
  23. */
  24. #define SC_INTERFACE_NUM (3UL) /*!< Smartcard interface numbers \hideinitializer */
  25. #define SC_PIN_STATE_HIGH (1UL) /*!< Smartcard pin status high \hideinitializer */
  26. #define SC_PIN_STATE_LOW (0UL) /*!< Smartcard pin status low \hideinitializer */
  27. #define SC_PIN_STATE_IGNORE (0xFFFFFFFFUL) /*!< Ignore pin status \hideinitializer */
  28. #define SC_CLK_ON (1UL) /*!< Smartcard clock on \hideinitializer */
  29. #define SC_CLK_OFF (0UL) /*!< Smartcard clock off \hideinitializer */
  30. #define SC_TMR_MODE_0 (0UL << SC_TMRCTL0_OPMODE_Pos) /*!<Timer Operation Mode 0, down count \hideinitializer */
  31. #define SC_TMR_MODE_1 (1UL << SC_TMRCTL0_OPMODE_Pos) /*!<Timer Operation Mode 1, down count, start after detect start bit \hideinitializer */
  32. #define SC_TMR_MODE_2 (2UL << SC_TMRCTL0_OPMODE_Pos) /*!<Timer Operation Mode 2, down count, start after receive start bit \hideinitializer */
  33. #define SC_TMR_MODE_3 (3UL << SC_TMRCTL0_OPMODE_Pos) /*!<Timer Operation Mode 3, down count, use for activation, only timer 0 support this mode \hideinitializer */
  34. #define SC_TMR_MODE_4 (4UL << SC_TMRCTL0_OPMODE_Pos) /*!<Timer Operation Mode 4, down count with reload after timeout \hideinitializer */
  35. #define SC_TMR_MODE_5 (5UL << SC_TMRCTL0_OPMODE_Pos) /*!<Timer Operation Mode 5, down count, start after detect start bit, reload after timeout \hideinitializer */
  36. #define SC_TMR_MODE_6 (6UL << SC_TMRCTL0_OPMODE_Pos) /*!<Timer Operation Mode 6, down count, start after receive start bit, reload after timeout \hideinitializer */
  37. #define SC_TMR_MODE_7 (7UL << SC_TMRCTL0_OPMODE_Pos) /*!<Timer Operation Mode 7, down count, start and reload after detect start bit \hideinitializer */
  38. #define SC_TMR_MODE_8 (8UL << SC_TMRCTL0_OPMODE_Pos) /*!<Timer Operation Mode 8, up count \hideinitializer */
  39. #define SC_TMR_MODE_F (0xFUL << SC_TMRCTL0_OPMODE_Pos) /*!<Timer Operation Mode 15, down count, reload after detect start bit \hideinitializer */
  40. #define SC_TIMEOUT (SystemCoreClock) /*!< 1 second time-out */
  41. /**@}*/ /* end of group SC_EXPORTED_CONSTANTS */
  42. /** @addtogroup SC_EXPORTED_FUNCTIONS SC Exported Functions
  43. @{
  44. */
  45. /**
  46. * @brief This macro enable smartcard interrupt
  47. *
  48. * @param[in] sc The pointer of smartcard module.
  49. * @param[in] u32Mask Interrupt mask to be enabled. A combination of
  50. * - \ref SC_INTEN_ACERRIEN_Msk
  51. * - \ref SC_INTEN_RXTOIEN_Msk
  52. * - \ref SC_INTEN_INITIEN_Msk
  53. * - \ref SC_INTEN_CDIEN_Msk
  54. * - \ref SC_INTEN_BGTIEN_Msk
  55. * - \ref SC_INTEN_TMR2IEN_Msk
  56. * - \ref SC_INTEN_TMR1IEN_Msk
  57. * - \ref SC_INTEN_TMR0IEN_Msk
  58. * - \ref SC_INTEN_TERRIEN_Msk
  59. * - \ref SC_INTEN_TBEIEN_Msk
  60. * - \ref SC_INTEN_RDAIEN_Msk
  61. *
  62. * @return None
  63. *
  64. * @details The macro is used to enable Auto-convention error interrupt, Receiver buffer time-out interrupt, Initial end interrupt,
  65. * Card detect interrupt, Block guard time interrupt, Timer2 interrupt, Timer1 interrupt, Timer0 interrupt,
  66. * Transfer error interrupt, Transmit buffer empty interrupt or Receive data reach trigger level interrupt.
  67. * \hideinitializer
  68. */
  69. #define SC_ENABLE_INT(sc, u32Mask) ((sc)->INTEN |= (u32Mask))
  70. /**
  71. * @brief This macro disable smartcard interrupt
  72. *
  73. * @param[in] sc The pointer of smartcard module.
  74. * @param[in] u32Mask Interrupt mask to be disabled. A combination of
  75. * - \ref SC_INTEN_ACERRIEN_Msk
  76. * - \ref SC_INTEN_RXTOIEN_Msk
  77. * - \ref SC_INTEN_INITIEN_Msk
  78. * - \ref SC_INTEN_CDIEN_Msk
  79. * - \ref SC_INTEN_BGTIEN_Msk
  80. * - \ref SC_INTEN_TMR2IEN_Msk
  81. * - \ref SC_INTEN_TMR1IEN_Msk
  82. * - \ref SC_INTEN_TMR0IEN_Msk
  83. * - \ref SC_INTEN_TERRIEN_Msk
  84. * - \ref SC_INTEN_TBEIEN_Msk
  85. * - \ref SC_INTEN_RDAIEN_Msk
  86. *
  87. * @return None
  88. *
  89. * @details The macro is used to disable Auto-convention error interrupt, Receiver buffer time-out interrupt, Initial end interrupt,
  90. * Card detect interrupt, Block guard time interrupt, Timer2 interrupt, Timer1 interrupt, Timer0 interrupt,
  91. * Transfer error interrupt, Transmit buffer empty interrupt or Receive data reach trigger level interrupt.
  92. * \hideinitializer
  93. */
  94. #define SC_DISABLE_INT(sc, u32Mask) ((sc)->INTEN &= ~(u32Mask))
  95. /**
  96. * @brief This macro set VCC pin state of smartcard interface
  97. *
  98. * @param[in] sc The pointer of smartcard module.
  99. * @param[in] u32State Pin state of VCC pin, valid parameters are \ref SC_PIN_STATE_HIGH and \ref SC_PIN_STATE_LOW.
  100. *
  101. * @return None
  102. *
  103. * @details User can set PWREN (SC_PINCTL[0]) and PWRINV (SC_PINCTL[11]) to decide SC_PWR pin is in high or low level.
  104. * \hideinitializer
  105. */
  106. #define SC_SET_VCC_PIN(sc, u32State) \
  107. do {\
  108. uint32_t u32TimeOutCount = SC_TIMEOUT;\
  109. while(((sc)->PINCTL & SC_PINCTL_SYNC_Msk) == SC_PINCTL_SYNC_Msk)\
  110. if(--u32TimeOutCount == 0) break;\
  111. if(u32State)\
  112. (sc)->PINCTL |= SC_PINCTL_PWREN_Msk;\
  113. else\
  114. (sc)->PINCTL &= ~SC_PINCTL_PWREN_Msk;\
  115. }while(0)
  116. /**
  117. * @brief This macro turns CLK output on or off
  118. *
  119. * @param[in] sc The pointer of smartcard module.
  120. * @param[in] u32OnOff Clock on or off for selected smartcard module, valid values are \ref SC_CLK_ON and \ref SC_CLK_OFF.
  121. *
  122. * @return None
  123. *
  124. * @details User can set CLKKEEP (SC_PINCTL[6]) to decide SC_CLK pin always keeps free running or not.
  125. * \hideinitializer
  126. */
  127. #define SC_SET_CLK_PIN(sc, u32OnOff)\
  128. do {\
  129. uint32_t u32TimeOutCount = SC_TIMEOUT;\
  130. while(((sc)->PINCTL & SC_PINCTL_SYNC_Msk) == SC_PINCTL_SYNC_Msk)\
  131. if(--u32TimeOutCount == 0) break;\
  132. if(u32OnOff)\
  133. (sc)->PINCTL |= SC_PINCTL_CLKKEEP_Msk;\
  134. else\
  135. (sc)->PINCTL &= ~(SC_PINCTL_CLKKEEP_Msk);\
  136. }while(0)
  137. /**
  138. * @brief This macro set I/O pin state of smartcard interface
  139. *
  140. * @param[in] sc The pointer of smartcard module.
  141. * @param[in] u32State Pin state of I/O pin, valid parameters are \ref SC_PIN_STATE_HIGH and \ref SC_PIN_STATE_LOW.
  142. *
  143. * @return None
  144. *
  145. * @details User can set SCDATA (SC_PINCTL[9]) to decide SC_DATA pin to high or low.
  146. * \hideinitializer
  147. */
  148. #define SC_SET_IO_PIN(sc, u32State)\
  149. do {\
  150. uint32_t u32TimeOutCount = SC_TIMEOUT;\
  151. while(((sc)->PINCTL & SC_PINCTL_SYNC_Msk) == SC_PINCTL_SYNC_Msk)\
  152. if(--u32TimeOutCount == 0) break;\
  153. if(u32State)\
  154. (sc)->PINCTL |= SC_PINCTL_SCDATA_Msk;\
  155. else\
  156. (sc)->PINCTL &= ~SC_PINCTL_SCDATA_Msk;\
  157. }while(0)
  158. /**
  159. * @brief This macro set RST pin state of smartcard interface
  160. *
  161. * @param[in] sc The pointer of smartcard module.
  162. * @param[in] u32State Pin state of RST pin, valid parameters are \ref SC_PIN_STATE_HIGH and \ref SC_PIN_STATE_LOW.
  163. *
  164. * @return None
  165. *
  166. * @details User can set SCRST (SC_PINCTL[1]) to decide SC_RST pin to high or low.
  167. * \hideinitializer
  168. */
  169. #define SC_SET_RST_PIN(sc, u32State)\
  170. do {\
  171. uint32_t u32TimeOutCount = SC_TIMEOUT;\
  172. while(((sc)->PINCTL & SC_PINCTL_SYNC_Msk) == SC_PINCTL_SYNC_Msk)\
  173. if(--u32TimeOutCount == 0) break;\
  174. if(u32State)\
  175. (sc)->PINCTL |= SC_PINCTL_RSTEN_Msk;\
  176. else\
  177. (sc)->PINCTL &= ~SC_PINCTL_RSTEN_Msk;\
  178. }while(0)
  179. /**
  180. * @brief This macro read one byte from smartcard module receive FIFO
  181. *
  182. * @param[in] sc The pointer of smartcard module.
  183. *
  184. * @return One byte read from receive FIFO
  185. *
  186. * @details By reading DAT register, the SC will return an 8-bit received data.
  187. * \hideinitializer
  188. */
  189. #define SC_READ(sc) ((char)((sc)->DAT))
  190. /**
  191. * @brief This macro write one byte to smartcard module transmit FIFO
  192. *
  193. * @param[in] sc The pointer of smartcard module.
  194. * @param[in] u8Data Data to write to transmit FIFO.
  195. *
  196. * @return None
  197. *
  198. * @details By writing data to DAT register, the SC will send out an 8-bit data.
  199. * \hideinitializer
  200. */
  201. #define SC_WRITE(sc, u8Data) ((sc)->DAT = (u8Data))
  202. /**
  203. * @brief This macro set smartcard stop bit length
  204. *
  205. * @param[in] sc The pointer of smartcard module.
  206. * @param[in] u32Len Stop bit length, ether 1 or 2.
  207. *
  208. * @return None
  209. *
  210. * @details Stop bit length must be 1 for T = 1 protocol and 2 for T = 0 protocol.
  211. * \hideinitializer
  212. */
  213. #define SC_SET_STOP_BIT_LEN(sc, u32Len) ((sc)->CTL = ((sc)->CTL & ~SC_CTL_NSB_Msk) | (((u32Len) == 1)? SC_CTL_NSB_Msk : 0))
  214. /*---------------------------------------------------------------------------------------------------------*/
  215. /* static inline functions */
  216. /*---------------------------------------------------------------------------------------------------------*/
  217. /* Declare these inline functions here to avoid MISRA C 2004 rule 8.1 error */
  218. __STATIC_INLINE void SC_SetTxRetry(SC_T *sc, uint32_t u32Count);
  219. __STATIC_INLINE void SC_SetRxRetry(SC_T *sc, uint32_t u32Count);
  220. /**
  221. * @brief Enable/Disable Tx error retry, and set Tx error retry count
  222. *
  223. * @param[in] sc The pointer of smartcard module.
  224. * @param[in] u32Count The number of times of Tx error retry count, between 0~8. 0 means disable Tx error retry.
  225. *
  226. * @return None
  227. *
  228. * @details This function is used to enable/disable transmitter retry function when parity error has occurred, and set error retry count.
  229. */
  230. __STATIC_INLINE void SC_SetTxRetry(SC_T *sc, uint32_t u32Count)
  231. {
  232. uint32_t u32TimeOutCount = 0;
  233. u32TimeOutCount = SC_TIMEOUT;
  234. while (((sc)->CTL & SC_CTL_SYNC_Msk) == SC_CTL_SYNC_Msk)
  235. {
  236. if (--u32TimeOutCount == 0) break;
  237. }
  238. /* Retry count must set while enable bit disabled, so disable it first */
  239. (sc)->CTL &= ~(SC_CTL_TXRTY_Msk | SC_CTL_TXRTYEN_Msk);
  240. if ((u32Count) != 0UL)
  241. {
  242. u32TimeOutCount = SC_TIMEOUT;
  243. while (((sc)->CTL & SC_CTL_SYNC_Msk) == SC_CTL_SYNC_Msk)
  244. {
  245. if (--u32TimeOutCount == 0) break;
  246. }
  247. (sc)->CTL |= (((u32Count) - 1UL) << SC_CTL_TXRTY_Pos) | SC_CTL_TXRTYEN_Msk;
  248. }
  249. }
  250. /**
  251. * @brief Enable/Disable Rx error retry, and set Rx error retry count
  252. *
  253. * @param[in] sc The pointer of smartcard module.
  254. * @param[in] u32Count The number of times of Rx error retry count, between 0~8. 0 means disable Rx error retry.
  255. *
  256. * @return None
  257. *
  258. * @details This function is used to enable/disable receiver retry function when parity error has occurred, and set error retry count.
  259. */
  260. __STATIC_INLINE void SC_SetRxRetry(SC_T *sc, uint32_t u32Count)
  261. {
  262. uint32_t u32TimeOutCount = 0;
  263. u32TimeOutCount = SC_TIMEOUT;
  264. while (((sc)->CTL & SC_CTL_SYNC_Msk) == SC_CTL_SYNC_Msk)
  265. {
  266. if (--u32TimeOutCount == 0) break;
  267. }
  268. /* Retry count must set while enable bit disabled, so disable it first */
  269. (sc)->CTL &= ~(SC_CTL_RXRTY_Msk | SC_CTL_RXRTYEN_Msk);
  270. if ((u32Count) != 0UL)
  271. {
  272. u32TimeOutCount = SC_TIMEOUT;
  273. while (((sc)->CTL & SC_CTL_SYNC_Msk) == SC_CTL_SYNC_Msk)
  274. {
  275. if (--u32TimeOutCount == 0) break;
  276. }
  277. (sc)->CTL |= (((u32Count) - 1UL) << SC_CTL_RXRTY_Pos) | SC_CTL_RXRTYEN_Msk;
  278. }
  279. }
  280. uint32_t SC_IsCardInserted(SC_T *sc);
  281. void SC_ClearFIFO(SC_T *sc);
  282. void SC_Close(SC_T *sc);
  283. void SC_Open(SC_T *sc, uint32_t u32CardDet, uint32_t u32PWR);
  284. void SC_ResetReader(SC_T *sc);
  285. void SC_SetBlockGuardTime(SC_T *sc, uint32_t u32BGT);
  286. void SC_SetCharGuardTime(SC_T *sc, uint32_t u32CGT);
  287. void SC_StopAllTimer(SC_T *sc);
  288. void SC_StartTimer(SC_T *sc, uint32_t u32TimerNum, uint32_t u32Mode, uint32_t u32ETUCount);
  289. void SC_StopTimer(SC_T *sc, uint32_t u32TimerNum);
  290. uint32_t SC_GetInterfaceClock(SC_T *sc);
  291. /**@}*/ /* end of group SC_EXPORTED_FUNCTIONS */
  292. /**@}*/ /* end of group SC_Driver */
  293. /**@}*/ /* end of group Standard_Driver */
  294. #ifdef __cplusplus
  295. }
  296. #endif
  297. #endif /* __NU_SC_H__ */