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

📄 m500auc.c

📁 RC500 MF1 非接触读卡器 开发板 资料
💻 C
📖 第 1 页 / 共 5 页
字号:
      {  
            
         if (ReadIO(RegSecondaryStatus) & 0x07) // Check RxLastBits for error
         {
            status = MI_BITCOUNTERR;
			printf("\nMI_Bitconterr");	
         }
         else
         {
            ResetInfo(MInfo);
            MInfo.nBytesToSend = 0;
            //printf("\nauthent2 Start ........");
            if ((status = M500PcdCmd(PCD_AUTHENT2,
                                     MSndBuffer,
                                     MRcvBuffer,
                                     &MInfo)) == MI_OK) 
            {  
               if ( ReadIO(RegControl) & 0x08 ) // Crypto1 activated
               {
                   status = MI_OK; printf("\nAUT2 ok 1crypto ative..%b02X", (block/4));
               }
               else
               {
                   status = MI_AUTHERR;
					printf("\nAuthent2 Error ");
               } 
            }
         }
      } 
    }
   return status;
}

///////////////////////////////////////////////////////////////////////
//          M I F A R E   R E A D   
///////////////////////////////////////////////////////////////////////
char M500PiccRead(  unsigned char addr,
                  unsigned char *_data)
{
   char  status = MI_OK;
   char  tmp    = 0;
   char bitsExpected;

   FlushFIFO();          // empty FIFO

       // long timeout 

   WriteIO(RegChannelRedundancy,0x0F); // RxCRC, TxCRC, Parity enable
   
   // ************* Cmd Sequence ********************************** 
   ResetInfo(MInfo);   
   MSndBuffer[0] = PICC_READ;   // read command code
   MSndBuffer[1] = addr;
   MInfo.nBytesToSend   = 2; 
   M500PcdSetTmo(4);  
   status = M500PcdCmd(PCD_TRANSCEIVE,
                    MSndBuffer,
                       MRcvBuffer,
                       &MInfo);
   
	if (status == MI_OK)
   {
      if (status != MI_NOTAGERR ) // no timeout occured
      {   
         
         if (MInfo.nBitsReceived == 8 && (ReadIO(RegDecoderControl) & 0x01))
         {
            // upper nibble should be equal to lower nibble, otherwise 
            // there is a coding error on card side.
            if ((MRcvBuffer[0] & 0x0f) == ((MRcvBuffer[0] >> 4) & 0x0f))
               bitsExpected = 8;
            else
            {
               status = MI_CODINGERR;
               bitsExpected = 0; // data is copied to output
            }
         }
         else
            bitsExpected = 4;  
         if (MInfo.nBitsReceived ==bitsExpected )  // NACK
         {
             MRcvBuffer[0] &= 0x0f;  // mask out upper nibble
             if ((MRcvBuffer[0] & 0x0a) == 0)
             {
                status = MI_NOTAUTHERR;
             }
             else
             {
                status = MI_CODEERR;
             }
          memcpy(_data,"0000000000000000",16); // in case of an error initialise 
         }                                   // data
      
         else if(MInfo.nBytesReceived !=16) //ACK
   
      {
         status = MI_BYTECOUNTERR;
         memcpy(_data,"0000000000000000",16);
      }
      else
      {  if (MInfo.nBytesReceived ==16)
          memcpy(_data,MRcvBuffer,16);
      }
    } 
  } 

   M500PcdSetTmo(3);               // short timeout   
   return status; 
}

///////////////////////////////////////////////////////////////////////
//          M I F A R E   W R I T E     
///////////////////////////////////////////////////////////////////////
char M500PiccWrite( unsigned char addr,
                  unsigned char *_data)
{
   char  status = MI_OK;

     // ************* Cmd Sequence ********************************** 
     ResetInfo(MInfo);   
     MSndBuffer[0] = PICC_WRITE;        // Write command code
     MSndBuffer[1] = addr;
     MInfo.nBytesToSend   = 2;
     status = M500PcdCmd(PCD_TRANSCEIVE,
                         MSndBuffer,
                         MRcvBuffer,
                         &MInfo);

     if (status != MI_NOTAGERR)   // no timeout error
     {
        if (MInfo.nBitsReceived != 4)   // 4 bits are necessary
        {
           status = MI_BITCOUNTERR;
        }
        else                     // 4 bit received
        {
           MRcvBuffer[0] &= 0x0f; // mask out upper nibble
           if ((MRcvBuffer[0] & 0x0a) == 0)
           {
              status = MI_NOTAUTHERR;                   
           }
           else
           {
              if (MRcvBuffer[0] == 0x0a)
              {
                 status = MI_OK;
              }
              else 
              {
                 status = MI_CODEERR;
              }
           }
        }
     }

     if ( status == MI_OK)
     {

        M500PcdSetTmo(3);     // long timeout 

        ResetInfo(MInfo);   
        memcpy(MSndBuffer,_data,16);
        MInfo.nBytesToSend   = 16;
        status = M500PcdCmd(PCD_TRANSCEIVE,
                            MSndBuffer,
                            MRcvBuffer,
                            &MInfo);
        
        if (status & 0x80)    // timeout occured
        {
           status = MI_NOTAGERR;
        }
        else
        {
         
           if (MInfo.nBitsReceived != 4)  // 4 bits are necessary
           {
              status = MI_BITCOUNTERR;
           }
           else                     // 4 bit received
           {
              MRcvBuffer[0] &= 0x0f; // mask out upper nibble
              if ((MRcvBuffer[0] & 0x0a) == 0)
              {
                 status = MI_WRITEERR;
              }
              else
              {
                 if (MRcvBuffer[0] == 0x0a)
                 {
                    status = MI_OK;
                 }
                 else 
                 {
                    status = MI_CODEERR;
                 }
              }     
           }
        }        
        M500PcdSetTmo(1);    // short timeout
     }
  return status;
}

///////////////////////////////////////////////////////////////////////
//                V A L U E   M A N I P U L A T I O N 
///////////////////////////////////////////////////////////////////////
char M500PiccValue(unsigned char dd_mode, 
                    unsigned char addr, 
                    unsigned char *value,
                    unsigned char trans_addr)
{
   char status = MI_OK;

   M500PcdSetTmo(1);    // short timeout
   // ************* Cmd Sequence ********************************** 
   ResetInfo(MInfo);   
   MSndBuffer[0] = dd_mode;        // Inc,Dec command code
   MSndBuffer[1] = addr;
   MInfo.nBytesToSend   = 2;
   status = M500PcdCmd(PCD_TRANSCEIVE,
                       MSndBuffer,
                       MRcvBuffer,
                       &MInfo);

   if (status != MI_NOTAGERR)   // no timeout error
   {
        if (MInfo.nBitsReceived != 4)   // 4 bits are necessary
        {
           status = MI_BITCOUNTERR;
        }
        else                     // 4 bit received
        {
           MRcvBuffer[0] &= 0x0f; // mask out upper nibble
           switch(MRcvBuffer[0])
           {
              case 0x00: 
                 status = MI_NOTAUTHERR;
                 break;
              case 0x0a:
                 status = MI_OK;
                 break;
              case 0x01:
                 status = MI_VALERR;
                 break;
              default:
                 status = MI_CODEERR;
                 break;
           }
        }
     }

     if ( status == MI_OK)
     {
        M500PcdSetTmo(3);     // long timeout 

        ResetInfo(MInfo);   
        memcpy(MSndBuffer,value,4);
        MInfo.nBytesToSend   = 4;
        status = M500PcdCmd(PCD_TRANSCEIVE,
                            MSndBuffer,
                            MRcvBuffer,
                            &MInfo);
        
        if (status == MI_OK)    // no timeout occured
        {
            if (MInfo.nBitsReceived != 4)   // 4 bits are necessary
            {
               status = MI_BITCOUNTERR;
            }
            else                     // 4 bit received
            {
               MRcvBuffer[0] &= 0x0f; // mask out upper nibble
               switch(MRcvBuffer[0])
               {
                  case 0x00: 
                     status = MI_NOTAUTHERR;
                     break;
                  case 0x01:
                     status = MI_VALERR;
                     break;
                  default:
                     status = MI_CODEERR;
                     break;
               }
            }
         }        
         else
         if (status == MI_NOTAGERR )
            status = MI_OK;  // no response after 4 byte value - 
                             // transfer command has to follow
     }
     if ( status == MI_OK)
     {
        ResetInfo(MInfo);   
        MSndBuffer[0] = PICC_TRANSFER;        // transfer command code
        MSndBuffer[1] = trans_addr;
        MInfo.nBytesToSend   = 2;
        status = M500PcdCmd(PCD_TRANSCEIVE,
                            MSndBuffer,
                            MRcvBuffer,
                            &MInfo);
        
        if (status != MI_NOTAGERR)    // timeout occured
        {
            if (MInfo.nBitsReceived != 4)   // 4 bits are necessary
            {
               status = MI_BITCOUNTERR;
            }
            else                     // 4 bit received
            {
               MRcvBuffer[0] &= 0x0f; // mask out upper nibble
               switch(MRcvBuffer[0])
               {
                  case 0x00: 
                     status = MI_NOTAUTHERR;
                     break;
                  case 0x0a:
                     status = MI_OK;
                     break;
                  case 0x01:
                     status = MI_VALERR;
                     break;
                  default:
                     status = MI_CODEERR;
                     break;
               }
            }
        }        
     }
   return status;
}

///////////////////////////////////////////////////////////////////////
//   V A L U E   M A N I P U L A T I O N   W I T H   B A C K U P
///////////////////////////////////////////////////////////////////////
char M500PiccValueDebit(unsigned char dd_mode, 
                         unsigned char addr, 
                         unsigned char *value)
{
   char status = MI_OK;
   
   M500PcdSetTmo(1);    // short timeout
   ResetInfo(MInfo);   
   MSndBuffer[0] = dd_mode;        // Inc,Dec command code
   MSndBuffer[1] = addr;
   MInf

⌨️ 快捷键说明

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