]> git.zerfleddert.de Git - proxmark3-svn/blame - armsrc/iso15693.c
fix warning under linux (#709)
[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
8c6cca0b 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
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.
9455b51c 21//
22// VCD (reader) -> VICC (tag)
23// 1 out of 256:
8c6cca0b 24// data rate: 1,66 kbit/s (fc/8192)
9455b51c 25// used for long range
26// 1 out of 4:
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:
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//-----------------------------------------------------------------------------
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.
50// *) remove or refactor code under "depricated"
9455b51c 51// *) document all the functions
52
bd20f8f4 53
e30c654b 54#include "proxmark3.h"
f7e3ed82 55#include "util.h"
15c4dc5a 56#include "apps.h"
9ab7a6c7 57#include "string.h"
9455b51c 58#include "iso15693tools.h"
8c6cca0b 59#include "protocols.h"
902cb3c0 60#include "cmd.h"
15c4dc5a 61
15c4dc5a 62#define arraylen(x) (sizeof(x)/sizeof((x)[0]))
63
70b2fc0a 64static int DEBUG = 0;
65
9455b51c 66///////////////////////////////////////////////////////////////////////
67// ISO 15693 Part 2 - Air Interface
68// This section basicly contains transmission and receiving of bits
69///////////////////////////////////////////////////////////////////////
70
71#define FrameSOF Iso15693FrameSOF
72#define Logic0 Iso15693Logic0
73#define Logic1 Iso15693Logic1
74#define FrameEOF Iso15693FrameEOF
15c4dc5a 75
9455b51c 76#define Crc(data,datalen) Iso15693Crc(data,datalen)
77#define AddCrc(data,datalen) Iso15693AddCrc(data,datalen)
78#define sprintUID(target,uid) Iso15693sprintUID(target,uid)
15c4dc5a 79
8c6cca0b 80// approximate amplitude=sqrt(ci^2+cq^2) by amplitude = max(|ci|,|cq|) + 1/2*min(|ci|,|cq|)
70b2fc0a 81#define AMPLITUDE(ci, cq) (MAX(ABS(ci), ABS(cq)) + MIN(ABS(ci), ABS(cq))/2)
9455b51c 82
8c6cca0b 83// buffers
84#define ISO15693_DMA_BUFFER_SIZE 128
85#define ISO15693_MAX_RESPONSE_LENGTH 36 // allows read single block with the maximum block size of 256bits. Read multiple blocks not supported yet
86#define ISO15693_MAX_COMMAND_LENGTH 45 // allows write single block with the maximum block size of 256bits. Write multiple blocks not supported yet
87
88// timing. Delays in SSP_CLK ticks.
89#define DELAY_READER_TO_ARM 8
90#define DELAY_ARM_TO_READER 1
91#define DELAY_ISO15693_VCD_TO_VICC 132 // 132/423.75kHz = 311.5us from end of EOF to start of tag response
9455b51c 92
93// ---------------------------
8c6cca0b 94// Signal Processing
9455b51c 95// ---------------------------
96
97// prepare data using "1 out of 4" code for later transmission
8c6cca0b 98// resulting data rate is 26.48 kbit/s (fc/512)
9455b51c 99// cmd ... data
100// n ... length of data
f7e3ed82 101static void CodeIso15693AsReader(uint8_t *cmd, int n)
15c4dc5a 102{
103 int i, j;
104
105 ToSendReset();
106
107 // Give it a bit of slack at the beginning
108 for(i = 0; i < 24; i++) {
109 ToSendStuffBit(1);
110 }
111
9455b51c 112 // SOF for 1of4
15c4dc5a 113 ToSendStuffBit(0);
114 ToSendStuffBit(1);
115 ToSendStuffBit(1);
116 ToSendStuffBit(1);
117 ToSendStuffBit(1);
118 ToSendStuffBit(0);
119 ToSendStuffBit(1);
120 ToSendStuffBit(1);
121 for(i = 0; i < n; i++) {
122 for(j = 0; j < 8; j += 2) {
123 int these = (cmd[i] >> j) & 3;
124 switch(these) {
125 case 0:
126 ToSendStuffBit(1);
127 ToSendStuffBit(0);
128 ToSendStuffBit(1);
129 ToSendStuffBit(1);
130 ToSendStuffBit(1);
131 ToSendStuffBit(1);
132 ToSendStuffBit(1);
133 ToSendStuffBit(1);
134 break;
135 case 1:
136 ToSendStuffBit(1);
137 ToSendStuffBit(1);
138 ToSendStuffBit(1);
139 ToSendStuffBit(0);
140 ToSendStuffBit(1);
141 ToSendStuffBit(1);
142 ToSendStuffBit(1);
143 ToSendStuffBit(1);
144 break;
145 case 2:
146 ToSendStuffBit(1);
147 ToSendStuffBit(1);
148 ToSendStuffBit(1);
149 ToSendStuffBit(1);
150 ToSendStuffBit(1);
151 ToSendStuffBit(0);
152 ToSendStuffBit(1);
153 ToSendStuffBit(1);
154 break;
155 case 3:
156 ToSendStuffBit(1);
157 ToSendStuffBit(1);
158 ToSendStuffBit(1);
159 ToSendStuffBit(1);
160 ToSendStuffBit(1);
161 ToSendStuffBit(1);
162 ToSendStuffBit(1);
163 ToSendStuffBit(0);
164 break;
165 }
166 }
167 }
9455b51c 168 // EOF
15c4dc5a 169 ToSendStuffBit(1);
170 ToSendStuffBit(1);
171 ToSendStuffBit(0);
172 ToSendStuffBit(1);
173
70b2fc0a 174 // Fill remainder of last byte with 1
175 for(i = 0; i < 4; i++) {
15c4dc5a 176 ToSendStuffBit(1);
177 }
178}
179
70b2fc0a 180// encode data using "1 out of 256" scheme
8c6cca0b 181// data rate is 1,66 kbit/s (fc/8192)
9455b51c 182// is designed for more robust communication over longer distances
183static void CodeIso15693AsReader256(uint8_t *cmd, int n)
15c4dc5a 184{
15c4dc5a 185 int i, j;
186
9455b51c 187 ToSendReset();
188
189 // Give it a bit of slack at the beginning
190 for(i = 0; i < 24; i++) {
191 ToSendStuffBit(1);
192 }
193
194 // SOF for 1of256
195 ToSendStuffBit(0);
196 ToSendStuffBit(1);
197 ToSendStuffBit(1);
198 ToSendStuffBit(1);
199 ToSendStuffBit(1);
200 ToSendStuffBit(1);
201 ToSendStuffBit(1);
202 ToSendStuffBit(0);
8c6cca0b 203
15c4dc5a 204 for(i = 0; i < n; i++) {
9455b51c 205 for (j = 0; j<=255; j++) {
206 if (cmd[i]==j) {
207 ToSendStuffBit(1);
208 ToSendStuffBit(0);
15c4dc5a 209 } else {
9455b51c 210 ToSendStuffBit(1);
211 ToSendStuffBit(1);
8c6cca0b 212 }
213 }
15c4dc5a 214 }
9455b51c 215 // EOF
216 ToSendStuffBit(1);
217 ToSendStuffBit(1);
218 ToSendStuffBit(0);
219 ToSendStuffBit(1);
15c4dc5a 220
8c6cca0b 221 // Fill remainder of last byte with 1
222 for(i = 0; i < 4; i++) {
9455b51c 223 ToSendStuffBit(1);
224 }
8c6cca0b 225
226 ToSendMax++;
227}
228
229
230static void CodeIso15693AsTag(uint8_t *cmd, int n)
231{
232 ToSendReset();
233
234 // SOF
235 ToSendStuffBit(0);
236 ToSendStuffBit(0);
237 ToSendStuffBit(0);
238 ToSendStuffBit(1);
239 ToSendStuffBit(1);
240 ToSendStuffBit(1);
241 ToSendStuffBit(0);
242 ToSendStuffBit(1);
243
244 // data
245 for(int i = 0; i < n; i++) {
246 for(int j = 0; j < 8; j++) {
247 if ((cmd[i] >> j) & 0x01) {
248 ToSendStuffBit(0);
249 ToSendStuffBit(1);
250 } else {
251 ToSendStuffBit(1);
252 ToSendStuffBit(0);
253 }
254 }
255 }
256
257 // EOF
258 ToSendStuffBit(1);
259 ToSendStuffBit(0);
260 ToSendStuffBit(1);
261 ToSendStuffBit(1);
262 ToSendStuffBit(1);
263 ToSendStuffBit(0);
264 ToSendStuffBit(0);
265 ToSendStuffBit(0);
266
267 ToSendMax++;
15c4dc5a 268}
269
9455b51c 270
70b2fc0a 271// Transmit the command (to the tag) that was placed in cmd[].
272static void TransmitTo15693Tag(const uint8_t *cmd, int len)
15c4dc5a 273{
6a5d4e17 274 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_TX);
15c4dc5a 275 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX);
15c4dc5a 276
70b2fc0a 277 LED_B_ON();
278 for(int c = 0; c < len; ) {
15c4dc5a 279 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
6a5d4e17 280 AT91C_BASE_SSC->SSC_THR = ~cmd[c];
15c4dc5a 281 c++;
15c4dc5a 282 }
283 WDT_HIT();
284 }
70b2fc0a 285 LED_B_OFF();
15c4dc5a 286}
287
288//-----------------------------------------------------------------------------
8c6cca0b 289// Transmit the tag response (to the reader) that was placed in cmd[].
15c4dc5a 290//-----------------------------------------------------------------------------
8c6cca0b 291static void TransmitTo15693Reader(const uint8_t *cmd, size_t len, uint32_t start_time, bool slow)
15c4dc5a 292{
8c6cca0b 293 // don't use the FPGA_HF_SIMULATOR_MODULATE_424K_8BIT minor mode. It would spoil GetCountSspClk()
70b2fc0a 294 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR | FPGA_HF_SIMULATOR_MODULATE_424K);
15c4dc5a 295
8c6cca0b 296 uint8_t shift_delay = start_time & 0x00000007;
297 uint8_t bitmask = 0x00;
298 for (int i = 0; i < shift_delay; i++) {
299 bitmask |= (0x01 << i);
300 }
301
302 while (GetCountSspClk() < (start_time & 0xfffffff8)) ;
303 AT91C_BASE_SSC->SSC_THR = 0x00; // clear TXRDY
304
70b2fc0a 305 LED_C_ON();
8c6cca0b 306 uint8_t bits_to_shift = 0x00;
307 for(size_t c = 0; c <= len; c++) {
308 uint8_t bits_to_send = bits_to_shift << (8 - shift_delay) | (c==len?0x00:cmd[c]) >> shift_delay;
309 bits_to_shift = cmd[c] & bitmask;
310 for (int i = 7; i >= 0; i--) {
311 for (int j = 0; j < (slow?4:1); ) {
312 if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_TXRDY) {
313 if (bits_to_send >> i & 0x01) {
314 AT91C_BASE_SSC->SSC_THR = 0xff;
315 } else {
316 AT91C_BASE_SSC->SSC_THR = 0x00;
317 }
318 j++;
319 }
320 WDT_HIT();
321 }
15c4dc5a 322 }
15c4dc5a 323 }
70b2fc0a 324 LED_C_OFF();
15c4dc5a 325}
326
9455b51c 327
70b2fc0a 328//=============================================================================
8c6cca0b 329// An ISO 15693 decoder for tag responses (one subcarrier only).
330// Uses cross correlation to identify the SOF, each bit, and EOF.
70b2fc0a 331// This function is called 8 times per bit (every 2 subcarrier cycles).
8c6cca0b 332// Subcarrier frequency fs is 424kHz, 1/fs = 2,36us,
70b2fc0a 333// i.e. function is called every 4,72us
334// LED handling:
335// LED C -> ON once we have received the SOF and are expecting the rest.
336// LED C -> OFF once we have received EOF or are unsynced
337//
338// Returns: true if we received a EOF
339// false if we are still waiting for some more
340//=============================================================================
341
342#define SUBCARRIER_DETECT_THRESHOLD 2
343#define SOF_CORRELATOR_LEN (1<<5)
344
8c6cca0b 345typedef struct DecodeTag {
70b2fc0a 346 enum {
8c6cca0b 347 STATE_TAG_UNSYNCD,
348 STATE_TAG_AWAIT_SOF_1,
349 STATE_TAG_AWAIT_SOF_2,
350 STATE_TAG_RECEIVING_DATA,
351 STATE_TAG_AWAIT_EOF
70b2fc0a 352 } state;
353 int bitCount;
354 int posCount;
355 enum {
356 LOGIC0,
357 LOGIC1,
358 SOF_PART1,
359 SOF_PART2
360 } lastBit;
361 uint16_t shiftReg;
362 uint8_t *output;
363 int len;
364 int sum1, sum2;
365 uint8_t SOF_low;
366 uint8_t SOF_high;
367 uint8_t SOF_last;
368 int32_t SOF_corr;
369 int32_t SOF_corr_prev;
370 uint8_t SOF_correlator[SOF_CORRELATOR_LEN];
8c6cca0b 371} DecodeTag_t;
70b2fc0a 372
8c6cca0b 373static int Handle15693SamplesFromTag(int8_t ci, int8_t cq, DecodeTag_t *DecodeTag)
15c4dc5a 374{
8c6cca0b 375 switch(DecodeTag->state) {
376 case STATE_TAG_UNSYNCD:
70b2fc0a 377 // initialize SOF correlator. We are looking for 12 samples low and 12 samples high.
8c6cca0b 378 DecodeTag->SOF_low = 0;
379 DecodeTag->SOF_high = 12;
380 DecodeTag->SOF_last = 23;
381 memset(DecodeTag->SOF_correlator, 0x00, DecodeTag->SOF_last + 1);
382 DecodeTag->SOF_correlator[DecodeTag->SOF_last] = AMPLITUDE(ci,cq);
383 DecodeTag->SOF_corr = DecodeTag->SOF_correlator[DecodeTag->SOF_last];
384 DecodeTag->SOF_corr_prev = DecodeTag->SOF_corr;
385 // initialize Decoder
386 DecodeTag->posCount = 0;
387 DecodeTag->bitCount = 0;
388 DecodeTag->len = 0;
389 DecodeTag->state = STATE_TAG_AWAIT_SOF_1;
70b2fc0a 390 break;
8c6cca0b 391
392 case STATE_TAG_AWAIT_SOF_1:
70b2fc0a 393 // calculate the correlation in real time. Look at differences only.
8c6cca0b 394 DecodeTag->SOF_corr += DecodeTag->SOF_correlator[DecodeTag->SOF_low++];
395 DecodeTag->SOF_corr -= 2*DecodeTag->SOF_correlator[DecodeTag->SOF_high++];
396 DecodeTag->SOF_last++;
397 DecodeTag->SOF_low &= (SOF_CORRELATOR_LEN-1);
398 DecodeTag->SOF_high &= (SOF_CORRELATOR_LEN-1);
399 DecodeTag->SOF_last &= (SOF_CORRELATOR_LEN-1);
400 DecodeTag->SOF_correlator[DecodeTag->SOF_last] = AMPLITUDE(ci,cq);
401 DecodeTag->SOF_corr += DecodeTag->SOF_correlator[DecodeTag->SOF_last];
70b2fc0a 402
403 // if correlation increases for 10 consecutive samples, we are close to maximum correlation
8c6cca0b 404 if (DecodeTag->SOF_corr > DecodeTag->SOF_corr_prev + SUBCARRIER_DETECT_THRESHOLD) {
405 DecodeTag->posCount++;
70b2fc0a 406 } else {
8c6cca0b 407 DecodeTag->posCount = 0;
15c4dc5a 408 }
15c4dc5a 409
8c6cca0b 410 if (DecodeTag->posCount == 10) { // correlation increased 10 times
411 DecodeTag->state = STATE_TAG_AWAIT_SOF_2;
70b2fc0a 412 }
8c6cca0b 413
414 DecodeTag->SOF_corr_prev = DecodeTag->SOF_corr;
415
70b2fc0a 416 break;
417
8c6cca0b 418 case STATE_TAG_AWAIT_SOF_2:
70b2fc0a 419 // calculate the correlation in real time. Look at differences only.
8c6cca0b 420 DecodeTag->SOF_corr += DecodeTag->SOF_correlator[DecodeTag->SOF_low++];
421 DecodeTag->SOF_corr -= 2*DecodeTag->SOF_correlator[DecodeTag->SOF_high++];
422 DecodeTag->SOF_last++;
423 DecodeTag->SOF_low &= (SOF_CORRELATOR_LEN-1);
424 DecodeTag->SOF_high &= (SOF_CORRELATOR_LEN-1);
425 DecodeTag->SOF_last &= (SOF_CORRELATOR_LEN-1);
426 DecodeTag->SOF_correlator[DecodeTag->SOF_last] = AMPLITUDE(ci,cq);
427 DecodeTag->SOF_corr += DecodeTag->SOF_correlator[DecodeTag->SOF_last];
428
429 if (DecodeTag->SOF_corr >= DecodeTag->SOF_corr_prev) { // we are looking for the maximum correlation
430 DecodeTag->SOF_corr_prev = DecodeTag->SOF_corr;
70b2fc0a 431 } else {
8c6cca0b 432 DecodeTag->lastBit = SOF_PART1; // detected 1st part of SOF
433 DecodeTag->sum1 = DecodeTag->SOF_correlator[DecodeTag->SOF_last];
434 DecodeTag->sum2 = 0;
435 DecodeTag->posCount = 2;
436 DecodeTag->state = STATE_TAG_RECEIVING_DATA;
70b2fc0a 437 LED_C_ON();
438 }
8c6cca0b 439
70b2fc0a 440 break;
15c4dc5a 441
8c6cca0b 442 case STATE_TAG_RECEIVING_DATA:
443 if (DecodeTag->posCount == 1) {
444 DecodeTag->sum1 = 0;
445 DecodeTag->sum2 = 0;
70b2fc0a 446 }
15c4dc5a 447
8c6cca0b 448 if (DecodeTag->posCount <= 4) {
449 DecodeTag->sum1 += AMPLITUDE(ci, cq);
70b2fc0a 450 } else {
8c6cca0b 451 DecodeTag->sum2 += AMPLITUDE(ci, cq);
70b2fc0a 452 }
15c4dc5a 453
8c6cca0b 454 if (DecodeTag->posCount == 8) {
455 int16_t corr_1 = (DecodeTag->sum2 - DecodeTag->sum1) / 4;
456 int16_t corr_0 = (DecodeTag->sum1 - DecodeTag->sum2) / 4;
457 int16_t corr_EOF = (DecodeTag->sum1 + DecodeTag->sum2) / 8;
70b2fc0a 458 if (corr_EOF > corr_0 && corr_EOF > corr_1) {
8c6cca0b 459 DecodeTag->state = STATE_TAG_AWAIT_EOF;
70b2fc0a 460 } else if (corr_1 > corr_0) {
461 // logic 1
8c6cca0b 462 if (DecodeTag->lastBit == SOF_PART1) { // still part of SOF
463 DecodeTag->lastBit = SOF_PART2;
70b2fc0a 464 } else {
8c6cca0b 465 DecodeTag->lastBit = LOGIC1;
466 DecodeTag->shiftReg >>= 1;
467 DecodeTag->shiftReg |= 0x80;
468 DecodeTag->bitCount++;
469 if (DecodeTag->bitCount == 8) {
470 DecodeTag->output[DecodeTag->len] = DecodeTag->shiftReg;
471 DecodeTag->len++;
472 DecodeTag->bitCount = 0;
473 DecodeTag->shiftReg = 0;
70b2fc0a 474 }
475 }
476 } else {
477 // logic 0
8c6cca0b 478 if (DecodeTag->lastBit == SOF_PART1) { // incomplete SOF
479 DecodeTag->state = STATE_TAG_UNSYNCD;
70b2fc0a 480 LED_C_OFF();
481 } else {
8c6cca0b 482 DecodeTag->lastBit = LOGIC0;
483 DecodeTag->shiftReg >>= 1;
484 DecodeTag->bitCount++;
485 if (DecodeTag->bitCount == 8) {
486 DecodeTag->output[DecodeTag->len] = DecodeTag->shiftReg;
487 DecodeTag->len++;
488 DecodeTag->bitCount = 0;
489 DecodeTag->shiftReg = 0;
70b2fc0a 490 }
491 }
492 }
8c6cca0b 493 DecodeTag->posCount = 0;
70b2fc0a 494 }
8c6cca0b 495 DecodeTag->posCount++;
70b2fc0a 496 break;
8c6cca0b 497
498 case STATE_TAG_AWAIT_EOF:
499 if (DecodeTag->lastBit == LOGIC0) { // this was already part of EOF
70b2fc0a 500 LED_C_OFF();
501 return true;
502 } else {
8c6cca0b 503 DecodeTag->state = STATE_TAG_UNSYNCD;
70b2fc0a 504 LED_C_OFF();
505 }
506 break;
507
508 default:
8c6cca0b 509 DecodeTag->state = STATE_TAG_UNSYNCD;
70b2fc0a 510 LED_C_OFF();
511 break;
15c4dc5a 512 }
15c4dc5a 513
70b2fc0a 514 return false;
515}
15c4dc5a 516
15c4dc5a 517
8c6cca0b 518static void DecodeTagInit(DecodeTag_t *DecodeTag, uint8_t *data)
70b2fc0a 519{
8c6cca0b 520 DecodeTag->output = data;
521 DecodeTag->state = STATE_TAG_UNSYNCD;
70b2fc0a 522}
523
70b2fc0a 524/*
8c6cca0b 525 * Receive and decode the tag response, also log to tracebuffer
70b2fc0a 526 */
527static int GetIso15693AnswerFromTag(uint8_t* response, int timeout)
528{
529 int maxBehindBy = 0;
530 int lastRxCounter, samples = 0;
531 int8_t ci, cq;
532 bool gotFrame = false;
70b2fc0a 533
8c6cca0b 534 uint16_t dmaBuf[ISO15693_DMA_BUFFER_SIZE];
70b2fc0a 535
8c6cca0b 536 // the Decoder data structure
537 DecodeTag_t DecodeTag;
538 DecodeTagInit(&DecodeTag, response);
70b2fc0a 539
540 // wait for last transfer to complete
541 while (!(AT91C_BASE_SSC->SSC_SR & AT91C_SSC_TXEMPTY));
542
543 // And put the FPGA in the appropriate mode
544 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR);
545
546 // Setup and start DMA.
547 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_RX_XCORR);
548 FpgaSetupSscDma((uint8_t*) dmaBuf, ISO15693_DMA_BUFFER_SIZE);
70b2fc0a 549 uint16_t *upTo = dmaBuf;
550 lastRxCounter = ISO15693_DMA_BUFFER_SIZE;
551
552 for(;;) {
553 int behindBy = (lastRxCounter - AT91C_BASE_PDC_SSC->PDC_RCR) & (ISO15693_DMA_BUFFER_SIZE-1);
554 if(behindBy > maxBehindBy) {
555 maxBehindBy = behindBy;
15c4dc5a 556 }
70b2fc0a 557
558 if (behindBy < 1) continue;
8c6cca0b 559
70b2fc0a 560 ci = (int8_t)(*upTo >> 8);
561 cq = (int8_t)(*upTo & 0xff);
562
563 upTo++;
564 lastRxCounter--;
565 if(upTo >= dmaBuf + ISO15693_DMA_BUFFER_SIZE) { // we have read all of the DMA buffer content.
566 upTo = dmaBuf; // start reading the circular buffer from the beginning
567 lastRxCounter += ISO15693_DMA_BUFFER_SIZE;
15c4dc5a 568 }
70b2fc0a 569 if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_ENDRX)) { // DMA Counter Register had reached 0, already rotated.
570 AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) dmaBuf; // refresh the DMA Next Buffer and
571 AT91C_BASE_PDC_SSC->PDC_RNCR = ISO15693_DMA_BUFFER_SIZE; // DMA Next Counter registers
15c4dc5a 572 }
70b2fc0a 573 samples++;
15c4dc5a 574
8c6cca0b 575 if (Handle15693SamplesFromTag(ci, cq, &DecodeTag)) {
70b2fc0a 576 gotFrame = true;
577 break;
578 }
15c4dc5a 579
8c6cca0b 580 if(samples > timeout && DecodeTag.state < STATE_TAG_RECEIVING_DATA) {
581 DecodeTag.len = 0;
70b2fc0a 582 break;
583 }
8c6cca0b 584
70b2fc0a 585 }
586
587 FpgaDisableSscDma();
588
8c6cca0b 589 if (DEBUG) Dbprintf("max behindby = %d, samples = %d, gotFrame = %d, Decoder: state = %d, len = %d, bitCount = %d, posCount = %d",
590 maxBehindBy, samples, gotFrame, DecodeTag.state, DecodeTag.len, DecodeTag.bitCount, DecodeTag.posCount);
70b2fc0a 591
8c6cca0b 592 if (tracing && DecodeTag.len > 0) {
593 LogTrace(DecodeTag.output, DecodeTag.len, 0, 0, NULL, false);
70b2fc0a 594 }
595
8c6cca0b 596 return DecodeTag.len;
15c4dc5a 597}
598
9455b51c 599
8c6cca0b 600//=============================================================================
601// An ISO15693 decoder for reader commands.
602//
603// This function is called 4 times per bit (every 2 subcarrier cycles).
604// Subcarrier frequency fs is 848kHz, 1/fs = 1,18us, i.e. function is called every 2,36us
605// LED handling:
606// LED B -> ON once we have received the SOF and are expecting the rest.
607// LED B -> OFF once we have received EOF or are in error state or unsynced
608//
609// Returns: true if we received a EOF
610// false if we are still waiting for some more
611//=============================================================================
612
613typedef struct DecodeReader {
614 enum {
615 STATE_READER_UNSYNCD,
616 STATE_READER_AWAIT_1ST_RISING_EDGE_OF_SOF,
617 STATE_READER_AWAIT_2ND_FALLING_EDGE_OF_SOF,
618 STATE_READER_AWAIT_2ND_RISING_EDGE_OF_SOF,
619 STATE_READER_AWAIT_END_OF_SOF_1_OUT_OF_4,
620 STATE_READER_RECEIVE_DATA_1_OUT_OF_4,
621 STATE_READER_RECEIVE_DATA_1_OUT_OF_256
622 } state;
623 enum {
624 CODING_1_OUT_OF_4,
625 CODING_1_OUT_OF_256
626 } Coding;
627 uint8_t shiftReg;
628 uint8_t bitCount;
629 int byteCount;
630 int byteCountMax;
631 int posCount;
632 int sum1, sum2;
633 uint8_t *output;
634} DecodeReader_t;
635
636
637static int Handle15693SampleFromReader(uint8_t bit, DecodeReader_t* DecodeReader)
15c4dc5a 638{
8c6cca0b 639 switch(DecodeReader->state) {
640 case STATE_READER_UNSYNCD:
641 if(!bit) {
642 // we went low, so this could be the beginning of a SOF
643 DecodeReader->state = STATE_READER_AWAIT_1ST_RISING_EDGE_OF_SOF;
644 DecodeReader->posCount = 1;
645 }
646 break;
15c4dc5a 647
8c6cca0b 648 case STATE_READER_AWAIT_1ST_RISING_EDGE_OF_SOF:
649 DecodeReader->posCount++;
650 if(bit) { // detected rising edge
651 if(DecodeReader->posCount < 4) { // rising edge too early (nominally expected at 5)
652 DecodeReader->state = STATE_READER_UNSYNCD;
653 } else { // SOF
654 DecodeReader->state = STATE_READER_AWAIT_2ND_FALLING_EDGE_OF_SOF;
655 }
656 } else {
657 if(DecodeReader->posCount > 5) { // stayed low for too long
658 DecodeReader->state = STATE_READER_UNSYNCD;
659 } else {
660 // do nothing, keep waiting
661 }
662 }
663 break;
664
665 case STATE_READER_AWAIT_2ND_FALLING_EDGE_OF_SOF:
666 DecodeReader->posCount++;
667 if(!bit) { // detected a falling edge
668 if (DecodeReader->posCount < 20) { // falling edge too early (nominally expected at 21 earliest)
669 DecodeReader->state = STATE_READER_UNSYNCD;
670 } else if (DecodeReader->posCount < 23) { // SOF for 1 out of 4 coding
671 DecodeReader->Coding = CODING_1_OUT_OF_4;
672 DecodeReader->state = STATE_READER_AWAIT_2ND_RISING_EDGE_OF_SOF;
673 } else if (DecodeReader->posCount < 28) { // falling edge too early (nominally expected at 29 latest)
674 DecodeReader->state = STATE_READER_UNSYNCD;
675 } else { // SOF for 1 out of 4 coding
676 DecodeReader->Coding = CODING_1_OUT_OF_256;
677 DecodeReader->state = STATE_READER_AWAIT_2ND_RISING_EDGE_OF_SOF;
678 }
679 } else {
680 if(DecodeReader->posCount > 29) { // stayed high for too long
681 DecodeReader->state = STATE_READER_UNSYNCD;
682 } else {
683 // do nothing, keep waiting
684 }
685 }
686 break;
687
688 case STATE_READER_AWAIT_2ND_RISING_EDGE_OF_SOF:
689 DecodeReader->posCount++;
690 if (bit) { // detected rising edge
691 if (DecodeReader->Coding == CODING_1_OUT_OF_256) {
692 if (DecodeReader->posCount < 32) { // rising edge too early (nominally expected at 33)
693 DecodeReader->state = STATE_READER_UNSYNCD;
694 } else {
695 DecodeReader->posCount = 1;
696 DecodeReader->bitCount = 0;
697 DecodeReader->byteCount = 0;
698 DecodeReader->sum1 = 1;
699 DecodeReader->state = STATE_READER_RECEIVE_DATA_1_OUT_OF_256;
700 LED_B_ON();
701 }
702 } else { // CODING_1_OUT_OF_4
703 if (DecodeReader->posCount < 24) { // rising edge too early (nominally expected at 25)
704 DecodeReader->state = STATE_READER_UNSYNCD;
705 } else {
706 DecodeReader->state = STATE_READER_AWAIT_END_OF_SOF_1_OUT_OF_4;
707 }
708 }
709 } else {
710 if (DecodeReader->Coding == CODING_1_OUT_OF_256) {
711 if (DecodeReader->posCount > 34) { // signal stayed low for too long
712 DecodeReader->state = STATE_READER_UNSYNCD;
713 } else {
714 // do nothing, keep waiting
715 }
716 } else { // CODING_1_OUT_OF_4
717 if (DecodeReader->posCount > 26) { // signal stayed low for too long
718 DecodeReader->state = STATE_READER_UNSYNCD;
719 } else {
720 // do nothing, keep waiting
721 }
722 }
723 }
724 break;
725
726 case STATE_READER_AWAIT_END_OF_SOF_1_OUT_OF_4:
727 DecodeReader->posCount++;
728 if (bit) {
729 if (DecodeReader->posCount == 33) {
730 DecodeReader->posCount = 1;
731 DecodeReader->bitCount = 0;
732 DecodeReader->byteCount = 0;
733 DecodeReader->sum1 = 1;
734 DecodeReader->state = STATE_READER_RECEIVE_DATA_1_OUT_OF_4;
735 LED_B_ON();
736 } else {
737 // do nothing, keep waiting
738 }
739 } else { // unexpected falling edge
740 DecodeReader->state = STATE_READER_UNSYNCD;
741 }
742 break;
743
744 case STATE_READER_RECEIVE_DATA_1_OUT_OF_4:
745 DecodeReader->posCount++;
746 if (DecodeReader->posCount == 1) {
747 DecodeReader->sum1 = bit;
748 } else if (DecodeReader->posCount <= 4) {
749 DecodeReader->sum1 += bit;
750 } else if (DecodeReader->posCount == 5) {
751 DecodeReader->sum2 = bit;
752 } else {
753 DecodeReader->sum2 += bit;
754 }
755 if (DecodeReader->posCount == 8) {
756 DecodeReader->posCount = 0;
757 int corr10 = DecodeReader->sum1 - DecodeReader->sum2;
758 int corr01 = DecodeReader->sum2 - DecodeReader->sum1;
759 int corr11 = (DecodeReader->sum1 + DecodeReader->sum2) / 2;
760 if (corr01 > corr11 && corr01 > corr10) { // EOF
761 LED_B_OFF(); // Finished receiving
762 DecodeReader->state = STATE_READER_UNSYNCD;
763 if (DecodeReader->byteCount != 0) {
764 return true;
765 }
766 }
767 if (corr10 > corr11) { // detected a 2bit position
768 DecodeReader->shiftReg >>= 2;
769 DecodeReader->shiftReg |= (DecodeReader->bitCount << 6);
770 }
771 if (DecodeReader->bitCount == 15) { // we have a full byte
772 DecodeReader->output[DecodeReader->byteCount++] = DecodeReader->shiftReg;
773 if (DecodeReader->byteCount > DecodeReader->byteCountMax) {
774 // buffer overflow, give up
775 LED_B_OFF();
776 DecodeReader->state = STATE_READER_UNSYNCD;
777 }
778 DecodeReader->bitCount = 0;
779 } else {
780 DecodeReader->bitCount++;
781 }
782 }
783 break;
784
785 case STATE_READER_RECEIVE_DATA_1_OUT_OF_256:
786 DecodeReader->posCount++;
787 if (DecodeReader->posCount == 1) {
788 DecodeReader->sum1 = bit;
789 } else if (DecodeReader->posCount <= 4) {
790 DecodeReader->sum1 += bit;
791 } else if (DecodeReader->posCount == 5) {
792 DecodeReader->sum2 = bit;
793 } else {
794 DecodeReader->sum2 += bit;
795 }
796 if (DecodeReader->posCount == 8) {
797 DecodeReader->posCount = 0;
798 int corr10 = DecodeReader->sum1 - DecodeReader->sum2;
799 int corr01 = DecodeReader->sum2 - DecodeReader->sum1;
800 int corr11 = (DecodeReader->sum1 + DecodeReader->sum2) / 2;
801 if (corr01 > corr11 && corr01 > corr10) { // EOF
802 LED_B_OFF(); // Finished receiving
803 DecodeReader->state = STATE_READER_UNSYNCD;
804 if (DecodeReader->byteCount != 0) {
805 return true;
806 }
807 }
808 if (corr10 > corr11) { // detected the bit position
809 DecodeReader->shiftReg = DecodeReader->bitCount;
810 }
811 if (DecodeReader->bitCount == 255) { // we have a full byte
812 DecodeReader->output[DecodeReader->byteCount++] = DecodeReader->shiftReg;
813 if (DecodeReader->byteCount > DecodeReader->byteCountMax) {
814 // buffer overflow, give up
815 LED_B_OFF();
816 DecodeReader->state = STATE_READER_UNSYNCD;
817 }
818 }
819 DecodeReader->bitCount++;
820 }
821 break;
822
823 default:
824 LED_B_OFF();
825 DecodeReader->state = STATE_READER_UNSYNCD;
826 break;
15c4dc5a 827 }
8c6cca0b 828
829 return false;
830}
831
832
833static void DecodeReaderInit(uint8_t *data, uint16_t max_len, DecodeReader_t* DecodeReader)
834{
835 DecodeReader->output = data;
836 DecodeReader->byteCountMax = max_len;
837 DecodeReader->state = STATE_READER_UNSYNCD;
838 DecodeReader->byteCount = 0;
839 DecodeReader->bitCount = 0;
3685f89c 840 DecodeReader->posCount = 0;
8c6cca0b 841 DecodeReader->shiftReg = 0;
842}
843
844
845//-----------------------------------------------------------------------------
846// Receive a command (from the reader to us, where we are the simulated tag),
847// and store it in the given buffer, up to the given maximum length. Keeps
848// spinning, waiting for a well-framed command, until either we get one
849// (returns true) or someone presses the pushbutton on the board (false).
850//
851// Assume that we're called with the SSC (to the FPGA) and ADC path set
852// correctly.
853//-----------------------------------------------------------------------------
854
855static int GetIso15693CommandFromReader(uint8_t *received, size_t max_len, uint32_t *eof_time)
856{
857 int maxBehindBy = 0;
858 int lastRxCounter, samples = 0;
859 bool gotFrame = false;
860 uint8_t b;
861
862 uint8_t dmaBuf[ISO15693_DMA_BUFFER_SIZE];
863
864 // the decoder data structure
6eeb5f1c 865 DecodeReader_t DecodeReader = {0};
8c6cca0b 866 DecodeReaderInit(received, max_len, &DecodeReader);
867
868 // wait for last transfer to complete
869 while (!(AT91C_BASE_SSC->SSC_SR & AT91C_SSC_TXEMPTY));
870
70b2fc0a 871 LED_D_OFF();
8c6cca0b 872 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR | FPGA_HF_SIMULATOR_NO_MODULATION);
15c4dc5a 873
8c6cca0b 874 // clear receive register and wait for next transfer
875 uint32_t temp = AT91C_BASE_SSC->SSC_RHR;
876 (void) temp;
877 while (!(AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY)) ;
15c4dc5a 878
8c6cca0b 879 uint32_t bit_time = GetCountSspClk() & 0xfffffff8;
15c4dc5a 880
8c6cca0b 881 // Setup and start DMA.
882 FpgaSetupSscDma(dmaBuf, ISO15693_DMA_BUFFER_SIZE);
883 uint8_t *upTo = dmaBuf;
884 lastRxCounter = ISO15693_DMA_BUFFER_SIZE;
15c4dc5a 885
8c6cca0b 886 for(;;) {
887 int behindBy = (lastRxCounter - AT91C_BASE_PDC_SSC->PDC_RCR) & (ISO15693_DMA_BUFFER_SIZE-1);
888 if(behindBy > maxBehindBy) {
889 maxBehindBy = behindBy;
15c4dc5a 890 }
70b2fc0a 891
8c6cca0b 892 if (behindBy < 1) continue;
15c4dc5a 893
8c6cca0b 894 b = *upTo++;
895 lastRxCounter--;
896 if(upTo >= dmaBuf + ISO15693_DMA_BUFFER_SIZE) { // we have read all of the DMA buffer content.
897 upTo = dmaBuf; // start reading the circular buffer from the beginning
898 lastRxCounter += ISO15693_DMA_BUFFER_SIZE;
899 }
900 if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_ENDRX)) { // DMA Counter Register had reached 0, already rotated.
901 AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) dmaBuf; // refresh the DMA Next Buffer and
902 AT91C_BASE_PDC_SSC->PDC_RNCR = ISO15693_DMA_BUFFER_SIZE; // DMA Next Counter registers
903 }
15c4dc5a 904
8c6cca0b 905 for (int i = 7; i >= 0; i--) {
906 if (Handle15693SampleFromReader((b >> i) & 0x01, &DecodeReader)) {
907 *eof_time = bit_time + samples - DELAY_READER_TO_ARM; // end of EOF
908 gotFrame = true;
9455b51c 909 break;
910 }
8c6cca0b 911 samples++;
15c4dc5a 912 }
8c6cca0b 913
914 if (gotFrame) {
915 break;
15c4dc5a 916 }
8c6cca0b 917
918 if (BUTTON_PRESS()) {
919 DecodeReader.byteCount = 0;
920 break;
15c4dc5a 921 }
15c4dc5a 922
8c6cca0b 923 WDT_HIT();
924 }
925
926
927 FpgaDisableSscDma();
928
929 if (DEBUG) Dbprintf("max behindby = %d, samples = %d, gotFrame = %d, Decoder: state = %d, len = %d, bitCount = %d, posCount = %d",
930 maxBehindBy, samples, gotFrame, DecodeReader.state, DecodeReader.byteCount, DecodeReader.bitCount, DecodeReader.posCount);
931
932 if (tracing && DecodeReader.byteCount > 0) {
933 LogTrace(DecodeReader.output, DecodeReader.byteCount, 0, 0, NULL, true);
934 }
935
936 return DecodeReader.byteCount;
15c4dc5a 937}
938
9455b51c 939
940static void BuildIdentifyRequest(void);
15c4dc5a 941//-----------------------------------------------------------------------------
942// Start to read an ISO 15693 tag. We send an identify request, then wait
943// for the response. The response is not demodulated, just left in the buffer
944// so that it can be downloaded to a PC and processed there.
945//-----------------------------------------------------------------------------
946void AcquireRawAdcSamplesIso15693(void)
947{
70b2fc0a 948 LEDsoff();
949 LED_A_ON();
8c6cca0b 950
117d9ec2 951 uint8_t *dest = BigBuf_get_addr();
15c4dc5a 952
7cc204bf 953 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
15c4dc5a 954 BuildIdentifyRequest();
955
956 SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
957
958 // Give the tags time to energize
70b2fc0a 959 LED_D_ON();
15c4dc5a 960 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR);
961 SpinDelay(100);
962
963 // Now send the command
6a5d4e17 964 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_TX);
15c4dc5a 965 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX);
966
70b2fc0a 967 LED_B_ON();
968 for(int c = 0; c < ToSendMax; ) {
15c4dc5a 969 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
6a5d4e17 970 AT91C_BASE_SSC->SSC_THR = ~ToSend[c];
15c4dc5a 971 c++;
15c4dc5a 972 }
15c4dc5a 973 WDT_HIT();
974 }
70b2fc0a 975 LED_B_OFF();
976
977 // wait for last transfer to complete
978 while (!(AT91C_BASE_SSC->SSC_SR & AT91C_SSC_TXEMPTY));
15c4dc5a 979
6a5d4e17 980 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_RX_XCORR);
15c4dc5a 981 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR);
982
70b2fc0a 983 for(int c = 0; c < 4000; ) {
15c4dc5a 984 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
6a5d4e17 985 uint16_t iq = AT91C_BASE_SSC->SSC_RHR;
9455b51c 986 // The samples are correlations against I and Q versions of the
8c6cca0b 987 // tone that the tag AM-modulates. We just want power,
6a5d4e17 988 // so abs(I) + abs(Q) is close to what we want.
70b2fc0a 989 int8_t i = (int8_t)(iq >> 8);
990 int8_t q = (int8_t)(iq & 0xff);
6a5d4e17 991 uint8_t r = AMPLITUDE(i, q);
6a5d4e17 992 dest[c++] = r;
9455b51c 993 }
994 }
70b2fc0a 995
996 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
997 LEDsoff();
9455b51c 998}
999
1000
8c6cca0b 1001// TODO: there is no trigger condition. The 14000 samples represent a time frame of 66ms.
70b2fc0a 1002// It is unlikely that we get something meaningful.
1003// TODO: Currently we only record tag answers. Add tracing of reader commands.
1004// TODO: would we get something at all? The carrier is switched on...
9455b51c 1005void RecordRawAdcSamplesIso15693(void)
1006{
70b2fc0a 1007 LEDsoff();
1008 LED_A_ON();
8c6cca0b 1009
117d9ec2 1010 uint8_t *dest = BigBuf_get_addr();
3fe4ff4f 1011
7cc204bf 1012 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
9455b51c 1013 // Setup SSC
6a5d4e17 1014 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_RX_XCORR);
9455b51c 1015
1016 // Start from off (no field generated)
6a5d4e17 1017 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1018 SpinDelay(200);
9455b51c 1019
1020 SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
1021
1022 SpinDelay(100);
1023
70b2fc0a 1024 LED_D_ON();
9455b51c 1025 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR);
1026
70b2fc0a 1027 for(int c = 0; c < 14000;) {
9455b51c 1028 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
6a5d4e17 1029 uint16_t iq = AT91C_BASE_SSC->SSC_RHR;
9455b51c 1030 // The samples are correlations against I and Q versions of the
8c6cca0b 1031 // tone that the tag AM-modulates. We just want power,
6a5d4e17 1032 // so abs(I) + abs(Q) is close to what we want.
70b2fc0a 1033 int8_t i = (int8_t)(iq >> 8);
1034 int8_t q = (int8_t)(iq & 0xff);
6a5d4e17 1035 uint8_t r = AMPLITUDE(i, q);
6a5d4e17 1036 dest[c++] = r;
9455b51c 1037 }
1038 }
70b2fc0a 1039
1040 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1041 LED_D_OFF();
6a5d4e17 1042 Dbprintf("finished recording");
70b2fc0a 1043 LED_A_OFF();
9455b51c 1044}
1045
1046
8c6cca0b 1047// Initialize the proxmark as iso15k reader
e6304bca 1048// (this might produces glitches that confuse some tags
70b2fc0a 1049static void Iso15693InitReader() {
7cc204bf 1050 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
9455b51c 1051 // Setup SSC
e6304bca 1052 // FpgaSetupSsc();
9455b51c 1053
1054 // Start from off (no field generated)
70b2fc0a 1055 LED_D_OFF();
9455b51c 1056 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
e6304bca 1057 SpinDelay(10);
9455b51c 1058
1059 SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
6a5d4e17 1060 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_RX_XCORR);
9455b51c 1061
1062 // Give the tags time to energize
70b2fc0a 1063 LED_D_ON();
9455b51c 1064 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR);
e6304bca 1065 SpinDelay(250);
9455b51c 1066}
1067
1068///////////////////////////////////////////////////////////////////////
1069// ISO 15693 Part 3 - Air Interface
70b2fc0a 1070// This section basically contains transmission and receiving of bits
9455b51c 1071///////////////////////////////////////////////////////////////////////
1072
1073// Encode (into the ToSend buffers) an identify request, which is the first
1074// thing that you must send to a tag to get a response.
1075static void BuildIdentifyRequest(void)
1076{
1077 uint8_t cmd[5];
1078
1079 uint16_t crc;
1080 // one sub-carrier, inventory, 1 slot, fast rate
1081 // AFI is at bit 5 (1<<4) when doing an INVENTORY
1082 cmd[0] = (1 << 2) | (1 << 5) | (1 << 1);
1083 // inventory command code
1084 cmd[1] = 0x01;
1085 // no mask
1086 cmd[2] = 0x00;
1087 //Now the CRC
1088 crc = Crc(cmd, 3);
1089 cmd[3] = crc & 0xff;
1090 cmd[4] = crc >> 8;
1091
1092 CodeIso15693AsReader(cmd, sizeof(cmd));
1093}
1094
1095// uid is in transmission order (which is reverse of display order)
1096static void BuildReadBlockRequest(uint8_t *uid, uint8_t blockNumber )
1097{
1098 uint8_t cmd[13];
1099
1100 uint16_t crc;
1101 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1102 // followed by teh block data
1103 // one sub-carrier, inventory, 1 slot, fast rate
1104 cmd[0] = (1 << 6)| (1 << 5) | (1 << 1); // no SELECT bit, ADDR bit, OPTION bit
1105 // READ BLOCK command code
1106 cmd[1] = 0x20;
1107 // UID may be optionally specified here
1108 // 64-bit UID
1109 cmd[2] = uid[0];
1110 cmd[3] = uid[1];
1111 cmd[4] = uid[2];
1112 cmd[5] = uid[3];
1113 cmd[6] = uid[4];
1114 cmd[7] = uid[5];
1115 cmd[8] = uid[6];
1116 cmd[9] = uid[7]; // 0xe0; // always e0 (not exactly unique)
1117 // Block number to read
1118 cmd[10] = blockNumber;//0x00;
1119 //Now the CRC
8c6cca0b 1120 crc = Crc(cmd, 11); // the crc needs to be calculated over 11 bytes
9455b51c 1121 cmd[11] = crc & 0xff;
1122 cmd[12] = crc >> 8;
1123
1124 CodeIso15693AsReader(cmd, sizeof(cmd));
1125}
1126
70b2fc0a 1127
9455b51c 1128// Now the VICC>VCD responses when we are simulating a tag
8c6cca0b 1129static void BuildInventoryResponse(uint8_t *uid)
9455b51c 1130{
1131 uint8_t cmd[12];
1132
1133 uint16_t crc;
8c6cca0b 1134
1135 cmd[0] = 0; // No error, no protocol format extension
3fe4ff4f 1136 cmd[1] = 0; // DSFID (data storage format identifier). 0x00 = not supported
9455b51c 1137 // 64-bit UID
3fe4ff4f 1138 cmd[2] = uid[7]; //0x32;
1139 cmd[3] = uid[6]; //0x4b;
1140 cmd[4] = uid[5]; //0x03;
1141 cmd[5] = uid[4]; //0x01;
1142 cmd[6] = uid[3]; //0x00;
1143 cmd[7] = uid[2]; //0x10;
1144 cmd[8] = uid[1]; //0x05;
1145 cmd[9] = uid[0]; //0xe0;
9455b51c 1146 //Now the CRC
1147 crc = Crc(cmd, 10);
1148 cmd[10] = crc & 0xff;
1149 cmd[11] = crc >> 8;
1150
8c6cca0b 1151 CodeIso15693AsTag(cmd, sizeof(cmd));
9455b51c 1152}
1153
e6304bca 1154// Universal Method for sending to and recv bytes from a tag
9455b51c 1155// init ... should we initialize the reader?
8c6cca0b 1156// speed ... 0 low speed, 1 hi speed
9455b51c 1157// **recv will return you a pointer to the received data
8c6cca0b 1158// If you do not need the answer use NULL for *recv[]
9455b51c 1159// return: lenght of received data
70b2fc0a 1160int SendDataTag(uint8_t *send, int sendlen, bool init, int speed, uint8_t **recv) {
9455b51c 1161
9455b51c 1162 LED_A_ON();
70b2fc0a 1163 LED_B_OFF();
9455b51c 1164 LED_C_OFF();
8c6cca0b 1165
09ffd16e 1166 if (init) Iso15693InitReader();
1167
9455b51c 1168 int answerLen=0;
315e18e6 1169 uint8_t *answer = BigBuf_get_addr() + 4000;
117d9ec2 1170 if (recv != NULL) memset(answer, 0, 100);
9455b51c 1171
9455b51c 1172 if (!speed) {
1173 // low speed (1 out of 256)
1174 CodeIso15693AsReader256(send, sendlen);
1175 } else {
1176 // high speed (1 out of 4)
1177 CodeIso15693AsReader(send, sendlen);
1178 }
8c6cca0b 1179
1180 TransmitTo15693Tag(ToSend,ToSendMax);
9455b51c 1181 // Now wait for a response
1182 if (recv!=NULL) {
8c6cca0b 1183 answerLen = GetIso15693AnswerFromTag(answer, 100);
9455b51c 1184 *recv=answer;
1185 }
1186
1187 LED_A_OFF();
8c6cca0b 1188
9455b51c 1189 return answerLen;
1190}
15c4dc5a 1191
15c4dc5a 1192
9455b51c 1193// --------------------------------------------------------------------
8c6cca0b 1194// Debug Functions
9455b51c 1195// --------------------------------------------------------------------
15c4dc5a 1196
9455b51c 1197// Decodes a message from a tag and displays its metadata and content
1198#define DBD15STATLEN 48
1199void DbdecodeIso15693Answer(int len, uint8_t *d) {
1200 char status[DBD15STATLEN+1]={0};
1201 uint16_t crc;
1202
1203 if (len>3) {
8c6cca0b 1204 if (d[0]&(1<<3))
9455b51c 1205 strncat(status,"ProtExt ",DBD15STATLEN);
8c6cca0b 1206 if (d[0]&1) {
9455b51c 1207 // error
1208 strncat(status,"Error ",DBD15STATLEN);
1209 switch (d[1]) {
8c6cca0b 1210 case 0x01:
9455b51c 1211 strncat(status,"01:notSupp",DBD15STATLEN);
15c4dc5a 1212 break;
8c6cca0b 1213 case 0x02:
9455b51c 1214 strncat(status,"02:notRecog",DBD15STATLEN);
1215 break;
8c6cca0b 1216 case 0x03:
9455b51c 1217 strncat(status,"03:optNotSupp",DBD15STATLEN);
1218 break;
8c6cca0b 1219 case 0x0f:
9455b51c 1220 strncat(status,"0f:noInfo",DBD15STATLEN);
1221 break;
8c6cca0b 1222 case 0x10:
9455b51c 1223 strncat(status,"10:dontExist",DBD15STATLEN);
1224 break;
8c6cca0b 1225 case 0x11:
9455b51c 1226 strncat(status,"11:lockAgain",DBD15STATLEN);
1227 break;
8c6cca0b 1228 case 0x12:
9455b51c 1229 strncat(status,"12:locked",DBD15STATLEN);
1230 break;
8c6cca0b 1231 case 0x13:
9455b51c 1232 strncat(status,"13:progErr",DBD15STATLEN);
1233 break;
8c6cca0b 1234 case 0x14:
9455b51c 1235 strncat(status,"14:lockErr",DBD15STATLEN);
1236 break;
1237 default:
1238 strncat(status,"unknownErr",DBD15STATLEN);
15c4dc5a 1239 }
9455b51c 1240 strncat(status," ",DBD15STATLEN);
1241 } else {
1242 strncat(status,"NoErr ",DBD15STATLEN);
15c4dc5a 1243 }
8c6cca0b 1244
9455b51c 1245 crc=Crc(d,len-2);
8c6cca0b 1246 if ( (( crc & 0xff ) == d[len-2]) && (( crc >> 8 ) == d[len-1]) )
9455b51c 1247 strncat(status,"CrcOK",DBD15STATLEN);
1248 else
8c6cca0b 1249 strncat(status,"CrcFail!",DBD15STATLEN);
9455b51c 1250
1251 Dbprintf("%s",status);
15c4dc5a 1252 }
1253}
1254
9455b51c 1255
1256
1257///////////////////////////////////////////////////////////////////////
1258// Functions called via USB/Client
1259///////////////////////////////////////////////////////////////////////
1260
1261void SetDebugIso15693(uint32_t debug) {
1262 DEBUG=debug;
1263 Dbprintf("Iso15693 Debug is now %s",DEBUG?"on":"off");
1264 return;
1265}
1266
15c4dc5a 1267//-----------------------------------------------------------------------------
1268// Simulate an ISO15693 reader, perform anti-collision and then attempt to read a sector
1269// all demodulation performed in arm rather than host. - greg
1270//-----------------------------------------------------------------------------
f7e3ed82 1271void ReaderIso15693(uint32_t parameter)
15c4dc5a 1272{
70b2fc0a 1273 LEDsoff();
15c4dc5a 1274 LED_A_ON();
15c4dc5a 1275
15c4dc5a 1276 int answerLen1 = 0;
3fe4ff4f 1277 uint8_t TagUID[8] = {0x00};
1278
09ffd16e 1279 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
15c4dc5a 1280
315e18e6 1281 uint8_t *answer1 = BigBuf_get_addr() + 4000;
315e18e6 1282 memset(answer1, 0x00, 200);
15c4dc5a 1283
3fe4ff4f 1284 SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
15c4dc5a 1285 // Setup SSC
6a5d4e17 1286 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER_RX_XCORR);
15c4dc5a 1287
1288 // Start from off (no field generated)
09ffd16e 1289 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1290 SpinDelay(200);
15c4dc5a 1291
15c4dc5a 1292 // Give the tags time to energize
70b2fc0a 1293 LED_D_ON();
15c4dc5a 1294 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR);
1295 SpinDelay(200);
1296
15c4dc5a 1297 // FIRST WE RUN AN INVENTORY TO GET THE TAG UID
1298 // THIS MEANS WE CAN PRE-BUILD REQUESTS TO SAVE CPU TIME
15c4dc5a 1299
1300 // Now send the IDENTIFY command
1301 BuildIdentifyRequest();
8c6cca0b 1302
70b2fc0a 1303 TransmitTo15693Tag(ToSend,ToSendMax);
8c6cca0b 1304
15c4dc5a 1305 // Now wait for a response
70b2fc0a 1306 answerLen1 = GetIso15693AnswerFromTag(answer1, 100) ;
15c4dc5a 1307
1308 if (answerLen1 >=12) // we should do a better check than this
1309 {
15c4dc5a 1310 TagUID[0] = answer1[2];
1311 TagUID[1] = answer1[3];
1312 TagUID[2] = answer1[4];
1313 TagUID[3] = answer1[5];
1314 TagUID[4] = answer1[6];
1315 TagUID[5] = answer1[7];
1316 TagUID[6] = answer1[8]; // IC Manufacturer code
9455b51c 1317 TagUID[7] = answer1[9]; // always E0
15c4dc5a 1318
15c4dc5a 1319 }
1320
9455b51c 1321 Dbprintf("%d octets read from IDENTIFY request:", answerLen1);
8c6cca0b 1322 DbdecodeIso15693Answer(answerLen1, answer1);
1323 Dbhexdump(answerLen1, answer1, false);
9455b51c 1324
1325 // UID is reverse
8c6cca0b 1326 if (answerLen1 >= 12)
3fe4ff4f 1327 Dbprintf("UID = %02hX%02hX%02hX%02hX%02hX%02hX%02hX%02hX",
1328 TagUID[7],TagUID[6],TagUID[5],TagUID[4],
1329 TagUID[3],TagUID[2],TagUID[1],TagUID[0]);
9455b51c 1330
1331
315e18e6 1332 // Dbprintf("%d octets read from SELECT request:", answerLen2);
1333 // DbdecodeIso15693Answer(answerLen2,answer2);
1334 // Dbhexdump(answerLen2,answer2,true);
9455b51c 1335
315e18e6 1336 // Dbprintf("%d octets read from XXX request:", answerLen3);
1337 // DbdecodeIso15693Answer(answerLen3,answer3);
1338 // Dbhexdump(answerLen3,answer3,true);
9455b51c 1339
9455b51c 1340 // read all pages
70b2fc0a 1341 if (answerLen1 >= 12 && DEBUG) {
1342 uint8_t *answer2 = BigBuf_get_addr() + 4100;
8c6cca0b 1343 int i = 0;
1344 while (i < 32) { // sanity check, assume max 32 pages
1345 BuildReadBlockRequest(TagUID, i);
1346 TransmitTo15693Tag(ToSend, ToSendMax);
70b2fc0a 1347 int answerLen2 = GetIso15693AnswerFromTag(answer2, 100);
8c6cca0b 1348 if (answerLen2 > 0) {
1349 Dbprintf("READ SINGLE BLOCK %d returned %d octets:", i, answerLen2);
1350 DbdecodeIso15693Answer(answerLen2, answer2);
1351 Dbhexdump(answerLen2, answer2, false);
1352 if ( *((uint32_t*) answer2) == 0x07160101 ) break; // exit on NoPageErr
1353 }
9455b51c 1354 i++;
8c6cca0b 1355 }
9455b51c 1356 }
15c4dc5a 1357
8c6cca0b 1358 // for the time being, switch field off to protect rdv4.0
70b2fc0a 1359 // note: this prevents using hf 15 cmd with s option - which isn't implemented yet anyway
1360 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
15c4dc5a 1361 LED_D_OFF();
8c6cca0b 1362
70b2fc0a 1363 LED_A_OFF();
15c4dc5a 1364}
1365
8c6cca0b 1366
1367// Simulate an ISO15693 TAG.
1368// For Inventory command: print command and send Inventory Response with given UID
1369// TODO: interpret other reader commands and send appropriate response
3fe4ff4f 1370void SimTagIso15693(uint32_t parameter, uint8_t *uid)
15c4dc5a 1371{
70b2fc0a 1372 LEDsoff();
15c4dc5a 1373 LED_A_ON();
15c4dc5a 1374
7cc204bf 1375 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
15c4dc5a 1376 SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
8c6cca0b 1377 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR | FPGA_HF_SIMULATOR_NO_MODULATION);
1378 FpgaSetupSsc(FPGA_MAJOR_MODE_HF_SIMULATOR);
15c4dc5a 1379
8c6cca0b 1380 StartCountSspClk();
15c4dc5a 1381
8c6cca0b 1382 uint8_t cmd[ISO15693_MAX_COMMAND_LENGTH];
15c4dc5a 1383
8c6cca0b 1384 // Build a suitable response to the reader INVENTORY command
1385 BuildInventoryResponse(uid);
15c4dc5a 1386
8c6cca0b 1387 // Listen to reader
1388 while (!BUTTON_PRESS()) {
1389 uint32_t eof_time = 0, start_time = 0;
1390 int cmd_len = GetIso15693CommandFromReader(cmd, sizeof(cmd), &eof_time);
1391
1392 if ((cmd_len >= 5) && (cmd[0] & ISO15693_REQ_INVENTORY) && (cmd[1] == ISO15693_INVENTORY)) { // TODO: check more flags
1393 bool slow = !(cmd[0] & ISO15693_REQ_DATARATE_HIGH);
1394 start_time = eof_time + DELAY_ISO15693_VCD_TO_VICC - DELAY_ARM_TO_READER;
1395 TransmitTo15693Reader(ToSend, ToSendMax, start_time, slow);
1396 }
3fe4ff4f 1397
8c6cca0b 1398 Dbprintf("%d bytes read from reader:", cmd_len);
1399 Dbhexdump(cmd_len, cmd, false);
1400 }
15c4dc5a 1401
70b2fc0a 1402 LEDsoff();
15c4dc5a 1403}
9455b51c 1404
1405
1406// Since there is no standardized way of reading the AFI out of a tag, we will brute force it
1407// (some manufactures offer a way to read the AFI, though)
8c6cca0b 1408void BruteforceIso15693Afi(uint32_t speed)
1409{
70b2fc0a 1410 LEDsoff();
1411 LED_A_ON();
8c6cca0b 1412
9455b51c 1413 uint8_t data[20];
1414 uint8_t *recv=data;
1415 int datalen=0, recvlen=0;
8c6cca0b 1416
9455b51c 1417 Iso15693InitReader();
8c6cca0b 1418
9455b51c 1419 // first without AFI
8c6cca0b 1420 // Tags should respond without AFI and with AFI=0 even when AFI is active
1421
1422 data[0] = ISO15693_REQ_DATARATE_HIGH | ISO15693_REQ_INVENTORY | ISO15693_REQINV_SLOT1;
1423 data[1] = ISO15693_INVENTORY;
1424 data[2] = 0; // mask length
1425 datalen = AddCrc(data,3);
1426 recvlen = SendDataTag(data, datalen, false, speed, &recv);
9455b51c 1427 WDT_HIT();
1428 if (recvlen>=12) {
1429 Dbprintf("NoAFI UID=%s",sprintUID(NULL,&recv[2]));
1430 }
8c6cca0b 1431
9455b51c 1432 // now with AFI
8c6cca0b 1433
1434 data[0] = ISO15693_REQ_DATARATE_HIGH | ISO15693_REQ_INVENTORY | ISO15693_REQINV_AFI | ISO15693_REQINV_SLOT1;
1435 data[1] = ISO15693_INVENTORY;
1436 data[2] = 0; // AFI
1437 data[3] = 0; // mask length
1438
9455b51c 1439 for (int i=0;i<256;i++) {
1440 data[2]=i & 0xFF;
1441 datalen=AddCrc(data,4);
70b2fc0a 1442 recvlen=SendDataTag(data, datalen, false, speed, &recv);
9455b51c 1443 WDT_HIT();
1444 if (recvlen>=12) {
8c6cca0b 1445 Dbprintf("AFI=%i UID=%s", i, sprintUID(NULL,&recv[2]));
9455b51c 1446 }
8c6cca0b 1447 }
9455b51c 1448 Dbprintf("AFI Bruteforcing done.");
8c6cca0b 1449
70b2fc0a 1450 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1451 LEDsoff();
9455b51c 1452}
1453
1454// Allows to directly send commands to the tag via the client
70b2fc0a 1455void DirectTag15693Command(uint32_t datalen, uint32_t speed, uint32_t recv, uint8_t data[]) {
9455b51c 1456
1457 int recvlen=0;
117d9ec2 1458 uint8_t *recvbuf = BigBuf_get_addr();
8c6cca0b 1459
70b2fc0a 1460 LED_A_ON();
8c6cca0b 1461
9455b51c 1462 if (DEBUG) {
1463 Dbprintf("SEND");
8c6cca0b 1464 Dbhexdump(datalen, data, false);
9455b51c 1465 }
8c6cca0b 1466
70b2fc0a 1467 recvlen = SendDataTag(data, datalen, true, speed, (recv?&recvbuf:NULL));
9455b51c 1468
8c6cca0b 1469 if (recv) {
70b2fc0a 1470 cmd_send(CMD_ACK, recvlen>48?48:recvlen, 0, 0, recvbuf, 48);
8c6cca0b 1471
9455b51c 1472 if (DEBUG) {
1473 Dbprintf("RECV");
8c6cca0b 1474 DbdecodeIso15693Answer(recvlen,recvbuf);
1475 Dbhexdump(recvlen, recvbuf, false);
9455b51c 1476 }
1477 }
1478
8c6cca0b 1479 // for the time being, switch field off to protect rdv4.0
70b2fc0a 1480 // note: this prevents using hf 15 cmd with s option - which isn't implemented yet anyway
1481 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
1482 LED_D_OFF();
8c6cca0b 1483
70b2fc0a 1484 LED_A_OFF();
9455b51c 1485}
1486
1487
1488
1489
1490// --------------------------------------------------------------------
1491// -- Misc & deprecated functions
1492// --------------------------------------------------------------------
1493
e6304bca 1494/*
9455b51c 1495
1496// do not use; has a fix UID
1497static void __attribute__((unused)) BuildSysInfoRequest(uint8_t *uid)
1498{
1499 uint8_t cmd[12];
1500
1501 uint16_t crc;
1502 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1503 // followed by teh block data
1504 // one sub-carrier, inventory, 1 slot, fast rate
1505 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1506 // System Information command code
1507 cmd[1] = 0x2B;
1508 // UID may be optionally specified here
1509 // 64-bit UID
1510 cmd[2] = 0x32;
1511 cmd[3]= 0x4b;
1512 cmd[4] = 0x03;
1513 cmd[5] = 0x01;
1514 cmd[6] = 0x00;
1515 cmd[7] = 0x10;
1516 cmd[8] = 0x05;
1517 cmd[9]= 0xe0; // always e0 (not exactly unique)
1518 //Now the CRC
1519 crc = Crc(cmd, 10); // the crc needs to be calculated over 2 bytes
1520 cmd[10] = crc & 0xff;
1521 cmd[11] = crc >> 8;
1522
1523 CodeIso15693AsReader(cmd, sizeof(cmd));
1524}
1525
9455b51c 1526
1527// do not use; has a fix UID
1528static void __attribute__((unused)) BuildReadMultiBlockRequest(uint8_t *uid)
1529{
1530 uint8_t cmd[14];
1531
1532 uint16_t crc;
1533 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1534 // followed by teh block data
1535 // one sub-carrier, inventory, 1 slot, fast rate
1536 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1537 // READ Multi BLOCK command code
1538 cmd[1] = 0x23;
1539 // UID may be optionally specified here
1540 // 64-bit UID
1541 cmd[2] = 0x32;
1542 cmd[3]= 0x4b;
1543 cmd[4] = 0x03;
1544 cmd[5] = 0x01;
1545 cmd[6] = 0x00;
1546 cmd[7] = 0x10;
1547 cmd[8] = 0x05;
1548 cmd[9]= 0xe0; // always e0 (not exactly unique)
1549 // First Block number to read
1550 cmd[10] = 0x00;
1551 // Number of Blocks to read
1552 cmd[11] = 0x2f; // read quite a few
1553 //Now the CRC
1554 crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes
1555 cmd[12] = crc & 0xff;
1556 cmd[13] = crc >> 8;
1557
1558 CodeIso15693AsReader(cmd, sizeof(cmd));
1559}
1560
1561// do not use; has a fix UID
1562static void __attribute__((unused)) BuildArbitraryRequest(uint8_t *uid,uint8_t CmdCode)
1563{
1564 uint8_t cmd[14];
1565
1566 uint16_t crc;
1567 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1568 // followed by teh block data
1569 // one sub-carrier, inventory, 1 slot, fast rate
1570 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1571 // READ BLOCK command code
1572 cmd[1] = CmdCode;
1573 // UID may be optionally specified here
1574 // 64-bit UID
1575 cmd[2] = 0x32;
1576 cmd[3]= 0x4b;
1577 cmd[4] = 0x03;
1578 cmd[5] = 0x01;
1579 cmd[6] = 0x00;
1580 cmd[7] = 0x10;
1581 cmd[8] = 0x05;
1582 cmd[9]= 0xe0; // always e0 (not exactly unique)
1583 // Parameter
1584 cmd[10] = 0x00;
1585 cmd[11] = 0x0a;
1586
1587// cmd[12] = 0x00;
1588// cmd[13] = 0x00; //Now the CRC
1589 crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes
1590 cmd[12] = crc & 0xff;
1591 cmd[13] = crc >> 8;
1592
1593 CodeIso15693AsReader(cmd, sizeof(cmd));
1594}
1595
1596// do not use; has a fix UID
1597static void __attribute__((unused)) BuildArbitraryCustomRequest(uint8_t uid[], uint8_t CmdCode)
1598{
1599 uint8_t cmd[14];
1600
1601 uint16_t crc;
1602 // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block
1603 // followed by teh block data
1604 // one sub-carrier, inventory, 1 slot, fast rate
1605 cmd[0] = (1 << 5) | (1 << 1); // no SELECT bit
1606 // READ BLOCK command code
1607 cmd[1] = CmdCode;
1608 // UID may be optionally specified here
1609 // 64-bit UID
1610 cmd[2] = 0x32;
1611 cmd[3]= 0x4b;
1612 cmd[4] = 0x03;
1613 cmd[5] = 0x01;
1614 cmd[6] = 0x00;
1615 cmd[7] = 0x10;
1616 cmd[8] = 0x05;
1617 cmd[9]= 0xe0; // always e0 (not exactly unique)
1618 // Parameter
1619 cmd[10] = 0x05; // for custom codes this must be manufcturer code
1620 cmd[11] = 0x00;
1621
1622// cmd[12] = 0x00;
1623// cmd[13] = 0x00; //Now the CRC
1624 crc = Crc(cmd, 12); // the crc needs to be calculated over 2 bytes
1625 cmd[12] = crc & 0xff;
1626 cmd[13] = crc >> 8;
1627
1628 CodeIso15693AsReader(cmd, sizeof(cmd));
1629}
1630
1631
1632
1633
e6304bca 1634*/
9455b51c 1635
1636
Impressum, Datenschutz