+ break;
+
+ case STATE_READER_AWAIT_2ND_RISING_EDGE_OF_SOF:
+ DecodeReader->posCount++;
+ if (bit) { // detected rising edge
+ if (DecodeReader->Coding == CODING_1_OUT_OF_256) {
+ if (DecodeReader->posCount < 32) { // rising edge too early (nominally expected at 33)
+ DecodeReader->state = STATE_READER_AWAIT_1ST_FALLING_EDGE_OF_SOF;
+ } else {
+ DecodeReader->posCount = 1;
+ DecodeReader->bitCount = 0;
+ DecodeReader->byteCount = 0;
+ DecodeReader->sum1 = 1;
+ DecodeReader->state = STATE_READER_RECEIVE_DATA_1_OUT_OF_256;
+ LED_B_ON();
+ }
+ } else { // CODING_1_OUT_OF_4
+ if (DecodeReader->posCount < 24) { // rising edge too early (nominally expected at 25)
+ DecodeReader->state = STATE_READER_AWAIT_1ST_FALLING_EDGE_OF_SOF;
+ } else {
+ DecodeReader->posCount = 1;
+ DecodeReader->state = STATE_READER_AWAIT_END_OF_SOF_1_OUT_OF_4;
+ }
+ }
+ } else {
+ if (DecodeReader->Coding == CODING_1_OUT_OF_256) {
+ if (DecodeReader->posCount > 34) { // signal stayed low for too long
+ DecodeReaderReset(DecodeReader);
+ } else {
+ // do nothing, keep waiting
+ }
+ } else { // CODING_1_OUT_OF_4
+ if (DecodeReader->posCount > 26) { // signal stayed low for too long
+ DecodeReaderReset(DecodeReader);
+ } else {
+ // do nothing, keep waiting
+ }
+ }
+ }
+ break;
+
+ case STATE_READER_AWAIT_END_OF_SOF_1_OUT_OF_4:
+ DecodeReader->posCount++;
+ if (bit) {
+ if (DecodeReader->posCount == 9) {
+ DecodeReader->posCount = 1;
+ DecodeReader->bitCount = 0;
+ DecodeReader->byteCount = 0;
+ DecodeReader->sum1 = 1;
+ DecodeReader->state = STATE_READER_RECEIVE_DATA_1_OUT_OF_4;
+ LED_B_ON();
+ } else {
+ // do nothing, keep waiting
+ }
+ } else { // unexpected falling edge
+ DecodeReaderReset(DecodeReader);
+ }
+ break;
+
+ case STATE_READER_RECEIVE_DATA_1_OUT_OF_4:
+ 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 a 2bit position
+ DecodeReader->shiftReg >>= 2;
+ DecodeReader->shiftReg |= (DecodeReader->bitCount << 6);
+ }
+ if (DecodeReader->bitCount == 15) { // 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 = 0;
+ DecodeReader->shiftReg = 0;
+ } else {
+ DecodeReader->bitCount++;
+ }
+ }
+ 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 len) or someone presses the pushbutton on the board (returns -1).
+//
+// Assume that we're called with the SSC (to the FPGA) and ADC path set
+// correctly.
+//-----------------------------------------------------------------------------
+
+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[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 dma_start_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;