I'm working on a RFID project and it's stopped by a strange problem. When I'm trying to read a block of data, 16 byte along with 2 byte CRC is received. Then I try to calculate data CRC and compare these two, but they are not equal!
Data bytes are correct. I checked with NFC tools (Android). CRC worked fine with SAK, but not this. I wonder what the problem could be.
For details: 16 bytes are all 0x0000 and tag CRC is 0x4F68, module CRC is 0x4937
I have tried for days and found no fix for this yet.
Previous levels such as request, anti collision, select tag and authenticate were fine.
Here is the code:
unsigned short RC522::CalculateCRC(unsigned char * Data, unsigned char Dlen)
{
CommandWrite(CMD_Idle);
CRC_InterruptStatus();// Clear CRC interrupt and return it
FIFO_Flush();
for (unsigned char i = 0; i < Dlen; i++)
FIFO_Write(Data[i]);
CommandWrite(CMD_CalcCRC);
for (unsigned short c = 100; c > 0; c--)
{
if (CRC_InterruptStatus())
break;
delay.Mili(1);
}
unsigned short Result = CRC_ResultRead();
CommandWrite(CMD_Idle);
return Result;
}
unsigned char RC522::ReadBlock(unsigned char Block, unsigned char * Data)
{
unsigned char CMD[4] = {PICC_READ, Block, 0, 0};
unsigned int Rlen;
unsigned char Buf[18];
unsigned short crc = CalculateCRC(CMD, 2);
CMD[2] = crc & 0xFF;
CMD[3] = (crc >> 8) & 0xFF;
TX_LastBitsWrite(0);
unsigned char Status = ToCard(CMD_Tranceive, CMD, 4, Buf, &Rlen);
if ((Status == CARD_FOUND) && (Rlen == 144))
{
unsigned short crcr = (Buf[17] << 8) | Buf[16];
crc = CalculateCRC(Buf, 16);
if (crcr == crc) // Mismatch here !!!
{
for (unsigned char ptr = 0; ptr < 16; ptr++)
Data[ptr] = Buf[ptr];
return CARD_FOUND;
}
}
return ERROR;
}