I try to read byte from RFID which represents version number of the device. Using SPI protocol I get 0x92 byte (RC522 Version 2.0), and request byte is 0x37. Everything works well except the positions of the byte. Before I get the right one (0x92) as was planned to get which I do not know why it is printed. The output:
Hello, world
received: 1
received: 92
= v2.0
received: 0
received: 92
= v2.0
received: 0
received: 92
= v2.0
received: 0
received: 92
= v2.0
received: 0
received: 92
= v2.0
I said, well Ok, let me just delete the printf("received: %x\n", data); \
linefrom the SPI.h library. But then I get nothing printed (except Hello World). Another function which should print only "version 2.0"
when 92h returned won't do that, why- because the v
variable returned is 9f
instead of 92
and all previous are different.
#define SPI_READ(data) \
do { \
SPI_TXBUF = 0; \
SPI_WAITFOREORx(); \
data = SPI_RXBUF; \
printf("received: %x\n", data); \
} while(0)
Maybe I should place some wait or delay like function in order to let the RC522 to execute the command? And right after let it print. But even if that would happened, why the printf
deletion breaks the whole output? Here is the code where SPI_READ is in called from:
static uint8_t
read_register_rfid(uint8_t dev_cmd)
{
uint8_t ret = 0;
GPIO_CLR_PIN(GPIO_C_BASE, GPIO_PIN_1);
SPI_WRITE(((dev_cmd << 1) & 0x7e) | 0x80);
SPI_READ(ret);
GPIO_SET_PIN(GPIO_C_BASE, GPIO_PIN_1);
return ret;
}
and the function of checking the version:
void
test_ver()
{
uint8_t reg=1;
// Get the MFRC522 firmware version
uint8_t v = 0;
while(reg!=0xa){
v = read_register_rfid(0x37);
//printf("%x\n", v);
// Lookup which version
switch(v) {
case 0x88: printf(" = (clone)"); break;
case 0x90: printf(" = v0.0"); break;
case 0x91: printf(" = v1.0"); break;
case 0x92: printf(" = v2.0\n"); break;
// default: printf(" = (unknown)\n");
}
reg++;
//When 0x00 or 0xFF is returned, communication probably failed
}
if ((v == 0x00) || (v == 0xFF))
printf("WARNING: Communication failure?\n");
}
Here I shared updated functions for reading and writing to RC522 using SPI library:
void
writeMFRC522(uint8_t adr, uint8_t val)
{
GPIO_CLR_PIN(GPIO_C_BASE, GPIO_PIN_1);
GPIO_CLR_PIN(GPIO_C_BASE, GPIO_PIN_1);
SPI_WRITE((adr << 1) & 0x7e);
SPI_WRITE(val);
SPI_FLUSH();
//printf("456\n");
GPIO_SET_PIN(GPIO_C_BASE, GPIO_PIN_1);
}
static uint8_t
readMFRC522(uint8_t dev_cmd)
{
uint8_t ret = 0x0;
GPIO_CLR_PIN(GPIO_C_BASE, GPIO_PIN_1);
GPIO_CLR_PIN(GPIO_C_BASE, GPIO_PIN_1);
SPI_WRITE(((dev_cmd << 1) & 0x7e) | 0x80);
SPI_READ(ret);
ret = 0;
SPI_WRITE(0x00);
SPI_READ(ret);
GPIO_SET_PIN(GPIO_C_BASE, GPIO_PIN_1);
return ret;
}