]> cvs.zerfleddert.de Git - proxmark3-svn/blobdiff - armsrc/iso14443.c
Added enios dirthreshold command, patch from http://www.proxmark.org/forum/viewtopic...
[proxmark3-svn] / armsrc / iso14443.c
index fab7336bc1bb73484bc14dbdc4c2eb816d3fb392..5e8eddd24dd5d9fa3314f2eb513606a45ca40c66 100644 (file)
-//-----------------------------------------------------------------------------\r
-// Routines to support ISO 14443. This includes both the reader software and\r
-// the `fake tag' modes. At the moment only the Type B modulation is\r
-// supported.\r
-// Jonathan Westhues, split Nov 2006\r
-//-----------------------------------------------------------------------------\r
-#include <proxmark3.h>\r
-#include "apps.h"\r
-#include "../common/iso14443_crc.c"\r
-\r
-\r
-//static void GetSamplesFor14443(BOOL weTx, int n);\r
-\r
-#define DMA_BUFFER_SIZE 256\r
-\r
-//=============================================================================\r
-// An ISO 14443 Type B tag. We listen for commands from the reader, using\r
-// a UART kind of thing that's implemented in software. When we get a\r
-// frame (i.e., a group of bytes between SOF and EOF), we check the CRC.\r
-// If it's good, then we can do something appropriate with it, and send\r
-// a response.\r
-//=============================================================================\r
-\r
-//-----------------------------------------------------------------------------\r
-// Code up a string of octets at layer 2 (including CRC, we don't generate\r
-// that here) so that they can be transmitted to the reader. Doesn't transmit\r
-// them yet, just leaves them ready to send in ToSend[].\r
-//-----------------------------------------------------------------------------\r
-static void CodeIso14443bAsTag(const BYTE *cmd, int len)\r
-{\r
-    int i;\r
-\r
-    ToSendReset();\r
-\r
-    // Transmit a burst of ones, as the initial thing that lets the\r
-    // reader get phase sync. This (TR1) must be > 80/fs, per spec,\r
-    // but tag that I've tried (a Paypass) exceeds that by a fair bit,\r
-    // so I will too.\r
-    for(i = 0; i < 20; i++) {\r
-        ToSendStuffBit(1);\r
-        ToSendStuffBit(1);\r
-        ToSendStuffBit(1);\r
-        ToSendStuffBit(1);\r
-    }\r
-\r
-    // Send SOF.\r
-    for(i = 0; i < 10; i++) {\r
-        ToSendStuffBit(0);\r
-        ToSendStuffBit(0);\r
-        ToSendStuffBit(0);\r
-        ToSendStuffBit(0);\r
-    }\r
-    for(i = 0; i < 2; i++) {\r
-        ToSendStuffBit(1);\r
-        ToSendStuffBit(1);\r
-        ToSendStuffBit(1);\r
-        ToSendStuffBit(1);\r
-    }\r
-\r
-    for(i = 0; i < len; i++) {\r
-        int j;\r
-        BYTE b = cmd[i];\r
-\r
-        // Start bit\r
-        ToSendStuffBit(0);\r
-        ToSendStuffBit(0);\r
-        ToSendStuffBit(0);\r
-        ToSendStuffBit(0);\r
-\r
-        // Data bits\r
-        for(j = 0; j < 8; j++) {\r
-            if(b & 1) {\r
-                ToSendStuffBit(1);\r
-                ToSendStuffBit(1);\r
-                ToSendStuffBit(1);\r
-                ToSendStuffBit(1);\r
-            } else {\r
-                ToSendStuffBit(0);\r
-                ToSendStuffBit(0);\r
-                ToSendStuffBit(0);\r
-                ToSendStuffBit(0);\r
-            }\r
-            b >>= 1;\r
-        }\r
-\r
-        // Stop bit\r
-        ToSendStuffBit(1);\r
-        ToSendStuffBit(1);\r
-        ToSendStuffBit(1);\r
-        ToSendStuffBit(1);\r
-    }\r
-\r
-    // Send SOF.\r
-    for(i = 0; i < 10; i++) {\r
-        ToSendStuffBit(0);\r
-        ToSendStuffBit(0);\r
-        ToSendStuffBit(0);\r
-        ToSendStuffBit(0);\r
-    }\r
-    for(i = 0; i < 10; i++) {\r
-        ToSendStuffBit(1);\r
-        ToSendStuffBit(1);\r
-        ToSendStuffBit(1);\r
-        ToSendStuffBit(1);\r
-    }\r
-\r
-    // Convert from last byte pos to length\r
-    ToSendMax++;\r
-\r
-    // Add a few more for slop\r
-    ToSendMax += 2;\r
-}\r
-\r
-//-----------------------------------------------------------------------------\r
-// The software UART that receives commands from the reader, and its state\r
-// variables.\r
-//-----------------------------------------------------------------------------\r
-static struct {\r
-    enum {\r
-        STATE_UNSYNCD,\r
-        STATE_GOT_FALLING_EDGE_OF_SOF,\r
-        STATE_AWAITING_START_BIT,\r
-        STATE_RECEIVING_DATA,\r
-        STATE_ERROR_WAIT\r
-    }       state;\r
-    WORD    shiftReg;\r
-    int     bitCnt;\r
-    int     byteCnt;\r
-    int     byteCntMax;\r
-    int     posCnt;\r
-    BYTE   *output;\r
-} Uart;\r
-\r
-/* Receive & handle a bit coming from the reader.\r
- *\r
- * LED handling:\r
- * LED A -> ON once we have received the SOF and are expecting the rest.\r
- * LED A -> OFF once we have received EOF or are in error state or unsynced\r
- *\r
- * Returns: true if we received a EOF\r
- *          false if we are still waiting for some more\r
- */\r
-static BOOL Handle14443UartBit(int bit)\r
-{\r
-    switch(Uart.state) {\r
-        case STATE_UNSYNCD:\r
-               LED_A_OFF();\r
-            if(!bit) {\r
-                // we went low, so this could be the beginning\r
-                // of an SOF\r
-                Uart.state = STATE_GOT_FALLING_EDGE_OF_SOF;\r
-                Uart.posCnt = 0;\r
-                Uart.bitCnt = 0;\r
-            }\r
-            break;\r
-\r
-        case STATE_GOT_FALLING_EDGE_OF_SOF:\r
-            Uart.posCnt++;\r
-            if(Uart.posCnt == 2) {\r
-                if(bit) {\r
-                    if(Uart.bitCnt >= 10) {\r
-                        // we've seen enough consecutive\r
-                        // zeros that it's a valid SOF\r
-                        Uart.posCnt = 0;\r
-                        Uart.byteCnt = 0;\r
-                        Uart.state = STATE_AWAITING_START_BIT;\r
-                        LED_A_ON(); // Indicate we got a valid SOF\r
-                    } else {\r
-                        // didn't stay down long enough\r
-                        // before going high, error\r
-                        Uart.state = STATE_ERROR_WAIT;\r
-                    }\r
-                } else {\r
-                    // do nothing, keep waiting\r
-                }\r
-                Uart.bitCnt++;\r
-            }\r
-            if(Uart.posCnt >= 4) Uart.posCnt = 0;\r
-            if(Uart.bitCnt > 14) {\r
-                // Give up if we see too many zeros without\r
-                // a one, too.\r
-                Uart.state = STATE_ERROR_WAIT;\r
-            }\r
-            break;\r
-\r
-        case STATE_AWAITING_START_BIT:\r
-            Uart.posCnt++;\r
-            if(bit) {\r
-                if(Uart.posCnt > 25) {\r
-                    // stayed high for too long between\r
-                    // characters, error\r
-                    Uart.state = STATE_ERROR_WAIT;\r
-                }\r
-            } else {\r
-                // falling edge, this starts the data byte\r
-                Uart.posCnt = 0;\r
-                Uart.bitCnt = 0;\r
-                Uart.shiftReg = 0;\r
-                Uart.state = STATE_RECEIVING_DATA;\r
-                LED_A_ON(); // Indicate we're receiving\r
-            }\r
-            break;\r
-\r
-        case STATE_RECEIVING_DATA:\r
-            Uart.posCnt++;\r
-            if(Uart.posCnt == 2) {\r
-                // time to sample a bit\r
-                Uart.shiftReg >>= 1;\r
-                if(bit) {\r
-                    Uart.shiftReg |= 0x200;\r
-                }\r
-                Uart.bitCnt++;\r
-            }\r
-            if(Uart.posCnt >= 4) {\r
-                Uart.posCnt = 0;\r
-            }\r
-            if(Uart.bitCnt == 10) {\r
-                if((Uart.shiftReg & 0x200) && !(Uart.shiftReg & 0x001))\r
-                {\r
-                    // this is a data byte, with correct\r
-                    // start and stop bits\r
-                    Uart.output[Uart.byteCnt] = (Uart.shiftReg >> 1) & 0xff;\r
-                    Uart.byteCnt++;\r
-\r
-                    if(Uart.byteCnt >= Uart.byteCntMax) {\r
-                        // Buffer overflowed, give up\r
-                        Uart.posCnt = 0;\r
-                        Uart.state = STATE_ERROR_WAIT;\r
-                    } else {\r
-                        // so get the next byte now\r
-                        Uart.posCnt = 0;\r
-                        Uart.state = STATE_AWAITING_START_BIT;\r
-                    }\r
-                } else if(Uart.shiftReg == 0x000) {\r
-                    // this is an EOF byte\r
-                       LED_A_OFF(); // Finished receiving\r
-                    return TRUE;\r
-                } else {\r
-                    // this is an error\r
-                    Uart.posCnt = 0;\r
-                    Uart.state = STATE_ERROR_WAIT;\r
-                }\r
-            }\r
-            break;\r
-\r
-        case STATE_ERROR_WAIT:\r
-            // We're all screwed up, so wait a little while\r
-            // for whatever went wrong to finish, and then\r
-            // start over.\r
-            Uart.posCnt++;\r
-            if(Uart.posCnt > 10) {\r
-                Uart.state = STATE_UNSYNCD;\r
-            }\r
-            break;\r
-\r
-        default:\r
-            Uart.state = STATE_UNSYNCD;\r
-            break;\r
-    }\r
-\r
-    if (Uart.state == STATE_ERROR_WAIT) LED_A_OFF(); // Error\r
-\r
-    return FALSE;\r
-}\r
-\r
-//-----------------------------------------------------------------------------\r
-// Receive a command (from the reader to us, where we are the simulated tag),\r
-// and store it in the given buffer, up to the given maximum length. Keeps\r
-// spinning, waiting for a well-framed command, until either we get one\r
-// (returns TRUE) or someone presses the pushbutton on the board (FALSE).\r
-//\r
-// Assume that we're called with the SSC (to the FPGA) and ADC path set\r
-// correctly.\r
-//-----------------------------------------------------------------------------\r
-static BOOL GetIso14443CommandFromReader(BYTE *received, int *len, int maxLen)\r
-{\r
-    BYTE mask;\r
-    int i, bit;\r
-\r
-    // Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen\r
-    // only, since we are receiving, not transmitting).\r
-    // Signal field is off with the appropriate LED\r
-    LED_D_OFF();\r
-    FpgaWriteConfWord(\r
-       FPGA_MAJOR_MODE_HF_SIMULATOR | FPGA_HF_SIMULATOR_NO_MODULATION);\r
-\r
-\r
-    // Now run a `software UART' on the stream of incoming samples.\r
-    Uart.output = received;\r
-    Uart.byteCntMax = maxLen;\r
-    Uart.state = STATE_UNSYNCD;\r
-\r
-    for(;;) {\r
-        WDT_HIT();\r
-\r
-        if(BUTTON_PRESS()) return FALSE;\r
-\r
-        if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {\r
-            AT91C_BASE_SSC->SSC_THR = 0x00;\r
-        }\r
-        if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {\r
-            BYTE b = (BYTE)AT91C_BASE_SSC->SSC_RHR;\r
-\r
-            mask = 0x80;\r
-            for(i = 0; i < 8; i++, mask >>= 1) {\r
-                bit = (b & mask);\r
-                if(Handle14443UartBit(bit)) {\r
-                    *len = Uart.byteCnt;\r
-                    return TRUE;\r
-                }\r
-            }\r
-        }\r
-    }\r
-}\r
-\r
-//-----------------------------------------------------------------------------\r
-// Main loop of simulated tag: receive commands from reader, decide what\r
-// response to send, and send it.\r
-//-----------------------------------------------------------------------------\r
-void SimulateIso14443Tag(void)\r
-{\r
-    static const BYTE cmd1[] = { 0x05, 0x00, 0x08, 0x39, 0x73 };\r
-    static const BYTE response1[] = {\r
-        0x50, 0x82, 0x0d, 0xe1, 0x74, 0x20, 0x38, 0x19, 0x22,\r
-        0x00, 0x21, 0x85, 0x5e, 0xd7\r
-    };\r
-\r
-    BYTE *resp;\r
-    int respLen;\r
-\r
-    BYTE *resp1 = (((BYTE *)BigBuf) + 800);\r
-    int resp1Len;\r
-\r
-    BYTE *receivedCmd = (BYTE *)BigBuf;\r
-    int len;\r
-\r
-    int i;\r
-\r
-    int cmdsRecvd = 0;\r
-\r
-    memset(receivedCmd, 0x44, 400);\r
-\r
-    CodeIso14443bAsTag(response1, sizeof(response1));\r
-    memcpy(resp1, ToSend, ToSendMax); resp1Len = ToSendMax;\r
-\r
-    // We need to listen to the high-frequency, peak-detected path.\r
-    SetAdcMuxFor(GPIO_MUXSEL_HIPKD);\r
-    FpgaSetupSsc();\r
-\r
-    cmdsRecvd = 0;\r
-\r
-    for(;;) {\r
-        BYTE b1, b2;\r
-\r
-        if(!GetIso14443CommandFromReader(receivedCmd, &len, 100)) {\r
-            DbpIntegers(cmdsRecvd, 0, 0);\r
-            DbpString("button press");\r
-            break;\r
-        }\r
-\r
-        // Good, look at the command now.\r
-\r
-        if(len == sizeof(cmd1) && memcmp(receivedCmd, cmd1, len)==0) {\r
-            resp = resp1; respLen = resp1Len;\r
-        } else {\r
-            DbpString("new cmd from reader:");\r
-            DbpIntegers(len, 0x1234, cmdsRecvd);\r
-            // And print whether the CRC fails, just for good measure\r
-            ComputeCrc14443(CRC_14443_B, receivedCmd, len-2, &b1, &b2);\r
-            if(b1 != receivedCmd[len-2] || b2 != receivedCmd[len-1]) {\r
-                // Not so good, try again.\r
-                DbpString("+++CRC fail");\r
-            } else {\r
-                DbpString("CRC passes");\r
-            }\r
-            break;\r
-        }\r
-\r
-        memset(receivedCmd, 0x44, 32);\r
-\r
-        cmdsRecvd++;\r
-\r
-        if(cmdsRecvd > 0x30) {\r
-            DbpString("many commands later...");\r
-            break;\r
-        }\r
-\r
-        if(respLen <= 0) continue;\r
-\r
-        // Modulate BPSK\r
-        // Signal field is off with the appropriate LED\r
-        LED_D_OFF();\r
-        FpgaWriteConfWord(\r
-               FPGA_MAJOR_MODE_HF_SIMULATOR | FPGA_HF_SIMULATOR_MODULATE_BPSK);\r
-        AT91C_BASE_SSC->SSC_THR = 0xff;\r
-        FpgaSetupSsc();\r
-\r
-        // Transmit the response.\r
-        i = 0;\r
-        for(;;) {\r
-            if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {\r
-                BYTE b = resp[i];\r
-\r
-                AT91C_BASE_SSC->SSC_THR = b;\r
-\r
-                i++;\r
-                if(i > respLen) {\r
-                    break;\r
-                }\r
-            }\r
-            if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {\r
-                volatile BYTE b = (BYTE)AT91C_BASE_SSC->SSC_RHR;\r
-                (void)b;\r
-            }\r
-        }\r
-    }\r
-}\r
-\r
-//=============================================================================\r
-// An ISO 14443 Type B reader. We take layer two commands, code them\r
-// appropriately, and then send them to the tag. We then listen for the\r
-// tag's response, which we leave in the buffer to be demodulated on the\r
-// PC side.\r
-//=============================================================================\r
-\r
-static struct {\r
-    enum {\r
-        DEMOD_UNSYNCD,\r
-        DEMOD_PHASE_REF_TRAINING,\r
-        DEMOD_AWAITING_FALLING_EDGE_OF_SOF,\r
-        DEMOD_GOT_FALLING_EDGE_OF_SOF,\r
-        DEMOD_AWAITING_START_BIT,\r
-        DEMOD_RECEIVING_DATA,\r
-        DEMOD_ERROR_WAIT\r
-    }       state;\r
-    int     bitCount;\r
-    int     posCount;\r
-    int     thisBit;\r
-    int     metric;\r
-    int     metricN;\r
-    WORD    shiftReg;\r
-    BYTE   *output;\r
-    int     len;\r
-    int     sumI;\r
-    int     sumQ;\r
-} Demod;\r
-\r
-/*\r
- * Handles reception of a bit from the tag\r
- *\r
- * LED handling:\r
- * LED C -> ON once we have received the SOF and are expecting the rest.\r
- * LED C -> OFF once we have received EOF or are unsynced\r
- *\r
- * Returns: true if we received a EOF\r
- *          false if we are still waiting for some more\r
- *\r
- */\r
-static BOOL Handle14443SamplesDemod(int ci, int cq)\r
-{\r
-    int v;\r
-\r
-    // The soft decision on the bit uses an estimate of just the\r
-    // quadrant of the reference angle, not the exact angle.\r
-#define MAKE_SOFT_DECISION() { \\r
-        if(Demod.sumI > 0) { \\r
-            v = ci; \\r
-        } else { \\r
-            v = -ci; \\r
-        } \\r
-        if(Demod.sumQ > 0) { \\r
-            v += cq; \\r
-        } else { \\r
-            v -= cq; \\r
-        } \\r
-    }\r
-\r
-    switch(Demod.state) {\r
-        case DEMOD_UNSYNCD:\r
-            v = ci;\r
-            if(v < 0) v = -v;\r
-            if(cq > 0) {\r
-                v += cq;\r
-            } else {\r
-                v -= cq;\r
-            }\r
-            if(v > 40) {\r
-                Demod.posCount = 0;\r
-                Demod.state = DEMOD_PHASE_REF_TRAINING;\r
-                Demod.sumI = 0;\r
-                Demod.sumQ = 0;\r
-            }\r
-            break;\r
-\r
-        case DEMOD_PHASE_REF_TRAINING:\r
-            if(Demod.posCount < 8) {\r
-                Demod.sumI += ci;\r
-                Demod.sumQ += cq;\r
-            } else if(Demod.posCount > 100) {\r
-                // error, waited too long\r
-                Demod.state = DEMOD_UNSYNCD;\r
-            } else {\r
-                MAKE_SOFT_DECISION();\r
-                if(v < 0) {\r
-                    Demod.state = DEMOD_AWAITING_FALLING_EDGE_OF_SOF;\r
-                    Demod.posCount = 0;\r
-                }\r
-            }\r
-            Demod.posCount++;\r
-            break;\r
-\r
-        case DEMOD_AWAITING_FALLING_EDGE_OF_SOF:\r
-            MAKE_SOFT_DECISION();\r
-            if(v < 0) {\r
-                Demod.state = DEMOD_GOT_FALLING_EDGE_OF_SOF;\r
-                Demod.posCount = 0;\r
-            } else {\r
-                if(Demod.posCount > 100) {\r
-                    Demod.state = DEMOD_UNSYNCD;\r
-                }\r
-            }\r
-            Demod.posCount++;\r
-            break;\r
-\r
-        case DEMOD_GOT_FALLING_EDGE_OF_SOF:\r
-            MAKE_SOFT_DECISION();\r
-            if(v > 0) {\r
-                if(Demod.posCount < 12) {\r
-                    Demod.state = DEMOD_UNSYNCD;\r
-                } else {\r
-                       LED_C_ON(); // Got SOF\r
-                    Demod.state = DEMOD_AWAITING_START_BIT;\r
-                    Demod.posCount = 0;\r
-                    Demod.len = 0;\r
-                    Demod.metricN = 0;\r
-                    Demod.metric = 0;\r
-                }\r
-            } else {\r
-                if(Demod.posCount > 100) {\r
-                    Demod.state = DEMOD_UNSYNCD;\r
-                }\r
-            }\r
-            Demod.posCount++;\r
-            break;\r
-\r
-        case DEMOD_AWAITING_START_BIT:\r
-            MAKE_SOFT_DECISION();\r
-            if(v > 0) {\r
-                if(Demod.posCount > 10) {\r
-                    Demod.state = DEMOD_UNSYNCD;\r
-                }\r
-            } else {\r
-                Demod.bitCount = 0;\r
-                Demod.posCount = 1;\r
-                Demod.thisBit = v;\r
-                Demod.shiftReg = 0;\r
-                Demod.state = DEMOD_RECEIVING_DATA;\r
-            }\r
-            break;\r
-\r
-        case DEMOD_RECEIVING_DATA:\r
-            MAKE_SOFT_DECISION();\r
-            if(Demod.posCount == 0) {\r
-                Demod.thisBit = v;\r
-                Demod.posCount = 1;\r
-            } else {\r
-                Demod.thisBit += v;\r
-\r
-                if(Demod.thisBit > 0) {\r
-                    Demod.metric += Demod.thisBit;\r
-                } else {\r
-                    Demod.metric -= Demod.thisBit;\r
-                }\r
-                (Demod.metricN)++;\r
-\r
-                Demod.shiftReg >>= 1;\r
-                if(Demod.thisBit > 0) {\r
-                    Demod.shiftReg |= 0x200;\r
-                }\r
-\r
-                Demod.bitCount++;\r
-                if(Demod.bitCount == 10) {\r
-                    WORD s = Demod.shiftReg;\r
-                    if((s & 0x200) && !(s & 0x001)) {\r
-                        BYTE b = (s >> 1);\r
-                        Demod.output[Demod.len] = b;\r
-                        Demod.len++;\r
-                        Demod.state = DEMOD_AWAITING_START_BIT;\r
-                    } else if(s == 0x000) {\r
-                        // This is EOF\r
-                       LED_C_OFF();\r
-                        return TRUE;\r
-                        Demod.state = DEMOD_UNSYNCD;\r
-                    } else {\r
-                        Demod.state = DEMOD_UNSYNCD;\r
-                    }\r
-                }\r
-                Demod.posCount = 0;\r
-            }\r
-            break;\r
-\r
-        default:\r
-            Demod.state = DEMOD_UNSYNCD;\r
-            break;\r
-    }\r
-\r
-    if (Demod.state == DEMOD_UNSYNCD) LED_C_OFF(); // Not synchronized...\r
-    return FALSE;\r
-}\r
-\r
-/*\r
- *  Demodulate the samples we received from the tag\r
- *  weTx: set to 'TRUE' if we behave like a reader\r
- *        set to 'FALSE' if we behave like a snooper\r
- *  quiet: set to 'TRUE' to disable debug output\r
- */\r
-static void GetSamplesFor14443Demod(BOOL weTx, int n, BOOL quiet)\r
-{\r
-    int max = 0;\r
-    BOOL gotFrame = FALSE;\r
-\r
-//#   define DMA_BUFFER_SIZE 8\r
-    SBYTE *dmaBuf;\r
-\r
-    int lastRxCounter;\r
-    SBYTE *upTo;\r
-\r
-    int ci, cq;\r
-\r
-    int samples = 0;\r
-\r
-    // Clear out the state of the "UART" that receives from the tag.\r
-    memset(BigBuf, 0x44, 400);\r
-    Demod.output = (BYTE *)BigBuf;\r
-    Demod.len = 0;\r
-    Demod.state = DEMOD_UNSYNCD;\r
-\r
-    // And the UART that receives from the reader\r
-    Uart.output = (((BYTE *)BigBuf) + 1024);\r
-    Uart.byteCntMax = 100;\r
-    Uart.state = STATE_UNSYNCD;\r
-\r
-    // Setup for the DMA.\r
-    dmaBuf = (SBYTE *)(BigBuf + 32);\r
-    upTo = dmaBuf;\r
-    lastRxCounter = DMA_BUFFER_SIZE;\r
-    FpgaSetupSscDma((BYTE *)dmaBuf, DMA_BUFFER_SIZE);\r
-\r
-    // Signal field is ON with the appropriate LED:\r
-       if (weTx) LED_D_ON(); else LED_D_OFF();\r
-    // And put the FPGA in the appropriate mode\r
-    FpgaWriteConfWord(\r
-       FPGA_MAJOR_MODE_HF_READER_RX_XCORR | FPGA_HF_READER_RX_XCORR_848_KHZ |\r
-       (weTx ? 0 : FPGA_HF_READER_RX_XCORR_SNOOP));\r
-\r
-    for(;;) {\r
-        int behindBy = lastRxCounter - AT91C_BASE_PDC_SSC->PDC_RCR;\r
-        if(behindBy > max) max = behindBy;\r
-\r
-        while(((lastRxCounter-AT91C_BASE_PDC_SSC->PDC_RCR) & (DMA_BUFFER_SIZE-1))\r
-                    > 2)\r
-        {\r
-            ci = upTo[0];\r
-            cq = upTo[1];\r
-            upTo += 2;\r
-            if(upTo - dmaBuf > DMA_BUFFER_SIZE) {\r
-                upTo -= DMA_BUFFER_SIZE;\r
-                AT91C_BASE_PDC_SSC->PDC_RNPR = (DWORD)upTo;\r
-                AT91C_BASE_PDC_SSC->PDC_RNCR = DMA_BUFFER_SIZE;\r
-            }\r
-            lastRxCounter -= 2;\r
-            if(lastRxCounter <= 0) {\r
-                lastRxCounter += DMA_BUFFER_SIZE;\r
-            }\r
-\r
-            samples += 2;\r
-\r
-            Handle14443UartBit(1);\r
-            Handle14443UartBit(1);\r
-\r
-            if(Handle14443SamplesDemod(ci, cq)) {\r
-                gotFrame = 1;\r
-            }\r
-        }\r
-\r
-        if(samples > 2000) {\r
-            break;\r
-        }\r
-    }\r
-    AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTDIS;\r
-    if (!quiet) DbpIntegers(max, gotFrame, Demod.len);\r
-}\r
-\r
-//-----------------------------------------------------------------------------\r
-// Read the tag's response. We just receive a stream of slightly-processed\r
-// samples from the FPGA, which we will later do some signal processing on,\r
-// to get the bits.\r
-//-----------------------------------------------------------------------------\r
-/*static void GetSamplesFor14443(BOOL weTx, int n)\r
-{\r
-    BYTE *dest = (BYTE *)BigBuf;\r
-    int c;\r
-\r
-    FpgaWriteConfWord(\r
-       FPGA_MAJOR_MODE_HF_READER_RX_XCORR | FPGA_HF_READER_RX_XCORR_848_KHZ |\r
-       (weTx ? 0 : FPGA_HF_READER_RX_XCORR_SNOOP));\r
-\r
-    c = 0;\r
-    for(;;) {\r
-        if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {\r
-            AT91C_BASE_SSC->SSC_THR = 0x43;\r
-        }\r
-        if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {\r
-            SBYTE b;\r
-            b = (SBYTE)AT91C_BASE_SSC->SSC_RHR;\r
-\r
-            dest[c++] = (BYTE)b;\r
-\r
-            if(c >= n) {\r
-                break;\r
-            }\r
-        }\r
-    }\r
-}*/\r
-\r
-//-----------------------------------------------------------------------------\r
-// Transmit the command (to the tag) that was placed in ToSend[].\r
-//-----------------------------------------------------------------------------\r
-static void TransmitFor14443(void)\r
-{\r
-    int c;\r
-\r
-    FpgaSetupSsc();\r
-\r
-    while(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {\r
-        AT91C_BASE_SSC->SSC_THR = 0xff;\r
-    }\r
-\r
-    // Signal field is ON with the appropriate Red LED\r
-       LED_D_ON();\r
-       // Signal we are transmitting with the Green LED\r
-       LED_B_ON();\r
-       FpgaWriteConfWord(\r
-       FPGA_MAJOR_MODE_HF_READER_TX | FPGA_HF_READER_TX_SHALLOW_MOD);\r
-\r
-    for(c = 0; c < 10;) {\r
-        if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {\r
-            AT91C_BASE_SSC->SSC_THR = 0xff;\r
-            c++;\r
-        }\r
-        if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {\r
-            volatile DWORD r = AT91C_BASE_SSC->SSC_RHR;\r
-            (void)r;\r
-        }\r
-        WDT_HIT();\r
-    }\r
-\r
-    c = 0;\r
-    for(;;) {\r
-        if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {\r
-            AT91C_BASE_SSC->SSC_THR = ToSend[c];\r
-            c++;\r
-            if(c >= ToSendMax) {\r
-                break;\r
-            }\r
-        }\r
-        if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {\r
-            volatile DWORD r = AT91C_BASE_SSC->SSC_RHR;\r
-            (void)r;\r
-        }\r
-        WDT_HIT();\r
-    }\r
-    LED_B_OFF(); // Finished sending\r
-}\r
-\r
-//-----------------------------------------------------------------------------\r
-// Code a layer 2 command (string of octets, including CRC) into ToSend[],\r
-// so that it is ready to transmit to the tag using TransmitFor14443().\r
-//-----------------------------------------------------------------------------\r
-void CodeIso14443bAsReader(const BYTE *cmd, int len)\r
-{\r
-    int i, j;\r
-    BYTE b;\r
-\r
-    ToSendReset();\r
-\r
-    // Establish initial reference level\r
-    for(i = 0; i < 40; i++) {\r
-        ToSendStuffBit(1);\r
-    }\r
-    // Send SOF\r
-    for(i = 0; i < 10; i++) {\r
-        ToSendStuffBit(0);\r
-    }\r
-\r
-    for(i = 0; i < len; i++) {\r
-        // Stop bits/EGT\r
-        ToSendStuffBit(1);\r
-        ToSendStuffBit(1);\r
-        // Start bit\r
-        ToSendStuffBit(0);\r
-        // Data bits\r
-        b = cmd[i];\r
-        for(j = 0; j < 8; j++) {\r
-            if(b & 1) {\r
-                ToSendStuffBit(1);\r
-            } else {\r
-                ToSendStuffBit(0);\r
-            }\r
-            b >>= 1;\r
-        }\r
-    }\r
-    // Send EOF\r
-    ToSendStuffBit(1);\r
-    for(i = 0; i < 10; i++) {\r
-        ToSendStuffBit(0);\r
-    }\r
-    for(i = 0; i < 8; i++) {\r
-        ToSendStuffBit(1);\r
-    }\r
-\r
-    // And then a little more, to make sure that the last character makes\r
-    // it out before we switch to rx mode.\r
-    for(i = 0; i < 24; i++) {\r
-        ToSendStuffBit(1);\r
-    }\r
-\r
-    // Convert from last character reference to length\r
-    ToSendMax++;\r
-}\r
-\r
-//-----------------------------------------------------------------------------\r
-// Read an ISO 14443 tag. We send it some set of commands, and record the\r
-// responses.\r
-// The command name is misleading, it actually decodes the reponse in HEX\r
-// into the output buffer (read the result using hexsamples, not hisamples)\r
-//-----------------------------------------------------------------------------\r
-void AcquireRawAdcSamplesIso14443(DWORD parameter)\r
-{\r
-    BYTE cmd1[] = { 0x05, 0x00, 0x08, 0x39, 0x73 };\r
-\r
-    // Make sure that we start from off, since the tags are stateful;\r
-    // confusing things will happen if we don't reset them between reads.\r
-    FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
-    LED_D_OFF();\r
-    SpinDelay(200);\r
-\r
-    SetAdcMuxFor(GPIO_MUXSEL_HIPKD);\r
-    FpgaSetupSsc();\r
-\r
-    // Now give it time to spin up.\r
-    // Signal field is on with the appropriate LED\r
-    LED_D_ON();\r
-    FpgaWriteConfWord(\r
-       FPGA_MAJOR_MODE_HF_READER_RX_XCORR | FPGA_HF_READER_RX_XCORR_848_KHZ);\r
-    SpinDelay(200);\r
-\r
-    CodeIso14443bAsReader(cmd1, sizeof(cmd1));\r
-    TransmitFor14443();\r
-//    LED_A_ON();\r
-    GetSamplesFor14443Demod(TRUE, 2000, FALSE);\r
-//    LED_A_OFF();\r
-}\r
-\r
-//-----------------------------------------------------------------------------\r
-// Read a SRI512 ISO 14443 tag.\r
-//\r
-// SRI512 tags are just simple memory tags, here we're looking at making a dump\r
-// of the contents of the memory. No anticollision algorithm is done, we assume\r
-// we have a single tag in the field.\r
-//\r
-// I tried to be systematic and check every answer of the tag, every CRC, etc...\r
-//-----------------------------------------------------------------------------\r
-void ReadSRI512Iso14443(DWORD parameter)\r
-{\r
-    BYTE i = 0x00;\r
-\r
-    // Make sure that we start from off, since the tags are stateful;\r
-    // confusing things will happen if we don't reset them between reads.\r
-    LED_D_OFF();\r
-    FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
-    SpinDelay(200);\r
-\r
-    SetAdcMuxFor(GPIO_MUXSEL_HIPKD);\r
-    FpgaSetupSsc();\r
-\r
-    // Now give it time to spin up.\r
-    // Signal field is on with the appropriate LED\r
-    LED_D_ON();\r
-    FpgaWriteConfWord(\r
-       FPGA_MAJOR_MODE_HF_READER_RX_XCORR | FPGA_HF_READER_RX_XCORR_848_KHZ);\r
-    SpinDelay(200);\r
-\r
-    // First command: wake up the tag using the INITIATE command\r
-    BYTE cmd1[] = { 0x06, 0x00, 0x97, 0x5b};\r
-    CodeIso14443bAsReader(cmd1, sizeof(cmd1));\r
-    TransmitFor14443();\r
-//    LED_A_ON();\r
-    GetSamplesFor14443Demod(TRUE, 2000,TRUE);\r
-//    LED_A_OFF();\r
-\r
-    if (Demod.len == 0) {\r
-       DbpString("No response from tag");\r
-       return;\r
-    } else {\r
-       DbpString("Randomly generated UID from tag (+ 2 byte CRC):");\r
-       DbpIntegers(Demod.output[0], Demod.output[1],Demod.output[2]);\r
-    }\r
-    // There is a response, SELECT the uid\r
-    DbpString("Now SELECT tag:");\r
-    cmd1[0] = 0x0E; // 0x0E is SELECT\r
-    cmd1[1] = Demod.output[0];\r
-    ComputeCrc14443(CRC_14443_B, cmd1, 2, &cmd1[2], &cmd1[3]);\r
-    CodeIso14443bAsReader(cmd1, sizeof(cmd1));\r
-    TransmitFor14443();\r
-//    LED_A_ON();\r
-    GetSamplesFor14443Demod(TRUE, 2000,TRUE);\r
-//    LED_A_OFF();\r
-    if (Demod.len != 3) {\r
-       DbpString("Expected 3 bytes from tag, got:");\r
-       DbpIntegers(Demod.len,0x0,0x0);\r
-       return;\r
-    }\r
-    // Check the CRC of the answer:\r
-    ComputeCrc14443(CRC_14443_B, Demod.output, 1 , &cmd1[2], &cmd1[3]);\r
-    if(cmd1[2] != Demod.output[1] || cmd1[3] != Demod.output[2]) {\r
-       DbpString("CRC Error reading select response.");\r
-       return;\r
-    }\r
-    // Check response from the tag: should be the same UID as the command we just sent:\r
-    if (cmd1[1] != Demod.output[0]) {\r
-       DbpString("Bad response to SELECT from Tag, aborting:");\r
-       DbpIntegers(cmd1[1],Demod.output[0],0x0);\r
-       return;\r
-    }\r
-    // Tag is now selected,\r
-    // First get the tag's UID:\r
-    cmd1[0] = 0x0B;\r
-    ComputeCrc14443(CRC_14443_B, cmd1, 1 , &cmd1[1], &cmd1[2]);\r
-    CodeIso14443bAsReader(cmd1, 3); // Only first three bytes for this one\r
-    TransmitFor14443();\r
-//    LED_A_ON();\r
-    GetSamplesFor14443Demod(TRUE, 2000,TRUE);\r
-//    LED_A_OFF();\r
-    if (Demod.len != 10) {\r
-       DbpString("Expected 10 bytes from tag, got:");\r
-       DbpIntegers(Demod.len,0x0,0x0);\r
-       return;\r
-    }\r
-    // The check the CRC of the answer (use cmd1 as temporary variable):\r
-    ComputeCrc14443(CRC_14443_B, Demod.output, 8, &cmd1[2], &cmd1[3]);\r
-           if(cmd1[2] != Demod.output[8] || cmd1[3] != Demod.output[9]) {\r
-       DbpString("CRC Error reading block! - Below: expected, got");\r
-       DbpIntegers( (cmd1[2]<<8)+cmd1[3], (Demod.output[8]<<8)+Demod.output[9],0);\r
-       // Do not return;, let's go on... (we should retry, maybe ?)\r
-    }\r
-    DbpString("Tag UID (64 bits):");\r
-    DbpIntegers((Demod.output[7]<<24) + (Demod.output[6]<<16) + (Demod.output[5]<<8) + Demod.output[4], (Demod.output[3]<<24) + (Demod.output[2]<<16) + (Demod.output[1]<<8) + Demod.output[0], 0);\r
-\r
-    // Now loop to read all 16 blocks, address from 0 to 15\r
-    DbpString("Tag memory dump, block 0 to 15");\r
-    cmd1[0] = 0x08;\r
-    i = 0x00;\r
-    for (;;) {\r
-           if (i == 0x10) {\r
-                   DbpString("System area block (0xff):");\r
-                   i = 0xff;\r
-           }\r
-           cmd1[1] = i;\r
-           ComputeCrc14443(CRC_14443_B, cmd1, 2, &cmd1[2], &cmd1[3]);\r
-           CodeIso14443bAsReader(cmd1, sizeof(cmd1));\r
-           TransmitFor14443();\r
-//         LED_A_ON();\r
-           GetSamplesFor14443Demod(TRUE, 2000,TRUE);\r
-//         LED_A_OFF();\r
-           if (Demod.len != 6) { // Check if we got an answer from the tag\r
-               DbpString("Expected 6 bytes from tag, got less...");\r
-               return;\r
-           }\r
-           // The check the CRC of the answer (use cmd1 as temporary variable):\r
-           ComputeCrc14443(CRC_14443_B, Demod.output, 4, &cmd1[2], &cmd1[3]);\r
-            if(cmd1[2] != Demod.output[4] || cmd1[3] != Demod.output[5]) {\r
-               DbpString("CRC Error reading block! - Below: expected, got");\r
-               DbpIntegers( (cmd1[2]<<8)+cmd1[3], (Demod.output[4]<<8)+Demod.output[5],0);\r
-               // Do not return;, let's go on... (we should retry, maybe ?)\r
-           }\r
-           // Now print out the memory location:\r
-           DbpString("Address , Contents, CRC");\r
-           DbpIntegers(i, (Demod.output[3]<<24) + (Demod.output[2]<<16) + (Demod.output[1]<<8) + Demod.output[0], (Demod.output[4]<<8)+Demod.output[5]);\r
-           if (i == 0xff) {\r
-               break;\r
-           }\r
-           i++;\r
-    }\r
-}\r
-\r
-\r
-//=============================================================================\r
-// Finally, the `sniffer' combines elements from both the reader and\r
-// simulated tag, to show both sides of the conversation.\r
-//=============================================================================\r
-\r
-//-----------------------------------------------------------------------------\r
-// Record the sequence of commands sent by the reader to the tag, with\r
-// triggering so that we start recording at the point that the tag is moved\r
-// near the reader.\r
-//-----------------------------------------------------------------------------\r
-/*\r
- * Memory usage for this function, (within BigBuf)\r
- * 0-1023 : Demodulated samples receive (1024 bytes)\r
- * 1024-1535 : Last Received command, 512 bytes (reader->tag)\r
- * 1536-2047 : Last Received command, 512 bytes(tag->reader)\r
- * 2048-2304 : DMA Buffer, 256 bytes (samples)\r
- */\r
-void SnoopIso14443(void)\r
-{\r
-    // We won't start recording the frames that we acquire until we trigger;\r
-    // a good trigger condition to get started is probably when we see a\r
-    // response from the tag.\r
-    BOOL triggered = FALSE;\r
-\r
-    // The command (reader -> tag) that we're working on receiving.\r
-    BYTE *receivedCmd = (BYTE *)(BigBuf) + 1024;\r
-    // The response (tag -> reader) that we're working on receiving.\r
-    BYTE *receivedResponse = (BYTE *)(BigBuf) + 1536;\r
-\r
-    // As we receive stuff, we copy it from receivedCmd or receivedResponse\r
-    // into trace, along with its length and other annotations.\r
-    BYTE *trace = (BYTE *)BigBuf;\r
-    int traceLen = 0;\r
-\r
-    // The DMA buffer, used to stream samples from the FPGA.\r
-    SBYTE *dmaBuf = (SBYTE *)(BigBuf) + 2048;\r
-    int lastRxCounter;\r
-    SBYTE *upTo;\r
-    int ci, cq;\r
-    int maxBehindBy = 0;\r
-\r
-    // Count of samples received so far, so that we can include timing\r
-    // information in the trace buffer.\r
-    int samples = 0;\r
-\r
-    // Initialize the trace buffer\r
-    memset(trace, 0x44, 1024);\r
-\r
-    // Set up the demodulator for tag -> reader responses.\r
-    Demod.output = receivedResponse;\r
-    Demod.len = 0;\r
-    Demod.state = DEMOD_UNSYNCD;\r
-\r
-    // And the reader -> tag commands\r
-    memset(&Uart, 0, sizeof(Uart));\r
-    Uart.output = receivedCmd;\r
-    Uart.byteCntMax = 100;\r
-    Uart.state = STATE_UNSYNCD;\r
-\r
-    // And put the FPGA in the appropriate mode\r
-    // Signal field is off with the appropriate LED\r
-    LED_D_OFF();\r
-    FpgaWriteConfWord(\r
-       FPGA_MAJOR_MODE_HF_READER_RX_XCORR | FPGA_HF_READER_RX_XCORR_848_KHZ |\r
-       FPGA_HF_READER_RX_XCORR_SNOOP);\r
-    SetAdcMuxFor(GPIO_MUXSEL_HIPKD);\r
-\r
-    // Setup for the DMA.\r
-    FpgaSetupSsc();\r
-    upTo = dmaBuf;\r
-    lastRxCounter = DMA_BUFFER_SIZE;\r
-    FpgaSetupSscDma((BYTE *)dmaBuf, DMA_BUFFER_SIZE);\r
-    // And now we loop, receiving samples.\r
-    for(;;) {\r
-       int behindBy = (lastRxCounter - AT91C_BASE_PDC_SSC->PDC_RCR) &\r
-                                (DMA_BUFFER_SIZE-1);\r
-        if(behindBy > maxBehindBy) {\r
-            maxBehindBy = behindBy;\r
-            if(behindBy > (DMA_BUFFER_SIZE-2)) { // TODO: understand whether we can increase/decrease as we want or not?\r
-                DbpString("blew circular buffer!");\r
-                DbpIntegers(behindBy,0,0);\r
-                goto done;\r
-            }\r
-        }\r
-        if(behindBy < 2) continue;\r
-\r
-        ci = upTo[0];\r
-        cq = upTo[1];\r
-        upTo += 2;\r
-        lastRxCounter -= 2;\r
-        if(upTo - dmaBuf > DMA_BUFFER_SIZE) {\r
-            upTo -= DMA_BUFFER_SIZE;\r
-            lastRxCounter += DMA_BUFFER_SIZE;\r
-            AT91C_BASE_PDC_SSC->PDC_RNPR = (DWORD) upTo;\r
-            AT91C_BASE_PDC_SSC->PDC_RNCR = DMA_BUFFER_SIZE;\r
-        }\r
-\r
-        samples += 2;\r
-\r
-#define HANDLE_BIT_IF_BODY \\r
-            if(triggered) { \\r
-                trace[traceLen++] = ((samples >>  0) & 0xff); \\r
-                trace[traceLen++] = ((samples >>  8) & 0xff); \\r
-                trace[traceLen++] = ((samples >> 16) & 0xff); \\r
-                trace[traceLen++] = ((samples >> 24) & 0xff); \\r
-                trace[traceLen++] = 0; \\r
-                trace[traceLen++] = 0; \\r
-                trace[traceLen++] = 0; \\r
-                trace[traceLen++] = 0; \\r
-                trace[traceLen++] = Uart.byteCnt; \\r
-                memcpy(trace+traceLen, receivedCmd, Uart.byteCnt); \\r
-                traceLen += Uart.byteCnt; \\r
-                if(traceLen > 1000) break; \\r
-            } \\r
-            /* And ready to receive another command. */ \\r
-            memset(&Uart, 0, sizeof(Uart)); \\r
-            Uart.output = receivedCmd; \\r
-            Uart.byteCntMax = 100; \\r
-            Uart.state = STATE_UNSYNCD; \\r
-            /* And also reset the demod code, which might have been */ \\r
-            /* false-triggered by the commands from the reader. */ \\r
-            memset(&Demod, 0, sizeof(Demod)); \\r
-            Demod.output = receivedResponse; \\r
-            Demod.state = DEMOD_UNSYNCD; \\r
-\r
-        if(Handle14443UartBit(ci & 1)) {\r
-            HANDLE_BIT_IF_BODY\r
-        }\r
-        if(Handle14443UartBit(cq & 1)) {\r
-            HANDLE_BIT_IF_BODY\r
-        }\r
-\r
-        if(Handle14443SamplesDemod(ci, cq)) {\r
-            // timestamp, as a count of samples\r
-            trace[traceLen++] = ((samples >>  0) & 0xff);\r
-            trace[traceLen++] = ((samples >>  8) & 0xff);\r
-            trace[traceLen++] = ((samples >> 16) & 0xff);\r
-            trace[traceLen++] = 0x80 | ((samples >> 24) & 0xff);\r
-            // correlation metric (~signal strength estimate)\r
-            if(Demod.metricN != 0) {\r
-                Demod.metric /= Demod.metricN;\r
-            }\r
-            trace[traceLen++] = ((Demod.metric >>  0) & 0xff);\r
-            trace[traceLen++] = ((Demod.metric >>  8) & 0xff);\r
-            trace[traceLen++] = ((Demod.metric >> 16) & 0xff);\r
-            trace[traceLen++] = ((Demod.metric >> 24) & 0xff);\r
-            // length\r
-            trace[traceLen++] = Demod.len;\r
-            memcpy(trace+traceLen, receivedResponse, Demod.len);\r
-            traceLen += Demod.len;\r
-            if(traceLen > 1000) break;\r
-\r
-            triggered = TRUE;\r
-\r
-            // And ready to receive another response.\r
-            memset(&Demod, 0, sizeof(Demod));\r
-            Demod.output = receivedResponse;\r
-            Demod.state = DEMOD_UNSYNCD;\r
-        }\r
-               WDT_HIT();\r
-\r
-        if(BUTTON_PRESS()) {\r
-            DbpString("cancelled");\r
-            goto done;\r
-        }\r
-    }\r
-\r
-    DbpString("in done pt");\r
-\r
-    DbpIntegers(maxBehindBy, Uart.state, Uart.byteCnt);\r
-    DbpIntegers(Uart.byteCntMax, traceLen, 0x23);\r
-\r
-done:\r
-       LED_D_OFF();\r
-    AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTDIS;\r
-}\r
+//-----------------------------------------------------------------------------
+// Jonathan Westhues, split Nov 2006
+//
+// This code is licensed to you under the terms of the GNU GPL, version 2 or,
+// at your option, any later version. See the LICENSE.txt file for the text of
+// the license.
+//-----------------------------------------------------------------------------
+// Routines to support ISO 14443. This includes both the reader software and
+// the `fake tag' modes. At the moment only the Type B modulation is
+// supported.
+//-----------------------------------------------------------------------------
+
+#include "proxmark3.h"
+#include "apps.h"
+#include "util.h"
+#include "string.h"
+
+#include "iso14443crc.h"
+
+//static void GetSamplesFor14443(int weTx, int n);
+
+#define DEMOD_TRACE_SIZE 4096
+#define READER_TAG_BUFFER_SIZE 2048
+#define TAG_READER_BUFFER_SIZE 2048
+#define DEMOD_DMA_BUFFER_SIZE 1024
+
+//=============================================================================
+// An ISO 14443 Type B tag. We listen for commands from the reader, using
+// a UART kind of thing that's implemented in software. When we get a
+// frame (i.e., a group of bytes between SOF and EOF), we check the CRC.
+// If it's good, then we can do something appropriate with it, and send
+// a response.
+//=============================================================================
+
+//-----------------------------------------------------------------------------
+// Code up a string of octets at layer 2 (including CRC, we don't generate
+// that here) so that they can be transmitted to the reader. Doesn't transmit
+// them yet, just leaves them ready to send in ToSend[].
+//-----------------------------------------------------------------------------
+static void CodeIso14443bAsTag(const uint8_t *cmd, int len)
+{
+    int i;
+
+    ToSendReset();
+
+    // Transmit a burst of ones, as the initial thing that lets the
+    // reader get phase sync. This (TR1) must be > 80/fs, per spec,
+    // but tag that I've tried (a Paypass) exceeds that by a fair bit,
+    // so I will too.
+    for(i = 0; i < 20; i++) {
+        ToSendStuffBit(1);
+        ToSendStuffBit(1);
+        ToSendStuffBit(1);
+        ToSendStuffBit(1);
+    }
+
+    // Send SOF.
+    for(i = 0; i < 10; i++) {
+        ToSendStuffBit(0);
+        ToSendStuffBit(0);
+        ToSendStuffBit(0);
+        ToSendStuffBit(0);
+    }
+    for(i = 0; i < 2; i++) {
+        ToSendStuffBit(1);
+        ToSendStuffBit(1);
+        ToSendStuffBit(1);
+        ToSendStuffBit(1);
+    }
+
+    for(i = 0; i < len; i++) {
+        int j;
+        uint8_t b = cmd[i];
+
+        // Start bit
+        ToSendStuffBit(0);
+        ToSendStuffBit(0);
+        ToSendStuffBit(0);
+        ToSendStuffBit(0);
+
+        // Data bits
+        for(j = 0; j < 8; j++) {
+            if(b & 1) {
+                ToSendStuffBit(1);
+                ToSendStuffBit(1);
+                ToSendStuffBit(1);
+                ToSendStuffBit(1);
+            } else {
+                ToSendStuffBit(0);
+                ToSendStuffBit(0);
+                ToSendStuffBit(0);
+                ToSendStuffBit(0);
+            }
+            b >>= 1;
+        }
+
+        // Stop bit
+        ToSendStuffBit(1);
+        ToSendStuffBit(1);
+        ToSendStuffBit(1);
+        ToSendStuffBit(1);
+    }
+
+    // Send SOF.
+    for(i = 0; i < 10; i++) {
+        ToSendStuffBit(0);
+        ToSendStuffBit(0);
+        ToSendStuffBit(0);
+        ToSendStuffBit(0);
+    }
+    for(i = 0; i < 10; i++) {
+        ToSendStuffBit(1);
+        ToSendStuffBit(1);
+        ToSendStuffBit(1);
+        ToSendStuffBit(1);
+    }
+
+    // Convert from last byte pos to length
+    ToSendMax++;
+
+    // Add a few more for slop
+    ToSendMax += 2;
+}
+
+//-----------------------------------------------------------------------------
+// The software UART that receives commands from the reader, and its state
+// variables.
+//-----------------------------------------------------------------------------
+static struct {
+    enum {
+        STATE_UNSYNCD,
+        STATE_GOT_FALLING_EDGE_OF_SOF,
+        STATE_AWAITING_START_BIT,
+        STATE_RECEIVING_DATA,
+        STATE_ERROR_WAIT
+    }       state;
+    uint16_t    shiftReg;
+    int     bitCnt;
+    int     byteCnt;
+    int     byteCntMax;
+    int     posCnt;
+    uint8_t   *output;
+} Uart;
+
+/* Receive & handle a bit coming from the reader.
+ *
+ * LED handling:
+ * LED A -> ON once we have received the SOF and are expecting the rest.
+ * LED A -> OFF once we have received EOF or are in error state or unsynced
+ *
+ * Returns: true if we received a EOF
+ *          false if we are still waiting for some more
+ */
+static int Handle14443UartBit(int bit)
+{
+    switch(Uart.state) {
+        case STATE_UNSYNCD:
+               LED_A_OFF();
+            if(!bit) {
+                // we went low, so this could be the beginning
+                // of an SOF
+                Uart.state = STATE_GOT_FALLING_EDGE_OF_SOF;
+                Uart.posCnt = 0;
+                Uart.bitCnt = 0;
+            }
+            break;
+
+        case STATE_GOT_FALLING_EDGE_OF_SOF:
+            Uart.posCnt++;
+            if(Uart.posCnt == 2) {
+                if(bit) {
+                    if(Uart.bitCnt >= 10) {
+                        // we've seen enough consecutive
+                        // zeros that it's a valid SOF
+                        Uart.posCnt = 0;
+                        Uart.byteCnt = 0;
+                        Uart.state = STATE_AWAITING_START_BIT;
+                        LED_A_ON(); // Indicate we got a valid SOF
+                    } else {
+                        // didn't stay down long enough
+                        // before going high, error
+                        Uart.state = STATE_ERROR_WAIT;
+                    }
+                } else {
+                    // do nothing, keep waiting
+                }
+                Uart.bitCnt++;
+            }
+            if(Uart.posCnt >= 4) Uart.posCnt = 0;
+            if(Uart.bitCnt > 14) {
+                // Give up if we see too many zeros without
+                // a one, too.
+                Uart.state = STATE_ERROR_WAIT;
+            }
+            break;
+
+        case STATE_AWAITING_START_BIT:
+            Uart.posCnt++;
+            if(bit) {
+                if(Uart.posCnt > 25) {
+                    // stayed high for too long between
+                    // characters, error
+                    Uart.state = STATE_ERROR_WAIT;
+                }
+            } else {
+                // falling edge, this starts the data byte
+                Uart.posCnt = 0;
+                Uart.bitCnt = 0;
+                Uart.shiftReg = 0;
+                Uart.state = STATE_RECEIVING_DATA;
+                LED_A_ON(); // Indicate we're receiving
+            }
+            break;
+
+        case STATE_RECEIVING_DATA:
+            Uart.posCnt++;
+            if(Uart.posCnt == 2) {
+                // time to sample a bit
+                Uart.shiftReg >>= 1;
+                if(bit) {
+                    Uart.shiftReg |= 0x200;
+                }
+                Uart.bitCnt++;
+            }
+            if(Uart.posCnt >= 4) {
+                Uart.posCnt = 0;
+            }
+            if(Uart.bitCnt == 10) {
+                if((Uart.shiftReg & 0x200) && !(Uart.shiftReg & 0x001))
+                {
+                    // this is a data byte, with correct
+                    // start and stop bits
+                    Uart.output[Uart.byteCnt] = (Uart.shiftReg >> 1) & 0xff;
+                    Uart.byteCnt++;
+
+                    if(Uart.byteCnt >= Uart.byteCntMax) {
+                        // Buffer overflowed, give up
+                        Uart.posCnt = 0;
+                        Uart.state = STATE_ERROR_WAIT;
+                    } else {
+                        // so get the next byte now
+                        Uart.posCnt = 0;
+                        Uart.state = STATE_AWAITING_START_BIT;
+                    }
+                } else if(Uart.shiftReg == 0x000) {
+                    // this is an EOF byte
+                       LED_A_OFF(); // Finished receiving
+                    return TRUE;
+                } else {
+                    // this is an error
+                    Uart.posCnt = 0;
+                    Uart.state = STATE_ERROR_WAIT;
+                }
+            }
+            break;
+
+        case STATE_ERROR_WAIT:
+            // We're all screwed up, so wait a little while
+            // for whatever went wrong to finish, and then
+            // start over.
+            Uart.posCnt++;
+            if(Uart.posCnt > 10) {
+                Uart.state = STATE_UNSYNCD;
+            }
+            break;
+
+        default:
+            Uart.state = STATE_UNSYNCD;
+            break;
+    }
+
+    // This row make the error blew circular buffer in hf 14b snoop
+    //if (Uart.state == STATE_ERROR_WAIT) LED_A_OFF(); // Error
+
+    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 GetIso14443CommandFromReader(uint8_t *received, int *len, int maxLen)
+{
+    uint8_t mask;
+    int i, bit;
+
+    // Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen
+    // only, since we are receiving, not transmitting).
+    // Signal field is off with the appropriate LED
+    LED_D_OFF();
+    FpgaWriteConfWord(
+       FPGA_MAJOR_MODE_HF_SIMULATOR | FPGA_HF_SIMULATOR_NO_MODULATION);
+
+
+    // Now run a `software UART' on the stream of incoming samples.
+    Uart.output = received;
+    Uart.byteCntMax = maxLen;
+    Uart.state = STATE_UNSYNCD;
+
+    for(;;) {
+        WDT_HIT();
+
+        if(BUTTON_PRESS()) return FALSE;
+
+        if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
+            AT91C_BASE_SSC->SSC_THR = 0x00;
+        }
+        if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
+            uint8_t b = (uint8_t)AT91C_BASE_SSC->SSC_RHR;
+
+            mask = 0x80;
+            for(i = 0; i < 8; i++, mask >>= 1) {
+                bit = (b & mask);
+                if(Handle14443UartBit(bit)) {
+                    *len = Uart.byteCnt;
+                    return TRUE;
+                }
+            }
+        }
+    }
+}
+
+//-----------------------------------------------------------------------------
+// Main loop of simulated tag: receive commands from reader, decide what
+// response to send, and send it.
+//-----------------------------------------------------------------------------
+void SimulateIso14443Tag(void)
+{
+    static const uint8_t cmd1[] = { 0x05, 0x00, 0x08, 0x39, 0x73 };
+    static const uint8_t response1[] = {
+        0x50, 0x82, 0x0d, 0xe1, 0x74, 0x20, 0x38, 0x19, 0x22,
+        0x00, 0x21, 0x85, 0x5e, 0xd7
+    };
+
+    uint8_t *resp;
+    int respLen;
+
+    uint8_t *resp1 = (((uint8_t *)BigBuf) + 800);
+    int resp1Len;
+
+    uint8_t *receivedCmd = (uint8_t *)BigBuf;
+    int len;
+
+    int i;
+
+    int cmdsRecvd = 0;
+
+    memset(receivedCmd, 0x44, 400);
+
+    CodeIso14443bAsTag(response1, sizeof(response1));
+    memcpy(resp1, ToSend, ToSendMax); resp1Len = ToSendMax;
+
+    // We need to listen to the high-frequency, peak-detected path.
+    SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
+    FpgaSetupSsc();
+
+    cmdsRecvd = 0;
+
+    for(;;) {
+        uint8_t b1, b2;
+
+        if(!GetIso14443CommandFromReader(receivedCmd, &len, 100)) {
+               Dbprintf("button pressed, received %d commands", cmdsRecvd);
+               break;
+        }
+
+        // Good, look at the command now.
+
+        if(len == sizeof(cmd1) && memcmp(receivedCmd, cmd1, len)==0) {
+            resp = resp1; respLen = resp1Len;
+        } else {
+            Dbprintf("new cmd from reader: len=%d, cmdsRecvd=%d", len, cmdsRecvd);
+            // And print whether the CRC fails, just for good measure
+            ComputeCrc14443(CRC_14443_B, receivedCmd, len-2, &b1, &b2);
+            if(b1 != receivedCmd[len-2] || b2 != receivedCmd[len-1]) {
+                // Not so good, try again.
+                DbpString("+++CRC fail");
+            } else {
+                DbpString("CRC passes");
+            }
+            break;
+        }
+
+        memset(receivedCmd, 0x44, 32);
+
+        cmdsRecvd++;
+
+        if(cmdsRecvd > 0x30) {
+            DbpString("many commands later...");
+            break;
+        }
+
+        if(respLen <= 0) continue;
+
+        // Modulate BPSK
+        // Signal field is off with the appropriate LED
+        LED_D_OFF();
+        FpgaWriteConfWord(
+               FPGA_MAJOR_MODE_HF_SIMULATOR | FPGA_HF_SIMULATOR_MODULATE_BPSK);
+        AT91C_BASE_SSC->SSC_THR = 0xff;
+        FpgaSetupSsc();
+
+        // Transmit the response.
+        i = 0;
+        for(;;) {
+            if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
+                uint8_t b = resp[i];
+
+                AT91C_BASE_SSC->SSC_THR = b;
+
+                i++;
+                if(i > respLen) {
+                    break;
+                }
+            }
+            if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
+                volatile uint8_t b = (uint8_t)AT91C_BASE_SSC->SSC_RHR;
+                (void)b;
+            }
+        }
+    }
+}
+
+//=============================================================================
+// An ISO 14443 Type B reader. We take layer two commands, code them
+// appropriately, and then send them to the tag. We then listen for the
+// tag's response, which we leave in the buffer to be demodulated on the
+// PC side.
+//=============================================================================
+
+static struct {
+    enum {
+        DEMOD_UNSYNCD,
+        DEMOD_PHASE_REF_TRAINING,
+        DEMOD_AWAITING_FALLING_EDGE_OF_SOF,
+        DEMOD_GOT_FALLING_EDGE_OF_SOF,
+        DEMOD_AWAITING_START_BIT,
+        DEMOD_RECEIVING_DATA,
+        DEMOD_ERROR_WAIT
+    }       state;
+    int     bitCount;
+    int     posCount;
+    int     thisBit;
+    int     metric;
+    int     metricN;
+    uint16_t    shiftReg;
+    uint8_t   *output;
+    int     len;
+    int     sumI;
+    int     sumQ;
+} Demod;
+
+/*
+ * Handles reception of a bit from the tag
+ *
+ * LED handling:
+ * LED C -> ON once we have received the SOF and are expecting the rest.
+ * LED C -> OFF once we have received EOF or are unsynced
+ *
+ * Returns: true if we received a EOF
+ *          false if we are still waiting for some more
+ *
+ */
+static RAMFUNC int Handle14443SamplesDemod(int ci, int cq)
+{
+    int v;
+
+    // The soft decision on the bit uses an estimate of just the
+    // quadrant of the reference angle, not the exact angle.
+#define MAKE_SOFT_DECISION() { \
+        if(Demod.sumI > 0) { \
+            v = ci; \
+        } else { \
+            v = -ci; \
+        } \
+        if(Demod.sumQ > 0) { \
+            v += cq; \
+        } else { \
+            v -= cq; \
+        } \
+    }
+
+    switch(Demod.state) {
+        case DEMOD_UNSYNCD:
+            v = ci;
+            if(v < 0) v = -v;
+            if(cq > 0) {
+                v += cq;
+            } else {
+                v -= cq;
+            }
+            if(v > 40) {
+                Demod.posCount = 0;
+                Demod.state = DEMOD_PHASE_REF_TRAINING;
+                Demod.sumI = 0;
+                Demod.sumQ = 0;
+            }
+            break;
+
+        case DEMOD_PHASE_REF_TRAINING:
+            if(Demod.posCount < 8) {
+                Demod.sumI += ci;
+                Demod.sumQ += cq;
+            } else if(Demod.posCount > 100) {
+                // error, waited too long
+                Demod.state = DEMOD_UNSYNCD;
+            } else {
+                MAKE_SOFT_DECISION();
+                if(v < 0) {
+                    Demod.state = DEMOD_AWAITING_FALLING_EDGE_OF_SOF;
+                    Demod.posCount = 0;
+                }
+            }
+            Demod.posCount++;
+            break;
+
+        case DEMOD_AWAITING_FALLING_EDGE_OF_SOF:
+            MAKE_SOFT_DECISION();
+            if(v < 0) {
+                Demod.state = DEMOD_GOT_FALLING_EDGE_OF_SOF;
+                Demod.posCount = 0;
+            } else {
+                if(Demod.posCount > 100) {
+                    Demod.state = DEMOD_UNSYNCD;
+                }
+            }
+            Demod.posCount++;
+            break;
+
+        case DEMOD_GOT_FALLING_EDGE_OF_SOF:
+            MAKE_SOFT_DECISION();
+            if(v > 0) {
+                if(Demod.posCount < 12) {
+                    Demod.state = DEMOD_UNSYNCD;
+                } else {
+                    LED_C_ON(); // Got SOF
+                    Demod.state = DEMOD_AWAITING_START_BIT;
+                    Demod.posCount = 0;
+                    Demod.len = 0;
+                    Demod.metricN = 0;
+                    Demod.metric = 0;
+                }
+            } else {
+                if(Demod.posCount > 100) {
+                    Demod.state = DEMOD_UNSYNCD;
+                }
+            }
+            Demod.posCount++;
+            break;
+
+        case DEMOD_AWAITING_START_BIT:
+            MAKE_SOFT_DECISION();
+            if(v > 0) {
+                if(Demod.posCount > 10) {
+                    Demod.state = DEMOD_UNSYNCD;
+                }
+            } else {
+                Demod.bitCount = 0;
+                Demod.posCount = 1;
+                Demod.thisBit = v;
+                Demod.shiftReg = 0;
+                Demod.state = DEMOD_RECEIVING_DATA;
+            }
+            break;
+
+        case DEMOD_RECEIVING_DATA:
+            MAKE_SOFT_DECISION();
+            if(Demod.posCount == 0) {
+                Demod.thisBit = v;
+                Demod.posCount = 1;
+            } else {
+                Demod.thisBit += v;
+
+                if(Demod.thisBit > 0) {
+                    Demod.metric += Demod.thisBit;
+                } else {
+                    Demod.metric -= Demod.thisBit;
+                }
+                (Demod.metricN)++;
+
+                Demod.shiftReg >>= 1;
+                if(Demod.thisBit > 0) {
+                    Demod.shiftReg |= 0x200;
+                }
+
+                Demod.bitCount++;
+                if(Demod.bitCount == 10) {
+                    uint16_t s = Demod.shiftReg;
+                    if((s & 0x200) && !(s & 0x001)) {
+                        uint8_t b = (s >> 1);
+                        Demod.output[Demod.len] = b;
+                        Demod.len++;
+                        Demod.state = DEMOD_AWAITING_START_BIT;
+                    } else if(s == 0x000) {
+                        // This is EOF
+                       LED_C_OFF();
+                        Demod.state = DEMOD_UNSYNCD;
+                        return TRUE;
+                    } else {
+                        Demod.state = DEMOD_UNSYNCD;
+                    }
+                }
+                Demod.posCount = 0;
+            }
+            break;
+
+        default:
+            Demod.state = DEMOD_UNSYNCD;
+            break;
+    }
+
+    if (Demod.state == DEMOD_UNSYNCD) LED_C_OFF(); // Not synchronized...
+    return FALSE;
+}
+
+/*
+ *  Demodulate the samples we received from the tag
+ *  weTx: set to 'TRUE' if we behave like a reader
+ *        set to 'FALSE' if we behave like a snooper
+ *  quiet: set to 'TRUE' to disable debug output
+ */
+static void GetSamplesFor14443Demod(int weTx, int n, int quiet)
+{
+    int max = 0;
+    int gotFrame = FALSE;
+
+//#   define DMA_BUFFER_SIZE 8
+    int8_t *dmaBuf;
+
+    int lastRxCounter;
+    int8_t *upTo;
+
+    int ci, cq;
+
+    int samples = 0;
+
+    // Clear out the state of the "UART" that receives from the tag.
+    memset(BigBuf, 0x00, 400);
+    Demod.output = (uint8_t *)BigBuf;
+    Demod.len = 0;
+    Demod.state = DEMOD_UNSYNCD;
+
+    // And the UART that receives from the reader
+    Uart.output = (((uint8_t *)BigBuf) + 1024);
+    Uart.byteCntMax = 100;
+    Uart.state = STATE_UNSYNCD;
+
+    // Setup for the DMA.
+    dmaBuf = (int8_t *)(BigBuf + 32);
+    upTo = dmaBuf;
+    lastRxCounter = DEMOD_DMA_BUFFER_SIZE;
+    FpgaSetupSscDma((uint8_t *)dmaBuf, DEMOD_DMA_BUFFER_SIZE);
+
+    // Signal field is ON with the appropriate LED:
+    if (weTx) LED_D_ON(); else LED_D_OFF();
+    // And put the FPGA in the appropriate mode
+    FpgaWriteConfWord(
+       FPGA_MAJOR_MODE_HF_READER_RX_XCORR | FPGA_HF_READER_RX_XCORR_848_KHZ |
+       (weTx ? 0 : FPGA_HF_READER_RX_XCORR_SNOOP));
+
+    for(;;) {
+        int behindBy = lastRxCounter - AT91C_BASE_PDC_SSC->PDC_RCR;
+        if(behindBy > max) max = behindBy;
+
+        while(((lastRxCounter-AT91C_BASE_PDC_SSC->PDC_RCR) & (DEMOD_DMA_BUFFER_SIZE-1))
+                    > 2)
+        {
+            ci = upTo[0];
+            cq = upTo[1];
+            upTo += 2;
+            if(upTo - dmaBuf > DEMOD_DMA_BUFFER_SIZE) {
+                upTo -= DEMOD_DMA_BUFFER_SIZE;
+                AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) upTo;
+                AT91C_BASE_PDC_SSC->PDC_RNCR = DEMOD_DMA_BUFFER_SIZE;
+            }
+            lastRxCounter -= 2;
+            if(lastRxCounter <= 0) {
+                lastRxCounter += DEMOD_DMA_BUFFER_SIZE;
+            }
+
+            samples += 2;
+
+            Handle14443UartBit(1);
+            Handle14443UartBit(1);
+
+            if(Handle14443SamplesDemod(ci, cq)) {
+                gotFrame = 1;
+            }
+        }
+
+        if(samples > 2000) {
+            break;
+        }
+    }
+    AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTDIS;
+    if (!quiet) Dbprintf("%x %x %x", max, gotFrame, Demod.len);
+}
+
+//-----------------------------------------------------------------------------
+// Read the tag's response. We just receive a stream of slightly-processed
+// samples from the FPGA, which we will later do some signal processing on,
+// to get the bits.
+//-----------------------------------------------------------------------------
+/*static void GetSamplesFor14443(int weTx, int n)
+{
+    uint8_t *dest = (uint8_t *)BigBuf;
+    int c;
+
+    FpgaWriteConfWord(
+       FPGA_MAJOR_MODE_HF_READER_RX_XCORR | FPGA_HF_READER_RX_XCORR_848_KHZ |
+       (weTx ? 0 : FPGA_HF_READER_RX_XCORR_SNOOP));
+
+    c = 0;
+    for(;;) {
+        if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
+            AT91C_BASE_SSC->SSC_THR = 0x43;
+        }
+        if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
+            int8_t b;
+            b = (int8_t)AT91C_BASE_SSC->SSC_RHR;
+
+            dest[c++] = (uint8_t)b;
+
+            if(c >= n) {
+                break;
+            }
+        }
+    }
+}*/
+
+//-----------------------------------------------------------------------------
+// Transmit the command (to the tag) that was placed in ToSend[].
+//-----------------------------------------------------------------------------
+static void TransmitFor14443(void)
+{
+    int c;
+
+    FpgaSetupSsc();
+
+    while(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
+        AT91C_BASE_SSC->SSC_THR = 0xff;
+    }
+
+    // Signal field is ON with the appropriate Red LED
+       LED_D_ON();
+       // Signal we are transmitting with the Green LED
+       LED_B_ON();
+       FpgaWriteConfWord(
+       FPGA_MAJOR_MODE_HF_READER_TX | FPGA_HF_READER_TX_SHALLOW_MOD);
+
+    for(c = 0; c < 10;) {
+        if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
+            AT91C_BASE_SSC->SSC_THR = 0xff;
+            c++;
+        }
+        if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
+            volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR;
+            (void)r;
+        }
+        WDT_HIT();
+    }
+
+    c = 0;
+    for(;;) {
+        if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) {
+            AT91C_BASE_SSC->SSC_THR = ToSend[c];
+            c++;
+            if(c >= ToSendMax) {
+                break;
+            }
+        }
+        if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
+            volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR;
+            (void)r;
+        }
+        WDT_HIT();
+    }
+    LED_B_OFF(); // Finished sending
+}
+
+//-----------------------------------------------------------------------------
+// Code a layer 2 command (string of octets, including CRC) into ToSend[],
+// so that it is ready to transmit to the tag using TransmitFor14443().
+//-----------------------------------------------------------------------------
+static void CodeIso14443bAsReader(const uint8_t *cmd, int len)
+{
+    int i, j;
+    uint8_t b;
+
+    ToSendReset();
+
+    // Establish initial reference level
+    for(i = 0; i < 40; i++) {
+        ToSendStuffBit(1);
+    }
+    // Send SOF
+    for(i = 0; i < 10; i++) {
+        ToSendStuffBit(0);
+    }
+
+    for(i = 0; i < len; i++) {
+        // Stop bits/EGT
+        ToSendStuffBit(1);
+        ToSendStuffBit(1);
+        // Start bit
+        ToSendStuffBit(0);
+        // Data bits
+        b = cmd[i];
+        for(j = 0; j < 8; j++) {
+            if(b & 1) {
+                ToSendStuffBit(1);
+            } else {
+                ToSendStuffBit(0);
+            }
+            b >>= 1;
+        }
+    }
+    // Send EOF
+    ToSendStuffBit(1);
+    for(i = 0; i < 10; i++) {
+        ToSendStuffBit(0);
+    }
+    for(i = 0; i < 8; i++) {
+        ToSendStuffBit(1);
+    }
+
+    // And then a little more, to make sure that the last character makes
+    // it out before we switch to rx mode.
+    for(i = 0; i < 24; i++) {
+        ToSendStuffBit(1);
+    }
+
+    // Convert from last character reference to length
+    ToSendMax++;
+}
+
+//-----------------------------------------------------------------------------
+// Read an ISO 14443 tag. We send it some set of commands, and record the
+// responses.
+// The command name is misleading, it actually decodes the reponse in HEX
+// into the output buffer (read the result using hexsamples, not hisamples)
+//
+// obsolete function only for test
+//-----------------------------------------------------------------------------
+void AcquireRawAdcSamplesIso14443(uint32_t parameter)
+{
+    uint8_t cmd1[] = { 0x05, 0x00, 0x08, 0x39, 0x73 };
+
+    SendRawCommand14443B(sizeof(cmd1),1,1,cmd1);
+}
+
+//-----------------------------------------------------------------------------
+// Read a SRI512 ISO 14443 tag.
+//
+// SRI512 tags are just simple memory tags, here we're looking at making a dump
+// of the contents of the memory. No anticollision algorithm is done, we assume
+// we have a single tag in the field.
+//
+// I tried to be systematic and check every answer of the tag, every CRC, etc...
+//-----------------------------------------------------------------------------
+void ReadSTMemoryIso14443(uint32_t dwLast)
+{
+    uint8_t i = 0x00;
+
+    // Make sure that we start from off, since the tags are stateful;
+    // confusing things will happen if we don't reset them between reads.
+    LED_D_OFF();
+    FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
+    SpinDelay(200);
+
+    SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
+    FpgaSetupSsc();
+
+    // Now give it time to spin up.
+    // Signal field is on with the appropriate LED
+    LED_D_ON();
+    FpgaWriteConfWord(
+       FPGA_MAJOR_MODE_HF_READER_RX_XCORR | FPGA_HF_READER_RX_XCORR_848_KHZ);
+    SpinDelay(200);
+
+    // First command: wake up the tag using the INITIATE command
+    uint8_t cmd1[] = { 0x06, 0x00, 0x97, 0x5b};
+    CodeIso14443bAsReader(cmd1, sizeof(cmd1));
+    TransmitFor14443();
+//    LED_A_ON();
+    GetSamplesFor14443Demod(TRUE, 2000,TRUE);
+//    LED_A_OFF();
+
+    if (Demod.len == 0) {
+       DbpString("No response from tag");
+       return;
+    } else {
+       Dbprintf("Randomly generated UID from tag (+ 2 byte CRC): %x %x %x",
+               Demod.output[0], Demod.output[1],Demod.output[2]);
+    }
+    // There is a response, SELECT the uid
+    DbpString("Now SELECT tag:");
+    cmd1[0] = 0x0E; // 0x0E is SELECT
+    cmd1[1] = Demod.output[0];
+    ComputeCrc14443(CRC_14443_B, cmd1, 2, &cmd1[2], &cmd1[3]);
+    CodeIso14443bAsReader(cmd1, sizeof(cmd1));
+    TransmitFor14443();
+//    LED_A_ON();
+    GetSamplesFor14443Demod(TRUE, 2000,TRUE);
+//    LED_A_OFF();
+    if (Demod.len != 3) {
+       Dbprintf("Expected 3 bytes from tag, got %d", Demod.len);
+       return;
+    }
+    // Check the CRC of the answer:
+    ComputeCrc14443(CRC_14443_B, Demod.output, 1 , &cmd1[2], &cmd1[3]);
+    if(cmd1[2] != Demod.output[1] || cmd1[3] != Demod.output[2]) {
+       DbpString("CRC Error reading select response.");
+       return;
+    }
+    // Check response from the tag: should be the same UID as the command we just sent:
+    if (cmd1[1] != Demod.output[0]) {
+       Dbprintf("Bad response to SELECT from Tag, aborting: %x %x", cmd1[1], Demod.output[0]);
+       return;
+    }
+    // Tag is now selected,
+    // First get the tag's UID:
+    cmd1[0] = 0x0B;
+    ComputeCrc14443(CRC_14443_B, cmd1, 1 , &cmd1[1], &cmd1[2]);
+    CodeIso14443bAsReader(cmd1, 3); // Only first three bytes for this one
+    TransmitFor14443();
+//    LED_A_ON();
+    GetSamplesFor14443Demod(TRUE, 2000,TRUE);
+//    LED_A_OFF();
+    if (Demod.len != 10) {
+       Dbprintf("Expected 10 bytes from tag, got %d", Demod.len);
+       return;
+    }
+    // The check the CRC of the answer (use cmd1 as temporary variable):
+    ComputeCrc14443(CRC_14443_B, Demod.output, 8, &cmd1[2], &cmd1[3]);
+           if(cmd1[2] != Demod.output[8] || cmd1[3] != Demod.output[9]) {
+       Dbprintf("CRC Error reading block! - Below: expected, got %x %x",
+               (cmd1[2]<<8)+cmd1[3], (Demod.output[8]<<8)+Demod.output[9]);
+       // Do not return;, let's go on... (we should retry, maybe ?)
+    }
+    Dbprintf("Tag UID (64 bits): %08x %08x",
+       (Demod.output[7]<<24) + (Demod.output[6]<<16) + (Demod.output[5]<<8) + Demod.output[4],
+       (Demod.output[3]<<24) + (Demod.output[2]<<16) + (Demod.output[1]<<8) + Demod.output[0]);
+
+    // Now loop to read all 16 blocks, address from 0 to last block
+    Dbprintf("Tag memory dump, block 0 to %d",dwLast);
+    cmd1[0] = 0x08;
+    i = 0x00;
+    dwLast++;
+    for (;;) {
+           if (i == dwLast) {
+                   DbpString("System area block (0xff):");
+                   i = 0xff;
+           }
+           cmd1[1] = i;
+           ComputeCrc14443(CRC_14443_B, cmd1, 2, &cmd1[2], &cmd1[3]);
+           CodeIso14443bAsReader(cmd1, sizeof(cmd1));
+           TransmitFor14443();
+//         LED_A_ON();
+           GetSamplesFor14443Demod(TRUE, 2000,TRUE);
+//         LED_A_OFF();
+           if (Demod.len != 6) { // Check if we got an answer from the tag
+               DbpString("Expected 6 bytes from tag, got less...");
+               return;
+           }
+           // The check the CRC of the answer (use cmd1 as temporary variable):
+           ComputeCrc14443(CRC_14443_B, Demod.output, 4, &cmd1[2], &cmd1[3]);
+            if(cmd1[2] != Demod.output[4] || cmd1[3] != Demod.output[5]) {
+               Dbprintf("CRC Error reading block! - Below: expected, got %x %x",
+                       (cmd1[2]<<8)+cmd1[3], (Demod.output[4]<<8)+Demod.output[5]);
+               // Do not return;, let's go on... (we should retry, maybe ?)
+           }
+           // Now print out the memory location:
+           Dbprintf("Address=%x, Contents=%x, CRC=%x", i,
+               (Demod.output[3]<<24) + (Demod.output[2]<<16) + (Demod.output[1]<<8) + Demod.output[0],
+               (Demod.output[4]<<8)+Demod.output[5]);
+           if (i == 0xff) {
+               break;
+           }
+           i++;
+    }
+}
+
+
+//=============================================================================
+// Finally, the `sniffer' combines elements from both the reader and
+// simulated tag, to show both sides of the conversation.
+//=============================================================================
+
+//-----------------------------------------------------------------------------
+// Record the sequence of commands sent by the reader to the tag, with
+// triggering so that we start recording at the point that the tag is moved
+// near the reader.
+//-----------------------------------------------------------------------------
+/*
+ * Memory usage for this function, (within BigBuf)
+ * 0-4095 : Demodulated samples receive (4096 bytes) - DEMOD_TRACE_SIZE
+ * 4096-6143 : Last Received command, 2048 bytes (reader->tag) - READER_TAG_BUFFER_SIZE
+ * 6144-8191 : Last Received command, 2048 bytes(tag->reader) - TAG_READER_BUFFER_SIZE
+ * 8192-9215 : DMA Buffer, 1024 bytes (samples) - DEMOD_DMA_BUFFER_SIZE
+ */
+void RAMFUNC SnoopIso14443(void)
+{
+    // We won't start recording the frames that we acquire until we trigger;
+    // a good trigger condition to get started is probably when we see a
+    // response from the tag.
+    int triggered = TRUE;
+
+    // The command (reader -> tag) that we're working on receiving.
+    uint8_t *receivedCmd = (uint8_t *)(BigBuf) + DEMOD_TRACE_SIZE;
+    // The response (tag -> reader) that we're working on receiving.
+    uint8_t *receivedResponse = (uint8_t *)(BigBuf) + DEMOD_TRACE_SIZE + READER_TAG_BUFFER_SIZE;
+
+    // As we receive stuff, we copy it from receivedCmd or receivedResponse
+    // into trace, along with its length and other annotations.
+    uint8_t *trace = (uint8_t *)BigBuf;
+    int traceLen = 0;
+
+    // The DMA buffer, used to stream samples from the FPGA.
+    int8_t *dmaBuf = (int8_t *)(BigBuf) + DEMOD_TRACE_SIZE + READER_TAG_BUFFER_SIZE + TAG_READER_BUFFER_SIZE;
+    int lastRxCounter;
+    int8_t *upTo;
+    int ci, cq;
+    int maxBehindBy = 0;
+
+    // Count of samples received so far, so that we can include timing
+    // information in the trace buffer.
+    int samples = 0;
+
+    // Initialize the trace buffer
+    memset(trace, 0x44, DEMOD_TRACE_SIZE);
+
+    // Set up the demodulator for tag -> reader responses.
+    Demod.output = receivedResponse;
+    Demod.len = 0;
+    Demod.state = DEMOD_UNSYNCD;
+
+    // And the reader -> tag commands
+    memset(&Uart, 0, sizeof(Uart));
+    Uart.output = receivedCmd;
+    Uart.byteCntMax = 100;
+    Uart.state = STATE_UNSYNCD;
+
+    // Print some debug information about the buffer sizes
+    Dbprintf("Snooping buffers initialized:");
+    Dbprintf("  Trace: %i bytes", DEMOD_TRACE_SIZE);
+    Dbprintf("  Reader -> tag: %i bytes", READER_TAG_BUFFER_SIZE);
+    Dbprintf("  tag -> Reader: %i bytes", TAG_READER_BUFFER_SIZE);
+    Dbprintf("  DMA: %i bytes", DEMOD_DMA_BUFFER_SIZE);
+
+    // And put the FPGA in the appropriate mode
+    // Signal field is off with the appropriate LED
+    LED_D_OFF();
+    FpgaWriteConfWord(
+       FPGA_MAJOR_MODE_HF_READER_RX_XCORR | FPGA_HF_READER_RX_XCORR_848_KHZ |
+       FPGA_HF_READER_RX_XCORR_SNOOP);
+    SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
+
+    // Setup for the DMA.
+    FpgaSetupSsc();
+    upTo = dmaBuf;
+    lastRxCounter = DEMOD_DMA_BUFFER_SIZE;
+    FpgaSetupSscDma((uint8_t *)dmaBuf, DEMOD_DMA_BUFFER_SIZE);
+               
+    LED_A_ON();
+               
+    // And now we loop, receiving samples.
+    for(;;) {
+       int behindBy = (lastRxCounter - AT91C_BASE_PDC_SSC->PDC_RCR) &
+                                (DEMOD_DMA_BUFFER_SIZE-1);
+        if(behindBy > maxBehindBy) {
+            maxBehindBy = behindBy;
+            if(behindBy > (DEMOD_DMA_BUFFER_SIZE-2)) { // TODO: understand whether we can increase/decrease as we want or not?
+                Dbprintf("blew circular buffer! behindBy=0x%x", behindBy);
+                goto done;
+            }
+        }
+        if(behindBy < 2) continue;
+
+        ci = upTo[0];
+        cq = upTo[1];
+        upTo += 2;
+        lastRxCounter -= 2;
+        if(upTo - dmaBuf > DEMOD_DMA_BUFFER_SIZE) {
+            upTo -= DEMOD_DMA_BUFFER_SIZE;
+            lastRxCounter += DEMOD_DMA_BUFFER_SIZE;
+            AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) upTo;
+            AT91C_BASE_PDC_SSC->PDC_RNCR = DEMOD_DMA_BUFFER_SIZE;
+        }
+
+        samples += 2;
+
+#define HANDLE_BIT_IF_BODY \
+            if(triggered) { \
+                trace[traceLen++] = ((samples >>  0) & 0xff); \
+                trace[traceLen++] = ((samples >>  8) & 0xff); \
+                trace[traceLen++] = ((samples >> 16) & 0xff); \
+                trace[traceLen++] = ((samples >> 24) & 0xff); \
+                trace[traceLen++] = 0; \
+                trace[traceLen++] = 0; \
+                trace[traceLen++] = 0; \
+                trace[traceLen++] = 0; \
+                trace[traceLen++] = Uart.byteCnt; \
+                memcpy(trace+traceLen, receivedCmd, Uart.byteCnt); \
+                traceLen += Uart.byteCnt; \
+                if(traceLen > 1000) break; \
+            } \
+            /* And ready to receive another command. */ \
+            memset(&Uart, 0, sizeof(Uart)); \
+            Uart.output = receivedCmd; \
+            Uart.byteCntMax = 100; \
+            Uart.state = STATE_UNSYNCD; \
+            /* And also reset the demod code, which might have been */ \
+            /* false-triggered by the commands from the reader. */ \
+            memset(&Demod, 0, sizeof(Demod)); \
+            Demod.output = receivedResponse; \
+            Demod.state = DEMOD_UNSYNCD; \
+
+        if(Handle14443UartBit(ci & 1)) {
+            HANDLE_BIT_IF_BODY
+        }
+        if(Handle14443UartBit(cq & 1)) {
+            HANDLE_BIT_IF_BODY
+        }
+
+        if(Handle14443SamplesDemod(ci, cq)) {
+            // timestamp, as a count of samples
+            trace[traceLen++] = ((samples >>  0) & 0xff);
+            trace[traceLen++] = ((samples >>  8) & 0xff);
+            trace[traceLen++] = ((samples >> 16) & 0xff);
+            trace[traceLen++] = 0x80 | ((samples >> 24) & 0xff);
+            // correlation metric (~signal strength estimate)
+            if(Demod.metricN != 0) {
+                Demod.metric /= Demod.metricN;
+            }
+            trace[traceLen++] = ((Demod.metric >>  0) & 0xff);
+            trace[traceLen++] = ((Demod.metric >>  8) & 0xff);
+            trace[traceLen++] = ((Demod.metric >> 16) & 0xff);
+            trace[traceLen++] = ((Demod.metric >> 24) & 0xff);
+            // length
+            trace[traceLen++] = Demod.len;
+            memcpy(trace+traceLen, receivedResponse, Demod.len);
+            traceLen += Demod.len;
+            if(traceLen > DEMOD_TRACE_SIZE) {
+                               DbpString("Reached trace limit");
+                               goto done;
+                       }
+
+            triggered = TRUE;
+            LED_A_OFF();
+            LED_B_ON();
+
+            // And ready to receive another response.
+            memset(&Demod, 0, sizeof(Demod));
+            Demod.output = receivedResponse;
+            Demod.state = DEMOD_UNSYNCD;
+        }
+       WDT_HIT();
+
+        if(BUTTON_PRESS()) {
+            DbpString("cancelled");
+            goto done;
+        }
+    }
+
+done:
+       LED_A_OFF();
+       LED_B_OFF();
+       LED_C_OFF();
+  AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTDIS;
+       DbpString("Snoop statistics:");
+  Dbprintf("  Max behind by: %i", maxBehindBy);
+       Dbprintf("  Uart State: %x", Uart.state);
+       Dbprintf("  Uart ByteCnt: %i", Uart.byteCnt);
+       Dbprintf("  Uart ByteCntMax: %i", Uart.byteCntMax);
+       Dbprintf("  Trace length: %i", traceLen);
+}
+
+/*
+ * Send raw command to tag ISO14443B
+ * @Input
+ * datalen     len of buffer data
+ * recv        bool when true wait for data from tag and send to client
+ * powerfield  bool leave the field on when true
+ * data        buffer with byte to send
+ *
+ * @Output
+ * none
+ *
+ */
+
+void SendRawCommand14443B(uint32_t datalen, uint32_t recv,uint8_t powerfield, uint8_t data[])
+{
+    if(!powerfield)
+    {
+        // Make sure that we start from off, since the tags are stateful;
+        // confusing things will happen if we don't reset them between reads.
+        FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
+        LED_D_OFF();
+        SpinDelay(200);
+    }
+
+    if(!GETBIT(GPIO_LED_D))
+    {
+        SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
+        FpgaSetupSsc();
+
+        // Now give it time to spin up.
+        // Signal field is on with the appropriate LED
+        LED_D_ON();
+        FpgaWriteConfWord(
+            FPGA_MAJOR_MODE_HF_READER_RX_XCORR | FPGA_HF_READER_RX_XCORR_848_KHZ);
+        SpinDelay(200);
+    }
+
+    CodeIso14443bAsReader(data, datalen);
+    TransmitFor14443();
+    if(recv)
+    {
+        uint16_t iLen = MIN(Demod.len,USB_CMD_DATA_SIZE);
+        GetSamplesFor14443Demod(TRUE, 2000, TRUE);
+        cmd_send(CMD_ACK,iLen,0,0,Demod.output,iLen);
+    }
+    if(!powerfield)
+    {
+        FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
+        LED_D_OFF();
+    }
+}
+
Impressum, Datenschutz