| NUC472_NUC442_BSP V3.03.003 The Board Support Package for NUC472/NUC442

sc.c
Go to the documentation of this file.
1/**************************************************************************/
12#include "NUC472_442.h"
13
14// Below are variables used locally by SC driver and does not want to parse by doxygen unless HIDDEN_SYMBOLS is defined
16 static uint32_t u32CardStateIgnore[SC_INTERFACE_NUM] = {0, 0, 0, 0, 0, 0};
17
19
40uint32_t SC_IsCardInserted(SC_T *sc)
41{
42 // put conditions into two variable to remove IAR compilation warning
43 uint32_t cond1 = ((sc->STATUS & SC_STATUS_CDPINSTS_Msk) >> SC_STATUS_CDPINSTS_Pos);
44 uint32_t cond2 = ((sc->CTL & SC_CTL_CDLV_Msk) >> SC_CTL_CDLV_Pos);
45
46 if(sc == SC0 && u32CardStateIgnore[0] == 1)
47 return TRUE;
48 else if(sc == SC1 && u32CardStateIgnore[1] == 1)
49 return TRUE;
50 else if(sc == SC2 && u32CardStateIgnore[2] == 1)
51 return TRUE;
52 else if(sc == SC3 && u32CardStateIgnore[3] == 1)
53 return TRUE;
54 else if(sc == SC4 && u32CardStateIgnore[4] == 1)
55 return TRUE;
56 else if(sc == SC5 && u32CardStateIgnore[5] == 1)
57 return TRUE;
58 else if(cond1 != cond2)
59 return FALSE;
60 else
61 return TRUE;
62}
63
69 void SC_ClearFIFO(SC_T *sc)
70{
71 sc->ALTCTL |= (SC_ALTCTL_TXRST_Msk | SC_ALTCTL_RXRST_Msk);
72}
73
79 void SC_Close(SC_T *sc)
80{
81 sc->INTEN = 0;
82 while(sc->PINCTL & SC_PINCTL_SYNC_Msk);
83 sc->PINCTL = 0;
84 sc->ALTCTL = 0;
85 while(sc->CTL & SC_CTL_SYNC_Msk);
86 sc->CTL = 0;
87}
88
101 void SC_Open(SC_T *sc, uint32_t u32CD, uint32_t u32PWR)
102{
103 uint32_t u32Reg = 0, u32Intf;
104
105 if(sc == SC0)
106 u32Intf = 0;
107 else if(sc == SC1)
108 u32Intf = 1;
109 else if(sc == SC2)
110 u32Intf = 2;
111 else if(sc == SC3)
112 u32Intf = 3;
113 else if(sc == SC4)
114 u32Intf = 4;
115 else
116 u32Intf = 5;
117
118 if(u32CD != SC_PIN_STATE_IGNORE)
119 {
120 u32Reg = u32CD ? 0: SC_CTL_CDLV_Msk;
121 u32CardStateIgnore[u32Intf] = 0;
122 }
123 else
124 {
125 u32CardStateIgnore[u32Intf] = 1;
126 }
127 sc->PINCTL = u32PWR ? 0 : SC_PINCTL_PWRINV_Msk;
128 while(sc->CTL & SC_CTL_SYNC_Msk);
129 sc->CTL = SC_CTL_SCEN_Msk | u32Reg;
130}
131
137 void SC_ResetReader(SC_T *sc)
138{
139 uint32_t u32Intf;
140
141 if(sc == SC0)
142 u32Intf = 0;
143 else if(sc == SC1)
144 u32Intf = 1;
145 else if(sc == SC2)
146 u32Intf = 2;
147 else if(sc == SC3)
148 u32Intf = 3;
149 else if(sc == SC4)
150 u32Intf = 4;
151 else
152 u32Intf = 5;
153
154 // Reset FIFO, enable auto de-activation while card removal
155 sc->ALTCTL |= (SC_ALTCTL_TXRST_Msk | SC_ALTCTL_RXRST_Msk | SC_ALTCTL_ADACEN_Msk);
156 // Set Rx trigger level to 1 character, longest card detect debounce period, disable error retry (EMV ATR does not use error retry)
157 while(sc->CTL & SC_CTL_SYNC_Msk);
158 sc->CTL &= ~(SC_CTL_RXTRGLV_Msk |
159 SC_CTL_CDDBSEL_Msk |
160 SC_CTL_TXRTY_Msk |
161 SC_CTL_TXRTYEN_Msk |
162 SC_CTL_RXRTY_Msk |
163 SC_CTL_RXRTYEN_Msk);
164 while(sc->CTL & SC_CTL_SYNC_Msk);
165 // Enable auto convention, and all three smartcard internal timers
166 sc->CTL |= SC_CTL_AUTOCEN_Msk | SC_CTL_TMRSEL_Msk;
167 // Disable Rx timeout
168 sc->RXTOUT = 0;
169 // 372 clocks per ETU by default
170 sc->ETUCTL= 371;
171
172
173 /* Enable necessary interrupt for smartcard operation */
174 if(u32CardStateIgnore[u32Intf]) // Do not enable card detect interrupt if card present state ignore
175 sc->INTEN = (SC_INTEN_RDAIEN_Msk |
176 SC_INTEN_TERRIEN_Msk |
177 SC_INTEN_TMR0IEN_Msk |
178 SC_INTEN_TMR1IEN_Msk |
179 SC_INTEN_TMR2IEN_Msk |
180 SC_INTEN_BGTIEN_Msk |
181 SC_INTEN_ACERRIEN_Msk);
182 else
183 sc->INTEN = (SC_INTEN_RDAIEN_Msk |
184 SC_INTEN_TERRIEN_Msk |
185 SC_INTEN_TMR0IEN_Msk |
186 SC_INTEN_TMR1IEN_Msk |
187 SC_INTEN_TMR2IEN_Msk |
188 SC_INTEN_BGTIEN_Msk |
189 SC_INTEN_CDIEN_Msk |
190 SC_INTEN_ACERRIEN_Msk);
191
192 return;
193}
194
201 void SC_SetBlockGuardTime(SC_T *sc, uint32_t u32BGT)
202{
203 sc->CTL = (sc->CTL & ~SC_CTL_BGT_Msk) | ((u32BGT - 1) << SC_CTL_BGT_Pos);
204}
205
212 void SC_SetCharGuardTime(SC_T *sc, uint32_t u32CGT)
213{
214 u32CGT -= sc->CTL & SC_CTL_NSB_Msk ? 11: 12;
215 sc->EGT = u32CGT;
216}
217
224 void SC_StopAllTimer(SC_T *sc)
225{
226 sc->ALTCTL &= ~(SC_ALTCTL_CNTEN0_Msk | SC_ALTCTL_CNTEN1_Msk | SC_ALTCTL_CNTEN2_Msk);
227}
228
250 void SC_StartTimer(SC_T *sc, uint32_t u32TimerNum, uint32_t u32Mode, uint32_t u32ETUCount)
251{
252 uint32_t reg = u32Mode | (SC_TMRCTL0_CNT_Msk & (u32ETUCount - 1));
253
254 if(u32TimerNum == 0)
255 {
256 sc->TMRCTL0 = reg;
257 sc->ALTCTL |= SC_ALTCTL_CNTEN0_Msk;
258 }
259 else if(u32TimerNum == 1)
260 {
261 sc->TMRCTL1 = reg;
262 sc->ALTCTL |= SC_ALTCTL_CNTEN1_Msk;
263 }
264 else // timer 2
265 {
266 sc->TMRCTL2 = reg;
267 sc->ALTCTL |= SC_ALTCTL_CNTEN2_Msk;
268 }
269}
270
278 void SC_StopTimer(SC_T *sc, uint32_t u32TimerNum)
279{
280 if(u32TimerNum == 0)
281 sc->ALTCTL &= ~SC_ALTCTL_CNTEN0_Msk;
282 else if(u32TimerNum == 1)
283 sc->ALTCTL &= ~SC_ALTCTL_CNTEN1_Msk;
284 else // timer 2
285 sc->ALTCTL &= ~SC_ALTCTL_CNTEN2_Msk;
286}
287
288
289 /* end of group NUC472_442_SC_EXPORTED_FUNCTIONS */
291 /* end of group NUC472_442_SC_Driver */
293 /* end of group NUC472_442_Device_Driver */
295
296/*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/
NUC472_442.h
NUC472/NUC442 peripheral access layer header file. This file contains all the peripheral register's d...
SC_INTEN_TMR2IEN_Msk
#define SC_INTEN_TMR2IEN_Msk
Definition: NUC472_442.h:21870
SC_STATUS_CDPINSTS_Pos
#define SC_STATUS_CDPINSTS_Pos
Definition: NUC472_442.h:21953
SC_INTEN_ACERRIEN_Msk
#define SC_INTEN_ACERRIEN_Msk
Definition: NUC472_442.h:21885
SC_INTEN_CDIEN_Msk
#define SC_INTEN_CDIEN_Msk
Definition: NUC472_442.h:21876
SC_PINCTL_PWRINV_Msk
#define SC_PINCTL_PWRINV_Msk
Definition: NUC472_442.h:21993
SC_CTL_TXRTYEN_Msk
#define SC_CTL_TXRTYEN_Msk
Definition: NUC472_442.h:21789
SC_CTL_RXRTYEN_Msk
#define SC_CTL_RXRTYEN_Msk
Definition: NUC472_442.h:21783
SC_STATUS_CDPINSTS_Msk
#define SC_STATUS_CDPINSTS_Msk
Definition: NUC472_442.h:21954
SC_INTEN_TMR1IEN_Msk
#define SC_INTEN_TMR1IEN_Msk
Definition: NUC472_442.h:21867
SC_CTL_NSB_Msk
#define SC_CTL_NSB_Msk
Definition: NUC472_442.h:21777
SC_CTL_CDLV_Pos
#define SC_CTL_CDLV_Pos
Definition: NUC472_442.h:21794
SC_ALTCTL_CNTEN1_Msk
#define SC_ALTCTL_CNTEN1_Msk
Definition: NUC472_442.h:21819
SC_INTEN_TMR0IEN_Msk
#define SC_INTEN_TMR0IEN_Msk
Definition: NUC472_442.h:21864
SC_CTL_CDLV_Msk
#define SC_CTL_CDLV_Msk
Definition: NUC472_442.h:21795
SC_CTL_SYNC_Msk
#define SC_CTL_SYNC_Msk
Definition: NUC472_442.h:21798
SC_PINCTL_SYNC_Msk
#define SC_PINCTL_SYNC_Msk
Definition: NUC472_442.h:22005
SC_CTL_CDDBSEL_Msk
#define SC_CTL_CDDBSEL_Msk
Definition: NUC472_442.h:21792
SC_ALTCTL_ADACEN_Msk
#define SC_ALTCTL_ADACEN_Msk
Definition: NUC472_442.h:21828
SC_CTL_TMRSEL_Msk
#define SC_CTL_TMRSEL_Msk
Definition: NUC472_442.h:21774
SC_ALTCTL_CNTEN2_Msk
#define SC_ALTCTL_CNTEN2_Msk
Definition: NUC472_442.h:21822
SC_CTL_BGT_Pos
#define SC_CTL_BGT_Pos
Definition: NUC472_442.h:21770
SC_INTEN_BGTIEN_Msk
#define SC_INTEN_BGTIEN_Msk
Definition: NUC472_442.h:21873
SC_CTL_SCEN_Msk
#define SC_CTL_SCEN_Msk
Definition: NUC472_442.h:21753
SC_INTEN_RDAIEN_Msk
#define SC_INTEN_RDAIEN_Msk
Definition: NUC472_442.h:21855
SC_ALTCTL_TXRST_Msk
#define SC_ALTCTL_TXRST_Msk
Definition: NUC472_442.h:21801
SC_TMRCTL0_CNT_Msk
#define SC_TMRCTL0_CNT_Msk
Definition: NUC472_442.h:22008
SC_ALTCTL_CNTEN0_Msk
#define SC_ALTCTL_CNTEN0_Msk
Definition: NUC472_442.h:21816
SC_ALTCTL_RXRST_Msk
#define SC_ALTCTL_RXRST_Msk
Definition: NUC472_442.h:21804
SC_CTL_RXTRGLV_Msk
#define SC_CTL_RXTRGLV_Msk
Definition: NUC472_442.h:21768
SC_CTL_RXRTY_Msk
#define SC_CTL_RXRTY_Msk
Definition: NUC472_442.h:21780
SC_INTEN_TERRIEN_Msk
#define SC_INTEN_TERRIEN_Msk
Definition: NUC472_442.h:21861
SC_CTL_AUTOCEN_Msk
#define SC_CTL_AUTOCEN_Msk
Definition: NUC472_442.h:21762
SC_CTL_TXRTY_Msk
#define SC_CTL_TXRTY_Msk
Definition: NUC472_442.h:21786
SC4
#define SC4
Definition: NUC472_442.h:28868
SC5
#define SC5
Definition: NUC472_442.h:28869
SC2
#define SC2
Definition: NUC472_442.h:28866
SC1
#define SC1
Definition: NUC472_442.h:28865
SC3
#define SC3
Definition: NUC472_442.h:28867
SC0
#define SC0
Definition: NUC472_442.h:28864
SC_INTERFACE_NUM
#define SC_INTERFACE_NUM
Definition: sc.h:32
SC_PIN_STATE_IGNORE
#define SC_PIN_STATE_IGNORE
Definition: sc.h:35
SC_SetBlockGuardTime
void SC_SetBlockGuardTime(SC_T *sc, uint32_t u32BGT)
This function block guard time (BGT) of specified smartcard module.
Definition: sc.c:201
SC_SetCharGuardTime
void SC_SetCharGuardTime(SC_T *sc, uint32_t u32CGT)
This function character guard time (CGT) of specified smartcard module.
Definition: sc.c:212
SC_ResetReader
void SC_ResetReader(SC_T *sc)
This function reset specified smartcard module to its default state for activate smartcard.
Definition: sc.c:137
SC_Open
void SC_Open(SC_T *sc, uint32_t u32CardDet, uint32_t u32PWR)
This function initialized smartcard module.
Definition: sc.c:101
SC_StopAllTimer
void SC_StopAllTimer(SC_T *sc)
This function stop all smartcard timer of specified smartcard module.
Definition: sc.c:224
SC_IsCardInserted
uint32_t SC_IsCardInserted(SC_T *sc)
This function indicates specified smartcard slot status.
Definition: sc.c:40
SC_StopTimer
void SC_StopTimer(SC_T *sc, uint32_t u32TimerNum)
This function stop a smartcard timer of specified smartcard module.
Definition: sc.c:278
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:250
SC_ClearFIFO
void SC_ClearFIFO(SC_T *sc)
This function reset both transmit and receive FIFO of specified smartcard module.
Definition: sc.c:69
SC_Close
void SC_Close(SC_T *sc)
This function disable specified smartcard module.
Definition: sc.c:79
SC_T::TMRCTL2
__IO uint32_t TMRCTL2
Definition: NUC472_442.h:21684
SC_T::ALTCTL
__IO uint32_t ALTCTL
Definition: NUC472_442.h:21325
SC_T::CTL
__IO uint32_t CTL
Definition: NUC472_442.h:21226
SC_T::EGT
__IO uint32_t EGT
Definition: NUC472_442.h:21338
SC_T::PINCTL
__IO uint32_t PINCTL
Definition: NUC472_442.h:21639
SC_T::STATUS
__IO uint32_t STATUS
Definition: NUC472_442.h:21569
TRUE
#define TRUE
Boolean true, define to use in API parameters or return value.
Definition: NUC472_442.h:29021
FALSE
#define FALSE
Boolean false, define to use in API parameters or return value.
Definition: NUC472_442.h:29022
SC_T::RXTOUT
__IO uint32_t RXTOUT
Definition: NUC472_442.h:21353
SC_T::TMRCTL0
__IO uint32_t TMRCTL0
Definition: NUC472_442.h:21654
SC_T::INTEN
__IO uint32_t INTEN
Definition: NUC472_442.h:21427
SC_T::ETUCTL
__IO uint32_t ETUCTL
Definition: NUC472_442.h:21372
SC_T::TMRCTL1
__IO uint32_t TMRCTL1
Definition: NUC472_442.h:21669
SC_T
Definition: NUC472_442.h:21114