Skip to content

SD lib w/ card 'flakey'? #272

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

Closed
ladyada opened this issue Mar 18, 2017 · 104 comments
Closed

SD lib w/ card 'flakey'? #272

ladyada opened this issue Mar 18, 2017 · 104 comments

Comments

@ladyada
Copy link
Contributor

ladyada commented Mar 18, 2017

heya im using known good hardware, with pullups on the SD CS line, no level shifters, PCB traces not jumpers - but getting 'flakiness' on init/mount

[E][sd_diskio.cpp:99] sdSelectCard(): timeout
[E][sd_diskio.cpp:708] sdcard_mount(): f_mount failed 0x(3)
Card Mount Failed

oddly, it works maybe 10% of the time. once it starts working, it 'keeps' working until a full power reset. if it doesn't work, sometimes hitting software reset helps, but usually not. this is with the SPI library - tried changing SPI speed in SD.begin() without any noticable changes. still investigating ... but making a note here in case others can reproduce

fyi, grabbing the non-optimized SD library works great with no hiccups - https://github.com/adafruit/arduino-esp32/tree/d602169ebcd126c309009e87c1e455f2c892d08f (yeah sorry it got smooshed in the upstream merge so please download the zip from that point to get the library files)

@me-no-dev
Copy link
Member

Hm that is rather strange. I had gazillion of issues with SD_MMC and pullups but none with SD. I just connected the 4 data lines directly to SPI and that was it (I use SD->MicroSD with some wires soldered directly on the pads).
Which pins are you using?

@ladyada
Copy link
Contributor Author

ladyada commented Mar 19, 2017

static const uint8_t MOSI = 18;
static const uint8_t MISO = 19;
static const uint8_t SCK = 5;

and cs on any pin really, but im using
#define SD_CS 33

other than the SD card, all other SPI devices ive tested are working perfectly - TFT display, LoRa and RFM radio modules, VS1053

@me-no-dev
Copy link
Member

Done :)

@ladyada
Copy link
Contributor Author

ladyada commented Mar 19, 2017

looks good - would you like me to re-test?

@me-no-dev
Copy link
Member

Sure :) Confirmation is always good, but I was able to reproduce the issue and after the fix, was not able to anymore, regardless of what I did :) I wonder how it was working while I was developing the library?
BTW I tested with your pin configuration

void mountSdCard(){
    SPI.begin(5,19,18);
    if(!SD.begin(33)){
        Serial.println("Card Mount Failed");
        return;
    }
}

@ladyada
Copy link
Contributor Author

ladyada commented Mar 21, 2017

hiya - ok so for one of my feather setups, with cardcs on IO 27, it now works every time. on another feather, with cs on IO 14 it still doesnt work :/

Initializing SD card...[E][sd_diskio.cpp:99] sdSelectCard(): timeout
[W][sd_diskio.cpp:452] ff_sd_initialize(): GO_IDLE_STATE failed
[E][sd_diskio.cpp:724] sdcard_mount(): f_mount failed 0x(3)
failed!

@me-no-dev
Copy link
Member

maybe on io14 there is another issue? I mean, if SPI is properly configured, and you use pins below 34, the lib itself does not care which pin you have selected. It calls digitalWrite to control SS

@ladyada
Copy link
Contributor Author

ladyada commented Mar 21, 2017

yeah unknown. ill investigate more later... :)

@NickWaterton
Copy link

Interesting, I am having the same problem. Using the TFT feather wing (CS 14), the SD card works about 50% of the time, using the music maker featherwing, I can't get the same SD card to work at all (also CS 14).

SPI works fine with graphics and music etc, it's just the SD card that is unreliable. If I wasn't having a similar problem (but not as bad) with the TFT featherwing, I'd suspect a bad reader on the music maker.

When I have a chance I'll try it with an M0 or a WICED, see if they work with the music maker.

@badcf00d
Copy link

badcf00d commented Jun 28, 2017

I'm using the same pins as ladyada, coming from the adalogger featherwing, and I can't get the card to mount at all using:

#include <SD.h>
#include <SPI.h>

void setup() {
  Serial.begin(115200);
  SPI.begin(5, 19, 18);
  if (!SD.begin(33)) {
    Serial.println("Card Mount Failed");
  }
}

SD card works in an M0 datalogger, but no luck with the ESP32 :/

@ladyada
Copy link
Contributor Author

ladyada commented Jun 28, 2017

@me-no-dev hmm do you mind if we send you some hardware that you can test with? the SD card works if we use our ported 'arduino-SD-library' but not with the SD library ya'll included. so I think its some init thing?

@me-no-dev
Copy link
Member

@ladyada yes please! let's get this fixed :) How are we going to arrange this?

@me-no-dev
Copy link
Member

done :)

@ladyada
Copy link
Contributor Author

ladyada commented Jun 29, 2017

k email addr deleted, we'll reply

@dstoiko
Copy link
Contributor

dstoiko commented Jul 4, 2017

[E][sd_diskio.cpp:724] sdcard_mount(): f_mount failed 0x(3)
@me-no-dev thanks for this lib! Having the same issue here... Thought it was an issue with pull-up resistors at first but it doesn't seem to be. Any updates on this?

@me-no-dev
Copy link
Member

Things just showed up :) will solder the boards tonight and prep everything

@dstoiko
Copy link
Contributor

dstoiko commented Jul 4, 2017

That sounds exciting

@ladyada
Copy link
Contributor Author

ladyada commented Jul 4, 2017

yay - we have a full kit of cousre, and can try replicating any branches/fixes

@dstoiko
Copy link
Contributor

dstoiko commented Jul 5, 2017

So do we know where this issue comes from? What can I do to further test?

@me-no-dev
Copy link
Member

OK I need more info on the failing cards because here is my first result at this (Feather selected as board) Maybe there are particular steps to follow?:
fullsizerender 21
screen shot 2017-07-07 at 1 36 37 am
screen shot 2017-07-07 at 1 36 50 am

@ladyada
Copy link
Contributor Author

ladyada commented Jul 7, 2017

can you try a few times? also try plugging the esp32 into the tft feather and drawing a BMP from SD? that's the only thing i can say we tried and had difficulty with :)

@NickWaterton
Copy link

NickWaterton commented Jul 7, 2017 via email

@bbx10
Copy link
Contributor

bbx10 commented Jul 7, 2017

@NickWaterton Are you referring to the SD card slot on the music maker wing? The VS1053 part works fine using the ESP32-Radio project.

@me-no-dev
Copy link
Member

Hit a wall with the TFT Wing :) looking into it :)
@bbx10 yes, SD is the issue

@me-no-dev
Copy link
Member

@ladyada I seem to get intermittent issues wit the touch on the TFT as well. Like it will not start every once in a while. Have you notice this?

@me-no-dev
Copy link
Member

please try the latest commit :)

@me-no-dev
Copy link
Member

And here is a JPEG read from the SD card :)
fullsizerender 22

@bbx10
Copy link
Contributor

bbx10 commented Jul 7, 2017

@me-no-dev Works fine with SD card slot on Feather Adalogger (CS=33) and Music Maker (CS=14).

@merdok
Copy link

merdok commented Jun 4, 2019

I do not really understand this... Did not Lolin test the D32 Pro with both SD card and TFT before releasing it?

@michaelkleinhenz but how do you use the SD_MMC library? I also tried it and it also fails. Or did you wire a new sd card port to the GPIOs for that?

@G6EJD If this is an issue with one the libraries then it should be easy to fix, right? I mean both of them are open source so it should be possible to adjust the code and see if that fixes the issue?

@G6EJD
Copy link

G6EJD commented Jun 4, 2019

@merdok yes, I just haven't got the time right now to trace through the use of CS values to ensure they remain separate, as I'm still sure this is the issue. The Lolin D32 Pro schematic shows no SPI bus design or wiring issues that I can see.
What size SD card are you trying, I think the maximum size is 32GB?

@merdok
Copy link

merdok commented Jun 7, 2019

@G6EJD Any chance you could have a look at it?
I did try 16GB and 32GB and both of them work, but just when i disconnect the Touch TFT...

@G6EJD
Copy link

G6EJD commented Jun 8, 2019

Here's what I've found so-far:
Test Setup:
Wemos D1 Mini ESP8266 or ESP32 D3 Pro
Wemos 2.4"TFT
Wemos SD-Card Shield for Wemos D1 Mini
Adafruit_ILI9341 and Adafruit_GFX

  1. TFT (no SD-Card fitted) works on either ESP8266 or ESP32.
  2. SD-Card works on either ESP8266 or ESP32, but requires addition of #include <FS.h>
  3. ESP8266 + TFT + SD Card does not work. (Adafuit_ILI9341 lib)
  4. ESP32 + TFT + SD Card does not work. (Adafuit_ILI9341 lib)
  5. SD-Card examples do-not function if TFT not fitted but Adafruit_ILI9341 lib included.
  6. So now we can infer there is something in the Adafruit_ILI9341 library that is preventing SD-Card operations.
  7. A cursory look at the Adafruit_ILI9341 library compared to the SD-Card library does not reveal any obvious conflicts.
  8. Other tests conducted - added pull-ups (10K) to all SPI and CS data lines, made no difference.
  9. Slowed down the SPI bus - made no difference.
  10. Needs more time now...

@G6EJD
Copy link

G6EJD commented Jun 8, 2019

There are a few projects around that use both ili9341 and SD Card, now they don't work once complied with the latest release of the libraries.

@merdok
Copy link

merdok commented Jun 8, 2019

Oh, so i does not work also with the Wemos(Lolin) SD-Card Shield on the Wemos(Lolin) D1 Mini? I thought that this issue is just related to the Lolin D32 Pro with the built-in SD Card slot...

Also during my tests i had always the Adafruit_ILI9341 lib included and when i disconnected the TFT Touch shield the SD card was working, i also did leave the TFT init code inside. Now comes the interesting part, when i then hot plug the Touch TFT shield and do a reset with the onboard reset button, both the TFT and SD card work. But when i unplug the power cable and plug it back in the SD does not work again.

Isn't the issue that there are initially some wrong values set by either the Adafruit_ILI9341 or SD card lib when both of them are connected? I mean they both work together fine if you follow the steps which i mentioned above so this makes me think that this is just some issue with initialization.

I do not have enough skills to debug that two cases, but maybe you could do that and there will be something obvious?

@G6EJD
Copy link

G6EJD commented Jun 8, 2019

I have concluded and observed the same a hot plug of the tft starts the sd card working again, this does indeed point to a device initiation issue and we have concluded the same. So yes, I have all Lolin components and they don’t work together (8266 or 32) there is nothing wrong that I can see with the hardware. Previously the two did work together now not.
In my tests I disconnected the TFT and with an include Adafruit_ili9341 SD operations stop or of course the same may happen in converse a working ili9341 might stop when the SD library is included.
I have no answers yet but will keep investigating. I have yet to go back in the versions of the libraries back to when it was all working. To me it seems to be either the TFT or SD have been initialised to be bus masters not slaves and hence won’t work together.

@arash-pyk
Copy link

arash-pyk commented Jun 8, 2019

Hello guys. I noticed the thread is active again. Have you tried the solution I posted last year? In short: put a 100 ohm resistor in series with both the mosi and miso lines, I discovered this randomly but later found an explanation, it is to do with the fact that one of the devices (cannot remember which one probably sd controller chip) doesn’t have the standard tri-state I/O so even when the CS is pulled low, the data lines are low impedance. If only one device is on the bus it is not a problem but as soon as you add the second one the low impedance data lines pull the whole bus down introducing all sort of issues most noticeably problems in intialization of either salve devices on the bus even though they both work on their own on the bus. The resistor overcomes the issue in a very simple and crude way but it works. Hope it helps, happy to discuss it further.

P.s.the 100 ohm resistor is only needed on one of the data lines, either mosi or miso can’t remember which one but it is not a problem to put one on each data line

@merdok
Copy link

merdok commented Jun 8, 2019

@G6EJD That is good that we came to the conclusion :) I hope this brings are more closer to a solution :)
Did with a previous version everything work? I noticed the issue around December last year so if it was working then it was before that.

@arash-pyk I did not try that, but it seems the hardware is correctly designed so why would we need to put an additional resistor there?

@arash-pyk
Copy link

arash-pyk commented Jun 8, 2019

the issue I was having was to do with the internal design of the monolithic IC of the SD card controller and not the external components of the circuit. The solution is for people having problem with two slave devices on the SPI bus given that everything is properly setup, CS lines are managed correctly etc. give it a try, you only need to add the resistor to the second device on the bus.

@Edzelf
Copy link

Edzelf commented Jun 9, 2019

Maybe it's a good idea to make a setup with 2 SD cards and a setup with 2 TFT's to make sure which device causes the trouble.

@G6EJD
Copy link

G6EJD commented Jun 9, 2019

What is strange is that many projects/designs have been made that simultaneously display data from an SD card on a TFT and the hardware arrangement is identical. This does therefore point to a software issue. Today I’ll try an ILI9341 with in-built (unbuffered) SD (although not uSD) card reader to see if that still works.

@merdok
Copy link

merdok commented Jun 9, 2019

I guess the issue is the the touch capability of the Lolin 2.4 Touch TFT shield. It uses an additional CS line as far as i know. I also have the Lolin 2.13 EPD shield which does not have touch and he SD card works without any issues together with the display.
That is why i guess other projects work (because those displays in the other projects does not have touch)

@G6EJD
Copy link

G6EJD commented Jun 9, 2019

TF_CS is GPIO-12 and no conflict with other devices according to the schematic.

@merdok
Copy link

merdok commented Jun 9, 2019

That is why this issue is so strange. Everything looks fine, so why it does not work?

@G6EJD
Copy link

G6EJD commented Jun 10, 2019

Out of interest SD Library no-longer works on ESP8266, but works on ESP32; an Issue is already pending on here.

@GC-RnD
Copy link

GC-RnD commented May 22, 2020

Was there ever a resolution to this issue?
I am coming from the micropython universe and I am experiencing the same exact problem.
https://forum.micropython.org/viewtopic.php?f=18&t=8352

Why was this post closed?

@merdok
Copy link

merdok commented May 22, 2020

I still have the issue...

@GC-RnD
Copy link

GC-RnD commented May 22, 2020

I've been working on this issue for weeks and can't find a solution.
Again... why was this post considered closed?

@atanisoft
Copy link
Collaborator

@merdok @GC-RnD Open a new issue and follow the issue template and you might get some assistance, commenting on an old/closed issue won't get much traction for you.

@G6EJD
Copy link

G6EJD commented May 22, 2020

It’s generally a hardware problem, some designs work, some don’t. It was closed because there is no software issue. Usually the addition of pull-up resistor can help but in all cases the SD card needs to be buffered from the SPI bus to ensure correct operation and few if any are.

@GC-RnD
Copy link

GC-RnD commented May 22, 2020

For reference I'm using a wemos Lolin D32 Pro v2.
Has all pull-ups.
Not able to use ili9341 and sd card on shared spi
Please explain "buffered from the SPI"
Do you mean separated not shared?

@G6EJD
Copy link

G6EJD commented May 22, 2020

What happens is the display does not release the SPI lines and loads them, by a buffer I mean a isolation buffer frequently found on SD devices. For example take a look at separate SD cards on AliExpress you will find buffeted and unbuffered types, it is the latter required. If you purchase an ili9341 display with on-board SD card you will see a buffer device that separates the bus devices. Wemos made a mistake with these boards, you can use the SD card alone or Ili9341 alone but not together. The design is flawed.

You can see the buffer device at the bottom right corner: £1.84 | Mini Data Logger Module Logging Shield for Arduino For Raspberry Pi Logging Recorder Data Logger Module Shield V1.0 SD Card
https://a.aliexpress.com/_dSRMQ5f

@Edzelf
Copy link

Edzelf commented May 22, 2020

Some interface boards for SD card have a design error. The MISO line will be at ground level if no card is inserted. Even if the CS is not active. The enable input of the buffer for MISO is connected to ground and not to CS.

@merdok
Copy link

merdok commented May 22, 2020

@G6EJD You say that this is a design error, but if you boot the Lolin d32 pro board without the screen connected and then connect the screen after the board has booted then magically both the screen and the sd card slot work together without issues. You can even reset the board with the on-board reset button and they will still work, but as soon as you disconnect the power completely they will not work and you have to repeat the process...

@Edzelf
Copy link

Edzelf commented May 22, 2020

Could you please check the level of the MISO signal in various (working and not working) test situations? MOSI should be HIGH most of the time.

@cyberman54
Copy link
Contributor

Same issue here, using TTGO V2.1 boards. But could solve it by moving to this lib:
https://github.com/nhatuan84/esp32-micro-sdcard

It works for SD access via SPI interface, while SD.cpp from arduino-esp32 doesn't.

blue-2357 pushed a commit to blue-2357/arduino-esp32 that referenced this issue Jul 17, 2024
The card seems to need a bit of a nudge at boot to properly mount
sometimes.
Fixes: espressif/arduino-esp32#272
dash0820 added a commit to dash0820/arduino-esp32-stripped that referenced this issue Mar 10, 2025
The card seems to need a bit of a nudge at boot to properly mount
sometimes.
Fixes: espressif/arduino-esp32#272
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