|
发表于 2012-3-26 20:58:43
|
显示全部楼层
///////////////////////////////////////////////////////////////////// 这个是我用的认证函数
//功 能:验证卡片密码
//参数说明: auth_mode[IN]: 密码验证模式
// 0x60 = 验证A密钥
// 0x61 = 验证B密钥
// addr[IN]:块地址
// pKey[IN]:密码
// pSnr[IN]:卡片序列号,4字节
//返 回: 成功返回MI_OK
/////////////////////////////////////////////////////////////////////
char PcdAuthState(unsigned char auth_mode,unsigned char addr,unsigned char *pKey,unsigned char *pSnr)
{
char status;
unsigned int unLen;
unsigned char i,ucComMF522Buf[MAXRLEN];
ucComMF522Buf[0] = auth_mode;
ucComMF522Buf[1] = addr;
for (i=0; i<6; i++)
{ ucComMF522Buf[i+2] = *(pKey+i); }
for (i=0; i<4; i++)
{ ucComMF522Buf[i+8] = *(pSnr+i); }
status = PcdComMF522(PCD_AUTHENT,ucComMF522Buf,12,ucComMF522Buf,&unLen);
if ((status != MI_OK) || (!(ReadRawRC(Status2Reg) & 0x08)))
// if ((status != MI_OK))
{ status = MI_ERR; }
ClearBitMask(Status2Reg,0x08);
return status;
}
/************************* RC522和ISO14443卡通讯 函数 *************************
**功 能:通过RC522和ISO14443卡通讯
**参数说明:Command[IN]:RC522命令字
** pInData[IN]:通过RC522发送到卡片的数据
** InLenByte[IN]:发送数据的字节长度
** pOutData[OUT]:接收到的卡片返回数据
** *pOutLenBit[OUT]:返回数据的位长度
********************************************************************************/
char PcdComMF522(unsigned char Command,
unsigned char *pInData,
unsigned char InLenByte,
unsigned char *pOutData,
unsigned int *pOutLenBit)
{
char status = MI_ERR;
unsigned char errorflag;
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 = 5000;
do
{
n = ReadRawRC(ComIrqReg);
i--;
} while ((i!=0) && !(n&0x01) && !(n&waitFor));//0x00110000
errorflag = ReadRawRC(ErrorReg);
ClearBitMask(BitFramingReg,0x80);
if (i!=0)
{
//if(!(ReadRawRC(ErrorReg)&0x1B))
if(!(errorflag&0x1B))
{
status = MI_OK;
if (n & irqEn & 0x01)
{ status = MI_NOTAGERR; }
if (Command == PCD_TRANSCEIVE)
{
n = ReadRawRC(FIFOLevelReg); //FIFO 中的字节数目
lastBits = (ReadRawRC(ControlReg) & (0x07)); //如果为0 则接受到的字节有效
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;
}
主函数是这样的:
status_findcard = PcdRequest(PICC_REQIDL,tt1); //find cards
if(status_findcard == 0)
{
GPIOF->BRR = (GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9);
}
else
{
GPIOF->BSRR = (GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9);
}
status_anticoll = PcdAnticoll(sn); /**/ //防冲突
if(status_anticoll == 0)
{
status_selectcard = PcdSelect(sn , &size1); //select a card and read the size
}
status_auth = PcdAuthState(0x60 , 0x08 , key , sn ); //验证 验证返回正确值
status_read = PcdRead(0x08,block); //read a block 但是读取返回失败
}
请各位大侠指点 多谢 |
|