Go Down

Topic: deinitialization for SD library (Read 2724 times) previous topic - next topic


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 {
  /** 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) {
   * 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);
     SPCR &= ~_BV(SPE);

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

class SDClass {

  // These are required for initialisation and use of sdfatlib
  Sd2Card card;
  SdVolume volume;
  SdFile root;
  // 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()

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

Code: [Select]

  // 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:

  // Using SD card....
  // deinitialize SD card

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

Excuse for my bad English

Best regards,
Alexander (Azimutx)


makes sense for some apps, have you checked the author of the library? or - http://code.google.com/p/beta-lib/issues/list
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)


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.

Go Up