]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - armsrc/pcf7931.c
FIX: lf hitag : Mea culpa, simulation should not have reader_field on. thanks to...
[proxmark3-svn] / armsrc / pcf7931.c
index 2312795bef38ad8d5721099a6a9d8c9235aee3b9..df8b1a96a8c8175e35e1f20df5dd01f1e02a2fe4 100644 (file)
@@ -3,9 +3,6 @@
 #define T0_PCF 8 //period for the pcf7931 in us
 #define ALLOC 16
 
-#define abs(x) ( ((x)<0) ? -(x) : (x) )
-#define max(x,y) ( x<y ? y:x)
-
 int DemodPCF7931(uint8_t **outBlocks) {
 
     uint8_t bits[256] = {0x00};
@@ -15,8 +12,7 @@ int DemodPCF7931(uint8_t **outBlocks) {
        int GraphTraceLen = BigBuf_max_traceLen();
        if (  GraphTraceLen > 18000 )
                GraphTraceLen = 18000;
-       
-       
+               
        int i, j, lastval, bitidx, half_switch;
        int clock = 64;
        int tolerance = clock / 8;
@@ -25,6 +21,8 @@ int DemodPCF7931(uint8_t **outBlocks) {
        int num_blocks = 0;
        int lmin=128, lmax=128;
        uint8_t dir;
+       //clear read buffer
+       BigBuf_Clear_keep_EM();
 
        LFSetupFPGAForADC(95, true);
        DoAcquisition_default(0, true);
@@ -66,7 +64,7 @@ int DemodPCF7931(uint8_t **outBlocks) {
 
                        // Switch depending on lc length:
                        // Tolerance is 1/8 of clock rate (arbitrary)
-                       if (abs(lc-clock/4) < tolerance) {
+                       if (ABS(lc-clock/4) < tolerance) {
                                // 16T0
                                if((i - pmc) == lc) { /* 16T0 was previous one */
                                        /* It's a PMC ! */
@@ -78,7 +76,7 @@ int DemodPCF7931(uint8_t **outBlocks) {
                                else {
                                        pmc = i;
                                }
-                       } else if (abs(lc-clock/2) < tolerance) {
+                       } else if (ABS(lc-clock/2) < tolerance) {
                                // 32TO
                                if((i - pmc) == lc) { /* 16T0 was previous one */
                                        /* It's a PMC ! */
@@ -93,7 +91,7 @@ int DemodPCF7931(uint8_t **outBlocks) {
                                }
                                else
                                        half_switch++;
-                       } else if (abs(lc-clock) < tolerance) {
+                       } else if (ABS(lc-clock) < tolerance) {
                                // 64TO
                 bits[bitidx++] = 1;
                        } else {
@@ -148,10 +146,12 @@ int IsBlock0PCF7931(uint8_t *Block) {
 
 int IsBlock1PCF7931(uint8_t *Block) {
        // Assume RFU means 0 :)
-       if(Block[10] == 0 && Block[11] == 0 && Block[12] == 0 && Block[13] == 0)
-               if((Block[14] & 0x7f) <= 9 && Block[15] <= 9)
+       if( Block[10] == 0 && 
+           Block[11] == 0 && 
+               Block[12] == 0 && 
+               Block[13] == 0)
+                if ( (Block[14] & 0x7f) <= 9 && Block[15] <= 9)
                        return 1;
-
        return 0;
 }
 
@@ -198,7 +198,7 @@ void ReadPCF7931() {
                                                Blocks[0][ALLOC] = 1;
                                                memcpy(Blocks[1], tmpBlocks[i+1], 16);
                                                Blocks[1][ALLOC] = 1;
-                                               max_blocks = max((Blocks[1][14] & 0x7f), Blocks[1][15]) + 1;
+                                               max_blocks = MAX((Blocks[1][14] & 0x7f), Blocks[1][15]) + 1;
                                                // Debug print
                                                Dbprintf("(dbg) Max blocks: %d", max_blocks);
                                                num_blocks = 2;
@@ -264,7 +264,7 @@ void ReadPCF7931() {
        }
        Dbprintf("-----------------------------------------");
 
-       return ;
+       cmd_send(CMD_ACK,0,0,0,0,0);
 }
 
 
@@ -275,8 +275,7 @@ void ReadPCF7931() {
  */
 void WritePCF7931(uint8_t pass1, uint8_t pass2, uint8_t pass3, uint8_t pass4, uint8_t pass5, uint8_t pass6, uint8_t pass7, uint16_t init_delay, int32_t l, int32_t p, uint8_t address, uint8_t byte, uint8_t data)
 {
-
-       uint32_t tab[1024]={0}; // data times frame
+       uint32_t tab[1024] = {0}; // data times frame
        uint32_t u = 0;
        uint8_t parity = 0;
        bool comp = 0;
@@ -295,7 +294,6 @@ void WritePCF7931(uint8_t pass1, uint8_t pass2, uint8_t pass3, uint8_t pass4, ui
        //password indication bit
        AddBitPCF7931(1, tab, l, p);
 
-
        //password (on 56 bits)
        Dbprintf("Password (LSB first on each byte) : %02x %02x %02x %02x %02x %02x %02x", pass1,pass2,pass3,pass4,pass5,pass6,pass7);
        AddBytePCF7931(pass1, tab, l, p);
@@ -362,8 +360,7 @@ void WritePCF7931(uint8_t pass1, uint8_t pass2, uint8_t pass3, uint8_t pass4, ui
                tab[u]=(tab[u] * 3)/2;
        }
 
-
-       //compennsation of the counter reload
+       //compensation of the counter reload
        while (!comp){
                comp = 1;
                for(u=0;tab[u]!=0;u++){
@@ -384,10 +381,9 @@ void WritePCF7931(uint8_t pass1, uint8_t pass2, uint8_t pass3, uint8_t pass4, ui
  */
 
 void SendCmdPCF7931(uint32_t * tab){
-       uint16_t u=0;
-       uint16_t tempo=0;
+       uint16_t u=0, tempo=0;
 
-       Dbprintf("SENDING DATA FRAME...");
+       Dbprintf("Sending data frame...");
 
        FpgaDownloadAndGo(FPGA_BITSTREAM_LF);
 
@@ -402,7 +398,7 @@ void SendCmdPCF7931(uint32_t * tab){
        AT91C_BASE_PIOA->PIO_OER = GPIO_SSC_DOUT;
 
        //initialization of the timer
-       AT91C_BASE_PMC->PMC_PCER |= (0x1 << 12) | (0x1 << 13) | (0x1 << 14);
+       AT91C_BASE_PMC->PMC_PCER |= (0x1 << AT91C_ID_TC0);
        AT91C_BASE_TCB->TCB_BMR = AT91C_TCB_TC0XC0S_NONE | AT91C_TCB_TC1XC1S_TIOA0 | AT91C_TCB_TC2XC2S_NONE;
        AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS; // timer disable
        AT91C_BASE_TC0->TC_CMR = AT91C_TC_CLKS_TIMER_DIV3_CLOCK;  //clock at 48/32 MHz
@@ -411,39 +407,26 @@ void SendCmdPCF7931(uint32_t * tab){
 
 
        tempo = AT91C_BASE_TC0->TC_CV;
-       for(u=0;tab[u]!= 0;u+=3){
-
+       for( u = 0; tab[u] != 0; u += 3){
 
                // modulate antenna
                HIGH(GPIO_SSC_DOUT);
-               while(tempo !=  tab[u]){
-                       tempo = AT91C_BASE_TC0->TC_CV;
-               }
+               while(tempo != tab[u]) tempo = AT91C_BASE_TC0->TC_CV;           
 
                // stop modulating antenna
                LOW(GPIO_SSC_DOUT);
-               while(tempo !=  tab[u+1]){
-                       tempo = AT91C_BASE_TC0->TC_CV;
-               }
-
+               while(tempo != tab[u+1]) tempo = AT91C_BASE_TC0->TC_CV;
 
                // modulate antenna
                HIGH(GPIO_SSC_DOUT);
-               while(tempo !=  tab[u+2]){
-                       tempo = AT91C_BASE_TC0->TC_CV;
-               }
-
-
+               while(tempo != tab[u+2]) tempo = AT91C_BASE_TC0->TC_CV;         
        }
 
        LED_A_OFF();
        FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
        SpinDelay(200);
 
-
        AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS; // timer disable
-       DbpString("FINISH !");
-       DbpString("(Could be usefull to send the same trame many times)");
        LED(0xFFFF, 1000);
 }
 
@@ -458,15 +441,14 @@ void SendCmdPCF7931(uint32_t * tab){
 bool AddBytePCF7931(uint8_t byte, uint32_t * tab, int32_t l, int32_t p){
 
        uint32_t u;
-       for (u=0; u<8; u++)
+       for ( u=0; u<8; u++)
        {
                if (byte&(1<<u)) {      //bit à 1
-                       if(AddBitPCF7931(1, tab, l, p)==1)return 1;
+                       if( AddBitPCF7931(1, tab, l, p)==1) return 1;
                } else { //bit à 0
-                       if(AddBitPCF7931(0, tab, l, p)==1)return 1;
+                       if (AddBitPCF7931(0, tab, l, p)==1) return 1;
                }
        }
-
        return 0;
 }
 
@@ -479,27 +461,29 @@ bool AddBytePCF7931(uint8_t byte, uint32_t * tab, int32_t l, int32_t p){
 bool AddBitPCF7931(bool b, uint32_t * tab, int32_t l, int32_t p){
        uint8_t u = 0;
 
-       for(u=0;tab[u]!=0;u+=3){} //we put the cursor at the last value of the array
+       //we put the cursor at the last value of the array
+       for ( u = 0; tab[u] != 0; u += 3 ) { } 
        
+       if ( b == 1 ) { //add a bit 1
+               if ( u == 0 ) 
+                       tab[u] = 34 * T0_PCF + p;
+               else
+                       tab[u] = 34 * T0_PCF + tab[u-1] + p;
 
-       if(b==1){       //add a bit 1
-               if(u==0) tab[u] = 34*T0_PCF+p;
-               else     tab[u] = 34*T0_PCF+tab[u-1]+p;
-
-               tab[u+1] = 6*T0_PCF+tab[u]+l;
-               tab[u+2] = 88*T0_PCF+tab[u+1]-l-p;
+               tab[u+1] =  6 * T0_PCF + tab[u] + l;
+               tab[u+2] = 88 * T0_PCF + tab[u+1] - l - p;
                return 0;
-       }else{          //add a bit 0
+       } else {                //add a bit 0
 
-               if(u==0) tab[u] = 98*T0_PCF+p;
-               else     tab[u] = 98*T0_PCF+tab[u-1]+p;
+               if ( u == 0 )
+                       tab[u] = 98 * T0_PCF + p;
+               else
+                       tab[u] = 98 * T0_PCF + tab[u-1] + p;
 
-               tab[u+1] = 6*T0_PCF+tab[u]+l;
-               tab[u+2] = 24*T0_PCF+tab[u+1]-l-p;
+               tab[u+1] =  6 * T0_PCF + tab[u] + l;
+               tab[u+2] = 24 * T0_PCF + tab[u+1] - l - p;
                return 0;
        }
-
-       
        return 1;
 }
 
@@ -511,13 +495,11 @@ bool AddBitPCF7931(bool b, uint32_t * tab, int32_t l, int32_t p){
  */
 bool AddPatternPCF7931(uint32_t a, uint32_t b, uint32_t c, uint32_t * tab){
        uint32_t u = 0;
-       for(u=0;tab[u]!=0;u+=3){} //we put the cursor at the last value of the array
-
-       if(u==0) tab[u] = a;
-       else tab[u] = a + tab[u-1];
+       for(u = 0; tab[u] != 0; u += 3){} //we put the cursor at the last value of the array
 
-       tab[u+1] = b+tab[u];
-       tab[u+2] = c+tab[u+1];
+       tab[u]   = (u == 0) ? a : a + tab[u-1];
+       tab[u+1] = b + tab[u];
+       tab[u+2] = c + tab[u+1];
 
        return 0;
 }
\ No newline at end of file
Impressum, Datenschutz