Skip to content

Commit 7f7e658

Browse files
authored
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
1 parent 92373a9 commit 7f7e658

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

libraries/SD/src/SD.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,17 @@ boolean SDClass::begin(uint8_t csPin, uint32_t speed) {
350350
root.openRoot(volume);
351351
}
352352

353+
//Warning: see comment in SD.h about possible card corruption.
354+
void SDClass::end(bool endSPI)
355+
{
356+
if(card.errorCode() == 0 && root.isOpen()) {
357+
root.close(); //Warning: this calls sync(), see above comment about corruption.
358+
}
359+
360+
card.end(endSPI);
361+
}
362+
363+
353364
// this little helper is used to traverse paths
354365
SdFile SDClass::getParentDir(const char *filepath, int *index) {
355366
// get parent directory

libraries/SD/src/SD.h

+18
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,24 @@ class SDClass {
9090
// before other methods are used.
9191
boolean begin(uint8_t csPin = SD_CHIP_SELECT_PIN, uint32_t speed = SPI_HALF_SPEED);
9292

93+
/*
94+
In the following sequence:
95+
//Insert SD Card A
96+
SD.begin()
97+
//do operations
98+
//remove card A
99+
//insert SD card B
100+
SD.end()
101+
102+
It is possible that card A becomes corrupted due to removal before calling SD.end().
103+
It is possible that card B becomes corrupted if there were ops pending for card A at the time SD.end() is called.
104+
105+
Call SD.end() or SD.end(true) to shut everything down.
106+
Call SD.end(false) to shut everything but the SPI object down.
107+
*/
108+
void end(bool endSPI = true);
109+
110+
93111
// Open the specified file/directory with the supplied mode (e.g. read or
94112
// write, etc). Returns a File object for interacting with the file.
95113
// Note that currently only one file can be open at a time.

libraries/SD/src/utility/Sd2Card.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,24 @@ uint8_t Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) {
349349
chipSelectHigh();
350350
return false;
351351
}
352+
353+
//------------------------------------------------------------------------------
354+
/**
355+
* Shut down Sd2Card, which at this point means end SPI.
356+
*
357+
* \param[in] endSPI The value true (non-zero) or FALSE (zero).
358+
359+
Call card.end() or card.end(true) to shut everything down.
360+
Call card.end(false) to shut everything but the SPI object down.
361+
*/
362+
void Sd2Card::end(bool endSPI)
363+
{
364+
if(endSPI)
365+
SPI.end();
366+
}
367+
368+
369+
352370
//------------------------------------------------------------------------------
353371
/**
354372
* Enable or disable partial block reads.

libraries/SD/src/utility/Sd2Card.h

+3
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ class Sd2Card {
200200
}
201201
uint8_t init(uint8_t sckRateID, uint8_t chipSelectPin);
202202
#endif
203+
204+
void end(bool endSPI = true);
205+
203206
void partialBlockRead(uint8_t value);
204207
/** Returns the current value, true or false, for partial block read. */
205208
uint8_t partialBlockRead(void) const {return partialBlockRead_;}

0 commit comments

Comments
 (0)