Coder Social home page Coder Social logo

abobija / esp-idf-rc522 Goto Github PK

View Code? Open in Web Editor NEW
127.0 5.0 36.0 96 KB

C library for interfacing ESP32 with MFRC522 RFID card reader, packaged as ESP-IDF component

License: MIT License

C 99.69% CMake 0.31%
esp-idf mfrc522 rfid card reader esp32 esp-idf-component

esp-idf-rc522's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

esp-idf-rc522's Issues

Please, add a ".clang-format" file

I would be happy to work on some TODO in this project (in particular the new I2C library for IDF >= 5.2.0). Could you please provide a ".clang-format" file with your preferences for the code formatting?

Error Event, Crashes The ESP32

Hey,
Greetings, hope you are doing good.
i really loved your library and i had it up and running like hardly in less than 5 minutes. really great job.
perhaps, i purposely wanted to try and remove the module, and wanted the library to throw errors and halt as it is without it trying anymore, but apparently even if i checked the error states, it seems like your code is not really returning anything back. i wanted to sit and check it line by line, but i had a lot on my plate to solve it so rather thought to bug you instead.

here's the code i used

#include <esp_log.h>
#include <inttypes.h>
#include "RC522.h"

static const char* TAG = "rc522-demo";
static rc522_handle_t scanner;

static void rc522_handler(void* arg, esp_event_base_t base, int32_t event_id, void* event_data)
{
    rc522_event_data_t* data = (rc522_event_data_t*) event_data;

    switch(event_id) {
        case RC522_EVENT_TAG_SCANNED: {
                rc522_tag_t* tag = (rc522_tag_t*) data->ptr;
                ESP_LOGI(TAG, "Tag scanned (sn: %" PRIu64 ")", tag->serial_number);
            }
            break;
    }
}

void app_main()
{
    esp_err_t err;

    rc522_config_t config = {
        .spi.host = VSPI_HOST,
        .spi.miso_gpio = 19,
        .spi.mosi_gpio = 23,
        .spi.sck_gpio = 18,
        .spi.sda_gpio = 5,
    };

    err = rc522_create(&config, &scanner);
    if(err != ESP_OK)
        ESP_LOGE(TAG, "Failed to create RC522 scanner: %s", esp_err_to_name(err));

    
    err = rc522_register_events(scanner, RC522_EVENT_ANY, rc522_handler, NULL);
    if(err != ESP_OK)
        ESP_LOGE(TAG, "Failed to register RC522 events: %s", esp_err_to_name(err));
    
    
    err = rc522_start(scanner);
    if(err != ESP_OK)
    {
        ESP_LOGE(TAG, "Failed to start RC522 scanner: %s", esp_err_to_name(err));
        rc522_destroy(scanner);
    }
        
}

now as you can see its just your same example code nothing new or fancy, but i just wanna initially check if the module is even present or not, and if not, then just do other things and forget about it.

here is the log for you

 (208) cpu_start: App cpu up.
I (236) cpu_start: Pro cpu start user code
I (236) cpu_start: cpu freq: 160000000
I (236) cpu_start: Application information:
I (241) cpu_start: Project name:     RFID_RC522
I (246) cpu_start: App version:      1
I (250) cpu_start: Compile time:     Sep  7 2022 01:12:13
I (257) cpu_start: ELF file SHA256:  4ed91e1f5ce277fd...
I (263) cpu_start: ESP-IDF:          v4.4.1
I (268) heap_init: Initializing. RAM available for dynamic allocation:
I (275) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (281) heap_init: At 3FFB2DF8 len 0002D208 (180 KiB): DRAM
I (287) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (293) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (300) heap_init: At 4008DEC0 len 00012140 (72 KiB): IRAM
I (307) spi_flash: detected chip: generic
I (311) spi_flash: flash io: dio
I (316) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
E (327) rc522: Read/write test failed
I (327) gpio: GPIO[5]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
E (427) rc522: write: Unknown transport
E (427) rc522: Failed to write data (err: ESP_ERR_INVALID_STATE)
E (427) rc522: write: Unknown transport
E (427) rc522: Failed to write data (err: ESP_ERR_INVALID_STATE)
E (437) rc522: read: Unknown transport
E (437) rc522: Failed to read data (err: ESP_ERR_INVALID_STATE)
Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.

Core  0 register dump:
PC      : 0x400d557f  PS      : 0x00060530  A0      : 0x800d5703  A1      : 0x3ffb7ea0  
0x400d557f: rc522_read at E:\ESP32_Projects\RFID_RC522\build/../components/RC522_RFID/RC522.c:92
 (inlined by) rc522_clear_bitmask at E:\ESP32_Projects\RFID_RC522\build/../components/RC522_RFID/RC522.c:105
 (inlined by) rc522_card_write at E:\ESP32_Projects\RFID_RC522\build/../components/RC522_RFID/RC522.c:337

A2      : 0x3ffaff6c  A3      : 0x0000000c  A4      : 0x00000030  A5      : 0xfffffff7
A6      : 0x3ffb7f10  A7      : 0xffffff80  A8      : 0x800d557f  A9      : 0x3ffb7e80
A10     : 0x00000000  A11     : 0x000001b5  A12     : 0x00000001  A13     : 0x00000000
A14     : 0x3ffaff6c  A15     : 0x00000002  SAR     : 0x00000004  EXCCAUSE: 0x0000001c
EXCVADDR: 0x00000000  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xfffffffa


Backtrace:0x400d557c:0x3ffb7ea00x400d5700:0x3ffb7ee0 0x400d5759:0x3ffb7f10 0x400d57e7:0x3ffb7f40 0x40089a4d:0x3ffb7f70
0x400d557c: rc522_read at E:\ESP32_Projects\RFID_RC522\build/../components/RC522_RFID/RC522.c:91
 (inlined by) rc522_clear_bitmask at E:\ESP32_Projects\RFID_RC522\build/../components/RC522_RFID/RC522.c:105
 (inlined by) rc522_card_write at E:\ESP32_Projects\RFID_RC522\build/../components/RC522_RFID/RC522.c:337

0x400d5700: rc522_request at E:\ESP32_Projects\RFID_RC522\build/../components/RC522_RFID/RC522.c:400

0x400d5759: rc522_get_tag at E:\ESP32_Projects\RFID_RC522\build/../components/RC522_RFID/RC522.c:433

0x400d57e7: rc522_task at E:\ESP32_Projects\RFID_RC522\build/../components/RC522_RFID/RC522.c:661

0x40089a4d: vPortTaskWrapper at C:/ESPIDF32/esp-idf/components/freertos/port/xtensa/port.c:131

i would definitely personally take interest in this and try and fix this, but before i do that, i thought it is better to point it out to you so you can shed some light on this.
Thanks a ton in advance
Cheers!

Use interrupts instead of hot idling

Currently, when waiting for a tag, the task is put to sleep for 100ms and then activated again. The MFRC522 module has a IRQ pin and we could use an interrupt when the tag is present.

Unexpected primary token before '.'

Hi!

I'm trying to use this library with a Heltec WiFi LoRa 32 v2 board that also has an SPI screen attached.

When compiling the code, I'm seeing the following output from Platformio:

src/main.cpp: In function 'void app_main()':
src/main.cpp:95:9: error: expected primary-expression before '.' token
         .spi.host = VSPI_HOST,
         ^
src/main.cpp:96:9: error: expected primary-expression before '.' token
         .spi.miso_gpio = 25,
         ^
src/main.cpp:97:9: error: expected primary-expression before '.' token
         .spi.mosi_gpio = 23,
         ^
src/main.cpp:98:9: error: expected primary-expression before '.' token
         .spi.sck_gpio = 19,
         ^
src/main.cpp:99:9: error: expected primary-expression before '.' token
         .spi.sda_gpio = 22,
         ^
src/main.cpp:102:19: error: 'config' was not declared in this scope
     rc522_create(&config, &scanner);
                   ^~~~~~
src/main.cpp:102:19: note: suggested alternative: 'confstr'
     rc522_create(&config, &scanner);
                   ^~~~~~
                   confstr
src/main.cpp:94:20: warning: unused variable 'rc522_config' [-Wunused-variable]
     rc522_config_t rc522_config = {
                    ^~~~~~~~~~~~

The full application code is as follows:

/*******************************************************************************
 * 
 * ttn-esp32 - The Things Network device library for ESP-IDF / SX127x
 * 
 * Copyright (c) 2018 Manuel Bleichenbacher
 * 
 * Licensed under MIT License
 * https://opensource.org/licenses/MIT
 *
 * Sample program showing how to send and receive messages.
 *******************************************************************************/

#include "freertos/FreeRTOS.h"
#include "esp_event.h"
#include "driver/gpio.h"
#include "nvs_flash.h"

#include "TheThingsNetwork.h"

// NOTE:
// The LoRaWAN frequency and the radio chip must be configured by running 'idf.py menuconfig'.
// Go to Components / The Things Network, select the appropriate values and save.

// Copy the below hex strings from the TTN console (Applications > Your application > End devices
// > Your device > Activation information)

// AppEUI (sometimes called JoinEUI)
const char *appEui = "0000000000000000";
// DevEUI
const char *devEui = "<DEVEUI>";
// AppKey
const char *appKey = "<APPKEY>";

// Pins and other resources
#define TTN_SPI_HOST      SPI2_HOST
#define TTN_SPI_DMA_CHAN  SPI_DMA_DISABLED
#define TTN_PIN_SPI_SCLK  5
#define TTN_PIN_SPI_MOSI  27
#define TTN_PIN_SPI_MISO  19
#define TTN_PIN_NSS       18
#define TTN_PIN_RXTX      TTN_NOT_CONNECTED
#define TTN_PIN_RST       14
#define TTN_PIN_DIO0      26
#define TTN_PIN_DIO1      35

// RFID Reader Setup
#include <esp_log.h>
#include <inttypes.h>
#include "rc522.h"
static const char* TAG = "rc522-demo";
static rc522_handle_t scanner;

static void rc522_handler(void* arg, esp_event_base_t base, int32_t event_id, void* event_data)
{
    rc522_event_data_t* data = (rc522_event_data_t*) event_data;

    switch(event_id) {
        case RC522_EVENT_TAG_SCANNED: {
                rc522_tag_t* tag = (rc522_tag_t*) data->ptr;
                ESP_LOGI(TAG, "Tag scanned (sn: %" PRIu64 ")", tag->serial_number);
            }
            break;
    }
}

static TheThingsNetwork ttn;

const unsigned TX_INTERVAL = 30;
static uint8_t msgData[] = "Hello, world";


void sendMessages(void* pvParameter)
{
    while (1) {
        printf("Sending message...\n");
        TTNResponseCode res = ttn.transmitMessage(msgData, sizeof(msgData) - 1);
        printf(res == kTTNSuccessfulTransmission ? "Message sent.\n" : "Transmission failed.\n");

        vTaskDelay(TX_INTERVAL * pdMS_TO_TICKS(1000));
    }
}

void messageReceived(const uint8_t* message, size_t length, ttn_port_t port)
{
    printf("Message of %d bytes received on port %d:", length, port);
    for (int i = 0; i < length; i++)
        printf(" %02x", message[i]);
    printf("\n");
}

extern "C" void app_main(void)
{
    // Configure the RFID Reader
    rc522_config_t rc522_config = {
        .spi.host = VSPI_HOST,
        .spi.miso_gpio = 25,
        .spi.mosi_gpio = 23,
        .spi.sck_gpio = 19,
        .spi.sda_gpio = 22,
    };

    rc522_create(&config, &scanner);
    rc522_register_events(scanner, RC522_EVENT_ANY, rc522_handler, NULL);
    rc522_start(scanner);
    esp_err_t err;
    // Initialize the GPIO ISR handler service
    err = gpio_install_isr_service(ESP_INTR_FLAG_IRAM);
    ESP_ERROR_CHECK(err);
    
    // Initialize the NVS (non-volatile storage) for saving and restoring the keys
    err = nvs_flash_init();
    ESP_ERROR_CHECK(err);

    // Initialize SPI bus
    spi_bus_config_t spi_bus_config;
    spi_bus_config.miso_io_num = TTN_PIN_SPI_MISO;
    spi_bus_config.mosi_io_num = TTN_PIN_SPI_MOSI;
    spi_bus_config.sclk_io_num = TTN_PIN_SPI_SCLK;
    spi_bus_config.quadwp_io_num = -1;
    spi_bus_config.quadhd_io_num = -1;
    spi_bus_config.max_transfer_sz = 0;
    err = spi_bus_initialize(TTN_SPI_HOST, &spi_bus_config, TTN_SPI_DMA_CHAN);
    ESP_ERROR_CHECK(err);

    // Configure the SX127x pins
    ttn.configurePins(TTN_SPI_HOST, TTN_PIN_NSS, TTN_PIN_RXTX, TTN_PIN_RST, TTN_PIN_DIO0, TTN_PIN_DIO1);


    // The below line can be commented after the first run as the data is saved in NVS
    ttn.provision(devEui, appEui, appKey);

    // Register callback for received messages
    ttn.onMessage(messageReceived);

//    ttn.setAdrEnabled(false);
//    ttn.setDataRate(kTTNDataRate_US915_SF7);
//    ttn.setMaxTxPower(14);

    printf("Joining...\n");
    if (ttn.join())
    {
        printf("Joined.\n");
        xTaskCreate(sendMessages, "send_messages", 1024 * 4, (void* )0, 3, nullptr);
    }
    else
    {
        printf("Join failed. Goodbye\n");
    }
}

Without the rc522 library, the code compiles and connects to The Things Network.

I want to trigger a message that sends the card UID to TTN when someone taps a card to the reader, like the one at https://www.youtube.com/watch?v=etzn2lIEOX4&ab_channel=1nformatica but using LoRaWAN instead of LoRa.

I'm well out of my depth on this now, so any help you can provide would be more than welcome! :D

Connect RC522 to ESP32 Cam (AI Thinker)

I want to connect RC522 to ESP32 Cam (AI Thinker). Almost all pins are used, So I am running SD card in SPI mode and connected RC522 with same SD Card SPI with a separate CS pin. But I am receiving error spi_bus_initialize(756): SPI bus already initialized.

How to add RC522 to existing SPI bus?

Log
I (29) boot: ESP-IDF v4.4 2nd stage bootloader
I (29) boot: compile time 02:26:29
I (29) boot: chip revision: 1
I (32) boot_comm: chip revision: 1, min. bootloader chip revision: 0
I (39) boot.esp32: SPI Speed : 40MHz
I (43) boot.esp32: SPI Mode : DIO
I (48) boot.esp32: SPI Flash Size : 4MB
I (52) boot: Enabling RNG early entropy source...
I (58) boot: Partition Table:
I (61) boot: ## Label Usage Type ST Offset Length
I (69) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (76) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (84) boot: 2 factory factory app 00 00 00010000 00100000
I (91) boot: End of partition table
I (95) boot_comm: chip revision: 1, min. application chip revision: 0
I (102) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=101d8h ( 66008) map
I (135) esp_image: segment 1: paddr=00020200 vaddr=3ffb0000 size=026ach ( 9900) load
I (139) esp_image: segment 2: paddr=000228b4 vaddr=40080000 size=0d764h ( 55140) load
I (163) esp_image: segment 3: paddr=00030020 vaddr=400d0020 size=29350h (168784) map
I (225) esp_image: segment 4: paddr=00059378 vaddr=4008d764 size=04cc8h ( 19656) load
I (233) esp_image: segment 5: paddr=0005e048 vaddr=50000000 size=00010h ( 16) load
I (242) boot: Loaded app from partition at offset 0x10000
I (242) boot: Disabling RNG early entropy source...
I (256) psram: This chip is ESP32-D0WD
I (258) spiram: Found 64MBit SPI RAM device
I (258) spiram: SPI RAM mode: flash 40m sram 40m
I (261) spiram: PSRAM initialized, cache is in low/high (2-core) mode.
I (268) cpu_start: Pro cpu up.
I (272) cpu_start: Starting app cpu, entry point is 0x40081380
0x40081380: call_start_cpu1 at /home/v-robotics/esp6/esp-idf/components/esp_system/port/cpu_start.c:156

I (0) cpu_start: App cpu up.
I (1163) spiram: SPI SRAM memory test OK
I (1171) cpu_start: Pro cpu start user code
I (1171) cpu_start: cpu freq: 160000000
I (1171) cpu_start: Application information:
I (1174) cpu_start: Project name: main
I (1179) cpu_start: App version: 1
I (1183) cpu_start: Compile time: Aug 3 2022 02:26:18
I (1189) cpu_start: ELF file SHA256: b8fb734b42121eed...
I (1195) cpu_start: ESP-IDF: v4.4
I (1201) heap_init: Initializing. RAM available for dynamic allocation:
I (1208) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (1214) heap_init: At 3FFB3068 len 0002CF98 (179 KiB): DRAM
I (1220) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (1226) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (1233) heap_init: At 4009242C len 0000DBD4 (54 KiB): IRAM
I (1240) spiram: Adding pool of 4095K of external SPI memory to heap allocator
I (1249) spi_flash: detected chip: generic
I (1252) spi_flash: flash io: dio
I (1258) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (1267) spiram: Reserving pool of 32K of internal memory for DMA/internal allocations
I (1277) ESP-RC522: Initialized (firmware: 0x92)

SD card mounting...I (1277) SDSPI: Initializing SD card
I (1287) SDSPI: Using SPI peripheral
E (1287) spi: spi_bus_initialize(756): SPI bus already initialized.
E (1297) SDSPI: Failed to initialize bus.
I (1307) SDSPI: Opening file
E (1307) SDSPI: Failed to open file for writing
I (22197) SDSPI: Tag: 0x50 0x41 0xc5 0xd4 0
I (23517) SDSPI: Tag: 0x50 0x41 0xc5 0xd4 0

Code
// Pin mapping
#define SD_MISO 2
#define SD_MOSI 15
#define SD_SCLK 14
#define SD_CS 	13
#define RC522_SS                   (13)
void app_main(void)
{
  rfid_init();
  esp_err_t err = SD_Mount(); 
}


void rfid_init(){
  // RFID init
  const rc522_start_args_t start_args = {
        .miso_io  = SD_MISO,
        .mosi_io  = SD_MOSI,
        .sck_io   = SD_SCLK,
        .sda_io   = RC522_SS,
        .callback = &rfidtag_handler,

        // Uncomment next line for attaching RC522 to SPI2 bus. Default is VSPI_HOST (SPI3)
        .spi_host_id = HSPI_HOST
    };
    
    rc522_start(start_args);
}

void rfidtag_handler(uint8_t* sn) { // serial number is always 5 bytes long
    ESP_LOGI(TAG, "Tag: %#x %#x %#x %#x %#x",
        sn[0], sn[1], sn[2], sn[3], sn[4]
    );
}


// Mount SD Card
int SD_Mount(){
  //GPIO_SetDigitalIn(SD_MOSI);
  
  esp_err_t ret;
  esp_vfs_fat_sdmmc_mount_config_t mount_config = {
    .format_if_mount_failed = true,
    .max_files = 5,
    .allocation_unit_size = 16 * 1024
  };
  ESP_LOGI(TAG, "Initializing SD card");
  ESP_LOGI(TAG, "Using SPI peripheral");
  spi_bus_config_t bus_cfg = {
        .mosi_io_num = SD_MOSI,
        .miso_io_num = SD_MISO,
        .sclk_io_num = SD_SCLK,
        .quadwp_io_num = -1,
        .quadhd_io_num = -1,
        .max_transfer_sz = 4000,
  };
  ret = spi_bus_initialize(host.slot, &bus_cfg, SPI_DMA_CHAN);
  if (ret != ESP_OK) {
        ESP_LOGE(TAG, "Failed to initialize bus.");
        return 0;
  }
  // This initializes the slot without card detect (CD) and write protect (WP) signals.
  // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
  sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
  slot_config.gpio_cs = SD_CS;
  slot_config.host_id = host.slot;

  ret = esp_vfs_fat_sdspi_mount(mount_point, &host, &slot_config, &mount_config, &card);

  if (ret != ESP_OK) {
        if (ret == ESP_FAIL) {
            ESP_LOGE(TAG, "Failed to mount filesystem. "
                     "If you want the card to be formatted, set the EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option.");
        } else {
            ESP_LOGE(TAG, "Failed to initialize the card (%s). "
                     "Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(ret));
        }
        return 0;
  }

  // Card has been initialized, print its properties
  sdmmc_card_print_info(stdout, card);
  
  //test();
  
  return 1;
}

No such file or directory

hello!
Prompt for file not found,The content is as follows:
../main/ESP32_LittleVGL_V8.c:17:10: fatal error: rc522.h: No such file or directory
#include "rc522.h"

But I have made sure to include it

pecial keyA in our project.

Hi
We use this library in our project.
We use special keyA in our project.

Where in the library have abobija defined the value of keyA ( or even keyB )???????????

We need to change it in our project .

Core panic if rc522 not connected

If rfid-rc522 not connected, even if checking return values of the API, core panic occures. Looking in the code, this is because of these functions:

static uint8_t* rc522_read_n(rc522_handle_t rc522, uint8_t addr, uint8_t n)
{
    uint8_t* buffer = (uint8_t*) malloc(n); // FIXME: memcheck
    esp_err_t ret;
    switch(rc522->config->transport) {
        case RC522_TRANSPORT_SPI:
            ret = rc522_spi_receive(rc522, buffer, n, addr);
            break;
        case RC522_TRANSPORT_I2C:
            ret = rc522_i2c_receive(rc522, buffer, n, addr);
            break;
        default:
            ESP_LOGE(TAG, "read: Unknown transport");
            ret = ESP_ERR_INVALID_STATE; // unknown transport
    }
    if(ESP_OK != ret) {
        free(buffer);
        buffer = NULL;
        ESP_LOGE(TAG, "Failed to read data (err: %s)", esp_err_to_name(ret));
    }
    return buffer;
}

static inline uint8_t rc522_read(rc522_handle_t rc522, uint8_t addr)
{
    uint8_t* buffer = rc522_read_n(rc522, addr, 1);
    uint8_t res = buffer[0];
    free(buffer);

    return res;
}

The most interesting here is that rc522_read_n will return NULL if rc522 not connected, but rc522_read dereferences returned pointer without checking if it's NULL in line

uint8_t res = buffer[0]; // buffer[0] is dereference of NULL

Also, if NULL returned, as we can see, rc522_read_n calls free to free the buffer, but rc522_read do the same on it's own, making another potential problem which is double free.

Fixing this will allow to repair in case rc522 not connected and will not cause esp32 to indefinetely restart in case of problems with sensor.

How this may look:

static inline uint8_t rc522_read(rc522_handle_t rc522, uint8_t addr)
{
    uint8_t* buffer = rc522_read_n(rc522, addr, 1);
    
    // If buffer is NULL, it's already freed and also we don't want to dereference it, so just returning some value indicating that there are some errors. All functions using this should check if return value equal to that 
    if (buffer == NULL) return 0;
    
    uint8_t res = buffer[0];
    free(buffer);

    return res;
}

ESP32S3 Compability

I encountered a Problem while Using the ESP32S3. The device prohibits a MISO and MOSI datastream to be active at the same time in Half_Duplex. Therefore the function rc522_read_n crashed.

I first tried to make it work in full duplex but the "0x26" command was not send to the chip. After hours of being to stupid to solve the problem I decided to look into the half_duplex problem. The solution was fairly simple albeit not elegant. Just leaving this here if someone needs a quick and dirty fix.

static uint8_t* rc522_read_n(uint8_t addr, uint8_t n) {
    if (n <= 0) {
        return NULL;
    }

    spi_transaction_t t;
    memset(&t, 0, sizeof(t));

    uint8_t* buffer = (uint8_t*) malloc(n);
    
    t.flags = SPI_TRANS_USE_TXDATA;
    t.length = 8;
    t.tx_data[0] = ((addr << 1) & 0x7E) | 0x80;
    t.rxlength = 8 * n;
    t.rx_buffer = NULL;

    esp_err_t ret = spi_device_transmit(hndl->spi, &t);
    assert(ret == ESP_OK);

    t.flags = 0x00;
    t.length = 8;
    t.tx_buffer = NULL;
    t.rxlength = 8 * n;
    t.rx_buffer = buffer;

    ret = spi_device_transmit(hndl->spi, &t);
    assert(ret == ESP_OK);

    return buffer;
}

Cannot figure out how to interface mfrc522 with AI-Thinker esp32cam through SPI.

This is after applying pull requests #20, #22, #24

The pin map:

/*
 * SPI for sdcard of esp32-cam and rc522
*/
#define SPI_MISO 2
#define SPI_MOSI 15
#define SPI_SCLK 14

#define SDCARD_CS (13)  // the chip select pin for sdcard

#define RC522_SS (16)  // the chip select pin for scanner

sdcard is initialized in the following way:

esp_err_t init_sd_card(sdmmc_card_t **out)
{

    sdmmc_host_t host = SDSPI_HOST_DEFAULT();

    spi_bus_config_t bus_cfg = {
        .mosi_io_num = SPI_MOSI,
        .miso_io_num = SPI_MISO,
        .sclk_io_num = SPI_SCLK,
        .quadwp_io_num = -1,
        .quadhd_io_num = -1,
        .max_transfer_sz = 4000,
    };

    esp_err_t ret = spi_bus_initialize(host.slot, &bus_cfg, SPI2_HOST);
    if (ret != ESP_OK)
    {
        ESP_LOGE(TAG, "Failed to initialize bus.");
        return ret;
    }

    sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
    slot_config.gpio_cs = SDCARD_CS;
    slot_config.host_id = host.slot;

    esp_vfs_fat_sdmmc_mount_config_t mount_config = {
        .format_if_mount_failed = true,
        .max_files = 5,
        .allocation_unit_size = 16 * 1024};

    sdmmc_card_t *card;
    ret = esp_vfs_fat_sdspi_mount(MOUNT_POINT, &host, &slot_config, &mount_config, &card);
    if (ret != ESP_OK)
    {
        return ret;
    }
    sdmmc_card_print_info(stdout, card);
    *out = card;

    return ESP_OK;
}

rc522 is initialized in the following way:

esp_err_t initialize_rc522(rc522_handle_t *out)
{
    rc522_config_t config = {
#if defined CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
        .spi.host = HSPI_HOST,
#else
        .spi.host = SPI2_HOST
#endif
        // .spi.miso_gpio = SPI_MISO,
        // .spi.mosi_gpio = SPI_MOSI,
        // .spi.sck_gpio = SPI_SCLK,
        .spi.sda_gpio = RC522_SS,
        .spi.bus_is_initialized = true,  // use existing spi bus created for sdcard
    };

    esp_err_t ret = ESP_OK;
    if (ESP_OK == (ret = rc522_create(&config, &scanner)))
    {
        *out = scanner;

        ESP_LOGI(TAG, "Registering event handler for rc522 events.");
        rc522_register_events(scanner, RC522_EVENT_ANY, rc522_handler, NULL);

        ESP_LOGI(TAG, "Starting the rc522 scanner.");
        if (ESP_OK != (ret = rc522_start(scanner)))
        {
            ESP_LOGE(TAG, "Error %s from rc522_start", esp_err_to_name(ret));
        }
    }
    else
    {
        ESP_LOGE(TAG, "Couldn't create rc522 scanner. %s", esp_err_to_name(ret));
    }

    return ret;
}

The above function calling block:

{  
   uint8_t sd_valid = 0; // 1 if card is valid sdmmc_card_t else 0
    sdmmc_card_t *card = NULL;

    uint8_t rfid_scanner_valid = 0; // 1 if rfid scanner is valid rc522 scanner else 0
    rc522_handle_t scanner = NULL;

    /** Not using when using rc522 as both use SPI protocol */
    if (USE_ESP32CAM == 1)
    {

        // initialize camera
        ESP_LOGI(TAG, "Initializing camera for taking photo.\n");
        camera_init();

        // taking a photo
        ESP_LOGI(TAG, "Taking a photo");
        camera_capture();

        if (ESP_OK == init_sd_card(&card))
        {
            sd_valid = 1;
        }
    }

    if (USE_RC522 == 1)
    {
        // initialize rfid stuffs
        ESP_LOGI(TAG, "Initializing rc522 for rfid scan.\n");
        if (ESP_OK == initialize_rc522(&scanner))
            rfid_scanner_valid = 1;
    }
    
    // ...
    // some other logic to blink leds to keep main loop alive
    //...
}

Log for initialization of sdcard followed by rc522

␛[0;32mI (3513) RFID Based Attendance System: Initializing camera for taking photo.
␛[0m
␛[0;32mI (3523) gpio: GPIO[25]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:2 ␛[0m
␛[0;32mI (3533) cam_hal: cam init ok␛[0m
␛[0;32mI (3533) sccb: pin_sda 26 pin_scl 27␛[0m
␛[0;32mI (3543) sccb: sccb_i2c_port=1␛[0m
␛[0;32mI (3543) gpio: GPIO[32]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0 ␛[0m
␛[0;32mI (3583) camera: Detected camera at address=0x30␛[0m
␛[0;32mI (3583) camera: Detected OV2640 camera␛[0m
␛[0;32mI (3583) camera: Camera PID=0x26 VER=0x42 MIDL=0x7f MIDH=0xa2␛[0m
␛[0;32mI (3673) cam_hal: buffer_size: 32768, half_buffer_size: 4096, node_buffer_size: 2048, node_cnt: 16, total_cnt: 93␛[0m
␛[0;32mI (3673) cam_hal: Allocating 384000 Byte frame buffer in PSRAM␛[0m
␛[0;32mI (3683) cam_hal: cam config ok␛[0m
␛[0;32mI (3683) ov2640: Set PLL: clk_2x: 0, clk_div: 0, pclk_auto: 0, pclk_div: 12␛[0m
␛[0;32mI (3763) RFID Based Attendance System: Taking a photo␛[0m
␛[0;32mI (3963) gpio: GPIO[13]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0 ␛[0m
␛[0;32mI (4003) sdspi_transaction: cmd=52, R1 response: command not supported␛[0m
␛[0;32mI (4203) sdspi_transaction: cmd=5, R1 response: command not supported␛[0m
Name: SD2G
Type: SDSC
Speed: 20.00 MHz (limit: 20.00 MHz)
Size: 1892MB
CSD: ver=1, sector_size=512, capacity=3874816 read_bl_len=10
SSR: bus_width=1
␛[0;32mI (4993) RFID Based Attendance System: Initializing rc522 for rfid scan.
␛[0m
␛[0;32mI (5003) RFID Based Attendance System: Registering event handler for rc522 events.␛[0m
␛[0;32mI (5003) RFID Based Attendance System: Starting the rc522 scanner.␛[0m
␛[0;32mI (5013) rc522: Initialized (firmware v2.0)␛[0m
Guru Meditation Error: Core  0 panic'ed (StoreProhibited). Exception was unhandled.

Core  0 register dump:
PC      : 0x4008bafb  PS      : 0x00060a30  A0      : 0x800818b5  A1      : 0x3ffbd210
A2      : 0x00000400  A3      : 0x3ffe46bc  A4      : 0x00000100  A5      : 0x00060c23  
A6      : 0xb33fffff  A7      : 0xb33fffff  A8      : 0xffffffff  A9      : 0x00000000
A10     : 0x000000ff  A11     : 0x00060c23  A12     : 0x00060c20  A13     : 0x3ffbd268  
A14     : 0x00000000  A15     : 0x00000000  SAR     : 0x0000000c  EXCCAUSE: 0x0000001d
EXCVADDR: 0xffffffff  LBEG    : 0x4008ce94  LEND    : 0x4008ceb0  LCOUNT  : 0xffffffff  


Backtrace: 0x4008baf8:0x3ffbd210 0x400818b2:0x3ffbd230 0x400dada0:0x3ffbd250 0x40093869:0x3ffbd290

Different Serials

Hello, I read an RFID card using this library and received the value 1007534008355, which in hexadecimal is EA 95B4 E823. However, when I read the same card with the Arduino library, it provides the following value: 95 B4 E8 23. How can I convert these values to each other?

Support I2C

I wonder if it is possible to support both SPI and I2C.

I try to connect a ESP32-LyraT V4.3 which does not support SPI.

Why is device accessed using SPI_DEVICE_HALFDUPLEX?

Hi,

First of all, thanks for your work on the library.

I'm trying to use an rc522 on the same bus as an sd card reader through sdspi. I therefore need a single bus configuration that works for both devices. Unfortunately, in practice, sdspi requires setting up the bus to use DMA as far as I can tell (or buffers will be very very small) and devices that use SPI_DEVICE_HALFDUPLEX are not compatible with that (it's slightly more complicated than that actually[1]).

I've tried to remove that flag but then my tags don't get recognized anymore. Initialization (including tests) succeeds. I've tried to understand why it was used but I've got absolutely no idea and I found no reference whatsoever.

Can you shine a light on this matter? Is there a specific reason/timing/reference for this?

Thanks!

[1] https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/spi_master.html#spi-known-issues

Read write test failing causes `Failed to write data (err: ESP_ERR_INVALID_STATE)`

I am using an esp32s2 mini, and I bought my card readers from both amazon and aliexpress and it seems both work with the arduino library for
rfid.

I used the following arduino code:

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN 3 // Configurable, see typical pin layout above
#define SS_PIN 5  // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
void setup()
{
  Serial.begin(115200); // Initialize serial communications with the PC
  while (!Serial)
    ; // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  Serial.println("Testing out connecting to SPI ");
  
  SPI.begin(7, 11, 9, 5); // Init SPI bus
  // SPI.begin(); // Init SPI bus
  Serial.println("Testing out connecting to mfrc");
  mfrc522.PCD_Init();                // Init MFRC522
  delay(4);                          // Optional delay. Some board do need more time after init to be ready, see Readme
  mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
  Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
}

void loop()
{
  if (!mfrc522.PICC_IsNewCardPresent())
  {
    return;
  }
  if (!mfrc522.PICC_ReadCardSerial())
  {
    return;
  }
  MFRC522::Uid *uid = &mfrc522.uid;

  for (byte i = 0; i < uid->size; i++)
  {
    // After F hex wraps around to 10 so print space and 0 to go to new
    if (uid->uidByte[i] < 0x10)
      Serial.print(F(" 0"));
    else
      Serial.print(F(" "));
    Serial.print(uid->uidByte[i], HEX);
  }
  Serial.print("\n");
}

However when using this library, I seem to be getting the log: Read/write test failed followed by repeating messages of:

E (2220) rc522: write: Unknown transport
E (2220) rc522: Failed to write data (err: ESP_ERR_INVALID_STATE)

Code:

#include <esp_log.h>
#include <inttypes.h>
#include "rc522.h"

static const char *TAG = "rc522-demo";
static rc522_handle_t scanner;

static void rc522_handler(void *arg, esp_event_base_t base, int32_t event_id, void *event_data)
{
	rc522_event_data_t *data = (rc522_event_data_t *)event_data;

	switch (event_id)
	{
	case RC522_EVENT_TAG_SCANNED:
	{
		rc522_tag_t *tag = (rc522_tag_t *)data->ptr;
		ESP_LOGI(TAG, "Tag scanned (sn: %" PRIu64 ")", tag->serial_number);
	}
	break;
	}
}

void app_main()
{
	vTaskDelay(pdMS_TO_TICKS(2000));
	ESP_LOGI(TAG, "Starting up");
	rc522_config_t config = {
		.spi.host = SPI3_HOST,
		.spi.miso_gpio = GPIO_NUM_11,
		.spi.mosi_gpio = GPIO_NUM_9,
		.spi.sck_gpio = GPIO_NUM_7,
		.spi.sda_gpio = GPIO_NUM_5,
	};
	rc522_create(&config, &scanner);
	rc522_register_events(scanner, RC522_EVENT_ANY, rc522_handler, NULL);
	rc522_start(scanner);

	// while (1)
	// {
	// 	ESP_LOGI(TAG, "Done 1 tick");
	// 	vTaskDelay(pdMS_TO_TICKS(2000));
	// }
}

Having a deeper look at the source code, it turns out the invalid state error is there because in rc522_start if the test fails, rc522_destroy is called causing the SPI device to be destroyed but the task still remains for some reason.

I had a further look and changed the source of the read write test to:

        const uint8_t test_addr = RC522_MOD_WIDTH_REG, test_val = 0x25;
        uint8_t pass = 0;

        for (uint8_t i = test_val; i < test_val + 2; i++)
        {
            err = rc522_write(rc522, test_addr, i);

            if (err == ESP_OK)
            {
                err = rc522_read(rc522, test_addr, &tmp);
                if (err != ESP_OK)
                {
                    ESP_LOGE(TAG, "Read test failed (err: %s)", esp_err_to_name(err));
                }

                if (err == ESP_OK && tmp == i)
                {
                    pass = 1;
                }
                else
                {
                    ESP_LOGE(TAG, "Invalid return %d, expected %d", tmp, i);
                }
            }
            else
            {
                ESP_LOGE(TAG, "Write test failed (err: %s)", esp_err_to_name(err));
            }

            if (pass != 1)
            {
                ESP_LOGE(TAG, "Read/write test failed");
                // For some reason the task still goes on in the background, giving ESP_ERR_INVALID_STATE when trying to write
                rc522_destroy(rc522);

                return err;
            }
        }

I get the output: Invalid return 0, expected 37. So im guessing either the read or the write couldve failed here.
I might try testing a known readable value, on the datasheet it says the address 37h is the VersionReg but im not sure how to use the SPI interface especially with rc522_read sorry 🥲.
I can't use pin 19 for sck as recommended by the library and the video as it isnt exposed through the dev board im using.
For now im throwing more hardware at the problem and using the pn532 😆.

Crash when rc522 is not connected with multiple devices on same SPI bus

I have two devices on the SPI bus and rc522 is initialized later with .spi.bus_is_initialized = true. However, when rc522 is not connected, the driver throws an error and esp32 crashes.

Is it possible that it does not crash if rc522 is not connected? Thanks.

The error log is:

E (7397) spi_master: spi_master_deinit_driver(272): not all CSses freed
I (7407) gpio: GPIO[23]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (7417) gpio: GPIO[19]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (7427) gpio: GPIO[18]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0

assert failed: spi_bus_deinit_lock spi_bus_lock.c:599 (atomic_load(&lock->dev[i]) == (intptr_t)NULL)

Backtrace: 0x40082005:0x3ffcd710 0x4008b665:0x3ffcd730 0x400918e1:0x3ffcd750 0x401174c9:0x3ffcd870 0x40116bdd:0x3ffcd890 0x4010fad1:0x3ffcd8b0 0x401100f4:0x3ffcd8d0 0x4011026d:0x3ffcd8f0 0x400d9995:0x3ffcd920 0x400d99db:0x3ffcd970 0x4008e635:0x3ffcda00
0x40082005: panic_abort at E:/Software/Espressif/frameworks/esp-idf-v4.4.5/components/esp_system/panic.c:408

After run, it have crash

error

This is esp32s3 chip with 2 CPU core(s), WiFi/BLE, silicon revision 0, 2MB external flash
Minimum free heap size: 376784 bytes
E (341) rc522: Read/write test failed
I (341) gpio: GPIO[16]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
E (441) rc522: write: Unknown transport
E (441) rc522: Failed to write data (err: ESP_ERR_INVALID_STATE)
E (441) rc522: write: Unknown transport
E (441) rc522: Failed to write data (err: ESP_ERR_INVALID_STATE)
E (451) rc522: read: Unknown transport
E (451) rc522: Failed to read data (err: ESP_ERR_INVALID_STATE)
Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.

Core  0 register dump:
PC      : 0x42006147  PS      : 0x00060530  A0      : 0x820062e4  A1      : 0x3fcead70  
0x42006147: rc522_read at /home/develop/temp/test_nfc/main/rc522.c:87
 (inlined by) rc522_clear_bitmask at /home/develop/temp/test_nfc/main/rc522.c:100                                                                                                                                                  
 (inlined by) rc522_card_write at /home/develop/temp/test_nfc/main/rc522.c:312                                                                                                                                                     
                                                                                                                                                                                                                                   
A2      : 0x3fce9af8  A3      : 0x0000000c  A4      : 0x00000030  A5      : 0xfffffff7  
A6      : 0x3fceade0  A7      : 0xffffff80  A8      : 0x82006147  A9      : 0x3fcead50  
A10     : 0x00000000  A11     : 0x000001c3  A12     : 0x00000001  A13     : 0x00000000  
A14     : 0x3fce9af8  A15     : 0x00000002  SAR     : 0x00000004  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00000000  LBEG    : 0x400556d5  LEND    : 0x400556e5  LCOUNT  : 0xfffffffa  


Backtrace: 0x42006144:0x3fcead70 0x420062e1:0x3fceadb0 0x42006341:0x3fceade0 0x420063db:0x3fceae10 0x4037de69:0x3fceae40
0x42006144: rc522_read at /home/develop/temp/test_nfc/main/rc522.c:86
 (inlined by) rc522_clear_bitmask at /home/develop/temp/test_nfc/main/rc522.c:100                                                                                                                                                  
 (inlined by) rc522_card_write at /home/develop/temp/test_nfc/main/rc522.c:312                                                                                                                                                     
                                                                                                                                                                                                                                   
0x420062e1: rc522_request at /home/develop/temp/test_nfc/main/rc522.c:365
                                                                                                                                                                                                                                   
0x42006341: rc522_get_tag at /home/develop/temp/test_nfc/main/rc522.c:396
                                                                                                                                                                                                                                   
0x420063db: rc522_task at /home/develop/temp/test_nfc/main/rc522.c:602
                                                                                                                                                                                                                                   
0x4037de69: vPortTaskWrapper at /home/develop/esp/esp-idf/components/freertos/port/xtensa/port.c:142

authenticate function

Hi
When we use authenticate function for a card , we must determine KeyA.
keyA len is 6 bytes.
Default keyA is 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF.
But this is not a good idea in terms of security.
Because hackers can clone my card.
For this reason, I have changed keyA value in my project.

When you read the serial number you have to define keyA value.
So you must have defined keyA value somewhere in your library
Where in this library have you defined the value of keyA ( or even keyB )???????????

Problem while Calling again rc522_start(start_args) After rc522_destroy()

Problem while Calling again rc522_start(start_args) After rc522_destroy() . 2 times reading working properly. After crashes in different errors.

#include <stdio.h>
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#include "rc522.h"

static const char* TAG = "RFID";

void rfidtag_handler(uint8_t* sn);

uint8_t rfidready=0;

const rc522_start_args_t start_args = {
        .miso_io  = RC522_MISO,
        .mosi_io  = RC522_MOSI,
        .sck_io   = RC522_SCK,
        .sda_io   = RC522_SDA,
        .callback = &rfidtag_handler,

        // Uncomment next line for attaching RC522 to SPI2 bus. Default is VSPI_HOST (SPI3)
        //.spi_host_id = HSPI_HOST
    };

void app_main(void)
{
    while(1){
      vTaskDelay(1000 / portTICK_PERIOD_MS);
      printf(".");
      fflush(stdout);
      
      if(rfidready==0){
        rc522_start(start_args);
        rfidready=1;
      }
    }
}

void rfidtag_handler(uint8_t* sn) { // serial number is always 5 bytes long
    ESP_LOGI(TAG, "Tag: %#x %#x %#x %#x %#x",
        sn[0], sn[1], sn[2], sn[3], sn[4]
    );
    
    vTaskDelay(1000 / portTICK_PERIOD_MS);
    rc522_destroy();
    rfidready=0;
}

Log
entry 0x40080694
I (29) boot: ESP-IDF v4.4 2nd stage bootloader
I (29) boot: compile time 08:50:28
I (29) boot: chip revision: 1
I (31) boot_comm: chip revision: 1, min. bootloader chip revision: 0
I (39) boot.esp32: SPI Speed : 40MHz
I (43) boot.esp32: SPI Mode : DIO
I (48) boot.esp32: SPI Flash Size : 4MB
I (52) boot: Enabling RNG early entropy source...
I (58) boot: Partition Table:
I (61) boot: ## Label Usage Type ST Offset Length
I (69) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (76) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (83) boot: 2 factory factory app 00 00 00010000 00100000
I (91) boot: End of partition table
I (95) boot_comm: chip revision: 1, min. application chip revision: 0
I (102) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=09dc4h ( 40388) map
I (125) esp_image: segment 1: paddr=00019dec vaddr=3ffb0000 size=02494h ( 9364) load
I (129) esp_image: segment 2: paddr=0001c288 vaddr=40080000 size=03d90h ( 15760) load
I (138) esp_image: segment 3: paddr=00020020 vaddr=400d0020 size=18ce0h (101600) map
I (177) esp_image: segment 4: paddr=00038d08 vaddr=40083d90 size=08acch ( 35532) load
I (191) esp_image: segment 5: paddr=000417dc vaddr=50000000 size=00010h ( 16) load
I (198) boot: Loaded app from partition at offset 0x10000
I (198) boot: Disabling RNG early entropy source...
I (211) cpu_start: Pro cpu up.
I (212) cpu_start: Starting app cpu, entry point is 0x400810a4
0x400810a4: call_start_cpu1 at /home/v-robotics/esp6/esp-idf/components/esp_system/port/cpu_start.c:156

I (198) cpu_start: App cpu up.
I (226) cpu_start: Pro cpu start user code
I (226) cpu_start: cpu freq: 160000000
I (226) cpu_start: Application information:
I (231) cpu_start: Project name: main
I (235) cpu_start: App version: 1
I (240) cpu_start: Compile time: Aug 4 2022 08:50:14
I (246) cpu_start: ELF file SHA256: 24761f48d297bde2...
I (252) cpu_start: ESP-IDF: v4.4
I (257) heap_init: Initializing. RAM available for dynamic allocation:
I (264) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (270) heap_init: At 3FFB2DB8 len 0002D248 (180 KiB): DRAM
I (276) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (282) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (289) heap_init: At 4008C85C len 000137A4 (77 KiB): IRAM
I (296) spi_flash: detected chip: generic
I (300) spi_flash: flash io: dio
I (305) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (316) ESP-RC522: Initialized (firmware: 0x92)
.I (2246) RFID: Tag: 0x50 0x41 0xc5 0xd4 0
.I (3256) RFID: RC522 Destroy...
.I (3356) gpio: GPIO[12]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (3356) gpio: GPIO[15]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (3356) gpio: GPIO[2]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (3366) gpio: GPIO[14]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
.I (4376) ESP-RC522: Initialized (firmware: 0x92)
..I (6436) RFID: Tag: 0x50 0x41 0xc5 0xd4 0
I (6456) RFID: Tag: 0x50 0x41 0xc5 0xd4 0
.I (7436) RFID: RC522 Destroy...
I (7466) RFID: RC522 Destroy...
I (7536) gpio: GPIO[12]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (7536) gpio: GPIO[15]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (7536) gpio: GPIO[2]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (7546) gpio: GPIO[14]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
Guru Meditation Error: Core 0 panic'ed (LoadProhibited). Exception was unhandled.

Core 0 register dump:
PC : 0x400d53f7 PS : 0x00060c30 A0 : 0x800d4ea1 A1 : 0x3ffb8280
0x400d53f7: rc522_destroy at /home/v-robotics/esp6/IoTAttendance/test/RFID/build/../main/rc522.c:434

A2 : 0x00000000 A3 : 0x3ffaf89c A4 : 0x3f403084 A5 : 0x00001d2a
A6 : 0x3f40304c A7 : 0x00000050 A8 : 0x800d53f5 A9 : 0x3ffb8260
A10 : 0x0000000a A11 : 0x3ffb11b8 A12 : 0x3f403084 A13 : 0x3ffb8280
A14 : 0x3ffb8260 A15 : 0x0000000c SAR : 0x00000004 EXCCAUSE: 0x0000001c
EXCVADDR: 0x00000008 LBEG : 0x400014fd LEND : 0x4000150d LCOUNT : 0xfffffffe

Backtrace:0x400d53f4:0x3ffb82800x400d4e9e:0x3ffb82a0 0x400d532d:0x3ffb82f0 0x4008859d:0x3ffb8310
0x400d53f4: rc522_destroy at /home/v-robotics/esp6/IoTAttendance/test/RFID/build/../main/rc522.c:433

0x400d4e9e: rfidtag_handler at /home/v-robotics/esp6/IoTAttendance/test/RFID/build/../main/main.c:44

0x400d532d: rc522_task at /home/v-robotics/esp6/IoTAttendance/test/RFID/build/../main/rc522.c:458 (discriminator 1)

0x4008859d: vPortTaskWrapper at /home/v-robotics/esp6/esp-idf/components/freertos/port/xtensa/port.c:131

ELF file SHA256: 24761f48d297bde2

Rebooting...
ets Jun 8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:6604
ho 0 tail 12 room 4
load:0x40078000,len:14780
load:0x40080400,len:3792
0x40080400: _init at ??:?

entry 0x40080694
I (29) boot: ESP-IDF v4.4 2nd stage bootloader
I (29) boot: compile time 08:50:28
I (29) boot: chip revision: 1
I (31) boot_comm: chip revision: 1, min. bootloader chip revision: 0
I (39) boot.esp32: SPI Speed : 40MHz
I (43) boot.esp32: SPI Mode : DIO
I (48) boot.esp32: SPI Flash Size : 4MB
I (52) boot: Enabling RNG early entropy source...
I (58) boot: Partition Table:
I (61) boot: ## Label Usage Type ST Offset Length
I (69) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (76) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (83) boot: 2 factory factory app 00 00 00010000 00100000
I (91) boot: End of partition table
I (95) boot_comm: chip revision: 1, min. application chip revision: 0
I (102) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=09dc4h ( 40388) map
I (125) esp_image: segment 1: paddr=00019dec vaddr=3ffb0000 size=02494h ( 9364) load
I (129) esp_image: segment 2: paddr=0001c288 vaddr=40080000 size=03d90h ( 15760) load
I (138) esp_image: segment 3: paddr=00020020 vaddr=400d0020 size=18ce0h (101600) map
I (177) esp_image: segment 4: paddr=00038d08 vaddr=40083d90 size=08acch ( 35532) load
I (191) esp_image: segment 5: paddr=000417dc vaddr=50000000 size=00010h ( 16) load
I (198) boot: Loaded app from partition at offset 0x10000
I (198) boot: Disabling RNG early entropy source...
I (211) cpu_start: Pro cpu up.
I (212) cpu_start: Starting app cpu, entry point is 0x400810a4
0x400810a4: call_start_cpu1 at /home/v-robotics/esp6/esp-idf/components/esp_system/port/cpu_start.c:156

I (198) cpu_start: App cpu up.
I (226) cpu_start: Pro cpu start user code
I (226) cpu_start: cpu freq: 160000000
I (226) cpu_start: Application information:
I (231) cpu_start: Project name: main
I (235) cpu_start: App version: 1
I (240) cpu_start: Compile time: Aug 4 2022 08:50:14
I (246) cpu_start: ELF file SHA256: 24761f48d297bde2...
I (252) cpu_start: ESP-IDF: v4.4
I (257) heap_init: Initializing. RAM available for dynamic allocation:
I (264) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (270) heap_init: At 3FFB2DB8 len 0002D248 (180 KiB): DRAM
I (276) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (282) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (289) heap_init: At 4008C85C len 000137A4 (77 KiB): IRAM
I (296) spi_flash: detected chip: generic
I (300) spi_flash: flash io: dio
I (305) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (316) ESP-RC522: Initialized (firmware: 0x92)
....I (4576) RFID: Tag: 0x50 0x41 0xc5 0xd4 0
.I (5586) RFID: RC522 Destroy...
I (5686) gpio: GPIO[12]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (5686) gpio: GPIO[15]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (5686) gpio: GPIO[2]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (5696) gpio: GPIO[14]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
.I (6706) ESP-RC522: Initialized (firmware: 0x92)
assert failed: spi_device_transmit spi_master.c:865 (ret_trans == trans_desc)

Backtrace:0x400818de:0x3ffb80800x400859e1:0x3ffb80a0 0x4008b541:0x3ffb80c0 0x400d80a1:0x3ffb81e0 0x400d4f44:0x3ffb8210 0x400d4f5f:0x3ffb8260 0x400d5250:0x3ffb8290 0x400d52aa:0x3ffb82c0 0x400d5314:0x3ffb82f0 0x4008859d:0x3ffb8310
0x400818de: panic_abort at /home/v-robotics/esp6/esp-idf/components/esp_system/panic.c:402

0x400859e1: esp_system_abort at /home/v-robotics/esp6/esp-idf/components/esp_system/esp_system.c:121

0x4008b541: __assert_func at /home/v-robotics/esp6/esp-idf/components/newlib/assert.c:85

0x400d80a1: spi_device_transmit at /home/v-robotics/esp6/esp-idf/components/driver/spi_master.c:865 (discriminator 1)

0x400d4f44: rc522_write_n at /home/v-robotics/esp6/IoTAttendance/test/RFID/build/../main/rc522.c:93

0x400d4f5f: rc522_write at /home/v-robotics/esp6/IoTAttendance/test/RFID/build/../main/rc522.c:101

0x400d5250: rc522_request at /home/v-robotics/esp6/IoTAttendance/test/RFID/build/../main/rc522.c:343

0x400d52aa: rc522_get_tag at /home/v-robotics/esp6/IoTAttendance/test/RFID/build/../main/rc522.c:375

0x400d5314: rc522_task at /home/v-robotics/esp6/IoTAttendance/test/RFID/build/../main/rc522.c:454

0x4008859d: vPortTaskWrapper at /home/v-robotics/esp6/esp-idf/components/freertos/port/xtensa/port.c:131

ELF file SHA256: 24761f48d297bde2

Rebooting...
ets Jun 8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:6604
ho 0 tail 12 room 4
load:0x40078000,len:14780
load:0x40080400,len:3792
0x40080400: _init at ??:?

entry 0x40080694
I (29) boot: ESP-IDF v4.4 2nd stage bootloader
I (29) boot: compile time 08:50:28
I (29) boot: chip revision: 1
I (31) boot_comm: chip revision: 1, min. bootloader chip revision: 0
I (39) boot.esp32: SPI Speed : 40MHz
I (43) boot.esp32: SPI Mode : DIO
I (48) boot.esp32: SPI Flash Size : 4MB
I (52) boot: Enabling RNG early entropy source...
I (58) boot: Partition Table:
I (61) boot: ## Label Usage Type ST Offset Length
I (69) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (76) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (83) boot: 2 factory factory app 00 00 00010000 00100000
I (91) boot: End of partition table
I (95) boot_comm: chip revision: 1, min. application chip revision: 0
I (102) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=09dc4h ( 40388) map
I (125) esp_image: segment 1: paddr=00019dec vaddr=3ffb0000 size=02494h ( 9364) load
I (129) esp_image: segment 2: paddr=0001c288 vaddr=40080000 size=03d90h ( 15760) load
I (138) esp_image: segment 3: paddr=00020020 vaddr=400d0020 size=18ce0h (101600) map
I (177) esp_image: segment 4: paddr=00038d08 vaddr=40083d90 size=08acch ( 35532) load
I (191) esp_image: segment 5: paddr=000417dc vaddr=50000000 size=00010h ( 16) load
I (198) boot: Loaded app from partition at offset 0x10000
I (198) boot: Disabling RNG early entropy source...
I (211) cpu_start: Pro cpu up.
I (212) cpu_start: Starting app cpu, entry point is 0x400810a4
0x400810a4: call_start_cpu1 at /home/v-robotics/esp6/esp-idf/components/esp_system/port/cpu_start.c:156

I (198) cpu_start: App cpu up.
I (226) cpu_start: Pro cpu start user code
I (226) cpu_start: cpu freq: 160000000
I (226) cpu_start: Application information:
I (231) cpu_start: Project name: main
I (235) cpu_start: App version: 1
I (240) cpu_start: Compile time: Aug 4 2022 08:50:14
I (246) cpu_start: ELF file SHA256: 24761f48d297bde2...
I (252) cpu_start: ESP-IDF: v4.4
I (257) heap_init: Initializing. RAM available for dynamic allocation:
I (264) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (270) heap_init: At 3FFB2DB8 len 0002D248 (180 KiB): DRAM
I (276) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (282) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (289) heap_init: At 4008C85C len 000137A4 (77 KiB): IRAM
I (296) spi_flash: detected chip: generic
I (300) spi_flash: flash io: dio
I (305) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (316) ESP-RC522: Initialized (firmware: 0x92)

esp-idf-rc522 build error

Description

esp-idf-rc522.h throws a build error when added as a component
In file included from /home/abdellah/esp-idf/components/esp-idf-rc522/rc522.c:7: /home/abdellah/esp-idf/components/esp-idf-rc522/rc522.h:8:10: fatal error: driver/spi_master.h: No such file or directory 8 | #include <driver/spi_master.h> | ^~~~~~~~~~~~~~~~~~~~~ compilation terminated. ninja: build stopped: subcommand failed.

Solution

add driver to REQUIRES in CMakeLists.txt
CMakeLists.txt

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.