1 //-----------------------------------------------------------------------------
2 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
3 // at your option, any later version. See the LICENSE.txt file for the text of
5 //-----------------------------------------------------------------------------
6 // Miscellaneous routines for low frequency tag operations.
7 // Tags supported here so far are Texas Instruments (TI), HID
8 // Also routines for raw mode reading/simulating of LF waveform
9 //-----------------------------------------------------------------------------
11 #include "proxmark3.h"
18 void LFSetupFPGAForADC(int divisor
, bool lf_field
)
20 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
21 if ( (divisor
== 1) || (divisor
< 0) || (divisor
> 255) )
22 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
23 else if (divisor
== 0)
24 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
26 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, divisor
);
28 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC
| (lf_field
? FPGA_LF_ADC_READER_FIELD
: 0));
30 // Connect the A/D to the peak-detected low-frequency path.
31 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
32 // Give it a bit of time for the resonant antenna to settle.
34 // Now set up the SSC to get the ADC samples that are now streaming at us.
38 void AcquireRawAdcSamples125k(int divisor
)
40 LFSetupFPGAForADC(divisor
, true);
41 DoAcquisition125k(-1);
44 void SnoopLFRawAdcSamples(int divisor
, int trigger_threshold
)
46 LFSetupFPGAForADC(divisor
, false);
47 DoAcquisition125k(trigger_threshold
);
50 // split into two routines so we can avoid timing issues after sending commands //
51 void DoAcquisition125k(int trigger_threshold
)
53 uint8_t *dest
= (uint8_t *)BigBuf
;
54 int n
= sizeof(BigBuf
);
60 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
) {
61 AT91C_BASE_SSC
->SSC_THR
= 0x43;
64 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
65 dest
[i
] = (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
67 if (trigger_threshold
!= -1 && dest
[i
] < trigger_threshold
)
70 trigger_threshold
= -1;
74 Dbprintf("buffer samples: %02x %02x %02x %02x %02x %02x %02x %02x ...",
75 dest
[0], dest
[1], dest
[2], dest
[3], dest
[4], dest
[5], dest
[6], dest
[7]);
78 void ModThenAcquireRawAdcSamples125k(int delay_off
, int period_0
, int period_1
, uint8_t *command
)
82 /* Make sure the tag is reset */
83 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
84 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
87 // see if 'h' was specified
88 if (command
[strlen((char *) command
) - 1] == 'h')
94 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
96 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
98 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC
| FPGA_LF_ADC_READER_FIELD
);
100 // Give it a bit of time for the resonant antenna to settle.
102 // And a little more time for the tag to fully power up
105 // Now set up the SSC to get the ADC samples that are now streaming at us.
108 // now modulate the reader field
109 while(*command
!= '\0' && *command
!= ' ') {
110 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
112 SpinDelayUs(delay_off
);
114 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
116 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
118 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC
| FPGA_LF_ADC_READER_FIELD
);
120 if(*(command
++) == '0')
121 SpinDelayUs(period_0
);
123 SpinDelayUs(period_1
);
125 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
127 SpinDelayUs(delay_off
);
129 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
131 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
133 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC
| FPGA_LF_ADC_READER_FIELD
);
136 DoAcquisition125k(-1);
139 /* blank r/w tag data stream
140 ...0000000000000000 01111111
141 1010101010101010101010101010101010101010101010101010101010101010
144 101010101010101[0]000...
146 [5555fe852c5555555555555555fe0000]
150 // some hardcoded initial params
151 // when we read a TI tag we sample the zerocross line at 2Mhz
152 // TI tags modulate a 1 as 16 cycles of 123.2Khz
153 // TI tags modulate a 0 as 16 cycles of 134.2Khz
154 #define FSAMPLE 2000000
155 #define FREQLO 123200
156 #define FREQHI 134200
158 signed char *dest
= (signed char *)BigBuf
;
159 int n
= sizeof(BigBuf
);
160 // int *dest = GraphBuffer;
161 // int n = GraphTraceLen;
163 // 128 bit shift register [shift3:shift2:shift1:shift0]
164 uint32_t shift3
= 0, shift2
= 0, shift1
= 0, shift0
= 0;
166 int i
, cycles
=0, samples
=0;
167 // how many sample points fit in 16 cycles of each frequency
168 uint32_t sampleslo
= (FSAMPLE
<<4)/FREQLO
, sampleshi
= (FSAMPLE
<<4)/FREQHI
;
169 // when to tell if we're close enough to one freq or another
170 uint32_t threshold
= (sampleslo
- sampleshi
+ 1)>>1;
172 // TI tags charge at 134.2Khz
173 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
174 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
176 // Place FPGA in passthrough mode, in this mode the CROSS_LO line
177 // connects to SSP_DIN and the SSP_DOUT logic level controls
178 // whether we're modulating the antenna (high)
179 // or listening to the antenna (low)
180 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_PASSTHRU
);
182 // get TI tag data into the buffer
185 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
187 for (i
=0; i
<n
-1; i
++) {
188 // count cycles by looking for lo to hi zero crossings
189 if ( (dest
[i
]<0) && (dest
[i
+1]>0) ) {
191 // after 16 cycles, measure the frequency
194 samples
=i
-samples
; // number of samples in these 16 cycles
196 // TI bits are coming to us lsb first so shift them
197 // right through our 128 bit right shift register
198 shift0
= (shift0
>>1) | (shift1
<< 31);
199 shift1
= (shift1
>>1) | (shift2
<< 31);
200 shift2
= (shift2
>>1) | (shift3
<< 31);
203 // check if the cycles fall close to the number
204 // expected for either the low or high frequency
205 if ( (samples
>(sampleslo
-threshold
)) && (samples
<(sampleslo
+threshold
)) ) {
206 // low frequency represents a 1
208 } else if ( (samples
>(sampleshi
-threshold
)) && (samples
<(sampleshi
+threshold
)) ) {
209 // high frequency represents a 0
211 // probably detected a gay waveform or noise
212 // use this as gaydar or discard shift register and start again
213 shift3
= shift2
= shift1
= shift0
= 0;
217 // for each bit we receive, test if we've detected a valid tag
219 // if we see 17 zeroes followed by 6 ones, we might have a tag
220 // remember the bits are backwards
221 if ( ((shift0
& 0x7fffff) == 0x7e0000) ) {
222 // if start and end bytes match, we have a tag so break out of the loop
223 if ( ((shift0
>>16)&0xff) == ((shift3
>>8)&0xff) ) {
224 cycles
= 0xF0B; //use this as a flag (ugly but whatever)
232 // if flag is set we have a tag
234 DbpString("Info: No valid tag detected.");
236 // put 64 bit data into shift1 and shift0
237 shift0
= (shift0
>>24) | (shift1
<< 8);
238 shift1
= (shift1
>>24) | (shift2
<< 8);
240 // align 16 bit crc into lower half of shift2
241 shift2
= ((shift2
>>24) | (shift3
<< 8)) & 0x0ffff;
243 // if r/w tag, check ident match
244 if ( shift3
&(1<<15) ) {
245 DbpString("Info: TI tag is rewriteable");
246 // only 15 bits compare, last bit of ident is not valid
247 if ( ((shift3
>>16)^shift0
)&0x7fff ) {
248 DbpString("Error: Ident mismatch!");
250 DbpString("Info: TI tag ident is valid");
253 DbpString("Info: TI tag is readonly");
256 // WARNING the order of the bytes in which we calc crc below needs checking
257 // i'm 99% sure the crc algorithm is correct, but it may need to eat the
258 // bytes in reverse or something
262 crc
= update_crc16(crc
, (shift0
)&0xff);
263 crc
= update_crc16(crc
, (shift0
>>8)&0xff);
264 crc
= update_crc16(crc
, (shift0
>>16)&0xff);
265 crc
= update_crc16(crc
, (shift0
>>24)&0xff);
266 crc
= update_crc16(crc
, (shift1
)&0xff);
267 crc
= update_crc16(crc
, (shift1
>>8)&0xff);
268 crc
= update_crc16(crc
, (shift1
>>16)&0xff);
269 crc
= update_crc16(crc
, (shift1
>>24)&0xff);
271 Dbprintf("Info: Tag data: %x%08x, crc=%x",
272 (unsigned int)shift1
, (unsigned int)shift0
, (unsigned int)shift2
& 0xFFFF);
273 if (crc
!= (shift2
&0xffff)) {
274 Dbprintf("Error: CRC mismatch, expected %x", (unsigned int)crc
);
276 DbpString("Info: CRC is good");
281 void WriteTIbyte(uint8_t b
)
285 // modulate 8 bits out to the antenna
289 // stop modulating antenna
296 // stop modulating antenna
306 void AcquireTiType(void)
309 // tag transmission is <20ms, sampling at 2M gives us 40K samples max
310 // each sample is 1 bit stuffed into a uint32_t so we need 1250 uint32_t
311 #define TIBUFLEN 1250
314 memset(BigBuf
,0,sizeof(BigBuf
));
316 // Set up the synchronous serial port
317 AT91C_BASE_PIOA
->PIO_PDR
= GPIO_SSC_DIN
;
318 AT91C_BASE_PIOA
->PIO_ASR
= GPIO_SSC_DIN
;
320 // steal this pin from the SSP and use it to control the modulation
321 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
322 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
324 AT91C_BASE_SSC
->SSC_CR
= AT91C_SSC_SWRST
;
325 AT91C_BASE_SSC
->SSC_CR
= AT91C_SSC_RXEN
| AT91C_SSC_TXEN
;
327 // Sample at 2 Mbit/s, so TI tags are 16.2 vs. 14.9 clocks long
328 // 48/2 = 24 MHz clock must be divided by 12
329 AT91C_BASE_SSC
->SSC_CMR
= 12;
331 AT91C_BASE_SSC
->SSC_RCMR
= SSC_CLOCK_MODE_SELECT(0);
332 AT91C_BASE_SSC
->SSC_RFMR
= SSC_FRAME_MODE_BITS_IN_WORD(32) | AT91C_SSC_MSBF
;
333 AT91C_BASE_SSC
->SSC_TCMR
= 0;
334 AT91C_BASE_SSC
->SSC_TFMR
= 0;
341 // Charge TI tag for 50ms.
344 // stop modulating antenna and listen
351 if(AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
352 BigBuf
[i
] = AT91C_BASE_SSC
->SSC_RHR
; // store 32 bit values in buffer
353 i
++; if(i
>= TIBUFLEN
) break;
358 // return stolen pin to SSP
359 AT91C_BASE_PIOA
->PIO_PDR
= GPIO_SSC_DOUT
;
360 AT91C_BASE_PIOA
->PIO_ASR
= GPIO_SSC_DIN
| GPIO_SSC_DOUT
;
362 char *dest
= (char *)BigBuf
;
365 for (i
=TIBUFLEN
-1; i
>=0; i
--) {
366 for (j
=0; j
<32; j
++) {
367 if(BigBuf
[i
] & (1 << j
)) {
376 // arguments: 64bit data split into 32bit idhi:idlo and optional 16bit crc
377 // if crc provided, it will be written with the data verbatim (even if bogus)
378 // if not provided a valid crc will be computed from the data and written.
379 void WriteTItag(uint32_t idhi
, uint32_t idlo
, uint16_t crc
)
381 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
383 crc
= update_crc16(crc
, (idlo
)&0xff);
384 crc
= update_crc16(crc
, (idlo
>>8)&0xff);
385 crc
= update_crc16(crc
, (idlo
>>16)&0xff);
386 crc
= update_crc16(crc
, (idlo
>>24)&0xff);
387 crc
= update_crc16(crc
, (idhi
)&0xff);
388 crc
= update_crc16(crc
, (idhi
>>8)&0xff);
389 crc
= update_crc16(crc
, (idhi
>>16)&0xff);
390 crc
= update_crc16(crc
, (idhi
>>24)&0xff);
392 Dbprintf("Writing to tag: %x%08x, crc=%x",
393 (unsigned int) idhi
, (unsigned int) idlo
, crc
);
395 // TI tags charge at 134.2Khz
396 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
397 // Place FPGA in passthrough mode, in this mode the CROSS_LO line
398 // connects to SSP_DIN and the SSP_DOUT logic level controls
399 // whether we're modulating the antenna (high)
400 // or listening to the antenna (low)
401 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_PASSTHRU
);
404 // steal this pin from the SSP and use it to control the modulation
405 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
406 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
408 // writing algorithm:
409 // a high bit consists of a field off for 1ms and field on for 1ms
410 // a low bit consists of a field off for 0.3ms and field on for 1.7ms
411 // initiate a charge time of 50ms (field on) then immediately start writing bits
412 // start by writing 0xBB (keyword) and 0xEB (password)
413 // then write 80 bits of data (or 64 bit data + 16 bit crc if you prefer)
414 // finally end with 0x0300 (write frame)
415 // all data is sent lsb firts
416 // finish with 15ms programming time
420 SpinDelay(50); // charge time
422 WriteTIbyte(0xbb); // keyword
423 WriteTIbyte(0xeb); // password
424 WriteTIbyte( (idlo
)&0xff );
425 WriteTIbyte( (idlo
>>8 )&0xff );
426 WriteTIbyte( (idlo
>>16)&0xff );
427 WriteTIbyte( (idlo
>>24)&0xff );
428 WriteTIbyte( (idhi
)&0xff );
429 WriteTIbyte( (idhi
>>8 )&0xff );
430 WriteTIbyte( (idhi
>>16)&0xff );
431 WriteTIbyte( (idhi
>>24)&0xff ); // data hi to lo
432 WriteTIbyte( (crc
)&0xff ); // crc lo
433 WriteTIbyte( (crc
>>8 )&0xff ); // crc hi
434 WriteTIbyte(0x00); // write frame lo
435 WriteTIbyte(0x03); // write frame hi
437 SpinDelay(50); // programming time
441 // get TI tag data into the buffer
444 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
445 DbpString("Now use tiread to check");
448 void SimulateTagLowFrequency(int period
, int gap
, int ledcontrol
)
451 uint8_t *tab
= (uint8_t *)BigBuf
;
453 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
454 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_EDGE_DETECT
);
456 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
| GPIO_SSC_CLK
;
458 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
459 AT91C_BASE_PIOA
->PIO_ODR
= GPIO_SSC_CLK
;
461 #define SHORT_COIL() LOW(GPIO_SSC_DOUT)
462 #define OPEN_COIL() HIGH(GPIO_SSC_DOUT)
466 while(!(AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_CLK
)) {
468 DbpString("Stopped");
485 while(AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_CLK
) {
487 DbpString("Stopped");
504 #define DEBUG_FRAME_CONTENTS 1
505 void SimulateTagLowFrequencyBidir(int divisor
, int t0
)
509 // compose fc/8 fc/10 waveform
510 static void fc(int c
, int *n
) {
511 uint8_t *dest
= (uint8_t *)BigBuf
;
514 // for when we want an fc8 pattern every 4 logical bits
525 // an fc/8 encoded bit is a bit pattern of 11000000 x6 = 48 samples
527 for (idx
=0; idx
<6; idx
++) {
539 // an fc/10 encoded bit is a bit pattern of 1110000000 x5 = 50 samples
541 for (idx
=0; idx
<5; idx
++) {
556 // prepare a waveform pattern in the buffer based on the ID given then
557 // simulate a HID tag until the button is pressed
558 void CmdHIDsimTAG(int hi
, int lo
, int ledcontrol
)
562 HID tag bitstream format
563 The tag contains a 44bit unique code. This is sent out MSB first in sets of 4 bits
564 A 1 bit is represented as 6 fc8 and 5 fc10 patterns
565 A 0 bit is represented as 5 fc10 and 6 fc8 patterns
566 A fc8 is inserted before every 4 bits
567 A special start of frame pattern is used consisting a0b0 where a and b are neither 0
568 nor 1 bits, they are special patterns (a = set of 12 fc8 and b = set of 10 fc10)
572 DbpString("Tags can only have 44 bits.");
576 // special start of frame marker containing invalid bit sequences
577 fc(8, &n
); fc(8, &n
); // invalid
578 fc(8, &n
); fc(10, &n
); // logical 0
579 fc(10, &n
); fc(10, &n
); // invalid
580 fc(8, &n
); fc(10, &n
); // logical 0
583 // manchester encode bits 43 to 32
584 for (i
=11; i
>=0; i
--) {
585 if ((i
%4)==3) fc(0,&n
);
587 fc(10, &n
); fc(8, &n
); // low-high transition
589 fc(8, &n
); fc(10, &n
); // high-low transition
594 // manchester encode bits 31 to 0
595 for (i
=31; i
>=0; i
--) {
596 if ((i
%4)==3) fc(0,&n
);
598 fc(10, &n
); fc(8, &n
); // low-high transition
600 fc(8, &n
); fc(10, &n
); // high-low transition
606 SimulateTagLowFrequency(n
, 0, ledcontrol
);
613 // loop to capture raw HID waveform then FSK demodulate the TAG ID from it
614 void CmdHIDdemodFSK(int findone
, int *high
, int *low
, int ledcontrol
)
616 uint8_t *dest
= (uint8_t *)BigBuf
;
617 int m
=0, n
=0, i
=0, idx
=0, found
=0, lastval
=0;
618 uint32_t hi2
=0, hi
=0, lo
=0;
620 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
621 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
622 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC
| FPGA_LF_ADC_READER_FIELD
);
624 // Connect the A/D to the peak-detected low-frequency path.
625 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
627 // Give it a bit of time for the resonant antenna to settle.
630 // Now set up the SSC to get the ADC samples that are now streaming at us.
638 DbpString("Stopped");
648 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
649 AT91C_BASE_SSC
->SSC_THR
= 0x43;
653 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
654 dest
[i
] = (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
655 // we don't care about actual value, only if it's more or less than a
656 // threshold essentially we capture zero crossings for later analysis
657 if(dest
[i
] < 127) dest
[i
] = 0; else dest
[i
] = 1;
669 // sync to first lo-hi transition
670 for( idx
=1; idx
<m
; idx
++) {
671 if (dest
[idx
-1]<dest
[idx
])
677 // count cycles between consecutive lo-hi transitions, there should be either 8 (fc/8)
678 // or 10 (fc/10) cycles but in practice due to noise etc we may end up with with anywhere
679 // between 7 to 11 cycles so fuzz it by treat anything <9 as 8 and anything else as 10
680 for( i
=0; idx
<m
; idx
++) {
681 if (dest
[idx
-1]<dest
[idx
]) {
696 // we now have a set of cycle counts, loop over previous results and aggregate data into bit patterns
701 for( idx
=0; idx
<m
; idx
++) {
702 if (dest
[idx
]==lastval
) {
705 // a bit time is five fc/10 or six fc/8 cycles so figure out how many bits a pattern width represents,
706 // an extra fc/8 pattern preceeds every 4 bits (about 200 cycles) just to complicate things but it gets
707 // swallowed up by rounding
708 // expected results are 1 or 2 bits, any more and it's an invalid manchester encoding
709 // special start of frame markers use invalid manchester states (no transitions) by using sequences
712 n
=(n
+1)/6; // fc/8 in sets of 6
714 n
=(n
+1)/5; // fc/10 in sets of 5
716 switch (n
) { // stuff appropriate bits in buffer
719 dest
[i
++]=dest
[idx
-1];
722 dest
[i
++]=dest
[idx
-1];
723 dest
[i
++]=dest
[idx
-1];
725 case 3: // 3 bit start of frame markers
726 dest
[i
++]=dest
[idx
-1];
727 dest
[i
++]=dest
[idx
-1];
728 dest
[i
++]=dest
[idx
-1];
730 // When a logic 0 is immediately followed by the start of the next transmisson
731 // (special pattern) a pattern of 4 bit duration lengths is created.
733 dest
[i
++]=dest
[idx
-1];
734 dest
[i
++]=dest
[idx
-1];
735 dest
[i
++]=dest
[idx
-1];
736 dest
[i
++]=dest
[idx
-1];
738 default: // this shouldn't happen, don't stuff any bits
748 // final loop, go over previously decoded manchester data and decode into usable tag ID
749 // 111000 bit pattern represent start of frame, 01 pattern represents a 1 and 10 represents a 0
750 for( idx
=0; idx
<m
-6; idx
++) {
751 // search for a start of frame marker
752 if ( dest
[idx
] && dest
[idx
+1] && dest
[idx
+2] && (!dest
[idx
+3]) && (!dest
[idx
+4]) && (!dest
[idx
+5]) )
756 if (found
&& (hi2
|hi
|lo
)) {
758 Dbprintf("TAG ID: %x%08x%08x (%d)",
759 (unsigned int) hi2
, (unsigned int) hi
, (unsigned int) lo
, (unsigned int) (lo
>>1) & 0xFFFF);
762 Dbprintf("TAG ID: %x%08x (%d)",
763 (unsigned int) hi
, (unsigned int) lo
, (unsigned int) (lo
>>1) & 0xFFFF);
765 /* if we're only looking for one tag */
779 if (dest
[idx
] && (!dest
[idx
+1]) ) {
780 hi2
=(hi2
<<1)|(hi
>>31);
783 } else if ( (!dest
[idx
]) && dest
[idx
+1]) {
784 hi2
=(hi2
<<1)|(hi
>>31);
795 if ( dest
[idx
] && dest
[idx
+1] && dest
[idx
+2] && (!dest
[idx
+3]) && (!dest
[idx
+4]) && (!dest
[idx
+5]) )
799 if (found
&& (hi
|lo
)) {
801 Dbprintf("TAG ID: %x%08x%08x (%d)",
802 (unsigned int) hi2
, (unsigned int) hi
, (unsigned int) lo
, (unsigned int) (lo
>>1) & 0xFFFF);
805 Dbprintf("TAG ID: %x%08x (%d)",
806 (unsigned int) hi
, (unsigned int) lo
, (unsigned int) (lo
>>1) & 0xFFFF);
808 /* if we're only looking for one tag */
826 void CmdIOdemodFSK(int findone
, int *high
, int *low
, int ledcontrol
)
828 uint8_t *dest
= (uint8_t *)BigBuf
;
829 int m
=0, n
=0, i
=0, idx
=0, lastval
=0;
831 uint32_t code
=0, code2
=0;
832 //uint32_t hi2=0, hi=0, lo=0;
834 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
835 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
836 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC
| FPGA_LF_ADC_READER_FIELD
);
838 // Connect the A/D to the peak-detected low-frequency path.
839 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
841 // Give it a bit of time for the resonant antenna to settle.
844 // Now set up the SSC to get the ADC samples that are now streaming at us.
852 DbpString("Stopped");
862 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
863 AT91C_BASE_SSC
->SSC_THR
= 0x43;
867 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
868 dest
[i
] = (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
869 // we don't care about actual value, only if it's more or less than a
870 // threshold essentially we capture zero crossings for later analysis
871 if(dest
[i
] < 127) dest
[i
] = 0; else dest
[i
] = 1;
883 // sync to first lo-hi transition
884 for( idx
=1; idx
<m
; idx
++) {
885 if (dest
[idx
-1]<dest
[idx
])
891 // count cycles between consecutive lo-hi transitions, there should be either 8 (fc/8)
892 // or 10 (fc/10) cycles but in practice due to noise etc we may end up with with anywhere
893 // between 7 to 11 cycles so fuzz it by treat anything <9 as 8 and anything else as 10
894 for( i
=0; idx
<m
; idx
++) {
895 if (dest
[idx
-1]<dest
[idx
]) {
910 // we now have a set of cycle counts, loop over previous results and aggregate data into bit patterns
915 for( idx
=0; idx
<m
; idx
++) {
916 if (dest
[idx
]==lastval
) {
919 // a bit time is five fc/10 or six fc/8 cycles so figure out how many bits a pattern width represents,
920 // an extra fc/8 pattern preceeds every 4 bits (about 200 cycles) just to complicate things but it gets
921 // swallowed up by rounding
922 // expected results are 1 or 2 bits, any more and it's an invalid manchester encoding
923 // special start of frame markers use invalid manchester states (no transitions) by using sequences
926 n
=(n
+1)/7; // fc/8 in sets of 7
928 n
=(n
+1)/6; // fc/10 in sets of 6
931 for(int j
=0; j
<n
; j
++){
932 dest
[i
++]=dest
[idx
-1]^1;
942 uint8_t mask
[] = {0,0,0,0,0,0,0,0,0,1};
943 for( idx
=0; idx
< m
- 64; idx
++) {
945 if ( memcmp(dest
+ idx
, mask
, sizeof(mask
)) ) continue;
948 Dbprintf("%d%d%d%d%d%d%d%d",dest
[idx
], dest
[idx
+1], dest
[idx
+2],dest
[idx
+3],dest
[idx
+4],dest
[idx
+5],dest
[idx
+6],dest
[idx
+7]);
949 Dbprintf("%d%d%d%d%d%d%d%d",dest
[idx
+8], dest
[idx
+9], dest
[idx
+10],dest
[idx
+11],dest
[idx
+12],dest
[idx
+13],dest
[idx
+14],dest
[idx
+15]);
950 Dbprintf("%d%d%d%d%d%d%d%d",dest
[idx
+16],dest
[idx
+17],dest
[idx
+18],dest
[idx
+19],dest
[idx
+20],dest
[idx
+21],dest
[idx
+22],dest
[idx
+23]);
951 Dbprintf("%d%d%d%d%d%d%d%d",dest
[idx
+24],dest
[idx
+25],dest
[idx
+26],dest
[idx
+27],dest
[idx
+28],dest
[idx
+29],dest
[idx
+30],dest
[idx
+31]);
952 Dbprintf("%d%d%d%d%d%d%d%d",dest
[idx
+32],dest
[idx
+33],dest
[idx
+34],dest
[idx
+35],dest
[idx
+36],dest
[idx
+37],dest
[idx
+38],dest
[idx
+39]);
953 Dbprintf("%d%d%d%d%d%d%d%d",dest
[idx
+40],dest
[idx
+41],dest
[idx
+42],dest
[idx
+43],dest
[idx
+44],dest
[idx
+45],dest
[idx
+46],dest
[idx
+47]);
954 Dbprintf("%d%d%d%d%d%d%d%d",dest
[idx
+48],dest
[idx
+49],dest
[idx
+50],dest
[idx
+51],dest
[idx
+52],dest
[idx
+53],dest
[idx
+54],dest
[idx
+55]);
955 Dbprintf("%d%d%d%d%d%d%d%d",dest
[idx
+56],dest
[idx
+57],dest
[idx
+58],dest
[idx
+59],dest
[idx
+60],dest
[idx
+61],dest
[idx
+62],dest
[idx
+63]);
957 short version
='\x00';
960 for(int j
=14;j
<18;j
++){
961 //Dbprintf("%d",dest[idx+j]);
963 if (dest
[idx
+j
]) version
|= 1;
965 for(int j
=18;j
<26;j
++){
966 //Dbprintf("%d",dest[idx+j]);
968 if (dest
[idx
+j
]) unknown
|= 1;
970 for(int j
=36;j
<45;j
++){
971 //Dbprintf("%d",dest[idx+j]);
973 if (dest
[idx
+j
]) number
|= 1;
975 for(int j
=46;j
<53;j
++){
976 //Dbprintf("%d",dest[idx+j]);
978 if (dest
[idx
+j
]) number
|= 1;
980 for(int j
=0; j
<32; j
++){
982 if(dest
[idx
+j
]) code
|= 1;
984 for(int j
=32; j
<64; j
++){
986 if(dest
[idx
+j
]) code2
|= 1;
989 Dbprintf("XSF(%02d)%02x:%d (%08x%08x)",version
,unknown
,number
,code
,code2
);
993 // if we're only looking for one tag
1006 /*------------------------------
1007 * T5555/T5557/T5567 routines
1008 *------------------------------
1011 /* T55x7 configuration register definitions */
1012 #define T55x7_POR_DELAY 0x00000001
1013 #define T55x7_ST_TERMINATOR 0x00000008
1014 #define T55x7_PWD 0x00000010
1015 #define T55x7_MAXBLOCK_SHIFT 5
1016 #define T55x7_AOR 0x00000200
1017 #define T55x7_PSKCF_RF_2 0
1018 #define T55x7_PSKCF_RF_4 0x00000400
1019 #define T55x7_PSKCF_RF_8 0x00000800
1020 #define T55x7_MODULATION_DIRECT 0
1021 #define T55x7_MODULATION_PSK1 0x00001000
1022 #define T55x7_MODULATION_PSK2 0x00002000
1023 #define T55x7_MODULATION_PSK3 0x00003000
1024 #define T55x7_MODULATION_FSK1 0x00004000
1025 #define T55x7_MODULATION_FSK2 0x00005000
1026 #define T55x7_MODULATION_FSK1a 0x00006000
1027 #define T55x7_MODULATION_FSK2a 0x00007000
1028 #define T55x7_MODULATION_MANCHESTER 0x00008000
1029 #define T55x7_MODULATION_BIPHASE 0x00010000
1030 #define T55x7_BITRATE_RF_8 0
1031 #define T55x7_BITRATE_RF_16 0x00040000
1032 #define T55x7_BITRATE_RF_32 0x00080000
1033 #define T55x7_BITRATE_RF_40 0x000C0000
1034 #define T55x7_BITRATE_RF_50 0x00100000
1035 #define T55x7_BITRATE_RF_64 0x00140000
1036 #define T55x7_BITRATE_RF_100 0x00180000
1037 #define T55x7_BITRATE_RF_128 0x001C0000
1039 /* T5555 (Q5) configuration register definitions */
1040 #define T5555_ST_TERMINATOR 0x00000001
1041 #define T5555_MAXBLOCK_SHIFT 0x00000001
1042 #define T5555_MODULATION_MANCHESTER 0
1043 #define T5555_MODULATION_PSK1 0x00000010
1044 #define T5555_MODULATION_PSK2 0x00000020
1045 #define T5555_MODULATION_PSK3 0x00000030
1046 #define T5555_MODULATION_FSK1 0x00000040
1047 #define T5555_MODULATION_FSK2 0x00000050
1048 #define T5555_MODULATION_BIPHASE 0x00000060
1049 #define T5555_MODULATION_DIRECT 0x00000070
1050 #define T5555_INVERT_OUTPUT 0x00000080
1051 #define T5555_PSK_RF_2 0
1052 #define T5555_PSK_RF_4 0x00000100
1053 #define T5555_PSK_RF_8 0x00000200
1054 #define T5555_USE_PWD 0x00000400
1055 #define T5555_USE_AOR 0x00000800
1056 #define T5555_BITRATE_SHIFT 12
1057 #define T5555_FAST_WRITE 0x00004000
1058 #define T5555_PAGE_SELECT 0x00008000
1061 * Relevant times in microsecond
1062 * To compensate antenna falling times shorten the write times
1063 * and enlarge the gap ones.
1065 #define START_GAP 250
1066 #define WRITE_GAP 160
1067 #define WRITE_0 144 // 192
1068 #define WRITE_1 400 // 432 for T55x7; 448 for E5550
1070 // Write one bit to card
1071 void T55xxWriteBit(int bit
)
1073 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
1074 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1075 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC
| FPGA_LF_ADC_READER_FIELD
);
1077 SpinDelayUs(WRITE_0
);
1079 SpinDelayUs(WRITE_1
);
1080 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1081 SpinDelayUs(WRITE_GAP
);
1084 // Write one card block in page 0, no lock
1085 void T55xxWriteBlock(uint32_t Data
, uint32_t Block
, uint32_t Pwd
, uint8_t PwdMode
)
1089 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
1090 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1091 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC
| FPGA_LF_ADC_READER_FIELD
);
1093 // Give it a bit of time for the resonant antenna to settle.
1094 // And for the tag to fully power up
1097 // Now start writting
1098 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1099 SpinDelayUs(START_GAP
);
1103 T55xxWriteBit(0); //Page 0
1106 for (i
= 0x80000000; i
!= 0; i
>>= 1)
1107 T55xxWriteBit(Pwd
& i
);
1113 for (i
= 0x80000000; i
!= 0; i
>>= 1)
1114 T55xxWriteBit(Data
& i
);
1117 for (i
= 0x04; i
!= 0; i
>>= 1)
1118 T55xxWriteBit(Block
& i
);
1120 // Now perform write (nominal is 5.6 ms for T55x7 and 18ms for E5550,
1121 // so wait a little more)
1122 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1123 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC
| FPGA_LF_ADC_READER_FIELD
);
1125 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1128 // Read one card block in page 0
1129 void T55xxReadBlock(uint32_t Block
, uint32_t Pwd
, uint8_t PwdMode
)
1131 uint8_t *dest
= (uint8_t *)BigBuf
;
1134 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
1136 // Clear destination buffer before sending the command
1137 memset(dest
, 128, m
);
1138 // Connect the A/D to the peak-detected low-frequency path.
1139 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
1140 // Now set up the SSC to get the ADC samples that are now streaming at us.
1144 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1145 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC
| FPGA_LF_ADC_READER_FIELD
);
1147 // Give it a bit of time for the resonant antenna to settle.
1148 // And for the tag to fully power up
1151 // Now start writting
1152 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1153 SpinDelayUs(START_GAP
);
1157 T55xxWriteBit(0); //Page 0
1160 for (i
= 0x80000000; i
!= 0; i
>>= 1)
1161 T55xxWriteBit(Pwd
& i
);
1166 for (i
= 0x04; i
!= 0; i
>>= 1)
1167 T55xxWriteBit(Block
& i
);
1169 // Turn field on to read the response
1170 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1171 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC
| FPGA_LF_ADC_READER_FIELD
);
1173 // Now do the acquisition
1176 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
) {
1177 AT91C_BASE_SSC
->SSC_THR
= 0x43;
1179 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
1180 dest
[i
] = (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
1181 // we don't care about actual value, only if it's more or less than a
1182 // threshold essentially we capture zero crossings for later analysis
1183 // if(dest[i] < 127) dest[i] = 0; else dest[i] = 1;
1189 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); // field off
1194 // Read card traceability data (page 1)
1195 void T55xxReadTrace(void){
1196 uint8_t *dest
= (uint8_t *)BigBuf
;
1199 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
1201 // Clear destination buffer before sending the command
1202 memset(dest
, 128, m
);
1203 // Connect the A/D to the peak-detected low-frequency path.
1204 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
1205 // Now set up the SSC to get the ADC samples that are now streaming at us.
1209 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1210 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC
| FPGA_LF_ADC_READER_FIELD
);
1212 // Give it a bit of time for the resonant antenna to settle.
1213 // And for the tag to fully power up
1216 // Now start writting
1217 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1218 SpinDelayUs(START_GAP
);
1222 T55xxWriteBit(1); //Page 1
1224 // Turn field on to read the response
1225 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1226 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC
| FPGA_LF_ADC_READER_FIELD
);
1228 // Now do the acquisition
1231 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
) {
1232 AT91C_BASE_SSC
->SSC_THR
= 0x43;
1234 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
1235 dest
[i
] = (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
1241 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); // field off
1246 /*-------------- Cloning routines -----------*/
1247 // Copy HID id to card and setup block 0 config
1248 void CopyHIDtoT55x7(uint32_t hi2
, uint32_t hi
, uint32_t lo
, uint8_t longFMT
)
1250 int data1
=0, data2
=0, data3
=0, data4
=0, data5
=0, data6
=0; //up to six blocks for long format
1254 // Ensure no more than 84 bits supplied
1256 DbpString("Tags can only have 84 bits.");
1259 // Build the 6 data blocks for supplied 84bit ID
1261 data1
= 0x1D96A900; // load preamble (1D) & long format identifier (9E manchester encoded)
1262 for (int i
=0;i
<4;i
++) {
1263 if (hi2
& (1<<(19-i
)))
1264 data1
|= (1<<(((3-i
)*2)+1)); // 1 -> 10
1266 data1
|= (1<<((3-i
)*2)); // 0 -> 01
1270 for (int i
=0;i
<16;i
++) {
1271 if (hi2
& (1<<(15-i
)))
1272 data2
|= (1<<(((15-i
)*2)+1)); // 1 -> 10
1274 data2
|= (1<<((15-i
)*2)); // 0 -> 01
1278 for (int i
=0;i
<16;i
++) {
1279 if (hi
& (1<<(31-i
)))
1280 data3
|= (1<<(((15-i
)*2)+1)); // 1 -> 10
1282 data3
|= (1<<((15-i
)*2)); // 0 -> 01
1286 for (int i
=0;i
<16;i
++) {
1287 if (hi
& (1<<(15-i
)))
1288 data4
|= (1<<(((15-i
)*2)+1)); // 1 -> 10
1290 data4
|= (1<<((15-i
)*2)); // 0 -> 01
1294 for (int i
=0;i
<16;i
++) {
1295 if (lo
& (1<<(31-i
)))
1296 data5
|= (1<<(((15-i
)*2)+1)); // 1 -> 10
1298 data5
|= (1<<((15-i
)*2)); // 0 -> 01
1302 for (int i
=0;i
<16;i
++) {
1303 if (lo
& (1<<(15-i
)))
1304 data6
|= (1<<(((15-i
)*2)+1)); // 1 -> 10
1306 data6
|= (1<<((15-i
)*2)); // 0 -> 01
1310 // Ensure no more than 44 bits supplied
1312 DbpString("Tags can only have 44 bits.");
1316 // Build the 3 data blocks for supplied 44bit ID
1319 data1
= 0x1D000000; // load preamble
1321 for (int i
=0;i
<12;i
++) {
1322 if (hi
& (1<<(11-i
)))
1323 data1
|= (1<<(((11-i
)*2)+1)); // 1 -> 10
1325 data1
|= (1<<((11-i
)*2)); // 0 -> 01
1329 for (int i
=0;i
<16;i
++) {
1330 if (lo
& (1<<(31-i
)))
1331 data2
|= (1<<(((15-i
)*2)+1)); // 1 -> 10
1333 data2
|= (1<<((15-i
)*2)); // 0 -> 01
1337 for (int i
=0;i
<16;i
++) {
1338 if (lo
& (1<<(15-i
)))
1339 data3
|= (1<<(((15-i
)*2)+1)); // 1 -> 10
1341 data3
|= (1<<((15-i
)*2)); // 0 -> 01
1346 // Program the data blocks for supplied ID
1347 // and the block 0 for HID format
1348 T55xxWriteBlock(data1
,1,0,0);
1349 T55xxWriteBlock(data2
,2,0,0);
1350 T55xxWriteBlock(data3
,3,0,0);
1352 if (longFMT
) { // if long format there are 6 blocks
1353 T55xxWriteBlock(data4
,4,0,0);
1354 T55xxWriteBlock(data5
,5,0,0);
1355 T55xxWriteBlock(data6
,6,0,0);
1358 // Config for HID (RF/50, FSK2a, Maxblock=3 for short/6 for long)
1359 T55xxWriteBlock(T55x7_BITRATE_RF_50
|
1360 T55x7_MODULATION_FSK2a
|
1361 last_block
<< T55x7_MAXBLOCK_SHIFT
,
1369 void CopyIOtoT55x7(uint32_t hi
, uint32_t lo
, uint8_t longFMT
)
1371 int data1
=0, data2
=0; //up to six blocks for long format
1373 data1
= hi
; // load preamble
1377 // Program the data blocks for supplied ID
1378 // and the block 0 for HID format
1379 T55xxWriteBlock(data1
,1,0,0);
1380 T55xxWriteBlock(data2
,2,0,0);
1383 T55xxWriteBlock(0x00147040,0,0,0);
1389 // Define 9bit header for EM410x tags
1390 #define EM410X_HEADER 0x1FF
1391 #define EM410X_ID_LENGTH 40
1393 void WriteEM410x(uint32_t card
, uint32_t id_hi
, uint32_t id_lo
)
1396 uint64_t id
= EM410X_HEADER
;
1397 uint64_t rev_id
= 0; // reversed ID
1398 int c_parity
[4]; // column parity
1399 int r_parity
= 0; // row parity
1402 // Reverse ID bits given as parameter (for simpler operations)
1403 for (i
= 0; i
< EM410X_ID_LENGTH
; ++i
) {
1405 rev_id
= (rev_id
<< 1) | (id_lo
& 1);
1408 rev_id
= (rev_id
<< 1) | (id_hi
& 1);
1413 for (i
= 0; i
< EM410X_ID_LENGTH
; ++i
) {
1414 id_bit
= rev_id
& 1;
1417 // Don't write row parity bit at start of parsing
1419 id
= (id
<< 1) | r_parity
;
1420 // Start counting parity for new row
1427 // First elements in column?
1429 // Fill out first elements
1430 c_parity
[i
] = id_bit
;
1432 // Count column parity
1433 c_parity
[i
% 4] ^= id_bit
;
1436 id
= (id
<< 1) | id_bit
;
1440 // Insert parity bit of last row
1441 id
= (id
<< 1) | r_parity
;
1443 // Fill out column parity at the end of tag
1444 for (i
= 0; i
< 4; ++i
)
1445 id
= (id
<< 1) | c_parity
[i
];
1450 Dbprintf("Started writing %s tag ...", card
? "T55x7":"T5555");
1454 T55xxWriteBlock((uint32_t)(id
>> 32), 1, 0, 0);
1455 T55xxWriteBlock((uint32_t)id
, 2, 0, 0);
1457 // Config for EM410x (RF/64, Manchester, Maxblock=2)
1459 // Clock rate is stored in bits 8-15 of the card value
1460 clock
= (card
& 0xFF00) >> 8;
1461 Dbprintf("Clock rate: %d", clock
);
1465 clock
= T55x7_BITRATE_RF_32
;
1468 clock
= T55x7_BITRATE_RF_16
;
1471 // A value of 0 is assumed to be 64 for backwards-compatibility
1474 clock
= T55x7_BITRATE_RF_64
;
1477 Dbprintf("Invalid clock rate: %d", clock
);
1481 // Writing configuration for T55x7 tag
1482 T55xxWriteBlock(clock
|
1483 T55x7_MODULATION_MANCHESTER
|
1484 2 << T55x7_MAXBLOCK_SHIFT
,
1488 // Writing configuration for T5555(Q5) tag
1489 T55xxWriteBlock(0x1F << T5555_BITRATE_SHIFT
|
1490 T5555_MODULATION_MANCHESTER
|
1491 2 << T5555_MAXBLOCK_SHIFT
,
1495 Dbprintf("Tag %s written with 0x%08x%08x\n", card
? "T55x7":"T5555",
1496 (uint32_t)(id
>> 32), (uint32_t)id
);
1499 // Clone Indala 64-bit tag by UID to T55x7
1500 void CopyIndala64toT55x7(int hi
, int lo
)
1503 //Program the 2 data blocks for supplied 64bit UID
1504 // and the block 0 for Indala64 format
1505 T55xxWriteBlock(hi
,1,0,0);
1506 T55xxWriteBlock(lo
,2,0,0);
1507 //Config for Indala (RF/32;PSK1 with RF/2;Maxblock=2)
1508 T55xxWriteBlock(T55x7_BITRATE_RF_32
|
1509 T55x7_MODULATION_PSK1
|
1510 2 << T55x7_MAXBLOCK_SHIFT
,
1512 //Alternative config for Indala (Extended mode;RF/32;PSK1 with RF/2;Maxblock=2;Inverse data)
1513 // T5567WriteBlock(0x603E1042,0);
1519 void CopyIndala224toT55x7(int uid1
, int uid2
, int uid3
, int uid4
, int uid5
, int uid6
, int uid7
)
1522 //Program the 7 data blocks for supplied 224bit UID
1523 // and the block 0 for Indala224 format
1524 T55xxWriteBlock(uid1
,1,0,0);
1525 T55xxWriteBlock(uid2
,2,0,0);
1526 T55xxWriteBlock(uid3
,3,0,0);
1527 T55xxWriteBlock(uid4
,4,0,0);
1528 T55xxWriteBlock(uid5
,5,0,0);
1529 T55xxWriteBlock(uid6
,6,0,0);
1530 T55xxWriteBlock(uid7
,7,0,0);
1531 //Config for Indala (RF/32;PSK1 with RF/2;Maxblock=7)
1532 T55xxWriteBlock(T55x7_BITRATE_RF_32
|
1533 T55x7_MODULATION_PSK1
|
1534 7 << T55x7_MAXBLOCK_SHIFT
,
1536 //Alternative config for Indala (Extended mode;RF/32;PSK1 with RF/2;Maxblock=7;Inverse data)
1537 // T5567WriteBlock(0x603E10E2,0);
1544 #define abs(x) ( ((x)<0) ? -(x) : (x) )
1545 #define max(x,y) ( x<y ? y:x)
1547 int DemodPCF7931(uint8_t **outBlocks
) {
1548 uint8_t BitStream
[256];
1549 uint8_t Blocks
[8][16];
1550 uint8_t *GraphBuffer
= (uint8_t *)BigBuf
;
1551 int GraphTraceLen
= sizeof(BigBuf
);
1552 int i
, j
, lastval
, bitidx
, half_switch
;
1554 int tolerance
= clock
/ 8;
1555 int pmc
, block_done
;
1556 int lc
, warnings
= 0;
1558 int lmin
=128, lmax
=128;
1561 AcquireRawAdcSamples125k(0);
1568 /* Find first local max/min */
1569 if(GraphBuffer
[1] > GraphBuffer
[0]) {
1570 while(i
< GraphTraceLen
) {
1571 if( !(GraphBuffer
[i
] > GraphBuffer
[i
-1]) && GraphBuffer
[i
] > lmax
)
1578 while(i
< GraphTraceLen
) {
1579 if( !(GraphBuffer
[i
] < GraphBuffer
[i
-1]) && GraphBuffer
[i
] < lmin
)
1591 for (bitidx
= 0; i
< GraphTraceLen
; i
++)
1593 if ( (GraphBuffer
[i
-1] > GraphBuffer
[i
] && dir
== 1 && GraphBuffer
[i
] > lmax
) || (GraphBuffer
[i
-1] < GraphBuffer
[i
] && dir
== 0 && GraphBuffer
[i
] < lmin
))
1598 // Switch depending on lc length:
1599 // Tolerance is 1/8 of clock rate (arbitrary)
1600 if (abs(lc
-clock
/4) < tolerance
) {
1602 if((i
- pmc
) == lc
) { /* 16T0 was previous one */
1604 i
+= (128+127+16+32+33+16)-1;
1612 } else if (abs(lc
-clock
/2) < tolerance
) {
1614 if((i
- pmc
) == lc
) { /* 16T0 was previous one */
1616 i
+= (128+127+16+32+33)-1;
1621 else if(half_switch
== 1) {
1622 BitStream
[bitidx
++] = 0;
1627 } else if (abs(lc
-clock
) < tolerance
) {
1629 BitStream
[bitidx
++] = 1;
1635 Dbprintf("Error: too many detection errors, aborting.");
1640 if(block_done
== 1) {
1642 for(j
=0; j
<16; j
++) {
1643 Blocks
[num_blocks
][j
] = 128*BitStream
[j
*8+7]+
1644 64*BitStream
[j
*8+6]+
1645 32*BitStream
[j
*8+5]+
1646 16*BitStream
[j
*8+4]+
1658 if (GraphBuffer
[i
-1] > GraphBuffer
[i
]) dir
=0;
1664 if(num_blocks
== 4) break;
1666 memcpy(outBlocks
, Blocks
, 16*num_blocks
);
1670 int IsBlock0PCF7931(uint8_t *Block
) {
1671 // Assume RFU means 0 :)
1672 if((memcmp(Block
, "\x00\x00\x00\x00\x00\x00\x00\x01", 8) == 0) && memcmp(Block
+9, "\x00\x00\x00\x00\x00\x00\x00", 7) == 0) // PAC enabled
1674 if((memcmp(Block
+9, "\x00\x00\x00\x00\x00\x00\x00", 7) == 0) && Block
[7] == 0) // PAC disabled, can it *really* happen ?
1679 int IsBlock1PCF7931(uint8_t *Block
) {
1680 // Assume RFU means 0 :)
1681 if(Block
[10] == 0 && Block
[11] == 0 && Block
[12] == 0 && Block
[13] == 0)
1682 if((Block
[14] & 0x7f) <= 9 && Block
[15] <= 9)
1690 void ReadPCF7931() {
1691 uint8_t Blocks
[8][17];
1692 uint8_t tmpBlocks
[4][16];
1693 int i
, j
, ind
, ind2
, n
;
1700 memset(Blocks
, 0, 8*17*sizeof(uint8_t));
1703 memset(tmpBlocks
, 0, 4*16*sizeof(uint8_t));
1704 n
= DemodPCF7931((uint8_t**)tmpBlocks
);
1707 if(error
==10 && num_blocks
== 0) {
1708 Dbprintf("Error, no tag or bad tag");
1711 else if (tries
==20 || error
==10) {
1712 Dbprintf("Error reading the tag");
1713 Dbprintf("Here is the partial content");
1718 Dbprintf("(dbg) %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
1719 tmpBlocks
[i
][0], tmpBlocks
[i
][1], tmpBlocks
[i
][2], tmpBlocks
[i
][3], tmpBlocks
[i
][4], tmpBlocks
[i
][5], tmpBlocks
[i
][6], tmpBlocks
[i
][7],
1720 tmpBlocks
[i
][8], tmpBlocks
[i
][9], tmpBlocks
[i
][10], tmpBlocks
[i
][11], tmpBlocks
[i
][12], tmpBlocks
[i
][13], tmpBlocks
[i
][14], tmpBlocks
[i
][15]);
1722 for(i
=0; i
<n
; i
++) {
1723 if(IsBlock0PCF7931(tmpBlocks
[i
])) {
1725 if(i
< n
-1 && IsBlock1PCF7931(tmpBlocks
[i
+1])) {
1729 memcpy(Blocks
[0], tmpBlocks
[i
], 16);
1730 Blocks
[0][ALLOC
] = 1;
1731 memcpy(Blocks
[1], tmpBlocks
[i
+1], 16);
1732 Blocks
[1][ALLOC
] = 1;
1733 max_blocks
= max((Blocks
[1][14] & 0x7f), Blocks
[1][15]) + 1;
1735 Dbprintf("(dbg) Max blocks: %d", max_blocks
);
1737 // Handle following blocks
1738 for(j
=i
+2, ind2
=2; j
!=i
; j
++, ind2
++, num_blocks
++) {
1741 memcpy(Blocks
[ind2
], tmpBlocks
[j
], 16);
1742 Blocks
[ind2
][ALLOC
] = 1;
1750 for(i
=0; i
<n
; i
++) { // Look for identical block in known blocks
1751 if(memcmp(tmpBlocks
[i
], "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16)) { // Block is not full of 00
1752 for(j
=0; j
<max_blocks
; j
++) {
1753 if(Blocks
[j
][ALLOC
] == 1 && !memcmp(tmpBlocks
[i
], Blocks
[j
], 16)) {
1754 // Found an identical block
1755 for(ind
=i
-1,ind2
=j
-1; ind
>= 0; ind
--,ind2
--) {
1758 if(!Blocks
[ind2
][ALLOC
]) { // Block ind2 not already found
1759 // Dbprintf("Tmp %d -> Block %d", ind, ind2);
1760 memcpy(Blocks
[ind2
], tmpBlocks
[ind
], 16);
1761 Blocks
[ind2
][ALLOC
] = 1;
1763 if(num_blocks
== max_blocks
) goto end
;
1766 for(ind
=i
+1,ind2
=j
+1; ind
< n
; ind
++,ind2
++) {
1767 if(ind2
> max_blocks
)
1769 if(!Blocks
[ind2
][ALLOC
]) { // Block ind2 not already found
1770 // Dbprintf("Tmp %d -> Block %d", ind, ind2);
1771 memcpy(Blocks
[ind2
], tmpBlocks
[ind
], 16);
1772 Blocks
[ind2
][ALLOC
] = 1;
1774 if(num_blocks
== max_blocks
) goto end
;
1783 if (BUTTON_PRESS()) return;
1784 } while (num_blocks
!= max_blocks
);
1786 Dbprintf("-----------------------------------------");
1787 Dbprintf("Memory content:");
1788 Dbprintf("-----------------------------------------");
1789 for(i
=0; i
<max_blocks
; i
++) {
1790 if(Blocks
[i
][ALLOC
]==1)
1791 Dbprintf("%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
1792 Blocks
[i
][0], Blocks
[i
][1], Blocks
[i
][2], Blocks
[i
][3], Blocks
[i
][4], Blocks
[i
][5], Blocks
[i
][6], Blocks
[i
][7],
1793 Blocks
[i
][8], Blocks
[i
][9], Blocks
[i
][10], Blocks
[i
][11], Blocks
[i
][12], Blocks
[i
][13], Blocks
[i
][14], Blocks
[i
][15]);
1795 Dbprintf("<missing block %d>", i
);
1797 Dbprintf("-----------------------------------------");
1803 //-----------------------------------
1804 // EM4469 / EM4305 routines
1805 //-----------------------------------
1806 #define FWD_CMD_LOGIN 0xC //including the even parity, binary mirrored
1807 #define FWD_CMD_WRITE 0xA
1808 #define FWD_CMD_READ 0x9
1809 #define FWD_CMD_DISABLE 0x5
1812 uint8_t forwardLink_data
[64]; //array of forwarded bits
1813 uint8_t * forward_ptr
; //ptr for forward message preparation
1814 uint8_t fwd_bit_sz
; //forwardlink bit counter
1815 uint8_t * fwd_write_ptr
; //forwardlink bit pointer
1817 //====================================================================
1818 // prepares command bits
1820 //====================================================================
1821 //--------------------------------------------------------------------
1822 uint8_t Prepare_Cmd( uint8_t cmd
) {
1823 //--------------------------------------------------------------------
1825 *forward_ptr
++ = 0; //start bit
1826 *forward_ptr
++ = 0; //second pause for 4050 code
1828 *forward_ptr
++ = cmd
;
1830 *forward_ptr
++ = cmd
;
1832 *forward_ptr
++ = cmd
;
1834 *forward_ptr
++ = cmd
;
1836 return 6; //return number of emited bits
1839 //====================================================================
1840 // prepares address bits
1842 //====================================================================
1844 //--------------------------------------------------------------------
1845 uint8_t Prepare_Addr( uint8_t addr
) {
1846 //--------------------------------------------------------------------
1848 register uint8_t line_parity
;
1853 *forward_ptr
++ = addr
;
1854 line_parity
^= addr
;
1858 *forward_ptr
++ = (line_parity
& 1);
1860 return 7; //return number of emited bits
1863 //====================================================================
1864 // prepares data bits intreleaved with parity bits
1866 //====================================================================
1868 //--------------------------------------------------------------------
1869 uint8_t Prepare_Data( uint16_t data_low
, uint16_t data_hi
) {
1870 //--------------------------------------------------------------------
1872 register uint8_t line_parity
;
1873 register uint8_t column_parity
;
1874 register uint8_t i
, j
;
1875 register uint16_t data
;
1880 for(i
=0; i
<4; i
++) {
1882 for(j
=0; j
<8; j
++) {
1883 line_parity
^= data
;
1884 column_parity
^= (data
& 1) << j
;
1885 *forward_ptr
++ = data
;
1888 *forward_ptr
++ = line_parity
;
1893 for(j
=0; j
<8; j
++) {
1894 *forward_ptr
++ = column_parity
;
1895 column_parity
>>= 1;
1899 return 45; //return number of emited bits
1902 //====================================================================
1903 // Forward Link send function
1904 // Requires: forwarLink_data filled with valid bits (1 bit per byte)
1905 // fwd_bit_count set with number of bits to be sent
1906 //====================================================================
1907 void SendForward(uint8_t fwd_bit_count
) {
1909 fwd_write_ptr
= forwardLink_data
;
1910 fwd_bit_sz
= fwd_bit_count
;
1915 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
1916 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1917 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC
| FPGA_LF_ADC_READER_FIELD
);
1919 // Give it a bit of time for the resonant antenna to settle.
1920 // And for the tag to fully power up
1923 // force 1st mod pulse (start gap must be longer for 4305)
1924 fwd_bit_sz
--; //prepare next bit modulation
1926 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); // field off
1927 SpinDelayUs(55*8); //55 cycles off (8us each)for 4305
1928 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1929 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC
| FPGA_LF_ADC_READER_FIELD
);//field on
1930 SpinDelayUs(16*8); //16 cycles on (8us each)
1932 // now start writting
1933 while(fwd_bit_sz
-- > 0) { //prepare next bit modulation
1934 if(((*fwd_write_ptr
++) & 1) == 1)
1935 SpinDelayUs(32*8); //32 cycles at 125Khz (8us each)
1937 //These timings work for 4469/4269/4305 (with the 55*8 above)
1938 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); // field off
1939 SpinDelayUs(23*8); //16-4 cycles off (8us each)
1940 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1941 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC
| FPGA_LF_ADC_READER_FIELD
);//field on
1942 SpinDelayUs(9*8); //16 cycles on (8us each)
1947 void EM4xLogin(uint32_t Password
) {
1949 uint8_t fwd_bit_count
;
1951 forward_ptr
= forwardLink_data
;
1952 fwd_bit_count
= Prepare_Cmd( FWD_CMD_LOGIN
);
1953 fwd_bit_count
+= Prepare_Data( Password
&0xFFFF, Password
>>16 );
1955 SendForward(fwd_bit_count
);
1957 //Wait for command to complete
1962 void EM4xReadWord(uint8_t Address
, uint32_t Pwd
, uint8_t PwdMode
) {
1964 uint8_t fwd_bit_count
;
1965 uint8_t *dest
= (uint8_t *)BigBuf
;
1968 //If password mode do login
1969 if (PwdMode
== 1) EM4xLogin(Pwd
);
1971 forward_ptr
= forwardLink_data
;
1972 fwd_bit_count
= Prepare_Cmd( FWD_CMD_READ
);
1973 fwd_bit_count
+= Prepare_Addr( Address
);
1976 // Clear destination buffer before sending the command
1977 memset(dest
, 128, m
);
1978 // Connect the A/D to the peak-detected low-frequency path.
1979 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
1980 // Now set up the SSC to get the ADC samples that are now streaming at us.
1983 SendForward(fwd_bit_count
);
1985 // Now do the acquisition
1988 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
) {
1989 AT91C_BASE_SSC
->SSC_THR
= 0x43;
1991 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
1992 dest
[i
] = (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
1997 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); // field off
2001 void EM4xWriteWord(uint32_t Data
, uint8_t Address
, uint32_t Pwd
, uint8_t PwdMode
) {
2003 uint8_t fwd_bit_count
;
2005 //If password mode do login
2006 if (PwdMode
== 1) EM4xLogin(Pwd
);
2008 forward_ptr
= forwardLink_data
;
2009 fwd_bit_count
= Prepare_Cmd( FWD_CMD_WRITE
);
2010 fwd_bit_count
+= Prepare_Addr( Address
);
2011 fwd_bit_count
+= Prepare_Data( Data
&0xFFFF, Data
>>16 );
2013 SendForward(fwd_bit_count
);
2015 //Wait for write to complete
2017 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); // field off