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 at134khz
)
21 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
23 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
25 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
27 // Connect the A/D to the peak-detected low-frequency path.
28 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
30 // Give it a bit of time for the resonant antenna to settle.
33 // Now set up the SSC to get the ADC samples that are now streaming at us.
36 // Now call the acquisition routine
40 // split into two routines so we can avoid timing issues after sending commands //
41 void DoAcquisition125k(void)
43 uint8_t *dest
= (uint8_t *)BigBuf
;
44 int n
= sizeof(BigBuf
);
50 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
) {
51 AT91C_BASE_SSC
->SSC_THR
= 0x43;
54 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
55 dest
[i
] = (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
61 Dbprintf("buffer samples: %02x %02x %02x %02x %02x %02x %02x %02x ...",
62 dest
[0], dest
[1], dest
[2], dest
[3], dest
[4], dest
[5], dest
[6], dest
[7]);
65 void ModThenAcquireRawAdcSamples125k(int delay_off
, int period_0
, int period_1
, uint8_t *command
)
69 /* Make sure the tag is reset */
70 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
73 // see if 'h' was specified
74 if (command
[strlen((char *) command
) - 1] == 'h')
80 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
82 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
84 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
86 // Give it a bit of time for the resonant antenna to settle.
88 // And a little more time for the tag to fully power up
91 // Now set up the SSC to get the ADC samples that are now streaming at us.
94 // now modulate the reader field
95 while(*command
!= '\0' && *command
!= ' ') {
96 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
98 SpinDelayUs(delay_off
);
100 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
102 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
104 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
106 if(*(command
++) == '0')
107 SpinDelayUs(period_0
);
109 SpinDelayUs(period_1
);
111 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
113 SpinDelayUs(delay_off
);
115 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
117 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
119 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
125 /* blank r/w tag data stream
126 ...0000000000000000 01111111
127 1010101010101010101010101010101010101010101010101010101010101010
130 101010101010101[0]000...
132 [5555fe852c5555555555555555fe0000]
136 // some hardcoded initial params
137 // when we read a TI tag we sample the zerocross line at 2Mhz
138 // TI tags modulate a 1 as 16 cycles of 123.2Khz
139 // TI tags modulate a 0 as 16 cycles of 134.2Khz
140 #define FSAMPLE 2000000
141 #define FREQLO 123200
142 #define FREQHI 134200
144 signed char *dest
= (signed char *)BigBuf
;
145 int n
= sizeof(BigBuf
);
146 // int *dest = GraphBuffer;
147 // int n = GraphTraceLen;
149 // 128 bit shift register [shift3:shift2:shift1:shift0]
150 uint32_t shift3
= 0, shift2
= 0, shift1
= 0, shift0
= 0;
152 int i
, cycles
=0, samples
=0;
153 // how many sample points fit in 16 cycles of each frequency
154 uint32_t sampleslo
= (FSAMPLE
<<4)/FREQLO
, sampleshi
= (FSAMPLE
<<4)/FREQHI
;
155 // when to tell if we're close enough to one freq or another
156 uint32_t threshold
= (sampleslo
- sampleshi
+ 1)>>1;
158 // TI tags charge at 134.2Khz
159 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
161 // Place FPGA in passthrough mode, in this mode the CROSS_LO line
162 // connects to SSP_DIN and the SSP_DOUT logic level controls
163 // whether we're modulating the antenna (high)
164 // or listening to the antenna (low)
165 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_PASSTHRU
);
167 // get TI tag data into the buffer
170 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
172 for (i
=0; i
<n
-1; i
++) {
173 // count cycles by looking for lo to hi zero crossings
174 if ( (dest
[i
]<0) && (dest
[i
+1]>0) ) {
176 // after 16 cycles, measure the frequency
179 samples
=i
-samples
; // number of samples in these 16 cycles
181 // TI bits are coming to us lsb first so shift them
182 // right through our 128 bit right shift register
183 shift0
= (shift0
>>1) | (shift1
<< 31);
184 shift1
= (shift1
>>1) | (shift2
<< 31);
185 shift2
= (shift2
>>1) | (shift3
<< 31);
188 // check if the cycles fall close to the number
189 // expected for either the low or high frequency
190 if ( (samples
>(sampleslo
-threshold
)) && (samples
<(sampleslo
+threshold
)) ) {
191 // low frequency represents a 1
193 } else if ( (samples
>(sampleshi
-threshold
)) && (samples
<(sampleshi
+threshold
)) ) {
194 // high frequency represents a 0
196 // probably detected a gay waveform or noise
197 // use this as gaydar or discard shift register and start again
198 shift3
= shift2
= shift1
= shift0
= 0;
202 // for each bit we receive, test if we've detected a valid tag
204 // if we see 17 zeroes followed by 6 ones, we might have a tag
205 // remember the bits are backwards
206 if ( ((shift0
& 0x7fffff) == 0x7e0000) ) {
207 // if start and end bytes match, we have a tag so break out of the loop
208 if ( ((shift0
>>16)&0xff) == ((shift3
>>8)&0xff) ) {
209 cycles
= 0xF0B; //use this as a flag (ugly but whatever)
217 // if flag is set we have a tag
219 DbpString("Info: No valid tag detected.");
221 // put 64 bit data into shift1 and shift0
222 shift0
= (shift0
>>24) | (shift1
<< 8);
223 shift1
= (shift1
>>24) | (shift2
<< 8);
225 // align 16 bit crc into lower half of shift2
226 shift2
= ((shift2
>>24) | (shift3
<< 8)) & 0x0ffff;
228 // if r/w tag, check ident match
229 if ( shift3
&(1<<15) ) {
230 DbpString("Info: TI tag is rewriteable");
231 // only 15 bits compare, last bit of ident is not valid
232 if ( ((shift3
>>16)^shift0
)&0x7fff ) {
233 DbpString("Error: Ident mismatch!");
235 DbpString("Info: TI tag ident is valid");
238 DbpString("Info: TI tag is readonly");
241 // WARNING the order of the bytes in which we calc crc below needs checking
242 // i'm 99% sure the crc algorithm is correct, but it may need to eat the
243 // bytes in reverse or something
247 crc
= update_crc16(crc
, (shift0
)&0xff);
248 crc
= update_crc16(crc
, (shift0
>>8)&0xff);
249 crc
= update_crc16(crc
, (shift0
>>16)&0xff);
250 crc
= update_crc16(crc
, (shift0
>>24)&0xff);
251 crc
= update_crc16(crc
, (shift1
)&0xff);
252 crc
= update_crc16(crc
, (shift1
>>8)&0xff);
253 crc
= update_crc16(crc
, (shift1
>>16)&0xff);
254 crc
= update_crc16(crc
, (shift1
>>24)&0xff);
256 Dbprintf("Info: Tag data: %x%08x, crc=%x",
257 (unsigned int)shift1
, (unsigned int)shift0
, (unsigned int)shift2
& 0xFFFF);
258 if (crc
!= (shift2
&0xffff)) {
259 Dbprintf("Error: CRC mismatch, expected %x", (unsigned int)crc
);
261 DbpString("Info: CRC is good");
266 void WriteTIbyte(uint8_t b
)
270 // modulate 8 bits out to the antenna
274 // stop modulating antenna
281 // stop modulating antenna
291 void AcquireTiType(void)
294 // tag transmission is <20ms, sampling at 2M gives us 40K samples max
295 // each sample is 1 bit stuffed into a uint32_t so we need 1250 uint32_t
296 #define TIBUFLEN 1250
299 memset(BigBuf
,0,sizeof(BigBuf
));
301 // Set up the synchronous serial port
302 AT91C_BASE_PIOA
->PIO_PDR
= GPIO_SSC_DIN
;
303 AT91C_BASE_PIOA
->PIO_ASR
= GPIO_SSC_DIN
;
305 // steal this pin from the SSP and use it to control the modulation
306 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
307 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
309 AT91C_BASE_SSC
->SSC_CR
= AT91C_SSC_SWRST
;
310 AT91C_BASE_SSC
->SSC_CR
= AT91C_SSC_RXEN
| AT91C_SSC_TXEN
;
312 // Sample at 2 Mbit/s, so TI tags are 16.2 vs. 14.9 clocks long
313 // 48/2 = 24 MHz clock must be divided by 12
314 AT91C_BASE_SSC
->SSC_CMR
= 12;
316 AT91C_BASE_SSC
->SSC_RCMR
= SSC_CLOCK_MODE_SELECT(0);
317 AT91C_BASE_SSC
->SSC_RFMR
= SSC_FRAME_MODE_BITS_IN_WORD(32) | AT91C_SSC_MSBF
;
318 AT91C_BASE_SSC
->SSC_TCMR
= 0;
319 AT91C_BASE_SSC
->SSC_TFMR
= 0;
326 // Charge TI tag for 50ms.
329 // stop modulating antenna and listen
336 if(AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
337 BigBuf
[i
] = AT91C_BASE_SSC
->SSC_RHR
; // store 32 bit values in buffer
338 i
++; if(i
>= TIBUFLEN
) break;
343 // return stolen pin to SSP
344 AT91C_BASE_PIOA
->PIO_PDR
= GPIO_SSC_DOUT
;
345 AT91C_BASE_PIOA
->PIO_ASR
= GPIO_SSC_DIN
| GPIO_SSC_DOUT
;
347 char *dest
= (char *)BigBuf
;
350 for (i
=TIBUFLEN
-1; i
>=0; i
--) {
351 for (j
=0; j
<32; j
++) {
352 if(BigBuf
[i
] & (1 << j
)) {
361 // arguments: 64bit data split into 32bit idhi:idlo and optional 16bit crc
362 // if crc provided, it will be written with the data verbatim (even if bogus)
363 // if not provided a valid crc will be computed from the data and written.
364 void WriteTItag(uint32_t idhi
, uint32_t idlo
, uint16_t crc
)
367 crc
= update_crc16(crc
, (idlo
)&0xff);
368 crc
= update_crc16(crc
, (idlo
>>8)&0xff);
369 crc
= update_crc16(crc
, (idlo
>>16)&0xff);
370 crc
= update_crc16(crc
, (idlo
>>24)&0xff);
371 crc
= update_crc16(crc
, (idhi
)&0xff);
372 crc
= update_crc16(crc
, (idhi
>>8)&0xff);
373 crc
= update_crc16(crc
, (idhi
>>16)&0xff);
374 crc
= update_crc16(crc
, (idhi
>>24)&0xff);
376 Dbprintf("Writing to tag: %x%08x, crc=%x",
377 (unsigned int) idhi
, (unsigned int) idlo
, crc
);
379 // TI tags charge at 134.2Khz
380 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
381 // Place FPGA in passthrough mode, in this mode the CROSS_LO line
382 // connects to SSP_DIN and the SSP_DOUT logic level controls
383 // whether we're modulating the antenna (high)
384 // or listening to the antenna (low)
385 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_PASSTHRU
);
388 // steal this pin from the SSP and use it to control the modulation
389 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
390 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
392 // writing algorithm:
393 // a high bit consists of a field off for 1ms and field on for 1ms
394 // a low bit consists of a field off for 0.3ms and field on for 1.7ms
395 // initiate a charge time of 50ms (field on) then immediately start writing bits
396 // start by writing 0xBB (keyword) and 0xEB (password)
397 // then write 80 bits of data (or 64 bit data + 16 bit crc if you prefer)
398 // finally end with 0x0300 (write frame)
399 // all data is sent lsb firts
400 // finish with 15ms programming time
404 SpinDelay(50); // charge time
406 WriteTIbyte(0xbb); // keyword
407 WriteTIbyte(0xeb); // password
408 WriteTIbyte( (idlo
)&0xff );
409 WriteTIbyte( (idlo
>>8 )&0xff );
410 WriteTIbyte( (idlo
>>16)&0xff );
411 WriteTIbyte( (idlo
>>24)&0xff );
412 WriteTIbyte( (idhi
)&0xff );
413 WriteTIbyte( (idhi
>>8 )&0xff );
414 WriteTIbyte( (idhi
>>16)&0xff );
415 WriteTIbyte( (idhi
>>24)&0xff ); // data hi to lo
416 WriteTIbyte( (crc
)&0xff ); // crc lo
417 WriteTIbyte( (crc
>>8 )&0xff ); // crc hi
418 WriteTIbyte(0x00); // write frame lo
419 WriteTIbyte(0x03); // write frame hi
421 SpinDelay(50); // programming time
425 // get TI tag data into the buffer
428 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
429 DbpString("Now use tiread to check");
432 void SimulateTagLowFrequency(int period
, int gap
, int ledcontrol
)
435 uint8_t *tab
= (uint8_t *)BigBuf
;
437 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_EDGE_DETECT
);
439 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
| GPIO_SSC_CLK
;
441 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
442 AT91C_BASE_PIOA
->PIO_ODR
= GPIO_SSC_CLK
;
444 #define SHORT_COIL() LOW(GPIO_SSC_DOUT)
445 #define OPEN_COIL() HIGH(GPIO_SSC_DOUT)
449 while(!(AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_CLK
)) {
451 DbpString("Stopped");
468 while(AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_CLK
) {
470 DbpString("Stopped");
487 #define DEBUG_FRAME_CONTENTS 1
488 void SimulateTagLowFrequencyBidir(int divisor
, int t0
)
492 // compose fc/8 fc/10 waveform
493 static void fc(int c
, int *n
) {
494 uint8_t *dest
= (uint8_t *)BigBuf
;
497 // for when we want an fc8 pattern every 4 logical bits
508 // an fc/8 encoded bit is a bit pattern of 11000000 x6 = 48 samples
510 for (idx
=0; idx
<6; idx
++) {
522 // an fc/10 encoded bit is a bit pattern of 1110000000 x5 = 50 samples
524 for (idx
=0; idx
<5; idx
++) {
539 // prepare a waveform pattern in the buffer based on the ID given then
540 // simulate a HID tag until the button is pressed
541 void CmdHIDsimTAG(int hi
, int lo
, int ledcontrol
)
545 HID tag bitstream format
546 The tag contains a 44bit unique code. This is sent out MSB first in sets of 4 bits
547 A 1 bit is represented as 6 fc8 and 5 fc10 patterns
548 A 0 bit is represented as 5 fc10 and 6 fc8 patterns
549 A fc8 is inserted before every 4 bits
550 A special start of frame pattern is used consisting a0b0 where a and b are neither 0
551 nor 1 bits, they are special patterns (a = set of 12 fc8 and b = set of 10 fc10)
555 DbpString("Tags can only have 44 bits.");
559 // special start of frame marker containing invalid bit sequences
560 fc(8, &n
); fc(8, &n
); // invalid
561 fc(8, &n
); fc(10, &n
); // logical 0
562 fc(10, &n
); fc(10, &n
); // invalid
563 fc(8, &n
); fc(10, &n
); // logical 0
566 // manchester encode bits 43 to 32
567 for (i
=11; i
>=0; i
--) {
568 if ((i
%4)==3) fc(0,&n
);
570 fc(10, &n
); fc(8, &n
); // low-high transition
572 fc(8, &n
); fc(10, &n
); // high-low transition
577 // manchester encode bits 31 to 0
578 for (i
=31; i
>=0; i
--) {
579 if ((i
%4)==3) fc(0,&n
);
581 fc(10, &n
); fc(8, &n
); // low-high transition
583 fc(8, &n
); fc(10, &n
); // high-low transition
589 SimulateTagLowFrequency(n
, 0, ledcontrol
);
596 // loop to capture raw HID waveform then FSK demodulate the TAG ID from it
597 void CmdHIDdemodFSK(int findone
, int *high
, int *low
, int ledcontrol
)
599 uint8_t *dest
= (uint8_t *)BigBuf
;
600 int m
=0, n
=0, i
=0, idx
=0, found
=0, lastval
=0;
601 uint32_t hi2
=0, hi
=0, lo
=0;
603 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
604 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
606 // Connect the A/D to the peak-detected low-frequency path.
607 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
609 // Give it a bit of time for the resonant antenna to settle.
612 // Now set up the SSC to get the ADC samples that are now streaming at us.
620 DbpString("Stopped");
630 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
631 AT91C_BASE_SSC
->SSC_THR
= 0x43;
635 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
636 dest
[i
] = (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
637 // we don't care about actual value, only if it's more or less than a
638 // threshold essentially we capture zero crossings for later analysis
639 if(dest
[i
] < 127) dest
[i
] = 0; else dest
[i
] = 1;
651 // sync to first lo-hi transition
652 for( idx
=1; idx
<m
; idx
++) {
653 if (dest
[idx
-1]<dest
[idx
])
659 // count cycles between consecutive lo-hi transitions, there should be either 8 (fc/8)
660 // or 10 (fc/10) cycles but in practice due to noise etc we may end up with with anywhere
661 // between 7 to 11 cycles so fuzz it by treat anything <9 as 8 and anything else as 10
662 for( i
=0; idx
<m
; idx
++) {
663 if (dest
[idx
-1]<dest
[idx
]) {
678 // we now have a set of cycle counts, loop over previous results and aggregate data into bit patterns
683 for( idx
=0; idx
<m
; idx
++) {
684 if (dest
[idx
]==lastval
) {
687 // a bit time is five fc/10 or six fc/8 cycles so figure out how many bits a pattern width represents,
688 // an extra fc/8 pattern preceeds every 4 bits (about 200 cycles) just to complicate things but it gets
689 // swallowed up by rounding
690 // expected results are 1 or 2 bits, any more and it's an invalid manchester encoding
691 // special start of frame markers use invalid manchester states (no transitions) by using sequences
694 n
=(n
+1)/6; // fc/8 in sets of 6
696 n
=(n
+1)/5; // fc/10 in sets of 5
698 switch (n
) { // stuff appropriate bits in buffer
701 dest
[i
++]=dest
[idx
-1];
704 dest
[i
++]=dest
[idx
-1];
705 dest
[i
++]=dest
[idx
-1];
707 case 3: // 3 bit start of frame markers
708 dest
[i
++]=dest
[idx
-1];
709 dest
[i
++]=dest
[idx
-1];
710 dest
[i
++]=dest
[idx
-1];
712 // When a logic 0 is immediately followed by the start of the next transmisson
713 // (special pattern) a pattern of 4 bit duration lengths is created.
715 dest
[i
++]=dest
[idx
-1];
716 dest
[i
++]=dest
[idx
-1];
717 dest
[i
++]=dest
[idx
-1];
718 dest
[i
++]=dest
[idx
-1];
720 default: // this shouldn't happen, don't stuff any bits
730 // final loop, go over previously decoded manchester data and decode into usable tag ID
731 // 111000 bit pattern represent start of frame, 01 pattern represents a 1 and 10 represents a 0
732 for( idx
=0; idx
<m
-6; idx
++) {
733 // search for a start of frame marker
734 if ( dest
[idx
] && dest
[idx
+1] && dest
[idx
+2] && (!dest
[idx
+3]) && (!dest
[idx
+4]) && (!dest
[idx
+5]) )
738 if (found
&& (hi2
|hi
|lo
)) {
740 Dbprintf("TAG ID: %x%08x%08x (%d)",
741 (unsigned int) hi2
, (unsigned int) hi
, (unsigned int) lo
, (unsigned int) (lo
>>1) & 0xFFFF);
744 Dbprintf("TAG ID: %x%08x (%d)",
745 (unsigned int) hi
, (unsigned int) lo
, (unsigned int) (lo
>>1) & 0xFFFF);
747 /* if we're only looking for one tag */
761 if (dest
[idx
] && (!dest
[idx
+1]) ) {
762 hi2
=(hi2
<<1)|(hi
>>31);
765 } else if ( (!dest
[idx
]) && dest
[idx
+1]) {
766 hi2
=(hi2
<<1)|(hi
>>31);
777 if ( dest
[idx
] && dest
[idx
+1] && dest
[idx
+2] && (!dest
[idx
+3]) && (!dest
[idx
+4]) && (!dest
[idx
+5]) )
781 if (found
&& (hi
|lo
)) {
783 Dbprintf("TAG ID: %x%08x%08x (%d)",
784 (unsigned int) hi2
, (unsigned int) hi
, (unsigned int) lo
, (unsigned int) (lo
>>1) & 0xFFFF);
787 Dbprintf("TAG ID: %x%08x (%d)",
788 (unsigned int) hi
, (unsigned int) lo
, (unsigned int) (lo
>>1) & 0xFFFF);
790 /* if we're only looking for one tag */
809 /*------------------------------
810 * T5555/T5557/T5567 routines
811 *------------------------------
814 /* T55x7 configuration register definitions */
815 #define T55x7_POR_DELAY 0x00000001
816 #define T55x7_ST_TERMINATOR 0x00000008
817 #define T55x7_PWD 0x00000010
818 #define T55x7_MAXBLOCK_SHIFT 5
819 #define T55x7_AOR 0x00000200
820 #define T55x7_PSKCF_RF_2 0
821 #define T55x7_PSKCF_RF_4 0x00000400
822 #define T55x7_PSKCF_RF_8 0x00000800
823 #define T55x7_MODULATION_DIRECT 0
824 #define T55x7_MODULATION_PSK1 0x00001000
825 #define T55x7_MODULATION_PSK2 0x00002000
826 #define T55x7_MODULATION_PSK3 0x00003000
827 #define T55x7_MODULATION_FSK1 0x00004000
828 #define T55x7_MODULATION_FSK2 0x00005000
829 #define T55x7_MODULATION_FSK1a 0x00006000
830 #define T55x7_MODULATION_FSK2a 0x00007000
831 #define T55x7_MODULATION_MANCHESTER 0x00008000
832 #define T55x7_MODULATION_BIPHASE 0x00010000
833 #define T55x7_BITRATE_RF_8 0
834 #define T55x7_BITRATE_RF_16 0x00040000
835 #define T55x7_BITRATE_RF_32 0x00080000
836 #define T55x7_BITRATE_RF_40 0x000C0000
837 #define T55x7_BITRATE_RF_50 0x00100000
838 #define T55x7_BITRATE_RF_64 0x00140000
839 #define T55x7_BITRATE_RF_100 0x00180000
840 #define T55x7_BITRATE_RF_128 0x001C0000
842 /* T5555 (Q5) configuration register definitions */
843 #define T5555_ST_TERMINATOR 0x00000001
844 #define T5555_MAXBLOCK_SHIFT 0x00000001
845 #define T5555_MODULATION_MANCHESTER 0
846 #define T5555_MODULATION_PSK1 0x00000010
847 #define T5555_MODULATION_PSK2 0x00000020
848 #define T5555_MODULATION_PSK3 0x00000030
849 #define T5555_MODULATION_FSK1 0x00000040
850 #define T5555_MODULATION_FSK2 0x00000050
851 #define T5555_MODULATION_BIPHASE 0x00000060
852 #define T5555_MODULATION_DIRECT 0x00000070
853 #define T5555_INVERT_OUTPUT 0x00000080
854 #define T5555_PSK_RF_2 0
855 #define T5555_PSK_RF_4 0x00000100
856 #define T5555_PSK_RF_8 0x00000200
857 #define T5555_USE_PWD 0x00000400
858 #define T5555_USE_AOR 0x00000800
859 #define T5555_BITRATE_SHIFT 12
860 #define T5555_FAST_WRITE 0x00004000
861 #define T5555_PAGE_SELECT 0x00008000
864 * Relevant times in microsecond
865 * To compensate antenna falling times shorten the write times
866 * and enlarge the gap ones.
868 #define START_GAP 250
869 #define WRITE_GAP 160
870 #define WRITE_0 144 // 192
871 #define WRITE_1 400 // 432 for T55x7; 448 for E5550
873 // Write one bit to card
874 void T55xxWriteBit(int bit
)
876 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
877 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
879 SpinDelayUs(WRITE_0
);
881 SpinDelayUs(WRITE_1
);
882 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
883 SpinDelayUs(WRITE_GAP
);
886 // Write one card block in page 0, no lock
887 void T55xxWriteBlock(uint32_t Data
, uint32_t Block
, uint32_t Pwd
, uint8_t PwdMode
)
891 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
892 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
894 // Give it a bit of time for the resonant antenna to settle.
895 // And for the tag to fully power up
898 // Now start writting
899 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
900 SpinDelayUs(START_GAP
);
904 T55xxWriteBit(0); //Page 0
907 for (i
= 0x80000000; i
!= 0; i
>>= 1)
908 T55xxWriteBit(Pwd
& i
);
914 for (i
= 0x80000000; i
!= 0; i
>>= 1)
915 T55xxWriteBit(Data
& i
);
918 for (i
= 0x04; i
!= 0; i
>>= 1)
919 T55xxWriteBit(Block
& i
);
921 // Now perform write (nominal is 5.6 ms for T55x7 and 18ms for E5550,
922 // so wait a little more)
923 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
924 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
926 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
930 // Read one card block in page 0
931 void T55xxReadBlock(uint32_t Block
, uint32_t Pwd
, uint8_t PwdMode
)
933 uint8_t *dest
= (uint8_t *)BigBuf
;
937 // Clear destination buffer before sending the command
938 memset(dest
, 128, m
);
939 // Connect the A/D to the peak-detected low-frequency path.
940 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
941 // Now set up the SSC to get the ADC samples that are now streaming at us.
945 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
946 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
948 // Give it a bit of time for the resonant antenna to settle.
949 // And for the tag to fully power up
952 // Now start writting
953 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
954 SpinDelayUs(START_GAP
);
958 T55xxWriteBit(0); //Page 0
961 for (i
= 0x80000000; i
!= 0; i
>>= 1)
962 T55xxWriteBit(Pwd
& i
);
967 for (i
= 0x04; i
!= 0; i
>>= 1)
968 T55xxWriteBit(Block
& i
);
970 // Turn field on to read the response
971 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
972 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
974 // Now do the acquisition
977 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
) {
978 AT91C_BASE_SSC
->SSC_THR
= 0x43;
980 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
981 dest
[i
] = (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
982 // we don't care about actual value, only if it's more or less than a
983 // threshold essentially we capture zero crossings for later analysis
984 // if(dest[i] < 127) dest[i] = 0; else dest[i] = 1;
990 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); // field off
995 // Read card traceability data (page 1)
996 void T55xxReadTrace(void){
997 uint8_t *dest
= (uint8_t *)BigBuf
;
1001 // Clear destination buffer before sending the command
1002 memset(dest
, 128, m
);
1003 // Connect the A/D to the peak-detected low-frequency path.
1004 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
1005 // Now set up the SSC to get the ADC samples that are now streaming at us.
1009 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1010 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
1012 // Give it a bit of time for the resonant antenna to settle.
1013 // And for the tag to fully power up
1016 // Now start writting
1017 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1018 SpinDelayUs(START_GAP
);
1022 T55xxWriteBit(1); //Page 1
1024 // Turn field on to read the response
1025 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1026 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
1028 // Now do the acquisition
1031 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
) {
1032 AT91C_BASE_SSC
->SSC_THR
= 0x43;
1034 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
1035 dest
[i
] = (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
1041 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); // field off
1046 /*-------------- Cloning routines -----------*/
1047 // Copy HID id to card and setup block 0 config
1048 void CopyHIDtoT55x7(uint32_t hi2
, uint32_t hi
, uint32_t lo
, uint8_t longFMT
)
1050 int data1
, data2
, data3
, data4
, data5
, data6
; //up to six blocks for long format
1054 // Ensure no more than 84 bits supplied
1056 DbpString("Tags can only have 84 bits.");
1059 // Build the 6 data blocks for supplied 84bit ID
1061 data1
= 0x1D96A900; // load preamble (1D) & long format identifier (9E manchester encoded)
1062 for (int i
=0;i
<4;i
++) {
1063 if (hi2
& (1<<(19-i
)))
1064 data1
|= (1<<(((3-i
)*2)+1)); // 1 -> 10
1066 data1
|= (1<<((3-i
)*2)); // 0 -> 01
1070 for (int i
=0;i
<16;i
++) {
1071 if (hi2
& (1<<(15-i
)))
1072 data2
|= (1<<(((15-i
)*2)+1)); // 1 -> 10
1074 data2
|= (1<<((15-i
)*2)); // 0 -> 01
1078 for (int i
=0;i
<16;i
++) {
1079 if (hi
& (1<<(31-i
)))
1080 data3
|= (1<<(((15-i
)*2)+1)); // 1 -> 10
1082 data3
|= (1<<((15-i
)*2)); // 0 -> 01
1086 for (int i
=0;i
<16;i
++) {
1087 if (hi
& (1<<(15-i
)))
1088 data4
|= (1<<(((15-i
)*2)+1)); // 1 -> 10
1090 data4
|= (1<<((15-i
)*2)); // 0 -> 01
1094 for (int i
=0;i
<16;i
++) {
1095 if (lo
& (1<<(31-i
)))
1096 data5
|= (1<<(((15-i
)*2)+1)); // 1 -> 10
1098 data5
|= (1<<((15-i
)*2)); // 0 -> 01
1102 for (int i
=0;i
<16;i
++) {
1103 if (lo
& (1<<(15-i
)))
1104 data6
|= (1<<(((15-i
)*2)+1)); // 1 -> 10
1106 data6
|= (1<<((15-i
)*2)); // 0 -> 01
1110 // Ensure no more than 44 bits supplied
1112 DbpString("Tags can only have 44 bits.");
1116 // Build the 3 data blocks for supplied 44bit ID
1119 data1
= 0x1D000000; // load preamble
1121 for (int i
=0;i
<12;i
++) {
1122 if (hi
& (1<<(11-i
)))
1123 data1
|= (1<<(((11-i
)*2)+1)); // 1 -> 10
1125 data1
|= (1<<((11-i
)*2)); // 0 -> 01
1129 for (int i
=0;i
<16;i
++) {
1130 if (lo
& (1<<(31-i
)))
1131 data2
|= (1<<(((15-i
)*2)+1)); // 1 -> 10
1133 data2
|= (1<<((15-i
)*2)); // 0 -> 01
1137 for (int i
=0;i
<16;i
++) {
1138 if (lo
& (1<<(15-i
)))
1139 data3
|= (1<<(((15-i
)*2)+1)); // 1 -> 10
1141 data3
|= (1<<((15-i
)*2)); // 0 -> 01
1146 // Program the data blocks for supplied ID
1147 // and the block 0 for HID format
1148 T55xxWriteBlock(data1
,1,0,0);
1149 T55xxWriteBlock(data2
,2,0,0);
1150 T55xxWriteBlock(data3
,3,0,0);
1152 if (longFMT
) { // if long format there are 6 blocks
1153 T55xxWriteBlock(data4
,4,0,0);
1154 T55xxWriteBlock(data5
,5,0,0);
1155 T55xxWriteBlock(data6
,6,0,0);
1158 // Config for HID (RF/50, FSK2a, Maxblock=3 for short/6 for long)
1159 T55xxWriteBlock(T55x7_BITRATE_RF_50
|
1160 T55x7_MODULATION_FSK2a
|
1161 last_block
<< T55x7_MAXBLOCK_SHIFT
,
1169 // Define 9bit header for EM410x tags
1170 #define EM410X_HEADER 0x1FF
1171 #define EM410X_ID_LENGTH 40
1173 void WriteEM410x(uint32_t card
, uint32_t id_hi
, uint32_t id_lo
)
1176 uint64_t id
= EM410X_HEADER
;
1177 uint64_t rev_id
= 0; // reversed ID
1178 int c_parity
[4]; // column parity
1179 int r_parity
= 0; // row parity
1181 // Reverse ID bits given as parameter (for simpler operations)
1182 for (i
= 0; i
< EM410X_ID_LENGTH
; ++i
) {
1184 rev_id
= (rev_id
<< 1) | (id_lo
& 1);
1187 rev_id
= (rev_id
<< 1) | (id_hi
& 1);
1192 for (i
= 0; i
< EM410X_ID_LENGTH
; ++i
) {
1193 id_bit
= rev_id
& 1;
1196 // Don't write row parity bit at start of parsing
1198 id
= (id
<< 1) | r_parity
;
1199 // Start counting parity for new row
1206 // First elements in column?
1208 // Fill out first elements
1209 c_parity
[i
] = id_bit
;
1211 // Count column parity
1212 c_parity
[i
% 4] ^= id_bit
;
1215 id
= (id
<< 1) | id_bit
;
1219 // Insert parity bit of last row
1220 id
= (id
<< 1) | r_parity
;
1222 // Fill out column parity at the end of tag
1223 for (i
= 0; i
< 4; ++i
)
1224 id
= (id
<< 1) | c_parity
[i
];
1229 Dbprintf("Started writing %s tag ...", card
? "T55x7":"T5555");
1233 T55xxWriteBlock((uint32_t)(id
>> 32), 1, 0, 0);
1234 T55xxWriteBlock((uint32_t)id
, 2, 0, 0);
1236 // Config for EM410x (RF/64, Manchester, Maxblock=2)
1238 // Writing configuration for T55x7 tag
1239 T55xxWriteBlock(T55x7_BITRATE_RF_64
|
1240 T55x7_MODULATION_MANCHESTER
|
1241 2 << T55x7_MAXBLOCK_SHIFT
,
1244 // Writing configuration for T5555(Q5) tag
1245 T55xxWriteBlock(0x1F << T5555_BITRATE_SHIFT
|
1246 T5555_MODULATION_MANCHESTER
|
1247 2 << T5555_MAXBLOCK_SHIFT
,
1251 Dbprintf("Tag %s written with 0x%08x%08x\n", card
? "T55x7":"T5555",
1252 (uint32_t)(id
>> 32), (uint32_t)id
);
1255 // Clone Indala 64-bit tag by UID to T55x7
1256 void CopyIndala64toT55x7(int hi
, int lo
)
1259 //Program the 2 data blocks for supplied 64bit UID
1260 // and the block 0 for Indala64 format
1261 T55xxWriteBlock(hi
,1,0,0);
1262 T55xxWriteBlock(lo
,2,0,0);
1263 //Config for Indala (RF/32;PSK1 with RF/2;Maxblock=2)
1264 T55xxWriteBlock(T55x7_BITRATE_RF_32
|
1265 T55x7_MODULATION_PSK1
|
1266 2 << T55x7_MAXBLOCK_SHIFT
,
1268 //Alternative config for Indala (Extended mode;RF/32;PSK1 with RF/2;Maxblock=2;Inverse data)
1269 // T5567WriteBlock(0x603E1042,0);
1275 void CopyIndala224toT55x7(int uid1
, int uid2
, int uid3
, int uid4
, int uid5
, int uid6
, int uid7
)
1278 //Program the 7 data blocks for supplied 224bit UID
1279 // and the block 0 for Indala224 format
1280 T55xxWriteBlock(uid1
,1,0,0);
1281 T55xxWriteBlock(uid2
,2,0,0);
1282 T55xxWriteBlock(uid3
,3,0,0);
1283 T55xxWriteBlock(uid4
,4,0,0);
1284 T55xxWriteBlock(uid5
,5,0,0);
1285 T55xxWriteBlock(uid6
,6,0,0);
1286 T55xxWriteBlock(uid7
,7,0,0);
1287 //Config for Indala (RF/32;PSK1 with RF/2;Maxblock=7)
1288 T55xxWriteBlock(T55x7_BITRATE_RF_32
|
1289 T55x7_MODULATION_PSK1
|
1290 7 << T55x7_MAXBLOCK_SHIFT
,
1292 //Alternative config for Indala (Extended mode;RF/32;PSK1 with RF/2;Maxblock=7;Inverse data)
1293 // T5567WriteBlock(0x603E10E2,0);
1299 #define abs(x) ( ((x)<0) ? -(x) : (x) )
1300 #define max(x,y) ( x<y ? y:x)
1302 int DemodPCF7931(uint8_t **outBlocks
) {
1303 uint8_t BitStream
[256];
1304 uint8_t Blocks
[8][16];
1305 uint8_t *GraphBuffer
= (uint8_t *)BigBuf
;
1306 int GraphTraceLen
= sizeof(BigBuf
);
1307 int i
, j
, lastval
, bitidx
, half_switch
;
1309 int tolerance
= clock
/ 8;
1310 int pmc
, block_done
;
1311 int lc
, warnings
= 0;
1313 int lmin
=128, lmax
=128;
1316 AcquireRawAdcSamples125k(0);
1323 /* Find first local max/min */
1324 if(GraphBuffer
[1] > GraphBuffer
[0]) {
1325 while(i
< GraphTraceLen
) {
1326 if( !(GraphBuffer
[i
] > GraphBuffer
[i
-1]) && GraphBuffer
[i
] > lmax
)
1333 while(i
< GraphTraceLen
) {
1334 if( !(GraphBuffer
[i
] < GraphBuffer
[i
-1]) && GraphBuffer
[i
] < lmin
)
1346 for (bitidx
= 0; i
< GraphTraceLen
; i
++)
1348 if ( (GraphBuffer
[i
-1] > GraphBuffer
[i
] && dir
== 1 && GraphBuffer
[i
] > lmax
) || (GraphBuffer
[i
-1] < GraphBuffer
[i
] && dir
== 0 && GraphBuffer
[i
] < lmin
))
1353 // Switch depending on lc length:
1354 // Tolerance is 1/8 of clock rate (arbitrary)
1355 if (abs(lc
-clock
/4) < tolerance
) {
1357 if((i
- pmc
) == lc
) { /* 16T0 was previous one */
1359 i
+= (128+127+16+32+33+16)-1;
1367 } else if (abs(lc
-clock
/2) < tolerance
) {
1369 if((i
- pmc
) == lc
) { /* 16T0 was previous one */
1371 i
+= (128+127+16+32+33)-1;
1376 else if(half_switch
== 1) {
1377 BitStream
[bitidx
++] = 0;
1382 } else if (abs(lc
-clock
) < tolerance
) {
1384 BitStream
[bitidx
++] = 1;
1390 Dbprintf("Error: too many detection errors, aborting.");
1395 if(block_done
== 1) {
1397 for(j
=0; j
<16; j
++) {
1398 Blocks
[num_blocks
][j
] = 128*BitStream
[j
*8+7]+
1399 64*BitStream
[j
*8+6]+
1400 32*BitStream
[j
*8+5]+
1401 16*BitStream
[j
*8+4]+
1413 if (GraphBuffer
[i
-1] > GraphBuffer
[i
]) dir
=0;
1419 if(num_blocks
== 4) break;
1421 memcpy(outBlocks
, Blocks
, 16*num_blocks
);
1425 int IsBlock0PCF7931(uint8_t *Block
) {
1426 // Assume RFU means 0 :)
1427 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
1429 if((memcmp(Block
+9, "\x00\x00\x00\x00\x00\x00\x00", 7) == 0) && Block
[7] == 0) // PAC disabled, can it *really* happen ?
1434 int IsBlock1PCF7931(uint8_t *Block
) {
1435 // Assume RFU means 0 :)
1436 if(Block
[10] == 0 && Block
[11] == 0 && Block
[12] == 0 && Block
[13] == 0)
1437 if((Block
[14] & 0x7f) <= 9 && Block
[15] <= 9)
1445 void ReadPCF7931() {
1446 uint8_t Blocks
[8][17];
1447 uint8_t tmpBlocks
[4][16];
1448 int i
, j
, ind
, ind2
, n
;
1455 memset(Blocks
, 0, 8*17*sizeof(uint8_t));
1458 memset(tmpBlocks
, 0, 4*16*sizeof(uint8_t));
1459 n
= DemodPCF7931((uint8_t**)tmpBlocks
);
1462 if(error
==10 && num_blocks
== 0) {
1463 Dbprintf("Error, no tag or bad tag");
1466 else if (tries
==20 || error
==10) {
1467 Dbprintf("Error reading the tag");
1468 Dbprintf("Here is the partial content");
1473 Dbprintf("(dbg) %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
1474 tmpBlocks
[i
][0], tmpBlocks
[i
][1], tmpBlocks
[i
][2], tmpBlocks
[i
][3], tmpBlocks
[i
][4], tmpBlocks
[i
][5], tmpBlocks
[i
][6], tmpBlocks
[i
][7],
1475 tmpBlocks
[i
][8], tmpBlocks
[i
][9], tmpBlocks
[i
][10], tmpBlocks
[i
][11], tmpBlocks
[i
][12], tmpBlocks
[i
][13], tmpBlocks
[i
][14], tmpBlocks
[i
][15]);
1477 for(i
=0; i
<n
; i
++) {
1478 if(IsBlock0PCF7931(tmpBlocks
[i
])) {
1480 if(i
< n
-1 && IsBlock1PCF7931(tmpBlocks
[i
+1])) {
1484 memcpy(Blocks
[0], tmpBlocks
[i
], 16);
1485 Blocks
[0][ALLOC
] = 1;
1486 memcpy(Blocks
[1], tmpBlocks
[i
+1], 16);
1487 Blocks
[1][ALLOC
] = 1;
1488 max_blocks
= max((Blocks
[1][14] & 0x7f), Blocks
[1][15]) + 1;
1490 Dbprintf("(dbg) Max blocks: %d", max_blocks
);
1492 // Handle following blocks
1493 for(j
=i
+2, ind2
=2; j
!=i
; j
++, ind2
++, num_blocks
++) {
1496 memcpy(Blocks
[ind2
], tmpBlocks
[j
], 16);
1497 Blocks
[ind2
][ALLOC
] = 1;
1505 for(i
=0; i
<n
; i
++) { // Look for identical block in known blocks
1506 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
1507 for(j
=0; j
<max_blocks
; j
++) {
1508 if(Blocks
[j
][ALLOC
] == 1 && !memcmp(tmpBlocks
[i
], Blocks
[j
], 16)) {
1509 // Found an identical block
1510 for(ind
=i
-1,ind2
=j
-1; ind
>= 0; ind
--,ind2
--) {
1513 if(!Blocks
[ind2
][ALLOC
]) { // Block ind2 not already found
1514 // Dbprintf("Tmp %d -> Block %d", ind, ind2);
1515 memcpy(Blocks
[ind2
], tmpBlocks
[ind
], 16);
1516 Blocks
[ind2
][ALLOC
] = 1;
1518 if(num_blocks
== max_blocks
) goto end
;
1521 for(ind
=i
+1,ind2
=j
+1; ind
< n
; ind
++,ind2
++) {
1522 if(ind2
> max_blocks
)
1524 if(!Blocks
[ind2
][ALLOC
]) { // Block ind2 not already found
1525 // Dbprintf("Tmp %d -> Block %d", ind, ind2);
1526 memcpy(Blocks
[ind2
], tmpBlocks
[ind
], 16);
1527 Blocks
[ind2
][ALLOC
] = 1;
1529 if(num_blocks
== max_blocks
) goto end
;
1538 if (BUTTON_PRESS()) return;
1539 } while (num_blocks
!= max_blocks
);
1541 Dbprintf("-----------------------------------------");
1542 Dbprintf("Memory content:");
1543 Dbprintf("-----------------------------------------");
1544 for(i
=0; i
<max_blocks
; i
++) {
1545 if(Blocks
[i
][ALLOC
]==1)
1546 Dbprintf("%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
1547 Blocks
[i
][0], Blocks
[i
][1], Blocks
[i
][2], Blocks
[i
][3], Blocks
[i
][4], Blocks
[i
][5], Blocks
[i
][6], Blocks
[i
][7],
1548 Blocks
[i
][8], Blocks
[i
][9], Blocks
[i
][10], Blocks
[i
][11], Blocks
[i
][12], Blocks
[i
][13], Blocks
[i
][14], Blocks
[i
][15]);
1550 Dbprintf("<missing block %d>", i
);
1552 Dbprintf("-----------------------------------------");
1558 //-----------------------------------
1559 // EM4469 / EM4305 routines
1560 //-----------------------------------
1561 #define FWD_CMD_LOGIN 0xC //including the even parity, binary mirrored
1562 #define FWD_CMD_WRITE 0xA
1563 #define FWD_CMD_READ 0x9
1564 #define FWD_CMD_DISABLE 0x5
1567 uint8_t forwardLink_data
[64]; //array of forwarded bits
1568 uint8_t * forward_ptr
; //ptr for forward message preparation
1569 uint8_t fwd_bit_sz
; //forwardlink bit counter
1570 uint8_t * fwd_write_ptr
; //forwardlink bit pointer
1572 //====================================================================
1573 // prepares command bits
1575 //====================================================================
1576 //--------------------------------------------------------------------
1577 uint8_t Prepare_Cmd( uint8_t cmd
) {
1578 //--------------------------------------------------------------------
1580 *forward_ptr
++ = 0; //start bit
1581 *forward_ptr
++ = 0; //second pause for 4050 code
1583 *forward_ptr
++ = cmd
;
1585 *forward_ptr
++ = cmd
;
1587 *forward_ptr
++ = cmd
;
1589 *forward_ptr
++ = cmd
;
1591 return 6; //return number of emited bits
1594 //====================================================================
1595 // prepares address bits
1597 //====================================================================
1599 //--------------------------------------------------------------------
1600 uint8_t Prepare_Addr( uint8_t addr
) {
1601 //--------------------------------------------------------------------
1603 register uint8_t line_parity
;
1608 *forward_ptr
++ = addr
;
1609 line_parity
^= addr
;
1613 *forward_ptr
++ = (line_parity
& 1);
1615 return 7; //return number of emited bits
1618 //====================================================================
1619 // prepares data bits intreleaved with parity bits
1621 //====================================================================
1623 //--------------------------------------------------------------------
1624 uint8_t Prepare_Data( uint16_t data_low
, uint16_t data_hi
) {
1625 //--------------------------------------------------------------------
1627 register uint8_t line_parity
;
1628 register uint8_t column_parity
;
1629 register uint8_t i
, j
;
1630 register uint16_t data
;
1635 for(i
=0; i
<4; i
++) {
1637 for(j
=0; j
<8; j
++) {
1638 line_parity
^= data
;
1639 column_parity
^= (data
& 1) << j
;
1640 *forward_ptr
++ = data
;
1643 *forward_ptr
++ = line_parity
;
1648 for(j
=0; j
<8; j
++) {
1649 *forward_ptr
++ = column_parity
;
1650 column_parity
>>= 1;
1654 return 45; //return number of emited bits
1657 //====================================================================
1658 // Forward Link send function
1659 // Requires: forwarLink_data filled with valid bits (1 bit per byte)
1660 // fwd_bit_count set with number of bits to be sent
1661 //====================================================================
1662 void SendForward(uint8_t fwd_bit_count
) {
1664 fwd_write_ptr
= forwardLink_data
;
1665 fwd_bit_sz
= fwd_bit_count
;
1670 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1671 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
1673 // Give it a bit of time for the resonant antenna to settle.
1674 // And for the tag to fully power up
1677 // force 1st mod pulse (start gap must be longer for 4305)
1678 fwd_bit_sz
--; //prepare next bit modulation
1680 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); // field off
1681 SpinDelayUs(55*8); //55 cycles off (8us each)for 4305
1682 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1683 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);//field on
1684 SpinDelayUs(16*8); //16 cycles on (8us each)
1686 // now start writting
1687 while(fwd_bit_sz
-- > 0) { //prepare next bit modulation
1688 if(((*fwd_write_ptr
++) & 1) == 1)
1689 SpinDelayUs(32*8); //32 cycles at 125Khz (8us each)
1691 //These timings work for 4469/4269/4305 (with the 55*8 above)
1692 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); // field off
1693 SpinDelayUs(23*8); //16-4 cycles off (8us each)
1694 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
1695 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);//field on
1696 SpinDelayUs(9*8); //16 cycles on (8us each)
1701 void Login (uint32_t Password
) {
1703 uint8_t fwd_bit_count
;
1705 forward_ptr
= forwardLink_data
;
1706 fwd_bit_count
= Prepare_Cmd( FWD_CMD_LOGIN
);
1707 fwd_bit_count
+= Prepare_Data( Password
&0xFFFF, Password
>>16 );
1709 SendForward(fwd_bit_count
);
1711 //Wait for command to complete
1716 void EM4xReadWord(uint8_t Address
, uint32_t Pwd
, uint8_t PwdMode
) {
1718 uint8_t fwd_bit_count
;
1719 uint8_t *dest
= (uint8_t *)BigBuf
;
1722 //If password mode do login
1723 if (PwdMode
== 1) Login(Pwd
);
1725 forward_ptr
= forwardLink_data
;
1726 fwd_bit_count
= Prepare_Cmd( FWD_CMD_READ
);
1727 fwd_bit_count
+= Prepare_Addr( Address
);
1730 // Clear destination buffer before sending the command
1731 memset(dest
, 128, m
);
1732 // Connect the A/D to the peak-detected low-frequency path.
1733 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
1734 // Now set up the SSC to get the ADC samples that are now streaming at us.
1737 SendForward(fwd_bit_count
);
1739 // Now do the acquisition
1742 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
) {
1743 AT91C_BASE_SSC
->SSC_THR
= 0x43;
1745 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
) {
1746 dest
[i
] = (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
1751 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); // field off
1755 void EM4xWriteWord(uint32_t Data
, uint8_t Address
, uint32_t Pwd
, uint8_t PwdMode
) {
1757 uint8_t fwd_bit_count
;
1759 //If password mode do login
1760 if (PwdMode
== 1) Login(Pwd
);
1762 forward_ptr
= forwardLink_data
;
1763 fwd_bit_count
= Prepare_Cmd( FWD_CMD_WRITE
);
1764 fwd_bit_count
+= Prepare_Addr( Address
);
1765 fwd_bit_count
+= Prepare_Data( Data
&0xFFFF, Data
>>16 );
1767 SendForward(fwd_bit_count
);
1769 //Wait for write to complete
1771 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); // field off