fsl_smartcard_emvsim.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2017 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include "fsl_smartcard_emvsim.h"
  35. /* Component ID definition, used by tools. */
  36. #ifndef FSL_COMPONENT_ID
  37. #define FSL_COMPONENT_ID "platform.drivers.smartcard_emvsim"
  38. #endif
  39. /*******************************************************************************
  40. * Variables
  41. ******************************************************************************/
  42. /*! @brief Pointers to emvsim bases for each instance. */
  43. static EMVSIM_Type *const s_emvsimBases[] = EMVSIM_BASE_PTRS;
  44. /*! @brief Pointers to emvsim IRQ number for each instance. */
  45. static const IRQn_Type s_emvsimIRQ[] = EMVSIM_IRQS;
  46. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  47. /*! @brief Pointers to emvsim clocks for each instance. */
  48. static const clock_ip_name_t s_emvsimClock[] = EMVSIM_CLOCKS;
  49. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  50. /* #define CARDSIM_EXTRADELAY_USED */
  51. /*******************************************************************************
  52. * Private Functions
  53. ******************************************************************************/
  54. static void smartcard_emvsim_CompleteSendData(EMVSIM_Type *base, smartcard_context_t *context);
  55. static void smartcard_emvsim_StartSendData(EMVSIM_Type *base, smartcard_context_t *context);
  56. static void smartcard_emvsim_CompleteReceiveData(EMVSIM_Type *base, smartcard_context_t *context);
  57. static void smartcard_emvsim_StartReceiveData(EMVSIM_Type *base, smartcard_context_t *context);
  58. static void smartcard_emvsim_SetTransferType(EMVSIM_Type *base,
  59. smartcard_context_t *context,
  60. smartcard_control_t control);
  61. static uint32_t smartcard_emvsim_GetInstance(EMVSIM_Type *base);
  62. /*******************************************************************************
  63. * Code
  64. ******************************************************************************/
  65. /*!
  66. * @brief Get the UART instance from peripheral base address.
  67. *
  68. * @param base UART peripheral base address.
  69. * @return UART instance.
  70. */
  71. static uint32_t smartcard_emvsim_GetInstance(EMVSIM_Type *base)
  72. {
  73. uint8_t instance = 0;
  74. uint32_t emvsimArrayCount = (sizeof(s_emvsimBases) / sizeof(s_emvsimBases[0]));
  75. /* Find the instance index from base address mappings. */
  76. for (instance = 0; instance < emvsimArrayCount; instance++)
  77. {
  78. if (s_emvsimBases[instance] == base)
  79. {
  80. break;
  81. }
  82. }
  83. assert(instance < emvsimArrayCount);
  84. return instance;
  85. }
  86. /*!
  87. * @brief Finish up a transmit by completing the process of sending data and disabling the interrupt.
  88. *
  89. * @param base The EMVSIM peripheral base address.
  90. * @param context A pointer to a SMARTCARD driver context structure.
  91. */
  92. static void smartcard_emvsim_CompleteSendData(EMVSIM_Type *base, smartcard_context_t *context)
  93. {
  94. assert((NULL != context));
  95. /* Disable ETC and TDT interrupt */
  96. base->INT_MASK |= (EMVSIM_INT_MASK_ETC_IM_MASK | EMVSIM_INT_MASK_TDT_IM_MASK);
  97. /* Disable transmitter */
  98. base->CTRL &= ~EMVSIM_CTRL_XMT_EN_MASK;
  99. /* Clear receive status flag */
  100. base->RX_STATUS = EMVSIM_RX_STATUS_RX_DATA_MASK;
  101. /* Enable Receiver */
  102. base->CTRL |= EMVSIM_CTRL_RCV_EN_MASK;
  103. /* Update the information of the module driver context */
  104. context->xIsBusy = false;
  105. context->transferState = kSMARTCARD_IdleState;
  106. /* Clear txSize to avoid any spurious transmit from ISR */
  107. context->xSize = 0u;
  108. /* Invoke user call-back */
  109. if (NULL != context->transferCallback)
  110. {
  111. context->transferCallback(context, context->transferCallbackParam);
  112. }
  113. }
  114. /*!
  115. * @brief Finish up a receive by completing the process of receiving data and disabling the interrupt.
  116. *
  117. * @param base The EMVSIM peripheral base address.
  118. * @param context A pointer to a SMARTCARD driver context structure.
  119. */
  120. static void smartcard_emvsim_CompleteReceiveData(EMVSIM_Type *base, smartcard_context_t *context)
  121. {
  122. assert((NULL != context));
  123. /* Disable RDT and RX_DATA interrupt */
  124. base->INT_MASK |= (EMVSIM_INT_MASK_RDT_IM_MASK | EMVSIM_INT_MASK_RX_DATA_IM_MASK);
  125. /* Read data from fifo */
  126. while ((base->RX_STATUS & EMVSIM_RX_STATUS_RX_CNT_MASK) && (context->xSize))
  127. {
  128. /* Get data and put into receive buffer */
  129. *context->xBuff = (uint8_t)(base->RX_BUF);
  130. ++context->xBuff;
  131. --context->xSize;
  132. }
  133. /* Update the information of the module driver context */
  134. context->xIsBusy = false;
  135. /* Invoke user call-back */
  136. if (NULL != context->transferCallback)
  137. {
  138. context->transferCallback(context, context->transferCallbackParam);
  139. }
  140. }
  141. /*!
  142. * @brief Initiate (start) a transmit by beginning the process of sending data and enabling the interrupt.
  143. *
  144. * @param base The EMVSIM peripheral base address.
  145. * @param context A pointer to a SMARTCARD driver context structure.
  146. */
  147. static void smartcard_emvsim_StartSendData(EMVSIM_Type *base, smartcard_context_t *context)
  148. {
  149. assert((NULL != context));
  150. uint32_t delay = 0u;
  151. uint32_t control = 0u;
  152. /* Block guard time */
  153. /* 22 etus (16 Receiver Clocks == 1 etu) */
  154. delay = 22u * 16u;
  155. /* Disable all functionality like protocol timers, NACK generation */
  156. control = base->CTRL;
  157. base->CTRL = 0u;
  158. base->TX_GETU = context->cardParams.GTN;
  159. /* Clear Global counter time-out flag */
  160. base->TX_STATUS = EMVSIM_TX_STATUS_GPCNT1_TO_MASK;
  161. /* Disable counter interrupt */
  162. base->INT_MASK |= EMVSIM_INT_MASK_GPCNT1_IM_MASK;
  163. /* Set counter value */
  164. base->GPCNT1_VAL = delay;
  165. /* Select the clock for GPCNT */
  166. base->CLKCFG =
  167. (base->CLKCFG & ~EMVSIM_CLKCFG_GPCNT1_CLK_SEL_MASK) | EMVSIM_CLKCFG_GPCNT1_CLK_SEL(kEMVSIM_GPCRxClock);
  168. /* Trigger the counter */
  169. base->CTRL |= EMVSIM_CTRL_RCV_EN_MASK;
  170. /* Wait until counter overflow event occur */
  171. while ((!(base->TX_STATUS & EMVSIM_TX_STATUS_GPCNT1_TO_MASK)))
  172. {
  173. }
  174. /* Clear status flag and disable GPCNT1 clock */
  175. base->TX_STATUS = EMVSIM_TX_STATUS_GPCNT1_TO_MASK;
  176. base->CLKCFG &= ~EMVSIM_CLKCFG_GPCNT1_CLK_SEL_MASK;
  177. /* Restore Control register */
  178. base->CTRL = control & ~(EMVSIM_CTRL_XMT_EN_MASK | EMVSIM_CTRL_RCV_EN_MASK);
  179. /* Update transferState */
  180. context->transferState = kSMARTCARD_TransmittingState;
  181. context->xIsBusy = true;
  182. /* Flush transmitter */
  183. base->CTRL |= EMVSIM_CTRL_FLSH_TX_MASK;
  184. /* Enable transmitter */
  185. base->CTRL |= EMVSIM_CTRL_XMT_EN_MASK;
  186. /* Set transmitter data threshold value to 0 - TDTF is set when the fifo is empty */
  187. base->TX_THD &= ~EMVSIM_TX_THD_TDT_MASK;
  188. /* Enable TDT interrupt */
  189. base->INT_MASK &= ~EMVSIM_INT_MASK_TDT_IM_MASK;
  190. }
  191. /*!
  192. * @brief Initiate (start) a receive by beginning the process of receiving data and enabling the interrupt.
  193. *
  194. * @param base The EMVSIM peripheral base address.
  195. * @param context A pointer to a SMARTCARD driver context structure.
  196. */
  197. static void smartcard_emvsim_StartReceiveData(EMVSIM_Type *base, smartcard_context_t *context)
  198. {
  199. assert((NULL != context));
  200. /* Initialize the module driver context structure to indicate transfer in progress */
  201. context->xIsBusy = true;
  202. /* Enable BWT Timer interrupt to occur */
  203. base->INT_MASK &= ~EMVSIM_INT_MASK_BWT_ERR_IM_MASK;
  204. /* Disable transmitter */
  205. base->CTRL &= ~EMVSIM_CTRL_XMT_EN_MASK;
  206. /* Enable receiver and switch to receive direction */
  207. base->CTRL |= EMVSIM_CTRL_RCV_EN_MASK;
  208. /* Set rx threshold value - number of bytes that must exist in the Receive FIFO to trigger the receive data
  209. * threshold interrupt flag (RDTF).*/
  210. if (context->xSize < context->rxFifoThreshold)
  211. {
  212. base->RX_THD = ((base->RX_THD & ~EMVSIM_RX_THD_RDT_MASK) | context->xSize);
  213. }
  214. else
  215. {
  216. base->RX_THD = ((base->RX_THD & ~EMVSIM_RX_THD_RDT_MASK) | context->rxFifoThreshold);
  217. }
  218. /* Enable RDT interrupt - count of bytes in rx fifo is equal or greater than threshold RX_THD[RDT] */
  219. base->INT_MASK &= ~EMVSIM_INT_MASK_RDT_IM_MASK;
  220. if (context->tType == kSMARTCARD_T1Transport)
  221. {
  222. /* Enable interrupt when new byte is received - in T=1 is necessary to disable BWT interrupt and enable CWT
  223. * interrupt after receiving the first byte */
  224. base->INT_MASK &= ~EMVSIM_INT_MASK_RX_DATA_IM_MASK;
  225. }
  226. }
  227. /*!
  228. * @brief Sets up the EMVSIM hardware for T=0 or T=1 protocol data exchange and initialize timer values.
  229. *
  230. * @param base The EMVSIM peripheral base address.
  231. * @param context A pointer to a SMARTCARD driver context structure.
  232. */
  233. static void smartcard_emvsim_SetTransferType(EMVSIM_Type *base,
  234. smartcard_context_t *context,
  235. smartcard_control_t control)
  236. {
  237. assert((NULL != context));
  238. assert((control == kSMARTCARD_SetupATRMode) || (control == kSMARTCARD_SetupT0Mode) ||
  239. (control == kSMARTCARD_SetupT1Mode));
  240. uint16_t temp16 = 0u;
  241. uint32_t bwiVal = 0u;
  242. uint8_t tdt = 0u;
  243. if (control == kSMARTCARD_SetupATRMode)
  244. {
  245. /* Disable all functionality at first */
  246. base->CTRL &= ~(EMVSIM_CTRL_RCVR_11_MASK | EMVSIM_CTRL_XMT_CRC_LRC_MASK | EMVSIM_CTRL_LRC_EN_MASK |
  247. EMVSIM_CTRL_ANACK_MASK | EMVSIM_CTRL_ONACK_MASK | EMVSIM_CTRL_RCV_EN_MASK);
  248. /* Set default values as per EMV specification */
  249. context->cardParams.Fi = 372u;
  250. context->cardParams.Di = 1u;
  251. context->cardParams.currentD = 1u;
  252. context->cardParams.WI = 0x0Au;
  253. context->cardParams.GTN = 0x00u;
  254. /* Set default baudrate/ETU time based on EMV parameters and card clock */
  255. base->DIVISOR = ((context->cardParams.Fi / context->cardParams.currentD) & 0x1FFu);
  256. /* EMV expectation: WWT = (960 x D x WI) + (D x 480)
  257. * EMVSIM formula: BWT_VAL[15:0] = CWT_VAL[15:0] */
  258. temp16 = (960u * context->cardParams.currentD * context->cardParams.WI) +
  259. (context->cardParams.currentD * 480u) + SMARTCARD_WWT_ADJUSTMENT;
  260. base->CWT_VAL = temp16;
  261. base->BWT_VAL = temp16;
  262. /* Set Extended Guard Timer value
  263. * EMV expectation: GT = GTN not equal to 255 -> 12 + GTN = GTN equal to 255 -> 12
  264. * EMVSIM formula: same as above */
  265. base->TX_GETU = context->cardParams.GTN;
  266. /* Setting Rx threshold so that an interrupt is generated when a NACK is
  267. sent either due to parity error or wrong INIT char*/
  268. base->RX_THD = EMVSIM_RX_THD_RDT(1);
  269. /* Setting up Tx NACK threshold */
  270. tdt = ((base->PARAM & EMVSIM_PARAM_TX_FIFO_DEPTH_MASK) >> EMVSIM_PARAM_TX_FIFO_DEPTH_SHIFT) - 1;
  271. base->TX_THD = (EMVSIM_TX_THD_TNCK_THD(SMARTCARD_EMV_TX_NACK_THRESHOLD) | EMVSIM_TX_THD_TDT(tdt));
  272. /* Clear all pending interrupts */
  273. base->RX_STATUS = 0xFFFFFFFFu;
  274. /* Enable Tx NACK threshold interrupt to occur */
  275. base->INT_MASK &= ~EMVSIM_INT_MASK_TNACK_IM_MASK;
  276. /* Set transport type to T=0 in SMARTCARD context structure */
  277. context->tType = kSMARTCARD_T0Transport;
  278. }
  279. else if (control == kSMARTCARD_SetupT0Mode)
  280. {
  281. /* Disable receiver at first if it's not, Disable T=0 mode counters 1st,
  282. * Setup for single wire ISO7816 mode (setup 12 etu mode).
  283. * Set transport protocol type to T=0, Disable initial character detection.*/
  284. base->CTRL &=
  285. ~(EMVSIM_CTRL_RCV_EN_MASK | EMVSIM_CTRL_CWT_EN_MASK | EMVSIM_CTRL_BWT_EN_MASK | EMVSIM_CTRL_RCVR_11_MASK |
  286. EMVSIM_CTRL_XMT_CRC_LRC_MASK | EMVSIM_CTRL_LRC_EN_MASK | EMVSIM_CTRL_ICM_MASK);
  287. /* EMV expectation: WWT = (960 x D x WI) + (D x 480)
  288. * EMVSIM formula: BWT_VAL[15:0] = CWT_VAL[15:0] */
  289. temp16 = (960u * context->cardParams.currentD * context->cardParams.WI) +
  290. (context->cardParams.currentD * 480u) + SMARTCARD_WWT_ADJUSTMENT;
  291. base->CWT_VAL = temp16;
  292. base->BWT_VAL = temp16;
  293. /* Set Extended Guard Timer value
  294. * EMV expectation: GT = GTN not equal to 255 -> 12 + GTN = GTN equal to 255 -> 12
  295. * EMVSIM formula: same as above for range [0:254]
  296. * Fix for EMV. If TX_GETU == 0 in T0 mode, 3 stop bits are inserted. */
  297. context->cardParams.GTN = (context->cardParams.GTN == 0xFFu) ? 0x00u : context->cardParams.GTN;
  298. base->TX_GETU = context->cardParams.GTN;
  299. /* Setting Rx threshold so that an interrupt is generated when a NACK is
  300. sent either due to parity error or wrong INIT char */
  301. base->RX_THD = (EMVSIM_RX_THD_RNCK_THD(SMARTCARD_EMV_RX_NACK_THRESHOLD) | EMVSIM_RX_THD_RDT(1));
  302. /* Setting up Tx NACK threshold */
  303. tdt = ((base->PARAM & EMVSIM_PARAM_TX_FIFO_DEPTH_MASK) >> EMVSIM_PARAM_TX_FIFO_DEPTH_SHIFT) - 1;
  304. base->TX_THD = (EMVSIM_TX_THD_TNCK_THD(SMARTCARD_EMV_TX_NACK_THRESHOLD) | EMVSIM_TX_THD_TDT(tdt));
  305. /* Enable Tx NACK threshold interrupt to occur */
  306. base->INT_MASK &= ~EMVSIM_INT_MASK_TNACK_IM_MASK;
  307. /* Enable T=0 mode counters, Enable NACK on error interrupt and NACK on overflow interrupt */
  308. base->CTRL |=
  309. (EMVSIM_CTRL_CWT_EN_MASK | EMVSIM_CTRL_BWT_EN_MASK | EMVSIM_CTRL_ANACK_MASK | EMVSIM_CTRL_ONACK_MASK);
  310. /* Set transport type to T=0 in SMARTCARD context structure */
  311. context->tType = kSMARTCARD_T0Transport;
  312. }
  313. else
  314. { /* Disable T=1 mode counters 1st, Disable NACK on error interrupt, Disable NACK on overflow interrupt */
  315. base->CTRL &= ~(EMVSIM_CTRL_CWT_EN_MASK | EMVSIM_CTRL_BWT_EN_MASK | EMVSIM_CTRL_ANACK_MASK |
  316. EMVSIM_CTRL_ONACK_MASK | EMVSIM_CTRL_XMT_CRC_LRC_MASK | EMVSIM_CTRL_LRC_EN_MASK);
  317. /* Calculate and set Block Wait Timer (BWT) value
  318. * EMV expectation: BWT = 11 + (2^BWI x 960 x D) + (D x 960) = 11 + (2^BWI + 1) x 960 x D
  319. * EMVSIM formula: BWT = Same */
  320. bwiVal = 11 + (((1 << context->cardParams.BWI) + 1u) * 960u * context->cardParams.currentD);
  321. #ifdef CARDSIM_EXTRADELAY_USED
  322. base->BWT_VAL = bwiVal + 100;
  323. #else
  324. base->BWT_VAL = bwiVal;
  325. #endif
  326. /* Calculate and set Character Wait Timer (CWT) value
  327. * EMV expectation: CWT = ((2^CWI + 11) + 4)
  328. * EMVSIM formula: CWT = Same */
  329. if (context->cardParams.currentD == 1u)
  330. {
  331. #ifdef CARDSIM_EXTRADELAY_USED
  332. temp16 = (1u << context->cardParams.CWI) + 16u;
  333. #else
  334. temp16 = (1u << context->cardParams.CWI) + 15u;
  335. #endif
  336. }
  337. else
  338. {
  339. #ifdef CARDSIM_EXTRADELAY_USED
  340. temp16 = (1u << context->cardParams.CWI) + 20u + SMARTCARD_CWT_ADJUSTMENT;
  341. #else
  342. temp16 = (1u << context->cardParams.CWI) + 15u + SMARTCARD_CWT_ADJUSTMENT;
  343. #endif
  344. }
  345. /* EMV = 15, ISO = 11,
  346. * EMV expectation: BGT = 22
  347. * EMVSIM formula: BGT = Same */
  348. base->CWT_VAL = temp16;
  349. context->cardParams.BGI = 22u;
  350. base->BGT_VAL = context->cardParams.BGI;
  351. /* Set Extended Guard Timer value
  352. * EMV expectation: GT = GTN not equal to 255 -> 12 + GTN = GTN equal to 255 -> 11
  353. * EMVSIM formula: same as above */
  354. base->TX_GETU = context->cardParams.GTN;
  355. /* Setup for single wire ISO7816 mode,
  356. * Set transport protocol type to T=1, Enable T=0 mode counters */
  357. base->CTRL |= (EMVSIM_CTRL_RCVR_11_MASK | EMVSIM_CTRL_CWT_EN_MASK | EMVSIM_CTRL_BWT_EN_MASK);
  358. /* Setting Rx threshold */
  359. base->RX_THD = (EMVSIM_RX_THD_RNCK_THD(SMARTCARD_EMV_RX_NACK_THRESHOLD) | EMVSIM_RX_THD_RDT(1));
  360. /* Setting up Tx threshold */
  361. tdt = ((base->PARAM & EMVSIM_PARAM_TX_FIFO_DEPTH_MASK) >> EMVSIM_PARAM_TX_FIFO_DEPTH_SHIFT) - 1;
  362. base->TX_THD = (EMVSIM_TX_THD_TDT(tdt) | EMVSIM_TX_THD_TNCK_THD(SMARTCARD_EMV_TX_NACK_THRESHOLD));
  363. /* Set transport type to T=1 in SMARTCARD context structure */
  364. context->tType = kSMARTCARD_T1Transport;
  365. }
  366. }
  367. void SMARTCARD_EMVSIM_GetDefaultConfig(smartcard_card_params_t *cardParams)
  368. {
  369. /* EMV default values */
  370. cardParams->Fi = 372u;
  371. cardParams->Di = 1u;
  372. cardParams->currentD = 1u;
  373. cardParams->WI = 0x0Au;
  374. cardParams->GTN = 0x00u;
  375. }
  376. status_t SMARTCARD_EMVSIM_Init(EMVSIM_Type *base, smartcard_context_t *context, uint32_t srcClock_Hz)
  377. {
  378. assert((NULL != base));
  379. if ((NULL == context) || (srcClock_Hz == 0u))
  380. {
  381. return kStatus_SMARTCARD_InvalidInput;
  382. }
  383. uint32_t instance = smartcard_emvsim_GetInstance(base);
  384. /* Set source clock for EMVSIM MCGPLLCLK */
  385. #if (defined(FSL_FEATURE_SOC_MCG_COUNT) && FSL_FEATURE_SOC_MCG_COUNT)
  386. CLOCK_SetEmvsimClock(1u);
  387. #endif
  388. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  389. /* Enable emvsim clock */
  390. CLOCK_EnableClock(s_emvsimClock[instance]);
  391. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  392. context->base = base;
  393. /* Initialize EMVSIM to a known context. */
  394. base->CLKCFG = 0u;
  395. base->DIVISOR = 372u;
  396. base->CTRL = 0x300u;
  397. base->INT_MASK = 0x7FFFu;
  398. base->RX_THD = 1u;
  399. base->TX_THD = 0u;
  400. base->PCSR = 0x1000000u;
  401. base->TX_GETU = 0u;
  402. base->CWT_VAL = 0xFFFFu;
  403. base->BWT_VAL = 0xFFFFFFFFu;
  404. base->BGT_VAL = 0u;
  405. base->GPCNT0_VAL = 0xFFFFu;
  406. base->GPCNT1_VAL = 0xFFFFu;
  407. /* Initialize EMVSIM module for SMARTCARD mode of default operation */
  408. smartcard_emvsim_SetTransferType(base, context, kSMARTCARD_SetupATRMode);
  409. /* Store information about tx fifo depth */
  410. context->txFifoEntryCount = (base->PARAM & EMVSIM_PARAM_TX_FIFO_DEPTH_MASK) >> EMVSIM_PARAM_TX_FIFO_DEPTH_SHIFT;
  411. /* Compute max value of rx fifo threshold */
  412. context->rxFifoThreshold = (base->PARAM & EMVSIM_PARAM_RX_FIFO_DEPTH_MASK) >> EMVSIM_PARAM_RX_FIFO_DEPTH_SHIFT;
  413. if ((EMVSIM_RX_THD_RDT_MASK >> EMVSIM_RX_THD_RDT_SHIFT) < context->rxFifoThreshold)
  414. {
  415. context->rxFifoThreshold = (EMVSIM_RX_THD_RDT_MASK >> EMVSIM_RX_THD_RDT_SHIFT);
  416. }
  417. /* Enable EMVSIM interrupt on NVIC level. */
  418. #if defined(FSL_FEATURE_SOC_INTMUX_COUNT) && FSL_FEATURE_SOC_INTMUX_COUNT
  419. if (s_emvsimIRQ[instance] < FSL_FEATURE_INTMUX_IRQ_START_INDEX)
  420. {
  421. NVIC_EnableIRQ(s_emvsimIRQ[instance]);
  422. }
  423. #else
  424. NVIC_EnableIRQ(s_emvsimIRQ[instance]);
  425. #endif
  426. /* Finally, disable the EMVSIM receiver and transmitter */
  427. base->CTRL &= ~EMVSIM_CTRL_XMT_EN_MASK & ~EMVSIM_CTRL_RCV_EN_MASK;
  428. return kStatus_SMARTCARD_Success;
  429. }
  430. void SMARTCARD_EMVSIM_Deinit(EMVSIM_Type *base)
  431. {
  432. uint32_t instance = 0u;
  433. /* In case there is still data in the TX FIFO or shift register that is
  434. * being transmitted wait till transmit is complete.
  435. * Wait until the data is completely shifted out of shift register */
  436. if (base->TX_STATUS & EMVSIM_TX_STATUS_TX_CNT_MASK)
  437. {
  438. while ((!(base->TX_STATUS & EMVSIM_TX_STATUS_ETCF_MASK)))
  439. {
  440. }
  441. }
  442. instance = smartcard_emvsim_GetInstance(base);
  443. /* Disable TX and RX */
  444. base->CTRL &= ~EMVSIM_CTRL_XMT_EN_MASK & ~EMVSIM_CTRL_RCV_EN_MASK;
  445. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  446. /* Gate EMVSIM module clock */
  447. CLOCK_DisableClock(s_emvsimClock[instance]);
  448. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  449. /* Disable emvsim interrupt in NVIC */
  450. #if defined(FSL_FEATURE_SOC_INTMUX_COUNT) && FSL_FEATURE_SOC_INTMUX_COUNT
  451. if (s_emvsimIRQ[instance] < FSL_FEATURE_INTMUX_IRQ_START_INDEX)
  452. {
  453. NVIC_DisableIRQ(s_emvsimIRQ[instance]);
  454. }
  455. #else
  456. NVIC_DisableIRQ(s_emvsimIRQ[instance]);
  457. #endif
  458. }
  459. status_t SMARTCARD_EMVSIM_TransferNonBlocking(EMVSIM_Type *base, smartcard_context_t *context, smartcard_xfer_t *xfer)
  460. {
  461. if ((NULL == context) || (NULL == xfer) || (xfer->buff == NULL))
  462. {
  463. return kStatus_SMARTCARD_InvalidInput;
  464. }
  465. /* Check input parameters */
  466. if ((0u == xfer->size))
  467. {
  468. return kStatus_SMARTCARD_Success;
  469. }
  470. /* Check if some transfer is in progress */
  471. if (0u != SMARTCARD_EMVSIM_GetTransferRemainingBytes(base, context))
  472. {
  473. if (kSMARTCARD_Receive == context->direction)
  474. {
  475. return kStatus_SMARTCARD_RxBusy;
  476. }
  477. else
  478. {
  479. return kStatus_SMARTCARD_TxBusy;
  480. }
  481. }
  482. /* Initialize error check flags */
  483. context->rxtCrossed = false;
  484. context->txtCrossed = false;
  485. context->parityError = false;
  486. /* Initialize SMARTCARD context structure to start transfer */
  487. context->xBuff = xfer->buff;
  488. context->xSize = xfer->size;
  489. if (kSMARTCARD_Receive == xfer->direction)
  490. {
  491. context->direction = xfer->direction;
  492. context->transferState = kSMARTCARD_ReceivingState;
  493. /* Start transfer */
  494. smartcard_emvsim_StartReceiveData(base, context);
  495. }
  496. else if (kSMARTCARD_Transmit == xfer->direction)
  497. {
  498. context->direction = xfer->direction;
  499. context->transferState = kSMARTCARD_TransmittingState;
  500. /* Start transfer */
  501. smartcard_emvsim_StartSendData(base, context);
  502. }
  503. else
  504. {
  505. return kStatus_SMARTCARD_InvalidInput;
  506. }
  507. return kStatus_SMARTCARD_Success;
  508. }
  509. int32_t SMARTCARD_EMVSIM_GetTransferRemainingBytes(EMVSIM_Type *base, smartcard_context_t *context)
  510. {
  511. if ((NULL == context))
  512. {
  513. return -1;
  514. }
  515. if (context->xIsBusy)
  516. {
  517. if (context->direction == kSMARTCARD_Transmit)
  518. {
  519. /* Count of bytes in buffer + data in fifo */
  520. return (context->xSize +
  521. ((base->TX_STATUS & EMVSIM_TX_STATUS_TX_CNT_MASK) >> EMVSIM_TX_STATUS_TX_CNT_SHIFT));
  522. }
  523. return context->xSize;
  524. }
  525. return 0;
  526. }
  527. status_t SMARTCARD_EMVSIM_AbortTransfer(EMVSIM_Type *base, smartcard_context_t *context)
  528. {
  529. if ((NULL == context))
  530. {
  531. return kStatus_SMARTCARD_InvalidInput;
  532. }
  533. context->abortTransfer = true;
  534. /* Check if a transfer is running. */
  535. if ((!context->xIsBusy))
  536. {
  537. return kStatus_SMARTCARD_NoTransferInProgress;
  538. }
  539. /* Call transfer complete to abort transfer */
  540. if (kSMARTCARD_Receive == context->direction)
  541. { /* Stop the running transfer. */
  542. smartcard_emvsim_CompleteReceiveData(base, context);
  543. }
  544. else if (kSMARTCARD_Transmit == context->direction)
  545. { /* Stop the running transfer. */
  546. smartcard_emvsim_CompleteSendData(base, context);
  547. }
  548. else
  549. {
  550. return kStatus_SMARTCARD_InvalidInput;
  551. }
  552. return kStatus_SMARTCARD_Success;
  553. }
  554. void SMARTCARD_EMVSIM_IRQHandler(EMVSIM_Type *base, smartcard_context_t *context)
  555. {
  556. if (NULL == context)
  557. {
  558. return;
  559. }
  560. /* Check card insertion/removal interrupt occurs, only EMVSIM DIRECT interface driver using enables this interrupt
  561. * to occur */
  562. if ((!(base->PCSR & EMVSIM_PCSR_SPDIM_MASK)) && (base->PCSR & EMVSIM_PCSR_SPDIF_MASK))
  563. {
  564. /* Clear card presence interrupt status */
  565. base->PCSR |= EMVSIM_PCSR_SPDIF_MASK;
  566. /* Set PD signal edge behaviour */
  567. if (((emvsim_presence_detect_edge_t)((base->PCSR & EMVSIM_PCSR_SPDES_MASK) >> EMVSIM_PCSR_SPDES_SHIFT) ==
  568. kEMVSIM_DetectOnFallingEdge) &&
  569. ((emvsim_presence_detect_status_t)((base->PCSR & EMVSIM_PCSR_SPDP_MASK) >> EMVSIM_PCSR_SPDP_SHIFT) ==
  570. kEMVSIM_DetectPinIsLow))
  571. { /* Set rising edge interrupt */
  572. base->PCSR |= EMVSIM_PCSR_SPDES_MASK;
  573. }
  574. if (((emvsim_presence_detect_edge_t)((base->PCSR & EMVSIM_PCSR_SPDES_MASK) >> EMVSIM_PCSR_SPDES_SHIFT) ==
  575. kEMVSIM_DetectOnRisingEdge) &&
  576. ((emvsim_presence_detect_status_t)((base->PCSR & EMVSIM_PCSR_SPDP_MASK) >> EMVSIM_PCSR_SPDP_SHIFT) ==
  577. kEMVSIM_DetectPinIsHigh))
  578. { /* Set falling edge interrupt */
  579. base->PCSR &= ~EMVSIM_PCSR_SPDES_MASK;
  580. }
  581. /* Card presence(insertion)/removal detected */
  582. /* Invoke callback if there is one */
  583. if (NULL != context->interfaceCallback)
  584. {
  585. context->interfaceCallback(context, context->interfaceCallbackParam);
  586. }
  587. return;
  588. }
  589. /* Check if timer for initial character (TS) detection has expired */
  590. if (((base->INT_MASK & EMVSIM_INT_MASK_GPCNT0_IM_MASK) >> EMVSIM_INT_MASK_GPCNT0_IM_SHIFT == 0) &&
  591. (base->TX_STATUS & EMVSIM_TX_STATUS_GPCNT0_TO_MASK))
  592. {
  593. /* Disable TS and ADT timers by clearing source clock to 0 */
  594. base->CLKCFG &= ~(EMVSIM_CLKCFG_GPCNT0_CLK_SEL_MASK | EMVSIM_CLKCFG_GPCNT1_CLK_SEL_MASK);
  595. context->timersState.initCharTimerExpired = true;
  596. /* Disable and clear GPCNT interrupt */
  597. base->INT_MASK |= EMVSIM_INT_MASK_GPCNT0_IM_MASK;
  598. base->TX_STATUS = EMVSIM_TX_STATUS_GPCNT0_TO_MASK;
  599. /* Down counter trigger, and clear any pending counter status flag */
  600. base->CTRL &= ~EMVSIM_CTRL_RCV_EN_MASK;
  601. base->CTRL |= EMVSIM_CTRL_RCV_EN_MASK;
  602. context->transferState = kSMARTCARD_IdleState;
  603. /* Unblock the caller */
  604. smartcard_emvsim_CompleteReceiveData(base, context);
  605. return;
  606. }
  607. /* Check if timer for ATR duration timer has expired */
  608. if ((!(base->INT_MASK & EMVSIM_INT_MASK_GPCNT1_IM_MASK)) && (base->TX_STATUS & EMVSIM_TX_STATUS_GPCNT1_TO_MASK))
  609. { /* Disable clock counter by clearing source clock to 0 */
  610. base->CLKCFG &= ~EMVSIM_CLKCFG_GPCNT1_CLK_SEL_MASK;
  611. /* Disable and clear GPCNT interrupt */
  612. base->INT_MASK |= EMVSIM_INT_MASK_GPCNT1_IM_MASK;
  613. base->TX_STATUS = EMVSIM_TX_STATUS_GPCNT1_TO_MASK;
  614. context->timersState.adtExpired = true;
  615. /* Unblock the caller */
  616. smartcard_emvsim_CompleteReceiveData(base, context);
  617. return;
  618. }
  619. /*
  620. * Check if a parity error was indicated.
  621. * A parity error will cause transmission of NACK if ANACK bit is set in
  622. * CTRL register and PEF bit will not be asserted. When ANACK is not set,
  623. * PEF will be asserted.
  624. */
  625. if ((base->RX_STATUS & EMVSIM_RX_STATUS_PEF_MASK))
  626. {
  627. context->parityError = true;
  628. /* Clear parity error indication */
  629. base->RX_STATUS = EMVSIM_RX_STATUS_PEF_MASK;
  630. }
  631. /* Check if transmit NACK generation threshold was reached */
  632. if ((base->TX_STATUS & EMVSIM_TX_STATUS_TNTE_MASK))
  633. {
  634. context->txtCrossed = true;
  635. /* Disable transmit NACK threshold interrupt */
  636. base->INT_MASK |= EMVSIM_INT_MASK_TNACK_IM_MASK;
  637. /* Clear transmit NACK threshold error flag */
  638. base->TX_STATUS = EMVSIM_TX_STATUS_TNTE_MASK;
  639. /* Unblock the caller */
  640. smartcard_emvsim_CompleteSendData(base, context);
  641. return;
  642. }
  643. /* Check if receive NACK generation threshold was reached */
  644. if (base->RX_STATUS & EMVSIM_RX_STATUS_RTE_MASK)
  645. {
  646. context->rxtCrossed = true;
  647. /* Clear receiver NACK threshold interrupt status */
  648. base->RX_STATUS = EMVSIM_RX_STATUS_RTE_MASK;
  649. if (context->xIsBusy)
  650. { /* Unblock the caller */
  651. smartcard_emvsim_CompleteReceiveData(base, context);
  652. }
  653. }
  654. /* Check if a Character Wait Timer expired */
  655. if ((!(base->INT_MASK & EMVSIM_INT_MASK_CWT_ERR_IM_MASK)) && (base->RX_STATUS & EMVSIM_RX_STATUS_CWT_ERR_MASK))
  656. { /* Disable Character Wait Timer interrupt */
  657. base->INT_MASK |= EMVSIM_INT_MASK_CWT_ERR_IM_MASK;
  658. /* Reset the counter */
  659. base->CTRL &= ~EMVSIM_CTRL_CWT_EN_MASK;
  660. /* Clear interrupt status */
  661. base->RX_STATUS = EMVSIM_RX_STATUS_CWT_ERR_MASK;
  662. /* Enable CWT timer */
  663. base->CTRL |= EMVSIM_CTRL_CWT_EN_MASK;
  664. context->transferState = kSMARTCARD_IdleState;
  665. if (kSMARTCARD_T0Transport == context->tType)
  666. { /* Indicate WWT expired */
  667. context->timersState.wwtExpired = true;
  668. }
  669. else
  670. { /* Indicate CWT expired */
  671. context->timersState.cwtExpired = true;
  672. }
  673. if (context->xIsBusy)
  674. { /* Terminate and unblock any caller */
  675. smartcard_emvsim_CompleteReceiveData(base, context);
  676. }
  677. }
  678. /* Check if a Block Wait Timer expired */
  679. if ((!(base->INT_MASK & EMVSIM_INT_MASK_BWT_ERR_IM_MASK)) && (base->RX_STATUS & EMVSIM_RX_STATUS_BWT_ERR_MASK))
  680. { /* Disable Block Wait Timer interrupt */
  681. base->INT_MASK |= EMVSIM_INT_MASK_BWT_ERR_IM_MASK;
  682. /* Clear interrupt status flag */
  683. base->CTRL &= ~EMVSIM_CTRL_BWT_EN_MASK;
  684. /* Clear error */
  685. base->RX_STATUS = EMVSIM_RX_STATUS_BWT_ERR_MASK;
  686. /* Enable BWT timer */
  687. base->CTRL |= EMVSIM_CTRL_BWT_EN_MASK;
  688. if (kSMARTCARD_T0Transport == context->tType)
  689. { /* Indicate WWT expired */
  690. context->timersState.wwtExpired = true;
  691. }
  692. else
  693. { /* Indicate BWT expired */
  694. context->timersState.bwtExpired = true;
  695. }
  696. /* Check if Wait Time Extension(WTX) was requested */
  697. if (context->wtxRequested)
  698. { /* Reset WTX to default */
  699. SMARTCARD_EMVSIM_Control(base, context, kSMARTCARD_ResetWaitTimeMultiplier, 1);
  700. }
  701. if (context->xIsBusy)
  702. { /* Terminate and unblock any caller */
  703. smartcard_emvsim_CompleteReceiveData(base, context);
  704. }
  705. }
  706. /* RX_DATA IRQ */
  707. /* Used in T=1 after receive 1st byte - disable BWT and enable CWT interrupt */
  708. if ((!(base->INT_MASK & EMVSIM_INT_MASK_RX_DATA_IM_MASK)) && (base->RX_STATUS & EMVSIM_RX_STATUS_RX_DATA_MASK))
  709. {
  710. if ((context->tType == kSMARTCARD_T1Transport) && (context->xSize > 0u) &&
  711. (!(base->INT_MASK & EMVSIM_INT_MASK_BWT_ERR_IM_MASK)))
  712. {
  713. context->timersState.cwtExpired = false;
  714. /* Clear CWT error flag */
  715. base->RX_STATUS = EMVSIM_RX_STATUS_CWT_ERR_MASK;
  716. /* Enable CWT */
  717. base->CTRL |= EMVSIM_CTRL_CWT_EN_MASK;
  718. /* Only the 1st byte has been received, now time to disable BWT interrupt and enable CWT interrupt */
  719. base->INT_MASK = (base->INT_MASK & ~EMVSIM_INT_MASK_CWT_ERR_IM_MASK) | EMVSIM_INT_MASK_BWT_ERR_IM_MASK;
  720. }
  721. /* Disable interrupt when is received new byte */
  722. base->INT_MASK |= EMVSIM_INT_MASK_RX_DATA_IM_MASK;
  723. }
  724. /* RDT IRQ - count of bytes in rx fifo reached the rx threshold value RX_THD[RDT] */
  725. if ((!(base->INT_MASK & EMVSIM_INT_MASK_RDT_IM_MASK)) && (base->RX_STATUS & EMVSIM_RX_STATUS_RDTF_MASK))
  726. {
  727. if (kSMARTCARD_WaitingForTSState == context->transferState)
  728. {
  729. /* Read byte */
  730. (void)(base->RX_BUF);
  731. if (base->CTRL & EMVSIM_CTRL_ICM_MASK)
  732. { /* ICM mode still enabled, this is due to parity error */
  733. context->transferState = kSMARTCARD_InvalidTSDetecetedState;
  734. }
  735. else
  736. { /* Received valid TS */
  737. context->transferState = kSMARTCARD_ReceivingState;
  738. /* Get Data Convention form by reading IC bit of EMVSIM_CTRL register */
  739. context->cardParams.convention =
  740. (smartcard_card_convention_t)((base->CTRL & EMVSIM_CTRL_IC_MASK) >> EMVSIM_CTRL_IC_SHIFT);
  741. }
  742. if (kSMARTCARD_InvalidTSDetecetedState == context->transferState)
  743. { /* Stop initial character (TS) detection timer, ADT timer and it's interrupt to occur */
  744. base->CLKCFG &= ~(EMVSIM_CLKCFG_GPCNT0_CLK_SEL_MASK | EMVSIM_CLKCFG_GPCNT1_CLK_SEL_MASK);
  745. base->INT_MASK |= EMVSIM_INT_MASK_GPCNT0_IM_MASK;
  746. smartcard_emvsim_CompleteReceiveData(base, context);
  747. }
  748. if (kSMARTCARD_ReceivingState == context->transferState)
  749. { /* Stop initial character (TS) detection timer and disable ATR duration timer to reset it */
  750. base->CLKCFG &= ~(EMVSIM_CLKCFG_GPCNT0_CLK_SEL_MASK | EMVSIM_CLKCFG_GPCNT1_CLK_SEL_MASK);
  751. /* Start ATR duration counter (restart GPCNT) */
  752. base->CLKCFG |= EMVSIM_CLKCFG_GPCNT1_CLK_SEL(kEMVSIM_GPCTxClock);
  753. /* Start ATR duration counter, Disable counter 0 interrupt and Enable counter 1 interrupt */
  754. base->INT_MASK = (base->INT_MASK & ~EMVSIM_INT_MASK_GPCNT1_IM_MASK) | EMVSIM_INT_MASK_GPCNT0_IM_MASK;
  755. /* Complete receive transfer */
  756. smartcard_emvsim_CompleteReceiveData(base, context);
  757. }
  758. /* Return anyway */
  759. return;
  760. }
  761. while ((base->RX_STATUS & EMVSIM_RX_STATUS_RX_CNT_MASK) && (context->xSize))
  762. {
  763. /* Get data and put into receive buffer */
  764. *context->xBuff = (uint8_t)(base->RX_BUF);
  765. ++context->xBuff;
  766. --context->xSize;
  767. }
  768. /* Check if the last byte was received */
  769. if (context->xSize == 0)
  770. {
  771. smartcard_emvsim_CompleteReceiveData(base, context);
  772. }
  773. else
  774. {
  775. /* If the count of remaining bytes to receive is less than depth of fifo, update the value of the receiver
  776. * data threshold */
  777. if (context->xSize < context->rxFifoThreshold)
  778. {
  779. /* Set receiver data threshold value to count of remaining bytes */
  780. base->RX_THD = ((base->RX_THD & ~EMVSIM_RX_THD_RDT_MASK) | context->xSize);
  781. }
  782. }
  783. }
  784. /* ETC IRQ - all data from fifo is transmitted */
  785. if ((!(base->INT_MASK & EMVSIM_INT_MASK_ETC_IM_MASK)) && (base->TX_STATUS & EMVSIM_TX_STATUS_ETCF_MASK))
  786. {
  787. smartcard_emvsim_CompleteSendData(base, context);
  788. }
  789. /* TDT IRQ - tx fifo is empty */
  790. if ((!(base->INT_MASK & EMVSIM_INT_MASK_TDT_IM_MASK)) && (base->TX_STATUS & EMVSIM_TX_STATUS_TDTF_MASK))
  791. {
  792. if (context->xSize == 0)
  793. {
  794. smartcard_emvsim_CompleteSendData(base, context);
  795. }
  796. if (context->xSize == 1)
  797. {
  798. /* Disable TDT interrupt */
  799. base->INT_MASK |= EMVSIM_INT_MASK_TDT_IM_MASK;
  800. /* When the TX_GETU is not zero while sending last byte, the transmitter sends one byte more */
  801. base->TX_GETU = 0;
  802. /* Write data to fifo */
  803. base->TX_BUF = *(context->xBuff);
  804. ++context->xBuff;
  805. --context->xSize;
  806. /* Last byte was written to fifo - wait for ETC interrupt */
  807. /* Clear ETC flag and enable ETC interrupt */
  808. base->TX_STATUS |= EMVSIM_TX_STATUS_ETCF_MASK;
  809. base->INT_MASK &= ~EMVSIM_INT_MASK_ETC_IM_MASK;
  810. }
  811. else
  812. {
  813. /* To fifo will be written 2 or more bytes */
  814. int getu_tail = (base->TX_GETU > 0);
  815. while ((context->txFifoEntryCount -
  816. ((base->TX_STATUS & EMVSIM_TX_STATUS_TX_CNT_MASK) >> EMVSIM_TX_STATUS_TX_CNT_SHIFT)) &&
  817. (context->xSize > getu_tail))
  818. {
  819. /* Write data to fifo */
  820. base->TX_BUF = *(context->xBuff);
  821. ++context->xBuff;
  822. --context->xSize;
  823. }
  824. if (context->xSize == 0)
  825. {
  826. /* Disable TDT interrupt */
  827. base->INT_MASK |= EMVSIM_INT_MASK_TDT_IM_MASK;
  828. /* Clear ETC flag and enable ETC interrupt */
  829. base->TX_STATUS |= EMVSIM_TX_STATUS_ETCF_MASK;
  830. base->INT_MASK &= ~EMVSIM_INT_MASK_ETC_IM_MASK;
  831. }
  832. }
  833. }
  834. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  835. exception return operation might vector to incorrect interrupt */
  836. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  837. __DSB();
  838. #endif
  839. }
  840. status_t SMARTCARD_EMVSIM_Control(EMVSIM_Type *base,
  841. smartcard_context_t *context,
  842. smartcard_control_t control,
  843. uint32_t param)
  844. {
  845. if ((NULL == context))
  846. {
  847. return kStatus_SMARTCARD_InvalidInput;
  848. }
  849. uint32_t temp32 = 0u;
  850. switch (control)
  851. {
  852. case kSMARTCARD_EnableADT:
  853. /* Do nothing, ADT counter has been loaded and started after reset
  854. * and during starting TS delay counter only. This is because, once
  855. * TS counter has been triggered with RCV_EN down-up, we should not
  856. * trigger again after TS is received(to avoid missing next character to
  857. * TS. Rather, after TS is received, the ATR duration counter should just
  858. * be restarted w/o re-triggering the counter. */
  859. break;
  860. case kSMARTCARD_DisableADT:
  861. base->CTRL &= ~EMVSIM_CTRL_RCV_EN_MASK;
  862. /* Stop ADT specific counter and it's interrupt to occur */
  863. base->CLKCFG &= ~EMVSIM_CLKCFG_GPCNT1_CLK_SEL_MASK;
  864. base->TX_STATUS = EMVSIM_TX_STATUS_GPCNT1_TO_MASK;
  865. base->INT_MASK |= EMVSIM_INT_MASK_GPCNT1_IM_MASK;
  866. break;
  867. case kSMARTCARD_EnableGTV:
  868. /* Enable GTV specific interrupt */
  869. base->INT_MASK &= ~EMVSIM_INT_MASK_BGT_ERR_IM_MASK;
  870. break;
  871. case kSMARTCARD_DisableGTV:
  872. /* Disable GTV specific interrupt */
  873. base->INT_MASK |= EMVSIM_INT_MASK_BGT_ERR_IM_MASK;
  874. break;
  875. case kSMARTCARD_ResetWWT:
  876. /* Reset WWT Timer */
  877. base->CTRL &= ~(EMVSIM_CTRL_CWT_EN_MASK | EMVSIM_CTRL_BWT_EN_MASK);
  878. base->CTRL |= (EMVSIM_CTRL_CWT_EN_MASK | EMVSIM_CTRL_BWT_EN_MASK);
  879. break;
  880. case kSMARTCARD_EnableWWT:
  881. /* BGT must be masked */
  882. base->INT_MASK |= EMVSIM_INT_MASK_BGT_ERR_IM_MASK;
  883. /* Enable WWT Timer interrupt to occur */
  884. base->INT_MASK &= (~EMVSIM_INT_MASK_CWT_ERR_IM_MASK & ~EMVSIM_INT_MASK_BWT_ERR_IM_MASK);
  885. break;
  886. case kSMARTCARD_DisableWWT:
  887. /* Disable WWT Timer interrupt to occur */
  888. base->INT_MASK |= (EMVSIM_INT_MASK_CWT_ERR_IM_MASK | EMVSIM_INT_MASK_BWT_ERR_IM_MASK);
  889. break;
  890. case kSMARTCARD_ResetCWT:
  891. /* Reset CWT Timer */
  892. base->CTRL &= ~EMVSIM_CTRL_CWT_EN_MASK;
  893. base->CTRL |= EMVSIM_CTRL_CWT_EN_MASK;
  894. break;
  895. case kSMARTCARD_EnableCWT:
  896. base->CTRL |= EMVSIM_CTRL_CWT_EN_MASK;
  897. /* Enable CWT Timer interrupt to occur */
  898. base->INT_MASK &= ~EMVSIM_INT_MASK_CWT_ERR_IM_MASK;
  899. break;
  900. case kSMARTCARD_DisableCWT:
  901. /* CWT counter is for receive mode only */
  902. base->CTRL &= ~EMVSIM_CTRL_CWT_EN_MASK;
  903. /* Disable CWT Timer interrupt to occur */
  904. base->INT_MASK |= EMVSIM_INT_MASK_CWT_ERR_IM_MASK;
  905. break;
  906. case kSMARTCARD_ResetBWT:
  907. /* Reset BWT Timer */
  908. base->CTRL &= ~EMVSIM_CTRL_BWT_EN_MASK;
  909. base->CTRL |= EMVSIM_CTRL_BWT_EN_MASK;
  910. break;
  911. case kSMARTCARD_EnableBWT:
  912. base->CTRL |= EMVSIM_CTRL_BWT_EN_MASK;
  913. /* Enable BWT Timer interrupt to occur */
  914. base->INT_MASK &= ~EMVSIM_INT_MASK_BWT_ERR_IM_MASK;
  915. break;
  916. case kSMARTCARD_DisableBWT:
  917. /* Disable BWT Timer interrupt to occur */
  918. base->INT_MASK |= EMVSIM_INT_MASK_BWT_ERR_IM_MASK;
  919. break;
  920. case kSMARTCARD_EnableInitDetect:
  921. /* Clear all ISO7816 interrupt flags */
  922. base->RX_STATUS = 0xFFFFFFFFu;
  923. /* Enable initial character detection : hardware method */
  924. context->transferState = kSMARTCARD_WaitingForTSState;
  925. /* Enable initial character detection */
  926. base->CTRL |= EMVSIM_CTRL_ICM_MASK;
  927. base->CTRL |= EMVSIM_CTRL_RCV_EN_MASK;
  928. break;
  929. case kSMARTCARD_EnableAnack:
  930. /* Enable NACK-on-error interrupt to occur */
  931. base->CTRL |= EMVSIM_CTRL_ANACK_MASK;
  932. break;
  933. case kSMARTCARD_DisableAnack:
  934. /* Disable NACK-on-error interrupt to occur */
  935. base->CTRL &= ~EMVSIM_CTRL_ANACK_MASK;
  936. break;
  937. case kSMARTCARD_ConfigureBaudrate:
  938. /* Set default baudrate/ETU time based on EMV parameters and card clock */
  939. base->DIVISOR = ((context->cardParams.Fi / context->cardParams.currentD) & 0x1FFu);
  940. break;
  941. case kSMARTCARD_SetupATRMode:
  942. /* Set in default ATR mode */
  943. smartcard_emvsim_SetTransferType(base, context, kSMARTCARD_SetupATRMode);
  944. break;
  945. case kSMARTCARD_SetupT0Mode:
  946. /* Set transport protocol type to T=0 */
  947. smartcard_emvsim_SetTransferType(base, context, kSMARTCARD_SetupT0Mode);
  948. break;
  949. case kSMARTCARD_SetupT1Mode:
  950. /* Set transport protocol type to T=1 */
  951. smartcard_emvsim_SetTransferType(base, context, kSMARTCARD_SetupT1Mode);
  952. break;
  953. case kSMARTCARD_EnableReceiverMode:
  954. /* Enable receiver mode and switch to receive direction */
  955. base->CTRL |= EMVSIM_CTRL_RCV_EN_MASK;
  956. /* Set receiver threshold value to 1 */
  957. base->RX_THD = ((base->RX_THD & ~EMVSIM_RX_THD_RDT_MASK) | 1);
  958. /* Enable RDT interrupt */
  959. base->INT_MASK &= ~EMVSIM_INT_MASK_RDT_IM_MASK;
  960. break;
  961. case kSMARTCARD_DisableReceiverMode:
  962. /* Disable receiver */
  963. base->CTRL &= ~EMVSIM_CTRL_RCV_EN_MASK;
  964. break;
  965. case kSMARTCARD_EnableTransmitterMode:
  966. /* Enable transmitter mode and switch to transmit direction */
  967. base->CTRL |= EMVSIM_CTRL_XMT_EN_MASK;
  968. break;
  969. case kSMARTCARD_DisableTransmitterMode:
  970. /* Disable transmitter */
  971. base->CTRL &= ~EMVSIM_CTRL_XMT_EN_MASK;
  972. break;
  973. case kSMARTCARD_ResetWaitTimeMultiplier:
  974. base->CTRL &= ~EMVSIM_CTRL_BWT_EN_MASK;
  975. /* Reset Wait Timer Multiplier
  976. * EMV Formula : WTX x (11 + ((2^BWI + 1) x 960 x D)) */
  977. temp32 = ((uint8_t)param) *
  978. (11u + (((1 << context->cardParams.BWI) + 1u) * 960u * context->cardParams.currentD));
  979. #ifdef CARDSIM_EXTRADELAY_USED
  980. temp32 += context->cardParams.currentD * 50;
  981. #endif
  982. base->BWT_VAL = temp32;
  983. /* Set flag to SMARTCARD context accordingly */
  984. if (param > 1u)
  985. {
  986. context->wtxRequested = true;
  987. }
  988. else
  989. {
  990. context->wtxRequested = false;
  991. }
  992. base->CTRL |= EMVSIM_CTRL_BWT_EN_MASK;
  993. break;
  994. default:
  995. return kStatus_SMARTCARD_InvalidInput;
  996. }
  997. return kStatus_SMARTCARD_Success;
  998. }