deinitialization for SD library

At use SD card in devices with a battary power, the great value has consumption of energy by the card which can make 40-80 ma.
For reduction of consumption of energy by a SD card it is necessary to switch it in IDLE state when it is not used.
For this purpose to add to SD library a method making deinitialization cards and SPI:

in file Sd2Card.h to add the bolded:

/**
* \class Sd2Card
* \brief Raw access to SD and SDHC flash memory cards.
*/
class Sd2Card {
public:
_ /** Construct an instance of Sd2Card. */_

  • Sd2Card(void) : errorCode_(0), inBlock_(0), partialBlockRead_(0), type_(0) {}*
  • uint32_t cardSize(void);*
  • uint8_t erase(uint32_t firstBlock, uint32_t lastBlock);*
  • uint8_t eraseSingleBlockEnable(void);*
    _ /**_
    _ * \return error code for last error. See Sd2Card.h for a list of error codes._
    _ */_
  • uint8_t errorCode(void) const {return errorCode_;}*
    _ /** \return error data for last error. */_
  • uint8_t errorData(void) const {return status_;}*
    _ /**_
    _ * Initialize an SD flash memory card with default clock rate and chip_
    * select pin. See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin).
    _ */_
  • uint8_t init(void) {*
  • return init(SPI_FULL_SPEED, SD_CHIP_SELECT_PIN);*
  • }*
    _ /**_
    _ * Initialize an SD flash memory card with the selected SPI clock rate_
    _ * and the default SD chip select pin._
    * See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin).
    _ */_
  • uint8_t init(uint8_t sckRateID) {*
  • return init(sckRateID, SD_CHIP_SELECT_PIN);*
  • }*
  • uint8_t init(uint8_t sckRateID, uint8_t chipSelectPin);*
    __ /*
    * deinitialization SD flash memory and stop SPI__
    __ /__
    _
    * void stop();**_

In file Sd2Card.cpp:

void Sd2Card::stop()
{
** // command to go idle in SPI mode**
** status_ = cardCommand(CMD0, 0);
#ifndef SOFTWARE_SPI**
** SPCR &= ~BV(SPE);
_
#endif**

}

And also to add this method in a wrapper. In file SD.h:

class SDClass {
private:

  • // These are required for initialisation and use of sdfatlib*
  • Sd2Card card;*
  • SdVolume volume;*
  • SdFile root;*

public:

  • // This needs to be called to set up the connection to the SD card*
  • // before other methods are used.*
  • boolean begin(uint8_t csPin = SD_CHIP_SELECT_PIN);*
    ** void stop();**

In file SD.spp:

void SDClass::stop()
{
** card.stop();**
}

Now when the card isn't necessary it is possible to cause this method. See example:

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    SD.stop();
    return;
  }

  // Using SD card....
  
  // deinitialize SD card
  SD.stop();

The card in IDLE-stae will consume considerably smaller current that will lead to the big economy of energy of the battery.

P.S.
Excuse for my bad English

Best regards,
Alexander (Azimutx)

makes sense for some apps, have you checked the author of the library? or - Google Code Archive - Long-term storage for Google Code Project Hosting.

Thanks Azimutx for this. The major hurdle for the data logger that i am working on was the sd card power consumption. I was initially going to use an opticoupler on the cards ground. This saves me the effort, and the pin.

What type card are you using?

Modern cards draw less than 1 ma when initialized and idle. I tested a Transcend 8 GB class 6 card and it draws less than 1 ma before and after it is initialized. It draws over 30 ma during read access and more than 40 ma for writes.

An older SanDisk 1 GB card, manufactured in 2006, draws 1 ma before initialization and 8 ma when initialized. I think this card was designed long before 2006.