2 * LEGIC RF simulation code
4 * (c) 2009 Henryk Plötz <henryk@ploetzli.ch>
14 static struct legic_frame
{
20 static void setup_timer(void)
22 /* Set up Timer 1 to use for measuring time between pulses. Since we're bit-banging
23 * this it won't be terribly accurate but should be good enough.
25 AT91C_BASE_PMC
->PMC_PCER
= (1 << AT91C_ID_TC1
);
26 timer
= AT91C_BASE_TC1
;
27 timer
->TC_CCR
= AT91C_TC_CLKDIS
;
28 timer
->TC_CMR
= TC_CMR_TCCLKS_TIMER_CLOCK3
;
29 timer
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
31 /* At TIMER_CLOCK3 (MCK/32) */
32 #define RWD_TIME_1 150 /* RWD_TIME_PAUSE off, 80us on = 100us */
33 #define RWD_TIME_0 90 /* RWD_TIME_PAUSE off, 40us on = 60us */
34 #define RWD_TIME_PAUSE 30 /* 20us */
35 #define RWD_TIME_FUZZ 20 /* rather generous 13us, since the peak detector + hysteresis fuzz quite a bit */
36 #define TAG_TIME_BIT 150 /* 100us for every bit */
37 #define TAG_TIME_WAIT 490 /* time from RWD frame end to tag frame start, experimentally determined */
41 #define FUZZ_EQUAL(value, target, fuzz) ((value) > ((target)-(fuzz)) && (value) < ((target)+(fuzz)))
43 static const struct legic_frame queries
[] = {
44 {7, 0x55}, /* 1010 101 */
47 static const struct legic_frame responses
[] = {
48 {6, 0x3b}, /* 1101 11 */
51 /* Send a frame in tag mode, the FPGA must have been set up by
54 static void frame_send_tag(uint16_t response
, int bits
)
57 /* Use the SSC to send a response. 8-bit transfers, LSBit first, 100us per bit */
59 /* Bitbang the response */
60 AT91C_BASE_PIOA
->PIO_CODR
= GPIO_SSC_DOUT
;
61 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
62 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
64 /* Wait for the frame start */
65 while(timer
->TC_CV
< TAG_TIME_WAIT
) ;
68 for(i
=0; i
<bits
; i
++) {
69 int nextbit
= timer
->TC_CV
+ TAG_TIME_BIT
;
70 int bit
= response
& 1;
71 response
= response
>> 1;
73 AT91C_BASE_PIOA
->PIO_SODR
= GPIO_SSC_DOUT
;
75 AT91C_BASE_PIOA
->PIO_CODR
= GPIO_SSC_DOUT
;
76 while(timer
->TC_CV
< nextbit
) ;
78 AT91C_BASE_PIOA
->PIO_CODR
= GPIO_SSC_DOUT
;
82 /* Figure out a response to a frame in tag mode */
83 static void frame_respond_tag(struct legic_frame
const * const f
)
89 for(i
=0; i
<sizeof(queries
)/sizeof(queries
[0]); i
++) {
90 if(f
->bits
== queries
[i
].bits
&& f
->data
== queries
[i
].data
) {
91 r_data
= responses
[i
].data
;
92 r_size
= responses
[i
].bits
;
98 frame_send_tag(r_data
, r_size
);
107 static void frame_append_bit(struct legic_frame
* const f
, int bit
)
110 return; /* Overflow, won't happen */
111 f
->data
|= (bit
<<f
->bits
);
115 static int frame_is_empty(struct legic_frame
const * const f
)
117 return( f
->bits
<= 4 );
120 /* Handle (whether to respond) a frame in tag mode */
121 static void frame_handle_tag(struct legic_frame
const * const f
)
127 if( !frame_is_empty(f
) ) {
128 frame_respond_tag(f
);
132 static void frame_clean(struct legic_frame
* const f
)
139 EMIT_RWD
, /* Emit in tag simulation mode, e.g. the source is the RWD */
140 EMIT_TAG
/* Emit in reader simulation mode, e.g. the source is the TAG */
142 static void emit(enum emit_mode mode
, int bit
)
145 if(mode
== EMIT_RWD
) {
146 frame_handle_tag(¤t_frame
);
148 frame_clean(¤t_frame
);
149 } else if(bit
== 0) {
150 frame_append_bit(¤t_frame
, 0);
151 } else if(bit
== 1) {
152 frame_append_bit(¤t_frame
, 1);
156 void LegicRfSimulate(void)
158 /* ADC path high-frequency peak detector, FPGA in high-frequency simulator mode,
159 * modulation mode set to 212kHz subcarrier. We are getting the incoming raw
160 * envelope waveform on DIN and should send our response on DOUT.
162 * The LEGIC RF protocol is pulse-pause-encoding from reader to card, so we'll
163 * measure the time between two rising edges on DIN, and no encoding on the
164 * subcarrier from card to reader, so we'll just shift out our verbatim data
165 * on DOUT, 1 bit is 100us. The time from reader to card frame is still unclear,
166 * seems to be 300us-ish.
168 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
170 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
| FPGA_HF_SIMULATOR_MODULATE_212K
);
172 /* Bitbang the receiver */
173 AT91C_BASE_PIOA
->PIO_ODR
= GPIO_SSC_DIN
;
174 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DIN
;
181 while(!BUTTON_PRESS()) {
182 int level
= !!(AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_DIN
);
183 int time
= timer
->TC_CV
;
185 if(level
!= old_level
) {
187 timer
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
188 if(FUZZ_EQUAL(time
, RWD_TIME_1
, RWD_TIME_FUZZ
)) {
193 } else if(FUZZ_EQUAL(time
, RWD_TIME_0
, RWD_TIME_FUZZ
)) {
207 if(time
>= (RWD_TIME_1
+RWD_TIME_FUZZ
) && active
) {
214 if(time
>= (20*RWD_TIME_1
) && (timer
->TC_SR
& AT91C_TC_CLKSTA
)) {
215 timer
->TC_CCR
= AT91C_TC_CLKDIS
;