智能卡RC500读卡程序.docx
《智能卡RC500读卡程序.docx》由会员分享,可在线阅读,更多相关《智能卡RC500读卡程序.docx(31页珍藏版)》请在冰豆网上搜索。
智能卡RC500读卡程序
#include"MyDefine.c"
#include"MfErrNo.h"
#include"MfRc500R.h"
#include"MfRcuC.h"
#include"ExternVariableDef.c"
#defineD_data
externvoidDelay_10us(uchar);
//unsignedcharidataMLastSelectedSnr[5];
unsignedcharidataMRcvBuffer[16];
unsignedcharidataMSndBuffer[18];
staticMfCmdInfoidataMInfo;
externvoidDelay_ms(unsignedint);
bithaveset;
/**************************************************************************************\
*名称:
s_data*
*功能:
发送1字节HEX*
*参数:
*
***************************************************************************************/
unsignedcharMf500PcdConfig(void)
{
unsignedcharD_datastatus=MI_RESETERR;
unsignedintD_dataii=5000;
unsignedcharD_datast_data;
do
{
RST_RC500=1;//resetRC500
Delay_10us(200);//wait
RST_RC500=0;//clearresetpin
Delay_10us(200);//wait
st_data=ReadRC(RegPage);
if(st_data==0x80)break;
}while(--ii);
ii=5000;
do
{
WriteRC(RegPage,0x00);//Dummyaccessinordertodeterminethebus
st_data=ReadRC(RegCommand);
if(!
st_data)break;
}while(--ii);//configuration
//necessaryreadaccess
//afterfirstwriteaccess,thereturnedvalue
//shouldbezero==>interfacerecognized
if(st_data)
status=MI_INTERFACEERR;
else
status=0;
if(!
status)
{
XBYTE[RegClockQControl]=0x0;
XBYTE[RegClockQControl]=0x40;
Delay_10us(5);
ClearBitMask(RegClockQControl,0x40);//clearbitClkQCalibfor
XBYTE[RegBitPhase]=0xAd;
XBYTE[RegRxThreshold]=0xFF;
XBYTE[RegRxControl2]=01;
XBYTE[RegFIFOLevel]=0x1A;//initializeto26d
XBYTE[RegTimerControl]=0x02;//TStopRxEnd=0,TStopRxBeg=0,
XBYTE[RegIRqPinConfig]=0x3;//interruptactivelowenable
status=PcdRfReset
(1);//Rf-resetandenableoutputdriver
}
returnstatus;
}
unsignedcharMf500PiccRequest(unsignedcharreq_code,unsignedchar*atq)
{
unsignedcharD_datastatus=MI_OK;
PcdSetTmo
(2);
WriteRC(RegChannelRedundancy,0x03);//RxCRCandTxCRCdisable,parityenable
ClearBitMask(RegControl,0x08);//disablecrypto1unit
WriteRC(RegBitFraming,0x07);//setTxLastBitsto7
ResetInfo();
MSndBuffer[0]=req_code;
MInfo.nBytesToSend=1;
status=PcdSingleResponseCmd(PCD_TRANSCEIVE);
if(status)//erroroccured
atq[0]=0;
else
{
if(MInfo.nBitsReceived!
=16)//2bytesexpected
{
atq[0]=0;
status=MI_BITCOUNTERR;
}
else
{
status=MI_OK;
memcpy(atq,MRcvBuffer,2);
}
}
returnstatus;
}
unsignedcharMf500PiccCascSelect(unsignedcharselect_code,unsignedchar*snr,unsignedchar*sak)
{
unsignedcharD_datastatus=MI_OK;
//if(CardSortChar==102)//1102卡不需要
//return0;
PcdSetTmo
(2);
WriteRC(RegChannelRedundancy,0x0F);//RxCRC,TxCRC,Parityenable
ClearBitMask(RegControl,0x08);//disablecrypto1unit
ResetInfo();
MSndBuffer[0]=select_code;
MSndBuffer[1]=0x70;//numberofbytessend
memcpy(MSndBuffer+2,snr,4);
MSndBuffer[6]=MSndBuffer[2]^MSndBuffer[3]^MSndBuffer[4]^MSndBuffer[5];
MInfo.nBytesToSend=7;
status=PcdSingleResponseCmd(PCD_TRANSCEIVE);
sak[0]=0;
if(status==MI_OK)//notimeoutoccured
{
if(MInfo.nBitsReceived!
=8)//lastbyteisnotcomplete
status=MI_BITCOUNTERR;
else
{
sak[0]=MRcvBuffer[0];
//memcpy(MLastSelectedSnr,snr,4);
}
}
returnstatus;
}
unsignedcharMf500PiccAnticoll(unsignedcharbcnt,unsignedchar*snr)
{
returnMf500PiccCascAnticoll(0x93,bcnt,snr);//firstcascadelevel
}
unsignedcharMf500PiccCascAnticoll(unsignedcharselect_code,unsignedcharbcnt,unsignedchar*snr)
{
unsignedcharD_datastatus=MI_OK;
unsignedcharD_datasnr_in[4];//copyoftheinputparametersnr
unsignedcharD_datanbytes=0;//howmanybytesreceived
unsignedcharD_datanbits=0;//howmanybitsreceived
unsignedcharD_datacomplete=0;//completesnrrecived
unsignedcharD_datai=0;
unsignedcharD_databyteOffset=0;
unsignedcharD_datasnr_crc;//checkbytecalculation
unsignedcharD_datasnr_check;
unsignedcharD_datadummyShift1;//dummybyteforsnrshift
unsignedcharD_datadummyShift2;//dummybyteforsnrshift
//if(CardSortChar==102)//1102卡不需要
//return0;
PcdSetTmo
(2);
memcpy(snr_in,snr,4);
WriteRC(RegDecoderControl,0x28);//ZeroAfterCollaktivieren
ClearBitMask(RegControl,0x08);//disablecrypto1unit
complete=0;
while(!
complete&&(status==MI_OK))
{
ResetInfo();
WriteRC(RegChannelRedundancy,0x03);//RxCRCandTxCRCdisable,parityenable
nbits=bcnt%8;
if(nbits)
{
WriteRC(RegBitFraming,nbits<<4|nbits);//TxLastBits/RxAlignaufnb_bi
nbytes=bcnt/8+1;
if(nbits==7)
{
MInfo.cmd=PICC_ANTICOLL1;//passcommandflagtoISR
WriteRC(RegBitFraming,nbits);//resetRxAligntozero
}
}
else
nbytes=bcnt/8;
MSndBuffer[0]=select_code;
MSndBuffer[1]=0x20+((bcnt/8)<<4)+nbits;//numberofbytessend
memcpy(MSndBuffer+2,snr_in,nbytes);
MInfo.nBytesToSend=2+nbytes;
status=PcdSingleResponseCmd(PCD_TRANSCEIVE);
//inordertosolveaninconsistancyintheanticollisionsequence
//(willbesolvedsoon),thecaseof7bitshastobetreatedina
//separateway
if(nbits==7)
{
//reorderreceivedbits
dummyShift1=0x00;
for(i=0;i{
dummyShift2=MRcvBuffer[i];
MRcvBuffer[i]=(dummyShift1>>(i+1))|(MRcvBuffer[i]<<(7-i));
dummyShift1=dummyShift2;
}
MInfo.nBitsReceived-=MInfo.nBytesReceived;//subtractreceivedparitybits
//recalculationofcollisionposition
if(MInfo.collPos)MInfo.collPos+=7-(MInfo.collPos+6)/9;
}
if(status==MI_OK||status==MI_COLLERR)//nootheroccured
{
//ResponseProcessing
if(MInfo.nBitsReceived!
=40)//not5bytesanswered
status=MI_BITCOUNTERR;
else
{
byteOffset=0;
if(nbits!
=0)//lastbytewasnotcomplete
{
snr_in[nbytes-1]=snr_in[nbytes-1]|MRcvBuffer[0];
byteOffset=1;
}
for(i=0;i<(4-nbytes);i++)
snr_in[nbytes+i]=MRcvBuffer[i+byteOffset];
if(status!
=MI_COLLERR)//noerrorandnocollision
{
//SerChcheck
snr_crc=snr_in[0]^snr_in[1]^snr_in[2]^snr_in[3];
snr_check=MRcvBuffer[4];
if(snr_crc!
=snr_check)
status=MI_SERNRERR;
else
complete=1;
}
else//collisionoccured
{
bcnt=bcnt+MInfo.collPos-nbits;
status=MI_OK;
}
}
}
}
if(status==MI_OK)
memcpy(snr,MRcvBuffer,4);
ClearBitMask(RegDecoderControl,0x20);//ZeroAfterColldisable
returnstatus;
}
voidSetBitMask(unsignedintreg,unsignedcharmask)
{
XBYTE[reg]|=mask;
}//clearbitmask
voidClearBitMask(unsignedintreg,unsignedcharmask)
{
XBYTE[reg]&=~mask;
}//clearbitmask
voidFlushFIFO(void)
{
SetBitMask(RegControl,0x01);
}
voidWriteRC(unsignedintAddress,unsignedcharvalue)
{
XBYTE[Address]=value;
}//writevalueatthespecified
unsignedcharReadRC(unsignedintAddress)
{
returnXBYTE[Address];
}//readvalueatthespecified
unsignedcharPcdRfReset(unsignedintnms)
{
unsignedcharD_datastatus=MI_OK;
ClearBitMask(RegTxControl,0x03);//Tx2RF-En,Tx1RF-Endisablen
if(nms>0)
{
nms=1000*nms;
Delay_10us(nms);
SetBitMask(RegTxControl,0x03);
}
returnstatus;
}
voidPcdSetTmo(unsignedchartmoLength)
{
switch(tmoLength)
{//timerclockfrequency13,56MHz
case1:
//shorttimeout(1,0ms)
WriteRC(RegTimerClock,0x07);//TAutoRestart=0,TPrescale=128
WriteRC(RegTimerReload,0x6a);//TReloadVal='h6a=106(dec)
break;
case2:
//mediumtimeout(1,5ms)
WriteRC(RegTimerClock,0x07);//TAutoRestart=0,TPrescale=128
WriteRC(RegTimerReload,0xa0);//TReloadVal='ha0=160(dec)
break;
case3:
//longtimeout(6ms)
WriteRC(RegTimerClock,0x09);//TAutoRestart=0,TPrescale=4*128
WriteRC(RegTimerReload,0xa0);//TReloadVal='ha0=160(dec)
break;
case4:
//longtimeout(19.2ms)
WriteRC(RegTimerClock,0x0a);//TAutoRestart=0,TPrescale=4*128
WriteRC(RegTimerReload,0xff);//TReloadVal='ff=255(dec)
break;
case5:
//longtimeout(38.4ms)
WriteRC(RegTimerClock,0x0b);//TAutoRestart=0,TPrescale=4*4*128
WriteRC(RegTimerReload,0xff);//TReloadVal='ff=255(dec)
break;
case6:
//longtimeout(76.8ms)
WriteRC(RegTimerClock,0x0c);//TAutoRestart=0,TPrescale=2*4*4*128
WriteRC(RegTimerReload,0xff);//TReloadVal='ff=255(dec)
break;
case7:
//longtimeout(153.6ms)
WriteRC(RegTimerClock,0x0d);//TAutoRestart=0,TPrescale=2*2*4*4*128
WriteRC(RegTimerReload,0xff);//TReloadVal='ff=255(dec)
break;
default:
//shorttimeout(1,0ms)
WriteRC(RegTimerClock,0x07);//TAutoRestart=0,TPrescale=128
WriteRC(RegTimerReload,tmoLength);//TReloadVal=tmoLength
break;
}
}
voidResetInfo(void)
{
MInfo.cmd=0;
MInfo.status=MI_OK;
MInfo.irqSource=0;
MInfo.nBytesSent=0;
MInfo.nBytesToSend=0;
MInfo.nBytesReceived=0;
MInfo.nBitsReceived=0;
MInfo.collPos=0;
}
unsignedcharPcdSingleResponseCmd(unsignedcharcmd)
{
unsignedcharD_datastatus=MI_OK;
unsignedcharD_datatmpStatus;
unsignedcharD_datalastBits;
unsignedcharD_datairqEn=0x00;
unsignedcharD_datawaitFor=0x00;
unsignedcharD_datatimerCtl=0x00;
unsignedintD_dataii=50000;
WriteRC(RegInterruptEn,0x7F);//disableallinterrupts
WriteRC(RegInterruptRq,0x7F);//resetinterruptrequests
WriteRC(RegCommand,PCD_IDLE);//terminateprobablyrunningcommand
FlushFIFO();//flushFIFObuffer
haveset=1;
//initialisingtheISR-Functionpointerformifare
//protocol-dothisafterinitialisingtheMpXXXXvariables
MInfo.irqSource=0x0;//resetinterruptflags
//dependingonthecommandcode,appropria