1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +03:00

Add SD.end() method, including arg to leave SPI up (#5402)

* Add SD.end() method with endSPI flag as arg

* cleanup and fix a default arg

* Fix typo
This commit is contained in:
Develo 2018-11-30 18:25:47 -03:00 committed by GitHub
parent 92373a9837
commit 7f7e658d66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 0 deletions

View File

@ -350,6 +350,17 @@ boolean SDClass::begin(uint8_t csPin, uint32_t speed) {
root.openRoot(volume); root.openRoot(volume);
} }
//Warning: see comment in SD.h about possible card corruption.
void SDClass::end(bool endSPI)
{
if(card.errorCode() == 0 && root.isOpen()) {
root.close(); //Warning: this calls sync(), see above comment about corruption.
}
card.end(endSPI);
}
// this little helper is used to traverse paths // this little helper is used to traverse paths
SdFile SDClass::getParentDir(const char *filepath, int *index) { SdFile SDClass::getParentDir(const char *filepath, int *index) {
// get parent directory // get parent directory

View File

@ -90,6 +90,24 @@ public:
// before other methods are used. // before other methods are used.
boolean begin(uint8_t csPin = SD_CHIP_SELECT_PIN, uint32_t speed = SPI_HALF_SPEED); boolean begin(uint8_t csPin = SD_CHIP_SELECT_PIN, uint32_t speed = SPI_HALF_SPEED);
/*
In the following sequence:
//Insert SD Card A
SD.begin()
//do operations
//remove card A
//insert SD card B
SD.end()
It is possible that card A becomes corrupted due to removal before calling SD.end().
It is possible that card B becomes corrupted if there were ops pending for card A at the time SD.end() is called.
Call SD.end() or SD.end(true) to shut everything down.
Call SD.end(false) to shut everything but the SPI object down.
*/
void end(bool endSPI = true);
// Open the specified file/directory with the supplied mode (e.g. read or // Open the specified file/directory with the supplied mode (e.g. read or
// write, etc). Returns a File object for interacting with the file. // write, etc). Returns a File object for interacting with the file.
// Note that currently only one file can be open at a time. // Note that currently only one file can be open at a time.

View File

@ -349,6 +349,24 @@ uint8_t Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) {
chipSelectHigh(); chipSelectHigh();
return false; return false;
} }
//------------------------------------------------------------------------------
/**
* Shut down Sd2Card, which at this point means end SPI.
*
* \param[in] endSPI The value true (non-zero) or FALSE (zero).
Call card.end() or card.end(true) to shut everything down.
Call card.end(false) to shut everything but the SPI object down.
*/
void Sd2Card::end(bool endSPI)
{
if(endSPI)
SPI.end();
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
/** /**
* Enable or disable partial block reads. * Enable or disable partial block reads.

View File

@ -200,6 +200,9 @@ class Sd2Card {
} }
uint8_t init(uint8_t sckRateID, uint8_t chipSelectPin); uint8_t init(uint8_t sckRateID, uint8_t chipSelectPin);
#endif #endif
void end(bool endSPI = true);
void partialBlockRead(uint8_t value); void partialBlockRead(uint8_t value);
/** Returns the current value, true or false, for partial block read. */ /** Returns the current value, true or false, for partial block read. */
uint8_t partialBlockRead(void) const {return partialBlockRead_;} uint8_t partialBlockRead(void) const {return partialBlockRead_;}