- for (c = 0; c < *wait;) {
- if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
- AT91C_BASE_SSC->SSC_THR = 0x00; // For exact timing!
- c++;
- }
- if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
- volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR;
- (void)r;
- }
- WDT_HIT();
- }
- }
-
- uint8_t sendbyte;
- bool firstpart = true;
- c = 0;
- for (;;) {
- if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
-
- // DOUBLE THE SAMPLES!
- if (firstpart) {
- sendbyte = (cmd[c] & 0xf0) | (cmd[c] >> 4);
- } else {
- sendbyte = (cmd[c] & 0x0f) | (cmd[c] << 4);
- c++;
- }
- if (sendbyte == 0xff) {
- sendbyte = 0xfe;
- }
- AT91C_BASE_SSC->SSC_THR = sendbyte;
- firstpart = !firstpart;
-
- if (c >= len) {
- break;
- }
- }
- if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
- volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR;
- (void)r;
- }
- WDT_HIT();
- }
- if (samples && wait) *samples = (c + *wait) << 3;
-}
-
-
-//-----------------------------------------------------------------------------
-// Prepare iClass reader command to send to FPGA
-//-----------------------------------------------------------------------------
-void CodeIClassCommand(const uint8_t *cmd, int len) {
- int i, j, k;
-
- ToSendReset();
-
- // Start of Communication: 1 out of 4
- ToSend[++ToSendMax] = 0xf0;
- ToSend[++ToSendMax] = 0x00;
- ToSend[++ToSendMax] = 0x0f;
- ToSend[++ToSendMax] = 0x00;
-
- // Modulate the bytes
- for (i = 0; i < len; i++) {
- uint8_t b = cmd[i];
- for (j = 0; j < 4; j++) {
- for (k = 0; k < 4; k++) {
- if (k == (b & 3)) {
- ToSend[++ToSendMax] = 0xf0;
- } else {
- ToSend[++ToSendMax] = 0x00;
- }
- }
- b >>= 2;
- }
- }
-
- // End of Communication
- ToSend[++ToSendMax] = 0x00;
- ToSend[++ToSendMax] = 0x00;
- ToSend[++ToSendMax] = 0xf0;
- ToSend[++ToSendMax] = 0x00;
-
- // Convert from last character reference to length
- ToSendMax++;
-}
-
-static void ReaderTransmitIClass(uint8_t *frame, int len) {
- int wait = 0;
- int samples = 0;
-
- // This is tied to other size changes
- CodeIClassCommand(frame, len);
-
- // Select the card
- TransmitIClassCommand(ToSend, ToSendMax, &samples, &wait);
- if (trigger)
- LED_A_ON();
-
- // Store reader command in buffer
- uint8_t par[MAX_PARITY_SIZE];
- GetParity(frame, len, par);
- LogTrace(frame, len, rsamples, rsamples, par, true);
-}
-
-//-----------------------------------------------------------------------------
-// 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;
- }
- }
- }
-}