]>
Commit | Line | Data |
---|---|---|
6949aca9 | 1 | //-----------------------------------------------------------------------------\r |
2 | // The main application code. This is the first thing called after start.c\r | |
3 | // executes.\r | |
4 | // Jonathan Westhues, Mar 2006\r | |
5 | // Edits by Gerhard de Koning Gans, Sep 2007 (##)\r | |
6 | //-----------------------------------------------------------------------------\r | |
7 | \r | |
8 | #include <proxmark3.h>\r | |
9 | #include <stdlib.h>\r | |
10 | #include "apps.h"\r | |
a7247d85 | 11 | #include "legicrf.h"\r |
6949aca9 | 12 | #ifdef WITH_LCD\r |
13 | #include "fonts.h"\r | |
14 | #include "LCD.h"\r | |
15 | #endif\r | |
16 | \r | |
17 | //=============================================================================\r | |
18 | // A buffer where we can queue things up to be sent through the FPGA, for\r | |
19 | // any purpose (fake tag, as reader, whatever). We go MSB first, since that\r | |
20 | // is the order in which they go out on the wire.\r | |
21 | //=============================================================================\r | |
22 | \r | |
23 | BYTE ToSend[256];\r | |
24 | int ToSendMax;\r | |
25 | static int ToSendBit;\r | |
26 | struct common_area common_area __attribute__((section(".commonarea")));\r | |
27 | \r | |
28 | void BufferClear(void)\r | |
29 | {\r | |
30 | memset(BigBuf,0,sizeof(BigBuf));\r | |
31 | DbpString("Buffer cleared");\r | |
32 | }\r | |
33 | \r | |
34 | void ToSendReset(void)\r | |
35 | {\r | |
36 | ToSendMax = -1;\r | |
37 | ToSendBit = 8;\r | |
38 | }\r | |
39 | \r | |
40 | void ToSendStuffBit(int b)\r | |
41 | {\r | |
42 | if(ToSendBit >= 8) {\r | |
43 | ToSendMax++;\r | |
44 | ToSend[ToSendMax] = 0;\r | |
45 | ToSendBit = 0;\r | |
46 | }\r | |
47 | \r | |
48 | if(b) {\r | |
49 | ToSend[ToSendMax] |= (1 << (7 - ToSendBit));\r | |
50 | }\r | |
51 | \r | |
52 | ToSendBit++;\r | |
53 | \r | |
54 | if(ToSendBit >= sizeof(ToSend)) {\r | |
55 | ToSendBit = 0;\r | |
56 | DbpString("ToSendStuffBit overflowed!");\r | |
57 | }\r | |
58 | }\r | |
59 | \r | |
60 | //=============================================================================\r | |
61 | // Debug print functions, to go out over USB, to the usual PC-side client.\r | |
62 | //=============================================================================\r | |
63 | \r | |
64 | void DbpString(char *str)\r | |
65 | {\r | |
66 | /* this holds up stuff unless we're connected to usb */\r | |
67 | if (!UsbConnected())\r | |
68 | return;\r | |
69 | \r | |
70 | UsbCommand c;\r | |
71 | c.cmd = CMD_DEBUG_PRINT_STRING;\r | |
72 | c.ext1 = strlen(str);\r | |
73 | memcpy(c.d.asBytes, str, c.ext1);\r | |
74 | \r | |
75 | UsbSendPacket((BYTE *)&c, sizeof(c));\r | |
76 | // TODO fix USB so stupid things like this aren't req'd\r | |
77 | SpinDelay(50);\r | |
78 | }\r | |
79 | \r | |
80 | void DbpIntegers(int x1, int x2, int x3)\r | |
81 | {\r | |
82 | /* this holds up stuff unless we're connected to usb */\r | |
83 | if (!UsbConnected())\r | |
84 | return;\r | |
85 | \r | |
86 | UsbCommand c;\r | |
87 | c.cmd = CMD_DEBUG_PRINT_INTEGERS;\r | |
88 | c.ext1 = x1;\r | |
89 | c.ext2 = x2;\r | |
90 | c.ext3 = x3;\r | |
91 | \r | |
92 | UsbSendPacket((BYTE *)&c, sizeof(c));\r | |
93 | // XXX\r | |
94 | SpinDelay(50);\r | |
95 | }\r | |
96 | \r | |
97 | //-----------------------------------------------------------------------------\r | |
98 | // Read an ADC channel and block till it completes, then return the result\r | |
99 | // in ADC units (0 to 1023). Also a routine to average 32 samples and\r | |
100 | // return that.\r | |
101 | //-----------------------------------------------------------------------------\r | |
102 | static int ReadAdc(int ch)\r | |
103 | {\r | |
104 | DWORD d;\r | |
105 | \r | |
106 | AT91C_BASE_ADC->ADC_CR = AT91C_ADC_SWRST;\r | |
107 | AT91C_BASE_ADC->ADC_MR =\r | |
108 | ADC_MODE_PRESCALE(32) |\r | |
109 | ADC_MODE_STARTUP_TIME(16) |\r | |
110 | ADC_MODE_SAMPLE_HOLD_TIME(8);\r | |
111 | AT91C_BASE_ADC->ADC_CHER = ADC_CHANNEL(ch);\r | |
112 | \r | |
113 | AT91C_BASE_ADC->ADC_CR = AT91C_ADC_START;\r | |
114 | while(!(AT91C_BASE_ADC->ADC_SR & ADC_END_OF_CONVERSION(ch)))\r | |
115 | ;\r | |
116 | d = AT91C_BASE_ADC->ADC_CDR[ch];\r | |
117 | \r | |
118 | return d;\r | |
119 | }\r | |
120 | \r | |
121 | static int AvgAdc(int ch)\r | |
122 | {\r | |
123 | int i;\r | |
124 | int a = 0;\r | |
125 | \r | |
126 | for(i = 0; i < 32; i++) {\r | |
127 | a += ReadAdc(ch);\r | |
128 | }\r | |
129 | \r | |
130 | return (a + 15) >> 5;\r | |
131 | }\r | |
132 | \r | |
133 | void MeasureAntennaTuning(void)\r | |
134 | {\r | |
135 | BYTE *dest = (BYTE *)BigBuf;\r | |
136 | int i, ptr = 0, adcval = 0, peak = 0, peakv = 0, peakf = 0;;\r | |
137 | int vLf125 = 0, vLf134 = 0, vHf = 0; // in mV\r | |
138 | \r | |
139 | UsbCommand c;\r | |
140 | \r | |
141 | DbpString("Measuring antenna characteristics, please wait.");\r | |
142 | memset(BigBuf,0,sizeof(BigBuf));\r | |
143 | \r | |
144 | /*\r | |
145 | * Sweeps the useful LF range of the proxmark from\r | |
146 | * 46.8kHz (divisor=255) to 600kHz (divisor=19) and\r | |
147 | * read the voltage in the antenna, the result left\r | |
148 | * in the buffer is a graph which should clearly show\r | |
149 | * the resonating frequency of your LF antenna\r | |
150 | * ( hopefully around 95 if it is tuned to 125kHz!)\r | |
151 | */\r | |
152 | FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER);\r | |
153 | for (i=255; i>19; i--) {\r | |
154 | FpgaSendCommand(FPGA_CMD_SET_DIVISOR, i);\r | |
155 | SpinDelay(20);\r | |
156 | // Vref = 3.3V, and a 10000:240 voltage divider on the input\r | |
157 | // can measure voltages up to 137500 mV\r | |
158 | adcval = ((137500 * AvgAdc(ADC_CHAN_LF)) >> 10);\r | |
159 | if (i==95) vLf125 = adcval; // voltage at 125Khz\r | |
160 | if (i==89) vLf134 = adcval; // voltage at 134Khz\r | |
161 | \r | |
162 | dest[i] = adcval>>8; // scale int to fit in byte for graphing purposes\r | |
163 | if(dest[i] > peak) {\r | |
164 | peakv = adcval;\r | |
165 | peak = dest[i];\r | |
166 | peakf = i;\r | |
167 | ptr = i;\r | |
168 | }\r | |
169 | }\r | |
170 | \r | |
171 | // Let the FPGA drive the high-frequency antenna around 13.56 MHz.\r | |
172 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR);\r | |
173 | SpinDelay(20);\r | |
174 | // Vref = 3300mV, and an 10:1 voltage divider on the input\r | |
175 | // can measure voltages up to 33000 mV\r | |
176 | vHf = (33000 * AvgAdc(ADC_CHAN_HF)) >> 10;\r | |
177 | \r | |
178 | c.cmd = CMD_MEASURED_ANTENNA_TUNING;\r | |
179 | c.ext1 = (vLf125 << 0) | (vLf134 << 16);\r | |
180 | c.ext2 = vHf;\r | |
181 | c.ext3 = peakf | (peakv << 16);\r | |
182 | UsbSendPacket((BYTE *)&c, sizeof(c));\r | |
183 | }\r | |
184 | \r | |
185 | void SimulateTagHfListen(void)\r | |
186 | {\r | |
187 | BYTE *dest = (BYTE *)BigBuf;\r | |
188 | int n = sizeof(BigBuf);\r | |
189 | BYTE v = 0;\r | |
190 | int i;\r | |
191 | int p = 0;\r | |
192 | \r | |
193 | // We're using this mode just so that I can test it out; the simulated\r | |
194 | // tag mode would work just as well and be simpler.\r | |
195 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR | FPGA_HF_READER_RX_XCORR_848_KHZ | FPGA_HF_READER_RX_XCORR_SNOOP);\r | |
196 | \r | |
197 | // We need to listen to the high-frequency, peak-detected path.\r | |
198 | SetAdcMuxFor(GPIO_MUXSEL_HIPKD);\r | |
199 | \r | |
200 | FpgaSetupSsc();\r | |
201 | \r | |
202 | i = 0;\r | |
203 | for(;;) {\r | |
204 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {\r | |
205 | AT91C_BASE_SSC->SSC_THR = 0xff;\r | |
206 | }\r | |
207 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {\r | |
208 | BYTE r = (BYTE)AT91C_BASE_SSC->SSC_RHR;\r | |
209 | \r | |
210 | v <<= 1;\r | |
211 | if(r & 1) {\r | |
212 | v |= 1;\r | |
213 | }\r | |
214 | p++;\r | |
215 | \r | |
216 | if(p >= 8) {\r | |
217 | dest[i] = v;\r | |
218 | v = 0;\r | |
219 | p = 0;\r | |
220 | i++;\r | |
221 | \r | |
222 | if(i >= n) {\r | |
223 | break;\r | |
224 | }\r | |
225 | }\r | |
226 | }\r | |
227 | }\r | |
228 | DbpString("simulate tag (now type bitsamples)");\r | |
229 | }\r | |
230 | \r | |
231 | void ReadMem(int addr)\r | |
232 | {\r | |
233 | const DWORD *data = ((DWORD *)addr);\r | |
234 | int i;\r | |
235 | \r | |
236 | DbpString("Reading memory at address");\r | |
237 | DbpIntegers(0, 0, addr);\r | |
238 | for (i = 0; i < 8; i+= 2)\r | |
239 | DbpIntegers(0, data[i], data[i+1]);\r | |
240 | }\r | |
241 | \r | |
242 | /* osimage version information is linked in */\r | |
243 | extern struct version_information version_information;\r | |
244 | /* bootrom version information is pointed to from _bootphase1_version_pointer */\r | |
245 | extern char *_bootphase1_version_pointer, _flash_start, _flash_end;\r | |
246 | void SendVersion(void)\r | |
247 | {\r | |
248 | char temp[48]; /* Limited data payload in USB packets */\r | |
249 | DbpString("Prox/RFID mark3 RFID instrument");\r | |
250 | \r | |
251 | /* Try to find the bootrom version information. Expect to find a pointer at \r | |
252 | * symbol _bootphase1_version_pointer, perform slight sanity checks on the\r | |
253 | * pointer, then use it.\r | |
254 | */\r | |
255 | char *bootrom_version = *(char**)&_bootphase1_version_pointer;\r | |
256 | if( bootrom_version < &_flash_start || bootrom_version >= &_flash_end ) {\r | |
257 | DbpString("bootrom version information appears invalid");\r | |
258 | } else {\r | |
259 | FormatVersionInformation(temp, sizeof(temp), "bootrom: ", bootrom_version);\r | |
260 | DbpString(temp);\r | |
261 | }\r | |
262 | \r | |
263 | FormatVersionInformation(temp, sizeof(temp), "os: ", &version_information);\r | |
264 | DbpString(temp);\r | |
265 | \r | |
266 | FpgaGatherVersion(temp, sizeof(temp));\r | |
267 | DbpString(temp);\r | |
268 | }\r | |
269 | \r | |
e7014022 | 270 | #ifdef WITH_LF\r |
6949aca9 | 271 | // samy's sniff and repeat routine\r |
272 | void SamyRun()\r | |
273 | {\r | |
274 | DbpString("Stand-alone mode! No PC necessary.");\r | |
275 | \r | |
276 | // 3 possible options? no just 2 for now\r | |
277 | #define OPTS 2\r | |
278 | \r | |
279 | int high[OPTS], low[OPTS];\r | |
280 | \r | |
281 | // Oooh pretty -- notify user we're in elite samy mode now\r | |
282 | LED(LED_RED, 200);\r | |
283 | LED(LED_ORANGE, 200);\r | |
284 | LED(LED_GREEN, 200);\r | |
285 | LED(LED_ORANGE, 200);\r | |
286 | LED(LED_RED, 200);\r | |
287 | LED(LED_ORANGE, 200);\r | |
288 | LED(LED_GREEN, 200);\r | |
289 | LED(LED_ORANGE, 200);\r | |
290 | LED(LED_RED, 200);\r | |
291 | \r | |
292 | int selected = 0;\r | |
293 | int playing = 0;\r | |
294 | \r | |
295 | // Turn on selected LED\r | |
296 | LED(selected + 1, 0);\r | |
297 | \r | |
298 | for (;;)\r | |
299 | {\r | |
300 | UsbPoll(FALSE);\r | |
301 | WDT_HIT();\r | |
302 | \r | |
303 | // Was our button held down or pressed?\r | |
304 | int button_pressed = BUTTON_HELD(1000);\r | |
305 | SpinDelay(300);\r | |
306 | \r | |
307 | // Button was held for a second, begin recording\r | |
308 | if (button_pressed > 0)\r | |
309 | {\r | |
310 | LEDsoff();\r | |
311 | LED(selected + 1, 0);\r | |
312 | LED(LED_RED2, 0);\r | |
313 | \r | |
314 | // record\r | |
315 | DbpString("Starting recording");\r | |
316 | \r | |
317 | // wait for button to be released\r | |
318 | while(BUTTON_PRESS())\r | |
319 | WDT_HIT();\r | |
320 | \r | |
321 | /* need this delay to prevent catching some weird data */\r | |
322 | SpinDelay(500);\r | |
323 | \r | |
324 | CmdHIDdemodFSK(1, &high[selected], &low[selected], 0);\r | |
325 | DbpString("Recorded");\r | |
326 | DbpIntegers(selected, high[selected], low[selected]);\r | |
327 | \r | |
328 | LEDsoff();\r | |
329 | LED(selected + 1, 0);\r | |
330 | // Finished recording\r | |
331 | \r | |
332 | // If we were previously playing, set playing off\r | |
333 | // so next button push begins playing what we recorded\r | |
334 | playing = 0;\r | |
335 | }\r | |
336 | \r | |
337 | // Change where to record (or begin playing)\r | |
338 | else if (button_pressed)\r | |
339 | {\r | |
340 | // Next option if we were previously playing\r | |
341 | if (playing)\r | |
342 | selected = (selected + 1) % OPTS;\r | |
343 | playing = !playing;\r | |
344 | \r | |
345 | LEDsoff();\r | |
346 | LED(selected + 1, 0);\r | |
347 | \r | |
348 | // Begin transmitting\r | |
349 | if (playing)\r | |
350 | {\r | |
351 | LED(LED_GREEN, 0);\r | |
352 | DbpString("Playing");\r | |
353 | // wait for button to be released\r | |
354 | while(BUTTON_PRESS())\r | |
355 | WDT_HIT();\r | |
356 | DbpIntegers(selected, high[selected], low[selected]);\r | |
357 | CmdHIDsimTAG(high[selected], low[selected], 0);\r | |
358 | DbpString("Done playing");\r | |
359 | if (BUTTON_HELD(1000) > 0)\r | |
360 | {\r | |
361 | DbpString("Exiting");\r | |
362 | LEDsoff();\r | |
363 | return;\r | |
364 | }\r | |
365 | \r | |
366 | /* We pressed a button so ignore it here with a delay */\r | |
367 | SpinDelay(300);\r | |
368 | \r | |
369 | // when done, we're done playing, move to next option\r | |
370 | selected = (selected + 1) % OPTS;\r | |
371 | playing = !playing;\r | |
372 | LEDsoff();\r | |
373 | LED(selected + 1, 0);\r | |
374 | }\r | |
375 | else\r | |
376 | while(BUTTON_PRESS())\r | |
377 | WDT_HIT();\r | |
378 | }\r | |
379 | }\r | |
380 | }\r | |
c0d04e95 | 381 | #endif\r |
6949aca9 | 382 | \r |
383 | /*\r | |
384 | OBJECTIVE\r | |
385 | Listen and detect an external reader. Determine the best location\r | |
386 | for the antenna.\r | |
387 | \r | |
388 | INSTRUCTIONS:\r | |
389 | Inside the ListenReaderField() function, there is two mode.\r | |
390 | By default, when you call the function, you will enter mode 1.\r | |
391 | If you press the PM3 button one time, you will enter mode 2.\r | |
392 | If you press the PM3 button a second time, you will exit the function.\r | |
393 | \r | |
394 | DESCRIPTION OF MODE 1:\r | |
395 | This mode just listens for an external reader field and lights up green\r | |
396 | for HF and/or red for LF. This is the original mode of the detectreader\r | |
397 | function.\r | |
398 | \r | |
399 | DESCRIPTION OF MODE 2:\r | |
400 | This mode will visually represent, using the LEDs, the actual strength of the\r | |
401 | current compared to the maximum current detected. Basically, once you know\r | |
402 | what kind of external reader is present, it will help you spot the best location to place\r | |
403 | your antenna. You will probably not get some good results if there is a LF and a HF reader\r | |
404 | at the same place! :-)\r | |
405 | \r | |
406 | LIGHT SCHEME USED:\r | |
407 | */\r | |
408 | static const char LIGHT_SCHEME[] = {\r | |
409 | 0x0, /* ---- | No field detected */\r | |
410 | 0x1, /* X--- | 14% of maximum current detected */\r | |
411 | 0x2, /* -X-- | 29% of maximum current detected */\r | |
412 | 0x4, /* --X- | 43% of maximum current detected */\r | |
413 | 0x8, /* ---X | 57% of maximum current detected */\r | |
414 | 0xC, /* --XX | 71% of maximum current detected */\r | |
415 | 0xE, /* -XXX | 86% of maximum current detected */\r | |
416 | 0xF, /* XXXX | 100% of maximum current detected */\r | |
417 | };\r | |
418 | static const int LIGHT_LEN = sizeof(LIGHT_SCHEME)/sizeof(LIGHT_SCHEME[0]);\r | |
419 | \r | |
420 | void ListenReaderField(int limit)\r | |
421 | {\r | |
422 | int lf_av, lf_av_new, lf_baseline= 0, lf_count= 0, lf_max;\r | |
423 | int hf_av, hf_av_new, hf_baseline= 0, hf_count= 0, hf_max;\r | |
424 | int mode=1, display_val, display_max, i;\r | |
425 | \r | |
426 | #define LF_ONLY 1\r | |
427 | #define HF_ONLY 2\r | |
428 | \r | |
429 | LEDsoff();\r | |
430 | \r | |
431 | lf_av=lf_max=ReadAdc(ADC_CHAN_LF);\r | |
432 | \r | |
433 | if(limit != HF_ONLY) {\r | |
434 | DbpString("LF 125/134 Baseline:");\r | |
435 | DbpIntegers(lf_av,0,0);\r | |
436 | lf_baseline= lf_av;\r | |
437 | }\r | |
438 | \r | |
439 | hf_av=hf_max=ReadAdc(ADC_CHAN_HF);\r | |
440 | \r | |
441 | if (limit != LF_ONLY) {\r | |
442 | DbpString("HF 13.56 Baseline:");\r | |
443 | DbpIntegers(hf_av,0,0);\r | |
444 | hf_baseline= hf_av;\r | |
445 | }\r | |
446 | \r | |
447 | for(;;) {\r | |
448 | if (BUTTON_PRESS()) {\r | |
449 | SpinDelay(500);\r | |
450 | switch (mode) {\r | |
451 | case 1:\r | |
452 | mode=2;\r | |
453 | DbpString("Signal Strength Mode");\r | |
454 | break;\r | |
455 | case 2:\r | |
456 | default:\r | |
457 | DbpString("Stopped");\r | |
458 | LEDsoff();\r | |
459 | return;\r | |
460 | break;\r | |
461 | }\r | |
462 | }\r | |
463 | WDT_HIT();\r | |
464 | \r | |
465 | if (limit != HF_ONLY) {\r | |
466 | if(mode==1) {\r | |
467 | if (abs(lf_av - lf_baseline) > 10) LED_D_ON();\r | |
468 | else LED_D_OFF();\r | |
469 | }\r | |
470 | \r | |
471 | ++lf_count;\r | |
472 | lf_av_new= ReadAdc(ADC_CHAN_LF);\r | |
473 | // see if there's a significant change\r | |
474 | if(abs(lf_av - lf_av_new) > 10) {\r | |
475 | DbpString("LF 125/134 Field Change:");\r | |
476 | DbpIntegers(lf_av,lf_av_new,lf_count);\r | |
477 | lf_av= lf_av_new;\r | |
478 | if (lf_av > lf_max)\r | |
479 | lf_max = lf_av;\r | |
480 | lf_count= 0;\r | |
481 | }\r | |
482 | }\r | |
483 | \r | |
484 | if (limit != LF_ONLY) {\r | |
485 | if (mode == 1){\r | |
486 | if (abs(hf_av - hf_baseline) > 10) LED_B_ON();\r | |
487 | else LED_B_OFF();\r | |
488 | }\r | |
489 | \r | |
490 | ++hf_count;\r | |
491 | hf_av_new= ReadAdc(ADC_CHAN_HF);\r | |
492 | // see if there's a significant change\r | |
493 | if(abs(hf_av - hf_av_new) > 10) {\r | |
494 | DbpString("HF 13.56 Field Change:");\r | |
495 | DbpIntegers(hf_av,hf_av_new,hf_count);\r | |
496 | hf_av= hf_av_new;\r | |
497 | if (hf_av > hf_max)\r | |
498 | hf_max = hf_av;\r | |
499 | hf_count= 0;\r | |
500 | }\r | |
501 | }\r | |
502 | \r | |
503 | if(mode == 2) {\r | |
504 | if (limit == LF_ONLY) {\r | |
505 | display_val = lf_av;\r | |
506 | display_max = lf_max;\r | |
507 | } else if (limit == HF_ONLY) {\r | |
508 | display_val = hf_av;\r | |
509 | display_max = hf_max;\r | |
510 | } else { /* Pick one at random */\r | |
511 | if( (hf_max - hf_baseline) > (lf_max - lf_baseline) ) {\r | |
512 | display_val = hf_av;\r | |
513 | display_max = hf_max;\r | |
514 | } else {\r | |
515 | display_val = lf_av;\r | |
516 | display_max = lf_max;\r | |
517 | }\r | |
518 | }\r | |
519 | for (i=0; i<LIGHT_LEN; i++) {\r | |
520 | if (display_val >= ((display_max/LIGHT_LEN)*i) && display_val <= ((display_max/LIGHT_LEN)*(i+1))) {\r | |
521 | if (LIGHT_SCHEME[i] & 0x1) LED_C_ON(); else LED_C_OFF();\r | |
522 | if (LIGHT_SCHEME[i] & 0x2) LED_A_ON(); else LED_A_OFF();\r | |
523 | if (LIGHT_SCHEME[i] & 0x4) LED_B_ON(); else LED_B_OFF();\r | |
524 | if (LIGHT_SCHEME[i] & 0x8) LED_D_ON(); else LED_D_OFF();\r | |
525 | break;\r | |
526 | }\r | |
527 | }\r | |
528 | }\r | |
529 | }\r | |
530 | }\r | |
531 | \r | |
532 | void UsbPacketReceived(BYTE *packet, int len)\r | |
533 | {\r | |
534 | UsbCommand *c = (UsbCommand *)packet;\r | |
535 | \r | |
536 | switch(c->cmd) {\r | |
e7014022 | 537 | #ifdef WITH_LF\r |
6949aca9 | 538 | case CMD_ACQUIRE_RAW_ADC_SAMPLES_125K:\r |
539 | AcquireRawAdcSamples125k(c->ext1);\r | |
540 | break;\r | |
c0d04e95 | 541 | #endif\r |
6949aca9 | 542 | \r |
e7014022 | 543 | #ifdef WITH_LF\r |
6949aca9 | 544 | case CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K:\r |
545 | ModThenAcquireRawAdcSamples125k(c->ext1,c->ext2,c->ext3,c->d.asBytes);\r | |
546 | break;\r | |
c0d04e95 | 547 | #endif\r |
6949aca9 | 548 | \r |
b1083ec9 | 549 | #ifdef WITH_ISO15693\r |
6949aca9 | 550 | case CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_15693:\r |
551 | AcquireRawAdcSamplesIso15693();\r | |
552 | break;\r | |
b1083ec9 | 553 | #endif\r |
6949aca9 | 554 | \r |
555 | case CMD_BUFF_CLEAR:\r | |
556 | BufferClear();\r | |
557 | break;\r | |
558 | \r | |
b1083ec9 | 559 | #ifdef WITH_ISO15693\r |
6949aca9 | 560 | case CMD_READER_ISO_15693:\r |
561 | ReaderIso15693(c->ext1);\r | |
562 | break;\r | |
b1083ec9 | 563 | #endif\r |
6949aca9 | 564 | \r |
dcc10e5e | 565 | case CMD_READER_LEGIC_RF:\r |
566 | LegicRfReader();\r | |
567 | break;\r | |
568 | \r | |
b1083ec9 | 569 | #ifdef WITH_ISO15693\r |
6949aca9 | 570 | case CMD_SIMTAG_ISO_15693:\r |
571 | SimTagIso15693(c->ext1);\r | |
572 | break;\r | |
b1083ec9 | 573 | #endif\r |
6949aca9 | 574 | \r |
b1083ec9 | 575 | #ifdef WITH_ISO14443b\r |
6949aca9 | 576 | case CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443:\r |
577 | AcquireRawAdcSamplesIso14443(c->ext1);\r | |
578 | break;\r | |
b1083ec9 | 579 | #endif\r |
6949aca9 | 580 | \r |
b1083ec9 | 581 | #ifdef WITH_ISO14443b\r |
6949aca9 | 582 | case CMD_READ_SRI512_TAG:\r |
583 | ReadSRI512Iso14443(c->ext1);\r | |
584 | break;\r | |
c86cc308 | 585 | case CMD_READ_SRIX4K_TAG: |
586 | ReadSRIX4KIso14443(c->ext1); | |
587 | break; | |
b1083ec9 | 588 | #endif\r |
6949aca9 | 589 | \r |
b1083ec9 | 590 | #ifdef WITH_ISO14443a\r |
6949aca9 | 591 | case CMD_READER_ISO_14443a:\r |
592 | ReaderIso14443a(c->ext1);\r | |
593 | break;\r | |
b1083ec9 | 594 | #endif\r |
6949aca9 | 595 | \r |
b1083ec9 | 596 | #ifdef WITH_ISO14443b\r |
6949aca9 | 597 | case CMD_SNOOP_ISO_14443:\r |
598 | SnoopIso14443();\r | |
599 | break;\r | |
b1083ec9 | 600 | #endif\r |
6949aca9 | 601 | \r |
b1083ec9 | 602 | #ifdef WITH_ISO14443a\r |
6949aca9 | 603 | case CMD_SNOOP_ISO_14443a:\r |
604 | SnoopIso14443a();\r | |
605 | break;\r | |
b1083ec9 | 606 | #endif\r |
6949aca9 | 607 | \r |
608 | case CMD_SIMULATE_TAG_HF_LISTEN:\r | |
609 | SimulateTagHfListen();\r | |
610 | break;\r | |
611 | \r | |
b1083ec9 | 612 | #ifdef WITH_ISO14443b\r |
6949aca9 | 613 | case CMD_SIMULATE_TAG_ISO_14443:\r |
614 | SimulateIso14443Tag();\r | |
615 | break;\r | |
b1083ec9 | 616 | #endif\r |
a7247d85 | 617 | \r |
b1083ec9 | 618 | #ifdef WITH_ISO14443a\r |
6949aca9 | 619 | case CMD_SIMULATE_TAG_ISO_14443a:\r |
620 | SimulateIso14443aTag(c->ext1, c->ext2); // ## Simulate iso14443a tag - pass tag type & UID\r | |
621 | break;\r | |
b1083ec9 | 622 | #endif\r |
6949aca9 | 623 | \r |
a7247d85 | 624 | case CMD_SIMULATE_TAG_LEGIC_RF:\r |
625 | LegicRfSimulate();\r | |
626 | break;\r | |
627 | \r | |
6949aca9 | 628 | case CMD_MEASURE_ANTENNA_TUNING:\r |
629 | MeasureAntennaTuning();\r | |
630 | break;\r | |
631 | \r | |
632 | case CMD_LISTEN_READER_FIELD:\r | |
633 | ListenReaderField(c->ext1);\r | |
634 | break;\r | |
635 | \r | |
e7014022 | 636 | #ifdef WITH_LF\r |
6949aca9 | 637 | case CMD_HID_DEMOD_FSK:\r |
638 | CmdHIDdemodFSK(0, 0, 0, 1); // Demodulate HID tag\r | |
639 | break;\r | |
c0d04e95 | 640 | #endif\r |
6949aca9 | 641 | \r |
e7014022 | 642 | #ifdef WITH_LF\r |
6949aca9 | 643 | case CMD_HID_SIM_TAG:\r |
644 | CmdHIDsimTAG(c->ext1, c->ext2, 1); // Simulate HID tag by ID\r | |
645 | break;\r | |
c0d04e95 | 646 | #endif\r |
6949aca9 | 647 | \r |
648 | case CMD_FPGA_MAJOR_MODE_OFF: // ## FPGA Control\r | |
649 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r | |
650 | SpinDelay(200);\r | |
651 | LED_D_OFF(); // LED D indicates field ON or OFF\r | |
652 | break;\r | |
653 | \r | |
e7014022 | 654 | #ifdef WITH_LF\r |
6949aca9 | 655 | case CMD_READ_TI_TYPE:\r |
656 | ReadTItag();\r | |
657 | break;\r | |
c0d04e95 | 658 | #endif\r |
6949aca9 | 659 | \r |
e7014022 | 660 | #ifdef WITH_LF\r |
6949aca9 | 661 | case CMD_WRITE_TI_TYPE:\r |
662 | WriteTItag(c->ext1,c->ext2,c->ext3);\r | |
663 | break;\r | |
c0d04e95 | 664 | #endif\r |
6949aca9 | 665 | \r |
666 | case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K: {\r | |
667 | UsbCommand n;\r | |
668 | if(c->cmd == CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K) {\r | |
669 | n.cmd = CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K;\r | |
670 | } else {\r | |
671 | n.cmd = CMD_DOWNLOADED_RAW_BITS_TI_TYPE;\r | |
672 | }\r | |
673 | n.ext1 = c->ext1;\r | |
674 | memcpy(n.d.asDwords, BigBuf+c->ext1, 12*sizeof(DWORD));\r | |
675 | UsbSendPacket((BYTE *)&n, sizeof(n));\r | |
676 | break;\r | |
677 | }\r | |
c0d04e95 | 678 | \r |
6949aca9 | 679 | case CMD_DOWNLOADED_SIM_SAMPLES_125K: {\r |
680 | BYTE *b = (BYTE *)BigBuf;\r | |
681 | memcpy(b+c->ext1, c->d.asBytes, 48);\r | |
682 | break;\r | |
683 | }\r | |
c0d04e95 | 684 | \r |
e7014022 | 685 | #ifdef WITH_LF\r |
6949aca9 | 686 | case CMD_SIMULATE_TAG_125K:\r |
687 | LED_A_ON();\r | |
688 | SimulateTagLowFrequency(c->ext1, 1);\r | |
689 | LED_A_OFF();\r | |
690 | break;\r | |
c0d04e95 | 691 | #endif\r |
692 | \r | |
6949aca9 | 693 | case CMD_READ_MEM:\r |
694 | ReadMem(c->ext1);\r | |
695 | break;\r | |
c0d04e95 | 696 | \r |
6949aca9 | 697 | case CMD_SET_LF_DIVISOR:\r |
698 | FpgaSendCommand(FPGA_CMD_SET_DIVISOR, c->ext1);\r | |
699 | break;\r | |
c0d04e95 | 700 | \r |
5fc8250f | 701 | case CMD_SET_ADC_MUX:\r |
702 | switch(c->ext1) {\r | |
c0d04e95 | 703 | case 0: SetAdcMuxFor(GPIO_MUXSEL_LOPKD); break;\r |
704 | case 1: SetAdcMuxFor(GPIO_MUXSEL_LORAW); break;\r | |
705 | case 2: SetAdcMuxFor(GPIO_MUXSEL_HIPKD); break;\r | |
706 | case 3: SetAdcMuxFor(GPIO_MUXSEL_HIRAW); break;\r | |
5fc8250f | 707 | }\r |
708 | break;\r | |
c0d04e95 | 709 | \r |
6949aca9 | 710 | case CMD_VERSION:\r |
711 | SendVersion();\r | |
712 | break;\r | |
c0d04e95 | 713 | \r |
e7014022 | 714 | #ifdef WITH_LF\r |
6949aca9 | 715 | case CMD_LF_SIMULATE_BIDIR:\r |
716 | SimulateTagLowFrequencyBidir(c->ext1, c->ext2);\r | |
717 | break;\r | |
c0d04e95 | 718 | #endif\r |
719 | \r | |
6949aca9 | 720 | #ifdef WITH_LCD\r |
721 | case CMD_LCD_RESET:\r | |
722 | LCDReset();\r | |
723 | break;\r | |
724 | case CMD_LCD:\r | |
725 | LCDSend(c->ext1);\r | |
726 | break;\r | |
727 | #endif\r | |
728 | case CMD_SETUP_WRITE:\r | |
729 | case CMD_FINISH_WRITE:\r | |
730 | case CMD_HARDWARE_RESET:\r | |
731 | USB_D_PLUS_PULLUP_OFF();\r | |
732 | SpinDelay(1000);\r | |
733 | SpinDelay(1000);\r | |
734 | AT91C_BASE_RSTC->RSTC_RCR = RST_CONTROL_KEY | AT91C_RSTC_PROCRST;\r | |
735 | for(;;) {\r | |
736 | // We're going to reset, and the bootrom will take control.\r | |
737 | }\r | |
738 | break;\r | |
c0d04e95 | 739 | \r |
6949aca9 | 740 | case CMD_START_FLASH:\r |
741 | if(common_area.flags.bootrom_present) {\r | |
742 | common_area.command = COMMON_AREA_COMMAND_ENTER_FLASH_MODE;\r | |
743 | }\r | |
744 | USB_D_PLUS_PULLUP_OFF();\r | |
745 | AT91C_BASE_RSTC->RSTC_RCR = RST_CONTROL_KEY | AT91C_RSTC_PROCRST;\r | |
746 | for(;;);\r | |
747 | break;\r | |
748 | \r | |
749 | case CMD_DEVICE_INFO: {\r | |
750 | UsbCommand c;\r | |
751 | c.cmd = CMD_DEVICE_INFO;\r | |
752 | c.ext1 = DEVICE_INFO_FLAG_OSIMAGE_PRESENT | DEVICE_INFO_FLAG_CURRENT_MODE_OS;\r | |
753 | if(common_area.flags.bootrom_present) c.ext1 |= DEVICE_INFO_FLAG_BOOTROM_PRESENT;\r | |
754 | UsbSendPacket((BYTE*)&c, sizeof(c));\r | |
755 | }\r | |
756 | break;\r | |
757 | default:\r | |
758 | DbpString("unknown command");\r | |
759 | break;\r | |
760 | }\r | |
761 | }\r | |
762 | \r | |
763 | void __attribute__((noreturn)) AppMain(void)\r | |
764 | {\r | |
765 | SpinDelay(100);\r | |
766 | \r | |
767 | if(common_area.magic != COMMON_AREA_MAGIC || common_area.version != 1) {\r | |
768 | /* Initialize common area */\r | |
769 | memset(&common_area, 0, sizeof(common_area));\r | |
770 | common_area.magic = COMMON_AREA_MAGIC;\r | |
771 | common_area.version = 1;\r | |
772 | }\r | |
773 | common_area.flags.osimage_present = 1;\r | |
774 | \r | |
775 | LED_D_OFF();\r | |
776 | LED_C_OFF();\r | |
777 | LED_B_OFF();\r | |
778 | LED_A_OFF();\r | |
779 | \r | |
780 | UsbStart();\r | |
781 | \r | |
782 | // The FPGA gets its clock from us from PCK0 output, so set that up.\r | |
783 | AT91C_BASE_PIOA->PIO_BSR = GPIO_PCK0;\r | |
784 | AT91C_BASE_PIOA->PIO_PDR = GPIO_PCK0;\r | |
785 | AT91C_BASE_PMC->PMC_SCER = AT91C_PMC_PCK0;\r | |
786 | // PCK0 is PLL clock / 4 = 96Mhz / 4 = 24Mhz\r | |
787 | AT91C_BASE_PMC->PMC_PCKR[0] = AT91C_PMC_CSS_PLL_CLK |\r | |
788 | AT91C_PMC_PRES_CLK_4;\r | |
789 | AT91C_BASE_PIOA->PIO_OER = GPIO_PCK0;\r | |
790 | \r | |
791 | // Reset SPI\r | |
792 | AT91C_BASE_SPI->SPI_CR = AT91C_SPI_SWRST;\r | |
793 | // Reset SSC\r | |
794 | AT91C_BASE_SSC->SSC_CR = AT91C_SSC_SWRST;\r | |
795 | \r | |
796 | // Load the FPGA image, which we have stored in our flash.\r | |
797 | FpgaDownloadAndGo();\r | |
798 | \r | |
799 | #ifdef WITH_LCD\r | |
800 | \r | |
801 | LCDInit();\r | |
802 | \r | |
803 | // test text on different colored backgrounds\r | |
804 | LCDString(" The quick brown fox ", (char *)&FONT6x8,1,1+8*0,WHITE ,BLACK );\r | |
805 | LCDString(" jumped over the ", (char *)&FONT6x8,1,1+8*1,BLACK ,WHITE );\r | |
806 | LCDString(" lazy dog. ", (char *)&FONT6x8,1,1+8*2,YELLOW ,RED );\r | |
807 | LCDString(" AaBbCcDdEeFfGgHhIiJj ", (char *)&FONT6x8,1,1+8*3,RED ,GREEN );\r | |
808 | LCDString(" KkLlMmNnOoPpQqRrSsTt ", (char *)&FONT6x8,1,1+8*4,MAGENTA,BLUE );\r | |
809 | LCDString("UuVvWwXxYyZz0123456789", (char *)&FONT6x8,1,1+8*5,BLUE ,YELLOW);\r | |
810 | LCDString("`-=[]_;',./~!@#$%^&*()", (char *)&FONT6x8,1,1+8*6,BLACK ,CYAN );\r | |
811 | LCDString(" _+{}|:\\\"<>? ",(char *)&FONT6x8,1,1+8*7,BLUE ,MAGENTA);\r | |
812 | \r | |
813 | // color bands\r | |
814 | LCDFill(0, 1+8* 8, 132, 8, BLACK);\r | |
815 | LCDFill(0, 1+8* 9, 132, 8, WHITE);\r | |
816 | LCDFill(0, 1+8*10, 132, 8, RED);\r | |
817 | LCDFill(0, 1+8*11, 132, 8, GREEN);\r | |
818 | LCDFill(0, 1+8*12, 132, 8, BLUE);\r | |
819 | LCDFill(0, 1+8*13, 132, 8, YELLOW);\r | |
820 | LCDFill(0, 1+8*14, 132, 8, CYAN);\r | |
821 | LCDFill(0, 1+8*15, 132, 8, MAGENTA);\r | |
822 | \r | |
823 | #endif\r | |
824 | \r | |
825 | for(;;) {\r | |
826 | UsbPoll(FALSE);\r | |
827 | WDT_HIT();\r | |
828 | \r | |
e7014022 | 829 | #ifdef WITH_LF\r |
6949aca9 | 830 | if (BUTTON_HELD(1000) > 0)\r |
831 | SamyRun();\r | |
c0d04e95 | 832 | #endif\r |
6949aca9 | 833 | }\r |
834 | }\r |