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
;
39 static void setup_timer(void) {
40 // Set up Timer 1 to use for measuring time between pulses. Since we're bit-banging
41 // this it won't be terribly accurate but should be good enough.
43 AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_TC1);
44 timer = AT91C_BASE_TC1;
45 timer->TC_CCR = AT91C_TC_CLKDIS;
46 timer->TC_CMR = AT91C_TC_CLKS_TIMER_DIV3_CLOCK;
47 timer->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
50 // Set up Timer 2 to use for measuring time between frames in
51 // tag simulation mode. Runs 4x faster as Timer 1
53 AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_TC2);
54 prng_timer = AT91C_BASE_TC2;
55 prng_timer->TC_CCR = AT91C_TC_CLKDIS;
56 prng_timer->TC_CMR = AT91C_TC_CLKS_TIMER_DIV2_CLOCK;
57 prng_timer->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
61 // At TIMER_CLOCK3 (MCK/32)
62 //#define RWD_TIME_1 150 /* RWD_TIME_PAUSE off, 80us on = 100us */
63 //#define RWD_TIME_0 90 /* RWD_TIME_PAUSE off, 40us on = 60us */
64 //#define RWD_TIME_PAUSE 30 /* 20us */
66 #define RWD_TIME_1 80-4 /* READER_TIME_PAUSE off, 80us on = 100us */
67 #define RWD_TIME_0 40-4 /* READER_TIME_PAUSE off, 40us on = 60us */
68 #define RWD_TIME_PAUSE 20-4 /* 20us */
70 #define TAG_BIT_PERIOD 100-8 // 100us for every bit
72 #define RWD_TIME_FUZZ 20 /* rather generous 13us, since the peak detector + hysteresis fuzz quite a bit */
74 #define TAG_TIME_WAIT 330 // 330us from READER frame end to TAG frame start, experimentally determined (490)
75 #define RDW_TIME_WAIT 258 //
78 #define SIM_DIVISOR 586 /* prng_time/SIM_DIVISOR count prng needs to be forwared */
79 #define SIM_SHIFT 900 /* prng_time+SIM_SHIFT shift of delayed start */
81 #define OFFSET_LOG 1024
83 #define FUZZ_EQUAL(value, target, fuzz) ((value) > ((target)-(fuzz)) && (value) < ((target)+(fuzz)))
86 //#define LOW(x) AT91C_BASE_PIOA->PIO_CODR = (x)
87 # define SHORT_COIL LOW(GPIO_SSC_DOUT);
90 //#define HIGH(x) AT91C_BASE_PIOA->PIO_SODR = (x)
91 # define OPEN_COIL HIGH(GPIO_SSC_DOUT);
94 uint32_t stop_send_frame_us
= 0;
96 // ~ 258us + 100us*delay
97 #define WAIT(delay) SpinDelayCountUs((delay));
98 #define COIL_PULSE(x) { SHORT_COIL; WAIT(RWD_TIME_PAUSE); OPEN_COIL; WAIT((x)); }
99 #define COIL_PULSE_PAUSE { SHORT_COIL; WAIT(RWD_TIME_PAUSE); OPEN_COIL; }
101 // ToDo: define a meaningful maximum size for auth_table. The bigger this is, the lower will be the available memory for traces.
102 // Historically it used to be FREE_BUFFER_SIZE, which was 2744.
103 #define LEGIC_CARD_MEMSIZE 1024
104 static uint8_t* cardmem
;
106 // Starts Clock and waits until its reset
107 static void Reset(AT91PS_TC clock
){
108 clock
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
109 while(clock
->TC_CV
> 1) ;
112 // Starts Clock and waits until its reset
113 static void ResetClock(void){
117 static void frame_append_bit(struct legic_frame
* const f
, int bit
) {
118 // Overflow, won't happen
119 if (f
->bits
>= 31) return;
121 f
->data
|= (bit
<< f
->bits
);
125 static void frame_clean(struct legic_frame
* const f
) {
130 // Prng works when waiting in 99.1us cycles.
131 // and while sending/receiving in bit frames (100, 60)
132 /*static void CalibratePrng( uint32_t time){
133 // Calculate Cycles based on timer 100us
134 uint32_t i = (time - stop_send_frame_us) / 100 ;
136 // substract cycles of finished frames
137 int k = i - legic_prng_count()+1;
139 // substract current frame length, rewind to beginning
141 legic_prng_forward(k);
145 /* Generate Keystream */
146 static uint32_t get_key_stream(int skip
, int count
)
151 // Use int to enlarge timer tc to 32bit
152 legic_prng_bc
+= prng_timer
->TC_CV
;
154 // reset the prng timer.
157 /* If skip == -1, forward prng time based */
159 i
= (legic_prng_bc
+ SIM_SHIFT
)/SIM_DIVISOR
; /* Calculate Cycles based on timer */
160 i
-= legic_prng_count(); /* substract cycles of finished frames */
161 i
-= count
; /* substract current frame length, rewind to beginning */
162 legic_prng_forward(i
);
164 legic_prng_forward(skip
);
167 i
= (count
== 6) ? -1 : legic_read_count
;
169 /* Write Time Data into LOG */
170 // uint8_t *BigBuf = BigBuf_get_addr();
171 // BigBuf[OFFSET_LOG+128+i] = legic_prng_count();
172 // BigBuf[OFFSET_LOG+256+i*4] = (legic_prng_bc >> 0) & 0xff;
173 // BigBuf[OFFSET_LOG+256+i*4+1] = (legic_prng_bc >> 8) & 0xff;
174 // BigBuf[OFFSET_LOG+256+i*4+2] = (legic_prng_bc >>16) & 0xff;
175 // BigBuf[OFFSET_LOG+256+i*4+3] = (legic_prng_bc >>24) & 0xff;
176 // BigBuf[OFFSET_LOG+384+i] = count;
178 /* Generate KeyStream */
179 for(i
=0; i
<count
; i
++) {
180 key
|= legic_prng_get_bit() << i
;
181 legic_prng_forward(1);
186 /* Send a frame in tag mode, the FPGA must have been set up by
189 static void frame_send_tag(uint16_t response
, uint8_t bits
, uint8_t crypt
) {
190 /* Bitbang the response */
192 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
193 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
195 /* Use time to crypt frame */
197 legic_prng_forward(2); /* TAG_TIME_WAIT -> shift by 2 */
198 response
^= legic_prng_get_bits(bits
);
201 /* Wait for the frame start */
202 WAIT( TAG_TIME_WAIT
)
205 for(int i
= 0; i
< bits
; i
++) {
220 /* Send a frame in reader mode, the FPGA must have been set up by
223 static void frame_sendAsReader(uint32_t data
, uint8_t bits
){
225 uint32_t starttime
= GetCountUS();
226 uint32_t send
= data
;
227 uint8_t prng1
= legic_prng_count() ;
229 uint16_t lfsr
= legic_prng_get_bits(bits
);
231 // xor the lsfr onto data.
234 for (; mask
< BITMASK(bits
); mask
<<= 1) {
236 COIL_PULSE(RWD_TIME_1
);
238 COIL_PULSE(RWD_TIME_0
);
242 // One final pause to mark the end of the frame
245 stop_send_frame_us
= GetCountUS();
246 uint8_t cmdbytes
[] = {
255 LogTrace(cmdbytes
, sizeof(cmdbytes
), starttime
, stop_send_frame_us
, NULL
, TRUE
);
258 /* Receive a frame from the card in reader emulation mode, the FPGA and
259 * timer must have been set up by LegicRfReader and frame_sendAsReader.
261 * The LEGIC RF protocol from card to reader does not include explicit
262 * frame start/stop information or length information. The reader must
263 * know beforehand how many bits it wants to receive. (Notably: a card
264 * sending a stream of 0-bits is indistinguishable from no card present.)
266 * Receive methodology: There is a fancy correlator in hi_read_rx_xcorr, but
267 * I'm not smart enough to use it. Instead I have patched hi_read_tx to output
268 * the ADC signal with hysteresis on SSP_DIN. Bit-bang that signal and look
269 * for edges. Count the edges in each bit interval. If they are approximately
270 * 0 this was a 0-bit, if they are approximately equal to the number of edges
271 * expected for a 212kHz subcarrier, this was a 1-bit. For timing we use the
272 * timer that's still running from frame_sendAsReader in order to get a synchronization
273 * with the frame that we just sent.
275 * FIXME: Because we're relying on the hysteresis to just do the right thing
276 * the range is severely reduced (and you'll probably also need a good antenna).
277 * So this should be fixed some time in the future for a proper receiver.
279 static void frame_receiveAsReader(struct legic_frame
* const f
, uint8_t bits
, uint8_t crypt
) {
283 uint8_t i
= 0, edges
= 0;
285 uint32_t the_bit
= 1, next_bit_at
, data
;
286 int old_level
= 0, level
= 0;
288 if(bits
> 32) bits
= 32;
290 uint32_t starttime
= GetCountUS();
292 // calibrate the prng.
293 legic_prng_forward(2);
294 //CalibratePrng( starttime );
296 // precompute the cipher
297 uint8_t prng_before
= legic_prng_count() ;
300 lsfr
= legic_prng_get_bits(bits
);
304 next_bit_at
= GetCountUS() + TAG_BIT_PERIOD
;
306 //FIXED time between sending frame and now listening frame. 330us
307 uint32_t icetime
= TAG_TIME_WAIT
- ( GetCountUS() - stop_send_frame_us
);
309 WAIT( icetime
); // 21.3us inc.
311 AT91C_BASE_PIOA
->PIO_ODR
= GPIO_SSC_DIN
;
312 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DIN
;
314 for( i
= 0; i
< bits
; i
++) {
316 while ( GetCountUS() < next_bit_at
) {
318 level
= (AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_DIN
);
320 if (level
!= old_level
)
325 next_bit_at
+= TAG_BIT_PERIOD
;
327 // We expect 42 edges == ONE
328 if(edges
> 20 && edges
< 60)
339 uint8_t cmdbytes
[] = {
348 (icetime
>> 8) & 0xFF
350 LogTrace(cmdbytes
, sizeof(cmdbytes
), starttime
, GetCountUS(), NULL
, FALSE
);
354 // Setup pm3 as a Legic Reader
355 static uint32_t perform_setup_phase_rwd(uint8_t iv
) {
357 // Switch on carrier and let the tag charge for 1ms
367 frame_sendAsReader(iv
, 7);
369 // Now both tag and reader has same IV. Prng can start.
372 frame_receiveAsReader(¤t_frame
, 6, 1);
374 // fixed delay before sending ack.
375 WAIT(TAG_BIT_PERIOD
);
377 // Send obsfuscated acknowledgment frame.
378 // 0x19 = 0x18 MIM22, 0x01 LSB READCMD
379 // 0x39 = 0x38 MIM256, MIM1024 0x01 LSB READCMD
380 switch ( current_frame
.data
) {
382 frame_sendAsReader(0x19, 6);
386 frame_sendAsReader(0x39, 6);
391 return current_frame
.data
;
393 // End of Setup Phase.
396 static void LegicCommonInit(void) {
397 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
398 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX
);
399 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
402 /* Bitbang the transmitter */
404 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
405 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
407 // reserve a cardmem, meaning we can use the tracelog function in bigbuff easier.
408 cardmem
= BigBuf_malloc(LEGIC_CARD_MEMSIZE
);
409 memset(cardmem
, 0x00, LEGIC_CARD_MEMSIZE
);
414 crc_init(&legic_crc
, 4, 0x19 >> 1, 0x5, 0);
419 /* Switch off carrier, make sure tag is reset */
420 static void switch_off_tag_rwd(void) {
427 // calculate crc4 for a legic READ command
428 // 5,8,10 address size.
429 static uint32_t LegicCRC(uint16_t byte_index
, uint8_t value
, uint8_t cmd_sz
) {
430 crc_clear(&legic_crc
);
431 uint32_t temp
= (value
<< cmd_sz
) | (byte_index
<< 1) | LEGIC_READ
;
432 crc_update(&legic_crc
, temp
, cmd_sz
+ 8 );
433 // crc_update(&legic_crc, LEGIC_READ, 1);
434 // crc_update(&legic_crc, byte_index, cmd_sz-1);
435 // crc_update(&legic_crc, value, 8);
436 return crc_finish(&legic_crc
);
439 int legic_read_byte(int byte_index
, int cmd_sz
) {
441 uint8_t byte
= 0, crc
= 0;
442 uint32_t calcCrc
= 0;
443 uint32_t cmd
= (byte_index
<< 1) | LEGIC_READ
;
445 legic_prng_forward(3);
448 frame_sendAsReader(cmd
, cmd_sz
);
450 frame_receiveAsReader(¤t_frame
, 12, 1);
452 byte
= current_frame
.data
& 0xFF;
454 calcCrc
= LegicCRC(byte_index
, byte
, cmd_sz
);
455 crc
= (current_frame
.data
>> 8);
457 if( calcCrc
!= crc
) {
458 Dbprintf("!!! crc mismatch: expected %x but got %x !!!", calcCrc
, crc
);
466 * - assemble a write_cmd_frame with crc and send it
467 * - wait until the tag sends back an ACK ('1' bit unencrypted)
468 * - forward the prng based on the timing
470 //int legic_write_byte(int byte, int addr, int addr_sz, int PrngCorrection) {
471 int legic_write_byte(int byte
, int addr
, int addr_sz
) {
473 //do not write UID, CRC at offset 0-4.
474 if(addr
<= 0x04) return 0;
477 crc_clear(&legic_crc
);
478 crc_update(&legic_crc
, 0, 1); /* CMD_WRITE */
479 crc_update(&legic_crc
, addr
, addr_sz
);
480 crc_update(&legic_crc
, byte
, 8);
481 uint32_t crc
= crc_finish(&legic_crc
);
483 // send write command
484 uint32_t cmd
= ((crc
<<(addr_sz
+1+8)) //CRC
485 |(byte
<<(addr_sz
+1)) //Data
486 |(addr
<<1) //Address
487 |(0x00 <<0)); //CMD = W
488 uint32_t cmd_sz
= addr_sz
+1+8+4; //crc+data+cmd
490 legic_prng_forward(2); /* we wait anyways */
492 while(timer
->TC_CV
< 387) ; /* ~ 258us */
494 frame_sendAsReader(cmd
, cmd_sz
);
496 // wllm-rbnt doesnt have these
497 // AT91C_BASE_PIOA->PIO_ODR = GPIO_SSC_DIN;
498 // AT91C_BASE_PIOA->PIO_PER = GPIO_SSC_DIN;
501 int t
, old_level
= 0, edges
= 0;
504 while(timer
->TC_CV
< 387) ; /* ~ 258us */
506 for( t
= 0; t
< 80; t
++) {
508 next_bit_at
+= TAG_BIT_PERIOD
;
509 while(timer
->TC_CV
< next_bit_at
) {
510 int level
= (AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_DIN
);
511 if(level
!= old_level
) {
516 if(edges
> 20 && edges
< 60) { /* expected are 42 edges */
517 int t
= timer
->TC_CV
;
518 int c
= t
/ TAG_BIT_PERIOD
;
521 legic_prng_forward(c
);
530 int LegicRfReader(int offset
, int bytes
, int iv
) {
532 int byte_index
= 0, cmd_sz
= 0, card_sz
= 0;
534 if ( MF_DBGLEVEL
>= 2) {
535 Dbprintf("setting up legic card, IV = %x", iv
);
537 Dbprintf("ONE %d ZERO %d PAUSE %d", RWD_TIME_1
, RWD_TIME_0
, RWD_TIME_PAUSE
);
538 Dbprintf("TAG BIT PERIOD %d FUZZ %d TAG WAIT TIME %d", TAG_BIT_PERIOD
, RWD_TIME_FUZZ
, TAG_TIME_WAIT
);
543 uint32_t tag_type
= perform_setup_phase_rwd(iv
);
545 //we lose to mutch time with dprintf
546 switch_off_tag_rwd();
550 if ( MF_DBGLEVEL
>= 2) DbpString("MIM22 card found, reading card ...");
555 if ( MF_DBGLEVEL
>= 2) DbpString("MIM256 card found, reading card ...");
560 if ( MF_DBGLEVEL
>= 2) DbpString("MIM1024 card found, reading card ...");
565 if ( MF_DBGLEVEL
>= 1) Dbprintf("Unknown card format: %x",tag_type
);
571 if(bytes
+offset
>= card_sz
)
572 bytes
= card_sz
- offset
;
574 // Start setup and read bytes.
575 perform_setup_phase_rwd(iv
);
578 while (byte_index
< bytes
) {
579 int r
= legic_read_byte(byte_index
+offset
, cmd_sz
);
581 if (r
== -1 || BUTTON_PRESS()) {
582 switch_off_tag_rwd();
584 if ( MF_DBGLEVEL
>= 2) DbpString("operation aborted");
585 cmd_send(CMD_ACK
,0,0,0,0,0);
588 cardmem
[byte_index
] = r
;
593 switch_off_tag_rwd();
595 uint8_t len
= (bytes
& 0x3FF);
596 cmd_send(CMD_ACK
,1,len
,0,0,0);
600 /*int _LegicRfWriter(int offset, int bytes, int addr_sz, uint8_t *BigBuf, int RoundBruteforceValue) {
604 perform_setup_phase_rwd(iv);
605 //legic_prng_forward(2);
606 while(byte_index < bytes) {
609 //check if the DCF should be changed
610 if ( (offset == 0x05) && (bytes == 0x02) ) {
611 //write DCF in reverse order (addr 0x06 before 0x05)
612 r = legic_write_byte(BigBuf[(0x06-byte_index)], (0x06-byte_index), addr_sz, RoundBruteforceValue);
613 //legic_prng_forward(1);
616 r = legic_write_byte(BigBuf[(0x06-byte_index)], (0x06-byte_index), addr_sz, RoundBruteforceValue);
618 //legic_prng_forward(1);
621 r = legic_write_byte(BigBuf[byte_index+offset], byte_index+offset, addr_sz, RoundBruteforceValue);
623 if((r != 0) || BUTTON_PRESS()) {
624 Dbprintf("operation aborted @ 0x%03.3x", byte_index);
625 switch_off_tag_rwd();
633 if(byte_index & 0x10) LED_C_ON(); else LED_C_OFF();
637 DbpString("write successful");
641 void LegicRfWriter(int offset
, int bytes
, int iv
) {
643 int byte_index
= 0, addr_sz
= 0;
647 if ( MF_DBGLEVEL
>= 2) DbpString("setting up legic card");
649 uint32_t tag_type
= perform_setup_phase_rwd(iv
);
651 switch_off_tag_rwd();
655 if(offset
+bytes
> 22) {
656 Dbprintf("Error: can not write to 0x%03.3x on MIM22", offset
+bytes
);
660 if ( MF_DBGLEVEL
>= 2) Dbprintf("MIM22 card found, writing 0x%02.2x - 0x%02.2x ...", offset
, offset
+bytes
);
663 if(offset
+bytes
> 0x100) {
664 Dbprintf("Error: can not write to 0x%03.3x on MIM256", offset
+bytes
);
668 if ( MF_DBGLEVEL
>= 2) Dbprintf("MIM256 card found, writing 0x%02.2x - 0x%02.2x ...", offset
, offset
+bytes
);
671 if(offset
+bytes
> 0x400) {
672 Dbprintf("Error: can not write to 0x%03.3x on MIM1024", offset
+bytes
);
676 if ( MF_DBGLEVEL
>= 2) Dbprintf("MIM1024 card found, writing 0x%03.3x - 0x%03.3x ...", offset
, offset
+bytes
);
679 Dbprintf("No or unknown card found, aborting");
684 perform_setup_phase_rwd(iv
);
685 while(byte_index
< bytes
) {
688 //check if the DCF should be changed
689 if ( ((byte_index
+offset
) == 0x05) && (bytes
>= 0x02) ) {
690 //write DCF in reverse order (addr 0x06 before 0x05)
691 r
= legic_write_byte(cardmem
[(0x06-byte_index
)], (0x06-byte_index
), addr_sz
);
693 // write second byte on success...
696 r
= legic_write_byte(cardmem
[(0x06-byte_index
)], (0x06-byte_index
), addr_sz
);
700 r
= legic_write_byte(cardmem
[byte_index
+offset
], byte_index
+offset
, addr_sz
);
703 if((r
!= 0) || BUTTON_PRESS()) {
704 Dbprintf("operation aborted @ 0x%03.3x", byte_index
);
705 switch_off_tag_rwd();
714 if ( MF_DBGLEVEL
>= 1) DbpString("write successful");
717 void LegicRfRawWriter(int address
, int byte
, int iv
) {
719 int byte_index
= 0, addr_sz
= 0;
723 if ( MF_DBGLEVEL
>= 2) DbpString("setting up legic card");
725 uint32_t tag_type
= perform_setup_phase_rwd(iv
);
727 switch_off_tag_rwd();
732 Dbprintf("Error: can not write to 0x%03.3x on MIM22", address
);
736 if ( MF_DBGLEVEL
>= 2) Dbprintf("MIM22 card found, writing at addr 0x%02.2x - value 0x%02.2x ...", address
, byte
);
739 if(address
> 0x100) {
740 Dbprintf("Error: can not write to 0x%03.3x on MIM256", address
);
744 if ( MF_DBGLEVEL
>= 2) Dbprintf("MIM256 card found, writing at addr 0x%02.2x - value 0x%02.2x ...", address
, byte
);
747 if(address
> 0x400) {
748 Dbprintf("Error: can not write to 0x%03.3x on MIM1024", address
);
752 if ( MF_DBGLEVEL
>= 2) Dbprintf("MIM1024 card found, writing at addr 0x%03.3x - value 0x%03.3x ...", address
, byte
);
755 Dbprintf("No or unknown card found, aborting");
759 Dbprintf("integer value: %d address: %d addr_sz: %d", byte
, address
, addr_sz
);
762 perform_setup_phase_rwd(iv
);
763 //legic_prng_forward(2);
765 int r
= legic_write_byte(byte
, address
, addr_sz
);
767 if((r
!= 0) || BUTTON_PRESS()) {
768 Dbprintf("operation aborted @ 0x%03.3x (%1d)", byte_index
, r
);
769 switch_off_tag_rwd();
775 if ( MF_DBGLEVEL
>= 1) DbpString("write successful");
778 /* Handle (whether to respond) a frame in tag mode
779 * Only called when simulating a tag.
781 static void frame_handle_tag(struct legic_frame
const * const f
)
783 uint8_t *BigBuf
= BigBuf_get_addr();
785 /* First Part of Handshake (IV) */
793 legic_prng_init(f
->data
);
794 frame_send_tag(0x3d, 6, 1); /* 0x3d^0x26 = 0x1B */
795 legic_state
= STATE_IV
;
796 legic_read_count
= 0;
798 legic_prng_iv
= f
->data
;
803 //while(timer->TC_CV < 280);
809 if(legic_state
== STATE_IV
) {
810 int local_key
= get_key_stream(3, 6);
811 int xored
= 0x39 ^ local_key
;
812 if((f
->bits
== 6) && (f
->data
== xored
)) {
813 legic_state
= STATE_CON
;
818 //while(timer->TC_CV < 200);
823 legic_state
= STATE_DISCON
;
825 Dbprintf("iv: %02x frame: %02x key: %02x xored: %02x", legic_prng_iv
, f
->data
, local_key
, xored
);
832 if(legic_state
== STATE_CON
) {
833 int key
= get_key_stream(2, 11); //legic_phase_drift, 11);
834 int addr
= f
->data
^ key
; addr
= addr
>> 1;
835 int data
= BigBuf
[addr
];
836 int hash
= LegicCRC(addr
, data
, 11) << 8;
837 BigBuf
[OFFSET_LOG
+legic_read_count
] = (uint8_t)addr
;
840 //Dbprintf("Data:%03.3x, key:%03.3x, addr: %03.3x, read_c:%u", f->data, key, addr, read_c);
841 legic_prng_forward(legic_reqresp_drift
);
843 frame_send_tag(hash
| data
, 12, 1);
848 legic_prng_forward(2);
849 //while(timer->TC_CV < 180);
858 int key
= get_key_stream(-1, 23); //legic_frame_drift, 23);
859 int addr
= f
->data
^ key
; addr
= addr
>> 1; addr
= addr
& 0x3ff;
860 int data
= f
->data
^ key
; data
= data
>> 11; data
= data
& 0xff;
863 legic_state
= STATE_DISCON
;
865 Dbprintf("write - addr: %x, data: %x", addr
, data
);
869 if(legic_state
!= STATE_DISCON
) {
870 Dbprintf("Unexpected: sz:%u, Data:%03.3x, State:%u, Count:%u", f
->bits
, f
->data
, legic_state
, legic_read_count
);
872 Dbprintf("IV: %03.3x", legic_prng_iv
);
873 for(i
= 0; i
<legic_read_count
; i
++) {
874 Dbprintf("Read Nb: %u, Addr: %u", i
, BigBuf
[OFFSET_LOG
+i
]);
877 for(i
= -1; i
<legic_read_count
; i
++) {
879 t
= BigBuf
[OFFSET_LOG
+256+i
*4];
880 t
|= BigBuf
[OFFSET_LOG
+256+i
*4+1] << 8;
881 t
|= BigBuf
[OFFSET_LOG
+256+i
*4+2] <<16;
882 t
|= BigBuf
[OFFSET_LOG
+256+i
*4+3] <<24;
884 Dbprintf("Cycles: %u, Frame Length: %u, Time: %u",
885 BigBuf
[OFFSET_LOG
+128+i
],
886 BigBuf
[OFFSET_LOG
+384+i
],
890 legic_state
= STATE_DISCON
;
891 legic_read_count
= 0;
897 /* Read bit by bit untill full frame is received
898 * Call to process frame end answer
900 static void emit(int bit
) {
904 frame_append_bit(¤t_frame
, 1);
907 frame_append_bit(¤t_frame
, 0);
910 if(current_frame
.bits
<= 4) {
911 frame_clean(¤t_frame
);
913 frame_handle_tag(¤t_frame
);
914 frame_clean(¤t_frame
);
921 void LegicRfSimulate(int phase
, int frame
, int reqresp
)
923 /* ADC path high-frequency peak detector, FPGA in high-frequency simulator mode,
924 * modulation mode set to 212kHz subcarrier. We are getting the incoming raw
925 * envelope waveform on DIN and should send our response on DOUT.
927 * The LEGIC RF protocol is pulse-pause-encoding from reader to card, so we'll
928 * measure the time between two rising edges on DIN, and no encoding on the
929 * subcarrier from card to reader, so we'll just shift out our verbatim data
930 * on DOUT, 1 bit is 100us. The time from reader to card frame is still unclear,
931 * seems to be 300us-ish.
934 legic_phase_drift
= phase
;
935 legic_frame_drift
= frame
;
936 legic_reqresp_drift
= reqresp
;
938 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
939 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
941 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
| FPGA_HF_SIMULATOR_MODULATE_212K
);
943 /* Bitbang the receiver */
944 AT91C_BASE_PIOA
->PIO_ODR
= GPIO_SSC_DIN
;
945 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DIN
;
948 crc_init(&legic_crc
, 4, 0x19 >> 1, 0x5, 0);
952 legic_state
= STATE_DISCON
;
955 DbpString("Starting Legic emulator, press button to end");
957 while(!BUTTON_PRESS() && !usb_poll_validate_length()) {
958 int level
= !!(AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_DIN
);
959 int time
= timer
->TC_CV
;
961 if(level
!= old_level
) {
963 timer
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
965 if (FUZZ_EQUAL(time
, RWD_TIME_1
, RWD_TIME_FUZZ
)) {
970 } else if (FUZZ_EQUAL(time
, RWD_TIME_0
, RWD_TIME_FUZZ
)) {
985 if(time
>= (RWD_TIME_1
+RWD_TIME_FUZZ
) && active
) {
991 if(time
>= (20*RWD_TIME_1
) && (timer
->TC_SR
& AT91C_TC_CLKSTA
)) {
992 timer
->TC_CCR
= AT91C_TC_CLKDIS
;
998 if ( MF_DBGLEVEL
>= 1) DbpString("Stopped");
1002 //-----------------------------------------------------------------------------
1003 //-----------------------------------------------------------------------------
1006 //-----------------------------------------------------------------------------
1007 // Code up a string of octets at layer 2 (including CRC, we don't generate
1008 // that here) so that they can be transmitted to the reader. Doesn't transmit
1009 // them yet, just leaves them ready to send in ToSend[].
1010 //-----------------------------------------------------------------------------
1011 // static void CodeLegicAsTag(const uint8_t *cmd, int len)
1017 // // Transmit a burst of ones, as the initial thing that lets the
1018 // // reader get phase sync. This (TR1) must be > 80/fs, per spec,
1019 // // but tag that I've tried (a Paypass) exceeds that by a fair bit,
1020 // // so I will too.
1021 // for(i = 0; i < 20; i++) {
1022 // ToSendStuffBit(1);
1023 // ToSendStuffBit(1);
1024 // ToSendStuffBit(1);
1025 // ToSendStuffBit(1);
1029 // for(i = 0; i < 10; i++) {
1030 // ToSendStuffBit(0);
1031 // ToSendStuffBit(0);
1032 // ToSendStuffBit(0);
1033 // ToSendStuffBit(0);
1035 // for(i = 0; i < 2; i++) {
1036 // ToSendStuffBit(1);
1037 // ToSendStuffBit(1);
1038 // ToSendStuffBit(1);
1039 // ToSendStuffBit(1);
1042 // for(i = 0; i < len; i++) {
1044 // uint8_t b = cmd[i];
1047 // ToSendStuffBit(0);
1048 // ToSendStuffBit(0);
1049 // ToSendStuffBit(0);
1050 // ToSendStuffBit(0);
1053 // for(j = 0; j < 8; j++) {
1055 // ToSendStuffBit(1);
1056 // ToSendStuffBit(1);
1057 // ToSendStuffBit(1);
1058 // ToSendStuffBit(1);
1060 // ToSendStuffBit(0);
1061 // ToSendStuffBit(0);
1062 // ToSendStuffBit(0);
1063 // ToSendStuffBit(0);
1069 // ToSendStuffBit(1);
1070 // ToSendStuffBit(1);
1071 // ToSendStuffBit(1);
1072 // ToSendStuffBit(1);
1076 // for(i = 0; i < 10; i++) {
1077 // ToSendStuffBit(0);
1078 // ToSendStuffBit(0);
1079 // ToSendStuffBit(0);
1080 // ToSendStuffBit(0);
1082 // for(i = 0; i < 2; i++) {
1083 // ToSendStuffBit(1);
1084 // ToSendStuffBit(1);
1085 // ToSendStuffBit(1);
1086 // ToSendStuffBit(1);
1089 // // Convert from last byte pos to length
1093 //-----------------------------------------------------------------------------
1094 // The software UART that receives commands from the reader, and its state
1096 //-----------------------------------------------------------------------------
1100 STATE_GOT_FALLING_EDGE_OF_SOF
,
1101 STATE_AWAITING_START_BIT
,
1102 STATE_RECEIVING_DATA
1112 /* Receive & handle a bit coming from the reader.
1114 * This function is called 4 times per bit (every 2 subcarrier cycles).
1115 * Subcarrier frequency fs is 212kHz, 1/fs = 4,72us, i.e. function is called every 9,44us
1118 * LED A -> ON once we have received the SOF and are expecting the rest.
1119 * LED A -> OFF once we have received EOF or are in error state or unsynced
1121 * Returns: true if we received a EOF
1122 * false if we are still waiting for some more
1124 // static RAMFUNC int HandleLegicUartBit(uint8_t bit)
1126 // switch(Uart.state) {
1127 // case STATE_UNSYNCD:
1129 // // we went low, so this could be the beginning of an SOF
1130 // Uart.state = STATE_GOT_FALLING_EDGE_OF_SOF;
1136 // case STATE_GOT_FALLING_EDGE_OF_SOF:
1138 // if(Uart.posCnt == 2) { // sample every 4 1/fs in the middle of a bit
1140 // if(Uart.bitCnt > 9) {
1141 // // we've seen enough consecutive
1142 // // zeros that it's a valid SOF
1144 // Uart.byteCnt = 0;
1145 // Uart.state = STATE_AWAITING_START_BIT;
1146 // LED_A_ON(); // Indicate we got a valid SOF
1148 // // didn't stay down long enough
1149 // // before going high, error
1150 // Uart.state = STATE_UNSYNCD;
1153 // // do nothing, keep waiting
1157 // if(Uart.posCnt >= 4) Uart.posCnt = 0;
1158 // if(Uart.bitCnt > 12) {
1159 // // Give up if we see too many zeros without
1162 // Uart.state = STATE_UNSYNCD;
1166 // case STATE_AWAITING_START_BIT:
1169 // if(Uart.posCnt > 50/2) { // max 57us between characters = 49 1/fs, max 3 etus after low phase of SOF = 24 1/fs
1170 // // stayed high for too long between
1171 // // characters, error
1172 // Uart.state = STATE_UNSYNCD;
1175 // // falling edge, this starts the data byte
1178 // Uart.shiftReg = 0;
1179 // Uart.state = STATE_RECEIVING_DATA;
1183 // case STATE_RECEIVING_DATA:
1185 // if(Uart.posCnt == 2) {
1186 // // time to sample a bit
1187 // Uart.shiftReg >>= 1;
1189 // Uart.shiftReg |= 0x200;
1193 // if(Uart.posCnt >= 4) {
1196 // if(Uart.bitCnt == 10) {
1197 // if((Uart.shiftReg & 0x200) && !(Uart.shiftReg & 0x001))
1199 // // this is a data byte, with correct
1200 // // start and stop bits
1201 // Uart.output[Uart.byteCnt] = (Uart.shiftReg >> 1) & 0xff;
1204 // if(Uart.byteCnt >= Uart.byteCntMax) {
1205 // // Buffer overflowed, give up
1207 // Uart.state = STATE_UNSYNCD;
1209 // // so get the next byte now
1211 // Uart.state = STATE_AWAITING_START_BIT;
1213 // } else if (Uart.shiftReg == 0x000) {
1214 // // this is an EOF byte
1215 // LED_A_OFF(); // Finished receiving
1216 // Uart.state = STATE_UNSYNCD;
1217 // if (Uart.byteCnt != 0) {
1221 // // this is an error
1223 // Uart.state = STATE_UNSYNCD;
1230 // Uart.state = STATE_UNSYNCD;
1238 static void UartReset() {
1239 Uart
.byteCntMax
= 3;
1240 Uart
.state
= STATE_UNSYNCD
;
1244 memset(Uart
.output
, 0x00, 3);
1247 // static void UartInit(uint8_t *data) {
1248 // Uart.output = data;
1252 //=============================================================================
1253 // An LEGIC reader. We take layer two commands, code them
1254 // appropriately, and then send them to the tag. We then listen for the
1255 // tag's response, which we leave in the buffer to be demodulated on the
1257 //=============================================================================
1262 DEMOD_PHASE_REF_TRAINING
,
1263 DEMOD_AWAITING_FALLING_EDGE_OF_SOF
,
1264 DEMOD_GOT_FALLING_EDGE_OF_SOF
,
1265 DEMOD_AWAITING_START_BIT
,
1266 DEMOD_RECEIVING_DATA
1279 * Handles reception of a bit from the tag
1281 * This function is called 2 times per bit (every 4 subcarrier cycles).
1282 * Subcarrier frequency fs is 212kHz, 1/fs = 4,72us, i.e. function is called every 9,44us
1285 * LED C -> ON once we have received the SOF and are expecting the rest.
1286 * LED C -> OFF once we have received EOF or are unsynced
1288 * Returns: true if we received a EOF
1289 * false if we are still waiting for some more
1293 #ifndef SUBCARRIER_DETECT_THRESHOLD
1294 # define SUBCARRIER_DETECT_THRESHOLD 8
1297 // Subcarrier amplitude v = sqrt(ci^2 + cq^2), approximated here by max(abs(ci),abs(cq)) + 1/2*min(abs(ci),abs(cq)))
1298 #ifndef CHECK_FOR_SUBCARRIER
1299 # define CHECK_FOR_SUBCARRIER() { v = MAX(ai, aq) + MIN(halfci, halfcq); }
1302 // The soft decision on the bit uses an estimate of just the
1303 // quadrant of the reference angle, not the exact angle.
1304 // Subcarrier amplitude v = sqrt(ci^2 + cq^2), approximated here by max(abs(ci),abs(cq)) + 1/2*min(abs(ci),abs(cq)))
1305 #define MAKE_SOFT_DECISION() { \
1306 if(Demod.sumI > 0) \
1311 if(Demod.sumQ > 0) \
1318 static RAMFUNC
int HandleLegicSamplesDemod(int ci
, int cq
)
1323 int halfci
= (ai
>> 1);
1324 int halfcq
= (aq
>> 1);
1326 switch(Demod
.state
) {
1329 CHECK_FOR_SUBCARRIER()
1331 if(v
> SUBCARRIER_DETECT_THRESHOLD
) { // subcarrier detected
1332 Demod
.state
= DEMOD_PHASE_REF_TRAINING
;
1339 case DEMOD_PHASE_REF_TRAINING
:
1340 if(Demod
.posCount
< 8) {
1342 CHECK_FOR_SUBCARRIER()
1344 if (v
> SUBCARRIER_DETECT_THRESHOLD
) {
1345 // set the reference phase (will code a logic '1') by averaging over 32 1/fs.
1346 // note: synchronization time > 80 1/fs
1352 Demod
.state
= DEMOD_UNSYNCD
;
1355 Demod
.state
= DEMOD_AWAITING_FALLING_EDGE_OF_SOF
;
1359 case DEMOD_AWAITING_FALLING_EDGE_OF_SOF
:
1361 MAKE_SOFT_DECISION()
1363 //Dbprintf("ICE: %d %d %d %d %d", v, Demod.sumI, Demod.sumQ, ci, cq );
1364 // logic '0' detected
1367 Demod
.state
= DEMOD_GOT_FALLING_EDGE_OF_SOF
;
1369 // start of SOF sequence
1372 // maximum length of TR1 = 200 1/fs
1373 if(Demod
.posCount
> 25*2) Demod
.state
= DEMOD_UNSYNCD
;
1378 case DEMOD_GOT_FALLING_EDGE_OF_SOF
:
1381 MAKE_SOFT_DECISION()
1384 // low phase of SOF too short (< 9 etu). Note: spec is >= 10, but FPGA tends to "smear" edges
1385 if(Demod
.posCount
< 10*2) {
1386 Demod
.state
= DEMOD_UNSYNCD
;
1388 LED_C_ON(); // Got SOF
1389 Demod
.state
= DEMOD_AWAITING_START_BIT
;
1394 // low phase of SOF too long (> 12 etu)
1395 if(Demod
.posCount
> 13*2) {
1396 Demod
.state
= DEMOD_UNSYNCD
;
1402 case DEMOD_AWAITING_START_BIT
:
1405 MAKE_SOFT_DECISION()
1408 // max 19us between characters = 16 1/fs, max 3 etu after low phase of SOF = 24 1/fs
1409 if(Demod
.posCount
> 3*2) {
1410 Demod
.state
= DEMOD_UNSYNCD
;
1414 // start bit detected
1416 Demod
.posCount
= 1; // this was the first half
1419 Demod
.state
= DEMOD_RECEIVING_DATA
;
1423 case DEMOD_RECEIVING_DATA
:
1425 MAKE_SOFT_DECISION()
1427 if(Demod
.posCount
== 0) {
1428 // first half of bit
1432 // second half of bit
1434 Demod
.shiftReg
>>= 1;
1436 if(Demod
.thisBit
> 0)
1437 Demod
.shiftReg
|= 0x200;
1441 if(Demod
.bitCount
== 10) {
1443 uint16_t s
= Demod
.shiftReg
;
1445 if((s
& 0x200) && !(s
& 0x001)) {
1446 // stop bit == '1', start bit == '0'
1447 uint8_t b
= (s
>> 1);
1448 Demod
.output
[Demod
.len
] = b
;
1450 Demod
.state
= DEMOD_AWAITING_START_BIT
;
1452 Demod
.state
= DEMOD_UNSYNCD
;
1456 // This is EOF (start, stop and all data bits == '0'
1466 Demod
.state
= DEMOD_UNSYNCD
;
1473 // Clear out the state of the "UART" that receives from the tag.
1474 static void DemodReset() {
1476 Demod
.state
= DEMOD_UNSYNCD
;
1483 memset(Demod
.output
, 0x00, 3);
1486 static void DemodInit(uint8_t *data
) {
1487 Demod
.output
= data
;
1492 * Demodulate the samples we received from the tag, also log to tracebuffer
1493 * quiet: set to 'TRUE' to disable debug output
1495 #define LEGIC_DMA_BUFFER_SIZE 256
1496 static void GetSamplesForLegicDemod(int n
, bool quiet
)
1499 bool gotFrame
= FALSE
;
1500 int lastRxCounter
= LEGIC_DMA_BUFFER_SIZE
;
1501 int ci
, cq
, samples
= 0;
1505 // And put the FPGA in the appropriate mode
1506 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
| FPGA_HF_READER_RX_XCORR_QUARTER_FREQ
);
1508 // The response (tag -> reader) that we're receiving.
1509 // Set up the demodulator for tag -> reader responses.
1510 DemodInit(BigBuf_malloc(MAX_FRAME_SIZE
));
1512 // The DMA buffer, used to stream samples from the FPGA
1513 int8_t *dmaBuf
= (int8_t*) BigBuf_malloc(LEGIC_DMA_BUFFER_SIZE
);
1514 int8_t *upTo
= dmaBuf
;
1516 // Setup and start DMA.
1517 if ( !FpgaSetupSscDma((uint8_t*) dmaBuf
, LEGIC_DMA_BUFFER_SIZE
) ){
1518 if (MF_DBGLEVEL
> 1) Dbprintf("FpgaSetupSscDma failed. Exiting");
1522 // Signal field is ON with the appropriate LED:
1525 int behindBy
= lastRxCounter
- AT91C_BASE_PDC_SSC
->PDC_RCR
;
1526 if(behindBy
> max
) max
= behindBy
;
1528 while(((lastRxCounter
-AT91C_BASE_PDC_SSC
->PDC_RCR
) & (LEGIC_DMA_BUFFER_SIZE
-1)) > 2) {
1532 if(upTo
>= dmaBuf
+ LEGIC_DMA_BUFFER_SIZE
) {
1534 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (uint32_t) upTo
;
1535 AT91C_BASE_PDC_SSC
->PDC_RNCR
= LEGIC_DMA_BUFFER_SIZE
;
1538 if(lastRxCounter
<= 0)
1539 lastRxCounter
= LEGIC_DMA_BUFFER_SIZE
;
1543 gotFrame
= HandleLegicSamplesDemod(ci
, cq
);
1548 if(samples
> n
|| gotFrame
)
1552 FpgaDisableSscDma();
1554 if (!quiet
&& Demod
.len
== 0) {
1555 Dbprintf("max behindby = %d, samples = %d, gotFrame = %d, Demod.len = %d, Demod.sumI = %d, Demod.sumQ = %d",
1566 if (Demod
.len
> 0) {
1567 uint8_t parity
[MAX_PARITY_SIZE
] = {0x00};
1568 LogTrace(Demod
.output
, Demod
.len
, 0, 0, parity
, FALSE
);
1571 //-----------------------------------------------------------------------------
1572 // Transmit the command (to the tag) that was placed in ToSend[].
1573 //-----------------------------------------------------------------------------
1574 static void TransmitForLegic(void)
1580 while(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
))
1581 AT91C_BASE_SSC
->SSC_THR
= 0xff;
1583 // Signal field is ON with the appropriate Red LED
1586 // Signal we are transmitting with the Green LED
1588 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX
| FPGA_HF_READER_TX_SHALLOW_MOD
);
1590 for(c
= 0; c
< 10;) {
1591 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1592 AT91C_BASE_SSC
->SSC_THR
= 0xff;
1595 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1596 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1604 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1605 AT91C_BASE_SSC
->SSC_THR
= ToSend
[c
];
1606 legic_prng_forward(1); // forward the lfsr
1608 if(c
>= ToSendMax
) {
1612 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1613 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1622 //-----------------------------------------------------------------------------
1623 // Code a layer 2 command (string of octets, including CRC) into ToSend[],
1624 // so that it is ready to transmit to the tag using TransmitForLegic().
1625 //-----------------------------------------------------------------------------
1626 static void CodeLegicBitsAsReader(const uint8_t *cmd
, uint8_t cmdlen
, int bits
)
1634 for(i
= 0; i
< 7; i
++)
1638 for(i
= 0; i
< cmdlen
; i
++) {
1644 for(j
= 0; j
< bits
; j
++) {
1654 // Convert from last character reference to length
1659 Convenience function to encode, transmit and trace Legic comms
1661 static void CodeAndTransmitLegicAsReader(const uint8_t *cmd
, uint8_t cmdlen
, int bits
)
1663 CodeLegicBitsAsReader(cmd
, cmdlen
, bits
);
1666 uint8_t parity
[1] = {0x00};
1667 LogTrace(cmd
, cmdlen
, 0, 0, parity
, TRUE
);
1671 int ice_legic_select_card()
1673 //int cmd_size=0, card_size=0;
1674 uint8_t wakeup
[] = { 0x7F };
1675 uint8_t getid
[] = {0x19};
1677 //legic_prng_init(SESSION_IV);
1679 // first, wake up the tag, 7bits
1680 CodeAndTransmitLegicAsReader(wakeup
, sizeof(wakeup
), 7);
1682 GetSamplesForLegicDemod(1000, TRUE
);
1684 //frame_receiveAsReader(¤t_frame, 6, 1);
1686 legic_prng_forward(1); /* we wait anyways */
1688 //while(timer->TC_CV < 387) ; /* ~ 258us */
1689 //frame_sendAsReader(0x19, 6);
1690 CodeAndTransmitLegicAsReader(getid
, sizeof(getid
), 8);
1691 GetSamplesForLegicDemod(1000, TRUE
);
1693 //if (Demod.len < 14) return 2;
1694 Dbprintf("CARD TYPE: %02x LEN: %d", Demod
.output
[0], Demod
.len
);
1696 switch(Demod
.output
[0]) {
1698 DbpString("MIM 256 card found");
1703 DbpString("MIM 1024 card found");
1705 // card_size = 1024;
1712 // bytes = card_size;
1714 // if(bytes + offset >= card_size)
1715 // bytes = card_size - offset;
1717 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1722 // Set up LEGIC communication
1723 void ice_legic_setup() {
1726 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1727 BigBuf_free(); BigBuf_Clear_ext(false);
1733 // Set up the synchronous serial port
1736 // connect Demodulated Signal to ADC:
1737 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1739 // Signal field is on with the appropriate LED
1741 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX
| FPGA_HF_READER_TX_SHALLOW_MOD
);
1744 //StartCountSspClk();
1747 crc_init(&legic_crc
, 4, 0x19 >> 1, 0x5, 0);