-
-//-----------------------------------------------------------------------------
-// Record the sequence of commands sent by the reader to the tag, with
-// triggering so that we start recording at the point that the tag is moved
-// near the reader.
-//-----------------------------------------------------------------------------
-void RAMFUNC SnoopIClass(void) {
-
-       // We won't start recording the frames that we acquire until we trigger;
-       // a good trigger condition to get started is probably when we see a
-       // response from the tag.
-       //int triggered = false; // false to wait first for card
-
-       // The command (reader -> tag) that we're receiving.
-       // The length of a received command will in most cases be no more than 18 bytes.
-       // So 32 should be enough!
-       #define ICLASS_BUFFER_SIZE 32
-       uint8_t readerToTagCmd[ICLASS_BUFFER_SIZE];
-       // The response (tag -> reader) that we're receiving.
-       uint8_t tagToReaderResponse[ICLASS_BUFFER_SIZE];
-
-       FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
-
-       // free all BigBuf memory
-       BigBuf_free();
-       // The DMA buffer, used to stream samples from the FPGA
-       uint8_t *dmaBuf = BigBuf_malloc(DMA_BUFFER_SIZE);
-
-       set_tracing(true);
-       clear_trace();
-       iso14a_set_trigger(false);
-
-       int lastRxCounter;
-       uint8_t *upTo;
-       int smpl;
-       int maxBehindBy = 0;
-
-       // Count of samples received so far, so that we can include timing
-       // information in the trace buffer.
-       int samples = 0;
-       rsamples = 0;
-
-       // Set up the demodulator for tag -> reader responses.
-       Demod.output = tagToReaderResponse;
-       Demod.len = 0;
-       Demod.state = DEMOD_UNSYNCD;
-
-       // Setup for the DMA.
-       FpgaSetupSsc(FPGA_MAJOR_MODE_HF_ISO14443A);
-       upTo = dmaBuf;
-       lastRxCounter = DMA_BUFFER_SIZE;
-       FpgaSetupSscDma((uint8_t *)dmaBuf, DMA_BUFFER_SIZE);
-
-       // And the reader -> tag commands
-       memset(&Uart, 0, sizeof(Uart));
-       Uart.output = readerToTagCmd;
-       Uart.byteCntMax = 32; // was 100 (greg)////////////////////////////////////////////////////////////////////////
-       Uart.state = STATE_UNSYNCD;
-
-       // And put the FPGA in the appropriate mode
-       // Signal field is off with the appropriate LED
-       LED_D_OFF();
-       FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_SNIFFER);
-       SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
-
-       uint32_t time_0 = GetCountSspClk();
-       uint32_t time_start = 0;
-       uint32_t time_stop  = 0;
-
-       int div = 0;
-       //int div2 = 0;
-       int decbyte = 0;
-       int decbyter = 0;
-
-       // And now we loop, receiving samples.
-       for (;;) {
-               LED_A_ON();
-               WDT_HIT();
-               int behindBy = (lastRxCounter - AT91C_BASE_PDC_SSC->PDC_RCR) & (DMA_BUFFER_SIZE-1);
-               if (behindBy > maxBehindBy) {
-                       maxBehindBy = behindBy;
-                       if (behindBy > (9 * DMA_BUFFER_SIZE / 10)) {
-                               Dbprintf("blew circular buffer! behindBy=0x%x", behindBy);
-                               goto done;
-                       }
-               }
-               if (behindBy < 1) continue;
-
-               LED_A_OFF();
-               smpl = upTo[0];
-               upTo++;
-               lastRxCounter -= 1;
-               if (upTo - dmaBuf > DMA_BUFFER_SIZE) {
-                       upTo -= DMA_BUFFER_SIZE;
-                       lastRxCounter += DMA_BUFFER_SIZE;
-                       AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) upTo;
-                       AT91C_BASE_PDC_SSC->PDC_RNCR = DMA_BUFFER_SIZE;
-               }
-
-               //samples += 4;
-               samples += 1;
-
-               if (smpl & 0xF) {
-                       decbyte ^= (1 << (3 - div));
-               }
-
-               // FOR READER SIDE COMMUMICATION...
-
-               decbyter <<= 2;
-               decbyter ^= (smpl & 0x30);
-
-               div++;
-
-               if ((div + 1) % 2 == 0) {
-                       smpl = decbyter;
-                       if (OutOfNDecoding((smpl & 0xF0) >> 4)) {
-                               rsamples = samples - Uart.samples;
-                               time_stop = (GetCountSspClk()-time_0) << 4;
-                               LED_C_ON();
-
-                               //if (!LogTrace(Uart.output, Uart.byteCnt, rsamples, Uart.parityBits,true)) break;
-                               //if (!LogTrace(NULL, 0, Uart.endTime*16 - DELAY_READER_AIR2ARM_AS_SNIFFER, 0, true)) break;
-                               uint8_t parity[MAX_PARITY_SIZE];
-                               GetParity(Uart.output, Uart.byteCnt, parity);
-                               LogTrace(Uart.output, Uart.byteCnt, time_start, time_stop, parity, true);
-
-                               /* And ready to receive another command. */
-                               Uart.state = STATE_UNSYNCD;
-                               /* And also reset the demod code, which might have been */
-                               /* false-triggered by the commands from the reader. */
-                               Demod.state = DEMOD_UNSYNCD;
-                               LED_B_OFF();
-                               Uart.byteCnt = 0;
-                       } else {
-                               time_start = (GetCountSspClk()-time_0) << 4;
-                       }
-                       decbyter = 0;
-               }
-
-               if (div > 3) {
-                       smpl = decbyte;
-                       if (ManchesterDecoding(smpl & 0x0F)) {
-                               time_stop = (GetCountSspClk()-time_0) << 4;
-
-                               rsamples = samples - Demod.samples;
-                               LED_B_ON();
-
-                               uint8_t parity[MAX_PARITY_SIZE];
-                               GetParity(Demod.output, Demod.len, parity);
-                               LogTrace(Demod.output, Demod.len, time_start, time_stop, parity, false);
-
-                               // And ready to receive another response.
-                               memset(&Demod, 0, sizeof(Demod));
-                               Demod.output = tagToReaderResponse;
-                               Demod.state = DEMOD_UNSYNCD;
-                               LED_C_OFF();
-                       } else {
-                               time_start = (GetCountSspClk()-time_0) << 4;
-                       }
-
-                       div = 0;
-                       decbyte = 0x00;
-               }
-
-               if (BUTTON_PRESS()) {
-                       DbpString("cancelled_a");
-                       goto done;
-               }
-       }
-
-       DbpString("COMMAND FINISHED");
-
-       Dbprintf("%x %x %x", maxBehindBy, Uart.state, Uart.byteCnt);
-       Dbprintf("%x %x %x", Uart.byteCntMax, BigBuf_get_traceLen(), (int)Uart.output[0]);
-
-done:
-       AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTDIS;
-       Dbprintf("%x %x %x", maxBehindBy, Uart.state, Uart.byteCnt);
-       Dbprintf("%x %x %x", Uart.byteCntMax, BigBuf_get_traceLen(), (int)Uart.output[0]);
-       LEDsoff();