M4521_BSP V3.02.000
The Board Support Package for M4521
sc.c
Go to the documentation of this file.
1/**************************************************************************/
12#include "M4521.h"
13
14// Below are variables used locally by SC driver and does not want to parse by doxygen unless HIDDEN_SYMBOLS is defined
16static uint32_t u32CardStateIgnore[SC_INTERFACE_NUM] = {0};
17
19
28int32_t g_SC_i32ErrCode = 0;
29
42{
43 // put conditions into two variable to remove IAR compilation warning
44 uint32_t cond1 = ((sc->STATUS & SC_STATUS_CDPINSTS_Msk) >> SC_STATUS_CDPINSTS_Pos);
45 uint32_t cond2 = ((sc->CTL & SC_CTL_CDLV_Msk) >> SC_CTL_CDLV_Pos);
46
47 if(sc == SC0 && u32CardStateIgnore[0] == 1)
48 return TRUE;
49#if 0 /* M4521 has only one SC interface */
50 else if(sc == SC1 && u32CardStateIgnore[1] == 1)
51 return TRUE;
52 else if(sc == SC2 && u32CardStateIgnore[2] == 1)
53 return TRUE;
54 else if(sc == SC3 && u32CardStateIgnore[3] == 1)
55 return TRUE;
56 else if(sc == SC4 && u32CardStateIgnore[4] == 1)
57 return TRUE;
58 else if(sc == SC5 && u32CardStateIgnore[5] == 1)
59 return TRUE;
60#endif
61 else if(cond1 != cond2)
62 return FALSE;
63 else
64 return TRUE;
65}
66
74{
76}
77
84void SC_Close(SC_T *sc)
85{
86 sc->INTEN = 0;
87 sc->PINCTL = 0;
88 sc->ALTCTL = 0;
89 sc->CTL = 0;
90}
91
105void SC_Open(SC_T *sc, uint32_t u32CD, uint32_t u32PWR)
106{
107 uint32_t u32Reg = 0, u32Intf;
108 uint32_t u32Timeout;
109
110 if(sc == SC0)
111 u32Intf = 0;
112#if 0 /* M4521 has only one SC interface */
113 else if(sc == SC1)
114 u32Intf = 1;
115 else if(sc == SC2)
116 u32Intf = 2;
117 else if(sc == SC3)
118 u32Intf = 3;
119 else if(sc == SC4)
120 u32Intf = 4;
121 else if(sc == SC5)
122 u32Intf = 5;
123#endif
124 else
125 return ;
126
127 if(u32CD != SC_PIN_STATE_IGNORE)
128 {
129 u32Reg = u32CD ? 0: SC_CTL_CDLV_Msk;
130 u32CardStateIgnore[u32Intf] = 0;
131 }
132 else
133 {
134 u32CardStateIgnore[u32Intf] = 1;
135 }
136
137 g_SC_i32ErrCode = 0;
138 u32Timeout = SystemCoreClock;
139 while ((sc->PINCTL & SC_PINCTL_SYNC_Msk) && (u32Timeout-- > 0));
140 if (sc->PINCTL & SC_PINCTL_SYNC_Msk)
141 g_SC_i32ErrCode = -1;
142 sc->PINCTL = u32PWR ? 0 : SC_PINCTL_PWRINV_Msk;
143
144 u32Timeout = SystemCoreClock;
145 while ((sc->CTL & SC_CTL_SYNC_Msk) && (u32Timeout-- > 0));
146 if (sc->CTL & SC_CTL_SYNC_Msk)
147 g_SC_i32ErrCode = -1;
148 sc->CTL = SC_CTL_SCEN_Msk | u32Reg;
149}
150
158{
159 uint32_t u32Intf;
160 uint32_t u32Timeout;
161
162 if(sc == SC0)
163 u32Intf = 0;
164#if 0 /* M4521 has only one SC interface */
165 else if(sc == SC1)
166 u32Intf = 1;
167 else if(sc == SC2)
168 u32Intf = 2;
169 else if(sc == SC3)
170 u32Intf = 3;
171 else if(sc == SC4)
172 u32Intf = 4;
173 else if(sc == SC5)
174 u32Intf = 5;
175#endif
176 else
177 return ;
178
179 // Reset FIFO, enable auto de-activation while card removal
181 // Set Rx trigger level to 1 character, longest card detect debounce period, disable error retry (EMV ATR does not use error retry)
182 u32Timeout = SystemCoreClock;
183 while ((sc->CTL & SC_CTL_SYNC_Msk) && (u32Timeout-- > 0));
184 if (sc->CTL & SC_CTL_SYNC_Msk)
185 g_SC_i32ErrCode = -1;
186
188 // Enable auto convention, and all three smartcard internal timers
190 // Disable Rx timeout
191 sc->RXTOUT = 0;
192 // 372 clocks per ETU by default
193 sc->ETUCTL = 371;
194
195 /* Enable necessary interrupt for smartcard operation */
196 if(u32CardStateIgnore[u32Intf]) // Do not enable card detect interrupt if card present state ignore
204 else
213
214 return;
215}
216
224void SC_SetBlockGuardTime(SC_T *sc, uint32_t u32BGT)
225{
226 sc->CTL = (sc->CTL & ~SC_CTL_BGT_Msk) | ((u32BGT - 1) << SC_CTL_BGT_Pos);
227}
228
237void SC_SetCharGuardTime(SC_T *sc, uint32_t u32CGT)
238{
239 u32CGT -= sc->CTL & SC_CTL_NSB_Msk ? 11 : 12;
240 sc->EGT = u32CGT;
241}
242
251{
253}
254
277void SC_StartTimer(SC_T *sc, uint32_t u32TimerNum, uint32_t u32Mode, uint32_t u32ETUCount)
278{
279 uint32_t reg = u32Mode | (SC_TMRCTL0_CNT_Msk & (u32ETUCount - 1));
280
281 if(u32TimerNum == 0)
282 {
283 sc->TMRCTL0 = reg;
285 }
286 else if(u32TimerNum == 1)
287 {
288 sc->TMRCTL1 = reg;
290 }
291 else // timer 2
292 {
293 sc->TMRCTL2 = reg;
295 }
296}
297
306void SC_StopTimer(SC_T *sc, uint32_t u32TimerNum)
307{
308 if(u32TimerNum == 0)
309 sc->ALTCTL &= ~SC_ALTCTL_CNTEN0_Msk;
310 else if(u32TimerNum == 1)
311 sc->ALTCTL &= ~SC_ALTCTL_CNTEN1_Msk;
312 else // timer 2
313 sc->ALTCTL &= ~SC_ALTCTL_CNTEN2_Msk;
314}
315
316
317 /* end of group SC_EXPORTED_FUNCTIONS */
319 /* end of group SC_Driver */
321 /* end of group Standard_Driver */
323
324/*** (C) COPYRIGHT 2022 Nuvoton Technology Corp. ***/
#define SC_INTEN_TMR2IEN_Msk
Definition: M4521.h:8649
#define SC_STATUS_CDPINSTS_Pos
Definition: M4521.h:8732
#define SC_INTEN_ACERRIEN_Msk
Definition: M4521.h:8664
#define SC_INTEN_CDIEN_Msk
Definition: M4521.h:8655
#define SC_PINCTL_PWRINV_Msk
Definition: M4521.h:8775
#define SC_STATUS_CDPINSTS_Msk
Definition: M4521.h:8733
#define SC_INTEN_TMR1IEN_Msk
Definition: M4521.h:8646
#define SC_CTL_NSB_Msk
Definition: M4521.h:8550
#define SC_CTL_CDLV_Pos
Definition: M4521.h:8567
#define SC_ALTCTL_CNTEN1_Msk
Definition: M4521.h:8595
#define SC_INTEN_TMR0IEN_Msk
Definition: M4521.h:8643
#define SC_CTL_CDLV_Msk
Definition: M4521.h:8568
#define SC_CTL_SYNC_Msk
Definition: M4521.h:8571
#define SC_PINCTL_SYNC_Msk
Definition: M4521.h:8790
#define SC_CTL_CDDBSEL_Msk
Definition: M4521.h:8565
#define SC_ALTCTL_ADACEN_Msk
Definition: M4521.h:8604
#define SC_CTL_TMRSEL_Msk
Definition: M4521.h:8547
#define SC_ALTCTL_CNTEN2_Msk
Definition: M4521.h:8598
#define SC_CTL_BGT_Pos
Definition: M4521.h:8543
#define SC_INTEN_BGTIEN_Msk
Definition: M4521.h:8652
#define SC_CTL_SCEN_Msk
Definition: M4521.h:8526
#define SC_INTEN_RDAIEN_Msk
Definition: M4521.h:8634
#define SC_ALTCTL_TXRST_Msk
Definition: M4521.h:8577
#define SC_TMRCTL0_CNT_Msk
Definition: M4521.h:8796
#define SC_ALTCTL_CNTEN0_Msk
Definition: M4521.h:8592
#define SC_ALTCTL_RXRST_Msk
Definition: M4521.h:8580
#define SC_CTL_RXTRGLV_Msk
Definition: M4521.h:8541
#define SC_CTL_RXRTY_Msk
Definition: M4521.h:8553
#define SC_INTEN_TERRIEN_Msk
Definition: M4521.h:8640
#define SC_CTL_AUTOCEN_Msk
Definition: M4521.h:8535
#define SC_CTL_TXRTY_Msk
Definition: M4521.h:8559
__IO uint32_t TMRCTL2
Definition: M4521.h:8508
__IO uint32_t ALTCTL
Definition: M4521.h:8498
__IO uint32_t ETUCTL
Definition: M4521.h:8501
__IO uint32_t INTEN
Definition: M4521.h:8502
__IO uint32_t CTL
Definition: M4521.h:8497
__IO uint32_t STATUS
Definition: M4521.h:8504
__IO uint32_t PINCTL
Definition: M4521.h:8505
__IO uint32_t TMRCTL0
Definition: M4521.h:8506
__IO uint32_t TMRCTL1
Definition: M4521.h:8507
#define TRUE
Definition: M4521.h:13905
#define FALSE
Definition: M4521.h:13908
__IO uint32_t RXTOUT
Definition: M4521.h:8500
#define SC0
Definition: M4521.h:13856
__IO uint32_t EGT
Definition: M4521.h:8499
int32_t g_SC_i32ErrCode
Definition: sc.c:28
#define SC_INTERFACE_NUM
Definition: sc.h:32
#define SC_PIN_STATE_IGNORE
Definition: sc.h:35
void SC_SetBlockGuardTime(SC_T *sc, uint32_t u32BGT)
Set Block Guard Time.
Definition: sc.c:224
void SC_SetCharGuardTime(SC_T *sc, uint32_t u32CGT)
Set character guard time.
Definition: sc.c:237
void SC_ResetReader(SC_T *sc)
This function reset specified smartcard module to its default state for activate smartcard.
Definition: sc.c:157
void SC_Open(SC_T *sc, uint32_t u32CardDet, uint32_t u32PWR)
This function initialized smartcard module.
Definition: sc.c:105
void SC_StopAllTimer(SC_T *sc)
Stop all Timer counting.
Definition: sc.c:250
uint32_t SC_IsCardInserted(SC_T *sc)
This function indicates specified smartcard slot status.
Definition: sc.c:41
void SC_StopTimer(SC_T *sc, uint32_t u32TimerNum)
Stop Timer counting.
Definition: sc.c:306
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:277
void SC_ClearFIFO(SC_T *sc)
Reset the Tx/Rx FIFO.
Definition: sc.c:73
void SC_Close(SC_T *sc)
This function disable specified smartcard module.
Definition: sc.c:84
Definition: M4521.h:7895
uint32_t SystemCoreClock
Definition: system_M4521.c:24