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]))
69 ///////////////////////////////////////////////////////////////////////
70 // ISO 15693 Part 2 - Air Interface
71 // This section basicly contains transmission and receiving of bits
72 ///////////////////////////////////////////////////////////////////////
74 #define Crc(data,datalen) Iso15693Crc(data,datalen)
75 #define AddCrc(data,datalen) Iso15693AddCrc(data,datalen)
76 #define sprintUID(target,uid) Iso15693sprintUID(target,uid)
79 #define ISO15693_DMA_BUFFER_SIZE 2048 // must be a power of 2
80 #define ISO15693_MAX_RESPONSE_LENGTH 36 // allows read single block with the maximum block size of 256bits. Read multiple blocks not supported yet
81 #define ISO15693_MAX_COMMAND_LENGTH 45 // allows write single block with the maximum block size of 256bits. Write multiple blocks not supported yet
83 // timing. Delays in SSP_CLK ticks.
84 #define DELAY_READER_TO_ARM 8
85 #define DELAY_ARM_TO_READER 1
86 #define DELAY_ISO15693_VCD_TO_VICC 132 // 132/423.75kHz = 311.5us from end of EOF to start of tag response
87 #define DELAY_ISO15693_VICC_TO_VCD 1017 // 1017/3.39MHz = 300us between end of tag response and next reader command
89 // ---------------------------
91 // ---------------------------
93 // prepare data using "1 out of 4" code for later transmission
94 // resulting data rate is 26.48 kbit/s (fc/512)
96 // n ... length of data
97 static void CodeIso15693AsReader(uint8_t *cmd
, int n
)
103 // Give it a bit of slack at the beginning
104 for(i
= 0; i
< 24; i
++) {
117 for(i
= 0; i
< n
; i
++) {
118 for(j
= 0; j
< 8; j
+= 2) {
119 int these
= (cmd
[i
] >> j
) & 3;
170 // Fill remainder of last byte with 1
171 for(i
= 0; i
< 4; i
++) {
178 // encode data using "1 out of 256" scheme
179 // data rate is 1,66 kbit/s (fc/8192)
180 // is designed for more robust communication over longer distances
181 static void CodeIso15693AsReader256(uint8_t *cmd
, int n
)
187 // Give it a bit of slack at the beginning
188 for(i
= 0; i
< 24; i
++) {
202 for(i
= 0; i
< n
; i
++) {
203 for (j
= 0; j
<=255; j
++) {
219 // Fill remainder of last byte with 1
220 for(i
= 0; i
< 4; i
++) {
228 static void CodeIso15693AsTag(uint8_t *cmd
, int n
)
243 for(int i
= 0; i
< n
; i
++) {
244 for(int j
= 0; j
< 8; j
++) {
245 if ((cmd
[i
] >> j
) & 0x01) {
269 // Transmit the command (to the tag) that was placed in cmd[].
270 static void TransmitTo15693Tag(const uint8_t *cmd
, int len
, uint32_t start_time
)
272 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_TX
);
273 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX
);
275 while (GetCountSspClk() < start_time
);
278 for(int c
= 0; c
< len
; ) {
279 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
280 AT91C_BASE_SSC
->SSC_THR
= ~cmd
[c
];
288 //-----------------------------------------------------------------------------
289 // Transmit the tag response (to the reader) that was placed in cmd[].
290 //-----------------------------------------------------------------------------
291 static void TransmitTo15693Reader(const uint8_t *cmd
, size_t len
, uint32_t start_time
, bool slow
)
293 // don't use the FPGA_HF_SIMULATOR_MODULATE_424K_8BIT minor mode. It would spoil GetCountSspClk()
294 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
| FPGA_HF_SIMULATOR_MODULATE_424K
);
296 uint8_t shift_delay
= start_time
& 0x00000007;
297 uint8_t bitmask
= 0x00;
298 for (int i
= 0; i
< shift_delay
; i
++) {
299 bitmask
|= (0x01 << i
);
302 while (GetCountSspClk() < (start_time
& 0xfffffff8)) ;
304 AT91C_BASE_SSC
->SSC_THR
= 0x00; // clear TXRDY
307 uint8_t bits_to_shift
= 0x00;
308 for(size_t c
= 0; c
<= len
; c
++) {
309 uint8_t bits_to_send
= bits_to_shift
<< (8 - shift_delay
) | (c
==len
?0x00:cmd
[c
]) >> shift_delay
;
310 bits_to_shift
= cmd
[c
] & bitmask
;
311 for (int i
= 7; i
>= 0; i
--) {
312 for (int j
= 0; j
< (slow
?4:1); ) {
313 if (AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXRDY
) {
314 if (bits_to_send
>> i
& 0x01) {
315 AT91C_BASE_SSC
->SSC_THR
= 0xff;
317 AT91C_BASE_SSC
->SSC_THR
= 0x00;
329 //=============================================================================
330 // An ISO 15693 decoder for tag responses (one subcarrier only).
331 // Uses cross correlation to identify each bit and EOF.
332 // This function is called 8 times per bit (every 2 subcarrier cycles).
333 // Subcarrier frequency fs is 424kHz, 1/fs = 2,36us,
334 // i.e. function is called every 4,72us
336 // LED C -> ON once we have received the SOF and are expecting the rest.
337 // LED C -> OFF once we have received EOF or are unsynced
339 // Returns: true if we received a EOF
340 // false if we are still waiting for some more
341 //=============================================================================
343 #define NOISE_THRESHOLD 160 // don't try to correlate noise
345 typedef struct DecodeTag
{
349 STATE_TAG_SOF_HIGH_END
,
350 STATE_TAG_RECEIVING_DATA
,
369 static int inline __attribute__((always_inline
)) Handle15693SamplesFromTag(uint16_t amplitude
, DecodeTag_t
*DecodeTag
)
371 switch(DecodeTag
->state
) {
372 case STATE_TAG_SOF_LOW
:
373 // waiting for 12 times low (11 times low is accepted as well)
374 if (amplitude
< NOISE_THRESHOLD
) {
375 DecodeTag
->posCount
++;
377 if (DecodeTag
->posCount
> 10) {
378 DecodeTag
->posCount
= 1;
380 DecodeTag
->state
= STATE_TAG_SOF_HIGH
;
382 DecodeTag
->posCount
= 0;
387 case STATE_TAG_SOF_HIGH
:
388 // waiting for 10 times high. Take average over the last 8
389 if (amplitude
> NOISE_THRESHOLD
) {
390 DecodeTag
->posCount
++;
391 if (DecodeTag
->posCount
> 2) {
392 DecodeTag
->sum1
+= amplitude
; // keep track of average high value
394 if (DecodeTag
->posCount
== 10) {
395 DecodeTag
->sum1
>>= 4; // calculate half of average high value (8 samples)
396 DecodeTag
->state
= STATE_TAG_SOF_HIGH_END
;
398 } else { // high phase was too short
399 DecodeTag
->posCount
= 1;
400 DecodeTag
->state
= STATE_TAG_SOF_LOW
;
404 case STATE_TAG_SOF_HIGH_END
:
405 // waiting for a falling edge
406 if (amplitude
< DecodeTag
->sum1
) { // signal drops below 50% average high: a falling edge
407 DecodeTag
->lastBit
= SOF_PART1
; // detected 1st part of SOF (12 samples low and 12 samples high)
408 DecodeTag
->shiftReg
= 0;
409 DecodeTag
->bitCount
= 0;
411 DecodeTag
->sum1
= amplitude
;
413 DecodeTag
->posCount
= 2;
414 DecodeTag
->state
= STATE_TAG_RECEIVING_DATA
;
417 DecodeTag
->posCount
++;
418 if (DecodeTag
->posCount
> 13) { // high phase too long
419 DecodeTag
->posCount
= 0;
420 DecodeTag
->state
= STATE_TAG_SOF_LOW
;
426 case STATE_TAG_RECEIVING_DATA
:
427 if (DecodeTag
->posCount
== 1) {
431 if (DecodeTag
->posCount
<= 4) {
432 DecodeTag
->sum1
+= amplitude
;
434 DecodeTag
->sum2
+= amplitude
;
436 if (DecodeTag
->posCount
== 8) {
437 int32_t corr_1
= DecodeTag
->sum2
- DecodeTag
->sum1
;
438 int32_t corr_0
= -corr_1
;
439 int32_t corr_EOF
= (DecodeTag
->sum1
+ DecodeTag
->sum2
) / 2;
440 if (corr_EOF
> corr_0
&& corr_EOF
> corr_1
) {
441 if (DecodeTag
->lastBit
== LOGIC0
) { // this was already part of EOF
442 DecodeTag
->state
= STATE_TAG_EOF
;
444 DecodeTag
->posCount
= 0;
445 DecodeTag
->state
= STATE_TAG_SOF_LOW
;
448 } else if (corr_1
> corr_0
) {
450 if (DecodeTag
->lastBit
== SOF_PART1
) { // still part of SOF
451 DecodeTag
->lastBit
= SOF_PART2
; // SOF completed
453 DecodeTag
->lastBit
= LOGIC1
;
454 DecodeTag
->shiftReg
>>= 1;
455 DecodeTag
->shiftReg
|= 0x80;
456 DecodeTag
->bitCount
++;
457 if (DecodeTag
->bitCount
== 8) {
458 DecodeTag
->output
[DecodeTag
->len
] = DecodeTag
->shiftReg
;
460 if (DecodeTag
->len
> DecodeTag
->max_len
) {
461 // buffer overflow, give up
462 DecodeTag
->posCount
= 0;
463 DecodeTag
->state
= STATE_TAG_SOF_LOW
;
466 DecodeTag
->bitCount
= 0;
467 DecodeTag
->shiftReg
= 0;
472 if (DecodeTag
->lastBit
== SOF_PART1
) { // incomplete SOF
473 DecodeTag
->posCount
= 0;
474 DecodeTag
->state
= STATE_TAG_SOF_LOW
;
477 DecodeTag
->lastBit
= LOGIC0
;
478 DecodeTag
->shiftReg
>>= 1;
479 DecodeTag
->bitCount
++;
480 if (DecodeTag
->bitCount
== 8) {
481 DecodeTag
->output
[DecodeTag
->len
] = DecodeTag
->shiftReg
;
483 if (DecodeTag
->len
> DecodeTag
->max_len
) {
484 // buffer overflow, give up
485 DecodeTag
->posCount
= 0;
486 DecodeTag
->state
= STATE_TAG_SOF_LOW
;
489 DecodeTag
->bitCount
= 0;
490 DecodeTag
->shiftReg
= 0;
494 DecodeTag
->posCount
= 0;
496 DecodeTag
->posCount
++;
500 if (DecodeTag
->posCount
== 1) {
504 if (DecodeTag
->posCount
<= 4) {
505 DecodeTag
->sum1
+= amplitude
;
507 DecodeTag
->sum2
+= amplitude
;
509 if (DecodeTag
->posCount
== 8) {
510 int32_t corr_1
= DecodeTag
->sum2
- DecodeTag
->sum1
;
511 int32_t corr_0
= -corr_1
;
512 int32_t corr_EOF
= (DecodeTag
->sum1
+ DecodeTag
->sum2
) / 2;
513 if (corr_EOF
> corr_0
|| corr_1
> corr_0
) {
514 DecodeTag
->posCount
= 0;
515 DecodeTag
->state
= STATE_TAG_SOF_LOW
;
522 DecodeTag
->posCount
++;
531 static void DecodeTagInit(DecodeTag_t
*DecodeTag
, uint8_t *data
, uint16_t max_len
)
533 DecodeTag
->posCount
= 0;
534 DecodeTag
->state
= STATE_TAG_SOF_LOW
;
535 DecodeTag
->output
= data
;
536 DecodeTag
->max_len
= max_len
;
540 static void DecodeTagReset(DecodeTag_t
*DecodeTag
)
542 DecodeTag
->posCount
= 0;
543 DecodeTag
->state
= STATE_TAG_SOF_LOW
;
548 * Receive and decode the tag response, also log to tracebuffer
550 static int GetIso15693AnswerFromTag(uint8_t* response
, uint16_t max_len
, int timeout
)
553 bool gotFrame
= false;
555 uint16_t *dmaBuf
= (uint16_t*)BigBuf_malloc(ISO15693_DMA_BUFFER_SIZE
*sizeof(uint16_t));
557 // the Decoder data structure
558 DecodeTag_t DecodeTag
= { 0 };
559 DecodeTagInit(&DecodeTag
, response
, max_len
);
561 // wait for last transfer to complete
562 while (!(AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXEMPTY
));
564 // And put the FPGA in the appropriate mode
565 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
| FPGA_HF_READER_RX_XCORR_AMPLITUDE
);
567 // Setup and start DMA.
568 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
569 FpgaSetupSscDma((uint8_t*) dmaBuf
, ISO15693_DMA_BUFFER_SIZE
);
570 uint16_t *upTo
= dmaBuf
;
573 uint16_t behindBy
= ((uint16_t*)AT91C_BASE_PDC_SSC
->PDC_RPR
- upTo
) & (ISO15693_DMA_BUFFER_SIZE
-1);
575 if (behindBy
== 0) continue;
577 uint16_t tagdata
= *upTo
++;
579 if(upTo
>= dmaBuf
+ ISO15693_DMA_BUFFER_SIZE
) { // we have read all of the DMA buffer content.
580 upTo
= dmaBuf
; // start reading the circular buffer from the beginning
581 if(behindBy
> (9*ISO15693_DMA_BUFFER_SIZE
/10)) {
582 Dbprintf("About to blow circular buffer - aborted! behindBy=%d", behindBy
);
586 if (AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_ENDRX
)) { // DMA Counter Register had reached 0, already rotated.
587 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (uint32_t) dmaBuf
; // refresh the DMA Next Buffer and
588 AT91C_BASE_PDC_SSC
->PDC_RNCR
= ISO15693_DMA_BUFFER_SIZE
; // DMA Next Counter registers
593 if (Handle15693SamplesFromTag(tagdata
, &DecodeTag
)) {
598 if (samples
> timeout
&& DecodeTag
.state
< STATE_TAG_RECEIVING_DATA
) {
608 if (DEBUG
) Dbprintf("samples = %d, gotFrame = %d, Decoder: state = %d, len = %d, bitCount = %d, posCount = %d",
609 samples
, gotFrame
, DecodeTag
.state
, DecodeTag
.len
, DecodeTag
.bitCount
, DecodeTag
.posCount
);
611 if (DecodeTag
.len
> 0) {
612 LogTrace(DecodeTag
.output
, DecodeTag
.len
, 0, 0, NULL
, false);
615 return DecodeTag
.len
;
619 //=============================================================================
620 // An ISO15693 decoder for reader commands.
622 // This function is called 4 times per bit (every 2 subcarrier cycles).
623 // Subcarrier frequency fs is 848kHz, 1/fs = 1,18us, i.e. function is called every 2,36us
625 // LED B -> ON once we have received the SOF and are expecting the rest.
626 // LED B -> OFF once we have received EOF or are in error state or unsynced
628 // Returns: true if we received a EOF
629 // false if we are still waiting for some more
630 //=============================================================================
632 typedef struct DecodeReader
{
634 STATE_READER_UNSYNCD
,
635 STATE_READER_AWAIT_1ST_RISING_EDGE_OF_SOF
,
636 STATE_READER_AWAIT_2ND_FALLING_EDGE_OF_SOF
,
637 STATE_READER_AWAIT_2ND_RISING_EDGE_OF_SOF
,
638 STATE_READER_AWAIT_END_OF_SOF_1_OUT_OF_4
,
639 STATE_READER_RECEIVE_DATA_1_OUT_OF_4
,
640 STATE_READER_RECEIVE_DATA_1_OUT_OF_256
656 static void DecodeReaderInit(DecodeReader_t
* DecodeReader
, uint8_t *data
, uint16_t max_len
)
658 DecodeReader
->output
= data
;
659 DecodeReader
->byteCountMax
= max_len
;
660 DecodeReader
->state
= STATE_READER_UNSYNCD
;
661 DecodeReader
->byteCount
= 0;
662 DecodeReader
->bitCount
= 0;
663 DecodeReader
->posCount
= 1;
664 DecodeReader
->shiftReg
= 0;
668 static void DecodeReaderReset(DecodeReader_t
* DecodeReader
)
670 DecodeReader
->state
= STATE_READER_UNSYNCD
;
674 static int inline __attribute__((always_inline
)) Handle15693SampleFromReader(uint8_t bit
, DecodeReader_t
*restrict DecodeReader
)
676 switch(DecodeReader
->state
) {
677 case STATE_READER_UNSYNCD
:
679 // we went low, so this could be the beginning of a SOF
680 DecodeReader
->posCount
= 1;
681 DecodeReader
->state
= STATE_READER_AWAIT_1ST_RISING_EDGE_OF_SOF
;
685 case STATE_READER_AWAIT_1ST_RISING_EDGE_OF_SOF
:
686 DecodeReader
->posCount
++;
687 if(bit
) { // detected rising edge
688 if(DecodeReader
->posCount
< 4) { // rising edge too early (nominally expected at 5)
689 DecodeReaderReset(DecodeReader
);
691 DecodeReader
->state
= STATE_READER_AWAIT_2ND_FALLING_EDGE_OF_SOF
;
694 if(DecodeReader
->posCount
> 5) { // stayed low for too long
695 DecodeReaderReset(DecodeReader
);
697 // do nothing, keep waiting
702 case STATE_READER_AWAIT_2ND_FALLING_EDGE_OF_SOF
:
703 DecodeReader
->posCount
++;
704 if(!bit
) { // detected a falling edge
705 if (DecodeReader
->posCount
< 20) { // falling edge too early (nominally expected at 21 earliest)
706 DecodeReaderReset(DecodeReader
);
707 } else if (DecodeReader
->posCount
< 23) { // SOF for 1 out of 4 coding
708 DecodeReader
->Coding
= CODING_1_OUT_OF_4
;
709 DecodeReader
->state
= STATE_READER_AWAIT_2ND_RISING_EDGE_OF_SOF
;
710 } else if (DecodeReader
->posCount
< 28) { // falling edge too early (nominally expected at 29 latest)
711 DecodeReaderReset(DecodeReader
);
712 } else { // SOF for 1 out of 4 coding
713 DecodeReader
->Coding
= CODING_1_OUT_OF_256
;
714 DecodeReader
->state
= STATE_READER_AWAIT_2ND_RISING_EDGE_OF_SOF
;
717 if(DecodeReader
->posCount
> 29) { // stayed high for too long
718 DecodeReaderReset(DecodeReader
);
720 // do nothing, keep waiting
725 case STATE_READER_AWAIT_2ND_RISING_EDGE_OF_SOF
:
726 DecodeReader
->posCount
++;
727 if (bit
) { // detected rising edge
728 if (DecodeReader
->Coding
== CODING_1_OUT_OF_256
) {
729 if (DecodeReader
->posCount
< 32) { // rising edge too early (nominally expected at 33)
730 DecodeReaderReset(DecodeReader
);
732 DecodeReader
->posCount
= 1;
733 DecodeReader
->bitCount
= 0;
734 DecodeReader
->byteCount
= 0;
735 DecodeReader
->sum1
= 1;
736 DecodeReader
->state
= STATE_READER_RECEIVE_DATA_1_OUT_OF_256
;
739 } else { // CODING_1_OUT_OF_4
740 if (DecodeReader
->posCount
< 24) { // rising edge too early (nominally expected at 25)
741 DecodeReaderReset(DecodeReader
);
743 DecodeReader
->state
= STATE_READER_AWAIT_END_OF_SOF_1_OUT_OF_4
;
747 if (DecodeReader
->Coding
== CODING_1_OUT_OF_256
) {
748 if (DecodeReader
->posCount
> 34) { // signal stayed low for too long
749 DecodeReaderReset(DecodeReader
);
751 // do nothing, keep waiting
753 } else { // CODING_1_OUT_OF_4
754 if (DecodeReader
->posCount
> 26) { // signal stayed low for too long
755 DecodeReaderReset(DecodeReader
);
757 // do nothing, keep waiting
763 case STATE_READER_AWAIT_END_OF_SOF_1_OUT_OF_4
:
764 DecodeReader
->posCount
++;
766 if (DecodeReader
->posCount
== 33) {
767 DecodeReader
->posCount
= 1;
768 DecodeReader
->bitCount
= 0;
769 DecodeReader
->byteCount
= 0;
770 DecodeReader
->sum1
= 1;
771 DecodeReader
->state
= STATE_READER_RECEIVE_DATA_1_OUT_OF_4
;
774 // do nothing, keep waiting
776 } else { // unexpected falling edge
777 DecodeReaderReset(DecodeReader
);
781 case STATE_READER_RECEIVE_DATA_1_OUT_OF_4
:
782 DecodeReader
->posCount
++;
783 if (DecodeReader
->posCount
== 1) {
784 DecodeReader
->sum1
= bit
;
785 } else if (DecodeReader
->posCount
<= 4) {
786 DecodeReader
->sum1
+= bit
;
787 } else if (DecodeReader
->posCount
== 5) {
788 DecodeReader
->sum2
= bit
;
790 DecodeReader
->sum2
+= bit
;
792 if (DecodeReader
->posCount
== 8) {
793 DecodeReader
->posCount
= 0;
794 int corr10
= DecodeReader
->sum1
- DecodeReader
->sum2
;
795 int corr01
= DecodeReader
->sum2
- DecodeReader
->sum1
;
796 int corr11
= (DecodeReader
->sum1
+ DecodeReader
->sum2
) / 2;
797 if (corr01
> corr11
&& corr01
> corr10
) { // EOF
798 LED_B_OFF(); // Finished receiving
799 DecodeReaderReset(DecodeReader
);
800 if (DecodeReader
->byteCount
!= 0) {
804 if (corr10
> corr11
) { // detected a 2bit position
805 DecodeReader
->shiftReg
>>= 2;
806 DecodeReader
->shiftReg
|= (DecodeReader
->bitCount
<< 6);
808 if (DecodeReader
->bitCount
== 15) { // we have a full byte
809 DecodeReader
->output
[DecodeReader
->byteCount
++] = DecodeReader
->shiftReg
;
810 if (DecodeReader
->byteCount
> DecodeReader
->byteCountMax
) {
811 // buffer overflow, give up
813 DecodeReaderReset(DecodeReader
);
815 DecodeReader
->bitCount
= 0;
816 DecodeReader
->shiftReg
= 0;
818 DecodeReader
->bitCount
++;
823 case STATE_READER_RECEIVE_DATA_1_OUT_OF_256
:
824 DecodeReader
->posCount
++;
825 if (DecodeReader
->posCount
== 1) {
826 DecodeReader
->sum1
= bit
;
827 } else if (DecodeReader
->posCount
<= 4) {
828 DecodeReader
->sum1
+= bit
;
829 } else if (DecodeReader
->posCount
== 5) {
830 DecodeReader
->sum2
= bit
;
832 DecodeReader
->sum2
+= bit
;
834 if (DecodeReader
->posCount
== 8) {
835 DecodeReader
->posCount
= 0;
836 int corr10
= DecodeReader
->sum1
- DecodeReader
->sum2
;
837 int corr01
= DecodeReader
->sum2
- DecodeReader
->sum1
;
838 int corr11
= (DecodeReader
->sum1
+ DecodeReader
->sum2
) / 2;
839 if (corr01
> corr11
&& corr01
> corr10
) { // EOF
840 LED_B_OFF(); // Finished receiving
841 DecodeReaderReset(DecodeReader
);
842 if (DecodeReader
->byteCount
!= 0) {
846 if (corr10
> corr11
) { // detected the bit position
847 DecodeReader
->shiftReg
= DecodeReader
->bitCount
;
849 if (DecodeReader
->bitCount
== 255) { // we have a full byte
850 DecodeReader
->output
[DecodeReader
->byteCount
++] = DecodeReader
->shiftReg
;
851 if (DecodeReader
->byteCount
> DecodeReader
->byteCountMax
) {
852 // buffer overflow, give up
854 DecodeReaderReset(DecodeReader
);
857 DecodeReader
->bitCount
++;
863 DecodeReaderReset(DecodeReader
);
871 //-----------------------------------------------------------------------------
872 // Receive a command (from the reader to us, where we are the simulated tag),
873 // and store it in the given buffer, up to the given maximum length. Keeps
874 // spinning, waiting for a well-framed command, until either we get one
875 // (returns true) or someone presses the pushbutton on the board (false).
877 // Assume that we're called with the SSC (to the FPGA) and ADC path set
879 //-----------------------------------------------------------------------------
881 static int GetIso15693CommandFromReader(uint8_t *received
, size_t max_len
, uint32_t *eof_time
)
884 bool gotFrame
= false;
887 uint8_t *dmaBuf
= BigBuf_malloc(ISO15693_DMA_BUFFER_SIZE
);
889 // the decoder data structure
890 DecodeReader_t DecodeReader
= {0};
891 DecodeReaderInit(&DecodeReader
, received
, max_len
);
893 // wait for last transfer to complete
894 while (!(AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXEMPTY
));
897 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
| FPGA_HF_SIMULATOR_NO_MODULATION
);
899 // clear receive register and wait for next transfer
900 uint32_t temp
= AT91C_BASE_SSC
->SSC_RHR
;
902 while (!(AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_RXRDY
)) ;
904 uint32_t bit_time
= GetCountSspClk() & 0xfffffff8;
906 // Setup and start DMA.
907 FpgaSetupSscDma(dmaBuf
, ISO15693_DMA_BUFFER_SIZE
);
908 uint8_t *upTo
= dmaBuf
;
911 uint16_t behindBy
= ((uint8_t*)AT91C_BASE_PDC_SSC
->PDC_RPR
- upTo
) & (ISO15693_DMA_BUFFER_SIZE
-1);
913 if (behindBy
== 0) continue;
916 if(upTo
>= dmaBuf
+ ISO15693_DMA_BUFFER_SIZE
) { // we have read all of the DMA buffer content.
917 upTo
= dmaBuf
; // start reading the circular buffer from the beginning
918 if(behindBy
> (9*ISO15693_DMA_BUFFER_SIZE
/10)) {
919 Dbprintf("About to blow circular buffer - aborted! behindBy=%d", behindBy
);
923 if (AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_ENDRX
)) { // DMA Counter Register had reached 0, already rotated.
924 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (uint32_t) dmaBuf
; // refresh the DMA Next Buffer and
925 AT91C_BASE_PDC_SSC
->PDC_RNCR
= ISO15693_DMA_BUFFER_SIZE
; // DMA Next Counter registers
928 for (int i
= 7; i
>= 0; i
--) {
929 if (Handle15693SampleFromReader((b
>> i
) & 0x01, &DecodeReader
)) {
930 *eof_time
= bit_time
+ samples
- DELAY_READER_TO_ARM
; // end of EOF
941 if (BUTTON_PRESS()) {
942 DecodeReader
.byteCount
= 0;
951 BigBuf_free_keep_EM();
953 if (DEBUG
) Dbprintf("samples = %d, gotFrame = %d, Decoder: state = %d, len = %d, bitCount = %d, posCount = %d",
954 samples
, gotFrame
, DecodeReader
.state
, DecodeReader
.byteCount
, DecodeReader
.bitCount
, DecodeReader
.posCount
);
956 if (DecodeReader
.byteCount
> 0) {
957 LogTrace(DecodeReader
.output
, DecodeReader
.byteCount
, 0, 0, NULL
, true);
960 return DecodeReader
.byteCount
;
964 // Encode (into the ToSend buffers) an identify request, which is the first
965 // thing that you must send to a tag to get a response.
966 static void BuildIdentifyRequest(void)
971 // one sub-carrier, inventory, 1 slot, fast rate
972 // AFI is at bit 5 (1<<4) when doing an INVENTORY
973 cmd
[0] = (1 << 2) | (1 << 5) | (1 << 1);
974 // inventory command code
983 CodeIso15693AsReader(cmd
, sizeof(cmd
));
987 //-----------------------------------------------------------------------------
988 // Start to read an ISO 15693 tag. We send an identify request, then wait
989 // for the response. The response is not demodulated, just left in the buffer
990 // so that it can be downloaded to a PC and processed there.
991 //-----------------------------------------------------------------------------
992 void AcquireRawAdcSamplesIso15693(void)
997 uint8_t *dest
= BigBuf_get_addr();
999 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1000 BuildIdentifyRequest();
1002 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1004 // Give the tags time to energize
1006 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
1009 // Now send the command
1010 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_TX
);
1011 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX
);
1014 for(int c
= 0; c
< ToSendMax
; ) {
1015 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
1016 AT91C_BASE_SSC
->SSC_THR
= ~ToSend
[c
];
1023 // wait for last transfer to complete
1024 while (!(AT91C_BASE_SSC
->SSC_SR
& AT91C_SSC_TXEMPTY
));
1026 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
1027 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
| FPGA_HF_READER_RX_XCORR_AMPLITUDE
);
1029 for(int c
= 0; c
< 4000; ) {
1030 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
1031 uint16_t r
= AT91C_BASE_SSC
->SSC_RHR
;
1036 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1041 void SnoopIso15693(void)
1044 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1051 // The DMA buffer, used to stream samples from the FPGA
1052 uint16_t* dmaBuf
= (uint16_t*)BigBuf_malloc(ISO15693_DMA_BUFFER_SIZE
*sizeof(uint16_t));
1055 // Count of samples received so far, so that we can include timing
1056 // information in the trace buffer.
1059 DecodeTag_t DecodeTag
= {0};
1060 uint8_t response
[ISO15693_MAX_RESPONSE_LENGTH
];
1061 DecodeTagInit(&DecodeTag
, response
, sizeof(response
));
1063 DecodeReader_t DecodeReader
= {0};;
1064 uint8_t cmd
[ISO15693_MAX_COMMAND_LENGTH
];
1065 DecodeReaderInit(&DecodeReader
, cmd
, sizeof(cmd
));
1067 // Print some debug information about the buffer sizes
1069 Dbprintf("Snooping buffers initialized:");
1070 Dbprintf(" Trace: %i bytes", BigBuf_max_traceLen());
1071 Dbprintf(" Reader -> tag: %i bytes", ISO15693_MAX_COMMAND_LENGTH
);
1072 Dbprintf(" tag -> Reader: %i bytes", ISO15693_MAX_RESPONSE_LENGTH
);
1073 Dbprintf(" DMA: %i bytes", ISO15693_DMA_BUFFER_SIZE
* sizeof(uint16_t));
1075 Dbprintf("Snoop started. Press button to stop.");
1077 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
| FPGA_HF_READER_RX_XCORR_SNOOP
| FPGA_HF_READER_RX_XCORR_AMPLITUDE
);
1078 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1080 // Setup for the DMA.
1081 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
1083 FpgaSetupSscDma((uint8_t*) dmaBuf
, ISO15693_DMA_BUFFER_SIZE
);
1085 bool TagIsActive
= false;
1086 bool ReaderIsActive
= false;
1087 bool ExpectTagAnswer
= false;
1089 // And now we loop, receiving samples.
1091 uint16_t behindBy
= ((uint16_t*)AT91C_BASE_PDC_SSC
->PDC_RPR
- upTo
) & (ISO15693_DMA_BUFFER_SIZE
-1);
1093 if (behindBy
== 0) continue;
1095 uint16_t snoopdata
= *upTo
++;
1097 if(upTo
>= dmaBuf
+ ISO15693_DMA_BUFFER_SIZE
) { // we have read all of the DMA buffer content.
1098 upTo
= dmaBuf
; // start reading the circular buffer from the beginning
1099 if(behindBy
> (9*ISO15693_DMA_BUFFER_SIZE
/10)) {
1100 Dbprintf("About to blow circular buffer - aborted! behindBy=%d, samples=%d", behindBy
, samples
);
1103 if (AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_ENDRX
)) { // DMA Counter Register had reached 0, already rotated.
1104 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (uint32_t) dmaBuf
; // refresh the DMA Next Buffer and
1105 AT91C_BASE_PDC_SSC
->PDC_RNCR
= ISO15693_DMA_BUFFER_SIZE
; // DMA Next Counter registers
1107 if(BUTTON_PRESS()) {
1108 DbpString("Snoop stopped.");
1115 if (!TagIsActive
) { // no need to try decoding reader data if the tag is sending
1116 if (Handle15693SampleFromReader(snoopdata
& 0x02, &DecodeReader
)) {
1117 FpgaDisableSscDma();
1118 ExpectTagAnswer
= true;
1119 LogTrace(DecodeReader
.output
, DecodeReader
.byteCount
, samples
, samples
, NULL
, true);
1120 /* And ready to receive another command. */
1121 DecodeReaderReset(&DecodeReader
);
1122 /* And also reset the demod code, which might have been */
1123 /* false-triggered by the commands from the reader. */
1124 DecodeTagReset(&DecodeTag
);
1126 FpgaSetupSscDma((uint8_t*) dmaBuf
, ISO15693_DMA_BUFFER_SIZE
);
1128 if (Handle15693SampleFromReader(snoopdata
& 0x01, &DecodeReader
)) {
1129 FpgaDisableSscDma();
1130 ExpectTagAnswer
= true;
1131 LogTrace(DecodeReader
.output
, DecodeReader
.byteCount
, samples
, samples
, NULL
, true);
1132 /* And ready to receive another command. */
1133 DecodeReaderReset(&DecodeReader
);
1134 /* And also reset the demod code, which might have been */
1135 /* false-triggered by the commands from the reader. */
1136 DecodeTagReset(&DecodeTag
);
1138 FpgaSetupSscDma((uint8_t*) dmaBuf
, ISO15693_DMA_BUFFER_SIZE
);
1140 ReaderIsActive
= (DecodeReader
.state
>= STATE_READER_AWAIT_2ND_RISING_EDGE_OF_SOF
);
1143 if (!ReaderIsActive
&& ExpectTagAnswer
) { // no need to try decoding tag data if the reader is currently sending or no answer expected yet
1144 if (Handle15693SamplesFromTag(snoopdata
>> 2, &DecodeTag
)) {
1145 FpgaDisableSscDma();
1146 //Use samples as a time measurement
1147 LogTrace(DecodeTag
.output
, DecodeTag
.len
, samples
, samples
, NULL
, false);
1148 // And ready to receive another response.
1149 DecodeTagReset(&DecodeTag
);
1150 DecodeReaderReset(&DecodeReader
);
1151 ExpectTagAnswer
= false;
1153 FpgaSetupSscDma((uint8_t*) dmaBuf
, ISO15693_DMA_BUFFER_SIZE
);
1155 TagIsActive
= (DecodeTag
.state
>= STATE_TAG_RECEIVING_DATA
);
1160 FpgaDisableSscDma();
1165 DbpString("Snoop statistics:");
1166 Dbprintf(" ExpectTagAnswer: %d", ExpectTagAnswer
);
1167 Dbprintf(" DecodeTag State: %d", DecodeTag
.state
);
1168 Dbprintf(" DecodeTag byteCnt: %d", DecodeTag
.len
);
1169 Dbprintf(" DecodeReader State: %d", DecodeReader
.state
);
1170 Dbprintf(" DecodeReader byteCnt: %d", DecodeReader
.byteCount
);
1171 Dbprintf(" Trace length: %d", BigBuf_get_traceLen());
1175 // Initialize the proxmark as iso15k reader
1176 // (this might produces glitches that confuse some tags
1177 static void Iso15693InitReader() {
1178 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1182 // Start from off (no field generated)
1184 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1187 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1188 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
1190 // Give the tags time to energize
1192 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
1196 ///////////////////////////////////////////////////////////////////////
1197 // ISO 15693 Part 3 - Air Interface
1198 // This section basically contains transmission and receiving of bits
1199 ///////////////////////////////////////////////////////////////////////
1202 // uid is in transmission order (which is reverse of display order)
1203 static void BuildReadBlockRequest(uint8_t *uid
, uint8_t blockNumber
)
1208 // If we set the Option_Flag in this request, the VICC will respond with the security status of the block
1209 // followed by the block data
1210 cmd
[0] = ISO15693_REQ_OPTION
| ISO15693_REQ_ADDRESS
| ISO15693_REQ_DATARATE_HIGH
;
1211 // READ BLOCK command code
1212 cmd
[1] = ISO15693_READBLOCK
;
1213 // UID may be optionally specified here
1222 cmd
[9] = uid
[7]; // 0xe0; // always e0 (not exactly unique)
1223 // Block number to read
1224 cmd
[10] = blockNumber
;
1226 crc
= Crc(cmd
, 11); // the crc needs to be calculated over 11 bytes
1227 cmd
[11] = crc
& 0xff;
1230 CodeIso15693AsReader(cmd
, sizeof(cmd
));
1234 // Now the VICC>VCD responses when we are simulating a tag
1235 static void BuildInventoryResponse(uint8_t *uid
)
1241 cmd
[0] = 0; // No error, no protocol format extension
1242 cmd
[1] = 0; // DSFID (data storage format identifier). 0x00 = not supported
1244 cmd
[2] = uid
[7]; //0x32;
1245 cmd
[3] = uid
[6]; //0x4b;
1246 cmd
[4] = uid
[5]; //0x03;
1247 cmd
[5] = uid
[4]; //0x01;
1248 cmd
[6] = uid
[3]; //0x00;
1249 cmd
[7] = uid
[2]; //0x10;
1250 cmd
[8] = uid
[1]; //0x05;
1251 cmd
[9] = uid
[0]; //0xe0;
1254 cmd
[10] = crc
& 0xff;
1257 CodeIso15693AsTag(cmd
, sizeof(cmd
));
1260 // Universal Method for sending to and recv bytes from a tag
1261 // init ... should we initialize the reader?
1262 // speed ... 0 low speed, 1 hi speed
1263 // *recv will contain the tag's answer
1264 // return: lenght of received data
1265 int SendDataTag(uint8_t *send
, int sendlen
, bool init
, int speed
, uint8_t *recv
, uint16_t max_recv_len
, uint32_t start_time
) {
1271 if (init
) Iso15693InitReader();
1276 // low speed (1 out of 256)
1277 CodeIso15693AsReader256(send
, sendlen
);
1279 // high speed (1 out of 4)
1280 CodeIso15693AsReader(send
, sendlen
);
1283 TransmitTo15693Tag(ToSend
, ToSendMax
, start_time
);
1285 // Now wait for a response
1287 answerLen
= GetIso15693AnswerFromTag(recv
, max_recv_len
, DELAY_ISO15693_VCD_TO_VICC
* 2);
1296 // --------------------------------------------------------------------
1298 // --------------------------------------------------------------------
1300 // Decodes a message from a tag and displays its metadata and content
1301 #define DBD15STATLEN 48
1302 void DbdecodeIso15693Answer(int len
, uint8_t *d
) {
1303 char status
[DBD15STATLEN
+1]={0};
1307 if (d
[0] & ISO15693_RES_EXT
)
1308 strncat(status
,"ProtExt ", DBD15STATLEN
);
1309 if (d
[0] & ISO15693_RES_ERROR
) {
1311 strncat(status
,"Error ", DBD15STATLEN
);
1314 strncat(status
,"01:notSupp", DBD15STATLEN
);
1317 strncat(status
,"02:notRecog", DBD15STATLEN
);
1320 strncat(status
,"03:optNotSupp", DBD15STATLEN
);
1323 strncat(status
,"0f:noInfo", DBD15STATLEN
);
1326 strncat(status
,"10:doesn'tExist", DBD15STATLEN
);
1329 strncat(status
,"11:lockAgain", DBD15STATLEN
);
1332 strncat(status
,"12:locked", DBD15STATLEN
);
1335 strncat(status
,"13:progErr", DBD15STATLEN
);
1338 strncat(status
,"14:lockErr", DBD15STATLEN
);
1341 strncat(status
,"unknownErr", DBD15STATLEN
);
1343 strncat(status
," ", DBD15STATLEN
);
1345 strncat(status
,"NoErr ", DBD15STATLEN
);
1349 if ( (( crc
& 0xff ) == d
[len
-2]) && (( crc
>> 8 ) == d
[len
-1]) )
1350 strncat(status
,"CrcOK",DBD15STATLEN
);
1352 strncat(status
,"CrcFail!",DBD15STATLEN
);
1354 Dbprintf("%s",status
);
1360 ///////////////////////////////////////////////////////////////////////
1361 // Functions called via USB/Client
1362 ///////////////////////////////////////////////////////////////////////
1364 void SetDebugIso15693(uint32_t debug
) {
1366 Dbprintf("Iso15693 Debug is now %s",DEBUG
?"on":"off");
1371 //-----------------------------------------------------------------------------
1372 // Simulate an ISO15693 reader, perform anti-collision and then attempt to read a sector
1373 // all demodulation performed in arm rather than host. - greg
1374 //-----------------------------------------------------------------------------
1375 void ReaderIso15693(uint32_t parameter
)
1383 uint8_t TagUID
[8] = {0x00};
1385 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1387 uint8_t answer
[ISO15693_MAX_RESPONSE_LENGTH
];
1389 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1391 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
1393 // Start from off (no field generated)
1394 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1397 // Give the tags time to energize
1399 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR
);
1404 // FIRST WE RUN AN INVENTORY TO GET THE TAG UID
1405 // THIS MEANS WE CAN PRE-BUILD REQUESTS TO SAVE CPU TIME
1407 // Now send the IDENTIFY command
1408 BuildIdentifyRequest();
1409 TransmitTo15693Tag(ToSend
, ToSendMax
, 0);
1411 // Now wait for a response
1412 answerLen
= GetIso15693AnswerFromTag(answer
, sizeof(answer
), DELAY_ISO15693_VCD_TO_VICC
* 2) ;
1413 uint32_t start_time
= GetCountSspClk() + DELAY_ISO15693_VICC_TO_VCD
;
1415 if (answerLen
>=12) // we should do a better check than this
1417 TagUID
[0] = answer
[2];
1418 TagUID
[1] = answer
[3];
1419 TagUID
[2] = answer
[4];
1420 TagUID
[3] = answer
[5];
1421 TagUID
[4] = answer
[6];
1422 TagUID
[5] = answer
[7];
1423 TagUID
[6] = answer
[8]; // IC Manufacturer code
1424 TagUID
[7] = answer
[9]; // always E0
1428 Dbprintf("%d octets read from IDENTIFY request:", answerLen
);
1429 DbdecodeIso15693Answer(answerLen
, answer
);
1430 Dbhexdump(answerLen
, answer
, false);
1433 if (answerLen
>= 12)
1434 Dbprintf("UID = %02hX%02hX%02hX%02hX%02hX%02hX%02hX%02hX",
1435 TagUID
[7],TagUID
[6],TagUID
[5],TagUID
[4],
1436 TagUID
[3],TagUID
[2],TagUID
[1],TagUID
[0]);
1439 // Dbprintf("%d octets read from SELECT request:", answerLen2);
1440 // DbdecodeIso15693Answer(answerLen2,answer2);
1441 // Dbhexdump(answerLen2,answer2,true);
1443 // Dbprintf("%d octets read from XXX request:", answerLen3);
1444 // DbdecodeIso15693Answer(answerLen3,answer3);
1445 // Dbhexdump(answerLen3,answer3,true);
1448 if (answerLen
>= 12 && DEBUG
) {
1450 // debugptr = BigBuf_get_addr();
1453 while (i
< 32) { // sanity check, assume max 32 pages
1454 BuildReadBlockRequest(TagUID
, i
);
1455 TransmitTo15693Tag(ToSend
, ToSendMax
, start_time
);
1456 int answerLen
= GetIso15693AnswerFromTag(answer
, sizeof(answer
), DELAY_ISO15693_VCD_TO_VICC
* 2);
1457 start_time
= GetCountSspClk() + DELAY_ISO15693_VICC_TO_VCD
;
1458 if (answerLen
> 0) {
1459 Dbprintf("READ SINGLE BLOCK %d returned %d octets:", i
, answerLen
);
1460 DbdecodeIso15693Answer(answerLen
, answer
);
1461 Dbhexdump(answerLen
, answer
, false);
1462 if ( *((uint32_t*) answer
) == 0x07160101 ) break; // exit on NoPageErr
1468 // for the time being, switch field off to protect rdv4.0
1469 // note: this prevents using hf 15 cmd with s option - which isn't implemented yet anyway
1470 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1477 // Simulate an ISO15693 TAG.
1478 // For Inventory command: print command and send Inventory Response with given UID
1479 // TODO: interpret other reader commands and send appropriate response
1480 void SimTagIso15693(uint32_t parameter
, uint8_t *uid
)
1485 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1486 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1487 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR
| FPGA_HF_SIMULATOR_NO_MODULATION
);
1488 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_SIMULATOR
);
1492 uint8_t cmd
[ISO15693_MAX_COMMAND_LENGTH
];
1494 // Build a suitable response to the reader INVENTORY command
1495 BuildInventoryResponse(uid
);
1498 while (!BUTTON_PRESS()) {
1499 uint32_t eof_time
= 0, start_time
= 0;
1500 int cmd_len
= GetIso15693CommandFromReader(cmd
, sizeof(cmd
), &eof_time
);
1502 if ((cmd_len
>= 5) && (cmd
[0] & ISO15693_REQ_INVENTORY
) && (cmd
[1] == ISO15693_INVENTORY
)) { // TODO: check more flags
1503 bool slow
= !(cmd
[0] & ISO15693_REQ_DATARATE_HIGH
);
1504 start_time
= eof_time
+ DELAY_ISO15693_VCD_TO_VICC
- DELAY_ARM_TO_READER
;
1505 TransmitTo15693Reader(ToSend
, ToSendMax
, start_time
, slow
);
1508 Dbprintf("%d bytes read from reader:", cmd_len
);
1509 Dbhexdump(cmd_len
, cmd
, false);
1516 // Since there is no standardized way of reading the AFI out of a tag, we will brute force it
1517 // (some manufactures offer a way to read the AFI, though)
1518 void BruteforceIso15693Afi(uint32_t speed
)
1524 uint8_t recv
[ISO15693_MAX_RESPONSE_LENGTH
];
1526 int datalen
=0, recvlen
=0;
1528 Iso15693InitReader();
1531 // first without AFI
1532 // Tags should respond without AFI and with AFI=0 even when AFI is active
1534 data
[0] = ISO15693_REQ_DATARATE_HIGH
| ISO15693_REQ_INVENTORY
| ISO15693_REQINV_SLOT1
;
1535 data
[1] = ISO15693_INVENTORY
;
1536 data
[2] = 0; // mask length
1537 datalen
= AddCrc(data
,3);
1538 recvlen
= SendDataTag(data
, datalen
, false, speed
, recv
, sizeof(recv
), 0);
1539 uint32_t start_time
= GetCountSspClk() + DELAY_ISO15693_VCD_TO_VICC
;
1542 Dbprintf("NoAFI UID=%s", sprintUID(NULL
, &recv
[2]));
1547 data
[0] = ISO15693_REQ_DATARATE_HIGH
| ISO15693_REQ_INVENTORY
| ISO15693_REQINV_AFI
| ISO15693_REQINV_SLOT1
;
1548 data
[1] = ISO15693_INVENTORY
;
1550 data
[3] = 0; // mask length
1552 for (int i
= 0; i
< 256; i
++) {
1554 datalen
= AddCrc(data
,4);
1555 recvlen
= SendDataTag(data
, datalen
, false, speed
, recv
, sizeof(recv
), start_time
);
1556 start_time
= GetCountSspClk() + DELAY_ISO15693_VCD_TO_VICC
;
1558 if (recvlen
>= 12) {
1559 Dbprintf("AFI=%i UID=%s", i
, sprintUID(NULL
, &recv
[2]));
1562 Dbprintf("AFI Bruteforcing done.");
1564 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1568 // Allows to directly send commands to the tag via the client
1569 void DirectTag15693Command(uint32_t datalen
, uint32_t speed
, uint32_t recv
, uint8_t data
[]) {
1572 uint8_t recvbuf
[ISO15693_MAX_RESPONSE_LENGTH
];
1578 Dbhexdump(datalen
, data
, false);
1581 recvlen
= SendDataTag(data
, datalen
, true, speed
, (recv
?recvbuf
:NULL
), sizeof(recvbuf
), 0);
1586 Dbhexdump(recvlen
, recvbuf
, false);
1587 DbdecodeIso15693Answer(recvlen
, recvbuf
);
1590 cmd_send(CMD_ACK
, recvlen
>ISO15693_MAX_RESPONSE_LENGTH
?ISO15693_MAX_RESPONSE_LENGTH
:recvlen
, 0, 0, recvbuf
, ISO15693_MAX_RESPONSE_LENGTH
);
1594 // for the time being, switch field off to protect rdv4.0
1595 // note: this prevents using hf 15 cmd with s option - which isn't implemented yet anyway
1596 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1605 // --------------------------------------------------------------------
1606 // -- Misc & deprecated functions
1607 // --------------------------------------------------------------------
1611 // do not use; has a fix UID
1612 static void __attribute__((unused)) BuildSysInfoRequest(uint8_t *uid)
1617 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1618 // followed by teh block data
1619 // one sub-carrier, inventory, 1 slot, fast rate
1620 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1621 // System Information command code
1623 // UID may be optionally specified here
1632 cmd[9]= 0xe0; // always e0 (not exactly unique)
1634 crc = Crc(cmd, 10); // the crc needs to be calculated over 2 bytes
1635 cmd[10] = crc & 0xff;
1638 CodeIso15693AsReader(cmd, sizeof(cmd));
1642 // do not use; has a fix UID
1643 static void __attribute__((unused)) BuildReadMultiBlockRequest(uint8_t *uid)
1648 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1649 // followed by teh block data
1650 // one sub-carrier, inventory, 1 slot, fast rate
1651 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1652 // READ Multi BLOCK command code
1654 // UID may be optionally specified here
1663 cmd[9]= 0xe0; // always e0 (not exactly unique)
1664 // First Block number to read
1666 // Number of Blocks to read
1667 cmd[11] = 0x2f; // read quite a few
1669 crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes
1670 cmd[12] = crc & 0xff;
1673 CodeIso15693AsReader(cmd, sizeof(cmd));
1676 // do not use; has a fix UID
1677 static void __attribute__((unused)) BuildArbitraryRequest(uint8_t *uid,uint8_t CmdCode)
1682 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1683 // followed by teh block data
1684 // one sub-carrier, inventory, 1 slot, fast rate
1685 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1686 // READ BLOCK command code
1688 // UID may be optionally specified here
1697 cmd[9]= 0xe0; // always e0 (not exactly unique)
1703 // cmd[13] = 0x00; //Now the CRC
1704 crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes
1705 cmd[12] = crc & 0xff;
1708 CodeIso15693AsReader(cmd, sizeof(cmd));
1711 // do not use; has a fix UID
1712 static void __attribute__((unused)) BuildArbitraryCustomRequest(uint8_t uid[], uint8_t CmdCode)
1717 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1718 // followed by teh block data
1719 // one sub-carrier, inventory, 1 slot, fast rate
1720 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1721 // READ BLOCK command code
1723 // UID may be optionally specified here
1732 cmd[9]= 0xe0; // always e0 (not exactly unique)
1734 cmd[10] = 0x05; // for custom codes this must be manufcturer code
1738 // cmd[13] = 0x00; //Now the CRC
1739 crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes
1740 cmd[12] = crc & 0xff;
1743 CodeIso15693AsReader(cmd, sizeof(cmd));