]>
Commit | Line | Data |
---|---|---|
bd20f8f4 | 1 | //----------------------------------------------------------------------------- |
2 | // (c) 2009 Henryk Plötz <henryk@ploetzli.ch> | |
9015ae0f | 3 | // 2016 Iceman |
bd20f8f4 | 4 | // |
5 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, | |
6 | // at your option, any later version. See the LICENSE.txt file for the text of | |
7 | // the license. | |
8 | //----------------------------------------------------------------------------- | |
9 | // LEGIC RF simulation code | |
10 | //----------------------------------------------------------------------------- | |
f7e3ed82 | 11 | #include "legicrf.h" |
8e220a91 | 12 | |
a7247d85 | 13 | static struct legic_frame { |
a3994421 | 14 | uint8_t bits; |
a2b1414f | 15 | uint32_t data; |
a7247d85 | 16 | } current_frame; |
8e220a91 | 17 | |
3612a8a8 | 18 | static enum { |
19 | STATE_DISCON, | |
20 | STATE_IV, | |
21 | STATE_CON, | |
22 | } legic_state; | |
23 | ||
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; | |
28 | ||
29 | static int legic_phase_drift; | |
30 | static int legic_frame_drift; | |
31 | static int legic_reqresp_drift; | |
8e220a91 | 32 | |
add16a62 | 33 | AT91PS_TC timer; |
3612a8a8 | 34 | AT91PS_TC prng_timer; |
add16a62 | 35 | |
ad5bc8cc | 36 | /* |
c71c5ee1 | 37 | static void setup_timer(void) { |
ad5bc8cc | 38 | // Set up Timer 1 to use for measuring time between pulses. Since we're bit-banging |
39 | // this it won't be terribly accurate but should be good enough. | |
40 | // | |
add16a62 | 41 | AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_TC1); |
42 | timer = AT91C_BASE_TC1; | |
43 | timer->TC_CCR = AT91C_TC_CLKDIS; | |
0aa4cfc2 | 44 | timer->TC_CMR = AT91C_TC_CLKS_TIMER_DIV3_CLOCK; |
add16a62 | 45 | timer->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG; |
46 | ||
ad5bc8cc | 47 | // |
48 | // Set up Timer 2 to use for measuring time between frames in | |
49 | // tag simulation mode. Runs 4x faster as Timer 1 | |
50 | // | |
3612a8a8 | 51 | AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_TC2); |
52 | prng_timer = AT91C_BASE_TC2; | |
53 | prng_timer->TC_CCR = AT91C_TC_CLKDIS; | |
54 | prng_timer->TC_CMR = AT91C_TC_CLKS_TIMER_DIV2_CLOCK; | |
55 | prng_timer->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG; | |
56 | } | |
111c6934 | 57 | |
58 | AT91C_BASE_PMC->PMC_PCER |= (0x1 << 12) | (0x1 << 13) | (0x1 << 14); | |
59 | AT91C_BASE_TCB->TCB_BMR = AT91C_TCB_TC0XC0S_NONE | AT91C_TCB_TC1XC1S_TIOA0 | AT91C_TCB_TC2XC2S_NONE; | |
60 | ||
61 | // fast clock | |
62 | AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS; // timer disable | |
63 | AT91C_BASE_TC0->TC_CMR = AT91C_TC_CLKS_TIMER_DIV3_CLOCK | // MCK(48MHz)/32 -- tick=1.5mks | |
64 | AT91C_TC_WAVE | AT91C_TC_WAVESEL_UP_AUTO | AT91C_TC_ACPA_CLEAR | | |
65 | AT91C_TC_ACPC_SET | AT91C_TC_ASWTRG_SET; | |
66 | AT91C_BASE_TC0->TC_RA = 1; | |
67 | AT91C_BASE_TC0->TC_RC = 0xBFFF + 1; // 0xC000 | |
68 | ||
ad5bc8cc | 69 | */ |
70 | ||
71 | // At TIMER_CLOCK3 (MCK/32) | |
22f4dca8 | 72 | // testing calculating in (us) microseconds. |
111c6934 | 73 | #define RWD_TIME_1 120 // READER_TIME_PAUSE 20us off, 80us on = 100us 80 * 1.5 == 120ticks |
74 | #define RWD_TIME_0 60 // READER_TIME_PAUSE 20us off, 40us on = 60us 40 * 1.5 == 60ticks | |
76471e5d | 75 | #define RWD_TIME_PAUSE 30 // 20us == 20 * 1.5 == 30ticks */ |
7a8db2f6 | 76 | #define TAG_BIT_PERIOD 142 // 100us == 100 * 1.5 == 150ticks |
111c6934 | 77 | #define TAG_FRAME_WAIT 495 // 330us from READER frame end to TAG frame start. 330 * 1.5 == 495 |
ad5bc8cc | 78 | |
76471e5d | 79 | #define RWD_TIME_FUZZ 20 // rather generous 13us, since the peak detector + hysteresis fuzz quite a bit |
add16a62 | 80 | |
3612a8a8 | 81 | #define SIM_DIVISOR 586 /* prng_time/SIM_DIVISOR count prng needs to be forwared */ |
82 | #define SIM_SHIFT 900 /* prng_time+SIM_SHIFT shift of delayed start */ | |
83 | ||
3612a8a8 | 84 | #define OFFSET_LOG 1024 |
add16a62 | 85 | |
86 | #define FUZZ_EQUAL(value, target, fuzz) ((value) > ((target)-(fuzz)) && (value) < ((target)+(fuzz))) | |
aac23b24 | 87 | |
ad5bc8cc | 88 | #ifndef SHORT_COIL |
9015ae0f | 89 | # define SHORT_COIL LOW(GPIO_SSC_DOUT); |
ad5bc8cc | 90 | #endif |
91 | #ifndef OPEN_COIL | |
b4a6775b | 92 | # define OPEN_COIL HIGH(GPIO_SSC_DOUT); |
ad5bc8cc | 93 | #endif |
e4a8d1e2 | 94 | #ifndef LINE_IN |
b8168868 | 95 | # define LINE_IN AT91C_BASE_PIOA->PIO_PER = GPIO_SSC_DIN; |
e4a8d1e2 | 96 | #endif |
111c6934 | 97 | // Pause pulse, off in 20us / 30ticks, |
98 | // ONE / ZERO bit pulse, | |
99 | // one == 80us / 120ticks | |
100 | // zero == 40us / 60ticks | |
101 | #ifndef COIL_PULSE | |
25d52dd2 | 102 | # define COIL_PULSE(x) \ |
103 | do { \ | |
76471e5d | 104 | SHORT_COIL; \ |
25d52dd2 | 105 | WaitTicks( (RWD_TIME_PAUSE) ); \ |
76471e5d | 106 | OPEN_COIL; \ |
22f4dca8 | 107 | WaitTicks((x)); \ |
9015ae0f | 108 | } while (0); |
111c6934 | 109 | #endif |
c71c5ee1 | 110 | |
111 | // ToDo: define a meaningful maximum size for auth_table. The bigger this is, the lower will be the available memory for traces. | |
112 | // Historically it used to be FREE_BUFFER_SIZE, which was 2744. | |
113 | #define LEGIC_CARD_MEMSIZE 1024 | |
114 | static uint8_t* cardmem; | |
115 | ||
faabfafe | 116 | static void frame_append_bit(struct legic_frame * const f, uint8_t bit) { |
b4a6775b | 117 | // Overflow, won't happen |
118 | if (f->bits >= 31) return; | |
119 | ||
120 | f->data |= (bit << f->bits); | |
121 | f->bits++; | |
122 | } | |
123 | ||
124 | static void frame_clean(struct legic_frame * const f) { | |
125 | f->data = 0; | |
126 | f->bits = 0; | |
127 | } | |
128 | ||
ad5bc8cc | 129 | // Prng works when waiting in 99.1us cycles. |
130 | // and while sending/receiving in bit frames (100, 60) | |
b4a6775b | 131 | /*static void CalibratePrng( uint32_t time){ |
ad5bc8cc | 132 | // Calculate Cycles based on timer 100us |
87342aad | 133 | uint32_t i = (time - sendFrameStop) / 100 ; |
ad5bc8cc | 134 | |
135 | // substract cycles of finished frames | |
136 | int k = i - legic_prng_count()+1; | |
137 | ||
138 | // substract current frame length, rewind to beginning | |
139 | if ( k > 0 ) | |
140 | legic_prng_forward(k); | |
141 | } | |
b4a6775b | 142 | */ |
ad5bc8cc | 143 | |
3612a8a8 | 144 | /* Generate Keystream */ |
22f4dca8 | 145 | uint32_t get_key_stream(int skip, int count) { |
633d0686 | 146 | |
c71c5ee1 | 147 | int i; |
edaf10af | 148 | |
c71c5ee1 | 149 | // Use int to enlarge timer tc to 32bit |
edaf10af | 150 | legic_prng_bc += prng_timer->TC_CV; |
c71c5ee1 | 151 | |
152 | // reset the prng timer. | |
edaf10af | 153 | |
154 | /* If skip == -1, forward prng time based */ | |
155 | if(skip == -1) { | |
c71c5ee1 | 156 | i = (legic_prng_bc + SIM_SHIFT)/SIM_DIVISOR; /* Calculate Cycles based on timer */ |
edaf10af | 157 | i -= legic_prng_count(); /* substract cycles of finished frames */ |
c71c5ee1 | 158 | i -= count; /* substract current frame length, rewind to beginning */ |
edaf10af | 159 | legic_prng_forward(i); |
160 | } else { | |
161 | legic_prng_forward(skip); | |
162 | } | |
163 | ||
edaf10af | 164 | i = (count == 6) ? -1 : legic_read_count; |
165 | ||
edaf10af | 166 | /* Generate KeyStream */ |
633d0686 | 167 | return legic_prng_get_bits(count); |
3612a8a8 | 168 | } |
169 | ||
170 | /* Send a frame in tag mode, the FPGA must have been set up by | |
171 | * LegicRfSimulate | |
172 | */ | |
633d0686 | 173 | void frame_send_tag(uint16_t response, uint8_t bits) { |
174 | ||
175 | uint16_t mask = 1; | |
176 | ||
ad5bc8cc | 177 | /* Bitbang the response */ |
633d0686 | 178 | SHORT_COIL; |
ad5bc8cc | 179 | AT91C_BASE_PIOA->PIO_OER = GPIO_SSC_DOUT; |
180 | AT91C_BASE_PIOA->PIO_PER = GPIO_SSC_DOUT; | |
3612a8a8 | 181 | |
633d0686 | 182 | /* TAG_FRAME_WAIT -> shift by 2 */ |
183 | legic_prng_forward(2); | |
184 | response ^= legic_prng_get_bits(bits); | |
c71c5ee1 | 185 | |
ad5bc8cc | 186 | /* Wait for the frame start */ |
633d0686 | 187 | WaitTicks( TAG_FRAME_WAIT ); |
8e220a91 | 188 | |
633d0686 | 189 | for (; mask < BITMASK(bits); mask <<= 1) { |
00271f77 | 190 | if (response & mask) |
b1cd7d5c | 191 | OPEN_COIL |
edaf10af | 192 | else |
b1cd7d5c | 193 | SHORT_COIL |
633d0686 | 194 | WaitTicks(TAG_BIT_PERIOD); |
ad5bc8cc | 195 | } |
633d0686 | 196 | SHORT_COIL; |
ad5bc8cc | 197 | } |
c71c5ee1 | 198 | |
ad5bc8cc | 199 | /* Send a frame in reader mode, the FPGA must have been set up by |
200 | * LegicRfReader | |
201 | */ | |
22f4dca8 | 202 | void frame_sendAsReader(uint32_t data, uint8_t bits){ |
c71c5ee1 | 203 | |
b8168868 | 204 | uint32_t starttime = GET_TICKS, send = 0, mask = 1; |
111c6934 | 205 | |
206 | // xor lsfr onto data. | |
207 | send = data ^ legic_prng_get_bits(bits); | |
ad5bc8cc | 208 | |
209 | for (; mask < BITMASK(bits); mask <<= 1) { | |
fabef615 | 210 | if (send & mask) |
9015ae0f | 211 | COIL_PULSE(RWD_TIME_1) |
fabef615 | 212 | else |
9015ae0f | 213 | COIL_PULSE(RWD_TIME_0) |
dcc10e5e | 214 | } |
e30c654b | 215 | |
76471e5d | 216 | // Final pause to mark the end of the frame |
76471e5d | 217 | COIL_PULSE(0); |
b4a6775b | 218 | |
fabef615 | 219 | // log |
7e7d3de5 | 220 | uint8_t cmdbytes[] = {bits, BYTEx(data,0), BYTEx(data,1), BYTEx(data,2), BYTEx(send,0), BYTEx(send,1), BYTEx(send,2)}; |
fabef615 | 221 | LogTrace(cmdbytes, sizeof(cmdbytes), starttime, GET_TICKS, NULL, TRUE); |
dcc10e5e | 222 | } |
223 | ||
224 | /* Receive a frame from the card in reader emulation mode, the FPGA and | |
ad5bc8cc | 225 | * timer must have been set up by LegicRfReader and frame_sendAsReader. |
e30c654b | 226 | * |
dcc10e5e | 227 | * The LEGIC RF protocol from card to reader does not include explicit |
228 | * frame start/stop information or length information. The reader must | |
229 | * know beforehand how many bits it wants to receive. (Notably: a card | |
230 | * sending a stream of 0-bits is indistinguishable from no card present.) | |
e30c654b | 231 | * |
dcc10e5e | 232 | * Receive methodology: There is a fancy correlator in hi_read_rx_xcorr, but |
233 | * I'm not smart enough to use it. Instead I have patched hi_read_tx to output | |
234 | * the ADC signal with hysteresis on SSP_DIN. Bit-bang that signal and look | |
235 | * for edges. Count the edges in each bit interval. If they are approximately | |
236 | * 0 this was a 0-bit, if they are approximately equal to the number of edges | |
237 | * expected for a 212kHz subcarrier, this was a 1-bit. For timing we use the | |
ad5bc8cc | 238 | * timer that's still running from frame_sendAsReader in order to get a synchronization |
dcc10e5e | 239 | * with the frame that we just sent. |
e30c654b | 240 | * |
241 | * FIXME: Because we're relying on the hysteresis to just do the right thing | |
dcc10e5e | 242 | * the range is severely reduced (and you'll probably also need a good antenna). |
e30c654b | 243 | * So this should be fixed some time in the future for a proper receiver. |
dcc10e5e | 244 | */ |
111c6934 | 245 | static void frame_receiveAsReader(struct legic_frame * const f, uint8_t bits) { |
ad5bc8cc | 246 | |
22f4dca8 | 247 | if ( bits > 32 ) return; |
3612a8a8 | 248 | |
22f4dca8 | 249 | uint8_t i = bits, edges = 0; |
d7e24e7c | 250 | uint32_t the_bit = 1, next_bit_at = 0, data = 0; |
fabef615 | 251 | uint32_t old_level = 0; |
252 | volatile uint32_t level = 0; | |
25d52dd2 | 253 | |
fabef615 | 254 | frame_clean(f); |
e4a8d1e2 | 255 | |
faabfafe | 256 | // calibrate the prng. |
b4a6775b | 257 | legic_prng_forward(2); |
c649c433 | 258 | data = legic_prng_get_bits(bits); |
b4a6775b | 259 | |
b4a6775b | 260 | //FIXED time between sending frame and now listening frame. 330us |
111c6934 | 261 | uint32_t starttime = GET_TICKS; |
0b0b182f | 262 | // its about 9+9 ticks delay from end-send to here. |
0b0b182f | 263 | WaitTicks( 477 ); |
faabfafe | 264 | |
c649c433 | 265 | next_bit_at = GET_TICKS + TAG_BIT_PERIOD; |
25d52dd2 | 266 | |
22f4dca8 | 267 | while ( i-- ){ |
dcc10e5e | 268 | edges = 0; |
111c6934 | 269 | while ( GET_TICKS < next_bit_at) { |
ad5bc8cc | 270 | |
b4a6775b | 271 | level = (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_DIN); |
ad5bc8cc | 272 | |
273 | if (level != old_level) | |
b4a6775b | 274 | ++edges; |
275 | ||
dcc10e5e | 276 | old_level = level; |
25d52dd2 | 277 | } |
278 | ||
ad5bc8cc | 279 | next_bit_at += TAG_BIT_PERIOD; |
3612a8a8 | 280 | |
fabef615 | 281 | // We expect 42 edges (ONE) |
faabfafe | 282 | if ( edges > 20 ) |
8e220a91 | 283 | data ^= the_bit; |
87342aad | 284 | |
285 | the_bit <<= 1; | |
dcc10e5e | 286 | } |
e30c654b | 287 | |
b4a6775b | 288 | // output |
dcc10e5e | 289 | f->data = data; |
290 | f->bits = bits; | |
db44e049 | 291 | |
fabef615 | 292 | // log |
cb7902cd | 293 | uint8_t cmdbytes[] = {bits, BYTEx(data, 0), BYTEx(data, 1)}; |
faabfafe | 294 | LogTrace(cmdbytes, sizeof(cmdbytes), starttime, GET_TICKS, NULL, FALSE); |
a7247d85 | 295 | } |
296 | ||
c71c5ee1 | 297 | // Setup pm3 as a Legic Reader |
87342aad | 298 | static uint32_t setup_phase_reader(uint8_t iv) { |
22f4dca8 | 299 | |
f7b42573 | 300 | // Switch on carrier and let the tag charge for 1ms |
ad5bc8cc | 301 | HIGH(GPIO_SSC_DOUT); |
77a689db | 302 | WaitUS(5000); |
ad5bc8cc | 303 | |
22f4dca8 | 304 | ResetTicks(); |
ad5bc8cc | 305 | |
f7b42573 | 306 | // no keystream yet |
c71c5ee1 | 307 | legic_prng_init(0); |
f7b42573 | 308 | |
ad5bc8cc | 309 | // send IV handshake |
310 | frame_sendAsReader(iv, 7); | |
311 | ||
312 | // Now both tag and reader has same IV. Prng can start. | |
3612a8a8 | 313 | legic_prng_init(iv); |
e30c654b | 314 | |
111c6934 | 315 | frame_receiveAsReader(¤t_frame, 6); |
f7b42573 | 316 | |
d7e24e7c | 317 | // 292us (438t) - fixed delay before sending ack. |
318 | // minus log and stuff 100tick? | |
319 | WaitTicks(338); | |
320 | legic_prng_forward(3); | |
ad5bc8cc | 321 | |
f7b42573 | 322 | // Send obsfuscated acknowledgment frame. |
ad5bc8cc | 323 | // 0x19 = 0x18 MIM22, 0x01 LSB READCMD |
324 | // 0x39 = 0x38 MIM256, MIM1024 0x01 LSB READCMD | |
325 | switch ( current_frame.data ) { | |
87342aad | 326 | case 0x0D: frame_sendAsReader(0x19, 6); break; |
327 | case 0x1D: | |
328 | case 0x3D: frame_sendAsReader(0x39, 6); break; | |
329 | default: break; | |
f7b42573 | 330 | } |
d7e24e7c | 331 | |
332 | legic_prng_forward(2); | |
8e220a91 | 333 | return current_frame.data; |
2561caa2 | 334 | } |
335 | ||
22f4dca8 | 336 | static void LegicCommonInit(void) { |
337 | ||
7cc204bf | 338 | FpgaDownloadAndGo(FPGA_BITSTREAM_HF); |
b4a6775b | 339 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX); |
dcc10e5e | 340 | SetAdcMuxFor(GPIO_MUXSEL_HIPKD); |
e30c654b | 341 | |
dcc10e5e | 342 | /* Bitbang the transmitter */ |
b8168868 | 343 | SHORT_COIL; |
dcc10e5e | 344 | AT91C_BASE_PIOA->PIO_OER = GPIO_SSC_DOUT; |
345 | AT91C_BASE_PIOA->PIO_PER = GPIO_SSC_DOUT; | |
b8168868 | 346 | AT91C_BASE_PIOA->PIO_ODR = GPIO_SSC_DIN; |
347 | ||
c71c5ee1 | 348 | // reserve a cardmem, meaning we can use the tracelog function in bigbuff easier. |
0b0b182f | 349 | cardmem = BigBuf_get_EM_addr(); |
c71c5ee1 | 350 | memset(cardmem, 0x00, LEGIC_CARD_MEMSIZE); |
351 | ||
352 | clear_trace(); | |
353 | set_tracing(TRUE); | |
8e220a91 | 354 | crc_init(&legic_crc, 4, 0x19 >> 1, 0x5, 0); |
ad5bc8cc | 355 | |
22f4dca8 | 356 | StartTicks(); |
8e220a91 | 357 | } |
358 | ||
111c6934 | 359 | // Switch off carrier, make sure tag is reset |
c71c5ee1 | 360 | static void switch_off_tag_rwd(void) { |
b8168868 | 361 | SHORT_COIL; |
3e750be3 | 362 | WaitUS(20); |
8e220a91 | 363 | WDT_HIT(); |
364 | } | |
c71c5ee1 | 365 | |
f7b42573 | 366 | // calculate crc4 for a legic READ command |
fabef615 | 367 | static uint32_t legic4Crc(uint8_t cmd, uint16_t byte_index, uint8_t value, uint8_t cmd_sz) { |
ad5bc8cc | 368 | crc_clear(&legic_crc); |
fabef615 | 369 | uint32_t temp = (value << cmd_sz) | (byte_index << 1) | cmd; |
cb7902cd | 370 | crc_update(&legic_crc, temp, cmd_sz + 8 ); |
8e220a91 | 371 | return crc_finish(&legic_crc); |
372 | } | |
373 | ||
fabef615 | 374 | int legic_read_byte( uint16_t index, uint8_t cmd_sz) { |
8e220a91 | 375 | |
fabef615 | 376 | uint8_t byte, crc, calcCrc = 0; |
377 | uint32_t cmd = (index << 1) | LEGIC_READ; | |
635d6e9b | 378 | |
379 | // 90ticks = 60us (should be 100us but crc calc takes time.) | |
380 | //WaitTicks(330); // 330ticks prng(4) - works | |
381 | WaitTicks(240); // 240ticks prng(3) - works | |
3e750be3 | 382 | |
ad5bc8cc | 383 | frame_sendAsReader(cmd, cmd_sz); |
111c6934 | 384 | frame_receiveAsReader(¤t_frame, 12); |
c71c5ee1 | 385 | |
c649c433 | 386 | // CRC check. |
111c6934 | 387 | byte = BYTEx(current_frame.data, 0); |
cb7902cd | 388 | crc = BYTEx(current_frame.data, 1); |
fabef615 | 389 | calcCrc = legic4Crc(LEGIC_READ, index, byte, cmd_sz); |
65c2d21d | 390 | |
cb7902cd | 391 | if( calcCrc != crc ) { |
b8168868 | 392 | Dbprintf("!!! crc mismatch: %x != %x !!!", calcCrc, crc); |
cb7902cd | 393 | return -1; |
394 | } | |
d7e24e7c | 395 | |
c15e07f1 | 396 | legic_prng_forward(3); |
8e220a91 | 397 | return byte; |
398 | } | |
399 | ||
c71c5ee1 | 400 | /* |
401 | * - assemble a write_cmd_frame with crc and send it | |
402 | * - wait until the tag sends back an ACK ('1' bit unencrypted) | |
403 | * - forward the prng based on the timing | |
8e220a91 | 404 | */ |
b8168868 | 405 | bool legic_write_byte(uint16_t index, uint8_t byte, uint8_t addr_sz) { |
406 | ||
407 | bool isOK = false; | |
715bed50 | 408 | int8_t i = 40; |
409 | uint8_t edges = 0; | |
b8168868 | 410 | uint8_t cmd_sz = addr_sz+1+8+4; //crc+data+cmd; |
411 | uint32_t steps = 0, next_bit_at, start, crc, old_level = 0; | |
c71c5ee1 | 412 | |
b8168868 | 413 | crc = legic4Crc(LEGIC_WRITE, index, byte, addr_sz+1); |
414 | ||
c71c5ee1 | 415 | // send write command |
c2ab5e8c | 416 | uint32_t cmd = LEGIC_WRITE; |
417 | cmd |= index << 1; // index | |
418 | cmd |= byte << (addr_sz+1); // Data | |
419 | cmd |= (crc & 0xF ) << (addr_sz+1+8); // CRC | |
c71c5ee1 | 420 | |
4409bf6e | 421 | WaitTicks(240); |
c71c5ee1 | 422 | |
ad5bc8cc | 423 | frame_sendAsReader(cmd, cmd_sz); |
b8168868 | 424 | |
e4a8d1e2 | 425 | LINE_IN; |
3612a8a8 | 426 | |
b8168868 | 427 | start = GET_TICKS; |
3e134b4c | 428 | |
b8168868 | 429 | // ACK, - one single "1" bit after 3.6ms |
430 | // 3.6ms = 3600us * 1.5 = 5400ticks. | |
7e7d3de5 | 431 | WaitTicks(5400); |
27c4a862 | 432 | |
b8168868 | 433 | next_bit_at = GET_TICKS + TAG_BIT_PERIOD; |
434 | ||
435 | while ( i-- ) { | |
436 | WDT_HIT(); | |
3612a8a8 | 437 | edges = 0; |
27c4a862 | 438 | while ( GET_TICKS < next_bit_at) { |
b8168868 | 439 | |
0b0b182f | 440 | volatile uint32_t level = (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_DIN); |
b8168868 | 441 | |
442 | if (level != old_level) | |
443 | ++edges; | |
111c6934 | 444 | |
3612a8a8 | 445 | old_level = level; |
446 | } | |
b8168868 | 447 | |
448 | next_bit_at += TAG_BIT_PERIOD; | |
449 | ||
450 | // We expect 42 edges (ONE) | |
0e8cabed | 451 | if(edges > 20 ) { |
b8168868 | 452 | steps = ( (GET_TICKS - start) / TAG_BIT_PERIOD); |
453 | legic_prng_forward(steps); | |
454 | isOK = true; | |
455 | goto OUT; | |
3612a8a8 | 456 | } |
457 | } | |
715bed50 | 458 | |
b8168868 | 459 | OUT: ; |
7e7d3de5 | 460 | legic_prng_forward(1); |
461 | ||
c2ab5e8c | 462 | uint8_t cmdbytes[] = {1, isOK, BYTEx(steps, 0), BYTEx(steps, 1) }; |
b8168868 | 463 | LogTrace(cmdbytes, sizeof(cmdbytes), start, GET_TICKS, NULL, FALSE); |
464 | return isOK; | |
3612a8a8 | 465 | } |
8e220a91 | 466 | |
fabef615 | 467 | int LegicRfReader(uint16_t offset, uint16_t len, uint8_t iv) { |
3e134b4c | 468 | |
fabef615 | 469 | uint16_t i = 0; |
a3994421 | 470 | uint8_t isOK = 1; |
471 | legic_card_select_t card; | |
472 | ||
8e220a91 | 473 | LegicCommonInit(); |
faabfafe | 474 | |
fabef615 | 475 | if ( legic_select_card_iv(&card, iv) ) { |
a3994421 | 476 | isOK = 0; |
477 | goto OUT; | |
478 | } | |
cb7902cd | 479 | |
539fd59e | 480 | if (len + offset > card.cardsize) |
fabef615 | 481 | len = card.cardsize - offset; |
a2b1414f | 482 | |
3612a8a8 | 483 | LED_B_ON(); |
c15e07f1 | 484 | while (i < len) { |
fabef615 | 485 | int r = legic_read_byte(offset + i, card.cmdsize); |
ad5bc8cc | 486 | |
487 | if (r == -1 || BUTTON_PRESS()) { | |
fabef615 | 488 | if ( MF_DBGLEVEL >= 2) DbpString("operation aborted"); |
87342aad | 489 | isOK = 0; |
490 | goto OUT; | |
a2b1414f | 491 | } |
fabef615 | 492 | cardmem[i++] = r; |
3612a8a8 | 493 | WDT_HIT(); |
2561caa2 | 494 | } |
c71c5ee1 | 495 | |
87342aad | 496 | OUT: |
faabfafe | 497 | WDT_HIT(); |
3612a8a8 | 498 | switch_off_tag_rwd(); |
c71c5ee1 | 499 | LEDsoff(); |
86087eba | 500 | cmd_send(CMD_ACK, isOK, len, 0, cardmem, len); |
3612a8a8 | 501 | return 0; |
502 | } | |
503 | ||
0e8cabed | 504 | void LegicRfWriter(uint16_t offset, uint16_t len, uint8_t iv, uint8_t *data) { |
117d9ec2 | 505 | |
f0fa6638 | 506 | #define LOWERLIMIT 4 |
539fd59e | 507 | uint8_t isOK = 1, msg = 0; |
f0fa6638 | 508 | legic_card_select_t card; |
0e8cabed | 509 | |
f0fa6638 | 510 | // uid NOT is writeable. |
511 | if ( offset <= LOWERLIMIT ) { | |
0e8cabed | 512 | isOK = 0; |
513 | goto OUT; | |
514 | } | |
515 | ||
fabef615 | 516 | LegicCommonInit(); |
c71c5ee1 | 517 | |
fabef615 | 518 | if ( legic_select_card_iv(&card, iv) ) { |
519 | isOK = 0; | |
539fd59e | 520 | msg = 1; |
fabef615 | 521 | goto OUT; |
522 | } | |
c71c5ee1 | 523 | |
539fd59e | 524 | if ( len + offset > card.cardsize) |
525 | len = card.cardsize - offset; | |
0e8cabed | 526 | |
527 | LED_B_ON(); | |
f0fa6638 | 528 | while( len > 0 ) { |
c2ab5e8c | 529 | --len; |
530 | if ( !legic_write_byte( len + offset, data[len], card.addrsize) ) { | |
4409bf6e | 531 | Dbprintf("operation failed | %02X | %02X | %02X", len + offset, len, data[len] ); |
fabef615 | 532 | isOK = 0; |
533 | goto OUT; | |
3612a8a8 | 534 | } |
0e8cabed | 535 | WDT_HIT(); |
3e134b4c | 536 | } |
fabef615 | 537 | OUT: |
539fd59e | 538 | cmd_send(CMD_ACK, isOK, msg,0,0,0); |
fabef615 | 539 | switch_off_tag_rwd(); |
540 | LEDsoff(); | |
3e134b4c | 541 | } |
542 | ||
fabef615 | 543 | int legic_select_card_iv(legic_card_select_t *p_card, uint8_t iv){ |
3e750be3 | 544 | |
a3994421 | 545 | if ( p_card == NULL ) return 1; |
3e750be3 | 546 | |
fabef615 | 547 | p_card->tagtype = setup_phase_reader(iv); |
a3994421 | 548 | |
549 | switch(p_card->tagtype) { | |
3e750be3 | 550 | case 0x0d: |
a3994421 | 551 | p_card->cmdsize = 6; |
fabef615 | 552 | p_card->addrsize = 5; |
a3994421 | 553 | p_card->cardsize = 22; |
3e750be3 | 554 | break; |
555 | case 0x1d: | |
a3994421 | 556 | p_card->cmdsize = 9; |
fabef615 | 557 | p_card->addrsize = 8; |
a3994421 | 558 | p_card->cardsize = 256; |
3e750be3 | 559 | break; |
560 | case 0x3d: | |
a3994421 | 561 | p_card->cmdsize = 11; |
fabef615 | 562 | p_card->addrsize = 10; |
a3994421 | 563 | p_card->cardsize = 1024; |
3e750be3 | 564 | break; |
565 | default: | |
a3994421 | 566 | p_card->cmdsize = 0; |
fabef615 | 567 | p_card->addrsize = 0; |
a3994421 | 568 | p_card->cardsize = 0; |
569 | return 2; | |
a3994421 | 570 | } |
571 | return 0; | |
572 | } | |
fabef615 | 573 | int legic_select_card(legic_card_select_t *p_card){ |
574 | return legic_select_card_iv(p_card, 0x01); | |
575 | } | |
a3994421 | 576 | |
0e8cabed | 577 | //----------------------------------------------------------------------------- |
578 | // Work with emulator memory | |
579 | // | |
580 | // Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_HF) here although FPGA is not | |
581 | // involved in dealing with emulator memory. But if it is called later, it might | |
582 | // destroy the Emulator Memory. | |
583 | //----------------------------------------------------------------------------- | |
584 | // arg0 = offset | |
585 | // arg1 = num of bytes | |
586 | void LegicEMemSet(uint32_t arg0, uint32_t arg1, uint8_t *data) { | |
587 | FpgaDownloadAndGo(FPGA_BITSTREAM_HF); | |
588 | legic_emlset_mem(data, arg0, arg1); | |
589 | } | |
590 | // arg0 = offset | |
591 | // arg1 = num of bytes | |
592 | void LegicEMemGet(uint32_t arg0, uint32_t arg1) { | |
593 | FpgaDownloadAndGo(FPGA_BITSTREAM_HF); | |
594 | uint8_t buf[USB_CMD_DATA_SIZE] = {0x00}; | |
595 | legic_emlget_mem(buf, arg0, arg1); | |
596 | LED_B_ON(); | |
597 | cmd_send(CMD_ACK, arg0, arg1, 0, buf, USB_CMD_DATA_SIZE); | |
598 | LED_B_OFF(); | |
599 | } | |
600 | void legic_emlset_mem(uint8_t *data, int offset, int numofbytes) { | |
601 | cardmem = BigBuf_get_EM_addr(); | |
602 | memcpy(cardmem + offset, data, numofbytes); | |
603 | } | |
604 | void legic_emlget_mem(uint8_t *data, int offset, int numofbytes) { | |
605 | cardmem = BigBuf_get_EM_addr(); | |
606 | memcpy(data, cardmem + offset, numofbytes); | |
607 | } | |
608 | ||
a3994421 | 609 | void LegicRfInfo(void){ |
610 | ||
0e8cabed | 611 | int r; |
612 | ||
a3994421 | 613 | uint8_t buf[sizeof(legic_card_select_t)] = {0x00}; |
614 | legic_card_select_t *card = (legic_card_select_t*) buf; | |
615 | ||
616 | LegicCommonInit(); | |
c649c433 | 617 | |
a3994421 | 618 | if ( legic_select_card(card) ) { |
619 | cmd_send(CMD_ACK,0,0,0,0,0); | |
620 | goto OUT; | |
3e750be3 | 621 | } |
622 | ||
fabef615 | 623 | // read UID bytes |
a3994421 | 624 | for ( uint8_t i = 0; i < sizeof(card->uid); ++i) { |
0e8cabed | 625 | r = legic_read_byte(i, card->cmdsize); |
3e750be3 | 626 | if ( r == -1 ) { |
627 | cmd_send(CMD_ACK,0,0,0,0,0); | |
628 | goto OUT; | |
629 | } | |
a3994421 | 630 | card->uid[i] = r & 0xFF; |
3e750be3 | 631 | } |
632 | ||
0e8cabed | 633 | // MCC byte. |
634 | r = legic_read_byte(4, card->cmdsize); | |
635 | uint32_t calc_mcc = CRC8Legic(card->uid, 4);; | |
636 | if ( r != calc_mcc) { | |
637 | cmd_send(CMD_ACK,0,0,0,0,0); | |
638 | goto OUT; | |
639 | } | |
640 | ||
641 | // OK | |
fabef615 | 642 | cmd_send(CMD_ACK, 1, 0, 0, buf, sizeof(legic_card_select_t)); |
635d6e9b | 643 | |
a3994421 | 644 | OUT: |
3e750be3 | 645 | switch_off_tag_rwd(); |
646 | LEDsoff(); | |
3e750be3 | 647 | } |
648 | ||
c71c5ee1 | 649 | /* Handle (whether to respond) a frame in tag mode |
650 | * Only called when simulating a tag. | |
651 | */ | |
3612a8a8 | 652 | static void frame_handle_tag(struct legic_frame const * const f) |
653 | { | |
e4a8d1e2 | 654 | // log |
655 | //uint8_t cmdbytes[] = {bits, BYTEx(data, 0), BYTEx(data, 1)}; | |
656 | //LogTrace(cmdbytes, sizeof(cmdbytes), starttime, GET_TICKS, NULL, FALSE); | |
657 | ||
658 | cardmem = BigBuf_get_EM_addr(); | |
117d9ec2 | 659 | |
633d0686 | 660 | /* First Part of Handshake (IV) */ |
661 | if(f->bits == 7) { | |
662 | ||
663 | LED_C_ON(); | |
c71c5ee1 | 664 | |
ad5bc8cc | 665 | // Reset prng timer |
22f4dca8 | 666 | ResetTimer(prng_timer); |
633d0686 | 667 | |
e4a8d1e2 | 668 | // IV from reader. |
633d0686 | 669 | legic_prng_init(f->data); |
e4a8d1e2 | 670 | |
671 | // We should have three tagtypes with three different answers. | |
633d0686 | 672 | frame_send_tag(0x3d, 6); /* 0x3d^0x26 = 0x1B */ |
e4a8d1e2 | 673 | |
633d0686 | 674 | legic_state = STATE_IV; |
675 | legic_read_count = 0; | |
676 | legic_prng_bc = 0; | |
677 | legic_prng_iv = f->data; | |
678 | ||
679 | ||
22f4dca8 | 680 | ResetTimer(timer); |
681 | WaitUS(280); | |
633d0686 | 682 | return; |
683 | } | |
3612a8a8 | 684 | |
685 | /* 0x19==??? */ | |
686 | if(legic_state == STATE_IV) { | |
e4a8d1e2 | 687 | uint32_t local_key = get_key_stream(3, 6); |
cc708897 | 688 | int xored = 0x39 ^ local_key; |
689 | if((f->bits == 6) && (f->data == xored)) { | |
3612a8a8 | 690 | legic_state = STATE_CON; |
691 | ||
22f4dca8 | 692 | ResetTimer(timer); |
693 | WaitUS(200); | |
3612a8a8 | 694 | return; |
111c6934 | 695 | |
696 | } else { | |
3612a8a8 | 697 | legic_state = STATE_DISCON; |
698 | LED_C_OFF(); | |
cc708897 | 699 | Dbprintf("iv: %02x frame: %02x key: %02x xored: %02x", legic_prng_iv, f->data, local_key, xored); |
3612a8a8 | 700 | return; |
701 | } | |
702 | } | |
703 | ||
704 | /* Read */ | |
705 | if(f->bits == 11) { | |
706 | if(legic_state == STATE_CON) { | |
e4a8d1e2 | 707 | uint32_t key = get_key_stream(2, 11); //legic_phase_drift, 11); |
708 | uint16_t addr = f->data ^ key; | |
709 | addr >>= 1; | |
710 | uint8_t data = cardmem[addr]; | |
111c6934 | 711 | int hash = legic4Crc(LEGIC_READ, addr, data, 11) << 8; |
3612a8a8 | 712 | |
e4a8d1e2 | 713 | legic_read_count++; |
3612a8a8 | 714 | legic_prng_forward(legic_reqresp_drift); |
715 | ||
633d0686 | 716 | frame_send_tag(hash | data, 12); |
22f4dca8 | 717 | ResetTimer(timer); |
cc708897 | 718 | legic_prng_forward(2); |
e4a8d1e2 | 719 | WaitTicks(330); |
3612a8a8 | 720 | return; |
721 | } | |
722 | } | |
723 | ||
724 | /* Write */ | |
539fd59e | 725 | if (f->bits == 23 || f->bits == 21 ) { |
e4a8d1e2 | 726 | uint32_t key = get_key_stream(-1, 23); //legic_frame_drift, 23); |
727 | uint16_t addr = f->data ^ key; | |
728 | addr >>= 1; | |
729 | addr &= 0x3ff; | |
730 | uint32_t data = f->data ^ key; | |
731 | data >>= 11; | |
732 | data &= 0xff; | |
733 | ||
734 | cardmem[addr] = data; | |
3612a8a8 | 735 | /* write command */ |
736 | legic_state = STATE_DISCON; | |
737 | LED_C_OFF(); | |
738 | Dbprintf("write - addr: %x, data: %x", addr, data); | |
539fd59e | 739 | // should send a ACK after 3.6ms |
3612a8a8 | 740 | return; |
741 | } | |
742 | ||
743 | if(legic_state != STATE_DISCON) { | |
744 | Dbprintf("Unexpected: sz:%u, Data:%03.3x, State:%u, Count:%u", f->bits, f->data, legic_state, legic_read_count); | |
3612a8a8 | 745 | Dbprintf("IV: %03.3x", legic_prng_iv); |
3612a8a8 | 746 | } |
e4a8d1e2 | 747 | |
3612a8a8 | 748 | legic_state = STATE_DISCON; |
749 | legic_read_count = 0; | |
750 | SpinDelay(10); | |
751 | LED_C_OFF(); | |
752 | return; | |
753 | } | |
754 | ||
755 | /* Read bit by bit untill full frame is received | |
756 | * Call to process frame end answer | |
757 | */ | |
c71c5ee1 | 758 | static void emit(int bit) { |
759 | ||
760 | switch (bit) { | |
761 | case 1: | |
762 | frame_append_bit(¤t_frame, 1); | |
763 | break; | |
764 | case 0: | |
765 | frame_append_bit(¤t_frame, 0); | |
766 | break; | |
767 | default: | |
768 | if(current_frame.bits <= 4) { | |
769 | frame_clean(¤t_frame); | |
770 | } else { | |
771 | frame_handle_tag(¤t_frame); | |
772 | frame_clean(¤t_frame); | |
773 | } | |
774 | WDT_HIT(); | |
775 | break; | |
776 | } | |
3612a8a8 | 777 | } |
778 | ||
779 | void LegicRfSimulate(int phase, int frame, int reqresp) | |
780 | { | |
781 | /* ADC path high-frequency peak detector, FPGA in high-frequency simulator mode, | |
782 | * modulation mode set to 212kHz subcarrier. We are getting the incoming raw | |
783 | * envelope waveform on DIN and should send our response on DOUT. | |
784 | * | |
785 | * The LEGIC RF protocol is pulse-pause-encoding from reader to card, so we'll | |
786 | * measure the time between two rising edges on DIN, and no encoding on the | |
787 | * subcarrier from card to reader, so we'll just shift out our verbatim data | |
788 | * on DOUT, 1 bit is 100us. The time from reader to card frame is still unclear, | |
cd79d972 | 789 | * seems to be 330us. |
3612a8a8 | 790 | */ |
e4a8d1e2 | 791 | |
792 | int old_level = 0, active = 0; | |
793 | legic_state = STATE_DISCON; | |
3612a8a8 | 794 | |
c71c5ee1 | 795 | legic_phase_drift = phase; |
796 | legic_frame_drift = frame; | |
797 | legic_reqresp_drift = reqresp; | |
798 | ||
799 | FpgaDownloadAndGo(FPGA_BITSTREAM_HF); | |
800 | SetAdcMuxFor(GPIO_MUXSEL_HIPKD); | |
c71c5ee1 | 801 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR | FPGA_HF_SIMULATOR_MODULATE_212K); |
802 | ||
803 | /* Bitbang the receiver */ | |
e4a8d1e2 | 804 | LINE_IN; |
805 | ||
806 | // need a way to determine which tagtype we are simulating | |
807 | ||
808 | // hook up emulator memory | |
809 | cardmem = BigBuf_get_EM_addr(); | |
810 | ||
811 | clear_trace(); | |
812 | set_tracing(TRUE); | |
c71c5ee1 | 813 | |
c71c5ee1 | 814 | crc_init(&legic_crc, 4, 0x19 >> 1, 0x5, 0); |
815 | ||
e4a8d1e2 | 816 | StartTicks(); |
c71c5ee1 | 817 | |
818 | LED_B_ON(); | |
819 | DbpString("Starting Legic emulator, press button to end"); | |
3612a8a8 | 820 | |
c71c5ee1 | 821 | while(!BUTTON_PRESS() && !usb_poll_validate_length()) { |
e4a8d1e2 | 822 | volatile uint32_t level = !!(AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_DIN); |
823 | ||
824 | uint32_t time = GET_TICKS; | |
825 | ||
826 | if (level != old_level) { | |
827 | ||
828 | if (level) { | |
c71c5ee1 | 829 | |
e4a8d1e2 | 830 | ResetTicks(); |
c71c5ee1 | 831 | |
832 | if (FUZZ_EQUAL(time, RWD_TIME_1, RWD_TIME_FUZZ)) { | |
833 | /* 1 bit */ | |
834 | emit(1); | |
835 | active = 1; | |
836 | LED_A_ON(); | |
837 | } else if (FUZZ_EQUAL(time, RWD_TIME_0, RWD_TIME_FUZZ)) { | |
838 | /* 0 bit */ | |
839 | emit(0); | |
840 | active = 1; | |
841 | LED_A_ON(); | |
842 | } else if (active) { | |
843 | /* invalid */ | |
844 | emit(-1); | |
845 | active = 0; | |
846 | LED_A_OFF(); | |
847 | } | |
848 | } | |
849 | } | |
3612a8a8 | 850 | |
c71c5ee1 | 851 | /* Frame end */ |
852 | if(time >= (RWD_TIME_1+RWD_TIME_FUZZ) && active) { | |
853 | emit(-1); | |
854 | active = 0; | |
855 | LED_A_OFF(); | |
856 | } | |
a2b1414f | 857 | |
e4a8d1e2 | 858 | /* |
859 | * Disable the counter, Then wait for the clock to acknowledge the | |
860 | * shutdown in its status register. Reading the SR has the | |
861 | * side-effect of clearing any pending state in there. | |
862 | */ | |
863 | if(time >= (20*RWD_TIME_1) && (timer->TC_SR & AT91C_TC_CLKSTA)) | |
864 | StopTicks(); | |
c71c5ee1 | 865 | |
866 | old_level = level; | |
867 | WDT_HIT(); | |
868 | } | |
e4a8d1e2 | 869 | |
870 | WDT_HIT(); | |
871 | switch_off_tag_rwd(); | |
c71c5ee1 | 872 | LEDsoff(); |
e4a8d1e2 | 873 | cmd_send(CMD_ACK, 1, 0, 0, 0, 0); |
c71c5ee1 | 874 | } |
3e134b4c | 875 | |
3e134b4c | 876 | //----------------------------------------------------------------------------- |
877 | // Code up a string of octets at layer 2 (including CRC, we don't generate | |
878 | // that here) so that they can be transmitted to the reader. Doesn't transmit | |
879 | // them yet, just leaves them ready to send in ToSend[]. | |
880 | //----------------------------------------------------------------------------- | |
881 | // static void CodeLegicAsTag(const uint8_t *cmd, int len) | |
882 | // { | |
883 | // int i; | |
884 | ||
885 | // ToSendReset(); | |
886 | ||
887 | // // Transmit a burst of ones, as the initial thing that lets the | |
888 | // // reader get phase sync. This (TR1) must be > 80/fs, per spec, | |
889 | // // but tag that I've tried (a Paypass) exceeds that by a fair bit, | |
890 | // // so I will too. | |
891 | // for(i = 0; i < 20; i++) { | |
892 | // ToSendStuffBit(1); | |
893 | // ToSendStuffBit(1); | |
894 | // ToSendStuffBit(1); | |
895 | // ToSendStuffBit(1); | |
896 | // } | |
897 | ||
898 | // // Send SOF. | |
899 | // for(i = 0; i < 10; i++) { | |
900 | // ToSendStuffBit(0); | |
901 | // ToSendStuffBit(0); | |
902 | // ToSendStuffBit(0); | |
903 | // ToSendStuffBit(0); | |
904 | // } | |
905 | // for(i = 0; i < 2; i++) { | |
906 | // ToSendStuffBit(1); | |
907 | // ToSendStuffBit(1); | |
908 | // ToSendStuffBit(1); | |
909 | // ToSendStuffBit(1); | |
910 | // } | |
911 | ||
912 | // for(i = 0; i < len; i++) { | |
913 | // int j; | |
914 | // uint8_t b = cmd[i]; | |
915 | ||
916 | // // Start bit | |
917 | // ToSendStuffBit(0); | |
918 | // ToSendStuffBit(0); | |
919 | // ToSendStuffBit(0); | |
920 | // ToSendStuffBit(0); | |
921 | ||
922 | // // Data bits | |
923 | // for(j = 0; j < 8; j++) { | |
924 | // if(b & 1) { | |
925 | // ToSendStuffBit(1); | |
926 | // ToSendStuffBit(1); | |
927 | // ToSendStuffBit(1); | |
928 | // ToSendStuffBit(1); | |
929 | // } else { | |
930 | // ToSendStuffBit(0); | |
931 | // ToSendStuffBit(0); | |
932 | // ToSendStuffBit(0); | |
933 | // ToSendStuffBit(0); | |
934 | // } | |
935 | // b >>= 1; | |
936 | // } | |
937 | ||
938 | // // Stop bit | |
939 | // ToSendStuffBit(1); | |
940 | // ToSendStuffBit(1); | |
941 | // ToSendStuffBit(1); | |
942 | // ToSendStuffBit(1); | |
943 | // } | |
944 | ||
945 | // // Send EOF. | |
946 | // for(i = 0; i < 10; i++) { | |
947 | // ToSendStuffBit(0); | |
948 | // ToSendStuffBit(0); | |
949 | // ToSendStuffBit(0); | |
950 | // ToSendStuffBit(0); | |
951 | // } | |
952 | // for(i = 0; i < 2; i++) { | |
953 | // ToSendStuffBit(1); | |
954 | // ToSendStuffBit(1); | |
955 | // ToSendStuffBit(1); | |
956 | // ToSendStuffBit(1); | |
957 | // } | |
958 | ||
959 | // // Convert from last byte pos to length | |
960 | // ToSendMax++; | |
961 | // } | |
962 | ||
963 | //----------------------------------------------------------------------------- | |
964 | // The software UART that receives commands from the reader, and its state | |
965 | // variables. | |
966 | //----------------------------------------------------------------------------- | |
62577a62 | 967 | /* |
3e134b4c | 968 | static struct { |
969 | enum { | |
970 | STATE_UNSYNCD, | |
971 | STATE_GOT_FALLING_EDGE_OF_SOF, | |
972 | STATE_AWAITING_START_BIT, | |
973 | STATE_RECEIVING_DATA | |
974 | } state; | |
975 | uint16_t shiftReg; | |
976 | int bitCnt; | |
977 | int byteCnt; | |
978 | int byteCntMax; | |
979 | int posCnt; | |
980 | uint8_t *output; | |
981 | } Uart; | |
62577a62 | 982 | */ |
3e134b4c | 983 | /* Receive & handle a bit coming from the reader. |
984 | * | |
985 | * This function is called 4 times per bit (every 2 subcarrier cycles). | |
986 | * Subcarrier frequency fs is 212kHz, 1/fs = 4,72us, i.e. function is called every 9,44us | |
987 | * | |
988 | * LED handling: | |
989 | * LED A -> ON once we have received the SOF and are expecting the rest. | |
990 | * LED A -> OFF once we have received EOF or are in error state or unsynced | |
991 | * | |
992 | * Returns: true if we received a EOF | |
993 | * false if we are still waiting for some more | |
994 | */ | |
995 | // static RAMFUNC int HandleLegicUartBit(uint8_t bit) | |
996 | // { | |
997 | // switch(Uart.state) { | |
998 | // case STATE_UNSYNCD: | |
999 | // if(!bit) { | |
1000 | // // we went low, so this could be the beginning of an SOF | |
1001 | // Uart.state = STATE_GOT_FALLING_EDGE_OF_SOF; | |
1002 | // Uart.posCnt = 0; | |
1003 | // Uart.bitCnt = 0; | |
1004 | // } | |
1005 | // break; | |
1006 | ||
1007 | // case STATE_GOT_FALLING_EDGE_OF_SOF: | |
1008 | // Uart.posCnt++; | |
1009 | // if(Uart.posCnt == 2) { // sample every 4 1/fs in the middle of a bit | |
1010 | // if(bit) { | |
1011 | // if(Uart.bitCnt > 9) { | |
1012 | // // we've seen enough consecutive | |
1013 | // // zeros that it's a valid SOF | |
1014 | // Uart.posCnt = 0; | |
1015 | // Uart.byteCnt = 0; | |
1016 | // Uart.state = STATE_AWAITING_START_BIT; | |
1017 | // LED_A_ON(); // Indicate we got a valid SOF | |
1018 | // } else { | |
1019 | // // didn't stay down long enough | |
1020 | // // before going high, error | |
1021 | // Uart.state = STATE_UNSYNCD; | |
1022 | // } | |
1023 | // } else { | |
1024 | // // do nothing, keep waiting | |
1025 | // } | |
1026 | // Uart.bitCnt++; | |
1027 | // } | |
1028 | // if(Uart.posCnt >= 4) Uart.posCnt = 0; | |
1029 | // if(Uart.bitCnt > 12) { | |
1030 | // // Give up if we see too many zeros without | |
1031 | // // a one, too. | |
1032 | // LED_A_OFF(); | |
1033 | // Uart.state = STATE_UNSYNCD; | |
1034 | // } | |
1035 | // break; | |
1036 | ||
1037 | // case STATE_AWAITING_START_BIT: | |
1038 | // Uart.posCnt++; | |
1039 | // if(bit) { | |
1040 | // if(Uart.posCnt > 50/2) { // max 57us between characters = 49 1/fs, max 3 etus after low phase of SOF = 24 1/fs | |
1041 | // // stayed high for too long between | |
1042 | // // characters, error | |
1043 | // Uart.state = STATE_UNSYNCD; | |
1044 | // } | |
1045 | // } else { | |
1046 | // // falling edge, this starts the data byte | |
1047 | // Uart.posCnt = 0; | |
1048 | // Uart.bitCnt = 0; | |
1049 | // Uart.shiftReg = 0; | |
1050 | // Uart.state = STATE_RECEIVING_DATA; | |
1051 | // } | |
1052 | // break; | |
1053 | ||
1054 | // case STATE_RECEIVING_DATA: | |
1055 | // Uart.posCnt++; | |
1056 | // if(Uart.posCnt == 2) { | |
1057 | // // time to sample a bit | |
1058 | // Uart.shiftReg >>= 1; | |
1059 | // if(bit) { | |
1060 | // Uart.shiftReg |= 0x200; | |
1061 | // } | |
1062 | // Uart.bitCnt++; | |
1063 | // } | |
1064 | // if(Uart.posCnt >= 4) { | |
1065 | // Uart.posCnt = 0; | |
1066 | // } | |
1067 | // if(Uart.bitCnt == 10) { | |
1068 | // if((Uart.shiftReg & 0x200) && !(Uart.shiftReg & 0x001)) | |
1069 | // { | |
1070 | // // this is a data byte, with correct | |
1071 | // // start and stop bits | |
1072 | // Uart.output[Uart.byteCnt] = (Uart.shiftReg >> 1) & 0xff; | |
1073 | // Uart.byteCnt++; | |
1074 | ||
1075 | // if(Uart.byteCnt >= Uart.byteCntMax) { | |
1076 | // // Buffer overflowed, give up | |
1077 | // LED_A_OFF(); | |
1078 | // Uart.state = STATE_UNSYNCD; | |
1079 | // } else { | |
1080 | // // so get the next byte now | |
1081 | // Uart.posCnt = 0; | |
1082 | // Uart.state = STATE_AWAITING_START_BIT; | |
1083 | // } | |
1084 | // } else if (Uart.shiftReg == 0x000) { | |
1085 | // // this is an EOF byte | |
1086 | // LED_A_OFF(); // Finished receiving | |
1087 | // Uart.state = STATE_UNSYNCD; | |
1088 | // if (Uart.byteCnt != 0) { | |
1089 | // return TRUE; | |
1090 | // } | |
1091 | // } else { | |
1092 | // // this is an error | |
1093 | // LED_A_OFF(); | |
1094 | // Uart.state = STATE_UNSYNCD; | |
1095 | // } | |
1096 | // } | |
1097 | // break; | |
1098 | ||
1099 | // default: | |
1100 | // LED_A_OFF(); | |
1101 | // Uart.state = STATE_UNSYNCD; | |
1102 | // break; | |
1103 | // } | |
1104 | ||
1105 | // return FALSE; | |
1106 | // } | |
62577a62 | 1107 | /* |
3e134b4c | 1108 | |
f7b42573 | 1109 | static void UartReset() { |
1110 | Uart.byteCntMax = 3; | |
3e134b4c | 1111 | Uart.state = STATE_UNSYNCD; |
1112 | Uart.byteCnt = 0; | |
1113 | Uart.bitCnt = 0; | |
1114 | Uart.posCnt = 0; | |
f7b42573 | 1115 | memset(Uart.output, 0x00, 3); |
3e134b4c | 1116 | } |
62577a62 | 1117 | */ |
f7b42573 | 1118 | // static void UartInit(uint8_t *data) { |
3e134b4c | 1119 | // Uart.output = data; |
1120 | // UartReset(); | |
1121 | // } | |
1122 | ||
1123 | //============================================================================= | |
1124 | // An LEGIC reader. We take layer two commands, code them | |
1125 | // appropriately, and then send them to the tag. We then listen for the | |
1126 | // tag's response, which we leave in the buffer to be demodulated on the | |
1127 | // PC side. | |
1128 | //============================================================================= | |
62577a62 | 1129 | /* |
3e134b4c | 1130 | static struct { |
1131 | enum { | |
1132 | DEMOD_UNSYNCD, | |
1133 | DEMOD_PHASE_REF_TRAINING, | |
1134 | DEMOD_AWAITING_FALLING_EDGE_OF_SOF, | |
1135 | DEMOD_GOT_FALLING_EDGE_OF_SOF, | |
1136 | DEMOD_AWAITING_START_BIT, | |
1137 | DEMOD_RECEIVING_DATA | |
1138 | } state; | |
1139 | int bitCount; | |
1140 | int posCount; | |
1141 | int thisBit; | |
1142 | uint16_t shiftReg; | |
1143 | uint8_t *output; | |
1144 | int len; | |
1145 | int sumI; | |
1146 | int sumQ; | |
1147 | } Demod; | |
62577a62 | 1148 | */ |
3e134b4c | 1149 | /* |
1150 | * Handles reception of a bit from the tag | |
1151 | * | |
1152 | * This function is called 2 times per bit (every 4 subcarrier cycles). | |
1153 | * Subcarrier frequency fs is 212kHz, 1/fs = 4,72us, i.e. function is called every 9,44us | |
1154 | * | |
1155 | * LED handling: | |
1156 | * LED C -> ON once we have received the SOF and are expecting the rest. | |
1157 | * LED C -> OFF once we have received EOF or are unsynced | |
1158 | * | |
1159 | * Returns: true if we received a EOF | |
1160 | * false if we are still waiting for some more | |
1161 | * | |
1162 | */ | |
3e134b4c | 1163 | |
62577a62 | 1164 | /* |
3e134b4c | 1165 | static RAMFUNC int HandleLegicSamplesDemod(int ci, int cq) |
1166 | { | |
1167 | int v = 0; | |
1168 | int ai = ABS(ci); | |
1169 | int aq = ABS(cq); | |
1170 | int halfci = (ai >> 1); | |
1171 | int halfcq = (aq >> 1); | |
1172 | ||
1173 | switch(Demod.state) { | |
1174 | case DEMOD_UNSYNCD: | |
1175 | ||
1176 | CHECK_FOR_SUBCARRIER() | |
1177 | ||
1178 | if(v > SUBCARRIER_DETECT_THRESHOLD) { // subcarrier detected | |
1179 | Demod.state = DEMOD_PHASE_REF_TRAINING; | |
1180 | Demod.sumI = ci; | |
1181 | Demod.sumQ = cq; | |
1182 | Demod.posCount = 1; | |
1183 | } | |
1184 | break; | |
1185 | ||
1186 | case DEMOD_PHASE_REF_TRAINING: | |
1187 | if(Demod.posCount < 8) { | |
1188 | ||
1189 | CHECK_FOR_SUBCARRIER() | |
1190 | ||
1191 | if (v > SUBCARRIER_DETECT_THRESHOLD) { | |
1192 | // set the reference phase (will code a logic '1') by averaging over 32 1/fs. | |
1193 | // note: synchronization time > 80 1/fs | |
1194 | Demod.sumI += ci; | |
1195 | Demod.sumQ += cq; | |
1196 | ++Demod.posCount; | |
1197 | } else { | |
1198 | // subcarrier lost | |
1199 | Demod.state = DEMOD_UNSYNCD; | |
1200 | } | |
1201 | } else { | |
1202 | Demod.state = DEMOD_AWAITING_FALLING_EDGE_OF_SOF; | |
1203 | } | |
1204 | break; | |
1205 | ||
1206 | case DEMOD_AWAITING_FALLING_EDGE_OF_SOF: | |
1207 | ||
1208 | MAKE_SOFT_DECISION() | |
1209 | ||
1210 | //Dbprintf("ICE: %d %d %d %d %d", v, Demod.sumI, Demod.sumQ, ci, cq ); | |
1211 | // logic '0' detected | |
1212 | if (v <= 0) { | |
1213 | ||
1214 | Demod.state = DEMOD_GOT_FALLING_EDGE_OF_SOF; | |
1215 | ||
1216 | // start of SOF sequence | |
1217 | Demod.posCount = 0; | |
1218 | } else { | |
1219 | // maximum length of TR1 = 200 1/fs | |
1220 | if(Demod.posCount > 25*2) Demod.state = DEMOD_UNSYNCD; | |
1221 | } | |
1222 | ++Demod.posCount; | |
1223 | break; | |
1224 | ||
1225 | case DEMOD_GOT_FALLING_EDGE_OF_SOF: | |
1226 | ++Demod.posCount; | |
1227 | ||
1228 | MAKE_SOFT_DECISION() | |
1229 | ||
1230 | if(v > 0) { | |
1231 | // low phase of SOF too short (< 9 etu). Note: spec is >= 10, but FPGA tends to "smear" edges | |
1232 | if(Demod.posCount < 10*2) { | |
1233 | Demod.state = DEMOD_UNSYNCD; | |
1234 | } else { | |
1235 | LED_C_ON(); // Got SOF | |
1236 | Demod.state = DEMOD_AWAITING_START_BIT; | |
1237 | Demod.posCount = 0; | |
1238 | Demod.len = 0; | |
1239 | } | |
1240 | } else { | |
1241 | // low phase of SOF too long (> 12 etu) | |
1242 | if(Demod.posCount > 13*2) { | |
1243 | Demod.state = DEMOD_UNSYNCD; | |
1244 | LED_C_OFF(); | |
1245 | } | |
1246 | } | |
1247 | break; | |
1248 | ||
1249 | case DEMOD_AWAITING_START_BIT: | |
1250 | ++Demod.posCount; | |
1251 | ||
1252 | MAKE_SOFT_DECISION() | |
1253 | ||
1254 | if(v > 0) { | |
1255 | // max 19us between characters = 16 1/fs, max 3 etu after low phase of SOF = 24 1/fs | |
1256 | if(Demod.posCount > 3*2) { | |
1257 | Demod.state = DEMOD_UNSYNCD; | |
1258 | LED_C_OFF(); | |
1259 | } | |
1260 | } else { | |
1261 | // start bit detected | |
1262 | Demod.bitCount = 0; | |
1263 | Demod.posCount = 1; // this was the first half | |
1264 | Demod.thisBit = v; | |
1265 | Demod.shiftReg = 0; | |
1266 | Demod.state = DEMOD_RECEIVING_DATA; | |
1267 | } | |
1268 | break; | |
1269 | ||
1270 | case DEMOD_RECEIVING_DATA: | |
1271 | ||
1272 | MAKE_SOFT_DECISION() | |
1273 | ||
1274 | if(Demod.posCount == 0) { | |
1275 | // first half of bit | |
1276 | Demod.thisBit = v; | |
1277 | Demod.posCount = 1; | |
1278 | } else { | |
1279 | // second half of bit | |
1280 | Demod.thisBit += v; | |
1281 | Demod.shiftReg >>= 1; | |
1282 | // logic '1' | |
1283 | if(Demod.thisBit > 0) | |
1284 | Demod.shiftReg |= 0x200; | |
1285 | ||
1286 | ++Demod.bitCount; | |
1287 | ||
1288 | if(Demod.bitCount == 10) { | |
1289 | ||
1290 | uint16_t s = Demod.shiftReg; | |
1291 | ||
1292 | if((s & 0x200) && !(s & 0x001)) { | |
1293 | // stop bit == '1', start bit == '0' | |
1294 | uint8_t b = (s >> 1); | |
1295 | Demod.output[Demod.len] = b; | |
1296 | ++Demod.len; | |
1297 | Demod.state = DEMOD_AWAITING_START_BIT; | |
1298 | } else { | |
1299 | Demod.state = DEMOD_UNSYNCD; | |
1300 | LED_C_OFF(); | |
1301 | ||
1302 | if(s == 0x000) { | |
1303 | // This is EOF (start, stop and all data bits == '0' | |
1304 | return TRUE; | |
1305 | } | |
1306 | } | |
1307 | } | |
1308 | Demod.posCount = 0; | |
1309 | } | |
1310 | break; | |
1311 | ||
1312 | default: | |
1313 | Demod.state = DEMOD_UNSYNCD; | |
1314 | LED_C_OFF(); | |
1315 | break; | |
1316 | } | |
1317 | return FALSE; | |
1318 | } | |
62577a62 | 1319 | */ |
1320 | /* | |
3e134b4c | 1321 | // Clear out the state of the "UART" that receives from the tag. |
1322 | static void DemodReset() { | |
1323 | Demod.len = 0; | |
1324 | Demod.state = DEMOD_UNSYNCD; | |
1325 | Demod.posCount = 0; | |
1326 | Demod.sumI = 0; | |
1327 | Demod.sumQ = 0; | |
1328 | Demod.bitCount = 0; | |
1329 | Demod.thisBit = 0; | |
1330 | Demod.shiftReg = 0; | |
f7b42573 | 1331 | memset(Demod.output, 0x00, 3); |
3e134b4c | 1332 | } |
1333 | ||
1334 | static void DemodInit(uint8_t *data) { | |
1335 | Demod.output = data; | |
1336 | DemodReset(); | |
1337 | } | |
62577a62 | 1338 | */ |
3e134b4c | 1339 | |
1340 | /* | |
1341 | * Demodulate the samples we received from the tag, also log to tracebuffer | |
1342 | * quiet: set to 'TRUE' to disable debug output | |
1343 | */ | |
62577a62 | 1344 | |
1345 | /* | |
3e134b4c | 1346 | #define LEGIC_DMA_BUFFER_SIZE 256 |
62577a62 | 1347 | |
1348 | static void GetSamplesForLegicDemod(int n, bool quiet) | |
3e134b4c | 1349 | { |
1350 | int max = 0; | |
1351 | bool gotFrame = FALSE; | |
1352 | int lastRxCounter = LEGIC_DMA_BUFFER_SIZE; | |
1353 | int ci, cq, samples = 0; | |
1354 | ||
1355 | BigBuf_free(); | |
1356 | ||
1357 | // And put the FPGA in the appropriate mode | |
1358 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR | FPGA_HF_READER_RX_XCORR_QUARTER_FREQ); | |
1359 | ||
1360 | // The response (tag -> reader) that we're receiving. | |
1361 | // Set up the demodulator for tag -> reader responses. | |
1362 | DemodInit(BigBuf_malloc(MAX_FRAME_SIZE)); | |
1363 | ||
1364 | // The DMA buffer, used to stream samples from the FPGA | |
1365 | int8_t *dmaBuf = (int8_t*) BigBuf_malloc(LEGIC_DMA_BUFFER_SIZE); | |
1366 | int8_t *upTo = dmaBuf; | |
1367 | ||
1368 | // Setup and start DMA. | |
1369 | if ( !FpgaSetupSscDma((uint8_t*) dmaBuf, LEGIC_DMA_BUFFER_SIZE) ){ | |
1370 | if (MF_DBGLEVEL > 1) Dbprintf("FpgaSetupSscDma failed. Exiting"); | |
1371 | return; | |
1372 | } | |
1373 | ||
1374 | // Signal field is ON with the appropriate LED: | |
1375 | LED_D_ON(); | |
1376 | for(;;) { | |
1377 | int behindBy = lastRxCounter - AT91C_BASE_PDC_SSC->PDC_RCR; | |
1378 | if(behindBy > max) max = behindBy; | |
1379 | ||
1380 | while(((lastRxCounter-AT91C_BASE_PDC_SSC->PDC_RCR) & (LEGIC_DMA_BUFFER_SIZE-1)) > 2) { | |
1381 | ci = upTo[0]; | |
1382 | cq = upTo[1]; | |
1383 | upTo += 2; | |
1384 | if(upTo >= dmaBuf + LEGIC_DMA_BUFFER_SIZE) { | |
1385 | upTo = dmaBuf; | |
1386 | AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) upTo; | |
1387 | AT91C_BASE_PDC_SSC->PDC_RNCR = LEGIC_DMA_BUFFER_SIZE; | |
1388 | } | |
1389 | lastRxCounter -= 2; | |
1390 | if(lastRxCounter <= 0) | |
1391 | lastRxCounter = LEGIC_DMA_BUFFER_SIZE; | |
1392 | ||
1393 | samples += 2; | |
1394 | ||
1395 | gotFrame = HandleLegicSamplesDemod(ci , cq ); | |
1396 | if ( gotFrame ) | |
1397 | break; | |
1398 | } | |
1399 | ||
1400 | if(samples > n || gotFrame) | |
1401 | break; | |
1402 | } | |
1403 | ||
1404 | FpgaDisableSscDma(); | |
1405 | ||
1406 | if (!quiet && Demod.len == 0) { | |
1407 | Dbprintf("max behindby = %d, samples = %d, gotFrame = %d, Demod.len = %d, Demod.sumI = %d, Demod.sumQ = %d", | |
1408 | max, | |
1409 | samples, | |
1410 | gotFrame, | |
1411 | Demod.len, | |
1412 | Demod.sumI, | |
1413 | Demod.sumQ | |
1414 | ); | |
1415 | } | |
1416 | ||
1417 | //Tracing | |
1418 | if (Demod.len > 0) { | |
1419 | uint8_t parity[MAX_PARITY_SIZE] = {0x00}; | |
1420 | LogTrace(Demod.output, Demod.len, 0, 0, parity, FALSE); | |
1421 | } | |
1422 | } | |
62577a62 | 1423 | |
1424 | */ | |
1425 | ||
3e134b4c | 1426 | //----------------------------------------------------------------------------- |
1427 | // Transmit the command (to the tag) that was placed in ToSend[]. | |
1428 | //----------------------------------------------------------------------------- | |
62577a62 | 1429 | /* |
3e134b4c | 1430 | static void TransmitForLegic(void) |
1431 | { | |
1432 | int c; | |
1433 | ||
1434 | FpgaSetupSsc(); | |
1435 | ||
1436 | while(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) | |
1437 | AT91C_BASE_SSC->SSC_THR = 0xff; | |
1438 | ||
1439 | // Signal field is ON with the appropriate Red LED | |
1440 | LED_D_ON(); | |
1441 | ||
1442 | // Signal we are transmitting with the Green LED | |
1443 | LED_B_ON(); | |
1444 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX | FPGA_HF_READER_TX_SHALLOW_MOD); | |
1445 | ||
1446 | for(c = 0; c < 10;) { | |
1447 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) { | |
1448 | AT91C_BASE_SSC->SSC_THR = 0xff; | |
1449 | c++; | |
1450 | } | |
1451 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { | |
1452 | volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR; | |
1453 | (void)r; | |
1454 | } | |
1455 | WDT_HIT(); | |
1456 | } | |
1457 | ||
1458 | c = 0; | |
1459 | for(;;) { | |
1460 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) { | |
1461 | AT91C_BASE_SSC->SSC_THR = ToSend[c]; | |
1462 | legic_prng_forward(1); // forward the lfsr | |
1463 | c++; | |
1464 | if(c >= ToSendMax) { | |
1465 | break; | |
1466 | } | |
1467 | } | |
1468 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { | |
1469 | volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR; | |
1470 | (void)r; | |
1471 | } | |
1472 | WDT_HIT(); | |
1473 | } | |
1474 | LED_B_OFF(); | |
1475 | } | |
62577a62 | 1476 | */ |
3e134b4c | 1477 | |
1478 | //----------------------------------------------------------------------------- | |
1479 | // Code a layer 2 command (string of octets, including CRC) into ToSend[], | |
1480 | // so that it is ready to transmit to the tag using TransmitForLegic(). | |
1481 | //----------------------------------------------------------------------------- | |
62577a62 | 1482 | /* |
bf2cd644 | 1483 | static void CodeLegicBitsAsReader(const uint8_t *cmd, uint8_t cmdlen, int bits) |
3e134b4c | 1484 | { |
1485 | int i, j; | |
1486 | uint8_t b; | |
1487 | ||
1488 | ToSendReset(); | |
1489 | ||
1490 | // Send SOF | |
bf2cd644 | 1491 | for(i = 0; i < 7; i++) |
3e134b4c | 1492 | ToSendStuffBit(1); |
3e134b4c | 1493 | |
bf2cd644 | 1494 | |
1495 | for(i = 0; i < cmdlen; i++) { | |
3e134b4c | 1496 | // Start bit |
1497 | ToSendStuffBit(0); | |
1498 | ||
1499 | // Data bits | |
1500 | b = cmd[i]; | |
bf2cd644 | 1501 | for(j = 0; j < bits; j++) { |
3e134b4c | 1502 | if(b & 1) { |
1503 | ToSendStuffBit(1); | |
1504 | } else { | |
1505 | ToSendStuffBit(0); | |
1506 | } | |
1507 | b >>= 1; | |
1508 | } | |
1509 | } | |
1510 | ||
1511 | // Convert from last character reference to length | |
1512 | ++ToSendMax; | |
1513 | } | |
62577a62 | 1514 | */ |
3e134b4c | 1515 | /** |
1516 | Convenience function to encode, transmit and trace Legic comms | |
1517 | **/ | |
62577a62 | 1518 | /* |
1519 | static void CodeAndTransmitLegicAsReader(const uint8_t *cmd, uint8_t cmdlen, int bits) | |
3e134b4c | 1520 | { |
bf2cd644 | 1521 | CodeLegicBitsAsReader(cmd, cmdlen, bits); |
3e134b4c | 1522 | TransmitForLegic(); |
1523 | if (tracing) { | |
1524 | uint8_t parity[1] = {0x00}; | |
3e82f956 | 1525 | LogTrace(cmd, cmdlen, 0, 0, parity, TRUE); |
3e134b4c | 1526 | } |
1527 | } | |
1528 | ||
62577a62 | 1529 | */ |
3e134b4c | 1530 | // Set up LEGIC communication |
62577a62 | 1531 | /* |
3e134b4c | 1532 | void ice_legic_setup() { |
1533 | ||
1534 | // standard things. | |
1535 | FpgaDownloadAndGo(FPGA_BITSTREAM_HF); | |
1536 | BigBuf_free(); BigBuf_Clear_ext(false); | |
1537 | clear_trace(); | |
1538 | set_tracing(TRUE); | |
1539 | DemodReset(); | |
1540 | UartReset(); | |
1541 | ||
1542 | // Set up the synchronous serial port | |
1543 | FpgaSetupSsc(); | |
1544 | ||
1545 | // connect Demodulated Signal to ADC: | |
1546 | SetAdcMuxFor(GPIO_MUXSEL_HIPKD); | |
1547 | ||
1548 | // Signal field is on with the appropriate LED | |
1549 | LED_D_ON(); | |
1550 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX | FPGA_HF_READER_TX_SHALLOW_MOD); | |
f7b42573 | 1551 | SpinDelay(20); |
3e134b4c | 1552 | // Start the timer |
1553 | //StartCountSspClk(); | |
1554 | ||
1555 | // initalize CRC | |
1556 | crc_init(&legic_crc, 4, 0x19 >> 1, 0x5, 0); | |
1557 | ||
1558 | // initalize prng | |
1559 | legic_prng_init(0); | |
62577a62 | 1560 | } |
1561 | */ |