AnsweredAssumed Answered

RC522/FM17550 Read Fifo Failer

Question asked by yaowu he on Jun 29, 2017

hi

this problem is :when i use the following function to write and read Rc522 Fifo ,it will success read and write if i write 64 bytes,but if i write over 64 bytes,the operation will fail,it can be successed read 128 bytes.

i will apperated if you can correct  me the following funciton .

thank you 

 

uint8 PcdComMF522(
uint8 Command,
uint8 *pInData,
uint8 InLenByte,
uint8 *pOutData,
uint16 *pOutLenBit)
{
uint32 j =0;
unsigned char n=0;
uint32 i=0;
uint8 recebyte = 0;
uint8 status;
unsigned char irqEn = 0x00;
unsigned char waitFor = 0x00;
unsigned char lastBits;
uint8 LastLenth,gFifoLenth,ErrorFlag;
volatile uint16 k;

switch (Command)
{
case PCD_IDLE:
irqEn = 0x00;
waitFor = 0x10;
break;
case PCD_AUTHENT:
irqEn = 0x12;
waitFor = 0x10;
break;
case PCD_CALCCRC:
irqEn = 0x11;
waitFor = 0x10;
break;
case PCD_RECEIVE:
irqEn = 0x06;
waitFor = 0x10;
recebyte=1;
break;
case PCD_TRANSMIT:
irqEn = 0x05;
waitFor = 0x10;
break;
case PCD_TRANSCEIVE:
irqEn = 0x3F;
waitFor = 0x30;
recebyte=1;
break;
default:
Command = RFID_UNKNOWN;
break;
}

//STEP 1 配置
//改变WaterLevelRegChallege的值来调整接收到的有效数据数量
if (Command != RFID_UNKNOWN)
{
WriteRawRC(ComIEnReg,irqEn|0x80);
SetBitMask(FIFOLevelReg,0x80); //清FIFO
WriteRawRC(WaterLevelReg,20); //上下限字节数为WaterLevelRegChallege个
ClearBitMask(ComIrqReg,0x80);
WriteRawRC(CommandReg,PCD_IDLE);//进入到空闲状态(取消上一次命令

LastLenth = InLenByte;
if(LastLenth>64)
{
WriteRawRC(CommandReg, Command); //向命令寄存器写操作码,连续发送状态(数据超过64个字节),必须先发发送命令。
}

while(1) //一个小算法用于发送超过FIFO容量(64字节)的数据。
{
gFifoLenth = FIFO_MAX_RLEN-(ReadRawRC(FIFOLevelReg)&0x7f);//得到剩余FIFO空间
if(LastLenth>gFifoLenth)
{ //说明一次不能发完所有的数据,需要分包发送
LastLenth -=gFifoLenth; //还剩下没有发送的数据
for(k=0; k<gFifoLenth; k++) //本次需要发送数据的长度
{
WriteRawRC(FIFODataReg, pInData[j++]);
}
if(Command== PCD_TRANSCEIVE) //在RC522中要想发送超过64个字节的数据,这个启动发送放置的位置非常重要
{
SetBitMask(BitFramingReg,0x80); //启动发送
}
i = 0x3500;
do
{
n = ReadRawRC(ComIrqReg);
i--;
}
while ((i!=0) && !(n&irqEn&0x04)&&!(n&waitFor)); //下限到了可以发送数据

if((n&0x04))
{
WriteRawRC(ComIrqReg,0x04); //清下限状态寄存器
continue; //还需要继续发送数据
}
else
{
return MI_ERR;
}
}
else
{
for(k=0; k<LastLenth; k++) //发送最后一包数据
{
WriteRawRC(FIFODataReg, pInData[j++]);
}
if(InLenByte>64)
{
if(Command== PCD_TRANSCEIVE) //在RC522中要想发送超过64个字节的数据,这个启动发送放置的位置非常重要
{
SetBitMask(BitFramingReg,0x80); //启动发送
}
i = 0x3500;
do
{
n = ReadRawRC(ComIrqReg);
i--;
}while ((i!=0) && !(n&irqEn&0x01) && !(n&0x40)&&!(n&waitFor)); //0x01表示定时器溢出//waitFor表示发送和接收都结束//0x40表示发送结束
}
break;
}
}
if(InLenByte<=64)
{
WriteRawRC(CommandReg,Command);
if(Command== PCD_TRANSCEIVE)
{
SetBitMask(BitFramingReg,0x80); //启动发送
}
i = 0x3500;
do
{
n = ReadRawRC(ComIrqReg);
i--;
}while ((i!=0) && !(n&irqEn&0x01) && !(n&0x40)&&!(n&waitFor)); //0x01表示定时器溢出//waitFor表示发送和接收都结束//0x40表示发送结束
}
j=0; //接收计数器
status = MI_ERR;
*pOutLenBit =0;
WriteRawRC(ComIrqReg,0x08); //清上限状态寄存器


while(1)
{
i = 0x6500;
do
{
n = ReadRawRC(ComIrqReg);
i--;
}while ((i!=0) && !(n&irqEn&0x01)&&!(n&0x20) && !(n&0x08)&&!(n&waitFor)); //0x01表示定时器溢出//waitFor表示发送和接收都结束//0x20表示接收完成//0x08高限告警

if((i==0)||(n & irqEn & 0x01)) //表示超时
{
status = MI_NOTAGERR;
break;
}

else if((n&0x20)||(n&waitFor)) //接收结束或者超时就退出
{
ErrorFlag = ReadRawRC(ErrorReg);
if (!(ErrorFlag&0x9F))
{
status = MI_OK;
if (recebyte)
{
n = ReadRawRC(FIFOLevelReg);
n = ReadRawRC(FIFOLevelReg);
lastBits = ReadRawRC(ControlReg) & 0x07;
if (lastBits)
{
*pOutLenBit += (n-1)*8 + lastBits;
}
else
{
*pOutLenBit += n*8;
}
if (n == 0)
{
n = 1;
}
for (k=0; k<n; k++)
{
pOutData[j++] = ReadRawRC(FIFODataReg);
}
}
break;
}
else if (ErrorFlag&0x08)
{
status = MI_ERR;
if (recebyte)
{
n = ReadRawRC(FIFOLevelReg);
lastBits = ReadRawRC(ControlReg) & 0x07;
if (lastBits)
{
*pOutLenBit = (n-1)*8 + lastBits;
}
else
{
*pOutLenBit = n*8;
}
if (n == 0)
{
n = 1;
}
for (i=0; i<n; i++)
{
pOutData[i+1] = ReadRawRC(FIFODataReg);
}
}
pOutData[0]=ReadRawRC(CollReg)&0x07;
}
else if (!(ReadRawRC(ErrorReg)&0x17))
{
status = MI_ERR;
}
else
{
status = MI_ERR;
}

break;
}
else if(n&0x08) //FIFO数据过高中断
{
if (recebyte)
{
n = ReadRawRC(FIFOLevelReg);
do
{
*pOutLenBit += n*8; //累计接收计数
for (k=0; k<n; k++)
{
pOutData[j++] = ReadRawRC(FIFODataReg);
}
n = ReadRawRC(FIFOLevelReg);
}while(n);
WriteRawRC(ComIrqReg,0x08); //清上限状态寄存器
}
continue;
}
else
{
continue;
}
}
WriteRawRC(BitFramingReg,0x00); //停止发送,清收发位存储
WriteRawRC(ComIEnReg,0x7F);
WriteRawRC(ComIrqReg,0x7F);
SetBitMask(ControlReg,0x80);
WriteRawRC(CommandReg,PCD_IDLE);
}
return status;
}

Outcomes