+ break;
+
+ case STATE_READER_RECEIVE_DATA_1_OUT_OF_256:
+ DecodeReader->posCount++;
+ if (DecodeReader->posCount == 1) {
+ DecodeReader->sum1 = bit;
+ } else if (DecodeReader->posCount <= 4) {
+ DecodeReader->sum1 += bit;
+ } else if (DecodeReader->posCount == 5) {
+ DecodeReader->sum2 = bit;
+ } else {
+ DecodeReader->sum2 += bit;
+ }
+ if (DecodeReader->posCount == 8) {
+ DecodeReader->posCount = 0;
+ int corr10 = DecodeReader->sum1 - DecodeReader->sum2;
+ int corr01 = DecodeReader->sum2 - DecodeReader->sum1;
+ int corr11 = (DecodeReader->sum1 + DecodeReader->sum2) / 2;
+ if (corr01 > corr11 && corr01 > corr10) { // EOF
+ LED_B_OFF(); // Finished receiving
+ DecodeReaderReset(DecodeReader);
+ if (DecodeReader->byteCount != 0) {
+ return true;
+ }
+ }
+ if (corr10 > corr11) { // detected the bit position
+ DecodeReader->shiftReg = DecodeReader->bitCount;
+ }
+ if (DecodeReader->bitCount == 255) { // we have a full byte
+ DecodeReader->output[DecodeReader->byteCount++] = DecodeReader->shiftReg;
+ if (DecodeReader->byteCount > DecodeReader->byteCountMax) {
+ // buffer overflow, give up
+ LED_B_OFF();
+ DecodeReaderReset(DecodeReader);
+ }
+ }
+ DecodeReader->bitCount++;
+ }
+ 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);