1 //-----------------------------------------------------------------------------
2 // Routines to support ISO 14443. This includes both the reader software and
3 // the `fake tag' modes. At the moment only the Type B modulation is
5 // Jonathan Westhues, split Nov 2006
6 //-----------------------------------------------------------------------------
9 #include "../common/iso14443_crc.c"
12 //static void GetSamplesFor14443(BOOL weTx, int n);
14 #define DMA_BUFFER_SIZE 256
16 //=============================================================================
17 // An ISO 14443 Type B tag. We listen for commands from the reader, using
18 // a UART kind of thing that's implemented in software. When we get a
19 // frame (i.e., a group of bytes between SOF and EOF), we check the CRC.
20 // If it's good, then we can do something appropriate with it, and send
22 //=============================================================================
24 //-----------------------------------------------------------------------------
25 // Code up a string of octets at layer 2 (including CRC, we don't generate
26 // that here) so that they can be transmitted to the reader. Doesn't transmit
27 // them yet, just leaves them ready to send in ToSend[].
28 //-----------------------------------------------------------------------------
29 static void CodeIso14443bAsTag(const BYTE
*cmd
, int len
)
35 // Transmit a burst of ones, as the initial thing that lets the
36 // reader get phase sync. This (TR1) must be > 80/fs, per spec,
37 // but tag that I've tried (a Paypass) exceeds that by a fair bit,
39 for(i
= 0; i
< 20; i
++) {
47 for(i
= 0; i
< 10; i
++) {
53 for(i
= 0; i
< 2; i
++) {
60 for(i
= 0; i
< len
; i
++) {
71 for(j
= 0; j
< 8; j
++) {
94 for(i
= 0; i
< 10; i
++) {
100 for(i
= 0; i
< 10; i
++) {
107 // Convert from last byte pos to length
110 // Add a few more for slop
114 //-----------------------------------------------------------------------------
115 // The software UART that receives commands from the reader, and its state
117 //-----------------------------------------------------------------------------
121 STATE_GOT_FALLING_EDGE_OF_SOF
,
122 STATE_AWAITING_START_BIT
,
123 STATE_RECEIVING_DATA
,
134 /* Receive & handle a bit coming from the reader.
137 * LED A -> ON once we have received the SOF and are expecting the rest.
138 * LED A -> OFF once we have received EOF or are in error state or unsynced
140 * Returns: true if we received a EOF
141 * false if we are still waiting for some more
143 static BOOL
Handle14443UartBit(int bit
)
149 // we went low, so this could be the beginning
151 Uart
.state
= STATE_GOT_FALLING_EDGE_OF_SOF
;
157 case STATE_GOT_FALLING_EDGE_OF_SOF
:
159 if(Uart
.posCnt
== 2) {
161 if(Uart
.bitCnt
>= 10) {
162 // we've seen enough consecutive
163 // zeros that it's a valid SOF
166 Uart
.state
= STATE_AWAITING_START_BIT
;
167 LED_A_ON(); // Indicate we got a valid SOF
169 // didn't stay down long enough
170 // before going high, error
171 Uart
.state
= STATE_ERROR_WAIT
;
174 // do nothing, keep waiting
178 if(Uart
.posCnt
>= 4) Uart
.posCnt
= 0;
179 if(Uart
.bitCnt
> 14) {
180 // Give up if we see too many zeros without
182 Uart
.state
= STATE_ERROR_WAIT
;
186 case STATE_AWAITING_START_BIT
:
189 if(Uart
.posCnt
> 25) {
190 // stayed high for too long between
192 Uart
.state
= STATE_ERROR_WAIT
;
195 // falling edge, this starts the data byte
199 Uart
.state
= STATE_RECEIVING_DATA
;
200 LED_A_ON(); // Indicate we're receiving
204 case STATE_RECEIVING_DATA
:
206 if(Uart
.posCnt
== 2) {
207 // time to sample a bit
210 Uart
.shiftReg
|= 0x200;
214 if(Uart
.posCnt
>= 4) {
217 if(Uart
.bitCnt
== 10) {
218 if((Uart
.shiftReg
& 0x200) && !(Uart
.shiftReg
& 0x001))
220 // this is a data byte, with correct
221 // start and stop bits
222 Uart
.output
[Uart
.byteCnt
] = (Uart
.shiftReg
>> 1) & 0xff;
225 if(Uart
.byteCnt
>= Uart
.byteCntMax
) {
226 // Buffer overflowed, give up
228 Uart
.state
= STATE_ERROR_WAIT
;
230 // so get the next byte now
232 Uart
.state
= STATE_AWAITING_START_BIT
;
234 } else if(Uart
.shiftReg
== 0x000) {
235 // this is an EOF byte
236 LED_A_OFF(); // Finished receiving
241 Uart
.state
= STATE_ERROR_WAIT
;
246 case STATE_ERROR_WAIT
:
247 // We're all screwed up, so wait a little while
248 // for whatever went wrong to finish, and then
251 if(Uart
.posCnt
> 10) {
252 Uart
.state
= STATE_UNSYNCD
;
257 Uart
.state
= STATE_UNSYNCD
;
261 if (Uart
.state
== STATE_ERROR_WAIT
) LED_A_OFF(); // Error
266 //-----------------------------------------------------------------------------
267 // Receive a command (from the reader to us, where we are the simulated tag),
268 // and store it in the given buffer, up to the given maximum length. Keeps
269 // spinning, waiting for a well-framed command, until either we get one
270 // (returns TRUE) or someone presses the pushbutton on the board (FALSE).
272 // Assume that we're called with the SSC (to the FPGA) and ADC path set
274 //-----------------------------------------------------------------------------
275 static BOOL
GetIso14443CommandFromReader(BYTE
*received
, int *len
, int maxLen
)
280 // Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen
281 // only, since we are receiving, not transmitting).
282 // Signal field is off with the appropriate LED
285 FPGA_MAJOR_MODE_HF_SIMULATOR
| FPGA_HF_SIMULATOR_NO_MODULATION
);
288 // Now run a `software UART' on the stream of incoming samples.
289 Uart
.output
= received
;
290 Uart
.byteCntMax
= maxLen
;
291 Uart
.state
= STATE_UNSYNCD
;
296 if(BUTTON_PRESS()) return FALSE
;
298 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
299 AT91C_BASE_SSC
->SSC_THR
= 0x00;
301 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
302 BYTE b
= (BYTE
)AT91C_BASE_SSC
->SSC_RHR
;
305 for(i
= 0; i
< 8; i
++, mask
>>= 1) {
307 if(Handle14443UartBit(bit
)) {
316 //-----------------------------------------------------------------------------
317 // Main loop of simulated tag: receive commands from reader, decide what
318 // response to send, and send it.
319 //-----------------------------------------------------------------------------
320 void SimulateIso14443Tag(void)
322 static const BYTE cmd1
[] = { 0x05, 0x00, 0x08, 0x39, 0x73 };
323 static const BYTE response1
[] = {
324 0x50, 0x82, 0x0d, 0xe1, 0x74, 0x20, 0x38, 0x19, 0x22,
325 0x00, 0x21, 0x85, 0x5e, 0xd7
331 BYTE
*resp1
= (((BYTE
*)BigBuf
) + 800);
334 BYTE
*receivedCmd
= (BYTE
*)BigBuf
;
341 memset(receivedCmd
, 0x44, 400);
343 CodeIso14443bAsTag(response1
, sizeof(response1
));
344 memcpy(resp1
, ToSend
, ToSendMax
); resp1Len
= ToSendMax
;
346 // We need to listen to the high-frequency, peak-detected path.
347 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
355 if(!GetIso14443CommandFromReader(receivedCmd
, &len
, 100)) {
356 DbpIntegers(cmdsRecvd
, 0, 0);
357 DbpString("button press");
361 // Good, look at the command now.
363 if(len
== sizeof(cmd1
) && memcmp(receivedCmd
, cmd1
, len
)==0) {
364 resp
= resp1
; respLen
= resp1Len
;
366 DbpString("new cmd from reader:");
367 DbpIntegers(len
, 0x1234, cmdsRecvd
);
368 // And print whether the CRC fails, just for good measure
369 ComputeCrc14443(CRC_14443_B
, receivedCmd
, len
-2, &b1
, &b2
);
370 if(b1
!= receivedCmd
[len
-2] || b2
!= receivedCmd
[len
-1]) {
371 // Not so good, try again.
372 DbpString("+++CRC fail");
374 DbpString("CRC passes");
379 memset(receivedCmd
, 0x44, 32);
383 if(cmdsRecvd
> 0x30) {
384 DbpString("many commands later...");
388 if(respLen
<= 0) continue;
391 // Signal field is off with the appropriate LED
394 FPGA_MAJOR_MODE_HF_SIMULATOR
| FPGA_HF_SIMULATOR_MODULATE_BPSK
);
395 AT91C_BASE_SSC
->SSC_THR
= 0xff;
398 // Transmit the response.
401 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
404 AT91C_BASE_SSC
->SSC_THR
= b
;
411 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
412 volatile BYTE b
= (BYTE
)AT91C_BASE_SSC
->SSC_RHR
;
419 //=============================================================================
420 // An ISO 14443 Type B reader. We take layer two commands, code them
421 // appropriately, and then send them to the tag. We then listen for the
422 // tag's response, which we leave in the buffer to be demodulated on the
424 //=============================================================================
429 DEMOD_PHASE_REF_TRAINING
,
430 DEMOD_AWAITING_FALLING_EDGE_OF_SOF
,
431 DEMOD_GOT_FALLING_EDGE_OF_SOF
,
432 DEMOD_AWAITING_START_BIT
,
433 DEMOD_RECEIVING_DATA
,
449 * Handles reception of a bit from the tag
452 * LED C -> ON once we have received the SOF and are expecting the rest.
453 * LED C -> OFF once we have received EOF or are unsynced
455 * Returns: true if we received a EOF
456 * false if we are still waiting for some more
459 static BOOL
Handle14443SamplesDemod(int ci
, int cq
)
463 // The soft decision on the bit uses an estimate of just the
464 // quadrant of the reference angle, not the exact angle.
465 #define MAKE_SOFT_DECISION() { \
466 if(Demod.sumI > 0) { \
471 if(Demod.sumQ > 0) { \
478 switch(Demod
.state
) {
489 Demod
.state
= DEMOD_PHASE_REF_TRAINING
;
495 case DEMOD_PHASE_REF_TRAINING
:
496 if(Demod
.posCount
< 8) {
499 } else if(Demod
.posCount
> 100) {
500 // error, waited too long
501 Demod
.state
= DEMOD_UNSYNCD
;
503 MAKE_SOFT_DECISION();
505 Demod
.state
= DEMOD_AWAITING_FALLING_EDGE_OF_SOF
;
512 case DEMOD_AWAITING_FALLING_EDGE_OF_SOF
:
513 MAKE_SOFT_DECISION();
515 Demod
.state
= DEMOD_GOT_FALLING_EDGE_OF_SOF
;
518 if(Demod
.posCount
> 100) {
519 Demod
.state
= DEMOD_UNSYNCD
;
525 case DEMOD_GOT_FALLING_EDGE_OF_SOF
:
526 MAKE_SOFT_DECISION();
528 if(Demod
.posCount
< 12) {
529 Demod
.state
= DEMOD_UNSYNCD
;
531 LED_C_ON(); // Got SOF
532 Demod
.state
= DEMOD_AWAITING_START_BIT
;
539 if(Demod
.posCount
> 100) {
540 Demod
.state
= DEMOD_UNSYNCD
;
546 case DEMOD_AWAITING_START_BIT
:
547 MAKE_SOFT_DECISION();
549 if(Demod
.posCount
> 10) {
550 Demod
.state
= DEMOD_UNSYNCD
;
557 Demod
.state
= DEMOD_RECEIVING_DATA
;
561 case DEMOD_RECEIVING_DATA
:
562 MAKE_SOFT_DECISION();
563 if(Demod
.posCount
== 0) {
569 if(Demod
.thisBit
> 0) {
570 Demod
.metric
+= Demod
.thisBit
;
572 Demod
.metric
-= Demod
.thisBit
;
576 Demod
.shiftReg
>>= 1;
577 if(Demod
.thisBit
> 0) {
578 Demod
.shiftReg
|= 0x200;
582 if(Demod
.bitCount
== 10) {
583 WORD s
= Demod
.shiftReg
;
584 if((s
& 0x200) && !(s
& 0x001)) {
586 Demod
.output
[Demod
.len
] = b
;
588 Demod
.state
= DEMOD_AWAITING_START_BIT
;
589 } else if(s
== 0x000) {
593 Demod
.state
= DEMOD_UNSYNCD
;
595 Demod
.state
= DEMOD_UNSYNCD
;
603 Demod
.state
= DEMOD_UNSYNCD
;
607 if (Demod
.state
== DEMOD_UNSYNCD
) LED_C_OFF(); // Not synchronized...
612 * Demodulate the samples we received from the tag
613 * weTx: set to 'TRUE' if we behave like a reader
614 * set to 'FALSE' if we behave like a snooper
615 * quiet: set to 'TRUE' to disable debug output
617 static void GetSamplesFor14443Demod(BOOL weTx
, int n
, BOOL quiet
)
620 BOOL gotFrame
= FALSE
;
622 //# define DMA_BUFFER_SIZE 8
632 // Clear out the state of the "UART" that receives from the tag.
633 memset(BigBuf
, 0x44, 400);
634 Demod
.output
= (BYTE
*)BigBuf
;
636 Demod
.state
= DEMOD_UNSYNCD
;
638 // And the UART that receives from the reader
639 Uart
.output
= (((BYTE
*)BigBuf
) + 1024);
640 Uart
.byteCntMax
= 100;
641 Uart
.state
= STATE_UNSYNCD
;
643 // Setup for the DMA.
644 dmaBuf
= (SBYTE
*)(BigBuf
+ 32);
646 lastRxCounter
= DMA_BUFFER_SIZE
;
647 FpgaSetupSscDma((BYTE
*)dmaBuf
, DMA_BUFFER_SIZE
);
649 // Signal field is ON with the appropriate LED:
650 if (weTx
) LED_D_ON(); else LED_D_OFF();
651 // And put the FPGA in the appropriate mode
653 FPGA_MAJOR_MODE_HF_READER_RX_XCORR
| FPGA_HF_READER_RX_XCORR_848_KHZ
|
654 (weTx
? 0 : FPGA_HF_READER_RX_XCORR_SNOOP
));
657 int behindBy
= lastRxCounter
- AT91C_BASE_PDC_SSC
->PDC_RCR
;
658 if(behindBy
> max
) max
= behindBy
;
660 while(((lastRxCounter
-AT91C_BASE_PDC_SSC
->PDC_RCR
) & (DMA_BUFFER_SIZE
-1))
666 if(upTo
- dmaBuf
> DMA_BUFFER_SIZE
) {
667 upTo
-= DMA_BUFFER_SIZE
;
668 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (DWORD
)upTo
;
669 AT91C_BASE_PDC_SSC
->PDC_RNCR
= DMA_BUFFER_SIZE
;
672 if(lastRxCounter
<= 0) {
673 lastRxCounter
+= DMA_BUFFER_SIZE
;
678 Handle14443UartBit(1);
679 Handle14443UartBit(1);
681 if(Handle14443SamplesDemod(ci
, cq
)) {
690 AT91C_BASE_PDC_SSC
->PDC_PTCR
= AT91C_PDC_RXTDIS
;
691 if (!quiet
) DbpIntegers(max
, gotFrame
, Demod
.len
);
694 //-----------------------------------------------------------------------------
695 // Read the tag's response. We just receive a stream of slightly-processed
696 // samples from the FPGA, which we will later do some signal processing on,
698 //-----------------------------------------------------------------------------
699 /*static void GetSamplesFor14443(BOOL weTx, int n)
701 BYTE *dest = (BYTE *)BigBuf;
705 FPGA_MAJOR_MODE_HF_READER_RX_XCORR | FPGA_HF_READER_RX_XCORR_848_KHZ |
706 (weTx ? 0 : FPGA_HF_READER_RX_XCORR_SNOOP));
710 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
711 AT91C_BASE_SSC->SSC_THR = 0x43;
713 if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
715 b = (SBYTE)AT91C_BASE_SSC->SSC_RHR;
726 //-----------------------------------------------------------------------------
727 // Transmit the command (to the tag) that was placed in ToSend[].
728 //-----------------------------------------------------------------------------
729 static void TransmitFor14443(void)
735 while(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
736 AT91C_BASE_SSC
->SSC_THR
= 0xff;
739 // Signal field is ON with the appropriate Red LED
741 // Signal we are transmitting with the Green LED
744 FPGA_MAJOR_MODE_HF_READER_TX
| FPGA_HF_READER_TX_SHALLOW_MOD
);
746 for(c
= 0; c
< 10;) {
747 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
748 AT91C_BASE_SSC
->SSC_THR
= 0xff;
751 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
752 volatile DWORD r
= AT91C_BASE_SSC
->SSC_RHR
;
760 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_TXRDY
)) {
761 AT91C_BASE_SSC
->SSC_THR
= ToSend
[c
];
767 if(AT91C_BASE_SSC
->SSC_SR
& (AT91C_SSC_RXRDY
)) {
768 volatile DWORD r
= AT91C_BASE_SSC
->SSC_RHR
;
773 LED_B_OFF(); // Finished sending
776 //-----------------------------------------------------------------------------
777 // Code a layer 2 command (string of octets, including CRC) into ToSend[],
778 // so that it is ready to transmit to the tag using TransmitFor14443().
779 //-----------------------------------------------------------------------------
780 void CodeIso14443bAsReader(const BYTE
*cmd
, int len
)
787 // Establish initial reference level
788 for(i
= 0; i
< 40; i
++) {
792 for(i
= 0; i
< 10; i
++) {
796 for(i
= 0; i
< len
; i
++) {
804 for(j
= 0; j
< 8; j
++) {
815 for(i
= 0; i
< 10; i
++) {
818 for(i
= 0; i
< 8; i
++) {
822 // And then a little more, to make sure that the last character makes
823 // it out before we switch to rx mode.
824 for(i
= 0; i
< 24; i
++) {
828 // Convert from last character reference to length
832 //-----------------------------------------------------------------------------
833 // Read an ISO 14443 tag. We send it some set of commands, and record the
835 // The command name is misleading, it actually decodes the reponse in HEX
836 // into the output buffer (read the result using hexsamples, not hisamples)
837 //-----------------------------------------------------------------------------
838 void AcquireRawAdcSamplesIso14443(DWORD parameter
)
840 BYTE cmd1
[] = { 0x05, 0x00, 0x08, 0x39, 0x73 };
842 // Make sure that we start from off, since the tags are stateful;
843 // confusing things will happen if we don't reset them between reads.
844 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
848 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
851 // Now give it time to spin up.
852 // Signal field is on with the appropriate LED
855 FPGA_MAJOR_MODE_HF_READER_RX_XCORR
| FPGA_HF_READER_RX_XCORR_848_KHZ
);
858 CodeIso14443bAsReader(cmd1
, sizeof(cmd1
));
861 GetSamplesFor14443Demod(TRUE
, 2000, FALSE
);
865 //-----------------------------------------------------------------------------
866 // Read a SRI512 ISO 14443 tag.
868 // SRI512 tags are just simple memory tags, here we're looking at making a dump
869 // of the contents of the memory. No anticollision algorithm is done, we assume
870 // we have a single tag in the field.
872 // I tried to be systematic and check every answer of the tag, every CRC, etc...
873 //-----------------------------------------------------------------------------
874 void ReadSRI512Iso14443(DWORD parameter
)
876 ReadSTMemoryIso14443(parameter
,0x0F);
878 void ReadSRIX4KIso14443(DWORD parameter
)
880 ReadSTMemoryIso14443(parameter
,0x7F);
883 void ReadSTMemoryIso14443(DWORD parameter
,DWORD dwLast
)
887 // Make sure that we start from off, since the tags are stateful;
888 // confusing things will happen if we don't reset them between reads.
890 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
893 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
896 // Now give it time to spin up.
897 // Signal field is on with the appropriate LED
900 FPGA_MAJOR_MODE_HF_READER_RX_XCORR
| FPGA_HF_READER_RX_XCORR_848_KHZ
);
903 // First command: wake up the tag using the INITIATE command
904 BYTE cmd1
[] = { 0x06, 0x00, 0x97, 0x5b};
905 CodeIso14443bAsReader(cmd1
, sizeof(cmd1
));
908 GetSamplesFor14443Demod(TRUE
, 2000,TRUE
);
911 if (Demod
.len
== 0) {
912 DbpString("No response from tag");
915 DbpString("Randomly generated UID from tag (+ 2 byte CRC):");
916 DbpIntegers(Demod
.output
[0], Demod
.output
[1],Demod
.output
[2]);
918 // There is a response, SELECT the uid
919 DbpString("Now SELECT tag:");
920 cmd1
[0] = 0x0E; // 0x0E is SELECT
921 cmd1
[1] = Demod
.output
[0];
922 ComputeCrc14443(CRC_14443_B
, cmd1
, 2, &cmd1
[2], &cmd1
[3]);
923 CodeIso14443bAsReader(cmd1
, sizeof(cmd1
));
926 GetSamplesFor14443Demod(TRUE
, 2000,TRUE
);
928 if (Demod
.len
!= 3) {
929 DbpString("Expected 3 bytes from tag, got:");
930 DbpIntegers(Demod
.len
,0x0,0x0);
933 // Check the CRC of the answer:
934 ComputeCrc14443(CRC_14443_B
, Demod
.output
, 1 , &cmd1
[2], &cmd1
[3]);
935 if(cmd1
[2] != Demod
.output
[1] || cmd1
[3] != Demod
.output
[2]) {
936 DbpString("CRC Error reading select response.");
939 // Check response from the tag: should be the same UID as the command we just sent:
940 if (cmd1
[1] != Demod
.output
[0]) {
941 DbpString("Bad response to SELECT from Tag, aborting:");
942 DbpIntegers(cmd1
[1],Demod
.output
[0],0x0);
945 // Tag is now selected,
946 // First get the tag's UID:
948 ComputeCrc14443(CRC_14443_B
, cmd1
, 1 , &cmd1
[1], &cmd1
[2]);
949 CodeIso14443bAsReader(cmd1
, 3); // Only first three bytes for this one
952 GetSamplesFor14443Demod(TRUE
, 2000,TRUE
);
954 if (Demod
.len
!= 10) {
955 DbpString("Expected 10 bytes from tag, got:");
956 DbpIntegers(Demod
.len
,0x0,0x0);
959 // The check the CRC of the answer (use cmd1 as temporary variable):
960 ComputeCrc14443(CRC_14443_B
, Demod
.output
, 8, &cmd1
[2], &cmd1
[3]);
961 if(cmd1
[2] != Demod
.output
[8] || cmd1
[3] != Demod
.output
[9]) {
962 DbpString("CRC Error reading block! - Below: expected, got");
963 DbpIntegers( (cmd1
[2]<<8)+cmd1
[3], (Demod
.output
[8]<<8)+Demod
.output
[9],0);
964 // Do not return;, let's go on... (we should retry, maybe ?)
966 DbpString("Tag UID (64 bits):");
967 DbpIntegers((Demod
.output
[7]<<24) + (Demod
.output
[6]<<16) + (Demod
.output
[5]<<8) + Demod
.output
[4], (Demod
.output
[3]<<24) + (Demod
.output
[2]<<16) + (Demod
.output
[1]<<8) + Demod
.output
[0], 0);
969 // Now loop to read all 16 blocks, address from 0 to 15
970 DbpString("Tag memory dump, block 0 to 15");
976 DbpString("System area block (0xff):");
980 ComputeCrc14443(CRC_14443_B
, cmd1
, 2, &cmd1
[2], &cmd1
[3]);
981 CodeIso14443bAsReader(cmd1
, sizeof(cmd1
));
984 GetSamplesFor14443Demod(TRUE
, 2000,TRUE
);
986 if (Demod
.len
!= 6) { // Check if we got an answer from the tag
987 DbpString("Expected 6 bytes from tag, got less...");
990 // The check the CRC of the answer (use cmd1 as temporary variable):
991 ComputeCrc14443(CRC_14443_B
, Demod
.output
, 4, &cmd1
[2], &cmd1
[3]);
992 if(cmd1
[2] != Demod
.output
[4] || cmd1
[3] != Demod
.output
[5]) {
993 DbpString("CRC Error reading block! - Below: expected, got");
994 DbpIntegers( (cmd1
[2]<<8)+cmd1
[3], (Demod
.output
[4]<<8)+Demod
.output
[5],0);
995 // Do not return;, let's go on... (we should retry, maybe ?)
997 // Now print out the memory location:
998 DbpString("Address , Contents, CRC");
999 DbpIntegers(i
, (Demod
.output
[3]<<24) + (Demod
.output
[2]<<16) + (Demod
.output
[1]<<8) + Demod
.output
[0], (Demod
.output
[4]<<8)+Demod
.output
[5]);
1008 //=============================================================================
1009 // Finally, the `sniffer' combines elements from both the reader and
1010 // simulated tag, to show both sides of the conversation.
1011 //=============================================================================
1013 //-----------------------------------------------------------------------------
1014 // Record the sequence of commands sent by the reader to the tag, with
1015 // triggering so that we start recording at the point that the tag is moved
1017 //-----------------------------------------------------------------------------
1019 * Memory usage for this function, (within BigBuf)
1020 * 0-1023 : Demodulated samples receive (1024 bytes)
1021 * 1024-1535 : Last Received command, 512 bytes (reader->tag)
1022 * 1536-2047 : Last Received command, 512 bytes(tag->reader)
1023 * 2048-2304 : DMA Buffer, 256 bytes (samples)
1025 void SnoopIso14443(void)
1027 // We won't start recording the frames that we acquire until we trigger;
1028 // a good trigger condition to get started is probably when we see a
1029 // response from the tag.
1030 BOOL triggered
= FALSE
;
1032 // The command (reader -> tag) that we're working on receiving.
1033 BYTE
*receivedCmd
= (BYTE
*)(BigBuf
) + 1024;
1034 // The response (tag -> reader) that we're working on receiving.
1035 BYTE
*receivedResponse
= (BYTE
*)(BigBuf
) + 1536;
1037 // As we receive stuff, we copy it from receivedCmd or receivedResponse
1038 // into trace, along with its length and other annotations.
1039 BYTE
*trace
= (BYTE
*)BigBuf
;
1042 // The DMA buffer, used to stream samples from the FPGA.
1043 SBYTE
*dmaBuf
= (SBYTE
*)(BigBuf
) + 2048;
1047 int maxBehindBy
= 0;
1049 // Count of samples received so far, so that we can include timing
1050 // information in the trace buffer.
1053 // Initialize the trace buffer
1054 memset(trace
, 0x44, 1024);
1056 // Set up the demodulator for tag -> reader responses.
1057 Demod
.output
= receivedResponse
;
1059 Demod
.state
= DEMOD_UNSYNCD
;
1061 // And the reader -> tag commands
1062 memset(&Uart
, 0, sizeof(Uart
));
1063 Uart
.output
= receivedCmd
;
1064 Uart
.byteCntMax
= 100;
1065 Uart
.state
= STATE_UNSYNCD
;
1067 // And put the FPGA in the appropriate mode
1068 // Signal field is off with the appropriate LED
1071 FPGA_MAJOR_MODE_HF_READER_RX_XCORR
| FPGA_HF_READER_RX_XCORR_848_KHZ
|
1072 FPGA_HF_READER_RX_XCORR_SNOOP
);
1073 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
1075 // Setup for the DMA.
1078 lastRxCounter
= DMA_BUFFER_SIZE
;
1079 FpgaSetupSscDma((BYTE
*)dmaBuf
, DMA_BUFFER_SIZE
);
1080 // And now we loop, receiving samples.
1082 int behindBy
= (lastRxCounter
- AT91C_BASE_PDC_SSC
->PDC_RCR
) &
1083 (DMA_BUFFER_SIZE
-1);
1084 if(behindBy
> maxBehindBy
) {
1085 maxBehindBy
= behindBy
;
1086 if(behindBy
> (DMA_BUFFER_SIZE
-2)) { // TODO: understand whether we can increase/decrease as we want or not?
1087 DbpString("blew circular buffer!");
1088 DbpIntegers(behindBy
,0,0);
1092 if(behindBy
< 2) continue;
1098 if(upTo
- dmaBuf
> DMA_BUFFER_SIZE
) {
1099 upTo
-= DMA_BUFFER_SIZE
;
1100 lastRxCounter
+= DMA_BUFFER_SIZE
;
1101 AT91C_BASE_PDC_SSC
->PDC_RNPR
= (DWORD
) upTo
;
1102 AT91C_BASE_PDC_SSC
->PDC_RNCR
= DMA_BUFFER_SIZE
;
1107 #define HANDLE_BIT_IF_BODY \
1109 trace[traceLen++] = ((samples >> 0) & 0xff); \
1110 trace[traceLen++] = ((samples >> 8) & 0xff); \
1111 trace[traceLen++] = ((samples >> 16) & 0xff); \
1112 trace[traceLen++] = ((samples >> 24) & 0xff); \
1113 trace[traceLen++] = 0; \
1114 trace[traceLen++] = 0; \
1115 trace[traceLen++] = 0; \
1116 trace[traceLen++] = 0; \
1117 trace[traceLen++] = Uart.byteCnt; \
1118 memcpy(trace+traceLen, receivedCmd, Uart.byteCnt); \
1119 traceLen += Uart.byteCnt; \
1120 if(traceLen > 1000) break; \
1122 /* And ready to receive another command. */ \
1123 memset(&Uart, 0, sizeof(Uart)); \
1124 Uart.output = receivedCmd; \
1125 Uart.byteCntMax = 100; \
1126 Uart.state = STATE_UNSYNCD; \
1127 /* And also reset the demod code, which might have been */ \
1128 /* false-triggered by the commands from the reader. */ \
1129 memset(&Demod, 0, sizeof(Demod)); \
1130 Demod.output = receivedResponse; \
1131 Demod.state = DEMOD_UNSYNCD; \
1133 if(Handle14443UartBit(ci & 1)) {
1136 if(Handle14443UartBit(cq
& 1)) {
1140 if(Handle14443SamplesDemod(ci
, cq
)) {
1141 // timestamp, as a count of samples
1142 trace
[traceLen
++] = ((samples
>> 0) & 0xff);
1143 trace
[traceLen
++] = ((samples
>> 8) & 0xff);
1144 trace
[traceLen
++] = ((samples
>> 16) & 0xff);
1145 trace
[traceLen
++] = 0x80 | ((samples
>> 24) & 0xff);
1146 // correlation metric (~signal strength estimate)
1147 if(Demod
.metricN
!= 0) {
1148 Demod
.metric
/= Demod
.metricN
;
1150 trace
[traceLen
++] = ((Demod
.metric
>> 0) & 0xff);
1151 trace
[traceLen
++] = ((Demod
.metric
>> 8) & 0xff);
1152 trace
[traceLen
++] = ((Demod
.metric
>> 16) & 0xff);
1153 trace
[traceLen
++] = ((Demod
.metric
>> 24) & 0xff);
1155 trace
[traceLen
++] = Demod
.len
;
1156 memcpy(trace
+traceLen
, receivedResponse
, Demod
.len
);
1157 traceLen
+= Demod
.len
;
1158 if(traceLen
> 1000) break;
1162 // And ready to receive another response.
1163 memset(&Demod
, 0, sizeof(Demod
));
1164 Demod
.output
= receivedResponse
;
1165 Demod
.state
= DEMOD_UNSYNCD
;
1169 if(BUTTON_PRESS()) {
1170 DbpString("cancelled");
1175 DbpString("in done pt");
1177 DbpIntegers(maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
1178 DbpIntegers(Uart
.byteCntMax
, traceLen
, 0x23);
1182 AT91C_BASE_PDC_SSC
->PDC_PTCR
= AT91C_PDC_RXTDIS
;