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 // split into two routines so we can avoid timing issues after sending commands //
19 void DoAcquisition125k_internal(bool silent
)
21 uint8_t *dest
= (uint8_t *)BigBuf
;
22 int n
= sizeof(BigBuf
);
28 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
) {
29 AT91C_BASE_SSC
->SSC_THR
= 0x43;
32 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
33 dest
[i
] = (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
41 Dbprintf("buffer samples: %02x %02x %02x %02x %02x %02x %02x %02x ...",
42 dest
[0], dest
[1], dest
[2], dest
[3], dest
[4], dest
[5], dest
[6], dest
[7]);
46 void DoAcquisition125k(void)
48 DoAcquisition125k_internal(false);
51 void SetupToAcquireRawAdcSamples(int divisor
)
53 if ( (divisor
== 1) || (divisor
< 0) || (divisor
> 255) )
54 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
55 else if (divisor
== 0)
56 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
58 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, divisor
);
60 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
62 // Connect the A/D to the peak-detected low-frequency path.
63 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
65 // Give it a bit of time for the resonant antenna to settle.
68 // Now set up the SSC to get the ADC samples that are now streaming at us.
72 void AcquireRawAdcSamples125k(int divisor
)
74 SetupToAcquireRawAdcSamples(divisor
);
75 // Now call the acquisition routine
76 DoAcquisition125k_internal(false);
79 void ModThenAcquireRawAdcSamples125k(int delay_off
, int period_0
, int period_1
, uint8_t *command
)
83 /* Make sure the tag is reset */
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_READER
);
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_READER
);
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_READER
);
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 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
175 // Place FPGA in passthrough mode, in this mode the CROSS_LO line
176 // connects to SSP_DIN and the SSP_DOUT logic level controls
177 // whether we're modulating the antenna (high)
178 // or listening to the antenna (low)
179 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_PASSTHRU
);
181 // get TI tag data into the buffer
184 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
186 for (i
=0; i
<n
-1; i
++) {
187 // count cycles by looking for lo to hi zero crossings
188 if ( (dest
[i
]<0) && (dest
[i
+1]>0) ) {
190 // after 16 cycles, measure the frequency
193 samples
=i
-samples
; // number of samples in these 16 cycles
195 // TI bits are coming to us lsb first so shift them
196 // right through our 128 bit right shift register
197 shift0
= (shift0
>>1) | (shift1
<< 31);
198 shift1
= (shift1
>>1) | (shift2
<< 31);
199 shift2
= (shift2
>>1) | (shift3
<< 31);
202 // check if the cycles fall close to the number
203 // expected for either the low or high frequency
204 if ( (samples
>(sampleslo
-threshold
)) && (samples
<(sampleslo
+threshold
)) ) {
205 // low frequency represents a 1
207 } else if ( (samples
>(sampleshi
-threshold
)) && (samples
<(sampleshi
+threshold
)) ) {
208 // high frequency represents a 0
210 // probably detected a gay waveform or noise
211 // use this as gaydar or discard shift register and start again
212 shift3
= shift2
= shift1
= shift0
= 0;
216 // for each bit we receive, test if we've detected a valid tag
218 // if we see 17 zeroes followed by 6 ones, we might have a tag
219 // remember the bits are backwards
220 if ( ((shift0
& 0x7fffff) == 0x7e0000) ) {
221 // if start and end bytes match, we have a tag so break out of the loop
222 if ( ((shift0
>>16)&0xff) == ((shift3
>>8)&0xff) ) {
223 cycles
= 0xF0B; //use this as a flag (ugly but whatever)
231 // if flag is set we have a tag
233 DbpString("Info: No valid tag detected.");
235 // put 64 bit data into shift1 and shift0
236 shift0
= (shift0
>>24) | (shift1
<< 8);
237 shift1
= (shift1
>>24) | (shift2
<< 8);
239 // align 16 bit crc into lower half of shift2
240 shift2
= ((shift2
>>24) | (shift3
<< 8)) & 0x0ffff;
242 // if r/w tag, check ident match
243 if ( shift3
&(1<<15) ) {
244 DbpString("Info: TI tag is rewriteable");
245 // only 15 bits compare, last bit of ident is not valid
246 if ( ((shift3
>>16)^shift0
)&0x7fff ) {
247 DbpString("Error: Ident mismatch!");
249 DbpString("Info: TI tag ident is valid");
252 DbpString("Info: TI tag is readonly");
255 // WARNING the order of the bytes in which we calc crc below needs checking
256 // i'm 99% sure the crc algorithm is correct, but it may need to eat the
257 // bytes in reverse or something
261 crc
= update_crc16(crc
, (shift0
)&0xff);
262 crc
= update_crc16(crc
, (shift0
>>8)&0xff);
263 crc
= update_crc16(crc
, (shift0
>>16)&0xff);
264 crc
= update_crc16(crc
, (shift0
>>24)&0xff);
265 crc
= update_crc16(crc
, (shift1
)&0xff);
266 crc
= update_crc16(crc
, (shift1
>>8)&0xff);
267 crc
= update_crc16(crc
, (shift1
>>16)&0xff);
268 crc
= update_crc16(crc
, (shift1
>>24)&0xff);
270 Dbprintf("Info: Tag data: %x%08x, crc=%x",
271 (unsigned int)shift1
, (unsigned int)shift0
, (unsigned int)shift2
& 0xFFFF);
272 if (crc
!= (shift2
&0xffff)) {
273 Dbprintf("Error: CRC mismatch, expected %x", (unsigned int)crc
);
275 DbpString("Info: CRC is good");
280 void WriteTIbyte(uint8_t b
)
284 // modulate 8 bits out to the antenna
288 // stop modulating antenna
295 // stop modulating antenna
305 void AcquireTiType(void)
308 // tag transmission is <20ms, sampling at 2M gives us 40K samples max
309 // each sample is 1 bit stuffed into a uint32_t so we need 1250 uint32_t
310 #define TIBUFLEN 1250
313 memset(BigBuf
,0,sizeof(BigBuf
));
315 // Set up the synchronous serial port
316 AT91C_BASE_PIOA
->PIO_PDR
= GPIO_SSC_DIN
;
317 AT91C_BASE_PIOA
->PIO_ASR
= GPIO_SSC_DIN
;
319 // steal this pin from the SSP and use it to control the modulation
320 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
321 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
323 AT91C_BASE_SSC
->SSC_CR
= AT91C_SSC_SWRST
;
324 AT91C_BASE_SSC
->SSC_CR
= AT91C_SSC_RXEN
| AT91C_SSC_TXEN
;
326 // Sample at 2 Mbit/s, so TI tags are 16.2 vs. 14.9 clocks long
327 // 48/2 = 24 MHz clock must be divided by 12
328 AT91C_BASE_SSC
->SSC_CMR
= 12;
330 AT91C_BASE_SSC
->SSC_RCMR
= SSC_CLOCK_MODE_SELECT(0);
331 AT91C_BASE_SSC
->SSC_RFMR
= SSC_FRAME_MODE_BITS_IN_WORD(32) | AT91C_SSC_MSBF
;
332 AT91C_BASE_SSC
->SSC_TCMR
= 0;
333 AT91C_BASE_SSC
->SSC_TFMR
= 0;
340 // Charge TI tag for 50ms.
343 // stop modulating antenna and listen
350 if(AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
351 BigBuf
[i
] = AT91C_BASE_SSC
->SSC_RHR
; // store 32 bit values in buffer
352 i
++; if(i
>= TIBUFLEN
) break;
357 // return stolen pin to SSP
358 AT91C_BASE_PIOA
->PIO_PDR
= GPIO_SSC_DOUT
;
359 AT91C_BASE_PIOA
->PIO_ASR
= GPIO_SSC_DIN
| GPIO_SSC_DOUT
;
361 char *dest
= (char *)BigBuf
;
364 for (i
=TIBUFLEN
-1; i
>=0; i
--) {
365 for (j
=0; j
<32; j
++) {
366 if(BigBuf
[i
] & (1 << j
)) {
375 // arguments: 64bit data split into 32bit idhi:idlo and optional 16bit crc
376 // if crc provided, it will be written with the data verbatim (even if bogus)
377 // if not provided a valid crc will be computed from the data and written.
378 void WriteTItag(uint32_t idhi
, uint32_t idlo
, uint16_t crc
)
381 crc
= update_crc16(crc
, (idlo
)&0xff);
382 crc
= update_crc16(crc
, (idlo
>>8)&0xff);
383 crc
= update_crc16(crc
, (idlo
>>16)&0xff);
384 crc
= update_crc16(crc
, (idlo
>>24)&0xff);
385 crc
= update_crc16(crc
, (idhi
)&0xff);
386 crc
= update_crc16(crc
, (idhi
>>8)&0xff);
387 crc
= update_crc16(crc
, (idhi
>>16)&0xff);
388 crc
= update_crc16(crc
, (idhi
>>24)&0xff);
390 Dbprintf("Writing to tag: %x%08x, crc=%x",
391 (unsigned int) idhi
, (unsigned int) idlo
, crc
);
393 // TI tags charge at 134.2Khz
394 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
395 // Place FPGA in passthrough mode, in this mode the CROSS_LO line
396 // connects to SSP_DIN and the SSP_DOUT logic level controls
397 // whether we're modulating the antenna (high)
398 // or listening to the antenna (low)
399 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_PASSTHRU
);
402 // steal this pin from the SSP and use it to control the modulation
403 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
404 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
406 // writing algorithm:
407 // a high bit consists of a field off for 1ms and field on for 1ms
408 // a low bit consists of a field off for 0.3ms and field on for 1.7ms
409 // initiate a charge time of 50ms (field on) then immediately start writing bits
410 // start by writing 0xBB (keyword) and 0xEB (password)
411 // then write 80 bits of data (or 64 bit data + 16 bit crc if you prefer)
412 // finally end with 0x0300 (write frame)
413 // all data is sent lsb firts
414 // finish with 15ms programming time
418 SpinDelay(50); // charge time
420 WriteTIbyte(0xbb); // keyword
421 WriteTIbyte(0xeb); // password
422 WriteTIbyte( (idlo
)&0xff );
423 WriteTIbyte( (idlo
>>8 )&0xff );
424 WriteTIbyte( (idlo
>>16)&0xff );
425 WriteTIbyte( (idlo
>>24)&0xff );
426 WriteTIbyte( (idhi
)&0xff );
427 WriteTIbyte( (idhi
>>8 )&0xff );
428 WriteTIbyte( (idhi
>>16)&0xff );
429 WriteTIbyte( (idhi
>>24)&0xff ); // data hi to lo
430 WriteTIbyte( (crc
)&0xff ); // crc lo
431 WriteTIbyte( (crc
>>8 )&0xff ); // crc hi
432 WriteTIbyte(0x00); // write frame lo
433 WriteTIbyte(0x03); // write frame hi
435 SpinDelay(50); // programming time
439 // get TI tag data into the buffer
442 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
443 DbpString("Now use tiread to check");
446 void SimulateTagLowFrequency(int period
, int gap
, int ledcontrol
)
449 uint8_t *tab
= (uint8_t *)BigBuf
;
451 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_EDGE_DETECT
);
453 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
| GPIO_SSC_CLK
;
455 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
456 AT91C_BASE_PIOA
->PIO_ODR
= GPIO_SSC_CLK
;
458 #define SHORT_COIL() LOW(GPIO_SSC_DOUT)
459 #define OPEN_COIL() HIGH(GPIO_SSC_DOUT)
463 while(!(AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_CLK
)) {
465 DbpString("Stopped");
482 while(AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_CLK
) {
484 DbpString("Stopped");
501 #define DEBUG_FRAME_CONTENTS 1
502 void SimulateTagLowFrequencyBidir(int divisor
, int t0
)
506 // compose fc/8 fc/10 waveform
507 static void fc(int c
, int *n
) {
508 uint8_t *dest
= (uint8_t *)BigBuf
;
511 // for when we want an fc8 pattern every 4 logical bits
522 // an fc/8 encoded bit is a bit pattern of 11000000 x6 = 48 samples
524 for (idx
=0; idx
<6; idx
++) {
536 // an fc/10 encoded bit is a bit pattern of 1110000000 x5 = 50 samples
538 for (idx
=0; idx
<5; idx
++) {
553 // prepare a waveform pattern in the buffer based on the ID given then
554 // simulate a HID tag until the button is pressed
555 void CmdHIDsimTAG(int hi
, int lo
, int ledcontrol
)
559 HID tag bitstream format
560 The tag contains a 44bit unique code. This is sent out MSB first in sets of 4 bits
561 A 1 bit is represented as 6 fc8 and 5 fc10 patterns
562 A 0 bit is represented as 5 fc10 and 6 fc8 patterns
563 A fc8 is inserted before every 4 bits
564 A special start of frame pattern is used consisting a0b0 where a and b are neither 0
565 nor 1 bits, they are special patterns (a = set of 12 fc8 and b = set of 10 fc10)
569 DbpString("Tags can only have 44 bits.");
573 // special start of frame marker containing invalid bit sequences
574 fc(8, &n
); fc(8, &n
); // invalid
575 fc(8, &n
); fc(10, &n
); // logical 0
576 fc(10, &n
); fc(10, &n
); // invalid
577 fc(8, &n
); fc(10, &n
); // logical 0
580 // manchester encode bits 43 to 32
581 for (i
=11; i
>=0; i
--) {
582 if ((i
%4)==3) fc(0,&n
);
584 fc(10, &n
); fc(8, &n
); // low-high transition
586 fc(8, &n
); fc(10, &n
); // high-low transition
591 // manchester encode bits 31 to 0
592 for (i
=31; i
>=0; i
--) {
593 if ((i
%4)==3) fc(0,&n
);
595 fc(10, &n
); fc(8, &n
); // low-high transition
597 fc(8, &n
); fc(10, &n
); // high-low transition
603 SimulateTagLowFrequency(n
, 0, ledcontrol
);
608 void setup_for_125khz()
610 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
611 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
613 // Connect the A/D to the peak-detected low-frequency path.
614 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
616 // Give it a bit of time for the resonant antenna to settle.
619 // Now set up the SSC to get the ADC samples that are now streaming at us.
623 void get_samples(int ledcontrol
, uint8_t* dest
, int size
)
627 memset(dest
,128,size
);
629 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
630 AT91C_BASE_SSC
->SSC_THR
= 0x43;
631 if (ledcontrol
) LED_D_ON();
633 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
634 dest
[i
] = (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
635 // we don't care about actual value, only if it's more or less than a
636 // threshold essentially we capture zero crossings for later analysis
637 if(dest
[i
] < 127) dest
[i
] = 0; else dest
[i
] = 1;
639 if (ledcontrol
) LED_D_OFF();
647 uint8_t fsk_demod(uint8_t * dest
, int size
)
649 uint8_t last_transition
= 0;
652 // we don't care about actual value, only if it's more or less than a
653 // threshold essentially we capture zero crossings for later analysis
654 uint8_t threshold_value
= 127;
658 // sync to first lo-hi transition, and threshold
660 //Need to threshold first sample
661 if(dest
[0] < threshold_value
) dest
[0] = 0;
665 // count cycles between consecutive lo-hi transitions, there should be either 8 (fc/8)
666 // or 10 (fc/10) cycles but in practice due to noise etc we may end up with with anywhere
667 // between 7 to 11 cycles so fuzz it by treat anything <9 as 8 and anything else as 10
668 for(idx
= 1; idx
< size
; idx
++) {
670 // threshold current value
671 if (dest
[idx
] < threshold_value
) dest
[idx
] = 0;
674 // Check for 0->1 transition
675 if (dest
[idx
-1] < dest
[idx
]) { // 0 -> 1 transition
677 if (idx
-last_transition
< 9) {
682 last_transition
= idx
;
686 return numBits
; //Actually, it returns the number of bytes, but each byte represents a bit: 1 or 0
689 uint8_t aggregate_bits(uint8_t *dest
,uint8_t size
, uint8_t h2l_crossing_value
,uint8_t l2h_crossing_value
, uint8_t maxConsequtiveBits
)
691 uint8_t lastval
=dest
[0];
696 for( idx
=1; idx
< size
; idx
++) {
698 if (dest
[idx
]==lastval
) {
702 //if lastval was 1, we have a 1->0 crossing
705 } else {// 0->1 crossing
710 memset(dest
+i
, lastval
^ 1, n
);
720 // loop to capture raw HID waveform then FSK demodulate the TAG ID from it
721 void CmdHIDdemodFSK(int findone
, int *high
, int *low
, int ledcontrol
)
723 uint8_t *dest
= (uint8_t *)BigBuf
;
725 int size
=0, idx
=0, found
=0;
726 uint32_t hi2
=0, hi
=0, lo
=0;
728 // Configure to go in 125Khz listen mode
729 SetupToAcquireRawAdcSamples(0);
736 DbpString("Stopped");
743 DoAcquisition125k_internal(true);
744 size
= sizeof(BigBuf
);
747 size
= fsk_demod(dest
, size
);
751 // we now have a set of cycle counts, loop over previous results and aggregate data into bit patterns
753 // 1->0 : fc/8 in sets of 6
754 // 0->1 : fc/10 in sets of 5
755 size
= aggregate_bits(dest
,size
, 6,5,5);
759 // final loop, go over previously decoded manchester data and decode into usable tag ID
760 // 111000 bit pattern represent start of frame, 01 pattern represents a 1 and 10 represents a 0
761 uint8_t frame_marker_mask
[] = {1,1,1,0,0,0};
763 for( idx
=0; idx
< size
-sizeof(frame_marker_mask
); idx
++) {
766 if(dest
[idx
] == dest
[idx
+1])
774 //Shift in a bit. Start by shifting high registers
775 hi2
= (hi2
<<1)|(hi
>>31);
776 hi
= (hi
<<1)|(lo
>>31);
777 //Then, shift in a 0 or one into low
778 if (dest
[idx
] && !dest
[idx
+1]) // 1 0
786 // search for a start of frame marker
787 if ( memcmp(dest
+idx
, frame_marker_mask
, sizeof(frame_marker_mask
)) == 0)
788 { // Found start of frame marker
790 idx
+=sizeof(frame_marker_mask
);
791 if (found
&& (hi2
|hi
|lo
)) {
793 Dbprintf("TAG ID: %x%08x%08x (%d)",
794 (unsigned int) hi2
, (unsigned int) hi
, (unsigned int) lo
, (unsigned int) (lo
>>1) & 0xFFFF);
797 Dbprintf("TAG ID: %x%08x (%d)",
798 (unsigned int) hi
, (unsigned int) lo
, (unsigned int) (lo
>>1) & 0xFFFF);
800 /* if we're only looking for one tag */
818 uint32_t bytebits_to_byte(uint8_t* src
, int numbits
)
821 for(int i
= 0 ; i
< numbits
; i
++)
823 num
= (num
<< 1) | (*src
);
830 void CmdIOdemodFSK(int findone
, int *high
, int *low
, int ledcontrol
)
832 uint8_t *dest
= (uint8_t *)BigBuf
;
834 uint32_t code
=0, code2
=0;
835 //uint32_t hi2=0, hi=0, lo=0;
844 DbpString("Stopped");
850 DoAcquisition125k_internal(true);
851 size
= sizeof(BigBuf
);
854 size
= fsk_demod(dest
, size
);
856 // we now have a set of cycle counts, loop over previous results and aggregate data into bit patterns
857 // 1->0 : fc/8 in sets of 7
858 // 0->1 : fc/10 in sets of 6
859 size
= aggregate_bits(dest
, size
, 7,6,13);
863 uint8_t mask
[] = {0,0,0,0,0,0,0,0,0,1};
864 for( idx
=0; idx
< size
- 64; idx
++) {
866 if ( memcmp(dest
+ idx
, mask
, sizeof(mask
)) ) continue;
868 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]);
869 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]);
870 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]);
871 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]);
872 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]);
873 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]);
874 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]);
875 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]);
877 code
= bytebits_to_byte(dest
+idx
,32);
878 code2
= bytebits_to_byte(dest
+idx
+32,32);
880 short version
= bytebits_to_byte(dest
+idx
+14,4);
881 char unknown
= bytebits_to_byte(dest
+idx
+19,8) ;
882 uint16_t number
= bytebits_to_byte(dest
+idx
+36,9);
884 Dbprintf("XSF(%02d)%02x:%d (%08x%08x)",version
,unknown
,number
,code
,code2
);
885 if (ledcontrol
) LED_D_OFF();
887 // if we're only looking for one tag
897 /*------------------------------
898 * T5555/T5557/T5567 routines
899 *------------------------------
902 /* T55x7 configuration register definitions */
903 #define T55x7_POR_DELAY 0x00000001
904 #define T55x7_ST_TERMINATOR 0x00000008
905 #define T55x7_PWD 0x00000010
906 #define T55x7_MAXBLOCK_SHIFT 5
907 #define T55x7_AOR 0x00000200
908 #define T55x7_PSKCF_RF_2 0
909 #define T55x7_PSKCF_RF_4 0x00000400
910 #define T55x7_PSKCF_RF_8 0x00000800
911 #define T55x7_MODULATION_DIRECT 0
912 #define T55x7_MODULATION_PSK1 0x00001000
913 #define T55x7_MODULATION_PSK2 0x00002000
914 #define T55x7_MODULATION_PSK3 0x00003000
915 #define T55x7_MODULATION_FSK1 0x00004000
916 #define T55x7_MODULATION_FSK2 0x00005000
917 #define T55x7_MODULATION_FSK1a 0x00006000
918 #define T55x7_MODULATION_FSK2a 0x00007000
919 #define T55x7_MODULATION_MANCHESTER 0x00008000
920 #define T55x7_MODULATION_BIPHASE 0x00010000
921 #define T55x7_BITRATE_RF_8 0
922 #define T55x7_BITRATE_RF_16 0x00040000
923 #define T55x7_BITRATE_RF_32 0x00080000
924 #define T55x7_BITRATE_RF_40 0x000C0000
925 #define T55x7_BITRATE_RF_50 0x00100000
926 #define T55x7_BITRATE_RF_64 0x00140000
927 #define T55x7_BITRATE_RF_100 0x00180000
928 #define T55x7_BITRATE_RF_128 0x001C0000
930 /* T5555 (Q5) configuration register definitions */
931 #define T5555_ST_TERMINATOR 0x00000001
932 #define T5555_MAXBLOCK_SHIFT 0x00000001
933 #define T5555_MODULATION_MANCHESTER 0
934 #define T5555_MODULATION_PSK1 0x00000010
935 #define T5555_MODULATION_PSK2 0x00000020
936 #define T5555_MODULATION_PSK3 0x00000030
937 #define T5555_MODULATION_FSK1 0x00000040
938 #define T5555_MODULATION_FSK2 0x00000050
939 #define T5555_MODULATION_BIPHASE 0x00000060
940 #define T5555_MODULATION_DIRECT 0x00000070
941 #define T5555_INVERT_OUTPUT 0x00000080
942 #define T5555_PSK_RF_2 0
943 #define T5555_PSK_RF_4 0x00000100
944 #define T5555_PSK_RF_8 0x00000200
945 #define T5555_USE_PWD 0x00000400
946 #define T5555_USE_AOR 0x00000800
947 #define T5555_BITRATE_SHIFT 12
948 #define T5555_FAST_WRITE 0x00004000
949 #define T5555_PAGE_SELECT 0x00008000
952 * Relevant times in microsecond
953 * To compensate antenna falling times shorten the write times
954 * and enlarge the gap ones.
956 #define START_GAP 250
957 #define WRITE_GAP 160
958 #define WRITE_0 144 // 192
959 #define WRITE_1 400 // 432 for T55x7; 448 for E5550
961 // Write one bit to card
962 void T55xxWriteBit(int bit
)
964 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
965 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
967 SpinDelayUs(WRITE_0
);
969 SpinDelayUs(WRITE_1
);
970 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
971 SpinDelayUs(WRITE_GAP
);
974 // Write one card block in page 0, no lock
975 void T55xxWriteBlock(uint32_t Data
, uint32_t Block
, uint32_t Pwd
, uint8_t PwdMode
)
979 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
980 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
982 // Give it a bit of time for the resonant antenna to settle.
983 // And for the tag to fully power up
986 // Now start writting
987 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
988 SpinDelayUs(START_GAP
);
992 T55xxWriteBit(0); //Page 0
995 for (i
= 0x80000000; i
!= 0; i
>>= 1)
996 T55xxWriteBit(Pwd
& i
);
1002 for (i
= 0x80000000; i
!= 0; i
>>= 1)
1003 T55xxWriteBit(Data
& i
);
1006 for (i
= 0x04; i
!= 0; i
>>= 1)
1007 T55xxWriteBit(Block
& i
);
1009 // Now perform write (nominal is 5.6 ms for T55x7 and 18ms for E5550,
1010 // so wait a little more)
1011 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1012 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
1014 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1017 // Read one card block in page 0
1018 void T55xxReadBlock(uint32_t Block
, uint32_t Pwd
, uint8_t PwdMode
)
1020 uint8_t *dest
= (uint8_t *)BigBuf
;
1024 // Clear destination buffer before sending the command
1025 memset(dest
, 128, m
);
1026 // Connect the A/D to the peak-detected low-frequency path.
1027 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
1028 // Now set up the SSC to get the ADC samples that are now streaming at us.
1032 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1033 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
1035 // Give it a bit of time for the resonant antenna to settle.
1036 // And for the tag to fully power up
1039 // Now start writting
1040 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1041 SpinDelayUs(START_GAP
);
1045 T55xxWriteBit(0); //Page 0
1048 for (i
= 0x80000000; i
!= 0; i
>>= 1)
1049 T55xxWriteBit(Pwd
& i
);
1054 for (i
= 0x04; i
!= 0; i
>>= 1)
1055 T55xxWriteBit(Block
& i
);
1057 // Turn field on to read the response
1058 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1059 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
1061 // Now do the acquisition
1064 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
) {
1065 AT91C_BASE_SSC
->SSC_THR
= 0x43;
1067 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
1068 dest
[i
] = (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
1069 // we don't care about actual value, only if it's more or less than a
1070 // threshold essentially we capture zero crossings for later analysis
1071 // if(dest[i] < 127) dest[i] = 0; else dest[i] = 1;
1077 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); // field off
1082 // Read card traceability data (page 1)
1083 void T55xxReadTrace(void){
1084 uint8_t *dest
= (uint8_t *)BigBuf
;
1088 // Clear destination buffer before sending the command
1089 memset(dest
, 128, m
);
1090 // Connect the A/D to the peak-detected low-frequency path.
1091 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
1092 // Now set up the SSC to get the ADC samples that are now streaming at us.
1096 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1097 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
1099 // Give it a bit of time for the resonant antenna to settle.
1100 // And for the tag to fully power up
1103 // Now start writting
1104 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1105 SpinDelayUs(START_GAP
);
1109 T55xxWriteBit(1); //Page 1
1111 // Turn field on to read the response
1112 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1113 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
1115 // Now do the acquisition
1118 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
) {
1119 AT91C_BASE_SSC
->SSC_THR
= 0x43;
1121 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
1122 dest
[i
] = (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
1128 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); // field off
1133 /*-------------- Cloning routines -----------*/
1134 // Copy HID id to card and setup block 0 config
1135 void CopyHIDtoT55x7(uint32_t hi2
, uint32_t hi
, uint32_t lo
, uint8_t longFMT
)
1137 int data1
=0, data2
=0, data3
=0, data4
=0, data5
=0, data6
=0; //up to six blocks for long format
1141 // Ensure no more than 84 bits supplied
1143 DbpString("Tags can only have 84 bits.");
1146 // Build the 6 data blocks for supplied 84bit ID
1148 data1
= 0x1D96A900; // load preamble (1D) & long format identifier (9E manchester encoded)
1149 for (int i
=0;i
<4;i
++) {
1150 if (hi2
& (1<<(19-i
)))
1151 data1
|= (1<<(((3-i
)*2)+1)); // 1 -> 10
1153 data1
|= (1<<((3-i
)*2)); // 0 -> 01
1157 for (int i
=0;i
<16;i
++) {
1158 if (hi2
& (1<<(15-i
)))
1159 data2
|= (1<<(((15-i
)*2)+1)); // 1 -> 10
1161 data2
|= (1<<((15-i
)*2)); // 0 -> 01
1165 for (int i
=0;i
<16;i
++) {
1166 if (hi
& (1<<(31-i
)))
1167 data3
|= (1<<(((15-i
)*2)+1)); // 1 -> 10
1169 data3
|= (1<<((15-i
)*2)); // 0 -> 01
1173 for (int i
=0;i
<16;i
++) {
1174 if (hi
& (1<<(15-i
)))
1175 data4
|= (1<<(((15-i
)*2)+1)); // 1 -> 10
1177 data4
|= (1<<((15-i
)*2)); // 0 -> 01
1181 for (int i
=0;i
<16;i
++) {
1182 if (lo
& (1<<(31-i
)))
1183 data5
|= (1<<(((15-i
)*2)+1)); // 1 -> 10
1185 data5
|= (1<<((15-i
)*2)); // 0 -> 01
1189 for (int i
=0;i
<16;i
++) {
1190 if (lo
& (1<<(15-i
)))
1191 data6
|= (1<<(((15-i
)*2)+1)); // 1 -> 10
1193 data6
|= (1<<((15-i
)*2)); // 0 -> 01
1197 // Ensure no more than 44 bits supplied
1199 DbpString("Tags can only have 44 bits.");
1203 // Build the 3 data blocks for supplied 44bit ID
1206 data1
= 0x1D000000; // load preamble
1208 for (int i
=0;i
<12;i
++) {
1209 if (hi
& (1<<(11-i
)))
1210 data1
|= (1<<(((11-i
)*2)+1)); // 1 -> 10
1212 data1
|= (1<<((11-i
)*2)); // 0 -> 01
1216 for (int i
=0;i
<16;i
++) {
1217 if (lo
& (1<<(31-i
)))
1218 data2
|= (1<<(((15-i
)*2)+1)); // 1 -> 10
1220 data2
|= (1<<((15-i
)*2)); // 0 -> 01
1224 for (int i
=0;i
<16;i
++) {
1225 if (lo
& (1<<(15-i
)))
1226 data3
|= (1<<(((15-i
)*2)+1)); // 1 -> 10
1228 data3
|= (1<<((15-i
)*2)); // 0 -> 01
1233 // Program the data blocks for supplied ID
1234 // and the block 0 for HID format
1235 T55xxWriteBlock(data1
,1,0,0);
1236 T55xxWriteBlock(data2
,2,0,0);
1237 T55xxWriteBlock(data3
,3,0,0);
1239 if (longFMT
) { // if long format there are 6 blocks
1240 T55xxWriteBlock(data4
,4,0,0);
1241 T55xxWriteBlock(data5
,5,0,0);
1242 T55xxWriteBlock(data6
,6,0,0);
1245 // Config for HID (RF/50, FSK2a, Maxblock=3 for short/6 for long)
1246 T55xxWriteBlock(T55x7_BITRATE_RF_50
|
1247 T55x7_MODULATION_FSK2a
|
1248 last_block
<< T55x7_MAXBLOCK_SHIFT
,
1256 void CopyIOtoT55x7(uint32_t hi
, uint32_t lo
, uint8_t longFMT
)
1258 int data1
=0, data2
=0; //up to six blocks for long format
1260 data1
= hi
; // load preamble
1264 // Program the data blocks for supplied ID
1265 // and the block 0 for HID format
1266 T55xxWriteBlock(data1
,1,0,0);
1267 T55xxWriteBlock(data2
,2,0,0);
1270 T55xxWriteBlock(0x00147040,0,0,0);
1276 // Define 9bit header for EM410x tags
1277 #define EM410X_HEADER 0x1FF
1278 #define EM410X_ID_LENGTH 40
1280 void WriteEM410x(uint32_t card
, uint32_t id_hi
, uint32_t id_lo
)
1283 uint64_t id
= EM410X_HEADER
;
1284 uint64_t rev_id
= 0; // reversed ID
1285 int c_parity
[4]; // column parity
1286 int r_parity
= 0; // row parity
1289 // Reverse ID bits given as parameter (for simpler operations)
1290 for (i
= 0; i
< EM410X_ID_LENGTH
; ++i
) {
1292 rev_id
= (rev_id
<< 1) | (id_lo
& 1);
1295 rev_id
= (rev_id
<< 1) | (id_hi
& 1);
1300 for (i
= 0; i
< EM410X_ID_LENGTH
; ++i
) {
1301 id_bit
= rev_id
& 1;
1304 // Don't write row parity bit at start of parsing
1306 id
= (id
<< 1) | r_parity
;
1307 // Start counting parity for new row
1314 // First elements in column?
1316 // Fill out first elements
1317 c_parity
[i
] = id_bit
;
1319 // Count column parity
1320 c_parity
[i
% 4] ^= id_bit
;
1323 id
= (id
<< 1) | id_bit
;
1327 // Insert parity bit of last row
1328 id
= (id
<< 1) | r_parity
;
1330 // Fill out column parity at the end of tag
1331 for (i
= 0; i
< 4; ++i
)
1332 id
= (id
<< 1) | c_parity
[i
];
1337 Dbprintf("Started writing %s tag ...", card
? "T55x7":"T5555");
1341 T55xxWriteBlock((uint32_t)(id
>> 32), 1, 0, 0);
1342 T55xxWriteBlock((uint32_t)id
, 2, 0, 0);
1344 // Config for EM410x (RF/64, Manchester, Maxblock=2)
1346 // Clock rate is stored in bits 8-15 of the card value
1347 clock
= (card
& 0xFF00) >> 8;
1348 Dbprintf("Clock rate: %d", clock
);
1352 clock
= T55x7_BITRATE_RF_32
;
1355 clock
= T55x7_BITRATE_RF_16
;
1358 // A value of 0 is assumed to be 64 for backwards-compatibility
1361 clock
= T55x7_BITRATE_RF_64
;
1364 Dbprintf("Invalid clock rate: %d", clock
);
1368 // Writing configuration for T55x7 tag
1369 T55xxWriteBlock(clock
|
1370 T55x7_MODULATION_MANCHESTER
|
1371 2 << T55x7_MAXBLOCK_SHIFT
,
1375 // Writing configuration for T5555(Q5) tag
1376 T55xxWriteBlock(0x1F << T5555_BITRATE_SHIFT
|
1377 T5555_MODULATION_MANCHESTER
|
1378 2 << T5555_MAXBLOCK_SHIFT
,
1382 Dbprintf("Tag %s written with 0x%08x%08x\n", card
? "T55x7":"T5555",
1383 (uint32_t)(id
>> 32), (uint32_t)id
);
1386 // Clone Indala 64-bit tag by UID to T55x7
1387 void CopyIndala64toT55x7(int hi
, int lo
)
1390 //Program the 2 data blocks for supplied 64bit UID
1391 // and the block 0 for Indala64 format
1392 T55xxWriteBlock(hi
,1,0,0);
1393 T55xxWriteBlock(lo
,2,0,0);
1394 //Config for Indala (RF/32;PSK1 with RF/2;Maxblock=2)
1395 T55xxWriteBlock(T55x7_BITRATE_RF_32
|
1396 T55x7_MODULATION_PSK1
|
1397 2 << T55x7_MAXBLOCK_SHIFT
,
1399 //Alternative config for Indala (Extended mode;RF/32;PSK1 with RF/2;Maxblock=2;Inverse data)
1400 // T5567WriteBlock(0x603E1042,0);
1406 void CopyIndala224toT55x7(int uid1
, int uid2
, int uid3
, int uid4
, int uid5
, int uid6
, int uid7
)
1409 //Program the 7 data blocks for supplied 224bit UID
1410 // and the block 0 for Indala224 format
1411 T55xxWriteBlock(uid1
,1,0,0);
1412 T55xxWriteBlock(uid2
,2,0,0);
1413 T55xxWriteBlock(uid3
,3,0,0);
1414 T55xxWriteBlock(uid4
,4,0,0);
1415 T55xxWriteBlock(uid5
,5,0,0);
1416 T55xxWriteBlock(uid6
,6,0,0);
1417 T55xxWriteBlock(uid7
,7,0,0);
1418 //Config for Indala (RF/32;PSK1 with RF/2;Maxblock=7)
1419 T55xxWriteBlock(T55x7_BITRATE_RF_32
|
1420 T55x7_MODULATION_PSK1
|
1421 7 << T55x7_MAXBLOCK_SHIFT
,
1423 //Alternative config for Indala (Extended mode;RF/32;PSK1 with RF/2;Maxblock=7;Inverse data)
1424 // T5567WriteBlock(0x603E10E2,0);
1431 #define abs(x) ( ((x)<0) ? -(x) : (x) )
1432 #define max(x,y) ( x<y ? y:x)
1434 int DemodPCF7931(uint8_t **outBlocks
) {
1435 uint8_t BitStream
[256];
1436 uint8_t Blocks
[8][16];
1437 uint8_t *GraphBuffer
= (uint8_t *)BigBuf
;
1438 int GraphTraceLen
= sizeof(BigBuf
);
1439 int i
, j
, lastval
, bitidx
, half_switch
;
1441 int tolerance
= clock
/ 8;
1442 int pmc
, block_done
;
1443 int lc
, warnings
= 0;
1445 int lmin
=128, lmax
=128;
1448 AcquireRawAdcSamples125k(0);
1455 /* Find first local max/min */
1456 if(GraphBuffer
[1] > GraphBuffer
[0]) {
1457 while(i
< GraphTraceLen
) {
1458 if( !(GraphBuffer
[i
] > GraphBuffer
[i
-1]) && GraphBuffer
[i
] > lmax
)
1465 while(i
< GraphTraceLen
) {
1466 if( !(GraphBuffer
[i
] < GraphBuffer
[i
-1]) && GraphBuffer
[i
] < lmin
)
1478 for (bitidx
= 0; i
< GraphTraceLen
; i
++)
1480 if ( (GraphBuffer
[i
-1] > GraphBuffer
[i
] && dir
== 1 && GraphBuffer
[i
] > lmax
) || (GraphBuffer
[i
-1] < GraphBuffer
[i
] && dir
== 0 && GraphBuffer
[i
] < lmin
))
1485 // Switch depending on lc length:
1486 // Tolerance is 1/8 of clock rate (arbitrary)
1487 if (abs(lc
-clock
/4) < tolerance
) {
1489 if((i
- pmc
) == lc
) { /* 16T0 was previous one */
1491 i
+= (128+127+16+32+33+16)-1;
1499 } else if (abs(lc
-clock
/2) < tolerance
) {
1501 if((i
- pmc
) == lc
) { /* 16T0 was previous one */
1503 i
+= (128+127+16+32+33)-1;
1508 else if(half_switch
== 1) {
1509 BitStream
[bitidx
++] = 0;
1514 } else if (abs(lc
-clock
) < tolerance
) {
1516 BitStream
[bitidx
++] = 1;
1522 Dbprintf("Error: too many detection errors, aborting.");
1527 if(block_done
== 1) {
1529 for(j
=0; j
<16; j
++) {
1530 Blocks
[num_blocks
][j
] = 128*BitStream
[j
*8+7]+
1531 64*BitStream
[j
*8+6]+
1532 32*BitStream
[j
*8+5]+
1533 16*BitStream
[j
*8+4]+
1545 if (GraphBuffer
[i
-1] > GraphBuffer
[i
]) dir
=0;
1551 if(num_blocks
== 4) break;
1553 memcpy(outBlocks
, Blocks
, 16*num_blocks
);
1557 int IsBlock0PCF7931(uint8_t *Block
) {
1558 // Assume RFU means 0 :)
1559 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
1561 if((memcmp(Block
+9, "\x00\x00\x00\x00\x00\x00\x00", 7) == 0) && Block
[7] == 0) // PAC disabled, can it *really* happen ?
1566 int IsBlock1PCF7931(uint8_t *Block
) {
1567 // Assume RFU means 0 :)
1568 if(Block
[10] == 0 && Block
[11] == 0 && Block
[12] == 0 && Block
[13] == 0)
1569 if((Block
[14] & 0x7f) <= 9 && Block
[15] <= 9)
1577 void ReadPCF7931() {
1578 uint8_t Blocks
[8][17];
1579 uint8_t tmpBlocks
[4][16];
1580 int i
, j
, ind
, ind2
, n
;
1587 memset(Blocks
, 0, 8*17*sizeof(uint8_t));
1590 memset(tmpBlocks
, 0, 4*16*sizeof(uint8_t));
1591 n
= DemodPCF7931((uint8_t**)tmpBlocks
);
1594 if(error
==10 && num_blocks
== 0) {
1595 Dbprintf("Error, no tag or bad tag");
1598 else if (tries
==20 || error
==10) {
1599 Dbprintf("Error reading the tag");
1600 Dbprintf("Here is the partial content");
1605 Dbprintf("(dbg) %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
1606 tmpBlocks
[i
][0], tmpBlocks
[i
][1], tmpBlocks
[i
][2], tmpBlocks
[i
][3], tmpBlocks
[i
][4], tmpBlocks
[i
][5], tmpBlocks
[i
][6], tmpBlocks
[i
][7],
1607 tmpBlocks
[i
][8], tmpBlocks
[i
][9], tmpBlocks
[i
][10], tmpBlocks
[i
][11], tmpBlocks
[i
][12], tmpBlocks
[i
][13], tmpBlocks
[i
][14], tmpBlocks
[i
][15]);
1609 for(i
=0; i
<n
; i
++) {
1610 if(IsBlock0PCF7931(tmpBlocks
[i
])) {
1612 if(i
< n
-1 && IsBlock1PCF7931(tmpBlocks
[i
+1])) {
1616 memcpy(Blocks
[0], tmpBlocks
[i
], 16);
1617 Blocks
[0][ALLOC
] = 1;
1618 memcpy(Blocks
[1], tmpBlocks
[i
+1], 16);
1619 Blocks
[1][ALLOC
] = 1;
1620 max_blocks
= max((Blocks
[1][14] & 0x7f), Blocks
[1][15]) + 1;
1622 Dbprintf("(dbg) Max blocks: %d", max_blocks
);
1624 // Handle following blocks
1625 for(j
=i
+2, ind2
=2; j
!=i
; j
++, ind2
++, num_blocks
++) {
1628 memcpy(Blocks
[ind2
], tmpBlocks
[j
], 16);
1629 Blocks
[ind2
][ALLOC
] = 1;
1637 for(i
=0; i
<n
; i
++) { // Look for identical block in known blocks
1638 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
1639 for(j
=0; j
<max_blocks
; j
++) {
1640 if(Blocks
[j
][ALLOC
] == 1 && !memcmp(tmpBlocks
[i
], Blocks
[j
], 16)) {
1641 // Found an identical block
1642 for(ind
=i
-1,ind2
=j
-1; ind
>= 0; ind
--,ind2
--) {
1645 if(!Blocks
[ind2
][ALLOC
]) { // Block ind2 not already found
1646 // Dbprintf("Tmp %d -> Block %d", ind, ind2);
1647 memcpy(Blocks
[ind2
], tmpBlocks
[ind
], 16);
1648 Blocks
[ind2
][ALLOC
] = 1;
1650 if(num_blocks
== max_blocks
) goto end
;
1653 for(ind
=i
+1,ind2
=j
+1; ind
< n
; ind
++,ind2
++) {
1654 if(ind2
> max_blocks
)
1656 if(!Blocks
[ind2
][ALLOC
]) { // Block ind2 not already found
1657 // Dbprintf("Tmp %d -> Block %d", ind, ind2);
1658 memcpy(Blocks
[ind2
], tmpBlocks
[ind
], 16);
1659 Blocks
[ind2
][ALLOC
] = 1;
1661 if(num_blocks
== max_blocks
) goto end
;
1670 if (BUTTON_PRESS()) return;
1671 } while (num_blocks
!= max_blocks
);
1673 Dbprintf("-----------------------------------------");
1674 Dbprintf("Memory content:");
1675 Dbprintf("-----------------------------------------");
1676 for(i
=0; i
<max_blocks
; i
++) {
1677 if(Blocks
[i
][ALLOC
]==1)
1678 Dbprintf("%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
1679 Blocks
[i
][0], Blocks
[i
][1], Blocks
[i
][2], Blocks
[i
][3], Blocks
[i
][4], Blocks
[i
][5], Blocks
[i
][6], Blocks
[i
][7],
1680 Blocks
[i
][8], Blocks
[i
][9], Blocks
[i
][10], Blocks
[i
][11], Blocks
[i
][12], Blocks
[i
][13], Blocks
[i
][14], Blocks
[i
][15]);
1682 Dbprintf("<missing block %d>", i
);
1684 Dbprintf("-----------------------------------------");
1690 //-----------------------------------
1691 // EM4469 / EM4305 routines
1692 //-----------------------------------
1693 #define FWD_CMD_LOGIN 0xC //including the even parity, binary mirrored
1694 #define FWD_CMD_WRITE 0xA
1695 #define FWD_CMD_READ 0x9
1696 #define FWD_CMD_DISABLE 0x5
1699 uint8_t forwardLink_data
[64]; //array of forwarded bits
1700 uint8_t * forward_ptr
; //ptr for forward message preparation
1701 uint8_t fwd_bit_sz
; //forwardlink bit counter
1702 uint8_t * fwd_write_ptr
; //forwardlink bit pointer
1704 //====================================================================
1705 // prepares command bits
1707 //====================================================================
1708 //--------------------------------------------------------------------
1709 uint8_t Prepare_Cmd( uint8_t cmd
) {
1710 //--------------------------------------------------------------------
1712 *forward_ptr
++ = 0; //start bit
1713 *forward_ptr
++ = 0; //second pause for 4050 code
1715 *forward_ptr
++ = cmd
;
1717 *forward_ptr
++ = cmd
;
1719 *forward_ptr
++ = cmd
;
1721 *forward_ptr
++ = cmd
;
1723 return 6; //return number of emited bits
1726 //====================================================================
1727 // prepares address bits
1729 //====================================================================
1731 //--------------------------------------------------------------------
1732 uint8_t Prepare_Addr( uint8_t addr
) {
1733 //--------------------------------------------------------------------
1735 register uint8_t line_parity
;
1740 *forward_ptr
++ = addr
;
1741 line_parity
^= addr
;
1745 *forward_ptr
++ = (line_parity
& 1);
1747 return 7; //return number of emited bits
1750 //====================================================================
1751 // prepares data bits intreleaved with parity bits
1753 //====================================================================
1755 //--------------------------------------------------------------------
1756 uint8_t Prepare_Data( uint16_t data_low
, uint16_t data_hi
) {
1757 //--------------------------------------------------------------------
1759 register uint8_t line_parity
;
1760 register uint8_t column_parity
;
1761 register uint8_t i
, j
;
1762 register uint16_t data
;
1767 for(i
=0; i
<4; i
++) {
1769 for(j
=0; j
<8; j
++) {
1770 line_parity
^= data
;
1771 column_parity
^= (data
& 1) << j
;
1772 *forward_ptr
++ = data
;
1775 *forward_ptr
++ = line_parity
;
1780 for(j
=0; j
<8; j
++) {
1781 *forward_ptr
++ = column_parity
;
1782 column_parity
>>= 1;
1786 return 45; //return number of emited bits
1789 //====================================================================
1790 // Forward Link send function
1791 // Requires: forwarLink_data filled with valid bits (1 bit per byte)
1792 // fwd_bit_count set with number of bits to be sent
1793 //====================================================================
1794 void SendForward(uint8_t fwd_bit_count
) {
1796 fwd_write_ptr
= forwardLink_data
;
1797 fwd_bit_sz
= fwd_bit_count
;
1802 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1803 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
1805 // Give it a bit of time for the resonant antenna to settle.
1806 // And for the tag to fully power up
1809 // force 1st mod pulse (start gap must be longer for 4305)
1810 fwd_bit_sz
--; //prepare next bit modulation
1812 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); // field off
1813 SpinDelayUs(55*8); //55 cycles off (8us each)for 4305
1814 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1815 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);//field on
1816 SpinDelayUs(16*8); //16 cycles on (8us each)
1818 // now start writting
1819 while(fwd_bit_sz
-- > 0) { //prepare next bit modulation
1820 if(((*fwd_write_ptr
++) & 1) == 1)
1821 SpinDelayUs(32*8); //32 cycles at 125Khz (8us each)
1823 //These timings work for 4469/4269/4305 (with the 55*8 above)
1824 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); // field off
1825 SpinDelayUs(23*8); //16-4 cycles off (8us each)
1826 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1827 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);//field on
1828 SpinDelayUs(9*8); //16 cycles on (8us each)
1833 void EM4xLogin(uint32_t Password
) {
1835 uint8_t fwd_bit_count
;
1837 forward_ptr
= forwardLink_data
;
1838 fwd_bit_count
= Prepare_Cmd( FWD_CMD_LOGIN
);
1839 fwd_bit_count
+= Prepare_Data( Password
&0xFFFF, Password
>>16 );
1841 SendForward(fwd_bit_count
);
1843 //Wait for command to complete
1848 void EM4xReadWord(uint8_t Address
, uint32_t Pwd
, uint8_t PwdMode
) {
1850 uint8_t fwd_bit_count
;
1851 uint8_t *dest
= (uint8_t *)BigBuf
;
1854 //If password mode do login
1855 if (PwdMode
== 1) EM4xLogin(Pwd
);
1857 forward_ptr
= forwardLink_data
;
1858 fwd_bit_count
= Prepare_Cmd( FWD_CMD_READ
);
1859 fwd_bit_count
+= Prepare_Addr( Address
);
1862 // Clear destination buffer before sending the command
1863 memset(dest
, 128, m
);
1864 // Connect the A/D to the peak-detected low-frequency path.
1865 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
1866 // Now set up the SSC to get the ADC samples that are now streaming at us.
1869 SendForward(fwd_bit_count
);
1871 // Now do the acquisition
1874 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
) {
1875 AT91C_BASE_SSC
->SSC_THR
= 0x43;
1877 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
1878 dest
[i
] = (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
1883 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); // field off
1887 void EM4xWriteWord(uint32_t Data
, uint8_t Address
, uint32_t Pwd
, uint8_t PwdMode
) {
1889 uint8_t fwd_bit_count
;
1891 //If password mode do login
1892 if (PwdMode
== 1) EM4xLogin(Pwd
);
1894 forward_ptr
= forwardLink_data
;
1895 fwd_bit_count
= Prepare_Cmd( FWD_CMD_WRITE
);
1896 fwd_bit_count
+= Prepare_Addr( Address
);
1897 fwd_bit_count
+= Prepare_Data( Data
&0xFFFF, Data
>>16 );
1899 SendForward(fwd_bit_count
);
1901 //Wait for write to complete
1903 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); // field off