sc.h 10.0 KB

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