Skip to content

Initializing SD card...initialization failed! with GIT but ok with 2.5 #6019

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
luc-github opened this issue Apr 27, 2019 · 24 comments · Fixed by #6023
Closed

Initializing SD card...initialization failed! with GIT but ok with 2.5 #6019

luc-github opened this issue Apr 27, 2019 · 24 comments · Fixed by #6023
Milestone

Comments

@luc-github
Copy link
Contributor

SD support looks broken in GIT version when it was Ok in 2.5

Basic Infos

  • [ X] This issue complies with the issue POLICY doc.
  • [X ] I have read the documentation at readthedocs and the issue is not addressed there.
  • [ X] I have tested that the issue is present in current master branch (aka latest git).
  • [ X] I have searched the issue tracker for a similar issue.
  • [ X] If there is a stack dump, I have decoded it.
  • [ X] I have filled out all fields below.

Platform

  • Hardware: ESP-12
  • Core Version: latest git 04/27/2019
  • Development Env: Arduino IDE
  • Operating System: Windows 10

Settings in IDE

  • Module: Generic ESP8266 Module
  • Flash Mode: DOUT
  • Flash Size: 4MB
  • lwip Variant: v2 Lower Memory
  • Reset Method: nodemcu
  • Flash Frequency: 40Mhz
  • CPU Frequency: 160MHz
  • Upload Using: SERIAL
  • Upload Speed: 115200

Problem Description

SD card init failed with git version
using sample : https://github.com/esp8266/Arduino/blob/master/libraries/SD/examples/listfiles/listfiles.ino

Initializing SD card...initialization failed!

on same hardware with core 2.5 same code works

Also tried the new library :
https://github.com/earlephilhower/ESP8266SdFat/blob/92ab4b59eb920bf3134347dbe07e7c059eb64d46/examples/ReadWrite/ReadWrite.ino modifing
#define SD_CS_PIN 4 to not change the wiring
same error:
Initializing SD card...initialization failed!

@earlephilhower
Copy link
Collaborator

I've just run the ReadFiles.ino example on 2 cards, changing the one line to D0 from 4 as that's the way I'm wired up:

    if (!SD.begin(D0)) {

On the 128MB Canon SDC-128MH card I get the expected output (Debug enabled):

SDK:2.2.1(cfd48f3)/Core:2.5.0-100-gcdb54957=20500100/lwIP:STABLE-2_1_2_RELEASE/glue:1.1-7-g82abda3/BearSSL:a143020
Initializing SD card...initialization done.
Writing to test.txt...done.
test.txt:
testing 1, 2, 3.

And the new Samsung EVO Select U1 32GB I get the right output, too:

SDK:2.2.1(cfd48f3)/Core:2.5.0-100-gcdb54957=20500100/lwIP:STABLE-2_1_2_RELEASE/glue:1.1-7-g82abda3/BearSSL:a143020
Initializing SD card...initialization done.
Writing to test.txt...done.
test.txt:
testing 1, 2, 3.

Assuming it is a card related issue, what can you tell us about the card? Is it a very old MMC one? What size, brand, type, any other info?

@earlephilhower earlephilhower added the waiting for feedback Waiting on additional info. If it's not received, the issue may be closed. label Apr 28, 2019
@earlephilhower
Copy link
Collaborator

Also, calling @TD-er. I think I remember you going to the GIT head for your ESPEasy. Does it support SD cards, and if so have you or any of your users tried it or had issues like this? Thx!

@luc-github
Copy link
Contributor Author

My card is Micro SD Kingston 4Gb microSDHC – Classe 4 -
I did not expect it was a SD card issue as it work on 3D printers and on version 2.5
It works also on ESP32 with SD library

I have also tried an ADATA 16GBMicro SD class 10 with same result

@earlephilhower
Copy link
Collaborator

earlephilhower commented Apr 28, 2019

..edit..since I can't spell PIN...and can't spell "since" either, it seems...

Also tried the new library ReadWrite.ino modifing
#define SD_CS_PIN 4 to not change the wiring
same error:
Initializing SD card...initialization failed!

The SD_CS_PIN define isn't used anywhere in the SD library:

earle@server:~/Arduino/hardware/esp8266com/esp8266/libraries/SD$ grep -r SD_CS_PIN
earle@server:~/Arduino/hardware/esp8266com/esp8266/libraries/SD$ 

SD_CS_PIN is used in some SdFat examples, but there is no actual use in the low-level libs, so defining it is a no-op, I think.

Can you edit the readfiles.ino example and change the same line I showed SD.begin(4) -> SD.begin(D0) to your pin (making sure to use appropriate D4 or whatever CS pin macro, or the raw integer if you're very sure of the mapping between 8266 internal GPI numbering and your board)?

@luc-github
Copy link
Contributor Author

I am not sure to understand what you mean SD_CS_PIN is used in file

#include <SPI.h>
//#include <SD.h>
#include "SdFat.h"

using namespace sdfat;

SdFat SD;

#define SD_CS_PIN 4
File myFile;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Initializing SD card...");

  if (!SD.begin(SD_CS_PIN)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

@earlephilhower
Copy link
Collaborator

Ahha, I think I see the difference:

I am using the SD library example ReadFiles.ino, which is the same as was always included in the ESP8266 ARduino tree.

You are using the ESP8266SdFat example ReadFiles.ino which is different and much updated by the SdFat maintainer. That library was just added by me since we threw out the unmaintained, cut-n-pasted version from Arduino.cc

So, there is something in the SdFat examples which is incorrect, but the SD ones are fine. LEt me see. It's probably a simple define/namespace/config issue.

@luc-github
Copy link
Contributor Author

luc-github commented Apr 28, 2019

and about SD library listfiles.ino :

Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

sample is not good anymore ?

@earlephilhower
Copy link
Collaborator

Think we cross-posted. You're not using the SD examples, but the SdFat ones...

@luc-github
Copy link
Contributor Author

read my first post I tried both

@earlephilhower
Copy link
Collaborator

So, what's going on is the ESP8266SdFat examples have an updated, incompatible with the older SD SD.h where they make an ESP8266-incompatible File (defined in a private header) and do other badness. It's fine for other systems where they're the only filesystem, just not good for us. And not needed since our SD.h is compatible with all our other Filesystems (SPIFFS, LittleFS, etc.).

Basically, I think the only answer is to ignore the SdFat examples and use the 8266-compatible SD ones. I can blow the examples away from the repo to avoid this issue.

@earlephilhower
Copy link
Collaborator

read my first post I tried both

For the SD one, did you modify the SD.begin() line or set the #define? The #define would be unused. I'm unable to repro any failure, so we'll need some others to chime in to get any other idea.

@luc-github
Copy link
Contributor Author

luc-github commented Apr 28, 2019

the sample code from SD :
https://github.com/esp8266/Arduino/blob/master/libraries/SD/examples/listfiles/listfiles.ino

I did not change anything - same code in 2.5 works and same code failed in GIT

both are identical sample and identical hardware and identical SD card

@luc-github
Copy link
Contributor Author

luc-github commented Apr 28, 2019

I changed code like you:

 if (!SD.begin(D0)) {
...

and I get

C:\Users\user\Desktop\listfiles\listfiles.ino: In function 'void setup()':

listfiles:38:17: error: 'D0' was not declared in this scope

   if (!SD.begin(D0)) {

                 ^

Multiple libraries were found for "SD.h"
 Used: C:\Users\user\Documents\Arduino\hardware\esp8266com\esp8266\libraries\SD
 Not used: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\libraries\SD
Using library SPI at version 1.0 in folder: C:\Users\user\Documents\Arduino\hardware\esp8266com\esp8266\libraries\SPI 
Using library SD at version 2.0.0 in folder: C:\Users\user\Documents\Arduino\hardware\esp8266com\esp8266\libraries\SD 
Using library SDFS at version 0.1.0 in folder: C:\Users\user\Documents\Arduino\hardware\esp8266com\esp8266\libraries\SDFS 
Using library ESP8266SdFat at version 1.0.16 in folder: C:\Users\user\Documents\Arduino\hardware\esp8266com\esp8266\libraries\ESP8266SdFat 
exit status 1
'D0' was not declared in this scope

is that normal ?

@earlephilhower
Copy link
Collaborator

That's definitely not normal.

Do you maybe have the GIT and the IDE version installed at the same time? Need to uninstall one to use the other successfully. The lack of a D0 define means even basic Arduino.h isn't working for you. Assuming that there is a "D0" on your board, of course. :)

@luc-github
Copy link
Contributor Author

Yes I carefully uninstall 2.5 , removed url from preferences, deleted directories:
image

I use ESP8266 generic board as mentioned in bug description:

Settings in IDE
Module: Generic ESP8266 Module
Flash Mode:	DOUT
Flash Size: 4MB
lwip Variant: v2 Lower Memory
Reset Method:	nodemcu
Flash Frequency:	40Mhz
CPU Frequency:	160MHz
Upload Using:	SERIAL
Upload Speed: 115200

where is the D0 defined ? I do not see it in pins_arduino.h
image

@luc-github
Copy link
Contributor Author

luc-github commented Apr 28, 2019

Ok seems D0 is defined for NodeMCU but not for generic
D0 is GPIO 16
changing wire to D0 and changing

if (!SD.begin(16)) {

still
Initializing SD card...initialization failed!

I originaly though it was my setup/ hardware - but rollback to 2.5 works and also on ESP32 that is why I raised issue

@TD-er
Copy link
Contributor

TD-er commented Apr 28, 2019

As a short reply to me being mentioned.

I know there is support in ESPeasy for SD card (must enable and build yourself), but not many use it.
At least I've not seen reported issues regarding SD card support being broken recently.

@earlephilhower
Copy link
Collaborator

Thanks, @TD-er .

@luc-github it could be a different default speed for the SPI bus which your reader/wiring may not support.

Try changing the begin line to set the SPI speed as well:

if (!SD.begin(16, SD_SCK_MHZ(1)) { 
...

Obviously using the CS pin you've wired up in place of 16.

@luc-github
Copy link
Contributor Author

luc-github commented Apr 28, 2019

Actually I tried before SPI_QUARTER_SPEED and SPI_HALF_SPEED without success, I tried these values because they worked with 2016 version of SDFat beta and because SPI_HALF_SPEED was default value in 2.5.

but SD_SCK_MHZ(1) seems to work (I did not know this way) , new SD need lower SPI SPEED ?

@earlephilhower
Copy link
Collaborator

That's probably a question better left to the SDFAT maintainer, @greiman. I just brought his massively updated and improved (LFNs, etc.) version into the 8266 Arduino repo, but did not actually change any of the guts (other than to place it in a namespace).

I think it's more likely the older settings were not doing what they were supposed to and you were really going slower/faster than expected on the board. Or, it could be some change to SPI (which I haven't been following, sorry) that's now interpreting things differently.

@earlephilhower
Copy link
Collaborator

And, FWIW, I'm not seeing any problem w/o the 1MHZ specification (i.e. I just use "SD.begin(XX)", no SPI settings param) when I run the SD examples. So it's very odd, maybe something timing related on the SPI bus changed....

@luc-github
Copy link
Contributor Author

Thank you, well looking at speeds in SD 2.5 they are defined here :

#ifdef ESP8266
#include "SPI.h"
uint32_t const SPI_FULL_SPEED = 8000000;
uint32_t const SPI_HALF_SPEED = 4000000;
uint32_t const SPI_QUARTER_SPEED = 2000000;
#else
/** Set SCK to max rate of F_CPU/2. See Sd2Card::setSckRate(). */
uint8_t const SPI_FULL_SPEED = 0;
/** Set SCK rate to F_CPU/4. See Sd2Card::setSckRate(). */
uint8_t const SPI_HALF_SPEED = 1;
/** Set SCK rate to F_CPU/8. Sd2Card::setSckRate(). */
uint8_t const SPI_QUARTER_SPEED = 2;
#endif

#ifdef ESP8266
#include "SPI.h"
uint32_t const SPI_FULL_SPEED = 8000000;
uint32_t const SPI_HALF_SPEED = 4000000;
uint32_t const SPI_QUARTER_SPEED = 2000000;
#else
/** Set SCK to max rate of F_CPU/2. See Sd2Card::setSckRate(). */
uint8_t const SPI_FULL_SPEED = 0;
/** Set SCK rate to F_CPU/4. See Sd2Card::setSckRate(). */
uint8_t const SPI_HALF_SPEED = 1;
/** Set SCK rate to F_CPU/8. Sd2Card::setSckRate(). */
uint8_t const SPI_QUARTER_SPEED = 2;
#endif

in new SD :

new SD
// SPI divisor constants
/** Set SCK to max rate of F_CPU/2. */
#define SPI_FULL_SPEED SD_SCK_MHZ(50)
/** Set SCK rate to F_CPU/3 for Due */
#define SPI_DIV3_SPEED SD_SCK_HZ(F_CPU/3)
/** Set SCK rate to F_CPU/4. */
#define SPI_HALF_SPEED SD_SCK_HZ(F_CPU/4)
/** Set SCK rate to F_CPU/6 for Due */
#define SPI_DIV6_SPEED SD_SCK_HZ(F_CPU/6)
/** Set SCK rate to F_CPU/8. */
#define SPI_QUARTER_SPEED SD_SCK_HZ(F_CPU/8)
/** Set SCK rate to F_CPU/16. */
#define SPI_EIGHTH_SPEED SD_SCK_HZ(F_CPU/16)
/** Set SCK rate to F_CPU/32. */
#define SPI_SIXTEENTH_SPEED SD_SCK_HZ(F_CPU/32)

which give following values:

SPI_FULL_SPEED 50000000
SPI_DIV3_SPEED 26666666
SPI_HALF_SPEED 20000000
SPI_DIV6_SPEED 13333333
SPI_QUARTER_SPEED 10000000
SPI_EIGHTH_SPEED 5000000
SPI_SIXTEENTH_SPEED 2500000
SD_SCK_MHZ(1) 1000000

Using SPI_SIXTEENTH_SPEED in new SD instead of SPI_HALF_SPEED in 2.5 SD make it works on my side
I do not know if SPI changed but using same range of value give consistent behavior

I can imagine your setup is using fast hardware, so going faster is not an issue, but on my side it is obviously a problem, and so even the new SD is far better than old one - somewhere compatibility is broken and a note concerning speeds would be welcome IMHO.

On my side I was using old SDFat beta 2016 until now, so I can do speed adjustment in same time I will integrate the new SD instead of old SDFat beta, so impact is not high, but for people that do not change anything (like I did using sample code) they may get surprised if same code failed now.

Thank you again for your great work - new lib will be far better than old one.

@earlephilhower
Copy link
Collaborator

earlephilhower commented Apr 29, 2019

@luc-github I'm reopening this as it's a simple fix to be backwards compatible.

I think @igrr fixed the ancient Arduino libs, manually. The same change looks not so bad in the real sdfat lib...

@earlephilhower earlephilhower removed the waiting for feedback Waiting on additional info. If it's not received, the issue may be closed. label Apr 29, 2019
@earlephilhower earlephilhower added this to the 2.5.1 milestone Apr 29, 2019
earlephilhower added a commit to earlephilhower/Arduino that referenced this issue Apr 29, 2019
Replace the default SPI_FULL/HALF/QUARTER_SPEEDs with ESP8266
appropriate ones taken from the original SD library that @igrr modified.

Fixes esp8266#6019
earlephilhower added a commit that referenced this issue Apr 29, 2019
Replace the default SPI_FULL/HALF/QUARTER_SPEEDs with ESP8266
appropriate ones taken from the original SD library that @igrr modified.

Fixes #6019
@earlephilhower
Copy link
Collaborator

Thanks again for the debug, @luc-github .

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

Successfully merging a pull request may close this issue.

3 participants