1 //-----------------------------------------------------------------------------
2 // (c) 2009 Henryk Plötz <henryk@ploetzli.ch>
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // LEGIC RF simulation code
9 //-----------------------------------------------------------------------------
11 #include "proxmark3.h"
17 #include "legic_prng.h"
20 static struct legic_frame
{
31 static crc_t legic_crc
;
32 static int legic_read_count
;
33 static uint32_t legic_prng_bc
;
34 static uint32_t legic_prng_iv
;
36 static int legic_phase_drift
;
37 static int legic_frame_drift
;
38 static int legic_reqresp_drift
;
43 static void setup_timer(void)
45 /* Set up Timer 1 to use for measuring time between pulses. Since we're bit-banging
46 * this it won't be terribly accurate but should be good enough.
48 AT91C_BASE_PMC
->PMC_PCER
= (1 << AT91C_ID_TC1
);
49 timer
= AT91C_BASE_TC1
;
50 timer
->TC_CCR
= AT91C_TC_CLKDIS
;
51 timer
->TC_CMR
= AT91C_TC_CLKS_TIMER_DIV3_CLOCK
;
52 timer
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
55 * Set up Timer 2 to use for measuring time between frames in
56 * tag simulation mode. Runs 4x faster as Timer 1
58 AT91C_BASE_PMC
->PMC_PCER
= (1 << AT91C_ID_TC2
);
59 prng_timer
= AT91C_BASE_TC2
;
60 prng_timer
->TC_CCR
= AT91C_TC_CLKDIS
;
61 prng_timer
->TC_CMR
= AT91C_TC_CLKS_TIMER_DIV2_CLOCK
;
62 prng_timer
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
65 /* At TIMER_CLOCK3 (MCK/32) */
66 #define RWD_TIME_1 150 /* RWD_TIME_PAUSE off, 80us on = 100us */
67 #define RWD_TIME_0 90 /* RWD_TIME_PAUSE off, 40us on = 60us */
68 #define RWD_TIME_PAUSE 30 /* 20us */
69 #define RWD_TIME_FUZZ 20 /* rather generous 13us, since the peak detector + hysteresis fuzz quite a bit */
70 #define TAG_TIME_BIT 150 /* 100us for every bit */
71 #define TAG_TIME_WAIT 490 /* time from RWD frame end to tag frame start, experimentally determined */
73 #define SIM_DIVISOR 586 /* prng_time/SIM_DIVISOR count prng needs to be forwared */
74 #define SIM_SHIFT 900 /* prng_time+SIM_SHIFT shift of delayed start */
76 #define SESSION_IV 0x55
77 #define OFFSET_LOG 1024
79 #define FUZZ_EQUAL(value, target, fuzz) ((value) > ((target)-(fuzz)) && (value) < ((target)+(fuzz)))
81 /* Generate Keystream */
82 static uint32_t get_key_stream(int skip
, int count
)
84 uint32_t key
=0; int i
;
86 /* Use int to enlarge timer tc to 32bit */
87 legic_prng_bc
+= prng_timer
->TC_CV
;
88 prng_timer
->TC_CCR
= AT91C_TC_SWTRG
;
90 /* If skip == -1, forward prng time based */
92 i
= (legic_prng_bc
+SIM_SHIFT
)/SIM_DIVISOR
; /* Calculate Cycles based on timer */
93 i
-= legic_prng_count(); /* substract cycles of finished frames */
94 i
-= count
; /* substract current frame length, rewidn to bedinning */
95 legic_prng_forward(i
);
97 legic_prng_forward(skip
);
100 /* Write Time Data into LOG */
101 uint8_t *BigBuf
= BigBuf_get_addr();
102 i
= (count
== 6) ? -1 : legic_read_count
;
104 BigBuf
[OFFSET_LOG
+128+i
] = legic_prng_count();
105 BigBuf
[OFFSET_LOG
+256+i
*4] = (legic_prng_bc
>> 0) & 0xff;
106 BigBuf
[OFFSET_LOG
+256+i
*4+1] = (legic_prng_bc
>> 8) & 0xff;
107 BigBuf
[OFFSET_LOG
+256+i
*4+2] = (legic_prng_bc
>>16) & 0xff;
108 BigBuf
[OFFSET_LOG
+256+i
*4+3] = (legic_prng_bc
>>24) & 0xff;
109 BigBuf
[OFFSET_LOG
+384+i
] = count
;
111 /* Generate KeyStream */
112 for(i
=0; i
<count
; i
++) {
113 key
|= legic_prng_get_bit() << i
;
114 legic_prng_forward(1);
119 /* Send a frame in tag mode, the FPGA must have been set up by
122 static void frame_send_tag(uint16_t response
, int bits
, int crypt
)
124 /* Bitbang the response */
125 AT91C_BASE_PIOA
->PIO_CODR
= GPIO_SSC_DOUT
;
126 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
127 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
129 /* Use time to crypt frame */
131 legic_prng_forward(2); /* TAG_TIME_WAIT -> shift by 2 */
133 for(i
=0; i
<bits
; i
++) {
134 key
|= legic_prng_get_bit() << i
;
135 legic_prng_forward(1);
137 //Dbprintf("key = 0x%x", key);
138 response
= response
^ key
;
141 /* Wait for the frame start */
142 while(timer
->TC_CV
< (TAG_TIME_WAIT
- 30)) ;
145 for(i
=0; i
<bits
; i
++) {
146 int nextbit
= timer
->TC_CV
+ TAG_TIME_BIT
;
147 int bit
= response
& 1;
148 response
= response
>> 1;
150 AT91C_BASE_PIOA
->PIO_SODR
= GPIO_SSC_DOUT
;
152 AT91C_BASE_PIOA
->PIO_CODR
= GPIO_SSC_DOUT
;
154 while(timer
->TC_CV
< nextbit
) ;
156 AT91C_BASE_PIOA
->PIO_CODR
= GPIO_SSC_DOUT
;
159 /* Send a frame in reader mode, the FPGA must have been set up by
162 static void frame_send_rwd(uint32_t data
, int bits
)
165 timer
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
166 while(timer
->TC_CV
> 1) ; /* Wait till the clock has reset */
169 for(i
=0; i
<bits
; i
++) {
170 int starttime
= timer
->TC_CV
;
171 int pause_end
= starttime
+ RWD_TIME_PAUSE
, bit_end
;
175 if(bit
^ legic_prng_get_bit())
176 bit_end
= starttime
+ RWD_TIME_1
;
178 bit_end
= starttime
+ RWD_TIME_0
;
181 /* RWD_TIME_PAUSE time off, then some time on, so that the complete bit time is
182 * RWD_TIME_x, where x is the bit to be transmitted */
183 AT91C_BASE_PIOA
->PIO_CODR
= GPIO_SSC_DOUT
;
184 while(timer
->TC_CV
< pause_end
) ;
185 AT91C_BASE_PIOA
->PIO_SODR
= GPIO_SSC_DOUT
;
186 legic_prng_forward(1); /* bit duration is longest. use this time to forward the lfsr */
188 while(timer
->TC_CV
< bit_end
);
191 /* One final pause to mark the end of the frame */
192 int pause_end
= timer
->TC_CV
+ RWD_TIME_PAUSE
;
193 AT91C_BASE_PIOA
->PIO_CODR
= GPIO_SSC_DOUT
;
194 while(timer
->TC_CV
< pause_end
) ;
195 AT91C_BASE_PIOA
->PIO_SODR
= GPIO_SSC_DOUT
;
198 /* Reset the timer, to measure time until the start of the tag frame */
199 timer
->TC_CCR
= AT91C_TC_SWTRG
;
200 while(timer
->TC_CV
> 1) ; /* Wait till the clock has reset */
203 /* Receive a frame from the card in reader emulation mode, the FPGA and
204 * timer must have been set up by LegicRfReader and frame_send_rwd.
206 * The LEGIC RF protocol from card to reader does not include explicit
207 * frame start/stop information or length information. The reader must
208 * know beforehand how many bits it wants to receive. (Notably: a card
209 * sending a stream of 0-bits is indistinguishable from no card present.)
211 * Receive methodology: There is a fancy correlator in hi_read_rx_xcorr, but
212 * I'm not smart enough to use it. Instead I have patched hi_read_tx to output
213 * the ADC signal with hysteresis on SSP_DIN. Bit-bang that signal and look
214 * for edges. Count the edges in each bit interval. If they are approximately
215 * 0 this was a 0-bit, if they are approximately equal to the number of edges
216 * expected for a 212kHz subcarrier, this was a 1-bit. For timing we use the
217 * timer that's still running from frame_send_rwd in order to get a synchronization
218 * with the frame that we just sent.
220 * FIXME: Because we're relying on the hysteresis to just do the right thing
221 * the range is severely reduced (and you'll probably also need a good antenna).
222 * So this should be fixed some time in the future for a proper receiver.
224 static void frame_receive_rwd(struct legic_frame
* const f
, int bits
, int crypt
)
226 uint32_t the_bit
= 1; /* Use a bitmask to save on shifts */
228 int i
, old_level
=0, edges
=0;
229 int next_bit_at
= TAG_TIME_WAIT
;
235 AT91C_BASE_PIOA
->PIO_ODR
= GPIO_SSC_DIN
;
236 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DIN
;
238 /* we have some time now, precompute the cipher
239 * since we cannot compute it on the fly while reading */
240 legic_prng_forward(2);
243 for(i
=0; i
<bits
; i
++) {
244 data
|= legic_prng_get_bit() << i
;
245 legic_prng_forward(1);
249 while(timer
->TC_CV
< next_bit_at
) ;
251 next_bit_at
+= TAG_TIME_BIT
;
253 for(i
=0; i
<bits
; i
++) {
255 while(timer
->TC_CV
< next_bit_at
) {
256 int level
= (AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_DIN
);
257 if(level
!= old_level
)
261 next_bit_at
+= TAG_TIME_BIT
;
263 if(edges
> 20 && edges
< 60) { /* expected are 42 edges */
272 /* Reset the timer, to synchronize the next frame */
273 timer
->TC_CCR
= AT91C_TC_SWTRG
;
274 while(timer
->TC_CV
> 1) ; /* Wait till the clock has reset */
277 static void frame_append_bit(struct legic_frame
* const f
, int bit
)
280 return; /* Overflow, won't happen */
282 f
->data
|= (bit
<< f
->bits
);
286 static void frame_clean(struct legic_frame
* const f
)
292 static uint32_t perform_setup_phase_rwd(int iv
)
295 /* Switch on carrier and let the tag charge for 1ms */
296 AT91C_BASE_PIOA
->PIO_SODR
= GPIO_SSC_DOUT
;
299 legic_prng_init(0); /* no keystream yet */
300 frame_send_rwd(iv
, 7);
303 frame_clean(¤t_frame
);
304 frame_receive_rwd(¤t_frame
, 6, 1);
305 legic_prng_forward(1); /* we wait anyways */
306 while(timer
->TC_CV
< 387) ; /* ~ 258us */
307 frame_send_rwd(0x19, 6);
309 return current_frame
.data
;
312 static void LegicCommonInit(void) {
313 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
314 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
316 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX
);
318 /* Bitbang the transmitter */
319 AT91C_BASE_PIOA
->PIO_CODR
= GPIO_SSC_DOUT
;
320 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
321 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
325 crc_init(&legic_crc
, 4, 0x19 >> 1, 0x5, 0);
328 static void switch_off_tag_rwd(void)
330 /* Switch off carrier, make sure tag is reset */
331 AT91C_BASE_PIOA
->PIO_CODR
= GPIO_SSC_DOUT
;
336 /* calculate crc for a legic command */
337 static int LegicCRC(int byte_index
, int value
, int cmd_sz
) {
338 crc_clear(&legic_crc
);
339 crc_update(&legic_crc
, 1, 1); /* CMD_READ */
340 crc_update(&legic_crc
, byte_index
, cmd_sz
-1);
341 crc_update(&legic_crc
, value
, 8);
342 return crc_finish(&legic_crc
);
345 int legic_read_byte(int byte_index
, int cmd_sz
) {
348 legic_prng_forward(4); /* we wait anyways */
349 while(timer
->TC_CV
< 387) ; /* ~ 258us + 100us*delay */
351 frame_send_rwd(1 | (byte_index
<< 1), cmd_sz
);
352 frame_clean(¤t_frame
);
354 frame_receive_rwd(¤t_frame
, 12, 1);
356 byte
= current_frame
.data
& 0xff;
358 if( LegicCRC(byte_index
, byte
, cmd_sz
) != (current_frame
.data
>> 8) ) {
359 Dbprintf("!!! crc mismatch: expected %x but got %x !!!",
360 LegicCRC(byte_index
, current_frame
.data
& 0xff, cmd_sz
),
361 current_frame
.data
>> 8);
368 /* legic_write_byte() is not included, however it's trivial to implement
369 * and here are some hints on what remains to be done:
371 * * assemble a write_cmd_frame with crc and send it
372 * * wait until the tag sends back an ACK ('1' bit unencrypted)
373 * * forward the prng based on the timing
375 int legic_write_byte(int byte
, int addr
, int addr_sz
) {
376 //do not write UID, CRC, DCF
380 //== send write command ==============================
381 crc_clear(&legic_crc
);
382 crc_update(&legic_crc
, 0, 1); /* CMD_WRITE */
383 crc_update(&legic_crc
, addr
, addr_sz
);
384 crc_update(&legic_crc
, byte
, 8);
386 uint32_t crc
= crc_finish(&legic_crc
);
387 uint32_t cmd
= ((crc
<<(addr_sz
+1+8)) //CRC
388 |(byte
<<(addr_sz
+1)) //Data
389 |(addr
<<1) //Address
390 |(0x00 <<0)); //CMD = W
391 uint32_t cmd_sz
= addr_sz
+1+8+4; //crc+data+cmd
393 legic_prng_forward(2); /* we wait anyways */
394 while(timer
->TC_CV
< 387) ; /* ~ 258us */
395 frame_send_rwd(cmd
, cmd_sz
);
397 //== wait for ack ====================================
398 int t
, old_level
=0, edges
=0;
400 while(timer
->TC_CV
< 387) ; /* ~ 258us */
401 for(t
=0; t
<80; t
++) {
403 next_bit_at
+= TAG_TIME_BIT
;
404 while(timer
->TC_CV
< next_bit_at
) {
405 int level
= (AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_DIN
);
406 if(level
!= old_level
) {
411 if(edges
> 20 && edges
< 60) { /* expected are 42 edges */
412 int t
= timer
->TC_CV
;
413 int c
= t
/TAG_TIME_BIT
;
414 timer
->TC_CCR
= AT91C_TC_SWTRG
;
415 while(timer
->TC_CV
> 1) ; /* Wait till the clock has reset */
416 legic_prng_forward(c
);
420 timer
->TC_CCR
= AT91C_TC_SWTRG
;
421 while(timer
->TC_CV
> 1) ; /* Wait till the clock has reset */
425 int LegicRfReader(int offset
, int bytes
) {
426 int byte_index
=0, cmd_sz
=0, card_sz
=0;
430 uint8_t *BigBuf
= BigBuf_get_addr();
431 memset(BigBuf
, 0, 1024);
433 DbpString("setting up legic card");
434 uint32_t tag_type
= perform_setup_phase_rwd(SESSION_IV
);
435 switch_off_tag_rwd(); //we lose to mutch time with dprintf
438 DbpString("MIM 256 card found, reading card ...");
443 DbpString("MIM 1024 card found, reading card ...");
448 Dbprintf("Unknown card format: %x",tag_type
);
454 if(bytes
+offset
>= card_sz
)
455 bytes
= card_sz
-offset
;
457 perform_setup_phase_rwd(SESSION_IV
);
460 while(byte_index
< bytes
) {
461 int r
= legic_read_byte(byte_index
+offset
, cmd_sz
);
462 if(r
== -1 ||BUTTON_PRESS()) {
463 DbpString("operation aborted");
464 switch_off_tag_rwd();
469 BigBuf
[byte_index
] = r
;
472 if(byte_index
& 0x10) LED_C_ON(); else LED_C_OFF();
476 switch_off_tag_rwd();
477 Dbprintf("Card read, use 'hf legic decode' or");
478 Dbprintf("'data hexsamples %d' to view results", (bytes
+7) & ~7);
482 void LegicRfWriter(int bytes
, int offset
) {
483 int byte_index
=0, addr_sz
=0;
484 uint8_t *BigBuf
= BigBuf_get_addr();
488 DbpString("setting up legic card");
489 uint32_t tag_type
= perform_setup_phase_rwd(SESSION_IV
);
490 switch_off_tag_rwd();
493 if(offset
+bytes
> 0x100) {
494 Dbprintf("Error: can not write to 0x%03.3x on MIM 256", offset
+bytes
);
498 Dbprintf("MIM 256 card found, writing 0x%02.2x - 0x%02.2x ...", offset
, offset
+bytes
);
501 if(offset
+bytes
> 0x400) {
502 Dbprintf("Error: can not write to 0x%03.3x on MIM 1024", offset
+bytes
);
506 Dbprintf("MIM 1024 card found, writing 0x%03.3x - 0x%03.3x ...", offset
, offset
+bytes
);
509 Dbprintf("No or unknown card found, aborting");
514 perform_setup_phase_rwd(SESSION_IV
);
515 legic_prng_forward(2);
516 while(byte_index
< bytes
) {
517 int r
= legic_write_byte(BigBuf
[byte_index
+offset
], byte_index
+offset
, addr_sz
);
518 if((r
!= 0) || BUTTON_PRESS()) {
519 Dbprintf("operation aborted @ 0x%03.3x", byte_index
);
520 switch_off_tag_rwd();
527 if(byte_index
& 0x10) LED_C_ON(); else LED_C_OFF();
531 DbpString("write successful");
536 /* Handle (whether to respond) a frame in tag mode */
537 static void frame_handle_tag(struct legic_frame
const * const f
)
539 uint8_t *BigBuf
= BigBuf_get_addr();
541 /* First Part of Handshake (IV) */
543 if(f
->data
== SESSION_IV
) {
545 prng_timer
->TC_CCR
= AT91C_TC_SWTRG
;
546 legic_prng_init(f
->data
);
547 frame_send_tag(0x3d, 6, 1); /* 0x3d^0x26 = 0x1b */
548 legic_state
= STATE_IV
;
549 legic_read_count
= 0;
551 legic_prng_iv
= f
->data
;
554 timer
->TC_CCR
= AT91C_TC_SWTRG
;
555 while(timer
->TC_CV
> 1);
556 while(timer
->TC_CV
< 280);
558 } else if((prng_timer
->TC_CV
% 50) > 40) {
559 legic_prng_init(f
->data
);
560 frame_send_tag(0x3d, 6, 1);
567 if(legic_state
== STATE_IV
) {
568 if((f
->bits
== 6) && (f
->data
== (0x19 ^ get_key_stream(1, 6)))) {
569 legic_state
= STATE_CON
;
572 timer
->TC_CCR
= AT91C_TC_SWTRG
;
573 while(timer
->TC_CV
> 1);
574 while(timer
->TC_CV
< 200);
577 legic_state
= STATE_DISCON
;
579 Dbprintf("0x19 - Frame: %03.3x", f
->data
);
586 if(legic_state
== STATE_CON
) {
587 int key
= get_key_stream(-1, 11); //legic_phase_drift, 11);
588 int addr
= f
->data
^ key
; addr
= addr
>> 1;
589 int data
= BigBuf
[addr
];
590 int hash
= LegicCRC(addr
, data
, 11) << 8;
591 BigBuf
[OFFSET_LOG
+legic_read_count
] = (uint8_t)addr
;
594 //Dbprintf("Data:%03.3x, key:%03.3x, addr: %03.3x, read_c:%u", f->data, key, addr, read_c);
595 legic_prng_forward(legic_reqresp_drift
);
597 frame_send_tag(hash
| data
, 12, 1);
600 timer
->TC_CCR
= AT91C_TC_SWTRG
;
601 while(timer
->TC_CV
> 1);
602 legic_prng_forward(legic_frame_drift
);
603 while(timer
->TC_CV
< 180);
610 int key
= get_key_stream(-1, 23); //legic_frame_drift, 23);
611 int addr
= f
->data
^ key
; addr
= addr
>> 1; addr
= addr
& 0x3ff;
612 int data
= f
->data
^ key
; data
= data
>> 11; data
= data
& 0xff;
615 legic_state
= STATE_DISCON
;
617 Dbprintf("write - addr: %x, data: %x", addr
, data
);
621 if(legic_state
!= STATE_DISCON
) {
622 Dbprintf("Unexpected: sz:%u, Data:%03.3x, State:%u, Count:%u", f
->bits
, f
->data
, legic_state
, legic_read_count
);
624 Dbprintf("IV: %03.3x", legic_prng_iv
);
625 for(i
= 0; i
<legic_read_count
; i
++) {
626 Dbprintf("Read Nb: %u, Addr: %u", i
, BigBuf
[OFFSET_LOG
+i
]);
629 for(i
= -1; i
<legic_read_count
; i
++) {
631 t
= BigBuf
[OFFSET_LOG
+256+i
*4];
632 t
|= BigBuf
[OFFSET_LOG
+256+i
*4+1] << 8;
633 t
|= BigBuf
[OFFSET_LOG
+256+i
*4+2] <<16;
634 t
|= BigBuf
[OFFSET_LOG
+256+i
*4+3] <<24;
636 Dbprintf("Cycles: %u, Frame Length: %u, Time: %u",
637 BigBuf
[OFFSET_LOG
+128+i
],
638 BigBuf
[OFFSET_LOG
+384+i
],
642 legic_state
= STATE_DISCON
;
643 legic_read_count
= 0;
649 /* Read bit by bit untill full frame is received
650 * Call to process frame end answer
652 static void emit(int bit
)
655 if(current_frame
.bits
<= 4) {
656 frame_clean(¤t_frame
);
658 frame_handle_tag(¤t_frame
);
659 frame_clean(¤t_frame
);
662 } else if(bit
== 0) {
663 frame_append_bit(¤t_frame
, 0);
664 } else if(bit
== 1) {
665 frame_append_bit(¤t_frame
, 1);
669 void LegicRfSimulate(int phase
, int frame
, int reqresp
)
671 /* ADC path high-frequency peak detector, FPGA in high-frequency simulator mode,
672 * modulation mode set to 212kHz subcarrier. We are getting the incoming raw
673 * envelope waveform on DIN and should send our response on DOUT.
675 * The LEGIC RF protocol is pulse-pause-encoding from reader to card, so we'll
676 * measure the time between two rising edges on DIN, and no encoding on the
677 * subcarrier from card to reader, so we'll just shift out our verbatim data
678 * on DOUT, 1 bit is 100us. The time from reader to card frame is still unclear,
679 * seems to be 300us-ish.
684 for(i
=0; i
<=reqresp
; i
++) {
685 legic_prng_init(SESSION_IV
);
686 Dbprintf("i=%u, key 0x%3.3x", i
, get_key_stream(i
, frame
));
691 legic_phase_drift
= phase
;
692 legic_frame_drift
= frame
;
693 legic_reqresp_drift
= reqresp
;
695 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
696 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
698 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
| FPGA_HF_SIMULATOR_MODULATE_212K
);
700 /* Bitbang the receiver */
701 AT91C_BASE_PIOA
->PIO_ODR
= GPIO_SSC_DIN
;
702 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DIN
;
705 crc_init(&legic_crc
, 4, 0x19 >> 1, 0x5, 0);
709 legic_state
= STATE_DISCON
;
712 DbpString("Starting Legic emulator, press button to end");
713 while(!BUTTON_PRESS() && !usb_poll_validate_length()) {
714 int level
= !!(AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_DIN
);
715 int time
= timer
->TC_CV
;
717 if(level
!= old_level
) {
719 timer
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
720 if(FUZZ_EQUAL(time
, RWD_TIME_1
, RWD_TIME_FUZZ
)) {
725 } else if(FUZZ_EQUAL(time
, RWD_TIME_0
, RWD_TIME_FUZZ
)) {
739 if(time
>= (RWD_TIME_1
+RWD_TIME_FUZZ
) && active
) {
746 if(time
>= (20*RWD_TIME_1
) && (timer
->TC_SR
& AT91C_TC_CLKSTA
)) {
747 timer
->TC_CCR
= AT91C_TC_CLKDIS
;
753 DbpString("Stopped");