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 // SSP_CLK runs at 13,56MHz / 32 = 423.75kHz when simulating a tag 
  85 #define DELAY_READER_TO_ARM_SIM           8 
  86 #define DELAY_ARM_TO_READER_SIM           1 
  87 #define DELAY_ISO15693_VCD_TO_VICC_SIM    132  // 132/423.75kHz = 311.5us from end of command EOF to start of tag response 
  88 //SSP_CLK runs at 13.56MHz / 4 = 3,39MHz when acting as reader 
  89 #define DELAY_ISO15693_VCD_TO_VICC_READER 1056 // 1056/3,39MHz = 311.5us from end of command EOF to start of tag response 
  90 #define DELAY_ISO15693_VICC_TO_VCD_READER 1017 // 1017/3.39MHz = 300us between end of tag response and next reader command 
  92 // --------------------------- 
  94 // --------------------------- 
  96 // prepare data using "1 out of 4" code for later transmission 
  97 // resulting data rate is 26.48 kbit/s (fc/512) 
  99 // n ... length of data 
 100 static void CodeIso15693AsReader(uint8_t *cmd
, int n
) 
 106         // Give it a bit of slack at the beginning 
 107         for(i 
= 0; i 
< 24; i
++) { 
 120         for(i 
= 0; i 
< n
; i
++) { 
 121                 for(j 
= 0; j 
< 8; j 
+= 2) { 
 122                         int these 
= (cmd
[i
] >> j
) & 3; 
 173         // Fill remainder of last byte with 1 
 174         for(i 
= 0; i 
< 4; i
++) { 
 181 // encode data using "1 out of 256" scheme 
 182 // data rate is 1,66 kbit/s (fc/8192) 
 183 // is designed for more robust communication over longer distances 
 184 static void CodeIso15693AsReader256(uint8_t *cmd
, int n
) 
 190         // Give it a bit of slack at the beginning 
 191         for(i 
= 0; i 
< 24; i
++) { 
 205         for(i 
= 0; i 
< n
; i
++) { 
 206                 for (j 
= 0; j
<=255; j
++) { 
 222         // Fill remainder of last byte with 1 
 223         for(i 
= 0; i 
< 4; i
++) { 
 231 static void CodeIso15693AsTag(uint8_t *cmd
, int n
) 
 246         for(int i 
= 0; i 
< n
; i
++) { 
 247                 for(int j 
= 0; j 
< 8; j
++) { 
 248                         if ((cmd
[i
] >> j
) & 0x01) { 
 272 // Transmit the command (to the tag) that was placed in cmd[]. 
 273 static void TransmitTo15693Tag(const uint8_t *cmd
, int len
, uint32_t start_time
) 
 275         FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER 
| FPGA_HF_READER_MODE_SEND_FULL_MOD
); 
 276         FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER
); 
 278         while (GetCountSspClk() < start_time
) ; 
 281         for(int c 
= 0; c 
< len
; c
++) { 
 282                 uint8_t data 
= cmd
[c
]; 
 283                 for (int i 
= 0; i 
< 8; i
++) { 
 284                         uint16_t send_word 
= (data 
& 0x80) ? 0x0000 : 0xffff; 
 285                         while (!(AT91C_BASE_SSC
->SSC_SR 
& (AT91C_SSC_TXRDY
))) ; 
 286                         AT91C_BASE_SSC
->SSC_THR 
= send_word
; 
 287                         while (!(AT91C_BASE_SSC
->SSC_SR 
& (AT91C_SSC_TXRDY
))) ; 
 288                         AT91C_BASE_SSC
->SSC_THR 
= send_word
; 
 297 //----------------------------------------------------------------------------- 
 298 // Transmit the tag response (to the reader) that was placed in cmd[]. 
 299 //----------------------------------------------------------------------------- 
 300 static void TransmitTo15693Reader(const uint8_t *cmd
, size_t len
, uint32_t start_time
, bool slow
) 
 302         // don't use the FPGA_HF_SIMULATOR_MODULATE_424K_8BIT minor mode. It would spoil GetCountSspClk() 
 303         FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR 
| FPGA_HF_SIMULATOR_MODULATE_424K
); 
 305         uint8_t shift_delay 
= start_time 
& 0x00000007; 
 306         uint8_t bitmask 
= 0x00; 
 307         for (int i 
= 0; i 
< shift_delay
; i
++) { 
 308                 bitmask 
|= (0x01 << i
); 
 311         while (GetCountSspClk() < (start_time 
& 0xfffffff8)) ; 
 313         AT91C_BASE_SSC
->SSC_THR 
= 0x00; // clear TXRDY 
 316         uint8_t bits_to_shift 
= 0x00; 
 317     for(size_t c 
= 0; c 
<= len
; c
++) { 
 318                 uint8_t bits_to_send 
= bits_to_shift 
<< (8 - shift_delay
) | (c
==len
?0x00:cmd
[c
]) >> shift_delay
; 
 319                 bits_to_shift 
= cmd
[c
] & bitmask
; 
 320                 for (int i 
= 7; i 
>= 0; i
--) { 
 321                         for (int j 
= 0; j 
< (slow
?4:1); ) { 
 322                                 if (AT91C_BASE_SSC
->SSC_SR 
& AT91C_SSC_TXRDY
) { 
 323                                         if (bits_to_send 
>> i 
& 0x01) { 
 324                                                 AT91C_BASE_SSC
->SSC_THR 
= 0xff; 
 326                                                 AT91C_BASE_SSC
->SSC_THR 
= 0x00; 
 338 //============================================================================= 
 339 // An ISO 15693 decoder for tag responses (one subcarrier only). 
 340 // Uses cross correlation to identify each bit and EOF. 
 341 // This function is called 8 times per bit (every 2 subcarrier cycles). 
 342 // Subcarrier frequency fs is 424kHz, 1/fs = 2,36us, 
 343 // i.e. function is called every 4,72us 
 345 //    LED C -> ON once we have received the SOF and are expecting the rest. 
 346 //    LED C -> OFF once we have received EOF or are unsynced 
 348 // Returns: true if we received a EOF 
 349 //          false if we are still waiting for some more 
 350 //============================================================================= 
 352 #define NOISE_THRESHOLD    160      // don't try to correlate noise 
 354 typedef struct DecodeTag 
{ 
 358                 STATE_TAG_SOF_HIGH_END
, 
 359                 STATE_TAG_RECEIVING_DATA
, 
 378 static int inline __attribute__((always_inline
)) Handle15693SamplesFromTag(uint16_t amplitude
, DecodeTag_t 
*DecodeTag
) 
 380         switch(DecodeTag
->state
) { 
 381                 case STATE_TAG_SOF_LOW
:  
 382                         // waiting for 12 times low (11 times low is accepted as well) 
 383                         if (amplitude 
< NOISE_THRESHOLD
) { 
 384                                 DecodeTag
->posCount
++; 
 386                                 if (DecodeTag
->posCount 
> 10) { 
 387                                         DecodeTag
->posCount 
= 1; 
 389                                         DecodeTag
->state 
= STATE_TAG_SOF_HIGH
; 
 391                                         DecodeTag
->posCount 
= 0; 
 396                 case STATE_TAG_SOF_HIGH
: 
 397                         // waiting for 10 times high. Take average over the last 8 
 398                         if (amplitude 
> NOISE_THRESHOLD
) { 
 399                                 DecodeTag
->posCount
++; 
 400                                 if (DecodeTag
->posCount 
> 2) { 
 401                                         DecodeTag
->sum1 
+= amplitude
; // keep track of average high value 
 403                                 if (DecodeTag
->posCount 
== 10) { 
 404                                         DecodeTag
->sum1 
>>= 4;        // calculate half of average high value (8 samples) 
 405                                         DecodeTag
->state 
= STATE_TAG_SOF_HIGH_END
; 
 407                         } else { // high phase was too short 
 408                                 DecodeTag
->posCount 
= 1; 
 409                                 DecodeTag
->state 
= STATE_TAG_SOF_LOW
; 
 413                 case STATE_TAG_SOF_HIGH_END
: 
 414                         // waiting for a falling edge 
 415                         if (amplitude 
< DecodeTag
->sum1
) {   // signal drops below 50% average high: a falling edge 
 416                                 DecodeTag
->lastBit 
= SOF_PART1
;  // detected 1st part of SOF (12 samples low and 12 samples high) 
 417                                 DecodeTag
->shiftReg 
= 0; 
 418                                 DecodeTag
->bitCount 
= 0; 
 420                                 DecodeTag
->sum1 
= amplitude
; 
 422                                 DecodeTag
->posCount 
= 2; 
 423                                 DecodeTag
->state 
= STATE_TAG_RECEIVING_DATA
; 
 426                                 DecodeTag
->posCount
++; 
 427                                 if (DecodeTag
->posCount 
> 13) { // high phase too long 
 428                                         DecodeTag
->posCount 
= 0; 
 429                                         DecodeTag
->state 
= STATE_TAG_SOF_LOW
; 
 435                 case STATE_TAG_RECEIVING_DATA
: 
 436                         if (DecodeTag
->posCount 
== 1) { 
 440                         if (DecodeTag
->posCount 
<= 4) { 
 441                                 DecodeTag
->sum1 
+= amplitude
; 
 443                                 DecodeTag
->sum2 
+= amplitude
; 
 445                         if (DecodeTag
->posCount 
== 8) { 
 446                                 int32_t corr_1 
= DecodeTag
->sum2 
- DecodeTag
->sum1
; 
 447                                 int32_t corr_0 
= -corr_1
; 
 448                                 int32_t corr_EOF 
= (DecodeTag
->sum1 
+ DecodeTag
->sum2
) / 2; 
 449                                 if (corr_EOF 
> corr_0 
&& corr_EOF 
> corr_1
) { 
 450                                         if (DecodeTag
->lastBit 
== LOGIC0
) {  // this was already part of EOF 
 451                                                 DecodeTag
->state 
= STATE_TAG_EOF
; 
 453                                                 DecodeTag
->posCount 
= 0; 
 454                                                 DecodeTag
->state 
= STATE_TAG_SOF_LOW
; 
 457                                 } else if (corr_1 
> corr_0
) { 
 459                                         if (DecodeTag
->lastBit 
== SOF_PART1
) { // still part of SOF 
 460                                                 DecodeTag
->lastBit 
= SOF_PART2
;    // SOF completed 
 462                                                 DecodeTag
->lastBit 
= LOGIC1
; 
 463                                                 DecodeTag
->shiftReg 
>>= 1; 
 464                                                 DecodeTag
->shiftReg 
|= 0x80; 
 465                                                 DecodeTag
->bitCount
++; 
 466                                                 if (DecodeTag
->bitCount 
== 8) { 
 467                                                         DecodeTag
->output
[DecodeTag
->len
] = DecodeTag
->shiftReg
; 
 469                                                         if (DecodeTag
->len 
> DecodeTag
->max_len
) { 
 470                                                                 // buffer overflow, give up 
 471                                                                 DecodeTag
->posCount 
= 0; 
 472                                                                 DecodeTag
->state 
= STATE_TAG_SOF_LOW
; 
 475                                                         DecodeTag
->bitCount 
= 0; 
 476                                                         DecodeTag
->shiftReg 
= 0; 
 481                                         if (DecodeTag
->lastBit 
== SOF_PART1
) { // incomplete SOF 
 482                                                 DecodeTag
->posCount 
= 0; 
 483                                                 DecodeTag
->state 
= STATE_TAG_SOF_LOW
; 
 486                                                 DecodeTag
->lastBit 
= LOGIC0
; 
 487                                                 DecodeTag
->shiftReg 
>>= 1; 
 488                                                 DecodeTag
->bitCount
++; 
 489                                                 if (DecodeTag
->bitCount 
== 8) { 
 490                                                         DecodeTag
->output
[DecodeTag
->len
] = DecodeTag
->shiftReg
; 
 492                                                         if (DecodeTag
->len 
> DecodeTag
->max_len
) { 
 493                                                                 // buffer overflow, give up 
 494                                                                 DecodeTag
->posCount 
= 0; 
 495                                                                 DecodeTag
->state 
= STATE_TAG_SOF_LOW
; 
 498                                                         DecodeTag
->bitCount 
= 0; 
 499                                                         DecodeTag
->shiftReg 
= 0; 
 503                                 DecodeTag
->posCount 
= 0; 
 505                         DecodeTag
->posCount
++; 
 509                         if (DecodeTag
->posCount 
== 1) { 
 513                         if (DecodeTag
->posCount 
<= 4) { 
 514                                 DecodeTag
->sum1 
+= amplitude
; 
 516                                 DecodeTag
->sum2 
+= amplitude
; 
 518                         if (DecodeTag
->posCount 
== 8) { 
 519                                 int32_t corr_1 
= DecodeTag
->sum2 
- DecodeTag
->sum1
; 
 520                                 int32_t corr_0 
= -corr_1
; 
 521                                 int32_t corr_EOF 
= (DecodeTag
->sum1 
+ DecodeTag
->sum2
) / 2; 
 522                                 if (corr_EOF 
> corr_0 
|| corr_1 
> corr_0
) { 
 523                                         DecodeTag
->posCount 
= 0; 
 524                                         DecodeTag
->state 
= STATE_TAG_SOF_LOW
; 
 531                         DecodeTag
->posCount
++; 
 540 static void DecodeTagInit(DecodeTag_t 
*DecodeTag
, uint8_t *data
, uint16_t max_len
) 
 542         DecodeTag
->posCount 
= 0; 
 543         DecodeTag
->state 
= STATE_TAG_SOF_LOW
; 
 544         DecodeTag
->output 
= data
; 
 545         DecodeTag
->max_len 
= max_len
; 
 549 static void DecodeTagReset(DecodeTag_t 
*DecodeTag
) 
 551         DecodeTag
->posCount 
= 0; 
 552         DecodeTag
->state 
= STATE_TAG_SOF_LOW
; 
 557  *  Receive and decode the tag response, also log to tracebuffer 
 559 static int GetIso15693AnswerFromTag(uint8_t* response
, uint16_t max_len
, int timeout
) 
 562         bool gotFrame 
= false; 
 564         uint16_t *dmaBuf 
= (uint16_t*)BigBuf_malloc(ISO15693_DMA_BUFFER_SIZE
*sizeof(uint16_t)); 
 566         // the Decoder data structure 
 567         DecodeTag_t DecodeTag 
= { 0 }; 
 568         DecodeTagInit(&DecodeTag
, response
, max_len
); 
 570         // wait for last transfer to complete 
 571         while (!(AT91C_BASE_SSC
->SSC_SR 
& AT91C_SSC_TXEMPTY
)); 
 573         // And put the FPGA in the appropriate mode 
 574         FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER 
| FPGA_HF_READER_SUBCARRIER_424_KHZ 
| FPGA_HF_READER_MODE_RECEIVE_AMPLITUDE
); 
 576         // Setup and start DMA. 
 577         FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER
); 
 578         FpgaSetupSscDma((uint8_t*) dmaBuf
, ISO15693_DMA_BUFFER_SIZE
); 
 579         uint16_t *upTo 
= dmaBuf
; 
 582                 uint16_t behindBy 
= ((uint16_t*)AT91C_BASE_PDC_SSC
->PDC_RPR 
- upTo
) & (ISO15693_DMA_BUFFER_SIZE
-1); 
 584                 if (behindBy 
== 0) continue; 
 586                 uint16_t tagdata 
= *upTo
++; 
 588                 if(upTo 
>= dmaBuf 
+ ISO15693_DMA_BUFFER_SIZE
) {                // we have read all of the DMA buffer content. 
 589                         upTo 
= dmaBuf
;                                             // start reading the circular buffer from the beginning 
 590                         if(behindBy 
> (9*ISO15693_DMA_BUFFER_SIZE
/10)) { 
 591                                 Dbprintf("About to blow circular buffer - aborted! behindBy=%d", behindBy
); 
 595                 if (AT91C_BASE_SSC
->SSC_SR 
& (AT91C_SSC_ENDRX
)) {              // DMA Counter Register had reached 0, already rotated. 
 596                         AT91C_BASE_PDC_SSC
->PDC_RNPR 
= (uint32_t) dmaBuf
;          // refresh the DMA Next Buffer and 
 597                         AT91C_BASE_PDC_SSC
->PDC_RNCR 
= ISO15693_DMA_BUFFER_SIZE
;   // DMA Next Counter registers 
 602                 if (Handle15693SamplesFromTag(tagdata
, &DecodeTag
)) { 
 607                 if (samples 
> timeout 
&& DecodeTag
.state 
< STATE_TAG_RECEIVING_DATA
) { 
 617         if (DEBUG
) Dbprintf("samples = %d, gotFrame = %d, Decoder: state = %d, len = %d, bitCount = %d, posCount = %d", 
 618                             samples
, gotFrame
, DecodeTag
.state
, DecodeTag
.len
, DecodeTag
.bitCount
, DecodeTag
.posCount
); 
 620         if (DecodeTag
.len 
> 0) { 
 621                 LogTrace(DecodeTag
.output
, DecodeTag
.len
, 0, 0, NULL
, false); 
 624         return DecodeTag
.len
; 
 628 //============================================================================= 
 629 // An ISO15693 decoder for reader commands. 
 631 // This function is called 4 times per bit (every 2 subcarrier cycles). 
 632 // Subcarrier frequency fs is 848kHz, 1/fs = 1,18us, i.e. function is called every 2,36us 
 634 //    LED B -> ON once we have received the SOF and are expecting the rest. 
 635 //    LED B -> OFF once we have received EOF or are in error state or unsynced 
 637 // Returns: true  if we received a EOF 
 638 //          false if we are still waiting for some more 
 639 //============================================================================= 
 641 typedef struct DecodeReader 
{ 
 643                 STATE_READER_UNSYNCD
, 
 644                 STATE_READER_AWAIT_1ST_RISING_EDGE_OF_SOF
, 
 645                 STATE_READER_AWAIT_2ND_FALLING_EDGE_OF_SOF
, 
 646                 STATE_READER_AWAIT_2ND_RISING_EDGE_OF_SOF
, 
 647                 STATE_READER_AWAIT_END_OF_SOF_1_OUT_OF_4
, 
 648                 STATE_READER_RECEIVE_DATA_1_OUT_OF_4
, 
 649                 STATE_READER_RECEIVE_DATA_1_OUT_OF_256
 
 665 static void DecodeReaderInit(DecodeReader_t
* DecodeReader
, uint8_t *data
, uint16_t max_len
) 
 667         DecodeReader
->output 
= data
; 
 668         DecodeReader
->byteCountMax 
= max_len
; 
 669         DecodeReader
->state 
= STATE_READER_UNSYNCD
; 
 670         DecodeReader
->byteCount 
= 0; 
 671         DecodeReader
->bitCount 
= 0; 
 672         DecodeReader
->posCount 
= 1; 
 673         DecodeReader
->shiftReg 
= 0; 
 677 static void DecodeReaderReset(DecodeReader_t
* DecodeReader
) 
 679         DecodeReader
->state 
= STATE_READER_UNSYNCD
; 
 683 static int inline __attribute__((always_inline
)) Handle15693SampleFromReader(uint8_t bit
, DecodeReader_t 
*restrict DecodeReader
) 
 685         switch(DecodeReader
->state
) { 
 686                 case STATE_READER_UNSYNCD
: 
 688                                 // we went low, so this could be the beginning of a SOF 
 689                                 DecodeReader
->posCount 
= 1; 
 690                                 DecodeReader
->state 
= STATE_READER_AWAIT_1ST_RISING_EDGE_OF_SOF
; 
 694                 case STATE_READER_AWAIT_1ST_RISING_EDGE_OF_SOF
: 
 695                         DecodeReader
->posCount
++; 
 696                         if(bit
) { // detected rising edge 
 697                                 if(DecodeReader
->posCount 
< 4) { // rising edge too early (nominally expected at 5) 
 698                                         DecodeReaderReset(DecodeReader
); 
 700                                         DecodeReader
->state 
= STATE_READER_AWAIT_2ND_FALLING_EDGE_OF_SOF
; 
 703                                 if(DecodeReader
->posCount 
> 5) { // stayed low for too long 
 704                                         DecodeReaderReset(DecodeReader
); 
 706                                         // do nothing, keep waiting 
 711                 case STATE_READER_AWAIT_2ND_FALLING_EDGE_OF_SOF
: 
 712                         DecodeReader
->posCount
++; 
 713                         if(!bit
) { // detected a falling edge 
 714                                 if (DecodeReader
->posCount 
< 20) {         // falling edge too early (nominally expected at 21 earliest) 
 715                                         DecodeReaderReset(DecodeReader
); 
 716                                 } else if (DecodeReader
->posCount 
< 23) {  // SOF for 1 out of 4 coding 
 717                                         DecodeReader
->Coding 
= CODING_1_OUT_OF_4
; 
 718                                         DecodeReader
->state 
= STATE_READER_AWAIT_2ND_RISING_EDGE_OF_SOF
; 
 719                                 } else if (DecodeReader
->posCount 
< 28) {  // falling edge too early (nominally expected at 29 latest) 
 720                                         DecodeReaderReset(DecodeReader
); 
 721                                 } else {                                 // SOF for 1 out of 4 coding 
 722                                         DecodeReader
->Coding 
= CODING_1_OUT_OF_256
; 
 723                                         DecodeReader
->state 
= STATE_READER_AWAIT_2ND_RISING_EDGE_OF_SOF
; 
 726                                 if(DecodeReader
->posCount 
> 29) { // stayed high for too long 
 727                                         DecodeReaderReset(DecodeReader
); 
 729                                         // do nothing, keep waiting 
 734                 case STATE_READER_AWAIT_2ND_RISING_EDGE_OF_SOF
: 
 735                         DecodeReader
->posCount
++; 
 736                         if (bit
) { // detected rising edge 
 737                                 if (DecodeReader
->Coding 
== CODING_1_OUT_OF_256
) { 
 738                                         if (DecodeReader
->posCount 
< 32) { // rising edge too early (nominally expected at 33) 
 739                                         DecodeReaderReset(DecodeReader
); 
 741                                                 DecodeReader
->posCount 
= 1; 
 742                                                 DecodeReader
->bitCount 
= 0; 
 743                                                 DecodeReader
->byteCount 
= 0; 
 744                                                 DecodeReader
->sum1 
= 1; 
 745                                                 DecodeReader
->state 
= STATE_READER_RECEIVE_DATA_1_OUT_OF_256
; 
 748                                 } else { // CODING_1_OUT_OF_4 
 749                                         if (DecodeReader
->posCount 
< 24) { // rising edge too early (nominally expected at 25) 
 750                                         DecodeReaderReset(DecodeReader
); 
 752                                                 DecodeReader
->state 
= STATE_READER_AWAIT_END_OF_SOF_1_OUT_OF_4
; 
 756                                 if (DecodeReader
->Coding 
== CODING_1_OUT_OF_256
) { 
 757                                         if (DecodeReader
->posCount 
> 34) { // signal stayed low for too long 
 758                                         DecodeReaderReset(DecodeReader
); 
 760                                                 // do nothing, keep waiting 
 762                                 } else { // CODING_1_OUT_OF_4 
 763                                         if (DecodeReader
->posCount 
> 26) { // signal stayed low for too long 
 764                                         DecodeReaderReset(DecodeReader
); 
 766                                                 // do nothing, keep waiting 
 772                 case STATE_READER_AWAIT_END_OF_SOF_1_OUT_OF_4
: 
 773                         DecodeReader
->posCount
++; 
 775                                 if (DecodeReader
->posCount 
== 33) { 
 776                                         DecodeReader
->posCount 
= 1; 
 777                                         DecodeReader
->bitCount 
= 0; 
 778                                         DecodeReader
->byteCount 
= 0; 
 779                                         DecodeReader
->sum1 
= 1; 
 780                                         DecodeReader
->state 
= STATE_READER_RECEIVE_DATA_1_OUT_OF_4
; 
 783                                         // do nothing, keep waiting 
 785                         } else { // unexpected falling edge 
 786                                         DecodeReaderReset(DecodeReader
); 
 790                 case STATE_READER_RECEIVE_DATA_1_OUT_OF_4
: 
 791                         DecodeReader
->posCount
++; 
 792                         if (DecodeReader
->posCount 
== 1) { 
 793                                 DecodeReader
->sum1 
= bit
; 
 794                         } else if (DecodeReader
->posCount 
<= 4) { 
 795                                 DecodeReader
->sum1 
+= bit
; 
 796                         } else if (DecodeReader
->posCount 
== 5) { 
 797                                 DecodeReader
->sum2 
= bit
; 
 799                                 DecodeReader
->sum2 
+= bit
; 
 801                         if (DecodeReader
->posCount 
== 8) { 
 802                                 DecodeReader
->posCount 
= 0; 
 803                                 int corr10 
= DecodeReader
->sum1 
- DecodeReader
->sum2
; 
 804                                 int corr01 
= DecodeReader
->sum2 
- DecodeReader
->sum1
; 
 805                                 int corr11 
= (DecodeReader
->sum1 
+ DecodeReader
->sum2
) / 2; 
 806                                 if (corr01 
> corr11 
&& corr01 
> corr10
) { // EOF 
 807                                         LED_B_OFF(); // Finished receiving 
 808                                         DecodeReaderReset(DecodeReader
); 
 809                                         if (DecodeReader
->byteCount 
!= 0) { 
 813                                 if (corr10 
> corr11
) { // detected a 2bit position 
 814                                         DecodeReader
->shiftReg 
>>= 2; 
 815                                         DecodeReader
->shiftReg 
|= (DecodeReader
->bitCount 
<< 6); 
 817                                 if (DecodeReader
->bitCount 
== 15) { // we have a full byte 
 818                                         DecodeReader
->output
[DecodeReader
->byteCount
++] = DecodeReader
->shiftReg
; 
 819                                         if (DecodeReader
->byteCount 
> DecodeReader
->byteCountMax
) { 
 820                                                 // buffer overflow, give up 
 822                                                 DecodeReaderReset(DecodeReader
); 
 824                                         DecodeReader
->bitCount 
= 0; 
 825                                         DecodeReader
->shiftReg 
= 0; 
 827                                         DecodeReader
->bitCount
++; 
 832                 case STATE_READER_RECEIVE_DATA_1_OUT_OF_256
: 
 833                         DecodeReader
->posCount
++; 
 834                         if (DecodeReader
->posCount 
== 1) { 
 835                                 DecodeReader
->sum1 
= bit
; 
 836                         } else if (DecodeReader
->posCount 
<= 4) { 
 837                                 DecodeReader
->sum1 
+= bit
; 
 838                         } else if (DecodeReader
->posCount 
== 5) { 
 839                                 DecodeReader
->sum2 
= bit
; 
 841                                 DecodeReader
->sum2 
+= bit
; 
 843                         if (DecodeReader
->posCount 
== 8) { 
 844                                 DecodeReader
->posCount 
= 0; 
 845                                 int corr10 
= DecodeReader
->sum1 
- DecodeReader
->sum2
; 
 846                                 int corr01 
= DecodeReader
->sum2 
- DecodeReader
->sum1
; 
 847                                 int corr11 
= (DecodeReader
->sum1 
+ DecodeReader
->sum2
) / 2; 
 848                                 if (corr01 
> corr11 
&& corr01 
> corr10
) { // EOF 
 849                                         LED_B_OFF(); // Finished receiving 
 850                                         DecodeReaderReset(DecodeReader
); 
 851                                         if (DecodeReader
->byteCount 
!= 0) { 
 855                                 if (corr10 
> corr11
) { // detected the bit position 
 856                                         DecodeReader
->shiftReg 
= DecodeReader
->bitCount
; 
 858                                 if (DecodeReader
->bitCount 
== 255) { // we have a full byte 
 859                                         DecodeReader
->output
[DecodeReader
->byteCount
++] = DecodeReader
->shiftReg
; 
 860                                         if (DecodeReader
->byteCount 
> DecodeReader
->byteCountMax
) { 
 861                                                 // buffer overflow, give up 
 863                                                 DecodeReaderReset(DecodeReader
); 
 866                                 DecodeReader
->bitCount
++; 
 872                         DecodeReaderReset(DecodeReader
); 
 880 //----------------------------------------------------------------------------- 
 881 // Receive a command (from the reader to us, where we are the simulated tag), 
 882 // and store it in the given buffer, up to the given maximum length. Keeps 
 883 // spinning, waiting for a well-framed command, until either we get one 
 884 // (returns true) or someone presses the pushbutton on the board (false). 
 886 // Assume that we're called with the SSC (to the FPGA) and ADC path set 
 888 //----------------------------------------------------------------------------- 
 890 static int GetIso15693CommandFromReader(uint8_t *received
, size_t max_len
, uint32_t *eof_time
) 
 893         bool gotFrame 
= false; 
 896         uint8_t *dmaBuf 
= BigBuf_malloc(ISO15693_DMA_BUFFER_SIZE
); 
 898         // the decoder data structure 
 899         DecodeReader_t DecodeReader 
= {0}; 
 900         DecodeReaderInit(&DecodeReader
, received
, max_len
); 
 902         // wait for last transfer to complete 
 903         while (!(AT91C_BASE_SSC
->SSC_SR 
& AT91C_SSC_TXEMPTY
)); 
 906         FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR 
| FPGA_HF_SIMULATOR_NO_MODULATION
); 
 908         // clear receive register and wait for next transfer 
 909         uint32_t temp 
= AT91C_BASE_SSC
->SSC_RHR
; 
 911         while (!(AT91C_BASE_SSC
->SSC_SR 
& AT91C_SSC_RXRDY
)) ; 
 913         uint32_t bit_time 
= GetCountSspClk() & 0xfffffff8; 
 915         // Setup and start DMA. 
 916         FpgaSetupSscDma(dmaBuf
, ISO15693_DMA_BUFFER_SIZE
); 
 917         uint8_t *upTo 
= dmaBuf
; 
 920                 uint16_t behindBy 
= ((uint8_t*)AT91C_BASE_PDC_SSC
->PDC_RPR 
- upTo
) & (ISO15693_DMA_BUFFER_SIZE
-1); 
 922                 if (behindBy 
== 0) continue; 
 925                 if(upTo 
>= dmaBuf 
+ ISO15693_DMA_BUFFER_SIZE
) {                // we have read all of the DMA buffer content. 
 926                         upTo 
= dmaBuf
;                                             // start reading the circular buffer from the beginning 
 927                         if(behindBy 
> (9*ISO15693_DMA_BUFFER_SIZE
/10)) { 
 928                                 Dbprintf("About to blow circular buffer - aborted! behindBy=%d", behindBy
); 
 932                 if (AT91C_BASE_SSC
->SSC_SR 
& (AT91C_SSC_ENDRX
)) {              // DMA Counter Register had reached 0, already rotated. 
 933                         AT91C_BASE_PDC_SSC
->PDC_RNPR 
= (uint32_t) dmaBuf
;          // refresh the DMA Next Buffer and 
 934                         AT91C_BASE_PDC_SSC
->PDC_RNCR 
= ISO15693_DMA_BUFFER_SIZE
;   // DMA Next Counter registers 
 937                 for (int i 
= 7; i 
>= 0; i
--) { 
 938                         if (Handle15693SampleFromReader((b 
>> i
) & 0x01, &DecodeReader
)) { 
 939                                 *eof_time 
= bit_time 
+ samples 
- DELAY_READER_TO_ARM_SIM
; // end of EOF 
 950                 if (BUTTON_PRESS()) { 
 951                         DecodeReader
.byteCount 
= 0; 
 960         BigBuf_free_keep_EM(); 
 962         if (DEBUG
) Dbprintf("samples = %d, gotFrame = %d, Decoder: state = %d, len = %d, bitCount = %d, posCount = %d", 
 963                             samples
, gotFrame
, DecodeReader
.state
, DecodeReader
.byteCount
, DecodeReader
.bitCount
, DecodeReader
.posCount
); 
 965         if (DecodeReader
.byteCount 
> 0) { 
 966                 LogTrace(DecodeReader
.output
, DecodeReader
.byteCount
, 0, *eof_time
, NULL
, true); 
 969         return DecodeReader
.byteCount
; 
 973 // Encode (into the ToSend buffers) an identify request, which is the first 
 974 // thing that you must send to a tag to get a response. 
 975 static void BuildIdentifyRequest(void) 
 980         // one sub-carrier, inventory, 1 slot, fast rate 
 981         // AFI is at bit 5 (1<<4) when doing an INVENTORY 
 982         cmd
[0] = (1 << 2) | (1 << 5) | (1 << 1); 
 983         // inventory command code 
 992         CodeIso15693AsReader(cmd
, sizeof(cmd
)); 
 996 //----------------------------------------------------------------------------- 
 997 // Start to read an ISO 15693 tag. We send an identify request, then wait 
 998 // for the response. The response is not demodulated, just left in the buffer 
 999 // so that it can be downloaded to a PC and processed there. 
1000 //----------------------------------------------------------------------------- 
1001 void AcquireRawAdcSamplesIso15693(void) 
1006         uint8_t *dest 
= BigBuf_get_addr(); 
1008         FpgaDownloadAndGo(FPGA_BITSTREAM_HF
); 
1009         FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER
); 
1010         FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER
); 
1011         SetAdcMuxFor(GPIO_MUXSEL_HIPKD
); 
1013         BuildIdentifyRequest(); 
1015         // Give the tags time to energize 
1019         // Now send the command 
1020         TransmitTo15693Tag(ToSend
, ToSendMax
, 0); 
1022         // wait for last transfer to complete 
1023         while (!(AT91C_BASE_SSC
->SSC_SR 
& AT91C_SSC_TXEMPTY
)) ; 
1025         FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER 
| FPGA_HF_READER_SUBCARRIER_424_KHZ 
| FPGA_HF_READER_MODE_RECEIVE_AMPLITUDE
); 
1027         for(int c 
= 0; c 
< 4000; ) { 
1028                 if(AT91C_BASE_SSC
->SSC_SR 
& (AT91C_SSC_RXRDY
)) { 
1029                         uint16_t r 
= AT91C_BASE_SSC
->SSC_RHR
; 
1034         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
1039 void SnoopIso15693(void) 
1042         FpgaDownloadAndGo(FPGA_BITSTREAM_HF
); 
1048         // The DMA buffer, used to stream samples from the FPGA 
1049         uint16_t* dmaBuf 
= (uint16_t*)BigBuf_malloc(ISO15693_DMA_BUFFER_SIZE
*sizeof(uint16_t)); 
1052         // Count of samples received so far, so that we can include timing 
1053         // information in the trace buffer. 
1056         DecodeTag_t DecodeTag 
= {0}; 
1057         uint8_t response
[ISO15693_MAX_RESPONSE_LENGTH
]; 
1058         DecodeTagInit(&DecodeTag
, response
, sizeof(response
)); 
1060         DecodeReader_t DecodeReader 
= {0};; 
1061         uint8_t cmd
[ISO15693_MAX_COMMAND_LENGTH
]; 
1062         DecodeReaderInit(&DecodeReader
, cmd
, sizeof(cmd
)); 
1064         // Print some debug information about the buffer sizes 
1066                 Dbprintf("Snooping buffers initialized:"); 
1067                 Dbprintf("  Trace:         %i bytes", BigBuf_max_traceLen()); 
1068                 Dbprintf("  Reader -> tag: %i bytes", ISO15693_MAX_COMMAND_LENGTH
); 
1069                 Dbprintf("  tag -> Reader: %i bytes", ISO15693_MAX_RESPONSE_LENGTH
); 
1070                 Dbprintf("  DMA:           %i bytes", ISO15693_DMA_BUFFER_SIZE 
* sizeof(uint16_t)); 
1072         Dbprintf("Snoop started. Press PM3 Button to stop."); 
1074         FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER 
| FPGA_HF_READER_MODE_SNOOP_AMPLITUDE
); 
1075         SetAdcMuxFor(GPIO_MUXSEL_HIPKD
); 
1077         // Setup for the DMA. 
1078         FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER
); 
1080         FpgaSetupSscDma((uint8_t*) dmaBuf
, ISO15693_DMA_BUFFER_SIZE
); 
1082         bool TagIsActive 
= false; 
1083         bool ReaderIsActive 
= false; 
1084         bool ExpectTagAnswer 
= false; 
1086         // And now we loop, receiving samples. 
1088                 uint16_t behindBy 
= ((uint16_t*)AT91C_BASE_PDC_SSC
->PDC_RPR 
- upTo
) & (ISO15693_DMA_BUFFER_SIZE
-1); 
1090                 if (behindBy 
== 0) continue; 
1092                 uint16_t snoopdata 
= *upTo
++; 
1094                 if(upTo 
>= dmaBuf 
+ ISO15693_DMA_BUFFER_SIZE
) {                    // we have read all of the DMA buffer content. 
1095                         upTo 
= dmaBuf
;                                                 // start reading the circular buffer from the beginning 
1096                         if(behindBy 
> (9*ISO15693_DMA_BUFFER_SIZE
/10)) { 
1097                                 Dbprintf("About to blow circular buffer - aborted! behindBy=%d, samples=%d", behindBy
, samples
); 
1100                         if (AT91C_BASE_SSC
->SSC_SR 
& (AT91C_SSC_ENDRX
)) {              // DMA Counter Register had reached 0, already rotated. 
1101                                 AT91C_BASE_PDC_SSC
->PDC_RNPR 
= (uint32_t) dmaBuf
;          // refresh the DMA Next Buffer and 
1102                                 AT91C_BASE_PDC_SSC
->PDC_RNCR 
= ISO15693_DMA_BUFFER_SIZE
;   // DMA Next Counter registers 
1104                                 if(BUTTON_PRESS()) { 
1105                                         DbpString("Snoop stopped."); 
1112                 if (!TagIsActive
) {                                            // no need to try decoding reader data if the tag is sending 
1113                         if (Handle15693SampleFromReader(snoopdata 
& 0x02, &DecodeReader
)) { 
1114                                 FpgaDisableSscDma(); 
1115                                 ExpectTagAnswer 
= true; 
1116                                 LogTrace(DecodeReader
.output
, DecodeReader
.byteCount
, samples
, samples
, NULL
, true); 
1117                                 /* And ready to receive another command. */ 
1118                                 DecodeReaderReset(&DecodeReader
); 
1119                                 /* And also reset the demod code, which might have been */ 
1120                                 /* false-triggered by the commands from the reader. */ 
1121                                 DecodeTagReset(&DecodeTag
); 
1123                                 FpgaSetupSscDma((uint8_t*) dmaBuf
, ISO15693_DMA_BUFFER_SIZE
); 
1125                         if (Handle15693SampleFromReader(snoopdata 
& 0x01, &DecodeReader
)) { 
1126                                 FpgaDisableSscDma(); 
1127                                 ExpectTagAnswer 
= true; 
1128                                 LogTrace(DecodeReader
.output
, DecodeReader
.byteCount
, samples
, samples
, NULL
, true); 
1129                                 /* And ready to receive another command. */ 
1130                                 DecodeReaderReset(&DecodeReader
); 
1131                                 /* And also reset the demod code, which might have been */ 
1132                                 /* false-triggered by the commands from the reader. */ 
1133                                 DecodeTagReset(&DecodeTag
); 
1135                                 FpgaSetupSscDma((uint8_t*) dmaBuf
, ISO15693_DMA_BUFFER_SIZE
); 
1137                         ReaderIsActive 
= (DecodeReader
.state 
>= STATE_READER_AWAIT_2ND_RISING_EDGE_OF_SOF
); 
1140                 if (!ReaderIsActive 
&& ExpectTagAnswer
) {                                               // no need to try decoding tag data if the reader is currently sending or no answer expected yet 
1141                         if (Handle15693SamplesFromTag(snoopdata 
>> 2, &DecodeTag
)) { 
1142                                 FpgaDisableSscDma(); 
1143                                 //Use samples as a time measurement 
1144                                 LogTrace(DecodeTag
.output
, DecodeTag
.len
, samples
, samples
, NULL
, false); 
1145                                 // And ready to receive another response. 
1146                                 DecodeTagReset(&DecodeTag
); 
1147                                 DecodeReaderReset(&DecodeReader
); 
1148                                 ExpectTagAnswer 
= false; 
1150                                 FpgaSetupSscDma((uint8_t*) dmaBuf
, ISO15693_DMA_BUFFER_SIZE
); 
1152                         TagIsActive 
= (DecodeTag
.state 
>= STATE_TAG_RECEIVING_DATA
); 
1157         FpgaDisableSscDma(); 
1162         DbpString("Snoop statistics:"); 
1163         Dbprintf("  ExpectTagAnswer: %d", ExpectTagAnswer
); 
1164         Dbprintf("  DecodeTag State: %d", DecodeTag
.state
); 
1165         Dbprintf("  DecodeTag byteCnt: %d", DecodeTag
.len
); 
1166         Dbprintf("  DecodeReader State: %d", DecodeReader
.state
); 
1167         Dbprintf("  DecodeReader byteCnt: %d", DecodeReader
.byteCount
); 
1168         Dbprintf("  Trace length: %d", BigBuf_get_traceLen()); 
1172 // Initialize the proxmark as iso15k reader 
1173 static void Iso15693InitReader() { 
1174         FpgaDownloadAndGo(FPGA_BITSTREAM_HF
); 
1178         // Start from off (no field generated) 
1180         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
1183         SetAdcMuxFor(GPIO_MUXSEL_HIPKD
); 
1184         FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER
); 
1186         // Give the tags time to energize 
1188         FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER
); 
1192 /////////////////////////////////////////////////////////////////////// 
1193 // ISO 15693 Part 3 - Air Interface 
1194 // This section basically contains transmission and receiving of bits 
1195 /////////////////////////////////////////////////////////////////////// 
1198 // uid is in transmission order (which is reverse of display order) 
1199 static void BuildReadBlockRequest(uint8_t *uid
, uint8_t blockNumber 
) 
1204         // If we set the Option_Flag in this request, the VICC will respond with the security status of the block 
1205         // followed by the block data 
1206         cmd
[0] = ISO15693_REQ_OPTION 
| ISO15693_REQ_ADDRESS 
| ISO15693_REQ_DATARATE_HIGH
;  
1207         // READ BLOCK command code 
1208         cmd
[1] = ISO15693_READBLOCK
; 
1209         // UID may be optionally specified here 
1218         cmd
[9] = uid
[7]; // 0xe0; // always e0 (not exactly unique) 
1219         // Block number to read 
1220         cmd
[10] = blockNumber
; 
1222         crc 
= Crc(cmd
, 11); // the crc needs to be calculated over 11 bytes 
1223         cmd
[11] = crc 
& 0xff; 
1226         CodeIso15693AsReader(cmd
, sizeof(cmd
)); 
1230 // Now the VICC>VCD responses when we are simulating a tag 
1231 static void BuildInventoryResponse(uint8_t *uid
) 
1237         cmd
[0] = 0; // No error, no protocol format extension 
1238         cmd
[1] = 0; // DSFID (data storage format identifier).  0x00 = not supported 
1240         cmd
[2] = uid
[7]; //0x32; 
1241         cmd
[3] = uid
[6]; //0x4b; 
1242         cmd
[4] = uid
[5]; //0x03; 
1243         cmd
[5] = uid
[4]; //0x01; 
1244         cmd
[6] = uid
[3]; //0x00; 
1245         cmd
[7] = uid
[2]; //0x10; 
1246         cmd
[8] = uid
[1]; //0x05; 
1247         cmd
[9] = uid
[0]; //0xe0; 
1250         cmd
[10] = crc 
& 0xff; 
1253         CodeIso15693AsTag(cmd
, sizeof(cmd
)); 
1256 // Universal Method for sending to and recv bytes from a tag 
1257 //      init ... should we initialize the reader? 
1258 //      speed ... 0 low speed, 1 hi speed 
1259 //      *recv will contain the tag's answer 
1260 //      return: lenght of received data 
1261 int SendDataTag(uint8_t *send
, int sendlen
, bool init
, int speed
, uint8_t *recv
, uint16_t max_recv_len
, uint32_t start_time
) { 
1267         if (init
) Iso15693InitReader(); 
1272                 // low speed (1 out of 256) 
1273                 CodeIso15693AsReader256(send
, sendlen
); 
1275                 // high speed (1 out of 4) 
1276                 CodeIso15693AsReader(send
, sendlen
); 
1279         TransmitTo15693Tag(ToSend
, ToSendMax
, start_time
); 
1281         // Now wait for a response 
1283                 answerLen 
= GetIso15693AnswerFromTag(recv
, max_recv_len
, DELAY_ISO15693_VCD_TO_VICC_READER 
* 2); 
1292 // -------------------------------------------------------------------- 
1294 // -------------------------------------------------------------------- 
1296 // Decodes a message from a tag and displays its metadata and content 
1297 #define DBD15STATLEN 48 
1298 void DbdecodeIso15693Answer(int len
, uint8_t *d
) { 
1299         char status
[DBD15STATLEN
+1]={0}; 
1303                 if (d
[0] & ISO15693_RES_EXT
) 
1304                         strncat(status
,"ProtExt ", DBD15STATLEN
); 
1305                 if (d
[0] & ISO15693_RES_ERROR
) { 
1307                         strncat(status
,"Error ", DBD15STATLEN
); 
1310                                         strncat(status
,"01:notSupp", DBD15STATLEN
); 
1313                                         strncat(status
,"02:notRecog", DBD15STATLEN
); 
1316                                         strncat(status
,"03:optNotSupp", DBD15STATLEN
); 
1319                                         strncat(status
,"0f:noInfo", DBD15STATLEN
); 
1322                                         strncat(status
,"10:doesn'tExist", DBD15STATLEN
); 
1325                                         strncat(status
,"11:lockAgain", DBD15STATLEN
); 
1328                                         strncat(status
,"12:locked", DBD15STATLEN
); 
1331                                         strncat(status
,"13:progErr", DBD15STATLEN
); 
1334                                         strncat(status
,"14:lockErr", DBD15STATLEN
); 
1337                                         strncat(status
,"unknownErr", DBD15STATLEN
); 
1339                         strncat(status
," ", DBD15STATLEN
); 
1341                         strncat(status
,"NoErr ", DBD15STATLEN
); 
1345                 if ( (( crc 
& 0xff ) == d
[len
-2]) && (( crc 
>> 8 ) == d
[len
-1]) ) 
1346                         strncat(status
,"CrcOK",DBD15STATLEN
); 
1348                         strncat(status
,"CrcFail!",DBD15STATLEN
); 
1350                 Dbprintf("%s",status
); 
1356 /////////////////////////////////////////////////////////////////////// 
1357 // Functions called via USB/Client 
1358 /////////////////////////////////////////////////////////////////////// 
1360 void SetDebugIso15693(uint32_t debug
) { 
1362         Dbprintf("Iso15693 Debug is now %s",DEBUG
?"on":"off"); 
1367 //--------------------------------------------------------------------------------------- 
1368 // Simulate an ISO15693 reader, perform anti-collision and then attempt to read a sector. 
1369 // all demodulation performed in arm rather than host. - greg 
1370 //--------------------------------------------------------------------------------------- 
1371 void ReaderIso15693(uint32_t parameter
) 
1379         uint8_t TagUID
[8] = {0x00}; 
1381         FpgaDownloadAndGo(FPGA_BITSTREAM_HF
); 
1383         uint8_t answer
[ISO15693_MAX_RESPONSE_LENGTH
]; 
1385         SetAdcMuxFor(GPIO_MUXSEL_HIPKD
); 
1387         FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER
); 
1389         // Start from off (no field generated) 
1390         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
1393         // Give the tags time to energize 
1395         FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER
); 
1400         // FIRST WE RUN AN INVENTORY TO GET THE TAG UID 
1401         // THIS MEANS WE CAN PRE-BUILD REQUESTS TO SAVE CPU TIME 
1403         // Now send the IDENTIFY command 
1404         BuildIdentifyRequest(); 
1405         TransmitTo15693Tag(ToSend
, ToSendMax
, 0); 
1407         // Now wait for a response 
1408         answerLen 
= GetIso15693AnswerFromTag(answer
, sizeof(answer
), DELAY_ISO15693_VCD_TO_VICC_READER 
* 2) ; 
1409         uint32_t start_time 
= GetCountSspClk() + DELAY_ISO15693_VICC_TO_VCD_READER
; 
1411         if (answerLen 
>=12) // we should do a better check than this 
1413                 TagUID
[0] = answer
[2]; 
1414                 TagUID
[1] = answer
[3]; 
1415                 TagUID
[2] = answer
[4]; 
1416                 TagUID
[3] = answer
[5]; 
1417                 TagUID
[4] = answer
[6]; 
1418                 TagUID
[5] = answer
[7]; 
1419                 TagUID
[6] = answer
[8]; // IC Manufacturer code 
1420                 TagUID
[7] = answer
[9]; // always E0 
1424         Dbprintf("%d octets read from IDENTIFY request:", answerLen
); 
1425         DbdecodeIso15693Answer(answerLen
, answer
); 
1426         Dbhexdump(answerLen
, answer
, false); 
1429         if (answerLen 
>= 12) 
1430                 Dbprintf("UID = %02hX%02hX%02hX%02hX%02hX%02hX%02hX%02hX", 
1431                         TagUID
[7],TagUID
[6],TagUID
[5],TagUID
[4], 
1432                         TagUID
[3],TagUID
[2],TagUID
[1],TagUID
[0]); 
1435         // Dbprintf("%d octets read from SELECT request:", answerLen2); 
1436         // DbdecodeIso15693Answer(answerLen2,answer2); 
1437         // Dbhexdump(answerLen2,answer2,true); 
1439         // Dbprintf("%d octets read from XXX request:", answerLen3); 
1440         // DbdecodeIso15693Answer(answerLen3,answer3); 
1441         // Dbhexdump(answerLen3,answer3,true); 
1444         if (answerLen 
>= 12 && DEBUG
) { 
1445                 for (int i 
= 0; i 
< 32; i
++) {  // sanity check, assume max 32 pages 
1446                         BuildReadBlockRequest(TagUID
, i
); 
1447                         TransmitTo15693Tag(ToSend
, ToSendMax
, start_time
); 
1448                         int answerLen 
= GetIso15693AnswerFromTag(answer
, sizeof(answer
), DELAY_ISO15693_VCD_TO_VICC_READER 
* 2); 
1449                         start_time 
= GetCountSspClk() + DELAY_ISO15693_VICC_TO_VCD_READER
; 
1450                         if (answerLen 
> 0) { 
1451                                 Dbprintf("READ SINGLE BLOCK %d returned %d octets:", i
, answerLen
); 
1452                                 DbdecodeIso15693Answer(answerLen
, answer
); 
1453                                 Dbhexdump(answerLen
, answer
, false); 
1454                                 if ( *((uint32_t*) answer
) == 0x07160101 ) break; // exit on NoPageErr 
1459         // for the time being, switch field off to protect rdv4.0 
1460         // note: this prevents using hf 15 cmd with s option - which isn't implemented yet anyway 
1461         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
1468 // Simulate an ISO15693 TAG. 
1469 // For Inventory command: print command and send Inventory Response with given UID 
1470 // TODO: interpret other reader commands and send appropriate response 
1471 void SimTagIso15693(uint32_t parameter
, uint8_t *uid
) 
1476         FpgaDownloadAndGo(FPGA_BITSTREAM_HF
); 
1477         SetAdcMuxFor(GPIO_MUXSEL_HIPKD
); 
1478         FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR 
| FPGA_HF_SIMULATOR_NO_MODULATION
); 
1479         FpgaSetupSsc(FPGA_MAJOR_MODE_HF_SIMULATOR
); 
1483         uint8_t cmd
[ISO15693_MAX_COMMAND_LENGTH
]; 
1485         // Build a suitable response to the reader INVENTORY command 
1486         BuildInventoryResponse(uid
); 
1489         while (!BUTTON_PRESS()) { 
1490                 uint32_t eof_time 
= 0, start_time 
= 0; 
1491                 int cmd_len 
= GetIso15693CommandFromReader(cmd
, sizeof(cmd
), &eof_time
); 
1493                 if ((cmd_len 
>= 5) && (cmd
[0] & ISO15693_REQ_INVENTORY
) && (cmd
[1] == ISO15693_INVENTORY
)) { // TODO: check more flags 
1494                         bool slow 
= !(cmd
[0] & ISO15693_REQ_DATARATE_HIGH
); 
1495                         start_time 
= eof_time 
+ DELAY_ISO15693_VCD_TO_VICC_SIM 
- DELAY_ARM_TO_READER_SIM
; 
1496                         TransmitTo15693Reader(ToSend
, ToSendMax
, start_time
, slow
); 
1499                 Dbprintf("%d bytes read from reader:", cmd_len
); 
1500                 Dbhexdump(cmd_len
, cmd
, false); 
1503         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
1508 // Since there is no standardized way of reading the AFI out of a tag, we will brute force it 
1509 // (some manufactures offer a way to read the AFI, though) 
1510 void BruteforceIso15693Afi(uint32_t speed
) 
1516         uint8_t recv
[ISO15693_MAX_RESPONSE_LENGTH
]; 
1518         int datalen
=0, recvlen
=0; 
1520         Iso15693InitReader(); 
1523         // first without AFI 
1524         // Tags should respond without AFI and with AFI=0 even when AFI is active 
1526         data
[0] = ISO15693_REQ_DATARATE_HIGH 
| ISO15693_REQ_INVENTORY 
| ISO15693_REQINV_SLOT1
; 
1527         data
[1] = ISO15693_INVENTORY
; 
1528         data
[2] = 0; // mask length 
1529         datalen 
= AddCrc(data
,3); 
1530         recvlen 
= SendDataTag(data
, datalen
, false, speed
, recv
, sizeof(recv
), 0); 
1531         uint32_t start_time 
= GetCountSspClk() + DELAY_ISO15693_VICC_TO_VCD_READER
; 
1534                 Dbprintf("NoAFI UID=%s", sprintUID(NULL
, &recv
[2])); 
1539         data
[0] = ISO15693_REQ_DATARATE_HIGH 
| ISO15693_REQ_INVENTORY 
| ISO15693_REQINV_AFI 
| ISO15693_REQINV_SLOT1
; 
1540         data
[1] = ISO15693_INVENTORY
; 
1542         data
[3] = 0; // mask length 
1544         for (int i 
= 0; i 
< 256; i
++) { 
1546                 datalen 
= AddCrc(data
,4); 
1547                 recvlen 
= SendDataTag(data
, datalen
, false, speed
, recv
, sizeof(recv
), start_time
); 
1548                 start_time 
= GetCountSspClk() + DELAY_ISO15693_VICC_TO_VCD_READER
; 
1550                 if (recvlen 
>= 12) { 
1551                         Dbprintf("AFI=%i UID=%s", i
, sprintUID(NULL
, &recv
[2])); 
1554         Dbprintf("AFI Bruteforcing done."); 
1556         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
1560 // Allows to directly send commands to the tag via the client 
1561 void DirectTag15693Command(uint32_t datalen
, uint32_t speed
, uint32_t recv
, uint8_t data
[]) { 
1564         uint8_t recvbuf
[ISO15693_MAX_RESPONSE_LENGTH
]; 
1570                 Dbhexdump(datalen
, data
, false); 
1573         recvlen 
= SendDataTag(data
, datalen
, true, speed
, (recv
?recvbuf
:NULL
), sizeof(recvbuf
), 0); 
1578                         Dbhexdump(recvlen
, recvbuf
, false); 
1579                         DbdecodeIso15693Answer(recvlen
, recvbuf
); 
1582                 cmd_send(CMD_ACK
, recvlen
>ISO15693_MAX_RESPONSE_LENGTH
?ISO15693_MAX_RESPONSE_LENGTH
:recvlen
, 0, 0, recvbuf
, ISO15693_MAX_RESPONSE_LENGTH
); 
1586         // for the time being, switch field off to protect rdv4.0 
1587         // note: this prevents using hf 15 cmd with s option - which isn't implemented yet anyway 
1588         FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
); 
1594 //----------------------------------------------------------------------------- 
1595 // Work with "magic Chinese" card. 
1597 //----------------------------------------------------------------------------- 
1599 // Set the UID to the tag (based on Iceman work). 
1600 void SetTag15693Uid(uint8_t *uid
) 
1602     uint8_t cmd
[4][9] = {0x00}; 
1607     uint8_t recvbuf
[ISO15693_MAX_RESPONSE_LENGTH
]; 
1611     // Command 1 : 02213E00000000 
1620     // Command 2 : 02213F69960000 
1629     // Command 3 : 022138u8u7u6u5 (where uX = uid byte X) 
1638     // Command 4 : 022139u4u3u2u1 (where uX = uid byte X) 
1647     for (int i
=0; i
<4; i
++) { 
1649         crc 
= Crc(cmd
[i
], 7); 
1650         cmd
[i
][7] = crc 
& 0xff; 
1651         cmd
[i
][8] = crc 
>> 8; 
1655             Dbhexdump(sizeof(cmd
[i
]), cmd
[i
], false); 
1658         recvlen 
= SendDataTag(cmd
[i
], sizeof(cmd
[i
]), true, 1, recvbuf
, sizeof(recvbuf
), 0); 
1662             Dbhexdump(recvlen
, recvbuf
, false); 
1663             DbdecodeIso15693Answer(recvlen
, recvbuf
); 
1666         cmd_send(CMD_ACK
, recvlen
>ISO15693_MAX_RESPONSE_LENGTH
?ISO15693_MAX_RESPONSE_LENGTH
:recvlen
, 0, 0, recvbuf
, ISO15693_MAX_RESPONSE_LENGTH
); 
1676 // -------------------------------------------------------------------- 
1677 // -- Misc & deprecated functions 
1678 // -------------------------------------------------------------------- 
1682 // do not use; has a fix UID 
1683 static void __attribute__((unused)) BuildSysInfoRequest(uint8_t *uid) 
1688         // If we set the Option_Flag in this request, the VICC will respond with the security status of the block 
1689         // followed by the block data 
1690         // one sub-carrier, inventory, 1 slot, fast rate 
1691         cmd[0] =  (1 << 5) | (1 << 1); // no SELECT bit 
1692         // System Information command code 
1694         // UID may be optionally specified here 
1703         cmd[9]= 0xe0; // always e0 (not exactly unique) 
1705         crc = Crc(cmd, 10); // the crc needs to be calculated over 2 bytes 
1706         cmd[10] = crc & 0xff; 
1709         CodeIso15693AsReader(cmd, sizeof(cmd)); 
1713 // do not use; has a fix UID 
1714 static void __attribute__((unused)) BuildReadMultiBlockRequest(uint8_t *uid) 
1719         // If we set the Option_Flag in this request, the VICC will respond with the security status of the block 
1720         // followed by the block data 
1721         // one sub-carrier, inventory, 1 slot, fast rate 
1722         cmd[0] =  (1 << 5) | (1 << 1); // no SELECT bit 
1723         // READ Multi BLOCK command code 
1725         // UID may be optionally specified here 
1734         cmd[9]= 0xe0; // always e0 (not exactly unique) 
1735         // First Block number to read 
1737         // Number of Blocks to read 
1738         cmd[11] = 0x2f; // read quite a few 
1740         crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes 
1741         cmd[12] = crc & 0xff; 
1744         CodeIso15693AsReader(cmd, sizeof(cmd)); 
1747 // do not use; has a fix UID 
1748 static void __attribute__((unused)) BuildArbitraryRequest(uint8_t *uid,uint8_t CmdCode) 
1753         // If we set the Option_Flag in this request, the VICC will respond with the security status of the block 
1754         // followed by the block data 
1755         // one sub-carrier, inventory, 1 slot, fast rate 
1756         cmd[0] =   (1 << 5) | (1 << 1); // no SELECT bit 
1757         // READ BLOCK command code 
1759         // UID may be optionally specified here 
1768         cmd[9]= 0xe0; // always e0 (not exactly unique) 
1774 //      cmd[13] = 0x00; //Now the CRC 
1775         crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes 
1776         cmd[12] = crc & 0xff; 
1779         CodeIso15693AsReader(cmd, sizeof(cmd)); 
1782 // do not use; has a fix UID 
1783 static void __attribute__((unused)) BuildArbitraryCustomRequest(uint8_t uid[], uint8_t CmdCode) 
1788         // If we set the Option_Flag in this request, the VICC will respond with the security status of the block 
1789         // followed by the block data 
1790         // one sub-carrier, inventory, 1 slot, fast rate 
1791         cmd[0] =   (1 << 5) | (1 << 1); // no SELECT bit 
1792         // READ BLOCK command code 
1794         // UID may be optionally specified here 
1803         cmd[9]= 0xe0; // always e0 (not exactly unique) 
1805         cmd[10] = 0x05; // for custom codes this must be manufacturer code 
1809 //      cmd[13] = 0x00; //Now the CRC 
1810         crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes 
1811         cmd[12] = crc & 0xff; 
1814         CodeIso15693AsReader(cmd, sizeof(cmd));