1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
|
/**************************************************************************//**
* @file sc.c
* @version V3.00
* @brief Smartcard(SC) driver source file
*
* @copyright SPDX-License-Identifier: Apache-2.0
* @copyright Copyright (C) 2021 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include "NuMicro.h"
/* Below are variables used locally by SC driver and does not want to parse by doxygen unless HIDDEN_SYMBOLS is defined */
/** @cond HIDDEN_SYMBOLS */
static uint32_t g_u32CardStateIgnore[SC_INTERFACE_NUM] = {0UL, 0UL, 0UL};
/** @endcond HIDDEN_SYMBOLS */
/** @addtogroup Standard_Driver Standard Driver
@{
*/
/** @addtogroup SC_Driver SC Driver
@{
*/
/** @addtogroup SC_EXPORTED_FUNCTIONS SC Exported Functions
@{
*/
/**
* @brief Indicates specified smartcard slot status
*
* @param[in] sc The pointer of smartcard module.
*
* @return Card insert status
* @retval TRUE Card insert
* @retval FALSE Card remove
*
* @details This function is used to check if specified smartcard slot is presented.
*/
uint32_t SC_IsCardInserted(SC_T *sc)
{
uint32_t u32Ret;
/* put conditions into two variable to remove IAR compilation warning */
uint32_t cond1 = ((sc->STATUS & SC_STATUS_CDPINSTS_Msk) >> SC_STATUS_CDPINSTS_Pos);
uint32_t cond2 = ((sc->CTL & SC_CTL_CDLV_Msk) >> SC_CTL_CDLV_Pos);
if ((sc == SC0) && (g_u32CardStateIgnore[0] == 1UL))
{
u32Ret = (uint32_t)TRUE;
}
else if ((sc == SC1) && (g_u32CardStateIgnore[1] == 1UL))
{
u32Ret = (uint32_t)TRUE;
}
else if ((sc == SC2) && (g_u32CardStateIgnore[2] == 1UL))
{
u32Ret = (uint32_t)TRUE;
}
else if (cond1 != cond2)
{
u32Ret = (uint32_t)FALSE;
}
else
{
u32Ret = (uint32_t)TRUE;
}
return u32Ret;
}
/*
* @brief Reset the Tx and Rx FIFO of smartcard module
*
* @param[in] sc The pointer of smartcard module.
*
* @return None
*
* @details This function reset both transmit and receive FIFO of specified smartcard module.
*/
void SC_ClearFIFO(SC_T *sc)
{
uint32_t u32TimeOutCount = SC_TIMEOUT;
while ((sc->ALTCTL & SC_ALTCTL_SYNC_Msk) == SC_ALTCTL_SYNC_Msk)
{
if (--u32TimeOutCount == 0) break;
}
sc->ALTCTL |= (SC_ALTCTL_TXRST_Msk | SC_ALTCTL_RXRST_Msk);
}
/**
* @brief Disable specified smartcard module
*
* @param[in] sc The pointer of smartcard module.
*
* @return None
*
* @details This function disable specified smartcard module, and force all transition to IDLE state.
*/
void SC_Close(SC_T *sc)
{
uint32_t u32TimeOutCount;
sc->INTEN = 0UL;
u32TimeOutCount = SC_TIMEOUT;
while ((sc->PINCTL & SC_PINCTL_SYNC_Msk) == SC_PINCTL_SYNC_Msk)
{
if (--u32TimeOutCount == 0UL) break;
}
sc->PINCTL = 0UL;
sc->ALTCTL = 0UL;
u32TimeOutCount = SC_TIMEOUT;
while ((sc->CTL & SC_CTL_SYNC_Msk) == SC_CTL_SYNC_Msk)
{
if (--u32TimeOutCount == 0UL) break;
}
sc->CTL = 0UL;
}
/**
* @brief Initialized smartcard module
*
* @param[in] sc The pointer of smartcard module.
* @param[in] u32CardDet Card detect polarity, select the SC_CD pin state which indicates card absent. Could be:
* -\ref SC_PIN_STATE_HIGH
* -\ref SC_PIN_STATE_LOW
* -\ref SC_PIN_STATE_IGNORE, no card detect pin, always assumes card present.
* @param[in] u32PWR Power off polarity, select the SC_PWR pin state which could set smartcard VCC to high level. Could be:
* -\ref SC_PIN_STATE_HIGH
* -\ref SC_PIN_STATE_LOW
*
* @return None
*
* @details This function initialized smartcard module.
*/
void SC_Open(SC_T *sc, uint32_t u32CardDet, uint32_t u32PWR)
{
uint32_t u32Reg = 0UL, u32Intf, u32TimeOutCount;
if (sc == SC0)
{
u32Intf = 0UL;
}
else if (sc == SC1)
{
u32Intf = 1UL;
}
else
{
u32Intf = 2UL;
}
if (u32CardDet != SC_PIN_STATE_IGNORE)
{
u32Reg = u32CardDet ? 0UL : SC_CTL_CDLV_Msk;
g_u32CardStateIgnore[u32Intf] = 0UL;
}
else
{
g_u32CardStateIgnore[u32Intf] = 1UL;
}
sc->PINCTL = u32PWR ? 0UL : SC_PINCTL_PWRINV_Msk;
u32TimeOutCount = SC_TIMEOUT;
while ((sc->CTL & SC_CTL_SYNC_Msk) == SC_CTL_SYNC_Msk)
{
if (--u32TimeOutCount == 0UL) break;
}
sc->CTL = SC_CTL_SCEN_Msk | SC_CTL_TMRSEL_Msk | u32Reg;
}
/**
* @brief Reset specified smartcard module
*
* @param[in] sc The pointer of smartcard module.
*
* @return None
*
* @details This function reset specified smartcard module to its default state for activate smartcard.
*/
void SC_ResetReader(SC_T *sc)
{
uint32_t u32Intf, u32TimeOutCount;
if (sc == SC0)
{
u32Intf = 0UL;
}
else if (sc == SC1)
{
u32Intf = 1UL;
}
else
{
u32Intf = 2UL;
}
/* Reset FIFO, enable auto de-activation while card removal */
sc->ALTCTL |= (SC_ALTCTL_TXRST_Msk | SC_ALTCTL_RXRST_Msk | SC_ALTCTL_ADACEN_Msk);
/* Set Rx trigger level to 1 character, longest card detect debounce period, disable error retry (EMV ATR does not use error retry) */
u32TimeOutCount = SC_TIMEOUT;
while ((sc->CTL & SC_CTL_SYNC_Msk) == SC_CTL_SYNC_Msk)
{
if (--u32TimeOutCount == 0) break;
}
sc->CTL &= ~(SC_CTL_RXTRGLV_Msk |
SC_CTL_CDDBSEL_Msk |
SC_CTL_TXRTY_Msk |
SC_CTL_TXRTYEN_Msk |
SC_CTL_RXRTY_Msk |
SC_CTL_RXRTYEN_Msk);
u32TimeOutCount = SC_TIMEOUT;
while ((sc->CTL & SC_CTL_SYNC_Msk) == SC_CTL_SYNC_Msk)
{
if (--u32TimeOutCount == 0) break;
}
/* Enable auto convention, and all three smartcard internal timers */
sc->CTL |= SC_CTL_AUTOCEN_Msk | SC_CTL_TMRSEL_Msk;
/* Disable Rx timeout */
sc->RXTOUT = 0UL;
/* 372 clocks per ETU by default */
sc->ETUCTL = 371UL;
/* Enable necessary interrupt for smartcard operation */
if (g_u32CardStateIgnore[u32Intf]) /* Do not enable card detect interrupt if card present state ignore */
{
sc->INTEN = (SC_INTEN_RDAIEN_Msk |
SC_INTEN_TERRIEN_Msk |
SC_INTEN_TMR0IEN_Msk |
SC_INTEN_TMR1IEN_Msk |
SC_INTEN_TMR2IEN_Msk |
SC_INTEN_BGTIEN_Msk |
SC_INTEN_ACERRIEN_Msk);
}
else
{
sc->INTEN = (SC_INTEN_RDAIEN_Msk |
SC_INTEN_TERRIEN_Msk |
SC_INTEN_TMR0IEN_Msk |
SC_INTEN_TMR1IEN_Msk |
SC_INTEN_TMR2IEN_Msk |
SC_INTEN_BGTIEN_Msk |
SC_INTEN_ACERRIEN_Msk |
SC_INTEN_CDIEN_Msk);
}
}
/**
* @brief Set Block Guard Time (BGT)
*
* @param[in] sc The pointer of smartcard module.
* @param[in] u32BGT Block guard time using ETU as unit, valid range are between 1 ~ 32.
*
* @return None
*
* @details This function is used to configure block guard time (BGT) of specified smartcard module.
*/
void SC_SetBlockGuardTime(SC_T *sc, uint32_t u32BGT)
{
sc->CTL = (sc->CTL & ~SC_CTL_BGT_Msk) | ((u32BGT - 1UL) << SC_CTL_BGT_Pos);
}
/**
* @brief Set Character Guard Time (CGT)
*
* @param[in] sc The pointer of smartcard module.
* @param[in] u32CGT Character guard time using ETU as unit, valid range are between 11 ~ 267.
*
* @return None
*
* @details This function is used to configure character guard time (CGT) of specified smartcard module.
* @note Before using this API, user should set the correct stop bit length first.
*/
void SC_SetCharGuardTime(SC_T *sc, uint32_t u32CGT)
{
/* CGT is "START bit" + "8-bits" + "Parity bit" + "STOP bit(s)" + "EGT counts" */
u32CGT -= ((sc->CTL & SC_CTL_NSB_Msk) == SC_CTL_NSB_Msk) ? 11UL : 12UL;
sc->EGT = u32CGT;
}
/**
* @brief Stop all smartcard timer
*
* @param[in] sc The pointer of smartcard module.
*
* @return None
*
* @note This function stop the timers within specified smartcard module, \b not timer module.
*/
void SC_StopAllTimer(SC_T *sc)
{
uint32_t u32TimeOutCount = SC_TIMEOUT;
while ((sc->ALTCTL & SC_ALTCTL_SYNC_Msk) == SC_ALTCTL_SYNC_Msk)
{
if (--u32TimeOutCount == 0) break;
}
sc->ALTCTL &= ~(SC_ALTCTL_CNTEN0_Msk | SC_ALTCTL_CNTEN1_Msk | SC_ALTCTL_CNTEN2_Msk);
}
/**
* @brief Configure and start smartcard timer
*
* @param[in] sc The pointer of smartcard module.
* @param[in] u32TimerNum Timer to start. Valid values are 0, 1, 2.
* @param[in] u32Mode Timer operating mode, valid values are:
* - \ref SC_TMR_MODE_0
* - \ref SC_TMR_MODE_1
* - \ref SC_TMR_MODE_2
* - \ref SC_TMR_MODE_3
* - \ref SC_TMR_MODE_4
* - \ref SC_TMR_MODE_5
* - \ref SC_TMR_MODE_6
* - \ref SC_TMR_MODE_7
* - \ref SC_TMR_MODE_8
* - \ref SC_TMR_MODE_F
* @param[in] u32ETUCount Timer timeout duration, ETU based. For timer 0, valid range are between 1 ~ 0x1000000 ETUs.
* For timer 1 and timer 2, valid range are between 1 ~ 0x100 ETUs.
*
* @return None
*
* @note This function start the timer within specified smartcard module, \b not timer module.
* @note Depend on the timer operating mode, timer may not start counting immediately and starts when condition match.
*/
void SC_StartTimer(SC_T *sc, uint32_t u32TimerNum, uint32_t u32Mode, uint32_t u32ETUCount)
{
uint32_t u32Reg = u32Mode | (SC_TMRCTL0_CNT_Msk & (u32ETUCount - 1UL));
uint32_t u32TimeOutCount = 0UL;
u32TimeOutCount = SC_TIMEOUT;
while ((sc->ALTCTL & SC_ALTCTL_SYNC_Msk) == SC_ALTCTL_SYNC_Msk)
{
if (--u32TimeOutCount == 0UL) break;
}
if (u32TimerNum == 0UL)
{
u32TimeOutCount = SC_TIMEOUT;
while ((sc->TMRCTL0 & SC_TMRCTL0_SYNC_Msk) == SC_TMRCTL0_SYNC_Msk)
{
if (--u32TimeOutCount == 0UL) break;
}
sc->TMRCTL0 = u32Reg;
sc->ALTCTL |= SC_ALTCTL_CNTEN0_Msk;
}
else if (u32TimerNum == 1UL)
{
u32TimeOutCount = SC_TIMEOUT;
while ((sc->TMRCTL1 & SC_TMRCTL1_SYNC_Msk) == SC_TMRCTL1_SYNC_Msk)
{
if (--u32TimeOutCount == 0UL) break;
}
sc->TMRCTL1 = u32Reg;
sc->ALTCTL |= SC_ALTCTL_CNTEN1_Msk;
}
else /* timer 2 */
{
u32TimeOutCount = SC_TIMEOUT;
while ((sc->TMRCTL2 & SC_TMRCTL2_SYNC_Msk) == SC_TMRCTL2_SYNC_Msk)
{
if (--u32TimeOutCount == 0UL) break;
}
sc->TMRCTL2 = u32Reg;
sc->ALTCTL |= SC_ALTCTL_CNTEN2_Msk;
}
}
/**
* @brief Stop a smartcard timer
*
* @param[in] sc The pointer of smartcard module.
* @param[in] u32TimerNum Timer to stop. Valid values are 0, 1, 2.
*
* @return None
*
* @note This function stop the timer within specified smartcard module, \b not timer module.
*/
void SC_StopTimer(SC_T *sc, uint32_t u32TimerNum)
{
uint32_t u32TimeOutCount = SC_TIMEOUT;
while (sc->ALTCTL & SC_ALTCTL_SYNC_Msk)
{
if (--u32TimeOutCount == 0UL) break;
}
if (u32TimerNum == 0UL) /* timer 0 */
{
sc->ALTCTL &= ~SC_ALTCTL_CNTEN0_Msk;
}
else if (u32TimerNum == 1UL) /* timer 1 */
{
sc->ALTCTL &= ~SC_ALTCTL_CNTEN1_Msk;
}
else /* timer 2 */
{
sc->ALTCTL &= ~SC_ALTCTL_CNTEN2_Msk;
}
}
/**
* @brief Get smartcard clock frequency
*
* @param[in] sc The pointer of smartcard module.
*
* @return Smartcard frequency in kHZ
*
* @details This function is used to get specified smartcard module clock frequency in kHz.
*/
uint32_t SC_GetInterfaceClock(SC_T *sc)
{
uint32_t u32ClkSrc = 0, u32Num = 0, u32ClkFreq = __HIRC, u32Div = 0;
/* Get smartcard module clock source and divider */
if (sc == SC0)
{
u32Num = 0UL;
u32ClkSrc = CLK_GetModuleClockSource(SC0_MODULE);
u32Div = CLK_GetModuleClockDivider(SC0_MODULE);
}
else if (sc == SC1)
{
u32Num = 1UL;
u32ClkSrc = CLK_GetModuleClockSource(SC1_MODULE);
u32Div = CLK_GetModuleClockDivider(SC1_MODULE);
}
else if (sc == SC2)
{
u32Num = 2UL;
u32ClkSrc = CLK_GetModuleClockSource(SC2_MODULE);
u32Div = CLK_GetModuleClockDivider(SC2_MODULE);
}
else
{
u32ClkFreq = 0UL;
}
if (u32ClkFreq != 0UL)
{
/* Get smartcard module clock */
if (u32ClkSrc == 0UL)
{
u32ClkFreq = __HXT;
}
else if (u32ClkSrc == 1UL)
{
u32ClkFreq = CLK_GetPLLClockFreq();
}
else if (u32ClkSrc == 2UL)
{
if (u32Num == 1UL)
{
u32ClkFreq = CLK_GetPCLK1Freq();
}
else
{
u32ClkFreq = CLK_GetPCLK0Freq();
}
}
else
{
u32ClkFreq = __HIRC;
}
u32ClkFreq /= (u32Div + 1UL) * 1000UL;
}
return u32ClkFreq;
}
/**@}*/ /* end of group SC_EXPORTED_FUNCTIONS */
/**@}*/ /* end of group SC_Driver */
/**@}*/ /* end of group Standard_Driver */
|