1 //-----------------------------------------------------------------------------
2 // (c) 2009 Henryk Plötz <henryk@ploetzli.ch>
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // LEGIC RF simulation code
9 //-----------------------------------------------------------------------------
11 #include "proxmark3.h"
17 #include "legic_prng.h"
20 static struct legic_frame
{
31 static crc_t legic_crc
;
32 static int legic_read_count
;
33 static uint32_t legic_prng_bc
;
34 static uint32_t legic_prng_iv
;
36 static int legic_phase_drift
;
37 static int legic_frame_drift
;
38 static int legic_reqresp_drift
;
43 static void setup_timer(void)
45 /* Set up Timer 1 to use for measuring time between pulses. Since we're bit-banging
46 * this it won't be terribly accurate but should be good enough.
48 AT91C_BASE_PMC
->PMC_PCER
= (1 << AT91C_ID_TC1
);
49 timer
= AT91C_BASE_TC1
;
50 timer
->TC_CCR
= AT91C_TC_CLKDIS
;
51 timer
->TC_CMR
= AT91C_TC_CLKS_TIMER_DIV3_CLOCK
;
52 timer
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
55 * Set up Timer 2 to use for measuring time between frames in
56 * tag simulation mode. Runs 4x faster as Timer 1
58 AT91C_BASE_PMC
->PMC_PCER
= (1 << AT91C_ID_TC2
);
59 prng_timer
= AT91C_BASE_TC2
;
60 prng_timer
->TC_CCR
= AT91C_TC_CLKDIS
;
61 prng_timer
->TC_CMR
= AT91C_TC_CLKS_TIMER_DIV2_CLOCK
;
62 prng_timer
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
65 /* At TIMER_CLOCK3 (MCK/32) */
66 #define RWD_TIME_1 150 /* RWD_TIME_PAUSE off, 80us on = 100us */
67 #define RWD_TIME_0 90 /* RWD_TIME_PAUSE off, 40us on = 60us */
68 #define RWD_TIME_PAUSE 30 /* 20us */
69 #define RWD_TIME_FUZZ 20 /* rather generous 13us, since the peak detector + hysteresis fuzz quite a bit */
70 #define TAG_TIME_BIT 150 /* 100us for every bit */
71 #define TAG_TIME_WAIT 490 /* time from RWD frame end to tag frame start, experimentally determined */
73 #define SIM_DIVISOR 586 /* prng_time/SIM_DIVISOR count prng needs to be forwared */
74 #define SIM_SHIFT 900 /* prng_time+SIM_SHIFT shift of delayed start */
76 #define SESSION_IV 0x55
77 #define OFFSET_LOG 1024
79 #define FUZZ_EQUAL(value, target, fuzz) ((value) > ((target)-(fuzz)) && (value) < ((target)+(fuzz)))
81 /* Generate Keystream */
82 static uint32_t get_key_stream(int skip
, int count
)
84 uint32_t key
=0; int i
;
86 /* Use int to enlarge timer tc to 32bit */
87 legic_prng_bc
+= prng_timer
->TC_CV
;
88 prng_timer
->TC_CCR
= AT91C_TC_SWTRG
;
90 /* If skip == -1, forward prng time based */
92 i
= (legic_prng_bc
+SIM_SHIFT
)/SIM_DIVISOR
; /* Calculate Cycles based on timer */
93 i
-= legic_prng_count(); /* substract cycles of finished frames */
94 i
-= count
; /* substract current frame length, rewidn to bedinning */
95 legic_prng_forward(i
);
97 legic_prng_forward(skip
);
100 /* Write Time Data into LOG */
101 uint8_t *BigBuf
= BigBuf_get_addr();
102 i
= (count
== 6) ? -1 : legic_read_count
;
104 BigBuf
[OFFSET_LOG
+128+i
] = legic_prng_count();
105 BigBuf
[OFFSET_LOG
+256+i
*4] = (legic_prng_bc
>> 0) & 0xff;
106 BigBuf
[OFFSET_LOG
+256+i
*4+1] = (legic_prng_bc
>> 8) & 0xff;
107 BigBuf
[OFFSET_LOG
+256+i
*4+2] = (legic_prng_bc
>>16) & 0xff;
108 BigBuf
[OFFSET_LOG
+256+i
*4+3] = (legic_prng_bc
>>24) & 0xff;
109 BigBuf
[OFFSET_LOG
+384+i
] = count
;
111 /* Generate KeyStream */
112 for(i
=0; i
<count
; i
++) {
113 key
|= legic_prng_get_bit() << i
;
114 legic_prng_forward(1);
119 /* Send a frame in tag mode, the FPGA must have been set up by
122 static void frame_send_tag(uint16_t response
, int bits
, int crypt
)
124 /* Bitbang the response */
125 AT91C_BASE_PIOA
->PIO_CODR
= GPIO_SSC_DOUT
;
126 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
127 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
129 /* Use time to crypt frame */
131 legic_prng_forward(2); /* TAG_TIME_WAIT -> shift by 2 */
133 for(i
=0; i
<bits
; i
++) {
134 key
|= legic_prng_get_bit() << i
;
135 legic_prng_forward(1);
137 //Dbprintf("key = 0x%x", key);
138 response
= response
^ key
;
141 /* Wait for the frame start */
142 while(timer
->TC_CV
< (TAG_TIME_WAIT
- 30)) ;
145 for(i
=0; i
<bits
; i
++) {
146 int nextbit
= timer
->TC_CV
+ TAG_TIME_BIT
;
147 int bit
= response
& 1;
148 response
= response
>> 1;
150 AT91C_BASE_PIOA
->PIO_SODR
= GPIO_SSC_DOUT
;
152 AT91C_BASE_PIOA
->PIO_CODR
= GPIO_SSC_DOUT
;
154 while(timer
->TC_CV
< nextbit
) ;
156 AT91C_BASE_PIOA
->PIO_CODR
= GPIO_SSC_DOUT
;
159 /* Send a frame in reader mode, the FPGA must have been set up by
162 static void frame_send_rwd(uint32_t data
, int bits
)
165 timer
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
166 while(timer
->TC_CV
> 1) ; /* Wait till the clock has reset */
169 for(i
=0; i
<bits
; i
++) {
170 int starttime
= timer
->TC_CV
;
171 int pause_end
= starttime
+ RWD_TIME_PAUSE
, bit_end
;
175 if(bit
^ legic_prng_get_bit())
176 bit_end
= starttime
+ RWD_TIME_1
;
178 bit_end
= starttime
+ RWD_TIME_0
;
181 /* RWD_TIME_PAUSE time off, then some time on, so that the complete bit time is
182 * RWD_TIME_x, where x is the bit to be transmitted */
183 AT91C_BASE_PIOA
->PIO_CODR
= GPIO_SSC_DOUT
;
184 while(timer
->TC_CV
< pause_end
) ;
185 AT91C_BASE_PIOA
->PIO_SODR
= GPIO_SSC_DOUT
;
186 legic_prng_forward(1); /* bit duration is longest. use this time to forward the lfsr */
188 while(timer
->TC_CV
< bit_end
);
191 /* One final pause to mark the end of the frame */
192 int pause_end
= timer
->TC_CV
+ RWD_TIME_PAUSE
;
193 AT91C_BASE_PIOA
->PIO_CODR
= GPIO_SSC_DOUT
;
194 while(timer
->TC_CV
< pause_end
) ;
195 AT91C_BASE_PIOA
->PIO_SODR
= GPIO_SSC_DOUT
;
198 /* Reset the timer, to measure time until the start of the tag frame */
199 timer
->TC_CCR
= AT91C_TC_SWTRG
;
200 while(timer
->TC_CV
> 1) ; /* Wait till the clock has reset */
203 /* Receive a frame from the card in reader emulation mode, the FPGA and
204 * timer must have been set up by LegicRfReader and frame_send_rwd.
206 * The LEGIC RF protocol from card to reader does not include explicit
207 * frame start/stop information or length information. The reader must
208 * know beforehand how many bits it wants to receive. (Notably: a card
209 * sending a stream of 0-bits is indistinguishable from no card present.)
211 * Receive methodology: There is a fancy correlator in hi_read_rx_xcorr, but
212 * I'm not smart enough to use it. Instead I have patched hi_read_tx to output
213 * the ADC signal with hysteresis on SSP_DIN. Bit-bang that signal and look
214 * for edges. Count the edges in each bit interval. If they are approximately
215 * 0 this was a 0-bit, if they are approximately equal to the number of edges
216 * expected for a 212kHz subcarrier, this was a 1-bit. For timing we use the
217 * timer that's still running from frame_send_rwd in order to get a synchronization
218 * with the frame that we just sent.
220 * FIXME: Because we're relying on the hysteresis to just do the right thing
221 * the range is severely reduced (and you'll probably also need a good antenna).
222 * So this should be fixed some time in the future for a proper receiver.
224 static void frame_receive_rwd(struct legic_frame
* const f
, int bits
, int crypt
)
226 uint32_t the_bit
= 1; /* Use a bitmask to save on shifts */
228 int i
, old_level
=0, edges
=0;
229 int next_bit_at
= TAG_TIME_WAIT
;
235 AT91C_BASE_PIOA
->PIO_ODR
= GPIO_SSC_DIN
;
236 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DIN
;
238 /* we have some time now, precompute the cipher
239 * since we cannot compute it on the fly while reading */
240 legic_prng_forward(2);
243 for(i
=0; i
<bits
; i
++) {
244 data
|= legic_prng_get_bit() << i
;
245 legic_prng_forward(1);
249 while(timer
->TC_CV
< next_bit_at
) ;
251 next_bit_at
+= TAG_TIME_BIT
;
253 for(i
=0; i
<bits
; i
++) {
255 while(timer
->TC_CV
< next_bit_at
) {
256 int level
= (AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_DIN
);
257 if(level
!= old_level
)
261 next_bit_at
+= TAG_TIME_BIT
;
263 if(edges
> 20 && edges
< 60) { /* expected are 42 edges */
272 /* Reset the timer, to synchronize the next frame */
273 timer
->TC_CCR
= AT91C_TC_SWTRG
;
274 while(timer
->TC_CV
> 1) ; /* Wait till the clock has reset */
277 static void frame_append_bit(struct legic_frame
* const f
, int bit
)
280 return; /* Overflow, won't happen */
282 f
->data
|= (bit
<< f
->bits
);
286 static void frame_clean(struct legic_frame
* const f
)
292 static uint32_t perform_setup_phase_rwd(int iv
)
295 /* Switch on carrier and let the tag charge for 1ms */
296 AT91C_BASE_PIOA
->PIO_SODR
= GPIO_SSC_DOUT
;
299 legic_prng_init(0); /* no keystream yet */
300 frame_send_rwd(iv
, 7);
303 frame_clean(¤t_frame
);
304 frame_receive_rwd(¤t_frame
, 6, 1);
305 legic_prng_forward(1); /* we wait anyways */
306 while(timer
->TC_CV
< 387) ; /* ~ 258us */
307 frame_send_rwd(0x19, 6);
309 return current_frame
.data
;
312 static void LegicCommonInit(void) {
313 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
314 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
316 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX
);
318 /* Bitbang the transmitter */
319 AT91C_BASE_PIOA
->PIO_CODR
= GPIO_SSC_DOUT
;
320 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
321 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
325 crc_init(&legic_crc
, 4, 0x19 >> 1, 0x5, 0);
328 /* Switch off carrier, make sure tag is reset */
329 static void switch_off_tag_rwd(void)
331 AT91C_BASE_PIOA
->PIO_CODR
= GPIO_SSC_DOUT
;
335 /* calculate crc for a legic command */
336 static int LegicCRC(int byte_index
, int value
, int cmd_sz
) {
337 crc_clear(&legic_crc
);
338 crc_update(&legic_crc
, 1, 1); /* CMD_READ */
339 crc_update(&legic_crc
, byte_index
, cmd_sz
-1);
340 crc_update(&legic_crc
, value
, 8);
341 return crc_finish(&legic_crc
);
344 int legic_read_byte(int byte_index
, int cmd_sz
) {
347 legic_prng_forward(4); /* we wait anyways */
348 while(timer
->TC_CV
< 387) ; /* ~ 258us + 100us*delay */
350 frame_send_rwd(1 | (byte_index
<< 1), cmd_sz
);
351 frame_clean(¤t_frame
);
353 frame_receive_rwd(¤t_frame
, 12, 1);
355 byte
= current_frame
.data
& 0xff;
357 if( LegicCRC(byte_index
, byte
, cmd_sz
) != (current_frame
.data
>> 8) ) {
358 Dbprintf("!!! crc mismatch: expected %x but got %x !!!",
359 LegicCRC(byte_index
, current_frame
.data
& 0xff, cmd_sz
),
360 current_frame
.data
>> 8);
367 /* legic_write_byte() is not included, however it's trivial to implement
368 * and here are some hints on what remains to be done:
370 * * assemble a write_cmd_frame with crc and send it
371 * * wait until the tag sends back an ACK ('1' bit unencrypted)
372 * * forward the prng based on the timing
374 //int legic_write_byte(int byte, int addr, int addr_sz, int PrngCorrection) {
375 int legic_write_byte(int byte
, int addr
, int addr_sz
) {
376 //do not write UID, CRC
380 //== send write command ==============================
381 crc_clear(&legic_crc
);
382 crc_update(&legic_crc
, 0, 1); /* CMD_WRITE */
383 crc_update(&legic_crc
, addr
, addr_sz
);
384 crc_update(&legic_crc
, byte
, 8);
386 uint32_t crc
= crc_finish(&legic_crc
);
387 uint32_t cmd
= ((crc
<<(addr_sz
+1+8)) //CRC
388 |(byte
<<(addr_sz
+1)) //Data
389 |(addr
<<1) //Address
390 |(0x00 <<0)); //CMD = W
391 uint32_t cmd_sz
= addr_sz
+1+8+4; //crc+data+cmd
393 legic_prng_forward(4); /* we wait anyways */
394 while(timer
->TC_CV
< 387) ; /* ~ 258us */
395 frame_send_rwd(cmd
, cmd_sz
);
397 AT91C_BASE_PIOA
->PIO_ODR
= GPIO_SSC_DIN
;
398 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DIN
;
400 //== wait for ack ====================================
401 int t
, old_level
=0, edges
=0;
403 while(timer
->TC_CV
< 387) ; /* ~ 258us */
404 for(t
=0; t
<80; t
++) {
406 next_bit_at
+= TAG_TIME_BIT
;
407 while(timer
->TC_CV
< next_bit_at
) {
408 int level
= (AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_DIN
);
409 if(level
!= old_level
) {
414 if(edges
> 20 && edges
< 60) { /* expected are 42 edges */
415 int t
= timer
->TC_CV
;
416 int c
= t
/TAG_TIME_BIT
;
417 timer
->TC_CCR
= AT91C_TC_SWTRG
;
418 while(timer
->TC_CV
> 1) ; /* Wait till the clock has reset */
419 legic_prng_forward(c
-1);
423 timer
->TC_CCR
= AT91C_TC_SWTRG
;
424 while(timer
->TC_CV
> 1) ; /* Wait till the clock has reset */
428 int LegicRfReader(int offset
, int bytes
) {
430 // ice_legic_setup();
431 // ice_legic_select_card();
434 int byte_index
=0, cmd_sz
=0, card_sz
=0;
438 uint8_t *BigBuf
= BigBuf_get_addr();
439 memset(BigBuf
, 0, 1024);
441 DbpString("setting up legic card");
442 uint32_t tag_type
= perform_setup_phase_rwd(SESSION_IV
);
443 switch_off_tag_rwd(); //we lose to mutch time with dprintf
446 DbpString("MIM22 card found, reading card ...");
451 DbpString("MIM256 card found, reading card ...");
456 DbpString("MIM1024 card found, reading card ...");
461 Dbprintf("Unknown card format: %x",tag_type
);
467 if(bytes
+offset
>= card_sz
)
468 bytes
= card_sz
-offset
;
470 perform_setup_phase_rwd(SESSION_IV
);
473 while(byte_index
< bytes
) {
474 int r
= legic_read_byte(byte_index
+offset
, cmd_sz
);
475 if(r
== -1 ||BUTTON_PRESS()) {
476 DbpString("operation aborted");
477 switch_off_tag_rwd();
482 BigBuf
[byte_index
] = r
;
485 if (byte_index
& 0x10) LED_C_ON(); else LED_C_OFF();
489 switch_off_tag_rwd();
490 Dbprintf("Card read, use 'hf legic decode' or");
491 Dbprintf("'data hexsamples %d' to view results", (bytes
+7) & ~7);
495 /*int _LegicRfWriter(int bytes, int offset, int addr_sz, uint8_t *BigBuf, int RoundBruteforceValue) {
499 perform_setup_phase_rwd(SESSION_IV);
500 //legic_prng_forward(2);
501 while(byte_index < bytes) {
504 //check if the DCF should be changed
505 if ( (offset == 0x05) && (bytes == 0x02) ) {
506 //write DCF in reverse order (addr 0x06 before 0x05)
507 r = legic_write_byte(BigBuf[(0x06-byte_index)], (0x06-byte_index), addr_sz, RoundBruteforceValue);
508 //legic_prng_forward(1);
511 r = legic_write_byte(BigBuf[(0x06-byte_index)], (0x06-byte_index), addr_sz, RoundBruteforceValue);
513 //legic_prng_forward(1);
516 r = legic_write_byte(BigBuf[byte_index+offset], byte_index+offset, addr_sz, RoundBruteforceValue);
518 if((r != 0) || BUTTON_PRESS()) {
519 Dbprintf("operation aborted @ 0x%03.3x", byte_index);
520 switch_off_tag_rwd();
528 if(byte_index & 0x10) LED_C_ON(); else LED_C_OFF();
532 DbpString("write successful");
536 void LegicRfWriter(int bytes
, int offset
) {
537 int byte_index
=0, addr_sz
=0;
538 uint8_t *BigBuf
= BigBuf_get_addr();
542 DbpString("setting up legic card");
543 uint32_t tag_type
= perform_setup_phase_rwd(SESSION_IV
);
544 switch_off_tag_rwd();
547 if(offset
+bytes
> 22) {
548 Dbprintf("Error: can not write to 0x%03.3x on MIM22", offset
+bytes
);
552 Dbprintf("MIM22 card found, writing 0x%02.2x - 0x%02.2x ...", offset
, offset
+bytes
);
555 if(offset
+bytes
> 0x100) {
556 Dbprintf("Error: can not write to 0x%03.3x on MIM256", offset
+bytes
);
560 Dbprintf("MIM256 card found, writing 0x%02.2x - 0x%02.2x ...", offset
, offset
+bytes
);
563 if(offset
+bytes
> 0x400) {
564 Dbprintf("Error: can not write to 0x%03.3x on MIM1024", offset
+bytes
);
568 Dbprintf("MIM1024 card found, writing 0x%03.3x - 0x%03.3x ...", offset
, offset
+bytes
);
571 Dbprintf("No or unknown card found, aborting");
577 perform_setup_phase_rwd(SESSION_IV
);
579 while(byte_index
< bytes
) {
582 //check if the DCF should be changed
583 if ( ((byte_index
+offset
) == 0x05) && (bytes
>= 0x02) ) {
584 //write DCF in reverse order (addr 0x06 before 0x05)
585 r
= legic_write_byte(BigBuf
[(0x06-byte_index
)], (0x06-byte_index
), addr_sz
);
587 // write second byte on success...
590 r
= legic_write_byte(BigBuf
[(0x06-byte_index
)], (0x06-byte_index
), addr_sz
);
594 r
= legic_write_byte(BigBuf
[byte_index
+offset
], byte_index
+offset
, addr_sz
);
596 if((r
!= 0) || BUTTON_PRESS()) {
597 Dbprintf("operation aborted @ 0x%03.3x", byte_index
);
598 switch_off_tag_rwd();
606 if(byte_index
& 0x10) LED_C_ON(); else LED_C_OFF();
610 DbpString("write successful");
612 for(byte_index
= -2; byte_index
< 200; byte_index
++)
614 Dbprintf("+ Try RndValue %d...", byte_index
);
615 if(_LegicRfWriter(bytes
, offset
, addr_sz
, BigBuf
, byte_index
) == 0)
622 void LegicRfRawWriter(int offset
, int byte
) {
623 int byte_index
=0, addr_sz
=0;
627 DbpString("setting up legic card");
628 uint32_t tag_type
= perform_setup_phase_rwd(SESSION_IV
);
629 switch_off_tag_rwd();
633 Dbprintf("Error: can not write to 0x%03.3x on MIM22", offset
);
637 Dbprintf("MIM22 card found, writing at addr 0x%02.2x - value 0x%02.2x ...", offset
, byte
);
641 Dbprintf("Error: can not write to 0x%03.3x on MIM256", offset
);
645 Dbprintf("MIM256 card found, writing at addr 0x%02.2x - value 0x%02.2x ...", offset
, byte
);
649 Dbprintf("Error: can not write to 0x%03.3x on MIM1024", offset
);
653 Dbprintf("MIM1024 card found, writing at addr 0x%03.3x - value 0x%03.3x ...", offset
, byte
);
656 Dbprintf("No or unknown card found, aborting");
659 Dbprintf("integer value: %d offset: %d addr_sz: %d", byte
, offset
, addr_sz
);
661 perform_setup_phase_rwd(SESSION_IV
);
662 //legic_prng_forward(2);
664 int r
= legic_write_byte(byte
, offset
, addr_sz
);
666 if((r
!= 0) || BUTTON_PRESS()) {
667 Dbprintf("operation aborted @ 0x%03.3x (%1d)", byte_index
, r
);
668 switch_off_tag_rwd();
676 DbpString("write successful");
681 /* Handle (whether to respond) a frame in tag mode */
682 static void frame_handle_tag(struct legic_frame
const * const f
)
684 uint8_t *BigBuf
= BigBuf_get_addr();
686 /* First Part of Handshake (IV) */
688 if(f
->data
== SESSION_IV
) {
690 prng_timer
->TC_CCR
= AT91C_TC_SWTRG
;
691 legic_prng_init(f
->data
);
692 frame_send_tag(0x3d, 6, 1); /* 0x3d^0x26 = 0x1b */
693 legic_state
= STATE_IV
;
694 legic_read_count
= 0;
696 legic_prng_iv
= f
->data
;
699 timer
->TC_CCR
= AT91C_TC_SWTRG
;
700 while(timer
->TC_CV
> 1);
701 while(timer
->TC_CV
< 280);
703 } else if((prng_timer
->TC_CV
% 50) > 40) {
704 legic_prng_init(f
->data
);
705 frame_send_tag(0x3d, 6, 1);
712 if(legic_state
== STATE_IV
) {
713 if((f
->bits
== 6) && (f
->data
== (0x19 ^ get_key_stream(1, 6)))) {
714 legic_state
= STATE_CON
;
717 timer
->TC_CCR
= AT91C_TC_SWTRG
;
718 while(timer
->TC_CV
> 1);
719 while(timer
->TC_CV
< 200);
722 legic_state
= STATE_DISCON
;
724 Dbprintf("0x19 - Frame: %03.3x", f
->data
);
731 if(legic_state
== STATE_CON
) {
732 int key
= get_key_stream(-1, 11); //legic_phase_drift, 11);
733 int addr
= f
->data
^ key
; addr
= addr
>> 1;
734 int data
= BigBuf
[addr
];
735 int hash
= LegicCRC(addr
, data
, 11) << 8;
736 BigBuf
[OFFSET_LOG
+legic_read_count
] = (uint8_t)addr
;
739 //Dbprintf("Data:%03.3x, key:%03.3x, addr: %03.3x, read_c:%u", f->data, key, addr, read_c);
740 legic_prng_forward(legic_reqresp_drift
);
742 frame_send_tag(hash
| data
, 12, 1);
745 timer
->TC_CCR
= AT91C_TC_SWTRG
;
746 while(timer
->TC_CV
> 1);
747 legic_prng_forward(legic_frame_drift
);
748 while(timer
->TC_CV
< 180);
755 int key
= get_key_stream(-1, 23); //legic_frame_drift, 23);
756 int addr
= f
->data
^ key
; addr
= addr
>> 1; addr
= addr
& 0x3ff;
757 int data
= f
->data
^ key
; data
= data
>> 11; data
= data
& 0xff;
760 legic_state
= STATE_DISCON
;
762 Dbprintf("write - addr: %x, data: %x", addr
, data
);
766 if(legic_state
!= STATE_DISCON
) {
767 Dbprintf("Unexpected: sz:%u, Data:%03.3x, State:%u, Count:%u", f
->bits
, f
->data
, legic_state
, legic_read_count
);
769 Dbprintf("IV: %03.3x", legic_prng_iv
);
770 for(i
= 0; i
<legic_read_count
; i
++) {
771 Dbprintf("Read Nb: %u, Addr: %u", i
, BigBuf
[OFFSET_LOG
+i
]);
774 for(i
= -1; i
<legic_read_count
; i
++) {
776 t
= BigBuf
[OFFSET_LOG
+256+i
*4];
777 t
|= BigBuf
[OFFSET_LOG
+256+i
*4+1] << 8;
778 t
|= BigBuf
[OFFSET_LOG
+256+i
*4+2] <<16;
779 t
|= BigBuf
[OFFSET_LOG
+256+i
*4+3] <<24;
781 Dbprintf("Cycles: %u, Frame Length: %u, Time: %u",
782 BigBuf
[OFFSET_LOG
+128+i
],
783 BigBuf
[OFFSET_LOG
+384+i
],
787 legic_state
= STATE_DISCON
;
788 legic_read_count
= 0;
794 /* Read bit by bit untill full frame is received
795 * Call to process frame end answer
797 static void emit(int bit
)
800 if(current_frame
.bits
<= 4) {
801 frame_clean(¤t_frame
);
803 frame_handle_tag(¤t_frame
);
804 frame_clean(¤t_frame
);
807 } else if(bit
== 0) {
808 frame_append_bit(¤t_frame
, 0);
809 } else if(bit
== 1) {
810 frame_append_bit(¤t_frame
, 1);
814 void LegicRfSimulate(int phase
, int frame
, int reqresp
)
816 /* ADC path high-frequency peak detector, FPGA in high-frequency simulator mode,
817 * modulation mode set to 212kHz subcarrier. We are getting the incoming raw
818 * envelope waveform on DIN and should send our response on DOUT.
820 * The LEGIC RF protocol is pulse-pause-encoding from reader to card, so we'll
821 * measure the time between two rising edges on DIN, and no encoding on the
822 * subcarrier from card to reader, so we'll just shift out our verbatim data
823 * on DOUT, 1 bit is 100us. The time from reader to card frame is still unclear,
824 * seems to be 300us-ish.
829 for(i
=0; i
<=reqresp
; i
++) {
830 legic_prng_init(SESSION_IV
);
831 Dbprintf("i=%u, key 0x%3.3x", i
, get_key_stream(i
, frame
));
836 legic_phase_drift
= phase
;
837 legic_frame_drift
= frame
;
838 legic_reqresp_drift
= reqresp
;
840 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
841 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
843 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
| FPGA_HF_SIMULATOR_MODULATE_212K
);
845 /* Bitbang the receiver */
846 AT91C_BASE_PIOA
->PIO_ODR
= GPIO_SSC_DIN
;
847 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DIN
;
850 crc_init(&legic_crc
, 4, 0x19 >> 1, 0x5, 0);
854 legic_state
= STATE_DISCON
;
857 DbpString("Starting Legic emulator, press button to end");
858 while(!BUTTON_PRESS() && !usb_poll_validate_length()) {
859 int level
= !!(AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_DIN
);
860 int time
= timer
->TC_CV
;
862 if(level
!= old_level
) {
864 timer
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
865 if(FUZZ_EQUAL(time
, RWD_TIME_1
, RWD_TIME_FUZZ
)) {
870 } else if(FUZZ_EQUAL(time
, RWD_TIME_0
, RWD_TIME_FUZZ
)) {
884 if(time
>= (RWD_TIME_1
+RWD_TIME_FUZZ
) && active
) {
891 if(time
>= (20*RWD_TIME_1
) && (timer
->TC_SR
& AT91C_TC_CLKSTA
)) {
892 timer
->TC_CCR
= AT91C_TC_CLKDIS
;
898 DbpString("Stopped");
905 //-----------------------------------------------------------------------------
906 //-----------------------------------------------------------------------------
909 //-----------------------------------------------------------------------------
910 // Code up a string of octets at layer 2 (including CRC, we don't generate
911 // that here) so that they can be transmitted to the reader. Doesn't transmit
912 // them yet, just leaves them ready to send in ToSend[].
913 //-----------------------------------------------------------------------------
914 // static void CodeLegicAsTag(const uint8_t *cmd, int len)
920 // // Transmit a burst of ones, as the initial thing that lets the
921 // // reader get phase sync. This (TR1) must be > 80/fs, per spec,
922 // // but tag that I've tried (a Paypass) exceeds that by a fair bit,
924 // for(i = 0; i < 20; i++) {
925 // ToSendStuffBit(1);
926 // ToSendStuffBit(1);
927 // ToSendStuffBit(1);
928 // ToSendStuffBit(1);
932 // for(i = 0; i < 10; i++) {
933 // ToSendStuffBit(0);
934 // ToSendStuffBit(0);
935 // ToSendStuffBit(0);
936 // ToSendStuffBit(0);
938 // for(i = 0; i < 2; i++) {
939 // ToSendStuffBit(1);
940 // ToSendStuffBit(1);
941 // ToSendStuffBit(1);
942 // ToSendStuffBit(1);
945 // for(i = 0; i < len; i++) {
947 // uint8_t b = cmd[i];
950 // ToSendStuffBit(0);
951 // ToSendStuffBit(0);
952 // ToSendStuffBit(0);
953 // ToSendStuffBit(0);
956 // for(j = 0; j < 8; j++) {
958 // ToSendStuffBit(1);
959 // ToSendStuffBit(1);
960 // ToSendStuffBit(1);
961 // ToSendStuffBit(1);
963 // ToSendStuffBit(0);
964 // ToSendStuffBit(0);
965 // ToSendStuffBit(0);
966 // ToSendStuffBit(0);
972 // ToSendStuffBit(1);
973 // ToSendStuffBit(1);
974 // ToSendStuffBit(1);
975 // ToSendStuffBit(1);
979 // for(i = 0; i < 10; i++) {
980 // ToSendStuffBit(0);
981 // ToSendStuffBit(0);
982 // ToSendStuffBit(0);
983 // ToSendStuffBit(0);
985 // for(i = 0; i < 2; i++) {
986 // ToSendStuffBit(1);
987 // ToSendStuffBit(1);
988 // ToSendStuffBit(1);
989 // ToSendStuffBit(1);
992 // // Convert from last byte pos to length
996 //-----------------------------------------------------------------------------
997 // The software UART that receives commands from the reader, and its state
999 //-----------------------------------------------------------------------------
1003 STATE_GOT_FALLING_EDGE_OF_SOF
,
1004 STATE_AWAITING_START_BIT
,
1005 STATE_RECEIVING_DATA
1015 /* Receive & handle a bit coming from the reader.
1017 * This function is called 4 times per bit (every 2 subcarrier cycles).
1018 * Subcarrier frequency fs is 212kHz, 1/fs = 4,72us, i.e. function is called every 9,44us
1021 * LED A -> ON once we have received the SOF and are expecting the rest.
1022 * LED A -> OFF once we have received EOF or are in error state or unsynced
1024 * Returns: true if we received a EOF
1025 * false if we are still waiting for some more
1027 // static RAMFUNC int HandleLegicUartBit(uint8_t bit)
1029 // switch(Uart.state) {
1030 // case STATE_UNSYNCD:
1032 // // we went low, so this could be the beginning of an SOF
1033 // Uart.state = STATE_GOT_FALLING_EDGE_OF_SOF;
1039 // case STATE_GOT_FALLING_EDGE_OF_SOF:
1041 // if(Uart.posCnt == 2) { // sample every 4 1/fs in the middle of a bit
1043 // if(Uart.bitCnt > 9) {
1044 // // we've seen enough consecutive
1045 // // zeros that it's a valid SOF
1047 // Uart.byteCnt = 0;
1048 // Uart.state = STATE_AWAITING_START_BIT;
1049 // LED_A_ON(); // Indicate we got a valid SOF
1051 // // didn't stay down long enough
1052 // // before going high, error
1053 // Uart.state = STATE_UNSYNCD;
1056 // // do nothing, keep waiting
1060 // if(Uart.posCnt >= 4) Uart.posCnt = 0;
1061 // if(Uart.bitCnt > 12) {
1062 // // Give up if we see too many zeros without
1065 // Uart.state = STATE_UNSYNCD;
1069 // case STATE_AWAITING_START_BIT:
1072 // if(Uart.posCnt > 50/2) { // max 57us between characters = 49 1/fs, max 3 etus after low phase of SOF = 24 1/fs
1073 // // stayed high for too long between
1074 // // characters, error
1075 // Uart.state = STATE_UNSYNCD;
1078 // // falling edge, this starts the data byte
1081 // Uart.shiftReg = 0;
1082 // Uart.state = STATE_RECEIVING_DATA;
1086 // case STATE_RECEIVING_DATA:
1088 // if(Uart.posCnt == 2) {
1089 // // time to sample a bit
1090 // Uart.shiftReg >>= 1;
1092 // Uart.shiftReg |= 0x200;
1096 // if(Uart.posCnt >= 4) {
1099 // if(Uart.bitCnt == 10) {
1100 // if((Uart.shiftReg & 0x200) && !(Uart.shiftReg & 0x001))
1102 // // this is a data byte, with correct
1103 // // start and stop bits
1104 // Uart.output[Uart.byteCnt] = (Uart.shiftReg >> 1) & 0xff;
1107 // if(Uart.byteCnt >= Uart.byteCntMax) {
1108 // // Buffer overflowed, give up
1110 // Uart.state = STATE_UNSYNCD;
1112 // // so get the next byte now
1114 // Uart.state = STATE_AWAITING_START_BIT;
1116 // } else if (Uart.shiftReg == 0x000) {
1117 // // this is an EOF byte
1118 // LED_A_OFF(); // Finished receiving
1119 // Uart.state = STATE_UNSYNCD;
1120 // if (Uart.byteCnt != 0) {
1124 // // this is an error
1126 // Uart.state = STATE_UNSYNCD;
1133 // Uart.state = STATE_UNSYNCD;
1141 static void UartReset()
1143 Uart
.byteCntMax
= MAX_FRAME_SIZE
;
1144 Uart
.state
= STATE_UNSYNCD
;
1148 memset(Uart
.output
, 0x00, MAX_FRAME_SIZE
);
1151 // static void UartInit(uint8_t *data)
1153 // Uart.output = data;
1157 //=============================================================================
1158 // An LEGIC reader. We take layer two commands, code them
1159 // appropriately, and then send them to the tag. We then listen for the
1160 // tag's response, which we leave in the buffer to be demodulated on the
1162 //=============================================================================
1167 DEMOD_PHASE_REF_TRAINING
,
1168 DEMOD_AWAITING_FALLING_EDGE_OF_SOF
,
1169 DEMOD_GOT_FALLING_EDGE_OF_SOF
,
1170 DEMOD_AWAITING_START_BIT
,
1171 DEMOD_RECEIVING_DATA
1184 * Handles reception of a bit from the tag
1186 * This function is called 2 times per bit (every 4 subcarrier cycles).
1187 * Subcarrier frequency fs is 212kHz, 1/fs = 4,72us, i.e. function is called every 9,44us
1190 * LED C -> ON once we have received the SOF and are expecting the rest.
1191 * LED C -> OFF once we have received EOF or are unsynced
1193 * Returns: true if we received a EOF
1194 * false if we are still waiting for some more
1198 #ifndef SUBCARRIER_DETECT_THRESHOLD
1199 # define SUBCARRIER_DETECT_THRESHOLD 8
1202 // Subcarrier amplitude v = sqrt(ci^2 + cq^2), approximated here by max(abs(ci),abs(cq)) + 1/2*min(abs(ci),abs(cq)))
1203 #ifndef CHECK_FOR_SUBCARRIER
1204 # define CHECK_FOR_SUBCARRIER() { v = MAX(ai, aq) + MIN(halfci, halfcq); }
1207 // The soft decision on the bit uses an estimate of just the
1208 // quadrant of the reference angle, not the exact angle.
1209 // Subcarrier amplitude v = sqrt(ci^2 + cq^2), approximated here by max(abs(ci),abs(cq)) + 1/2*min(abs(ci),abs(cq)))
1210 #define MAKE_SOFT_DECISION() { \
1211 if(Demod.sumI > 0) \
1216 if(Demod.sumQ > 0) \
1223 static RAMFUNC
int HandleLegicSamplesDemod(int ci
, int cq
)
1228 int halfci
= (ai
>> 1);
1229 int halfcq
= (aq
>> 1);
1231 switch(Demod
.state
) {
1234 CHECK_FOR_SUBCARRIER()
1236 if(v
> SUBCARRIER_DETECT_THRESHOLD
) { // subcarrier detected
1237 Demod
.state
= DEMOD_PHASE_REF_TRAINING
;
1244 case DEMOD_PHASE_REF_TRAINING
:
1245 if(Demod
.posCount
< 8) {
1247 CHECK_FOR_SUBCARRIER()
1249 if (v
> SUBCARRIER_DETECT_THRESHOLD
) {
1250 // set the reference phase (will code a logic '1') by averaging over 32 1/fs.
1251 // note: synchronization time > 80 1/fs
1257 Demod
.state
= DEMOD_UNSYNCD
;
1260 Demod
.state
= DEMOD_AWAITING_FALLING_EDGE_OF_SOF
;
1264 case DEMOD_AWAITING_FALLING_EDGE_OF_SOF
:
1266 MAKE_SOFT_DECISION()
1268 //Dbprintf("ICE: %d %d %d %d %d", v, Demod.sumI, Demod.sumQ, ci, cq );
1269 // logic '0' detected
1272 Demod
.state
= DEMOD_GOT_FALLING_EDGE_OF_SOF
;
1274 // start of SOF sequence
1277 // maximum length of TR1 = 200 1/fs
1278 if(Demod
.posCount
> 25*2) Demod
.state
= DEMOD_UNSYNCD
;
1283 case DEMOD_GOT_FALLING_EDGE_OF_SOF
:
1286 MAKE_SOFT_DECISION()
1289 // low phase of SOF too short (< 9 etu). Note: spec is >= 10, but FPGA tends to "smear" edges
1290 if(Demod
.posCount
< 10*2) {
1291 Demod
.state
= DEMOD_UNSYNCD
;
1293 LED_C_ON(); // Got SOF
1294 Demod
.state
= DEMOD_AWAITING_START_BIT
;
1299 // low phase of SOF too long (> 12 etu)
1300 if(Demod
.posCount
> 13*2) {
1301 Demod
.state
= DEMOD_UNSYNCD
;
1307 case DEMOD_AWAITING_START_BIT
:
1310 MAKE_SOFT_DECISION()
1313 // max 19us between characters = 16 1/fs, max 3 etu after low phase of SOF = 24 1/fs
1314 if(Demod
.posCount
> 3*2) {
1315 Demod
.state
= DEMOD_UNSYNCD
;
1319 // start bit detected
1321 Demod
.posCount
= 1; // this was the first half
1324 Demod
.state
= DEMOD_RECEIVING_DATA
;
1328 case DEMOD_RECEIVING_DATA
:
1330 MAKE_SOFT_DECISION()
1332 if(Demod
.posCount
== 0) {
1333 // first half of bit
1337 // second half of bit
1339 Demod
.shiftReg
>>= 1;
1341 if(Demod
.thisBit
> 0)
1342 Demod
.shiftReg
|= 0x200;
1346 if(Demod
.bitCount
== 10) {
1348 uint16_t s
= Demod
.shiftReg
;
1350 if((s
& 0x200) && !(s
& 0x001)) {
1351 // stop bit == '1', start bit == '0'
1352 uint8_t b
= (s
>> 1);
1353 Demod
.output
[Demod
.len
] = b
;
1355 Demod
.state
= DEMOD_AWAITING_START_BIT
;
1357 Demod
.state
= DEMOD_UNSYNCD
;
1361 // This is EOF (start, stop and all data bits == '0'
1371 Demod
.state
= DEMOD_UNSYNCD
;
1378 // Clear out the state of the "UART" that receives from the tag.
1379 static void DemodReset() {
1381 Demod
.state
= DEMOD_UNSYNCD
;
1388 memset(Demod
.output
, 0x00, MAX_FRAME_SIZE
);
1391 static void DemodInit(uint8_t *data
) {
1392 Demod
.output
= data
;
1397 * Demodulate the samples we received from the tag, also log to tracebuffer
1398 * quiet: set to 'TRUE' to disable debug output
1400 #define LEGIC_DMA_BUFFER_SIZE 256
1401 static void GetSamplesForLegicDemod(int n
, bool quiet
)
1404 bool gotFrame
= FALSE
;
1405 int lastRxCounter
= LEGIC_DMA_BUFFER_SIZE
;
1406 int ci
, cq
, samples
= 0;
1410 // And put the FPGA in the appropriate mode
1411 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
| FPGA_HF_READER_RX_XCORR_QUARTER_FREQ
);
1413 // The response (tag -> reader) that we're receiving.
1414 // Set up the demodulator for tag -> reader responses.
1415 DemodInit(BigBuf_malloc(MAX_FRAME_SIZE
));
1417 // The DMA buffer, used to stream samples from the FPGA
1418 int8_t *dmaBuf
= (int8_t*) BigBuf_malloc(LEGIC_DMA_BUFFER_SIZE
);
1419 int8_t *upTo
= dmaBuf
;
1421 // Setup and start DMA.
1422 if ( !FpgaSetupSscDma((uint8_t*) dmaBuf
, LEGIC_DMA_BUFFER_SIZE
) ){
1423 if (MF_DBGLEVEL
> 1) Dbprintf("FpgaSetupSscDma failed. Exiting");
1427 // Signal field is ON with the appropriate LED:
1430 int behindBy
= lastRxCounter
- AT91C_BASE_PDC_SSC
->PDC_RCR
;
1431 if(behindBy
> max
) max
= behindBy
;
1433 while(((lastRxCounter
-AT91C_BASE_PDC_SSC
->PDC_RCR
) & (LEGIC_DMA_BUFFER_SIZE
-1)) > 2) {
1437 if(upTo
>= dmaBuf
+ LEGIC_DMA_BUFFER_SIZE
) {
1439 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (uint32_t) upTo
;
1440 AT91C_BASE_PDC_SSC
->PDC_RNCR
= LEGIC_DMA_BUFFER_SIZE
;
1443 if(lastRxCounter
<= 0)
1444 lastRxCounter
= LEGIC_DMA_BUFFER_SIZE
;
1448 gotFrame
= HandleLegicSamplesDemod(ci
, cq
);
1453 if(samples
> n
|| gotFrame
)
1457 FpgaDisableSscDma();
1459 if (!quiet
&& Demod
.len
== 0) {
1460 Dbprintf("max behindby = %d, samples = %d, gotFrame = %d, Demod.len = %d, Demod.sumI = %d, Demod.sumQ = %d",
1471 if (Demod
.len
> 0) {
1472 uint8_t parity
[MAX_PARITY_SIZE
] = {0x00};
1473 LogTrace(Demod
.output
, Demod
.len
, 0, 0, parity
, FALSE
);
1476 //-----------------------------------------------------------------------------
1477 // Transmit the command (to the tag) that was placed in ToSend[].
1478 //-----------------------------------------------------------------------------
1479 static void TransmitForLegic(void)
1485 while(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
))
1486 AT91C_BASE_SSC
->SSC_THR
= 0xff;
1488 // Signal field is ON with the appropriate Red LED
1491 // Signal we are transmitting with the Green LED
1493 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX
| FPGA_HF_READER_TX_SHALLOW_MOD
);
1495 for(c
= 0; c
< 10;) {
1496 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1497 AT91C_BASE_SSC
->SSC_THR
= 0xff;
1500 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1501 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1509 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1510 AT91C_BASE_SSC
->SSC_THR
= ToSend
[c
];
1511 legic_prng_forward(1); // forward the lfsr
1513 if(c
>= ToSendMax
) {
1517 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1518 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1527 //-----------------------------------------------------------------------------
1528 // Code a layer 2 command (string of octets, including CRC) into ToSend[],
1529 // so that it is ready to transmit to the tag using TransmitForLegic().
1530 //-----------------------------------------------------------------------------
1531 static void CodeLegicBitsAsReader(const uint8_t *cmd
, uint8_t cmdlen
, int bits
)
1539 for(i
= 0; i
< 7; i
++)
1543 for(i
= 0; i
< cmdlen
; i
++) {
1549 for(j
= 0; j
< bits
; j
++) {
1559 // Convert from last character reference to length
1564 Convenience function to encode, transmit and trace Legic comms
1566 static void CodeAndTransmitLegicAsReader(const uint8_t *cmd
, uint8_t cmdlen
, int bits
)
1568 CodeLegicBitsAsReader(cmd
, cmdlen
, bits
);
1571 uint8_t parity
[1] = {0x00};
1572 LogTrace(cmd
, cmdlen
, 0, 0, parity
, TRUE
);
1576 int ice_legic_select_card()
1578 //int cmd_size=0, card_size=0;
1579 uint8_t wakeup
[] = { 0x7F };
1580 uint8_t getid
[] = {0x19};
1582 legic_prng_init(SESSION_IV
);
1584 // first, wake up the tag, 7bits
1585 CodeAndTransmitLegicAsReader(wakeup
, sizeof(wakeup
), 7);
1587 GetSamplesForLegicDemod(1000, TRUE
);
1589 // frame_clean(¤t_frame);
1590 //frame_receive_rwd(¤t_frame, 6, 1);
1592 legic_prng_forward(1); /* we wait anyways */
1594 //while(timer->TC_CV < 387) ; /* ~ 258us */
1595 //frame_send_rwd(0x19, 6);
1596 CodeAndTransmitLegicAsReader(getid
, sizeof(getid
), 8);
1597 GetSamplesForLegicDemod(1000, TRUE
);
1599 //if (Demod.len < 14) return 2;
1600 Dbprintf("CARD TYPE: %02x LEN: %d", Demod
.output
[0], Demod
.len
);
1602 switch(Demod
.output
[0]) {
1604 DbpString("MIM 256 card found");
1609 DbpString("MIM 1024 card found");
1611 // card_size = 1024;
1618 // bytes = card_size;
1620 // if(bytes + offset >= card_size)
1621 // bytes = card_size - offset;
1623 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1628 // Set up LEGIC communication
1629 void ice_legic_setup() {
1632 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1633 BigBuf_free(); BigBuf_Clear_ext(false);
1639 // Set up the synchronous serial port
1642 // connect Demodulated Signal to ADC:
1643 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1645 // Signal field is on with the appropriate LED
1647 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX
| FPGA_HF_READER_TX_SHALLOW_MOD
);
1650 //StartCountSspClk();
1653 crc_init(&legic_crc
, 4, 0x19 >> 1, 0x5, 0);