⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mfucfunc.lst

📁 此程序为13.56MHz的RFID底层读写卡程序
💻 LST
📖 第 1 页 / 共 4 页
字号:
 698             {
 699                //Get UID in 1 - 3 levels (standard, [double], [triple] )
 700                //-------
 701                switch(br)
 702                {
 703                   case 0: cmdASEL = PICC_ANTICOLL1; break;
 704                   default:
 705                        status = MI_BAUDRATE_NOT_SUPPORTED; break;
 706                }
 707             }
 708             if (status == MI_OK)
 709             {
 710                //Select UID in up to 3 cascade levels (standard, [double], [triple] )
 711                //------------------------------------
 712                cascade_level = 0;
 713                uid_index     = 0;
 714                tmpuid[0] = 0x88;     //first byte of cascaded UIDs is 0x88 (cascaded tag)
 715          
 716                do
 717                {
 718                  sel_code   = cmdASEL + (2 * cascade_level);
 719                  cmdASEL = PICC_ANTICOLL1; // reset anticollistion level for calculation
C51 COMPILER V4.01,  MFUCFUNC                                                              19/08/04  14:33:23  PAGE 12  

 720                  //get the next UID part if we need to cascade
 721                  if((uid_len - uid_index) > 4)
 722                  {
 723                    //ok, we need to cascade the UID
 724                    memcpy(&tmpuid[1], &uid[uid_index], 3);
 725                    uid_index += 3;
 726                  }
 727                  else
 728                  {
 729                    //ah, how nice. no need to cascade
 730                    memcpy(tmpuid, &uid[uid_index], 4);
 731                    uid_index += 4;
 732                  }
 733          
 734                  status = Mf500PiccCascSelect(sel_code, tmpuid, sak);
 735          
 736                  if(status == MI_OK)
 737                  {
 738                    cascade_level++;
 739                  }
 740                }
 741                while((status == MI_OK )    // error occured
 742                      && (*sak & 0x04)       // no further cascade level
 743                      && ((uid_index + 1) < uid_len) // all bytes of snr sent
 744                      && (cascade_level < 3)); // highest cascade level reached
 745             }
 746             if ( status == MI_OK)
 747             {
 748                //Exit function, if UID length is not of expected length
 749                if ((uid_index) != uid_len)
 750                {
 751                   status =  MI_SERNRERR ;
 752                }
 753             }
 754             if (status == MI_OK)
 755             {
 756                //Exit function, if cascade level is triple and sak indicates another
 757                //cascase level.
 758                if ((cascade_level == 3) && (*sak & 0x04))
 759                {
 760                   status = MI_SERNRERR;
 761                }
 762             }
 763             return status;
 764          }
 765            */
 766          ///////////////////////////////////////////////////////////////////////
 767          //          M I F A R E      A U T H E N T I C A T I O N
 768          //   calling compatible version
 769          ///////////////////////////////////////////////////////////////////////
 770          char Mf500PiccAuth(unsigned char   key_type,    // PICC_AUTHENT1A or PICC_AUTHENT1B
 771                             unsigned char   key_addr,    // key address in reader storage
 772                             unsigned char   block)       // block number which should be
 773                                                        // authenticated
 774          {
 775   1         char    status = MI_OK;
 776   1      
 777   1         status = Mf500PiccAuthE2(  key_type,
 778   1                                    MLastSelectedSnr,
 779   1                                    key_addr,
 780   1                                    block);
 781   1         return status;
 782   1      }
 783          
 784          ///////////////////////////////////////////////////////////////////////
 785          //                  A U T H E N T I C A T I O N
C51 COMPILER V4.01,  MFUCFUNC                                                              19/08/04  14:33:23  PAGE 13  

 786          //             W I T H   K E Y S   F R O M   E 2 P R O M
 787          ///////////////////////////////////////////////////////////////////////
 788          char Mf500PiccAuthE2(   unsigned char   auth_mode,   // PICC_AUTHENT1A or PICC_AUTHENT1B
 789                               unsigned char   *snr,        // 4 bytes card serial number
 790                               unsigned char   key_sector,  // 0 <= key_sector <= 15//key_sector1 10.17
 791                               unsigned char   block)      //  0 <= block <= 256
 792          {
 793   1         char   status = MI_OK;
 794   1         // eeprom address calculation
 795   1         // 0x80 ... offset
 796   1         // key_sector ... sector
 797   1         // 0x18 ... 2 * 12 = 24 = 0x18
 798   1         //unsigned short  e2addr = 0x80 + key_sector * 0x18;
 799   1         //unsigned char    *e2addrbuf = (unsigned char   *)&e2addr;
 800   1         unsigned char   key_sector1=key_sector;
 801   1      
 802   1         unsigned int  e2addr = 0x80 + key_sector1 * 0x18;
 803   1         unsigned int  *e2addrbuf = &e2addr;
 804   1      
 805   1         PcdSetTmo(106);
 806   1         if (auth_mode == PICC_AUTHENT1B)
 807   1            e2addr += 12; // key B offset
 808   1         FlushFIFO();    // empty FIFO
 809   1         ResetInfo(MInfo);
 810   1         key_sector1=(unsigned char  )e2addr;
 811   1         MRcvBuffer[0]=key_sector1;//e2addr&0x0F;//////////////10.17
 812   1         MRcvBuffer[1]=e2addr>>8&0x0F;///////////10.17
 813   1      
 814   1         //memcpy(MRcvBuffer,e2addrbuf,2); // write low and high byte of address
 815   1         MInfo.nBytesToSend   = 2;
 816   1          // write load command
 817   1         if ((status=PcdSingleResponseCmd(PCD_LOADKEYE2,MRcvBuffer,MRcvBuffer,&MInfo)) == MI_OK)
 818   1         {
 819   2            // execute authentication
 820   2            status = Mf500PiccAuthState(auth_mode,snr,block);
 821   2         }
 822   1         return status;
 823   1      }
 824          
 825          ///////////////////////////////////////////////////////////////////////
 826          //                      C O D E   K E Y S
 827          ///////////////////////////////////////////////////////////////////////
 828          char Mf500HostCodeKey(  unsigned char    *uncoded, // 6 bytes key value uncoded
 829                               unsigned char    *coded)   // 12 bytes key value coded
 830          {
 831   1         char    status = MI_OK;
 832   1         unsigned char    cnt = 0;
 833   1         unsigned char    ln  = 0;     // low nibble
 834   1         unsigned char    hn  = 0;     // high nibble
 835   1      
 836   1         for (cnt = 0; cnt < 6; cnt++)//6//
 837   1         {
 838   2            ln = uncoded[cnt] & 0x0F;
 839   2            hn = uncoded[cnt] >> 4;
 840   2            coded[cnt * 2 + 1]     =  (~ln << 4) | ln;
 841   2            coded[cnt * 2 ] =  (~hn << 4) | hn;
 842   2      
 843   2         }
 844   1         return MI_OK;
 845   1      }
 846          
 847          ///////////////////////////////////////////////////////////////////////
 848          //                  A U T H E N T I C A T I O N
 849          //             W I T H   P R O V I D E D   K E Y S
 850          ///////////////////////////////////////////////////////////////////////
 851          char Mf500PiccAuthKey(  unsigned char   auth_mode,
C51 COMPILER V4.01,  MFUCFUNC                                                              19/08/04  14:33:23  PAGE 14  

 852                               unsigned char    *snr,
 853                               unsigned char    *keys,
 854                               unsigned char   block)
 855          {
 856   1         char    status     = MI_OK;
 857   1         unsigned char    i = 0;
 858   1      
 859   1         PcdSetTmo(106);
 860   1         FlushFIFO();    // empty FIFO
 861   1         ResetInfo(MInfo);
 862   1         memcpy(MRcvBuffer,keys,12);                  // write 12 bytes of the key
 863   1         MInfo.nBytesToSend = 12;
 864   1          // write load command
 865   1         if ((status=PcdSingleResponseCmd(PCD_LOADKEY,MRcvBuffer,MRcvBuffer,&MInfo)) == MI_OK)
 866   1         {
 867   2            // execute authentication
 868   2            status = Mf500PiccAuthState(auth_mode,snr,block);
 869   2         }
 870   1         return status;
 871   1      }
 872          
 873          ///////////////////////////////////////////////////////////////////////
 874          //        S T O R E   K E Y S   I N   E E P R O M
 875          ///////////////////////////////////////////////////////////////////////
 876          char Mf500PcdLoadKeyE2(unsigned char   key_type,
 877                                 unsigned char   sector,
 878                                 unsigned char    *uncoded_keys)
 879          {
 880   1      
 881   1         // eeprom address calculation
 882   1         // 0x80 ... offset
 883   1         // key_sector ... sector
 884   1         // 0x18 ... 2 * 12 = 24 = 0x18
 885   1         signed char    status = MI_OK;
 886   1         unsigned short   e2addr = 0x80 + sector * 0x18;
 887   1         unsigned char    coded_keys[12];
 888   1      
 889   1         if (key_type == PICC_AUTHENT1B)
 890   1            e2addr += 12; // key B offset
 891   1         if ((status = Mf500HostCodeKey(uncoded_keys,coded_keys)) == MI_OK)
 892   1            status = PcdWriteE2(  e2addr,12,coded_keys);//12
 893   1         return status;
 894   1      }
 895          
 896          ///////////////////////////////////////////////////////////////////////
 897          //        A U T H E N T I C A T I O N   S T A T E S
 898          ///////////////////////////////////////////////////////////////////////
 899          char Mf500PiccAuthState(   unsigned char   auth_mode,
 900                                  unsigned char    *snr,
 901                                  unsigned char   block)
 902          {
 903   1         char    status = MI_OK;
 904   1         unsigned char    i = 0;
 905   1         //unsigned char    Temp;
 906   1         //unsigned char    CodedKey[12]={0xBD,0xDE,0x6F,0x37,0x83,0x83};
 907   1      
 908   1         status = ReadRC(RegErrorFlag);    // read error flags of the previous
 909   1                                          // key load
 910   1         if (status != MI_OK)
 911   1         {
 912   2            if (status & 0x40)            // key error flag set
 913   2               status = MI_KEYERR;
 914   2            else
 915   2               status = MI_AUTHERR;       // generic authentication error
 916   2         }
 917   1         else
C51 COMPILER V4.01,  MFUCFUNC                                                              19/08/04  14:33:23  PAGE 15  

 918   1         {
 919   2            PcdSetTmo(106);
 920   2            MRcvBuffer[0] = auth_mode;        // write authentication command
 921   2      
 922   2            MRcvBuffer[1] = block;    // write block number for authentication
 923   2            memcpy(MRcvBuffer + 2,snr,4); // write 4 bytes card serial number
 924   2            ResetInfo(MInfo);
 925   2            MInfo.nBytesToSend = 6;
 926   2            if ((status = PcdSingleResponseCmd(PCD_AUTHENT1,
 927   2                                     MRcvBuffer,
 928   2                                     MRcvBuffer,
 929   2                                     &MInfo)) == MI_OK)
 930   2            {
 931   3               if (ReadRC(RegSecondaryStatus) & 0x07) // RxLastBits mu

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -