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