+ samples++;
+ if (samples == 1) {
+ // DMA has transferred the very first data
+ dma_start_time = GetCountSspClk() & 0xfffffff0;
+ }
+
+ 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)) {
+ // FpgaDisableTracing();
+ 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;
+ }
+ }
+ }
+
+ if (!TagIsActive) { // no need to try decoding reader data if the tag is sending
+ if (Handle15693SampleFromReader(snoopdata & 0x02, &DecodeReader)) {
+ // FpgaDisableSscDma();
+ uint32_t eof_time = dma_start_time + samples*16 + 8 - DELAY_READER_TO_ARM_SNOOP; // end of EOF
+ if (DecodeReader.byteCount > 0) {
+ uint32_t sof_time = eof_time
+ - DecodeReader.byteCount * (DecodeReader.Coding==CODING_1_OUT_OF_4?128*16:2048*16) // time for byte transfers
+ - 32*16 // time for SOF transfer
+ - 16*16; // time for EOF transfer
+ LogTrace_ISO15693(DecodeReader.output, DecodeReader.byteCount, sof_time*4, eof_time*4, 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);
+ ReaderIsActive = false;
+ ExpectTagAnswer = true;
+ // upTo = dmaBuf;
+ // samples = 0;
+ // FpgaSetupSscDma((uint8_t*) dmaBuf, ISO15693_DMA_BUFFER_SIZE);
+ // continue;
+ } else if (Handle15693SampleFromReader(snoopdata & 0x01, &DecodeReader)) {
+ // FpgaDisableSscDma();
+ uint32_t eof_time = dma_start_time + samples*16 + 16 - DELAY_READER_TO_ARM_SNOOP; // end of EOF
+ if (DecodeReader.byteCount > 0) {
+ uint32_t sof_time = eof_time
+ - DecodeReader.byteCount * (DecodeReader.Coding==CODING_1_OUT_OF_4?128*16:2048*16) // time for byte transfers
+ - 32*16 // time for SOF transfer
+ - 16*16; // time for EOF transfer
+ LogTrace_ISO15693(DecodeReader.output, DecodeReader.byteCount, sof_time*4, eof_time*4, 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);
+ ReaderIsActive = false;
+ ExpectTagAnswer = true;
+ // upTo = dmaBuf;
+ // samples = 0;
+ // FpgaSetupSscDma((uint8_t*) dmaBuf, ISO15693_DMA_BUFFER_SIZE);
+ // continue;
+ } else {
+ ReaderIsActive = (DecodeReader.state >= STATE_READER_RECEIVE_DATA_1_OUT_OF_4);
+ }
+ }
+
+ 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();
+ uint32_t eof_time = dma_start_time + samples*16 - DELAY_TAG_TO_ARM_SNOOP; // end of EOF
+ if (DecodeTag.lastBit == SOF_PART2) {
+ eof_time -= 8*16; // needed 8 additional samples to confirm single SOF (iCLASS)
+ }
+ uint32_t sof_time = eof_time
+ - DecodeTag.len * 8 * 8 * 16 // time for byte transfers
+ - 32 * 16 // time for SOF transfer
+ - (DecodeTag.lastBit != SOF_PART2?32*16:0); // time for EOF transfer
+ LogTrace_ISO15693(DecodeTag.output, DecodeTag.len, sof_time*4, eof_time*4, NULL, false);
+ // And ready to receive another response.
+ DecodeTagReset(&DecodeTag);
+ DecodeReaderReset(&DecodeReader);
+ ExpectTagAnswer = false;
+ TagIsActive = false;
+ // upTo = dmaBuf;
+ // samples = 0;
+ // FpgaSetupSscDma((uint8_t*) dmaBuf, ISO15693_DMA_BUFFER_SIZE);
+ // continue;
+ } else {
+ TagIsActive = (DecodeTag.state >= STATE_TAG_RECEIVING_DATA);
+ }