nu_sc.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /**************************************************************************//**
  2. * @file nu_sc.h
  3. * @version V1.00
  4. * @brief M480 Smartcard (SC) 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_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 3 /*!< Smartcard interface numbers \hideinitializer */
  25. #define SC_PIN_STATE_HIGH 1 /*!< Smartcard pin status high \hideinitializer */
  26. #define SC_PIN_STATE_LOW 0 /*!< Smartcard pin status low \hideinitializer */
  27. #define SC_PIN_STATE_IGNORE 0xFFFFFFFF /*!< Ignore pin status \hideinitializer */
  28. #define SC_CLK_ON 1 /*!< Smartcard clock on \hideinitializer */
  29. #define SC_CLK_OFF 0 /*!< 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 (0xF << SC_TMRCTL0_OPMODE_Pos) /*!<Timer Operation Mode 15, down count, reload after detect start bit \hideinitializer */
  40. /*@}*/ /* end of group SC_EXPORTED_CONSTANTS */
  41. /** @addtogroup SC_EXPORTED_FUNCTIONS SC Exported Functions
  42. @{
  43. */
  44. /**
  45. * @brief This macro enable smartcard interrupt
  46. * @param[in] sc Base address of smartcard module
  47. * @param[in] u32Mask Interrupt mask to be enabled. A combination of
  48. * - \ref SC_INTEN_ACERRIEN_Msk
  49. * - \ref SC_INTEN_RXTOIEN_Msk
  50. * - \ref SC_INTEN_INITIEN_Msk
  51. * - \ref SC_INTEN_CDIEN_Msk
  52. * - \ref SC_INTEN_BGTIEN_Msk
  53. * - \ref SC_INTEN_TMR2IEN_Msk
  54. * - \ref SC_INTEN_TMR1IEN_Msk
  55. * - \ref SC_INTEN_TMR0IEN_Msk
  56. * - \ref SC_INTEN_TERRIEN_Msk
  57. * - \ref SC_INTEN_TBEIEN_Msk
  58. * - \ref SC_INTEN_RDAIEN_Msk
  59. * @return None
  60. * \hideinitializer
  61. */
  62. #define SC_ENABLE_INT(sc, u32Mask) ((sc)->INTEN |= (u32Mask))
  63. /**
  64. * @brief This macro disable smartcard interrupt
  65. * @param[in] sc Base address of smartcard module
  66. * @param[in] u32Mask Interrupt mask to be disabled. A combination of
  67. * - \ref SC_INTEN_ACERRIEN_Msk
  68. * - \ref SC_INTEN_RXTOIEN_Msk
  69. * - \ref SC_INTEN_INITIEN_Msk
  70. * - \ref SC_INTEN_CDIEN_Msk
  71. * - \ref SC_INTEN_BGTIEN_Msk
  72. * - \ref SC_INTEN_TMR2IEN_Msk
  73. * - \ref SC_INTEN_TMR1IEN_Msk
  74. * - \ref SC_INTEN_TMR0IEN_Msk
  75. * - \ref SC_INTEN_TERRIEN_Msk
  76. * - \ref SC_INTEN_TBEIEN_Msk
  77. * - \ref SC_INTEN_RDAIEN_Msk
  78. * @return None
  79. * \hideinitializer
  80. */
  81. #define SC_DISABLE_INT(sc, u32Mask) ((sc)->INTEN &= ~(u32Mask))
  82. /**
  83. * @brief This macro set VCC pin state of smartcard interface
  84. * @param[in] sc Base address of smartcard module
  85. * @param[in] u32State Pin state of VCC pin, valid parameters are \ref SC_PIN_STATE_HIGH and \ref SC_PIN_STATE_LOW
  86. * @return None
  87. * \hideinitializer
  88. */
  89. #define SC_SET_VCC_PIN(sc, u32State) \
  90. do {\
  91. while((sc)->PINCTL & SC_PINCTL_SYNC_Msk);\
  92. if(u32State)\
  93. (sc)->PINCTL |= SC_PINCTL_PWREN_Msk;\
  94. else\
  95. (sc)->PINCTL &= ~SC_PINCTL_PWREN_Msk;\
  96. }while(0)
  97. /**
  98. * @brief This macro turns CLK output on or off
  99. * @param[in] sc Base address of smartcard module
  100. * @param[in] u32OnOff Clock on or off for selected smartcard module, valid values are \ref SC_CLK_ON and \ref SC_CLK_OFF
  101. * @return None
  102. * \hideinitializer
  103. */
  104. #define SC_SET_CLK_PIN(sc, u32OnOff)\
  105. do {\
  106. while((sc)->PINCTL & SC_PINCTL_SYNC_Msk);\
  107. if(u32OnOff)\
  108. (sc)->PINCTL |= SC_PINCTL_CLKKEEP_Msk;\
  109. else\
  110. (sc)->PINCTL &= ~(SC_PINCTL_CLKKEEP_Msk);\
  111. }while(0)
  112. /**
  113. * @brief This macro set I/O pin state of smartcard interface
  114. * @param[in] sc Base address of smartcard module
  115. * @param[in] u32State Pin state of I/O pin, valid parameters are \ref SC_PIN_STATE_HIGH and \ref SC_PIN_STATE_LOW
  116. * @return None
  117. * \hideinitializer
  118. */
  119. #define SC_SET_IO_PIN(sc, u32State)\
  120. do {\
  121. while((sc)->PINCTL & SC_PINCTL_SYNC_Msk);\
  122. if(u32State)\
  123. (sc)->PINCTL |= SC_PINCTL_SCDATA_Msk;\
  124. else\
  125. (sc)->PINCTL &= ~SC_PINCTL_SCDATA_Msk;\
  126. }while(0)
  127. /**
  128. * @brief This macro set RST pin state of smartcard interface
  129. * @param[in] sc Base address of smartcard module
  130. * @param[in] u32State Pin state of RST pin, valid parameters are \ref SC_PIN_STATE_HIGH and \ref SC_PIN_STATE_LOW
  131. * @return None
  132. * \hideinitializer
  133. */
  134. #define SC_SET_RST_PIN(sc, u32State)\
  135. do {\
  136. while((sc)->PINCTL & SC_PINCTL_SYNC_Msk);\
  137. if(u32State)\
  138. (sc)->PINCTL |= SC_PINCTL_RSTEN_Msk;\
  139. else\
  140. (sc)->PINCTL &= ~SC_PINCTL_RSTEN_Msk;\
  141. }while(0)
  142. /**
  143. * @brief This macro read one byte from smartcard module receive FIFO
  144. * @param[in] sc Base address of smartcard module
  145. * @return One byte read from receive FIFO
  146. * \hideinitializer
  147. */
  148. #define SC_READ(sc) ((char)((sc)->DAT))
  149. /**
  150. * @brief This macro write one byte to smartcard module transmit FIFO
  151. * @param[in] sc Base address of smartcard module
  152. * @param[in] u8Data Data to write to transmit FIFO
  153. * @return None
  154. * \hideinitializer
  155. */
  156. #define SC_WRITE(sc, u8Data) ((sc)->DAT = (u8Data))
  157. /**
  158. * @brief This macro set smartcard stop bit length
  159. * @param[in] sc Base address of smartcard module
  160. * @param[in] u32Len Stop bit length, ether 1 or 2.
  161. * @return None
  162. * @details Stop bit length must be 1 for T = 1 protocol and 2 for T = 0 protocol.
  163. * \hideinitializer
  164. */
  165. #define SC_SET_STOP_BIT_LEN(sc, u32Len) ((sc)->CTL = ((sc)->CTL & ~SC_CTL_NSB_Msk) | ((u32Len) == 1 ? SC_CTL_NSB_Msk : 0))
  166. /* Declare these inline functions here to avoid MISRA C 2004 rule 8.1 error */
  167. __STATIC_INLINE void SC_SetTxRetry(SC_T *sc, uint32_t u32Count);
  168. __STATIC_INLINE void SC_SetRxRetry(SC_T *sc, uint32_t u32Count);
  169. /**
  170. * @brief Enable/Disable Tx error retry, and set Tx error retry count
  171. * @param[in] sc Base address of smartcard module
  172. * @param[in] u32Count The number of times of Tx error retry count, between 0~8. 0 means disable Tx error retry
  173. * @return None
  174. */
  175. __STATIC_INLINE void SC_SetTxRetry(SC_T *sc, uint32_t u32Count)
  176. {
  177. while((sc)->CTL & SC_CTL_SYNC_Msk)
  178. {
  179. ;
  180. }
  181. /* Retry count must set while enable bit disabled, so disable it first */
  182. (sc)->CTL &= ~(SC_CTL_TXRTY_Msk | SC_CTL_TXRTYEN_Msk);
  183. if((u32Count) != 0UL)
  184. {
  185. while((sc)->CTL & SC_CTL_SYNC_Msk)
  186. {
  187. ;
  188. }
  189. (sc)->CTL |= (((u32Count) - 1UL) << SC_CTL_TXRTY_Pos) | SC_CTL_TXRTYEN_Msk;
  190. }
  191. }
  192. /**
  193. * @brief Enable/Disable Rx error retry, and set Rx error retry count
  194. * @param[in] sc Base address of smartcard module
  195. * @param[in] u32Count The number of times of Rx error retry count, between 0~8. 0 means disable Rx error retry
  196. * @return None
  197. */
  198. __STATIC_INLINE void SC_SetRxRetry(SC_T *sc, uint32_t u32Count)
  199. {
  200. while((sc)->CTL & SC_CTL_SYNC_Msk)
  201. {
  202. ;
  203. }
  204. /* Retry count must set while enable bit disabled, so disable it first */
  205. (sc)->CTL &= ~(SC_CTL_RXRTY_Msk | SC_CTL_RXRTYEN_Msk);
  206. if((u32Count) != 0UL)
  207. {
  208. while((sc)->CTL & SC_CTL_SYNC_Msk)
  209. {
  210. ;
  211. }
  212. (sc)->CTL |= (((u32Count) - 1UL) << SC_CTL_RXRTY_Pos) | SC_CTL_RXRTYEN_Msk;
  213. }
  214. }
  215. uint32_t SC_IsCardInserted(SC_T *sc);
  216. void SC_ClearFIFO(SC_T *sc);
  217. void SC_Close(SC_T *sc);
  218. void SC_Open(SC_T *sc, uint32_t u32CardDet, uint32_t u32PWR);
  219. void SC_ResetReader(SC_T *sc);
  220. void SC_SetBlockGuardTime(SC_T *sc, uint32_t u32BGT);
  221. void SC_SetCharGuardTime(SC_T *sc, uint32_t u32CGT);
  222. void SC_StopAllTimer(SC_T *sc);
  223. void SC_StartTimer(SC_T *sc, uint32_t u32TimerNum, uint32_t u32Mode, uint32_t u32ETUCount);
  224. void SC_StopTimer(SC_T *sc, uint32_t u32TimerNum);
  225. uint32_t SC_GetInterfaceClock(SC_T *sc);
  226. /*@}*/ /* end of group SC_EXPORTED_FUNCTIONS */
  227. /*@}*/ /* end of group SC_Driver */
  228. /*@}*/ /* end of group Standard_Driver */
  229. #ifdef __cplusplus
  230. }
  231. #endif
  232. #endif /* __NU_SC_H__ */
  233. /*** (C) COPYRIGHT 2017 Nuvoton Technology Corp. ***/