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 //-----------------------------------------------------------------------------
13 static struct legic_frame
{
24 static crc_t legic_crc
;
25 static int legic_read_count
;
26 static uint32_t legic_prng_bc
;
27 static uint32_t legic_prng_iv
;
29 static int legic_phase_drift
;
30 static int legic_frame_drift
;
31 static int legic_reqresp_drift
;
38 static void setup_timer(void) {
39 /* Set up Timer 1 to use for measuring time between pulses. Since we're bit-banging
40 * this it won't be terribly accurate but should be good enough.
42 AT91C_BASE_PMC
->PMC_PCER
= (1 << AT91C_ID_TC1
);
43 timer
= AT91C_BASE_TC1
;
44 timer
->TC_CCR
= AT91C_TC_CLKDIS
;
45 timer
->TC_CMR
= AT91C_TC_CLKS_TIMER_DIV3_CLOCK
;
46 timer
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
49 * Set up Timer 2 to use for measuring time between frames in
50 * tag simulation mode. Runs 4x faster as Timer 1
52 AT91C_BASE_PMC
->PMC_PCER
= (1 << AT91C_ID_TC2
);
53 prng_timer
= AT91C_BASE_TC2
;
54 prng_timer
->TC_CCR
= AT91C_TC_CLKDIS
;
55 prng_timer
->TC_CMR
= AT91C_TC_CLKS_TIMER_DIV2_CLOCK
;
56 prng_timer
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
59 /* At TIMER_CLOCK3 (MCK/32) */
60 #define RWD_TIME_1 150 /* RWD_TIME_PAUSE off, 80us on = 100us */
61 #define RWD_TIME_0 90 /* RWD_TIME_PAUSE off, 40us on = 60us */
62 #define RWD_TIME_PAUSE 30 /* 20us */
63 #define RWD_TIME_FUZZ 20 /* rather generous 13us, since the peak detector + hysteresis fuzz quite a bit */
64 #define TAG_TIME_BIT 150 /* 100us for every bit */
65 #define TAG_TIME_WAIT 490 /* 490 time from RWD frame end to tag frame start, experimentally determined */
67 #define SIM_DIVISOR 586 /* prng_time/SIM_DIVISOR count prng needs to be forwared */
68 #define SIM_SHIFT 900 /* prng_time+SIM_SHIFT shift of delayed start */
70 #define SESSION_IV 0x55
71 #define OFFSET_LOG 1024
73 #define FUZZ_EQUAL(value, target, fuzz) ((value) > ((target)-(fuzz)) && (value) < ((target)+(fuzz)))
75 // ~ 258us + 100us*delay
76 #define WAIT_387 WAIT(387)
77 #define WAIT(delay) while(timer->TC_CV < (delay) );
80 // ToDo: define a meaningful maximum size for auth_table. The bigger this is, the lower will be the available memory for traces.
81 // Historically it used to be FREE_BUFFER_SIZE, which was 2744.
82 #define LEGIC_CARD_MEMSIZE 1024
83 static uint8_t* cardmem
;
88 // 32 bits timestamp (little endian)
89 // 16 bits duration (little endian)
90 // 16 bits data length (little endian, Highest Bit used as readerToTag flag)
92 // x Bytes parity (one byte per 8 bytes data)
95 /* Generate Keystream */
96 static uint32_t get_key_stream(int skip
, int count
)
101 // Use int to enlarge timer tc to 32bit
102 legic_prng_bc
+= prng_timer
->TC_CV
;
104 // reset the prng timer.
105 prng_timer
->TC_CCR
= AT91C_TC_SWTRG
;
106 while(prng_timer
->TC_CV
> 1) ;
108 /* If skip == -1, forward prng time based */
110 i
= (legic_prng_bc
+ SIM_SHIFT
)/SIM_DIVISOR
; /* Calculate Cycles based on timer */
111 i
-= legic_prng_count(); /* substract cycles of finished frames */
112 i
-= count
; /* substract current frame length, rewind to beginning */
113 legic_prng_forward(i
);
115 legic_prng_forward(skip
);
118 i
= (count
== 6) ? -1 : legic_read_count
;
120 /* Write Time Data into LOG */
121 // uint8_t *BigBuf = BigBuf_get_addr();
122 // BigBuf[OFFSET_LOG+128+i] = legic_prng_count();
123 // BigBuf[OFFSET_LOG+256+i*4] = (legic_prng_bc >> 0) & 0xff;
124 // BigBuf[OFFSET_LOG+256+i*4+1] = (legic_prng_bc >> 8) & 0xff;
125 // BigBuf[OFFSET_LOG+256+i*4+2] = (legic_prng_bc >>16) & 0xff;
126 // BigBuf[OFFSET_LOG+256+i*4+3] = (legic_prng_bc >>24) & 0xff;
127 // BigBuf[OFFSET_LOG+384+i] = count;
129 /* Generate KeyStream */
130 for(i
=0; i
<count
; i
++) {
131 key
|= legic_prng_get_bit() << i
;
132 legic_prng_forward(1);
137 /* Send a frame in tag mode, the FPGA must have been set up by
140 static void frame_send_tag(uint16_t response
, int bits
, int crypt
)
142 /* Bitbang the response */
143 AT91C_BASE_PIOA
->PIO_CODR
= GPIO_SSC_DOUT
;
144 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
145 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
147 /* Use time to crypt frame */
149 legic_prng_forward(2); /* TAG_TIME_WAIT -> shift by 2 */
151 for(int i
= 0; i
< bits
; i
++) {
152 key
|= legic_prng_get_bit() << i
;
153 legic_prng_forward(1);
155 response
= response
^ key
;
158 /* Wait for the frame start */
159 //while(timer->TC_CV < (TAG_TIME_WAIT - 30)) ;
160 WAIT( TAG_TIME_WAIT
- 30)
163 for(int i
= 0; i
< bits
; i
++) {
164 int nextbit
= timer
->TC_CV
+ TAG_TIME_BIT
;
169 AT91C_BASE_PIOA
->PIO_SODR
= GPIO_SSC_DOUT
;
171 AT91C_BASE_PIOA
->PIO_CODR
= GPIO_SSC_DOUT
;
173 //while(timer->TC_CV < nextbit) ;
176 AT91C_BASE_PIOA
->PIO_CODR
= GPIO_SSC_DOUT
;
179 // Starts Clock and waits until its reset
180 static void ResetClock(void){
181 timer
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
182 while(timer
->TC_CV
> 1) ;
185 /* Send a frame in reader mode, the FPGA must have been set up by
188 static void frame_send_rwd(uint32_t data
, uint8_t bits
){
191 uint32_t starttime
= 0, pause_end
= 0, bit_end
= 0, temp
= data
;
194 for(int i
= 0; i
< bits
; i
++) {
196 starttime
= timer
->TC_CV
;
197 pause_end
= starttime
+ RWD_TIME_PAUSE
;
201 if(bit
^ legic_prng_get_bit())
202 bit_end
= starttime
+ RWD_TIME_1
;
204 bit_end
= starttime
+ RWD_TIME_0
;
206 /* RWD_TIME_PAUSE time off, then some time on, so that the complete bit time is
207 * RWD_TIME_x, where x is the bit to be transmitted */
208 AT91C_BASE_PIOA
->PIO_CODR
= GPIO_SSC_DOUT
;
212 AT91C_BASE_PIOA
->PIO_SODR
= GPIO_SSC_DOUT
;
214 // bit duration is longest. use this time to forward the lfsr
215 legic_prng_forward(1);
220 // One final pause to mark the end of the frame
221 pause_end
= timer
->TC_CV
+ RWD_TIME_PAUSE
;
223 AT91C_BASE_PIOA
->PIO_CODR
= GPIO_SSC_DOUT
;
227 AT91C_BASE_PIOA
->PIO_SODR
= GPIO_SSC_DOUT
;
230 uint8_t cmdbytes
[2] = { (data
& 0xFF), 0 };
232 cmdbytes
[1] = (data
>> 8 ) & 0xFF;
233 LogTrace(cmdbytes
, 2, 0, timer
->TC_CV
, NULL
, TRUE
);
235 LogTrace(cmdbytes
, 1, 0, timer
->TC_CV
, NULL
, TRUE
);
237 /* Reset the timer, to measure time until the start of the tag frame */
241 /* Receive a frame from the card in reader emulation mode, the FPGA and
242 * timer must have been set up by LegicRfReader and frame_send_rwd.
244 * The LEGIC RF protocol from card to reader does not include explicit
245 * frame start/stop information or length information. The reader must
246 * know beforehand how many bits it wants to receive. (Notably: a card
247 * sending a stream of 0-bits is indistinguishable from no card present.)
249 * Receive methodology: There is a fancy correlator in hi_read_rx_xcorr, but
250 * I'm not smart enough to use it. Instead I have patched hi_read_tx to output
251 * the ADC signal with hysteresis on SSP_DIN. Bit-bang that signal and look
252 * for edges. Count the edges in each bit interval. If they are approximately
253 * 0 this was a 0-bit, if they are approximately equal to the number of edges
254 * expected for a 212kHz subcarrier, this was a 1-bit. For timing we use the
255 * timer that's still running from frame_send_rwd in order to get a synchronization
256 * with the frame that we just sent.
258 * FIXME: Because we're relying on the hysteresis to just do the right thing
259 * the range is severely reduced (and you'll probably also need a good antenna).
260 * So this should be fixed some time in the future for a proper receiver.
262 static void frame_receive_rwd(struct legic_frame
* const f
, int bits
, int crypt
)
264 uint32_t starttime
= timer
->TC_CV
;
266 uint32_t the_bit
= 1;
267 uint32_t data
= 0;/* Use a bitmask to save on shifts */
268 int i
, old_level
= 0, edges
= 0;
269 int next_bit_at
= TAG_TIME_WAIT
;
272 if(bits
> 32) bits
= 32;
274 AT91C_BASE_PIOA
->PIO_ODR
= GPIO_SSC_DIN
;
275 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DIN
;
277 /* we have some time now, precompute the cipher
278 * since we cannot compute it on the fly while reading */
279 legic_prng_forward(2);
282 for(i
=0; i
<bits
; i
++) {
283 data
|= legic_prng_get_bit() << i
;
284 legic_prng_forward(1);
288 // QUESTION: how long did those extra calls to logtrace take?
291 next_bit_at
+= TAG_TIME_BIT
;
293 for(i
=0; i
<bits
; i
++) {
295 while(timer
->TC_CV
< next_bit_at
) {
296 level
= (AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_DIN
);
297 if(level
!= old_level
)
301 next_bit_at
+= TAG_TIME_BIT
;
303 // We expect 42 edges
304 if(edges
> 20 && edges
< 60) {
314 uint8_t cmdbytes
[] = { (data
& 0xFF), (data
>> 8) & 0xFF };
315 LogTrace(cmdbytes
, 2, starttime
, timer
->TC_CV
, NULL
, FALSE
);
317 // Reset the timer, to synchronize the next frame
321 static void frame_append_bit(struct legic_frame
* const f
, int bit
) {
322 // Overflow, won't happen
323 if (f
->bits
>= 31) return;
325 f
->data
|= (bit
<< f
->bits
);
329 static void frame_clean(struct legic_frame
* const f
) {
334 // Setup pm3 as a Legic Reader
335 static uint32_t perform_setup_phase_rwd(uint8_t iv
) {
337 // Switch on carrier and let the tag charge for 1ms
338 AT91C_BASE_PIOA
->PIO_SODR
= GPIO_SSC_DOUT
;
339 SpinDelay(20); // was 1ms before.
344 frame_send_rwd(iv
, 7);
348 frame_clean(¤t_frame
);
350 frame_receive_rwd(¤t_frame
, 6, 1);
353 legic_prng_forward(3);
357 // Send obsfuscated acknowledgment frame.
359 // 0x39 = MIM256, MIM1024
360 if ( current_frame
.data
== 0x0D ){
361 frame_send_rwd(0x19, 6);
363 frame_send_rwd(0x39, 6);
366 return current_frame
.data
;
369 static void LegicCommonInit(void) {
371 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
372 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
374 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX
);
376 /* Bitbang the transmitter */
377 AT91C_BASE_PIOA
->PIO_CODR
= GPIO_SSC_DOUT
;
378 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
379 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
381 // reserve a cardmem, meaning we can use the tracelog function in bigbuff easier.
382 cardmem
= BigBuf_malloc(LEGIC_CARD_MEMSIZE
);
383 memset(cardmem
, 0x00, LEGIC_CARD_MEMSIZE
);
390 crc_init(&legic_crc
, 4, 0x19 >> 1, 0x5, 0);
393 /* Switch off carrier, make sure tag is reset */
394 static void switch_off_tag_rwd(void) {
395 AT91C_BASE_PIOA
->PIO_CODR
= GPIO_SSC_DOUT
;
400 // calculate crc4 for a legic READ command
401 // 5,8,10 address size.
402 static int LegicCRC(uint16_t byte_index
, uint8_t value
, uint8_t cmd_sz
) {
403 crc_clear(&legic_crc
);
404 crc_update(&legic_crc
, LEGIC_READ
, 1);
405 crc_update(&legic_crc
, byte_index
, cmd_sz
-1);
406 crc_update(&legic_crc
, value
, 8);
407 return crc_finish(&legic_crc
);
410 #define LEGIC_READ 0x01
411 #define LEGIC_WRITE 0x00
413 int legic_read_byte(int byte_index
, int cmd_sz
) {
415 int calcCrc
= 0, crc
= 0;
417 uint32_t cmd
= (byte_index
<< 1) | LEGIC_READ
;
422 frame_send_rwd(cmd
, cmd_sz
);
424 frame_clean(¤t_frame
);
427 frame_receive_rwd(¤t_frame
, 12, 1);
429 byte
= current_frame
.data
& 0xff;
430 calcCrc
= LegicCRC(byte_index
, byte
, cmd_sz
);
431 crc
= (current_frame
.data
>> 8);
433 if( calcCrc
!= crc
) {
434 Dbprintf("!!! crc mismatch: expected %x but got %x !!!", calcCrc
, crc
);
439 legic_prng_forward(4);
444 * - assemble a write_cmd_frame with crc and send it
445 * - wait until the tag sends back an ACK ('1' bit unencrypted)
446 * - forward the prng based on the timing
448 //int legic_write_byte(int byte, int addr, int addr_sz, int PrngCorrection) {
449 int legic_write_byte(int byte
, int addr
, int addr_sz
) {
451 //do not write UID, CRC at offset 0-4.
452 if(addr
<= 0x04) return 0;
455 crc_clear(&legic_crc
);
456 crc_update(&legic_crc
, 0, 1); /* CMD_WRITE */
457 crc_update(&legic_crc
, addr
, addr_sz
);
458 crc_update(&legic_crc
, byte
, 8);
459 uint32_t crc
= crc_finish(&legic_crc
);
461 // send write command
462 uint32_t cmd
= ((crc
<<(addr_sz
+1+8)) //CRC
463 |(byte
<<(addr_sz
+1)) //Data
464 |(addr
<<1) //Address
465 |(0x00 <<0)); //CMD = W
466 uint32_t cmd_sz
= addr_sz
+1+8+4; //crc+data+cmd
468 legic_prng_forward(2); /* we wait anyways */
470 while(timer
->TC_CV
< 387) ; /* ~ 258us */
472 frame_send_rwd(cmd
, cmd_sz
);
474 // wllm-rbnt doesnt have these
475 // AT91C_BASE_PIOA->PIO_ODR = GPIO_SSC_DIN;
476 // AT91C_BASE_PIOA->PIO_PER = GPIO_SSC_DIN;
479 int t
, old_level
= 0, edges
= 0;
482 while(timer
->TC_CV
< 387) ; /* ~ 258us */
484 for( t
= 0; t
< 80; t
++) {
486 next_bit_at
+= TAG_TIME_BIT
;
487 while(timer
->TC_CV
< next_bit_at
) {
488 int level
= (AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_DIN
);
489 if(level
!= old_level
) {
494 if(edges
> 20 && edges
< 60) { /* expected are 42 edges */
495 int t
= timer
->TC_CV
;
496 int c
= t
/ TAG_TIME_BIT
;
499 legic_prng_forward(c
);
508 int LegicRfReader(int offset
, int bytes
, int iv
) {
510 // ice_legic_setup();
511 // ice_legic_select_card();
513 int byte_index
= 0, cmd_sz
= 0, card_sz
= 0;
517 if ( MF_DBGLEVEL
>= 2) DbpString("setting up legic card");
519 uint32_t tag_type
= perform_setup_phase_rwd(iv
);
521 //we lose to mutch time with dprintf
522 switch_off_tag_rwd();
526 if ( MF_DBGLEVEL
>= 2) DbpString("MIM22 card found, reading card ...");
531 if ( MF_DBGLEVEL
>= 2) DbpString("MIM256 card found, reading card ...");
536 if ( MF_DBGLEVEL
>= 2) DbpString("MIM1024 card found, reading card ...");
541 if ( MF_DBGLEVEL
>= 1) Dbprintf("Unknown card format: %x",tag_type
);
547 if(bytes
+offset
>= card_sz
)
548 bytes
= card_sz
- offset
;
550 perform_setup_phase_rwd(iv
);
552 legic_prng_forward(2);
555 while(byte_index
< bytes
) {
556 int r
= legic_read_byte(byte_index
+offset
, cmd_sz
);
557 if(r
== -1 || BUTTON_PRESS()) {
558 switch_off_tag_rwd();
560 if ( MF_DBGLEVEL
>= 2) DbpString("operation aborted");
563 cardmem
[byte_index
] = r
;
568 switch_off_tag_rwd();
571 if ( MF_DBGLEVEL
>= 1) Dbprintf("Card read, use 'hf legic decode' or");
572 if ( MF_DBGLEVEL
>= 1) Dbprintf("'data hexsamples %d' to view results", (bytes
+7) & ~7);
576 /*int _LegicRfWriter(int offset, int bytes, int addr_sz, uint8_t *BigBuf, int RoundBruteforceValue) {
580 perform_setup_phase_rwd(SESSION_IV);
581 //legic_prng_forward(2);
582 while(byte_index < bytes) {
585 //check if the DCF should be changed
586 if ( (offset == 0x05) && (bytes == 0x02) ) {
587 //write DCF in reverse order (addr 0x06 before 0x05)
588 r = legic_write_byte(BigBuf[(0x06-byte_index)], (0x06-byte_index), addr_sz, RoundBruteforceValue);
589 //legic_prng_forward(1);
592 r = legic_write_byte(BigBuf[(0x06-byte_index)], (0x06-byte_index), addr_sz, RoundBruteforceValue);
594 //legic_prng_forward(1);
597 r = legic_write_byte(BigBuf[byte_index+offset], byte_index+offset, addr_sz, RoundBruteforceValue);
599 if((r != 0) || BUTTON_PRESS()) {
600 Dbprintf("operation aborted @ 0x%03.3x", byte_index);
601 switch_off_tag_rwd();
609 if(byte_index & 0x10) LED_C_ON(); else LED_C_OFF();
613 DbpString("write successful");
617 void LegicRfWriter(int offset
, int bytes
, int iv
) {
619 int byte_index
= 0, addr_sz
= 0;
621 iv
= (iv
<=0 ) ? SESSION_IV
: iv
;
625 if ( MF_DBGLEVEL
>= 2) DbpString("setting up legic card");
627 uint32_t tag_type
= perform_setup_phase_rwd(iv
);
629 switch_off_tag_rwd();
633 if(offset
+bytes
> 22) {
634 Dbprintf("Error: can not write to 0x%03.3x on MIM22", offset
+bytes
);
638 if ( MF_DBGLEVEL
>= 2) Dbprintf("MIM22 card found, writing 0x%02.2x - 0x%02.2x ...", offset
, offset
+bytes
);
641 if(offset
+bytes
> 0x100) {
642 Dbprintf("Error: can not write to 0x%03.3x on MIM256", offset
+bytes
);
646 if ( MF_DBGLEVEL
>= 2) Dbprintf("MIM256 card found, writing 0x%02.2x - 0x%02.2x ...", offset
, offset
+bytes
);
649 if(offset
+bytes
> 0x400) {
650 Dbprintf("Error: can not write to 0x%03.3x on MIM1024", offset
+bytes
);
654 if ( MF_DBGLEVEL
>= 2) Dbprintf("MIM1024 card found, writing 0x%03.3x - 0x%03.3x ...", offset
, offset
+bytes
);
657 Dbprintf("No or unknown card found, aborting");
662 perform_setup_phase_rwd(iv
);
663 while(byte_index
< bytes
) {
666 //check if the DCF should be changed
667 if ( ((byte_index
+offset
) == 0x05) && (bytes
>= 0x02) ) {
668 //write DCF in reverse order (addr 0x06 before 0x05)
669 r
= legic_write_byte(cardmem
[(0x06-byte_index
)], (0x06-byte_index
), addr_sz
);
671 // write second byte on success...
674 r
= legic_write_byte(cardmem
[(0x06-byte_index
)], (0x06-byte_index
), addr_sz
);
678 r
= legic_write_byte(cardmem
[byte_index
+offset
], byte_index
+offset
, addr_sz
);
681 if((r
!= 0) || BUTTON_PRESS()) {
682 Dbprintf("operation aborted @ 0x%03.3x", byte_index
);
683 switch_off_tag_rwd();
692 if ( MF_DBGLEVEL
>= 1) DbpString("write successful");
695 void LegicRfRawWriter(int address
, int byte
, int iv
) {
697 int byte_index
= 0, addr_sz
= 0;
699 iv
= (iv
<= 0) ? SESSION_IV
: iv
;
703 if ( MF_DBGLEVEL
>= 2) DbpString("setting up legic card");
705 uint32_t tag_type
= perform_setup_phase_rwd(iv
);
707 switch_off_tag_rwd();
712 Dbprintf("Error: can not write to 0x%03.3x on MIM22", address
);
716 if ( MF_DBGLEVEL
>= 2) Dbprintf("MIM22 card found, writing at addr 0x%02.2x - value 0x%02.2x ...", address
, byte
);
719 if(address
> 0x100) {
720 Dbprintf("Error: can not write to 0x%03.3x on MIM256", address
);
724 if ( MF_DBGLEVEL
>= 2) Dbprintf("MIM256 card found, writing at addr 0x%02.2x - value 0x%02.2x ...", address
, byte
);
727 if(address
> 0x400) {
728 Dbprintf("Error: can not write to 0x%03.3x on MIM1024", address
);
732 if ( MF_DBGLEVEL
>= 2) Dbprintf("MIM1024 card found, writing at addr 0x%03.3x - value 0x%03.3x ...", address
, byte
);
735 Dbprintf("No or unknown card found, aborting");
739 Dbprintf("integer value: %d address: %d addr_sz: %d", byte
, address
, addr_sz
);
742 perform_setup_phase_rwd(iv
);
743 //legic_prng_forward(2);
745 int r
= legic_write_byte(byte
, address
, addr_sz
);
747 if((r
!= 0) || BUTTON_PRESS()) {
748 Dbprintf("operation aborted @ 0x%03.3x (%1d)", byte_index
, r
);
749 switch_off_tag_rwd();
755 if ( MF_DBGLEVEL
>= 1) DbpString("write successful");
758 /* Handle (whether to respond) a frame in tag mode
759 * Only called when simulating a tag.
761 static void frame_handle_tag(struct legic_frame
const * const f
)
763 uint8_t *BigBuf
= BigBuf_get_addr();
765 /* First Part of Handshake (IV) */
770 prng_timer
->TC_CCR
= AT91C_TC_SWTRG
;
771 while(prng_timer
->TC_CV
> 1) ;
773 legic_prng_init(f
->data
);
774 frame_send_tag(0x3d, 6, 1); /* 0x3d^0x26 = 0x1b */
775 legic_state
= STATE_IV
;
776 legic_read_count
= 0;
778 legic_prng_iv
= f
->data
;
783 //while(timer->TC_CV < 280);
789 if(legic_state
== STATE_IV
) {
790 int local_key
= get_key_stream(3, 6);
791 int xored
= 0x39 ^ local_key
;
792 if((f
->bits
== 6) && (f
->data
== xored
)) {
793 legic_state
= STATE_CON
;
798 //while(timer->TC_CV < 200);
803 legic_state
= STATE_DISCON
;
805 Dbprintf("iv: %02x frame: %02x key: %02x xored: %02x", legic_prng_iv
, f
->data
, local_key
, xored
);
812 if(legic_state
== STATE_CON
) {
813 int key
= get_key_stream(2, 11); //legic_phase_drift, 11);
814 int addr
= f
->data
^ key
; addr
= addr
>> 1;
815 int data
= BigBuf
[addr
];
816 int hash
= LegicCRC(addr
, data
, 11) << 8;
817 BigBuf
[OFFSET_LOG
+legic_read_count
] = (uint8_t)addr
;
820 //Dbprintf("Data:%03.3x, key:%03.3x, addr: %03.3x, read_c:%u", f->data, key, addr, read_c);
821 legic_prng_forward(legic_reqresp_drift
);
823 frame_send_tag(hash
| data
, 12, 1);
828 legic_prng_forward(2);
829 //while(timer->TC_CV < 180);
838 int key
= get_key_stream(-1, 23); //legic_frame_drift, 23);
839 int addr
= f
->data
^ key
; addr
= addr
>> 1; addr
= addr
& 0x3ff;
840 int data
= f
->data
^ key
; data
= data
>> 11; data
= data
& 0xff;
843 legic_state
= STATE_DISCON
;
845 Dbprintf("write - addr: %x, data: %x", addr
, data
);
849 if(legic_state
!= STATE_DISCON
) {
850 Dbprintf("Unexpected: sz:%u, Data:%03.3x, State:%u, Count:%u", f
->bits
, f
->data
, legic_state
, legic_read_count
);
852 Dbprintf("IV: %03.3x", legic_prng_iv
);
853 for(i
= 0; i
<legic_read_count
; i
++) {
854 Dbprintf("Read Nb: %u, Addr: %u", i
, BigBuf
[OFFSET_LOG
+i
]);
857 for(i
= -1; i
<legic_read_count
; i
++) {
859 t
= BigBuf
[OFFSET_LOG
+256+i
*4];
860 t
|= BigBuf
[OFFSET_LOG
+256+i
*4+1] << 8;
861 t
|= BigBuf
[OFFSET_LOG
+256+i
*4+2] <<16;
862 t
|= BigBuf
[OFFSET_LOG
+256+i
*4+3] <<24;
864 Dbprintf("Cycles: %u, Frame Length: %u, Time: %u",
865 BigBuf
[OFFSET_LOG
+128+i
],
866 BigBuf
[OFFSET_LOG
+384+i
],
870 legic_state
= STATE_DISCON
;
871 legic_read_count
= 0;
877 /* Read bit by bit untill full frame is received
878 * Call to process frame end answer
880 static void emit(int bit
) {
884 frame_append_bit(¤t_frame
, 1);
887 frame_append_bit(¤t_frame
, 0);
890 if(current_frame
.bits
<= 4) {
891 frame_clean(¤t_frame
);
893 frame_handle_tag(¤t_frame
);
894 frame_clean(¤t_frame
);
901 void LegicRfSimulate(int phase
, int frame
, int reqresp
)
903 /* ADC path high-frequency peak detector, FPGA in high-frequency simulator mode,
904 * modulation mode set to 212kHz subcarrier. We are getting the incoming raw
905 * envelope waveform on DIN and should send our response on DOUT.
907 * The LEGIC RF protocol is pulse-pause-encoding from reader to card, so we'll
908 * measure the time between two rising edges on DIN, and no encoding on the
909 * subcarrier from card to reader, so we'll just shift out our verbatim data
910 * on DOUT, 1 bit is 100us. The time from reader to card frame is still unclear,
911 * seems to be 300us-ish.
914 legic_phase_drift
= phase
;
915 legic_frame_drift
= frame
;
916 legic_reqresp_drift
= reqresp
;
918 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
919 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
921 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
| FPGA_HF_SIMULATOR_MODULATE_212K
);
923 /* Bitbang the receiver */
924 AT91C_BASE_PIOA
->PIO_ODR
= GPIO_SSC_DIN
;
925 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DIN
;
928 crc_init(&legic_crc
, 4, 0x19 >> 1, 0x5, 0);
932 legic_state
= STATE_DISCON
;
935 DbpString("Starting Legic emulator, press button to end");
937 while(!BUTTON_PRESS() && !usb_poll_validate_length()) {
938 int level
= !!(AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_DIN
);
939 int time
= timer
->TC_CV
;
941 if(level
!= old_level
) {
943 timer
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
945 if (FUZZ_EQUAL(time
, RWD_TIME_1
, RWD_TIME_FUZZ
)) {
950 } else if (FUZZ_EQUAL(time
, RWD_TIME_0
, RWD_TIME_FUZZ
)) {
965 if(time
>= (RWD_TIME_1
+RWD_TIME_FUZZ
) && active
) {
971 if(time
>= (20*RWD_TIME_1
) && (timer
->TC_SR
& AT91C_TC_CLKSTA
)) {
972 timer
->TC_CCR
= AT91C_TC_CLKDIS
;
978 if ( MF_DBGLEVEL
>= 1) DbpString("Stopped");
982 //-----------------------------------------------------------------------------
983 //-----------------------------------------------------------------------------
986 //-----------------------------------------------------------------------------
987 // Code up a string of octets at layer 2 (including CRC, we don't generate
988 // that here) so that they can be transmitted to the reader. Doesn't transmit
989 // them yet, just leaves them ready to send in ToSend[].
990 //-----------------------------------------------------------------------------
991 // static void CodeLegicAsTag(const uint8_t *cmd, int len)
997 // // Transmit a burst of ones, as the initial thing that lets the
998 // // reader get phase sync. This (TR1) must be > 80/fs, per spec,
999 // // but tag that I've tried (a Paypass) exceeds that by a fair bit,
1000 // // so I will too.
1001 // for(i = 0; i < 20; i++) {
1002 // ToSendStuffBit(1);
1003 // ToSendStuffBit(1);
1004 // ToSendStuffBit(1);
1005 // ToSendStuffBit(1);
1009 // for(i = 0; i < 10; i++) {
1010 // ToSendStuffBit(0);
1011 // ToSendStuffBit(0);
1012 // ToSendStuffBit(0);
1013 // ToSendStuffBit(0);
1015 // for(i = 0; i < 2; i++) {
1016 // ToSendStuffBit(1);
1017 // ToSendStuffBit(1);
1018 // ToSendStuffBit(1);
1019 // ToSendStuffBit(1);
1022 // for(i = 0; i < len; i++) {
1024 // uint8_t b = cmd[i];
1027 // ToSendStuffBit(0);
1028 // ToSendStuffBit(0);
1029 // ToSendStuffBit(0);
1030 // ToSendStuffBit(0);
1033 // for(j = 0; j < 8; j++) {
1035 // ToSendStuffBit(1);
1036 // ToSendStuffBit(1);
1037 // ToSendStuffBit(1);
1038 // ToSendStuffBit(1);
1040 // ToSendStuffBit(0);
1041 // ToSendStuffBit(0);
1042 // ToSendStuffBit(0);
1043 // ToSendStuffBit(0);
1049 // ToSendStuffBit(1);
1050 // ToSendStuffBit(1);
1051 // ToSendStuffBit(1);
1052 // ToSendStuffBit(1);
1056 // for(i = 0; i < 10; i++) {
1057 // ToSendStuffBit(0);
1058 // ToSendStuffBit(0);
1059 // ToSendStuffBit(0);
1060 // ToSendStuffBit(0);
1062 // for(i = 0; i < 2; i++) {
1063 // ToSendStuffBit(1);
1064 // ToSendStuffBit(1);
1065 // ToSendStuffBit(1);
1066 // ToSendStuffBit(1);
1069 // // Convert from last byte pos to length
1073 //-----------------------------------------------------------------------------
1074 // The software UART that receives commands from the reader, and its state
1076 //-----------------------------------------------------------------------------
1080 STATE_GOT_FALLING_EDGE_OF_SOF
,
1081 STATE_AWAITING_START_BIT
,
1082 STATE_RECEIVING_DATA
1092 /* Receive & handle a bit coming from the reader.
1094 * This function is called 4 times per bit (every 2 subcarrier cycles).
1095 * Subcarrier frequency fs is 212kHz, 1/fs = 4,72us, i.e. function is called every 9,44us
1098 * LED A -> ON once we have received the SOF and are expecting the rest.
1099 * LED A -> OFF once we have received EOF or are in error state or unsynced
1101 * Returns: true if we received a EOF
1102 * false if we are still waiting for some more
1104 // static RAMFUNC int HandleLegicUartBit(uint8_t bit)
1106 // switch(Uart.state) {
1107 // case STATE_UNSYNCD:
1109 // // we went low, so this could be the beginning of an SOF
1110 // Uart.state = STATE_GOT_FALLING_EDGE_OF_SOF;
1116 // case STATE_GOT_FALLING_EDGE_OF_SOF:
1118 // if(Uart.posCnt == 2) { // sample every 4 1/fs in the middle of a bit
1120 // if(Uart.bitCnt > 9) {
1121 // // we've seen enough consecutive
1122 // // zeros that it's a valid SOF
1124 // Uart.byteCnt = 0;
1125 // Uart.state = STATE_AWAITING_START_BIT;
1126 // LED_A_ON(); // Indicate we got a valid SOF
1128 // // didn't stay down long enough
1129 // // before going high, error
1130 // Uart.state = STATE_UNSYNCD;
1133 // // do nothing, keep waiting
1137 // if(Uart.posCnt >= 4) Uart.posCnt = 0;
1138 // if(Uart.bitCnt > 12) {
1139 // // Give up if we see too many zeros without
1142 // Uart.state = STATE_UNSYNCD;
1146 // case STATE_AWAITING_START_BIT:
1149 // if(Uart.posCnt > 50/2) { // max 57us between characters = 49 1/fs, max 3 etus after low phase of SOF = 24 1/fs
1150 // // stayed high for too long between
1151 // // characters, error
1152 // Uart.state = STATE_UNSYNCD;
1155 // // falling edge, this starts the data byte
1158 // Uart.shiftReg = 0;
1159 // Uart.state = STATE_RECEIVING_DATA;
1163 // case STATE_RECEIVING_DATA:
1165 // if(Uart.posCnt == 2) {
1166 // // time to sample a bit
1167 // Uart.shiftReg >>= 1;
1169 // Uart.shiftReg |= 0x200;
1173 // if(Uart.posCnt >= 4) {
1176 // if(Uart.bitCnt == 10) {
1177 // if((Uart.shiftReg & 0x200) && !(Uart.shiftReg & 0x001))
1179 // // this is a data byte, with correct
1180 // // start and stop bits
1181 // Uart.output[Uart.byteCnt] = (Uart.shiftReg >> 1) & 0xff;
1184 // if(Uart.byteCnt >= Uart.byteCntMax) {
1185 // // Buffer overflowed, give up
1187 // Uart.state = STATE_UNSYNCD;
1189 // // so get the next byte now
1191 // Uart.state = STATE_AWAITING_START_BIT;
1193 // } else if (Uart.shiftReg == 0x000) {
1194 // // this is an EOF byte
1195 // LED_A_OFF(); // Finished receiving
1196 // Uart.state = STATE_UNSYNCD;
1197 // if (Uart.byteCnt != 0) {
1201 // // this is an error
1203 // Uart.state = STATE_UNSYNCD;
1210 // Uart.state = STATE_UNSYNCD;
1218 static void UartReset() {
1219 Uart
.byteCntMax
= 3;
1220 Uart
.state
= STATE_UNSYNCD
;
1224 memset(Uart
.output
, 0x00, 3);
1227 // static void UartInit(uint8_t *data) {
1228 // Uart.output = data;
1232 //=============================================================================
1233 // An LEGIC reader. We take layer two commands, code them
1234 // appropriately, and then send them to the tag. We then listen for the
1235 // tag's response, which we leave in the buffer to be demodulated on the
1237 //=============================================================================
1242 DEMOD_PHASE_REF_TRAINING
,
1243 DEMOD_AWAITING_FALLING_EDGE_OF_SOF
,
1244 DEMOD_GOT_FALLING_EDGE_OF_SOF
,
1245 DEMOD_AWAITING_START_BIT
,
1246 DEMOD_RECEIVING_DATA
1259 * Handles reception of a bit from the tag
1261 * This function is called 2 times per bit (every 4 subcarrier cycles).
1262 * Subcarrier frequency fs is 212kHz, 1/fs = 4,72us, i.e. function is called every 9,44us
1265 * LED C -> ON once we have received the SOF and are expecting the rest.
1266 * LED C -> OFF once we have received EOF or are unsynced
1268 * Returns: true if we received a EOF
1269 * false if we are still waiting for some more
1273 #ifndef SUBCARRIER_DETECT_THRESHOLD
1274 # define SUBCARRIER_DETECT_THRESHOLD 8
1277 // Subcarrier amplitude v = sqrt(ci^2 + cq^2), approximated here by max(abs(ci),abs(cq)) + 1/2*min(abs(ci),abs(cq)))
1278 #ifndef CHECK_FOR_SUBCARRIER
1279 # define CHECK_FOR_SUBCARRIER() { v = MAX(ai, aq) + MIN(halfci, halfcq); }
1282 // The soft decision on the bit uses an estimate of just the
1283 // quadrant of the reference angle, not the exact angle.
1284 // Subcarrier amplitude v = sqrt(ci^2 + cq^2), approximated here by max(abs(ci),abs(cq)) + 1/2*min(abs(ci),abs(cq)))
1285 #define MAKE_SOFT_DECISION() { \
1286 if(Demod.sumI > 0) \
1291 if(Demod.sumQ > 0) \
1298 static RAMFUNC
int HandleLegicSamplesDemod(int ci
, int cq
)
1303 int halfci
= (ai
>> 1);
1304 int halfcq
= (aq
>> 1);
1306 switch(Demod
.state
) {
1309 CHECK_FOR_SUBCARRIER()
1311 if(v
> SUBCARRIER_DETECT_THRESHOLD
) { // subcarrier detected
1312 Demod
.state
= DEMOD_PHASE_REF_TRAINING
;
1319 case DEMOD_PHASE_REF_TRAINING
:
1320 if(Demod
.posCount
< 8) {
1322 CHECK_FOR_SUBCARRIER()
1324 if (v
> SUBCARRIER_DETECT_THRESHOLD
) {
1325 // set the reference phase (will code a logic '1') by averaging over 32 1/fs.
1326 // note: synchronization time > 80 1/fs
1332 Demod
.state
= DEMOD_UNSYNCD
;
1335 Demod
.state
= DEMOD_AWAITING_FALLING_EDGE_OF_SOF
;
1339 case DEMOD_AWAITING_FALLING_EDGE_OF_SOF
:
1341 MAKE_SOFT_DECISION()
1343 //Dbprintf("ICE: %d %d %d %d %d", v, Demod.sumI, Demod.sumQ, ci, cq );
1344 // logic '0' detected
1347 Demod
.state
= DEMOD_GOT_FALLING_EDGE_OF_SOF
;
1349 // start of SOF sequence
1352 // maximum length of TR1 = 200 1/fs
1353 if(Demod
.posCount
> 25*2) Demod
.state
= DEMOD_UNSYNCD
;
1358 case DEMOD_GOT_FALLING_EDGE_OF_SOF
:
1361 MAKE_SOFT_DECISION()
1364 // low phase of SOF too short (< 9 etu). Note: spec is >= 10, but FPGA tends to "smear" edges
1365 if(Demod
.posCount
< 10*2) {
1366 Demod
.state
= DEMOD_UNSYNCD
;
1368 LED_C_ON(); // Got SOF
1369 Demod
.state
= DEMOD_AWAITING_START_BIT
;
1374 // low phase of SOF too long (> 12 etu)
1375 if(Demod
.posCount
> 13*2) {
1376 Demod
.state
= DEMOD_UNSYNCD
;
1382 case DEMOD_AWAITING_START_BIT
:
1385 MAKE_SOFT_DECISION()
1388 // max 19us between characters = 16 1/fs, max 3 etu after low phase of SOF = 24 1/fs
1389 if(Demod
.posCount
> 3*2) {
1390 Demod
.state
= DEMOD_UNSYNCD
;
1394 // start bit detected
1396 Demod
.posCount
= 1; // this was the first half
1399 Demod
.state
= DEMOD_RECEIVING_DATA
;
1403 case DEMOD_RECEIVING_DATA
:
1405 MAKE_SOFT_DECISION()
1407 if(Demod
.posCount
== 0) {
1408 // first half of bit
1412 // second half of bit
1414 Demod
.shiftReg
>>= 1;
1416 if(Demod
.thisBit
> 0)
1417 Demod
.shiftReg
|= 0x200;
1421 if(Demod
.bitCount
== 10) {
1423 uint16_t s
= Demod
.shiftReg
;
1425 if((s
& 0x200) && !(s
& 0x001)) {
1426 // stop bit == '1', start bit == '0'
1427 uint8_t b
= (s
>> 1);
1428 Demod
.output
[Demod
.len
] = b
;
1430 Demod
.state
= DEMOD_AWAITING_START_BIT
;
1432 Demod
.state
= DEMOD_UNSYNCD
;
1436 // This is EOF (start, stop and all data bits == '0'
1446 Demod
.state
= DEMOD_UNSYNCD
;
1453 // Clear out the state of the "UART" that receives from the tag.
1454 static void DemodReset() {
1456 Demod
.state
= DEMOD_UNSYNCD
;
1463 memset(Demod
.output
, 0x00, 3);
1466 static void DemodInit(uint8_t *data
) {
1467 Demod
.output
= data
;
1472 * Demodulate the samples we received from the tag, also log to tracebuffer
1473 * quiet: set to 'TRUE' to disable debug output
1475 #define LEGIC_DMA_BUFFER_SIZE 256
1476 static void GetSamplesForLegicDemod(int n
, bool quiet
)
1479 bool gotFrame
= FALSE
;
1480 int lastRxCounter
= LEGIC_DMA_BUFFER_SIZE
;
1481 int ci
, cq
, samples
= 0;
1485 // And put the FPGA in the appropriate mode
1486 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
| FPGA_HF_READER_RX_XCORR_QUARTER_FREQ
);
1488 // The response (tag -> reader) that we're receiving.
1489 // Set up the demodulator for tag -> reader responses.
1490 DemodInit(BigBuf_malloc(MAX_FRAME_SIZE
));
1492 // The DMA buffer, used to stream samples from the FPGA
1493 int8_t *dmaBuf
= (int8_t*) BigBuf_malloc(LEGIC_DMA_BUFFER_SIZE
);
1494 int8_t *upTo
= dmaBuf
;
1496 // Setup and start DMA.
1497 if ( !FpgaSetupSscDma((uint8_t*) dmaBuf
, LEGIC_DMA_BUFFER_SIZE
) ){
1498 if (MF_DBGLEVEL
> 1) Dbprintf("FpgaSetupSscDma failed. Exiting");
1502 // Signal field is ON with the appropriate LED:
1505 int behindBy
= lastRxCounter
- AT91C_BASE_PDC_SSC
->PDC_RCR
;
1506 if(behindBy
> max
) max
= behindBy
;
1508 while(((lastRxCounter
-AT91C_BASE_PDC_SSC
->PDC_RCR
) & (LEGIC_DMA_BUFFER_SIZE
-1)) > 2) {
1512 if(upTo
>= dmaBuf
+ LEGIC_DMA_BUFFER_SIZE
) {
1514 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (uint32_t) upTo
;
1515 AT91C_BASE_PDC_SSC
->PDC_RNCR
= LEGIC_DMA_BUFFER_SIZE
;
1518 if(lastRxCounter
<= 0)
1519 lastRxCounter
= LEGIC_DMA_BUFFER_SIZE
;
1523 gotFrame
= HandleLegicSamplesDemod(ci
, cq
);
1528 if(samples
> n
|| gotFrame
)
1532 FpgaDisableSscDma();
1534 if (!quiet
&& Demod
.len
== 0) {
1535 Dbprintf("max behindby = %d, samples = %d, gotFrame = %d, Demod.len = %d, Demod.sumI = %d, Demod.sumQ = %d",
1546 if (Demod
.len
> 0) {
1547 uint8_t parity
[MAX_PARITY_SIZE
] = {0x00};
1548 LogTrace(Demod
.output
, Demod
.len
, 0, 0, parity
, FALSE
);
1551 //-----------------------------------------------------------------------------
1552 // Transmit the command (to the tag) that was placed in ToSend[].
1553 //-----------------------------------------------------------------------------
1554 static void TransmitForLegic(void)
1560 while(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
))
1561 AT91C_BASE_SSC
->SSC_THR
= 0xff;
1563 // Signal field is ON with the appropriate Red LED
1566 // Signal we are transmitting with the Green LED
1568 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX
| FPGA_HF_READER_TX_SHALLOW_MOD
);
1570 for(c
= 0; c
< 10;) {
1571 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1572 AT91C_BASE_SSC
->SSC_THR
= 0xff;
1575 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1576 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1584 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1585 AT91C_BASE_SSC
->SSC_THR
= ToSend
[c
];
1586 legic_prng_forward(1); // forward the lfsr
1588 if(c
>= ToSendMax
) {
1592 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1593 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1602 //-----------------------------------------------------------------------------
1603 // Code a layer 2 command (string of octets, including CRC) into ToSend[],
1604 // so that it is ready to transmit to the tag using TransmitForLegic().
1605 //-----------------------------------------------------------------------------
1606 static void CodeLegicBitsAsReader(const uint8_t *cmd
, uint8_t cmdlen
, int bits
)
1614 for(i
= 0; i
< 7; i
++)
1618 for(i
= 0; i
< cmdlen
; i
++) {
1624 for(j
= 0; j
< bits
; j
++) {
1634 // Convert from last character reference to length
1639 Convenience function to encode, transmit and trace Legic comms
1641 static void CodeAndTransmitLegicAsReader(const uint8_t *cmd
, uint8_t cmdlen
, int bits
)
1643 CodeLegicBitsAsReader(cmd
, cmdlen
, bits
);
1646 uint8_t parity
[1] = {0x00};
1647 LogTrace(cmd
, cmdlen
, 0, 0, parity
, TRUE
);
1651 int ice_legic_select_card()
1653 //int cmd_size=0, card_size=0;
1654 uint8_t wakeup
[] = { 0x7F };
1655 uint8_t getid
[] = {0x19};
1657 legic_prng_init(SESSION_IV
);
1659 // first, wake up the tag, 7bits
1660 CodeAndTransmitLegicAsReader(wakeup
, sizeof(wakeup
), 7);
1662 GetSamplesForLegicDemod(1000, TRUE
);
1664 // frame_clean(¤t_frame);
1665 //frame_receive_rwd(¤t_frame, 6, 1);
1667 legic_prng_forward(1); /* we wait anyways */
1669 //while(timer->TC_CV < 387) ; /* ~ 258us */
1670 //frame_send_rwd(0x19, 6);
1671 CodeAndTransmitLegicAsReader(getid
, sizeof(getid
), 8);
1672 GetSamplesForLegicDemod(1000, TRUE
);
1674 //if (Demod.len < 14) return 2;
1675 Dbprintf("CARD TYPE: %02x LEN: %d", Demod
.output
[0], Demod
.len
);
1677 switch(Demod
.output
[0]) {
1679 DbpString("MIM 256 card found");
1684 DbpString("MIM 1024 card found");
1686 // card_size = 1024;
1693 // bytes = card_size;
1695 // if(bytes + offset >= card_size)
1696 // bytes = card_size - offset;
1698 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1703 // Set up LEGIC communication
1704 void ice_legic_setup() {
1707 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1708 BigBuf_free(); BigBuf_Clear_ext(false);
1714 // Set up the synchronous serial port
1717 // connect Demodulated Signal to ADC:
1718 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1720 // Signal field is on with the appropriate LED
1722 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX
| FPGA_HF_READER_TX_SHALLOW_MOD
);
1725 //StartCountSspClk();
1728 crc_init(&legic_crc
, 4, 0x19 >> 1, 0x5, 0);