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 /* Send a frame in reader mode, the FPGA must have been set up by
85 static void frame_send_rwd(uint16_t data
, int bits
)
88 timer
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
89 while(timer
->TC_CV
> 1) ; /* Wait till the clock has reset */
92 for(i
=0; i
<bits
; i
++) {
93 int starttime
= timer
->TC_CV
;
94 int pause_end
= starttime
+ RWD_TIME_PAUSE
, bit_end
;
99 bit_end
= starttime
+ RWD_TIME_1
;
101 bit_end
= starttime
+ RWD_TIME_0
;
104 /* RWD_TIME_PAUSE time off, then some time on, so that the complete bit time is
105 * RWD_TIME_x, where x is the bit to be transmitted */
106 AT91C_BASE_PIOA
->PIO_CODR
= GPIO_SSC_DOUT
;
107 while(timer
->TC_CV
< pause_end
) ;
108 AT91C_BASE_PIOA
->PIO_SODR
= GPIO_SSC_DOUT
;
109 while(timer
->TC_CV
< bit_end
) ;
113 /* One final pause to mark the end of the frame */
114 int pause_end
= timer
->TC_CV
+ RWD_TIME_PAUSE
;
115 AT91C_BASE_PIOA
->PIO_CODR
= GPIO_SSC_DOUT
;
116 while(timer
->TC_CV
< pause_end
) ;
117 AT91C_BASE_PIOA
->PIO_SODR
= GPIO_SSC_DOUT
;
120 /* Reset the timer, to measure time until the start of the tag frame */
121 timer
->TC_CCR
= AT91C_TC_SWTRG
;
124 /* Receive a frame from the card in reader emulation mode, the FPGA and
125 * timer must have been set up by LegicRfReader and frame_send_rwd.
127 * The LEGIC RF protocol from card to reader does not include explicit
128 * frame start/stop information or length information. The reader must
129 * know beforehand how many bits it wants to receive. (Notably: a card
130 * sending a stream of 0-bits is indistinguishable from no card present.)
132 * Receive methodology: There is a fancy correlator in hi_read_rx_xcorr, but
133 * I'm not smart enough to use it. Instead I have patched hi_read_tx to output
134 * the ADC signal with hysteresis on SSP_DIN. Bit-bang that signal and look
135 * for edges. Count the edges in each bit interval. If they are approximately
136 * 0 this was a 0-bit, if they are approximately equal to the number of edges
137 * expected for a 212kHz subcarrier, this was a 1-bit. For timing we use the
138 * timer that's still running from frame_send_rwd in order to get a synchronization
139 * with the frame that we just sent.
141 * FIXME: Because we're relying on the hysteresis to just do the right thing
142 * the range is severely reduced (and you'll probably also need a good antenna).
143 * So this should be fixed some time in the future for a proper receiver.
145 static void frame_receive_rwd(struct legic_frame
* const f
, int bits
)
147 uint16_t the_bit
= 1; /* Use a bitmask to save on shifts */
149 int i
, old_level
=0, edges
=0;
150 int next_bit_at
= TAG_TIME_WAIT
;
156 AT91C_BASE_PIOA
->PIO_ODR
= GPIO_SSC_DIN
;
157 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DIN
;
159 while(timer
->TC_CV
< next_bit_at
) ;
160 next_bit_at
+= TAG_TIME_BIT
;
162 for(i
=0; i
<bits
; i
++) {
164 while(timer
->TC_CV
< next_bit_at
) {
165 int level
= (AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_DIN
);
166 if(level
!= old_level
)
170 next_bit_at
+= TAG_TIME_BIT
;
172 if(edges
> 20 && edges
< 60) { /* expected are 42 edges */
184 /* Figure out a response to a frame in tag mode */
185 static void frame_respond_tag(struct legic_frame
const * const f
)
191 for(i
=0; i
<sizeof(queries
)/sizeof(queries
[0]); i
++) {
192 if(f
->bits
== queries
[i
].bits
&& f
->data
== queries
[i
].data
) {
193 r_data
= responses
[i
].data
;
194 r_size
= responses
[i
].bits
;
200 frame_send_tag(r_data
, r_size
);
209 static void frame_append_bit(struct legic_frame
* const f
, int bit
)
212 return; /* Overflow, won't happen */
213 f
->data
|= (bit
<<f
->bits
);
217 static int frame_is_empty(struct legic_frame
const * const f
)
219 return( f
->bits
<= 4 );
222 /* Handle (whether to respond) a frame in tag mode */
223 static void frame_handle_tag(struct legic_frame
const * const f
)
229 if( !frame_is_empty(f
) ) {
230 frame_respond_tag(f
);
234 static void frame_clean(struct legic_frame
* const f
)
241 EMIT_RWD
, /* Emit in tag simulation mode, e.g. the source is the RWD */
242 EMIT_TAG
/* Emit in reader simulation mode, e.g. the source is the TAG */
244 static void emit(enum emit_mode mode
, int bit
)
247 if(mode
== EMIT_RWD
) {
248 frame_handle_tag(¤t_frame
);
250 frame_clean(¤t_frame
);
251 } else if(bit
== 0) {
252 frame_append_bit(¤t_frame
, 0);
253 } else if(bit
== 1) {
254 frame_append_bit(¤t_frame
, 1);
258 void LegicRfSimulate(void)
260 /* ADC path high-frequency peak detector, FPGA in high-frequency simulator mode,
261 * modulation mode set to 212kHz subcarrier. We are getting the incoming raw
262 * envelope waveform on DIN and should send our response on DOUT.
264 * The LEGIC RF protocol is pulse-pause-encoding from reader to card, so we'll
265 * measure the time between two rising edges on DIN, and no encoding on the
266 * subcarrier from card to reader, so we'll just shift out our verbatim data
267 * on DOUT, 1 bit is 100us. The time from reader to card frame is still unclear,
268 * seems to be 300us-ish.
270 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
272 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
| FPGA_HF_SIMULATOR_MODULATE_212K
);
274 /* Bitbang the receiver */
275 AT91C_BASE_PIOA
->PIO_ODR
= GPIO_SSC_DIN
;
276 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DIN
;
283 while(!BUTTON_PRESS()) {
284 int level
= !!(AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_DIN
);
285 int time
= timer
->TC_CV
;
287 if(level
!= old_level
) {
289 timer
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
290 if(FUZZ_EQUAL(time
, RWD_TIME_1
, RWD_TIME_FUZZ
)) {
295 } else if(FUZZ_EQUAL(time
, RWD_TIME_0
, RWD_TIME_FUZZ
)) {
309 if(time
>= (RWD_TIME_1
+RWD_TIME_FUZZ
) && active
) {
316 if(time
>= (20*RWD_TIME_1
) && (timer
->TC_SR
& AT91C_TC_CLKSTA
)) {
317 timer
->TC_CCR
= AT91C_TC_CLKDIS
;
326 void LegicRfReader(void)
328 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
330 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX
);
332 /* Bitbang the transmitter */
333 AT91C_BASE_PIOA
->PIO_CODR
= GPIO_SSC_DOUT
;
334 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
335 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
339 while(!BUTTON_PRESS()) {
340 /* Switch on carrier and let the tag charge for 1ms */
341 AT91C_BASE_PIOA
->PIO_SODR
= GPIO_SSC_DOUT
;
345 frame_send_rwd(queries
[0].data
, queries
[0].bits
);
348 frame_clean(¤t_frame
);
350 frame_receive_rwd(¤t_frame
, responses
[0].bits
);
353 /* Switch off carrier, make sure tag is reset */
354 AT91C_BASE_PIOA
->PIO_CODR
= GPIO_SSC_DOUT
;