SD SPI mount error with custom wiring

dizcza
Posts: 56
Joined: Tue Sep 07, 2021 6:59 pm

SD SPI mount error with custom wiring

Postby dizcza » Mon Oct 10, 2022 12:32 pm

I have an ESP32 TTGO T8 board with an SD card slot embedded. The schematics are here: https://github.com/LilyGO/TTGO-T8-ESP32 ... v1.7.1.pdf. When I use this slot, SD SPI mode works fine. But when I try to wire an external SD card module HW-125 with SPI interface to custom pins, the sd card inserted in this HW-125 slot cannot be mounted in ESP-IDF but it IS mounted successfully with the "SD" ESP32 Arduino lib. Same pinout. The HW-125 module is powered with 5V.

Here is the ESP-IDF code. I'm using the latest master branch.

Code: Select all

#define SDCARD_MOUNT_CONFIG_DEFAULT() {\
    .format_if_mount_failed = false,\
    .max_files = 10,\
    .allocation_unit_size = SDCARD_ALLOCATION_UNIT_SIZE\
}

static void sdcard_mount_finish(esp_err_t err) {
    if (err == ESP_OK) {
        ESP_LOGI(TAG, "SD card mounted at %s", sdcard_mount_point);
        sdmmc_card_print_info(stdout, m_card);

        /* Print free memory size info */
        printf("Free: %lluMB\n", sdcard_get_free_bytes() / (1 << 20));
    } else {
        ESP_LOGW(TAG, "sdcard_mount failed (%s)",
                esp_err_to_name(err));
    }
}

esp_err_t sdcard_mount_spi(int miso_gpio, int mosi_gpio, int clk_gpio, int cs_gpio) {
    ESP_LOGI(TAG, "Mounting SD SPI: MISO %d, MOSI %d, CLK %d, CS %d", miso_gpio, mosi_gpio, clk_gpio, cs_gpio);
    esp_vfs_fat_sdmmc_mount_config_t mount_config = SDCARD_MOUNT_CONFIG_DEFAULT();

    sdmmc_host_t host = SDSPI_HOST_DEFAULT();
    host.slot = (spi_host_device_t) host.slot;

    spi_bus_config_t bus_cfg = {
        .mosi_io_num = mosi_gpio,
        .miso_io_num = miso_gpio,
        .sclk_io_num = clk_gpio,
        .quadwp_io_num = -1,
        .quadhd_io_num = -1,
        .max_transfer_sz = 4092,
    };

    ESP_ERROR_CHECK(spi_bus_initialize(host.slot, &bus_cfg, SPI_DMA_CH_AUTO));

    // 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 = (gpio_num_t) cs_gpio;
    slot_config.host_id = host.slot;

    esp_err_t err = esp_vfs_fat_sdspi_mount(sdcard_mount_point, &host,
            &slot_config, &mount_config, &m_card);

    sdcard_mount_finish(err);

    return err;
}
Logs.
  • Embedded SD slot. Works fine

    Code: Select all

    I (00:00:00.624) sdcard: Mounting SD SPI: MISO 2, MOSI 15, CLK 14, CS 13
    D (00:00:00.629) spi: SPI2 use gpio matrix.
    D (00:00:00.633) sdspi_host: sdspi_host_init_device: SPI2 cs=13 cd=-1 wp=-1
    D (912) intr_alloc: Connected src 30 to int 13 (cpu 0)
    D (00:00:00.645) spi_hal: eff: 400, limit: 26666k(/2), 0 dummy, -1 delay
    D (00:00:00.652) spi_master: SPI2: New device added to CS5, effective clock: 400kHz
    I (00:00:00.660) gpio: GPIO[13]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0 
    D (00:00:00.670) spi_master: device5 locked the bus
    D (00:00:00.715) sdspi_host: poll_busy: timeout
    D (00:00:00.715) spi_master: device5 release bus
    D (00:00:00.716) spi_master: device5 locked the bus
    D (00:00:00.758) sdspi_host: poll_busy: timeout
    D (00:00:00.759) spi_master: device5 release bus
    D (00:00:00.776) spi_master: device5 locked the bus
    D (00:00:00.816) sdspi_host: poll_busy: timeout
    D (00:00:00.816) spi_master: device5 release bus
    D (00:00:00.836) spi_master: device5 locked the bus
    D (00:00:00.876) sdspi_host: poll_busy: timeout
    D (00:00:00.876) spi_master: device5 release bus
    D (00:00:00.877) sdmmc_sd: SDHC/SDXC card
    D (00:00:00.878) spi_master: device5 locked the bus
    D (00:00:00.923) sdspi_host: poll_busy: timeout
    D (00:00:00.924) spi_master: device5 release bus
    I (00:00:00.924) sdspi_transaction: cmd=5, R1 response: command not supported
    D (00:00:00.929) sdmmc_cmd: cmd=5, sdmmc_req_run returned 0x106
    D (00:00:00.935) sdmmc_io: sdmmc_init_io: io_send_op_cond (1) returned 0x106; not IO card
    D (00:00:00.943) spi_master: device5 locked the bus
    D (00:00:00.988) sdspi_host: poll_busy: timeout
    D (00:00:00.989) spi_master: device5 release bus
    D (00:00:00.989) sdspi_host: data CRC set=1
    D (00:00:00.991) spi_master: device5 locked the bus
    D (00:00:01.036) sdspi_host: poll_busy: timeout
    D (00:00:01.036) spi_master: device5 release bus
    D (00:00:01.036) spi_master: device5 locked the bus
    D (00:00:01.079) sdspi_host: poll_busy: timeout
    D (00:00:01.079) spi_master: device5 release bus
    D (00:00:01.086) spi_master: device5 locked the bus
    D (00:00:01.126) sdspi_host: poll_busy: timeout
    D (00:00:01.126) spi_master: device5 release bus
    D (00:00:01.126) spi_master: device5 locked the bus
    D (00:00:01.169) sdspi_host: poll_busy: timeout
    D (00:00:01.169) spi_master: device5 release bus
    D (00:00:01.170) spi_master: device5 locked the bus
    D (00:00:01.212) sdspi_host: poll_busy: timeout
    D (00:00:01.213) spi_master: device5 release bus
    D (00:00:01.213) sdmmc_common: host_ocr=0x40ff8000 card_ocr=0xc0ff8000
    D (00:00:01.217) sdmmc_common: sdmmc_card_init: host_ocr=40ff8000, card_ocr=c0ff8000
    D (00:00:01.225) sdmmc_init: sdmmc_card_init: card type is SD
    D (00:00:01.231) spi_master: device5 locked the bus
    D (00:00:01.276) sdspi_host: poll_busy: timeout
    D (00:00:01.277) spi_master: device5 release bus
    D (00:00:01.277) spi_master: device5 locked the bus
    D (00:00:01.319) sdspi_host: poll_busy: timeout
    D (00:00:01.320) spi_master: device5 release bus
    D (00:00:01.320) spi_master: device5 locked the bus
    D (00:00:01.362) sdspi_host: poll_busy: timeout
    D (00:00:01.363) spi_master: device5 release bus
    D (00:00:01.363) spi_master: device5 locked the bus
    D (00:00:01.405) sdspi_host: poll_busy: timeout
    D (00:00:01.406) spi_master: device5 release bus
    D (00:00:01.406) spi_master: device5 locked the bus
    D (00:00:01.448) sdspi_host: poll_busy: timeout
    D (00:00:01.449) spi_master: device5 release bus
    D (00:00:01.449) spi_master: device5 locked the bus
    D (00:00:01.492) sdspi_host: poll_busy: timeout
    D (00:00:01.494) spi_master: device5 release bus
    D (00:00:01.494) sdspi_host: Setting card clock to 20000 kHz
    D (00:00:01.496) spi_hal: eff: 20000, limit: 26666k(/2), 0 dummy, 0 delay
    D (00:00:01.502) spi_master: SPI2: New device added to CS5, effective clock: 20000kHz
    D (00:00:01.510) spi_master: device5 locked the bus
    D (00:00:01.555) sdspi_host: poll_busy: timeout
    D (00:00:01.555) spi_master: device5 release bus
    D (00:00:01.556) spi_master: device5 locked the bus
    D (00:00:01.599) sdspi_host: poll_busy: timeout
    D (00:00:01.599) spi_master: device5 release bus
    D (00:00:01.599) vfs_fat_sdmmc: using pdrv=0
    D (00:00:01.601) spi_master: device5 locked the bus
    D (00:00:01.646) sdspi_host: poll_busy: timeout
    D (00:00:01.646) spi_master: device5 release bus
    D (00:00:01.646) spi_master: device5 locked the bus
    D (00:00:01.689) sdspi_host: poll_busy: timeout
    D (00:00:01.691) spi_master: device5 release bus
    D (00:00:01.691) spi_master: device5 locked the bus
    D (00:00:01.732) sdspi_host: poll_busy: timeout
    D (00:00:01.734) spi_master: device5 release bus
    D (00:00:01.734) spi_master: device5 locked the bus
    D (00:00:01.776) sdspi_host: poll_busy: timeout
    D (00:00:01.777) spi_master: device5 release bus
    I (00:00:01.777) sdcard: SD card mounted at /sd
    Name: SD16G
    Type: SDHC/SDXC
    Speed: 20000.00 MHz (limit: 20.00 MHz)
    Size: 7680MB
    CSD: ver=2, sector_size=512, capacity=15728640 read_bl_len=9
    SSR: bus_width=1
    Free: 7504MB
    
  • External SD slot HW-125 only (the internal is not used). Does not work.

    Code: Select all

    I (00:00:00.624) sdcard: Mounting SD SPI: MISO 18, MOSI 19, CLK 25, CS 22
    D (00:00:00.629) spi: SPI2 use gpio matrix.
    D (00:00:00.633) sdspi_host: sdspi_host_init_device: SPI2 cs=22 cd=-1 wp=-1
    D (912) intr_alloc: Connected src 30 to int 13 (cpu 0)
    D (00:00:00.645) spi_hal: eff: 400, limit: 26666k(/2), 0 dummy, -1 delay
    D (00:00:00.652) spi_master: SPI2: New device added to CS5, effective clock: 400kHz
    I (00:00:00.660) gpio: GPIO[22]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0 
    D (00:00:00.670) spi_master: device5 locked the bus
    D (00:00:00.675) spi_master: device5 release bus
    D (00:00:00.679) sdspi_host: sdspi_host_start_command: cmd=52 error=0x107
    D (00:00:00.686) sdmmc_cmd: cmd=52, sdmmc_req_run returned 0x107
    D (00:00:00.692) spi_master: device5 locked the bus
    D (00:00:00.698) spi_master: device5 release bus
    D (00:00:00.716) spi_master: device5 locked the bus
    D (00:00:00.717) spi_master: device5 release bus
    D (00:00:00.736) spi_master: device5 locked the bus
    D (00:00:00.736) spi_master: device5 release bus
    D (00:00:00.737) sdmmc_sd: SDHC/SDXC card
    D (00:00:00.738) spi_master: device5 locked the bus
    D (00:00:00.744) spi_master: device5 release bus
    I (00:00:00.748) sdspi_transaction: cmd=5, R1 response: command not supported
    D (00:00:00.756) sdmmc_cmd: cmd=5, sdmmc_req_run returned 0x106
    D (00:00:00.762) sdmmc_io: sdmmc_init_io: io_send_op_cond (1) returned 0x106; not IO card
    D (00:00:00.770) spi_master: device5 locked the bus
    D (00:00:00.775) spi_master: device5 release bus
    D (00:00:00.780) sdspi_host: data CRC set=1
    D (00:00:00.784) spi_master: device5 locked the bus
    D (00:00:00.789) spi_master: device5 release bus
    D (00:00:00.794) spi_master: device5 locked the bus
    D (00:00:00.799) spi_master: device5 release bus
    D (00:00:00.806) spi_master: device5 locked the bus
    D (00:00:00.809) spi_master: device5 release bus
    D (00:00:00.813) spi_master: device5 locked the bus
    D (00:00:00.818) spi_master: device5 release bus
    D (00:00:00.823) spi_master: device5 locked the bus
    D (00:00:00.828) spi_master: device5 release bus
    D (00:00:00.832) sdmmc_common: host_ocr=0x40ff8000 card_ocr=0xc0ff8000
    D (00:00:00.839) sdmmc_common: sdmmc_card_init: host_ocr=40ff8000, card_ocr=c0ff8000
    D (00:00:00.847) sdmmc_init: sdmmc_card_init: card type is SD
    D (00:00:00.852) spi_master: device5 locked the bus
    D (00:00:00.858) spi_master: device5 release bus
    D (00:00:00.862) spi_master: device5 locked the bus
    D (00:00:00.868) spi_master: device5 release bus
    D (00:00:00.872) spi_master: device5 locked the bus
    D (00:00:00.877) spi_master: device5 release bus
    D (00:00:00.881) spi_master: device5 locked the bus
    D (00:00:00.887) spi_master: device5 release bus
    D (00:00:00.891) spi_master: device5 locked the bus
    D (00:00:00.896) spi_master: device5 release bus
    D (00:00:00.901) spi_master: device5 locked the bus
    D (00:00:00.908) spi_master: device5 release bus
    D (00:00:00.910) sdspi_host: Setting card clock to 20000 kHz
    D (00:00:00.916) spi_hal: eff: 20000, limit: 26666k(/2), 0 dummy, 0 delay
    D (00:00:00.923) spi_master: SPI2: New device added to CS5, effective clock: 20000kHz
    D (00:00:00.931) spi_master: device5 locked the bus
    D (00:00:00.936) spi_master: device5 release bus
    D (00:00:00.940) sdspi_transaction: cmd=55, R1 response: command CRC error
    D (00:00:00.947) sdmmc_cmd: cmd=55, sdmmc_req_run returned 0x109
    E (00:00:00.953) sdmmc_sd: sdmmc_check_scr: send_scr returned 0x109
    D (00:00:00.960) sdmmc_init: sdmmc_card_init: sdmmc_check_scr returned 0x109
    E (00:00:00.967) vfs_fat_sdmmc: sdmmc_card_init failed (0x109).
    I (00:00:00.974) gpio: GPIO[22]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0 
    W (00:00:00.984) sdcard: sdcard_mount failed (ESP_ERR_INVALID_CRC)
    
And here is Arduino's equivalent (esp32-arduino release 2.0.4) that works both for internal and external SD slots.

Code: Select all

    // clk, miso, mosi, cs
    SPI.begin(25, 18, 19, 22);
    Serial.begin(115200);
    if (!SD.begin(22)) {
        Serial.println("Card Mount Failed");
        return;
    }
What am I doing wrong in ESP-IDF with an external SD slot and its custom pins?

Songluu
Posts: 5
Joined: Fri Nov 04, 2022 4:26 am

Re: SD SPI mount error with custom wiring

Postby Songluu » Thu Nov 17, 2022 3:52 pm

Hello,I have encountered same problem when using an external sdcard. Did you find the solution?

RalphD
Posts: 103
Joined: Thu Nov 25, 2021 9:02 pm

Re: SD SPI mount error with custom wiring

Postby RalphD » Thu Nov 17, 2022 4:07 pm

make sure that the custom wiring has external pull ups of 10k and if cables are used as short as possible. Keep in mind that SD card below 4GB and above 32GB do not work in all configurations.

dizcza
Posts: 56
Joined: Tue Sep 07, 2021 6:59 pm

Re: SD SPI mount error with custom wiring

Postby dizcza » Fri Nov 18, 2022 8:10 am

Songluu wrote:
Thu Nov 17, 2022 3:52 pm
Hello,I have encountered same problem when using an external sdcard. Did you find the solution?
The workaround was to lower the SD SPI speed to 7500 kHz. This is a workaround, still waiting for a solution.
RalphD wrote:
Thu Nov 17, 2022 4:07 pm
make sure that the custom wiring has external pull ups of 10k and if cables are used as short as possible. Keep in mind that SD card below 4GB and above 32GB do not work in all configurations.
Thanks, this is a general recommendation for all I2C and SPI communication lines. The issue is different though: same wiring but Arduino sdcard lib works at high speed while master ESP-IDF does not work work at SPI high speed.

RalphD
Posts: 103
Joined: Thu Nov 25, 2021 9:02 pm

Re: SD SPI mount error with custom wiring

Postby RalphD » Fri Nov 18, 2022 3:08 pm

The workaround was to lower the SD SPI speed to 7500 kHz. This is a workaround, still waiting for a solution.
Did you check if the Arduino library does really work on high speed? I don't know about the Arduino library but may be it has sort of auto clock speed detection.

dizcza
Posts: 56
Joined: Tue Sep 07, 2021 6:59 pm

Re: SD SPI mount error with custom wiring

Postby dizcza » Fri Nov 18, 2022 9:55 pm

RalphD wrote:
Fri Nov 18, 2022 3:08 pm
Did you check if the Arduino library does really work on high speed? I don't know about the Arduino library but may be it has sort of auto clock speed detection.
You're right, by default 4000 kHz is used in arduino sd spi lib: https://github.com/espressif/arduino-es ... D/src/SD.h

I remember I had a look at this number but the sight of it overwhelmed my eyes with zeros, though in fact, it's only 4000 kHz... need to be more careful next time.

So it's an expected behavior then, and yes, placing correct pull-up resistors would help.

Who is online

Users browsing this forum: No registered users and 417 guests