1 //-----------------------------------------------------------------------------
2 // Jonathan Westhues, split Nov 2006
3 // Modified by Greg Jones, Jan 2009
4 // Modified by Adrian Dabrowski "atrox", Mar-Sept 2010,Oct 2011
5 // Modified by piwi, Oct 2018
7 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
8 // at your option, any later version. See the LICENSE.txt file for the text of
10 //-----------------------------------------------------------------------------
11 // Routines to support ISO 15693. This includes both the reader software and
12 // the `fake tag' modes.
13 //-----------------------------------------------------------------------------
15 // The ISO 15693 describes two transmission modes from reader to tag, and four
16 // transmission modes from tag to reader. As of Oct 2018 this code supports
17 // both reader modes and the high speed variant with one subcarrier from card to reader.
18 // As long as the card fully support ISO 15693 this is no problem, since the
19 // reader chooses both data rates, but some non-standard tags do not.
20 // For card simulation, the code supports both high and low speed modes with one subcarrier.
22 // VCD (reader) -> VICC (tag)
24 // data rate: 1,66 kbit/s (fc/8192)
25 // used for long range
27 // data rate: 26,48 kbit/s (fc/512)
28 // used for short range, high speed
30 // VICC (tag) -> VCD (reader)
32 // ASK / one subcarrier (423,75 khz)
33 // FSK / two subcarriers (423,75 khz && 484,28 khz)
34 // Data Rates / Modes:
35 // low ASK: 6,62 kbit/s
36 // low FSK: 6.67 kbit/s
37 // high ASK: 26,48 kbit/s
38 // high FSK: 26,69 kbit/s
39 //-----------------------------------------------------------------------------
43 // *) UID is always used "transmission order" (LSB), which is reverse to display order
45 // TODO / BUGS / ISSUES:
46 // *) signal decoding is unable to detect collisions.
47 // *) add anti-collision support for inventory-commands
48 // *) read security status of a block
49 // *) sniffing and simulation do not support two subcarrier modes.
50 // *) remove or refactor code under "deprecated"
51 // *) document all the functions
55 #include "proxmark3.h"
59 #include "iso15693tools.h"
60 #include "protocols.h"
63 #include "fpgaloader.h"
65 #define arraylen(x) (sizeof(x)/sizeof((x)[0]))
67 // Delays in SSP_CLK ticks.
68 // SSP_CLK runs at 13,56MHz / 32 = 423.75kHz when simulating a tag
69 #define DELAY_READER_TO_ARM 8
70 #define DELAY_ARM_TO_READER 0
71 //SSP_CLK runs at 13.56MHz / 4 = 3,39MHz when acting as reader. All values should be multiples of 16
72 #define DELAY_ARM_TO_TAG 16
73 #define DELAY_TAG_TO_ARM 32
74 //SSP_CLK runs at 13.56MHz / 4 = 3,39MHz when snooping. All values should be multiples of 16
75 #define DELAY_TAG_TO_ARM_SNOOP 32
76 #define DELAY_READER_TO_ARM_SNOOP 32
78 // times in samples @ 212kHz when acting as reader
79 //#define ISO15693_READER_TIMEOUT 80 // 80/212kHz = 378us, nominal t1_max=313,9us
80 #define ISO15693_READER_TIMEOUT 330 // 330/212kHz = 1558us, should be even enough for iClass tags responding to ACTALL
81 #define ISO15693_READER_TIMEOUT_WRITE 4700 // 4700/212kHz = 22ms, nominal 20ms
87 ///////////////////////////////////////////////////////////////////////
88 // ISO 15693 Part 2 - Air Interface
89 // This section basically contains transmission and receiving of bits
90 ///////////////////////////////////////////////////////////////////////
93 #define ISO15693_DMA_BUFFER_SIZE 256 // must be a power of 2
94 #define ISO15693_MAX_RESPONSE_LENGTH 36 // allows read single block with the maximum block size of 256bits. Read multiple blocks not supported yet
95 #define ISO15693_MAX_COMMAND_LENGTH 45 // allows write single block with the maximum block size of 256bits. Write multiple blocks not supported yet
98 // specific LogTrace function for ISO15693: the duration needs to be scaled because otherwise it won't fit into a uint16_t
99 bool LogTrace_ISO15693(const uint8_t *btBytes
, uint16_t iLen
, uint32_t timestamp_start
, uint32_t timestamp_end
, uint8_t *parity
, bool readerToTag
) {
100 uint32_t duration
= timestamp_end
- timestamp_start
;
102 timestamp_end
= timestamp_start
+ duration
;
103 return LogTrace(btBytes
, iLen
, timestamp_start
, timestamp_end
, parity
, readerToTag
);
107 // ---------------------------
109 // ---------------------------
111 // prepare data using "1 out of 4" code for later transmission
112 // resulting data rate is 26.48 kbit/s (fc/512)
114 // n ... length of data
115 void CodeIso15693AsReader(uint8_t *cmd
, int n
) {
120 ToSend
[++ToSendMax
] = 0x84; //10000100
123 for (int i
= 0; i
< n
; i
++) {
124 for (int j
= 0; j
< 8; j
+= 2) {
125 int these
= (cmd
[i
] >> j
) & 0x03;
128 ToSend
[++ToSendMax
] = 0x40; //01000000
131 ToSend
[++ToSendMax
] = 0x10; //00010000
134 ToSend
[++ToSendMax
] = 0x04; //00000100
137 ToSend
[++ToSendMax
] = 0x01; //00000001
144 ToSend
[++ToSendMax
] = 0x20; //0010 + 0000 padding
151 static void CodeIso15693AsReaderEOF() {
153 ToSend
[++ToSendMax
] = 0x20;
158 // encode data using "1 out of 256" scheme
159 // data rate is 1,66 kbit/s (fc/8192)
160 // is designed for more robust communication over longer distances
161 static void CodeIso15693AsReader256(uint8_t *cmd
, int n
)
166 ToSend
[++ToSendMax
] = 0x81; //10000001
169 for(int i
= 0; i
< n
; i
++) {
170 for (int j
= 0; j
<= 255; j
++) {
182 ToSend
[++ToSendMax
] = 0x20; //0010 + 0000 padding
188 // static uint8_t encode4Bits(const uint8_t b) {
189 // uint8_t c = b & 0xF;
190 // // OTA, the least significant bits first
191 // // The columns are
192 // // 1 - Bit value to send
193 // // 2 - Reversed (big-endian)
194 // // 3 - Manchester Encoded
199 // case 15: return 0x55; // 1111 -> 1111 -> 01010101 -> 0x55
200 // case 14: return 0x95; // 1110 -> 0111 -> 10010101 -> 0x95
201 // case 13: return 0x65; // 1101 -> 1011 -> 01100101 -> 0x65
202 // case 12: return 0xa5; // 1100 -> 0011 -> 10100101 -> 0xa5
203 // case 11: return 0x59; // 1011 -> 1101 -> 01011001 -> 0x59
204 // case 10: return 0x99; // 1010 -> 0101 -> 10011001 -> 0x99
205 // case 9: return 0x69; // 1001 -> 1001 -> 01101001 -> 0x69
206 // case 8: return 0xa9; // 1000 -> 0001 -> 10101001 -> 0xa9
207 // case 7: return 0x56; // 0111 -> 1110 -> 01010110 -> 0x56
208 // case 6: return 0x96; // 0110 -> 0110 -> 10010110 -> 0x96
209 // case 5: return 0x66; // 0101 -> 1010 -> 01100110 -> 0x66
210 // case 4: return 0xa6; // 0100 -> 0010 -> 10100110 -> 0xa6
211 // case 3: return 0x5a; // 0011 -> 1100 -> 01011010 -> 0x5a
212 // case 2: return 0x9a; // 0010 -> 0100 -> 10011010 -> 0x9a
213 // case 1: return 0x6a; // 0001 -> 1000 -> 01101010 -> 0x6a
214 // default: return 0xaa; // 0000 -> 0000 -> 10101010 -> 0xaa
219 static const uint8_t encode_4bits
[16] = { 0xaa, 0x6a, 0x9a, 0x5a, 0xa6, 0x66, 0x96, 0x56, 0xa9, 0x69, 0x99, 0x59, 0xa5, 0x65, 0x95, 0x55 };
221 void CodeIso15693AsTag(uint8_t *cmd
, size_t len
) {
223 * SOF comprises 3 parts;
224 * * An unmodulated time of 56.64 us
225 * * 24 pulses of 423.75 kHz (fc/32)
226 * * A logic 1, which starts with an unmodulated time of 18.88us
227 * followed by 8 pulses of 423.75kHz (fc/32)
229 * EOF comprises 3 parts:
230 * - A logic 0 (which starts with 8 pulses of fc/32 followed by an unmodulated
232 * - 24 pulses of fc/32
233 * - An unmodulated time of 56.64 us
235 * A logic 0 starts with 8 pulses of fc/32
236 * followed by an unmodulated time of 256/fc (~18,88us).
238 * A logic 0 starts with unmodulated time of 256/fc (~18,88us) followed by
239 * 8 pulses of fc/32 (also 18.88us)
241 * A bit here becomes 8 pulses of fc/32. Therefore:
242 * The SOF can be written as 00011101 = 0x1D
243 * The EOF can be written as 10111000 = 0xb8
252 ToSend
[++ToSendMax
] = 0x1D; // 00011101
255 for (int i
= 0; i
< len
; i
++) {
256 ToSend
[++ToSendMax
] = encode_4bits
[cmd
[i
] & 0xF];
257 ToSend
[++ToSendMax
] = encode_4bits
[cmd
[i
] >> 4];
261 ToSend
[++ToSendMax
] = 0xB8; // 10111000
267 // Transmit the command (to the tag) that was placed in cmd[].
268 void TransmitTo15693Tag(const uint8_t *cmd
, int len
, uint32_t *start_time
) {
270 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER
| FPGA_HF_READER_MODE_SEND_FULL_MOD
);
272 if (*start_time
< DELAY_ARM_TO_TAG
) {
273 *start_time
= DELAY_ARM_TO_TAG
;
276 *start_time
= (*start_time
- DELAY_ARM_TO_TAG
) & 0xfffffff0;
278 if (GetCountSspClk() > *start_time
) { // we may miss the intended time
279 *start_time
= (GetCountSspClk() + 16) & 0xfffffff0; // next possible time
282 while (GetCountSspClk() < *start_time
)
286 for (int c
= 0; c
< len
; c
++) {
287 uint8_t data
= cmd
[c
];
288 for (int i
= 0; i
< 8; i
++) {
289 uint16_t send_word
= (data
& 0x80) ? 0xffff : 0x0000;
290 while (!(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
))) ;
291 AT91C_BASE_SSC
->SSC_THR
= send_word
;
292 while (!(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
))) ;
293 AT91C_BASE_SSC
->SSC_THR
= send_word
;
301 *start_time
= *start_time
+ DELAY_ARM_TO_TAG
;
306 //-----------------------------------------------------------------------------
307 // Transmit the tag response (to the reader) that was placed in cmd[].
308 //-----------------------------------------------------------------------------
309 void TransmitTo15693Reader(const uint8_t *cmd
, size_t len
, uint32_t *start_time
, uint32_t slot_time
, bool slow
) {
310 // don't use the FPGA_HF_SIMULATOR_MODULATE_424K_8BIT minor mode. It would spoil GetCountSspClk()
311 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
| FPGA_HF_SIMULATOR_MODULATE_424K
);
313 uint32_t modulation_start_time
= *start_time
- DELAY_ARM_TO_READER
+ 3 * 8; // no need to transfer the unmodulated start of SOF
315 while (GetCountSspClk() > (modulation_start_time
& 0xfffffff8) + 3) { // we will miss the intended time
317 modulation_start_time
+= slot_time
; // use next available slot
319 modulation_start_time
= (modulation_start_time
& 0xfffffff8) + 8; // next possible time
323 while (GetCountSspClk() < (modulation_start_time
& 0xfffffff8))
326 uint8_t shift_delay
= modulation_start_time
& 0x00000007;
328 *start_time
= modulation_start_time
+ DELAY_ARM_TO_READER
- 3 * 8;
331 uint8_t bits_to_shift
= 0x00;
332 uint8_t bits_to_send
= 0x00;
333 for (size_t c
= 0; c
< len
; c
++) {
334 for (int i
= (c
==0?4:7); i
>= 0; i
--) {
335 uint8_t cmd_bits
= ((cmd
[c
] >> i
) & 0x01) ? 0xff : 0x00;
336 for (int j
= 0; j
< (slow
?4:1); ) {
337 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
) {
338 bits_to_send
= bits_to_shift
<< (8 - shift_delay
) | cmd_bits
>> shift_delay
;
339 AT91C_BASE_SSC
->SSC_THR
= bits_to_send
;
340 bits_to_shift
= cmd_bits
;
347 // send the remaining bits, padded with 0:
348 bits_to_send
= bits_to_shift
<< (8 - shift_delay
);
350 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
) {
351 AT91C_BASE_SSC
->SSC_THR
= bits_to_send
;
359 //=============================================================================
360 // An ISO 15693 decoder for tag responses (one subcarrier only).
361 // Uses cross correlation to identify each bit and EOF.
362 // This function is called 8 times per bit (every 2 subcarrier cycles).
363 // Subcarrier frequency fs is 424kHz, 1/fs = 2,36us,
364 // i.e. function is called every 4,72us
366 // LED C -> ON once we have received the SOF and are expecting the rest.
367 // LED C -> OFF once we have received EOF or are unsynced
369 // Returns: true if we received a EOF
370 // false if we are still waiting for some more
371 //=============================================================================
373 #define NOISE_THRESHOLD 160 // don't try to correlate noise
374 #define MAX_PREVIOUS_AMPLITUDE (-1 - NOISE_THRESHOLD)
376 typedef struct DecodeTag
{
379 STATE_TAG_SOF_RISING_EDGE
,
381 STATE_TAG_SOF_HIGH_END
,
382 STATE_TAG_RECEIVING_DATA
,
401 uint16_t previous_amplitude
;
405 static int inline __attribute__((always_inline
)) Handle15693SamplesFromTag(uint16_t amplitude
, DecodeTag_t
*DecodeTag
) {
406 switch (DecodeTag
->state
) {
407 case STATE_TAG_SOF_LOW
:
408 // waiting for a rising edge
409 if (amplitude
> NOISE_THRESHOLD
+ DecodeTag
->previous_amplitude
) {
410 if (DecodeTag
->posCount
> 10) {
411 DecodeTag
->threshold_sof
= amplitude
- DecodeTag
->previous_amplitude
; // to be divided by 2
412 DecodeTag
->threshold_half
= 0;
413 DecodeTag
->state
= STATE_TAG_SOF_RISING_EDGE
;
415 DecodeTag
->posCount
= 0;
418 DecodeTag
->posCount
++;
419 DecodeTag
->previous_amplitude
= amplitude
;
423 case STATE_TAG_SOF_RISING_EDGE
:
424 if (amplitude
> DecodeTag
->threshold_sof
+ DecodeTag
->previous_amplitude
) { // edge still rising
425 if (amplitude
> DecodeTag
->threshold_sof
+ DecodeTag
->threshold_sof
) { // steeper edge, take this as time reference
426 DecodeTag
->posCount
= 1;
428 DecodeTag
->posCount
= 2;
430 DecodeTag
->threshold_sof
= (amplitude
- DecodeTag
->previous_amplitude
) / 2;
432 DecodeTag
->posCount
= 2;
433 DecodeTag
->threshold_sof
= DecodeTag
->threshold_sof
/2;
435 // DecodeTag->posCount = 2;
436 DecodeTag
->state
= STATE_TAG_SOF_HIGH
;
439 case STATE_TAG_SOF_HIGH
:
440 // waiting for 10 times high. Take average over the last 8
441 if (amplitude
> DecodeTag
->threshold_sof
) {
442 DecodeTag
->posCount
++;
443 if (DecodeTag
->posCount
> 2) {
444 DecodeTag
->threshold_half
+= amplitude
; // keep track of average high value
446 if (DecodeTag
->posCount
== 10) {
447 DecodeTag
->threshold_half
>>= 2; // (4 times 1/2 average)
448 DecodeTag
->state
= STATE_TAG_SOF_HIGH_END
;
450 } else { // high phase was too short
451 DecodeTag
->posCount
= 1;
452 DecodeTag
->previous_amplitude
= amplitude
;
453 DecodeTag
->state
= STATE_TAG_SOF_LOW
;
457 case STATE_TAG_SOF_HIGH_END
:
458 // check for falling edge
459 if (DecodeTag
->posCount
== 13 && amplitude
< DecodeTag
->threshold_sof
) {
460 DecodeTag
->lastBit
= SOF_PART1
; // detected 1st part of SOF (12 samples low and 12 samples high)
461 DecodeTag
->shiftReg
= 0;
462 DecodeTag
->bitCount
= 0;
464 DecodeTag
->sum1
= amplitude
;
466 DecodeTag
->posCount
= 2;
467 DecodeTag
->state
= STATE_TAG_RECEIVING_DATA
;
468 // FpgaDisableTracing(); // DEBUGGING
469 // Dbprintf("amplitude = %d, threshold_sof = %d, threshold_half/4 = %d, previous_amplitude = %d",
471 // DecodeTag->threshold_sof,
472 // DecodeTag->threshold_half/4,
473 // DecodeTag->previous_amplitude); // DEBUGGING
476 DecodeTag
->posCount
++;
477 if (DecodeTag
->posCount
> 13) { // high phase too long
478 DecodeTag
->posCount
= 0;
479 DecodeTag
->previous_amplitude
= amplitude
;
480 DecodeTag
->state
= STATE_TAG_SOF_LOW
;
486 case STATE_TAG_RECEIVING_DATA
:
487 // FpgaDisableTracing(); // DEBUGGING
488 // Dbprintf("amplitude = %d, threshold_sof = %d, threshold_half/4 = %d, previous_amplitude = %d",
490 // DecodeTag->threshold_sof,
491 // DecodeTag->threshold_half/4,
492 // DecodeTag->previous_amplitude); // DEBUGGING
493 if (DecodeTag
->posCount
== 1) {
497 if (DecodeTag
->posCount
<= 4) {
498 DecodeTag
->sum1
+= amplitude
;
500 DecodeTag
->sum2
+= amplitude
;
502 if (DecodeTag
->posCount
== 8) {
503 if (DecodeTag
->sum1
> DecodeTag
->threshold_half
&& DecodeTag
->sum2
> DecodeTag
->threshold_half
) { // modulation in both halves
504 if (DecodeTag
->lastBit
== LOGIC0
) { // this was already part of EOF
505 DecodeTag
->state
= STATE_TAG_EOF
;
507 DecodeTag
->posCount
= 0;
508 DecodeTag
->previous_amplitude
= amplitude
;
509 DecodeTag
->state
= STATE_TAG_SOF_LOW
;
512 } else if (DecodeTag
->sum1
< DecodeTag
->threshold_half
&& DecodeTag
->sum2
> DecodeTag
->threshold_half
) { // modulation in second half
514 if (DecodeTag
->lastBit
== SOF_PART1
) { // still part of SOF
515 DecodeTag
->lastBit
= SOF_PART2
; // SOF completed
517 DecodeTag
->lastBit
= LOGIC1
;
518 DecodeTag
->shiftReg
>>= 1;
519 DecodeTag
->shiftReg
|= 0x80;
520 DecodeTag
->bitCount
++;
521 if (DecodeTag
->bitCount
== 8) {
522 DecodeTag
->output
[DecodeTag
->len
] = DecodeTag
->shiftReg
;
524 // if (DecodeTag->shiftReg == 0x12 && DecodeTag->len == 1) FpgaDisableTracing(); // DEBUGGING
525 if (DecodeTag
->len
> DecodeTag
->max_len
) {
526 // buffer overflow, give up
530 DecodeTag
->bitCount
= 0;
531 DecodeTag
->shiftReg
= 0;
534 } else if (DecodeTag
->sum1
> DecodeTag
->threshold_half
&& DecodeTag
->sum2
< DecodeTag
->threshold_half
) { // modulation in first half
536 if (DecodeTag
->lastBit
== SOF_PART1
) { // incomplete SOF
537 DecodeTag
->posCount
= 0;
538 DecodeTag
->previous_amplitude
= amplitude
;
539 DecodeTag
->state
= STATE_TAG_SOF_LOW
;
542 DecodeTag
->lastBit
= LOGIC0
;
543 DecodeTag
->shiftReg
>>= 1;
544 DecodeTag
->bitCount
++;
545 if (DecodeTag
->bitCount
== 8) {
546 DecodeTag
->output
[DecodeTag
->len
] = DecodeTag
->shiftReg
;
548 // if (DecodeTag->shiftReg == 0x12 && DecodeTag->len == 1) FpgaDisableTracing(); // DEBUGGING
549 if (DecodeTag
->len
> DecodeTag
->max_len
) {
550 // buffer overflow, give up
551 DecodeTag
->posCount
= 0;
552 DecodeTag
->previous_amplitude
= amplitude
;
553 DecodeTag
->state
= STATE_TAG_SOF_LOW
;
556 DecodeTag
->bitCount
= 0;
557 DecodeTag
->shiftReg
= 0;
560 } else { // no modulation
561 if (DecodeTag
->lastBit
== SOF_PART2
) { // only SOF (this is OK for iClass)
565 DecodeTag
->posCount
= 0;
566 DecodeTag
->state
= STATE_TAG_SOF_LOW
;
570 DecodeTag
->posCount
= 0;
572 DecodeTag
->posCount
++;
576 if (DecodeTag
->posCount
== 1) {
580 if (DecodeTag
->posCount
<= 4) {
581 DecodeTag
->sum1
+= amplitude
;
583 DecodeTag
->sum2
+= amplitude
;
585 if (DecodeTag
->posCount
== 8) {
586 if (DecodeTag
->sum1
> DecodeTag
->threshold_half
&& DecodeTag
->sum2
< DecodeTag
->threshold_half
) { // modulation in first half
587 DecodeTag
->posCount
= 0;
588 DecodeTag
->state
= STATE_TAG_EOF_TAIL
;
590 DecodeTag
->posCount
= 0;
591 DecodeTag
->previous_amplitude
= amplitude
;
592 DecodeTag
->state
= STATE_TAG_SOF_LOW
;
596 DecodeTag
->posCount
++;
599 case STATE_TAG_EOF_TAIL
:
600 if (DecodeTag
->posCount
== 1) {
604 if (DecodeTag
->posCount
<= 4) {
605 DecodeTag
->sum1
+= amplitude
;
607 DecodeTag
->sum2
+= amplitude
;
609 if (DecodeTag
->posCount
== 8) {
610 if (DecodeTag
->sum1
< DecodeTag
->threshold_half
&& DecodeTag
->sum2
< DecodeTag
->threshold_half
) { // no modulation in both halves
614 DecodeTag
->posCount
= 0;
615 DecodeTag
->previous_amplitude
= amplitude
;
616 DecodeTag
->state
= STATE_TAG_SOF_LOW
;
620 DecodeTag
->posCount
++;
628 static void DecodeTagInit(DecodeTag_t
*DecodeTag
, uint8_t *data
, uint16_t max_len
) {
629 DecodeTag
->previous_amplitude
= MAX_PREVIOUS_AMPLITUDE
;
630 DecodeTag
->posCount
= 0;
631 DecodeTag
->state
= STATE_TAG_SOF_LOW
;
632 DecodeTag
->output
= data
;
633 DecodeTag
->max_len
= max_len
;
637 static void DecodeTagReset(DecodeTag_t
*DecodeTag
) {
638 DecodeTag
->posCount
= 0;
639 DecodeTag
->state
= STATE_TAG_SOF_LOW
;
640 DecodeTag
->previous_amplitude
= MAX_PREVIOUS_AMPLITUDE
;
645 * Receive and decode the tag response, also log to tracebuffer
647 int GetIso15693AnswerFromTag(uint8_t* response
, uint16_t max_len
, uint16_t timeout
, uint32_t *eof_time
) {
652 uint16_t dmaBuf
[ISO15693_DMA_BUFFER_SIZE
];
654 // the Decoder data structure
655 DecodeTag_t DecodeTag
= { 0 };
656 DecodeTagInit(&DecodeTag
, response
, max_len
);
658 // wait for last transfer to complete
659 while (!(AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXEMPTY
));
661 // And put the FPGA in the appropriate mode
662 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER
| FPGA_HF_READER_SUBCARRIER_424_KHZ
| FPGA_HF_READER_MODE_RECEIVE_AMPLITUDE
);
664 // Setup and start DMA.
665 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER
);
666 FpgaSetupSscDma((uint8_t*) dmaBuf
, ISO15693_DMA_BUFFER_SIZE
);
667 uint32_t dma_start_time
= 0;
668 uint16_t *upTo
= dmaBuf
;
671 uint16_t behindBy
= ((uint16_t*)AT91C_BASE_PDC_SSC
->PDC_RPR
- upTo
) & (ISO15693_DMA_BUFFER_SIZE
-1);
673 if (behindBy
== 0) continue;
677 // DMA has transferred the very first data
678 dma_start_time
= GetCountSspClk() & 0xfffffff0;
681 uint16_t tagdata
= *upTo
++;
683 if(upTo
>= dmaBuf
+ ISO15693_DMA_BUFFER_SIZE
) { // we have read all of the DMA buffer content.
684 upTo
= dmaBuf
; // start reading the circular buffer from the beginning
685 if (behindBy
> (9*ISO15693_DMA_BUFFER_SIZE
/10)) {
686 Dbprintf("About to blow circular buffer - aborted! behindBy=%d", behindBy
);
691 if (AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_ENDRX
)) { // DMA Counter Register had reached 0, already rotated.
692 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (uint32_t) dmaBuf
; // refresh the DMA Next Buffer and
693 AT91C_BASE_PDC_SSC
->PDC_RNCR
= ISO15693_DMA_BUFFER_SIZE
; // DMA Next Counter registers
696 if (Handle15693SamplesFromTag(tagdata
, &DecodeTag
)) {
697 *eof_time
= dma_start_time
+ samples
*16 - DELAY_TAG_TO_ARM
; // end of EOF
698 if (DecodeTag
.lastBit
== SOF_PART2
) {
699 *eof_time
-= 8*16; // needed 8 additional samples to confirm single SOF (iCLASS)
701 if (DecodeTag
.len
> DecodeTag
.max_len
) {
702 ret
= -2; // buffer overflow
707 if (samples
> timeout
&& DecodeTag
.state
< STATE_TAG_RECEIVING_DATA
) {
716 if (DEBUG
) Dbprintf("samples = %d, ret = %d, Decoder: state = %d, lastBit = %d, len = %d, bitCount = %d, posCount = %d",
717 samples
, ret
, DecodeTag
.state
, DecodeTag
.lastBit
, DecodeTag
.len
, DecodeTag
.bitCount
, DecodeTag
.posCount
);
723 uint32_t sof_time
= *eof_time
724 - DecodeTag
.len
* 8 * 8 * 16 // time for byte transfers
725 - 32 * 16 // time for SOF transfer
726 - (DecodeTag
.lastBit
!= SOF_PART2
?32*16:0); // time for EOF transfer
728 if (DEBUG
) Dbprintf("timing: sof_time = %d, eof_time = %d", sof_time
, *eof_time
);
730 LogTrace_ISO15693(DecodeTag
.output
, DecodeTag
.len
, sof_time
*4, *eof_time
*4, NULL
, false);
732 return DecodeTag
.len
;
736 //=============================================================================
737 // An ISO15693 decoder for reader commands.
739 // This function is called 4 times per bit (every 2 subcarrier cycles).
740 // Subcarrier frequency fs is 848kHz, 1/fs = 1,18us, i.e. function is called every 2,36us
742 // LED B -> ON once we have received the SOF and are expecting the rest.
743 // LED B -> OFF once we have received EOF or are in error state or unsynced
745 // Returns: true if we received a EOF
746 // false if we are still waiting for some more
747 //=============================================================================
749 typedef struct DecodeReader
{
751 STATE_READER_UNSYNCD
,
752 STATE_READER_AWAIT_1ST_FALLING_EDGE_OF_SOF
,
753 STATE_READER_AWAIT_1ST_RISING_EDGE_OF_SOF
,
754 STATE_READER_AWAIT_2ND_FALLING_EDGE_OF_SOF
,
755 STATE_READER_AWAIT_2ND_RISING_EDGE_OF_SOF
,
756 STATE_READER_AWAIT_END_OF_SOF_1_OUT_OF_4
,
757 STATE_READER_RECEIVE_DATA_1_OUT_OF_4
,
758 STATE_READER_RECEIVE_DATA_1_OUT_OF_256
,
759 STATE_READER_RECEIVE_JAMMING
772 uint8_t jam_search_len
;
773 uint8_t *jam_search_string
;
777 static void DecodeReaderInit(DecodeReader_t
* DecodeReader
, uint8_t *data
, uint16_t max_len
, uint8_t jam_search_len
, uint8_t *jam_search_string
) {
778 DecodeReader
->output
= data
;
779 DecodeReader
->byteCountMax
= max_len
;
780 DecodeReader
->state
= STATE_READER_UNSYNCD
;
781 DecodeReader
->byteCount
= 0;
782 DecodeReader
->bitCount
= 0;
783 DecodeReader
->posCount
= 1;
784 DecodeReader
->shiftReg
= 0;
785 DecodeReader
->jam_search_len
= jam_search_len
;
786 DecodeReader
->jam_search_string
= jam_search_string
;
790 static void DecodeReaderReset(DecodeReader_t
* DecodeReader
) {
791 DecodeReader
->state
= STATE_READER_UNSYNCD
;
795 static int inline __attribute__((always_inline
)) Handle15693SampleFromReader(bool bit
, DecodeReader_t
*DecodeReader
) {
796 switch (DecodeReader
->state
) {
797 case STATE_READER_UNSYNCD
:
798 // wait for unmodulated carrier
800 DecodeReader
->state
= STATE_READER_AWAIT_1ST_FALLING_EDGE_OF_SOF
;
804 case STATE_READER_AWAIT_1ST_FALLING_EDGE_OF_SOF
:
806 // we went low, so this could be the beginning of a SOF
807 DecodeReader
->posCount
= 1;
808 DecodeReader
->state
= STATE_READER_AWAIT_1ST_RISING_EDGE_OF_SOF
;
812 case STATE_READER_AWAIT_1ST_RISING_EDGE_OF_SOF
:
813 DecodeReader
->posCount
++;
814 if (bit
) { // detected rising edge
815 if (DecodeReader
->posCount
< 4) { // rising edge too early (nominally expected at 5)
816 DecodeReader
->state
= STATE_READER_AWAIT_1ST_FALLING_EDGE_OF_SOF
;
818 DecodeReader
->state
= STATE_READER_AWAIT_2ND_FALLING_EDGE_OF_SOF
;
821 if (DecodeReader
->posCount
> 5) { // stayed low for too long
822 DecodeReaderReset(DecodeReader
);
824 // do nothing, keep waiting
829 case STATE_READER_AWAIT_2ND_FALLING_EDGE_OF_SOF
:
830 DecodeReader
->posCount
++;
831 if (!bit
) { // detected a falling edge
832 if (DecodeReader
->posCount
< 20) { // falling edge too early (nominally expected at 21 earliest)
833 DecodeReaderReset(DecodeReader
);
834 } else if (DecodeReader
->posCount
< 23) { // SOF for 1 out of 4 coding
835 DecodeReader
->Coding
= CODING_1_OUT_OF_4
;
836 DecodeReader
->state
= STATE_READER_AWAIT_2ND_RISING_EDGE_OF_SOF
;
837 } else if (DecodeReader
->posCount
< 28) { // falling edge too early (nominally expected at 29 latest)
838 DecodeReaderReset(DecodeReader
);
839 } else { // SOF for 1 out of 256 coding
840 DecodeReader
->Coding
= CODING_1_OUT_OF_256
;
841 DecodeReader
->state
= STATE_READER_AWAIT_2ND_RISING_EDGE_OF_SOF
;
844 if (DecodeReader
->posCount
> 29) { // stayed high for too long
845 DecodeReader
->state
= STATE_READER_AWAIT_1ST_FALLING_EDGE_OF_SOF
;
847 // do nothing, keep waiting
852 case STATE_READER_AWAIT_2ND_RISING_EDGE_OF_SOF
:
853 DecodeReader
->posCount
++;
854 if (bit
) { // detected rising edge
855 if (DecodeReader
->Coding
== CODING_1_OUT_OF_256
) {
856 if (DecodeReader
->posCount
< 32) { // rising edge too early (nominally expected at 33)
857 DecodeReader
->state
= STATE_READER_AWAIT_1ST_FALLING_EDGE_OF_SOF
;
859 DecodeReader
->posCount
= 1;
860 DecodeReader
->bitCount
= 0;
861 DecodeReader
->byteCount
= 0;
862 DecodeReader
->sum1
= 1;
863 DecodeReader
->state
= STATE_READER_RECEIVE_DATA_1_OUT_OF_256
;
866 } else { // CODING_1_OUT_OF_4
867 if (DecodeReader
->posCount
< 24) { // rising edge too early (nominally expected at 25)
868 DecodeReader
->state
= STATE_READER_AWAIT_1ST_FALLING_EDGE_OF_SOF
;
870 DecodeReader
->posCount
= 1;
871 DecodeReader
->state
= STATE_READER_AWAIT_END_OF_SOF_1_OUT_OF_4
;
875 if (DecodeReader
->Coding
== CODING_1_OUT_OF_256
) {
876 if (DecodeReader
->posCount
> 34) { // signal stayed low for too long
877 DecodeReaderReset(DecodeReader
);
879 // do nothing, keep waiting
881 } else { // CODING_1_OUT_OF_4
882 if (DecodeReader
->posCount
> 26) { // signal stayed low for too long
883 DecodeReaderReset(DecodeReader
);
885 // do nothing, keep waiting
891 case STATE_READER_AWAIT_END_OF_SOF_1_OUT_OF_4
:
892 DecodeReader
->posCount
++;
894 if (DecodeReader
->posCount
== 9) {
895 DecodeReader
->posCount
= 1;
896 DecodeReader
->bitCount
= 0;
897 DecodeReader
->byteCount
= 0;
898 DecodeReader
->sum1
= 1;
899 DecodeReader
->state
= STATE_READER_RECEIVE_DATA_1_OUT_OF_4
;
902 // do nothing, keep waiting
904 } else { // unexpected falling edge
905 DecodeReaderReset(DecodeReader
);
909 case STATE_READER_RECEIVE_DATA_1_OUT_OF_4
:
910 DecodeReader
->posCount
++;
911 if (DecodeReader
->posCount
== 1) {
912 DecodeReader
->sum1
= bit
?1:0;
913 } else if (DecodeReader
->posCount
<= 4) {
914 if (bit
) DecodeReader
->sum1
++;
915 } else if (DecodeReader
->posCount
== 5) {
916 DecodeReader
->sum2
= bit
?1:0;
918 if (bit
) DecodeReader
->sum2
++;
920 if (DecodeReader
->posCount
== 8) {
921 DecodeReader
->posCount
= 0;
922 if (DecodeReader
->sum1
<= 1 && DecodeReader
->sum2
>= 3) { // EOF
923 LED_B_OFF(); // Finished receiving
924 DecodeReaderReset(DecodeReader
);
925 if (DecodeReader
->byteCount
!= 0) {
928 } else if (DecodeReader
->sum1
>= 3 && DecodeReader
->sum2
<= 1) { // detected a 2bit position
929 DecodeReader
->shiftReg
>>= 2;
930 DecodeReader
->shiftReg
|= (DecodeReader
->bitCount
<< 6);
932 if (DecodeReader
->bitCount
== 15) { // we have a full byte
933 DecodeReader
->output
[DecodeReader
->byteCount
++] = DecodeReader
->shiftReg
;
934 if (DecodeReader
->byteCount
> DecodeReader
->byteCountMax
) {
935 // buffer overflow, give up
937 DecodeReaderReset(DecodeReader
);
939 DecodeReader
->bitCount
= 0;
940 DecodeReader
->shiftReg
= 0;
941 if (DecodeReader
->byteCount
== DecodeReader
->jam_search_len
) {
942 if (!memcmp(DecodeReader
->output
, DecodeReader
->jam_search_string
, DecodeReader
->jam_search_len
)) {
944 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER
| FPGA_HF_READER_MODE_SEND_JAM
);
945 DecodeReader
->state
= STATE_READER_RECEIVE_JAMMING
;
949 DecodeReader
->bitCount
++;
954 case STATE_READER_RECEIVE_DATA_1_OUT_OF_256
:
955 DecodeReader
->posCount
++;
956 if (DecodeReader
->posCount
== 1) {
957 DecodeReader
->sum1
= bit
?1:0;
958 } else if (DecodeReader
->posCount
<= 4) {
959 if (bit
) DecodeReader
->sum1
++;
960 } else if (DecodeReader
->posCount
== 5) {
961 DecodeReader
->sum2
= bit
?1:0;
963 DecodeReader
->sum2
++;
965 if (DecodeReader
->posCount
== 8) {
966 DecodeReader
->posCount
= 0;
967 if (DecodeReader
->sum1
<= 1 && DecodeReader
->sum2
>= 3) { // EOF
968 LED_B_OFF(); // Finished receiving
969 DecodeReaderReset(DecodeReader
);
970 if (DecodeReader
->byteCount
!= 0) {
973 } else if (DecodeReader
->sum1
>= 3 && DecodeReader
->sum2
<= 1) { // detected the bit position
974 DecodeReader
->shiftReg
= DecodeReader
->bitCount
;
976 if (DecodeReader
->bitCount
== 255) { // we have a full byte
977 DecodeReader
->output
[DecodeReader
->byteCount
++] = DecodeReader
->shiftReg
;
978 if (DecodeReader
->byteCount
> DecodeReader
->byteCountMax
) {
979 // buffer overflow, give up
981 DecodeReaderReset(DecodeReader
);
983 if (DecodeReader
->byteCount
== DecodeReader
->jam_search_len
) {
984 if (!memcmp(DecodeReader
->output
, DecodeReader
->jam_search_string
, DecodeReader
->jam_search_len
)) {
986 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER
| FPGA_HF_READER_MODE_SEND_JAM
);
987 DecodeReader
->state
= STATE_READER_RECEIVE_JAMMING
;
991 DecodeReader
->bitCount
++;
995 case STATE_READER_RECEIVE_JAMMING
:
996 DecodeReader
->posCount
++;
997 if (DecodeReader
->Coding
== CODING_1_OUT_OF_4
) {
998 if (DecodeReader
->posCount
== 7*16) { // 7 bits jammed
999 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER
| FPGA_HF_READER_MODE_SNOOP_AMPLITUDE
); // stop jamming
1000 // FpgaDisableTracing();
1002 } else if (DecodeReader
->posCount
== 8*16) {
1003 DecodeReader
->posCount
= 0;
1004 DecodeReader
->output
[DecodeReader
->byteCount
++] = 0x00;
1005 DecodeReader
->state
= STATE_READER_RECEIVE_DATA_1_OUT_OF_4
;
1008 if (DecodeReader
->posCount
== 7*256) { // 7 bits jammend
1009 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER
| FPGA_HF_READER_MODE_SNOOP_AMPLITUDE
); // stop jamming
1011 } else if (DecodeReader
->posCount
== 8*256) {
1012 DecodeReader
->posCount
= 0;
1013 DecodeReader
->output
[DecodeReader
->byteCount
++] = 0x00;
1014 DecodeReader
->state
= STATE_READER_RECEIVE_DATA_1_OUT_OF_256
;
1021 DecodeReaderReset(DecodeReader
);
1029 //-----------------------------------------------------------------------------
1030 // Receive a command (from the reader to us, where we are the simulated tag),
1031 // and store it in the given buffer, up to the given maximum length. Keeps
1032 // spinning, waiting for a well-framed command, until either we get one
1033 // (returns len) or someone presses the pushbutton on the board (returns -1).
1035 // Assume that we're called with the SSC (to the FPGA) and ADC path set
1037 //-----------------------------------------------------------------------------
1039 int GetIso15693CommandFromReader(uint8_t *received
, size_t max_len
, uint32_t *eof_time
) {
1041 bool gotFrame
= false;
1044 uint8_t dmaBuf
[ISO15693_DMA_BUFFER_SIZE
];
1046 // the decoder data structure
1047 DecodeReader_t DecodeReader
= {0};
1048 DecodeReaderInit(&DecodeReader
, received
, max_len
, 0, NULL
);
1050 // wait for last transfer to complete
1051 while (!(AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXEMPTY
));
1054 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
| FPGA_HF_SIMULATOR_NO_MODULATION
);
1056 // clear receive register and wait for next transfer
1057 uint32_t temp
= AT91C_BASE_SSC
->SSC_RHR
;
1059 while (!(AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
)) ;
1061 uint32_t dma_start_time
= GetCountSspClk() & 0xfffffff8;
1063 // Setup and start DMA.
1064 FpgaSetupSscDma(dmaBuf
, ISO15693_DMA_BUFFER_SIZE
);
1065 uint8_t *upTo
= dmaBuf
;
1068 uint16_t behindBy
= ((uint8_t*)AT91C_BASE_PDC_SSC
->PDC_RPR
- upTo
) & (ISO15693_DMA_BUFFER_SIZE
-1);
1070 if (behindBy
== 0) continue;
1073 if (upTo
>= dmaBuf
+ ISO15693_DMA_BUFFER_SIZE
) { // we have read all of the DMA buffer content.
1074 upTo
= dmaBuf
; // start reading the circular buffer from the beginning
1075 if (behindBy
> (9*ISO15693_DMA_BUFFER_SIZE
/10)) {
1076 Dbprintf("About to blow circular buffer - aborted! behindBy=%d", behindBy
);
1080 if (AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_ENDRX
)) { // DMA Counter Register had reached 0, already rotated.
1081 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (uint32_t) dmaBuf
; // refresh the DMA Next Buffer and
1082 AT91C_BASE_PDC_SSC
->PDC_RNCR
= ISO15693_DMA_BUFFER_SIZE
; // DMA Next Counter registers
1085 for (int i
= 7; i
>= 0; i
--) {
1086 if (Handle15693SampleFromReader((b
>> i
) & 0x01, &DecodeReader
)) {
1087 *eof_time
= dma_start_time
+ samples
- DELAY_READER_TO_ARM
; // end of EOF
1098 if (BUTTON_PRESS()) {
1099 DecodeReader
.byteCount
= -1;
1106 FpgaDisableSscDma();
1108 if (DEBUG
) Dbprintf("samples = %d, gotFrame = %d, Decoder: state = %d, len = %d, bitCount = %d, posCount = %d",
1109 samples
, gotFrame
, DecodeReader
.state
, DecodeReader
.byteCount
, DecodeReader
.bitCount
, DecodeReader
.posCount
);
1111 if (DecodeReader
.byteCount
> 0) {
1112 uint32_t sof_time
= *eof_time
1113 - DecodeReader
.byteCount
* (DecodeReader
.Coding
==CODING_1_OUT_OF_4
?128:2048) // time for byte transfers
1114 - 32 // time for SOF transfer
1115 - 16; // time for EOF transfer
1116 LogTrace_ISO15693(DecodeReader
.output
, DecodeReader
.byteCount
, sof_time
*32, *eof_time
*32, NULL
, true);
1119 return DecodeReader
.byteCount
;
1123 // Construct an identify (Inventory) request, which is the first
1124 // thing that you must send to a tag to get a response.
1125 static void BuildIdentifyRequest(uint8_t *cmd
) {
1127 // one sub-carrier, inventory, 1 slot, fast rate
1128 cmd
[0] = ISO15693_REQ_INVENTORY
| ISO15693_REQINV_SLOT1
| ISO15693_REQ_DATARATE_HIGH
;
1129 // inventory command code
1134 crc
= Iso15693Crc(cmd
, 3);
1135 cmd
[3] = crc
& 0xff;
1140 //-----------------------------------------------------------------------------
1141 // Start to read an ISO 15693 tag. We send an identify request, then wait
1142 // for the response. The response is not demodulated, just left in the buffer
1143 // so that it can be downloaded to a PC and processed there.
1144 //-----------------------------------------------------------------------------
1145 void AcquireRawAdcSamplesIso15693(void) {
1148 uint8_t *dest
= BigBuf_get_addr();
1150 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1151 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER
);
1153 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER
);
1154 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1157 BuildIdentifyRequest(cmd
);
1158 CodeIso15693AsReader(cmd
, sizeof(cmd
));
1160 // Give the tags time to energize
1163 // Now send the command
1164 uint32_t start_time
= 0;
1165 TransmitTo15693Tag(ToSend
, ToSendMax
, &start_time
);
1167 // wait for last transfer to complete
1168 while (!(AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXEMPTY
)) ;
1170 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER
| FPGA_HF_READER_SUBCARRIER_424_KHZ
| FPGA_HF_READER_MODE_RECEIVE_AMPLITUDE
);
1172 for(int c
= 0; c
< 4000; ) {
1173 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1174 uint16_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1179 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1184 void SnoopIso15693(uint8_t jam_search_len
, uint8_t *jam_search_string
) {
1188 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1193 // The DMA buffer, used to stream samples from the FPGA
1194 uint16_t dmaBuf
[ISO15693_DMA_BUFFER_SIZE
];
1196 // Count of samples received so far, so that we can include timing
1197 // information in the trace buffer.
1200 DecodeTag_t DecodeTag
= {0};
1201 uint8_t response
[ISO15693_MAX_RESPONSE_LENGTH
];
1202 DecodeTagInit(&DecodeTag
, response
, sizeof(response
));
1204 DecodeReader_t DecodeReader
= {0};
1205 uint8_t cmd
[ISO15693_MAX_COMMAND_LENGTH
];
1206 DecodeReaderInit(&DecodeReader
, cmd
, sizeof(cmd
), jam_search_len
, jam_search_string
);
1208 // Print some debug information about the buffer sizes
1210 Dbprintf("Snooping buffers initialized:");
1211 Dbprintf(" Trace: %i bytes", BigBuf_max_traceLen());
1212 Dbprintf(" Reader -> tag: %i bytes", ISO15693_MAX_COMMAND_LENGTH
);
1213 Dbprintf(" tag -> Reader: %i bytes", ISO15693_MAX_RESPONSE_LENGTH
);
1214 Dbprintf(" DMA: %i bytes", ISO15693_DMA_BUFFER_SIZE
* sizeof(uint16_t));
1216 Dbprintf("Snoop started. Press PM3 Button to stop.");
1218 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER
| FPGA_HF_READER_MODE_SNOOP_AMPLITUDE
);
1220 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1221 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER
);
1223 FpgaSetupSscDma((uint8_t*) dmaBuf
, ISO15693_DMA_BUFFER_SIZE
);
1225 bool TagIsActive
= false;
1226 bool ReaderIsActive
= false;
1227 bool ExpectTagAnswer
= false;
1228 uint32_t dma_start_time
= 0;
1229 uint16_t *upTo
= dmaBuf
;
1231 uint16_t max_behindBy
= 0;
1233 // And now we loop, receiving samples.
1235 uint16_t behindBy
= ((uint16_t*)AT91C_BASE_PDC_SSC
->PDC_RPR
- upTo
) & (ISO15693_DMA_BUFFER_SIZE
-1);
1236 if (behindBy
> max_behindBy
) {
1237 max_behindBy
= behindBy
;
1240 if (behindBy
== 0) continue;
1244 // DMA has transferred the very first data
1245 dma_start_time
= GetCountSspClk() & 0xfffffff0;
1248 uint16_t snoopdata
= *upTo
++;
1250 if (upTo
>= dmaBuf
+ ISO15693_DMA_BUFFER_SIZE
) { // we have read all of the DMA buffer content.
1251 upTo
= dmaBuf
; // start reading the circular buffer from the beginning
1252 if (behindBy
> (9*ISO15693_DMA_BUFFER_SIZE
/10)) {
1253 // FpgaDisableTracing();
1254 Dbprintf("About to blow circular buffer - aborted! behindBy=%d, samples=%d", behindBy
, samples
);
1257 if (AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_ENDRX
)) { // DMA Counter Register had reached 0, already rotated.
1258 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (uint32_t) dmaBuf
; // refresh the DMA Next Buffer and
1259 AT91C_BASE_PDC_SSC
->PDC_RNCR
= ISO15693_DMA_BUFFER_SIZE
; // DMA Next Counter registers
1261 if (BUTTON_PRESS()) {
1262 DbpString("Snoop stopped.");
1268 if (!TagIsActive
) { // no need to try decoding reader data if the tag is sending
1269 if (Handle15693SampleFromReader(snoopdata
& 0x02, &DecodeReader
)) {
1270 // FpgaDisableSscDma();
1271 uint32_t eof_time
= dma_start_time
+ samples
*16 + 8 - DELAY_READER_TO_ARM_SNOOP
; // end of EOF
1272 if (DecodeReader
.byteCount
> 0) {
1273 uint32_t sof_time
= eof_time
1274 - DecodeReader
.byteCount
* (DecodeReader
.Coding
==CODING_1_OUT_OF_4
?128*16:2048*16) // time for byte transfers
1275 - 32*16 // time for SOF transfer
1276 - 16*16; // time for EOF transfer
1277 LogTrace_ISO15693(DecodeReader
.output
, DecodeReader
.byteCount
, sof_time
*4, eof_time
*4, NULL
, true);
1279 /* And ready to receive another command. */
1280 DecodeReaderReset(&DecodeReader
);
1281 /* And also reset the demod code, which might have been */
1282 /* false-triggered by the commands from the reader. */
1283 DecodeTagReset(&DecodeTag
);
1284 ReaderIsActive
= false;
1285 ExpectTagAnswer
= true;
1288 // FpgaSetupSscDma((uint8_t*) dmaBuf, ISO15693_DMA_BUFFER_SIZE);
1290 } else if (Handle15693SampleFromReader(snoopdata
& 0x01, &DecodeReader
)) {
1291 // FpgaDisableSscDma();
1292 uint32_t eof_time
= dma_start_time
+ samples
*16 + 16 - DELAY_READER_TO_ARM_SNOOP
; // end of EOF
1293 if (DecodeReader
.byteCount
> 0) {
1294 uint32_t sof_time
= eof_time
1295 - DecodeReader
.byteCount
* (DecodeReader
.Coding
==CODING_1_OUT_OF_4
?128*16:2048*16) // time for byte transfers
1296 - 32*16 // time for SOF transfer
1297 - 16*16; // time for EOF transfer
1298 LogTrace_ISO15693(DecodeReader
.output
, DecodeReader
.byteCount
, sof_time
*4, eof_time
*4, NULL
, true);
1300 /* And ready to receive another command. */
1301 DecodeReaderReset(&DecodeReader
);
1302 /* And also reset the demod code, which might have been */
1303 /* false-triggered by the commands from the reader. */
1304 DecodeTagReset(&DecodeTag
);
1305 ReaderIsActive
= false;
1306 ExpectTagAnswer
= true;
1309 // FpgaSetupSscDma((uint8_t*) dmaBuf, ISO15693_DMA_BUFFER_SIZE);
1312 ReaderIsActive
= (DecodeReader
.state
>= STATE_READER_RECEIVE_DATA_1_OUT_OF_4
);
1316 if (!ReaderIsActive
&& ExpectTagAnswer
) { // no need to try decoding tag data if the reader is currently sending or no answer expected yet
1317 if (Handle15693SamplesFromTag(snoopdata
>> 2, &DecodeTag
)) {
1318 // FpgaDisableSscDma();
1319 uint32_t eof_time
= dma_start_time
+ samples
*16 - DELAY_TAG_TO_ARM_SNOOP
; // end of EOF
1320 if (DecodeTag
.lastBit
== SOF_PART2
) {
1321 eof_time
-= 8*16; // needed 8 additional samples to confirm single SOF (iCLASS)
1323 uint32_t sof_time
= eof_time
1324 - DecodeTag
.len
* 8 * 8 * 16 // time for byte transfers
1325 - 32 * 16 // time for SOF transfer
1326 - (DecodeTag
.lastBit
!= SOF_PART2
?32*16:0); // time for EOF transfer
1327 LogTrace_ISO15693(DecodeTag
.output
, DecodeTag
.len
, sof_time
*4, eof_time
*4, NULL
, false);
1328 // And ready to receive another response.
1329 DecodeTagReset(&DecodeTag
);
1330 DecodeReaderReset(&DecodeReader
);
1331 ExpectTagAnswer
= false;
1332 TagIsActive
= false;
1335 // FpgaSetupSscDma((uint8_t*) dmaBuf, ISO15693_DMA_BUFFER_SIZE);
1338 TagIsActive
= (DecodeTag
.state
>= STATE_TAG_RECEIVING_DATA
);
1344 FpgaDisableSscDma();
1346 DbpString("Snoop statistics:");
1347 Dbprintf(" ExpectTagAnswer: %d, TagIsActive: %d, ReaderIsActive: %d", ExpectTagAnswer
, TagIsActive
, ReaderIsActive
);
1348 Dbprintf(" DecodeTag State: %d", DecodeTag
.state
);
1349 Dbprintf(" DecodeTag byteCnt: %d", DecodeTag
.len
);
1350 Dbprintf(" DecodeTag posCount: %d", DecodeTag
.posCount
);
1351 Dbprintf(" DecodeReader State: %d", DecodeReader
.state
);
1352 Dbprintf(" DecodeReader byteCnt: %d", DecodeReader
.byteCount
);
1353 Dbprintf(" DecodeReader posCount: %d", DecodeReader
.posCount
);
1354 Dbprintf(" Trace length: %d", BigBuf_get_traceLen());
1355 Dbprintf(" Max behindBy: %d", max_behindBy
);
1359 // Initialize the proxmark as iso15k reader
1360 void Iso15693InitReader() {
1361 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1363 // Start from off (no field generated)
1365 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1368 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1369 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER
);
1371 // Give the tags time to energize
1373 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER
);
1377 ///////////////////////////////////////////////////////////////////////
1378 // ISO 15693 Part 3 - Air Interface
1379 // This section basically contains transmission and receiving of bits
1380 ///////////////////////////////////////////////////////////////////////
1383 // uid is in transmission order (which is reverse of display order)
1384 static void BuildReadBlockRequest(uint8_t *uid
, uint8_t blockNumber
, uint8_t *cmd
) {
1386 // If we set the Option_Flag in this request, the VICC will respond with the security status of the block
1387 // followed by the block data
1388 cmd
[0] = ISO15693_REQ_OPTION
| ISO15693_REQ_ADDRESS
| ISO15693_REQ_DATARATE_HIGH
;
1389 // READ BLOCK command code
1390 cmd
[1] = ISO15693_READBLOCK
;
1391 // UID may be optionally specified here
1400 cmd
[9] = uid
[7]; // 0xe0; // always e0 (not exactly unique)
1401 // Block number to read
1402 cmd
[10] = blockNumber
;
1404 crc
= Iso15693Crc(cmd
, 11); // the crc needs to be calculated over 11 bytes
1405 cmd
[11] = crc
& 0xff;
1411 // Now the VICC>VCD responses when we are simulating a tag
1412 static void BuildInventoryResponse(uint8_t *uid
) {
1417 cmd
[0] = 0; // No error, no protocol format extension
1418 cmd
[1] = 0; // DSFID (data storage format identifier). 0x00 = not supported
1420 cmd
[2] = uid
[7]; //0x32;
1421 cmd
[3] = uid
[6]; //0x4b;
1422 cmd
[4] = uid
[5]; //0x03;
1423 cmd
[5] = uid
[4]; //0x01;
1424 cmd
[6] = uid
[3]; //0x00;
1425 cmd
[7] = uid
[2]; //0x10;
1426 cmd
[8] = uid
[1]; //0x05;
1427 cmd
[9] = uid
[0]; //0xe0;
1429 crc
= Iso15693Crc(cmd
, 10);
1430 cmd
[10] = crc
& 0xff;
1433 CodeIso15693AsTag(cmd
, sizeof(cmd
));
1436 // Universal Method for sending to and recv bytes from a tag
1437 // init ... should we initialize the reader?
1438 // speed ... 0 low speed, 1 hi speed
1439 // *recv will contain the tag's answer
1440 // return: length of received data, or -1 for timeout
1441 int SendDataTag(uint8_t *send
, int sendlen
, bool init
, bool speed_fast
, uint8_t *recv
, uint16_t max_recv_len
, uint32_t start_time
, uint16_t timeout
, uint32_t *eof_time
) {
1444 Iso15693InitReader();
1451 // high speed (1 out of 4)
1452 CodeIso15693AsReader(send
, sendlen
);
1454 // low speed (1 out of 256)
1455 CodeIso15693AsReader256(send
, sendlen
);
1458 TransmitTo15693Tag(ToSend
, ToSendMax
, &start_time
);
1459 uint32_t end_time
= start_time
+ 32*(8*ToSendMax
-4); // substract the 4 padding bits after EOF
1460 LogTrace_ISO15693(send
, sendlen
, start_time
*4, end_time
*4, NULL
, true);
1462 // Now wait for a response
1464 answerLen
= GetIso15693AnswerFromTag(recv
, max_recv_len
, timeout
, eof_time
);
1471 int SendDataTagEOF(uint8_t *recv
, uint16_t max_recv_len
, uint32_t start_time
, uint16_t timeout
, uint32_t *eof_time
) {
1475 CodeIso15693AsReaderEOF();
1477 TransmitTo15693Tag(ToSend
, ToSendMax
, &start_time
);
1478 uint32_t end_time
= start_time
+ 32*(8*ToSendMax
-4); // substract the 4 padding bits after EOF
1479 LogTrace_ISO15693(NULL
, 0, start_time
*4, end_time
*4, NULL
, true);
1481 // Now wait for a response
1483 answerLen
= GetIso15693AnswerFromTag(recv
, max_recv_len
, timeout
, eof_time
);
1490 // --------------------------------------------------------------------
1492 // --------------------------------------------------------------------
1494 // Decodes a message from a tag and displays its metadata and content
1495 #define DBD15STATLEN 48
1496 void DbdecodeIso15693Answer(int len
, uint8_t *d
) {
1497 char status
[DBD15STATLEN
+1]={0};
1501 if (d
[0] & ISO15693_RES_EXT
)
1502 strncat(status
,"ProtExt ", DBD15STATLEN
);
1503 if (d
[0] & ISO15693_RES_ERROR
) {
1505 strncat(status
,"Error ", DBD15STATLEN
);
1508 strncat(status
,"01:notSupp", DBD15STATLEN
);
1511 strncat(status
,"02:notRecog", DBD15STATLEN
);
1514 strncat(status
,"03:optNotSupp", DBD15STATLEN
);
1517 strncat(status
,"0f:noInfo", DBD15STATLEN
);
1520 strncat(status
,"10:doesn'tExist", DBD15STATLEN
);
1523 strncat(status
,"11:lockAgain", DBD15STATLEN
);
1526 strncat(status
,"12:locked", DBD15STATLEN
);
1529 strncat(status
,"13:progErr", DBD15STATLEN
);
1532 strncat(status
,"14:lockErr", DBD15STATLEN
);
1535 strncat(status
,"unknownErr", DBD15STATLEN
);
1537 strncat(status
," ", DBD15STATLEN
);
1539 strncat(status
,"NoErr ", DBD15STATLEN
);
1542 crc
=Iso15693Crc(d
,len
-2);
1543 if ( (( crc
& 0xff ) == d
[len
-2]) && (( crc
>> 8 ) == d
[len
-1]) )
1544 strncat(status
,"CrcOK",DBD15STATLEN
);
1546 strncat(status
,"CrcFail!",DBD15STATLEN
);
1548 Dbprintf("%s",status
);
1554 ///////////////////////////////////////////////////////////////////////
1555 // Functions called via USB/Client
1556 ///////////////////////////////////////////////////////////////////////
1558 void SetDebugIso15693(uint32_t debug
) {
1560 Dbprintf("Iso15693 Debug is now %s",DEBUG
?"on":"off");
1565 //---------------------------------------------------------------------------------------
1566 // Simulate an ISO15693 reader, perform anti-collision and then attempt to read a sector.
1567 // all demodulation performed in arm rather than host. - greg
1568 //---------------------------------------------------------------------------------------
1569 void ReaderIso15693(uint32_t parameter
) {
1575 uint8_t TagUID
[8] = {0x00};
1577 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1579 uint8_t answer
[ISO15693_MAX_RESPONSE_LENGTH
];
1581 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1583 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER
);
1585 // Start from off (no field generated)
1586 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1589 // Give the tags time to energize
1591 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER
);
1596 // FIRST WE RUN AN INVENTORY TO GET THE TAG UID
1597 // THIS MEANS WE CAN PRE-BUILD REQUESTS TO SAVE CPU TIME
1599 // Now send the IDENTIFY command
1601 BuildIdentifyRequest(cmd
);
1602 uint32_t start_time
= 0;
1604 int answerLen
= SendDataTag(cmd
, sizeof(cmd
), true, true, answer
, sizeof(answer
), start_time
, ISO15693_READER_TIMEOUT
, &eof_time
);
1605 start_time
= eof_time
+ DELAY_ISO15693_VICC_TO_VCD_READER
;
1607 if (answerLen
>= 12) { // we should do a better check than this
1608 TagUID
[0] = answer
[2];
1609 TagUID
[1] = answer
[3];
1610 TagUID
[2] = answer
[4];
1611 TagUID
[3] = answer
[5];
1612 TagUID
[4] = answer
[6];
1613 TagUID
[5] = answer
[7];
1614 TagUID
[6] = answer
[8]; // IC Manufacturer code
1615 TagUID
[7] = answer
[9]; // always E0
1618 Dbprintf("%d octets read from IDENTIFY request:", answerLen
);
1619 DbdecodeIso15693Answer(answerLen
, answer
);
1620 Dbhexdump(answerLen
, answer
, false);
1623 if (answerLen
>= 12)
1624 Dbprintf("UID = %02hX%02hX%02hX%02hX%02hX%02hX%02hX%02hX",
1625 TagUID
[7],TagUID
[6],TagUID
[5],TagUID
[4],
1626 TagUID
[3],TagUID
[2],TagUID
[1],TagUID
[0]);
1629 if (answerLen
>= 12 && DEBUG
) {
1630 for (int i
= 0; i
< 32; i
++) { // sanity check, assume max 32 pages
1632 BuildReadBlockRequest(TagUID
, i
, cmd
);
1633 answerLen
= SendDataTag(cmd
, sizeof(cmd
), false, true, answer
, sizeof(answer
), start_time
, ISO15693_READER_TIMEOUT
, &eof_time
);
1634 start_time
= eof_time
+ DELAY_ISO15693_VICC_TO_VCD_READER
;
1635 if (answerLen
> 0) {
1636 Dbprintf("READ SINGLE BLOCK %d returned %d octets:", i
, answerLen
);
1637 DbdecodeIso15693Answer(answerLen
, answer
);
1638 Dbhexdump(answerLen
, answer
, false);
1639 if ( *((uint32_t*) answer
) == 0x07160101 ) break; // exit on NoPageErr
1644 // for the time being, switch field off to protect RDV4
1645 // note: this prevents using hf 15 cmd with s option - which isn't implemented yet anyway
1646 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1653 // Simulate an ISO15693 TAG.
1654 // For Inventory command: print command and send Inventory Response with given UID
1655 // TODO: interpret other reader commands and send appropriate response
1656 void SimTagIso15693(uint32_t parameter
, uint8_t *uid
) {
1660 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1661 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1662 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
| FPGA_HF_SIMULATOR_NO_MODULATION
);
1663 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_SIMULATOR
);
1667 uint8_t cmd
[ISO15693_MAX_COMMAND_LENGTH
];
1669 // Build a suitable response to the reader INVENTORY command
1670 BuildInventoryResponse(uid
);
1673 while (!BUTTON_PRESS()) {
1674 uint32_t eof_time
= 0, start_time
= 0;
1675 int cmd_len
= GetIso15693CommandFromReader(cmd
, sizeof(cmd
), &eof_time
);
1677 if ((cmd_len
>= 5) && (cmd
[0] & ISO15693_REQ_INVENTORY
) && (cmd
[1] == ISO15693_INVENTORY
)) { // TODO: check more flags
1678 bool slow
= !(cmd
[0] & ISO15693_REQ_DATARATE_HIGH
);
1679 start_time
= eof_time
+ DELAY_ISO15693_VCD_TO_VICC_SIM
;
1680 TransmitTo15693Reader(ToSend
, ToSendMax
, &start_time
, 0, slow
);
1683 Dbprintf("%d bytes read from reader:", cmd_len
);
1684 Dbhexdump(cmd_len
, cmd
, false);
1687 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1693 // Since there is no standardized way of reading the AFI out of a tag, we will brute force it
1694 // (some manufactures offer a way to read the AFI, though)
1695 void BruteforceIso15693Afi(uint32_t speed
) {
1699 uint8_t recv
[ISO15693_MAX_RESPONSE_LENGTH
];
1700 int datalen
= 0, recvlen
= 0;
1703 // first without AFI
1704 // Tags should respond without AFI and with AFI=0 even when AFI is active
1706 data
[0] = ISO15693_REQ_DATARATE_HIGH
| ISO15693_REQ_INVENTORY
| ISO15693_REQINV_SLOT1
;
1707 data
[1] = ISO15693_INVENTORY
;
1708 data
[2] = 0; // mask length
1709 datalen
= Iso15693AddCrc(data
,3);
1710 uint32_t start_time
= GetCountSspClk();
1711 recvlen
= SendDataTag(data
, datalen
, true, speed
, recv
, sizeof(recv
), 0, ISO15693_READER_TIMEOUT
, &eof_time
);
1712 start_time
= eof_time
+ DELAY_ISO15693_VICC_TO_VCD_READER
;
1715 Dbprintf("NoAFI UID=%s", Iso15693sprintUID(NULL
, &recv
[2]));
1720 data
[0] = ISO15693_REQ_DATARATE_HIGH
| ISO15693_REQ_INVENTORY
| ISO15693_REQINV_AFI
| ISO15693_REQINV_SLOT1
;
1721 data
[1] = ISO15693_INVENTORY
;
1723 data
[3] = 0; // mask length
1725 for (int i
= 0; i
< 256; i
++) {
1727 datalen
= Iso15693AddCrc(data
,4);
1728 recvlen
= SendDataTag(data
, datalen
, false, speed
, recv
, sizeof(recv
), start_time
, ISO15693_READER_TIMEOUT
, &eof_time
);
1729 start_time
= eof_time
+ DELAY_ISO15693_VICC_TO_VCD_READER
;
1731 if (recvlen
>= 12) {
1732 Dbprintf("AFI=%i UID=%s", i
, Iso15693sprintUID(NULL
, &recv
[2]));
1735 Dbprintf("AFI Bruteforcing done.");
1737 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1743 // Allows to directly send commands to the tag via the client
1744 void DirectTag15693Command(uint32_t datalen
, uint32_t speed
, uint32_t recv
, uint8_t data
[]) {
1749 uint8_t recvbuf
[ISO15693_MAX_RESPONSE_LENGTH
];
1753 bool request_answer
= false;
1756 case ISO15693_WRITEBLOCK
:
1757 case ISO15693_LOCKBLOCK
:
1758 case ISO15693_WRITE_MULTI_BLOCK
:
1759 case ISO15693_WRITE_AFI
:
1760 case ISO15693_LOCK_AFI
:
1761 case ISO15693_WRITE_DSFID
:
1762 case ISO15693_LOCK_DSFID
:
1763 timeout
= ISO15693_READER_TIMEOUT_WRITE
;
1764 request_answer
= data
[0] & ISO15693_REQ_OPTION
;
1767 timeout
= ISO15693_READER_TIMEOUT
;
1772 Dbhexdump(datalen
, data
, false);
1775 recvlen
= SendDataTag(data
, datalen
, true, speed
, (recv
?recvbuf
:NULL
), sizeof(recvbuf
), 0, timeout
, &eof_time
);
1777 if (request_answer
) { // send a single EOF to get the tag response
1778 recvlen
= SendDataTagEOF((recv
?recvbuf
:NULL
), sizeof(recvbuf
), 0, ISO15693_READER_TIMEOUT
, &eof_time
);
1781 // for the time being, switch field off to protect rdv4.0
1782 // note: this prevents using hf 15 cmd with s option - which isn't implemented yet anyway
1783 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1790 Dbhexdump(recvlen
, recvbuf
, false);
1791 DbdecodeIso15693Answer(recvlen
, recvbuf
);
1794 if (recvlen
> ISO15693_MAX_RESPONSE_LENGTH
) {
1795 recvlen
= ISO15693_MAX_RESPONSE_LENGTH
;
1797 cmd_send(CMD_ACK
, recvlen
, 0, 0, recvbuf
, ISO15693_MAX_RESPONSE_LENGTH
);
1803 //-----------------------------------------------------------------------------
1804 // Work with "magic Chinese" card.
1806 //-----------------------------------------------------------------------------
1808 // Set the UID on Magic ISO15693 tag (based on Iceman's LUA-script).
1809 void SetTag15693Uid(uint8_t *uid
) {
1813 uint8_t cmd
[4][9] = {
1814 {ISO15693_REQ_DATARATE_HIGH
, ISO15693_WRITEBLOCK
, 0x3e, 0x00, 0x00, 0x00, 0x00},
1815 {ISO15693_REQ_DATARATE_HIGH
, ISO15693_WRITEBLOCK
, 0x3f, 0x69, 0x96, 0x00, 0x00},
1816 {ISO15693_REQ_DATARATE_HIGH
, ISO15693_WRITEBLOCK
, 0x38},
1817 {ISO15693_REQ_DATARATE_HIGH
, ISO15693_WRITEBLOCK
, 0x39}
1823 uint8_t recvbuf
[ISO15693_MAX_RESPONSE_LENGTH
];
1826 // Command 3 : 022138u8u7u6u5 (where uX = uid byte X)
1832 // Command 4 : 022139u4u3u2u1 (where uX = uid byte X)
1838 uint32_t start_time
= 0;
1840 for (int i
= 0; i
< 4; i
++) {
1842 crc
= Iso15693Crc(cmd
[i
], 7);
1843 cmd
[i
][7] = crc
& 0xff;
1844 cmd
[i
][8] = crc
>> 8;
1846 recvlen
= SendDataTag(cmd
[i
], sizeof(cmd
[i
]), i
==0?true:false, true, recvbuf
, sizeof(recvbuf
), start_time
, ISO15693_READER_TIMEOUT_WRITE
, &eof_time
);
1847 start_time
= eof_time
+ DELAY_ISO15693_VICC_TO_VCD_READER
;
1850 Dbhexdump(sizeof(cmd
[i
]), cmd
[i
], false);
1853 Dbhexdump(recvlen
, recvbuf
, false);
1854 DbdecodeIso15693Answer(recvlen
, recvbuf
);
1857 // Note: need to know if we expect an answer from one of the magic commands
1858 // if (recvlen < 0) {
1863 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1866 cmd_send(CMD_ACK
, recvlen
, 0, 0, recvbuf
, recvlen
);
1872 // --------------------------------------------------------------------
1873 // -- Misc & deprecated functions
1874 // --------------------------------------------------------------------
1878 // do not use; has a fix UID
1879 static void __attribute__((unused)) BuildSysInfoRequest(uint8_t *uid)
1884 // If we set the Option_Flag in this request, the VICC will respond with the security status of the block
1885 // followed by the block data
1886 // one sub-carrier, inventory, 1 slot, fast rate
1887 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1888 // System Information command code
1890 // UID may be optionally specified here
1899 cmd[9]= 0xe0; // always e0 (not exactly unique)
1901 crc = Iso15693Crc(cmd, 10); // the crc needs to be calculated over 2 bytes
1902 cmd[10] = crc & 0xff;
1905 CodeIso15693AsReader(cmd, sizeof(cmd));
1909 // do not use; has a fix UID
1910 static void __attribute__((unused)) BuildReadMultiBlockRequest(uint8_t *uid)
1915 // If we set the Option_Flag in this request, the VICC will respond with the security status of the block
1916 // followed by the block data
1917 // one sub-carrier, inventory, 1 slot, fast rate
1918 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1919 // READ Multi BLOCK command code
1921 // UID may be optionally specified here
1930 cmd[9]= 0xe0; // always e0 (not exactly unique)
1931 // First Block number to read
1933 // Number of Blocks to read
1934 cmd[11] = 0x2f; // read quite a few
1936 crc = Iso15693Crc(cmd, 12); // the crc needs to be calculated over 2 bytes
1937 cmd[12] = crc & 0xff;
1940 CodeIso15693AsReader(cmd, sizeof(cmd));
1943 // do not use; has a fix UID
1944 static void __attribute__((unused)) BuildArbitraryRequest(uint8_t *uid,uint8_t CmdCode)
1949 // If we set the Option_Flag in this request, the VICC will respond with the security status of the block
1950 // followed by the block data
1951 // one sub-carrier, inventory, 1 slot, fast rate
1952 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1953 // READ BLOCK command code
1955 // UID may be optionally specified here
1964 cmd[9]= 0xe0; // always e0 (not exactly unique)
1970 // cmd[13] = 0x00; //Now the CRC
1971 crc = Iso15693Crc(cmd, 12); // the crc needs to be calculated over 2 bytes
1972 cmd[12] = crc & 0xff;
1975 CodeIso15693AsReader(cmd, sizeof(cmd));
1978 // do not use; has a fix UID
1979 static void __attribute__((unused)) BuildArbitraryCustomRequest(uint8_t uid[], uint8_t CmdCode)
1984 // If we set the Option_Flag in this request, the VICC will respond with the security status of the block
1985 // followed by the block data
1986 // one sub-carrier, inventory, 1 slot, fast rate
1987 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1988 // READ BLOCK command code
1990 // UID may be optionally specified here
1999 cmd[9]= 0xe0; // always e0 (not exactly unique)
2001 cmd[10] = 0x05; // for custom codes this must be manufacturer code
2005 // cmd[13] = 0x00; //Now the CRC
2006 crc = Iso15693Crc(cmd, 12); // the crc needs to be calculated over 2 bytes
2007 cmd[12] = crc & 0xff;
2010 CodeIso15693AsReader(cmd, sizeof(cmd));