+ break;
+
+ default:
+ LED_B_OFF();
+ DecodeReaderReset(DecodeReader);
+ break;
+ }
+
+ return false;
+}
+
+
+//-----------------------------------------------------------------------------
+// Receive a command (from the reader to us, where we are the simulated tag),
+// and store it in the given buffer, up to the given maximum length. Keeps
+// spinning, waiting for a well-framed command, until either we get one
+// (returns true) or someone presses the pushbutton on the board (false).
+//
+// Assume that we're called with the SSC (to the FPGA) and ADC path set
+// correctly.
+//-----------------------------------------------------------------------------
+
+static int GetIso15693CommandFromReader(uint8_t *received, size_t max_len, uint32_t *eof_time)
+{
+ int samples = 0;
+ bool gotFrame = false;
+ uint8_t b;
+
+ uint8_t *dmaBuf = BigBuf_malloc(ISO15693_DMA_BUFFER_SIZE);
+
+ // the decoder data structure
+ DecodeReader_t DecodeReader = {0};
+ DecodeReaderInit(&DecodeReader, received, max_len);
+
+ // wait for last transfer to complete
+ while (!(AT91C_BASE_SSC->SSC_SR & AT91C_SSC_TXEMPTY));
+
+ LED_D_OFF();
+ FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SIMULATOR | FPGA_HF_SIMULATOR_NO_MODULATION);
+
+ // clear receive register and wait for next transfer
+ uint32_t temp = AT91C_BASE_SSC->SSC_RHR;
+ (void) temp;
+ while (!(AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY)) ;
+
+ uint32_t bit_time = GetCountSspClk() & 0xfffffff8;
+
+ // Setup and start DMA.
+ FpgaSetupSscDma(dmaBuf, ISO15693_DMA_BUFFER_SIZE);
+ uint8_t *upTo = dmaBuf;
+
+ for(;;) {
+ uint16_t behindBy = ((uint8_t*)AT91C_BASE_PDC_SSC->PDC_RPR - upTo) & (ISO15693_DMA_BUFFER_SIZE-1);
+
+ if (behindBy == 0) continue;
+
+ b = *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", behindBy);
+ 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
+ }
+
+ for (int i = 7; i >= 0; i--) {
+ if (Handle15693SampleFromReader((b >> i) & 0x01, &DecodeReader)) {
+ *eof_time = bit_time + samples - DELAY_READER_TO_ARM_SIM; // end of EOF
+ gotFrame = true;