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 static BOOL
Handle14443UartBit(int bit
)
139 // we went low, so this could be the beginning
141 Uart
.state
= STATE_GOT_FALLING_EDGE_OF_SOF
;
147 case STATE_GOT_FALLING_EDGE_OF_SOF
:
149 if(Uart
.posCnt
== 2) {
151 if(Uart
.bitCnt
>= 10) {
152 // we've seen enough consecutive
153 // zeros that it's a valid SOF
156 Uart
.state
= STATE_AWAITING_START_BIT
;
158 // didn't stay down long enough
159 // before going high, error
160 Uart
.state
= STATE_ERROR_WAIT
;
163 // do nothing, keep waiting
167 if(Uart
.posCnt
>= 4) Uart
.posCnt
= 0;
168 if(Uart
.bitCnt
> 14) {
169 // Give up if we see too many zeros without
171 Uart
.state
= STATE_ERROR_WAIT
;
175 case STATE_AWAITING_START_BIT
:
178 if(Uart
.posCnt
> 25) {
179 // stayed high for too long between
181 Uart
.state
= STATE_ERROR_WAIT
;
184 // falling edge, this starts the data byte
188 Uart
.state
= STATE_RECEIVING_DATA
;
192 case STATE_RECEIVING_DATA
:
194 if(Uart
.posCnt
== 2) {
195 // time to sample a bit
198 Uart
.shiftReg
|= 0x200;
202 if(Uart
.posCnt
>= 4) {
205 if(Uart
.bitCnt
== 10) {
206 if((Uart
.shiftReg
& 0x200) && !(Uart
.shiftReg
& 0x001))
208 // this is a data byte, with correct
209 // start and stop bits
210 Uart
.output
[Uart
.byteCnt
] = (Uart
.shiftReg
>> 1) & 0xff;
213 if(Uart
.byteCnt
>= Uart
.byteCntMax
) {
214 // Buffer overflowed, give up
216 Uart
.state
= STATE_ERROR_WAIT
;
218 // so get the next byte now
220 Uart
.state
= STATE_AWAITING_START_BIT
;
222 } else if(Uart
.shiftReg
== 0x000) {
223 // this is an EOF byte
228 Uart
.state
= STATE_ERROR_WAIT
;
233 case STATE_ERROR_WAIT
:
234 // We're all screwed up, so wait a little while
235 // for whatever went wrong to finish, and then
238 if(Uart
.posCnt
> 10) {
239 Uart
.state
= STATE_UNSYNCD
;
244 Uart
.state
= STATE_UNSYNCD
;
251 //-----------------------------------------------------------------------------
252 // Receive a command (from the reader to us, where we are the simulated tag),
253 // and store it in the given buffer, up to the given maximum length. Keeps
254 // spinning, waiting for a well-framed command, until either we get one
255 // (returns TRUE) or someone presses the pushbutton on the board (FALSE).
257 // Assume that we're called with the SSC (to the FPGA) and ADC path set
259 //-----------------------------------------------------------------------------
260 static BOOL
GetIso14443CommandFromReader(BYTE
*received
, int *len
, int maxLen
)
265 // Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen
266 // only, since we are receiving, not transmitting).
268 FPGA_MAJOR_MODE_HF_SIMULATOR
| FPGA_HF_SIMULATOR_NO_MODULATION
);
271 // Now run a `software UART' on the stream of incoming samples.
272 Uart
.output
= received
;
273 Uart
.byteCntMax
= maxLen
;
274 Uart
.state
= STATE_UNSYNCD
;
279 if(BUTTON_PRESS()) return FALSE
;
281 if(SSC_STATUS
& (SSC_STATUS_TX_READY
)) {
282 SSC_TRANSMIT_HOLDING
= 0x00;
284 if(SSC_STATUS
& (SSC_STATUS_RX_READY
)) {
285 BYTE b
= (BYTE
)SSC_RECEIVE_HOLDING
;
288 for(i
= 0; i
< 8; i
++, mask
>>= 1) {
290 if(Handle14443UartBit(bit
)) {
299 //-----------------------------------------------------------------------------
300 // Main loop of simulated tag: receive commands from reader, decide what
301 // response to send, and send it.
302 //-----------------------------------------------------------------------------
303 void SimulateIso14443Tag(void)
305 static const BYTE cmd1
[] = { 0x05, 0x00, 0x08, 0x39, 0x73 };
306 static const BYTE response1
[] = {
307 0x50, 0x82, 0x0d, 0xe1, 0x74, 0x20, 0x38, 0x19, 0x22,
308 0x00, 0x21, 0x85, 0x5e, 0xd7
314 BYTE
*resp1
= (((BYTE
*)BigBuf
) + 800);
317 BYTE
*receivedCmd
= (BYTE
*)BigBuf
;
324 memset(receivedCmd
, 0x44, 400);
326 CodeIso14443bAsTag(response1
, sizeof(response1
));
327 memcpy(resp1
, ToSend
, ToSendMax
); resp1Len
= ToSendMax
;
329 // We need to listen to the high-frequency, peak-detected path.
330 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
338 if(!GetIso14443CommandFromReader(receivedCmd
, &len
, 100)) {
339 DbpIntegers(cmdsRecvd
, 0, 0);
340 DbpString("button press");
344 // Good, look at the command now.
346 if(len
== sizeof(cmd1
) && memcmp(receivedCmd
, cmd1
, len
)==0) {
347 resp
= resp1
; respLen
= resp1Len
;
349 DbpString("new cmd from reader:");
350 DbpIntegers(len
, 0x1234, cmdsRecvd
);
351 // And print whether the CRC fails, just for good measure
352 ComputeCrc14443(CRC_14443_B
, receivedCmd
, len
-2, &b1
, &b2
);
353 if(b1
!= receivedCmd
[len
-2] || b2
!= receivedCmd
[len
-1]) {
354 // Not so good, try again.
355 DbpString("+++CRC fail");
357 DbpString("CRC passes");
362 memset(receivedCmd
, 0x44, 32);
366 if(cmdsRecvd
> 0x30) {
367 DbpString("many commands later...");
371 if(respLen
<= 0) continue;
375 FPGA_MAJOR_MODE_HF_SIMULATOR
| FPGA_HF_SIMULATOR_MODULATE_BPSK
);
376 SSC_TRANSMIT_HOLDING
= 0xff;
379 // Transmit the response.
382 if(SSC_STATUS
& (SSC_STATUS_TX_READY
)) {
385 SSC_TRANSMIT_HOLDING
= b
;
392 if(SSC_STATUS
& (SSC_STATUS_RX_READY
)) {
393 volatile BYTE b
= (BYTE
)SSC_RECEIVE_HOLDING
;
400 //=============================================================================
401 // An ISO 14443 Type B reader. We take layer two commands, code them
402 // appropriately, and then send them to the tag. We then listen for the
403 // tag's response, which we leave in the buffer to be demodulated on the
405 //=============================================================================
410 DEMOD_PHASE_REF_TRAINING
,
411 DEMOD_AWAITING_FALLING_EDGE_OF_SOF
,
412 DEMOD_GOT_FALLING_EDGE_OF_SOF
,
413 DEMOD_AWAITING_START_BIT
,
414 DEMOD_RECEIVING_DATA
,
429 static BOOL
Handle14443SamplesDemod(int ci
, int cq
)
433 // The soft decision on the bit uses an estimate of just the
434 // quadrant of the reference angle, not the exact angle.
435 #define MAKE_SOFT_DECISION() { \
436 if(Demod.sumI > 0) { \
441 if(Demod.sumQ > 0) { \
448 switch(Demod
.state
) {
459 Demod
.state
= DEMOD_PHASE_REF_TRAINING
;
465 case DEMOD_PHASE_REF_TRAINING
:
466 if(Demod
.posCount
< 8) {
469 } else if(Demod
.posCount
> 100) {
470 // error, waited too long
471 Demod
.state
= DEMOD_UNSYNCD
;
473 MAKE_SOFT_DECISION();
475 Demod
.state
= DEMOD_AWAITING_FALLING_EDGE_OF_SOF
;
482 case DEMOD_AWAITING_FALLING_EDGE_OF_SOF
:
483 MAKE_SOFT_DECISION();
485 Demod
.state
= DEMOD_GOT_FALLING_EDGE_OF_SOF
;
488 if(Demod
.posCount
> 100) {
489 Demod
.state
= DEMOD_UNSYNCD
;
495 case DEMOD_GOT_FALLING_EDGE_OF_SOF
:
496 MAKE_SOFT_DECISION();
498 if(Demod
.posCount
< 12) {
499 Demod
.state
= DEMOD_UNSYNCD
;
501 Demod
.state
= DEMOD_AWAITING_START_BIT
;
508 if(Demod
.posCount
> 100) {
509 Demod
.state
= DEMOD_UNSYNCD
;
515 case DEMOD_AWAITING_START_BIT
:
516 MAKE_SOFT_DECISION();
518 if(Demod
.posCount
> 10) {
519 Demod
.state
= DEMOD_UNSYNCD
;
526 Demod
.state
= DEMOD_RECEIVING_DATA
;
530 case DEMOD_RECEIVING_DATA
:
531 MAKE_SOFT_DECISION();
532 if(Demod
.posCount
== 0) {
538 if(Demod
.thisBit
> 0) {
539 Demod
.metric
+= Demod
.thisBit
;
541 Demod
.metric
-= Demod
.thisBit
;
545 Demod
.shiftReg
>>= 1;
546 if(Demod
.thisBit
> 0) {
547 Demod
.shiftReg
|= 0x200;
551 if(Demod
.bitCount
== 10) {
552 WORD s
= Demod
.shiftReg
;
553 if((s
& 0x200) && !(s
& 0x001)) {
555 Demod
.output
[Demod
.len
] = b
;
557 Demod
.state
= DEMOD_AWAITING_START_BIT
;
558 } else if(s
== 0x000) {
561 Demod
.state
= DEMOD_UNSYNCD
;
563 Demod
.state
= DEMOD_UNSYNCD
;
571 Demod
.state
= DEMOD_UNSYNCD
;
578 static void GetSamplesFor14443Demod(BOOL weTx
, int n
, BOOL quiet
)
581 BOOL gotFrame
= FALSE
;
583 //# define DMA_BUFFER_SIZE 8
593 // Clear out the state of the "UART" that receives from the tag.
594 memset(BigBuf
, 0x44, 400);
595 Demod
.output
= (BYTE
*)BigBuf
;
597 Demod
.state
= DEMOD_UNSYNCD
;
599 // And the UART that receives from the reader
600 Uart
.output
= (((BYTE
*)BigBuf
) + 1024);
601 Uart
.byteCntMax
= 100;
602 Uart
.state
= STATE_UNSYNCD
;
604 // Setup for the DMA.
605 dmaBuf
= (SBYTE
*)(BigBuf
+ 32);
607 lastRxCounter
= DMA_BUFFER_SIZE
;
608 FpgaSetupSscDma((BYTE
*)dmaBuf
, DMA_BUFFER_SIZE
);
610 // And put the FPGA in the appropriate mode
612 FPGA_MAJOR_MODE_HF_READER_RX_XCORR
| FPGA_HF_READER_RX_XCORR_848_KHZ
|
613 (weTx
? 0 : FPGA_HF_READER_RX_XCORR_SNOOP
));
616 int behindBy
= lastRxCounter
- PDC_RX_COUNTER(SSC_BASE
);
617 if(behindBy
> max
) max
= behindBy
;
620 while(((lastRxCounter
-PDC_RX_COUNTER(SSC_BASE
)) & (DMA_BUFFER_SIZE
-1))
626 if(upTo
- dmaBuf
> DMA_BUFFER_SIZE
) {
627 upTo
-= DMA_BUFFER_SIZE
;
628 PDC_RX_NEXT_POINTER(SSC_BASE
) = (DWORD
)upTo
;
629 PDC_RX_NEXT_COUNTER(SSC_BASE
) = DMA_BUFFER_SIZE
;
632 if(lastRxCounter
<= 0) {
633 lastRxCounter
+= DMA_BUFFER_SIZE
;
638 Handle14443UartBit(1);
639 Handle14443UartBit(1);
641 if(Handle14443SamplesDemod(ci
, cq
)) {
651 PDC_CONTROL(SSC_BASE
) = PDC_RX_DISABLE
;
652 if (!quiet
) DbpIntegers(max
, gotFrame
, Demod
.len
);
655 //-----------------------------------------------------------------------------
656 // Read the tag's response. We just receive a stream of slightly-processed
657 // samples from the FPGA, which we will later do some signal processing on,
659 //-----------------------------------------------------------------------------
660 /*static void GetSamplesFor14443(BOOL weTx, int n)
662 BYTE *dest = (BYTE *)BigBuf;
666 FPGA_MAJOR_MODE_HF_READER_RX_XCORR | FPGA_HF_READER_RX_XCORR_848_KHZ |
667 (weTx ? 0 : FPGA_HF_READER_RX_XCORR_SNOOP));
671 if(SSC_STATUS & (SSC_STATUS_TX_READY)) {
672 SSC_TRANSMIT_HOLDING = 0x43;
674 if(SSC_STATUS & (SSC_STATUS_RX_READY)) {
676 b = (SBYTE)SSC_RECEIVE_HOLDING;
687 //-----------------------------------------------------------------------------
688 // Transmit the command (to the tag) that was placed in ToSend[].
689 //-----------------------------------------------------------------------------
690 static void TransmitFor14443(void)
696 while(SSC_STATUS
& (SSC_STATUS_TX_READY
)) {
697 SSC_TRANSMIT_HOLDING
= 0xff;
701 FPGA_MAJOR_MODE_HF_READER_TX
| FPGA_HF_READER_TX_SHALLOW_MOD
);
703 for(c
= 0; c
< 10;) {
704 if(SSC_STATUS
& (SSC_STATUS_TX_READY
)) {
705 SSC_TRANSMIT_HOLDING
= 0xff;
708 if(SSC_STATUS
& (SSC_STATUS_RX_READY
)) {
709 volatile DWORD r
= SSC_RECEIVE_HOLDING
;
717 if(SSC_STATUS
& (SSC_STATUS_TX_READY
)) {
718 SSC_TRANSMIT_HOLDING
= ToSend
[c
];
724 if(SSC_STATUS
& (SSC_STATUS_RX_READY
)) {
725 volatile DWORD r
= SSC_RECEIVE_HOLDING
;
732 //-----------------------------------------------------------------------------
733 // Code a layer 2 command (string of octets, including CRC) into ToSend[],
734 // so that it is ready to transmit to the tag using TransmitFor14443().
735 //-----------------------------------------------------------------------------
736 void CodeIso14443bAsReader(const BYTE
*cmd
, int len
)
743 // Establish initial reference level
744 for(i
= 0; i
< 40; i
++) {
748 for(i
= 0; i
< 10; i
++) {
752 for(i
= 0; i
< len
; i
++) {
760 for(j
= 0; j
< 8; j
++) {
771 for(i
= 0; i
< 10; i
++) {
774 for(i
= 0; i
< 8; i
++) {
778 // And then a little more, to make sure that the last character makes
779 // it out before we switch to rx mode.
780 for(i
= 0; i
< 24; i
++) {
784 // Convert from last character reference to length
788 //-----------------------------------------------------------------------------
789 // Read an ISO 14443 tag. We send it some set of commands, and record the
791 // The command name is misleading, it actually decodes the reponse in HEX
792 // into the output buffer (read the result using hexsamples, not hisamples)
793 //-----------------------------------------------------------------------------
794 void AcquireRawAdcSamplesIso14443(DWORD parameter
)
796 BYTE cmd1
[] = { 0x05, 0x00, 0x08, 0x39, 0x73 };
798 // Make sure that we start from off, since the tags are stateful;
799 // confusing things will happen if we don't reset them between reads.
801 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
804 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
807 // Now give it time to spin up.
809 FPGA_MAJOR_MODE_HF_READER_RX_XCORR
| FPGA_HF_READER_RX_XCORR_848_KHZ
);
812 CodeIso14443bAsReader(cmd1
, sizeof(cmd1
));
815 GetSamplesFor14443Demod(TRUE
, 2000, FALSE
);
819 //-----------------------------------------------------------------------------
820 // Read a SRI512 ISO 14443 tag.
822 // SRI512 tags are just simple memory tags, here we're looking at making a dump
823 // of the contents of the memory. No anticollision algorithm is done, we assume
824 // we have a single tag in the field.
826 // I tried to be systematic and check every answer of the tag, every CRC, etc...
827 //-----------------------------------------------------------------------------
828 void ReadSRI512Iso14443(DWORD parameter
)
832 // Make sure that we start from off, since the tags are stateful;
833 // confusing things will happen if we don't reset them between reads.
835 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
838 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
841 // Now give it time to spin up.
843 FPGA_MAJOR_MODE_HF_READER_RX_XCORR
| FPGA_HF_READER_RX_XCORR_848_KHZ
);
846 // First command: wake up the tag using the INITIATE command
847 BYTE cmd1
[] = { 0x06, 0x00, 0x97, 0x5b};
848 CodeIso14443bAsReader(cmd1
, sizeof(cmd1
));
851 GetSamplesFor14443Demod(TRUE
, 2000,TRUE
);
854 if (Demod
.len
== 0) {
855 DbpString("No response from tag");
858 DbpString("Randomly generated UID from tag (+ 2 byte CRC):");
859 DbpIntegers(Demod
.output
[0], Demod
.output
[1],Demod
.output
[2]);
861 // There is a response, SELECT the uid
862 DbpString("Now SELECT tag:");
863 cmd1
[0] = 0x0E; // 0x0E is SELECT
864 cmd1
[1] = Demod
.output
[0];
865 ComputeCrc14443(CRC_14443_B
, cmd1
, 2, &cmd1
[2], &cmd1
[3]);
866 CodeIso14443bAsReader(cmd1
, sizeof(cmd1
));
869 GetSamplesFor14443Demod(TRUE
, 2000,TRUE
);
871 if (Demod
.len
!= 3) {
872 DbpString("Expected 3 bytes from tag, got:");
873 DbpIntegers(Demod
.len
,0x0,0x0);
876 // Check the CRC of the answer:
877 ComputeCrc14443(CRC_14443_B
, Demod
.output
, 1 , &cmd1
[2], &cmd1
[3]);
878 if(cmd1
[2] != Demod
.output
[1] || cmd1
[3] != Demod
.output
[2]) {
879 DbpString("CRC Error reading select response.");
882 // Check response from the tag: should be the same UID as the command we just sent:
883 if (cmd1
[1] != Demod
.output
[0]) {
884 DbpString("Bad response to SELECT from Tag, aborting:");
885 DbpIntegers(cmd1
[1],Demod
.output
[0],0x0);
888 // Tag is now selected,
889 // loop to read all 16 blocks, address from 0 to 15
890 DbpString("Tag memory dump, block 0 to 15");
895 DbpString("System area block (0xff):");
899 ComputeCrc14443(CRC_14443_B
, cmd1
, 2, &cmd1
[2], &cmd1
[3]);
900 CodeIso14443bAsReader(cmd1
, sizeof(cmd1
));
903 GetSamplesFor14443Demod(TRUE
, 2000,TRUE
);
905 if (Demod
.len
!= 6) { // Check if we got an answer from the tag
906 DbpString("Expected 6 bytes from tag, got less...");
909 // The check the CRC of the answer (use cmd1 as temporary variable):
910 ComputeCrc14443(CRC_14443_B
, Demod
.output
, 4, &cmd1
[2], &cmd1
[3]);
911 if(cmd1
[2] != Demod
.output
[4] || cmd1
[3] != Demod
.output
[5]) {
912 DbpString("CRC Error reading block! - Below: expected, got, 0x0: ");
913 DbpIntegers( (cmd1
[2]<<8)+cmd1
[3], (Demod
.output
[4]<<8)+Demod
.output
[5],0);
914 // Do not return;, let's go on... (we should retry, maybe ?)
916 // Now print out the memory location:
917 DbpString("Address , Contents, CRC");
918 DbpIntegers(i
, (Demod
.output
[0]<<24) + (Demod
.output
[1]<<16) + (Demod
.output
[2]<<8) + Demod
.output
[3], (Demod
.output
[4]<<8)+Demod
.output
[5]);
927 //=============================================================================
928 // Finally, the `sniffer' combines elements from both the reader and
929 // simulated tag, to show both sides of the conversation.
930 //=============================================================================
932 //-----------------------------------------------------------------------------
933 // Record the sequence of commands sent by the reader to the tag, with
934 // triggering so that we start recording at the point that the tag is moved
936 //-----------------------------------------------------------------------------
937 void SnoopIso14443(void)
939 // We won't start recording the frames that we acquire until we trigger;
940 // a good trigger condition to get started is probably when we see a
941 // response from the tag.
942 BOOL triggered
= FALSE
;
944 // The command (reader -> tag) that we're working on receiving.
945 BYTE
*receivedCmd
= (((BYTE
*)BigBuf
) + 1024);
946 // The response (tag -> reader) that we're working on receiving.
947 BYTE
*receivedResponse
= (((BYTE
*)BigBuf
) + 1536);
949 // As we receive stuff, we copy it from receivedCmd or receivedResponse
950 // into trace, along with its length and other annotations.
951 BYTE
*trace
= (BYTE
*)BigBuf
;
954 // The DMA buffer, used to stream samples from the FPGA.
955 //# define DMA_BUFFER_SIZE 256
956 SBYTE
*dmaBuf
= ((SBYTE
*)BigBuf
) + 2048;
962 // Count of samples received so far, so that we can include timing
963 // information in the trace buffer.
966 memset(trace
, 0x44, 1000);
968 // Set up the demodulator for tag -> reader responses.
969 Demod
.output
= receivedResponse
;
971 Demod
.state
= DEMOD_UNSYNCD
;
973 // And the reader -> tag commands
974 memset(&Uart
, 0, sizeof(Uart
));
975 Uart
.output
= receivedCmd
;
976 Uart
.byteCntMax
= 100;
977 Uart
.state
= STATE_UNSYNCD
;
979 // And put the FPGA in the appropriate mode
981 FPGA_MAJOR_MODE_HF_READER_RX_XCORR
| FPGA_HF_READER_RX_XCORR_848_KHZ
|
982 FPGA_HF_READER_RX_XCORR_SNOOP
);
983 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
985 // Setup for the DMA.
988 lastRxCounter
= DMA_BUFFER_SIZE
;
989 FpgaSetupSscDma((BYTE
*)dmaBuf
, DMA_BUFFER_SIZE
);
993 // And now we loop, receiving samples.
995 int behindBy
= (lastRxCounter
- PDC_RX_COUNTER(SSC_BASE
)) &
997 if(behindBy
> maxBehindBy
) {
998 maxBehindBy
= behindBy
;
1000 DbpString("blew circular buffer!");
1004 if(behindBy
< 2) continue;
1010 if(upTo
- dmaBuf
> DMA_BUFFER_SIZE
) {
1011 upTo
-= DMA_BUFFER_SIZE
;
1012 lastRxCounter
+= DMA_BUFFER_SIZE
;
1013 PDC_RX_NEXT_POINTER(SSC_BASE
) = (DWORD
)upTo
;
1014 PDC_RX_NEXT_COUNTER(SSC_BASE
) = DMA_BUFFER_SIZE
;
1019 #define HANDLE_BIT_IF_BODY \
1021 trace[traceLen++] = ((samples >> 0) & 0xff); \
1022 trace[traceLen++] = ((samples >> 8) & 0xff); \
1023 trace[traceLen++] = ((samples >> 16) & 0xff); \
1024 trace[traceLen++] = ((samples >> 24) & 0xff); \
1025 trace[traceLen++] = 0; \
1026 trace[traceLen++] = 0; \
1027 trace[traceLen++] = 0; \
1028 trace[traceLen++] = 0; \
1029 trace[traceLen++] = Uart.byteCnt; \
1030 memcpy(trace+traceLen, receivedCmd, Uart.byteCnt); \
1031 traceLen += Uart.byteCnt; \
1032 if(traceLen > 1000) break; \
1034 /* And ready to receive another command. */ \
1035 memset(&Uart, 0, sizeof(Uart)); \
1036 Uart.output = receivedCmd; \
1037 Uart.byteCntMax = 100; \
1038 Uart.state = STATE_UNSYNCD; \
1039 /* And also reset the demod code, which might have been */ \
1040 /* false-triggered by the commands from the reader. */ \
1041 memset(&Demod, 0, sizeof(Demod)); \
1042 Demod.output = receivedResponse; \
1043 Demod.state = DEMOD_UNSYNCD; \
1045 if(Handle14443UartBit(ci & 1)) {
1048 if(Handle14443UartBit(cq
& 1)) {
1052 if(Handle14443SamplesDemod(ci
, cq
)) {
1053 // timestamp, as a count of samples
1054 trace
[traceLen
++] = ((samples
>> 0) & 0xff);
1055 trace
[traceLen
++] = ((samples
>> 8) & 0xff);
1056 trace
[traceLen
++] = ((samples
>> 16) & 0xff);
1057 trace
[traceLen
++] = 0x80 | ((samples
>> 24) & 0xff);
1058 // correlation metric (~signal strength estimate)
1059 if(Demod
.metricN
!= 0) {
1060 Demod
.metric
/= Demod
.metricN
;
1062 trace
[traceLen
++] = ((Demod
.metric
>> 0) & 0xff);
1063 trace
[traceLen
++] = ((Demod
.metric
>> 8) & 0xff);
1064 trace
[traceLen
++] = ((Demod
.metric
>> 16) & 0xff);
1065 trace
[traceLen
++] = ((Demod
.metric
>> 24) & 0xff);
1067 trace
[traceLen
++] = Demod
.len
;
1068 memcpy(trace
+traceLen
, receivedResponse
, Demod
.len
);
1069 traceLen
+= Demod
.len
;
1070 if(traceLen
> 1000) break;
1076 // And ready to receive another response.
1077 memset(&Demod
, 0, sizeof(Demod
));
1078 Demod
.output
= receivedResponse
;
1079 Demod
.state
= DEMOD_UNSYNCD
;
1082 if(BUTTON_PRESS()) {
1083 DbpString("cancelled");
1088 DbpString("in done pt");
1090 DbpIntegers(maxBehindBy
, Uart
.state
, Uart
.byteCnt
);
1091 DbpIntegers(Uart
.byteCntMax
, traceLen
, 0x23);
1094 PDC_CONTROL(SSC_BASE
) = PDC_RX_DISABLE
;