| M480 BSP V3.05.001 The Board Support Package for M480 Series

sc.c
Go to the documentation of this file.
1 /**************************************************************************/
9 #include "NuMicro.h"
10
11 /* Below are variables used locally by SC driver and does not want to parse by doxygen unless HIDDEN_SYMBOLS is defined */
13 static uint32_t u32CardStateIgnore[SC_INTERFACE_NUM] = {0UL, 0UL, 0UL};
14
36 uint32_t SC_IsCardInserted(SC_T *sc)
37 {
38 uint32_t ret;
39 /* put conditions into two variable to remove IAR compilation warning */
40 uint32_t cond1 = ((sc->STATUS & SC_STATUS_CDPINSTS_Msk) >> SC_STATUS_CDPINSTS_Pos);
41 uint32_t cond2 = ((sc->CTL & SC_CTL_CDLV_Msk) >> SC_CTL_CDLV_Pos);
42
43 if((sc == SC0) && (u32CardStateIgnore[0] == 1UL))
44 {
45 ret = (uint32_t)TRUE;
46 }
47 else if((sc == SC1) && (u32CardStateIgnore[1] == 1UL))
48 {
49 ret = (uint32_t)TRUE;
50 }
51 else if((sc == SC2) && (u32CardStateIgnore[2] == 1UL))
52 {
53 ret = (uint32_t)TRUE;
54 }
55 else if(cond1 != cond2)
56 {
57 ret = (uint32_t)FALSE;
58 }
59 else
60 {
61 ret = (uint32_t)TRUE;
62 }
63 return ret;
64 }
65
71 void SC_ClearFIFO(SC_T *sc)
72 {
73 while(sc->ALTCTL & SC_ALTCTL_SYNC_Msk)
74 {
75 ;
76 }
77 sc->ALTCTL |= (SC_ALTCTL_TXRST_Msk | SC_ALTCTL_RXRST_Msk);
78 }
79
85 void SC_Close(SC_T *sc)
86 {
87 sc->INTEN = 0UL;
88 while(sc->PINCTL & SC_PINCTL_SYNC_Msk)
89 {
90 ;
91 }
92 sc->PINCTL = 0UL;
93 sc->ALTCTL = 0UL;
94 while(sc->CTL & SC_CTL_SYNC_Msk)
95 {
96 ;
97 }
98 sc->CTL = 0UL;
99 }
100
113 void SC_Open(SC_T *sc, uint32_t u32CardDet, uint32_t u32PWR)
114 {
115 uint32_t u32Reg = 0UL, u32Intf;
116
117 if(sc == SC0)
118 {
119 u32Intf = 0UL;
120 }
121 else if(sc == SC1)
122 {
123 u32Intf = 1UL;
124 }
125 else
126 {
127 u32Intf = 2UL;
128 }
129
130 if(u32CardDet != SC_PIN_STATE_IGNORE)
131 {
132 u32Reg = u32CardDet ? 0UL: SC_CTL_CDLV_Msk;
133 u32CardStateIgnore[u32Intf] = 0UL;
134 }
135 else
136 {
137 u32CardStateIgnore[u32Intf] = 1UL;
138 }
139 sc->PINCTL = u32PWR ? 0UL : SC_PINCTL_PWRINV_Msk;
140 while(sc->CTL & SC_CTL_SYNC_Msk)
141 {
142 ;
143 }
144 sc->CTL = SC_CTL_SCEN_Msk | SC_CTL_TMRSEL_Msk | u32Reg;
145 }
146
152 void SC_ResetReader(SC_T *sc)
153 {
154 uint32_t u32Intf;
155
156 if(sc == SC0)
157 {
158 u32Intf = 0UL;
159 }
160 else if(sc == SC1)
161 {
162 u32Intf = 1UL;
163 }
164 else
165 {
166 u32Intf = 2UL;
167 }
168
169 /* Reset FIFO, enable auto de-activation while card removal */
170 sc->ALTCTL |= (SC_ALTCTL_TXRST_Msk | SC_ALTCTL_RXRST_Msk | SC_ALTCTL_ADACEN_Msk);
171 /* Set Rx trigger level to 1 character, longest card detect debounce period, disable error retry (EMV ATR does not use error retry) */
172 while(sc->CTL & SC_CTL_SYNC_Msk)
173 {
174 ;
175 }
176 sc->CTL &= ~(SC_CTL_RXTRGLV_Msk |
177 SC_CTL_CDDBSEL_Msk |
178 SC_CTL_TXRTY_Msk |
179 SC_CTL_TXRTYEN_Msk |
180 SC_CTL_RXRTY_Msk |
181 SC_CTL_RXRTYEN_Msk);
182 while(sc->CTL & SC_CTL_SYNC_Msk)
183 {
184 ;
185 }
186 /* Enable auto convention, and all three smartcard internal timers */
187 sc->CTL |= SC_CTL_AUTOCEN_Msk | SC_CTL_TMRSEL_Msk;
188 /* Disable Rx timeout */
189 sc->RXTOUT = 0UL;
190 /* 372 clocks per ETU by default */
191 sc->ETUCTL= 371UL;
192
193
194 /* Enable necessary interrupt for smartcard operation */
195 if(u32CardStateIgnore[u32Intf]) /* Do not enable card detect interrupt if card present state ignore */
196 {
197 sc->INTEN = (SC_INTEN_RDAIEN_Msk |
198 SC_INTEN_TERRIEN_Msk |
199 SC_INTEN_TMR0IEN_Msk |
200 SC_INTEN_TMR1IEN_Msk |
201 SC_INTEN_TMR2IEN_Msk |
202 SC_INTEN_BGTIEN_Msk |
203 SC_INTEN_ACERRIEN_Msk);
204 }
205 else
206 {
207 sc->INTEN = (SC_INTEN_RDAIEN_Msk |
208 SC_INTEN_TERRIEN_Msk |
209 SC_INTEN_TMR0IEN_Msk |
210 SC_INTEN_TMR1IEN_Msk |
211 SC_INTEN_TMR2IEN_Msk |
212 SC_INTEN_BGTIEN_Msk |
213 SC_INTEN_CDIEN_Msk |
214 SC_INTEN_ACERRIEN_Msk);
215 }
216 return;
217 }
218
225 void SC_SetBlockGuardTime(SC_T *sc, uint32_t u32BGT)
226 {
227 sc->CTL = (sc->CTL & ~SC_CTL_BGT_Msk) | ((u32BGT - 1UL) << SC_CTL_BGT_Pos);
228 }
229
236 void SC_SetCharGuardTime(SC_T *sc, uint32_t u32CGT)
237 {
238 u32CGT -= sc->CTL & SC_CTL_NSB_Msk ? 11UL: 12UL;
239 sc->EGT = u32CGT;
240 }
241
248 void SC_StopAllTimer(SC_T *sc)
249 {
250 while(sc->ALTCTL & SC_ALTCTL_SYNC_Msk)
251 {
252 ;
253 }
254 sc->ALTCTL &= ~(SC_ALTCTL_CNTEN0_Msk | SC_ALTCTL_CNTEN1_Msk | SC_ALTCTL_CNTEN2_Msk);
255 }
256
278 void SC_StartTimer(SC_T *sc, uint32_t u32TimerNum, uint32_t u32Mode, uint32_t u32ETUCount)
279 {
280 uint32_t reg = u32Mode | (SC_TMRCTL0_CNT_Msk & (u32ETUCount - 1UL));
281 while(sc->ALTCTL & SC_ALTCTL_SYNC_Msk)
282 {
283 ;
284 }
285 if(u32TimerNum == 0UL)
286 {
287 while(sc->TMRCTL0 & SC_TMRCTL0_SYNC_Msk)
288 {
289 ;
290 }
291 sc->TMRCTL0 = reg;
292 sc->ALTCTL |= SC_ALTCTL_CNTEN0_Msk;
293 }
294 else if(u32TimerNum == 1UL)
295 {
296 while(sc->TMRCTL1 & SC_TMRCTL1_SYNC_Msk)
297 {
298 ;
299 }
300 sc->TMRCTL1 = reg;
301 sc->ALTCTL |= SC_ALTCTL_CNTEN1_Msk;
302 }
303 else /* timer 2 */
304 {
305 while(sc->TMRCTL2 & SC_TMRCTL2_SYNC_Msk)
306 {
307 ;
308 }
309 sc->TMRCTL2 = reg;
310 sc->ALTCTL |= SC_ALTCTL_CNTEN2_Msk;
311 }
312 }
313
321 void SC_StopTimer(SC_T *sc, uint32_t u32TimerNum)
322 {
323 while(sc->ALTCTL & SC_ALTCTL_SYNC_Msk)
324 {
325 ;
326 }
327 if(u32TimerNum == 0UL)
328 {
329 sc->ALTCTL &= ~SC_ALTCTL_CNTEN0_Msk;
330 }
331 else if(u32TimerNum == 1UL)
332 {
333 sc->ALTCTL &= ~SC_ALTCTL_CNTEN1_Msk;
334 }
335 else /* timer 2 */
336 {
337 sc->ALTCTL &= ~SC_ALTCTL_CNTEN2_Msk;
338 }
339 }
340
346 uint32_t SC_GetInterfaceClock(SC_T *sc)
347 {
348 uint32_t u32ClkSrc, u32Num, u32Clk;
349
350 if(sc == SC0)
351 {
352 u32Num = 0UL;
353 }
354 else if(sc == SC1)
355 {
356 u32Num = 1UL;
357 }
358 else
359 {
360 u32Num = 2UL;
361 }
362
363 u32ClkSrc = (CLK->CLKSEL3 >> (2UL * u32Num)) & CLK_CLKSEL3_SC0SEL_Msk;
364
365 /* Get smartcard module clock */
366 if(u32ClkSrc == 0UL)
367 {
368 u32Clk = __HXT;
369 }
370 else if(u32ClkSrc == 1UL)
371 {
372 u32Clk = CLK_GetPLLClockFreq();
373 }
374 else if(u32ClkSrc == 2UL)
375 {
376 if(u32Num == 1UL)
377 {
378 u32Clk = CLK_GetPCLK1Freq();
379 }
380 else
381 {
382 u32Clk = CLK_GetPCLK0Freq();
383 }
384 }
385 else
386 {
387 u32Clk = __HIRC;
388 }
389
390 u32Clk /= (((CLK->CLKDIV1 >> (8UL * u32Num)) & CLK_CLKDIV1_SC0DIV_Msk) + 1UL) * 1000UL;
391 return u32Clk;
392 }
393 /* end of group SC_EXPORTED_FUNCTIONS */
395 /* end of group SC_Driver */
397 /* end of group Standard_Driver */
399
400 /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/
SC_CTL_BGT_Msk
#define SC_CTL_BGT_Msk
Definition: sc_reg.h:1532
SC_TMRCTL2_SYNC_Msk
#define SC_TMRCTL2_SYNC_Msk
Definition: sc_reg.h:1793
SC_CTL_TXRTY_Msk
#define SC_CTL_TXRTY_Msk
Definition: sc_reg.h:1547
CLK
#define CLK
Definition: M480.h:368
SC_T
Definition: sc_reg.h:26
SC_T::STATUS
__IO uint32_t STATUS
Definition: sc_reg.h:1492
SC_INTEN_RDAIEN_Msk
#define SC_INTEN_RDAIEN_Msk
Definition: sc_reg.h:1616
SC_ResetReader
void SC_ResetReader(SC_T *sc)
This function reset specified smartcard module to its default state for activate smartcard.
Definition: sc.c:152
SC_CTL_TXRTYEN_Msk
#define SC_CTL_TXRTYEN_Msk
Definition: sc_reg.h:1550
SC_T::CTL
__IO uint32_t CTL
Definition: sc_reg.h:1485
SC_INTEN_TMR0IEN_Msk
#define SC_INTEN_TMR0IEN_Msk
Definition: sc_reg.h:1625
SC_SetCharGuardTime
void SC_SetCharGuardTime(SC_T *sc, uint32_t u32CGT)
This function character guard time (CGT) of specified smartcard module.
Definition: sc.c:236
SC_INTEN_TERRIEN_Msk
#define SC_INTEN_TERRIEN_Msk
Definition: sc_reg.h:1622
SC_StopTimer
void SC_StopTimer(SC_T *sc, uint32_t u32TimerNum)
This function stop a smartcard timer of specified smartcard module.
Definition: sc.c:321
SC_TMRCTL1_SYNC_Msk
#define SC_TMRCTL1_SYNC_Msk
Definition: sc_reg.h:1784
SC1
#define SC1
Definition: M480.h:441
SC_StartTimer
void SC_StartTimer(SC_T *sc, uint32_t u32TimerNum, uint32_t u32Mode, uint32_t u32ETUCount)
This function configure and start a smartcard timer of specified smartcard module.
Definition: sc.c:278
SC_PINCTL_PWRINV_Msk
#define SC_PINCTL_PWRINV_Msk
Definition: sc_reg.h:1754
SC_INTERFACE_NUM
#define SC_INTERFACE_NUM
Definition: sc.h:29
SC_ALTCTL_ADACEN_Msk
#define SC_ALTCTL_ADACEN_Msk
Definition: sc_reg.h:1589
SC_SetBlockGuardTime
void SC_SetBlockGuardTime(SC_T *sc, uint32_t u32BGT)
This function block guard time (BGT) of specified smartcard module.
Definition: sc.c:225
TRUE
#define TRUE
Boolean true, define to use in API parameters or return value.
Definition: M480.h:607
SC_GetInterfaceClock
uint32_t SC_GetInterfaceClock(SC_T *sc)
This function gets smartcard clock frequency.
Definition: sc.c:346
SC_Open
void SC_Open(SC_T *sc, uint32_t u32CardDet, uint32_t u32PWR)
This function initialized smartcard module.
Definition: sc.c:113
NuMicro.h
NuMicro peripheral access layer header file.
SC_STATUS_CDPINSTS_Msk
#define SC_STATUS_CDPINSTS_Msk
Definition: sc_reg.h:1715
SC_T::TMRCTL1
__IO uint32_t TMRCTL1
Definition: sc_reg.h:1495
SC_PIN_STATE_IGNORE
#define SC_PIN_STATE_IGNORE
Definition: sc.h:32
FALSE
#define FALSE
Boolean false, define to use in API parameters or return value.
Definition: M480.h:608
SC_CTL_TMRSEL_Msk
#define SC_CTL_TMRSEL_Msk
Definition: sc_reg.h:1535
SC_T::ETUCTL
__IO uint32_t ETUCTL
Definition: sc_reg.h:1489
SC_T::EGT
__IO uint32_t EGT
Definition: sc_reg.h:1487
SC_STATUS_CDPINSTS_Pos
#define SC_STATUS_CDPINSTS_Pos
Definition: sc_reg.h:1714
SC_Close
void SC_Close(SC_T *sc)
This function disable specified smartcard module.
Definition: sc.c:85
SC_ALTCTL_SYNC_Msk
#define SC_ALTCTL_SYNC_Msk
Definition: sc_reg.h:1604
SC_ALTCTL_RXRST_Msk
#define SC_ALTCTL_RXRST_Msk
Definition: sc_reg.h:1565
SC_CTL_RXRTY_Msk
#define SC_CTL_RXRTY_Msk
Definition: sc_reg.h:1541
CLK_GetPCLK1Freq
uint32_t CLK_GetPCLK1Freq(void)
Get PCLK1 frequency.
Definition: clk.c:204
SC_TMRCTL0_CNT_Msk
#define SC_TMRCTL0_CNT_Msk
Definition: sc_reg.h:1769
SC_T::TMRCTL0
__IO uint32_t TMRCTL0
Definition: sc_reg.h:1494
SC_T::INTEN
__IO uint32_t INTEN
Definition: sc_reg.h:1490
CLK_GetPCLK0Freq
uint32_t CLK_GetPCLK0Freq(void)
Get PCLK0 frequency.
Definition: clk.c:164
SC_CTL_SCEN_Msk
#define SC_CTL_SCEN_Msk
Definition: sc_reg.h:1514
SC_ALTCTL_CNTEN1_Msk
#define SC_ALTCTL_CNTEN1_Msk
Definition: sc_reg.h:1580
SC_TMRCTL0_SYNC_Msk
#define SC_TMRCTL0_SYNC_Msk
Definition: sc_reg.h:1775
SC_CTL_RXTRGLV_Msk
#define SC_CTL_RXTRGLV_Msk
Definition: sc_reg.h:1529
SC_T::PINCTL
__IO uint32_t PINCTL
Definition: sc_reg.h:1493
SC_T::TMRCTL2
__IO uint32_t TMRCTL2
Definition: sc_reg.h:1496
SC_CTL_RXRTYEN_Msk
#define SC_CTL_RXRTYEN_Msk
Definition: sc_reg.h:1544
SC_INTEN_TMR2IEN_Msk
#define SC_INTEN_TMR2IEN_Msk
Definition: sc_reg.h:1631
SC_ClearFIFO
void SC_ClearFIFO(SC_T *sc)
This function reset both transmit and receive FIFO of specified smartcard module.
Definition: sc.c:71
SC_ALTCTL_TXRST_Msk
#define SC_ALTCTL_TXRST_Msk
Definition: sc_reg.h:1562
SC_CTL_CDDBSEL_Msk
#define SC_CTL_CDDBSEL_Msk
Definition: sc_reg.h:1553
CLK_GetPLLClockFreq
uint32_t CLK_GetPLLClockFreq(void)
Get PLL clock frequency.
Definition: clk.c:1188
SC_CTL_CDLV_Pos
#define SC_CTL_CDLV_Pos
Definition: sc_reg.h:1555
SC_CTL_NSB_Msk
#define SC_CTL_NSB_Msk
Definition: sc_reg.h:1538
SC_INTEN_TMR1IEN_Msk
#define SC_INTEN_TMR1IEN_Msk
Definition: sc_reg.h:1628
SC_T::ALTCTL
__IO uint32_t ALTCTL
Definition: sc_reg.h:1486
SC_ALTCTL_CNTEN2_Msk
#define SC_ALTCTL_CNTEN2_Msk
Definition: sc_reg.h:1583
CLK_CLKSEL3_SC0SEL_Msk
#define CLK_CLKSEL3_SC0SEL_Msk
Definition: clk_reg.h:2580
SC_INTEN_BGTIEN_Msk
#define SC_INTEN_BGTIEN_Msk
Definition: sc_reg.h:1634
SC_PINCTL_SYNC_Msk
#define SC_PINCTL_SYNC_Msk
Definition: sc_reg.h:1766
SC_INTEN_CDIEN_Msk
#define SC_INTEN_CDIEN_Msk
Definition: sc_reg.h:1637
SC2
#define SC2
Definition: M480.h:442
SC_IsCardInserted
uint32_t SC_IsCardInserted(SC_T *sc)
This function indicates specified smartcard slot status.
Definition: sc.c:36
SC_INTEN_ACERRIEN_Msk
#define SC_INTEN_ACERRIEN_Msk
Definition: sc_reg.h:1646
__HIRC
#define __HIRC
Definition: system_M480.h:36
SC_T::RXTOUT
__IO uint32_t RXTOUT
Definition: sc_reg.h:1488
SC0
#define SC0
Definition: M480.h:440
SC_CTL_SYNC_Msk
#define SC_CTL_SYNC_Msk
Definition: sc_reg.h:1559
SC_CTL_AUTOCEN_Msk
#define SC_CTL_AUTOCEN_Msk
Definition: sc_reg.h:1523
SC_StopAllTimer
void SC_StopAllTimer(SC_T *sc)
This function stop all smartcard timer of specified smartcard module.
Definition: sc.c:248
SC_CTL_BGT_Pos
#define SC_CTL_BGT_Pos
Definition: sc_reg.h:1531
__HXT
#define __HXT
Definition: system_M480.h:29
SC_CTL_CDLV_Msk
#define SC_CTL_CDLV_Msk
Definition: sc_reg.h:1556
SC_ALTCTL_CNTEN0_Msk
#define SC_ALTCTL_CNTEN0_Msk
Definition: sc_reg.h:1577
CLK_CLKDIV1_SC0DIV_Msk
#define CLK_CLKDIV1_SC0DIV_Msk
Definition: clk_reg.h:2634