]> git.zerfleddert.de Git - proxmark3-svn/blame - armsrc/iso15693.c
Merge pull request #884 from pwpiwi/fix_iclass_snoop
[proxmark3-svn] / armsrc / iso15693.c
CommitLineData
15c4dc5a 1//-----------------------------------------------------------------------------
bd20f8f4 2// Jonathan Westhues, split Nov 2006
3// Modified by Greg Jones, Jan 2009
e6304bca 4// Modified by Adrian Dabrowski "atrox", Mar-Sept 2010,Oct 2011
a66f26da 5// Modified by piwi, Oct 2018
bd20f8f4 6//
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
9// the license.
10//-----------------------------------------------------------------------------
15c4dc5a 11// Routines to support ISO 15693. This includes both the reader software and
8c6cca0b 12// the `fake tag' modes.
15c4dc5a 13//-----------------------------------------------------------------------------
8c6cca0b 14
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
a66f26da 19// reader chooses both data rates, but some non-standard tags do not.
8c6cca0b 20// For card simulation, the code supports both high and low speed modes with one subcarrier.
9455b51c 21//
22// VCD (reader) -> VICC (tag)
23// 1 out of 256:
a66f26da 24// data rate: 1,66 kbit/s (fc/8192)
25// used for long range
9455b51c 26// 1 out of 4:
a66f26da 27// data rate: 26,48 kbit/s (fc/512)
28// used for short range, high speed
8c6cca0b 29//
9455b51c 30// VICC (tag) -> VCD (reader)
31// Modulation:
a66f26da 32// ASK / one subcarrier (423,75 khz)
33// FSK / two subcarriers (423,75 khz && 484,28 khz)
9455b51c 34// Data Rates / Modes:
a66f26da 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
9455b51c 39//-----------------------------------------------------------------------------
9455b51c 40
41
42// Random Remarks:
43// *) UID is always used "transmission order" (LSB), which is reverse to display order
44
45// TODO / BUGS / ISSUES:
8c6cca0b 46// *) signal decoding is unable to detect collisions.
47// *) add anti-collision support for inventory-commands
e6304bca 48// *) read security status of a block
8c6cca0b 49// *) sniffing and simulation do not support two subcarrier modes.
d9de20fa 50// *) remove or refactor code under "deprecated"
9455b51c 51// *) document all the functions
52
d9de20fa 53#include "iso15693.h"
bd20f8f4 54
e30c654b 55#include "proxmark3.h"
f7e3ed82 56#include "util.h"
15c4dc5a 57#include "apps.h"
9ab7a6c7 58#include "string.h"
9455b51c 59#include "iso15693tools.h"
8c6cca0b 60#include "protocols.h"
902cb3c0 61#include "cmd.h"
d9de20fa 62#include "BigBuf.h"
fc52fbd4 63#include "fpgaloader.h"
15c4dc5a 64
15c4dc5a 65#define arraylen(x) (sizeof(x)/sizeof((x)[0]))
66
c41dd5f9 67// Delays in SSP_CLK ticks.
68// SSP_CLK runs at 13,56MHz / 32 = 423.75kHz when simulating a tag
69#define DELAY_READER_TO_ARM 8
70#define DELAY_ARM_TO_READER 0
71//SSP_CLK runs at 13.56MHz / 4 = 3,39MHz when acting as reader. All values should be multiples of 16
c41dd5f9 72#define DELAY_ARM_TO_TAG 16
1ce68968 73#define DELAY_TAG_TO_ARM 32
74//SSP_CLK runs at 13.56MHz / 4 = 3,39MHz when snooping. All values should be multiples of 16
75#define DELAY_TAG_TO_ARM_SNOOP 32
76#define DELAY_READER_TO_ARM_SNOOP 32
c41dd5f9 77
70b2fc0a 78static int DEBUG = 0;
79
c41dd5f9 80
9455b51c 81///////////////////////////////////////////////////////////////////////
82// ISO 15693 Part 2 - Air Interface
3d2c9c9b 83// This section basically contains transmission and receiving of bits
9455b51c 84///////////////////////////////////////////////////////////////////////
85
8c6cca0b 86// buffers
cd028159 87#define ISO15693_DMA_BUFFER_SIZE 256 // must be a power of 2
d9de20fa 88#define ISO15693_MAX_RESPONSE_LENGTH 36 // allows read single block with the maximum block size of 256bits. Read multiple blocks not supported yet
89#define ISO15693_MAX_COMMAND_LENGTH 45 // allows write single block with the maximum block size of 256bits. Write multiple blocks not supported yet
8c6cca0b 90
be09ea86 91
92// specific LogTrace function for ISO15693: the duration needs to be scaled because otherwise it won't fit into a uint16_t
93bool LogTrace_ISO15693(const uint8_t *btBytes, uint16_t iLen, uint32_t timestamp_start, uint32_t timestamp_end, uint8_t *parity, bool readerToTag) {
94 uint32_t duration = timestamp_end - timestamp_start;
95 duration /= 32;
96 timestamp_end = timestamp_start + duration;
97 return LogTrace(btBytes, iLen, timestamp_start, timestamp_end, parity, readerToTag);
98}
99
100
9455b51c 101// ---------------------------
8c6cca0b 102// Signal Processing
9455b51c 103// ---------------------------
104
105// prepare data using "1 out of 4" code for later transmission
8c6cca0b 106// resulting data rate is 26.48 kbit/s (fc/512)
9455b51c 107// cmd ... data
108// n ... length of data
c41dd5f9 109void CodeIso15693AsReader(uint8_t *cmd, int n) {
15c4dc5a 110
111 ToSendReset();
112
9455b51c 113 // SOF for 1of4
c41dd5f9 114 ToSend[++ToSendMax] = 0x84; //10000100
115
116 // data
117 for (int i = 0; i < n; i++) {
118 for (int j = 0; j < 8; j += 2) {
119 int these = (cmd[i] >> j) & 0x03;
15c4dc5a 120 switch(these) {
121 case 0:
c41dd5f9 122 ToSend[++ToSendMax] = 0x40; //01000000
15c4dc5a 123 break;
124 case 1:
c41dd5f9 125 ToSend[++ToSendMax] = 0x10; //00010000
15c4dc5a 126 break;
127 case 2:
c41dd5f9 128 ToSend[++ToSendMax] = 0x04; //00000100
15c4dc5a 129 break;
130 case 3:
c41dd5f9 131 ToSend[++ToSendMax] = 0x01; //00000001
15c4dc5a 132 break;
133 }
134 }
135 }
a66f26da 136
c41dd5f9 137 // EOF
138 ToSend[++ToSendMax] = 0x20; //0010 + 0000 padding
ece38ef3 139
bdf96aae 140 ToSendMax++;
15c4dc5a 141}
142
70b2fc0a 143// encode data using "1 out of 256" scheme
8c6cca0b 144// data rate is 1,66 kbit/s (fc/8192)
9455b51c 145// is designed for more robust communication over longer distances
146static void CodeIso15693AsReader256(uint8_t *cmd, int n)
15c4dc5a 147{
9455b51c 148 ToSendReset();
149
9455b51c 150 // SOF for 1of256
c41dd5f9 151 ToSend[++ToSendMax] = 0x81; //10000001
152
153 // data
154 for(int i = 0; i < n; i++) {
155 for (int j = 0; j <= 255; j++) {
156 if (cmd[i] == j) {
9455b51c 157 ToSendStuffBit(0);
9455b51c 158 ToSendStuffBit(1);
c41dd5f9 159 } else {
160 ToSendStuffBit(0);
161 ToSendStuffBit(0);
8c6cca0b 162 }
163 }
15c4dc5a 164 }
c41dd5f9 165
9455b51c 166 // EOF
c41dd5f9 167 ToSend[++ToSendMax] = 0x20; //0010 + 0000 padding
8c6cca0b 168
169 ToSendMax++;
170}
171
172
3d2c9c9b 173// static uint8_t encode4Bits(const uint8_t b) {
174 // uint8_t c = b & 0xF;
175 // // OTA, the least significant bits first
176 // // The columns are
177 // // 1 - Bit value to send
178 // // 2 - Reversed (big-endian)
179 // // 3 - Manchester Encoded
180 // // 4 - Hex values
181
182 // switch(c){
183 // // 1 2 3 4
184 // case 15: return 0x55; // 1111 -> 1111 -> 01010101 -> 0x55
185 // case 14: return 0x95; // 1110 -> 0111 -> 10010101 -> 0x95
186 // case 13: return 0x65; // 1101 -> 1011 -> 01100101 -> 0x65
187 // case 12: return 0xa5; // 1100 -> 0011 -> 10100101 -> 0xa5
188 // case 11: return 0x59; // 1011 -> 1101 -> 01011001 -> 0x59
189 // case 10: return 0x99; // 1010 -> 0101 -> 10011001 -> 0x99
190 // case 9: return 0x69; // 1001 -> 1001 -> 01101001 -> 0x69
191 // case 8: return 0xa9; // 1000 -> 0001 -> 10101001 -> 0xa9
192 // case 7: return 0x56; // 0111 -> 1110 -> 01010110 -> 0x56
193 // case 6: return 0x96; // 0110 -> 0110 -> 10010110 -> 0x96
194 // case 5: return 0x66; // 0101 -> 1010 -> 01100110 -> 0x66
195 // case 4: return 0xa6; // 0100 -> 0010 -> 10100110 -> 0xa6
196 // case 3: return 0x5a; // 0011 -> 1100 -> 01011010 -> 0x5a
197 // case 2: return 0x9a; // 0010 -> 0100 -> 10011010 -> 0x9a
198 // case 1: return 0x6a; // 0001 -> 1000 -> 01101010 -> 0x6a
199 // default: return 0xaa; // 0000 -> 0000 -> 10101010 -> 0xaa
200
201 // }
202// }
203
8efd0b80 204static const uint8_t encode_4bits[16] = { 0xaa, 0x6a, 0x9a, 0x5a, 0xa6, 0x66, 0x96, 0x56, 0xa9, 0x69, 0x99, 0x59, 0xa5, 0x65, 0x95, 0x55 };
205
3d2c9c9b 206void CodeIso15693AsTag(uint8_t *cmd, size_t len) {
207 /*
208 * SOF comprises 3 parts;
209 * * An unmodulated time of 56.64 us
210 * * 24 pulses of 423.75 kHz (fc/32)
211 * * A logic 1, which starts with an unmodulated time of 18.88us
212 * followed by 8 pulses of 423.75kHz (fc/32)
213 *
214 * EOF comprises 3 parts:
215 * - A logic 0 (which starts with 8 pulses of fc/32 followed by an unmodulated
216 * time of 18.88us.
217 * - 24 pulses of fc/32
218 * - An unmodulated time of 56.64 us
219 *
220 * A logic 0 starts with 8 pulses of fc/32
221 * followed by an unmodulated time of 256/fc (~18,88us).
222 *
223 * A logic 0 starts with unmodulated time of 256/fc (~18,88us) followed by
224 * 8 pulses of fc/32 (also 18.88us)
225 *
226 * A bit here becomes 8 pulses of fc/32. Therefore:
227 * The SOF can be written as 00011101 = 0x1D
228 * The EOF can be written as 10111000 = 0xb8
229 * A logic 1 is 01
230 * A logic 0 is 10
231 *
232 * */
233
8c6cca0b 234 ToSendReset();
235
236 // SOF
3d2c9c9b 237 ToSend[++ToSendMax] = 0x1D; // 00011101
8c6cca0b 238
239 // data
8efd0b80 240 for (int i = 0; i < len; i++) {
241 ToSend[++ToSendMax] = encode_4bits[cmd[i] & 0xF];
242 ToSend[++ToSendMax] = encode_4bits[cmd[i] >> 4];
8c6cca0b 243 }
244
245 // EOF
3d2c9c9b 246 ToSend[++ToSendMax] = 0xB8; // 10111000
8c6cca0b 247
248 ToSendMax++;
15c4dc5a 249}
250
9455b51c 251
70b2fc0a 252// Transmit the command (to the tag) that was placed in cmd[].
c41dd5f9 253void TransmitTo15693Tag(const uint8_t *cmd, int len, uint32_t *start_time) {
254
5ea2a248 255 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER | FPGA_HF_READER_MODE_SEND_FULL_MOD);
ece38ef3 256
257 if (*start_time < DELAY_ARM_TO_TAG) {
258 *start_time = DELAY_ARM_TO_TAG;
259 }
260
c41dd5f9 261 *start_time = (*start_time - DELAY_ARM_TO_TAG) & 0xfffffff0;
262
496bb4be 263 if (GetCountSspClk() > *start_time) { // we may miss the intended time
264 *start_time = (GetCountSspClk() + 16) & 0xfffffff0; // next possible time
c41dd5f9 265 }
15c4dc5a 266
c41dd5f9 267 while (GetCountSspClk() < *start_time)
268 /* wait */ ;
d9de20fa 269
70b2fc0a 270 LED_B_ON();
c41dd5f9 271 for (int c = 0; c < len; c++) {
5ea2a248 272 uint8_t data = cmd[c];
273 for (int i = 0; i < 8; i++) {
c41dd5f9 274 uint16_t send_word = (data & 0x80) ? 0xffff : 0x0000;
5ea2a248 275 while (!(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY))) ;
276 AT91C_BASE_SSC->SSC_THR = send_word;
277 while (!(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY))) ;
278 AT91C_BASE_SSC->SSC_THR = send_word;
c41dd5f9 279
5ea2a248 280 data <<= 1;
281 }
282 WDT_HIT();
283 }
70b2fc0a 284 LED_B_OFF();
ece38ef3 285
c41dd5f9 286 *start_time = *start_time + DELAY_ARM_TO_TAG;
287
15c4dc5a 288}
289
5ea2a248 290
15c4dc5a 291//-----------------------------------------------------------------------------
8c6cca0b 292// Transmit the tag response (to the reader) that was placed in cmd[].
15c4dc5a 293//-----------------------------------------------------------------------------
8efd0b80 294void TransmitTo15693Reader(const uint8_t *cmd, size_t len, uint32_t *start_time, uint32_t slot_time, bool slow) {
8c6cca0b 295 // don't use the FPGA_HF_SIMULATOR_MODULATE_424K_8BIT minor mode. It would spoil GetCountSspClk()
70b2fc0a 296 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR | FPGA_HF_SIMULATOR_MODULATE_424K);
15c4dc5a 297
c41dd5f9 298 uint32_t modulation_start_time = *start_time - DELAY_ARM_TO_READER + 3 * 8; // no need to transfer the unmodulated start of SOF
ece38ef3 299
8efd0b80 300 while (GetCountSspClk() > (modulation_start_time & 0xfffffff8) + 3) { // we will miss the intended time
301 if (slot_time) {
302 modulation_start_time += slot_time; // use next available slot
303 } else {
304 modulation_start_time = (modulation_start_time & 0xfffffff8) + 8; // next possible time
305 }
306 }
307
ece38ef3 308 while (GetCountSspClk() < (modulation_start_time & 0xfffffff8))
8efd0b80 309 /* wait */ ;
8c6cca0b 310
8efd0b80 311 uint8_t shift_delay = modulation_start_time & 0x00000007;
312
c41dd5f9 313 *start_time = modulation_start_time + DELAY_ARM_TO_READER - 3 * 8;
d9de20fa 314
70b2fc0a 315 LED_C_ON();
8c6cca0b 316 uint8_t bits_to_shift = 0x00;
3d2c9c9b 317 uint8_t bits_to_send = 0x00;
8efd0b80 318 for (size_t c = 0; c < len; c++) {
319 for (int i = (c==0?4:7); i >= 0; i--) {
3d2c9c9b 320 uint8_t cmd_bits = ((cmd[c] >> i) & 0x01) ? 0xff : 0x00;
8c6cca0b 321 for (int j = 0; j < (slow?4:1); ) {
322 if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_TXRDY) {
a66f26da 323 bits_to_send = bits_to_shift << (8 - shift_delay) | cmd_bits >> shift_delay;
3d2c9c9b 324 AT91C_BASE_SSC->SSC_THR = bits_to_send;
a66f26da 325 bits_to_shift = cmd_bits;
8c6cca0b 326 j++;
327 }
8c6cca0b 328 }
a66f26da 329 }
3d2c9c9b 330 WDT_HIT();
a66f26da 331 }
3d2c9c9b 332 // send the remaining bits, padded with 0:
333 bits_to_send = bits_to_shift << (8 - shift_delay);
334 for ( ; ; ) {
335 if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_TXRDY) {
336 AT91C_BASE_SSC->SSC_THR = bits_to_send;
337 break;
338 }
339 }
70b2fc0a 340 LED_C_OFF();
15c4dc5a 341}
342
9455b51c 343
70b2fc0a 344//=============================================================================
8c6cca0b 345// An ISO 15693 decoder for tag responses (one subcarrier only).
d9de20fa 346// Uses cross correlation to identify each bit and EOF.
70b2fc0a 347// This function is called 8 times per bit (every 2 subcarrier cycles).
8c6cca0b 348// Subcarrier frequency fs is 424kHz, 1/fs = 2,36us,
70b2fc0a 349// i.e. function is called every 4,72us
350// LED handling:
351// LED C -> ON once we have received the SOF and are expecting the rest.
352// LED C -> OFF once we have received EOF or are unsynced
353//
354// Returns: true if we received a EOF
355// false if we are still waiting for some more
356//=============================================================================
357
c41dd5f9 358#define NOISE_THRESHOLD 160 // don't try to correlate noise
359#define MAX_PREVIOUS_AMPLITUDE (-1 - NOISE_THRESHOLD)
70b2fc0a 360
8c6cca0b 361typedef struct DecodeTag {
70b2fc0a 362 enum {
d9de20fa 363 STATE_TAG_SOF_LOW,
c41dd5f9 364 STATE_TAG_SOF_RISING_EDGE,
d9de20fa 365 STATE_TAG_SOF_HIGH,
366 STATE_TAG_SOF_HIGH_END,
8c6cca0b 367 STATE_TAG_RECEIVING_DATA,
c41dd5f9 368 STATE_TAG_EOF,
369 STATE_TAG_EOF_TAIL
70b2fc0a 370 } state;
371 int bitCount;
372 int posCount;
373 enum {
374 LOGIC0,
375 LOGIC1,
376 SOF_PART1,
377 SOF_PART2
378 } lastBit;
379 uint16_t shiftReg;
d9de20fa 380 uint16_t max_len;
70b2fc0a 381 uint8_t *output;
382 int len;
383 int sum1, sum2;
c41dd5f9 384 int threshold_sof;
385 int threshold_half;
386 uint16_t previous_amplitude;
8c6cca0b 387} DecodeTag_t;
70b2fc0a 388
d9de20fa 389
cd028159 390static int inline __attribute__((always_inline)) Handle15693SamplesFromTag(uint16_t amplitude, DecodeTag_t *DecodeTag) {
be09ea86 391 switch (DecodeTag->state) {
a66f26da 392 case STATE_TAG_SOF_LOW:
c41dd5f9 393 // waiting for a rising edge
394 if (amplitude > NOISE_THRESHOLD + DecodeTag->previous_amplitude) {
d9de20fa 395 if (DecodeTag->posCount > 10) {
1ce68968 396 DecodeTag->threshold_sof = amplitude - DecodeTag->previous_amplitude; // to be divided by 2
c41dd5f9 397 DecodeTag->threshold_half = 0;
398 DecodeTag->state = STATE_TAG_SOF_RISING_EDGE;
d9de20fa 399 } else {
400 DecodeTag->posCount = 0;
401 }
c41dd5f9 402 } else {
403 DecodeTag->posCount++;
404 DecodeTag->previous_amplitude = amplitude;
15c4dc5a 405 }
d9de20fa 406 break;
a66f26da 407
c41dd5f9 408 case STATE_TAG_SOF_RISING_EDGE:
1ce68968 409 if (amplitude > DecodeTag->threshold_sof + DecodeTag->previous_amplitude) { // edge still rising
410 if (amplitude > DecodeTag->threshold_sof + DecodeTag->threshold_sof) { // steeper edge, take this as time reference
c41dd5f9 411 DecodeTag->posCount = 1;
412 } else {
413 DecodeTag->posCount = 2;
414 }
415 DecodeTag->threshold_sof = (amplitude - DecodeTag->previous_amplitude) / 2;
416 } else {
417 DecodeTag->posCount = 2;
418 DecodeTag->threshold_sof = DecodeTag->threshold_sof/2;
419 }
420 // DecodeTag->posCount = 2;
421 DecodeTag->state = STATE_TAG_SOF_HIGH;
422 break;
ece38ef3 423
d9de20fa 424 case STATE_TAG_SOF_HIGH:
425 // waiting for 10 times high. Take average over the last 8
c41dd5f9 426 if (amplitude > DecodeTag->threshold_sof) {
d9de20fa 427 DecodeTag->posCount++;
428 if (DecodeTag->posCount > 2) {
c41dd5f9 429 DecodeTag->threshold_half += amplitude; // keep track of average high value
d9de20fa 430 }
431 if (DecodeTag->posCount == 10) {
c41dd5f9 432 DecodeTag->threshold_half >>= 2; // (4 times 1/2 average)
d9de20fa 433 DecodeTag->state = STATE_TAG_SOF_HIGH_END;
434 }
435 } else { // high phase was too short
436 DecodeTag->posCount = 1;
ece38ef3 437 DecodeTag->previous_amplitude = amplitude;
d9de20fa 438 DecodeTag->state = STATE_TAG_SOF_LOW;
70b2fc0a 439 }
70b2fc0a 440 break;
441
d9de20fa 442 case STATE_TAG_SOF_HIGH_END:
c41dd5f9 443 // check for falling edge
444 if (DecodeTag->posCount == 13 && amplitude < DecodeTag->threshold_sof) {
d9de20fa 445 DecodeTag->lastBit = SOF_PART1; // detected 1st part of SOF (12 samples low and 12 samples high)
446 DecodeTag->shiftReg = 0;
447 DecodeTag->bitCount = 0;
448 DecodeTag->len = 0;
449 DecodeTag->sum1 = amplitude;
8c6cca0b 450 DecodeTag->sum2 = 0;
451 DecodeTag->posCount = 2;
452 DecodeTag->state = STATE_TAG_RECEIVING_DATA;
1ce68968 453 // FpgaDisableTracing(); // DEBUGGING
496bb4be 454 // Dbprintf("amplitude = %d, threshold_sof = %d, threshold_half/4 = %d, previous_amplitude = %d",
455 // amplitude,
456 // DecodeTag->threshold_sof,
457 // DecodeTag->threshold_half/4,
458 // DecodeTag->previous_amplitude); // DEBUGGING
70b2fc0a 459 LED_C_ON();
d9de20fa 460 } else {
461 DecodeTag->posCount++;
462 if (DecodeTag->posCount > 13) { // high phase too long
463 DecodeTag->posCount = 0;
ece38ef3 464 DecodeTag->previous_amplitude = amplitude;
d9de20fa 465 DecodeTag->state = STATE_TAG_SOF_LOW;
466 LED_C_OFF();
467 }
70b2fc0a 468 }
70b2fc0a 469 break;
15c4dc5a 470
8c6cca0b 471 case STATE_TAG_RECEIVING_DATA:
1ce68968 472 // FpgaDisableTracing(); // DEBUGGING
473 // Dbprintf("amplitude = %d, threshold_sof = %d, threshold_half/4 = %d, previous_amplitude = %d",
474 // amplitude,
475 // DecodeTag->threshold_sof,
476 // DecodeTag->threshold_half/4,
477 // DecodeTag->previous_amplitude); // DEBUGGING
8c6cca0b 478 if (DecodeTag->posCount == 1) {
479 DecodeTag->sum1 = 0;
480 DecodeTag->sum2 = 0;
70b2fc0a 481 }
8c6cca0b 482 if (DecodeTag->posCount <= 4) {
d9de20fa 483 DecodeTag->sum1 += amplitude;
70b2fc0a 484 } else {
d9de20fa 485 DecodeTag->sum2 += amplitude;
70b2fc0a 486 }
8c6cca0b 487 if (DecodeTag->posCount == 8) {
c41dd5f9 488 if (DecodeTag->sum1 > DecodeTag->threshold_half && DecodeTag->sum2 > DecodeTag->threshold_half) { // modulation in both halves
d9de20fa 489 if (DecodeTag->lastBit == LOGIC0) { // this was already part of EOF
490 DecodeTag->state = STATE_TAG_EOF;
491 } else {
492 DecodeTag->posCount = 0;
ece38ef3 493 DecodeTag->previous_amplitude = amplitude;
d9de20fa 494 DecodeTag->state = STATE_TAG_SOF_LOW;
495 LED_C_OFF();
496 }
c41dd5f9 497 } else if (DecodeTag->sum1 < DecodeTag->threshold_half && DecodeTag->sum2 > DecodeTag->threshold_half) { // modulation in second half
70b2fc0a 498 // logic 1
8c6cca0b 499 if (DecodeTag->lastBit == SOF_PART1) { // still part of SOF
d9de20fa 500 DecodeTag->lastBit = SOF_PART2; // SOF completed
70b2fc0a 501 } else {
8c6cca0b 502 DecodeTag->lastBit = LOGIC1;
503 DecodeTag->shiftReg >>= 1;
504 DecodeTag->shiftReg |= 0x80;
505 DecodeTag->bitCount++;
506 if (DecodeTag->bitCount == 8) {
507 DecodeTag->output[DecodeTag->len] = DecodeTag->shiftReg;
508 DecodeTag->len++;
c41dd5f9 509 // if (DecodeTag->shiftReg == 0x12 && DecodeTag->len == 1) FpgaDisableTracing(); // DEBUGGING
d9de20fa 510 if (DecodeTag->len > DecodeTag->max_len) {
511 // buffer overflow, give up
d9de20fa 512 LED_C_OFF();
c41dd5f9 513 return true;
d9de20fa 514 }
8c6cca0b 515 DecodeTag->bitCount = 0;
516 DecodeTag->shiftReg = 0;
70b2fc0a 517 }
518 }
c41dd5f9 519 } else if (DecodeTag->sum1 > DecodeTag->threshold_half && DecodeTag->sum2 < DecodeTag->threshold_half) { // modulation in first half
70b2fc0a 520 // logic 0
8c6cca0b 521 if (DecodeTag->lastBit == SOF_PART1) { // incomplete SOF
d9de20fa 522 DecodeTag->posCount = 0;
ece38ef3 523 DecodeTag->previous_amplitude = amplitude;
d9de20fa 524 DecodeTag->state = STATE_TAG_SOF_LOW;
70b2fc0a 525 LED_C_OFF();
526 } else {
8c6cca0b 527 DecodeTag->lastBit = LOGIC0;
528 DecodeTag->shiftReg >>= 1;
529 DecodeTag->bitCount++;
530 if (DecodeTag->bitCount == 8) {
531 DecodeTag->output[DecodeTag->len] = DecodeTag->shiftReg;
532 DecodeTag->len++;
c41dd5f9 533 // if (DecodeTag->shiftReg == 0x12 && DecodeTag->len == 1) FpgaDisableTracing(); // DEBUGGING
d9de20fa 534 if (DecodeTag->len > DecodeTag->max_len) {
535 // buffer overflow, give up
536 DecodeTag->posCount = 0;
ece38ef3 537 DecodeTag->previous_amplitude = amplitude;
d9de20fa 538 DecodeTag->state = STATE_TAG_SOF_LOW;
539 LED_C_OFF();
540 }
8c6cca0b 541 DecodeTag->bitCount = 0;
542 DecodeTag->shiftReg = 0;
70b2fc0a 543 }
544 }
c41dd5f9 545 } else { // no modulation
546 if (DecodeTag->lastBit == SOF_PART2) { // only SOF (this is OK for iClass)
547 LED_C_OFF();
548 return true;
549 } else {
550 DecodeTag->posCount = 0;
551 DecodeTag->state = STATE_TAG_SOF_LOW;
552 LED_C_OFF();
553 }
70b2fc0a 554 }
8c6cca0b 555 DecodeTag->posCount = 0;
70b2fc0a 556 }
8c6cca0b 557 DecodeTag->posCount++;
70b2fc0a 558 break;
8c6cca0b 559
d9de20fa 560 case STATE_TAG_EOF:
561 if (DecodeTag->posCount == 1) {
562 DecodeTag->sum1 = 0;
563 DecodeTag->sum2 = 0;
564 }
565 if (DecodeTag->posCount <= 4) {
566 DecodeTag->sum1 += amplitude;
70b2fc0a 567 } else {
d9de20fa 568 DecodeTag->sum2 += amplitude;
70b2fc0a 569 }
d9de20fa 570 if (DecodeTag->posCount == 8) {
c41dd5f9 571 if (DecodeTag->sum1 > DecodeTag->threshold_half && DecodeTag->sum2 < DecodeTag->threshold_half) { // modulation in first half
d9de20fa 572 DecodeTag->posCount = 0;
c41dd5f9 573 DecodeTag->state = STATE_TAG_EOF_TAIL;
574 } else {
575 DecodeTag->posCount = 0;
ece38ef3 576 DecodeTag->previous_amplitude = amplitude;
d9de20fa 577 DecodeTag->state = STATE_TAG_SOF_LOW;
578 LED_C_OFF();
c41dd5f9 579 }
580 }
581 DecodeTag->posCount++;
582 break;
583
584 case STATE_TAG_EOF_TAIL:
585 if (DecodeTag->posCount == 1) {
586 DecodeTag->sum1 = 0;
587 DecodeTag->sum2 = 0;
588 }
589 if (DecodeTag->posCount <= 4) {
590 DecodeTag->sum1 += amplitude;
591 } else {
592 DecodeTag->sum2 += amplitude;
593 }
594 if (DecodeTag->posCount == 8) {
595 if (DecodeTag->sum1 < DecodeTag->threshold_half && DecodeTag->sum2 < DecodeTag->threshold_half) { // no modulation in both halves
d9de20fa 596 LED_C_OFF();
597 return true;
c41dd5f9 598 } else {
599 DecodeTag->posCount = 0;
ece38ef3 600 DecodeTag->previous_amplitude = amplitude;
c41dd5f9 601 DecodeTag->state = STATE_TAG_SOF_LOW;
602 LED_C_OFF();
d9de20fa 603 }
604 }
605 DecodeTag->posCount++;
70b2fc0a 606 break;
15c4dc5a 607 }
15c4dc5a 608
70b2fc0a 609 return false;
610}
15c4dc5a 611
15c4dc5a 612
ece38ef3 613static void DecodeTagInit(DecodeTag_t *DecodeTag, uint8_t *data, uint16_t max_len) {
c41dd5f9 614 DecodeTag->previous_amplitude = MAX_PREVIOUS_AMPLITUDE;
d9de20fa 615 DecodeTag->posCount = 0;
616 DecodeTag->state = STATE_TAG_SOF_LOW;
8c6cca0b 617 DecodeTag->output = data;
d9de20fa 618 DecodeTag->max_len = max_len;
619}
620
621
ece38ef3 622static void DecodeTagReset(DecodeTag_t *DecodeTag) {
d9de20fa 623 DecodeTag->posCount = 0;
624 DecodeTag->state = STATE_TAG_SOF_LOW;
c41dd5f9 625 DecodeTag->previous_amplitude = MAX_PREVIOUS_AMPLITUDE;
70b2fc0a 626}
627
d9de20fa 628
70b2fc0a 629/*
8c6cca0b 630 * Receive and decode the tag response, also log to tracebuffer
70b2fc0a 631 */
c41dd5f9 632int GetIso15693AnswerFromTag(uint8_t* response, uint16_t max_len, uint16_t timeout, uint32_t *eof_time) {
633
d9de20fa 634 int samples = 0;
c41dd5f9 635 int ret = 0;
70b2fc0a 636
c41dd5f9 637 uint16_t dmaBuf[ISO15693_DMA_BUFFER_SIZE];
a66f26da 638
8c6cca0b 639 // the Decoder data structure
d9de20fa 640 DecodeTag_t DecodeTag = { 0 };
641 DecodeTagInit(&DecodeTag, response, max_len);
70b2fc0a 642
643 // wait for last transfer to complete
644 while (!(AT91C_BASE_SSC->SSC_SR & AT91C_SSC_TXEMPTY));
645
646 // And put the FPGA in the appropriate mode
5ea2a248 647 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER | FPGA_HF_READER_SUBCARRIER_424_KHZ | FPGA_HF_READER_MODE_RECEIVE_AMPLITUDE);
70b2fc0a 648
649 // Setup and start DMA.
5ea2a248 650 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER);
70b2fc0a 651 FpgaSetupSscDma((uint8_t*) dmaBuf, ISO15693_DMA_BUFFER_SIZE);
c41dd5f9 652 uint32_t dma_start_time = 0;
70b2fc0a 653 uint16_t *upTo = dmaBuf;
70b2fc0a 654
655 for(;;) {
d9de20fa 656 uint16_t behindBy = ((uint16_t*)AT91C_BASE_PDC_SSC->PDC_RPR - upTo) & (ISO15693_DMA_BUFFER_SIZE-1);
70b2fc0a 657
d9de20fa 658 if (behindBy == 0) continue;
8c6cca0b 659
c41dd5f9 660 samples++;
661 if (samples == 1) {
ece38ef3 662 // DMA has transferred the very first data
c41dd5f9 663 dma_start_time = GetCountSspClk() & 0xfffffff0;
664 }
ece38ef3 665
d9de20fa 666 uint16_t tagdata = *upTo++;
70b2fc0a 667
70b2fc0a 668 if(upTo >= dmaBuf + ISO15693_DMA_BUFFER_SIZE) { // we have read all of the DMA buffer content.
669 upTo = dmaBuf; // start reading the circular buffer from the beginning
1ce68968 670 if (behindBy > (9*ISO15693_DMA_BUFFER_SIZE/10)) {
d9de20fa 671 Dbprintf("About to blow circular buffer - aborted! behindBy=%d", behindBy);
c41dd5f9 672 ret = -1;
d9de20fa 673 break;
674 }
15c4dc5a 675 }
70b2fc0a 676 if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_ENDRX)) { // DMA Counter Register had reached 0, already rotated.
677 AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) dmaBuf; // refresh the DMA Next Buffer and
678 AT91C_BASE_PDC_SSC->PDC_RNCR = ISO15693_DMA_BUFFER_SIZE; // DMA Next Counter registers
15c4dc5a 679 }
d9de20fa 680
d9de20fa 681 if (Handle15693SamplesFromTag(tagdata, &DecodeTag)) {
c41dd5f9 682 *eof_time = dma_start_time + samples*16 - DELAY_TAG_TO_ARM; // end of EOF
683 if (DecodeTag.lastBit == SOF_PART2) {
684 *eof_time -= 8*16; // needed 8 additional samples to confirm single SOF (iCLASS)
685 }
686 if (DecodeTag.len > DecodeTag.max_len) {
687 ret = -2; // buffer overflow
688 }
70b2fc0a 689 break;
690 }
15c4dc5a 691
d9de20fa 692 if (samples > timeout && DecodeTag.state < STATE_TAG_RECEIVING_DATA) {
ece38ef3 693 ret = -1; // timeout
70b2fc0a 694 break;
695 }
8c6cca0b 696
70b2fc0a 697 }
698
699 FpgaDisableSscDma();
a66f26da 700
c41dd5f9 701 if (DEBUG) Dbprintf("samples = %d, ret = %d, Decoder: state = %d, lastBit = %d, len = %d, bitCount = %d, posCount = %d",
702 samples, ret, DecodeTag.state, DecodeTag.lastBit, DecodeTag.len, DecodeTag.bitCount, DecodeTag.posCount);
70b2fc0a 703
c41dd5f9 704 if (ret < 0) {
705 return ret;
70b2fc0a 706 }
707
c41dd5f9 708 uint32_t sof_time = *eof_time
709 - DecodeTag.len * 8 * 8 * 16 // time for byte transfers
710 - 32 * 16 // time for SOF transfer
711 - (DecodeTag.lastBit != SOF_PART2?32*16:0); // time for EOF transfer
ece38ef3 712
c41dd5f9 713 if (DEBUG) Dbprintf("timing: sof_time = %d, eof_time = %d", sof_time, *eof_time);
ece38ef3 714
c41dd5f9 715 LogTrace_ISO15693(DecodeTag.output, DecodeTag.len, sof_time*4, *eof_time*4, NULL, false);
716
8c6cca0b 717 return DecodeTag.len;
15c4dc5a 718}
719
9455b51c 720
8c6cca0b 721//=============================================================================
722// An ISO15693 decoder for reader commands.
723//
724// This function is called 4 times per bit (every 2 subcarrier cycles).
725// Subcarrier frequency fs is 848kHz, 1/fs = 1,18us, i.e. function is called every 2,36us
726// LED handling:
727// LED B -> ON once we have received the SOF and are expecting the rest.
728// LED B -> OFF once we have received EOF or are in error state or unsynced
729//
730// Returns: true if we received a EOF
731// false if we are still waiting for some more
732//=============================================================================
733
734typedef struct DecodeReader {
735 enum {
736 STATE_READER_UNSYNCD,
5b12974a 737 STATE_READER_AWAIT_1ST_FALLING_EDGE_OF_SOF,
8c6cca0b 738 STATE_READER_AWAIT_1ST_RISING_EDGE_OF_SOF,
739 STATE_READER_AWAIT_2ND_FALLING_EDGE_OF_SOF,
740 STATE_READER_AWAIT_2ND_RISING_EDGE_OF_SOF,
741 STATE_READER_AWAIT_END_OF_SOF_1_OUT_OF_4,
742 STATE_READER_RECEIVE_DATA_1_OUT_OF_4,
cd028159 743 STATE_READER_RECEIVE_DATA_1_OUT_OF_256,
744 STATE_READER_RECEIVE_JAMMING
8c6cca0b 745 } state;
746 enum {
747 CODING_1_OUT_OF_4,
748 CODING_1_OUT_OF_256
749 } Coding;
750 uint8_t shiftReg;
751 uint8_t bitCount;
752 int byteCount;
753 int byteCountMax;
754 int posCount;
a66f26da 755 int sum1, sum2;
8c6cca0b 756 uint8_t *output;
be09ea86 757 uint8_t jam_search_len;
758 uint8_t *jam_search_string;
8c6cca0b 759} DecodeReader_t;
760
761
be09ea86 762static void DecodeReaderInit(DecodeReader_t* DecodeReader, uint8_t *data, uint16_t max_len, uint8_t jam_search_len, uint8_t *jam_search_string) {
d9de20fa 763 DecodeReader->output = data;
764 DecodeReader->byteCountMax = max_len;
765 DecodeReader->state = STATE_READER_UNSYNCD;
766 DecodeReader->byteCount = 0;
767 DecodeReader->bitCount = 0;
768 DecodeReader->posCount = 1;
769 DecodeReader->shiftReg = 0;
be09ea86 770 DecodeReader->jam_search_len = jam_search_len;
771 DecodeReader->jam_search_string = jam_search_string;
d9de20fa 772}
773
774
be09ea86 775static void DecodeReaderReset(DecodeReader_t* DecodeReader) {
d9de20fa 776 DecodeReader->state = STATE_READER_UNSYNCD;
777}
778
779
cd028159 780static int inline __attribute__((always_inline)) Handle15693SampleFromReader(bool bit, DecodeReader_t *DecodeReader) {
3d2c9c9b 781 switch (DecodeReader->state) {
8c6cca0b 782 case STATE_READER_UNSYNCD:
5b12974a 783 // wait for unmodulated carrier
784 if (bit) {
785 DecodeReader->state = STATE_READER_AWAIT_1ST_FALLING_EDGE_OF_SOF;
786 }
787 break;
788
789 case STATE_READER_AWAIT_1ST_FALLING_EDGE_OF_SOF:
3d2c9c9b 790 if (!bit) {
8c6cca0b 791 // we went low, so this could be the beginning of a SOF
8c6cca0b 792 DecodeReader->posCount = 1;
d9de20fa 793 DecodeReader->state = STATE_READER_AWAIT_1ST_RISING_EDGE_OF_SOF;
8c6cca0b 794 }
795 break;
15c4dc5a 796
8c6cca0b 797 case STATE_READER_AWAIT_1ST_RISING_EDGE_OF_SOF:
798 DecodeReader->posCount++;
3d2c9c9b 799 if (bit) { // detected rising edge
800 if (DecodeReader->posCount < 4) { // rising edge too early (nominally expected at 5)
5b12974a 801 DecodeReader->state = STATE_READER_AWAIT_1ST_FALLING_EDGE_OF_SOF;
8c6cca0b 802 } else { // SOF
803 DecodeReader->state = STATE_READER_AWAIT_2ND_FALLING_EDGE_OF_SOF;
804 }
805 } else {
3d2c9c9b 806 if (DecodeReader->posCount > 5) { // stayed low for too long
d9de20fa 807 DecodeReaderReset(DecodeReader);
8c6cca0b 808 } else {
809 // do nothing, keep waiting
810 }
811 }
812 break;
813
814 case STATE_READER_AWAIT_2ND_FALLING_EDGE_OF_SOF:
815 DecodeReader->posCount++;
3d2c9c9b 816 if (!bit) { // detected a falling edge
8c6cca0b 817 if (DecodeReader->posCount < 20) { // falling edge too early (nominally expected at 21 earliest)
d9de20fa 818 DecodeReaderReset(DecodeReader);
8c6cca0b 819 } else if (DecodeReader->posCount < 23) { // SOF for 1 out of 4 coding
820 DecodeReader->Coding = CODING_1_OUT_OF_4;
821 DecodeReader->state = STATE_READER_AWAIT_2ND_RISING_EDGE_OF_SOF;
822 } else if (DecodeReader->posCount < 28) { // falling edge too early (nominally expected at 29 latest)
d9de20fa 823 DecodeReaderReset(DecodeReader);
5b12974a 824 } else { // SOF for 1 out of 256 coding
8c6cca0b 825 DecodeReader->Coding = CODING_1_OUT_OF_256;
826 DecodeReader->state = STATE_READER_AWAIT_2ND_RISING_EDGE_OF_SOF;
827 }
828 } else {
3d2c9c9b 829 if (DecodeReader->posCount > 29) { // stayed high for too long
5b12974a 830 DecodeReader->state = STATE_READER_AWAIT_1ST_FALLING_EDGE_OF_SOF;
8c6cca0b 831 } else {
832 // do nothing, keep waiting
833 }
834 }
835 break;
836
837 case STATE_READER_AWAIT_2ND_RISING_EDGE_OF_SOF:
838 DecodeReader->posCount++;
839 if (bit) { // detected rising edge
840 if (DecodeReader->Coding == CODING_1_OUT_OF_256) {
841 if (DecodeReader->posCount < 32) { // rising edge too early (nominally expected at 33)
5b12974a 842 DecodeReader->state = STATE_READER_AWAIT_1ST_FALLING_EDGE_OF_SOF;
8c6cca0b 843 } else {
844 DecodeReader->posCount = 1;
845 DecodeReader->bitCount = 0;
846 DecodeReader->byteCount = 0;
847 DecodeReader->sum1 = 1;
848 DecodeReader->state = STATE_READER_RECEIVE_DATA_1_OUT_OF_256;
849 LED_B_ON();
850 }
851 } else { // CODING_1_OUT_OF_4
852 if (DecodeReader->posCount < 24) { // rising edge too early (nominally expected at 25)
5b12974a 853 DecodeReader->state = STATE_READER_AWAIT_1ST_FALLING_EDGE_OF_SOF;
8c6cca0b 854 } else {
5b12974a 855 DecodeReader->posCount = 1;
8c6cca0b 856 DecodeReader->state = STATE_READER_AWAIT_END_OF_SOF_1_OUT_OF_4;
857 }
858 }
859 } else {
860 if (DecodeReader->Coding == CODING_1_OUT_OF_256) {
861 if (DecodeReader->posCount > 34) { // signal stayed low for too long
5b12974a 862 DecodeReaderReset(DecodeReader);
8c6cca0b 863 } else {
864 // do nothing, keep waiting
865 }
866 } else { // CODING_1_OUT_OF_4
867 if (DecodeReader->posCount > 26) { // signal stayed low for too long
5b12974a 868 DecodeReaderReset(DecodeReader);
8c6cca0b 869 } else {
870 // do nothing, keep waiting
871 }
872 }
873 }
874 break;
875
876 case STATE_READER_AWAIT_END_OF_SOF_1_OUT_OF_4:
877 DecodeReader->posCount++;
878 if (bit) {
5b12974a 879 if (DecodeReader->posCount == 9) {
8c6cca0b 880 DecodeReader->posCount = 1;
881 DecodeReader->bitCount = 0;
882 DecodeReader->byteCount = 0;
883 DecodeReader->sum1 = 1;
884 DecodeReader->state = STATE_READER_RECEIVE_DATA_1_OUT_OF_4;
885 LED_B_ON();
886 } else {
887 // do nothing, keep waiting
888 }
889 } else { // unexpected falling edge
d9de20fa 890 DecodeReaderReset(DecodeReader);
8c6cca0b 891 }
892 break;
893
894 case STATE_READER_RECEIVE_DATA_1_OUT_OF_4:
895 DecodeReader->posCount++;
896 if (DecodeReader->posCount == 1) {
be09ea86 897 DecodeReader->sum1 = bit?1:0;
8c6cca0b 898 } else if (DecodeReader->posCount <= 4) {
be09ea86 899 if (bit) DecodeReader->sum1++;
8c6cca0b 900 } else if (DecodeReader->posCount == 5) {
be09ea86 901 DecodeReader->sum2 = bit?1:0;
8c6cca0b 902 } else {
be09ea86 903 if (bit) DecodeReader->sum2++;
8c6cca0b 904 }
905 if (DecodeReader->posCount == 8) {
906 DecodeReader->posCount = 0;
e49d31c0 907 if (DecodeReader->sum1 <= 1 && DecodeReader->sum2 >= 3) { // EOF
8c6cca0b 908 LED_B_OFF(); // Finished receiving
d9de20fa 909 DecodeReaderReset(DecodeReader);
8c6cca0b 910 if (DecodeReader->byteCount != 0) {
911 return true;
912 }
be09ea86 913 } else if (DecodeReader->sum1 >= 3 && DecodeReader->sum2 <= 1) { // detected a 2bit position
8c6cca0b 914 DecodeReader->shiftReg >>= 2;
915 DecodeReader->shiftReg |= (DecodeReader->bitCount << 6);
916 }
917 if (DecodeReader->bitCount == 15) { // we have a full byte
918 DecodeReader->output[DecodeReader->byteCount++] = DecodeReader->shiftReg;
919 if (DecodeReader->byteCount > DecodeReader->byteCountMax) {
920 // buffer overflow, give up
921 LED_B_OFF();
d9de20fa 922 DecodeReaderReset(DecodeReader);
8c6cca0b 923 }
924 DecodeReader->bitCount = 0;
d9de20fa 925 DecodeReader->shiftReg = 0;
cd028159 926 if (DecodeReader->byteCount == DecodeReader->jam_search_len) {
927 if (!memcmp(DecodeReader->output, DecodeReader->jam_search_string, DecodeReader->jam_search_len)) {
928 LED_D_ON();
929 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER | FPGA_HF_READER_MODE_SEND_JAM);
930 DecodeReader->state = STATE_READER_RECEIVE_JAMMING;
931 }
932 }
8c6cca0b 933 } else {
934 DecodeReader->bitCount++;
935 }
936 }
937 break;
938
939 case STATE_READER_RECEIVE_DATA_1_OUT_OF_256:
940 DecodeReader->posCount++;
941 if (DecodeReader->posCount == 1) {
be09ea86 942 DecodeReader->sum1 = bit?1:0;
8c6cca0b 943 } else if (DecodeReader->posCount <= 4) {
be09ea86 944 if (bit) DecodeReader->sum1++;
8c6cca0b 945 } else if (DecodeReader->posCount == 5) {
be09ea86 946 DecodeReader->sum2 = bit?1:0;
947 } else if (bit) {
948 DecodeReader->sum2++;
8c6cca0b 949 }
950 if (DecodeReader->posCount == 8) {
951 DecodeReader->posCount = 0;
e49d31c0 952 if (DecodeReader->sum1 <= 1 && DecodeReader->sum2 >= 3) { // EOF
8c6cca0b 953 LED_B_OFF(); // Finished receiving
d9de20fa 954 DecodeReaderReset(DecodeReader);
8c6cca0b 955 if (DecodeReader->byteCount != 0) {
956 return true;
957 }
be09ea86 958 } else if (DecodeReader->sum1 >= 3 && DecodeReader->sum2 <= 1) { // detected the bit position
8c6cca0b 959 DecodeReader->shiftReg = DecodeReader->bitCount;
960 }
961 if (DecodeReader->bitCount == 255) { // we have a full byte
962 DecodeReader->output[DecodeReader->byteCount++] = DecodeReader->shiftReg;
963 if (DecodeReader->byteCount > DecodeReader->byteCountMax) {
964 // buffer overflow, give up
965 LED_B_OFF();
d9de20fa 966 DecodeReaderReset(DecodeReader);
8c6cca0b 967 }
cd028159 968 if (DecodeReader->byteCount == DecodeReader->jam_search_len) {
969 if (!memcmp(DecodeReader->output, DecodeReader->jam_search_string, DecodeReader->jam_search_len)) {
970 LED_D_ON();
971 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER | FPGA_HF_READER_MODE_SEND_JAM);
972 DecodeReader->state = STATE_READER_RECEIVE_JAMMING;
973 }
974 }
8c6cca0b 975 }
976 DecodeReader->bitCount++;
977 }
978 break;
979
cd028159 980 case STATE_READER_RECEIVE_JAMMING:
981 DecodeReader->posCount++;
982 if (DecodeReader->Coding == CODING_1_OUT_OF_4) {
983 if (DecodeReader->posCount == 7*16) { // 7 bits jammed
984 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER | FPGA_HF_READER_MODE_SNOOP_AMPLITUDE); // stop jamming
985 // FpgaDisableTracing();
986 LED_D_OFF();
987 } else if (DecodeReader->posCount == 8*16) {
988 DecodeReader->posCount = 0;
989 DecodeReader->output[DecodeReader->byteCount++] = 0x00;
990 DecodeReader->state = STATE_READER_RECEIVE_DATA_1_OUT_OF_4;
991 }
992 } else {
993 if (DecodeReader->posCount == 7*256) { // 7 bits jammend
994 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER | FPGA_HF_READER_MODE_SNOOP_AMPLITUDE); // stop jamming
995 LED_D_OFF();
996 } else if (DecodeReader->posCount == 8*256) {
997 DecodeReader->posCount = 0;
998 DecodeReader->output[DecodeReader->byteCount++] = 0x00;
999 DecodeReader->state = STATE_READER_RECEIVE_DATA_1_OUT_OF_256;
1000 }
1001 }
1002 break;
1003
8c6cca0b 1004 default:
1005 LED_B_OFF();
d9de20fa 1006 DecodeReaderReset(DecodeReader);
8c6cca0b 1007 break;
15c4dc5a 1008 }
8c6cca0b 1009
1010 return false;
1011}
1012
1013
8c6cca0b 1014//-----------------------------------------------------------------------------
1015// Receive a command (from the reader to us, where we are the simulated tag),
1016// and store it in the given buffer, up to the given maximum length. Keeps
1017// spinning, waiting for a well-framed command, until either we get one
3d2c9c9b 1018// (returns len) or someone presses the pushbutton on the board (returns -1).
8c6cca0b 1019//
1020// Assume that we're called with the SSC (to the FPGA) and ADC path set
1021// correctly.
1022//-----------------------------------------------------------------------------
1023
3d2c9c9b 1024int GetIso15693CommandFromReader(uint8_t *received, size_t max_len, uint32_t *eof_time) {
d9de20fa 1025 int samples = 0;
8c6cca0b 1026 bool gotFrame = false;
1027 uint8_t b;
1028
3d2c9c9b 1029 uint8_t dmaBuf[ISO15693_DMA_BUFFER_SIZE];
8c6cca0b 1030
1031 // the decoder data structure
6eeb5f1c 1032 DecodeReader_t DecodeReader = {0};
be09ea86 1033 DecodeReaderInit(&DecodeReader, received, max_len, 0, NULL);
8c6cca0b 1034
1035 // wait for last transfer to complete
1036 while (!(AT91C_BASE_SSC->SSC_SR & AT91C_SSC_TXEMPTY));
1037
70b2fc0a 1038 LED_D_OFF();
8c6cca0b 1039 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR | FPGA_HF_SIMULATOR_NO_MODULATION);
15c4dc5a 1040
8c6cca0b 1041 // clear receive register and wait for next transfer
1042 uint32_t temp = AT91C_BASE_SSC->SSC_RHR;
1043 (void) temp;
1044 while (!(AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY)) ;
15c4dc5a 1045
3d2c9c9b 1046 uint32_t dma_start_time = GetCountSspClk() & 0xfffffff8;
15c4dc5a 1047
8c6cca0b 1048 // Setup and start DMA.
1049 FpgaSetupSscDma(dmaBuf, ISO15693_DMA_BUFFER_SIZE);
1050 uint8_t *upTo = dmaBuf;
15c4dc5a 1051
3d2c9c9b 1052 for (;;) {
d9de20fa 1053 uint16_t behindBy = ((uint8_t*)AT91C_BASE_PDC_SSC->PDC_RPR - upTo) & (ISO15693_DMA_BUFFER_SIZE-1);
70b2fc0a 1054
d9de20fa 1055 if (behindBy == 0) continue;
15c4dc5a 1056
8c6cca0b 1057 b = *upTo++;
3d2c9c9b 1058 if (upTo >= dmaBuf + ISO15693_DMA_BUFFER_SIZE) { // we have read all of the DMA buffer content.
8c6cca0b 1059 upTo = dmaBuf; // start reading the circular buffer from the beginning
3d2c9c9b 1060 if (behindBy > (9*ISO15693_DMA_BUFFER_SIZE/10)) {
d9de20fa 1061 Dbprintf("About to blow circular buffer - aborted! behindBy=%d", behindBy);
1062 break;
1063 }
8c6cca0b 1064 }
1065 if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_ENDRX)) { // DMA Counter Register had reached 0, already rotated.
1066 AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) dmaBuf; // refresh the DMA Next Buffer and
1067 AT91C_BASE_PDC_SSC->PDC_RNCR = ISO15693_DMA_BUFFER_SIZE; // DMA Next Counter registers
1068 }
15c4dc5a 1069
8c6cca0b 1070 for (int i = 7; i >= 0; i--) {
1071 if (Handle15693SampleFromReader((b >> i) & 0x01, &DecodeReader)) {
c41dd5f9 1072 *eof_time = dma_start_time + samples - DELAY_READER_TO_ARM; // end of EOF
8c6cca0b 1073 gotFrame = true;
9455b51c 1074 break;
1075 }
8c6cca0b 1076 samples++;
15c4dc5a 1077 }
8c6cca0b 1078
1079 if (gotFrame) {
1080 break;
15c4dc5a 1081 }
8c6cca0b 1082
1083 if (BUTTON_PRESS()) {
3d2c9c9b 1084 DecodeReader.byteCount = -1;
8c6cca0b 1085 break;
15c4dc5a 1086 }
15c4dc5a 1087
8c6cca0b 1088 WDT_HIT();
1089 }
1090
8c6cca0b 1091 FpgaDisableSscDma();
a66f26da 1092
d9de20fa 1093 if (DEBUG) Dbprintf("samples = %d, gotFrame = %d, Decoder: state = %d, len = %d, bitCount = %d, posCount = %d",
a66f26da 1094 samples, gotFrame, DecodeReader.state, DecodeReader.byteCount, DecodeReader.bitCount, DecodeReader.posCount);
8c6cca0b 1095
d9de20fa 1096 if (DecodeReader.byteCount > 0) {
a66f26da 1097 uint32_t sof_time = *eof_time
3d2c9c9b 1098 - DecodeReader.byteCount * (DecodeReader.Coding==CODING_1_OUT_OF_4?128:2048) // time for byte transfers
1099 - 32 // time for SOF transfer
1100 - 16; // time for EOF transfer
c41dd5f9 1101 LogTrace_ISO15693(DecodeReader.output, DecodeReader.byteCount, sof_time*32, *eof_time*32, NULL, true);
8c6cca0b 1102 }
1103
1104 return DecodeReader.byteCount;
15c4dc5a 1105}
1106
9455b51c 1107
d9de20fa 1108// Encode (into the ToSend buffers) an identify request, which is the first
1109// thing that you must send to a tag to get a response.
1110static void BuildIdentifyRequest(void)
1111{
1112 uint8_t cmd[5];
1113
1114 uint16_t crc;
1115 // one sub-carrier, inventory, 1 slot, fast rate
1116 // AFI is at bit 5 (1<<4) when doing an INVENTORY
1117 cmd[0] = (1 << 2) | (1 << 5) | (1 << 1);
1118 // inventory command code
1119 cmd[1] = 0x01;
1120 // no mask
1121 cmd[2] = 0x00;
1122 //Now the CRC
3d2c9c9b 1123 crc = Iso15693Crc(cmd, 3);
d9de20fa 1124 cmd[3] = crc & 0xff;
1125 cmd[4] = crc >> 8;
1126
1127 CodeIso15693AsReader(cmd, sizeof(cmd));
1128}
1129
1130
15c4dc5a 1131//-----------------------------------------------------------------------------
1132// Start to read an ISO 15693 tag. We send an identify request, then wait
1133// for the response. The response is not demodulated, just left in the buffer
1134// so that it can be downloaded to a PC and processed there.
1135//-----------------------------------------------------------------------------
1136void AcquireRawAdcSamplesIso15693(void)
1137{
70b2fc0a 1138 LED_A_ON();
8c6cca0b 1139
117d9ec2 1140 uint8_t *dest = BigBuf_get_addr();
15c4dc5a 1141
7cc204bf 1142 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
5ea2a248 1143 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER);
ece38ef3 1144 LED_D_ON();
5ea2a248 1145 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER);
15c4dc5a 1146 SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
1147
5ea2a248 1148 BuildIdentifyRequest();
1149
15c4dc5a 1150 // Give the tags time to energize
15c4dc5a 1151 SpinDelay(100);
1152
1153 // Now send the command
c41dd5f9 1154 uint32_t start_time = 0;
1155 TransmitTo15693Tag(ToSend, ToSendMax, &start_time);
70b2fc0a 1156
1157 // wait for last transfer to complete
5ea2a248 1158 while (!(AT91C_BASE_SSC->SSC_SR & AT91C_SSC_TXEMPTY)) ;
15c4dc5a 1159
5ea2a248 1160 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER | FPGA_HF_READER_SUBCARRIER_424_KHZ | FPGA_HF_READER_MODE_RECEIVE_AMPLITUDE);
15c4dc5a 1161
70b2fc0a 1162 for(int c = 0; c < 4000; ) {
15c4dc5a 1163 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
d9de20fa 1164 uint16_t r = AT91C_BASE_SSC->SSC_RHR;
1165 dest[c++] = r >> 5;
9455b51c 1166 }
1167 }
70b2fc0a 1168
1169 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1170 LEDsoff();
9455b51c 1171}
1172
1173
be09ea86 1174void SnoopIso15693(uint8_t jam_search_len, uint8_t *jam_search_string) {
1ce68968 1175
1523527f 1176 LED_A_ON();
be09ea86 1177
d9de20fa 1178 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
8c6cca0b 1179
d9de20fa 1180 clear_trace();
1181 set_tracing(true);
3fe4ff4f 1182
d9de20fa 1183 // The DMA buffer, used to stream samples from the FPGA
1ce68968 1184 uint16_t dmaBuf[ISO15693_DMA_BUFFER_SIZE];
d9de20fa 1185
1186 // Count of samples received so far, so that we can include timing
1187 // information in the trace buffer.
1188 int samples = 0;
1189
1190 DecodeTag_t DecodeTag = {0};
1191 uint8_t response[ISO15693_MAX_RESPONSE_LENGTH];
1192 DecodeTagInit(&DecodeTag, response, sizeof(response));
9455b51c 1193
be09ea86 1194 DecodeReader_t DecodeReader = {0};
d9de20fa 1195 uint8_t cmd[ISO15693_MAX_COMMAND_LENGTH];
be09ea86 1196 DecodeReaderInit(&DecodeReader, cmd, sizeof(cmd), jam_search_len, jam_search_string);
d9de20fa 1197
1198 // Print some debug information about the buffer sizes
1199 if (DEBUG) {
1200 Dbprintf("Snooping buffers initialized:");
1201 Dbprintf(" Trace: %i bytes", BigBuf_max_traceLen());
1202 Dbprintf(" Reader -> tag: %i bytes", ISO15693_MAX_COMMAND_LENGTH);
1203 Dbprintf(" tag -> Reader: %i bytes", ISO15693_MAX_RESPONSE_LENGTH);
1204 Dbprintf(" DMA: %i bytes", ISO15693_DMA_BUFFER_SIZE * sizeof(uint16_t));
1205 }
5ea2a248 1206 Dbprintf("Snoop started. Press PM3 Button to stop.");
a66f26da 1207
5ea2a248 1208 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER | FPGA_HF_READER_MODE_SNOOP_AMPLITUDE);
1ce68968 1209 LED_D_OFF();
9455b51c 1210 SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
5ea2a248 1211 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER);
1ce68968 1212 StartCountSspClk();
d9de20fa 1213 FpgaSetupSscDma((uint8_t*) dmaBuf, ISO15693_DMA_BUFFER_SIZE);
be09ea86 1214
d9de20fa 1215 bool TagIsActive = false;
1216 bool ReaderIsActive = false;
1217 bool ExpectTagAnswer = false;
1ce68968 1218 uint32_t dma_start_time = 0;
1219 uint16_t *upTo = dmaBuf;
be09ea86 1220
1221 uint16_t max_behindBy = 0;
1ce68968 1222
d9de20fa 1223 // And now we loop, receiving samples.
1224 for(;;) {
1225 uint16_t behindBy = ((uint16_t*)AT91C_BASE_PDC_SSC->PDC_RPR - upTo) & (ISO15693_DMA_BUFFER_SIZE-1);
be09ea86 1226 if (behindBy > max_behindBy) {
1227 max_behindBy = behindBy;
1228 }
1229
d9de20fa 1230 if (behindBy == 0) continue;
1231
1ce68968 1232 samples++;
1233 if (samples == 1) {
1234 // DMA has transferred the very first data
1235 dma_start_time = GetCountSspClk() & 0xfffffff0;
1236 }
be09ea86 1237
d9de20fa 1238 uint16_t snoopdata = *upTo++;
1239
1ce68968 1240 if (upTo >= dmaBuf + ISO15693_DMA_BUFFER_SIZE) { // we have read all of the DMA buffer content.
d9de20fa 1241 upTo = dmaBuf; // start reading the circular buffer from the beginning
1ce68968 1242 if (behindBy > (9*ISO15693_DMA_BUFFER_SIZE/10)) {
cd028159 1243 // FpgaDisableTracing();
d9de20fa 1244 Dbprintf("About to blow circular buffer - aborted! behindBy=%d, samples=%d", behindBy, samples);
1245 break;
1246 }
1247 if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_ENDRX)) { // DMA Counter Register had reached 0, already rotated.
1248 AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) dmaBuf; // refresh the DMA Next Buffer and
1249 AT91C_BASE_PDC_SSC->PDC_RNCR = ISO15693_DMA_BUFFER_SIZE; // DMA Next Counter registers
1250 WDT_HIT();
1ce68968 1251 if (BUTTON_PRESS()) {
d9de20fa 1252 DbpString("Snoop stopped.");
1253 break;
1254 }
1255 }
1256 }
a66f26da 1257
be09ea86 1258 if (!TagIsActive) { // no need to try decoding reader data if the tag is sending
d9de20fa 1259 if (Handle15693SampleFromReader(snoopdata & 0x02, &DecodeReader)) {
1ce68968 1260 // FpgaDisableSscDma();
1261 uint32_t eof_time = dma_start_time + samples*16 + 8 - DELAY_READER_TO_ARM_SNOOP; // end of EOF
1262 if (DecodeReader.byteCount > 0) {
1263 uint32_t sof_time = eof_time
1264 - DecodeReader.byteCount * (DecodeReader.Coding==CODING_1_OUT_OF_4?128*16:2048*16) // time for byte transfers
1265 - 32*16 // time for SOF transfer
1266 - 16*16; // time for EOF transfer
1267 LogTrace_ISO15693(DecodeReader.output, DecodeReader.byteCount, sof_time*4, eof_time*4, NULL, true);
1268 }
d9de20fa 1269 /* And ready to receive another command. */
1270 DecodeReaderReset(&DecodeReader);
1271 /* And also reset the demod code, which might have been */
1272 /* false-triggered by the commands from the reader. */
1273 DecodeTagReset(&DecodeTag);
1ce68968 1274 ReaderIsActive = false;
d9de20fa 1275 ExpectTagAnswer = true;
1ce68968 1276 // upTo = dmaBuf;
1277 // samples = 0;
1278 // FpgaSetupSscDma((uint8_t*) dmaBuf, ISO15693_DMA_BUFFER_SIZE);
1279 // continue;
1280 } else if (Handle15693SampleFromReader(snoopdata & 0x01, &DecodeReader)) {
1281 // FpgaDisableSscDma();
1282 uint32_t eof_time = dma_start_time + samples*16 + 16 - DELAY_READER_TO_ARM_SNOOP; // end of EOF
1283 if (DecodeReader.byteCount > 0) {
1284 uint32_t sof_time = eof_time
1285 - DecodeReader.byteCount * (DecodeReader.Coding==CODING_1_OUT_OF_4?128*16:2048*16) // time for byte transfers
1286 - 32*16 // time for SOF transfer
1287 - 16*16; // time for EOF transfer
1288 LogTrace_ISO15693(DecodeReader.output, DecodeReader.byteCount, sof_time*4, eof_time*4, NULL, true);
1289 }
d9de20fa 1290 /* And ready to receive another command. */
1291 DecodeReaderReset(&DecodeReader);
1292 /* And also reset the demod code, which might have been */
1293 /* false-triggered by the commands from the reader. */
1294 DecodeTagReset(&DecodeTag);
1ce68968 1295 ReaderIsActive = false;
1296 ExpectTagAnswer = true;
1297 // upTo = dmaBuf;
1298 // samples = 0;
1299 // FpgaSetupSscDma((uint8_t*) dmaBuf, ISO15693_DMA_BUFFER_SIZE);
1300 // continue;
1301 } else {
1302 ReaderIsActive = (DecodeReader.state >= STATE_READER_RECEIVE_DATA_1_OUT_OF_4);
d9de20fa 1303 }
9455b51c 1304 }
d9de20fa 1305
a66f26da 1306 if (!ReaderIsActive && ExpectTagAnswer) { // no need to try decoding tag data if the reader is currently sending or no answer expected yet
d9de20fa 1307 if (Handle15693SamplesFromTag(snoopdata >> 2, &DecodeTag)) {
1ce68968 1308 // FpgaDisableSscDma();
1309 uint32_t eof_time = dma_start_time + samples*16 - DELAY_TAG_TO_ARM_SNOOP; // end of EOF
1310 if (DecodeTag.lastBit == SOF_PART2) {
1311 eof_time -= 8*16; // needed 8 additional samples to confirm single SOF (iCLASS)
1312 }
1313 uint32_t sof_time = eof_time
1314 - DecodeTag.len * 8 * 8 * 16 // time for byte transfers
1315 - 32 * 16 // time for SOF transfer
1316 - (DecodeTag.lastBit != SOF_PART2?32*16:0); // time for EOF transfer
1317 LogTrace_ISO15693(DecodeTag.output, DecodeTag.len, sof_time*4, eof_time*4, NULL, false);
d9de20fa 1318 // And ready to receive another response.
1319 DecodeTagReset(&DecodeTag);
1320 DecodeReaderReset(&DecodeReader);
1321 ExpectTagAnswer = false;
1ce68968 1322 TagIsActive = false;
1323 // upTo = dmaBuf;
1324 // samples = 0;
1325 // FpgaSetupSscDma((uint8_t*) dmaBuf, ISO15693_DMA_BUFFER_SIZE);
1326 // continue;
1327 } else {
1328 TagIsActive = (DecodeTag.state >= STATE_TAG_RECEIVING_DATA);
d9de20fa 1329 }
d9de20fa 1330 }
1331
9455b51c 1332 }
70b2fc0a 1333
d9de20fa 1334 FpgaDisableSscDma();
a66f26da 1335
d9de20fa 1336 DbpString("Snoop statistics:");
be09ea86 1337 Dbprintf(" ExpectTagAnswer: %d, TagIsActive: %d, ReaderIsActive: %d", ExpectTagAnswer, TagIsActive, ReaderIsActive);
d9de20fa 1338 Dbprintf(" DecodeTag State: %d", DecodeTag.state);
1339 Dbprintf(" DecodeTag byteCnt: %d", DecodeTag.len);
be09ea86 1340 Dbprintf(" DecodeTag posCount: %d", DecodeTag.posCount);
d9de20fa 1341 Dbprintf(" DecodeReader State: %d", DecodeReader.state);
1342 Dbprintf(" DecodeReader byteCnt: %d", DecodeReader.byteCount);
be09ea86 1343 Dbprintf(" DecodeReader posCount: %d", DecodeReader.posCount);
d9de20fa 1344 Dbprintf(" Trace length: %d", BigBuf_get_traceLen());
be09ea86 1345 Dbprintf(" Max behindBy: %d", max_behindBy);
9455b51c 1346}
1347
1348
8c6cca0b 1349// Initialize the proxmark as iso15k reader
c41dd5f9 1350void Iso15693InitReader() {
7cc204bf 1351 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
9455b51c 1352
1353 // Start from off (no field generated)
70b2fc0a 1354 LED_D_OFF();
9455b51c 1355 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
e6304bca 1356 SpinDelay(10);
9455b51c 1357
1358 SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
5ea2a248 1359 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER);
9455b51c 1360
1361 // Give the tags time to energize
70b2fc0a 1362 LED_D_ON();
5ea2a248 1363 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER);
e6304bca 1364 SpinDelay(250);
9455b51c 1365}
1366
1367///////////////////////////////////////////////////////////////////////
1368// ISO 15693 Part 3 - Air Interface
70b2fc0a 1369// This section basically contains transmission and receiving of bits
9455b51c 1370///////////////////////////////////////////////////////////////////////
1371
9455b51c 1372
1373// uid is in transmission order (which is reverse of display order)
1374static void BuildReadBlockRequest(uint8_t *uid, uint8_t blockNumber )
1375{
1376 uint8_t cmd[13];
1377
1378 uint16_t crc;
d9de20fa 1379 // If we set the Option_Flag in this request, the VICC will respond with the security status of the block
1380 // followed by the block data
a66f26da 1381 cmd[0] = ISO15693_REQ_OPTION | ISO15693_REQ_ADDRESS | ISO15693_REQ_DATARATE_HIGH;
9455b51c 1382 // READ BLOCK command code
d9de20fa 1383 cmd[1] = ISO15693_READBLOCK;
9455b51c 1384 // UID may be optionally specified here
1385 // 64-bit UID
1386 cmd[2] = uid[0];
1387 cmd[3] = uid[1];
1388 cmd[4] = uid[2];
1389 cmd[5] = uid[3];
1390 cmd[6] = uid[4];
1391 cmd[7] = uid[5];
1392 cmd[8] = uid[6];
1393 cmd[9] = uid[7]; // 0xe0; // always e0 (not exactly unique)
1394 // Block number to read
d9de20fa 1395 cmd[10] = blockNumber;
9455b51c 1396 //Now the CRC
3d2c9c9b 1397 crc = Iso15693Crc(cmd, 11); // the crc needs to be calculated over 11 bytes
9455b51c 1398 cmd[11] = crc & 0xff;
1399 cmd[12] = crc >> 8;
1400
1401 CodeIso15693AsReader(cmd, sizeof(cmd));
1402}
1403
70b2fc0a 1404
9455b51c 1405// Now the VICC>VCD responses when we are simulating a tag
8c6cca0b 1406static void BuildInventoryResponse(uint8_t *uid)
9455b51c 1407{
1408 uint8_t cmd[12];
1409
1410 uint16_t crc;
8c6cca0b 1411
1412 cmd[0] = 0; // No error, no protocol format extension
3fe4ff4f 1413 cmd[1] = 0; // DSFID (data storage format identifier). 0x00 = not supported
9455b51c 1414 // 64-bit UID
3fe4ff4f 1415 cmd[2] = uid[7]; //0x32;
1416 cmd[3] = uid[6]; //0x4b;
1417 cmd[4] = uid[5]; //0x03;
1418 cmd[5] = uid[4]; //0x01;
1419 cmd[6] = uid[3]; //0x00;
1420 cmd[7] = uid[2]; //0x10;
1421 cmd[8] = uid[1]; //0x05;
1422 cmd[9] = uid[0]; //0xe0;
9455b51c 1423 //Now the CRC
3d2c9c9b 1424 crc = Iso15693Crc(cmd, 10);
9455b51c 1425 cmd[10] = crc & 0xff;
1426 cmd[11] = crc >> 8;
1427
8c6cca0b 1428 CodeIso15693AsTag(cmd, sizeof(cmd));
9455b51c 1429}
1430
e6304bca 1431// Universal Method for sending to and recv bytes from a tag
a66f26da 1432// init ... should we initialize the reader?
1433// speed ... 0 low speed, 1 hi speed
1434// *recv will contain the tag's answer
c41dd5f9 1435// return: length of received data, or -1 for timeout
1436int SendDataTag(uint8_t *send, int sendlen, bool init, int speed, uint8_t *recv, uint16_t max_recv_len, uint32_t start_time, uint32_t *eof_time) {
9455b51c 1437
c41dd5f9 1438 if (init) {
1439 Iso15693InitReader();
1440 StartCountSspClk();
1441 }
ece38ef3 1442
c41dd5f9 1443 int answerLen = 0;
ece38ef3 1444
9455b51c 1445 if (!speed) {
1446 // low speed (1 out of 256)
1447 CodeIso15693AsReader256(send, sendlen);
1448 } else {
1449 // high speed (1 out of 4)
1450 CodeIso15693AsReader(send, sendlen);
1451 }
8c6cca0b 1452
c41dd5f9 1453 TransmitTo15693Tag(ToSend, ToSendMax, &start_time);
d9de20fa 1454
9455b51c 1455 // Now wait for a response
d9de20fa 1456 if (recv != NULL) {
ece38ef3 1457 answerLen = GetIso15693AnswerFromTag(recv, max_recv_len, ISO15693_READER_TIMEOUT, eof_time);
9455b51c 1458 }
1459
9455b51c 1460 return answerLen;
1461}
15c4dc5a 1462
15c4dc5a 1463
9455b51c 1464// --------------------------------------------------------------------
8c6cca0b 1465// Debug Functions
9455b51c 1466// --------------------------------------------------------------------
15c4dc5a 1467
9455b51c 1468// Decodes a message from a tag and displays its metadata and content
1469#define DBD15STATLEN 48
1470void DbdecodeIso15693Answer(int len, uint8_t *d) {
1471 char status[DBD15STATLEN+1]={0};
1472 uint16_t crc;
1473
d9de20fa 1474 if (len > 3) {
1475 if (d[0] & ISO15693_RES_EXT)
1476 strncat(status,"ProtExt ", DBD15STATLEN);
1477 if (d[0] & ISO15693_RES_ERROR) {
9455b51c 1478 // error
d9de20fa 1479 strncat(status,"Error ", DBD15STATLEN);
9455b51c 1480 switch (d[1]) {
8c6cca0b 1481 case 0x01:
d9de20fa 1482 strncat(status,"01:notSupp", DBD15STATLEN);
15c4dc5a 1483 break;
8c6cca0b 1484 case 0x02:
d9de20fa 1485 strncat(status,"02:notRecog", DBD15STATLEN);
9455b51c 1486 break;
8c6cca0b 1487 case 0x03:
d9de20fa 1488 strncat(status,"03:optNotSupp", DBD15STATLEN);
9455b51c 1489 break;
8c6cca0b 1490 case 0x0f:
d9de20fa 1491 strncat(status,"0f:noInfo", DBD15STATLEN);
9455b51c 1492 break;
8c6cca0b 1493 case 0x10:
d9de20fa 1494 strncat(status,"10:doesn'tExist", DBD15STATLEN);
9455b51c 1495 break;
8c6cca0b 1496 case 0x11:
d9de20fa 1497 strncat(status,"11:lockAgain", DBD15STATLEN);
9455b51c 1498 break;
8c6cca0b 1499 case 0x12:
d9de20fa 1500 strncat(status,"12:locked", DBD15STATLEN);
9455b51c 1501 break;
8c6cca0b 1502 case 0x13:
d9de20fa 1503 strncat(status,"13:progErr", DBD15STATLEN);
9455b51c 1504 break;
8c6cca0b 1505 case 0x14:
d9de20fa 1506 strncat(status,"14:lockErr", DBD15STATLEN);
9455b51c 1507 break;
1508 default:
d9de20fa 1509 strncat(status,"unknownErr", DBD15STATLEN);
15c4dc5a 1510 }
d9de20fa 1511 strncat(status," ", DBD15STATLEN);
9455b51c 1512 } else {
d9de20fa 1513 strncat(status,"NoErr ", DBD15STATLEN);
15c4dc5a 1514 }
8c6cca0b 1515
3d2c9c9b 1516 crc=Iso15693Crc(d,len-2);
8c6cca0b 1517 if ( (( crc & 0xff ) == d[len-2]) && (( crc >> 8 ) == d[len-1]) )
9455b51c 1518 strncat(status,"CrcOK",DBD15STATLEN);
1519 else
8c6cca0b 1520 strncat(status,"CrcFail!",DBD15STATLEN);
9455b51c 1521
1522 Dbprintf("%s",status);
15c4dc5a 1523 }
1524}
1525
9455b51c 1526
1527
1528///////////////////////////////////////////////////////////////////////
1529// Functions called via USB/Client
1530///////////////////////////////////////////////////////////////////////
1531
1532void SetDebugIso15693(uint32_t debug) {
1533 DEBUG=debug;
1534 Dbprintf("Iso15693 Debug is now %s",DEBUG?"on":"off");
1535 return;
1536}
1537
d9de20fa 1538
5ea2a248 1539//---------------------------------------------------------------------------------------
1540// Simulate an ISO15693 reader, perform anti-collision and then attempt to read a sector.
15c4dc5a 1541// all demodulation performed in arm rather than host. - greg
5ea2a248 1542//---------------------------------------------------------------------------------------
ece38ef3 1543void ReaderIso15693(uint32_t parameter) {
1544
15c4dc5a 1545 LED_A_ON();
15c4dc5a 1546
d9de20fa 1547 set_tracing(true);
a66f26da 1548
d9de20fa 1549 int answerLen = 0;
3fe4ff4f 1550 uint8_t TagUID[8] = {0x00};
1551
09ffd16e 1552 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
15c4dc5a 1553
d9de20fa 1554 uint8_t answer[ISO15693_MAX_RESPONSE_LENGTH];
15c4dc5a 1555
3fe4ff4f 1556 SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
15c4dc5a 1557 // Setup SSC
5ea2a248 1558 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER);
15c4dc5a 1559
1560 // Start from off (no field generated)
a66f26da 1561 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1562 SpinDelay(200);
15c4dc5a 1563
15c4dc5a 1564 // Give the tags time to energize
70b2fc0a 1565 LED_D_ON();
5ea2a248 1566 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER);
15c4dc5a 1567 SpinDelay(200);
d9de20fa 1568 StartCountSspClk();
1569
15c4dc5a 1570
15c4dc5a 1571 // FIRST WE RUN AN INVENTORY TO GET THE TAG UID
1572 // THIS MEANS WE CAN PRE-BUILD REQUESTS TO SAVE CPU TIME
15c4dc5a 1573
1574 // Now send the IDENTIFY command
1575 BuildIdentifyRequest();
c41dd5f9 1576 uint32_t start_time = 0;
1577 TransmitTo15693Tag(ToSend, ToSendMax, &start_time);
a66f26da 1578
15c4dc5a 1579 // Now wait for a response
c41dd5f9 1580 uint32_t eof_time;
1581 answerLen = GetIso15693AnswerFromTag(answer, sizeof(answer), DELAY_ISO15693_VCD_TO_VICC_READER * 2, &eof_time) ;
1582 start_time = eof_time + DELAY_ISO15693_VICC_TO_VCD_READER;
15c4dc5a 1583
d9de20fa 1584 if (answerLen >=12) // we should do a better check than this
15c4dc5a 1585 {
d9de20fa 1586 TagUID[0] = answer[2];
1587 TagUID[1] = answer[3];
1588 TagUID[2] = answer[4];
1589 TagUID[3] = answer[5];
1590 TagUID[4] = answer[6];
1591 TagUID[5] = answer[7];
1592 TagUID[6] = answer[8]; // IC Manufacturer code
1593 TagUID[7] = answer[9]; // always E0
15c4dc5a 1594
15c4dc5a 1595 }
1596
d9de20fa 1597 Dbprintf("%d octets read from IDENTIFY request:", answerLen);
1598 DbdecodeIso15693Answer(answerLen, answer);
1599 Dbhexdump(answerLen, answer, false);
9455b51c 1600
1601 // UID is reverse
d9de20fa 1602 if (answerLen >= 12)
3fe4ff4f 1603 Dbprintf("UID = %02hX%02hX%02hX%02hX%02hX%02hX%02hX%02hX",
1604 TagUID[7],TagUID[6],TagUID[5],TagUID[4],
1605 TagUID[3],TagUID[2],TagUID[1],TagUID[0]);
9455b51c 1606
1607
315e18e6 1608 // Dbprintf("%d octets read from SELECT request:", answerLen2);
1609 // DbdecodeIso15693Answer(answerLen2,answer2);
1610 // Dbhexdump(answerLen2,answer2,true);
9455b51c 1611
315e18e6 1612 // Dbprintf("%d octets read from XXX request:", answerLen3);
1613 // DbdecodeIso15693Answer(answerLen3,answer3);
1614 // Dbhexdump(answerLen3,answer3,true);
9455b51c 1615
9455b51c 1616 // read all pages
d9de20fa 1617 if (answerLen >= 12 && DEBUG) {
5ea2a248 1618 for (int i = 0; i < 32; i++) { // sanity check, assume max 32 pages
8c6cca0b 1619 BuildReadBlockRequest(TagUID, i);
c41dd5f9 1620 TransmitTo15693Tag(ToSend, ToSendMax, &start_time);
1621 int answerLen = GetIso15693AnswerFromTag(answer, sizeof(answer), DELAY_ISO15693_VCD_TO_VICC_READER * 2, &eof_time);
1622 start_time = eof_time + DELAY_ISO15693_VICC_TO_VCD_READER;
d9de20fa 1623 if (answerLen > 0) {
1624 Dbprintf("READ SINGLE BLOCK %d returned %d octets:", i, answerLen);
1625 DbdecodeIso15693Answer(answerLen, answer);
1626 Dbhexdump(answerLen, answer, false);
1627 if ( *((uint32_t*) answer) == 0x07160101 ) break; // exit on NoPageErr
8c6cca0b 1628 }
8c6cca0b 1629 }
9455b51c 1630 }
15c4dc5a 1631
8c6cca0b 1632 // for the time being, switch field off to protect rdv4.0
70b2fc0a 1633 // note: this prevents using hf 15 cmd with s option - which isn't implemented yet anyway
a66f26da 1634 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
15c4dc5a 1635 LED_D_OFF();
8c6cca0b 1636
70b2fc0a 1637 LED_A_OFF();
15c4dc5a 1638}
1639
8c6cca0b 1640
1641// Simulate an ISO15693 TAG.
1642// For Inventory command: print command and send Inventory Response with given UID
1643// TODO: interpret other reader commands and send appropriate response
ece38ef3 1644void SimTagIso15693(uint32_t parameter, uint8_t *uid) {
1645
15c4dc5a 1646 LED_A_ON();
15c4dc5a 1647
7cc204bf 1648 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
15c4dc5a 1649 SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
a66f26da 1650 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR | FPGA_HF_SIMULATOR_NO_MODULATION);
8c6cca0b 1651 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_SIMULATOR);
15c4dc5a 1652
8c6cca0b 1653 StartCountSspClk();
15c4dc5a 1654
8c6cca0b 1655 uint8_t cmd[ISO15693_MAX_COMMAND_LENGTH];
15c4dc5a 1656
8c6cca0b 1657 // Build a suitable response to the reader INVENTORY command
1658 BuildInventoryResponse(uid);
15c4dc5a 1659
8c6cca0b 1660 // Listen to reader
1661 while (!BUTTON_PRESS()) {
1662 uint32_t eof_time = 0, start_time = 0;
1663 int cmd_len = GetIso15693CommandFromReader(cmd, sizeof(cmd), &eof_time);
1664
1665 if ((cmd_len >= 5) && (cmd[0] & ISO15693_REQ_INVENTORY) && (cmd[1] == ISO15693_INVENTORY)) { // TODO: check more flags
1666 bool slow = !(cmd[0] & ISO15693_REQ_DATARATE_HIGH);
c41dd5f9 1667 start_time = eof_time + DELAY_ISO15693_VCD_TO_VICC_SIM;
8efd0b80 1668 TransmitTo15693Reader(ToSend, ToSendMax, &start_time, 0, slow);
8c6cca0b 1669 }
3fe4ff4f 1670
8c6cca0b 1671 Dbprintf("%d bytes read from reader:", cmd_len);
1672 Dbhexdump(cmd_len, cmd, false);
1673 }
15c4dc5a 1674
a66f26da 1675 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
ece38ef3 1676 LED_D_OFF();
1677 LED_A_OFF();
15c4dc5a 1678}
9455b51c 1679
1680
1681// Since there is no standardized way of reading the AFI out of a tag, we will brute force it
1682// (some manufactures offer a way to read the AFI, though)
8c6cca0b 1683void BruteforceIso15693Afi(uint32_t speed)
1684{
70b2fc0a 1685 LED_A_ON();
8c6cca0b 1686
d9de20fa 1687 uint8_t data[6];
1688 uint8_t recv[ISO15693_MAX_RESPONSE_LENGTH];
c41dd5f9 1689 int datalen = 0, recvlen = 0;
1690 uint32_t eof_time;
a66f26da 1691
9455b51c 1692 // first without AFI
8c6cca0b 1693 // Tags should respond without AFI and with AFI=0 even when AFI is active
1694
1695 data[0] = ISO15693_REQ_DATARATE_HIGH | ISO15693_REQ_INVENTORY | ISO15693_REQINV_SLOT1;
1696 data[1] = ISO15693_INVENTORY;
1697 data[2] = 0; // mask length
3d2c9c9b 1698 datalen = Iso15693AddCrc(data,3);
c41dd5f9 1699 uint32_t start_time = GetCountSspClk();
1700 recvlen = SendDataTag(data, datalen, true, speed, recv, sizeof(recv), 0, &eof_time);
1701 start_time = eof_time + DELAY_ISO15693_VICC_TO_VCD_READER;
9455b51c 1702 WDT_HIT();
1703 if (recvlen>=12) {
3d2c9c9b 1704 Dbprintf("NoAFI UID=%s", Iso15693sprintUID(NULL, &recv[2]));
9455b51c 1705 }
8c6cca0b 1706
9455b51c 1707 // now with AFI
8c6cca0b 1708
1709 data[0] = ISO15693_REQ_DATARATE_HIGH | ISO15693_REQ_INVENTORY | ISO15693_REQINV_AFI | ISO15693_REQINV_SLOT1;
1710 data[1] = ISO15693_INVENTORY;
1711 data[2] = 0; // AFI
1712 data[3] = 0; // mask length
1713
d9de20fa 1714 for (int i = 0; i < 256; i++) {
1715 data[2] = i & 0xFF;
3d2c9c9b 1716 datalen = Iso15693AddCrc(data,4);
c41dd5f9 1717 recvlen = SendDataTag(data, datalen, false, speed, recv, sizeof(recv), start_time, &eof_time);
1718 start_time = eof_time + DELAY_ISO15693_VICC_TO_VCD_READER;
9455b51c 1719 WDT_HIT();
d9de20fa 1720 if (recvlen >= 12) {
3d2c9c9b 1721 Dbprintf("AFI=%i UID=%s", i, Iso15693sprintUID(NULL, &recv[2]));
9455b51c 1722 }
8c6cca0b 1723 }
9455b51c 1724 Dbprintf("AFI Bruteforcing done.");
8c6cca0b 1725
a66f26da 1726 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
ece38ef3 1727 LED_D_OFF();
1728 LED_A_OFF();
1729
9455b51c 1730}
1731
1732// Allows to directly send commands to the tag via the client
70b2fc0a 1733void DirectTag15693Command(uint32_t datalen, uint32_t speed, uint32_t recv, uint8_t data[]) {
9455b51c 1734
ece38ef3 1735 LED_A_ON();
1736
d9de20fa 1737 int recvlen = 0;
1738 uint8_t recvbuf[ISO15693_MAX_RESPONSE_LENGTH];
c41dd5f9 1739 uint32_t eof_time;
8c6cca0b 1740
9455b51c 1741 if (DEBUG) {
d9de20fa 1742 Dbprintf("SEND:");
8c6cca0b 1743 Dbhexdump(datalen, data, false);
9455b51c 1744 }
8c6cca0b 1745
c41dd5f9 1746 recvlen = SendDataTag(data, datalen, true, speed, (recv?recvbuf:NULL), sizeof(recvbuf), 0, &eof_time);
1747
1748 // for the time being, switch field off to protect rdv4.0
1749 // note: this prevents using hf 15 cmd with s option - which isn't implemented yet anyway
1750 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1751 LED_D_OFF();
9455b51c 1752
8c6cca0b 1753 if (recv) {
9455b51c 1754 if (DEBUG) {
d9de20fa 1755 Dbprintf("RECV:");
c41dd5f9 1756 if (recvlen > 0) {
1757 Dbhexdump(recvlen, recvbuf, false);
1758 DbdecodeIso15693Answer(recvlen, recvbuf);
1759 }
9455b51c 1760 }
c41dd5f9 1761 if (recvlen > ISO15693_MAX_RESPONSE_LENGTH) {
1762 recvlen = ISO15693_MAX_RESPONSE_LENGTH;
1763 }
1764 cmd_send(CMD_ACK, recvlen, 0, 0, recvbuf, ISO15693_MAX_RESPONSE_LENGTH);
9455b51c 1765 }
1766
70b2fc0a 1767 LED_A_OFF();
9455b51c 1768}
1769
096dee17 1770//-----------------------------------------------------------------------------
1771// Work with "magic Chinese" card.
1772//
1773//-----------------------------------------------------------------------------
1774
1775// Set the UID to the tag (based on Iceman work).
ece38ef3 1776void SetTag15693Uid(uint8_t *uid) {
a66f26da 1777
ece38ef3 1778 LED_A_ON();
1779
be09ea86 1780 uint8_t cmd[4][9] = {
1781 {0x02, 0x21, 0x3e, 0x00, 0x00, 0x00, 0x00},
1782 {0x02, 0x21, 0x3f, 0x69, 0x96, 0x00, 0x00},
1783 {0x02, 0x21, 0x38},
1784 {0x02, 0x21, 0x39}
1785 };
1786
a66f26da 1787 uint16_t crc;
1788
1789 int recvlen = 0;
1790 uint8_t recvbuf[ISO15693_MAX_RESPONSE_LENGTH];
c41dd5f9 1791 uint32_t eof_time;
a66f26da 1792
a66f26da 1793 // Command 3 : 022138u8u7u6u5 (where uX = uid byte X)
a66f26da 1794 cmd[2][3] = uid[7];
1795 cmd[2][4] = uid[6];
1796 cmd[2][5] = uid[5];
1797 cmd[2][6] = uid[4];
1798
1799 // Command 4 : 022139u4u3u2u1 (where uX = uid byte X)
a66f26da 1800 cmd[3][3] = uid[3];
1801 cmd[3][4] = uid[2];
1802 cmd[3][5] = uid[1];
1803 cmd[3][6] = uid[0];
1804
c41dd5f9 1805 for (int i = 0; i < 4; i++) {
a66f26da 1806 // Add the CRC
1807 crc = Iso15693Crc(cmd[i], 7);
1808 cmd[i][7] = crc & 0xff;
1809 cmd[i][8] = crc >> 8;
1810
1811 if (DEBUG) {
1812 Dbprintf("SEND:");
1813 Dbhexdump(sizeof(cmd[i]), cmd[i], false);
1814 }
1815
c41dd5f9 1816 recvlen = SendDataTag(cmd[i], sizeof(cmd[i]), true, 1, recvbuf, sizeof(recvbuf), 0, &eof_time);
a66f26da 1817
1818 if (DEBUG) {
1819 Dbprintf("RECV:");
c41dd5f9 1820 if (recvlen > 0) {
1821 Dbhexdump(recvlen, recvbuf, false);
1822 DbdecodeIso15693Answer(recvlen, recvbuf);
1823 }
a66f26da 1824 }
1825
1826 cmd_send(CMD_ACK, recvlen>ISO15693_MAX_RESPONSE_LENGTH?ISO15693_MAX_RESPONSE_LENGTH:recvlen, 0, 0, recvbuf, ISO15693_MAX_RESPONSE_LENGTH);
1827 }
1828
a66f26da 1829 LED_A_OFF();
096dee17 1830}
9455b51c 1831
1832
1833
1834// --------------------------------------------------------------------
1835// -- Misc & deprecated functions
1836// --------------------------------------------------------------------
1837
e6304bca 1838/*
9455b51c 1839
1840// do not use; has a fix UID
1841static void __attribute__((unused)) BuildSysInfoRequest(uint8_t *uid)
1842{
1843 uint8_t cmd[12];
1844
1845 uint16_t crc;
5ea2a248 1846 // If we set the Option_Flag in this request, the VICC will respond with the security status of the block
1847 // followed by the block data
9455b51c 1848 // one sub-carrier, inventory, 1 slot, fast rate
1849 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1850 // System Information command code
1851 cmd[1] = 0x2B;
1852 // UID may be optionally specified here
1853 // 64-bit UID
1854 cmd[2] = 0x32;
1855 cmd[3]= 0x4b;
1856 cmd[4] = 0x03;
1857 cmd[5] = 0x01;
1858 cmd[6] = 0x00;
1859 cmd[7] = 0x10;
1860 cmd[8] = 0x05;
1861 cmd[9]= 0xe0; // always e0 (not exactly unique)
1862 //Now the CRC
3d2c9c9b 1863 crc = Iso15693Crc(cmd, 10); // the crc needs to be calculated over 2 bytes
9455b51c 1864 cmd[10] = crc & 0xff;
1865 cmd[11] = crc >> 8;
1866
1867 CodeIso15693AsReader(cmd, sizeof(cmd));
1868}
1869
9455b51c 1870
1871// do not use; has a fix UID
1872static void __attribute__((unused)) BuildReadMultiBlockRequest(uint8_t *uid)
1873{
1874 uint8_t cmd[14];
1875
1876 uint16_t crc;
5ea2a248 1877 // If we set the Option_Flag in this request, the VICC will respond with the security status of the block
1878 // followed by the block data
9455b51c 1879 // one sub-carrier, inventory, 1 slot, fast rate
1880 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1881 // READ Multi BLOCK command code
1882 cmd[1] = 0x23;
1883 // UID may be optionally specified here
1884 // 64-bit UID
1885 cmd[2] = 0x32;
1886 cmd[3]= 0x4b;
1887 cmd[4] = 0x03;
1888 cmd[5] = 0x01;
1889 cmd[6] = 0x00;
1890 cmd[7] = 0x10;
1891 cmd[8] = 0x05;
1892 cmd[9]= 0xe0; // always e0 (not exactly unique)
1893 // First Block number to read
1894 cmd[10] = 0x00;
1895 // Number of Blocks to read
1896 cmd[11] = 0x2f; // read quite a few
1897 //Now the CRC
3d2c9c9b 1898 crc = Iso15693Crc(cmd, 12); // the crc needs to be calculated over 2 bytes
9455b51c 1899 cmd[12] = crc & 0xff;
1900 cmd[13] = crc >> 8;
1901
1902 CodeIso15693AsReader(cmd, sizeof(cmd));
1903}
1904
1905// do not use; has a fix UID
1906static void __attribute__((unused)) BuildArbitraryRequest(uint8_t *uid,uint8_t CmdCode)
1907{
1908 uint8_t cmd[14];
1909
1910 uint16_t crc;
5ea2a248 1911 // If we set the Option_Flag in this request, the VICC will respond with the security status of the block
1912 // followed by the block data
9455b51c 1913 // one sub-carrier, inventory, 1 slot, fast rate
1914 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1915 // READ BLOCK command code
1916 cmd[1] = CmdCode;
1917 // UID may be optionally specified here
1918 // 64-bit UID
1919 cmd[2] = 0x32;
1920 cmd[3]= 0x4b;
1921 cmd[4] = 0x03;
1922 cmd[5] = 0x01;
1923 cmd[6] = 0x00;
1924 cmd[7] = 0x10;
1925 cmd[8] = 0x05;
1926 cmd[9]= 0xe0; // always e0 (not exactly unique)
1927 // Parameter
1928 cmd[10] = 0x00;
1929 cmd[11] = 0x0a;
1930
a66f26da 1931// cmd[12] = 0x00;
1932// cmd[13] = 0x00; //Now the CRC
3d2c9c9b 1933 crc = Iso15693Crc(cmd, 12); // the crc needs to be calculated over 2 bytes
9455b51c 1934 cmd[12] = crc & 0xff;
1935 cmd[13] = crc >> 8;
1936
1937 CodeIso15693AsReader(cmd, sizeof(cmd));
1938}
1939
1940// do not use; has a fix UID
1941static void __attribute__((unused)) BuildArbitraryCustomRequest(uint8_t uid[], uint8_t CmdCode)
1942{
1943 uint8_t cmd[14];
1944
1945 uint16_t crc;
5ea2a248 1946 // If we set the Option_Flag in this request, the VICC will respond with the security status of the block
1947 // followed by the block data
9455b51c 1948 // one sub-carrier, inventory, 1 slot, fast rate
1949 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1950 // READ BLOCK command code
1951 cmd[1] = CmdCode;
1952 // UID may be optionally specified here
1953 // 64-bit UID
1954 cmd[2] = 0x32;
1955 cmd[3]= 0x4b;
1956 cmd[4] = 0x03;
1957 cmd[5] = 0x01;
1958 cmd[6] = 0x00;
1959 cmd[7] = 0x10;
1960 cmd[8] = 0x05;
1961 cmd[9]= 0xe0; // always e0 (not exactly unique)
1962 // Parameter
5ea2a248 1963 cmd[10] = 0x05; // for custom codes this must be manufacturer code
9455b51c 1964 cmd[11] = 0x00;
1965
a66f26da 1966// cmd[12] = 0x00;
1967// cmd[13] = 0x00; //Now the CRC
3d2c9c9b 1968 crc = Iso15693Crc(cmd, 12); // the crc needs to be calculated over 2 bytes
9455b51c 1969 cmd[12] = crc & 0xff;
1970 cmd[13] = crc >> 8;
1971
1972 CodeIso15693AsReader(cmd, sizeof(cmd));
1973}
1974
1975
1976
1977
e6304bca 1978*/
9455b51c 1979
1980
Impressum, Datenschutz