1 //-----------------------------------------------------------------------------
2 // Miscellaneous routines for low frequency tag operations.
3 // Tags supported here so far are Texas Instruments (TI), HID
4 // Also routines for raw mode reading/simulating of LF waveform
6 //-----------------------------------------------------------------------------
10 #include "../common/crc16.c"
12 void AcquireRawAdcSamples125k(BOOL at134khz
)
15 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
16 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
18 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
19 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
22 // Connect the A/D to the peak-detected low-frequency path.
23 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
25 // Give it a bit of time for the resonant antenna to settle.
28 // Now set up the SSC to get the ADC samples that are now streaming at us.
31 // Now call the acquisition routine
32 DoAcquisition125k(at134khz
);
35 // split into two routines so we can avoid timing issues after sending commands //
36 void DoAcquisition125k(BOOL at134khz
)
38 BYTE
*dest
= (BYTE
*)BigBuf
;
39 int n
= sizeof(BigBuf
);
45 if(SSC_STATUS
& (SSC_STATUS_TX_READY
)) {
46 SSC_TRANSMIT_HOLDING
= 0x43;
49 if(SSC_STATUS
& (SSC_STATUS_RX_READY
)) {
50 dest
[i
] = (BYTE
)SSC_RECEIVE_HOLDING
;
58 DbpIntegers(dest
[0], dest
[1], at134khz
);
61 void ModThenAcquireRawAdcSamples125k(int delay_off
,int period_0
,int period_1
,BYTE
*command
)
65 /* Make sure the tag is reset */
66 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
69 // see if 'h' was specified
70 if(command
[strlen((char *) command
) - 1] == 'h')
76 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
77 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
79 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
80 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
83 // Give it a bit of time for the resonant antenna to settle.
85 // And a little more time for the tag to fully power up
88 // Now set up the SSC to get the ADC samples that are now streaming at us.
91 // now modulate the reader field
92 while(*command
!= '\0' && *command
!= ' ')
94 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
96 SpinDelayUs(delay_off
);
98 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
99 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
101 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
102 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
105 if(*(command
++) == '0') {
106 SpinDelayUs(period_0
);
108 SpinDelayUs(period_1
);
111 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
113 SpinDelayUs(delay_off
);
115 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
116 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
118 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
119 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
123 DoAcquisition125k(at134khz
);
126 /* blank r/w tag data stream
127 ...0000000000000000 01111111
128 1010101010101010101010101010101010101010101010101010101010101010
131 101010101010101[0]000...
133 [5555fe852c5555555555555555fe0000]
137 // some hardcoded initial params
138 // when we read a TI tag we sample the zerocross line at 2Mhz
139 // TI tags modulate a 1 as 16 cycles of 123.2Khz
140 // TI tags modulate a 0 as 16 cycles of 134.2Khz
141 #define FSAMPLE 2000000
142 #define FREQLO 123200
143 #define FREQHI 134200
145 signed char *dest
= (signed char *)BigBuf
;
146 int n
= sizeof(BigBuf
);
147 // int *dest = GraphBuffer;
148 // int n = GraphTraceLen;
150 // 128 bit shift register [shift3:shift2:shift1:shift0]
151 DWORD shift3
= 0, shift2
= 0, shift1
= 0, shift0
= 0;
153 int i
, cycles
=0, samples
=0;
154 // how many sample points fit in 16 cycles of each frequency
155 DWORD sampleslo
= (FSAMPLE
<<4)/FREQLO
, sampleshi
= (FSAMPLE
<<4)/FREQHI
;
156 // when to tell if we're close enough to one freq or another
157 DWORD threshold
= (sampleslo
- sampleshi
+ 1)>>1;
159 // TI tags charge at 134.2Khz
160 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
162 // Place FPGA in passthrough mode, in this mode the CROSS_LO line
163 // connects to SSP_DIN and the SSP_DOUT logic level controls
164 // whether we're modulating the antenna (high)
165 // or listening to the antenna (low)
166 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_PASSTHRU
);
168 // get TI tag data into the buffer
171 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
173 for (i
=0; i
<n
-1; i
++) {
174 // count cycles by looking for lo to hi zero crossings
175 if ( (dest
[i
]<0) && (dest
[i
+1]>0) ) {
177 // after 16 cycles, measure the frequency
180 samples
=i
-samples
; // number of samples in these 16 cycles
182 // TI bits are coming to us lsb first so shift them
183 // right through our 128 bit right shift register
184 shift0
= (shift0
>>1) | (shift1
<< 31);
185 shift1
= (shift1
>>1) | (shift2
<< 31);
186 shift2
= (shift2
>>1) | (shift3
<< 31);
189 // check if the cycles fall close to the number
190 // expected for either the low or high frequency
191 if ( (samples
>(sampleslo
-threshold
)) && (samples
<(sampleslo
+threshold
)) ) {
192 // low frequency represents a 1
194 } else if ( (samples
>(sampleshi
-threshold
)) && (samples
<(sampleshi
+threshold
)) ) {
195 // high frequency represents a 0
197 // probably detected a gay waveform or noise
198 // use this as gaydar or discard shift register and start again
199 shift3
= shift2
= shift1
= shift0
= 0;
203 // for each bit we receive, test if we've detected a valid tag
205 // if we see 17 zeroes followed by 6 ones, we might have a tag
206 // remember the bits are backwards
207 if ( ((shift0
& 0x7fffff) == 0x7e0000) ) {
208 // if start and end bytes match, we have a tag so break out of the loop
209 if ( ((shift0
>>16)&0xff) == ((shift3
>>8)&0xff) ) {
210 cycles
= 0xF0B; //use this as a flag (ugly but whatever)
218 // if flag is set we have a tag
220 DbpString("Info: No valid tag detected.");
222 // put 64 bit data into shift1 and shift0
223 shift0
= (shift0
>>24) | (shift1
<< 8);
224 shift1
= (shift1
>>24) | (shift2
<< 8);
226 // align 16 bit crc into lower half of shift2
227 shift2
= ((shift2
>>24) | (shift3
<< 8)) & 0x0ffff;
229 // if r/w tag, check ident match
230 if ( shift3
&(1<<15) ) {
231 DbpString("Info: TI tag is rewriteable");
232 // only 15 bits compare, last bit of ident is not valid
233 if ( ((shift3
>>16)^shift0
)&0x7fff ) {
234 DbpString("Error: Ident mismatch!");
236 DbpString("Info: TI tag ident is valid");
239 DbpString("Info: TI tag is readonly");
242 // WARNING the order of the bytes in which we calc crc below needs checking
243 // i'm 99% sure the crc algorithm is correct, but it may need to eat the
244 // bytes in reverse or something
248 crc
= update_crc16(crc
, (shift0
)&0xff);
249 crc
= update_crc16(crc
, (shift0
>>8)&0xff);
250 crc
= update_crc16(crc
, (shift0
>>16)&0xff);
251 crc
= update_crc16(crc
, (shift0
>>24)&0xff);
252 crc
= update_crc16(crc
, (shift1
)&0xff);
253 crc
= update_crc16(crc
, (shift1
>>8)&0xff);
254 crc
= update_crc16(crc
, (shift1
>>16)&0xff);
255 crc
= update_crc16(crc
, (shift1
>>24)&0xff);
257 DbpString("Info: Tag data_hi, data_lo, crc = ");
258 DbpIntegers(shift1
, shift0
, shift2
&0xffff);
259 if (crc
!= (shift2
&0xffff)) {
260 DbpString("Error: CRC mismatch, expected");
261 DbpIntegers(0, 0, crc
);
263 DbpString("Info: CRC is good");
268 void WriteTIbyte(BYTE b
)
272 // modulate 8 bits out to the antenna
276 // stop modulating antenna
277 PIO_OUTPUT_DATA_CLEAR
= (1<<GPIO_SSC_DOUT
);
280 PIO_OUTPUT_DATA_SET
= (1<<GPIO_SSC_DOUT
);
283 // stop modulating antenna
284 PIO_OUTPUT_DATA_CLEAR
= (1<<GPIO_SSC_DOUT
);
287 PIO_OUTPUT_DATA_SET
= (1<<GPIO_SSC_DOUT
);
293 void AcquireTiType(void)
296 // tag transmission is <20ms, sampling at 2M gives us 40K samples max
297 // each sample is 1 bit stuffed into a DWORD so we need 1250 DWORDS
298 #define TIBUFLEN 1250
301 memset(BigBuf
,0,sizeof(BigBuf
));
303 // Set up the synchronous serial port
304 PIO_DISABLE
= (1<<GPIO_SSC_DIN
);
305 PIO_PERIPHERAL_A_SEL
= (1<<GPIO_SSC_DIN
);
307 // steal this pin from the SSP and use it to control the modulation
308 PIO_ENABLE
= (1<<GPIO_SSC_DOUT
);
309 PIO_OUTPUT_ENABLE
= (1<<GPIO_SSC_DOUT
);
311 SSC_CONTROL
= SSC_CONTROL_RESET
;
312 SSC_CONTROL
= SSC_CONTROL_RX_ENABLE
| SSC_CONTROL_TX_ENABLE
;
314 // Sample at 2 Mbit/s, so TI tags are 16.2 vs. 14.9 clocks long
315 // 48/2 = 24 MHz clock must be divided by 12
316 SSC_CLOCK_DIVISOR
= 12;
318 SSC_RECEIVE_CLOCK_MODE
= SSC_CLOCK_MODE_SELECT(0);
319 SSC_RECEIVE_FRAME_MODE
= SSC_FRAME_MODE_BITS_IN_WORD(32) | SSC_FRAME_MODE_MSB_FIRST
;
320 SSC_TRANSMIT_CLOCK_MODE
= 0;
321 SSC_TRANSMIT_FRAME_MODE
= 0;
326 PIO_OUTPUT_DATA_SET
= (1<<GPIO_SSC_DOUT
);
328 // Charge TI tag for 50ms.
331 // stop modulating antenna and listen
332 PIO_OUTPUT_DATA_CLEAR
= (1<<GPIO_SSC_DOUT
);
338 if(SSC_STATUS
& SSC_STATUS_RX_READY
) {
339 BigBuf
[i
] = SSC_RECEIVE_HOLDING
; // store 32 bit values in buffer
340 i
++; if(i
>= TIBUFLEN
) break;
345 // return stolen pin to SSP
346 PIO_DISABLE
= (1<<GPIO_SSC_DOUT
);
347 PIO_PERIPHERAL_A_SEL
= (1<<GPIO_SSC_DIN
) | (1<<GPIO_SSC_DOUT
);
349 char *dest
= (char *)BigBuf
;
352 for (i
=TIBUFLEN
-1; i
>=0; i
--) {
353 // DbpIntegers(0, 0, BigBuf[i]);
354 for (j
=0; j
<32; j
++) {
355 if(BigBuf
[i
] & (1 << j
)) {
364 // arguments: 64bit data split into 32bit idhi:idlo and optional 16bit crc
365 // if crc provided, it will be written with the data verbatim (even if bogus)
366 // if not provided a valid crc will be computed from the data and written.
367 void WriteTItag(DWORD idhi
, DWORD idlo
, WORD crc
)
370 // WARNING the order of the bytes in which we calc crc below needs checking
371 // i'm 99% sure the crc algorithm is correct, but it may need to eat the
372 // bytes in reverse or something
375 crc
= update_crc16(crc
, (idlo
)&0xff);
376 crc
= update_crc16(crc
, (idlo
>>8)&0xff);
377 crc
= update_crc16(crc
, (idlo
>>16)&0xff);
378 crc
= update_crc16(crc
, (idlo
>>24)&0xff);
379 crc
= update_crc16(crc
, (idhi
)&0xff);
380 crc
= update_crc16(crc
, (idhi
>>8)&0xff);
381 crc
= update_crc16(crc
, (idhi
>>16)&0xff);
382 crc
= update_crc16(crc
, (idhi
>>24)&0xff);
384 DbpString("Writing the following data to tag:");
385 DbpIntegers(idhi
, idlo
, crc
);
387 // TI tags charge at 134.2Khz
388 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 88); //134.8Khz
389 // Place FPGA in passthrough mode, in this mode the CROSS_LO line
390 // connects to SSP_DIN and the SSP_DOUT logic level controls
391 // whether we're modulating the antenna (high)
392 // or listening to the antenna (low)
393 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_PASSTHRU
);
396 // steal this pin from the SSP and use it to control the modulation
397 PIO_ENABLE
= (1<<GPIO_SSC_DOUT
);
398 PIO_OUTPUT_ENABLE
= (1<<GPIO_SSC_DOUT
);
400 // writing algorithm:
401 // a high bit consists of a field off for 1ms and field on for 1ms
402 // a low bit consists of a field off for 0.3ms and field on for 1.7ms
403 // initiate a charge time of 50ms (field on) then immediately start writing bits
404 // start by writing 0xBB (keyword) and 0xEB (password)
405 // then write 80 bits of data (or 64 bit data + 16 bit crc if you prefer)
406 // finally end with 0x0300 (write frame)
407 // all data is sent lsb firts
408 // finish with 15ms programming time
411 PIO_OUTPUT_DATA_SET
= (1<<GPIO_SSC_DOUT
);
412 SpinDelay(50); // charge time
414 WriteTIbyte(0xbb); // keyword
415 WriteTIbyte(0xeb); // password
416 WriteTIbyte( (idlo
)&0xff );
417 WriteTIbyte( (idlo
>>8 )&0xff );
418 WriteTIbyte( (idlo
>>16)&0xff );
419 WriteTIbyte( (idlo
>>24)&0xff );
420 WriteTIbyte( (idhi
)&0xff );
421 WriteTIbyte( (idhi
>>8 )&0xff );
422 WriteTIbyte( (idhi
>>16)&0xff );
423 WriteTIbyte( (idhi
>>24)&0xff ); // data hi to lo
424 WriteTIbyte( (crc
)&0xff ); // crc lo
425 WriteTIbyte( (crc
>>8 )&0xff ); // crc hi
426 WriteTIbyte(0x00); // write frame lo
427 WriteTIbyte(0x03); // write frame hi
428 PIO_OUTPUT_DATA_SET
= (1<<GPIO_SSC_DOUT
);
429 SpinDelay(50); // programming time
433 // get TI tag data into the buffer
436 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
437 DbpString("Now use tiread to check");
440 void SimulateTagLowFrequency(int period
, int ledcontrol
)
443 BYTE
*tab
= (BYTE
*)BigBuf
;
445 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_SIMULATOR
);
447 PIO_ENABLE
= (1 << GPIO_SSC_DOUT
) | (1 << GPIO_SSC_CLK
);
449 PIO_OUTPUT_ENABLE
= (1 << GPIO_SSC_DOUT
);
450 PIO_OUTPUT_DISABLE
= (1 << GPIO_SSC_CLK
);
452 #define SHORT_COIL() LOW(GPIO_SSC_DOUT)
453 #define OPEN_COIL() HIGH(GPIO_SSC_DOUT)
457 while(!(PIO_PIN_DATA_STATUS
& (1<<GPIO_SSC_CLK
))) {
459 DbpString("Stopped");
476 while(PIO_PIN_DATA_STATUS
& (1<<GPIO_SSC_CLK
)) {
478 DbpString("Stopped");
485 if(i
== period
) i
= 0;
489 /* Provides a framework for bidirectional LF tag communication
490 * Encoding is currently Hitag2, but the general idea can probably
491 * be transferred to other encodings.
493 * The new FPGA code will, for the LF simulator mode, give on SSC_FRAME
494 * (PA15) a thresholded version of the signal from the ADC. Setting the
495 * ADC path to the low frequency peak detection signal, will enable a
496 * somewhat reasonable receiver for modulation on the carrier signal
497 * that is generated by the reader. The signal is low when the reader
498 * field is switched off, and high when the reader field is active. Due
499 * to the way that the signal looks like, mostly only the rising edge is
500 * useful, your mileage may vary.
502 * Neat perk: PA15 can not only be used as a bit-banging GPIO, but is also
503 * TIOA1, which can be used as the capture input for timer 1. This should
504 * make it possible to measure the exact edge-to-edge time, without processor
507 * Arguments: divisor is the divisor to be sent to the FPGA (e.g. 95 for 125kHz)
508 * t0 is the carrier frequency cycle duration in terms of MCK (384 for 125kHz)
510 * The following defines are in carrier periods:
512 #define HITAG_T_0_MIN 15 /* T[0] should be 18..22 */
513 #define HITAG_T_1_MIN 24 /* T[1] should be 26..30 */
514 #define HITAG_T_EOF 40 /* T_EOF should be > 36 */
515 #define HITAG_T_WRESP 208 /* T_wresp should be 204..212 */
517 static void hitag_handle_frame(int t0
, int frame_len
, char *frame
);
518 //#define DEBUG_RA_VALUES 1
519 #define DEBUG_FRAME_CONTENTS 1
520 void SimulateTagLowFrequencyBidir(int divisor
, int t0
)
522 #if DEBUG_RA_VALUES || DEBUG_FRAME_CONTENTS
528 DbpString("Starting Hitag2 emulator, press button to end");
531 /* Set up simulator mode, frequency divisor which will drive the FPGA
532 * and analog mux selection.
534 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_SIMULATOR
);
535 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, divisor
);
536 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
540 * Capture mode, timer source MCK/2 (TIMER_CLOCK1), TIOA is external trigger,
541 * external trigger rising edge, load RA on rising edge of TIOA, load RB on rising
542 * edge of TIOA. Assign PA15 to TIOA1 (peripheral B)
545 PMC_PERIPHERAL_CLK_ENABLE
= (1 << PERIPH_TC1
);
546 PIO_PERIPHERAL_B_SEL
= (1 << GPIO_SSC_FRAME
);
547 TC1_CCR
= TC_CCR_CLKDIS
;
548 TC1_CMR
= TC_CMR_TCCLKS_TIMER_CLOCK1
| TC_CMR_ETRGEDG_RISING
| TC_CMR_ABETRG
|
549 TC_CMR_LDRA_RISING
| TC_CMR_LDRB_RISING
;
550 TC1_CCR
= TC_CCR_CLKEN
| TC_CCR_SWTRG
;
552 /* calculate the new value for the carrier period in terms of TC1 values */
556 while(!BUTTON_PRESS()) {
558 if(TC1_SR
& TC_SR_LDRAS
) {
560 if((ra
> t0
*HITAG_T_EOF
) | overflow
) ra
= t0
*HITAG_T_EOF
+1;
562 if(ra
> 255 || overflow
) ra
= 255;
563 ((char*)BigBuf
)[i
] = ra
;
567 if(overflow
|| (ra
> t0
*HITAG_T_EOF
) || (ra
< t0
*HITAG_T_0_MIN
)) {
569 } else if(ra
>= t0
*HITAG_T_1_MIN
) {
571 if(frame_pos
< 8*sizeof(frame
)) {
572 frame
[frame_pos
/ 8] |= 1<<( 7-(frame_pos
%8) );
575 } else if(ra
>= t0
*HITAG_T_0_MIN
) {
577 if(frame_pos
< 8*sizeof(frame
)) {
578 frame
[frame_pos
/ 8] |= 0<<( 7-(frame_pos
%8) );
586 if(TC1_CV
> t0
*HITAG_T_EOF
) {
587 /* Minor nuisance: In Capture mode, the timer can not be
588 * stopped by a Compare C. There's no way to stop the clock
589 * in software, so we'll just have to note the fact that an
590 * overflow happened and the next loaded timer value might
591 * have wrapped. Also, this marks the end of frame, and the
592 * still running counter can be used to determine the correct
593 * time for the start of the reply.
598 /* Have a frame, do something with it */
599 #if DEBUG_FRAME_CONTENTS
600 ((char*)BigBuf
)[i
++] = frame_pos
;
601 memcpy( ((char*)BigBuf
)+i
, frame
, 7);
603 i
= i
% sizeof(BigBuf
);
605 hitag_handle_frame(t0
, frame_pos
, frame
);
606 memset(frame
, 0, sizeof(frame
));
614 DbpString("All done");
617 static void hitag_send_bit(int t0
, int bit
) {
619 /* Manchester: Loaded, then unloaded */
622 while(TC1_CV
< t0
*15);
624 while(TC1_CV
< t0
*31);
626 } else if(bit
== 0) {
627 /* Manchester: Unloaded, then loaded */
630 while(TC1_CV
< t0
*15);
632 while(TC1_CV
< t0
*31);
635 TC1_CCR
= TC_CCR_SWTRG
; /* Reset clock for the next bit */
638 static void hitag_send_frame(int t0
, int frame_len
, const char const * frame
, int fdt
)
641 PIO_OUTPUT_ENABLE
= (1 << GPIO_SSC_DOUT
);
643 /* Wait for HITAG_T_WRESP carrier periods after the last reader bit,
644 * not that since the clock counts since the rising edge, but T_wresp is
645 * with respect to the falling edge, we need to wait actually (T_wresp - T_g)
646 * periods. The gap time T_g varies (4..10).
648 while(TC1_CV
< t0
*(fdt
-8));
650 int saved_cmr
= TC1_CMR
;
651 TC1_CMR
&= ~TC_CMR_ETRGEDG
; /* Disable external trigger for the clock */
652 TC1_CCR
= TC_CCR_SWTRG
; /* Reset the clock and use it for response timing */
656 hitag_send_bit(t0
, 1); /* Start of frame */
658 for(i
=0; i
<frame_len
; i
++) {
659 hitag_send_bit(t0
, !!(frame
[i
/ 8] & (1<<( 7-(i
%8) ))) );
666 /* Callback structure to cleanly separate tag emulation code from the radio layer. */
667 static int hitag_cb(const char* response_data
, const int response_length
, const int fdt
, void *cb_cookie
)
669 hitag_send_frame(*(int*)cb_cookie
, response_length
, response_data
, fdt
);
672 /* Frame length in bits, frame contents in MSBit first format */
673 static void hitag_handle_frame(int t0
, int frame_len
, char *frame
)
675 hitag2_handle_command(frame
, frame_len
, hitag_cb
, &t0
);
678 // compose fc/8 fc/10 waveform
679 static void fc(int c
, int *n
) {
680 BYTE
*dest
= (BYTE
*)BigBuf
;
683 // for when we want an fc8 pattern every 4 logical bits
694 // an fc/8 encoded bit is a bit pattern of 11000000 x6 = 48 samples
696 for (idx
=0; idx
<6; idx
++) {
708 // an fc/10 encoded bit is a bit pattern of 1110000000 x5 = 50 samples
710 for (idx
=0; idx
<5; idx
++) {
725 // prepare a waveform pattern in the buffer based on the ID given then
726 // simulate a HID tag until the button is pressed
727 void CmdHIDsimTAG(int hi
, int lo
, int ledcontrol
)
731 HID tag bitstream format
732 The tag contains a 44bit unique code. This is sent out MSB first in sets of 4 bits
733 A 1 bit is represented as 6 fc8 and 5 fc10 patterns
734 A 0 bit is represented as 5 fc10 and 6 fc8 patterns
735 A fc8 is inserted before every 4 bits
736 A special start of frame pattern is used consisting a0b0 where a and b are neither 0
737 nor 1 bits, they are special patterns (a = set of 12 fc8 and b = set of 10 fc10)
741 DbpString("Tags can only have 44 bits.");
745 // special start of frame marker containing invalid bit sequences
746 fc(8, &n
); fc(8, &n
); // invalid
747 fc(8, &n
); fc(10, &n
); // logical 0
748 fc(10, &n
); fc(10, &n
); // invalid
749 fc(8, &n
); fc(10, &n
); // logical 0
752 // manchester encode bits 43 to 32
753 for (i
=11; i
>=0; i
--) {
754 if ((i
%4)==3) fc(0,&n
);
756 fc(10, &n
); fc(8, &n
); // low-high transition
758 fc(8, &n
); fc(10, &n
); // high-low transition
763 // manchester encode bits 31 to 0
764 for (i
=31; i
>=0; i
--) {
765 if ((i
%4)==3) fc(0,&n
);
767 fc(10, &n
); fc(8, &n
); // low-high transition
769 fc(8, &n
); fc(10, &n
); // high-low transition
775 SimulateTagLowFrequency(n
, ledcontrol
);
782 // loop to capture raw HID waveform then FSK demodulate the TAG ID from it
783 void CmdHIDdemodFSK(int findone
, int *high
, int *low
, int ledcontrol
)
785 BYTE
*dest
= (BYTE
*)BigBuf
;
786 int m
=0, n
=0, i
=0, idx
=0, found
=0, lastval
=0;
789 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, 95); //125Khz
790 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
);
792 // Connect the A/D to the peak-detected low-frequency path.
793 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
795 // Give it a bit of time for the resonant antenna to settle.
798 // Now set up the SSC to get the ADC samples that are now streaming at us.
806 DbpString("Stopped");
816 if(SSC_STATUS
& (SSC_STATUS_TX_READY
)) {
817 SSC_TRANSMIT_HOLDING
= 0x43;
821 if(SSC_STATUS
& (SSC_STATUS_RX_READY
)) {
822 dest
[i
] = (BYTE
)SSC_RECEIVE_HOLDING
;
823 // we don't care about actual value, only if it's more or less than a
824 // threshold essentially we capture zero crossings for later analysis
825 if(dest
[i
] < 127) dest
[i
] = 0; else dest
[i
] = 1;
837 // sync to first lo-hi transition
838 for( idx
=1; idx
<m
; idx
++) {
839 if (dest
[idx
-1]<dest
[idx
])
845 // count cycles between consecutive lo-hi transitions, there should be either 8 (fc/8)
846 // or 10 (fc/10) cycles but in practice due to noise etc we may end up with with anywhere
847 // between 7 to 11 cycles so fuzz it by treat anything <9 as 8 and anything else as 10
848 for( i
=0; idx
<m
; idx
++) {
849 if (dest
[idx
-1]<dest
[idx
]) {
864 // we now have a set of cycle counts, loop over previous results and aggregate data into bit patterns
869 for( idx
=0; idx
<m
; idx
++) {
870 if (dest
[idx
]==lastval
) {
873 // a bit time is five fc/10 or six fc/8 cycles so figure out how many bits a pattern width represents,
874 // an extra fc/8 pattern preceeds every 4 bits (about 200 cycles) just to complicate things but it gets
875 // swallowed up by rounding
876 // expected results are 1 or 2 bits, any more and it's an invalid manchester encoding
877 // special start of frame markers use invalid manchester states (no transitions) by using sequences
880 n
=(n
+1)/6; // fc/8 in sets of 6
882 n
=(n
+1)/5; // fc/10 in sets of 5
884 switch (n
) { // stuff appropriate bits in buffer
887 dest
[i
++]=dest
[idx
-1];
890 dest
[i
++]=dest
[idx
-1];
891 dest
[i
++]=dest
[idx
-1];
893 case 3: // 3 bit start of frame markers
894 dest
[i
++]=dest
[idx
-1];
895 dest
[i
++]=dest
[idx
-1];
896 dest
[i
++]=dest
[idx
-1];
898 // When a logic 0 is immediately followed by the start of the next transmisson
899 // (special pattern) a pattern of 4 bit duration lengths is created.
901 dest
[i
++]=dest
[idx
-1];
902 dest
[i
++]=dest
[idx
-1];
903 dest
[i
++]=dest
[idx
-1];
904 dest
[i
++]=dest
[idx
-1];
906 default: // this shouldn't happen, don't stuff any bits
916 // final loop, go over previously decoded manchester data and decode into usable tag ID
917 // 111000 bit pattern represent start of frame, 01 pattern represents a 1 and 10 represents a 0
918 for( idx
=0; idx
<m
-6; idx
++) {
919 // search for a start of frame marker
920 if ( dest
[idx
] && dest
[idx
+1] && dest
[idx
+2] && (!dest
[idx
+3]) && (!dest
[idx
+4]) && (!dest
[idx
+5]) )
924 if (found
&& (hi
|lo
)) {
926 DbpIntegers(hi
, lo
, (lo
>>1)&0xffff);
927 /* if we're only looking for one tag */
940 if (dest
[idx
] && (!dest
[idx
+1]) ) {
943 } else if ( (!dest
[idx
]) && dest
[idx
+1]) {
953 if ( dest
[idx
] && dest
[idx
+1] && dest
[idx
+2] && (!dest
[idx
+3]) && (!dest
[idx
+4]) && (!dest
[idx
+5]) )
957 if (found
&& (hi
|lo
)) {
959 DbpIntegers(hi
, lo
, (lo
>>1)&0xffff);
960 /* if we're only looking for one tag */