2 * LEGIC RF simulation code
4 * (c) 2009 Henryk Plötz <henryk@ploetzli.ch>
12 static struct legic_frame
{
18 static char queries
[][4] = {
19 {7, 0x55}, /* 1010 101 */
21 static char responses
[][4] = {
22 {6, 0x3b}, /* 1101 11 */
25 static void frame_send(char *response
, int num_bytes
, int num_bits
)
28 /* Use the SSC to send a response. 8-bit transfers, LSBit first, 100us per bit */
30 /* Bitbang the response */
31 AT91C_BASE_PIOA
->PIO_CODR
= GPIO_SSC_DOUT
;
32 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
33 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
35 /* Wait for the frame start */
36 while(AT91C_BASE_TC1
->TC_CV
< 490) ;
39 for(i
=0; i
<(num_bytes
*8+num_bits
); i
++) {
40 int nextbit
= AT91C_BASE_TC1
->TC_CV
+ 150;
41 int bit
= response
[i
/8] & (1<<(i
%8));
43 AT91C_BASE_PIOA
->PIO_SODR
= GPIO_SSC_DOUT
;
45 AT91C_BASE_PIOA
->PIO_CODR
= GPIO_SSC_DOUT
;
46 while(AT91C_BASE_TC1
->TC_CV
< nextbit
) ;
48 AT91C_BASE_PIOA
->PIO_CODR
= GPIO_SSC_DOUT
;
52 static void frame_respond(struct legic_frame
*f
)
55 int bitcount
= f
->num_bytes
*8+f
->num_bits
;
57 for(i
=0; i
<sizeof(queries
)/sizeof(queries
[0]); i
++) {
58 if(bitcount
== queries
[i
][0] && f
->data
[0] == queries
[i
][1] && f
->data
[1] == queries
[i
][2]) {
65 frame_send(&responses
[r
][1], responses
[r
][0]/8, responses
[r
][0]%8);
74 static void frame_append_bit(struct legic_frame
*f
, int bit
)
76 if(f
->num_bytes
>= (int)sizeof(f
->data
))
77 return; /* Overflow, won't happen */
78 f
->data
[f
->num_bytes
] |= (bit
<<f
->num_bits
);
86 static int frame_is_empty(struct legic_frame
*f
)
88 return( (f
->num_bytes
*8 + f
->num_bits
) <= 4 );
91 static void frame_handle(struct legic_frame
*f
)
93 if(f
->num_bytes
== 0 && f
->num_bits
== 6) {
97 if( !frame_is_empty(f
) ) {
102 static void frame_clean(struct legic_frame
*f
)
104 if(!frame_is_empty(f
))
105 /* memset(f->data, 0, sizeof(f->data)); */
106 f
->data
[0] = f
->data
[1] = 0;
111 static void emit(int bit
)
114 frame_handle(¤t_frame
);
115 frame_clean(¤t_frame
);
116 } else if(bit
== 0) {
117 frame_append_bit(¤t_frame
, 0);
118 } else if(bit
== 1) {
119 frame_append_bit(¤t_frame
, 1);
123 void LegicRfSimulate(void)
125 /* ADC path high-frequency peak detector, FPGA in high-frequency simulator mode,
126 * modulation mode set to 212kHz subcarrier. We are getting the incoming raw
127 * envelope waveform on DIN and should send our response on DOUT.
129 * The LEGIC RF protocol is pulse-pause-encoding from reader to card, so we'll
130 * measure the time between two rising edges on DIN, and no encoding on the
131 * subcarrier from card to reader, so we'll just shift out our verbatim data
132 * on DOUT, 1 bit is 100us. The time from reader to card frame is still unclear,
133 * seems to be 300us-ish.
135 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
137 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
| FPGA_HF_SIMULATOR_MODULATE_212K
);
139 /* Bitbang the receiver */
140 AT91C_BASE_PIOA
->PIO_ODR
= GPIO_SSC_DIN
;
141 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DIN
;
143 /* Set up Timer 1 to use for measuring time between pulses. Since we're bit-banging
144 * this it won't be terribly accurate but should be good enough.
146 AT91C_BASE_PMC
->PMC_PCER
= (1 << AT91C_ID_TC1
);
147 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKDIS
;
148 AT91C_BASE_TC1
->TC_CMR
= TC_CMR_TCCLKS_TIMER_CLOCK3
;
149 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
152 /* At TIMER_CLOCK3 (MCK/32) */
153 #define BIT_TIME_1 150
154 #define BIT_TIME_0 90
155 #define BIT_TIME_FUZZ 20
158 while(!BUTTON_PRESS()) {
159 int level
= !!(AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_DIN
);
160 int time
= AT91C_BASE_TC1
->TC_CV
;
162 if(level
!= old_level
) {
164 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKEN
| AT91C_TC_SWTRG
;
165 if(time
> (BIT_TIME_1
-BIT_TIME_FUZZ
) && time
< (BIT_TIME_1
+BIT_TIME_FUZZ
)) {
170 } else if(time
> (BIT_TIME_0
-BIT_TIME_FUZZ
) && time
< (BIT_TIME_0
+BIT_TIME_FUZZ
)) {
184 if(time
>= (BIT_TIME_1
+BIT_TIME_FUZZ
) && active
) {
191 if(time
>= (20*BIT_TIME_1
) && (AT91C_BASE_TC1
->TC_SR
& AT91C_TC_CLKSTA
)) {
192 AT91C_BASE_TC1
->TC_CCR
= AT91C_TC_CLKDIS
;