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

📄 iso14443a.lst

📁 读ISO14443 CPU卡源代码
💻 LST
📖 第 1 页 / 共 3 页
字号:
C51 COMPILER V8.05a   ISO14443A                                                            07/05/2010 23:34:45 PAGE 1   


C51 COMPILER V8.05a, COMPILATION OF MODULE ISO14443A
OBJECT MODULE PLACED IN ISO14443A.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE ISO14443A.C ROM(COMPACT) BROWSE DEBUG OBJECTEXTEND

line level    source

   1          #include "reg52.h"
   2          #include "string.h"
   3          #include "main.h"
   4          #include "slrc632.h" 
   5          #include "iso14443a.h"
   6          #define FSD 64              //RC500 FIFO BUFFER SIZE
   7          extern bit g_bIblock;
   8          
   9          
  10          
  11          extern struct TranSciveBuffer{unsigned char MfCommand;
  12                                        unsigned int  MfLength;
  13                                        unsigned char MfData[64];
  14                                       };
  15                                        
  16          /////////////////////////////////////////////////////////////////////
  17          //功    能:寻卡
  18          //参数说明: req_code[IN]:寻卡方式
  19          //                0x52 = 寻感应区内所有符合14443A标准的卡
  20          //                0x26 = 寻未进入休眠状态的卡
  21          //         pTagType[OUT]:卡片类型代码
  22          //                0x4400 = Mifare_UltraLight
  23          //                0x0400 = Mifare_One(S50)
  24          //                0x0200 = Mifare_One(S70)
  25          //                0x0800 = Mifare_Pro
  26          //                0x0403 = Mifare_ProX
  27          //                0x4403 = Mifare_DESFire
  28          //返    回: 成功返回MI_OK
  29          /////////////////////////////////////////////////////////////////////
  30          char PcdRequest(unsigned char req_code,unsigned char *pTagType)
  31          {
  32   1         char status;   
  33   1         struct TranSciveBuffer MfComData;
  34   1         struct TranSciveBuffer *pi;
  35   1         pi = &MfComData;
  36   1      
  37   1         WriteRawRC(RegChannelRedundancy,0x03);
  38   1         ClearBitMask(RegControl,0x08);
  39   1         WriteRawRC(RegBitFraming,0x07);
  40   1         SetBitMask(RegTxControl,0x03);
  41   1         PcdSetTmo(4);
  42   1         MfComData.MfCommand = PCD_TRANSCEIVE;
  43   1         MfComData.MfLength  = 1;
  44   1         MfComData.MfData[0] = req_code;
  45   1      
  46   1         status = PcdComTransceive(pi);
  47   1         
  48   1         if (!status)
  49   1         {    
  50   2              if (MfComData.MfLength != 0x10)
  51   2              {   status = MI_BITCOUNTERR;   }
  52   2         }
  53   1         *pTagType     = MfComData.MfData[0];
  54   1         *(pTagType+1) = MfComData.MfData[1];
  55   1         
C51 COMPILER V8.05a   ISO14443A                                                            07/05/2010 23:34:45 PAGE 2   

  56   1         return status;
  57   1      }
  58          
  59          /////////////////////////////////////////////////////////////////////
  60          //防冲撞
  61          //input: g_cSNR=存放序列号(4byte)的内存单元首地址
  62          //output:status=MI_OK:成功
  63          //       得到的序列号放入指定单元
  64          /////////////////////////////////////////////////////////////////////
  65          char PcdAnticoll(unsigned char *pSnr)
  66          {
  67   1          char status ;
  68   1          unsigned char i;
  69   1          unsigned char ucBits;
  70   1          unsigned char ucBytes;
  71   1          unsigned char snr_check = 0;
  72   1          unsigned char ucCollPosition = 0;
  73   1          unsigned char ucTemp;
  74   1          unsigned char idata ucSNR[5] = {0, 0, 0, 0 ,0};
  75   1          struct TranSciveBuffer MfComData;
  76   1          struct TranSciveBuffer *pi;
  77   1          pi = &MfComData;
  78   1      
  79   1          WriteRawRC(RegDecoderControl,0x28);
  80   1          ClearBitMask(RegControl,0x08);
  81   1          WriteRawRC(RegChannelRedundancy,0x03);
  82   1          PcdSetTmo(4);
  83   1          
  84   1      
  85   1          do
  86   1          {
  87   2              ucBits = (ucCollPosition) % 8;
  88   2              if (ucBits != 0)
  89   2              {
  90   3                   ucBytes = ucCollPosition / 8 + 1;
  91   3                   WriteRawRC(RegBitFraming, (ucBits << 4) + ucBits);
  92   3              }
  93   2              else
  94   2              {
  95   3                   ucBytes = ucCollPosition / 8;
  96   3              }
  97   2              
  98   2              MfComData.MfCommand = PCD_TRANSCEIVE;
  99   2              MfComData.MfData[0] = PICC_ANTICOLL1;
 100   2              MfComData.MfData[1] = 0x20 + ((ucCollPosition / 8) << 4) + (ucBits & 0x0F);
 101   2              for (i=0; i<ucBytes; i++)
 102   2                  {
 103   3                      MfComData.MfData[i + 2] = ucSNR[i];
 104   3                  }
 105   2                  MfComData.MfLength = ucBytes + 2;
 106   2              
 107   2                  status = PcdComTransceive(pi);
 108   2              
 109   2                  ucTemp = ucSNR[(ucCollPosition / 8)];
 110   2                  if (status == MI_COLLERR)
 111   2                  {
 112   3                      for (i=0; i < 5 - (ucCollPosition / 8); i++)
 113   3                      {
 114   4                               ucSNR[i + (ucCollPosition / 8)] = MfComData.MfData[i+1];
 115   4                      }
 116   3                      ucSNR[(ucCollPosition / 8)] |= ucTemp;
 117   3                      ucCollPosition = MfComData.MfData[0];
C51 COMPILER V8.05a   ISO14443A                                                            07/05/2010 23:34:45 PAGE 3   

 118   3              }
 119   2              else if (status == MI_OK)
 120   2              {
 121   3                  for (i=0; i < (MfComData.MfLength / 8); i++)
 122   3                  {
 123   4                       ucSNR[4 - i] = MfComData.MfData[MfComData.MfLength/8 - i - 1];
 124   4                  }
 125   3                  ucSNR[(ucCollPosition / 8)] |= ucTemp;
 126   3              }
 127   2          } while (status == MI_COLLERR);
 128   1                              
 129   1                              
 130   1          if (status == MI_OK)
 131   1          {
 132   2               for (i=0; i<4; i++)
 133   2               {   
 134   3                   *(pSnr+i)  = ucSNR[i];
 135   3                   snr_check ^= ucSNR[i];
 136   3               }
 137   2               if (snr_check != ucSNR[i])
 138   2               {   status = MI_COM_ERR;    }
 139   2          }
 140   1          
 141   1          ClearBitMask(RegDecoderControl,0x20);
 142   1          return status;
 143   1      }
 144          
 145          /////////////////////////////////////////////////////////////////////
 146          //选定一张卡
 147          //input:g_cSNR=序列号
 148          /////////////////////////////////////////////////////////////////////
 149          char PcdSelect(unsigned char *pSnr,unsigned char *pSize)
 150          {
 151   1          unsigned char i;
 152   1          char status;
 153   1          unsigned char snr_check = 0;
 154   1          struct TranSciveBuffer MfComData;
 155   1          struct TranSciveBuffer *pi;
 156   1          pi = &MfComData;
 157   1      
 158   1          WriteRawRC(RegChannelRedundancy,0x0F);
 159   1          ClearBitMask(RegControl,0x08);
 160   1          PcdSetTmo(4);
 161   1          
 162   1          MfComData.MfCommand = PCD_TRANSCEIVE;
 163   1          MfComData.MfLength  = 7;
 164   1          MfComData.MfData[0] = PICC_ANTICOLL1;
 165   1          MfComData.MfData[1] = 0x70;
 166   1          for (i=0; i<4; i++)
 167   1          {
 168   2              snr_check ^= *(pSnr+i);
 169   2              MfComData.MfData[i+2] = *(pSnr+i);
 170   2          }
 171   1          MfComData.MfData[6] = snr_check;
 172   1      
 173   1          status = PcdComTransceive(pi);
 174   1          
 175   1          if (status == MI_OK)
 176   1          {
 177   2              if (MfComData.MfLength != 0x8)
 178   2              {   status = MI_BITCOUNTERR;   }
 179   2              else
C51 COMPILER V8.05a   ISO14443A                                                            07/05/2010 23:34:45 PAGE 4   

 180   2              {  *pSize = MfComData.MfData[0];  }
 181   2          }
 182   1      
 183   1          return status;
 184   1      }
 185          
 186          /////////////////////////////////////////////////////////////////////
 187          //将Mifare_One卡密钥转换为RC500接收格式
 188          //input: uncoded=6字节未转换的密钥
 189          //output:coded=12字节转换后的密钥
 190          /////////////////////////////////////////////////////////////////////
 191          char ChangeCodeKey(unsigned char *pUncoded,unsigned char *pCoded)
 192          {
 193   1         unsigned char cnt=0;
 194   1         unsigned char ln=0;
 195   1         unsigned char hn=0;
 196   1      
 197   1         for (cnt=0; cnt<6; cnt++)
 198   1         {
 199   2            ln = pUncoded[cnt] & 0x0F;
 200   2            hn = pUncoded[cnt] >> 4;
 201   2            pCoded[cnt*2+1] = (~ln<<4) | ln;
 202   2            pCoded[cnt*2]   = (~hn<<4) | hn;
 203   2         }
 204   1         return MI_OK;
 205   1      }
 206          
 207          /*
 208          /////////////////////////////////////////////////////////////////////
 209          //将存在RC500的EEPROM中的密钥匙调入RC500的FIFO
 210          //input: startaddr=EEPROM地址
 211          /////////////////////////////////////////////////////////////////////
 212          char PcdLoadKeyE2(unsigned int startaddr)
 213          {
 214              char status;
 215              struct TranSciveBuffer MfComData;
 216              struct TranSciveBuffer *pi;
 217              pi = &MfComData;
 218          
 219              MfComData.MfCommand = PCD_LOADKEYE2;
 220              MfComData.MfLength  = 2;
 221              MfComData.MfData[0] = startaddr & 0xFF;
 222              MfComData.MfData[1] = (startaddr >> 8) & 0xFF;
 223              
 224              status = PcdComTransceive(pi);
 225              
 226              return status;
 227          }
 228          */
 229          
 230          
 231          /////////////////////////////////////////////////////////////////////
 232          //功能:将已转换格式后的密钥送到RC500的FIFO中
 233          //input:keys=密钥
 234          /////////////////////////////////////////////////////////////////////
 235          char PcdAuthKey(unsigned char *pKeys)
 236          {
 237   1          char status;
 238   1          struct TranSciveBuffer MfComData;
 239   1          struct TranSciveBuffer *pi;
 240   1          pi = &MfComData;
 241   1      
C51 COMPILER V8.05a   ISO14443A                                                            07/05/2010 23:34:45 PAGE 5   

 242   1          PcdSetTmo(4);
 243   1          MfComData.MfCommand = PCD_LOADKEY;
 244   1          MfComData.MfLength  = 12;
 245   1          memcpy(&MfComData.MfData[0], pKeys, 12);    
 246   1      
 247   1          status = PcdComTransceive(pi);
 248   1      
 249   1          return status;
 250   1      }
 251          
 252          /////////////////////////////////////////////////////////////////////
 253          //功能:用存放RC500的FIFO中的密钥和卡上的密钥进行验证
 254          //input:auth_mode=验证方式,0x60:验证A密钥,0x61:验证B密钥
 255          //      block=要验证的绝对块号
 256          //      g_cSNR=序列号首地址
 257          /////////////////////////////////////////////////////////////////////
 258          char PcdAuthState(unsigned char auth_mode,unsigned char block,unsigned char *pSnr)
 259          {
 260   1          char status;
 261   1          struct TranSciveBuffer MfComData;
 262   1          struct TranSciveBuffer *pi;
 263   1          pi = &MfComData;
 264   1      
 265   1          WriteRawRC(RegChannelRedundancy,0x0F);
 266   1          PcdSetTmo(4);
 267   1          MfComData.MfCommand = PCD_AUTHENT1;
 268   1          MfComData.MfLength  = 6;
 269   1          MfComData.MfData[0] = auth_mode;

⌨️ 快捷键说明

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