From 3dd0a705e01f333af9777f8a76afd9f28bca9aa6 Mon Sep 17 00:00:00 2001 From: devyte Date: Fri, 30 Nov 2018 15:20:57 -0300 Subject: [PATCH 1/3] Add SD.end() method with endSPI flag as arg --- libraries/SD/src/SD.cpp | 11 +++++++++++ libraries/SD/src/SD.h | 18 ++++++++++++++++++ libraries/SD/src/utility/Sd2Card.cpp | 18 ++++++++++++++++++ libraries/SD/src/utility/Sd2Card.h | 3 +++ 4 files changed, 50 insertions(+) diff --git a/libraries/SD/src/SD.cpp b/libraries/SD/src/SD.cpp index ce575128b7..d00273e3b8 100644 --- a/libraries/SD/src/SD.cpp +++ b/libraries/SD/src/SD.cpp @@ -350,6 +350,17 @@ boolean SDClass::begin(uint8_t csPin, uint32_t speed) { root.openRoot(volume); } +//Warning: see comment in SD.h about possible card corruption. +void SdClass::end(bool closeSPI) +{ + if(card.errorCode() == 0 && root.isOpen()) { + root.close(); //Warning: this calls sync(), see above comment about corruption. + } + + card.end(closeSPI); +} + + // this little helper is used to traverse paths SdFile SDClass::getParentDir(const char *filepath, int *index) { // get parent directory diff --git a/libraries/SD/src/SD.h b/libraries/SD/src/SD.h index 9697d12923..12ce30955a 100644 --- a/libraries/SD/src/SD.h +++ b/libraries/SD/src/SD.h @@ -90,6 +90,24 @@ class SDClass { // before other methods are used. 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 closeSPI = true); + + // Open the specified file/directory with the supplied mode (e.g. read or // write, etc). Returns a File object for interacting with the file. // Note that currently only one file can be open at a time. diff --git a/libraries/SD/src/utility/Sd2Card.cpp b/libraries/SD/src/utility/Sd2Card.cpp index 8c8fadaf44..dd2db4bf63 100644 --- a/libraries/SD/src/utility/Sd2Card.cpp +++ b/libraries/SD/src/utility/Sd2Card.cpp @@ -349,6 +349,24 @@ uint8_t Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) { chipSelectHigh(); 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. diff --git a/libraries/SD/src/utility/Sd2Card.h b/libraries/SD/src/utility/Sd2Card.h index c7e54f66b2..295a1ff32a 100644 --- a/libraries/SD/src/utility/Sd2Card.h +++ b/libraries/SD/src/utility/Sd2Card.h @@ -200,6 +200,9 @@ class Sd2Card { } uint8_t init(uint8_t sckRateID, uint8_t chipSelectPin); #endif + + void end(bool endSPI = false); + void partialBlockRead(uint8_t value); /** Returns the current value, true or false, for partial block read. */ uint8_t partialBlockRead(void) const {return partialBlockRead_;} From 86eaad968f2ae1cbeea4b59e1e9b8db6d9fda72c Mon Sep 17 00:00:00 2001 From: devyte Date: Fri, 30 Nov 2018 15:26:50 -0300 Subject: [PATCH 2/3] cleanup and fix a default arg --- libraries/SD/src/SD.cpp | 4 ++-- libraries/SD/src/SD.h | 2 +- libraries/SD/src/utility/Sd2Card.h | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/libraries/SD/src/SD.cpp b/libraries/SD/src/SD.cpp index d00273e3b8..f5818dabea 100644 --- a/libraries/SD/src/SD.cpp +++ b/libraries/SD/src/SD.cpp @@ -351,13 +351,13 @@ boolean SDClass::begin(uint8_t csPin, uint32_t speed) { } //Warning: see comment in SD.h about possible card corruption. -void SdClass::end(bool closeSPI) +void SdClass::end(bool endSPI) { if(card.errorCode() == 0 && root.isOpen()) { root.close(); //Warning: this calls sync(), see above comment about corruption. } - card.end(closeSPI); + card.end(endSPI); } diff --git a/libraries/SD/src/SD.h b/libraries/SD/src/SD.h index 12ce30955a..c998c8bbe6 100644 --- a/libraries/SD/src/SD.h +++ b/libraries/SD/src/SD.h @@ -105,7 +105,7 @@ class SDClass { 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 closeSPI = true); + void end(bool endSPI = true); // Open the specified file/directory with the supplied mode (e.g. read or diff --git a/libraries/SD/src/utility/Sd2Card.h b/libraries/SD/src/utility/Sd2Card.h index 295a1ff32a..6b1d164905 100644 --- a/libraries/SD/src/utility/Sd2Card.h +++ b/libraries/SD/src/utility/Sd2Card.h @@ -201,7 +201,7 @@ class Sd2Card { uint8_t init(uint8_t sckRateID, uint8_t chipSelectPin); #endif - void end(bool endSPI = false); + void end(bool endSPI = true); void partialBlockRead(uint8_t value); /** Returns the current value, true or false, for partial block read. */ From 170636772f5426566416e958b89ed66d88852470 Mon Sep 17 00:00:00 2001 From: devyte Date: Fri, 30 Nov 2018 16:29:04 -0300 Subject: [PATCH 3/3] Fix typo --- libraries/SD/src/SD.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/SD/src/SD.cpp b/libraries/SD/src/SD.cpp index f5818dabea..0c3a1b0935 100644 --- a/libraries/SD/src/SD.cpp +++ b/libraries/SD/src/SD.cpp @@ -351,7 +351,7 @@ boolean SDClass::begin(uint8_t csPin, uint32_t speed) { } //Warning: see comment in SD.h about possible card corruption. -void SdClass::end(bool endSPI) +void SDClass::end(bool endSPI) { if(card.errorCode() == 0 && root.isOpen()) { root.close(); //Warning: this calls sync(), see above comment about corruption.