-//-----------------------------------------------------------------------------
-// Wait a certain time for tag response
-// If a response is captured return true
-// If it takes too long return false
-//-----------------------------------------------------------------------------
-static int GetIClassAnswer(uint8_t *receivedResponse, int maxLen, int *samples, int *elapsed) {
- //uint8_t *buffer
- // buffer needs to be 512 bytes
- int c;
-
- // Set FPGA mode to "reader listen mode", no modulation (listen
- // only, since we are receiving, not transmitting).
- FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_LISTEN);
-
- // Now get the answer from the card
- Demod.output = receivedResponse;
- Demod.len = 0;
- Demod.state = DEMOD_UNSYNCD;
-
- uint8_t b;
- if (elapsed) *elapsed = 0;
-
- bool skip = false;
-
- c = 0;
- for (;;) {
- WDT_HIT();
-
- if (BUTTON_PRESS()) return false;
-
- if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
- AT91C_BASE_SSC->SSC_THR = 0x00; // To make use of exact timing of next command from reader!!
- if (elapsed) (*elapsed)++;
- }
- if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
- if (c < timeout) {
- c++;
- } else {
- return false;
- }
- b = (uint8_t)AT91C_BASE_SSC->SSC_RHR;
- skip = !skip;
- if (skip) continue;
-
- if (ManchesterDecoding(b & 0x0f)) {
- *samples = c << 3;
- return true;
- }
- }
- }
-}
-
-static int ReaderReceiveIClass(uint8_t *receivedAnswer) {
- int samples = 0;
- if (!GetIClassAnswer(receivedAnswer, 160, &samples, 0)) {
- return false;
- }
- rsamples += samples;
- uint8_t parity[MAX_PARITY_SIZE];
- GetParity(receivedAnswer, Demod.len, parity);
- LogTrace(receivedAnswer, Demod.len, rsamples, rsamples, parity, false);
- if (samples == 0) return false;
- return Demod.len;
-}
-
-static void setupIclassReader() {
- FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
- // Reset trace buffer
- set_tracing(true);
- clear_trace();
-
- // Setup SSC
- FpgaSetupSsc(FPGA_MAJOR_MODE_HF_ISO14443A);
- // Start from off (no field generated)
- // Signal field is off with the appropriate LED
- LED_D_OFF();
- FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
- SpinDelay(200);
-
- SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
-
- // Now give it time to spin up.
- // Signal field is on with the appropriate LED
- FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_ISO14443A | FPGA_HF_ISO14443A_READER_MOD);
- SpinDelay(200);
- LED_A_ON();
-
-}
-
-static bool sendCmdGetResponseWithRetries(uint8_t* command, size_t cmdsize, uint8_t* resp, uint8_t expected_size, uint8_t retries) {