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 // testing calculating in ticks instead of (us) microseconds.
67 #define RWD_TIME_1 120 // READER_TIME_PAUSE off, 80us on = 100us 80 * 1.5 == 120ticks
68 #define RWD_TIME_0 60 // READER_TIME_PAUSE off, 40us on = 60us 40 * 1.5 == 60ticks
69 #define RWD_TIME_PAUSE 30 // 20us == 20 * 1.5 == 30ticks */
70 #define TAG_BIT_PERIOD 150 // 100us == 100 * 1.5 == 150ticks
72 #define RWD_TIME_FUZZ 20 // rather generous 13us, since the peak detector + hysteresis fuzz quite a bit
73 #define TAG_TIME_WAIT 495 // 330us from READER frame end to TAG frame start. 330 * 1.5 == 495
75 #define SIM_DIVISOR 586 /* prng_time/SIM_DIVISOR count prng needs to be forwared */
76 #define SIM_SHIFT 900 /* prng_time+SIM_SHIFT shift of delayed start */
78 #define OFFSET_LOG 1024
80 #define FUZZ_EQUAL(value, target, fuzz) ((value) > ((target)-(fuzz)) && (value) < ((target)+(fuzz)))
83 //#define LOW(x) AT91C_BASE_PIOA->PIO_CODR = (x)
84 # define SHORT_COIL LOW(GPIO_SSC_DOUT);
87 //#define HIGH(x) AT91C_BASE_PIOA->PIO_SODR = (x)
88 # define OPEN_COIL HIGH(GPIO_SSC_DOUT);
91 uint32_t stop_send_frame_us
= 0;
93 // ~ 258us + 100us*delay
94 #define WAIT(delay) SpinDelayCountUs((delay));
95 #define COIL_PULSE(x) { \
97 Wait(RWD_TIME_PAUSE); \
101 #define GET_COUNT_US GetCountUS()
104 // ToDo: define a meaningful maximum size for auth_table. The bigger this is, the lower will be the available memory for traces.
105 // Historically it used to be FREE_BUFFER_SIZE, which was 2744.
106 #define LEGIC_CARD_MEMSIZE 1024
107 static uint8_t* cardmem
;
109 static void Wait(uint32_t time
){
110 if ( time
== 0 ) return;
111 time
+= AT91C_BASE_TC0
->TC_CV
;
112 while(AT91C_BASE_TC0
->TC_CV
< time
);
114 // Starts Clock and waits until its reset
115 static void Reset(AT91PS_TC clock
){
116 clock
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
117 while(clock
->TC_CV
> 1) ;
120 // Starts Clock and waits until its reset
121 static void ResetClock(void){
125 static void frame_append_bit(struct legic_frame
* const f
, int bit
) {
126 // Overflow, won't happen
127 if (f
->bits
>= 31) return;
129 f
->data
|= (bit
<< f
->bits
);
133 static void frame_clean(struct legic_frame
* const f
) {
138 // Prng works when waiting in 99.1us cycles.
139 // and while sending/receiving in bit frames (100, 60)
140 /*static void CalibratePrng( uint32_t time){
141 // Calculate Cycles based on timer 100us
142 uint32_t i = (time - stop_send_frame_us) / 100 ;
144 // substract cycles of finished frames
145 int k = i - legic_prng_count()+1;
147 // substract current frame length, rewind to beginning
149 legic_prng_forward(k);
153 /* Generate Keystream */
154 static uint32_t get_key_stream(int skip
, int count
)
159 // Use int to enlarge timer tc to 32bit
160 legic_prng_bc
+= prng_timer
->TC_CV
;
162 // reset the prng timer.
165 /* If skip == -1, forward prng time based */
167 i
= (legic_prng_bc
+ SIM_SHIFT
)/SIM_DIVISOR
; /* Calculate Cycles based on timer */
168 i
-= legic_prng_count(); /* substract cycles of finished frames */
169 i
-= count
; /* substract current frame length, rewind to beginning */
170 legic_prng_forward(i
);
172 legic_prng_forward(skip
);
175 i
= (count
== 6) ? -1 : legic_read_count
;
177 /* Write Time Data into LOG */
178 // uint8_t *BigBuf = BigBuf_get_addr();
179 // BigBuf[OFFSET_LOG+128+i] = legic_prng_count();
180 // BigBuf[OFFSET_LOG+256+i*4] = (legic_prng_bc >> 0) & 0xff;
181 // BigBuf[OFFSET_LOG+256+i*4+1] = (legic_prng_bc >> 8) & 0xff;
182 // BigBuf[OFFSET_LOG+256+i*4+2] = (legic_prng_bc >>16) & 0xff;
183 // BigBuf[OFFSET_LOG+256+i*4+3] = (legic_prng_bc >>24) & 0xff;
184 // BigBuf[OFFSET_LOG+384+i] = count;
186 /* Generate KeyStream */
187 for(i
=0; i
<count
; i
++) {
188 key
|= legic_prng_get_bit() << i
;
189 legic_prng_forward(1);
194 /* Send a frame in tag mode, the FPGA must have been set up by
197 static void frame_send_tag(uint16_t response
, uint8_t bits
, uint8_t crypt
) {
198 /* Bitbang the response */
200 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
201 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
203 /* Use time to crypt frame */
205 legic_prng_forward(2); /* TAG_TIME_WAIT -> shift by 2 */
206 response
^= legic_prng_get_bits(bits
);
209 /* Wait for the frame start */
210 WAIT( TAG_TIME_WAIT
)
213 for(int i
= 0; i
< bits
; i
++) {
228 /* Send a frame in reader mode, the FPGA must have been set up by
231 static void frame_sendAsReader(uint32_t data
, uint8_t bits
){
233 uint32_t starttime
= AT91C_BASE_TC0
->TC_CV
;
234 uint32_t send
= data
;
235 uint8_t prng1
= legic_prng_count() ;
237 uint16_t lfsr
= legic_prng_get_bits(bits
);
239 // xor the lsfr onto data.
242 for (; mask
< BITMASK(bits
); mask
<<= 1) {
244 COIL_PULSE(RWD_TIME_1
);
246 COIL_PULSE(RWD_TIME_0
);
250 // Final pause to mark the end of the frame
251 // tempo = AT91C_BASE_TC0->TC_CV + RWD_TIME_PAUSE;
253 // while(AT91C_BASE_TC0->TC_CV < tempo);
257 stop_send_frame_us
= AT91C_BASE_TC0
->TC_CV
;
258 uint8_t cmdbytes
[] = {
267 LogTrace(cmdbytes
, sizeof(cmdbytes
), starttime
, stop_send_frame_us
, NULL
, TRUE
);
270 /* Receive a frame from the card in reader emulation mode, the FPGA and
271 * timer must have been set up by LegicRfReader and frame_sendAsReader.
273 * The LEGIC RF protocol from card to reader does not include explicit
274 * frame start/stop information or length information. The reader must
275 * know beforehand how many bits it wants to receive. (Notably: a card
276 * sending a stream of 0-bits is indistinguishable from no card present.)
278 * Receive methodology: There is a fancy correlator in hi_read_rx_xcorr, but
279 * I'm not smart enough to use it. Instead I have patched hi_read_tx to output
280 * the ADC signal with hysteresis on SSP_DIN. Bit-bang that signal and look
281 * for edges. Count the edges in each bit interval. If they are approximately
282 * 0 this was a 0-bit, if they are approximately equal to the number of edges
283 * expected for a 212kHz subcarrier, this was a 1-bit. For timing we use the
284 * timer that's still running from frame_sendAsReader in order to get a synchronization
285 * with the frame that we just sent.
287 * FIXME: Because we're relying on the hysteresis to just do the right thing
288 * the range is severely reduced (and you'll probably also need a good antenna).
289 * So this should be fixed some time in the future for a proper receiver.
291 static void frame_receiveAsReader(struct legic_frame
* const f
, uint8_t bits
, uint8_t crypt
) {
293 uint32_t starttime
= AT91C_BASE_TC0
->TC_CV
;
297 uint8_t i
= 0, edges
= 0;
299 uint32_t the_bit
= 1, next_bit_at
= 0, data
;
300 int old_level
= 0, level
= 0;
302 if(bits
> 32) bits
= 32;
304 AT91C_BASE_PIOA
->PIO_ODR
= GPIO_SSC_DIN
;
305 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DIN
;
307 // calibrate the prng.
308 legic_prng_forward(2);
309 //CalibratePrng( starttime );
311 // precompute the cipher
312 uint8_t prng_before
= legic_prng_count() ;
315 lsfr
= legic_prng_get_bits(bits
);
319 //FIXED time between sending frame and now listening frame. 330us
320 uint32_t icetime
= TAG_TIME_WAIT
- ( AT91C_BASE_TC0
->TC_CV
- stop_send_frame_us
);
321 while ( AT91C_BASE_TC0
->TC_CV
!= icetime
);
323 next_bit_at
= AT91C_BASE_TC0
->TC_CV
+ TAG_BIT_PERIOD
;
325 for( i
= 0; i
< bits
; i
++) {
327 while ( AT91C_BASE_TC0
->TC_CV
< next_bit_at
) {
329 level
= (AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_DIN
);
331 if (level
!= old_level
)
336 next_bit_at
+= TAG_BIT_PERIOD
;
338 // We expect 42 edges == ONE
339 if(edges
> 20 && edges
< 60)
350 stop_send_frame_us
= AT91C_BASE_TC0
->TC_CV
;
352 uint8_t cmdbytes
[] = {
361 (icetime
>> 8) & 0xFF
363 LogTrace(cmdbytes
, sizeof(cmdbytes
), starttime
, stop_send_frame_us
, NULL
, FALSE
);
367 // Setup pm3 as a Legic Reader
368 static uint32_t perform_setup_phase_rwd(uint8_t iv
) {
370 // Switch on carrier and let the tag charge for 1ms
380 frame_sendAsReader(iv
, 7);
382 // Now both tag and reader has same IV. Prng can start.
385 frame_receiveAsReader(¤t_frame
, 6, 1);
387 // fixed delay before sending ack.
388 WAIT(TAG_BIT_PERIOD
);
390 // Send obsfuscated acknowledgment frame.
391 // 0x19 = 0x18 MIM22, 0x01 LSB READCMD
392 // 0x39 = 0x38 MIM256, MIM1024 0x01 LSB READCMD
393 switch ( current_frame
.data
) {
395 frame_sendAsReader(0x19, 6);
399 frame_sendAsReader(0x39, 6);
404 return current_frame
.data
;
406 // End of Setup Phase.
409 static void LegicCommonInit(void) {
410 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
411 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX
);
412 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
415 /* Bitbang the transmitter */
417 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
418 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
420 // reserve a cardmem, meaning we can use the tracelog function in bigbuff easier.
421 cardmem
= BigBuf_malloc(LEGIC_CARD_MEMSIZE
);
422 memset(cardmem
, 0x00, LEGIC_CARD_MEMSIZE
);
427 crc_init(&legic_crc
, 4, 0x19 >> 1, 0x5, 0);
432 /* Switch off carrier, make sure tag is reset */
433 static void switch_off_tag_rwd(void) {
440 // calculate crc4 for a legic READ command
441 // 5,8,10 address size.
442 static uint32_t LegicCRC(uint16_t byte_index
, uint8_t value
, uint8_t cmd_sz
) {
443 crc_clear(&legic_crc
);
444 uint32_t temp
= (value
<< cmd_sz
) | (byte_index
<< 1) | LEGIC_READ
;
445 crc_update(&legic_crc
, temp
, cmd_sz
+ 8 );
446 // crc_update(&legic_crc, LEGIC_READ, 1);
447 // crc_update(&legic_crc, byte_index, cmd_sz-1);
448 // crc_update(&legic_crc, value, 8);
449 return crc_finish(&legic_crc
);
452 int legic_read_byte(int byte_index
, int cmd_sz
) {
454 uint8_t byte
= 0, crc
= 0;
455 uint32_t calcCrc
= 0;
456 uint32_t cmd
= (byte_index
<< 1) | LEGIC_READ
;
458 legic_prng_forward(3);
461 frame_sendAsReader(cmd
, cmd_sz
);
463 frame_receiveAsReader(¤t_frame
, 12, 1);
465 byte
= current_frame
.data
& 0xFF;
467 calcCrc
= LegicCRC(byte_index
, byte
, cmd_sz
);
468 crc
= (current_frame
.data
>> 8);
470 if( calcCrc
!= crc
) {
471 Dbprintf("!!! crc mismatch: expected %x but got %x !!!", calcCrc
, crc
);
479 * - assemble a write_cmd_frame with crc and send it
480 * - wait until the tag sends back an ACK ('1' bit unencrypted)
481 * - forward the prng based on the timing
483 //int legic_write_byte(int byte, int addr, int addr_sz, int PrngCorrection) {
484 int legic_write_byte(int byte
, int addr
, int addr_sz
) {
486 //do not write UID, CRC at offset 0-4.
487 if(addr
<= 0x04) return 0;
490 crc_clear(&legic_crc
);
491 crc_update(&legic_crc
, 0, 1); /* CMD_WRITE */
492 crc_update(&legic_crc
, addr
, addr_sz
);
493 crc_update(&legic_crc
, byte
, 8);
494 uint32_t crc
= crc_finish(&legic_crc
);
496 // send write command
497 uint32_t cmd
= ((crc
<<(addr_sz
+1+8)) //CRC
498 |(byte
<<(addr_sz
+1)) //Data
499 |(addr
<<1) //Address
500 |(0x00 <<0)); //CMD = W
501 uint32_t cmd_sz
= addr_sz
+1+8+4; //crc+data+cmd
503 legic_prng_forward(2); /* we wait anyways */
505 while(timer
->TC_CV
< 387) ; /* ~ 258us */
507 frame_sendAsReader(cmd
, cmd_sz
);
509 // wllm-rbnt doesnt have these
510 // AT91C_BASE_PIOA->PIO_ODR = GPIO_SSC_DIN;
511 // AT91C_BASE_PIOA->PIO_PER = GPIO_SSC_DIN;
514 int t
, old_level
= 0, edges
= 0;
517 while(timer
->TC_CV
< 387) ; /* ~ 258us */
519 for( t
= 0; t
< 80; t
++) {
521 next_bit_at
+= TAG_BIT_PERIOD
;
522 while(timer
->TC_CV
< next_bit_at
) {
523 int level
= (AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_DIN
);
524 if(level
!= old_level
) {
529 if(edges
> 20 && edges
< 60) { /* expected are 42 edges */
530 int t
= timer
->TC_CV
;
531 int c
= t
/ TAG_BIT_PERIOD
;
534 legic_prng_forward(c
);
543 int LegicRfReader(int offset
, int bytes
, int iv
) {
545 int byte_index
= 0, cmd_sz
= 0, card_sz
= 0;
547 if ( MF_DBGLEVEL
>= 2) {
548 Dbprintf("setting up legic card, IV = %x", iv
);
549 Dbprintf("ONE %d ZERO %d PAUSE %d", RWD_TIME_1
, RWD_TIME_0
, RWD_TIME_PAUSE
);
550 Dbprintf("TAG BIT PERIOD %d FUZZ %d TAG WAIT TIME %d", TAG_BIT_PERIOD
, RWD_TIME_FUZZ
, TAG_TIME_WAIT
);
553 // for ( uint8_t i =0; i<255; ++i){
554 // uint32_t t1 = GET_COUNT_US;
556 // t1 = GET_COUNT_US - t1;
557 // Dbprintf("WAIT(%d) == %u | %u | diff %d", i, t1-i );
563 uint32_t tag_type
= perform_setup_phase_rwd(iv
);
565 //we lose to mutch time with dprintf
566 switch_off_tag_rwd();
570 if ( MF_DBGLEVEL
>= 2) DbpString("MIM22 card found, reading card ...");
575 if ( MF_DBGLEVEL
>= 2) DbpString("MIM256 card found, reading card ...");
580 if ( MF_DBGLEVEL
>= 2) DbpString("MIM1024 card found, reading card ...");
585 if ( MF_DBGLEVEL
>= 1) Dbprintf("Unknown card format: %x",tag_type
);
591 if(bytes
+offset
>= card_sz
)
592 bytes
= card_sz
- offset
;
594 // Start setup and read bytes.
595 perform_setup_phase_rwd(iv
);
598 while (byte_index
< bytes
) {
599 int r
= legic_read_byte(byte_index
+offset
, cmd_sz
);
601 if (r
== -1 || BUTTON_PRESS()) {
602 switch_off_tag_rwd();
604 if ( MF_DBGLEVEL
>= 2) DbpString("operation aborted");
605 cmd_send(CMD_ACK
,0,0,0,0,0);
608 cardmem
[byte_index
] = r
;
613 switch_off_tag_rwd();
615 uint8_t len
= (bytes
& 0x3FF);
616 cmd_send(CMD_ACK
,1,len
,0,0,0);
620 /*int _LegicRfWriter(int offset, int bytes, int addr_sz, uint8_t *BigBuf, int RoundBruteforceValue) {
624 perform_setup_phase_rwd(iv);
625 //legic_prng_forward(2);
626 while(byte_index < bytes) {
629 //check if the DCF should be changed
630 if ( (offset == 0x05) && (bytes == 0x02) ) {
631 //write DCF in reverse order (addr 0x06 before 0x05)
632 r = legic_write_byte(BigBuf[(0x06-byte_index)], (0x06-byte_index), addr_sz, RoundBruteforceValue);
633 //legic_prng_forward(1);
636 r = legic_write_byte(BigBuf[(0x06-byte_index)], (0x06-byte_index), addr_sz, RoundBruteforceValue);
638 //legic_prng_forward(1);
641 r = legic_write_byte(BigBuf[byte_index+offset], byte_index+offset, addr_sz, RoundBruteforceValue);
643 if((r != 0) || BUTTON_PRESS()) {
644 Dbprintf("operation aborted @ 0x%03.3x", byte_index);
645 switch_off_tag_rwd();
653 if(byte_index & 0x10) LED_C_ON(); else LED_C_OFF();
657 DbpString("write successful");
661 void LegicRfWriter(int offset
, int bytes
, int iv
) {
663 int byte_index
= 0, addr_sz
= 0;
667 if ( MF_DBGLEVEL
>= 2) DbpString("setting up legic card");
669 uint32_t tag_type
= perform_setup_phase_rwd(iv
);
671 switch_off_tag_rwd();
675 if(offset
+bytes
> 22) {
676 Dbprintf("Error: can not write to 0x%03.3x on MIM22", offset
+bytes
);
680 if ( MF_DBGLEVEL
>= 2) Dbprintf("MIM22 card found, writing 0x%02.2x - 0x%02.2x ...", offset
, offset
+bytes
);
683 if(offset
+bytes
> 0x100) {
684 Dbprintf("Error: can not write to 0x%03.3x on MIM256", offset
+bytes
);
688 if ( MF_DBGLEVEL
>= 2) Dbprintf("MIM256 card found, writing 0x%02.2x - 0x%02.2x ...", offset
, offset
+bytes
);
691 if(offset
+bytes
> 0x400) {
692 Dbprintf("Error: can not write to 0x%03.3x on MIM1024", offset
+bytes
);
696 if ( MF_DBGLEVEL
>= 2) Dbprintf("MIM1024 card found, writing 0x%03.3x - 0x%03.3x ...", offset
, offset
+bytes
);
699 Dbprintf("No or unknown card found, aborting");
704 perform_setup_phase_rwd(iv
);
705 while(byte_index
< bytes
) {
708 //check if the DCF should be changed
709 if ( ((byte_index
+offset
) == 0x05) && (bytes
>= 0x02) ) {
710 //write DCF in reverse order (addr 0x06 before 0x05)
711 r
= legic_write_byte(cardmem
[(0x06-byte_index
)], (0x06-byte_index
), addr_sz
);
713 // write second byte on success...
716 r
= legic_write_byte(cardmem
[(0x06-byte_index
)], (0x06-byte_index
), addr_sz
);
720 r
= legic_write_byte(cardmem
[byte_index
+offset
], byte_index
+offset
, addr_sz
);
723 if((r
!= 0) || BUTTON_PRESS()) {
724 Dbprintf("operation aborted @ 0x%03.3x", byte_index
);
725 switch_off_tag_rwd();
734 if ( MF_DBGLEVEL
>= 1) DbpString("write successful");
737 void LegicRfRawWriter(int address
, int byte
, int iv
) {
739 int byte_index
= 0, addr_sz
= 0;
743 if ( MF_DBGLEVEL
>= 2) DbpString("setting up legic card");
745 uint32_t tag_type
= perform_setup_phase_rwd(iv
);
747 switch_off_tag_rwd();
752 Dbprintf("Error: can not write to 0x%03.3x on MIM22", address
);
756 if ( MF_DBGLEVEL
>= 2) Dbprintf("MIM22 card found, writing at addr 0x%02.2x - value 0x%02.2x ...", address
, byte
);
759 if(address
> 0x100) {
760 Dbprintf("Error: can not write to 0x%03.3x on MIM256", address
);
764 if ( MF_DBGLEVEL
>= 2) Dbprintf("MIM256 card found, writing at addr 0x%02.2x - value 0x%02.2x ...", address
, byte
);
767 if(address
> 0x400) {
768 Dbprintf("Error: can not write to 0x%03.3x on MIM1024", address
);
772 if ( MF_DBGLEVEL
>= 2) Dbprintf("MIM1024 card found, writing at addr 0x%03.3x - value 0x%03.3x ...", address
, byte
);
775 Dbprintf("No or unknown card found, aborting");
779 Dbprintf("integer value: %d address: %d addr_sz: %d", byte
, address
, addr_sz
);
782 perform_setup_phase_rwd(iv
);
783 //legic_prng_forward(2);
785 int r
= legic_write_byte(byte
, address
, addr_sz
);
787 if((r
!= 0) || BUTTON_PRESS()) {
788 Dbprintf("operation aborted @ 0x%03.3x (%1d)", byte_index
, r
);
789 switch_off_tag_rwd();
795 if ( MF_DBGLEVEL
>= 1) DbpString("write successful");
798 /* Handle (whether to respond) a frame in tag mode
799 * Only called when simulating a tag.
801 static void frame_handle_tag(struct legic_frame
const * const f
)
803 uint8_t *BigBuf
= BigBuf_get_addr();
805 /* First Part of Handshake (IV) */
813 legic_prng_init(f
->data
);
814 frame_send_tag(0x3d, 6, 1); /* 0x3d^0x26 = 0x1B */
815 legic_state
= STATE_IV
;
816 legic_read_count
= 0;
818 legic_prng_iv
= f
->data
;
823 //while(timer->TC_CV < 280);
829 if(legic_state
== STATE_IV
) {
830 int local_key
= get_key_stream(3, 6);
831 int xored
= 0x39 ^ local_key
;
832 if((f
->bits
== 6) && (f
->data
== xored
)) {
833 legic_state
= STATE_CON
;
838 //while(timer->TC_CV < 200);
843 legic_state
= STATE_DISCON
;
845 Dbprintf("iv: %02x frame: %02x key: %02x xored: %02x", legic_prng_iv
, f
->data
, local_key
, xored
);
852 if(legic_state
== STATE_CON
) {
853 int key
= get_key_stream(2, 11); //legic_phase_drift, 11);
854 int addr
= f
->data
^ key
; addr
= addr
>> 1;
855 int data
= BigBuf
[addr
];
856 int hash
= LegicCRC(addr
, data
, 11) << 8;
857 BigBuf
[OFFSET_LOG
+legic_read_count
] = (uint8_t)addr
;
860 //Dbprintf("Data:%03.3x, key:%03.3x, addr: %03.3x, read_c:%u", f->data, key, addr, read_c);
861 legic_prng_forward(legic_reqresp_drift
);
863 frame_send_tag(hash
| data
, 12, 1);
868 legic_prng_forward(2);
869 //while(timer->TC_CV < 180);
878 int key
= get_key_stream(-1, 23); //legic_frame_drift, 23);
879 int addr
= f
->data
^ key
; addr
= addr
>> 1; addr
= addr
& 0x3ff;
880 int data
= f
->data
^ key
; data
= data
>> 11; data
= data
& 0xff;
883 legic_state
= STATE_DISCON
;
885 Dbprintf("write - addr: %x, data: %x", addr
, data
);
889 if(legic_state
!= STATE_DISCON
) {
890 Dbprintf("Unexpected: sz:%u, Data:%03.3x, State:%u, Count:%u", f
->bits
, f
->data
, legic_state
, legic_read_count
);
892 Dbprintf("IV: %03.3x", legic_prng_iv
);
893 for(i
= 0; i
<legic_read_count
; i
++) {
894 Dbprintf("Read Nb: %u, Addr: %u", i
, BigBuf
[OFFSET_LOG
+i
]);
897 for(i
= -1; i
<legic_read_count
; i
++) {
899 t
= BigBuf
[OFFSET_LOG
+256+i
*4];
900 t
|= BigBuf
[OFFSET_LOG
+256+i
*4+1] << 8;
901 t
|= BigBuf
[OFFSET_LOG
+256+i
*4+2] <<16;
902 t
|= BigBuf
[OFFSET_LOG
+256+i
*4+3] <<24;
904 Dbprintf("Cycles: %u, Frame Length: %u, Time: %u",
905 BigBuf
[OFFSET_LOG
+128+i
],
906 BigBuf
[OFFSET_LOG
+384+i
],
910 legic_state
= STATE_DISCON
;
911 legic_read_count
= 0;
917 /* Read bit by bit untill full frame is received
918 * Call to process frame end answer
920 static void emit(int bit
) {
924 frame_append_bit(¤t_frame
, 1);
927 frame_append_bit(¤t_frame
, 0);
930 if(current_frame
.bits
<= 4) {
931 frame_clean(¤t_frame
);
933 frame_handle_tag(¤t_frame
);
934 frame_clean(¤t_frame
);
941 void LegicRfSimulate(int phase
, int frame
, int reqresp
)
943 /* ADC path high-frequency peak detector, FPGA in high-frequency simulator mode,
944 * modulation mode set to 212kHz subcarrier. We are getting the incoming raw
945 * envelope waveform on DIN and should send our response on DOUT.
947 * The LEGIC RF protocol is pulse-pause-encoding from reader to card, so we'll
948 * measure the time between two rising edges on DIN, and no encoding on the
949 * subcarrier from card to reader, so we'll just shift out our verbatim data
950 * on DOUT, 1 bit is 100us. The time from reader to card frame is still unclear,
951 * seems to be 300us-ish.
954 legic_phase_drift
= phase
;
955 legic_frame_drift
= frame
;
956 legic_reqresp_drift
= reqresp
;
958 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
959 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
961 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
| FPGA_HF_SIMULATOR_MODULATE_212K
);
963 /* Bitbang the receiver */
964 AT91C_BASE_PIOA
->PIO_ODR
= GPIO_SSC_DIN
;
965 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DIN
;
968 crc_init(&legic_crc
, 4, 0x19 >> 1, 0x5, 0);
972 legic_state
= STATE_DISCON
;
975 DbpString("Starting Legic emulator, press button to end");
977 while(!BUTTON_PRESS() && !usb_poll_validate_length()) {
978 int level
= !!(AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_DIN
);
979 int time
= timer
->TC_CV
;
981 if(level
!= old_level
) {
983 timer
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
985 if (FUZZ_EQUAL(time
, RWD_TIME_1
, RWD_TIME_FUZZ
)) {
990 } else if (FUZZ_EQUAL(time
, RWD_TIME_0
, RWD_TIME_FUZZ
)) {
1005 if(time
>= (RWD_TIME_1
+RWD_TIME_FUZZ
) && active
) {
1011 if(time
>= (20*RWD_TIME_1
) && (timer
->TC_SR
& AT91C_TC_CLKSTA
)) {
1012 timer
->TC_CCR
= AT91C_TC_CLKDIS
;
1018 if ( MF_DBGLEVEL
>= 1) DbpString("Stopped");
1022 //-----------------------------------------------------------------------------
1023 //-----------------------------------------------------------------------------
1026 //-----------------------------------------------------------------------------
1027 // Code up a string of octets at layer 2 (including CRC, we don't generate
1028 // that here) so that they can be transmitted to the reader. Doesn't transmit
1029 // them yet, just leaves them ready to send in ToSend[].
1030 //-----------------------------------------------------------------------------
1031 // static void CodeLegicAsTag(const uint8_t *cmd, int len)
1037 // // Transmit a burst of ones, as the initial thing that lets the
1038 // // reader get phase sync. This (TR1) must be > 80/fs, per spec,
1039 // // but tag that I've tried (a Paypass) exceeds that by a fair bit,
1040 // // so I will too.
1041 // for(i = 0; i < 20; i++) {
1042 // ToSendStuffBit(1);
1043 // ToSendStuffBit(1);
1044 // ToSendStuffBit(1);
1045 // ToSendStuffBit(1);
1049 // for(i = 0; i < 10; i++) {
1050 // ToSendStuffBit(0);
1051 // ToSendStuffBit(0);
1052 // ToSendStuffBit(0);
1053 // ToSendStuffBit(0);
1055 // for(i = 0; i < 2; i++) {
1056 // ToSendStuffBit(1);
1057 // ToSendStuffBit(1);
1058 // ToSendStuffBit(1);
1059 // ToSendStuffBit(1);
1062 // for(i = 0; i < len; i++) {
1064 // uint8_t b = cmd[i];
1067 // ToSendStuffBit(0);
1068 // ToSendStuffBit(0);
1069 // ToSendStuffBit(0);
1070 // ToSendStuffBit(0);
1073 // for(j = 0; j < 8; j++) {
1075 // ToSendStuffBit(1);
1076 // ToSendStuffBit(1);
1077 // ToSendStuffBit(1);
1078 // ToSendStuffBit(1);
1080 // ToSendStuffBit(0);
1081 // ToSendStuffBit(0);
1082 // ToSendStuffBit(0);
1083 // ToSendStuffBit(0);
1089 // ToSendStuffBit(1);
1090 // ToSendStuffBit(1);
1091 // ToSendStuffBit(1);
1092 // ToSendStuffBit(1);
1096 // for(i = 0; i < 10; i++) {
1097 // ToSendStuffBit(0);
1098 // ToSendStuffBit(0);
1099 // ToSendStuffBit(0);
1100 // ToSendStuffBit(0);
1102 // for(i = 0; i < 2; i++) {
1103 // ToSendStuffBit(1);
1104 // ToSendStuffBit(1);
1105 // ToSendStuffBit(1);
1106 // ToSendStuffBit(1);
1109 // // Convert from last byte pos to length
1113 //-----------------------------------------------------------------------------
1114 // The software UART that receives commands from the reader, and its state
1116 //-----------------------------------------------------------------------------
1120 STATE_GOT_FALLING_EDGE_OF_SOF
,
1121 STATE_AWAITING_START_BIT
,
1122 STATE_RECEIVING_DATA
1132 /* Receive & handle a bit coming from the reader.
1134 * This function is called 4 times per bit (every 2 subcarrier cycles).
1135 * Subcarrier frequency fs is 212kHz, 1/fs = 4,72us, i.e. function is called every 9,44us
1138 * LED A -> ON once we have received the SOF and are expecting the rest.
1139 * LED A -> OFF once we have received EOF or are in error state or unsynced
1141 * Returns: true if we received a EOF
1142 * false if we are still waiting for some more
1144 // static RAMFUNC int HandleLegicUartBit(uint8_t bit)
1146 // switch(Uart.state) {
1147 // case STATE_UNSYNCD:
1149 // // we went low, so this could be the beginning of an SOF
1150 // Uart.state = STATE_GOT_FALLING_EDGE_OF_SOF;
1156 // case STATE_GOT_FALLING_EDGE_OF_SOF:
1158 // if(Uart.posCnt == 2) { // sample every 4 1/fs in the middle of a bit
1160 // if(Uart.bitCnt > 9) {
1161 // // we've seen enough consecutive
1162 // // zeros that it's a valid SOF
1164 // Uart.byteCnt = 0;
1165 // Uart.state = STATE_AWAITING_START_BIT;
1166 // LED_A_ON(); // Indicate we got a valid SOF
1168 // // didn't stay down long enough
1169 // // before going high, error
1170 // Uart.state = STATE_UNSYNCD;
1173 // // do nothing, keep waiting
1177 // if(Uart.posCnt >= 4) Uart.posCnt = 0;
1178 // if(Uart.bitCnt > 12) {
1179 // // Give up if we see too many zeros without
1182 // Uart.state = STATE_UNSYNCD;
1186 // case STATE_AWAITING_START_BIT:
1189 // if(Uart.posCnt > 50/2) { // max 57us between characters = 49 1/fs, max 3 etus after low phase of SOF = 24 1/fs
1190 // // stayed high for too long between
1191 // // characters, error
1192 // Uart.state = STATE_UNSYNCD;
1195 // // falling edge, this starts the data byte
1198 // Uart.shiftReg = 0;
1199 // Uart.state = STATE_RECEIVING_DATA;
1203 // case STATE_RECEIVING_DATA:
1205 // if(Uart.posCnt == 2) {
1206 // // time to sample a bit
1207 // Uart.shiftReg >>= 1;
1209 // Uart.shiftReg |= 0x200;
1213 // if(Uart.posCnt >= 4) {
1216 // if(Uart.bitCnt == 10) {
1217 // if((Uart.shiftReg & 0x200) && !(Uart.shiftReg & 0x001))
1219 // // this is a data byte, with correct
1220 // // start and stop bits
1221 // Uart.output[Uart.byteCnt] = (Uart.shiftReg >> 1) & 0xff;
1224 // if(Uart.byteCnt >= Uart.byteCntMax) {
1225 // // Buffer overflowed, give up
1227 // Uart.state = STATE_UNSYNCD;
1229 // // so get the next byte now
1231 // Uart.state = STATE_AWAITING_START_BIT;
1233 // } else if (Uart.shiftReg == 0x000) {
1234 // // this is an EOF byte
1235 // LED_A_OFF(); // Finished receiving
1236 // Uart.state = STATE_UNSYNCD;
1237 // if (Uart.byteCnt != 0) {
1241 // // this is an error
1243 // Uart.state = STATE_UNSYNCD;
1250 // Uart.state = STATE_UNSYNCD;
1258 static void UartReset() {
1259 Uart
.byteCntMax
= 3;
1260 Uart
.state
= STATE_UNSYNCD
;
1264 memset(Uart
.output
, 0x00, 3);
1267 // static void UartInit(uint8_t *data) {
1268 // Uart.output = data;
1272 //=============================================================================
1273 // An LEGIC reader. We take layer two commands, code them
1274 // appropriately, and then send them to the tag. We then listen for the
1275 // tag's response, which we leave in the buffer to be demodulated on the
1277 //=============================================================================
1282 DEMOD_PHASE_REF_TRAINING
,
1283 DEMOD_AWAITING_FALLING_EDGE_OF_SOF
,
1284 DEMOD_GOT_FALLING_EDGE_OF_SOF
,
1285 DEMOD_AWAITING_START_BIT
,
1286 DEMOD_RECEIVING_DATA
1299 * Handles reception of a bit from the tag
1301 * This function is called 2 times per bit (every 4 subcarrier cycles).
1302 * Subcarrier frequency fs is 212kHz, 1/fs = 4,72us, i.e. function is called every 9,44us
1305 * LED C -> ON once we have received the SOF and are expecting the rest.
1306 * LED C -> OFF once we have received EOF or are unsynced
1308 * Returns: true if we received a EOF
1309 * false if we are still waiting for some more
1313 #ifndef SUBCARRIER_DETECT_THRESHOLD
1314 # define SUBCARRIER_DETECT_THRESHOLD 8
1317 // Subcarrier amplitude v = sqrt(ci^2 + cq^2), approximated here by max(abs(ci),abs(cq)) + 1/2*min(abs(ci),abs(cq)))
1318 #ifndef CHECK_FOR_SUBCARRIER
1319 # define CHECK_FOR_SUBCARRIER() { v = MAX(ai, aq) + MIN(halfci, halfcq); }
1322 // The soft decision on the bit uses an estimate of just the
1323 // quadrant of the reference angle, not the exact angle.
1324 // Subcarrier amplitude v = sqrt(ci^2 + cq^2), approximated here by max(abs(ci),abs(cq)) + 1/2*min(abs(ci),abs(cq)))
1325 #define MAKE_SOFT_DECISION() { \
1326 if(Demod.sumI > 0) \
1331 if(Demod.sumQ > 0) \
1338 static RAMFUNC
int HandleLegicSamplesDemod(int ci
, int cq
)
1343 int halfci
= (ai
>> 1);
1344 int halfcq
= (aq
>> 1);
1346 switch(Demod
.state
) {
1349 CHECK_FOR_SUBCARRIER()
1351 if(v
> SUBCARRIER_DETECT_THRESHOLD
) { // subcarrier detected
1352 Demod
.state
= DEMOD_PHASE_REF_TRAINING
;
1359 case DEMOD_PHASE_REF_TRAINING
:
1360 if(Demod
.posCount
< 8) {
1362 CHECK_FOR_SUBCARRIER()
1364 if (v
> SUBCARRIER_DETECT_THRESHOLD
) {
1365 // set the reference phase (will code a logic '1') by averaging over 32 1/fs.
1366 // note: synchronization time > 80 1/fs
1372 Demod
.state
= DEMOD_UNSYNCD
;
1375 Demod
.state
= DEMOD_AWAITING_FALLING_EDGE_OF_SOF
;
1379 case DEMOD_AWAITING_FALLING_EDGE_OF_SOF
:
1381 MAKE_SOFT_DECISION()
1383 //Dbprintf("ICE: %d %d %d %d %d", v, Demod.sumI, Demod.sumQ, ci, cq );
1384 // logic '0' detected
1387 Demod
.state
= DEMOD_GOT_FALLING_EDGE_OF_SOF
;
1389 // start of SOF sequence
1392 // maximum length of TR1 = 200 1/fs
1393 if(Demod
.posCount
> 25*2) Demod
.state
= DEMOD_UNSYNCD
;
1398 case DEMOD_GOT_FALLING_EDGE_OF_SOF
:
1401 MAKE_SOFT_DECISION()
1404 // low phase of SOF too short (< 9 etu). Note: spec is >= 10, but FPGA tends to "smear" edges
1405 if(Demod
.posCount
< 10*2) {
1406 Demod
.state
= DEMOD_UNSYNCD
;
1408 LED_C_ON(); // Got SOF
1409 Demod
.state
= DEMOD_AWAITING_START_BIT
;
1414 // low phase of SOF too long (> 12 etu)
1415 if(Demod
.posCount
> 13*2) {
1416 Demod
.state
= DEMOD_UNSYNCD
;
1422 case DEMOD_AWAITING_START_BIT
:
1425 MAKE_SOFT_DECISION()
1428 // max 19us between characters = 16 1/fs, max 3 etu after low phase of SOF = 24 1/fs
1429 if(Demod
.posCount
> 3*2) {
1430 Demod
.state
= DEMOD_UNSYNCD
;
1434 // start bit detected
1436 Demod
.posCount
= 1; // this was the first half
1439 Demod
.state
= DEMOD_RECEIVING_DATA
;
1443 case DEMOD_RECEIVING_DATA
:
1445 MAKE_SOFT_DECISION()
1447 if(Demod
.posCount
== 0) {
1448 // first half of bit
1452 // second half of bit
1454 Demod
.shiftReg
>>= 1;
1456 if(Demod
.thisBit
> 0)
1457 Demod
.shiftReg
|= 0x200;
1461 if(Demod
.bitCount
== 10) {
1463 uint16_t s
= Demod
.shiftReg
;
1465 if((s
& 0x200) && !(s
& 0x001)) {
1466 // stop bit == '1', start bit == '0'
1467 uint8_t b
= (s
>> 1);
1468 Demod
.output
[Demod
.len
] = b
;
1470 Demod
.state
= DEMOD_AWAITING_START_BIT
;
1472 Demod
.state
= DEMOD_UNSYNCD
;
1476 // This is EOF (start, stop and all data bits == '0'
1486 Demod
.state
= DEMOD_UNSYNCD
;
1493 // Clear out the state of the "UART" that receives from the tag.
1494 static void DemodReset() {
1496 Demod
.state
= DEMOD_UNSYNCD
;
1503 memset(Demod
.output
, 0x00, 3);
1506 static void DemodInit(uint8_t *data
) {
1507 Demod
.output
= data
;
1512 * Demodulate the samples we received from the tag, also log to tracebuffer
1513 * quiet: set to 'TRUE' to disable debug output
1515 #define LEGIC_DMA_BUFFER_SIZE 256
1516 static void GetSamplesForLegicDemod(int n
, bool quiet
)
1519 bool gotFrame
= FALSE
;
1520 int lastRxCounter
= LEGIC_DMA_BUFFER_SIZE
;
1521 int ci
, cq
, samples
= 0;
1525 // And put the FPGA in the appropriate mode
1526 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
| FPGA_HF_READER_RX_XCORR_QUARTER_FREQ
);
1528 // The response (tag -> reader) that we're receiving.
1529 // Set up the demodulator for tag -> reader responses.
1530 DemodInit(BigBuf_malloc(MAX_FRAME_SIZE
));
1532 // The DMA buffer, used to stream samples from the FPGA
1533 int8_t *dmaBuf
= (int8_t*) BigBuf_malloc(LEGIC_DMA_BUFFER_SIZE
);
1534 int8_t *upTo
= dmaBuf
;
1536 // Setup and start DMA.
1537 if ( !FpgaSetupSscDma((uint8_t*) dmaBuf
, LEGIC_DMA_BUFFER_SIZE
) ){
1538 if (MF_DBGLEVEL
> 1) Dbprintf("FpgaSetupSscDma failed. Exiting");
1542 // Signal field is ON with the appropriate LED:
1545 int behindBy
= lastRxCounter
- AT91C_BASE_PDC_SSC
->PDC_RCR
;
1546 if(behindBy
> max
) max
= behindBy
;
1548 while(((lastRxCounter
-AT91C_BASE_PDC_SSC
->PDC_RCR
) & (LEGIC_DMA_BUFFER_SIZE
-1)) > 2) {
1552 if(upTo
>= dmaBuf
+ LEGIC_DMA_BUFFER_SIZE
) {
1554 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (uint32_t) upTo
;
1555 AT91C_BASE_PDC_SSC
->PDC_RNCR
= LEGIC_DMA_BUFFER_SIZE
;
1558 if(lastRxCounter
<= 0)
1559 lastRxCounter
= LEGIC_DMA_BUFFER_SIZE
;
1563 gotFrame
= HandleLegicSamplesDemod(ci
, cq
);
1568 if(samples
> n
|| gotFrame
)
1572 FpgaDisableSscDma();
1574 if (!quiet
&& Demod
.len
== 0) {
1575 Dbprintf("max behindby = %d, samples = %d, gotFrame = %d, Demod.len = %d, Demod.sumI = %d, Demod.sumQ = %d",
1586 if (Demod
.len
> 0) {
1587 uint8_t parity
[MAX_PARITY_SIZE
] = {0x00};
1588 LogTrace(Demod
.output
, Demod
.len
, 0, 0, parity
, FALSE
);
1591 //-----------------------------------------------------------------------------
1592 // Transmit the command (to the tag) that was placed in ToSend[].
1593 //-----------------------------------------------------------------------------
1594 static void TransmitForLegic(void)
1600 while(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
))
1601 AT91C_BASE_SSC
->SSC_THR
= 0xff;
1603 // Signal field is ON with the appropriate Red LED
1606 // Signal we are transmitting with the Green LED
1608 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX
| FPGA_HF_READER_TX_SHALLOW_MOD
);
1610 for(c
= 0; c
< 10;) {
1611 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1612 AT91C_BASE_SSC
->SSC_THR
= 0xff;
1615 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1616 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1624 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1625 AT91C_BASE_SSC
->SSC_THR
= ToSend
[c
];
1626 legic_prng_forward(1); // forward the lfsr
1628 if(c
>= ToSendMax
) {
1632 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1633 volatile uint32_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1642 //-----------------------------------------------------------------------------
1643 // Code a layer 2 command (string of octets, including CRC) into ToSend[],
1644 // so that it is ready to transmit to the tag using TransmitForLegic().
1645 //-----------------------------------------------------------------------------
1646 static void CodeLegicBitsAsReader(const uint8_t *cmd
, uint8_t cmdlen
, int bits
)
1654 for(i
= 0; i
< 7; i
++)
1658 for(i
= 0; i
< cmdlen
; i
++) {
1664 for(j
= 0; j
< bits
; j
++) {
1674 // Convert from last character reference to length
1679 Convenience function to encode, transmit and trace Legic comms
1681 static void CodeAndTransmitLegicAsReader(const uint8_t *cmd
, uint8_t cmdlen
, int bits
)
1683 CodeLegicBitsAsReader(cmd
, cmdlen
, bits
);
1686 uint8_t parity
[1] = {0x00};
1687 LogTrace(cmd
, cmdlen
, 0, 0, parity
, TRUE
);
1691 int ice_legic_select_card()
1693 //int cmd_size=0, card_size=0;
1694 uint8_t wakeup
[] = { 0x7F };
1695 uint8_t getid
[] = {0x19};
1697 //legic_prng_init(SESSION_IV);
1699 // first, wake up the tag, 7bits
1700 CodeAndTransmitLegicAsReader(wakeup
, sizeof(wakeup
), 7);
1702 GetSamplesForLegicDemod(1000, TRUE
);
1704 //frame_receiveAsReader(¤t_frame, 6, 1);
1706 legic_prng_forward(1); /* we wait anyways */
1708 //while(timer->TC_CV < 387) ; /* ~ 258us */
1709 //frame_sendAsReader(0x19, 6);
1710 CodeAndTransmitLegicAsReader(getid
, sizeof(getid
), 8);
1711 GetSamplesForLegicDemod(1000, TRUE
);
1713 //if (Demod.len < 14) return 2;
1714 Dbprintf("CARD TYPE: %02x LEN: %d", Demod
.output
[0], Demod
.len
);
1716 switch(Demod
.output
[0]) {
1718 DbpString("MIM 256 card found");
1723 DbpString("MIM 1024 card found");
1725 // card_size = 1024;
1732 // bytes = card_size;
1734 // if(bytes + offset >= card_size)
1735 // bytes = card_size - offset;
1737 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1742 // Set up LEGIC communication
1743 void ice_legic_setup() {
1746 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1747 BigBuf_free(); BigBuf_Clear_ext(false);
1753 // Set up the synchronous serial port
1756 // connect Demodulated Signal to ADC:
1757 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1759 // Signal field is on with the appropriate LED
1761 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX
| FPGA_HF_READER_TX_SHALLOW_MOD
);
1764 //StartCountSspClk();
1767 crc_init(&legic_crc
, 4, 0x19 >> 1, 0x5, 0);