TelephoneBill
Well-known member
The purpose of this post is to get a novice (like myself) quickly writing his/her own code using the T3.6/T3.5 built in SD Card peripheral, without having to read through a mountain of detail in various forum locations. (Note - There are lots of other forum posts to search on this topic if you want to dig deeper).
For maximum speed on read/writes, I illustrate use of the "SdFat" library created by Bill Greiman, rather than the traditional Arduino "SD" library. This requires a new library to be uploaded and included into the IDE, and this will be described further in this post. Inside this library, the author has provided a new "sd" class known as "SdFatSdioEX" - the class that is used in this example.
The SD Card socket on the Teensy 3.6 (and 3.5) is intended to work with a newer and faster "SDIO mode" of data transfer, rather than with the older/slower "SPI mode". It employs four parallel data lines, whereas the SPI mode used only one. It does NOT use any of the SPI interfaces shown on the pinout card, but relies on a direct SD I/O interface built into the K66 chip and directly wired to the socket. The T3.6 interface operates a little faster than the T3.5, but both are pretty rapid reaching speeds of the order of 20 MBytes/sec.
Below is a minimal code example for writing/reading data to/from a new file on an SD card. Ensure you have an SD card inserted before downloading the program. Open the "Serial Monitor" once loaded.
(Edit2 - see also post #5 for more features.)
To install the "SdFat" Library...
1) In the IDE, select "Sketch" from the main menu.
2) Select "Include Library" and then "Manage Libraries...".
3) In the filter textbox (top right) of the Library Manager, type "SdFat".
4) Click on the entry for "SdFat" in the list box, Then click "Install".
5) Close the Library Manager once installed.
Edit1 - Removed the DISABLE_CHIP_SELECT line. Removed "sd.errorHalt" lines - stops sd working (replaced with Serial message). Included file "Delete".
Moved "sd.begin()" into main loop - allows card to be removed and re-inserted.
For maximum speed on read/writes, I illustrate use of the "SdFat" library created by Bill Greiman, rather than the traditional Arduino "SD" library. This requires a new library to be uploaded and included into the IDE, and this will be described further in this post. Inside this library, the author has provided a new "sd" class known as "SdFatSdioEX" - the class that is used in this example.
The SD Card socket on the Teensy 3.6 (and 3.5) is intended to work with a newer and faster "SDIO mode" of data transfer, rather than with the older/slower "SPI mode". It employs four parallel data lines, whereas the SPI mode used only one. It does NOT use any of the SPI interfaces shown on the pinout card, but relies on a direct SD I/O interface built into the K66 chip and directly wired to the socket. The T3.6 interface operates a little faster than the T3.5, but both are pretty rapid reaching speeds of the order of 20 MBytes/sec.
Below is a minimal code example for writing/reading data to/from a new file on an SD card. Ensure you have an SD card inserted before downloading the program. Open the "Serial Monitor" once loaded.
(Edit2 - see also post #5 for more features.)
Code:
//SD Card - Write/read/delete testing (v1.01)
//===========================================
//Date 08 OCT 2017
#include <SdFat.h>
#define USE_SDIO 1
const uint8_t SD_CHIP_SELECT = SS;
SdFatSdioEX sd;
SdFile myFile;
char fileName[] = "NoviceTest01.txt";
char KeyCode;
int FileData;
void setup()
{
Serial.begin(9600);
while(!Serial);
Serial.println("Enter 'w' key to write file data.");
Serial.println("Enter 'r' key to read file content.");
Serial.println("Enter 'd' key to delete file.");
}
void loop()
{
if (Serial.available()>0) {
KeyCode = Serial.read();
sd.begin();
if (KeyCode=='w') {
if (!myFile.open(fileName, O_RDWR | O_CREAT | O_AT_END)) {
Serial.println("Opening text file for write failed.");
}
else {
myFile.println("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
myFile.close();
Serial.println("...File written.");
}
}
if (KeyCode=='r') {
if (!myFile.open(fileName, O_READ)) {
Serial.println("Opening text file for read failed.");
}
else {
while ((FileData = myFile.read()) >= 0) Serial.write(FileData);
myFile.close();
}
}
if (KeyCode=='d') {
if (!sd.remove(fileName)) {
Serial.println("File delete failed.");
}
else {
Serial.println("...File deleted.");
}
}
}
}
To install the "SdFat" Library...
1) In the IDE, select "Sketch" from the main menu.
2) Select "Include Library" and then "Manage Libraries...".
3) In the filter textbox (top right) of the Library Manager, type "SdFat".
4) Click on the entry for "SdFat" in the list box, Then click "Install".
5) Close the Library Manager once installed.
Edit1 - Removed the DISABLE_CHIP_SELECT line. Removed "sd.errorHalt" lines - stops sd working (replaced with Serial message). Included file "Delete".
Moved "sd.begin()" into main loop - allows card to be removed and re-inserted.
Last edited: