ESP32 - supported types of microsd cards

sazanof
Posts: 36
Joined: Wed Sep 13, 2023 10:22 am
Contact:

Re: ESP32 - supported types of microsd cards

Postby sazanof » Sat Feb 22, 2025 8:30 am

ESP_rrtandler wrote:
Mon Feb 10, 2025 5:41 pm
In the logs of the working / failing card, the main difference I see, is the error code returned by the library when it tries to get the characteristics of the card. While the working card responses with 0x106 which means not supported, the failing card case shows 0x107 which means timeout. Situation very similar to this was discussed here:
https://github.com/espressif/esp-idf/issues/8582
and here:
https://github.com/espressif/esp-idf/issues/8683

You may check the signal idle voltage levels at your SD card and if you have oscilloscope, then also the voltage range of the signal to exclude the cases described in the links above.
Hello! I did more tests. I install Arduino with ESP32 boards and check SD SPI sketch example.

ALL cards are WORK! 1,4,8,16,32 GB

In the case of esp-idf, only 64GB cards work. What's wrong with the example https://github.com/espressif/esp-idf/tr ... card/sdspi? Where to dig?

I tried different host freq too....

teletypeguy
Posts: 10
Joined: Sat Feb 22, 2025 7:21 pm

Re: ESP32 - supported types of microsd cards

Postby teletypeguy » Tue Feb 25, 2025 1:34 am

Your scope signals are so bad that I am surprised you got any sd-cards to work. Whenever you see logic signals not at proper levels there is something wrong. It almost looks like mosi and miso are mixing somewhat -- do you have the 3.3V connected to the pullups? I did this once and then you have resistor tied directly between signals. Or is your sd connector on a little board connected by wires? Unclear exactly what your custom board looks like.

sazanof
Posts: 36
Joined: Wed Sep 13, 2023 10:22 am
Contact:

Re: ESP32 - supported types of microsd cards

Postby sazanof » Tue Feb 25, 2025 4:25 am

teletypeguy wrote:
Tue Feb 25, 2025 1:34 am
Your scope signals are so bad that I am surprised you got any sd-cards to work. Whenever you see logic signals not at proper levels there is something wrong. It almost looks like mosi and miso are mixing somewhat -- do you have the 3.3V connected to the pullups? I did this once and then you have resistor tied directly between signals. Or is your sd connector on a little board connected by wires? Unclear exactly what your custom board looks like.
This is what the last thing I tested looks like. The user board is the same, only the wires are even shorter, since everything is separated on the board.

And I've attached the diagram on the first page. Well, all the cards work in the arduino ide.
Attachments
photo_2025-02-25_07-23-12.jpg
photo_2025-02-25_07-23-12.jpg (148.46 KiB) Viewed 1365 times

sazanof
Posts: 36
Joined: Wed Sep 13, 2023 10:22 am
Contact:

Re: ESP32 - supported types of microsd cards

Postby sazanof » Tue Feb 25, 2025 4:30 am

teletypeguy wrote:
Tue Feb 25, 2025 1:34 am
It almost looks like mosi and miso are mixing somewhat -- do you have the 3.3V connected to the pullups?
I checked and pullup all the lines with resistors, checked and pullup each one in turn. In different ways. I wrote about it. Thanks for the help. Do you have any idea why the signals are mixed and the cards are not working?

teletypeguy
Posts: 10
Joined: Sat Feb 22, 2025 7:21 pm

Re: ESP32 - supported types of microsd cards

Postby teletypeguy » Tue Feb 25, 2025 6:14 am

Sheesh, don't use a breadboard and expect anything over a MHz or so to work well. There is no ground plane so all of those signals are coupling like crazy, lead inductances will be high giving excessive peaks of over/undershoot -- just horrible electromagnetically all ways around.

At least get a little pcb module from amazon or adafruit or somewhere:
https://www.adafruit.com/product/4682
https://learn.adafruit.com/adafruit-mic ... l?view=all

Also, for interconnecting boards, try to use ribbon cable with signals on the odd pins and ground and power on the even lines -- you get a nice rf-ground interleaved between every signal line and can run higher freqs. If you must use a breadboard, you can twist a signal wire with a ground or power wire to keep it from radiating as much.

You can also get some perfboards, solder-through enameled wire, and slug-and-snail tape (which is adhesive-backed copper foil) to make ground planes (with an xacto to slice bits out). Does not take that long to make a one-off that is electromagnetically cleaner:
1.png
1.png (1.33 MiB) Viewed 1302 times
2.png
2.png (1.74 MiB) Viewed 1302 times

sazanof
Posts: 36
Joined: Wed Sep 13, 2023 10:22 am
Contact:

Re: ESP32 - supported types of microsd cards

Postby sazanof » Tue Feb 25, 2025 8:00 pm

Once again, I reconnected everything with short wires using different ready-made SD card modules. The card was powered via AMS1117 3v3 (not from ESP32).

I conducted a comparative analysis of running programs on the Arduino IDE and on the ESP-IDF

Well, the cards do not work via the ESP-ID example! I doubt that the problem is in the hardware or such, or in the connections. Well, this cannot be - I checked on different ESP 32 models with different adapters.
photo_2025-02-25_22-56-12.jpg
photo_2025-02-25_22-56-12.jpg (92.32 KiB) Viewed 1198 times
photo_2025-02-25_22-56-17.jpg
photo_2025-02-25_22-56-17.jpg (164.06 KiB) Viewed 1198 times
On Arduino IDE with ESP32 all the cards work. On esp-idf only 1 64 GB card.

teletypeguy thank you! frequency is 400khz (I tested different frequencies). On the finished printed circuit board the sd card is routed correctly and does not work either (sd cards less than 64gb). You can check the example of work with the SD card and let me know - does it work?

teletypeguy
Posts: 10
Joined: Sat Feb 22, 2025 7:21 pm

Re: ESP32 - supported types of microsd cards

Postby teletypeguy » Tue Feb 25, 2025 10:13 pm

If the hardware is working with arduino framework, but not idf, then you are correct that it is likely software-related.

I don't know if this helps, but here is my sd-card init routine:
  1. //------------------------------------------------------------------------------
  2. /**
  3.     Inits SD Card
  4.  
  5.     @param      void
  6.     @return     sdcard-ready or error code
  7.  
  8.  * the handy-dandy esp_vfs_fat_sdspi_mount() function:
  9.  * - initializes an SPI Master device based on the SPI Master driver with configuration in
  10.  *   slot_config, and attach it to an initialized SPI bus.
  11.  * - initializes SD card with configuration in host_config_input
  12.  * - mounts FAT partition on SD card using FATFS library, with configuration in mount_config
  13.  * - registers FATFS library with VFS, with prefix given by base_prefix variable
  14.  *          @param base_path     path where partition should be registered (e.g. "/sdcard")
  15.  *          @param host_config_input Pointer to structure describing SDMMC host. This structure can be
  16.  *                      initialized using SDSPI_HOST_DEFAULT() macro.
  17.  *          @param slot_config   Pointer to structure with slot configuration.
  18.  *                      For SPI peripheral, pass a pointer to sdspi_device_config_t
  19.  *                      structure initialized using SDSPI_DEVICE_CONFIG_DEFAULT().
  20.  *          @param mount_config  pointer to structure with extra parameters for mounting FATFS
  21.  *          @param[out] out_card If not NULL, pointer to the card information structure will be returned via
  22.  *                      this argument. It is suggested to hold this handle and use it to unmount the card.
  23. */
  24.  
  25. sdcard_return_t sdcard_init(void)
  26. {
  27.     static const char *TAG = "sdcard_init";
  28.  
  29.     g_log(TAG, "----- Init SPI bus for SD-Card and Mount fat filesystem");
  30.  
  31.     esp_err_t           return_esp;
  32.  
  33.     sdmmc_host_t host = SDSPI_HOST_DEFAULT();       // init host to various defaults, max spi freq of 20MHz...
  34.     host.slot = SDCARD_PORT;                        // ...and use spi port defined in hwconfig.h
  35.  
  36.     spi_bus_config_t buscfg = {
  37.         .sclk_io_num =          SDCARD_SPI_SCK,     // pins defined in hwconfig.h
  38.         .mosi_io_num =          SDCARD_SPI_MOSI,
  39.         .miso_io_num =          SDCARD_SPI_MISO,
  40.         .quadwp_io_num =        -1,
  41.         .quadhd_io_num =        -1,
  42.         .max_transfer_sz =      4000 };
  43.  
  44.     return_esp = spi_bus_initialize(host.slot, &buscfg, SDSPI_DEFAULT_DMA);
  45.  
  46.     if (return_esp != ESP_OK)
  47.     {
  48.         g_logm(TAG, ">>> Failed to initialize SPI bus (%s)", esp_err_to_name(return_esp));
  49.         return SDCARD_ERR_SPI_BUS;
  50.     }
  51.  
  52.     sdspi_device_config_t slot_config = {
  53.         .host_id   = host.slot,
  54.         .gpio_cs   = SDCARD_SPI_CS,                 // pins defined in hwconfig.h
  55.         .gpio_cd   = SDSPI_SLOT_NO_CD,
  56.         .gpio_wp   = SDSPI_SLOT_NO_WP,
  57.         .gpio_int  = GPIO_NUM_NC,
  58.         .gpio_wp_polarity = SDSPI_IO_ACTIVE_LOW };
  59.  
  60.     esp_vfs_fat_sdmmc_mount_config_t mount_config = {
  61.         .format_if_mount_failed = FORMAT_IF_MOUNT_FAILS,
  62.         .max_files = 5,
  63.         .allocation_unit_size = (16 * 1024) };      // a good size?
  64.  
  65.     const char      mount_point[] = SD_MOUNT_POINT;
  66.     sdmmc_card_t    *card;
  67.  
  68.     return_esp = esp_vfs_fat_sdspi_mount(mount_point, &host, &slot_config, &mount_config, &card);
  69.  
  70.     if (return_esp != ESP_OK)
  71.     {
  72.         sdcard_available_g = false;
  73.         if (return_esp == ESP_FAIL)
  74.         {
  75.             g_logm(TAG, ">>> Failed to mount filesystem");
  76.             return SDCARD_ERR_FAT_MOUNT_FAIL;
  77.         }
  78.         else
  79.         {
  80.             g_logm(TAG, ">>> Failed to initialize the card (%s)", esp_err_to_name(return_esp));
  81.             return SDCARD_ERR_FAT_MOUNT_ERR;
  82.         }
  83.     }
  84.  
  85.     g_logb(TAG, "--- Filesystem mounted");
  86.  
  87.     sdcard_available_g = true;
  88.  
  89.     // --- card has been initialized, print card statistics and directory:
  90.     print_sdcard_info(card);
  91.     print_sdcard_dir();
  92.  
  93.     return READY;
  94. }
  95.  

and support files:

  1. //------------------------------------------------------------------------------
  2. static void print_sdcard_info(const sdmmc_card_t* card)
  3. {
  4.     #define     SD_OCR_SDHC_CAP     (1<<30)     // Card Capacity Status bit in OCR
  5.  
  6.     bool print_scr = false;
  7.     bool print_csd = false;
  8.     const char *type;
  9.  
  10.     printf("\n  SD-Card Info:\n");
  11.     printf("       SDCard Name: %s\n", card->cid.name);
  12.  
  13.     if (card->is_sdio)
  14.     {
  15.         type = "SDIO";
  16.         print_scr = true;
  17.         print_csd = true;
  18.     }
  19.     else if (card->is_mmc)
  20.     {
  21.         type = "MMC";
  22.         print_csd = true;
  23.     }
  24.     else
  25.     {
  26.         type = (card->ocr & SD_OCR_SDHC_CAP) ? "SDHC/SDXC" : "SDSC";
  27.         print_csd = true;
  28.     }
  29.     printf("       Card Type: %s\n", type);
  30.  
  31.     if (card->real_freq_khz == 0) {
  32.         printf("       Speed: N/A\n");
  33.     } else {
  34.         const char *freq_unit = card->real_freq_khz < 1000 ? "kHz" : "MHz";
  35.         const float freq = card->real_freq_khz < 1000 ? card->real_freq_khz : card->real_freq_khz / 1000.0;
  36.         const char *max_freq_unit = card->max_freq_khz < 1000 ? "kHz" : "MHz";
  37.         const float max_freq = card->max_freq_khz < 1000 ? card->max_freq_khz : card->max_freq_khz / 1000.0;
  38.         printf("       Speed: %.2f %s (limit: %.2f %s)%s\n", freq, freq_unit, max_freq, max_freq_unit, card->is_ddr ? ", DDR" : "");
  39.     }
  40.  
  41.     printf("       Size: %llu MB\n", ((uint64_t) card->csd.capacity) * card->csd.sector_size / (1024 * 1024));
  42.  
  43.     if (print_csd) {
  44.         printf("       CSD: ver=%d, sector_size=%d, capacity=%d read_bl_len=%d\n",
  45.                 (int) (card->is_mmc ? card->csd.csd_ver : card->csd.csd_ver + 1),
  46.                 card->csd.sector_size, card->csd.capacity, card->csd.read_block_len);
  47.         if (card->is_mmc) {
  48.             printf("       EXT CSD: bus_width=%" PRIu32 "\n", (uint32_t) (1 << card->log_bus_width));
  49.         } else if (!card->is_sdio){ // make sure card is SD
  50.             printf("       SSR: bus_width=%" PRIu32 "\n", (uint32_t) (card->ssr.cur_bus_width ? 4 : 1));
  51.         }
  52.     }
  53.     if (print_scr) {
  54.         printf("       SCR: sd_spec=%d, bus_width=%d\n", card->scr.sd_spec, card->scr.bus_width);
  55.     }
  56. }
  1. //------------------------------------------------------------------------------
  2. void print_sdcard_dir(void)
  3. {
  4.     DIR             *directory;
  5.     struct dirent   *entry;
  6.     struct stat     filestat;
  7.     int             mplen = strlen(SD_MOUNT_POINT);
  8.     int             fnlen;
  9.  
  10.     directory = opendir(SD_MOUNT_POINT);
  11.     if (directory)
  12.     {
  13.         printf("\n  SD-Card Dir (size/filename/datetime):\n");
  14.         while ((entry = readdir(directory)) != NULL)
  15.         {
  16.             // build full-path filename:
  17.             fnlen = strlen(entry->d_name);
  18.             char *filename = malloc(mplen + fnlen + 2);
  19.             sprintf(filename, "%s/%s", SD_MOUNT_POINT, entry->d_name);
  20.             // get file stats:
  21.             stat(filename, &filestat);
  22.             // need to reuse this variable-len string:
  23.             free(filename);
  24.  
  25.             // print dir or file size:
  26.             if( S_ISDIR(filestat.st_mode) )
  27.                 { printf("%12s   ","<DIR>"); }
  28.             else
  29.                 { printf("%12ld   ", filestat.st_size); }
  30.  
  31.             // print filename:
  32.             printf("%-64s", entry->d_name);
  33.  
  34.             // file datetime:
  35.             printf("%s", ctime(&filestat.st_mtime));
  36.         }
  37.         closedir(directory);
  38.         printf("\n");
  39.     }
  40.     else
  41.     {
  42.         printf("  SD-Card Dir Failed\n");
  43.     }
  44.     vTaskDelay(pdMS_TO_TICKS(1000));
  45. }
I have been having no problem with:
- 8GB level-4 Kingston
- 8GB level-4 PNY
- 32 GB level-10 PNY
and various others

sazanof
Posts: 36
Joined: Wed Sep 13, 2023 10:22 am
Contact:

Re: ESP32 - supported types of microsd cards

Postby sazanof » Wed Feb 26, 2025 6:18 am

teletypeguy wrote:
Tue Feb 25, 2025 10:13 pm
If the hardware is working with arduino framework, but not idf, then you are correct that it is likely software-related.
Exactly! That's what I'm talking about.
Thanks for the example. I'm going to try it now and I'll come back with an answer.

Can you tell me how your card is connected to esp32, which pull ups you using and which version of EDP-EDF is used with your code example?

teletypeguy
Posts: 10
Joined: Sat Feb 22, 2025 7:21 pm

Re: ESP32 - supported types of microsd cards

Postby teletypeguy » Wed Feb 26, 2025 7:19 am

I am using ESP-IDF v5.3.1 at the moment. I use 4GB to 32GB SDHC cards (formatted with FAT32). Generally I can just format them on windows, but some folks swear by the golden formatter from the sd association:

https://www.sdcard.org/downloads/formatter/

I have used sd-cards on many custom boards, but my current project is using an off-the-shelf Elecrow DLC35010R display module w/ esp-S3. Pullups on the sdcard lines are a mere 47k:
1.png
1.png (70.27 KiB) Viewed 1032 times
I see they have data1 and data2 lines pulled up as well -- I don't believe that is needed as I have left those lines open on many projects over the years, with various versions of the Chan fatfs code, and different microcontrollers.

This is a custom I did, where the sd tucks under the esp module:
3.png
3.png (285.36 KiB) Viewed 1032 times
2.png
2.png (157.12 KiB) Viewed 1032 times

sazanof
Posts: 36
Joined: Wed Sep 13, 2023 10:22 am
Contact:

Re: ESP32 - supported types of microsd cards

Postby sazanof » Wed Feb 26, 2025 9:15 am

teletypeguy - thank you!

https://github.com/espressif/esp-idf/issues/15450 I also open new issue on github.

In the meantime, I'll double-check the schematics and check how your code works on my device.

Who is online

Users browsing this forum: No registered users and 116 guests