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 
9 #ifndef _FSL_SMARTCARD_EMVSIM_H_
10 #define _FSL_SMARTCARD_EMVSIM_H_
11 
12 #include "fsl_smartcard.h"
13 
14 /*!
15  * @addtogroup smartcard_emvsim_driver
16  * @{
17  */
18 
19 /*******************************************************************************
20  * Definitions
21  ******************************************************************************/
22 
23 /*! @brief EMV RX NACK interrupt generation threshold */
24 #define SMARTCARD_EMV_RX_NACK_THRESHOLD (5u)
25 
26 /*! @brief EMV TX NACK interrupt generation threshold */
27 #define SMARTCARD_EMV_TX_NACK_THRESHOLD (5u)
28 
29 /*! @brief Smart card Word Wait Timer adjustment value */
30 #define SMARTCARD_WWT_ADJUSTMENT (160u)
31 
32 /*! @brief Smart card Character Wait Timer adjustment value */
33 #define SMARTCARD_CWT_ADJUSTMENT (3u)
34 
35 /*! @brief General Purpose Counter clock selections */
36 typedef enum _emvsim_gpc_clock_select
37 {
38     kEMVSIM_GPCClockDisable = 0u, /*!< Disabled */
39     kEMVSIM_GPCCardClock    = 1u, /*!< Card clock */
40     kEMVSIM_GPCRxClock      = 2u, /*!< Receive clock */
41     kEMVSIM_GPCTxClock      = 3u, /*!< Transmit ETU clock */
42 } emvsim_gpc_clock_select_t;
43 
44 /*! @brief EMVSIM card presence detection edge control */
45 typedef enum _presence_detect_edge
46 {
47     kEMVSIM_DetectOnFallingEdge = 0u, /*!< Presence detected on the falling edge */
48     kEMVSIM_DetectOnRisingEdge  = 1u, /*!< Presence detected on the rising edge */
49 } emvsim_presence_detect_edge_t;
50 
51 /*! @brief EMVSIM card presence detection status */
52 typedef enum _presence_detect_status
53 {
54     kEMVSIM_DetectPinIsLow  = 0u, /*!< Presence detected pin is logic low */
55     kEMVSIM_DetectPinIsHigh = 1u, /*!< Presence detected pin is logic high */
56 } emvsim_presence_detect_status_t;
57 
58 /*******************************************************************************
59  * API
60  ******************************************************************************/
61 #if defined(__cplusplus)
62 extern "C" {
63 #endif
64 
65 /*!
66  * @name Smart card EMVSIM Driver
67  * @{
68  */
69 
70 /*!
71  * @brief Fills in the smartcard_card_params structure with default values according to the EMV 4.3 specification.
72  *
73  * @param cardParams The configuration structure of type smartcard_interface_config_t.
74  * Function fill in members:
75  *        Fi = 372;
76  *        Di = 1;
77  *        currentD = 1;
78  *        WI = 0x0A;
79  *        GTN = 0x00;
80  * with default values.
81  */
82 void SMARTCARD_EMVSIM_GetDefaultConfig(smartcard_card_params_t *cardParams);
83 
84 /*!
85  * @brief Initializes an EMVSIM peripheral for the Smart card/ISO-7816 operation.
86  *
87  * This function un-gates the EMVSIM clock, initializes the module to EMV default settings,
88  * configures the IRQ, enables the module-level interrupt to the core and, initializes the driver context.
89  *
90  * @param base The EMVSIM peripheral base address.
91  * @param context A pointer to the smart card driver context structure.
92  * @param srcClock_Hz Smart card clock generation module source clock.
93  *
94  * @return An error code or kStatus_SMARTCARD_Success.
95  */
96 status_t SMARTCARD_EMVSIM_Init(EMVSIM_Type *base, smartcard_context_t *context, uint32_t srcClock_Hz);
97 
98 /*!
99  * @brief This function disables the EMVSIM interrupts, disables the transmitter and receiver,
100  * flushes the FIFOs, and gates EMVSIM clock in SIM.
101  *
102  * @param base The EMVSIM module base address.
103  */
104 void SMARTCARD_EMVSIM_Deinit(EMVSIM_Type *base);
105 
106 /*!
107  * @brief Returns whether the previous EMVSIM transfer has finished.
108  *
109  * When performing an async transfer, call this function to ascertain the context of the
110  * current transfer: in progress (or busy) or complete (success). If the
111  * transfer is still in progress, the user can obtain the number of words that have not been
112  * transferred.
113  *
114  * @param base The EMVSIM module base address.
115  * @param context A pointer to a smart card driver context structure.
116  *
117  * @return The number of bytes not transferred.
118  */
119 int32_t SMARTCARD_EMVSIM_GetTransferRemainingBytes(EMVSIM_Type *base, smartcard_context_t *context);
120 
121 /*!
122  * @brief Terminates an asynchronous EMVSIM transfer early.
123  *
124  * During an async EMVSIM transfer, the user can terminate the transfer early
125  * if the transfer is still in progress.
126  *
127  * @param base The EMVSIM peripheral address.
128  * @param context A pointer to a smart card driver context structure.
129  * @retval kStatus_SMARTCARD_Success The transmit abort was successful.
130  * @retval kStatus_SMARTCARD_NoTransmitInProgress No transmission is currently in progress.
131  */
132 status_t SMARTCARD_EMVSIM_AbortTransfer(EMVSIM_Type *base, smartcard_context_t *context);
133 
134 /*!
135  * @brief Transfer data using interrupts.
136  *
137  * A non-blocking (also known as asynchronous) function means that the function returns
138  * immediately after initiating the transfer function. The application has to get the
139  * transfer status to see when the transfer is complete. In other words, after calling the non-blocking
140  * (asynchronous) transfer function, the application must get the transfer status to check if the transmit
141  * is completed or not.
142  *
143  * @param base The EMVSIM peripheral base address.
144  * @param context A pointer to a smart card driver context structure.
145  * @param xfer A pointer to the smart card transfer structure where the linked buffers and sizes are stored.
146  *
147  * @return An error code or kStatus_SMARTCARD_Success.
148  */
149 status_t SMARTCARD_EMVSIM_TransferNonBlocking(EMVSIM_Type *base, smartcard_context_t *context, smartcard_xfer_t *xfer);
150 
151 /*!
152  * @brief Controls the EMVSIM module per different user request.
153  *
154  * @param base The EMVSIM peripheral base address.
155  * @param context A pointer to a smart card driver context structure.
156  * @param control Control type.
157  * @param param Integer value of specific to control command.
158  *
159  * return kStatus_SMARTCARD_Success in success.
160  * return kStatus_SMARTCARD_OtherError in case of error.
161  */
162 status_t SMARTCARD_EMVSIM_Control(EMVSIM_Type *base,
163                                   smartcard_context_t *context,
164                                   smartcard_control_t control,
165                                   uint32_t param);
166 
167 /*!
168  * @brief Handles EMVSIM module interrupts.
169  *
170  * @param base The EMVSIM peripheral base address.
171  * @param context A pointer to a smart card driver context structure.
172  */
173 void SMARTCARD_EMVSIM_IRQHandler(EMVSIM_Type *base, smartcard_context_t *context);
174 /*@}*/
175 
176 #if defined(__cplusplus)
177 }
178 #endif
179 
180 /*! @}*/
181 
182 #endif /* _FSL_SMARTCARD_EMVSIM_H_*/
183