A small foot print code for formatting SD cards

In my application which only has a 4L x 20C LCD for user interface, I need to include code for formatting / erase the SD card used in the Ethernet Shield board.

I have seen and also used the Formatter example that comes with SDFat library. No problem. But that being a generic one is quite a large program. Mt requirement is simple - I always will use a 4GB SDcard and when the user chooses to Format or Erase I should be able to invoke the function to do it.

Of course I can strip this functionality from the Formatter example but being not a expert C++ programmer, not very confident to do it.

Would be happy if anyone has already done this kind of a thing and can share it. ( Or even a note about the functions to eliminate is also good enough )

TIA.

There is a function in SdFat that restores an SD card to its original format. It only depends on the MBR and FAT boot sector being intact.

Calculating these is the difficult part of formatting an SD but they are rarely corrupted.

Having a 4GB card doesn't help much since the number of blocks in 4GB cards varies.

Here is a program that will return an SD to its original format.

// Example to wipe all data from an already formatted SD.
#include <SPI.h>
#include "SdFat.h"
const int chipSelect = SS;

SdFat sd;

void setup() {
  int c;
  Serial.begin(9600);
  while (!Serial) {}  // wait for Leonardo
  Serial.println("Type 'Y' to wipe all data.");
  while ((c = Serial.read()) <= 0) {}
  if (c != 'Y') {
    sd.errorHalt("Quitting, you did not type 'Y'.");
  }
  if (!sd.begin(chipSelect)) {
    sd.initErrorHalt();
  }
  // Use wipe() for no dot progress indicator.
  if (!sd.wipe(&Serial)) {
    sd.errorHalt("Wipe failed.");
  }
  // Must reinitialize after wipe.
  if (!sd.begin(chipSelect)) {
    sd.errorHalt("Second init failed.");
  }
  Serial.println("Done");
}

void loop() {
}

Perfect !!

That's exactly what I was looking for. Let me check it out once I am back with my system and let you know.

Thanks so much !!

Yes. It worked as expected. Good !

While I tried to read all file names with the OpenNext example I was surprised to see the Date / Time of modification same for all files !! When writing a file to the SD card how do I send in the date time reference ? Refer attachment ...

( I have a RTC running in the code and can use it to get in this data but not sure how...)

Look at this.

Yes that link had all that info that I wanted about adding date /time input to the file creation reference. Thanks.

And just a general query : How good are these SD cards in handling say about two wipes in a day ? I am OK even if they last for about a year at this rate . Or is it not a good idea anyway to wipe them so often ? I know there are documented specifications but nothing like a feedback from who has used them often. :slight_smile:

Edit: I modified this to account for the two FAT areas.

Wipe won't wear a 4GB card much. Modern SD cards have a wear leveling algorithm that uses a physical to virtual mapping.

Each physical area can be written about 1,000 times. This means you can write about 4TB of data. The wear leveling map makes sure each area is equally worn.

Assume your card was originally formatted with this formatter.

wipe() writes about 1,000,000 bytes per call. You could call wipe (4,000,000,000,000/1,000,000) = 4,000,000 times. (Actually it's 4,000,000 times independent of the card size).

Few users wear-out an SD card with an Arduino.

Wow - automatic wear levelling - that was a very reassuring thing to hear.
Never knew that.

Thanks so much !!