Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:01

0001 /*
0002  * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
0003  * Copyright 2016-2017 NXP
0004  * All rights reserved.
0005  *
0006  * SPDX-License-Identifier: BSD-3-Clause
0007  */
0008 
0009 #ifndef _FSL_SMARTCARD_H_
0010 #define _FSL_SMARTCARD_H_
0011 
0012 #include "fsl_common.h"
0013 
0014 /*!
0015  * @addtogroup smartcard
0016  * @{
0017  */
0018 
0019 /*******************************************************************************
0020  * Definitions
0021  ******************************************************************************/
0022 
0023 /*! @name Driver version */
0024 /*@{*/
0025 /*! @brief Smart card driver version 2.2.2.
0026  */
0027 #define FSL_SMARTCARD_DRIVER_VERSION (MAKE_VERSION(2, 2, 2))
0028 /*@}*/
0029 
0030 /*! @brief Smart card global define which specify number of clock cycles until initial 'TS' character has to be received
0031  */
0032 #define SMARTCARD_INIT_DELAY_CLOCK_CYCLES (42000u)
0033 
0034 /*! @brief Smart card global define which specify number of clock cycles during which ATR string has to be received */
0035 #define SMARTCARD_EMV_ATR_DURATION_ETU (20150u)
0036 
0037 /*! @brief Smart card specification initial TS character definition of direct convention */
0038 #define SMARTCARD_TS_DIRECT_CONVENTION (0x3Bu)
0039 
0040 /*! @brief Smart card specification initial TS character definition of inverse convention */
0041 #define SMARTCARD_TS_INVERSE_CONVENTION (0x3Fu)
0042 
0043 /*! @brief Smart card Error codes. */
0044 enum
0045 {
0046     kStatus_SMARTCARD_Success              = MAKE_STATUS(kStatusGroup_SMARTCARD, 0), /*!< Transfer ends successfully */
0047     kStatus_SMARTCARD_TxBusy               = MAKE_STATUS(kStatusGroup_SMARTCARD, 1), /*!< Transmit in progress */
0048     kStatus_SMARTCARD_RxBusy               = MAKE_STATUS(kStatusGroup_SMARTCARD, 2), /*!< Receiving in progress */
0049     kStatus_SMARTCARD_NoTransferInProgress = MAKE_STATUS(kStatusGroup_SMARTCARD, 3), /*!< No transfer in progress */
0050     kStatus_SMARTCARD_Timeout              = MAKE_STATUS(kStatusGroup_SMARTCARD, 4), /*!< Transfer ends with time-out */
0051     kStatus_SMARTCARD_Initialized =
0052         MAKE_STATUS(kStatusGroup_SMARTCARD, 5), /*!< Smart card driver is already initialized */
0053     kStatus_SMARTCARD_PhyInitialized =
0054         MAKE_STATUS(kStatusGroup_SMARTCARD, 6), /*!< Smart card PHY drive is already  initialized */
0055     kStatus_SMARTCARD_CardNotActivated = MAKE_STATUS(kStatusGroup_SMARTCARD, 7), /*!< Smart card is not activated */
0056     kStatus_SMARTCARD_InvalidInput =
0057         MAKE_STATUS(kStatusGroup_SMARTCARD, 8), /*!< Function called with invalid input arguments */
0058     kStatus_SMARTCARD_OtherError = MAKE_STATUS(kStatusGroup_SMARTCARD, 9) /*!< Some other error occur */
0059 };
0060 
0061 /*! @brief Control codes for the Smart card protocol timers and misc. */
0062 typedef enum _smartcard_control
0063 {
0064     kSMARTCARD_EnableADT               = 0x0u,
0065     kSMARTCARD_DisableADT              = 0x1u,
0066     kSMARTCARD_EnableGTV               = 0x2u,
0067     kSMARTCARD_DisableGTV              = 0x3u,
0068     kSMARTCARD_ResetWWT                = 0x4u,
0069     kSMARTCARD_EnableWWT               = 0x5u,
0070     kSMARTCARD_DisableWWT              = 0x6u,
0071     kSMARTCARD_ResetCWT                = 0x7u,
0072     kSMARTCARD_EnableCWT               = 0x8u,
0073     kSMARTCARD_DisableCWT              = 0x9u,
0074     kSMARTCARD_ResetBWT                = 0xAu,
0075     kSMARTCARD_EnableBWT               = 0xBu,
0076     kSMARTCARD_DisableBWT              = 0xCu,
0077     kSMARTCARD_EnableInitDetect        = 0xDu,
0078     kSMARTCARD_EnableAnack             = 0xEu,
0079     kSMARTCARD_DisableAnack            = 0xFu,
0080     kSMARTCARD_ConfigureBaudrate       = 0x10u,
0081     kSMARTCARD_SetupATRMode            = 0x11u,
0082     kSMARTCARD_SetupT0Mode             = 0x12u,
0083     kSMARTCARD_SetupT1Mode             = 0x13u,
0084     kSMARTCARD_EnableReceiverMode      = 0x14u,
0085     kSMARTCARD_DisableReceiverMode     = 0x15u,
0086     kSMARTCARD_EnableTransmitterMode   = 0x16u,
0087     kSMARTCARD_DisableTransmitterMode  = 0x17u,
0088     kSMARTCARD_ResetWaitTimeMultiplier = 0x18u,
0089 } smartcard_control_t;
0090 
0091 /*! @brief Defines Smart card interface voltage class values */
0092 typedef enum _smartcard_card_voltage_class
0093 {
0094     kSMARTCARD_VoltageClassUnknown = 0x0u,
0095     kSMARTCARD_VoltageClassA5_0V   = 0x1u,
0096     kSMARTCARD_VoltageClassB3_3V   = 0x2u,
0097     kSMARTCARD_VoltageClassC1_8V   = 0x3u
0098 } smartcard_card_voltage_class_t;
0099 
0100 /*! @brief Defines Smart card I/O transfer states */
0101 typedef enum _smartcard_transfer_state
0102 {
0103     kSMARTCARD_IdleState               = 0x0u,
0104     kSMARTCARD_WaitingForTSState       = 0x1u,
0105     kSMARTCARD_InvalidTSDetecetedState = 0x2u,
0106     kSMARTCARD_ReceivingState          = 0x3u,
0107     kSMARTCARD_TransmittingState       = 0x4u,
0108 } smartcard_transfer_state_t;
0109 
0110 /*! @brief Defines Smart card reset types */
0111 typedef enum _smartcard_reset_type
0112 {
0113     kSMARTCARD_ColdReset   = 0x0u,
0114     kSMARTCARD_WarmReset   = 0x1u,
0115     kSMARTCARD_NoColdReset = 0x2u,
0116     kSMARTCARD_NoWarmReset = 0x3u,
0117 } smartcard_reset_type_t;
0118 
0119 /*! @brief Defines Smart card transport protocol types */
0120 typedef enum _smartcard_transport_type
0121 {
0122     kSMARTCARD_T0Transport = 0x0u,
0123     kSMARTCARD_T1Transport = 0x1u
0124 } smartcard_transport_type_t;
0125 
0126 /*! @brief Defines Smart card data parity types */
0127 typedef enum _smartcard_parity_type
0128 {
0129     kSMARTCARD_EvenParity = 0x0u,
0130     kSMARTCARD_OddParity  = 0x1u
0131 } smartcard_parity_type_t;
0132 
0133 /*! @brief Defines data Convention format */
0134 typedef enum _smartcard_card_convention
0135 {
0136     kSMARTCARD_DirectConvention  = 0x0u,
0137     kSMARTCARD_InverseConvention = 0x1u
0138 } smartcard_card_convention_t;
0139 
0140 /*! @brief Defines Smart card interface IC control types */
0141 typedef enum _smartcard_interface_control
0142 {
0143     kSMARTCARD_InterfaceSetVcc               = 0x00u,
0144     kSMARTCARD_InterfaceSetClockToResetDelay = 0x01u,
0145     kSMARTCARD_InterfaceReadStatus           = 0x02u
0146 } smartcard_interface_control_t;
0147 
0148 /*! @brief Defines transfer direction.*/
0149 typedef enum _smartcard_direction
0150 {
0151     kSMARTCARD_Receive  = 0u,
0152     kSMARTCARD_Transmit = 1u
0153 } smartcard_direction_t;
0154 
0155 /*! @brief Smart card interface interrupt callback function type */
0156 typedef void (*smartcard_interface_callback_t)(void *smartcardContext, void *param);
0157 /*! @brief Smart card transfer interrupt callback function type */
0158 typedef void (*smartcard_transfer_callback_t)(void *smartcardContext, void *param);
0159 
0160 /*! @brief Time Delay function used to passive waiting using RTOS [us] */
0161 typedef void (*smartcard_time_delay_t)(uint32_t us);
0162 
0163 /*! @brief Defines card-specific parameters for Smart card driver */
0164 typedef struct _smartcard_card_params
0165 {
0166     /* ISO7816/EMV4.3 specification variables */
0167     uint16_t Fi;            /*!< 4 bits Fi - clock rate conversion integer */
0168     uint8_t fMax;           /*!< Maximum Smart card frequency in MHz */
0169     uint8_t WI;             /*!< 8 bits WI - work wait time integer */
0170     uint8_t Di;             /*!< 4 bits DI - baud rate divisor */
0171     uint8_t BWI;            /*!< 4 bits BWI - block wait time integer */
0172     uint8_t CWI;            /*!< 4 bits CWI - character wait time integer */
0173     uint8_t BGI;            /*!< 4 bits BGI - block guard time integer */
0174     uint8_t GTN;            /*!< 8 bits GTN - extended guard time integer */
0175     uint8_t IFSC;           /*!< Indicates IFSC value of the card */
0176     uint8_t modeNegotiable; /*!< Indicates if the card acts in negotiable or a specific mode. */
0177     uint8_t currentD;       /*!< 4 bits DI - current baud rate divisor*/
0178     /* Driver-specific variables */
0179     uint8_t status;                         /*!< Indicates smart card status */
0180     bool t0Indicated;                       /*!< Indicates ff T=0 indicated in TD1 byte */
0181     bool t1Indicated;                       /*!< Indicates if T=1 indicated in TD2 byte */
0182     bool atrComplete;                       /*!< Indicates whether the ATR received from the card was complete or not */
0183     bool atrValid;                          /*!< Indicates whether the ATR received from the card was valid or not */
0184     bool present;                           /*!< Indicates if a smart card is present */
0185     bool active;                            /*!< Indicates if the smart card is activated */
0186     bool faulty;                            /*!< Indicates whether smart card/interface is faulty */
0187     smartcard_card_convention_t convention; /*!< Card convention, kSMARTCARD_DirectConvention for direct convention,
0188                                                  kSMARTCARD_InverseConvention for inverse convention */
0189 } smartcard_card_params_t;
0190 
0191 /*! @brief Smart card defines the state of the EMV timers in the Smart card driver */
0192 typedef struct _smartcard_timers_state
0193 {
0194     volatile bool adtExpired;           /*!< Indicates whether ADT timer expired */
0195     volatile bool wwtExpired;           /*!< Indicates whether WWT timer expired */
0196     volatile bool cwtExpired;           /*!< Indicates whether CWT timer expired */
0197     volatile bool bwtExpired;           /*!< Indicates whether BWT timer expired */
0198     volatile bool initCharTimerExpired; /*!< Indicates whether reception timer
0199                 for initialization character (TS) after the RST has expired */
0200 } smartcard_timers_state_t;
0201 
0202 /*! @brief Defines user specified configuration of Smart card interface */
0203 typedef struct _smartcard_interface_config
0204 {
0205     uint32_t smartCardClock;            /*!< Smart card interface clock [Hz] */
0206     uint32_t clockToResetDelay;         /*!< Indicates clock to RST apply delay [smart card clock cycles] */
0207     uint8_t clockModule;                /*!< Smart card clock module number */
0208     uint8_t clockModuleChannel;         /*!< Smart card clock module channel number */
0209     uint8_t clockModuleSourceClock;     /*!< Smart card clock module source clock [e.g., BusClk] */
0210     smartcard_card_voltage_class_t vcc; /*!< Smart card voltage class */
0211     uint8_t controlPort;                /*!< Smart card PHY control port instance */
0212     uint8_t controlPin;                 /*!< Smart card PHY control pin instance */
0213     uint8_t irqPort;                    /*!< Smart card PHY Interrupt port instance */
0214     uint8_t irqPin;                     /*!< Smart card PHY Interrupt pin instance */
0215     uint8_t resetPort;                  /*!< Smart card reset port instance */
0216     uint8_t resetPin;                   /*!< Smart card reset pin instance */
0217     uint8_t vsel0Port;                  /*!< Smart card PHY Vsel0 control port instance */
0218     uint8_t vsel0Pin;                   /*!< Smart card PHY Vsel0 control pin instance */
0219     uint8_t vsel1Port;                  /*!< Smart card PHY Vsel1 control port instance */
0220     uint8_t vsel1Pin;                   /*!< Smart card PHY Vsel1 control pin instance */
0221     uint8_t dataPort;                   /*!< Smart card PHY data port instance */
0222     uint8_t dataPin;                    /*!< Smart card PHY data pin instance */
0223     uint8_t dataPinMux;                 /*!< Smart card PHY data pin mux option */
0224     uint8_t tsTimerId; /*!< Numerical identifier of the External HW timer for Initial character detection */
0225 } smartcard_interface_config_t;
0226 
0227 /*! @brief Defines user transfer structure used to initialize transfer */
0228 typedef struct _smartcard_xfer
0229 {
0230     smartcard_direction_t direction; /*!< Direction of communication. (RX/TX) */
0231     uint8_t *buff;                   /*!< The buffer of data. */
0232     size_t size;                     /*!< The number of transferred units. */
0233 } smartcard_xfer_t;
0234 
0235 /*!
0236  * @brief Runtime state of the Smart card driver.
0237  */
0238 typedef struct _smartcard_context
0239 {
0240     /* Xfer part */
0241     void *base;                      /*!< Smart card module base address */
0242     smartcard_direction_t direction; /*!< Direction of communication. (RX/TX) */
0243     uint8_t *xBuff;                  /*!< The buffer of data being transferred.*/
0244     volatile size_t xSize;           /*!< The number of bytes to be transferred. */
0245     volatile bool xIsBusy;           /*!< True if there is an active transfer. */
0246     uint8_t txFifoEntryCount;        /*!< Number of data word entries in transmit FIFO. */
0247     uint8_t rxFifoThreshold;         /*!< The max value of the receiver FIFO threshold. */
0248     /* Smart card Interface part */
0249     smartcard_interface_callback_t interfaceCallback; /*!< Callback to invoke after interface IC raised interrupt.*/
0250     smartcard_transfer_callback_t transferCallback;   /*!< Callback to invoke after transfer event occur.*/
0251     void *interfaceCallbackParam;                     /*!< Interface callback parameter pointer.*/
0252     void *transferCallbackParam;                      /*!< Transfer callback parameter pointer.*/
0253     smartcard_time_delay_t timeDelay;                 /*!< Function which handles time delay defined by user or RTOS. */
0254     smartcard_reset_type_t resetType; /*!< Indicates whether a Cold reset or Warm reset was requested. */
0255     smartcard_transport_type_t tType; /*!< Indicates current transfer protocol (T0 or T1) */
0256     /* Smart card State part */
0257     volatile smartcard_transfer_state_t transferState; /*!< Indicates the current transfer state */
0258     smartcard_timers_state_t timersState; /*!< Indicates the state of different protocol timers used in driver */
0259     smartcard_card_params_t
0260         cardParams; /*!< Smart card parameters(ATR and current) and interface slots states(ATR and current) */
0261     uint8_t IFSD;   /*!< Indicates the terminal IFSD */
0262     smartcard_parity_type_t parity; /*!< Indicates current parity even/odd */
0263     volatile bool rxtCrossed;       /*!< Indicates whether RXT thresholds has been crossed */
0264     volatile bool txtCrossed;       /*!< Indicates whether TXT thresholds has been crossed */
0265     volatile bool wtxRequested;     /*!< Indicates whether WTX has been requested or not*/
0266     volatile bool parityError;      /*!< Indicates whether a parity error has been detected */
0267     uint8_t statusBytes[2]; /*!< Used to store Status bytes SW1, SW2 of the last executed card command response */
0268     /* Configuration part */
0269     smartcard_interface_config_t interfaceConfig; /*!< Smart card interface configuration structure */
0270     bool abortTransfer;                           /*!< Used to abort transfer. */
0271 } smartcard_context_t;
0272 
0273 /*! @}*/
0274 #endif /* _FSL_SMARTCARD_H_*/