fsl_smartcard.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #ifndef _FSL_SMARTCARD_H_
  9. #define _FSL_SMARTCARD_H_
  10. #include "fsl_common.h"
  11. /*!
  12. * @addtogroup smartcard
  13. * @{
  14. */
  15. /*******************************************************************************
  16. * Definitions
  17. ******************************************************************************/
  18. /*! @name Driver version */
  19. /*@{*/
  20. /*! @brief Smart card driver version 2.2.2.
  21. */
  22. #define FSL_SMARTCARD_DRIVER_VERSION (MAKE_VERSION(2, 2, 2))
  23. /*@}*/
  24. /*! @brief Smart card global define which specify number of clock cycles until initial 'TS' character has to be received
  25. */
  26. #define SMARTCARD_INIT_DELAY_CLOCK_CYCLES (42000u)
  27. /*! @brief Smart card global define which specify number of clock cycles during which ATR string has to be received */
  28. #define SMARTCARD_EMV_ATR_DURATION_ETU (20150u)
  29. /*! @brief Smart card specification initial TS character definition of direct convention */
  30. #define SMARTCARD_TS_DIRECT_CONVENTION (0x3Bu)
  31. /*! @brief Smart card specification initial TS character definition of inverse convention */
  32. #define SMARTCARD_TS_INVERSE_CONVENTION (0x3Fu)
  33. /*! @brief Smart card Error codes. */
  34. enum
  35. {
  36. kStatus_SMARTCARD_Success = MAKE_STATUS(kStatusGroup_SMARTCARD, 0), /*!< Transfer ends successfully */
  37. kStatus_SMARTCARD_TxBusy = MAKE_STATUS(kStatusGroup_SMARTCARD, 1), /*!< Transmit in progress */
  38. kStatus_SMARTCARD_RxBusy = MAKE_STATUS(kStatusGroup_SMARTCARD, 2), /*!< Receiving in progress */
  39. kStatus_SMARTCARD_NoTransferInProgress = MAKE_STATUS(kStatusGroup_SMARTCARD, 3), /*!< No transfer in progress */
  40. kStatus_SMARTCARD_Timeout = MAKE_STATUS(kStatusGroup_SMARTCARD, 4), /*!< Transfer ends with time-out */
  41. kStatus_SMARTCARD_Initialized =
  42. MAKE_STATUS(kStatusGroup_SMARTCARD, 5), /*!< Smart card driver is already initialized */
  43. kStatus_SMARTCARD_PhyInitialized =
  44. MAKE_STATUS(kStatusGroup_SMARTCARD, 6), /*!< Smart card PHY drive is already initialized */
  45. kStatus_SMARTCARD_CardNotActivated = MAKE_STATUS(kStatusGroup_SMARTCARD, 7), /*!< Smart card is not activated */
  46. kStatus_SMARTCARD_InvalidInput =
  47. MAKE_STATUS(kStatusGroup_SMARTCARD, 8), /*!< Function called with invalid input arguments */
  48. kStatus_SMARTCARD_OtherError = MAKE_STATUS(kStatusGroup_SMARTCARD, 9) /*!< Some other error occur */
  49. };
  50. /*! @brief Control codes for the Smart card protocol timers and misc. */
  51. typedef enum _smartcard_control
  52. {
  53. kSMARTCARD_EnableADT = 0x0u,
  54. kSMARTCARD_DisableADT = 0x1u,
  55. kSMARTCARD_EnableGTV = 0x2u,
  56. kSMARTCARD_DisableGTV = 0x3u,
  57. kSMARTCARD_ResetWWT = 0x4u,
  58. kSMARTCARD_EnableWWT = 0x5u,
  59. kSMARTCARD_DisableWWT = 0x6u,
  60. kSMARTCARD_ResetCWT = 0x7u,
  61. kSMARTCARD_EnableCWT = 0x8u,
  62. kSMARTCARD_DisableCWT = 0x9u,
  63. kSMARTCARD_ResetBWT = 0xAu,
  64. kSMARTCARD_EnableBWT = 0xBu,
  65. kSMARTCARD_DisableBWT = 0xCu,
  66. kSMARTCARD_EnableInitDetect = 0xDu,
  67. kSMARTCARD_EnableAnack = 0xEu,
  68. kSMARTCARD_DisableAnack = 0xFu,
  69. kSMARTCARD_ConfigureBaudrate = 0x10u,
  70. kSMARTCARD_SetupATRMode = 0x11u,
  71. kSMARTCARD_SetupT0Mode = 0x12u,
  72. kSMARTCARD_SetupT1Mode = 0x13u,
  73. kSMARTCARD_EnableReceiverMode = 0x14u,
  74. kSMARTCARD_DisableReceiverMode = 0x15u,
  75. kSMARTCARD_EnableTransmitterMode = 0x16u,
  76. kSMARTCARD_DisableTransmitterMode = 0x17u,
  77. kSMARTCARD_ResetWaitTimeMultiplier = 0x18u,
  78. } smartcard_control_t;
  79. /*! @brief Defines Smart card interface voltage class values */
  80. typedef enum _smartcard_card_voltage_class
  81. {
  82. kSMARTCARD_VoltageClassUnknown = 0x0u,
  83. kSMARTCARD_VoltageClassA5_0V = 0x1u,
  84. kSMARTCARD_VoltageClassB3_3V = 0x2u,
  85. kSMARTCARD_VoltageClassC1_8V = 0x3u
  86. } smartcard_card_voltage_class_t;
  87. /*! @brief Defines Smart card I/O transfer states */
  88. typedef enum _smartcard_transfer_state
  89. {
  90. kSMARTCARD_IdleState = 0x0u,
  91. kSMARTCARD_WaitingForTSState = 0x1u,
  92. kSMARTCARD_InvalidTSDetecetedState = 0x2u,
  93. kSMARTCARD_ReceivingState = 0x3u,
  94. kSMARTCARD_TransmittingState = 0x4u,
  95. } smartcard_transfer_state_t;
  96. /*! @brief Defines Smart card reset types */
  97. typedef enum _smartcard_reset_type
  98. {
  99. kSMARTCARD_ColdReset = 0x0u,
  100. kSMARTCARD_WarmReset = 0x1u,
  101. kSMARTCARD_NoColdReset = 0x2u,
  102. kSMARTCARD_NoWarmReset = 0x3u,
  103. } smartcard_reset_type_t;
  104. /*! @brief Defines Smart card transport protocol types */
  105. typedef enum _smartcard_transport_type
  106. {
  107. kSMARTCARD_T0Transport = 0x0u,
  108. kSMARTCARD_T1Transport = 0x1u
  109. } smartcard_transport_type_t;
  110. /*! @brief Defines Smart card data parity types */
  111. typedef enum _smartcard_parity_type
  112. {
  113. kSMARTCARD_EvenParity = 0x0u,
  114. kSMARTCARD_OddParity = 0x1u
  115. } smartcard_parity_type_t;
  116. /*! @brief Defines data Convention format */
  117. typedef enum _smartcard_card_convention
  118. {
  119. kSMARTCARD_DirectConvention = 0x0u,
  120. kSMARTCARD_InverseConvention = 0x1u
  121. } smartcard_card_convention_t;
  122. /*! @brief Defines Smart card interface IC control types */
  123. typedef enum _smartcard_interface_control
  124. {
  125. kSMARTCARD_InterfaceSetVcc = 0x00u,
  126. kSMARTCARD_InterfaceSetClockToResetDelay = 0x01u,
  127. kSMARTCARD_InterfaceReadStatus = 0x02u
  128. } smartcard_interface_control_t;
  129. /*! @brief Defines transfer direction.*/
  130. typedef enum _smartcard_direction
  131. {
  132. kSMARTCARD_Receive = 0u,
  133. kSMARTCARD_Transmit = 1u
  134. } smartcard_direction_t;
  135. /*! @brief Smart card interface interrupt callback function type */
  136. typedef void (*smartcard_interface_callback_t)(void *smartcardContext, void *param);
  137. /*! @brief Smart card transfer interrupt callback function type */
  138. typedef void (*smartcard_transfer_callback_t)(void *smartcardContext, void *param);
  139. /*! @brief Time Delay function used to passive waiting using RTOS [us] */
  140. typedef void (*smartcard_time_delay_t)(uint32_t us);
  141. /*! @brief Defines card-specific parameters for Smart card driver */
  142. typedef struct _smartcard_card_params
  143. {
  144. /* ISO7816/EMV4.3 specification variables */
  145. uint16_t Fi; /*!< 4 bits Fi - clock rate conversion integer */
  146. uint8_t fMax; /*!< Maximum Smart card frequency in MHz */
  147. uint8_t WI; /*!< 8 bits WI - work wait time integer */
  148. uint8_t Di; /*!< 4 bits DI - baud rate divisor */
  149. uint8_t BWI; /*!< 4 bits BWI - block wait time integer */
  150. uint8_t CWI; /*!< 4 bits CWI - character wait time integer */
  151. uint8_t BGI; /*!< 4 bits BGI - block guard time integer */
  152. uint8_t GTN; /*!< 8 bits GTN - extended guard time integer */
  153. uint8_t IFSC; /*!< Indicates IFSC value of the card */
  154. uint8_t modeNegotiable; /*!< Indicates if the card acts in negotiable or a specific mode. */
  155. uint8_t currentD; /*!< 4 bits DI - current baud rate divisor*/
  156. /* Driver-specific variables */
  157. uint8_t status; /*!< Indicates smart card status */
  158. bool t0Indicated; /*!< Indicates ff T=0 indicated in TD1 byte */
  159. bool t1Indicated; /*!< Indicates if T=1 indicated in TD2 byte */
  160. bool atrComplete; /*!< Indicates whether the ATR received from the card was complete or not */
  161. bool atrValid; /*!< Indicates whether the ATR received from the card was valid or not */
  162. bool present; /*!< Indicates if a smart card is present */
  163. bool active; /*!< Indicates if the smart card is activated */
  164. bool faulty; /*!< Indicates whether smart card/interface is faulty */
  165. smartcard_card_convention_t convention; /*!< Card convention, kSMARTCARD_DirectConvention for direct convention,
  166. kSMARTCARD_InverseConvention for inverse convention */
  167. } smartcard_card_params_t;
  168. /*! @brief Smart card defines the state of the EMV timers in the Smart card driver */
  169. typedef struct _smartcard_timers_state
  170. {
  171. volatile bool adtExpired; /*!< Indicates whether ADT timer expired */
  172. volatile bool wwtExpired; /*!< Indicates whether WWT timer expired */
  173. volatile bool cwtExpired; /*!< Indicates whether CWT timer expired */
  174. volatile bool bwtExpired; /*!< Indicates whether BWT timer expired */
  175. volatile bool initCharTimerExpired; /*!< Indicates whether reception timer
  176. for initialization character (TS) after the RST has expired */
  177. } smartcard_timers_state_t;
  178. /*! @brief Defines user specified configuration of Smart card interface */
  179. typedef struct _smartcard_interface_config
  180. {
  181. uint32_t smartCardClock; /*!< Smart card interface clock [Hz] */
  182. uint32_t clockToResetDelay; /*!< Indicates clock to RST apply delay [smart card clock cycles] */
  183. uint8_t clockModule; /*!< Smart card clock module number */
  184. uint8_t clockModuleChannel; /*!< Smart card clock module channel number */
  185. uint8_t clockModuleSourceClock; /*!< Smart card clock module source clock [e.g., BusClk] */
  186. smartcard_card_voltage_class_t vcc; /*!< Smart card voltage class */
  187. uint8_t controlPort; /*!< Smart card PHY control port instance */
  188. uint8_t controlPin; /*!< Smart card PHY control pin instance */
  189. uint8_t irqPort; /*!< Smart card PHY Interrupt port instance */
  190. uint8_t irqPin; /*!< Smart card PHY Interrupt pin instance */
  191. uint8_t resetPort; /*!< Smart card reset port instance */
  192. uint8_t resetPin; /*!< Smart card reset pin instance */
  193. uint8_t vsel0Port; /*!< Smart card PHY Vsel0 control port instance */
  194. uint8_t vsel0Pin; /*!< Smart card PHY Vsel0 control pin instance */
  195. uint8_t vsel1Port; /*!< Smart card PHY Vsel1 control port instance */
  196. uint8_t vsel1Pin; /*!< Smart card PHY Vsel1 control pin instance */
  197. uint8_t dataPort; /*!< Smart card PHY data port instance */
  198. uint8_t dataPin; /*!< Smart card PHY data pin instance */
  199. uint8_t dataPinMux; /*!< Smart card PHY data pin mux option */
  200. uint8_t tsTimerId; /*!< Numerical identifier of the External HW timer for Initial character detection */
  201. } smartcard_interface_config_t;
  202. /*! @brief Defines user transfer structure used to initialize transfer */
  203. typedef struct _smartcard_xfer
  204. {
  205. smartcard_direction_t direction; /*!< Direction of communication. (RX/TX) */
  206. uint8_t *buff; /*!< The buffer of data. */
  207. size_t size; /*!< The number of transferred units. */
  208. } smartcard_xfer_t;
  209. /*!
  210. * @brief Runtime state of the Smart card driver.
  211. */
  212. typedef struct _smartcard_context
  213. {
  214. /* Xfer part */
  215. void *base; /*!< Smart card module base address */
  216. smartcard_direction_t direction; /*!< Direction of communication. (RX/TX) */
  217. uint8_t *xBuff; /*!< The buffer of data being transferred.*/
  218. volatile size_t xSize; /*!< The number of bytes to be transferred. */
  219. volatile bool xIsBusy; /*!< True if there is an active transfer. */
  220. uint8_t txFifoEntryCount; /*!< Number of data word entries in transmit FIFO. */
  221. uint8_t rxFifoThreshold; /*!< The max value of the receiver FIFO threshold. */
  222. /* Smart card Interface part */
  223. smartcard_interface_callback_t interfaceCallback; /*!< Callback to invoke after interface IC raised interrupt.*/
  224. smartcard_transfer_callback_t transferCallback; /*!< Callback to invoke after transfer event occur.*/
  225. void *interfaceCallbackParam; /*!< Interface callback parameter pointer.*/
  226. void *transferCallbackParam; /*!< Transfer callback parameter pointer.*/
  227. smartcard_time_delay_t timeDelay; /*!< Function which handles time delay defined by user or RTOS. */
  228. smartcard_reset_type_t resetType; /*!< Indicates whether a Cold reset or Warm reset was requested. */
  229. smartcard_transport_type_t tType; /*!< Indicates current transfer protocol (T0 or T1) */
  230. /* Smart card State part */
  231. volatile smartcard_transfer_state_t transferState; /*!< Indicates the current transfer state */
  232. smartcard_timers_state_t timersState; /*!< Indicates the state of different protocol timers used in driver */
  233. smartcard_card_params_t
  234. cardParams; /*!< Smart card parameters(ATR and current) and interface slots states(ATR and current) */
  235. uint8_t IFSD; /*!< Indicates the terminal IFSD */
  236. smartcard_parity_type_t parity; /*!< Indicates current parity even/odd */
  237. volatile bool rxtCrossed; /*!< Indicates whether RXT thresholds has been crossed */
  238. volatile bool txtCrossed; /*!< Indicates whether TXT thresholds has been crossed */
  239. volatile bool wtxRequested; /*!< Indicates whether WTX has been requested or not*/
  240. volatile bool parityError; /*!< Indicates whether a parity error has been detected */
  241. uint8_t statusBytes[2]; /*!< Used to store Status bytes SW1, SW2 of the last executed card command response */
  242. /* Configuration part */
  243. smartcard_interface_config_t interfaceConfig; /*!< Smart card interface configuration structure */
  244. bool abortTransfer; /*!< Used to abort transfer. */
  245. } smartcard_context_t;
  246. /*! @}*/
  247. #endif /* _FSL_SMARTCARD_H_*/