comparison bsp/sim32.c @ 0:75a11d740a02

initial import of gsm-fw from freecalypso-sw rev 1033:5ab737ac3ad7
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 09 Jun 2016 00:02:41 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:75a11d740a02
1 /*
2 * SIM32.C
3 *
4 * Pole Star SIM
5 *
6 * Target : ARM
7 *
8 * Copyright (c) Texas Instruments 1995
9 *
10 */
11
12 #define SIM32_C 1
13
14 #include "../include/config.h"
15 #include "../include/sys_types.h"
16
17 #include "iq.h"
18 #include "sim.h"
19
20
21 #ifdef SIM_DEBUG_TRACE
22 /* working buffer for NULL BYTE */
23 extern SYS_UWORD8 SIM_dbg_null[];
24 /* Nucleus variable given the current number of TDMA frames */
25 extern SYS_UWORD32 IQ_FrameCount;
26 /* working variable to calculate the TDMA ecart */
27 extern SYS_UWORD16 SIM_dbg_tdma_diff;
28 /* working variable storing the current number of TDMA frames elapsed */
29 SYS_UWORD32 SIM_dbg_local_count;
30 #endif
31
32 /*
33 * SIM_IntHandler
34 *
35 * Read cause of SIM interrupt :
36 *
37 * if receive buffer full, read char
38 * if transmitter empty, change direction, transmit a dummy char
39 *
40 */
41 void SIM_IntHandler(void)
42 {
43 volatile unsigned short it, i, stat, conf1;
44 volatile SYS_UWORD8 ins;
45 volatile SYS_UWORD8 rx;
46 volatile SYS_UWORD8 nack;
47 volatile SYS_UWORD8 nack1;
48
49
50 SIM_PORT *p;
51
52 p = &(Sim[0]);
53
54 p->rxParityErr = 0;
55 it = p->c->it;
56
57 if ((it & SIM_IT_ITRX) && !(p->c->maskit & SIM_MASK_RX)) // int on reception
58 {
59 stat = p->c->rx;
60 conf1 = p->conf1;
61
62 #ifdef SIM_DEBUG_TRACE
63 if ((IQ_FrameCount - SIM_dbg_local_count) > SIM_dbg_tdma_diff) {
64 SIM_dbg_tdma_diff = IQ_FrameCount - SIM_dbg_local_count;
65 }
66 SIM_dbg_local_count = IQ_FrameCount;
67 #endif
68
69 // Check if reception parity is enable
70 if (((conf1 & SIM_CONF1_CHKPAR) && ((stat & SIM_DRX_STATRXPAR) != 0))\
71 || ((conf1 & SIM_CONF1_CHKPAR) == 0))
72 {
73 rx = (SYS_UWORD8) (stat & 0x00FF);
74 ins = p->xbuf[1] & p->hw_mask;
75 nack = (~p->xbuf[1]) & p->hw_mask;
76
77 switch (p->moderx)
78 {
79 case 0: //mode of normal reception without proc char (like PTS proc)
80 p->rbuf[p->rx_index++] = rx;
81 break;
82
83 case 1: //mode wait for ACK
84 if ((rx & p->hw_mask) == ins)
85 {
86 p->moderx = 2;
87 }
88 else if ((rx & p->hw_mask) == nack)
89 {
90 p->moderx = 4;
91 }
92 else if (((rx & 0xF0) == 0x60) || ((rx & 0xF0) == 0x90))
93 {
94 if (rx != 0x60) //in case of error code (SW1/SW2) returned by sim card
95 {
96 p->rSW12[p->SWcount++] = rx;
97 p->moderx = 5;
98 }
99 else
100 {
101 p->null_received = 1;
102 #ifdef SIM_DEBUG_TRACE
103 SIM_dbg_null[0]++;
104 #endif
105 }
106 }
107 else
108 {
109 p->errorSIM = SIM_ERR_ABNORMAL_CASE2;
110 }
111 //if rx = 0x60 wait for ACK
112 break;
113
114 case 2: //mode reception by block
115 p->rbuf[p->rx_index++] = rx;
116
117 if(p->expected_data == 256)
118 {
119 if (p->rx_index == 0)
120 {
121 p->moderx = 5;
122 }
123 }
124 else
125 {
126 if (p->rx_index == p->expected_data)
127 {
128 p->moderx = 5;
129 }
130 }
131 break;
132
133 case 3: //mode reception char by char. reception of proc char
134 if ((rx & p->hw_mask) == ins)
135 {
136 p->moderx = 2;
137 }
138 else if ((rx & p->hw_mask) == nack)
139 {
140 p->moderx = 4;
141 } //if rx = 0x60 wait for ACK
142 else if (rx == 0x60)
143 {
144 p->null_received == 1;
145 #ifdef SIM_DEBUG_TRACE
146 SIM_dbg_null[1]++;
147 #endif
148 }
149
150 break;
151
152 case 4: //mode reception char by char. reception of data
153 p->rbuf[p->rx_index++] = rx;
154 p->moderx = 3; //switch to receive proc char mode
155
156 if(p->expected_data == 256)
157 {
158 if (p->rx_index == 0)
159 {
160 p->moderx = 5;
161 }
162 }
163 else
164 {
165 if (p->rx_index == p->expected_data)
166 {
167 p->moderx = 5;
168 }
169 }
170 break;
171
172 case 5: //mode wait for procedure character except NULL
173 if ((rx != 0x60) || (p->SWcount != 0)) //treat NULL character only if arriving before SW1 SW2
174 {
175 p->rSW12[p->SWcount++] = rx;
176 }
177 else
178 {
179 p->null_received = 1;
180 #ifdef SIM_DEBUG_TRACE
181 SIM_dbg_null[2]++;
182 #endif
183 }
184 break;
185
186 case 6: //give the acknowledge char
187 if (((rx & 0xF0) == 0x60) || ((rx & 0xF0) == 0x90))
188 {
189 if (rx != 0x60) //in case of error code (SW1/SW2) returned by sim card
190 {
191 p->rSW12[p->SWcount++] = rx;
192 p->moderx = 5;
193 }
194 else
195 {
196 p->null_received = 1;
197 #ifdef SIM_DEBUG_TRACE
198 SIM_dbg_null[3]++;
199 #endif
200 }
201 }
202 else
203 {
204 p->ack = rx;
205 }
206 }
207 }
208 else
209 {
210 p->rxParityErr = 1;
211 }
212 }
213
214 if ((it & SIM_IT_ITTX) && !(p->c->maskit & SIM_MASK_TX))
215 {
216 #ifdef SIM_DEBUG_TRACE
217 SIM_dbg_local_count = IQ_FrameCount;
218 #endif
219 // check the transmit parity
220 stat = p->c->stat;
221
222
223 if ((stat & SIM_STAT_TXPAR) || ((p->conf1 & SIM_CONF1_CHKPAR) == 0)) //parity disable
224 {
225 if (p->xOut != (p->xIn - 1)) //if only one char transmitted (already transmitted)
226 { //just need to have confirmation of reception
227 if (p->xOut == (p->xIn - 2))
228 {
229 p->xOut++;
230 p->c->tx = *(p->xOut); // transmit
231
232 p->conf1 &= ~SIM_CONF1_TXRX; // return the direction
233 p->c->conf1 = p->conf1;
234 }
235
236 if (p->xOut < (p->xIn - 2))
237 {
238 p->xOut++;
239 p->c->tx = *(p->xOut); // transmit
240 }
241 }
242 }
243 else
244 {
245 p->c->tx = *(p->xOut); // transmit same char
246 p->txParityErr++; // count number of transmit parity errors
247 }
248
249 }
250
251 // Handle errors
252 if ((it & SIM_IT_ITOV) && !(p->c->maskit & SIM_MASK_OV))
253 {
254 p->errorSIM = SIM_ERR_OVF;
255
256 }
257 if ((it & SIM_IT_WT) && !(p->c->maskit & SIM_MASK_WT))
258 {
259 p->errorSIM = SIM_ERR_READ;
260 }
261
262 // Reset the card in case of NATR to let the program continue
263 if ((it & SIM_IT_NATR) && !(p->c->maskit & SIM_MASK_NATR))
264 {
265 p->c->cmd = SIM_CMD_STOP;
266 p->errorSIM = SIM_ERR_NATR;
267 }
268
269 #if ((CHIPSET == 2) || (CHIPSET == 3))
270 // SIM card insertion / extraction
271 if ((it & SIM_IT_CD) && !(p->c->maskit & SIM_MASK_CD))
272 {
273 stat = p->c->stat;
274 if ((stat & SIM_STAT_CD) != SIM_STAT_CD)
275 {
276 (p->RemoveFunc)();
277 p->errorSIM = SIM_ERR_NOCARD;
278 }
279 }
280 #endif
281 }
282
283 #if ((CHIPSET == 4) || (CHIPSET == 5) || (CHIPSET == 6) || (CHIPSET == 7) || (CHIPSET == 8) || (CHIPSET == 9) || (CHIPSET == 10) || (CHIPSET == 11) || (CHIPSET == 12))
284 /*
285 * SIM_CD_IntHandler
286 *
287 * Read cause of SIM interrupt :
288 *
289 */
290 void SIM_CD_IntHandler(void)
291 {
292 volatile unsigned short it_cd, stat;
293 SIM_PORT *p;
294
295 p = &(Sim[0]);
296
297 p->rxParityErr = 0;
298 it_cd = p->c->it_cd;
299
300 // SIM card insertion / extraction
301 if ((it_cd & SIM_IT_CD) && !(p->c->maskit & SIM_MASK_CD))
302 {
303 stat = p->c->stat;
304 if ((stat & SIM_STAT_CD) != SIM_STAT_CD)
305 {
306 (p->RemoveFunc)();
307 p->errorSIM = SIM_ERR_NOCARD;
308 }
309 }
310 }
311 #endif
312
313
314 // to force this module to be linked
315 SYS_UWORD16 SIM_Dummy(void)
316 {
317
318 }