]>
Commit | Line | Data |
---|---|---|
489ef36c | 1 | //----------------------------------------------------------------------------- |
2 | // Jonathan Westhues, split Nov 2006 | |
3 | // | |
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 | |
6 | // the license. | |
7 | //----------------------------------------------------------------------------- | |
abb21530 | 8 | // Routines to support ISO 14443B. This includes both the reader software and |
9 | // the `fake tag' modes. | |
489ef36c | 10 | //----------------------------------------------------------------------------- |
6fc68747 | 11 | #include "iso14443b.h" |
489ef36c | 12 | |
b8622518 | 13 | #ifndef FWT_TIMEOUT_14B |
29f8c2cc | 14 | // defaults to 2000ms |
15 | # define FWT_TIMEOUT_14B 35312 | |
b8622518 | 16 | #endif |
17 | #ifndef ISO14443B_DMA_BUFFER_SIZE | |
18 | # define ISO14443B_DMA_BUFFER_SIZE 256 | |
19 | #endif | |
20 | #ifndef RECEIVE_MASK | |
21 | # define RECEIVE_MASK (ISO14443B_DMA_BUFFER_SIZE-1) | |
22 | #endif | |
489ef36c | 23 | |
11c2df83 | 24 | // Guard Time (per 14443-2) |
b8622518 | 25 | #ifndef TR0 |
26 | # define TR0 0 | |
27 | #endif | |
28 | ||
11c2df83 | 29 | // Synchronization time (per 14443-2) |
b8622518 | 30 | #ifndef TR1 |
31 | # define TR1 0 | |
32 | #endif | |
11c2df83 | 33 | // Frame Delay Time PICC to PCD (per 14443-3 Amendment 1) |
b8622518 | 34 | #ifndef TR2 |
35 | # define TR2 0 | |
36 | #endif | |
d51717ff | 37 | |
38 | // 4sample | |
c3e8413c | 39 | #define SEND4STUFFBIT(x) ToSendStuffBit(x);ToSendStuffBit(x);ToSendStuffBit(x);ToSendStuffBit(x); |
40 | //#define SEND4STUFFBIT(x) ToSendStuffBit(x); | |
29f8c2cc | 41 | // iceman, this threshold value, what makes 8 a good amplituted for this IQ values? |
42 | #ifndef SUBCARRIER_DETECT_THRESHOLD | |
86db8973 | 43 | # define SUBCARRIER_DETECT_THRESHOLD 8 |
29f8c2cc | 44 | #endif |
d51717ff | 45 | |
29f8c2cc | 46 | static void iso14b_set_timeout(uint32_t timeout); |
47 | static void iso14b_set_maxframesize(uint16_t size); | |
11c2df83 | 48 | static void switch_off(void); |
49 | ||
6fc68747 | 50 | // the block number for the ISO14443-4 PCB (used with APDUs) |
a62bf3af | 51 | static uint8_t pcb_blocknum = 0; |
b8622518 | 52 | static uint32_t iso14b_timeout = FWT_TIMEOUT_14B; |
11c2df83 | 53 | |
11c2df83 | 54 | |
489ef36c | 55 | //============================================================================= |
56 | // An ISO 14443 Type B tag. We listen for commands from the reader, using | |
57 | // a UART kind of thing that's implemented in software. When we get a | |
58 | // frame (i.e., a group of bytes between SOF and EOF), we check the CRC. | |
59 | // If it's good, then we can do something appropriate with it, and send | |
60 | // a response. | |
61 | //============================================================================= | |
62 | ||
cef590d9 | 63 | |
64 | //----------------------------------------------------------------------------- | |
11c2df83 | 65 | // The software UART that receives commands from the reader, and its state variables. |
cef590d9 | 66 | //----------------------------------------------------------------------------- |
67 | static struct { | |
68 | enum { | |
69 | STATE_UNSYNCD, | |
70 | STATE_GOT_FALLING_EDGE_OF_SOF, | |
71 | STATE_AWAITING_START_BIT, | |
72 | STATE_RECEIVING_DATA | |
73 | } state; | |
11c2df83 | 74 | uint16_t shiftReg; |
75 | int bitCnt; | |
76 | int byteCnt; | |
77 | int byteCntMax; | |
78 | int posCnt; | |
79 | uint8_t *output; | |
cef590d9 | 80 | } Uart; |
81 | ||
11c2df83 | 82 | static void UartReset() { |
cef590d9 | 83 | Uart.state = STATE_UNSYNCD; |
11c2df83 | 84 | Uart.shiftReg = 0; |
cef590d9 | 85 | Uart.bitCnt = 0; |
11c2df83 | 86 | Uart.byteCnt = 0; |
87 | Uart.byteCntMax = MAX_FRAME_SIZE; | |
cef590d9 | 88 | Uart.posCnt = 0; |
cef590d9 | 89 | } |
90 | ||
11c2df83 | 91 | static void UartInit(uint8_t *data) { |
cef590d9 | 92 | Uart.output = data; |
93 | UartReset(); | |
11c2df83 | 94 | // memset(Uart.output, 0x00, MAX_FRAME_SIZE); |
cef590d9 | 95 | } |
96 | ||
11c2df83 | 97 | //----------------------------------------------------------------------------- |
98 | // The software Demod that receives commands from the tag, and its state variables. | |
99 | //----------------------------------------------------------------------------- | |
cef590d9 | 100 | static struct { |
101 | enum { | |
102 | DEMOD_UNSYNCD, | |
103 | DEMOD_PHASE_REF_TRAINING, | |
104 | DEMOD_AWAITING_FALLING_EDGE_OF_SOF, | |
105 | DEMOD_GOT_FALLING_EDGE_OF_SOF, | |
106 | DEMOD_AWAITING_START_BIT, | |
107 | DEMOD_RECEIVING_DATA | |
108 | } state; | |
11c2df83 | 109 | uint16_t bitCount; |
110 | int posCount; | |
111 | int thisBit; | |
cef590d9 | 112 | /* this had been used to add RSSI (Received Signal Strength Indication) to traces. Currently not implemented. |
113 | int metric; | |
114 | int metricN; | |
115 | */ | |
11c2df83 | 116 | uint16_t shiftReg; |
117 | uint8_t *output; | |
118 | uint16_t len; | |
119 | int sumI; | |
120 | int sumQ; | |
121 | uint32_t startTime, endTime; | |
cef590d9 | 122 | } Demod; |
123 | ||
11c2df83 | 124 | // Clear out the state of the "UART" that receives from the tag. |
125 | static void DemodReset() { | |
cef590d9 | 126 | Demod.state = DEMOD_UNSYNCD; |
cef590d9 | 127 | Demod.bitCount = 0; |
11c2df83 | 128 | Demod.posCount = 0; |
cef590d9 | 129 | Demod.thisBit = 0; |
130 | Demod.shiftReg = 0; | |
11c2df83 | 131 | Demod.len = 0; |
132 | Demod.sumI = 0; | |
133 | Demod.sumQ = 0; | |
134 | Demod.startTime = 0; | |
135 | Demod.endTime = 0; | |
cef590d9 | 136 | } |
137 | ||
11c2df83 | 138 | static void DemodInit(uint8_t *data) { |
cef590d9 | 139 | Demod.output = data; |
140 | DemodReset(); | |
11c2df83 | 141 | // memset(Demod.output, 0x00, MAX_FRAME_SIZE); |
cef590d9 | 142 | } |
143 | ||
29f8c2cc | 144 | |
145 | /* | |
146 | * 9.4395 us = 1 ETU and clock is about 1.5 us | |
147 | * 13560000Hz | |
148 | * 1000ms/s | |
149 | * timeout in ETUs (time to transfer 1 bit, 9.4395 us) | |
150 | * | |
151 | * Formula to calculate FWT (in ETUs) by timeout (in ms): | |
152 | * fwt = 13560000 * 1000 / (8*16) * timeout; | |
153 | * Sample: 3sec == 3000ms | |
154 | * 13560000 * 1000 / (8*16) * 3000 == | |
155 | * 13560000000 / 384000 = 35312 FWT | |
156 | * @param timeout is in frame wait time, fwt, measured in ETUs | |
157 | */ | |
158 | static void iso14b_set_timeout(uint32_t timeout) { | |
159 | #define MAX_TIMEOUT 40542464 // 13560000Hz * 1000ms / (2^32-1) * (8*16) | |
160 | if(timeout > MAX_TIMEOUT) | |
161 | timeout = MAX_TIMEOUT; | |
162 | ||
163 | iso14b_timeout = timeout; | |
164 | if(MF_DBGLEVEL >= 3) Dbprintf("ISO14443B Timeout set to %ld fwt", iso14b_timeout); | |
165 | } | |
166 | static void iso14b_set_maxframesize(uint16_t size) { | |
167 | if (size > 256) | |
168 | size = MAX_FRAME_SIZE; | |
169 | ||
170 | Uart.byteCntMax = size; | |
171 | if(MF_DBGLEVEL >= 3) Dbprintf("ISO14443B Max frame size set to %d bytes", Uart.byteCntMax); | |
172 | } | |
173 | static void switch_off(void){ | |
174 | if (MF_DBGLEVEL > 3) Dbprintf("switch_off"); | |
175 | FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); | |
176 | SpinDelay(100); | |
177 | FpgaDisableSscDma(); | |
178 | set_tracing(FALSE); | |
179 | LEDsoff(); | |
180 | } | |
181 | ||
11c2df83 | 182 | void AppendCrc14443b(uint8_t* data, int len) { |
dccddaef | 183 | ComputeCrc14443(CRC_14443_B, data, len, data+len, data+len+1); |
6fc68747 | 184 | } |
185 | ||
489ef36c | 186 | //----------------------------------------------------------------------------- |
187 | // Code up a string of octets at layer 2 (including CRC, we don't generate | |
188 | // that here) so that they can be transmitted to the reader. Doesn't transmit | |
189 | // them yet, just leaves them ready to send in ToSend[]. | |
190 | //----------------------------------------------------------------------------- | |
11c2df83 | 191 | static void CodeIso14443bAsTag(const uint8_t *cmd, int len) { |
192 | /* ISO 14443 B | |
193 | * | |
194 | * Reader to card | ASK - Amplitude Shift Keying Modulation (PCD to PICC for Type B) (NRZ-L encodig) | |
195 | * Card to reader | BPSK - Binary Phase Shift Keying Modulation, (PICC to PCD for Type B) | |
196 | * | |
197 | * fc - carrier frequency 13.56mHz | |
198 | * TR0 - Guard Time per 14443-2 | |
199 | * TR1 - Synchronization Time per 14443-2 | |
200 | * TR2 - PICC to PCD Frame Delay Time (per 14443-3 Amendment 1) | |
201 | * | |
202 | * Elementary Time Unit (ETU) is | |
203 | * - 128 Carrier Cycles (9.4395 µS) = 8 Subcarrier Units | |
204 | * - 1 ETU = 1 bit | |
205 | * - 10 ETU = 1 startbit, 8 databits, 1 stopbit (10bits length) | |
206 | * - startbit is a 0 | |
207 | * - stopbit is a 1 | |
208 | * | |
209 | * Start of frame (SOF) is | |
210 | * - [10-11] ETU of ZEROS, unmodulated time | |
211 | * - [2-3] ETU of ONES, | |
212 | * | |
213 | * End of frame (EOF) is | |
214 | * - [10-11] ETU of ZEROS, unmodulated time | |
215 | * | |
216 | * -TO VERIFY THIS BELOW- | |
217 | * The mode FPGA_MAJOR_MODE_HF_SIMULATOR | FPGA_HF_SIMULATOR_MODULATE_BPSK which we use to simulate tag | |
218 | * works like this: | |
3b5aab18 | 219 | * - A 1-bit input to the FPGA becomes 8 pulses at 847.5kHz (1.18µS / pulse) == 9.44us |
220 | * - A 0-bit input to the FPGA becomes an unmodulated time of 1.18µS or does it become 8 nonpulses for 9.44us | |
11c2df83 | 221 | * |
3b5aab18 | 222 | * FPGA doesn't seem to work with ETU. It seems to work with pulse / duration instead. |
11c2df83 | 223 | * |
224 | * Card sends data ub 847.e kHz subcarrier | |
3b5aab18 | 225 | * subcar |duration| FC division |
226 | * -------+--------+------------ | |
227 | * 106kHz | 9.44µS | FC/128 | |
228 | * 212kHz | 4.72µS | FC/64 | |
229 | * 424kHz | 2.36µS | FC/32 | |
230 | * 848kHz | 1.18µS | FC/16 | |
231 | * -------+--------+------------ | |
11c2df83 | 232 | * |
233 | * Reader data transmission: | |
234 | * - no modulation ONES | |
235 | * - SOF | |
236 | * - Command, data and CRC_B | |
237 | * - EOF | |
238 | * - no modulation ONES | |
239 | * | |
240 | * Card data transmission | |
241 | * - TR1 | |
242 | * - SOF | |
86db8973 | 243 | * - data (each bytes is: 1startbit, 8bits, 1stopbit) |
11c2df83 | 244 | * - CRC_B |
245 | * - EOF | |
246 | * | |
247 | * FPGA implementation : | |
248 | * At this point only Type A is implemented. This means that we are using a | |
249 | * bit rate of 106 kbit/s, or fc/128. Oversample by 4, which ought to make | |
250 | * things practical for the ARM (fc/32, 423.8 kbits/s, ~50 kbytes/s) | |
251 | * | |
252 | */ | |
253 | ||
11c2df83 | 254 | int i,j; |
255 | uint8_t b; | |
256 | ||
489ef36c | 257 | ToSendReset(); |
258 | ||
259 | // Transmit a burst of ones, as the initial thing that lets the | |
11c2df83 | 260 | // reader get phase sync. |
261 | // This loop is TR1, per specification | |
262 | // TR1 minimum must be > 80/fs | |
263 | // TR1 maximum 200/fs | |
264 | // 80/fs < TR1 < 200/fs | |
265 | // 10 ETU < TR1 < 24 ETU | |
489ef36c | 266 | |
267 | // Send SOF. | |
11c2df83 | 268 | // 10-11 ETU * 4times samples ZEROS |
d51717ff | 269 | for(i = 0; i < 10; i++) { SEND4STUFFBIT(0); } |
c3e8413c | 270 | //for(i = 0; i < 10; i++) { ToSendStuffBit(0); } |
11c2df83 | 271 | |
272 | // 2-3 ETU * 4times samples ONES | |
d51717ff | 273 | for(i = 0; i < 3; i++) { SEND4STUFFBIT(1); } |
c3e8413c | 274 | //for(i = 0; i < 3; i++) { ToSendStuffBit(1); } |
11c2df83 | 275 | |
276 | // data | |
277 | for(i = 0; i < len; ++i) { | |
278 | ||
489ef36c | 279 | // Start bit |
d51717ff | 280 | SEND4STUFFBIT(0); |
c3e8413c | 281 | //ToSendStuffBit(0); |
489ef36c | 282 | |
283 | // Data bits | |
11c2df83 | 284 | b = cmd[i]; |
285 | for(j = 0; j < 8; ++j) { | |
86db8973 | 286 | // if(b & 1) { |
287 | // SEND4STUFFBIT(1); | |
288 | // //ToSendStuffBit(1); | |
289 | // } else { | |
290 | // SEND4STUFFBIT(0); | |
291 | // //ToSendStuffBit(0); | |
292 | // } | |
293 | SEND4STUFFBIT( b & 1 ); | |
489ef36c | 294 | b >>= 1; |
295 | } | |
296 | ||
297 | // Stop bit | |
d51717ff | 298 | SEND4STUFFBIT(1); |
c3e8413c | 299 | //ToSendStuffBit(1); |
11c2df83 | 300 | |
301 | // Extra Guard bit | |
302 | // For PICC it ranges 0-18us (1etu = 9us) | |
d51717ff | 303 | SEND4STUFFBIT(1); |
c3e8413c | 304 | //ToSendStuffBit(1); |
489ef36c | 305 | } |
306 | ||
abb21530 | 307 | // Send EOF. |
11c2df83 | 308 | // 10-11 ETU * 4 sample rate = ZEROS |
d51717ff | 309 | for(i = 0; i < 10; i++) { SEND4STUFFBIT(0); } |
c3e8413c | 310 | //for(i = 0; i < 10; i++) { ToSendStuffBit(0); } |
11c2df83 | 311 | |
312 | // why this? | |
d51717ff | 313 | for(i = 0; i < 40; i++) { SEND4STUFFBIT(1); } |
c3e8413c | 314 | //for(i = 0; i < 40; i++) { ToSendStuffBit(1); } |
11c2df83 | 315 | |
489ef36c | 316 | // Convert from last byte pos to length |
6fc68747 | 317 | ++ToSendMax; |
489ef36c | 318 | } |
319 | ||
cef590d9 | 320 | |
489ef36c | 321 | /* Receive & handle a bit coming from the reader. |
abb21530 | 322 | * |
323 | * This function is called 4 times per bit (every 2 subcarrier cycles). | |
324 | * Subcarrier frequency fs is 848kHz, 1/fs = 1,18us, i.e. function is called every 2,36us | |
489ef36c | 325 | * |
326 | * LED handling: | |
327 | * LED A -> ON once we have received the SOF and are expecting the rest. | |
328 | * LED A -> OFF once we have received EOF or are in error state or unsynced | |
329 | * | |
330 | * Returns: true if we received a EOF | |
331 | * false if we are still waiting for some more | |
332 | */ | |
11c2df83 | 333 | static RAMFUNC int Handle14443bReaderUartBit(uint8_t bit) { |
29f8c2cc | 334 | switch (Uart.state) { |
489ef36c | 335 | case STATE_UNSYNCD: |
29f8c2cc | 336 | if (!bit) { |
dccddaef | 337 | // we went low, so this could be the beginning of an SOF |
489ef36c | 338 | Uart.state = STATE_GOT_FALLING_EDGE_OF_SOF; |
339 | Uart.posCnt = 0; | |
340 | Uart.bitCnt = 0; | |
341 | } | |
342 | break; | |
343 | ||
344 | case STATE_GOT_FALLING_EDGE_OF_SOF: | |
345 | Uart.posCnt++; | |
29f8c2cc | 346 | if (Uart.posCnt == 2) { // sample every 4 1/fs in the middle of a bit |
347 | if (bit) { | |
348 | if (Uart.bitCnt > 9) { | |
489ef36c | 349 | // we've seen enough consecutive |
350 | // zeros that it's a valid SOF | |
351 | Uart.posCnt = 0; | |
352 | Uart.byteCnt = 0; | |
353 | Uart.state = STATE_AWAITING_START_BIT; | |
354 | LED_A_ON(); // Indicate we got a valid SOF | |
355 | } else { | |
29f8c2cc | 356 | // didn't stay down long enough before going high, error |
36f84d47 | 357 | Uart.state = STATE_UNSYNCD; |
489ef36c | 358 | } |
359 | } else { | |
360 | // do nothing, keep waiting | |
361 | } | |
362 | Uart.bitCnt++; | |
363 | } | |
29f8c2cc | 364 | if (Uart.posCnt >= 4) Uart.posCnt = 0; |
365 | if (Uart.bitCnt > 12) { | |
366 | // Give up if we see too many zeros without a one, too. | |
36f84d47 | 367 | LED_A_OFF(); |
368 | Uart.state = STATE_UNSYNCD; | |
489ef36c | 369 | } |
370 | break; | |
371 | ||
372 | case STATE_AWAITING_START_BIT: | |
373 | Uart.posCnt++; | |
29f8c2cc | 374 | if (bit) { |
375 | if (Uart.posCnt > 50/2) { // max 57us between characters = 49 1/fs, max 3 etus after low phase of SOF = 24 1/fs | |
376 | // stayed high for too long between characters, error | |
36f84d47 | 377 | Uart.state = STATE_UNSYNCD; |
489ef36c | 378 | } |
379 | } else { | |
380 | // falling edge, this starts the data byte | |
381 | Uart.posCnt = 0; | |
382 | Uart.bitCnt = 0; | |
383 | Uart.shiftReg = 0; | |
384 | Uart.state = STATE_RECEIVING_DATA; | |
489ef36c | 385 | } |
386 | break; | |
387 | ||
388 | case STATE_RECEIVING_DATA: | |
389 | Uart.posCnt++; | |
29f8c2cc | 390 | if (Uart.posCnt == 2) { |
489ef36c | 391 | // time to sample a bit |
392 | Uart.shiftReg >>= 1; | |
29f8c2cc | 393 | if (bit) { |
489ef36c | 394 | Uart.shiftReg |= 0x200; |
395 | } | |
396 | Uart.bitCnt++; | |
397 | } | |
29f8c2cc | 398 | if (Uart.posCnt >= 4) { |
489ef36c | 399 | Uart.posCnt = 0; |
400 | } | |
29f8c2cc | 401 | if (Uart.bitCnt == 10) { |
402 | if ((Uart.shiftReg & 0x200) && !(Uart.shiftReg & 0x001)) | |
489ef36c | 403 | { |
404 | // this is a data byte, with correct | |
405 | // start and stop bits | |
406 | Uart.output[Uart.byteCnt] = (Uart.shiftReg >> 1) & 0xff; | |
407 | Uart.byteCnt++; | |
408 | ||
29f8c2cc | 409 | if (Uart.byteCnt >= Uart.byteCntMax) { |
489ef36c | 410 | // Buffer overflowed, give up |
36f84d47 | 411 | LED_A_OFF(); |
412 | Uart.state = STATE_UNSYNCD; | |
489ef36c | 413 | } else { |
414 | // so get the next byte now | |
415 | Uart.posCnt = 0; | |
416 | Uart.state = STATE_AWAITING_START_BIT; | |
417 | } | |
46734099 | 418 | } else if (Uart.shiftReg == 0x000) { |
489ef36c | 419 | // this is an EOF byte |
420 | LED_A_OFF(); // Finished receiving | |
36f84d47 | 421 | Uart.state = STATE_UNSYNCD; |
29f8c2cc | 422 | if (Uart.byteCnt != 0) |
423 | return TRUE; | |
424 | ||
489ef36c | 425 | } else { |
426 | // this is an error | |
36f84d47 | 427 | LED_A_OFF(); |
46734099 | 428 | Uart.state = STATE_UNSYNCD; |
36f84d47 | 429 | } |
489ef36c | 430 | } |
431 | break; | |
432 | ||
433 | default: | |
36f84d47 | 434 | LED_A_OFF(); |
489ef36c | 435 | Uart.state = STATE_UNSYNCD; |
436 | break; | |
437 | } | |
489ef36c | 438 | return FALSE; |
439 | } | |
440 | ||
441 | //----------------------------------------------------------------------------- | |
442 | // Receive a command (from the reader to us, where we are the simulated tag), | |
443 | // and store it in the given buffer, up to the given maximum length. Keeps | |
444 | // spinning, waiting for a well-framed command, until either we get one | |
445 | // (returns TRUE) or someone presses the pushbutton on the board (FALSE). | |
446 | // | |
447 | // Assume that we're called with the SSC (to the FPGA) and ADC path set | |
448 | // correctly. | |
449 | //----------------------------------------------------------------------------- | |
11c2df83 | 450 | static int GetIso14443bCommandFromReader(uint8_t *received, uint16_t *len) { |
abb21530 | 451 | // Set FPGA mode to "simulated ISO 14443B tag", no modulation (listen |
489ef36c | 452 | // only, since we are receiving, not transmitting). |
453 | // Signal field is off with the appropriate LED | |
454 | LED_D_OFF(); | |
455 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR | FPGA_HF_SIMULATOR_NO_MODULATION); | |
ffeb77fd | 456 | |
11c2df83 | 457 | StartCountSspClk(); |
458 | ||
c7b4bcc4 | 459 | volatile uint8_t b = 0; |
ffeb77fd | 460 | |
461 | // clear receiving shift register and holding register | |
462 | // What does this loop do? Is it TR1? | |
463 | for(uint8_t c = 0; c < 10;) { | |
464 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) { | |
465 | AT91C_BASE_SSC->SSC_THR = 0xFF; | |
466 | ++c; | |
467 | } | |
468 | } | |
469 | ||
489ef36c | 470 | // Now run a `software UART' on the stream of incoming samples. |
36f84d47 | 471 | UartInit(received); |
ffeb77fd | 472 | |
ffeb77fd | 473 | uint8_t mask; |
dccddaef | 474 | while( !BUTTON_PRESS() ) { |
489ef36c | 475 | WDT_HIT(); |
476 | ||
dccddaef | 477 | if ( AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY ) { |
478 | b = (uint8_t) AT91C_BASE_SSC->SSC_RHR; | |
479 | for ( mask = 0x80; mask != 0; mask >>= 1) { | |
480 | if ( Handle14443bReaderUartBit(b & mask)) { | |
489ef36c | 481 | *len = Uart.byteCnt; |
482 | return TRUE; | |
483 | } | |
484 | } | |
485 | } | |
11c2df83 | 486 | } |
36f84d47 | 487 | return FALSE; |
489ef36c | 488 | } |
489 | ||
ffeb77fd | 490 | void ClearFpgaShiftingRegisters(void){ |
491 | ||
492 | volatile uint8_t b; | |
493 | ||
494 | // clear receiving shift register and holding register | |
c7b4bcc4 | 495 | while(!(AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY)) {}; |
c3e8413c | 496 | |
ffeb77fd | 497 | b = AT91C_BASE_SSC->SSC_RHR; (void) b; |
498 | ||
c7b4bcc4 | 499 | while(!(AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY)) {}; |
c3e8413c | 500 | |
ffeb77fd | 501 | b = AT91C_BASE_SSC->SSC_RHR; (void) b; |
3b5aab18 | 502 | |
ffeb77fd | 503 | // wait for the FPGA to signal fdt_indicator == 1 (the FPGA is ready to queue new data in its delay line) |
504 | for (uint8_t j = 0; j < 5; j++) { // allow timeout - better late than never | |
505 | while(!(AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY)); | |
506 | if (AT91C_BASE_SSC->SSC_RHR) break; | |
507 | } | |
508 | ||
509 | // Clear TXRDY: | |
c3e8413c | 510 | //AT91C_BASE_SSC->SSC_THR = 0xFF; |
ffeb77fd | 511 | } |
512 | ||
513 | void WaitForFpgaDelayQueueIsEmpty( uint16_t delay ){ | |
514 | // Ensure that the FPGA Delay Queue is empty before we switch to TAGSIM_LISTEN again: | |
515 | uint8_t fpga_queued_bits = delay >> 3; // twich /8 ?? >>3, | |
516 | for (uint8_t i = 0; i <= fpga_queued_bits/8 + 1; ) { | |
517 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) { | |
518 | AT91C_BASE_SSC->SSC_THR = 0xFF; | |
519 | i++; | |
520 | } | |
521 | } | |
522 | } | |
dccddaef | 523 | |
524 | static void TransmitFor14443b_AsTag( uint8_t *response, uint16_t len) { | |
525 | ||
b8622518 | 526 | volatile uint32_t b; |
527 | ||
528 | // Signal field is off with the appropriate LED | |
529 | LED_D_OFF(); | |
530 | //uint16_t fpgasendQueueDelay = 0; | |
531 | ||
532 | // Modulate BPSK | |
533 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR | FPGA_HF_SIMULATOR_MODULATE_BPSK); | |
534 | SpinDelay(40); | |
535 | ||
536 | ClearFpgaShiftingRegisters(); | |
537 | ||
538 | FpgaSetupSsc(); | |
dccddaef | 539 | |
b8622518 | 540 | // Transmit the response. |
541 | for(uint16_t i = 0; i < len;) { | |
542 | if(AT91C_BASE_SSC->SSC_SR & AT91C_SSC_TXRDY) { | |
543 | AT91C_BASE_SSC->SSC_THR = response[++i]; | |
dccddaef | 544 | } |
b8622518 | 545 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { |
546 | b = AT91C_BASE_SSC->SSC_RHR; | |
547 | (void)b; | |
548 | } | |
549 | } | |
550 | ||
551 | //WaitForFpgaDelayQueueIsEmpty(fpgasendQueueDelay); | |
552 | AT91C_BASE_SSC->SSC_THR = 0xFF; | |
dccddaef | 553 | } |
489ef36c | 554 | //----------------------------------------------------------------------------- |
555 | // Main loop of simulated tag: receive commands from reader, decide what | |
556 | // response to send, and send it. | |
557 | //----------------------------------------------------------------------------- | |
dccddaef | 558 | void SimulateIso14443bTag(uint32_t pupi) { |
dccddaef | 559 | |
0923c43c | 560 | ///////////// setup device. |
99cf19d9 | 561 | FpgaDownloadAndGo(FPGA_BITSTREAM_HF); |
562 | ||
11c2df83 | 563 | // allocate command receive buffer |
564 | BigBuf_free(); | |
565 | BigBuf_Clear_ext(false); | |
566 | clear_trace(); //sim | |
36f84d47 | 567 | set_tracing(TRUE); |
11c2df83 | 568 | |
dccddaef | 569 | // connect Demodulated Signal to ADC: |
570 | SetAdcMuxFor(GPIO_MUXSEL_HIPKD); | |
571 | ||
572 | // Set up the synchronous serial port | |
573 | FpgaSetupSsc(); | |
0923c43c | 574 | ///////////// |
dccddaef | 575 | |
0923c43c | 576 | uint16_t len, cmdsReceived = 0; |
577 | int cardSTATE = SIM_NOFIELD; | |
578 | int vHf = 0; // in mV | |
579 | // uint32_t time_0 = 0; | |
580 | // uint32_t t2r_time = 0; | |
581 | // uint32_t r2t_time = 0; | |
582 | uint8_t *receivedCmd = BigBuf_malloc(MAX_FRAME_SIZE); | |
dccddaef | 583 | |
0923c43c | 584 | // the only commands we understand is WUPB, AFI=0, Select All, N=1: |
585 | // static const uint8_t cmdWUPB[] = { ISO14443B_REQB, 0x00, 0x08, 0x39, 0x73 }; // WUPB | |
586 | // ... and REQB, AFI=0, Normal Request, N=1: | |
587 | // static const uint8_t cmdREQB[] = { ISO14443B_REQB, 0x00, 0x00, 0x71, 0xFF }; // REQB | |
588 | // ... and ATTRIB | |
589 | // static const uint8_t cmdATTRIB[] = { ISO14443B_ATTRIB, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; // ATTRIB | |
590 | ||
591 | // ... if not PUPI/UID is supplied we always respond with ATQB, PUPI = 820de174, Application Data = 0x20381922, | |
592 | // supports only 106kBit/s in both directions, max frame size = 32Bytes, | |
593 | // supports ISO14443-4, FWI=8 (77ms), NAD supported, CID not supported: | |
594 | uint8_t respATQB[] = { 0x50, 0x82, 0x0d, 0xe1, 0x74, 0x20, 0x38, 0x19, | |
595 | 0x22, 0x00, 0x21, 0x85, 0x5e, 0xd7 }; | |
596 | ||
597 | // response to HLTB and ATTRIB | |
598 | static const uint8_t respOK[] = {0x00, 0x78, 0xF0}; | |
599 | ||
600 | // ...PUPI/UID supplied from user. Adjust ATQB response accordingly | |
601 | if ( pupi > 0 ) { | |
c23d2618 | 602 | uint8_t len = sizeof(respATQB); |
0923c43c | 603 | num_to_bytes(pupi, 4, respATQB+1); |
c3e8413c | 604 | ComputeCrc14443(CRC_14443_B, respATQB, 12, &respATQB[len-2], &respATQB[len-1]); |
0923c43c | 605 | } |
606 | ||
607 | // prepare "ATQB" tag answer (encoded): | |
608 | CodeIso14443bAsTag(respATQB, sizeof(respATQB)); | |
609 | uint8_t *encodedATQB = BigBuf_malloc(ToSendMax); | |
610 | uint16_t encodedATQBLen = ToSendMax; | |
611 | memcpy(encodedATQB, ToSend, ToSendMax); | |
612 | ||
11c2df83 | 613 | |
0923c43c | 614 | // prepare "OK" tag answer (encoded): |
615 | CodeIso14443bAsTag(respOK, sizeof(respOK)); | |
616 | uint8_t *encodedOK = BigBuf_malloc(ToSendMax); | |
617 | uint16_t encodedOKLen = ToSendMax; | |
618 | memcpy(encodedOK, ToSend, ToSendMax); | |
11c2df83 | 619 | |
0923c43c | 620 | // Simulation loop |
dccddaef | 621 | while (!BUTTON_PRESS() && !usb_poll_validate_length()) { |
622 | WDT_HIT(); | |
489ef36c | 623 | |
dccddaef | 624 | // find reader field |
0923c43c | 625 | if (cardSTATE == SIM_NOFIELD) { |
dccddaef | 626 | vHf = (MAX_ADC_HF_VOLTAGE * AvgAdc(ADC_CHAN_HF)) >> 10; |
0923c43c | 627 | if ( vHf > MF_MINFIELDV ) { |
628 | cardSTATE = SIM_IDLE; | |
629 | LED_A_ON(); | |
630 | } | |
dccddaef | 631 | } |
0923c43c | 632 | if (cardSTATE == SIM_NOFIELD) continue; |
489ef36c | 633 | |
0923c43c | 634 | // Get reader command |
810f5379 | 635 | if (!GetIso14443bCommandFromReader(receivedCmd, &len)) { |
0923c43c | 636 | Dbprintf("button pressed, received %d commands", cmdsReceived); |
810f5379 | 637 | break; |
489ef36c | 638 | } |
639 | ||
0923c43c | 640 | // ISO14443-B protocol states: |
641 | // REQ or WUP request in ANY state | |
642 | // WUP in HALTED state | |
643 | if (len == 5 ) { | |
ffeb77fd | 644 | if ( (receivedCmd[0] == ISO14443B_REQB && (receivedCmd[2] & 0x8)== 0x8 && cardSTATE == SIM_HALTED) || |
645 | receivedCmd[0] == ISO14443B_REQB ){ | |
646 | LogTrace(receivedCmd, len, 0, 0, NULL, TRUE); | |
0923c43c | 647 | cardSTATE = SIM_SELECTING; |
0923c43c | 648 | } |
649 | } | |
650 | ||
651 | /* | |
652 | * How should this flow go? | |
653 | * REQB or WUPB | |
654 | * send response ( waiting for Attrib) | |
655 | * ATTRIB | |
656 | * send response ( waiting for commands 7816) | |
657 | * HALT | |
658 | send halt response ( waiting for wupb ) | |
659 | */ | |
d51717ff | 660 | |
b8622518 | 661 | switch (cardSTATE) { |
0923c43c | 662 | case SIM_NOFIELD: |
663 | case SIM_HALTED: | |
b8622518 | 664 | case SIM_IDLE: { |
dccddaef | 665 | LogTrace(receivedCmd, len, 0, 0, NULL, TRUE); |
666 | break; | |
667 | } | |
0923c43c | 668 | case SIM_SELECTING: { |
669 | TransmitFor14443b_AsTag( encodedATQB, encodedATQBLen ); | |
670 | LogTrace(respATQB, sizeof(respATQB), 0, 0, NULL, FALSE); | |
ffeb77fd | 671 | cardSTATE = SIM_WORK; |
dccddaef | 672 | break; |
0923c43c | 673 | } |
674 | case SIM_HALTING: { | |
675 | TransmitFor14443b_AsTag( encodedOK, encodedOKLen ); | |
676 | LogTrace(respOK, sizeof(respOK), 0, 0, NULL, FALSE); | |
677 | cardSTATE = SIM_HALTED; | |
dccddaef | 678 | break; |
0923c43c | 679 | } |
b8622518 | 680 | case SIM_ACKNOWLEDGE: { |
0923c43c | 681 | TransmitFor14443b_AsTag( encodedOK, encodedOKLen ); |
682 | LogTrace(respOK, sizeof(respOK), 0, 0, NULL, FALSE); | |
683 | cardSTATE = SIM_IDLE; | |
684 | break; | |
685 | } | |
b8622518 | 686 | case SIM_WORK: { |
d51717ff | 687 | if ( len == 7 && receivedCmd[0] == ISO14443B_HALT ) { |
688 | cardSTATE = SIM_HALTED; | |
689 | } else if ( len == 11 && receivedCmd[0] == ISO14443B_ATTRIB ) { | |
690 | cardSTATE = SIM_ACKNOWLEDGE; | |
691 | } else { | |
692 | // Todo: | |
693 | // - SLOT MARKER | |
694 | // - ISO7816 | |
695 | // - emulate with a memory dump | |
696 | Dbprintf("new cmd from reader: len=%d, cmdsRecvd=%d", len, cmdsReceived); | |
697 | ||
698 | // CRC Check | |
699 | uint8_t b1, b2; | |
700 | if (len >= 3){ // if crc exists | |
701 | ComputeCrc14443(CRC_14443_B, receivedCmd, len-2, &b1, &b2); | |
702 | if(b1 != receivedCmd[len-2] || b2 != receivedCmd[len-1]) | |
703 | DbpString("+++CRC fail"); | |
704 | else | |
705 | DbpString("CRC passes"); | |
706 | } | |
707 | cardSTATE = SIM_IDLE; | |
708 | } | |
dccddaef | 709 | break; |
d51717ff | 710 | } |
711 | default: break; | |
dccddaef | 712 | } |
713 | ||
0923c43c | 714 | ++cmdsReceived; |
b8622518 | 715 | // iceman, could add a switch to turn this on/off (if off, no logging?) |
0923c43c | 716 | if(cmdsReceived > 1000) { |
dccddaef | 717 | DbpString("14B Simulate, 1000 commands later..."); |
489ef36c | 718 | break; |
719 | } | |
489ef36c | 720 | } |
dccddaef | 721 | if (MF_DBGLEVEL >= 1) Dbprintf("Emulator stopped. Tracing: %d trace length: %d ", tracing, BigBuf_get_traceLen()); |
11c2df83 | 722 | switch_off(); //simulate |
489ef36c | 723 | } |
724 | ||
725 | //============================================================================= | |
726 | // An ISO 14443 Type B reader. We take layer two commands, code them | |
727 | // appropriately, and then send them to the tag. We then listen for the | |
728 | // tag's response, which we leave in the buffer to be demodulated on the | |
729 | // PC side. | |
730 | //============================================================================= | |
731 | ||
489ef36c | 732 | /* |
733 | * Handles reception of a bit from the tag | |
734 | * | |
abb21530 | 735 | * This function is called 2 times per bit (every 4 subcarrier cycles). |
736 | * Subcarrier frequency fs is 848kHz, 1/fs = 1,18us, i.e. function is called every 4,72us | |
737 | * | |
489ef36c | 738 | * LED handling: |
739 | * LED C -> ON once we have received the SOF and are expecting the rest. | |
740 | * LED C -> OFF once we have received EOF or are unsynced | |
741 | * | |
742 | * Returns: true if we received a EOF | |
743 | * false if we are still waiting for some more | |
744 | * | |
745 | */ | |
11c2df83 | 746 | static RAMFUNC int Handle14443bTagSamplesDemod(int ci, int cq) { |
29f8c2cc | 747 | int v = 0, myI = ABS(ci), myQ = ABS(cq); |
748 | ||
51d4f6f1 | 749 | // The soft decision on the bit uses an estimate of just the |
750 | // quadrant of the reference angle, not the exact angle. | |
489ef36c | 751 | #define MAKE_SOFT_DECISION() { \ |
5b59bf20 | 752 | if(Demod.sumI > 0) { \ |
753 | v = ci; \ | |
754 | } else { \ | |
755 | v = -ci; \ | |
756 | } \ | |
489ef36c | 757 | if(Demod.sumQ > 0) { \ |
758 | v += cq; \ | |
759 | } else { \ | |
760 | v -= cq; \ | |
761 | } \ | |
762 | } | |
763 | ||
cef590d9 | 764 | // Subcarrier amplitude v = sqrt(ci^2 + cq^2), approximated here by abs(ci) + abs(cq) |
abb21530 | 765 | // Subcarrier amplitude v = sqrt(ci^2 + cq^2), approximated here by max(abs(ci),abs(cq)) + 1/2*min(abs(ci),abs(cq))) |
b8622518 | 766 | #define CHECK_FOR_SUBCARRIER_old() { \ |
cef590d9 | 767 | if(ci < 0) { \ |
768 | if(cq < 0) { /* ci < 0, cq < 0 */ \ | |
769 | if (cq < ci) { \ | |
770 | v = -cq - (ci >> 1); \ | |
771 | } else { \ | |
772 | v = -ci - (cq >> 1); \ | |
773 | } \ | |
774 | } else { /* ci < 0, cq >= 0 */ \ | |
775 | if (cq < -ci) { \ | |
776 | v = -ci + (cq >> 1); \ | |
777 | } else { \ | |
778 | v = cq - (ci >> 1); \ | |
779 | } \ | |
780 | } \ | |
781 | } else { \ | |
782 | if(cq < 0) { /* ci >= 0, cq < 0 */ \ | |
783 | if (-cq < ci) { \ | |
784 | v = ci - (cq >> 1); \ | |
785 | } else { \ | |
786 | v = -cq + (ci >> 1); \ | |
787 | } \ | |
788 | } else { /* ci >= 0, cq >= 0 */ \ | |
789 | if (cq < ci) { \ | |
790 | v = ci + (cq >> 1); \ | |
791 | } else { \ | |
792 | v = cq + (ci >> 1); \ | |
793 | } \ | |
794 | } \ | |
795 | } \ | |
796 | } | |
db25599d | 797 | |
6fc68747 | 798 | //note: couldn't we just use MAX(ABS(ci),ABS(cq)) + (MIN(ABS(ci),ABS(cq))/2) from common.h - marshmellow |
b8622518 | 799 | #define CHECK_FOR_SUBCARRIER() { \ |
b8622518 | 800 | v = MAX(myI, myQ) + (MIN(myI, myQ) >> 1); \ |
6fc68747 | 801 | } |
db25599d | 802 | |
489ef36c | 803 | switch(Demod.state) { |
804 | case DEMOD_UNSYNCD: | |
cef590d9 | 805 | |
abb21530 | 806 | CHECK_FOR_SUBCARRIER(); |
c2df2883 | 807 | |
cef590d9 | 808 | // subcarrier detected |
86db8973 | 809 | if (v > SUBCARRIER_DETECT_THRESHOLD) { |
489ef36c | 810 | Demod.state = DEMOD_PHASE_REF_TRAINING; |
abb21530 | 811 | Demod.sumI = ci; |
812 | Demod.sumQ = cq; | |
813 | Demod.posCount = 1; | |
489ef36c | 814 | } |
815 | break; | |
816 | ||
817 | case DEMOD_PHASE_REF_TRAINING: | |
86db8973 | 818 | if (Demod.posCount < 8) { |
cef590d9 | 819 | |
abb21530 | 820 | CHECK_FOR_SUBCARRIER(); |
cef590d9 | 821 | |
abb21530 | 822 | if (v > SUBCARRIER_DETECT_THRESHOLD) { |
823 | // set the reference phase (will code a logic '1') by averaging over 32 1/fs. | |
824 | // note: synchronization time > 80 1/fs | |
b10a759f | 825 | Demod.sumI += ci; |
826 | Demod.sumQ += cq; | |
cef590d9 | 827 | ++Demod.posCount; |
828 | } else { | |
829 | // subcarrier lost | |
b10a759f | 830 | Demod.state = DEMOD_UNSYNCD; |
abb21530 | 831 | } |
489ef36c | 832 | } else { |
b10a759f | 833 | Demod.state = DEMOD_AWAITING_FALLING_EDGE_OF_SOF; |
489ef36c | 834 | } |
489ef36c | 835 | break; |
836 | ||
837 | case DEMOD_AWAITING_FALLING_EDGE_OF_SOF: | |
cef590d9 | 838 | |
489ef36c | 839 | MAKE_SOFT_DECISION(); |
cef590d9 | 840 | |
86db8973 | 841 | if (v < 0) { // logic '0' detected |
489ef36c | 842 | Demod.state = DEMOD_GOT_FALLING_EDGE_OF_SOF; |
abb21530 | 843 | Demod.posCount = 0; // start of SOF sequence |
489ef36c | 844 | } else { |
cef590d9 | 845 | // maximum length of TR1 = 200 1/fs |
c3e8413c | 846 | if(Demod.posCount > 26*2) Demod.state = DEMOD_UNSYNCD; |
489ef36c | 847 | } |
cef590d9 | 848 | ++Demod.posCount; |
489ef36c | 849 | break; |
850 | ||
851 | case DEMOD_GOT_FALLING_EDGE_OF_SOF: | |
cef590d9 | 852 | ++Demod.posCount; |
853 | ||
489ef36c | 854 | MAKE_SOFT_DECISION(); |
cef590d9 | 855 | |
86db8973 | 856 | if (v > 0) { |
cef590d9 | 857 | // low phase of SOF too short (< 9 etu). Note: spec is >= 10, but FPGA tends to "smear" edges |
86db8973 | 858 | if (Demod.posCount < 8*2) { |
489ef36c | 859 | Demod.state = DEMOD_UNSYNCD; |
860 | } else { | |
a62bf3af | 861 | LED_C_ON(); // Got SOF |
86db8973 | 862 | //Demod.startTime = GetCountSspClk(); |
489ef36c | 863 | Demod.state = DEMOD_AWAITING_START_BIT; |
864 | Demod.posCount = 0; | |
865 | Demod.len = 0; | |
489ef36c | 866 | } |
867 | } else { | |
cef590d9 | 868 | // low phase of SOF too long (> 12 etu) |
c3e8413c | 869 | if (Demod.posCount > 14*2) { |
489ef36c | 870 | Demod.state = DEMOD_UNSYNCD; |
47286d89 | 871 | LED_C_OFF(); |
489ef36c | 872 | } |
873 | } | |
489ef36c | 874 | break; |
875 | ||
876 | case DEMOD_AWAITING_START_BIT: | |
cef590d9 | 877 | ++Demod.posCount; |
878 | ||
489ef36c | 879 | MAKE_SOFT_DECISION(); |
cef590d9 | 880 | |
881 | if (v > 0) { | |
c3e8413c | 882 | if(Demod.posCount > 2*2) { // max 19us between characters = 16 1/fs, max 3 etu after low phase of SOF = 24 1/fs |
489ef36c | 883 | Demod.state = DEMOD_UNSYNCD; |
47286d89 | 884 | LED_C_OFF(); |
489ef36c | 885 | } |
abb21530 | 886 | } else { // start bit detected |
489ef36c | 887 | Demod.bitCount = 0; |
abb21530 | 888 | Demod.posCount = 1; // this was the first half |
489ef36c | 889 | Demod.thisBit = v; |
890 | Demod.shiftReg = 0; | |
891 | Demod.state = DEMOD_RECEIVING_DATA; | |
892 | } | |
893 | break; | |
894 | ||
895 | case DEMOD_RECEIVING_DATA: | |
cef590d9 | 896 | |
489ef36c | 897 | MAKE_SOFT_DECISION(); |
cef590d9 | 898 | |
899 | if (Demod.posCount == 0) { | |
900 | // first half of bit | |
489ef36c | 901 | Demod.thisBit = v; |
902 | Demod.posCount = 1; | |
cef590d9 | 903 | } else { |
904 | // second half of bit | |
489ef36c | 905 | Demod.thisBit += v; |
489ef36c | 906 | Demod.shiftReg >>= 1; |
489ef36c | 907 | |
86db8973 | 908 | // OR in a logic '1' |
b8622518 | 909 | if (Demod.thisBit > 0) Demod.shiftReg |= 0x200; |
cef590d9 | 910 | |
911 | ++Demod.bitCount; | |
912 | ||
b8622518 | 913 | // 1 start 8 data 1 stop = 10 |
914 | if (Demod.bitCount == 10) { | |
cef590d9 | 915 | |
489ef36c | 916 | uint16_t s = Demod.shiftReg; |
cef590d9 | 917 | |
918 | // stop bit == '1', start bit == '0' | |
29f8c2cc | 919 | if ((s & 0x200) && (s & 0x001) == 0 ) { |
920 | // left shift to drop the startbit | |
921 | Demod.output[Demod.len] = (s >> 1) & 0xFF; | |
cef590d9 | 922 | ++Demod.len; |
489ef36c | 923 | Demod.state = DEMOD_AWAITING_START_BIT; |
489ef36c | 924 | } else { |
29f8c2cc | 925 | // this one is a bit hard, either its a correc byte or its unsynced. |
489ef36c | 926 | Demod.state = DEMOD_UNSYNCD; |
86db8973 | 927 | //Demod.endTime = GetCountSspClk(); |
47286d89 | 928 | LED_C_OFF(); |
cef590d9 | 929 | |
930 | // This is EOF (start, stop and all data bits == '0' | |
29f8c2cc | 931 | if (s == 0) return TRUE; |
489ef36c | 932 | } |
933 | } | |
934 | Demod.posCount = 0; | |
935 | } | |
936 | break; | |
937 | ||
938 | default: | |
939 | Demod.state = DEMOD_UNSYNCD; | |
47286d89 | 940 | LED_C_OFF(); |
489ef36c | 941 | break; |
942 | } | |
489ef36c | 943 | return FALSE; |
944 | } | |
945 | ||
946 | ||
489ef36c | 947 | /* |
948 | * Demodulate the samples we received from the tag, also log to tracebuffer | |
489ef36c | 949 | * quiet: set to 'TRUE' to disable debug output |
950 | */ | |
dccddaef | 951 | static void GetTagSamplesFor14443bDemod() { |
b8622518 | 952 | bool gotFrame = FALSE, finished = FALSE; |
11c2df83 | 953 | int lastRxCounter = ISO14443B_DMA_BUFFER_SIZE; |
29f8c2cc | 954 | int ci = 0, cq = 0; |
11c2df83 | 955 | uint32_t time_0 = 0, time_stop = 0; |
489ef36c | 956 | |
11c2df83 | 957 | BigBuf_free(); |
958 | ||
489ef36c | 959 | // Set up the demodulator for tag -> reader responses. |
db25599d | 960 | DemodInit(BigBuf_malloc(MAX_FRAME_SIZE)); |
b10a759f | 961 | |
962 | // The DMA buffer, used to stream samples from the FPGA | |
963 | int8_t *dmaBuf = (int8_t*) BigBuf_malloc(ISO14443B_DMA_BUFFER_SIZE); | |
11c2df83 | 964 | int8_t *upTo = dmaBuf; |
cef590d9 | 965 | |
db25599d | 966 | // Setup and start DMA. |
11c2df83 | 967 | if ( !FpgaSetupSscDma((uint8_t*) dmaBuf, ISO14443B_DMA_BUFFER_SIZE) ){ |
968 | if (MF_DBGLEVEL > 1) Dbprintf("FpgaSetupSscDma failed. Exiting"); | |
969 | return; | |
970 | } | |
b8622518 | 971 | |
11c2df83 | 972 | // And put the FPGA in the appropriate mode |
973 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR | FPGA_HF_READER_RX_XCORR_848_KHZ); | |
489ef36c | 974 | |
b8622518 | 975 | // get current clock |
976 | time_0 = GetCountSspClk(); | |
977 | ||
978 | // rx counter - dma counter? (how much?) & (mod) mask > 2. (since 2bytes at the time is read) | |
979 | while ( !finished ) { | |
489ef36c | 980 | |
b8622518 | 981 | LED_A_INV(); |
982 | WDT_HIT(); | |
11c2df83 | 983 | |
b8622518 | 984 | // LSB is a fpga signal bit. |
985 | ci = upTo[0] >> 1; | |
986 | cq = upTo[1] >> 1; | |
987 | upTo += 2; | |
b8622518 | 988 | lastRxCounter -= 2; |
989 | ||
990 | // restart DMA buffer to receive again. | |
991 | if(upTo >= dmaBuf + ISO14443B_DMA_BUFFER_SIZE) { | |
992 | upTo = dmaBuf; | |
993 | lastRxCounter = ISO14443B_DMA_BUFFER_SIZE; | |
994 | AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) upTo; | |
995 | AT91C_BASE_PDC_SSC->PDC_RNCR = ISO14443B_DMA_BUFFER_SIZE; | |
489ef36c | 996 | } |
997 | ||
b8622518 | 998 | // https://github.com/Proxmark/proxmark3/issues/103 |
b8622518 | 999 | gotFrame = Handle14443bTagSamplesDemod(ci, cq); |
11c2df83 | 1000 | time_stop = GetCountSspClk() - time_0; |
b8622518 | 1001 | |
1002 | finished = (time_stop > iso14b_timeout || gotFrame); | |
489ef36c | 1003 | } |
11c2df83 | 1004 | |
1005 | FpgaDisableSscDma(); | |
d8b7a5f2 | 1006 | |
1007 | if ( upTo ) upTo = NULL; | |
1008 | ||
dccddaef | 1009 | if (MF_DBGLEVEL >= 3) { |
d8b7a5f2 | 1010 | Dbprintf("Demod.state = %d, Demod.len = %u, PDC_RCR = %u", |
cef590d9 | 1011 | Demod.state, |
d8b7a5f2 | 1012 | Demod.len, |
1013 | AT91C_BASE_PDC_SSC->PDC_RCR | |
b10a759f | 1014 | ); |
1015 | } | |
b8622518 | 1016 | |
d8b7a5f2 | 1017 | // print the last batch of IQ values from FPGA |
b8622518 | 1018 | if (MF_DBGLEVEL == 4) |
1019 | Dbhexdump(ISO14443B_DMA_BUFFER_SIZE, (uint8_t *)dmaBuf, FALSE); | |
1020 | ||
11c2df83 | 1021 | if ( Demod.len > 0 ) |
86db8973 | 1022 | LogTrace(Demod.output, Demod.len, time_0, time_stop, NULL, FALSE); |
489ef36c | 1023 | } |
1024 | ||
1025 | ||
489ef36c | 1026 | //----------------------------------------------------------------------------- |
1027 | // Transmit the command (to the tag) that was placed in ToSend[]. | |
1028 | //----------------------------------------------------------------------------- | |
11c2df83 | 1029 | static void TransmitFor14443b_AsReader(void) { |
489ef36c | 1030 | |
11c2df83 | 1031 | // we could been in following mode: |
1032 | // FPGA_MAJOR_MODE_HF_READER_RX_XCORR | FPGA_HF_READER_RX_XCORR_848_KHZ | |
1033 | // if its second call or more | |
c3e8413c | 1034 | |
1035 | // while(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) { | |
1036 | // AT91C_BASE_SSC->SSC_THR = 0XFF; | |
1037 | // } | |
1038 | ||
1039 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX | FPGA_HF_READER_TX_SHALLOW_MOD); | |
1040 | SpinDelay(40); | |
11c2df83 | 1041 | |
c3e8413c | 1042 | int c; |
1043 | volatile uint32_t b; | |
1044 | ||
11c2df83 | 1045 | // What does this loop do? Is it TR1? |
c3e8413c | 1046 | // 0xFF = 8 bits of 1. 1 bit == 1Etu,.. |
1047 | // loop 10 * 8 = 80 ETU of delay, with a non modulated signal. why? | |
1048 | // 80*9 = 720us. | |
1049 | for(c = 0; c < 50;) { | |
489ef36c | 1050 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) { |
11c2df83 | 1051 | AT91C_BASE_SSC->SSC_THR = 0xFF; |
cef590d9 | 1052 | ++c; |
489ef36c | 1053 | } |
c3e8413c | 1054 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { |
1055 | b = AT91C_BASE_SSC->SSC_RHR; | |
1056 | (void)b; | |
1057 | } | |
489ef36c | 1058 | } |
c3e8413c | 1059 | |
11c2df83 | 1060 | // Send frame loop |
1061 | for(c = 0; c < ToSendMax;) { | |
489ef36c | 1062 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) { |
c3e8413c | 1063 | AT91C_BASE_SSC->SSC_THR = ToSend[c++]; |
489ef36c | 1064 | } |
c3e8413c | 1065 | if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { |
1066 | b = AT91C_BASE_SSC->SSC_RHR; | |
1067 | (void)b; | |
1068 | } | |
489ef36c | 1069 | } |
c3e8413c | 1070 | //WaitForFpgaDelayQueueIsEmpty(delay); |
1071 | // We should wait here for the FPGA to send all bits. | |
11c2df83 | 1072 | WDT_HIT(); |
489ef36c | 1073 | } |
1074 | ||
489ef36c | 1075 | //----------------------------------------------------------------------------- |
1076 | // Code a layer 2 command (string of octets, including CRC) into ToSend[], | |
abb21530 | 1077 | // so that it is ready to transmit to the tag using TransmitFor14443b(). |
489ef36c | 1078 | //----------------------------------------------------------------------------- |
86db8973 | 1079 | static void CodeIso14443bAsReader(const uint8_t *cmd, int len) { |
11c2df83 | 1080 | /* |
1081 | * Reader data transmission: | |
1082 | * - no modulation ONES | |
1083 | * - SOF | |
1084 | * - Command, data and CRC_B | |
1085 | * - EOF | |
1086 | * - no modulation ONES | |
1087 | * | |
1088 | * 1 ETU == 1 BIT! | |
1089 | * TR0 - 8 ETUS minimum. | |
c3e8413c | 1090 | * |
1091 | * QUESTION: how long is a 1 or 0 in pulses in the xcorr_848 mode? | |
1092 | * 1 "stuffbit" = 1ETU (9us) | |
11c2df83 | 1093 | */ |
1094 | int i; | |
489ef36c | 1095 | uint8_t b; |
11c2df83 | 1096 | |
489ef36c | 1097 | ToSendReset(); |
1098 | ||
489ef36c | 1099 | // Send SOF |
11c2df83 | 1100 | // 10-11 ETUs of ZERO |
1101 | for(i = 0; i < 10; ++i) ToSendStuffBit(0); | |
1102 | ||
1103 | // 2-3 ETUs of ONE | |
1104 | ToSendStuffBit(1); | |
1105 | ToSendStuffBit(1); | |
1106 | ToSendStuffBit(1); | |
1107 | ||
1108 | // Sending cmd, LSB | |
1109 | // from here we add BITS | |
6fc68747 | 1110 | for(i = 0; i < len; ++i) { |
11c2df83 | 1111 | // Start bit |
489ef36c | 1112 | ToSendStuffBit(0); |
1113 | // Data bits | |
11c2df83 | 1114 | b = cmd[i]; |
86db8973 | 1115 | // if ( b & 1 ) ToSendStuffBit(1); else ToSendStuffBit(0); |
1116 | // if ( (b>>1) & 1) ToSendStuffBit(1); else ToSendStuffBit(0); | |
1117 | // if ( (b>>2) & 1) ToSendStuffBit(1); else ToSendStuffBit(0); | |
1118 | // if ( (b>>3) & 1) ToSendStuffBit(1); else ToSendStuffBit(0); | |
1119 | // if ( (b>>4) & 1) ToSendStuffBit(1); else ToSendStuffBit(0); | |
1120 | // if ( (b>>5) & 1) ToSendStuffBit(1); else ToSendStuffBit(0); | |
1121 | // if ( (b>>6) & 1) ToSendStuffBit(1); else ToSendStuffBit(0); | |
1122 | // if ( (b>>7) & 1) ToSendStuffBit(1); else ToSendStuffBit(0); | |
1123 | ||
1124 | ToSendStuffBit( b & 1); | |
1125 | ToSendStuffBit( (b>>1) & 1); | |
1126 | ToSendStuffBit( (b>>2) & 1); | |
1127 | ToSendStuffBit( (b>>3) & 1); | |
1128 | ToSendStuffBit( (b>>4) & 1); | |
1129 | ToSendStuffBit( (b>>5) & 1); | |
1130 | ToSendStuffBit( (b>>6) & 1); | |
1131 | ToSendStuffBit( (b>>7) & 1); | |
1132 | ||
11c2df83 | 1133 | // Stop bit |
489ef36c | 1134 | ToSendStuffBit(1); |
11c2df83 | 1135 | // EGT extra guard time |
1136 | // For PCD it ranges 0-57us (1etu = 9us) | |
489ef36c | 1137 | ToSendStuffBit(1); |
11c2df83 | 1138 | ToSendStuffBit(1); |
1139 | ToSendStuffBit(1); | |
1140 | } | |
1141 | ||
1142 | // Send EOF | |
1143 | // 10-11 ETUs of ZERO | |
1144 | for(i = 0; i < 10; ++i) ToSendStuffBit(0); | |
489ef36c | 1145 | |
11c2df83 | 1146 | // Transition time. TR0 - guard time |
1147 | // 8ETUS minum? | |
1148 | // Per specification, Subcarrier must be stopped no later than 2 ETUs after EOF. | |
c3e8413c | 1149 | // I'm guessing this is for the FPGA to be able to send all bits before we switch to listening mode |
1150 | for(i = 0; i < 32 ; ++i) ToSendStuffBit(1); | |
11c2df83 | 1151 | |
1152 | // TR1 - Synchronization time | |
489ef36c | 1153 | // Convert from last character reference to length |
cef590d9 | 1154 | ++ToSendMax; |
489ef36c | 1155 | } |
1156 | ||
1157 | ||
86db8973 | 1158 | /* |
1159 | * Convenience function to encode, transmit and trace iso 14443b comms | |
1160 | */ | |
11c2df83 | 1161 | static void CodeAndTransmit14443bAsReader(const uint8_t *cmd, int len) { |
86db8973 | 1162 | |
1163 | uint32_t time_start = GetCountSspClk(); | |
11c2df83 | 1164 | |
489ef36c | 1165 | CodeIso14443bAsReader(cmd, len); |
6fc68747 | 1166 | |
11c2df83 | 1167 | TransmitFor14443b_AsReader(); |
86db8973 | 1168 | |
6fc68747 | 1169 | if(trigger) LED_A_ON(); |
86db8973 | 1170 | |
dccddaef | 1171 | LogTrace(cmd, len, time_start, GetCountSspClk()-time_start, NULL, TRUE); |
489ef36c | 1172 | } |
1173 | ||
a62bf3af | 1174 | /* Sends an APDU to the tag |
1175 | * TODO: check CRC and preamble | |
1176 | */ | |
6fc68747 | 1177 | uint8_t iso14443b_apdu(uint8_t const *message, size_t message_length, uint8_t *response) |
a62bf3af | 1178 | { |
6fc68747 | 1179 | uint8_t crc[2] = {0x00, 0x00}; |
a62bf3af | 1180 | uint8_t message_frame[message_length + 4]; |
1181 | // PCB | |
1182 | message_frame[0] = 0x0A | pcb_blocknum; | |
1183 | pcb_blocknum ^= 1; | |
1184 | // CID | |
1185 | message_frame[1] = 0; | |
1186 | // INF | |
1187 | memcpy(message_frame + 2, message, message_length); | |
1188 | // EDC (CRC) | |
1189 | ComputeCrc14443(CRC_14443_B, message_frame, message_length + 2, &message_frame[message_length + 2], &message_frame[message_length + 3]); | |
1190 | // send | |
11c2df83 | 1191 | CodeAndTransmit14443bAsReader(message_frame, message_length + 4); //no |
a62bf3af | 1192 | // get response |
dccddaef | 1193 | GetTagSamplesFor14443bDemod(); //no |
a62bf3af | 1194 | if(Demod.len < 3) |
a62bf3af | 1195 | return 0; |
cef590d9 | 1196 | |
6fc68747 | 1197 | // VALIDATE CRC |
1198 | ComputeCrc14443(CRC_14443_B, Demod.output, Demod.len-2, &crc[0], &crc[1]); | |
1199 | if ( crc[0] != Demod.output[Demod.len-2] || crc[1] != Demod.output[Demod.len-1] ) | |
1200 | return 0; | |
1201 | ||
a62bf3af | 1202 | // copy response contents |
1203 | if(response != NULL) | |
a62bf3af | 1204 | memcpy(response, Demod.output, Demod.len); |
cef590d9 | 1205 | |
a62bf3af | 1206 | return Demod.len; |
1207 | } | |
1208 | ||
6fc68747 | 1209 | /** |
1210 | * SRx Initialise. | |
1211 | */ | |
1212 | uint8_t iso14443b_select_srx_card(iso14b_card_select_t *card ) | |
1213 | { | |
1214 | // INITIATE command: wake up the tag using the INITIATE | |
1215 | static const uint8_t init_srx[] = { ISO14443B_INITIATE, 0x00, 0x97, 0x5b }; | |
1216 | // SELECT command (with space for CRC) | |
1217 | uint8_t select_srx[] = { ISO14443B_SELECT, 0x00, 0x00, 0x00}; | |
1218 | // temp to calc crc. | |
1219 | uint8_t crc[2] = {0x00, 0x00}; | |
1220 | ||
1221 | CodeAndTransmit14443bAsReader(init_srx, sizeof(init_srx)); | |
dccddaef | 1222 | GetTagSamplesFor14443bDemod(); //no |
6fc68747 | 1223 | |
1224 | if (Demod.len == 0) return 2; | |
1225 | ||
1226 | // Randomly generated Chip ID | |
1227 | if (card) card->chipid = Demod.output[0]; | |
1228 | ||
1229 | select_srx[1] = Demod.output[0]; | |
1230 | ||
1231 | ComputeCrc14443(CRC_14443_B, select_srx, 2, &select_srx[2], &select_srx[3]); | |
1232 | CodeAndTransmit14443bAsReader(select_srx, sizeof(select_srx)); | |
dccddaef | 1233 | GetTagSamplesFor14443bDemod(); //no |
6fc68747 | 1234 | |
1235 | if (Demod.len != 3) return 2; | |
1236 | ||
1237 | // Check the CRC of the answer: | |
1238 | ComputeCrc14443(CRC_14443_B, Demod.output, Demod.len-2 , &crc[0], &crc[1]); | |
1239 | if(crc[0] != Demod.output[1] || crc[1] != Demod.output[2]) return 3; | |
1240 | ||
1241 | // Check response from the tag: should be the same UID as the command we just sent: | |
1242 | if (select_srx[1] != Demod.output[0]) return 1; | |
1243 | ||
1244 | // First get the tag's UID: | |
1245 | select_srx[0] = ISO14443B_GET_UID; | |
1246 | ||
1247 | ComputeCrc14443(CRC_14443_B, select_srx, 1 , &select_srx[1], &select_srx[2]); | |
1248 | CodeAndTransmit14443bAsReader(select_srx, 3); // Only first three bytes for this one | |
dccddaef | 1249 | GetTagSamplesFor14443bDemod(); //no |
6fc68747 | 1250 | |
1251 | if (Demod.len != 10) return 2; | |
1252 | ||
1253 | // The check the CRC of the answer | |
1254 | ComputeCrc14443(CRC_14443_B, Demod.output, Demod.len-2, &crc[0], &crc[1]); | |
1255 | if(crc[0] != Demod.output[8] || crc[1] != Demod.output[9]) return 3; | |
1256 | ||
1257 | if (card) { | |
1258 | card->uidlen = 8; | |
1259 | memcpy(card->uid, Demod.output, 8); | |
1260 | } | |
1261 | ||
1262 | return 0; | |
1263 | } | |
a62bf3af | 1264 | /* Perform the ISO 14443 B Card Selection procedure |
1265 | * Currently does NOT do any collision handling. | |
1266 | * It expects 0-1 cards in the device's range. | |
1267 | * TODO: Support multiple cards (perform anticollision) | |
1268 | * TODO: Verify CRC checksums | |
1269 | */ | |
6fc68747 | 1270 | uint8_t iso14443b_select_card(iso14b_card_select_t *card ) |
a62bf3af | 1271 | { |
1272 | // WUPB command (including CRC) | |
1273 | // Note: WUPB wakes up all tags, REQB doesn't wake up tags in HALT state | |
6fc68747 | 1274 | static const uint8_t wupb[] = { ISO14443B_REQB, 0x00, 0x08, 0x39, 0x73 }; |
a62bf3af | 1275 | // ATTRIB command (with space for CRC) |
6fc68747 | 1276 | uint8_t attrib[] = { ISO14443B_ATTRIB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00}; |
a62bf3af | 1277 | |
6fc68747 | 1278 | // temp to calc crc. |
1279 | uint8_t crc[2] = {0x00, 0x00}; | |
1280 | ||
a62bf3af | 1281 | // first, wake up the tag |
1282 | CodeAndTransmit14443bAsReader(wupb, sizeof(wupb)); | |
dccddaef | 1283 | GetTagSamplesFor14443bDemod(); //select_card |
6fc68747 | 1284 | |
a62bf3af | 1285 | // ATQB too short? |
6fc68747 | 1286 | if (Demod.len < 14) return 2; |
1287 | ||
1288 | // VALIDATE CRC | |
1289 | ComputeCrc14443(CRC_14443_B, Demod.output, Demod.len-2, &crc[0], &crc[1]); | |
1290 | if ( crc[0] != Demod.output[12] || crc[1] != Demod.output[13] ) | |
1291 | return 3; | |
1292 | ||
1293 | if (card) { | |
1294 | card->uidlen = 4; | |
1295 | memcpy(card->uid, Demod.output+1, 4); | |
1296 | memcpy(card->atqb, Demod.output+5, 7); | |
1297 | } | |
a62bf3af | 1298 | |
11c2df83 | 1299 | // copy the PUPI to ATTRIB ( PUPI == UID ) |
a62bf3af | 1300 | memcpy(attrib + 1, Demod.output + 1, 4); |
6fc68747 | 1301 | |
1302 | // copy the protocol info from ATQB (Protocol Info -> Protocol_Type) into ATTRIB (Param 3) | |
a62bf3af | 1303 | attrib[7] = Demod.output[10] & 0x0F; |
1304 | ComputeCrc14443(CRC_14443_B, attrib, 9, attrib + 9, attrib + 10); | |
6fc68747 | 1305 | |
a62bf3af | 1306 | CodeAndTransmit14443bAsReader(attrib, sizeof(attrib)); |
dccddaef | 1307 | GetTagSamplesFor14443bDemod();//select_card |
6fc68747 | 1308 | |
a62bf3af | 1309 | // Answer to ATTRIB too short? |
6fc68747 | 1310 | if(Demod.len < 3) return 2; |
1311 | ||
1312 | // VALIDATE CRC | |
1313 | ComputeCrc14443(CRC_14443_B, Demod.output, Demod.len-2, &crc[0], &crc[1]); | |
1314 | if ( crc[0] != Demod.output[1] || crc[1] != Demod.output[2] ) | |
1315 | return 3; | |
29f8c2cc | 1316 | |
65cdf0e3 | 1317 | if (card) { |
29f8c2cc | 1318 | |
1319 | // CID | |
65cdf0e3 | 1320 | card->cid = Demod.output[0]; |
29f8c2cc | 1321 | |
1322 | // MAX FRAME | |
1323 | uint16_t maxFrame = card->atqb[5] >> 4; | |
1324 | if (maxFrame < 5) maxFrame = 8 * maxFrame + 16; | |
1325 | else if (maxFrame == 5) maxFrame = 64; | |
1326 | else if (maxFrame == 6) maxFrame = 96; | |
1327 | else if (maxFrame == 7) maxFrame = 128; | |
1328 | else if (maxFrame == 8) maxFrame = 256; | |
1329 | else maxFrame = 257; | |
1330 | iso14b_set_maxframesize(maxFrame); | |
1331 | ||
1332 | // FWT | |
65cdf0e3 | 1333 | uint8_t fwt = card->atqb[6] >> 4; |
1334 | if ( fwt < 16 ){ | |
1335 | uint32_t fwt_time = (302 << fwt); | |
1336 | iso14b_set_timeout( fwt_time); | |
1337 | } | |
11c2df83 | 1338 | } |
a62bf3af | 1339 | // reset PCB block number |
1340 | pcb_blocknum = 0; | |
6fc68747 | 1341 | return 0; |
a62bf3af | 1342 | } |
1343 | ||
1344 | // Set up ISO 14443 Type B communication (similar to iso14443a_setup) | |
11c2df83 | 1345 | // field is setup for "Sending as Reader" |
a62bf3af | 1346 | void iso14443b_setup() { |
11c2df83 | 1347 | if (MF_DBGLEVEL > 3) Dbprintf("iso1443b_setup Enter"); |
1348 | LEDsoff(); | |
a62bf3af | 1349 | FpgaDownloadAndGo(FPGA_BITSTREAM_HF); |
11c2df83 | 1350 | //BigBuf_free(); |
1351 | //BigBuf_Clear_ext(false); | |
ff3e0744 | 1352 | |
11c2df83 | 1353 | // Initialize Demod and Uart structs |
1354 | DemodInit(BigBuf_malloc(MAX_FRAME_SIZE)); | |
1355 | UartInit(BigBuf_malloc(MAX_FRAME_SIZE)); | |
cef590d9 | 1356 | |
a62bf3af | 1357 | // connect Demodulated Signal to ADC: |
1358 | SetAdcMuxFor(GPIO_MUXSEL_HIPKD); | |
1359 | ||
11c2df83 | 1360 | // Set up the synchronous serial port |
1361 | FpgaSetupSsc(); | |
1362 | ||
a62bf3af | 1363 | // Signal field is on with the appropriate LED |
a62bf3af | 1364 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX | FPGA_HF_READER_TX_SHALLOW_MOD); |
11c2df83 | 1365 | SpinDelay(100); |
a62bf3af | 1366 | |
1367 | // Start the timer | |
ff3e0744 | 1368 | StartCountSspClk(); |
11c2df83 | 1369 | |
1370 | LED_D_ON(); | |
1371 | if (MF_DBGLEVEL > 3) Dbprintf("iso1443b_setup Exit"); | |
a62bf3af | 1372 | } |
489ef36c | 1373 | |
1374 | //----------------------------------------------------------------------------- | |
abb21530 | 1375 | // Read a SRI512 ISO 14443B tag. |
489ef36c | 1376 | // |
1377 | // SRI512 tags are just simple memory tags, here we're looking at making a dump | |
1378 | // of the contents of the memory. No anticollision algorithm is done, we assume | |
1379 | // we have a single tag in the field. | |
1380 | // | |
1381 | // I tried to be systematic and check every answer of the tag, every CRC, etc... | |
1382 | //----------------------------------------------------------------------------- | |
6fc68747 | 1383 | void ReadSTMemoryIso14443b(uint8_t numofblocks) |
489ef36c | 1384 | { |
17ad0e09 | 1385 | FpgaDownloadAndGo(FPGA_BITSTREAM_HF); |
489ef36c | 1386 | |
489ef36c | 1387 | // Make sure that we start from off, since the tags are stateful; |
1388 | // confusing things will happen if we don't reset them between reads. | |
11c2df83 | 1389 | switch_off(); // before ReadStMemory |
1390 | ||
1391 | set_tracing(TRUE); | |
1392 | ||
1393 | uint8_t i = 0x00; | |
99cf19d9 | 1394 | |
489ef36c | 1395 | SetAdcMuxFor(GPIO_MUXSEL_HIPKD); |
1396 | FpgaSetupSsc(); | |
1397 | ||
1398 | // Now give it time to spin up. | |
1399 | // Signal field is on with the appropriate LED | |
1400 | LED_D_ON(); | |
22e24700 | 1401 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR | FPGA_HF_READER_RX_XCORR_848_KHZ); |
11c2df83 | 1402 | SpinDelay(20); |
489ef36c | 1403 | |
1404 | // First command: wake up the tag using the INITIATE command | |
6fc68747 | 1405 | uint8_t cmd1[] = {ISO14443B_INITIATE, 0x00, 0x97, 0x5b}; |
11c2df83 | 1406 | CodeAndTransmit14443bAsReader(cmd1, sizeof(cmd1)); //no |
dccddaef | 1407 | GetTagSamplesFor14443bDemod(); // no |
489ef36c | 1408 | |
1409 | if (Demod.len == 0) { | |
22e24700 | 1410 | DbpString("No response from tag"); |
5ee53a0e | 1411 | set_tracing(FALSE); |
22e24700 | 1412 | return; |
489ef36c | 1413 | } else { |
705bfa10 | 1414 | Dbprintf("Randomly generated Chip ID (+ 2 byte CRC): %02x %02x %02x", |
1415 | Demod.output[0], Demod.output[1], Demod.output[2]); | |
489ef36c | 1416 | } |
705bfa10 | 1417 | |
489ef36c | 1418 | // There is a response, SELECT the uid |
1419 | DbpString("Now SELECT tag:"); | |
6fc68747 | 1420 | cmd1[0] = ISO14443B_SELECT; // 0x0E is SELECT |
489ef36c | 1421 | cmd1[1] = Demod.output[0]; |
1422 | ComputeCrc14443(CRC_14443_B, cmd1, 2, &cmd1[2], &cmd1[3]); | |
11c2df83 | 1423 | CodeAndTransmit14443bAsReader(cmd1, sizeof(cmd1)); //no |
dccddaef | 1424 | GetTagSamplesFor14443bDemod(); //no |
489ef36c | 1425 | if (Demod.len != 3) { |
22e24700 | 1426 | Dbprintf("Expected 3 bytes from tag, got %d", Demod.len); |
5ee53a0e | 1427 | set_tracing(FALSE); |
22e24700 | 1428 | return; |
489ef36c | 1429 | } |
1430 | // Check the CRC of the answer: | |
1431 | ComputeCrc14443(CRC_14443_B, Demod.output, 1 , &cmd1[2], &cmd1[3]); | |
1432 | if(cmd1[2] != Demod.output[1] || cmd1[3] != Demod.output[2]) { | |
22e24700 | 1433 | DbpString("CRC Error reading select response."); |
5ee53a0e | 1434 | set_tracing(FALSE); |
22e24700 | 1435 | return; |
489ef36c | 1436 | } |
1437 | // Check response from the tag: should be the same UID as the command we just sent: | |
1438 | if (cmd1[1] != Demod.output[0]) { | |
22e24700 | 1439 | Dbprintf("Bad response to SELECT from Tag, aborting: %02x %02x", cmd1[1], Demod.output[0]); |
5ee53a0e | 1440 | set_tracing(FALSE); |
22e24700 | 1441 | return; |
489ef36c | 1442 | } |
705bfa10 | 1443 | |
489ef36c | 1444 | // Tag is now selected, |
1445 | // First get the tag's UID: | |
6fc68747 | 1446 | cmd1[0] = ISO14443B_GET_UID; |
489ef36c | 1447 | ComputeCrc14443(CRC_14443_B, cmd1, 1 , &cmd1[1], &cmd1[2]); |
11c2df83 | 1448 | CodeAndTransmit14443bAsReader(cmd1, 3); // no -- Only first three bytes for this one |
dccddaef | 1449 | GetTagSamplesFor14443bDemod(); //no |
489ef36c | 1450 | if (Demod.len != 10) { |
22e24700 | 1451 | Dbprintf("Expected 10 bytes from tag, got %d", Demod.len); |
5ee53a0e | 1452 | set_tracing(FALSE); |
22e24700 | 1453 | return; |
489ef36c | 1454 | } |
1455 | // The check the CRC of the answer (use cmd1 as temporary variable): | |
1456 | ComputeCrc14443(CRC_14443_B, Demod.output, 8, &cmd1[2], &cmd1[3]); | |
51d4f6f1 | 1457 | if(cmd1[2] != Demod.output[8] || cmd1[3] != Demod.output[9]) { |
22e24700 | 1458 | Dbprintf("CRC Error reading block! Expected: %04x got: %04x", |
705bfa10 | 1459 | (cmd1[2]<<8)+cmd1[3], (Demod.output[8]<<8)+Demod.output[9]); |
489ef36c | 1460 | // Do not return;, let's go on... (we should retry, maybe ?) |
1461 | } | |
1462 | Dbprintf("Tag UID (64 bits): %08x %08x", | |
705bfa10 | 1463 | (Demod.output[7]<<24) + (Demod.output[6]<<16) + (Demod.output[5]<<8) + Demod.output[4], |
1464 | (Demod.output[3]<<24) + (Demod.output[2]<<16) + (Demod.output[1]<<8) + Demod.output[0]); | |
489ef36c | 1465 | |
1466 | // Now loop to read all 16 blocks, address from 0 to last block | |
6fc68747 | 1467 | Dbprintf("Tag memory dump, block 0 to %d", numofblocks); |
489ef36c | 1468 | cmd1[0] = 0x08; |
1469 | i = 0x00; | |
6fc68747 | 1470 | ++numofblocks; |
1471 | ||
489ef36c | 1472 | for (;;) { |
6fc68747 | 1473 | if (i == numofblocks) { |
489ef36c | 1474 | DbpString("System area block (0xff):"); |
1475 | i = 0xff; | |
1476 | } | |
1477 | cmd1[1] = i; | |
1478 | ComputeCrc14443(CRC_14443_B, cmd1, 2, &cmd1[2], &cmd1[3]); | |
11c2df83 | 1479 | CodeAndTransmit14443bAsReader(cmd1, sizeof(cmd1)); //no |
dccddaef | 1480 | GetTagSamplesFor14443bDemod(); //no |
6fc68747 | 1481 | |
489ef36c | 1482 | if (Demod.len != 6) { // Check if we got an answer from the tag |
6fc68747 | 1483 | DbpString("Expected 6 bytes from tag, got less..."); |
1484 | return; | |
489ef36c | 1485 | } |
1486 | // The check the CRC of the answer (use cmd1 as temporary variable): | |
1487 | ComputeCrc14443(CRC_14443_B, Demod.output, 4, &cmd1[2], &cmd1[3]); | |
1488 | if(cmd1[2] != Demod.output[4] || cmd1[3] != Demod.output[5]) { | |
132a0217 | 1489 | Dbprintf("CRC Error reading block! Expected: %04x got: %04x", |
705bfa10 | 1490 | (cmd1[2]<<8)+cmd1[3], (Demod.output[4]<<8)+Demod.output[5]); |
489ef36c | 1491 | // Do not return;, let's go on... (we should retry, maybe ?) |
1492 | } | |
1493 | // Now print out the memory location: | |
22e24700 | 1494 | Dbprintf("Address=%02x, Contents=%08x, CRC=%04x", i, |
705bfa10 | 1495 | (Demod.output[3]<<24) + (Demod.output[2]<<16) + (Demod.output[1]<<8) + Demod.output[0], |
17ad0e09 | 1496 | (Demod.output[4]<<8)+Demod.output[5]); |
6fc68747 | 1497 | |
1498 | if (i == 0xff) break; | |
1499 | ++i; | |
489ef36c | 1500 | } |
5ee53a0e | 1501 | |
1502 | set_tracing(FALSE); | |
489ef36c | 1503 | } |
1504 | ||
11c2df83 | 1505 | |
1506 | static void iso1444b_setup_snoop(void){ | |
1507 | if (MF_DBGLEVEL > 3) Dbprintf("iso1443b_setup_snoop Enter"); | |
1508 | LEDsoff(); | |
1509 | FpgaDownloadAndGo(FPGA_BITSTREAM_HF); | |
1510 | BigBuf_free(); | |
1511 | BigBuf_Clear_ext(false); | |
1512 | clear_trace();//setup snoop | |
1513 | set_tracing(TRUE); | |
1514 | ||
1515 | // Initialize Demod and Uart structs | |
1516 | DemodInit(BigBuf_malloc(MAX_FRAME_SIZE)); | |
1517 | UartInit(BigBuf_malloc(MAX_FRAME_SIZE)); | |
1518 | ||
1519 | if (MF_DBGLEVEL > 1) { | |
1520 | // Print debug information about the buffer sizes | |
1521 | Dbprintf("Snooping buffers initialized:"); | |
1522 | Dbprintf(" Trace: %i bytes", BigBuf_max_traceLen()); | |
1523 | Dbprintf(" Reader -> tag: %i bytes", MAX_FRAME_SIZE); | |
1524 | Dbprintf(" tag -> Reader: %i bytes", MAX_FRAME_SIZE); | |
1525 | Dbprintf(" DMA: %i bytes", ISO14443B_DMA_BUFFER_SIZE); | |
1526 | } | |
1527 | ||
1528 | // connect Demodulated Signal to ADC: | |
1529 | SetAdcMuxFor(GPIO_MUXSEL_HIPKD); | |
1530 | ||
1531 | // Setup for the DMA. | |
1532 | FpgaSetupSsc(); | |
1533 | ||
1534 | // Set FPGA in the appropriate mode | |
1535 | FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR | FPGA_HF_READER_RX_XCORR_848_KHZ | FPGA_HF_READER_RX_XCORR_SNOOP); | |
1536 | SpinDelay(20); | |
1537 | ||
1538 | // Start the SSP timer | |
1539 | StartCountSspClk(); | |
1540 | if (MF_DBGLEVEL > 3) Dbprintf("iso1443b_setup_snoop Exit"); | |
1541 | } | |
1542 | ||
489ef36c | 1543 | //============================================================================= |
1544 | // Finally, the `sniffer' combines elements from both the reader and | |
1545 | // simulated tag, to show both sides of the conversation. | |
1546 | //============================================================================= | |
1547 | ||
1548 | //----------------------------------------------------------------------------- | |
1549 | // Record the sequence of commands sent by the reader to the tag, with | |
1550 | // triggering so that we start recording at the point that the tag is moved | |
1551 | // near the reader. | |
1552 | //----------------------------------------------------------------------------- | |
1553 | /* | |
1554 | * Memory usage for this function, (within BigBuf) | |
47286d89 | 1555 | * Last Received command (reader->tag) - MAX_FRAME_SIZE |
1556 | * Last Received command (tag->reader) - MAX_FRAME_SIZE | |
705bfa10 | 1557 | * DMA Buffer - ISO14443B_DMA_BUFFER_SIZE |
47286d89 | 1558 | * Demodulated samples received - all the rest |
489ef36c | 1559 | */ |
11c2df83 | 1560 | void RAMFUNC SnoopIso14443b(void) { |
1561 | ||
1562 | uint32_t time_0 = 0, time_start = 0, time_stop = 0; | |
d8b7a5f2 | 1563 | int ci = 0, cq = 0; |
1564 | int lastRxCounter = ISO14443B_DMA_BUFFER_SIZE; | |
1565 | ||
489ef36c | 1566 | // We won't start recording the frames that we acquire until we trigger; |
1567 | // a good trigger condition to get started is probably when we see a | |
1568 | // response from the tag. | |
d8b7a5f2 | 1569 | bool triggered = TRUE; // TODO: set and evaluate trigger condition |
f53020e7 | 1570 | bool TagIsActive = FALSE; |
1571 | bool ReaderIsActive = FALSE; | |
11c2df83 | 1572 | |
1573 | iso1444b_setup_snoop(); | |
1574 | ||
1575 | // The DMA buffer, used to stream samples from the FPGA | |
1576 | int8_t *dmaBuf = (int8_t*) BigBuf_malloc(ISO14443B_DMA_BUFFER_SIZE); | |
1577 | int8_t *upTo = dmaBuf; | |
1578 | ||
1579 | // Setup and start DMA. | |
1580 | if ( !FpgaSetupSscDma((uint8_t*) dmaBuf, ISO14443B_DMA_BUFFER_SIZE) ){ | |
1581 | if (MF_DBGLEVEL > 1) Dbprintf("FpgaSetupSscDma failed. Exiting"); | |
1582 | BigBuf_free(); | |
1583 | return; | |
1584 | } | |
1585 | ||
1586 | time_0 = GetCountSspClk(); | |
489ef36c | 1587 | |
1588 | // And now we loop, receiving samples. | |
1589 | for(;;) { | |
abb21530 | 1590 | |
11c2df83 | 1591 | WDT_HIT(); |
489ef36c | 1592 | |
1593 | ci = upTo[0]; | |
1594 | cq = upTo[1]; | |
d8b7a5f2 | 1595 | upTo += 2; |
489ef36c | 1596 | lastRxCounter -= 2; |
11c2df83 | 1597 | |
1598 | if (upTo >= dmaBuf + ISO14443B_DMA_BUFFER_SIZE) { | |
489ef36c | 1599 | upTo = dmaBuf; |
b8622518 | 1600 | lastRxCounter = ISO14443B_DMA_BUFFER_SIZE; |
489ef36c | 1601 | AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) dmaBuf; |
705bfa10 | 1602 | AT91C_BASE_PDC_SSC->PDC_RNCR = ISO14443B_DMA_BUFFER_SIZE; |
d8b7a5f2 | 1603 | |
1604 | if (!tracing) { | |
1605 | if (MF_DBGLEVEL >= 2) DbpString("Trace full"); | |
abb21530 | 1606 | break; |
1607 | } | |
11c2df83 | 1608 | |
d8b7a5f2 | 1609 | if (BUTTON_PRESS()) { |
1610 | if (MF_DBGLEVEL >= 2) DbpString("cancelled"); | |
abb21530 | 1611 | break; |
1612 | } | |
489ef36c | 1613 | } |
11c2df83 | 1614 | |
1615 | if (!TagIsActive) { | |
1616 | ||
1617 | LED_A_ON(); | |
1618 | ||
1619 | // no need to try decoding reader data if the tag is sending | |
1620 | if (Handle14443bReaderUartBit(ci & 0x01)) { | |
489ef36c | 1621 | |
b8622518 | 1622 | time_stop = GetCountSspClk() - time_0; |
11c2df83 | 1623 | |
1624 | if (triggered) | |
1625 | LogTrace(Uart.output, Uart.byteCnt, time_start, time_stop, NULL, TRUE); | |
6fc68747 | 1626 | |
810f5379 | 1627 | /* And ready to receive another command. */ |
1628 | UartReset(); | |
1629 | /* And also reset the demod code, which might have been */ | |
1630 | /* false-triggered by the commands from the reader. */ | |
1631 | DemodReset(); | |
11c2df83 | 1632 | } else { |
b8622518 | 1633 | time_start = GetCountSspClk() - time_0; |
489ef36c | 1634 | } |
6fc68747 | 1635 | |
11c2df83 | 1636 | if (Handle14443bReaderUartBit(cq & 0x01)) { |
1637 | ||
b8622518 | 1638 | time_stop = GetCountSspClk() - time_0; |
11c2df83 | 1639 | |
1640 | if (triggered) | |
1641 | LogTrace(Uart.output, Uart.byteCnt, time_start, time_stop, NULL, TRUE); | |
6fc68747 | 1642 | |
1cb9b2a3 RW |
1643 | /* And ready to receive another command. */ |
1644 | UartReset(); | |
1645 | /* And also reset the demod code, which might have been */ | |
1646 | /* false-triggered by the commands from the reader. */ | |
1647 | DemodReset(); | |
11c2df83 | 1648 | } else { |
b8622518 | 1649 | time_start = GetCountSspClk() - time_0; |
6fc68747 | 1650 | } |
36f84d47 | 1651 | ReaderIsActive = (Uart.state > STATE_GOT_FALLING_EDGE_OF_SOF); |
11c2df83 | 1652 | LED_A_OFF(); |
47286d89 | 1653 | } |
11c2df83 | 1654 | |
d8b7a5f2 | 1655 | if (!ReaderIsActive) { |
11c2df83 | 1656 | // no need to try decoding tag data if the reader is sending - and we cannot afford the time |
d8af608f | 1657 | // is this | 0x01 the error? & 0xfe in https://github.com/Proxmark/proxmark3/issues/103 |
d8b7a5f2 | 1658 | // LSB is a fpga signal bit. |
1659 | if (Handle14443bTagSamplesDemod(ci >> 1, cq >> 1)) { | |
11c2df83 | 1660 | |
b8622518 | 1661 | time_stop = GetCountSspClk() - time_0; |
11c2df83 | 1662 | |
1663 | LogTrace(Demod.output, Demod.len, time_start, time_stop, NULL, FALSE); | |
489ef36c | 1664 | |
810f5379 | 1665 | triggered = TRUE; |
1666 | ||
1667 | // And ready to receive another response. | |
1668 | DemodReset(); | |
11c2df83 | 1669 | } else { |
b8622518 | 1670 | time_start = GetCountSspClk() - time_0; |
810f5379 | 1671 | } |
22e24700 | 1672 | TagIsActive = (Demod.state > DEMOD_GOT_FALLING_EDGE_OF_SOF); |
47286d89 | 1673 | } |
489ef36c | 1674 | } |
abb21530 | 1675 | |
11c2df83 | 1676 | switch_off(); // Snoop |
810f5379 | 1677 | |
489ef36c | 1678 | DbpString("Snoop statistics:"); |
11c2df83 | 1679 | Dbprintf(" Uart State: %x ByteCount: %i ByteCountMax: %i", Uart.state, Uart.byteCnt, Uart.byteCntMax); |
489ef36c | 1680 | Dbprintf(" Trace length: %i", BigBuf_get_traceLen()); |
11c2df83 | 1681 | |
1682 | // free mem refs. | |
d8b7a5f2 | 1683 | if ( upTo ) upTo = NULL; |
c3e8413c | 1684 | |
11c2df83 | 1685 | // Uart.byteCntMax should be set with ATQB value.. |
489ef36c | 1686 | } |
1687 | ||
6fc68747 | 1688 | void iso14b_set_trigger(bool enable) { |
1689 | trigger = enable; | |
1690 | } | |
489ef36c | 1691 | |
1692 | /* | |
1693 | * Send raw command to tag ISO14443B | |
1694 | * @Input | |
6fc68747 | 1695 | * param flags enum ISO14B_COMMAND. (mifare.h) |
1696 | * len len of buffer data | |
1697 | * data buffer with bytes to send | |
489ef36c | 1698 | * |
1699 | * @Output | |
1700 | * none | |
1701 | * | |
1702 | */ | |
6fc68747 | 1703 | void SendRawCommand14443B_Ex(UsbCommand *c) |
489ef36c | 1704 | { |
6fc68747 | 1705 | iso14b_command_t param = c->arg[0]; |
1706 | size_t len = c->arg[1] & 0xffff; | |
1707 | uint8_t *cmd = c->d.asBytes; | |
1708 | uint8_t status = 0; | |
1709 | uint32_t sendlen = sizeof(iso14b_card_select_t); | |
1710 | uint8_t buf[USB_CMD_DATA_SIZE] = {0x00}; | |
1711 | ||
11c2df83 | 1712 | if (MF_DBGLEVEL > 3) Dbprintf("14b raw: param, %04x", param ); |
b10a759f | 1713 | |
6fc68747 | 1714 | // turn on trigger (LED_A) |
11c2df83 | 1715 | if ((param & ISO14B_REQUEST_TRIGGER) == ISO14B_REQUEST_TRIGGER) |
6fc68747 | 1716 | iso14b_set_trigger(TRUE); |
1717 | ||
11c2df83 | 1718 | if ((param & ISO14B_CONNECT) == ISO14B_CONNECT) { |
6fc68747 | 1719 | // Make sure that we start from off, since the tags are stateful; |
1720 | // confusing things will happen if we don't reset them between reads. | |
11c2df83 | 1721 | //switch_off(); // before connect in raw |
6fc68747 | 1722 | iso14443b_setup(); |
99cf19d9 | 1723 | } |
6fc68747 | 1724 | |
1725 | set_tracing(TRUE); | |
489ef36c | 1726 | |
11c2df83 | 1727 | if ((param & ISO14B_SELECT_STD) == ISO14B_SELECT_STD) { |
6fc68747 | 1728 | iso14b_card_select_t *card = (iso14b_card_select_t*)buf; |
1729 | status = iso14443b_select_card(card); | |
1730 | cmd_send(CMD_ACK, status, sendlen, 0, buf, sendlen); | |
1731 | // 0: OK 2: attrib fail, 3:crc fail, | |
1732 | if ( status > 0 ) return; | |
1733 | } | |
1734 | ||
11c2df83 | 1735 | if ((param & ISO14B_SELECT_SR) == ISO14B_SELECT_SR) { |
6fc68747 | 1736 | iso14b_card_select_t *card = (iso14b_card_select_t*)buf; |
1737 | status = iso14443b_select_srx_card(card); | |
1738 | cmd_send(CMD_ACK, status, sendlen, 0, buf, sendlen); | |
1739 | // 0: OK 2: attrib fail, 3:crc fail, | |
1740 | if ( status > 0 ) return; | |
1741 | } | |
1742 | ||
11c2df83 | 1743 | if ((param & ISO14B_APDU) == ISO14B_APDU) { |
6fc68747 | 1744 | status = iso14443b_apdu(cmd, len, buf); |
1745 | cmd_send(CMD_ACK, status, status, 0, buf, status); | |
489ef36c | 1746 | } |
abb21530 | 1747 | |
11c2df83 | 1748 | if ((param & ISO14B_RAW) == ISO14B_RAW) { |
1749 | if((param & ISO14B_APPEND_CRC) == ISO14B_APPEND_CRC) { | |
6fc68747 | 1750 | AppendCrc14443b(cmd, len); |
1751 | len += 2; | |
1752 | } | |
1753 | ||
11c2df83 | 1754 | CodeAndTransmit14443bAsReader(cmd, len); // raw |
dccddaef | 1755 | GetTagSamplesFor14443bDemod(); // raw |
6fc68747 | 1756 | |
1757 | sendlen = MIN(Demod.len, USB_CMD_DATA_SIZE); | |
1758 | status = (Demod.len > 0) ? 0 : 1; | |
1759 | cmd_send(CMD_ACK, status, sendlen, 0, Demod.output, sendlen); | |
1760 | } | |
1761 | ||
1762 | // turn off trigger (LED_A) | |
11c2df83 | 1763 | if ((param & ISO14B_REQUEST_TRIGGER) == ISO14B_REQUEST_TRIGGER) |
1764 | iso14b_set_trigger(FALSE); | |
6fc68747 | 1765 | |
1766 | // turn off antenna et al | |
1767 | // we don't send a HALT command. | |
11c2df83 | 1768 | if ((param & ISO14B_DISCONNECT) == ISO14B_DISCONNECT) { |
6fc68747 | 1769 | if (MF_DBGLEVEL > 3) Dbprintf("disconnect"); |
11c2df83 | 1770 | switch_off(); // disconnect raw |
1771 | } else { | |
1772 | //FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX | FPGA_HF_READER_TX_SHALLOW_MOD); | |
489ef36c | 1773 | } |
11c2df83 | 1774 | |
1cb9b2a3 | 1775 | } |