1 /*
2  * Copyright 2021-2022 NXP
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef FSL_SMARTCARD_USIM_H_
8 #define FSL_SMARTCARD_USIM_H_
9 
10 #include "fsl_smartcard.h"
11 
12 /*!
13  * @addtogroup smartcard_usim_driver
14  * @{
15  */
16 
17 /*******************************************************************************
18  * Definitions
19  ******************************************************************************/
20 #define SMARTCARD_Control(base, handle, control, param)   SMARTCARD_USIM_Control(base, handle, control, 0)
21 #define SMARTCARD_TransferNonBlocking(base, handle, xfer) SMARTCARD_USIM_TransferNonBlocking(base, handle, xfer)
22 #define SMARTCARD_Init(base, handle, sourceClockHz)       SMARTCARD_USIM_Init(base, handle, sourceClockHz)
23 #define SMARTCARD_Deinit(base)                            SMARTCARD_USIM_Deinit(base)
24 #define SMARTCARD_GetTransferRemainingBytes(base, handle) SMARTCARD_USIM_GetTransferRemainingBytes(base, handle)
25 #define SMARTCARD_AbortTransfer(base, handle)             SMARTCARD_USIM_AbortTransfer(base, handle)
26 
27 /*! @brief EMV RX NACK interrupt generation threshold */
28 #define SMARTCARD_EMV_RX_NACK_THRESHOLD (4U)
29 
30 /*! @brief EMV TX NACK interrupt generation threshold */
31 #define SMARTCARD_EMV_TX_NACK_THRESHOLD (4U)
32 
33 /*! @brief Smart card T0 Character Wait Timer adjustment value */
34 #define SMARTCARD_T0_CWT_ADJUSTMENT (12U)
35 
36 /*! @brief Smart card T1 Character Wait Timer adjustment value */
37 #define SMARTCARD_T1_CWT_ADJUSTMENT (11U)
38 
39 /*! @brief Smart card T0 Block Wait Timer adjustment value */
40 #define SMARTCARD_T0_BWT_ADJUSTMENT (12U)
41 
42 /*! @brief Smart card T1 Block Wait Timer adjustment value */
43 #define SMARTCARD_T1_BWT_ADJUSTMENT (11U)
44 
45 /*! @brief Rx FIFO max receive trigger level */
46 #define SMARTCARD_MAX_RX_TRIGGER_LEVEL (12U)
47 
48 /*! @brief USIM Rx FIFO receiver trigger level enumeration. */
49 typedef enum _usim_rx_fifo_trigger_level
50 {
51     kUSIM_1ByteOrMore  = 0u, /*!< 1 byte or more in the RX-FIFO can trigger receiver data ready interrupt. */
52     kUSIM_4ByteOrMore  = 1u, /*!< 4 byte or more in the RX-FIFO can trigger receiver data ready interrupt. */
53     kUSIM_8ByteOrMore  = 2u, /*!< 8 byte or more in the RX-FIFO can trigger receiver data ready interrupt. */
54     kUSIM_12ByteOrMore = 3u, /*!< 12 byte or more in the RX-FIFO can trigger receiver data ready interrupt. */
55 } usim_rx_fifo_trigger_level_t;
56 
57 /*! @brief USIM Find max Rx FIFO receiver trigger level according to bytes numbers. */
58 #define USIM_FIND_RX_FIFO_TRIGGER_LEVEL(x)      \
59     (uint32_t)(((x) < 4U) ? kUSIM_1ByteOrMore : \
60                             (((x) < 8U) ? kUSIM_4ByteOrMore : (((x) < 12U) ? kUSIM_8ByteOrMore : kUSIM_12ByteOrMore)))
61 /*******************************************************************************
62  * API
63  ******************************************************************************/
64 #if defined(__cplusplus)
65 extern "C" {
66 #endif
67 
68 /*!
69  * @name Smart card USIM Driver
70  * @{
71  */
72 
73 /*!
74  * @brief Fills in the smartcard_card_params structure with default values according to the EMV 4.3 specification.
75  *
76  * @param cardParams The configuration structure of type @ref smartcard_interface_config_t.
77  * Function fill in members:
78  *        Fi = 372;
79  *        Di = 1;
80  *        currentD = 1;
81  *        WI = 0x0A;
82  *        GTN = 0x00;
83  * with default values.
84  */
85 void SMARTCARD_USIM_GetDefaultConfig(smartcard_card_params_t *cardParams);
86 
87 /*!
88  * @brief Initializes an USIM peripheral for the Smart card/ISO-7816 operation.
89  *
90  * This function un-gates the USIM clock, initializes the module to EMV default settings,
91  * configures the IRQ, enables the module-level interrupt to the core and, initializes the driver context.
92  *
93  * @param base The USIM peripheral base address.
94  * @param context A pointer to the smart card driver context structure.
95  * @param srcClock_Hz Smart card clock generation module source clock.
96  *
97  * @return An error code or kStatus_SMARTCARD_Success.
98  */
99 status_t SMARTCARD_USIM_Init(USIM_Type *base, smartcard_context_t *context, uint32_t srcClock_Hz);
100 
101 /*!
102  * @brief This function disables the USIM interrupts, disables the transmitter and receiver,
103  * flushes the FIFOs, and gates USIM clock in SIM.
104  *
105  * @param base The USIM module base address.
106  */
107 void SMARTCARD_USIM_Deinit(USIM_Type *base);
108 
109 /*!
110  * @brief Returns whether the previous USIM transfer has finished.
111  *
112  * When performing an async transfer, call this function to ascertain the context of the
113  * current transfer: in progress (or busy) or complete (success). If the
114  * transfer is still in progress, the user can obtain the number of words that have not been
115  * transferred.
116  *
117  * @param base The USIM module base address.
118  * @param context A pointer to a smart card driver context structure.
119  *
120  * @return The number of bytes not transferred.
121  */
122 int32_t SMARTCARD_USIM_GetTransferRemainingBytes(USIM_Type *base, smartcard_context_t *context);
123 
124 /*!
125  * @brief Terminates an asynchronous USIM transfer early.
126  *
127  * During an async USIM transfer, the user can terminate the transfer early
128  * if the transfer is still in progress.
129  *
130  * @param base The USIM peripheral address.
131  * @param context A pointer to a smart card driver context structure.
132  * @return kStatus_SMARTCARD_Success The transmit abort was successful.
133  * @return kStatus_SMARTCARD_NoTransmitInProgress No transmission is currently in progress.
134  */
135 status_t SMARTCARD_USIM_AbortTransfer(USIM_Type *base, smartcard_context_t *context);
136 
137 /*!
138  * @brief Transfer data using interrupts.
139  *
140  * A non-blocking (also known as asynchronous) function means that the function returns
141  * immediately after initiating the transfer function. The application has to get the
142  * transfer status to see when the transfer is complete. In other words, after calling the non-blocking
143  * (asynchronous) transfer function, the application must get the transfer status to check if the transmit
144  * is completed or not.
145  *
146  * @param base The USIM peripheral base address.
147  * @param context A pointer to a smart card driver context structure.
148  * @param xfer A pointer to the smart card transfer structure where the linked buffers and sizes are stored.
149  *
150  * @return An error code or kStatus_SMARTCARD_Success.
151  */
152 status_t SMARTCARD_USIM_TransferNonBlocking(USIM_Type *base, smartcard_context_t *context, smartcard_xfer_t *xfer);
153 
154 /*!
155  * @brief Controls the USIM module per different user request.
156  *
157  * @param base The USIM peripheral base address.
158  * @param context A pointer to a smart card driver context structure.
159  * @param control Control type.
160  * @param param Integer value of specific to control command.
161  *
162  * @return kStatus_SMARTCARD_Success in success.
163  * @return kStatus_SMARTCARD_OtherError in case of error.
164  */
165 status_t SMARTCARD_USIM_Control(USIM_Type *base,
166                                 smartcard_context_t *context,
167                                 smartcard_control_t control,
168                                 uint32_t param);
169 
170 /*!
171  * @brief Handles USIM module interrupts.
172  *
173  * @param base The USIM peripheral base address.
174  * @param context A pointer to a smart card driver context structure.
175  */
176 void SMARTCARD_USIM_IRQHandler(USIM_Type *base, smartcard_context_t *context);
177 
178 /*!
179  * @brief Handles initial TS character timer time-out event.
180  *
181  * @param base The USIM peripheral base address.
182  * @param context A pointer to a Smart card driver context structure.
183  */
184 void SMARTCARD_USIM_TSExpiryCallback(USIM_Type *base, smartcard_context_t *context);
185 
186 #if defined(FSL_FEATURE_SOC_CTIMER_COUNT) && (FSL_FEATURE_SOC_CTIMER_COUNT)
187 /*!
188  * @brief Initializes timer with input period, enable interrupt and start counter.
189  *
190  * @param time The time period.
191  */
192 void SMARTCARD_USIM_TimerStart(uint32_t time);
193 #endif /* FSL_FEATURE_SOC_CTIMER_COUNT */
194 
195 /*! @} */
196 
197 #if defined(__cplusplus)
198 }
199 #endif
200 
201 /*! @}*/
202 
203 #endif /* FSL_SMARTCARD_USIM_H_*/
204