Page 1 of 1

SD cards for product mfg intent

Posted: Fri Feb 02, 2024 2:30 pm
by sb_espressif
Hi;

I'm a single person trying to make the leap from hobbyist to producing a product; my thing requires the use of some sort of onboard storage for large files. At the moment I'm at the stage of breadboarding - I have an ESP32-s3 Feather and an adafruit SD breakout board. This setup has worked more or less ok, though it has been somewhat flaky in terms of mounting the SD card properly.

Recently, it seemed to stop recognizing the card altogether. Multiple attempts at reformatting the card failed, so I'm concluding it's corrupted. I tried buying replacement cards - these are reputable Sandisk cards, 64GB and 128GB, Class 3, A2, V30. My setup doesn't seem to recognize them at all.

I can find lots of threads on this and other forums where people are asking "why can't I mount my SD card?" But I can't really find a set of common threads in terms of troubleshooting, and I notice a handful of comments like "ESP32's are picky about SD cards" or "I tried a no-name cheap, old card and it works, but new, modern, larger-format cards don't".

Hobbyist me is ok with just buying some random cards from Amazon and trying until I find one that works, but this doesn't seem like a sustainable approach to trying to scale something. But I can't find any resources that say "Definitely use THIS spec, definitely do not use this" or whatever. My sense is that a proper business with a dev team has some internal lore or guidelines for solving this problem.

So I think my question is: what should my approach be here? SHOULD this card work? If not, why? ChatGPT gives me some vague advice like check my wiring (done, several times), 'maybe there's a voltage issue' (ok, but what is the issue? How do I troubleshoot?), "make sure the card is compatible" (how do I properly do this?), but none of it really is helping me make some rules for myself, and that's kind of what I need to do.

Is this apparent persnickity-ness a reason to consider alternative storage methods? If so, what should I consider?

Re: SD cards for product mfg intent

Posted: Fri Feb 02, 2024 2:58 pm
by MicroController
what should my approach be here? SHOULD this card work? If not, why?
SPI or SDMMC?

If SPI:
I believe that all cards conforming to the SD spec should (also) work with SPI; I also believe (almost) all actually do in some way.

In my experience, different cards are 'flaky'/picky over their initialization sequence to varying degrees. If a card is successfully initialized to use the SPI interface, the rest works too. Some of the flakyness/variations I observed was e.g. related to the number of 'dummy' SPI clocks a card requires before the first SPI command is issued. IIRC the spec says something around 'at least 80 clocks' (or 72?) or so. Playing around with these 'pre-initialization signals' and the timing/delay before/between initialization commands I was able to get cards to work which originally wouldn't.

TL;DR: You could look into the ESP-IDF drivers and see if/where you can modify them to give more slack during initialization, i.e. send more dummy clocks, wait a few ms, try with a different command sequence if one fails, maybe limit the SPI clock rate so that signal integrity over the connections is ensured. That might help with getting a significant fraction of contemporary cards to cooperate.
Properly terminating/impedance matching (e.g. via GPIO drive strength) the signals may also be worth trying.

Re: SD cards for product mfg intent

Posted: Fri Feb 02, 2024 8:01 pm
by sb_espressif
SPI or SDMMC?

Argh, thank you for asking - I'm working with SPI at the moment. By most accounts working with SD cards is "dead simple", but I admit your suggestions intimidate me a little:
In my experience, different cards are 'flaky'/picky over their initialization sequence to varying degrees. If a card is successfully initialized to use the SPI interface, the rest works too.

Hm, maybe a helpful clue for me to offer is that this line seems to be what's causing me trouble:

Code: Select all

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

The error that the default sd_card example throws is:

Code: Select all

E (401) sdmmc_sd: sdmmc_init_sd_if_cond: send_if_cond (1) returned 0x108
E (401) vfs_fat_sdmmc: sdmmc_card_init failed (0x108).
I (401) gpio: GPIO[5]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0 
E (411)   SB - sd-read-write: Failed to initialize the card (ESP_ERR_INVALID_RESPONSE). Make sure SD card lines have pull-up resistors in place.

I notice that the error code number there is 0x107 if I feed 3.3v into my breakout board, but 0x108 if I feed 5v into it. This adafruit board has a logic level shifter built into it, and it's a little unclear to me which voltage level I should be providing (I thought the level shifter would render this irrelevant, but the error codes suggest to me it's not).
Some of the flakyness/variations I observed was e.g. related to the number of 'dummy' SPI clocks a card requires before the first SPI command is issued. IIRC the spec says something around 'at least 80 clocks' (or 72?) or so. Playing around with these 'pre-initialization signals' and the timing/delay before/between initialization commands I was able to get cards to work which originally wouldn't.

Where/how might one do this? Is this a matter of tweaking values provided to config structs, or actually finding and editing code within one of the components provided by espressif? (I'm out of my element here, so unfamiliar with a lot of what you're suggesting, but up to my eyebrows in learning so I'm happy for this to be one more thing on the pile).

Thank you! You're very generous with your responses, they're super helpful and I'm very grateful.

Re: SD cards for product mfg intent

Posted: Fri Feb 02, 2024 8:46 pm
by MicroController
Is this a matter of tweaking values provided to config structs, or actually finding and editing code within one of the components provided by espressif?
I'm afraid there's nothing that can be done via configuration, so hacking the IDF driver it is. - Or maybe checking out the Arduino drivers which people reportedly have better success rates with.

What I described above were the results of experiments I did quite a while ago and not on an ESP chip, but the way the IDF driver behaves reminded me of exactly the issues I had back in the day where some cards would not respond 'properly' to my SPI initialization attempts unless they got tickled in just the right way. I also seem to remember that there are different ways/command sequences that can be used for intitialization, especially if you know/already figured out what kind of card you're actually talking to. E.g., you can skip some steps if you know a-priori that you don't have to support/probe for an MMC or SD v1.0 card or some of that legacy stuff, which also seemed to help.

OTOH, the 'professionals' rarely solder SD cards into their devices. eMMC is commonly used for on-board flash storage.