SDFAT read/write example

There seems to be a large number of examples in the library folder.

Try ...\hardware\teensy\avr\libraries\SdFat\examples\examplesV1\ReadWrite\ReadWrite.ino

If it does not work - post on the TD 1.54 b5 thread so it can be looked at.

Quick look shows 74 various example INO sketches.
 
@defragster, the V1 example of ReadWrite.ino doesn't compile for V2. I'm trying to move from SD lib SdFat 2 and running into some problems so i was looking for a simple example for V2 like ReadWrite.ino. I want to do a simple open file and write to it.
 
It should work if using it with TD 1.54 b5

If it has issues then it needs to be updated for release with TeensyDuino 1.54 that will have SdFat2 embedded when using SD.h.

As noted in prior post :: "If it does not work - post on the TD 1.54 b5 thread so it can be looked at."

There may be a general subset of those examples that need the same fixes for use. Please help find the issues for resolution with such a post on the beta thread.
 
Here is an example of write/read with SdFat 2.0 beta. As I have been working with Arduino 1.8.13 and TD 1.54B5 I have used the new SD library and the tweaked version of Sdfat. A lot of testing has been done so far with the Sdfat and SD API. Here is a version of readWrite.ino you can use to look at for reading and writing to both built in and external SD cards. It is fairly well commented for understanding how to use the SdfFat API for reading and writing to an SD card device. I adapted it from the readWrite.ino example in the V1 examples folder which is pretty much the same as the one in the SD examples folder. It is modified for use with SdFat.

Some code from Sdfat-beta was used in this sketch to properly setup access to both SD card devices. I have added lots of comments to the sketch to explain it's usage. There are some parts of setting up the chip selects that I do not understand yet and have commented that in the code.

I am curious to see it tested. It should work close to the same as the SD version. Here is the sketch:
Code:
/*
  SD card read/write with SdFat-beta

 This example shows how to read and write data to and from an SD card file
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13

 created   Nov 2010
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe
 Modified 12-15-20
 by Warren Watson
 This example code is in the public domain.

 */

#include "SdFat.h"

// Uncomment 'USE_EXTERNAL_SD' define to use an external SD card adapter or leave
// it commented to use the built in sd card.
//#define USE_EXTERNAL_SD 

#ifdef USE_EXTERNAL_SD
const uint8_t SD_CS_PIN = SS;
#define SPI_CLOCK SD_SCK_MHZ(10)
#define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI, SPI_CLOCK)
#else // Use built in SD card.
// Why does this #ifdef have to be here? I do not understand. If it is defined
// then why does the compiler give this error without the #ifdef/#endif:
// "ReadWrite:39: error: 'SDCARD_SS_PIN' was not declared in this scope"
// Either it is defined previously or it is not right???
// This one has me stumped. Uncomment the #ifdef and #endif and you will see what
// happens. I am probably missing something again! 
#ifdef SDCARD_SS_PIN
const uint8_t SD_CS_PIN = SDCARD_SS_PIN;
#endif // SDCARD_SS_PIN
// Setup for built in SD card.
#define SPI_CLOCK SD_SCK_MHZ(50)
#define SD_CONFIG SdioConfig(FIFO_SDIO)
#endif // USE_EXTERNAL_SD

// SdFat-beta usage
SdFs sd;
FsFile myFile;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.print("Initializing SD card...");

  if (!sd.begin(SD_CONFIG)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  // NOTE: With SdFat-Beta this is not the case anymore.
  myFile = sd.open("test.txt", FILE_WRITE);

  // Comment out the following line to append text to test.txt.
  myFile.truncate(0);

  // if the file opened okay, write to it:
  if (myFile) {
#ifdef USE_EXTERNAL_SD
    Serial.print("Writing test.txt to external SD card...");
#else
    Serial.print("Writing test.txt to built in SD card...");
#endif
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = sd.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop() {
  // nothing happens after setup
}

Hopefully this helps:)
 
Last edited:
I do not understand.
Does the ReadWrite.ino example in SD library not work?
Is it not the purpose of SD library that you do NOT interface directly with the SdFat library?
The OP asked for a simple SD read/write example for SdFat2 ( which is exactly done with SD)
 
@defragster, the V1 example of ReadWrite.ino doesn't compile for V2. I'm trying to move from SD lib SdFat 2 and running into some problems so i was looking for a simple example for V2 like ReadWrite.ino. I want to do a simple open file and write to it.

@WMXZ "I do not understand."
Me either - not clear? Is this a valid TD 1.54 b5 install with no sketchbook libraries confusing the build?

Awaiting feedback from p#4 - and time to try it here ...
 
Me either - not clear? Is this a valid TD 1.54 b5 install with no sketchbook libraries confusing the build?

Awaiting feedback from p#4 - and time to try it here ...

@WMXZ @defragster -I think i misunderstood what @fluxanode was after. I thought he wanted a version that just used SdFat2. The readWrite.ino works fine with the Sd library which is what FS is about and is the preferred way. All other examples in SD are working as well.

Sorry for any confusion this may have caused:(
 
@WMXZ @defragster @WWatson
The example "backwardscompatibility.ino" does not work with sdfat library when it is set up to use sdfat.

When USE_SD_H is "0" it doesn't compile.
// Set USE_SD_H nonzero to use SD.h.
// Set USE_SD_H zero to use SdFat.h.

Used to work with the older 1.x version and works with SD lib when USE_SD_H is 1.

@wwatson testing your version.

@defragster, is the version of sdfat distributed with the TD 1.54 b5 install the latest version, 2.0.2 that was updated 11 days ago?

And yes i wanted a simple read write example for SdFat that compiles.

Thanks for the help guys!
 
Last edited:
If using "TD 1.54 b5" it includes SdFat { installed with the TeensyInstaller } that is automatically used when specifying SD.h.

> That SdFat.h may be a reference to the original code that won't apply - and perhaps should be edited out for release.

> Do not need to download or bring in ANY external libraries. And if Verbose build info in console shows them used instead of the Teensy version - they must be removed.

Paul has removed the old SD.h code and it automatically links ONLY to the preinstalled SdFat V2 code - AFAIK.

So using SD.h is using SdFat v2 with "TD 1.54 b5"

... a few more interruptions here before I could try it ...
 
@defragster, then is there a need or a reason for the SD lib included in the ...\hardware\teensy\avr\libraries? Seems the SdFat lib should be making the SD lib irrelevant?
 
@defragster, then is there a need or a reason for the SD lib included in the ...\hardware\teensy\avr\libraries? Seems the SdFat lib should be making the SD lib irrelevant?

Other way around! Paul rewrote SD.h to be (map to) the SdFat lib and the goal is have have it transparently replace the old SD code with better performance and function. For instance the AUDIO library used SD.H, and it still does - except the SD code is now acutally the SdFat V2 code.

The goal is that on Teensy any use of SD.H still works without change needed in USER code, though under the covers SD support is now using the SdFat library.

That is why having this work there as needed is important. If there are issues they need to get raised as noted in post #2 on the Beta thread for resolution.
 
Back
Top