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 AcquireRawAdcSamples125k(int divisor
)
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_READER
);
30 // Connect the A/D to the peak-detected low-frequency path.
31 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
33 // Give it a bit of time for the resonant antenna to settle.
36 // Now set up the SSC to get the ADC samples that are now streaming at us.
39 // Now call the acquisition routine
43 // split into two routines so we can avoid timing issues after sending commands //
44 void DoAcquisition125k(void)
46 uint8_t *dest
= (uint8_t *)BigBuf
;
47 int n
= sizeof(BigBuf
);
53 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
) {
54 AT91C_BASE_SSC
->SSC_THR
= 0x43;
57 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
58 dest
[i
] = (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
64 Dbprintf("buffer samples: %02x %02x %02x %02x %02x %02x %02x %02x ...",
65 dest
[0], dest
[1], dest
[2], dest
[3], dest
[4], dest
[5], dest
[6], dest
[7]);
68 void ModThenAcquireRawAdcSamples125k(int delay_off
, int period_0
, int period_1
, uint8_t *command
)
72 /* Make sure the tag is reset */
73 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
74 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
77 // see if 'h' was specified
78 if (command
[strlen((char *) command
) - 1] == 'h')
84 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
86 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
88 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
90 // Give it a bit of time for the resonant antenna to settle.
92 // And a little more time for the tag to fully power up
95 // Now set up the SSC to get the ADC samples that are now streaming at us.
98 // now modulate the reader field
99 while(*command
!= '\0' && *command
!= ' ') {
100 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
102 SpinDelayUs(delay_off
);
104 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
106 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
108 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
110 if(*(command
++) == '0')
111 SpinDelayUs(period_0
);
113 SpinDelayUs(period_1
);
115 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
117 SpinDelayUs(delay_off
);
119 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
121 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
123 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
129 /* blank r/w tag data stream
130 ...0000000000000000 01111111
131 1010101010101010101010101010101010101010101010101010101010101010
134 101010101010101[0]000...
136 [5555fe852c5555555555555555fe0000]
140 // some hardcoded initial params
141 // when we read a TI tag we sample the zerocross line at 2Mhz
142 // TI tags modulate a 1 as 16 cycles of 123.2Khz
143 // TI tags modulate a 0 as 16 cycles of 134.2Khz
144 #define FSAMPLE 2000000
145 #define FREQLO 123200
146 #define FREQHI 134200
148 signed char *dest
= (signed char *)BigBuf
;
149 int n
= sizeof(BigBuf
);
150 // int *dest = GraphBuffer;
151 // int n = GraphTraceLen;
153 // 128 bit shift register [shift3:shift2:shift1:shift0]
154 uint32_t shift3
= 0, shift2
= 0, shift1
= 0, shift0
= 0;
156 int i
, cycles
=0, samples
=0;
157 // how many sample points fit in 16 cycles of each frequency
158 uint32_t sampleslo
= (FSAMPLE
<<4)/FREQLO
, sampleshi
= (FSAMPLE
<<4)/FREQHI
;
159 // when to tell if we're close enough to one freq or another
160 uint32_t threshold
= (sampleslo
- sampleshi
+ 1)>>1;
162 // TI tags charge at 134.2Khz
163 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
164 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
166 // Place FPGA in passthrough mode, in this mode the CROSS_LO line
167 // connects to SSP_DIN and the SSP_DOUT logic level controls
168 // whether we're modulating the antenna (high)
169 // or listening to the antenna (low)
170 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_PASSTHRU
);
172 // get TI tag data into the buffer
175 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
177 for (i
=0; i
<n
-1; i
++) {
178 // count cycles by looking for lo to hi zero crossings
179 if ( (dest
[i
]<0) && (dest
[i
+1]>0) ) {
181 // after 16 cycles, measure the frequency
184 samples
=i
-samples
; // number of samples in these 16 cycles
186 // TI bits are coming to us lsb first so shift them
187 // right through our 128 bit right shift register
188 shift0
= (shift0
>>1) | (shift1
<< 31);
189 shift1
= (shift1
>>1) | (shift2
<< 31);
190 shift2
= (shift2
>>1) | (shift3
<< 31);
193 // check if the cycles fall close to the number
194 // expected for either the low or high frequency
195 if ( (samples
>(sampleslo
-threshold
)) && (samples
<(sampleslo
+threshold
)) ) {
196 // low frequency represents a 1
198 } else if ( (samples
>(sampleshi
-threshold
)) && (samples
<(sampleshi
+threshold
)) ) {
199 // high frequency represents a 0
201 // probably detected a gay waveform or noise
202 // use this as gaydar or discard shift register and start again
203 shift3
= shift2
= shift1
= shift0
= 0;
207 // for each bit we receive, test if we've detected a valid tag
209 // if we see 17 zeroes followed by 6 ones, we might have a tag
210 // remember the bits are backwards
211 if ( ((shift0
& 0x7fffff) == 0x7e0000) ) {
212 // if start and end bytes match, we have a tag so break out of the loop
213 if ( ((shift0
>>16)&0xff) == ((shift3
>>8)&0xff) ) {
214 cycles
= 0xF0B; //use this as a flag (ugly but whatever)
222 // if flag is set we have a tag
224 DbpString("Info: No valid tag detected.");
226 // put 64 bit data into shift1 and shift0
227 shift0
= (shift0
>>24) | (shift1
<< 8);
228 shift1
= (shift1
>>24) | (shift2
<< 8);
230 // align 16 bit crc into lower half of shift2
231 shift2
= ((shift2
>>24) | (shift3
<< 8)) & 0x0ffff;
233 // if r/w tag, check ident match
234 if ( shift3
&(1<<15) ) {
235 DbpString("Info: TI tag is rewriteable");
236 // only 15 bits compare, last bit of ident is not valid
237 if ( ((shift3
>>16)^shift0
)&0x7fff ) {
238 DbpString("Error: Ident mismatch!");
240 DbpString("Info: TI tag ident is valid");
243 DbpString("Info: TI tag is readonly");
246 // WARNING the order of the bytes in which we calc crc below needs checking
247 // i'm 99% sure the crc algorithm is correct, but it may need to eat the
248 // bytes in reverse or something
252 crc
= update_crc16(crc
, (shift0
)&0xff);
253 crc
= update_crc16(crc
, (shift0
>>8)&0xff);
254 crc
= update_crc16(crc
, (shift0
>>16)&0xff);
255 crc
= update_crc16(crc
, (shift0
>>24)&0xff);
256 crc
= update_crc16(crc
, (shift1
)&0xff);
257 crc
= update_crc16(crc
, (shift1
>>8)&0xff);
258 crc
= update_crc16(crc
, (shift1
>>16)&0xff);
259 crc
= update_crc16(crc
, (shift1
>>24)&0xff);
261 Dbprintf("Info: Tag data: %x%08x, crc=%x",
262 (unsigned int)shift1
, (unsigned int)shift0
, (unsigned int)shift2
& 0xFFFF);
263 if (crc
!= (shift2
&0xffff)) {
264 Dbprintf("Error: CRC mismatch, expected %x", (unsigned int)crc
);
266 DbpString("Info: CRC is good");
271 void WriteTIbyte(uint8_t b
)
275 // modulate 8 bits out to the antenna
279 // stop modulating antenna
286 // stop modulating antenna
296 void AcquireTiType(void)
299 // tag transmission is <20ms, sampling at 2M gives us 40K samples max
300 // each sample is 1 bit stuffed into a uint32_t so we need 1250 uint32_t
301 #define TIBUFLEN 1250
304 memset(BigBuf
,0,sizeof(BigBuf
));
306 // Set up the synchronous serial port
307 AT91C_BASE_PIOA
->PIO_PDR
= GPIO_SSC_DIN
;
308 AT91C_BASE_PIOA
->PIO_ASR
= GPIO_SSC_DIN
;
310 // steal this pin from the SSP and use it to control the modulation
311 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
312 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
314 AT91C_BASE_SSC
->SSC_CR
= AT91C_SSC_SWRST
;
315 AT91C_BASE_SSC
->SSC_CR
= AT91C_SSC_RXEN
| AT91C_SSC_TXEN
;
317 // Sample at 2 Mbit/s, so TI tags are 16.2 vs. 14.9 clocks long
318 // 48/2 = 24 MHz clock must be divided by 12
319 AT91C_BASE_SSC
->SSC_CMR
= 12;
321 AT91C_BASE_SSC
->SSC_RCMR
= SSC_CLOCK_MODE_SELECT(0);
322 AT91C_BASE_SSC
->SSC_RFMR
= SSC_FRAME_MODE_BITS_IN_WORD(32) | AT91C_SSC_MSBF
;
323 AT91C_BASE_SSC
->SSC_TCMR
= 0;
324 AT91C_BASE_SSC
->SSC_TFMR
= 0;
331 // Charge TI tag for 50ms.
334 // stop modulating antenna and listen
341 if(AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
342 BigBuf
[i
] = AT91C_BASE_SSC
->SSC_RHR
; // store 32 bit values in buffer
343 i
++; if(i
>= TIBUFLEN
) break;
348 // return stolen pin to SSP
349 AT91C_BASE_PIOA
->PIO_PDR
= GPIO_SSC_DOUT
;
350 AT91C_BASE_PIOA
->PIO_ASR
= GPIO_SSC_DIN
| GPIO_SSC_DOUT
;
352 char *dest
= (char *)BigBuf
;
355 for (i
=TIBUFLEN
-1; i
>=0; i
--) {
356 for (j
=0; j
<32; j
++) {
357 if(BigBuf
[i
] & (1 << j
)) {
366 // arguments: 64bit data split into 32bit idhi:idlo and optional 16bit crc
367 // if crc provided, it will be written with the data verbatim (even if bogus)
368 // if not provided a valid crc will be computed from the data and written.
369 void WriteTItag(uint32_t idhi
, uint32_t idlo
, uint16_t crc
)
371 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
373 crc
= update_crc16(crc
, (idlo
)&0xff);
374 crc
= update_crc16(crc
, (idlo
>>8)&0xff);
375 crc
= update_crc16(crc
, (idlo
>>16)&0xff);
376 crc
= update_crc16(crc
, (idlo
>>24)&0xff);
377 crc
= update_crc16(crc
, (idhi
)&0xff);
378 crc
= update_crc16(crc
, (idhi
>>8)&0xff);
379 crc
= update_crc16(crc
, (idhi
>>16)&0xff);
380 crc
= update_crc16(crc
, (idhi
>>24)&0xff);
382 Dbprintf("Writing to tag: %x%08x, crc=%x",
383 (unsigned int) idhi
, (unsigned int) idlo
, crc
);
385 // TI tags charge at 134.2Khz
386 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
387 // Place FPGA in passthrough mode, in this mode the CROSS_LO line
388 // connects to SSP_DIN and the SSP_DOUT logic level controls
389 // whether we're modulating the antenna (high)
390 // or listening to the antenna (low)
391 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_PASSTHRU
);
394 // steal this pin from the SSP and use it to control the modulation
395 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
396 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
398 // writing algorithm:
399 // a high bit consists of a field off for 1ms and field on for 1ms
400 // a low bit consists of a field off for 0.3ms and field on for 1.7ms
401 // initiate a charge time of 50ms (field on) then immediately start writing bits
402 // start by writing 0xBB (keyword) and 0xEB (password)
403 // then write 80 bits of data (or 64 bit data + 16 bit crc if you prefer)
404 // finally end with 0x0300 (write frame)
405 // all data is sent lsb firts
406 // finish with 15ms programming time
410 SpinDelay(50); // charge time
412 WriteTIbyte(0xbb); // keyword
413 WriteTIbyte(0xeb); // password
414 WriteTIbyte( (idlo
)&0xff );
415 WriteTIbyte( (idlo
>>8 )&0xff );
416 WriteTIbyte( (idlo
>>16)&0xff );
417 WriteTIbyte( (idlo
>>24)&0xff );
418 WriteTIbyte( (idhi
)&0xff );
419 WriteTIbyte( (idhi
>>8 )&0xff );
420 WriteTIbyte( (idhi
>>16)&0xff );
421 WriteTIbyte( (idhi
>>24)&0xff ); // data hi to lo
422 WriteTIbyte( (crc
)&0xff ); // crc lo
423 WriteTIbyte( (crc
>>8 )&0xff ); // crc hi
424 WriteTIbyte(0x00); // write frame lo
425 WriteTIbyte(0x03); // write frame hi
427 SpinDelay(50); // programming time
431 // get TI tag data into the buffer
434 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
435 DbpString("Now use tiread to check");
438 void SimulateTagLowFrequency(int period
, int gap
, int ledcontrol
)
441 uint8_t *tab
= (uint8_t *)BigBuf
;
443 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
444 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_EDGE_DETECT
);
446 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
| GPIO_SSC_CLK
;
448 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
449 AT91C_BASE_PIOA
->PIO_ODR
= GPIO_SSC_CLK
;
451 #define SHORT_COIL() LOW(GPIO_SSC_DOUT)
452 #define OPEN_COIL() HIGH(GPIO_SSC_DOUT)
456 while(!(AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_CLK
)) {
458 DbpString("Stopped");
475 while(AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_CLK
) {
477 DbpString("Stopped");
494 #define DEBUG_FRAME_CONTENTS 1
495 void SimulateTagLowFrequencyBidir(int divisor
, int t0
)
499 // compose fc/8 fc/10 waveform
500 static void fc(int c
, int *n
) {
501 uint8_t *dest
= (uint8_t *)BigBuf
;
504 // for when we want an fc8 pattern every 4 logical bits
515 // an fc/8 encoded bit is a bit pattern of 11000000 x6 = 48 samples
517 for (idx
=0; idx
<6; idx
++) {
529 // an fc/10 encoded bit is a bit pattern of 1110000000 x5 = 50 samples
531 for (idx
=0; idx
<5; idx
++) {
546 // prepare a waveform pattern in the buffer based on the ID given then
547 // simulate a HID tag until the button is pressed
548 void CmdHIDsimTAG(int hi
, int lo
, int ledcontrol
)
552 HID tag bitstream format
553 The tag contains a 44bit unique code. This is sent out MSB first in sets of 4 bits
554 A 1 bit is represented as 6 fc8 and 5 fc10 patterns
555 A 0 bit is represented as 5 fc10 and 6 fc8 patterns
556 A fc8 is inserted before every 4 bits
557 A special start of frame pattern is used consisting a0b0 where a and b are neither 0
558 nor 1 bits, they are special patterns (a = set of 12 fc8 and b = set of 10 fc10)
562 DbpString("Tags can only have 44 bits.");
566 // special start of frame marker containing invalid bit sequences
567 fc(8, &n
); fc(8, &n
); // invalid
568 fc(8, &n
); fc(10, &n
); // logical 0
569 fc(10, &n
); fc(10, &n
); // invalid
570 fc(8, &n
); fc(10, &n
); // logical 0
573 // manchester encode bits 43 to 32
574 for (i
=11; i
>=0; i
--) {
575 if ((i
%4)==3) fc(0,&n
);
577 fc(10, &n
); fc(8, &n
); // low-high transition
579 fc(8, &n
); fc(10, &n
); // high-low transition
584 // manchester encode bits 31 to 0
585 for (i
=31; i
>=0; i
--) {
586 if ((i
%4)==3) fc(0,&n
);
588 fc(10, &n
); fc(8, &n
); // low-high transition
590 fc(8, &n
); fc(10, &n
); // high-low transition
596 SimulateTagLowFrequency(n
, 0, ledcontrol
);
603 // loop to capture raw HID waveform then FSK demodulate the TAG ID from it
604 void CmdHIDdemodFSK(int findone
, int *high
, int *low
, int ledcontrol
)
606 uint8_t *dest
= (uint8_t *)BigBuf
;
607 int m
=0, n
=0, i
=0, idx
=0, found
=0, lastval
=0;
608 uint32_t hi2
=0, hi
=0, lo
=0;
610 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
611 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
612 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
614 // Connect the A/D to the peak-detected low-frequency path.
615 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
617 // Give it a bit of time for the resonant antenna to settle.
620 // Now set up the SSC to get the ADC samples that are now streaming at us.
628 DbpString("Stopped");
638 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
639 AT91C_BASE_SSC
->SSC_THR
= 0x43;
643 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
644 dest
[i
] = (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
645 // we don't care about actual value, only if it's more or less than a
646 // threshold essentially we capture zero crossings for later analysis
647 if(dest
[i
] < 127) dest
[i
] = 0; else dest
[i
] = 1;
659 // sync to first lo-hi transition
660 for( idx
=1; idx
<m
; idx
++) {
661 if (dest
[idx
-1]<dest
[idx
])
667 // count cycles between consecutive lo-hi transitions, there should be either 8 (fc/8)
668 // or 10 (fc/10) cycles but in practice due to noise etc we may end up with with anywhere
669 // between 7 to 11 cycles so fuzz it by treat anything <9 as 8 and anything else as 10
670 for( i
=0; idx
<m
; idx
++) {
671 if (dest
[idx
-1]<dest
[idx
]) {
686 // we now have a set of cycle counts, loop over previous results and aggregate data into bit patterns
691 for( idx
=0; idx
<m
; idx
++) {
692 if (dest
[idx
]==lastval
) {
695 // a bit time is five fc/10 or six fc/8 cycles so figure out how many bits a pattern width represents,
696 // an extra fc/8 pattern preceeds every 4 bits (about 200 cycles) just to complicate things but it gets
697 // swallowed up by rounding
698 // expected results are 1 or 2 bits, any more and it's an invalid manchester encoding
699 // special start of frame markers use invalid manchester states (no transitions) by using sequences
702 n
=(n
+1)/6; // fc/8 in sets of 6
704 n
=(n
+1)/5; // fc/10 in sets of 5
706 switch (n
) { // stuff appropriate bits in buffer
709 dest
[i
++]=dest
[idx
-1];
712 dest
[i
++]=dest
[idx
-1];
713 dest
[i
++]=dest
[idx
-1];
715 case 3: // 3 bit start of frame markers
716 dest
[i
++]=dest
[idx
-1];
717 dest
[i
++]=dest
[idx
-1];
718 dest
[i
++]=dest
[idx
-1];
720 // When a logic 0 is immediately followed by the start of the next transmisson
721 // (special pattern) a pattern of 4 bit duration lengths is created.
723 dest
[i
++]=dest
[idx
-1];
724 dest
[i
++]=dest
[idx
-1];
725 dest
[i
++]=dest
[idx
-1];
726 dest
[i
++]=dest
[idx
-1];
728 default: // this shouldn't happen, don't stuff any bits
738 // final loop, go over previously decoded manchester data and decode into usable tag ID
739 // 111000 bit pattern represent start of frame, 01 pattern represents a 1 and 10 represents a 0
740 for( idx
=0; idx
<m
-6; idx
++) {
741 // search for a start of frame marker
742 if ( dest
[idx
] && dest
[idx
+1] && dest
[idx
+2] && (!dest
[idx
+3]) && (!dest
[idx
+4]) && (!dest
[idx
+5]) )
746 if (found
&& (hi2
|hi
|lo
)) {
748 Dbprintf("TAG ID: %x%08x%08x (%d)",
749 (unsigned int) hi2
, (unsigned int) hi
, (unsigned int) lo
, (unsigned int) (lo
>>1) & 0xFFFF);
752 Dbprintf("TAG ID: %x%08x (%d)",
753 (unsigned int) hi
, (unsigned int) lo
, (unsigned int) (lo
>>1) & 0xFFFF);
755 /* if we're only looking for one tag */
769 if (dest
[idx
] && (!dest
[idx
+1]) ) {
770 hi2
=(hi2
<<1)|(hi
>>31);
773 } else if ( (!dest
[idx
]) && dest
[idx
+1]) {
774 hi2
=(hi2
<<1)|(hi
>>31);
785 if ( dest
[idx
] && dest
[idx
+1] && dest
[idx
+2] && (!dest
[idx
+3]) && (!dest
[idx
+4]) && (!dest
[idx
+5]) )
789 if (found
&& (hi
|lo
)) {
791 Dbprintf("TAG ID: %x%08x%08x (%d)",
792 (unsigned int) hi2
, (unsigned int) hi
, (unsigned int) lo
, (unsigned int) (lo
>>1) & 0xFFFF);
795 Dbprintf("TAG ID: %x%08x (%d)",
796 (unsigned int) hi
, (unsigned int) lo
, (unsigned int) (lo
>>1) & 0xFFFF);
798 /* if we're only looking for one tag */
816 void CmdIOdemodFSK(int findone
, int *high
, int *low
, int ledcontrol
)
818 uint8_t *dest
= (uint8_t *)BigBuf
;
819 int m
=0, n
=0, i
=0, idx
=0, lastval
=0;
821 uint32_t code
=0, code2
=0;
822 //uint32_t hi2=0, hi=0, lo=0;
824 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
825 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
826 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
828 // Connect the A/D to the peak-detected low-frequency path.
829 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
831 // Give it a bit of time for the resonant antenna to settle.
834 // Now set up the SSC to get the ADC samples that are now streaming at us.
842 DbpString("Stopped");
852 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
853 AT91C_BASE_SSC
->SSC_THR
= 0x43;
857 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
858 dest
[i
] = (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
859 // we don't care about actual value, only if it's more or less than a
860 // threshold essentially we capture zero crossings for later analysis
861 if(dest
[i
] < 127) dest
[i
] = 0; else dest
[i
] = 1;
873 // sync to first lo-hi transition
874 for( idx
=1; idx
<m
; idx
++) {
875 if (dest
[idx
-1]<dest
[idx
])
881 // count cycles between consecutive lo-hi transitions, there should be either 8 (fc/8)
882 // or 10 (fc/10) cycles but in practice due to noise etc we may end up with with anywhere
883 // between 7 to 11 cycles so fuzz it by treat anything <9 as 8 and anything else as 10
884 for( i
=0; idx
<m
; idx
++) {
885 if (dest
[idx
-1]<dest
[idx
]) {
900 // we now have a set of cycle counts, loop over previous results and aggregate data into bit patterns
905 for( idx
=0; idx
<m
; idx
++) {
906 if (dest
[idx
]==lastval
) {
909 // a bit time is five fc/10 or six fc/8 cycles so figure out how many bits a pattern width represents,
910 // an extra fc/8 pattern preceeds every 4 bits (about 200 cycles) just to complicate things but it gets
911 // swallowed up by rounding
912 // expected results are 1 or 2 bits, any more and it's an invalid manchester encoding
913 // special start of frame markers use invalid manchester states (no transitions) by using sequences
916 n
=(n
+1)/7; // fc/8 in sets of 7
918 n
=(n
+1)/6; // fc/10 in sets of 6
920 switch (n
) { // stuff appropriate bits in buffer
923 dest
[i
++]=dest
[idx
-1]^1;
924 //Dbprintf("%d",dest[idx-1]);
927 dest
[i
++]=dest
[idx
-1]^1;
928 dest
[i
++]=dest
[idx
-1]^1;
929 //Dbprintf("%d",dest[idx-1]);
930 //Dbprintf("%d",dest[idx-1]);
932 case 3: // 3 bit start of frame markers
933 for(int j
=0; j
<3; j
++){
934 dest
[i
++]=dest
[idx
-1]^1;
935 // Dbprintf("%d",dest[idx-1]);
939 for(int j
=0; j
<4; j
++){
940 dest
[i
++]=dest
[idx
-1]^1;
941 // Dbprintf("%d",dest[idx-1]);
945 for(int j
=0; j
<5; j
++){
946 dest
[i
++]=dest
[idx
-1]^1;
947 // Dbprintf("%d",dest[idx-1]);
951 for(int j
=0; j
<6; j
++){
952 dest
[i
++]=dest
[idx
-1]^1;
953 // Dbprintf("%d",dest[idx-1]);
957 for(int j
=0; j
<7; j
++){
958 dest
[i
++]=dest
[idx
-1]^1;
959 // Dbprintf("%d",dest[idx-1]);
963 for(int j
=0; j
<8; j
++){
964 dest
[i
++]=dest
[idx
-1]^1;
965 // Dbprintf("%d",dest[idx-1]);
969 for(int j
=0; j
<9; j
++){
970 dest
[i
++]=dest
[idx
-1]^1;
971 // Dbprintf("%d",dest[idx-1]);
975 for(int j
=0; j
<10; j
++){
976 dest
[i
++]=dest
[idx
-1]^1;
977 // Dbprintf("%d",dest[idx-1]);
981 for(int j
=0; j
<11; j
++){
982 dest
[i
++]=dest
[idx
-1]^1;
983 // Dbprintf("%d",dest[idx-1]);
987 for(int j
=0; j
<12; j
++){
988 dest
[i
++]=dest
[idx
-1]^1;
989 // Dbprintf("%d",dest[idx-1]);
992 default: // this shouldn't happen, don't stuff any bits
993 //Dbprintf("%d",dest[idx-1]);
1000 /*for(int j=0; j<64;j+=8){
1001 Dbprintf("%d%d%d%d%d%d%d%d",dest[j],dest[j+1],dest[j+2],dest[j+3],dest[j+4],dest[j+5],dest[j+6],dest[j+7]);
1007 for( idx
=0; idx
<m
-9; idx
++) {
1008 if ( !(dest
[idx
]) && !(dest
[idx
+1]) && !(dest
[idx
+2]) && !(dest
[idx
+3]) && !(dest
[idx
+4]) && !(dest
[idx
+5]) && !(dest
[idx
+6]) && !(dest
[idx
+7]) && !(dest
[idx
+8])&& (dest
[idx
+9])){
1012 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]);
1013 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]);
1014 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]);
1015 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]);
1016 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]);
1017 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]);
1018 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]);
1019 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]);
1021 short version
='\x00';
1022 char unknown
='\x00';
1024 for(int j
=14;j
<18;j
++){
1025 //Dbprintf("%d",dest[idx+j]);
1027 if (dest
[idx
+j
]) version
|= 1;
1029 for(int j
=19;j
<27;j
++){
1030 //Dbprintf("%d",dest[idx+j]);
1032 if (dest
[idx
+j
]) unknown
|= 1;
1034 for(int j
=36;j
<45;j
++){
1035 //Dbprintf("%d",dest[idx+j]);
1037 if (dest
[idx
+j
]) number
|= 1;
1039 for(int j
=46;j
<53;j
++){
1040 //Dbprintf("%d",dest[idx+j]);
1042 if (dest
[idx
+j
]) number
|= 1;
1044 for(int j
=0; j
<32; j
++){
1046 if(dest
[idx
+j
]) code
|= 1;
1048 for(int j
=32; j
<64; j
++){
1050 if(dest
[idx
+j
]) code2
|= 1;
1053 Dbprintf("XSF(%02d)%02x:%d (%08x%08x)",version
,unknown
,number
,code
,code2
);
1057 // if we're only looking for one tag
1075 /*------------------------------
1076 * T5555/T5557/T5567 routines
1077 *------------------------------
1080 /* T55x7 configuration register definitions */
1081 #define T55x7_POR_DELAY 0x00000001
1082 #define T55x7_ST_TERMINATOR 0x00000008
1083 #define T55x7_PWD 0x00000010
1084 #define T55x7_MAXBLOCK_SHIFT 5
1085 #define T55x7_AOR 0x00000200
1086 #define T55x7_PSKCF_RF_2 0
1087 #define T55x7_PSKCF_RF_4 0x00000400
1088 #define T55x7_PSKCF_RF_8 0x00000800
1089 #define T55x7_MODULATION_DIRECT 0
1090 #define T55x7_MODULATION_PSK1 0x00001000
1091 #define T55x7_MODULATION_PSK2 0x00002000
1092 #define T55x7_MODULATION_PSK3 0x00003000
1093 #define T55x7_MODULATION_FSK1 0x00004000
1094 #define T55x7_MODULATION_FSK2 0x00005000
1095 #define T55x7_MODULATION_FSK1a 0x00006000
1096 #define T55x7_MODULATION_FSK2a 0x00007000
1097 #define T55x7_MODULATION_MANCHESTER 0x00008000
1098 #define T55x7_MODULATION_BIPHASE 0x00010000
1099 #define T55x7_BITRATE_RF_8 0
1100 #define T55x7_BITRATE_RF_16 0x00040000
1101 #define T55x7_BITRATE_RF_32 0x00080000
1102 #define T55x7_BITRATE_RF_40 0x000C0000
1103 #define T55x7_BITRATE_RF_50 0x00100000
1104 #define T55x7_BITRATE_RF_64 0x00140000
1105 #define T55x7_BITRATE_RF_100 0x00180000
1106 #define T55x7_BITRATE_RF_128 0x001C0000
1108 /* T5555 (Q5) configuration register definitions */
1109 #define T5555_ST_TERMINATOR 0x00000001
1110 #define T5555_MAXBLOCK_SHIFT 0x00000001
1111 #define T5555_MODULATION_MANCHESTER 0
1112 #define T5555_MODULATION_PSK1 0x00000010
1113 #define T5555_MODULATION_PSK2 0x00000020
1114 #define T5555_MODULATION_PSK3 0x00000030
1115 #define T5555_MODULATION_FSK1 0x00000040
1116 #define T5555_MODULATION_FSK2 0x00000050
1117 #define T5555_MODULATION_BIPHASE 0x00000060
1118 #define T5555_MODULATION_DIRECT 0x00000070
1119 #define T5555_INVERT_OUTPUT 0x00000080
1120 #define T5555_PSK_RF_2 0
1121 #define T5555_PSK_RF_4 0x00000100
1122 #define T5555_PSK_RF_8 0x00000200
1123 #define T5555_USE_PWD 0x00000400
1124 #define T5555_USE_AOR 0x00000800
1125 #define T5555_BITRATE_SHIFT 12
1126 #define T5555_FAST_WRITE 0x00004000
1127 #define T5555_PAGE_SELECT 0x00008000
1130 * Relevant times in microsecond
1131 * To compensate antenna falling times shorten the write times
1132 * and enlarge the gap ones.
1134 #define START_GAP 250
1135 #define WRITE_GAP 160
1136 #define WRITE_0 144 // 192
1137 #define WRITE_1 400 // 432 for T55x7; 448 for E5550
1139 // Write one bit to card
1140 void T55xxWriteBit(int bit
)
1142 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
1143 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1144 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
1146 SpinDelayUs(WRITE_0
);
1148 SpinDelayUs(WRITE_1
);
1149 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1150 SpinDelayUs(WRITE_GAP
);
1153 // Write one card block in page 0, no lock
1154 void T55xxWriteBlock(uint32_t Data
, uint32_t Block
, uint32_t Pwd
, uint8_t PwdMode
)
1158 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
1159 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1160 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
1162 // Give it a bit of time for the resonant antenna to settle.
1163 // And for the tag to fully power up
1166 // Now start writting
1167 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1168 SpinDelayUs(START_GAP
);
1172 T55xxWriteBit(0); //Page 0
1175 for (i
= 0x80000000; i
!= 0; i
>>= 1)
1176 T55xxWriteBit(Pwd
& i
);
1182 for (i
= 0x80000000; i
!= 0; i
>>= 1)
1183 T55xxWriteBit(Data
& i
);
1186 for (i
= 0x04; i
!= 0; i
>>= 1)
1187 T55xxWriteBit(Block
& i
);
1189 // Now perform write (nominal is 5.6 ms for T55x7 and 18ms for E5550,
1190 // so wait a little more)
1191 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1192 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
1194 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1197 // Read one card block in page 0
1198 void T55xxReadBlock(uint32_t Block
, uint32_t Pwd
, uint8_t PwdMode
)
1200 uint8_t *dest
= (uint8_t *)BigBuf
;
1203 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
1205 // Clear destination buffer before sending the command
1206 memset(dest
, 128, m
);
1207 // Connect the A/D to the peak-detected low-frequency path.
1208 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
1209 // Now set up the SSC to get the ADC samples that are now streaming at us.
1213 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1214 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
1216 // Give it a bit of time for the resonant antenna to settle.
1217 // And for the tag to fully power up
1220 // Now start writting
1221 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1222 SpinDelayUs(START_GAP
);
1226 T55xxWriteBit(0); //Page 0
1229 for (i
= 0x80000000; i
!= 0; i
>>= 1)
1230 T55xxWriteBit(Pwd
& i
);
1235 for (i
= 0x04; i
!= 0; i
>>= 1)
1236 T55xxWriteBit(Block
& i
);
1238 // Turn field on to read the response
1239 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1240 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
1242 // Now do the acquisition
1245 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
) {
1246 AT91C_BASE_SSC
->SSC_THR
= 0x43;
1248 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
1249 dest
[i
] = (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
1250 // we don't care about actual value, only if it's more or less than a
1251 // threshold essentially we capture zero crossings for later analysis
1252 // if(dest[i] < 127) dest[i] = 0; else dest[i] = 1;
1258 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); // field off
1263 // Read card traceability data (page 1)
1264 void T55xxReadTrace(void){
1265 uint8_t *dest
= (uint8_t *)BigBuf
;
1268 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
1270 // Clear destination buffer before sending the command
1271 memset(dest
, 128, m
);
1272 // Connect the A/D to the peak-detected low-frequency path.
1273 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
1274 // Now set up the SSC to get the ADC samples that are now streaming at us.
1278 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1279 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
1281 // Give it a bit of time for the resonant antenna to settle.
1282 // And for the tag to fully power up
1285 // Now start writting
1286 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1287 SpinDelayUs(START_GAP
);
1291 T55xxWriteBit(1); //Page 1
1293 // Turn field on to read the response
1294 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1295 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
1297 // Now do the acquisition
1300 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
) {
1301 AT91C_BASE_SSC
->SSC_THR
= 0x43;
1303 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
1304 dest
[i
] = (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
1310 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); // field off
1315 /*-------------- Cloning routines -----------*/
1316 // Copy HID id to card and setup block 0 config
1317 void CopyHIDtoT55x7(uint32_t hi2
, uint32_t hi
, uint32_t lo
, uint8_t longFMT
)
1319 int data1
=0, data2
=0, data3
=0, data4
=0, data5
=0, data6
=0; //up to six blocks for long format
1323 // Ensure no more than 84 bits supplied
1325 DbpString("Tags can only have 84 bits.");
1328 // Build the 6 data blocks for supplied 84bit ID
1330 data1
= 0x1D96A900; // load preamble (1D) & long format identifier (9E manchester encoded)
1331 for (int i
=0;i
<4;i
++) {
1332 if (hi2
& (1<<(19-i
)))
1333 data1
|= (1<<(((3-i
)*2)+1)); // 1 -> 10
1335 data1
|= (1<<((3-i
)*2)); // 0 -> 01
1339 for (int i
=0;i
<16;i
++) {
1340 if (hi2
& (1<<(15-i
)))
1341 data2
|= (1<<(((15-i
)*2)+1)); // 1 -> 10
1343 data2
|= (1<<((15-i
)*2)); // 0 -> 01
1347 for (int i
=0;i
<16;i
++) {
1348 if (hi
& (1<<(31-i
)))
1349 data3
|= (1<<(((15-i
)*2)+1)); // 1 -> 10
1351 data3
|= (1<<((15-i
)*2)); // 0 -> 01
1355 for (int i
=0;i
<16;i
++) {
1356 if (hi
& (1<<(15-i
)))
1357 data4
|= (1<<(((15-i
)*2)+1)); // 1 -> 10
1359 data4
|= (1<<((15-i
)*2)); // 0 -> 01
1363 for (int i
=0;i
<16;i
++) {
1364 if (lo
& (1<<(31-i
)))
1365 data5
|= (1<<(((15-i
)*2)+1)); // 1 -> 10
1367 data5
|= (1<<((15-i
)*2)); // 0 -> 01
1371 for (int i
=0;i
<16;i
++) {
1372 if (lo
& (1<<(15-i
)))
1373 data6
|= (1<<(((15-i
)*2)+1)); // 1 -> 10
1375 data6
|= (1<<((15-i
)*2)); // 0 -> 01
1379 // Ensure no more than 44 bits supplied
1381 DbpString("Tags can only have 44 bits.");
1385 // Build the 3 data blocks for supplied 44bit ID
1388 data1
= 0x1D000000; // load preamble
1390 for (int i
=0;i
<12;i
++) {
1391 if (hi
& (1<<(11-i
)))
1392 data1
|= (1<<(((11-i
)*2)+1)); // 1 -> 10
1394 data1
|= (1<<((11-i
)*2)); // 0 -> 01
1398 for (int i
=0;i
<16;i
++) {
1399 if (lo
& (1<<(31-i
)))
1400 data2
|= (1<<(((15-i
)*2)+1)); // 1 -> 10
1402 data2
|= (1<<((15-i
)*2)); // 0 -> 01
1406 for (int i
=0;i
<16;i
++) {
1407 if (lo
& (1<<(15-i
)))
1408 data3
|= (1<<(((15-i
)*2)+1)); // 1 -> 10
1410 data3
|= (1<<((15-i
)*2)); // 0 -> 01
1415 // Program the data blocks for supplied ID
1416 // and the block 0 for HID format
1417 T55xxWriteBlock(data1
,1,0,0);
1418 T55xxWriteBlock(data2
,2,0,0);
1419 T55xxWriteBlock(data3
,3,0,0);
1421 if (longFMT
) { // if long format there are 6 blocks
1422 T55xxWriteBlock(data4
,4,0,0);
1423 T55xxWriteBlock(data5
,5,0,0);
1424 T55xxWriteBlock(data6
,6,0,0);
1427 // Config for HID (RF/50, FSK2a, Maxblock=3 for short/6 for long)
1428 T55xxWriteBlock(T55x7_BITRATE_RF_50
|
1429 T55x7_MODULATION_FSK2a
|
1430 last_block
<< T55x7_MAXBLOCK_SHIFT
,
1438 void CopyIOtoT55x7(uint32_t hi
, uint32_t lo
, uint8_t longFMT
)
1440 int data1
=0, data2
=0; //up to six blocks for long format
1442 data1
= hi
; // load preamble
1446 // Program the data blocks for supplied ID
1447 // and the block 0 for HID format
1448 T55xxWriteBlock(data1
,1,0,0);
1449 T55xxWriteBlock(data2
,2,0,0);
1452 T55xxWriteBlock(0x00147040,0,0,0);
1458 // Define 9bit header for EM410x tags
1459 #define EM410X_HEADER 0x1FF
1460 #define EM410X_ID_LENGTH 40
1462 void WriteEM410x(uint32_t card
, uint32_t id_hi
, uint32_t id_lo
)
1465 uint64_t id
= EM410X_HEADER
;
1466 uint64_t rev_id
= 0; // reversed ID
1467 int c_parity
[4]; // column parity
1468 int r_parity
= 0; // row parity
1471 // Reverse ID bits given as parameter (for simpler operations)
1472 for (i
= 0; i
< EM410X_ID_LENGTH
; ++i
) {
1474 rev_id
= (rev_id
<< 1) | (id_lo
& 1);
1477 rev_id
= (rev_id
<< 1) | (id_hi
& 1);
1482 for (i
= 0; i
< EM410X_ID_LENGTH
; ++i
) {
1483 id_bit
= rev_id
& 1;
1486 // Don't write row parity bit at start of parsing
1488 id
= (id
<< 1) | r_parity
;
1489 // Start counting parity for new row
1496 // First elements in column?
1498 // Fill out first elements
1499 c_parity
[i
] = id_bit
;
1501 // Count column parity
1502 c_parity
[i
% 4] ^= id_bit
;
1505 id
= (id
<< 1) | id_bit
;
1509 // Insert parity bit of last row
1510 id
= (id
<< 1) | r_parity
;
1512 // Fill out column parity at the end of tag
1513 for (i
= 0; i
< 4; ++i
)
1514 id
= (id
<< 1) | c_parity
[i
];
1519 Dbprintf("Started writing %s tag ...", card
? "T55x7":"T5555");
1523 T55xxWriteBlock((uint32_t)(id
>> 32), 1, 0, 0);
1524 T55xxWriteBlock((uint32_t)id
, 2, 0, 0);
1526 // Config for EM410x (RF/64, Manchester, Maxblock=2)
1528 // Clock rate is stored in bits 8-15 of the card value
1529 clock
= (card
& 0xFF00) >> 8;
1530 Dbprintf("Clock rate: %d", clock
);
1534 clock
= T55x7_BITRATE_RF_32
;
1537 clock
= T55x7_BITRATE_RF_16
;
1540 // A value of 0 is assumed to be 64 for backwards-compatibility
1543 clock
= T55x7_BITRATE_RF_64
;
1546 Dbprintf("Invalid clock rate: %d", clock
);
1550 // Writing configuration for T55x7 tag
1551 T55xxWriteBlock(clock
|
1552 T55x7_MODULATION_MANCHESTER
|
1553 2 << T55x7_MAXBLOCK_SHIFT
,
1557 // Writing configuration for T5555(Q5) tag
1558 T55xxWriteBlock(0x1F << T5555_BITRATE_SHIFT
|
1559 T5555_MODULATION_MANCHESTER
|
1560 2 << T5555_MAXBLOCK_SHIFT
,
1564 Dbprintf("Tag %s written with 0x%08x%08x\n", card
? "T55x7":"T5555",
1565 (uint32_t)(id
>> 32), (uint32_t)id
);
1568 // Clone Indala 64-bit tag by UID to T55x7
1569 void CopyIndala64toT55x7(int hi
, int lo
)
1572 //Program the 2 data blocks for supplied 64bit UID
1573 // and the block 0 for Indala64 format
1574 T55xxWriteBlock(hi
,1,0,0);
1575 T55xxWriteBlock(lo
,2,0,0);
1576 //Config for Indala (RF/32;PSK1 with RF/2;Maxblock=2)
1577 T55xxWriteBlock(T55x7_BITRATE_RF_32
|
1578 T55x7_MODULATION_PSK1
|
1579 2 << T55x7_MAXBLOCK_SHIFT
,
1581 //Alternative config for Indala (Extended mode;RF/32;PSK1 with RF/2;Maxblock=2;Inverse data)
1582 // T5567WriteBlock(0x603E1042,0);
1588 void CopyIndala224toT55x7(int uid1
, int uid2
, int uid3
, int uid4
, int uid5
, int uid6
, int uid7
)
1591 //Program the 7 data blocks for supplied 224bit UID
1592 // and the block 0 for Indala224 format
1593 T55xxWriteBlock(uid1
,1,0,0);
1594 T55xxWriteBlock(uid2
,2,0,0);
1595 T55xxWriteBlock(uid3
,3,0,0);
1596 T55xxWriteBlock(uid4
,4,0,0);
1597 T55xxWriteBlock(uid5
,5,0,0);
1598 T55xxWriteBlock(uid6
,6,0,0);
1599 T55xxWriteBlock(uid7
,7,0,0);
1600 //Config for Indala (RF/32;PSK1 with RF/2;Maxblock=7)
1601 T55xxWriteBlock(T55x7_BITRATE_RF_32
|
1602 T55x7_MODULATION_PSK1
|
1603 7 << T55x7_MAXBLOCK_SHIFT
,
1605 //Alternative config for Indala (Extended mode;RF/32;PSK1 with RF/2;Maxblock=7;Inverse data)
1606 // T5567WriteBlock(0x603E10E2,0);
1613 #define abs(x) ( ((x)<0) ? -(x) : (x) )
1614 #define max(x,y) ( x<y ? y:x)
1616 int DemodPCF7931(uint8_t **outBlocks
) {
1617 uint8_t BitStream
[256];
1618 uint8_t Blocks
[8][16];
1619 uint8_t *GraphBuffer
= (uint8_t *)BigBuf
;
1620 int GraphTraceLen
= sizeof(BigBuf
);
1621 int i
, j
, lastval
, bitidx
, half_switch
;
1623 int tolerance
= clock
/ 8;
1624 int pmc
, block_done
;
1625 int lc
, warnings
= 0;
1627 int lmin
=128, lmax
=128;
1630 AcquireRawAdcSamples125k(0);
1637 /* Find first local max/min */
1638 if(GraphBuffer
[1] > GraphBuffer
[0]) {
1639 while(i
< GraphTraceLen
) {
1640 if( !(GraphBuffer
[i
] > GraphBuffer
[i
-1]) && GraphBuffer
[i
] > lmax
)
1647 while(i
< GraphTraceLen
) {
1648 if( !(GraphBuffer
[i
] < GraphBuffer
[i
-1]) && GraphBuffer
[i
] < lmin
)
1660 for (bitidx
= 0; i
< GraphTraceLen
; i
++)
1662 if ( (GraphBuffer
[i
-1] > GraphBuffer
[i
] && dir
== 1 && GraphBuffer
[i
] > lmax
) || (GraphBuffer
[i
-1] < GraphBuffer
[i
] && dir
== 0 && GraphBuffer
[i
] < lmin
))
1667 // Switch depending on lc length:
1668 // Tolerance is 1/8 of clock rate (arbitrary)
1669 if (abs(lc
-clock
/4) < tolerance
) {
1671 if((i
- pmc
) == lc
) { /* 16T0 was previous one */
1673 i
+= (128+127+16+32+33+16)-1;
1681 } else if (abs(lc
-clock
/2) < tolerance
) {
1683 if((i
- pmc
) == lc
) { /* 16T0 was previous one */
1685 i
+= (128+127+16+32+33)-1;
1690 else if(half_switch
== 1) {
1691 BitStream
[bitidx
++] = 0;
1696 } else if (abs(lc
-clock
) < tolerance
) {
1698 BitStream
[bitidx
++] = 1;
1704 Dbprintf("Error: too many detection errors, aborting.");
1709 if(block_done
== 1) {
1711 for(j
=0; j
<16; j
++) {
1712 Blocks
[num_blocks
][j
] = 128*BitStream
[j
*8+7]+
1713 64*BitStream
[j
*8+6]+
1714 32*BitStream
[j
*8+5]+
1715 16*BitStream
[j
*8+4]+
1727 if (GraphBuffer
[i
-1] > GraphBuffer
[i
]) dir
=0;
1733 if(num_blocks
== 4) break;
1735 memcpy(outBlocks
, Blocks
, 16*num_blocks
);
1739 int IsBlock0PCF7931(uint8_t *Block
) {
1740 // Assume RFU means 0 :)
1741 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
1743 if((memcmp(Block
+9, "\x00\x00\x00\x00\x00\x00\x00", 7) == 0) && Block
[7] == 0) // PAC disabled, can it *really* happen ?
1748 int IsBlock1PCF7931(uint8_t *Block
) {
1749 // Assume RFU means 0 :)
1750 if(Block
[10] == 0 && Block
[11] == 0 && Block
[12] == 0 && Block
[13] == 0)
1751 if((Block
[14] & 0x7f) <= 9 && Block
[15] <= 9)
1759 void ReadPCF7931() {
1760 uint8_t Blocks
[8][17];
1761 uint8_t tmpBlocks
[4][16];
1762 int i
, j
, ind
, ind2
, n
;
1769 memset(Blocks
, 0, 8*17*sizeof(uint8_t));
1772 memset(tmpBlocks
, 0, 4*16*sizeof(uint8_t));
1773 n
= DemodPCF7931((uint8_t**)tmpBlocks
);
1776 if(error
==10 && num_blocks
== 0) {
1777 Dbprintf("Error, no tag or bad tag");
1780 else if (tries
==20 || error
==10) {
1781 Dbprintf("Error reading the tag");
1782 Dbprintf("Here is the partial content");
1787 Dbprintf("(dbg) %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
1788 tmpBlocks
[i
][0], tmpBlocks
[i
][1], tmpBlocks
[i
][2], tmpBlocks
[i
][3], tmpBlocks
[i
][4], tmpBlocks
[i
][5], tmpBlocks
[i
][6], tmpBlocks
[i
][7],
1789 tmpBlocks
[i
][8], tmpBlocks
[i
][9], tmpBlocks
[i
][10], tmpBlocks
[i
][11], tmpBlocks
[i
][12], tmpBlocks
[i
][13], tmpBlocks
[i
][14], tmpBlocks
[i
][15]);
1791 for(i
=0; i
<n
; i
++) {
1792 if(IsBlock0PCF7931(tmpBlocks
[i
])) {
1794 if(i
< n
-1 && IsBlock1PCF7931(tmpBlocks
[i
+1])) {
1798 memcpy(Blocks
[0], tmpBlocks
[i
], 16);
1799 Blocks
[0][ALLOC
] = 1;
1800 memcpy(Blocks
[1], tmpBlocks
[i
+1], 16);
1801 Blocks
[1][ALLOC
] = 1;
1802 max_blocks
= max((Blocks
[1][14] & 0x7f), Blocks
[1][15]) + 1;
1804 Dbprintf("(dbg) Max blocks: %d", max_blocks
);
1806 // Handle following blocks
1807 for(j
=i
+2, ind2
=2; j
!=i
; j
++, ind2
++, num_blocks
++) {
1810 memcpy(Blocks
[ind2
], tmpBlocks
[j
], 16);
1811 Blocks
[ind2
][ALLOC
] = 1;
1819 for(i
=0; i
<n
; i
++) { // Look for identical block in known blocks
1820 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
1821 for(j
=0; j
<max_blocks
; j
++) {
1822 if(Blocks
[j
][ALLOC
] == 1 && !memcmp(tmpBlocks
[i
], Blocks
[j
], 16)) {
1823 // Found an identical block
1824 for(ind
=i
-1,ind2
=j
-1; ind
>= 0; ind
--,ind2
--) {
1827 if(!Blocks
[ind2
][ALLOC
]) { // Block ind2 not already found
1828 // Dbprintf("Tmp %d -> Block %d", ind, ind2);
1829 memcpy(Blocks
[ind2
], tmpBlocks
[ind
], 16);
1830 Blocks
[ind2
][ALLOC
] = 1;
1832 if(num_blocks
== max_blocks
) goto end
;
1835 for(ind
=i
+1,ind2
=j
+1; ind
< n
; ind
++,ind2
++) {
1836 if(ind2
> max_blocks
)
1838 if(!Blocks
[ind2
][ALLOC
]) { // Block ind2 not already found
1839 // Dbprintf("Tmp %d -> Block %d", ind, ind2);
1840 memcpy(Blocks
[ind2
], tmpBlocks
[ind
], 16);
1841 Blocks
[ind2
][ALLOC
] = 1;
1843 if(num_blocks
== max_blocks
) goto end
;
1852 if (BUTTON_PRESS()) return;
1853 } while (num_blocks
!= max_blocks
);
1855 Dbprintf("-----------------------------------------");
1856 Dbprintf("Memory content:");
1857 Dbprintf("-----------------------------------------");
1858 for(i
=0; i
<max_blocks
; i
++) {
1859 if(Blocks
[i
][ALLOC
]==1)
1860 Dbprintf("%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
1861 Blocks
[i
][0], Blocks
[i
][1], Blocks
[i
][2], Blocks
[i
][3], Blocks
[i
][4], Blocks
[i
][5], Blocks
[i
][6], Blocks
[i
][7],
1862 Blocks
[i
][8], Blocks
[i
][9], Blocks
[i
][10], Blocks
[i
][11], Blocks
[i
][12], Blocks
[i
][13], Blocks
[i
][14], Blocks
[i
][15]);
1864 Dbprintf("<missing block %d>", i
);
1866 Dbprintf("-----------------------------------------");
1872 //-----------------------------------
1873 // EM4469 / EM4305 routines
1874 //-----------------------------------
1875 #define FWD_CMD_LOGIN 0xC //including the even parity, binary mirrored
1876 #define FWD_CMD_WRITE 0xA
1877 #define FWD_CMD_READ 0x9
1878 #define FWD_CMD_DISABLE 0x5
1881 uint8_t forwardLink_data
[64]; //array of forwarded bits
1882 uint8_t * forward_ptr
; //ptr for forward message preparation
1883 uint8_t fwd_bit_sz
; //forwardlink bit counter
1884 uint8_t * fwd_write_ptr
; //forwardlink bit pointer
1886 //====================================================================
1887 // prepares command bits
1889 //====================================================================
1890 //--------------------------------------------------------------------
1891 uint8_t Prepare_Cmd( uint8_t cmd
) {
1892 //--------------------------------------------------------------------
1894 *forward_ptr
++ = 0; //start bit
1895 *forward_ptr
++ = 0; //second pause for 4050 code
1897 *forward_ptr
++ = cmd
;
1899 *forward_ptr
++ = cmd
;
1901 *forward_ptr
++ = cmd
;
1903 *forward_ptr
++ = cmd
;
1905 return 6; //return number of emited bits
1908 //====================================================================
1909 // prepares address bits
1911 //====================================================================
1913 //--------------------------------------------------------------------
1914 uint8_t Prepare_Addr( uint8_t addr
) {
1915 //--------------------------------------------------------------------
1917 register uint8_t line_parity
;
1922 *forward_ptr
++ = addr
;
1923 line_parity
^= addr
;
1927 *forward_ptr
++ = (line_parity
& 1);
1929 return 7; //return number of emited bits
1932 //====================================================================
1933 // prepares data bits intreleaved with parity bits
1935 //====================================================================
1937 //--------------------------------------------------------------------
1938 uint8_t Prepare_Data( uint16_t data_low
, uint16_t data_hi
) {
1939 //--------------------------------------------------------------------
1941 register uint8_t line_parity
;
1942 register uint8_t column_parity
;
1943 register uint8_t i
, j
;
1944 register uint16_t data
;
1949 for(i
=0; i
<4; i
++) {
1951 for(j
=0; j
<8; j
++) {
1952 line_parity
^= data
;
1953 column_parity
^= (data
& 1) << j
;
1954 *forward_ptr
++ = data
;
1957 *forward_ptr
++ = line_parity
;
1962 for(j
=0; j
<8; j
++) {
1963 *forward_ptr
++ = column_parity
;
1964 column_parity
>>= 1;
1968 return 45; //return number of emited bits
1971 //====================================================================
1972 // Forward Link send function
1973 // Requires: forwarLink_data filled with valid bits (1 bit per byte)
1974 // fwd_bit_count set with number of bits to be sent
1975 //====================================================================
1976 void SendForward(uint8_t fwd_bit_count
) {
1978 fwd_write_ptr
= forwardLink_data
;
1979 fwd_bit_sz
= fwd_bit_count
;
1984 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
1985 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1986 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
1988 // Give it a bit of time for the resonant antenna to settle.
1989 // And for the tag to fully power up
1992 // force 1st mod pulse (start gap must be longer for 4305)
1993 fwd_bit_sz
--; //prepare next bit modulation
1995 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); // field off
1996 SpinDelayUs(55*8); //55 cycles off (8us each)for 4305
1997 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1998 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);//field on
1999 SpinDelayUs(16*8); //16 cycles on (8us each)
2001 // now start writting
2002 while(fwd_bit_sz
-- > 0) { //prepare next bit modulation
2003 if(((*fwd_write_ptr
++) & 1) == 1)
2004 SpinDelayUs(32*8); //32 cycles at 125Khz (8us each)
2006 //These timings work for 4469/4269/4305 (with the 55*8 above)
2007 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); // field off
2008 SpinDelayUs(23*8); //16-4 cycles off (8us each)
2009 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
2010 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);//field on
2011 SpinDelayUs(9*8); //16 cycles on (8us each)
2016 void EM4xLogin(uint32_t Password
) {
2018 uint8_t fwd_bit_count
;
2020 forward_ptr
= forwardLink_data
;
2021 fwd_bit_count
= Prepare_Cmd( FWD_CMD_LOGIN
);
2022 fwd_bit_count
+= Prepare_Data( Password
&0xFFFF, Password
>>16 );
2024 SendForward(fwd_bit_count
);
2026 //Wait for command to complete
2031 void EM4xReadWord(uint8_t Address
, uint32_t Pwd
, uint8_t PwdMode
) {
2033 uint8_t fwd_bit_count
;
2034 uint8_t *dest
= (uint8_t *)BigBuf
;
2037 //If password mode do login
2038 if (PwdMode
== 1) EM4xLogin(Pwd
);
2040 forward_ptr
= forwardLink_data
;
2041 fwd_bit_count
= Prepare_Cmd( FWD_CMD_READ
);
2042 fwd_bit_count
+= Prepare_Addr( Address
);
2045 // Clear destination buffer before sending the command
2046 memset(dest
, 128, m
);
2047 // Connect the A/D to the peak-detected low-frequency path.
2048 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
2049 // Now set up the SSC to get the ADC samples that are now streaming at us.
2052 SendForward(fwd_bit_count
);
2054 // Now do the acquisition
2057 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
) {
2058 AT91C_BASE_SSC
->SSC_THR
= 0x43;
2060 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
2061 dest
[i
] = (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
2066 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); // field off
2070 void EM4xWriteWord(uint32_t Data
, uint8_t Address
, uint32_t Pwd
, uint8_t PwdMode
) {
2072 uint8_t fwd_bit_count
;
2074 //If password mode do login
2075 if (PwdMode
== 1) EM4xLogin(Pwd
);
2077 forward_ptr
= forwardLink_data
;
2078 fwd_bit_count
= Prepare_Cmd( FWD_CMD_WRITE
);
2079 fwd_bit_count
+= Prepare_Addr( Address
);
2080 fwd_bit_count
+= Prepare_Data( Data
&0xFFFF, Data
>>16 );
2082 SendForward(fwd_bit_count
);
2084 //Wait for write to complete
2086 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); // field off