log1:
Hello World.111
PLL Clock = 84000000 Hz
Core Clock = 42000000 Hz
+-------------------------------------------------------+
| Nano100 Series SPI_RC522 Sample Code |
+-------------------------------------------------------+
A Rest CommandReg = 0x20
A Rest ModeReg = 0x3f
B Rest ModeReg = 0x3d
retB = 0x3d
A TxControlReg 80
PcdAntennaOn 40
B TxControlReg 83
PcdRequest :
In PcdComMF522:
PcdComMF522: i= 199997,waitFor = 48,n= 100
Out PcdComMF522:
PcdRequest status = 0
failed
In PcdComMF522:
PcdComMF522: i= 199995,waitFor = 48,n= 100
Out PcdComMF522:
防冲撞state:0
Rec:a3
Rec:90
Rec:4a
Rec:5
In PcdComMF522:
PcdComMF522: i= 199993,waitFor = 48,n= 100
Out PcdComMF522:
:选定卡片state:fe
:e0
:51
:35
:b4
In PcdComMF522:
PcdComMF522: i= 199983,waitFor = 48,n= 100
Out PcdComMF522:
Rats :status:0,unLen:12
:10
:78
:80
:a0
:2
:20
:90
:0
:0
:0
:0
:0
:a3
:90
:4a
:5
:39
:4a
rats ############################### over!
cpucard_select_MF*************** over!
A pack_pcb:0
B pack_pcb :a
:a
:1
:0
:a4
:0
:0
:2
:3f
:0
In PcdComMF522:
PcdComMF522: i= 199973,waitFor = 48,n= 100
Out PcdComMF522:
:a
:1
:6f
:15
:84
:e
:31
:50
:41
:59
:2e
:53
:59
:53
:2e
:44
:44
:46
:0
:0
:0
:0
:0
:0
:75
:14
:0
:0
:28
status:0,unLen:1d
cpucard_select_MF over! status:0,unLen:1d
错误的log:
Hello World.111
PLL Clock = 84000000 Hz
Core Clock = 42000000 Hz
+-------------------------------------------------------+
| Nano100 Series SPI_RC522 Sample Code |
+-------------------------------------------------------+
A Rest CommandReg = 0x20
A Rest ModeReg = 0x3f
B Rest ModeReg = 0x3d
retB = 0x3d
A TxControlReg 80
PcdAntennaOn 40
B TxControlReg 83
PcdRequest :
ErrorReg = 0 --- i=5997
ABC status = MI_OK
n= 2
FIFODataReg = 0 --- i=2
PcdRequest status = 0
0--0x8
1--0x0
2--0x0
3--0x0
success
ErrorReg = 0 --- i=5995
ABC status = MI_OK
n= 5
FIFODataReg = 0 --- i=5
防冲撞state:0
Rec:a3
Rec:90
Rec:4a
Rec:5
ErrorReg = 0 --- i=5993
ABC status = MI_OK
n= 3
FIFODataReg = 90 --- i=3
:选定卡片state:0
:e0
:51
:35
:b4
ErrorReg = 0 --- i=5983
ABC status = MI_OK
n= 18
FIFODataReg = 0 --- i=18
Rats :status:0,unLen:90
:10
:78
:80
:a0
:2
:20
:90
:0
:0
:0
:0
:0
:a3
:90
:4a
:5
:39
:4a
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
rats ############################### over!
cpucard_select_MF*************** over!
A pack_pcb:0
B pack_pcb :a
:a
:1
:0
:a4
:0
:0
:2
:3f
:0
ErrorReg = 0 --- i=5973
ABC status = MI_OK
n= 18
FIFODataReg = 0 --- i=18
:a
:1
:6f
:15
:84
:e
:31
:50
:41
:59
:2e
:53
:59
:53
:2e
:44
:44
:46
:0
:0
:0
:0
:0
:0
:99
:14
:0
:0
:28
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:a0
:0
:0
:20
:63
:1
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
:0
status:0,unLen:e8
cpucard_select_MF over! status:0,unLen:e8
代码不同的地方:
错误代码:
//******************************************************************/
//功 能:通过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 irqEn = 0x00 ;
unsigned char waitFor = 0x00 ;
unsigned char lastBits ;
unsigned char n ;
unsigned int i ;
unsigned char ret ;
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) ; // 清空FIFO
for(i=0; i<InLenByte; i++)
WriteRawRC(FIFODataReg,pInData[i]) ; // 数据写入FIFO
WriteRawRC(CommandReg, Command) ; // 命令写入命令寄存器
if(Command == PCD_TRANSCEIVE)
SetBitMask(BitFramingReg,0x80) ; // 开始发送
i = 6000 ; //根据时钟频率调整,操作M1卡最大等待时间25ms
do
{
n = ReadRawRC(ComIrqReg) ;
//printf("ComIrqReg = %x, ---%d\n",n,i);
i-- ;
}
while((i!=0)&&!(n&0x01)&&!(n&waitFor)) ;
ClearBitMask(BitFramingReg,0x80) ;
if(i!=0)
{
ret = ReadRawRC(ErrorReg);
printf("ErrorReg = %x --- i=%d\n",ret,i);
if(!(ret&0x1B))
{
printf("ABC status = MI_OK\n");
status = MI_OK ;
if (n&irqEn&0x01){
status = MI_NOTAGERR ;
printf("n= %x,status = %x\n",n,status);
}
if(Command==PCD_TRANSCEIVE)
{
n = ReadRawRC(FIFOLevelReg) ;
//printf("FIFOLevelReg= %x\n",n);
ret = ReadRawRC(ControlReg);
//printf("FIFOLevelReg= %x\n",ret);
lastBits = ret&0x07 ;
if(lastBits)
*pOutLenBit = (n-1)*8 + lastBits ;
else
*pOutLenBit = n*8 ;
if(n==0)
n = 1 ;
if(n>MAXRLEN)
n = MAXRLEN ;
printf("n= %d\n",n);
for (i=0; i<n; i++)
pOutData[i] = ReadRawRC(FIFODataReg) ;
printf("FIFODataReg = %x --- i=%d\n",pOutData[i],i);
}
}
else
status = MI_ERR ;
}
SetBitMask(ControlReg,0x80) ;// stop timer now
WriteRawRC(CommandReg,PCD_IDLE) ;
return status;
}
正确的代码:
/////////////////////////////////////////////////////////////////////
//功 能:通过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 = (char)MI_ERR;
unsigned char ret_len;
unsigned char irqEn = 0x00;
unsigned char waitFor = 0x00;
unsigned char lastBits;
unsigned char n;
unsigned int i;
switch (Command)
{
case PCD_AUTHENT:
irqEn = 0x12; // 0001 0010
waitFor = 0x10; //
break;
case PCD_TRANSCEIVE:
irqEn = 0x77; // 0111 0111
waitFor = 0x30;
break;
default:
break;
}
printf("\n In PcdComMF522:\n");
/*中断请求传递到管脚使能*/
WriteRawRC(ComIEnReg,irqEn|0x80);
/*中断请求标志清零*/
ClearBitMask(ComIrqReg,0x80);
/*如果当前有运行的命令
取消
*/
WriteRawRC(CommandReg,PCD_IDLE);
/*复位FIFO缓冲区*/
SetBitMask(FIFOLevelReg,0x80);
/*0x26写进去了*/
for (i=0; i<InLenByte; i++)
{
WriteRawRC(FIFODataReg, pInData[i]);
// printf("FIFODataReg:%x\n",pInData[i]);
}
/*0x0C写进去了
命令是启动发送
*/
WriteRawRC(CommandReg, Command);
if (Command == PCD_TRANSCEIVE)
{
/*启动数据的发送*/
SetBitMask(BitFramingReg,0x80);
}
// i = 600;//根据时钟频率调整,操作M1卡最大等待时间25ms
i = 200000;
do
{
/*中断请求标志*/
n = ReadRawRC(ComIrqReg);
i--;
}while ((i!=0) && !(n&0x01) && !(n&waitFor));
/*
定时器结束,或者内部命令执行结束
或者读寄存器最大次数完毕
就退出
*/
ClearBitMask(BitFramingReg,0x80);
printf("\n PcdComMF522: i= %d,waitFor = %d,n= %d\n",i,waitFor,n);
/*i不为0*/
if (i!=0)
{
if(!(ReadRawRC(ErrorReg)&0x1B))
{
status = MI_OK;
if (n & irqEn & 0x01)
{ status = (char)MI_NOTAGERR; }
if (Command == PCD_TRANSCEIVE)
{
/*
指示FIFO中保存的字节数
*/
n = ReadRawRC(FIFOLevelReg);
ret_len=n;
/*
最后接收到的字节的有效位的数目
如果该位为0,则整个字节有效
*/
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);
}//error
}
}
else
{ status = (char)MI_ERR;
}
}
*pOutLenBit=ret_len;
SetBitMask(ControlReg,0x80); // stop timer now
WriteRawRC(CommandReg,PCD_IDLE);
printf("\n Out PcdComMF522:\n");
return status;
}
不要在里面 胡乱打印printf 打印log 占用时间,扰乱时序
重点查看:
TxAutoReg 0x15 的用法:
寻卡过程 需要 先 Find 卡片,然后在碰撞,然后再选卡,有一个流程。
// i = 600;//根据时钟频率调整,操作M1卡最大等待时间25ms
i = 200000;
这样做的原因。