#include
#define RST 1 << 8 // P0.7控制RC522复位,低电平复位
/*
*********************************************************************************************************
** 函数名称 :RC522_RST()
** 函数功能 :复位RC522
** 调试说明 :需将跳线JP5和RST连接。
*********************************************************************************************************
*/
int RC522_RST (void)
{
//PINSEL0 = 0x00000000; // 设置管脚连接GPIO
IO0DIR = RST; // 设置RST控制口为输出
IO0SET = RST; // P0.7置高
DelayNS(10); // 延时
IO0CLR = RST; // P0.7置低
DelayNS(3);
IO0SET = RST;
return 0;
}
/*
********************************************************************************************************
** 函数名称:ReadRawRC()
** 函数功能:读MF522寄存器
** 参数说明:Address[IN]:寄存器地址
** 返 回 值:读出的值
********************************************************************************************************
*/
unsigned char ReadRawRC(unsigned char Address)
{
unsigned char ucAddr;
unsigned char ucResult = 0;
ucAddr = ((Address<<1)&0x7E)|0x80;
ucResult = MSPI_SendData (ucAddr);
return ucResult;
}
/*
*******************************************************************************************************
** 函数名称:WriteRawRC()
** 函数功能:写MF522寄存器
** 参数说明:Address[IN]:寄存器地址
** value[IN]:写入的值
** 返 回 值:无
*******************************************************************************************************
*/
void WriteRawRC(unsigned char Address,unsigned char value)
{
unsigned char ucAddr;
ucAddr = ((Address<<1)&0x7E);
MSPI_SendData(ucAddr);
MSPI_SendData(value);
return 0;
}
/*
*******************************************************************************************************
** 函数名称:SetBitMask()
** 函数功能:置MF522寄存器位
** 参数说明:reg[IN]:寄存器地址
** mask[IN]:置位值
** 返 回 值:无
*******************************************************************************************************
*/
void SetBitMask(unsigned char reg,unsigned char mask)
{
char tmp = 0x0;
tmp = ReadRawRC(reg);
WriteRawRC(reg,tmp | mask);
return 0;
}
/*
*******************************************************************************************************
** 函数名称:ClearBitMask()
** 函数功能:清MF522寄存器位
** 参数说明:reg[IN]:寄存器地址
** mask[IN]:清位值
** 返 回 值:无
*******************************************************************************************************
*/
void ClearBitMask(unsigned char reg,unsigned char mask)
{
char tmp = 0x0;
tmp ReadRawRC(reg);
WriteRawRC(reg,tmp & ~mask);
return 0;
}
/*
*******************************************************************************************************
** 函数名称:PcdAntennaOn()
** 函数功能:开启天线
** 函数说明:每次开启或关闭天线发射之间至少有1ms的间隔
** 返 回 值:无
*******************************************************************************************************
*/
void PcdAntennaOn()
{
unsigned char i;
i = ReadRawRC(TxControlReg);
if (!(i & 0x03))
{
SetBitMask(TxControlReg,0x03);
}
return 0;
}
/*
*******************************************************************************************************
** 函数名称:PcdAntennaOff()
** 函数功能:关闭天线
** 函数说明:每次开启或关闭天线发射之间至少有1ms的间隔
** 返 回 值:无
*******************************************************************************************************
*/
void PcdAntennaOff()
{
ClearBitMask(TxControlReg,0x03);
}
/*
*******************************************************************************************************
** 函数名称:PcdComMF522()
** 函数功能:通过RC522和ISO14443卡通讯
** 函数说明:Command[IN]:RC522命令字
** pIndata[IN]:通过RC522发送到卡片的数据
** InLenByte[IN]:发送数据的字节长度
** pOutData[OUT]:接收到的卡片返回数据
** *pOutLenBit[OUT]:返回数据的位长度
** 返 回 值:成功返回 MI_OK
*******************************************************************************************************
*/
char PcdComMF522(unsigned char Command,
unsigned char *pInData,
unsigned char InLenByte,
unsigned char *pOutData,
unsigned int *pOutLenBit)
{
char status = MI_ERR;
unsigned char irqEn = 0x00;
unsigned char waitFor = 0x00;
unsigned char lastBits;
unsigned char n;
unsigned int i;
switch (Command)
{
case PCD_AUTHENT:
irqEn = 0x12;
waitFor = 0x10;
break;
case PCD_TRANSCEIVE:
irqEn = 0x77;
waitFor = 0x30;
break;
default:
break;
}
WriteRawRC(ComIEnReg,irqEn|0x80);
ClearBitMask(ComIrqReg,0x80);
WriteRawRC(CommandReg,PCD_IDLE);
SetBitMask(FIFOLevelReg,0x80);
for (i=0; i<InLenByte; i++)
{ WriteRawRC(FIFODataReg, pInData[i]); }
WriteRawRC(CommandReg, Command);
if (Command == PCD_TRANSCEIVE)
{ SetBitMask(BitFramingReg,0x80); }
i = 600;//根据时钟频率调整,操作M1卡最大等待时间25ms
do
{
n = ReadRawRC(ComIrqReg);
i--;
}
while ((i!=0) && !(n&0x01) && !(n&waitFor));
ClearBitMask(BitFramingReg,0x80);
if (i!=0)
{
if(!(ReadRawRC(ErrorReg)&0x1B))
{
status = MI_OK;
if (n & irqEn & 0x01)
{ status = MI_NOTAGERR; }
if (Command == PCD_TRANSCEIVE)
{
n = ReadRawRC(FIFOLevelReg);
lastBits = ReadRawRC(ControlReg) & 0x07;
if (lastBits)
{ *pOutLenBit = (n-1)*8 + lastBits; }
else
{ *pOutLenBit = n*8; }
if (n == 0)
{ n = 1; }
if (n > MAXRLEN)
{ n = MAXRLEN; }
for (i=0; i<n; i++)
{ pOutData[i] = ReadRawRC(FIFODataReg); }
}
}
else
{ status = MI_ERR; }
}
SetBitMask(ControlReg,0x80); // stop timer now
WriteRawRC(CommandReg,PCD_IDLE);
return status;
}
/*
*******************************************************************************************************
** 函数名称:PcdRequest()
** 函数功能:寻卡
** 函数说明:req_code[IN]:寻卡方式
** 0x52 = 寻感应区内所有符合14443A标准的卡
** 0x26 = 寻未进入休眠状态的卡
** pTagType[OUT]:卡片类型代码
** 0x4400 = Mifare_UltraLight
** 0x0400 = Mifare_One(S50)
** 0x0200 = Mifare_One(S70)
** 0x0800 = Mifare_Pro(X)
** 0x4403 = Mifare_DESFire
** 返 回 值:成功返回MI_OK
*******************************************************************************************************
*/
char PcdRequest(unsigned char req_code,unsigned char *pTagType)
{
char status;
unsigned int unLen;
unsigned char ucComMF522Buf[MAXRLEN];
ClearBitMask(Status2Reg,0x08);
WriteRawRC(BitFramingReg,0x07);
SetBitMask(TxControlReg,0x03);
ucComMF522Buf[0] = req_code;
status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,1,ucComMF522Buf,&unLen);
if ((status == MI_OK) && (unLen == 0x10))
{
*pTagType = ucComMF522Buf[0];
*(pTagType+1) = ucComMF522Buf[1];
}
else
{ status = MI_ERR; }
return status;
}
/*
*******************************************************************************************************
** 函数名称:PcdAnticoll()
** 函数功能:防冲撞
** 函数说明:pSnr[OUT]:卡片序列号,4字节
** 返 回 值:成功返回MI_OK
*******************************************************************************************************
*/
char PcdAnticoll(unsigned char *pSnr)
{
char status;
unsigned char i,snr_check=0;
unsigned int unLen;
unsigned char ucComMF522Buf[MAXRLEN];
ClearBitMask(Status2Reg,0x08);
WriteRawRC(BitFramingReg,0x00);
ClearBitMask(CollReg,0x80);
ucComMF522Buf[0] = PICC_ANTICOLL1;
ucComMF522Buf[1] = 0x20;
status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,2,ucComMF522Buf,&unLen);
if (status == MI_OK)
{
for (i=0; i<4; i++)
{
*(pSnr+i) = ucComMF522Buf[i];
snr_check ^= ucComMF522Buf[i];
}
if (snr_check != ucComMF522Buf[i])
{ status = MI_ERR; }
}
SetBitMask(CollReg,0x80);
return status;
}
/*
*******************************************************************************************************
** 函数名称:PcdSelect()
** 函数功能:选定卡片
** 函数说明:pSnr[IN]:卡片序列号,4字节
** 返 回 值:成功返回MI_OK
*******************************************************************************************************
*/
char PcdSelect(unsigned char *pSnr)
{
char status;
unsigned char i;
unsigned int unLen;
unsigned char ucComMF522Buf[MAXRLEN];
ucComMF522Buf[0] = PICC_ANTICOLL1;
ucComMF522Buf[1] = 0x70;
ucComMF522Buf[6] = 0;
for (i=0; i<4; i++)
{
ucComMF522Buf[i+2] = *(pSnr+i);
ucComMF522Buf[6] ^= *(pSnr+i);
}
CalulateCRC(ucComMF522Buf,7,&ucComMF522Buf[7]);
ClearBitMask(Status2Reg,0x08);
status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,9,ucComMF522Buf,&unLen);
if ((status == MI_OK) && (unLen == 0x18))
{ status = MI_OK; }
else
{ status = MI_ERR; }
return status;
}