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  }
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 
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 */
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 |
182  while(sc->CTL & SC_CTL_SYNC_Msk)
183  {
184  ;
185  }
186  /* Enable auto convention, and all three smartcard internal timers */
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 |
204  }
205  else
206  {
207  sc->INTEN = (SC_INTEN_RDAIEN_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 
249 {
250  while(sc->ALTCTL & SC_ALTCTL_SYNC_Msk)
251  {
252  ;
253  }
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;
293  }
294  else if(u32TimerNum == 1UL)
295  {
296  while(sc->TMRCTL1 & SC_TMRCTL1_SYNC_Msk)
297  {
298  ;
299  }
300  sc->TMRCTL1 = reg;
302  }
303  else /* timer 2 */
304  {
305  while(sc->TMRCTL2 & SC_TMRCTL2_SYNC_Msk)
306  {
307  ;
308  }
309  sc->TMRCTL2 = reg;
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  {
330  }
331  else if(u32TimerNum == 1UL)
332  {
334  }
335  else /* timer 2 */
336  {
338  }
339 }
340 
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. ***/
#define SC_CTL_BGT_Msk
Definition: sc_reg.h:1532
#define SC_TMRCTL2_SYNC_Msk
Definition: sc_reg.h:1793
#define SC_CTL_TXRTY_Msk
Definition: sc_reg.h:1547
#define CLK
Definition: M480.h:368
Definition: sc_reg.h:26
__IO uint32_t STATUS
Definition: sc_reg.h:1492
#define SC_INTEN_RDAIEN_Msk
Definition: sc_reg.h:1616
void SC_ResetReader(SC_T *sc)
This function reset specified smartcard module to its default state for activate smartcard.
Definition: sc.c:152
#define SC_CTL_TXRTYEN_Msk
Definition: sc_reg.h:1550
__IO uint32_t CTL
Definition: sc_reg.h:1485
#define SC_INTEN_TMR0IEN_Msk
Definition: sc_reg.h:1625
void SC_SetCharGuardTime(SC_T *sc, uint32_t u32CGT)
This function character guard time (CGT) of specified smartcard module.
Definition: sc.c:236
#define SC_INTEN_TERRIEN_Msk
Definition: sc_reg.h:1622
void SC_StopTimer(SC_T *sc, uint32_t u32TimerNum)
This function stop a smartcard timer of specified smartcard module.
Definition: sc.c:321
#define SC_TMRCTL1_SYNC_Msk
Definition: sc_reg.h:1784
#define SC1
Definition: M480.h:441
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
#define SC_PINCTL_PWRINV_Msk
Definition: sc_reg.h:1754
#define SC_INTERFACE_NUM
Definition: sc.h:29
#define SC_ALTCTL_ADACEN_Msk
Definition: sc_reg.h:1589
void SC_SetBlockGuardTime(SC_T *sc, uint32_t u32BGT)
This function block guard time (BGT) of specified smartcard module.
Definition: sc.c:225
#define TRUE
Boolean true, define to use in API parameters or return value.
Definition: M480.h:607
uint32_t SC_GetInterfaceClock(SC_T *sc)
This function gets smartcard clock frequency.
Definition: sc.c:346
void SC_Open(SC_T *sc, uint32_t u32CardDet, uint32_t u32PWR)
This function initialized smartcard module.
Definition: sc.c:113
NuMicro peripheral access layer header file.
#define SC_STATUS_CDPINSTS_Msk
Definition: sc_reg.h:1715
__IO uint32_t TMRCTL1
Definition: sc_reg.h:1495
#define SC_PIN_STATE_IGNORE
Definition: sc.h:32
#define FALSE
Boolean false, define to use in API parameters or return value.
Definition: M480.h:608
#define SC_CTL_TMRSEL_Msk
Definition: sc_reg.h:1535
__IO uint32_t ETUCTL
Definition: sc_reg.h:1489
__IO uint32_t EGT
Definition: sc_reg.h:1487
#define SC_STATUS_CDPINSTS_Pos
Definition: sc_reg.h:1714
void SC_Close(SC_T *sc)
This function disable specified smartcard module.
Definition: sc.c:85
#define SC_ALTCTL_SYNC_Msk
Definition: sc_reg.h:1604
#define SC_ALTCTL_RXRST_Msk
Definition: sc_reg.h:1565
#define SC_CTL_RXRTY_Msk
Definition: sc_reg.h:1541
uint32_t CLK_GetPCLK1Freq(void)
Get PCLK1 frequency.
Definition: clk.c:204
#define SC_TMRCTL0_CNT_Msk
Definition: sc_reg.h:1769
__IO uint32_t TMRCTL0
Definition: sc_reg.h:1494
__IO uint32_t INTEN
Definition: sc_reg.h:1490
uint32_t CLK_GetPCLK0Freq(void)
Get PCLK0 frequency.
Definition: clk.c:164
#define SC_CTL_SCEN_Msk
Definition: sc_reg.h:1514
#define SC_ALTCTL_CNTEN1_Msk
Definition: sc_reg.h:1580
#define SC_TMRCTL0_SYNC_Msk
Definition: sc_reg.h:1775
#define SC_CTL_RXTRGLV_Msk
Definition: sc_reg.h:1529
__IO uint32_t PINCTL
Definition: sc_reg.h:1493
__IO uint32_t TMRCTL2
Definition: sc_reg.h:1496
#define SC_CTL_RXRTYEN_Msk
Definition: sc_reg.h:1544
#define SC_INTEN_TMR2IEN_Msk
Definition: sc_reg.h:1631
void SC_ClearFIFO(SC_T *sc)
This function reset both transmit and receive FIFO of specified smartcard module.
Definition: sc.c:71
#define SC_ALTCTL_TXRST_Msk
Definition: sc_reg.h:1562
#define SC_CTL_CDDBSEL_Msk
Definition: sc_reg.h:1553
uint32_t CLK_GetPLLClockFreq(void)
Get PLL clock frequency.
Definition: clk.c:1188
#define SC_CTL_CDLV_Pos
Definition: sc_reg.h:1555
#define SC_CTL_NSB_Msk
Definition: sc_reg.h:1538
#define SC_INTEN_TMR1IEN_Msk
Definition: sc_reg.h:1628
__IO uint32_t ALTCTL
Definition: sc_reg.h:1486
#define SC_ALTCTL_CNTEN2_Msk
Definition: sc_reg.h:1583
#define CLK_CLKSEL3_SC0SEL_Msk
Definition: clk_reg.h:2580
#define SC_INTEN_BGTIEN_Msk
Definition: sc_reg.h:1634
#define SC_PINCTL_SYNC_Msk
Definition: sc_reg.h:1766
#define SC_INTEN_CDIEN_Msk
Definition: sc_reg.h:1637
#define SC2
Definition: M480.h:442
uint32_t SC_IsCardInserted(SC_T *sc)
This function indicates specified smartcard slot status.
Definition: sc.c:36
#define SC_INTEN_ACERRIEN_Msk
Definition: sc_reg.h:1646
#define __HIRC
Definition: system_M480.h:36
__IO uint32_t RXTOUT
Definition: sc_reg.h:1488
#define SC0
Definition: M480.h:440
#define SC_CTL_SYNC_Msk
Definition: sc_reg.h:1559
#define SC_CTL_AUTOCEN_Msk
Definition: sc_reg.h:1523
void SC_StopAllTimer(SC_T *sc)
This function stop all smartcard timer of specified smartcard module.
Definition: sc.c:248
#define SC_CTL_BGT_Pos
Definition: sc_reg.h:1531
#define __HXT
Definition: system_M480.h:29
#define SC_CTL_CDLV_Msk
Definition: sc_reg.h:1556
#define SC_ALTCTL_CNTEN0_Msk
Definition: sc_reg.h:1577
#define CLK_CLKDIV1_SC0DIV_Msk
Definition: clk_reg.h:2634