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

📄 rc500.c

📁 RC500的全部操作,读卡
💻 C
📖 第 1 页 / 共 3 页
字号:
        {
            status = MI_BITCOUNTERR;
        }
        else                    
        {
            SerBuffer[0] &= 0x0f; 
            switch(SerBuffer[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);    
        ResetInfo(MInfo);   
        memcpy(SerBuffer,value,4);
        MInfo.nBytesToSend   = 4;
        status = M500PcdCmd(PCD_TRANSCEIVE,
                            SerBuffer,
                            &MInfo);
        
        if (status == MI_OK)    
        {
            if (MInfo.nBitsReceived != 4)   
            {
                status = MI_BITCOUNTERR;
            }
            else                    
            {
                SerBuffer[0] &= 0x0f; 
                switch(SerBuffer[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;  
        }
    }
    if (status == MI_OK)
    {
        ResetInfo(MInfo);   
        SerBuffer[0] = PICC_TRANSFER;      
        SerBuffer[1] = trans_addr;
        MInfo.nBytesToSend   = 2;
        status = M500PcdCmd(PCD_TRANSCEIVE,
                            SerBuffer,
                            &MInfo);
        if (status != MI_NOTAGERR)  
        {
            if (MInfo.nBitsReceived != 4)
            {
                status = MI_BITCOUNTERR;
            }
            else                   
            {
                SerBuffer[0] &= 0x0f; 
                switch(SerBuffer[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;
}*/

///////////////////////////////////////////////////////////////////////
// Set card in HALT-state
// 终止卡的操作
///////////////////////////////////////////////////////////////////////
char M500PiccHalt(void)
{
    char idata status = MI_CODEERR;

    // ************* Cmd Sequence ********************************** 
    ResetInfo(MInfo);   
    SerBuffer[0] = PICC_HALT ;      // Halt command code
    SerBuffer[1] = 0x00;            // dummy address
    MInfo.nBytesToSend = 2;
    status = M500PcdCmd(PCD_TRANSCEIVE,
                       SerBuffer,
                       &MInfo);   
    if (status)
    {
        // timeout error ==> no NAK received ==> OK
        if (status == MI_NOTAGERR || status == MI_ACCESSTIMEOUT)
        {
            status = MI_OK;
        }
    }
    //reset command register - no response from tag
    WriteIO(RegCommand,PCD_IDLE);	//启动和停止命令的执行
    return status; 
}

///////////////////////////////////////////////////////////////////////
// Reset the MF RC500
//重启RC500 
///////////////////////////////////////////////////////////////////////
char M500PcdReset(void)
{
    char idata status = MI_OK;
    unsigned int idata timecnt=0;
    
    RC500RST = 0;  
    delay_ms(25); 
    RC500RST = 1;  
    delay_ms(10); 
    RC500RST = 0;  
    delay_ms(10);
    timecnt=1000;
    while ((ReadIO(RegCommand) & 0x3F) && timecnt--);
    if(!timecnt)
    {
        status = MI_RESETERR;
    }
    if (status == MI_OK)
    {
        //WriteIO(RegPage,0x80);
        if (ReadIO(RegCommand) != 0x00)
        {                           
            status = MI_INTERFACEERR;
        }
    }
    return status;
}

///////////////////////////////////////////////////////////////////////
// Configures the MF RC500
///////////////////////////////////////////////////////////////////////
char M500PcdConfig(void)
{
    char idata status;

    if ((status = M500PcdReset()) == MI_OK)
    {
        WriteIO(RegClockQControl,0x00);
        WriteIO(RegClockQControl,0x40);
        delay_100us();  
        ClearBitMask(RegClockQControl,0x40);
        WriteIO(RegBitPhase,0xAD);      
        WriteIO(RegRxThreshold,0xFF);   
        WriteIO(RegRxControl2,0x01);
        WriteIO(RegFIFOLevel,0x1A);
        WriteIO(RegTimerControl,0x02);
        WriteIO(RegIRqPinConfig,0x03); 
        M500PcdRfReset(1);               
    }
    return status;
}

///////////////////////////////////////////////////////////////
// Key loading into the MF RC500's EEPROM
//将一个密匙从E2PROM 复制到密匙缓冲区
///////////////////////////////////////////////////////////////		            
/*char M500PcdLoadKeyE2(unsigned char key_type,
                       unsigned char sector,
                       unsigned char *uncoded_keys)
{
    signed char status = MI_OK;
    unsigned int e2addr = 0x80 + sector * 0x18;
    unsigned char coded_keys[12];

    if (key_type == PICC_AUTHENT1B)
    {
        e2addr += 12;           // key B offset
    }
    if ((status = M500HostCodeKey(uncoded_keys,coded_keys)) == MI_OK)
    {
        status = PcdWriteE2(e2addr,12,coded_keys);
    }
    return status;
}*/

///////////////////////////////////////////////////////////////////////
// Write the MF RC500's EEPROM
//从FIFO 缓冲区获得数据并写入内部 E2PROM
///////////////////////////////////////////////////////////////////////
/*char PcdWriteE2(unsigned int startaddr,
                unsigned char length,
                unsigned char* _data)
{
    char status = MI_OK;
    ResetInfo(MInfo);   
    SerBuffer[0] = startaddr & 0xFF;
    SerBuffer[1] = (startaddr >> 8) & 0xFF;
    memcpy(SerBuffer + 2,_data,length);

    MInfo.nBytesToSend   = length + 2;
         
    status = M500PcdCmd(PCD_WRITEE2,
                       SerBuffer,
                       &MInfo); 
    return status;
} */  

///////////////////////////////////////////////////////////////////////
// Select Command defined in ISO14443(MIFARE)
///////////////////////////////////////////////////////////////////////
char M500PcdMfOutSelect(unsigned char type)
{
    WriteIO(RegMfOutSelect,type&0x7);
    return MI_OK;
}

///////////////////////////////////////////////////////////////////////
// Request Command defined in ISO14443(MIFARE)
// Request,Anticoll,Select,return CardType(2 bytes)+CardSerialNo(4 bytes)    
// 寻卡,防冲突,选择卡    返回卡类型(2 bytes)+ 卡系列号(4 bytes)
///////////////////////////////////////////////////////////////////////
char M500PiccCommonRequest(unsigned char req_code,unsigned char *atq)
{
    char idata status = MI_OK;
	
    M500PcdSetTmo(3);
    WriteIO(RegChannelRedundancy,0x03);
    ClearBitMask(RegControl,0x08);
    WriteIO(RegBitFraming,0x07);     
    SetBitMask(RegTxControl,0x03);     
    ResetInfo(MInfo);
    SerBuffer[0] = req_code;
    MInfo.nBytesToSend = 1;   
    
    status = M500PcdCmd(PCD_TRANSCEIVE,SerBuffer,&MInfo);
    if (status)     
    {
        *atq = 0;
    } 
    else 
    {
        if (MInfo.nBitsReceived != 16) 
        {
            *atq = 0;
            status = MI_BITCOUNTERR;
        } 
        else 
        {
            status = MI_OK;
            memcpy(atq,SerBuffer,2);
        }
    }
    return status; 
}

///////////////////////////////////////////////////////////////////
// Cascaded Anti-Collision Command defined in ISO14443(MIFARE)
// 防冲突 读卡的系列号 MLastSelectedSnr
///////////////////////////////////////////////////////////////////
char M500PiccCascAnticoll (unsigned char bcnt,unsigned char *snr)       
{
    char idata status = MI_OK;
    char idata snr_in[4];    
    char idata nbytes = 0;
    char idata nbits = 0;
    char idata complete = 0;
    char idata i        = 0;
    char idata byteOffset = 0;
    unsigned char dummyShift1;       
    unsigned char dummyShift2;      
    
    M500PcdSetTmo(106);
    memcpy(snr_in,snr,4);   
   
    WriteIO(RegDecoderControl,0x28); 
    ClearBitMask(RegControl,0x08);   
    complete = 0;
    while (!complete && (status == MI_OK) )
    {
        ResetInfo(MInfo);           
        WriteIO(RegChannelRedundancy,0x03);
        nbits = bcnt % 8;  
        if(nbits)
        {
            WriteIO(RegBitFraming,nbits << 4 | nbits); 
            nbytes = bcnt / 8 + 1;   
            if (nbits == 7)
            {
                MInfo.cmd = PICC_ANTICOLL1;  
                WriteIO(RegBitFraming,nbits); 
            }
        }  
        else
        {
            nbytes = bcnt / 8;
        }
        SerBuffer[0] = 0x93;
        SerBuffer[1] = 0x20 + ((bcnt/8) << 4) + nbits;
               
        for (i = 0; i < nbytes; i++)  
        {
            SerBuffer[i + 2] = snr_in[i];
        }
        MInfo.nBytesToSend   = 2 + nbytes;   
 
        status = M500PcdCmd(PCD_TRANSCEIVE,SerBuffer,&MInfo);
        if (nbits == 7)
        {
            dummyShift1 = 0x00;
            for (i = 0; i < MInfo.nBytesReceived; i++)
            {
                dummyShift2 = SerBuffer[i];
                SerBuffer[i] = (dummyShift1 >> (i+1)) | (SerBuffer[i] << (7-i));
                dummyShift1 = dummyShift2;
            }
            MInfo.nBitsReceived -= MInfo.nBytesReceived;       
            if ( MInfo.collPos ) MInfo.collPos += 7 - (MInfo.collPos + 6) / 9;
        }
        if ( status == MI_OK || status == MI_COLLERR)    
        {
            if ( MInfo.nBitsReceived != (40 - bcnt) )
            {
                status = MI_BITCOUNTERR; 
            } 
            else 
            {
                byteOffset = 0;
                if( nbits != 0 ) 
                {
                    snr_in[nbytes - 1] = snr_in[nbytes - 1] | SerBuffer[0];
                    byteOffset = 1;
                }

                for ( i =0; i < (4 - nbytes); i++)     
                {
                    snr_in[nbytes + i] = SerBuffer[i + byteOffset];
                }
  
                if (status != MI_COLLERR ) 
                {
                    dummyShift2 = snr_in[0] ^ snr_in[1] ^ snr_in[2] ^ snr_in[3];
                    dummyShift1 = SerBuffer[MInfo.nBytesReceived - 1];
                    if (dummyShift2 != dummyShift1)
                    {
                        status = MI_SERNRERR;
                    } 
                    else   
                    {
                        complete = 1;
                    }
                }
                else                 
                {
                    bcnt = bcnt + MInfo.collPos - nbits;
                    status = MI_OK;
                }
            }
        }
    }
    if (status == MI_OK)
    {
        memcpy(snr,snr_in,4);
    }
    else
    {
        memcpy(snr,"0000",4);
    }
    ClearBitMask(RegDecoderControl,0x20); 
   
    return status;  
}

//////////////////////////////////////////////////////////////////
// Cascaded Select command defined in ISO14443(MIFARE)
// 选择卡 Select Card
//////////////////////////////////////////////////////////////////
char M500PiccCascSelect(unsigned char *snr,unsigned char *sak)
{
    char idata status = MI_OK; 
 
    M500PcdSetTmo(106);
	

⌨️ 快捷键说明

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