#include
#include
#include
#include
#include
#include
#define uchar unsigned char
#define uint unsigned int
#define GetRegPage(addr)(0x80 | (addr>>3))
idata struct TranSciveBuffer{uchar MfCommand;
uchar MfLength;
uchar MfData[19];
};
void delay(uchar t)
{
uchar i,j;
for(i=0;ifor(j=0;j<110;j++);
}
void WriteRawIO(unsigned char Address,unsigned char value)
{
XBYTE[Address]=value;
}
unsigned char ReadRawIO(unsigned char Address)
{
return XBYTE[Address];
}
void WriteRC(unsigned char Address, unsigned char value)
{
WriteRawIO(0x00,GetRegPage(Address));
WriteRawIO(Address,value);
}
unsigned char ReadRC(unsigned char Address)
{
WriteRawIO(0x00,GetRegPage(Address));
return ReadRawIO(Address);
}
void ClearBitMask(uchar reg,uchar mask)
{
char tmp=0x0;
tmp = ReadRC(reg);
WriteRC(reg,tmp & ~mask);
}
void SetBitMask(uchar reg,uchar mask)
{
char tmp=0x0;
tmp=ReadRC(reg);
WriteRC(reg,tmp|mask);
}
void FlushFIFO(void)
{
SetBitMask(RegControl,0x01);
}
void PcdAntennaOff()
{
ClearBitMask(RegTxControl,0x03);
}
void PcdAntennaOn()
{
SetBitMask(RegTxControl,0x03);
}
void PcdSetTmo(unsigned char tmoLength)
{
switch(tmoLength)
{
case 1:
WriteRC(RegTimerClock,0x07);
WriteRC(RegTimerReload,0x6a);
break;
case 2:
WriteRC(RegTimerClock,0x07);
WriteRC(RegTimerReload,0xa0);
break;
case 3:
WriteRC(RegTimerClock,0x09);
WriteRC(RegTimerReload,0xa0);
break;
case 4:
WriteRC(RegTimerClock,0x09);
WriteRC(RegTimerReload,0xff);
break;
case 5:
WriteRC(RegTimerClock,0x0b);
WriteRC(RegTimerReload,0xff);
break;
case 6:
WriteRC(RegTimerClock,0x0d);
WriteRC(RegTimerReload,0xff);
break;
case 7:
WriteRC(RegTimerClock,0x0f);
WriteRC(RegTimerReload,0xff);
break;
default:
WriteRC(RegTimerClock,0x07);
WriteRC(RegTimerReload,tmoLength);
break;
}
}
char PcdComTransceive(struct TranSciveBuffer *pi)
{
bit recebyte=0;
char status;
uchar irqEn=0x00;
uchar waitFor=0x00;
uchar lastBits;
uchar n;
uint i;
FlushFIFO();
switch(pi->MfCommand)
{
case PCD_IDLE:
irqEn = 0x00;
waitFor = 0x00;
break;
case PCD_WRITEE2:
irqEn = 0x11;
waitFor = 0x10;
break;
case PCD_READE2:
irqEn = 0x07;
waitFor = 0x04;
recebyte=1;
break;
case PCD_LOADCONFIG:
case PCD_LOADKEYE2:
case PCD_AUTHENT1:
irqEn = 0x05;
waitFor = 0x04;
break;
case PCD_CALCCRC:
irqEn = 0x11;
waitFor = 0x10;
break;
case PCD_AUTHENT2:
irqEn = 0x04;
waitFor = 0x04;
break;
case PCD_RECEIVE:
irqEn = 0x06;
waitFor = 0x04;
recebyte=1;
break;
case PCD_LOADKEY:
irqEn = 0x05;
waitFor = 0x04;
break;
case PCD_TRANSMIT:
irqEn = 0x05;
waitFor = 0x04;
break;
case PCD_TRANSCEIVE:
irqEn = 0x3D;
waitFor = 0x04;
recebyte=1;
break;
default:
pi->MfCommand=MI_UNKNOWN_COMMAND;
break;
}
if(pi->MfCommand!=MI_UNKNOWN_COMMAND)
{
WriteRC(RegPage,0x00);
WriteRC(RegInterruptEn,0x7F);//使能一切的中止
WriteRC(RegInterruptRq,0x7F);//
WriteRC(RegCommand,PCD_IDLE);//搁置状况
SetBitMask(RegControl,0x01);//铲除FIFO
WriteRC(RegInterruptEn,irqEn|0x80); //答应对应的独断
for(i=0;iMfLength;i++)
{
WriteRC(RegFIFOData,pi->MfData[i]);//写数据至FIFO
}
WriteRC(RegCommand,pi->MfCommand);//执行命令
i=0x2000;
do
{
n=ReadRC(RegInterruptRq);
i–;
}
while((i!=0)&&!(n&irqEn&0x20)&&!(n&waitFor));//等候数据发送完 n&waitFor表明命令主动完毕
status=MI_COM_ERR;
if((i!=0)&&!(n&irqEn&0x20))//FIFO数据超限或i=0
{
if(!(ReadRC(RegErrorFlag)&0x17))//没犯错
{
status=MI_OK;
if(recebyte)//表明有回来数据
{
n=ReadRC(RegFIFOLength);//读出回来的数据长度
lastBits=ReadRC(RegSecondaryStatus)&0x07;
if(lastBits)
{
pi->MfLength=(n-1)*8+lastBits;
}
else
{
pi->MfLength=n*8;
}
if(n==0)
{
n=1;
}
for(i=0;i{
pi->MfData[i]=ReadRC(RegFIFOData);
}
}
}
}
else if(n&irqEn&0x20)//FIFO数据超限
{
status=MI_NOTAGERR;
}
else
{
status=MI_COM_ERR;
}
WriteRC(RegInterruptEn,0x7F);
delay(10);
WriteRC(RegInterruptRq,0x7F);
}
return status;
}
声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/fangan/257941.html