Skip to content
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

De-initialize/Reset SD Card to save power #351

Closed
savejeff opened this issue Dec 10, 2021 · 3 comments
Closed

De-initialize/Reset SD Card to save power #351

savejeff opened this issue Dec 10, 2021 · 3 comments

Comments

@savejeff
Copy link

savejeff commented Dec 10, 2021

Found a solution. could this be tags as a feature request ?

Hi,

I'm working with an ESP32 based board that has already an inbuild SD Card Slot.
Looking into sleep current I noticed that the SD Card takes more power after sd.end than before sd.begin.

If I completely remove the sd card code but have the SD Card plugged in I get ~1mA (might be lower, limited by decimal points of measurement)
when I init the sd card and de-init it again, i get around 13-20 mA.
here is the interesting thing: in deep sleep, when i take out the sd card and put it in again it goes down to ~1mA.

Thus I conclude the sd card is after sd.end still held in some initialized state.
Is there a way besides cutting the power to the sd card with a transistor (which is not possible as the board is a given) to reset/de-init the sd card.

I read another thread from earlier that just stopping communicating with the sd would put it into a sleep state, I'm not seeing this. I tried putting all sd spi spins in input/input_pullup/input_pulldown and it didn't really help

@savejeff
Copy link
Author

savejeff commented Dec 10, 2021

I Found a nice solution here:

https://forum.arduino.cc/t/resolved-how-can-i-reboot-an-sd-card/349700

sending 0xFF over SPI often enough will cause the SD Card to reset and send 0xFF back.
It can be used to reset a card that is stuck or not responding after a crash but also to "unmount" the sd card.

this simple code


pinMode(PIN_SPI_CS_SD, OUTPUT);
digitalWrite(PIN_SPI_CS_SD, LOW);

for(int i = 0; i < 10; i++)
{
  byte v = SPI.transfer(0xFF);
  if(v == 0xFF)
	  break;
}

digitalWrite(PIN_SPI_CS_SD, HIGH);

reduces the power consumption to sub 1mA after sd.end.

Would it be possible to add this to the sd.end code or add a sd.reset() function to also use it to reset a sd card if sd.begin fails?

@greiman
Copy link
Owner

greiman commented Dec 10, 2021

Some cards remain active after chips select is high until they see a few clocks.

Try sending a 0XFF byte something like this with SD chip select high.

SPI.begin();
SPI.beginTransaction(SPISettings(400000, MSBFIRST, SPI_MODE0));
SPI.transfer(0XFF);
SPI.endTransaction();
SPI.end();

I will experiment with the sd.end() call to see if adding a transfer of an extra byte improves sleep.

You can't really put a card back in the initial power on state. You can only reinitialize a card. For example once a card is in SPI mode you must cycle power to use 4-bit SDIO mode.

@savejeff
Copy link
Author

looks like i was a minutes faster with the solution.

Would look to have a sd.reset function added.

Might be helpful for some people when they have a system crash.

i can test this with a couple of sd cards. i have around 7 different types of sd cards here

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

2 participants