Can not initialize sd card using spi bus

noweare
Posts: 74
Joined: Tue Jul 02, 2019 11:35 am

Can not initialize sd card using spi bus

Postby noweare » Sat Dec 18, 2021 2:42 am

I am using the sd card example in the example folder /esp-idf/examples/storage/sd_card/
I am using a Micro SD Card Adapter Module 6Pin SPI Interface Module
I am using version 4.3.1 of the idf
I am getting errors that the sd card initialization failed.

Just for fun I fired up the arduino IDE and using the esp32-arduino core I could access the micro sd card.




The errors are from calling this function esp_vfs_fat_sdspi_mount(,,,)

here is the output from idf

I (0) cpu_start: Starting scheduler on APP CPU.
I (325) example: Initializing SD card
I (335) gpio: GPIO[5]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
I (335) sdspi_transaction: cmd=52, R1 response: command not supported
I (385) sdspi_transaction: cmd=5, R1 response: command not supported
E (415) sdmmc_sd: sdmmc_check_scr: send_scr returned 0x109
E (415) vfs_fat_sdmmc: sdmmc_card_init failed (0x109).
I (415) gpio: GPIO[5]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0

Here is the code:

Code: Select all

#include <stdio.h>
#include <string.h>
#include <sys/unistd.h>
#include <sys/stat.h>
#include "esp_err.h"
#include "esp_log.h"
#include "esp_vfs_fat.h"
#include "driver/sdspi_host.h"
#include "driver/spi_common.h"
#include "sdmmc_cmd.h"
#include "sdkconfig.h"

#define CONFIG_IDF_TARGET_ESP32 1

#include "driver/sdmmc_host.h"


static const char *TAG = "example";

#define MOUNT_POINT "/sdcard"
#define USE_SPI_MODE
#define SPI_DMA_CHAN    1

#ifdef USE_SPI_MODE
    #if CONFIG_IDF_TARGET_ESP32 
        #define PIN_NUM_MISO 19
        #define PIN_NUM_MOSI 21
        #define PIN_NUM_CLK  18
        #define PIN_NUM_CS   5
    #endif //CONFIG_IDF_TARGET_ESP32
#endif //USE_SPI_MODE

void app_main(void)
{
    esp_err_t ret;
      
    esp_vfs_fat_sdmmc_mount_config_t mount_config = {
        .format_if_mount_failed = false,
        .max_files = 5,
        .allocation_unit_size = 16 * 1024
    };
    
    sdmmc_card_t* card;
    const char mount_point[] = MOUNT_POINT;
    
    ESP_LOGI(TAG, "Initializing SD card");

    sdmmc_host_t host = SDSPI_HOST_DEFAULT();
   
    spi_bus_config_t bus_cfg = {
        .mosi_io_num = PIN_NUM_MOSI,
        .miso_io_num = PIN_NUM_MISO,
        .sclk_io_num = PIN_NUM_CLK,
        .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;
    }
    
    sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
    slot_config.gpio_cs = PIN_NUM_CS;
    slot_config.host_id = host.slot;
    
    ret = esp_vfs_fat_sdspi_mount(mount_point, &host, &slot_config, &mount_config, &card); //this fn causing problem

Any help figuring this out is appreciated. I may download an older version and see if it works.

noweare
Posts: 74
Joined: Tue Jul 02, 2019 11:35 am

Re: Can not initialize sd card using spi bus

Postby noweare » Sun Dec 19, 2021 6:55 pm

So I found the answer to my problem.

In sdmmc_types.h I changed SDMMC_FREQ_DEFAULT from 20000 to 5000 and get past the error.
Only took me a few days on and off to find the solution.

I found it here https://github.com/espressif/esp-idf/issues/2478
it was solved by glissmann commented on Aug 31, 2020

So I hope this helps anyone running into this problem.

Who is online

Users browsing this forum: No registered users and 78 guests