I am trying to communicate with NRF52832 on MRFC522 vscode. My Write and Read functions are not working

Below I have my read and write functions. Are my SPI configuration settings correct? I want to read the RFID card ID value.

MAİN.C

#include <zephyr/drivers/gpio.h>

#include <zephyr/drivers/SPi.h>
#include "MRFC522.h"



/* The devicetree node identifier for the "led0" alias. */
#define LED0_NODE DT_ALIAS(led0)
uint8_t ID[5];
/*
 * A build error on this line means your board is unsupported.
 * See the sample documentation for information on how to fix this.
 */
static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios);

int main(void)
{
	int ret;
SPI_Init();
TM_MFRC522_Init();
	if (!gpio_is_ready_dt(&led)) {
		return 0;
	}

	ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);
	if (ret < 0) {
		return 0;
	}

	while (1) {

    if ((TM_MFRC522_Check(ID) == MI_OK))
	{
		gpio_pin_toggle_dt(&led);
printk(" [%02x%02x%02x%02x%02x]\n\r\n", ID[0], ID[1], ID[2], ID[3], ID[4]);
	}

	
	
	}
	return 0;
}

Parents Reply Children
  • Great to hear! And thank you for sharing, in case anyone else encounters similar issues with this sensor.

    Best regards,

    Edvin

  • Dear damar

    Thank you for providing the sample. I was able to read the UID of the card based on what you provided.

    My current issue is that I cannot read the data in the blocks of the card.

    Hardware: nRF52DK, MFRC522, MIFARE 1K.

    Function Read data on Block 6

    void ReadBlock6(uint8_t *cardUID)
    {
        uint8_t defaultKeyA[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
        uint8_t buffer[16] = {0};
        printf("cardUID: %02X-%02x-%02x-%02x \n", cardUID[0], cardUID[1], cardUID[2], cardUID[3]);
        if (TM_MFRC522_Auth(PICC_AUTHENT1A, 6, defaultKeyA, cardUID) == MI_OK)
        {
            TM_MFRC522_ReadBlock(6, buffer);
        }
        else
        {
            printf("[ERROR] Khong the doc block 6 do loi authen\n");
        }
    }
    uint8_t TM_MFRC522_Auth(uint8_t authMode, uint8_t blockAddr, uint8_t *sectorKey, uint8_t *cardUID)
    {
        uint8_t buff[12];
    
        buff[0] = authMode;  // Loại xác thực (PICC_AUTHENT1A hoặc PICC_AUTHENT1B)
        buff[1] = blockAddr; // Block cần xác thực
    
        // Copy key 6 byte
        for (uint8_t i = 0; i < 6; i++)
        {
            buff[i + 2] = sectorKey[i];
        }
    
        // Copy UID (4 byte)
        for (uint8_t i = 0; i < 4; i++)
        {
            buff[i + 8] = cardUID[i];
        }
    
        // Gửi lệnh đến thẻ
        uint8_t status = 1;
        printk("buff: %X-%X-%X-%X-%X-%X-%X-%X-%X-%X-%X-%X\n", buff[0], buff[1], buff[2], buff[3], 
            buff[4], buff[5], buff[6], buff[7], buff[8], buff[9], buff[10], buff[11]);
        status = TM_MFRC522_ToCard(PCD_AUTHENT, buff, 12, NULL, NULL);
        printk("Authen status: %d\n", status);
        if (status != MI_OK)
        {
            printf("[ERROR] Authen that bai block %d\n", blockAddr);
        }
        else
        {
            printf("[INFO] Authen thanh cong block %d\n", blockAddr);
        }
    
        return status;
    }
    
    TM_MFRC522_STS_T TM_MFRC522_ReadBlock(uint8_t blockAddr, uint8_t *buffer)
    {
        TM_MFRC522_STS_T status;
        uint8_t sendData[2];
        uint16_t recvLen;
    
        printf("[INFO] Dang doc du lieu tu block: %d\n", blockAddr);
    
        sendData[0] = PICC_READ; // Lenh doc block
        sendData[1] = 6;         // Dia chi block can doc
    
        printf("[DEBUG] Gui lenh doc: 0x%02X 0x%02X\n", sendData[0], sendData[1]);
    
        // Gui lenh doc block den the
        status = TM_MFRC522_ToCard(PCD_TRANSCEIVE, sendData, 2, buffer, &recvLen);
    
        if (status != MI_OK)
        {
            printf("[ERROR] Khong the doc block %d!\n", blockAddr);
            return MI_ERR;
        }
    
        if (recvLen != (MFRC522_MAX_LEN + 2))
        {
            printf("[ERROR] So byte nhan duoc khong dung! Nhan: %d, Ky vong: %d\n", recvLen, MFRC522_MAX_LEN + 2);
            return MI_ERR;
        }
    
        printf("[INFO] Du lieu tu block %d: ", blockAddr);
        for (int i = 0; i < MFRC522_MAX_LEN; i++)
        {
            printf("%02X ", buffer[i]);
        }
        printf("\n");
    
        return MI_OK;
    }
     
    Log:

     
Related