- for(int c = 0; c < 14000;) {
- if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
- uint16_t iq = AT91C_BASE_SSC->SSC_RHR;
- // The samples are correlations against I and Q versions of the
- // tone that the tag AM-modulates. We just want power,
- // so abs(I) + abs(Q) is close to what we want.
- int8_t i = (int8_t)(iq >> 8);
- int8_t q = (int8_t)(iq & 0xff);
- uint8_t r = AMPLITUDE(i, q);
- dest[c++] = r;
+ // And now we loop, receiving samples.
+ for(;;) {
+ uint16_t behindBy = ((uint16_t*)AT91C_BASE_PDC_SSC->PDC_RPR - upTo) & (ISO15693_DMA_BUFFER_SIZE-1);
+
+ if (behindBy == 0) continue;
+
+ uint16_t snoopdata = *upTo++;
+
+ if(upTo >= dmaBuf + ISO15693_DMA_BUFFER_SIZE) { // we have read all of the DMA buffer content.
+ upTo = dmaBuf; // start reading the circular buffer from the beginning
+ if(behindBy > (9*ISO15693_DMA_BUFFER_SIZE/10)) {
+ Dbprintf("About to blow circular buffer - aborted! behindBy=%d, samples=%d", behindBy, samples);
+ break;
+ }
+ if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_ENDRX)) { // DMA Counter Register had reached 0, already rotated.
+ AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) dmaBuf; // refresh the DMA Next Buffer and
+ AT91C_BASE_PDC_SSC->PDC_RNCR = ISO15693_DMA_BUFFER_SIZE; // DMA Next Counter registers
+ WDT_HIT();
+ if(BUTTON_PRESS()) {
+ DbpString("Snoop stopped.");
+ break;
+ }
+ }
+ }
+ samples++;
+
+ if (!TagIsActive) { // no need to try decoding reader data if the tag is sending
+ if (Handle15693SampleFromReader(snoopdata & 0x02, &DecodeReader)) {
+ FpgaDisableSscDma();
+ ExpectTagAnswer = true;
+ LogTrace(DecodeReader.output, DecodeReader.byteCount, samples, samples, NULL, true);
+ /* And ready to receive another command. */
+ DecodeReaderReset(&DecodeReader);
+ /* And also reset the demod code, which might have been */
+ /* false-triggered by the commands from the reader. */
+ DecodeTagReset(&DecodeTag);
+ upTo = dmaBuf;
+ FpgaSetupSscDma((uint8_t*) dmaBuf, ISO15693_DMA_BUFFER_SIZE);
+ }
+ if (Handle15693SampleFromReader(snoopdata & 0x01, &DecodeReader)) {
+ FpgaDisableSscDma();
+ ExpectTagAnswer = true;
+ LogTrace(DecodeReader.output, DecodeReader.byteCount, samples, samples, NULL, true);
+ /* And ready to receive another command. */
+ DecodeReaderReset(&DecodeReader);
+ /* And also reset the demod code, which might have been */
+ /* false-triggered by the commands from the reader. */
+ DecodeTagReset(&DecodeTag);
+ upTo = dmaBuf;
+ FpgaSetupSscDma((uint8_t*) dmaBuf, ISO15693_DMA_BUFFER_SIZE);
+ }
+ ReaderIsActive = (DecodeReader.state >= STATE_READER_AWAIT_2ND_RISING_EDGE_OF_SOF);
+ }
+
+ if (!ReaderIsActive && ExpectTagAnswer) { // no need to try decoding tag data if the reader is currently sending or no answer expected yet
+ if (Handle15693SamplesFromTag(snoopdata >> 2, &DecodeTag)) {
+ FpgaDisableSscDma();
+ //Use samples as a time measurement
+ LogTrace(DecodeTag.output, DecodeTag.len, samples, samples, NULL, false);
+ // And ready to receive another response.
+ DecodeTagReset(&DecodeTag);
+ DecodeReaderReset(&DecodeReader);
+ ExpectTagAnswer = false;
+ upTo = dmaBuf;
+ FpgaSetupSscDma((uint8_t*) dmaBuf, ISO15693_DMA_BUFFER_SIZE);
+ }
+ TagIsActive = (DecodeTag.state >= STATE_TAG_RECEIVING_DATA);