SD library help

Hi,

I've been using the SD library from adafruit in my datalog project because it has an SD.end() function which allows to unmount de SD card.

I use this code to unmount the SD card:

else if (Serial.peek() == 'u')
 {
  Serial.read(); // Clear serial buffer
  Serial.println(F("- SD card unmounting -"));
  Serial.print(F("SD card unmounted"));
  SD.end();
  Serial.println(); // Print newline
  Serial.println(); // Print newline
  repetitiveStrings(); // Print information
  }

As the official arduino SD library does not have and SD.end() function how can I adapt my code to unmount de SD card?

Thanks

Vasco

All end does is close the internal root file.

//call this when a card is removed. It will allow you to inster and initialise a new card.
void SDClass::end()
{
  root.close();
}

It sort of fixes a bug in SD.begin() that has been in the "official" SD.h for over five years.

end() does not do what unmount() would do. It does not flush data and properly close files.

You can edit the standard SD.h and add the root.close() statement to SD.begin().

SD.begin() is at about line 335 of SD.cpp.

boolean SDClass::begin(uint8_t csPin) {
  /*

    Performs the initialisation required by the sdfatlib library.

    Return true if initialization succeeds, false otherwise.

   */

  root.close();  // <============== ADD this line

  return card.init(SPI_HALF_SPEED, csPin) &&
         volume.init(card) &&
         root.openRoot(volume);
}

If you like the end call, edit SD.h and add this at about line 70.

  // 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 end() {root.close();}  // <================ ADD
  
  // 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.
  File open(const char *filename, uint8_t mode = FILE_READ);