]> cvs.zerfleddert.de Git - proxmark3-svn/blob - armsrc/lfops.c
Fix QT CXXFLAGS/LDFLAGS
[proxmark3-svn] / armsrc / lfops.c
1 //-----------------------------------------------------------------------------
2 // Miscellaneous routines for low frequency tag operations.
3 // Tags supported here so far are Texas Instruments (TI), HID
4 // Also routines for raw mode reading/simulating of LF waveform
5 //
6 //-----------------------------------------------------------------------------
7 #include <proxmark3.h>
8 #include "apps.h"
9 #include "hitag2.h"
10 #include "../common/crc16.c"
11
12 void AcquireRawAdcSamples125k(BOOL at134khz)
13 {
14 if(at134khz) {
15 FpgaSendCommand(FPGA_CMD_SET_DIVISOR, 88); //134.8Khz
16 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER);
17 } else {
18 FpgaSendCommand(FPGA_CMD_SET_DIVISOR, 95); //125Khz
19 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER);
20 }
21
22 // Connect the A/D to the peak-detected low-frequency path.
23 SetAdcMuxFor(GPIO_MUXSEL_LOPKD);
24
25 // Give it a bit of time for the resonant antenna to settle.
26 SpinDelay(50);
27
28 // Now set up the SSC to get the ADC samples that are now streaming at us.
29 FpgaSetupSsc();
30
31 // Now call the acquisition routine
32 DoAcquisition125k(at134khz);
33 }
34
35 // split into two routines so we can avoid timing issues after sending commands //
36 void DoAcquisition125k(BOOL at134khz)
37 {
38 BYTE *dest = (BYTE *)BigBuf;
39 int n = sizeof(BigBuf);
40 int i;
41
42 memset(dest,0,n);
43 i = 0;
44 for(;;) {
45 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
46 AT91C_BASE_SSC->SSC_THR = 0x43;
47 LED_D_ON();
48 }
49 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
50 dest[i] = (BYTE)AT91C_BASE_SSC->SSC_RHR;
51 i++;
52 LED_D_OFF();
53 if(i >= n) {
54 break;
55 }
56 }
57 }
58 DbpIntegers(dest[0], dest[1], at134khz);
59 }
60
61 void ModThenAcquireRawAdcSamples125k(int delay_off,int period_0,int period_1,BYTE *command)
62 {
63 BOOL at134khz;
64
65 /* Make sure the tag is reset */
66 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
67 SpinDelay(2500);
68
69 // see if 'h' was specified
70 if(command[strlen((char *) command) - 1] == 'h')
71 at134khz= TRUE;
72 else
73 at134khz= FALSE;
74
75 if(at134khz) {
76 FpgaSendCommand(FPGA_CMD_SET_DIVISOR, 88); //134.8Khz
77 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER);
78 } else {
79 FpgaSendCommand(FPGA_CMD_SET_DIVISOR, 95); //125Khz
80 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER);
81 }
82
83 // Give it a bit of time for the resonant antenna to settle.
84 SpinDelay(50);
85 // And a little more time for the tag to fully power up
86 SpinDelay(2000);
87
88 // Now set up the SSC to get the ADC samples that are now streaming at us.
89 FpgaSetupSsc();
90
91 // now modulate the reader field
92 while(*command != '\0' && *command != ' ')
93 {
94 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
95 LED_D_OFF();
96 SpinDelayUs(delay_off);
97 if(at134khz) {
98 FpgaSendCommand(FPGA_CMD_SET_DIVISOR, 88); //134.8Khz
99 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER);
100 } else {
101 FpgaSendCommand(FPGA_CMD_SET_DIVISOR, 95); //125Khz
102 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER);
103 }
104 LED_D_ON();
105 if(*(command++) == '0') {
106 SpinDelayUs(period_0);
107 } else {
108 SpinDelayUs(period_1);
109 }
110 }
111 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
112 LED_D_OFF();
113 SpinDelayUs(delay_off);
114 if(at134khz) {
115 FpgaSendCommand(FPGA_CMD_SET_DIVISOR, 88); //134.8Khz
116 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER);
117 } else {
118 FpgaSendCommand(FPGA_CMD_SET_DIVISOR, 95); //125Khz
119 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER);
120 }
121
122 // now do the read
123 DoAcquisition125k(at134khz);
124 }
125
126 /* blank r/w tag data stream
127 ...0000000000000000 01111111
128 1010101010101010101010101010101010101010101010101010101010101010
129 0011010010100001
130 01111111
131 101010101010101[0]000...
132
133 [5555fe852c5555555555555555fe0000]
134 */
135 void ReadTItag()
136 {
137 // some hardcoded initial params
138 // when we read a TI tag we sample the zerocross line at 2Mhz
139 // TI tags modulate a 1 as 16 cycles of 123.2Khz
140 // TI tags modulate a 0 as 16 cycles of 134.2Khz
141 #define FSAMPLE 2000000
142 #define FREQLO 123200
143 #define FREQHI 134200
144
145 signed char *dest = (signed char *)BigBuf;
146 int n = sizeof(BigBuf);
147 // int *dest = GraphBuffer;
148 // int n = GraphTraceLen;
149
150 // 128 bit shift register [shift3:shift2:shift1:shift0]
151 DWORD shift3 = 0, shift2 = 0, shift1 = 0, shift0 = 0;
152
153 int i, cycles=0, samples=0;
154 // how many sample points fit in 16 cycles of each frequency
155 DWORD sampleslo = (FSAMPLE<<4)/FREQLO, sampleshi = (FSAMPLE<<4)/FREQHI;
156 // when to tell if we're close enough to one freq or another
157 DWORD threshold = (sampleslo - sampleshi + 1)>>1;
158
159 // TI tags charge at 134.2Khz
160 FpgaSendCommand(FPGA_CMD_SET_DIVISOR, 88); //134.8Khz
161
162 // Place FPGA in passthrough mode, in this mode the CROSS_LO line
163 // connects to SSP_DIN and the SSP_DOUT logic level controls
164 // whether we're modulating the antenna (high)
165 // or listening to the antenna (low)
166 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_PASSTHRU);
167
168 // get TI tag data into the buffer
169 AcquireTiType();
170
171 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
172
173 for (i=0; i<n-1; i++) {
174 // count cycles by looking for lo to hi zero crossings
175 if ( (dest[i]<0) && (dest[i+1]>0) ) {
176 cycles++;
177 // after 16 cycles, measure the frequency
178 if (cycles>15) {
179 cycles=0;
180 samples=i-samples; // number of samples in these 16 cycles
181
182 // TI bits are coming to us lsb first so shift them
183 // right through our 128 bit right shift register
184 shift0 = (shift0>>1) | (shift1 << 31);
185 shift1 = (shift1>>1) | (shift2 << 31);
186 shift2 = (shift2>>1) | (shift3 << 31);
187 shift3 >>= 1;
188
189 // check if the cycles fall close to the number
190 // expected for either the low or high frequency
191 if ( (samples>(sampleslo-threshold)) && (samples<(sampleslo+threshold)) ) {
192 // low frequency represents a 1
193 shift3 |= (1<<31);
194 } else if ( (samples>(sampleshi-threshold)) && (samples<(sampleshi+threshold)) ) {
195 // high frequency represents a 0
196 } else {
197 // probably detected a gay waveform or noise
198 // use this as gaydar or discard shift register and start again
199 shift3 = shift2 = shift1 = shift0 = 0;
200 }
201 samples = i;
202
203 // for each bit we receive, test if we've detected a valid tag
204
205 // if we see 17 zeroes followed by 6 ones, we might have a tag
206 // remember the bits are backwards
207 if ( ((shift0 & 0x7fffff) == 0x7e0000) ) {
208 // if start and end bytes match, we have a tag so break out of the loop
209 if ( ((shift0>>16)&0xff) == ((shift3>>8)&0xff) ) {
210 cycles = 0xF0B; //use this as a flag (ugly but whatever)
211 break;
212 }
213 }
214 }
215 }
216 }
217
218 // if flag is set we have a tag
219 if (cycles!=0xF0B) {
220 DbpString("Info: No valid tag detected.");
221 } else {
222 // put 64 bit data into shift1 and shift0
223 shift0 = (shift0>>24) | (shift1 << 8);
224 shift1 = (shift1>>24) | (shift2 << 8);
225
226 // align 16 bit crc into lower half of shift2
227 shift2 = ((shift2>>24) | (shift3 << 8)) & 0x0ffff;
228
229 // if r/w tag, check ident match
230 if ( shift3&(1<<15) ) {
231 DbpString("Info: TI tag is rewriteable");
232 // only 15 bits compare, last bit of ident is not valid
233 if ( ((shift3>>16)^shift0)&0x7fff ) {
234 DbpString("Error: Ident mismatch!");
235 } else {
236 DbpString("Info: TI tag ident is valid");
237 }
238 } else {
239 DbpString("Info: TI tag is readonly");
240 }
241
242 // WARNING the order of the bytes in which we calc crc below needs checking
243 // i'm 99% sure the crc algorithm is correct, but it may need to eat the
244 // bytes in reverse or something
245 // calculate CRC
246 DWORD crc=0;
247
248 crc = update_crc16(crc, (shift0)&0xff);
249 crc = update_crc16(crc, (shift0>>8)&0xff);
250 crc = update_crc16(crc, (shift0>>16)&0xff);
251 crc = update_crc16(crc, (shift0>>24)&0xff);
252 crc = update_crc16(crc, (shift1)&0xff);
253 crc = update_crc16(crc, (shift1>>8)&0xff);
254 crc = update_crc16(crc, (shift1>>16)&0xff);
255 crc = update_crc16(crc, (shift1>>24)&0xff);
256
257 DbpString("Info: Tag data_hi, data_lo, crc = ");
258 DbpIntegers(shift1, shift0, shift2&0xffff);
259 if (crc != (shift2&0xffff)) {
260 DbpString("Error: CRC mismatch, expected");
261 DbpIntegers(0, 0, crc);
262 } else {
263 DbpString("Info: CRC is good");
264 }
265 }
266 }
267
268 void WriteTIbyte(BYTE b)
269 {
270 int i = 0;
271
272 // modulate 8 bits out to the antenna
273 for (i=0; i<8; i++)
274 {
275 if (b&(1<<i)) {
276 // stop modulating antenna
277 LOW(GPIO_SSC_DOUT);
278 SpinDelayUs(1000);
279 // modulate antenna
280 HIGH(GPIO_SSC_DOUT);
281 SpinDelayUs(1000);
282 } else {
283 // stop modulating antenna
284 LOW(GPIO_SSC_DOUT);
285 SpinDelayUs(300);
286 // modulate antenna
287 HIGH(GPIO_SSC_DOUT);
288 SpinDelayUs(1700);
289 }
290 }
291 }
292
293 void AcquireTiType(void)
294 {
295 int i, j, n;
296 // tag transmission is <20ms, sampling at 2M gives us 40K samples max
297 // each sample is 1 bit stuffed into a DWORD so we need 1250 DWORDS
298 #define TIBUFLEN 1250
299
300 // clear buffer
301 memset(BigBuf,0,sizeof(BigBuf));
302
303 // Set up the synchronous serial port
304 AT91C_BASE_PIOA->PIO_PDR = GPIO_SSC_DIN;
305 AT91C_BASE_PIOA->PIO_ASR = GPIO_SSC_DIN;
306
307 // steal this pin from the SSP and use it to control the modulation
308 AT91C_BASE_PIOA->PIO_PER = GPIO_SSC_DOUT;
309 AT91C_BASE_PIOA->PIO_OER = GPIO_SSC_DOUT;
310
311 AT91C_BASE_SSC->SSC_CR = AT91C_SSC_SWRST;
312 AT91C_BASE_SSC->SSC_CR = AT91C_SSC_RXEN | AT91C_SSC_TXEN;
313
314 // Sample at 2 Mbit/s, so TI tags are 16.2 vs. 14.9 clocks long
315 // 48/2 = 24 MHz clock must be divided by 12
316 AT91C_BASE_SSC->SSC_CMR = 12;
317
318 AT91C_BASE_SSC->SSC_RCMR = SSC_CLOCK_MODE_SELECT(0);
319 AT91C_BASE_SSC->SSC_RFMR = SSC_FRAME_MODE_BITS_IN_WORD(32) | AT91C_SSC_MSBF;
320 AT91C_BASE_SSC->SSC_TCMR = 0;
321 AT91C_BASE_SSC->SSC_TFMR = 0;
322
323 LED_D_ON();
324
325 // modulate antenna
326 HIGH(GPIO_SSC_DOUT);
327
328 // Charge TI tag for 50ms.
329 SpinDelay(50);
330
331 // stop modulating antenna and listen
332 LOW(GPIO_SSC_DOUT);
333
334 LED_D_OFF();
335
336 i = 0;
337 for(;;) {
338 if(AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY) {
339 BigBuf[i] = AT91C_BASE_SSC->SSC_RHR; // store 32 bit values in buffer
340 i++; if(i >= TIBUFLEN) break;
341 }
342 WDT_HIT();
343 }
344
345 // return stolen pin to SSP
346 AT91C_BASE_PIOA->PIO_PDR = GPIO_SSC_DOUT;
347 AT91C_BASE_PIOA->PIO_ASR = GPIO_SSC_DIN | GPIO_SSC_DOUT;
348
349 char *dest = (char *)BigBuf;
350 n = TIBUFLEN*32;
351 // unpack buffer
352 for (i=TIBUFLEN-1; i>=0; i--) {
353 // DbpIntegers(0, 0, BigBuf[i]);
354 for (j=0; j<32; j++) {
355 if(BigBuf[i] & (1 << j)) {
356 dest[--n] = 1;
357 } else {
358 dest[--n] = -1;
359 }
360 }
361 }
362 }
363
364 // arguments: 64bit data split into 32bit idhi:idlo and optional 16bit crc
365 // if crc provided, it will be written with the data verbatim (even if bogus)
366 // if not provided a valid crc will be computed from the data and written.
367 void WriteTItag(DWORD idhi, DWORD idlo, WORD crc)
368 {
369
370 // WARNING the order of the bytes in which we calc crc below needs checking
371 // i'm 99% sure the crc algorithm is correct, but it may need to eat the
372 // bytes in reverse or something
373
374 if(crc == 0) {
375 crc = update_crc16(crc, (idlo)&0xff);
376 crc = update_crc16(crc, (idlo>>8)&0xff);
377 crc = update_crc16(crc, (idlo>>16)&0xff);
378 crc = update_crc16(crc, (idlo>>24)&0xff);
379 crc = update_crc16(crc, (idhi)&0xff);
380 crc = update_crc16(crc, (idhi>>8)&0xff);
381 crc = update_crc16(crc, (idhi>>16)&0xff);
382 crc = update_crc16(crc, (idhi>>24)&0xff);
383 }
384 DbpString("Writing the following data to tag:");
385 DbpIntegers(idhi, idlo, crc);
386
387 // TI tags charge at 134.2Khz
388 FpgaSendCommand(FPGA_CMD_SET_DIVISOR, 88); //134.8Khz
389 // Place FPGA in passthrough mode, in this mode the CROSS_LO line
390 // connects to SSP_DIN and the SSP_DOUT logic level controls
391 // whether we're modulating the antenna (high)
392 // or listening to the antenna (low)
393 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_PASSTHRU);
394 LED_A_ON();
395
396 // steal this pin from the SSP and use it to control the modulation
397 AT91C_BASE_PIOA->PIO_PER = GPIO_SSC_DOUT;
398 AT91C_BASE_PIOA->PIO_OER = GPIO_SSC_DOUT;
399
400 // writing algorithm:
401 // a high bit consists of a field off for 1ms and field on for 1ms
402 // a low bit consists of a field off for 0.3ms and field on for 1.7ms
403 // initiate a charge time of 50ms (field on) then immediately start writing bits
404 // start by writing 0xBB (keyword) and 0xEB (password)
405 // then write 80 bits of data (or 64 bit data + 16 bit crc if you prefer)
406 // finally end with 0x0300 (write frame)
407 // all data is sent lsb firts
408 // finish with 15ms programming time
409
410 // modulate antenna
411 HIGH(GPIO_SSC_DOUT);
412 SpinDelay(50); // charge time
413
414 WriteTIbyte(0xbb); // keyword
415 WriteTIbyte(0xeb); // password
416 WriteTIbyte( (idlo )&0xff );
417 WriteTIbyte( (idlo>>8 )&0xff );
418 WriteTIbyte( (idlo>>16)&0xff );
419 WriteTIbyte( (idlo>>24)&0xff );
420 WriteTIbyte( (idhi )&0xff );
421 WriteTIbyte( (idhi>>8 )&0xff );
422 WriteTIbyte( (idhi>>16)&0xff );
423 WriteTIbyte( (idhi>>24)&0xff ); // data hi to lo
424 WriteTIbyte( (crc )&0xff ); // crc lo
425 WriteTIbyte( (crc>>8 )&0xff ); // crc hi
426 WriteTIbyte(0x00); // write frame lo
427 WriteTIbyte(0x03); // write frame hi
428 HIGH(GPIO_SSC_DOUT);
429 SpinDelay(50); // programming time
430
431 LED_A_OFF();
432
433 // get TI tag data into the buffer
434 AcquireTiType();
435
436 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
437 DbpString("Now use tiread to check");
438 }
439
440 void SimulateTagLowFrequency(int period, int ledcontrol)
441 {
442 int i;
443 BYTE *tab = (BYTE *)BigBuf;
444
445 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_SIMULATOR);
446
447 AT91C_BASE_PIOA->PIO_PER = GPIO_SSC_DOUT | GPIO_SSC_CLK;
448
449 AT91C_BASE_PIOA->PIO_OER = GPIO_SSC_DOUT;
450 AT91C_BASE_PIOA->PIO_ODR = GPIO_SSC_CLK;
451
452 #define SHORT_COIL() LOW(GPIO_SSC_DOUT)
453 #define OPEN_COIL() HIGH(GPIO_SSC_DOUT)
454
455 i = 0;
456 for(;;) {
457 while(!(AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK)) {
458 if(BUTTON_PRESS()) {
459 DbpString("Stopped");
460 return;
461 }
462 WDT_HIT();
463 }
464
465 if (ledcontrol)
466 LED_D_ON();
467
468 if(tab[i])
469 OPEN_COIL();
470 else
471 SHORT_COIL();
472
473 if (ledcontrol)
474 LED_D_OFF();
475
476 while(AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK) {
477 if(BUTTON_PRESS()) {
478 DbpString("Stopped");
479 return;
480 }
481 WDT_HIT();
482 }
483
484 i++;
485 if(i == period) i = 0;
486 }
487 }
488
489 /* Provides a framework for bidirectional LF tag communication
490 * Encoding is currently Hitag2, but the general idea can probably
491 * be transferred to other encodings.
492 *
493 * The new FPGA code will, for the LF simulator mode, give on SSC_FRAME
494 * (PA15) a thresholded version of the signal from the ADC. Setting the
495 * ADC path to the low frequency peak detection signal, will enable a
496 * somewhat reasonable receiver for modulation on the carrier signal
497 * that is generated by the reader. The signal is low when the reader
498 * field is switched off, and high when the reader field is active. Due
499 * to the way that the signal looks like, mostly only the rising edge is
500 * useful, your mileage may vary.
501 *
502 * Neat perk: PA15 can not only be used as a bit-banging GPIO, but is also
503 * TIOA1, which can be used as the capture input for timer 1. This should
504 * make it possible to measure the exact edge-to-edge time, without processor
505 * intervention.
506 *
507 * Arguments: divisor is the divisor to be sent to the FPGA (e.g. 95 for 125kHz)
508 * t0 is the carrier frequency cycle duration in terms of MCK (384 for 125kHz)
509 *
510 * The following defines are in carrier periods:
511 */
512 #define HITAG_T_0_MIN 15 /* T[0] should be 18..22 */
513 #define HITAG_T_1_MIN 24 /* T[1] should be 26..30 */
514 #define HITAG_T_EOF 40 /* T_EOF should be > 36 */
515 #define HITAG_T_WRESP 208 /* T_wresp should be 204..212 */
516
517 static void hitag_handle_frame(int t0, int frame_len, char *frame);
518 //#define DEBUG_RA_VALUES 1
519 #define DEBUG_FRAME_CONTENTS 1
520 void SimulateTagLowFrequencyBidir(int divisor, int t0)
521 {
522 #if DEBUG_RA_VALUES || DEBUG_FRAME_CONTENTS
523 int i = 0;
524 #endif
525 char frame[10];
526 int frame_pos=0;
527
528 DbpString("Starting Hitag2 emulator, press button to end");
529 hitag2_init();
530
531 /* Set up simulator mode, frequency divisor which will drive the FPGA
532 * and analog mux selection.
533 */
534 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_SIMULATOR);
535 FpgaSendCommand(FPGA_CMD_SET_DIVISOR, divisor);
536 SetAdcMuxFor(GPIO_MUXSEL_LOPKD);
537 RELAY_OFF();
538
539 /* Set up Timer 1:
540 * Capture mode, timer source MCK/2 (TIMER_CLOCK1), TIOA is external trigger,
541 * external trigger rising edge, load RA on rising edge of TIOA, load RB on rising
542 * edge of TIOA. Assign PA15 to TIOA1 (peripheral B)
543 */
544
545 AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_TC1);
546 AT91C_BASE_PIOA->PIO_BSR = GPIO_SSC_FRAME;
547 AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS;
548 AT91C_BASE_TC1->TC_CMR = TC_CMR_TCCLKS_TIMER_CLOCK1 |
549 AT91C_TC_ETRGEDG_RISING |
550 AT91C_TC_ABETRG |
551 AT91C_TC_LDRA_RISING |
552 AT91C_TC_LDRB_RISING;
553 AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKEN |
554 AT91C_TC_SWTRG;
555
556 /* calculate the new value for the carrier period in terms of TC1 values */
557 t0 = t0/2;
558
559 int overflow = 0;
560 while(!BUTTON_PRESS()) {
561 WDT_HIT();
562 if(AT91C_BASE_TC1->TC_SR & AT91C_TC_LDRAS) {
563 int ra = AT91C_BASE_TC1->TC_RA;
564 if((ra > t0*HITAG_T_EOF) | overflow) ra = t0*HITAG_T_EOF+1;
565 #if DEBUG_RA_VALUES
566 if(ra > 255 || overflow) ra = 255;
567 ((char*)BigBuf)[i] = ra;
568 i = (i+1) % 8000;
569 #endif
570
571 if(overflow || (ra > t0*HITAG_T_EOF) || (ra < t0*HITAG_T_0_MIN)) {
572 /* Ignore */
573 } else if(ra >= t0*HITAG_T_1_MIN ) {
574 /* '1' bit */
575 if(frame_pos < 8*sizeof(frame)) {
576 frame[frame_pos / 8] |= 1<<( 7-(frame_pos%8) );
577 frame_pos++;
578 }
579 } else if(ra >= t0*HITAG_T_0_MIN) {
580 /* '0' bit */
581 if(frame_pos < 8*sizeof(frame)) {
582 frame[frame_pos / 8] |= 0<<( 7-(frame_pos%8) );
583 frame_pos++;
584 }
585 }
586
587 overflow = 0;
588 LED_D_ON();
589 } else {
590 if(AT91C_BASE_TC1->TC_CV > t0*HITAG_T_EOF) {
591 /* Minor nuisance: In Capture mode, the timer can not be
592 * stopped by a Compare C. There's no way to stop the clock
593 * in software, so we'll just have to note the fact that an
594 * overflow happened and the next loaded timer value might
595 * have wrapped. Also, this marks the end of frame, and the
596 * still running counter can be used to determine the correct
597 * time for the start of the reply.
598 */
599 overflow = 1;
600
601 if(frame_pos > 0) {
602 /* Have a frame, do something with it */
603 #if DEBUG_FRAME_CONTENTS
604 ((char*)BigBuf)[i++] = frame_pos;
605 memcpy( ((char*)BigBuf)+i, frame, 7);
606 i+=7;
607 i = i % sizeof(BigBuf);
608 #endif
609 hitag_handle_frame(t0, frame_pos, frame);
610 memset(frame, 0, sizeof(frame));
611 }
612 frame_pos = 0;
613
614 }
615 LED_D_OFF();
616 }
617 }
618 DbpString("All done");
619 }
620
621 static void hitag_send_bit(int t0, int bit) {
622 if(bit == 1) {
623 /* Manchester: Loaded, then unloaded */
624 LED_A_ON();
625 SHORT_COIL();
626 while(AT91C_BASE_TC1->TC_CV < t0*15);
627 OPEN_COIL();
628 while(AT91C_BASE_TC1->TC_CV < t0*31);
629 LED_A_OFF();
630 } else if(bit == 0) {
631 /* Manchester: Unloaded, then loaded */
632 LED_B_ON();
633 OPEN_COIL();
634 while(AT91C_BASE_TC1->TC_CV < t0*15);
635 SHORT_COIL();
636 while(AT91C_BASE_TC1->TC_CV < t0*31);
637 LED_B_OFF();
638 }
639 AT91C_BASE_TC1->TC_CCR = AT91C_TC_SWTRG; /* Reset clock for the next bit */
640
641 }
642 static void hitag_send_frame(int t0, int frame_len, const char const * frame, int fdt)
643 {
644 OPEN_COIL();
645 AT91C_BASE_PIOA->PIO_OER = GPIO_SSC_DOUT;
646
647 /* Wait for HITAG_T_WRESP carrier periods after the last reader bit,
648 * not that since the clock counts since the rising edge, but T_wresp is
649 * with respect to the falling edge, we need to wait actually (T_wresp - T_g)
650 * periods. The gap time T_g varies (4..10).
651 */
652 while(AT91C_BASE_TC1->TC_CV < t0*(fdt-8));
653
654 int saved_cmr = AT91C_BASE_TC1->TC_CMR;
655 AT91C_BASE_TC1->TC_CMR &= ~AT91C_TC_ETRGEDG; /* Disable external trigger for the clock */
656 AT91C_BASE_TC1->TC_CCR = AT91C_TC_SWTRG; /* Reset the clock and use it for response timing */
657
658 int i;
659 for(i=0; i<5; i++)
660 hitag_send_bit(t0, 1); /* Start of frame */
661
662 for(i=0; i<frame_len; i++) {
663 hitag_send_bit(t0, !!(frame[i/ 8] & (1<<( 7-(i%8) ))) );
664 }
665
666 OPEN_COIL();
667 AT91C_BASE_TC1->TC_CMR = saved_cmr;
668 }
669
670 /* Callback structure to cleanly separate tag emulation code from the radio layer. */
671 static int hitag_cb(const char* response_data, const int response_length, const int fdt, void *cb_cookie)
672 {
673 hitag_send_frame(*(int*)cb_cookie, response_length, response_data, fdt);
674 return 0;
675 }
676 /* Frame length in bits, frame contents in MSBit first format */
677 static void hitag_handle_frame(int t0, int frame_len, char *frame)
678 {
679 hitag2_handle_command(frame, frame_len, hitag_cb, &t0);
680 }
681
682 // compose fc/8 fc/10 waveform
683 static void fc(int c, int *n) {
684 BYTE *dest = (BYTE *)BigBuf;
685 int idx;
686
687 // for when we want an fc8 pattern every 4 logical bits
688 if(c==0) {
689 dest[((*n)++)]=1;
690 dest[((*n)++)]=1;
691 dest[((*n)++)]=0;
692 dest[((*n)++)]=0;
693 dest[((*n)++)]=0;
694 dest[((*n)++)]=0;
695 dest[((*n)++)]=0;
696 dest[((*n)++)]=0;
697 }
698 // an fc/8 encoded bit is a bit pattern of 11000000 x6 = 48 samples
699 if(c==8) {
700 for (idx=0; idx<6; idx++) {
701 dest[((*n)++)]=1;
702 dest[((*n)++)]=1;
703 dest[((*n)++)]=0;
704 dest[((*n)++)]=0;
705 dest[((*n)++)]=0;
706 dest[((*n)++)]=0;
707 dest[((*n)++)]=0;
708 dest[((*n)++)]=0;
709 }
710 }
711
712 // an fc/10 encoded bit is a bit pattern of 1110000000 x5 = 50 samples
713 if(c==10) {
714 for (idx=0; idx<5; idx++) {
715 dest[((*n)++)]=1;
716 dest[((*n)++)]=1;
717 dest[((*n)++)]=1;
718 dest[((*n)++)]=0;
719 dest[((*n)++)]=0;
720 dest[((*n)++)]=0;
721 dest[((*n)++)]=0;
722 dest[((*n)++)]=0;
723 dest[((*n)++)]=0;
724 dest[((*n)++)]=0;
725 }
726 }
727 }
728
729 // prepare a waveform pattern in the buffer based on the ID given then
730 // simulate a HID tag until the button is pressed
731 void CmdHIDsimTAG(int hi, int lo, int ledcontrol)
732 {
733 int n=0, i=0;
734 /*
735 HID tag bitstream format
736 The tag contains a 44bit unique code. This is sent out MSB first in sets of 4 bits
737 A 1 bit is represented as 6 fc8 and 5 fc10 patterns
738 A 0 bit is represented as 5 fc10 and 6 fc8 patterns
739 A fc8 is inserted before every 4 bits
740 A special start of frame pattern is used consisting a0b0 where a and b are neither 0
741 nor 1 bits, they are special patterns (a = set of 12 fc8 and b = set of 10 fc10)
742 */
743
744 if (hi>0xFFF) {
745 DbpString("Tags can only have 44 bits.");
746 return;
747 }
748 fc(0,&n);
749 // special start of frame marker containing invalid bit sequences
750 fc(8, &n); fc(8, &n); // invalid
751 fc(8, &n); fc(10, &n); // logical 0
752 fc(10, &n); fc(10, &n); // invalid
753 fc(8, &n); fc(10, &n); // logical 0
754
755 WDT_HIT();
756 // manchester encode bits 43 to 32
757 for (i=11; i>=0; i--) {
758 if ((i%4)==3) fc(0,&n);
759 if ((hi>>i)&1) {
760 fc(10, &n); fc(8, &n); // low-high transition
761 } else {
762 fc(8, &n); fc(10, &n); // high-low transition
763 }
764 }
765
766 WDT_HIT();
767 // manchester encode bits 31 to 0
768 for (i=31; i>=0; i--) {
769 if ((i%4)==3) fc(0,&n);
770 if ((lo>>i)&1) {
771 fc(10, &n); fc(8, &n); // low-high transition
772 } else {
773 fc(8, &n); fc(10, &n); // high-low transition
774 }
775 }
776
777 if (ledcontrol)
778 LED_A_ON();
779 SimulateTagLowFrequency(n, ledcontrol);
780
781 if (ledcontrol)
782 LED_A_OFF();
783 }
784
785
786 // loop to capture raw HID waveform then FSK demodulate the TAG ID from it
787 void CmdHIDdemodFSK(int findone, int *high, int *low, int ledcontrol)
788 {
789 BYTE *dest = (BYTE *)BigBuf;
790 int m=0, n=0, i=0, idx=0, found=0, lastval=0;
791 DWORD hi=0, lo=0;
792
793 FpgaSendCommand(FPGA_CMD_SET_DIVISOR, 95); //125Khz
794 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER);
795
796 // Connect the A/D to the peak-detected low-frequency path.
797 SetAdcMuxFor(GPIO_MUXSEL_LOPKD);
798
799 // Give it a bit of time for the resonant antenna to settle.
800 SpinDelay(50);
801
802 // Now set up the SSC to get the ADC samples that are now streaming at us.
803 FpgaSetupSsc();
804
805 for(;;) {
806 WDT_HIT();
807 if (ledcontrol)
808 LED_A_ON();
809 if(BUTTON_PRESS()) {
810 DbpString("Stopped");
811 if (ledcontrol)
812 LED_A_OFF();
813 return;
814 }
815
816 i = 0;
817 m = sizeof(BigBuf);
818 memset(dest,128,m);
819 for(;;) {
820 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
821 AT91C_BASE_SSC->SSC_THR = 0x43;
822 if (ledcontrol)
823 LED_D_ON();
824 }
825 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
826 dest[i] = (BYTE)AT91C_BASE_SSC->SSC_RHR;
827 // we don't care about actual value, only if it's more or less than a
828 // threshold essentially we capture zero crossings for later analysis
829 if(dest[i] < 127) dest[i] = 0; else dest[i] = 1;
830 i++;
831 if (ledcontrol)
832 LED_D_OFF();
833 if(i >= m) {
834 break;
835 }
836 }
837 }
838
839 // FSK demodulator
840
841 // sync to first lo-hi transition
842 for( idx=1; idx<m; idx++) {
843 if (dest[idx-1]<dest[idx])
844 lastval=idx;
845 break;
846 }
847 WDT_HIT();
848
849 // count cycles between consecutive lo-hi transitions, there should be either 8 (fc/8)
850 // or 10 (fc/10) cycles but in practice due to noise etc we may end up with with anywhere
851 // between 7 to 11 cycles so fuzz it by treat anything <9 as 8 and anything else as 10
852 for( i=0; idx<m; idx++) {
853 if (dest[idx-1]<dest[idx]) {
854 dest[i]=idx-lastval;
855 if (dest[i] <= 8) {
856 dest[i]=1;
857 } else {
858 dest[i]=0;
859 }
860
861 lastval=idx;
862 i++;
863 }
864 }
865 m=i;
866 WDT_HIT();
867
868 // we now have a set of cycle counts, loop over previous results and aggregate data into bit patterns
869 lastval=dest[0];
870 idx=0;
871 i=0;
872 n=0;
873 for( idx=0; idx<m; idx++) {
874 if (dest[idx]==lastval) {
875 n++;
876 } else {
877 // a bit time is five fc/10 or six fc/8 cycles so figure out how many bits a pattern width represents,
878 // an extra fc/8 pattern preceeds every 4 bits (about 200 cycles) just to complicate things but it gets
879 // swallowed up by rounding
880 // expected results are 1 or 2 bits, any more and it's an invalid manchester encoding
881 // special start of frame markers use invalid manchester states (no transitions) by using sequences
882 // like 111000
883 if (dest[idx-1]) {
884 n=(n+1)/6; // fc/8 in sets of 6
885 } else {
886 n=(n+1)/5; // fc/10 in sets of 5
887 }
888 switch (n) { // stuff appropriate bits in buffer
889 case 0:
890 case 1: // one bit
891 dest[i++]=dest[idx-1];
892 break;
893 case 2: // two bits
894 dest[i++]=dest[idx-1];
895 dest[i++]=dest[idx-1];
896 break;
897 case 3: // 3 bit start of frame markers
898 dest[i++]=dest[idx-1];
899 dest[i++]=dest[idx-1];
900 dest[i++]=dest[idx-1];
901 break;
902 // When a logic 0 is immediately followed by the start of the next transmisson
903 // (special pattern) a pattern of 4 bit duration lengths is created.
904 case 4:
905 dest[i++]=dest[idx-1];
906 dest[i++]=dest[idx-1];
907 dest[i++]=dest[idx-1];
908 dest[i++]=dest[idx-1];
909 break;
910 default: // this shouldn't happen, don't stuff any bits
911 break;
912 }
913 n=0;
914 lastval=dest[idx];
915 }
916 }
917 m=i;
918 WDT_HIT();
919
920 // final loop, go over previously decoded manchester data and decode into usable tag ID
921 // 111000 bit pattern represent start of frame, 01 pattern represents a 1 and 10 represents a 0
922 for( idx=0; idx<m-6; idx++) {
923 // search for a start of frame marker
924 if ( dest[idx] && dest[idx+1] && dest[idx+2] && (!dest[idx+3]) && (!dest[idx+4]) && (!dest[idx+5]) )
925 {
926 found=1;
927 idx+=6;
928 if (found && (hi|lo)) {
929 DbpString("TAG ID");
930 DbpIntegers(hi, lo, (lo>>1)&0xffff);
931 /* if we're only looking for one tag */
932 if (findone)
933 {
934 *high = hi;
935 *low = lo;
936 return;
937 }
938 hi=0;
939 lo=0;
940 found=0;
941 }
942 }
943 if (found) {
944 if (dest[idx] && (!dest[idx+1]) ) {
945 hi=(hi<<1)|(lo>>31);
946 lo=(lo<<1)|0;
947 } else if ( (!dest[idx]) && dest[idx+1]) {
948 hi=(hi<<1)|(lo>>31);
949 lo=(lo<<1)|1;
950 } else {
951 found=0;
952 hi=0;
953 lo=0;
954 }
955 idx++;
956 }
957 if ( dest[idx] && dest[idx+1] && dest[idx+2] && (!dest[idx+3]) && (!dest[idx+4]) && (!dest[idx+5]) )
958 {
959 found=1;
960 idx+=6;
961 if (found && (hi|lo)) {
962 DbpString("TAG ID");
963 DbpIntegers(hi, lo, (lo>>1)&0xffff);
964 /* if we're only looking for one tag */
965 if (findone)
966 {
967 *high = hi;
968 *low = lo;
969 return;
970 }
971 hi=0;
972 lo=0;
973 found=0;
974 }
975 }
976 }
977 WDT_HIT();
978 }
979 }
Impressum, Datenschutz