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)