Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
earthPerson-001 opened this issue Nov 6, 2023 · 0 comments

Comments

@earthPerson-001
Copy link
Contributor

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant