Skip to content

Commit

Permalink
Teensy 3.5/3.6 SDIO
Browse files Browse the repository at this point in the history
  • Loading branch information
greiman committed Sep 5, 2016
1 parent 405edfe commit 0a1f307
Show file tree
Hide file tree
Showing 328 changed files with 5,847 additions and 5,863 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
### Warning: This version has major changes so it may be unstable.

Teensy 3.5/3.6 SDIO support has been added. Try the TeensySdioDemo example.
Many other example will work with Teensy SDIO if you use the SdFatSdio classes
and call begin with no parameters.

```
SdFatSdio sd;
....
if (!sd.begin()) {
// Handle failure.
}
```

Recent versions of the Arduino IDE have bugs that may cause SdFat-beta to crash.

https://forum.arduino.cc/index.php?topic=419264.0
Expand Down Expand Up @@ -44,4 +59,4 @@ SdFile, File, StdioStream, ifstream, ofstream, and others.

Please continue by reading the html documentation.

Updated 19 Aug 2016
Updated 5 Sep 2016
2 changes: 1 addition & 1 deletion SdFat/examples/QuickStart/QuickStart.ino
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Quick hardware test.
// Quick hardware test for SPI card access.
//
#include <SPI.h>
#include "SdFat.h"
Expand Down
43 changes: 34 additions & 9 deletions SdFat/examples/SdFormatter/SdFormatter.ino
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@
* For smaller cards this program uses FAT16
* and SDFormatter uses FAT12.
*/
// Print extra info for debug if DEBUG_PRINT is nonzero
#define DEBUG_PRINT 0
#include <SPI.h>
#include "SdFat.h"
#if DEBUG_PRINT
#include "FreeStack.h"
#endif // DEBUG_PRINT

// Set USE_SDIO to zero for SPI card access.
#define USE_SDIO 0
//
// Change the value of chipSelect if your hardware does
// not use the default value, SS. Common values are:
Expand All @@ -30,12 +26,25 @@ const uint8_t chipSelect = SS;
// Reduce max speed if errors occur.
#define SPI_SPEED SD_SCK_MHZ(50)

// Print extra info for debug if DEBUG_PRINT is nonzero
#define DEBUG_PRINT 0
#include <SPI.h>
#include "SdFat.h"
#if DEBUG_PRINT
#include "FreeStack.h"
#endif // DEBUG_PRINT

// Serial output stream
ArduinoOutStream cout(Serial);

#if USE_SDIO
SdioCard card;
#else // USE_SDIO
Sd2Card card;
#endif // USE_SDIO

uint32_t cardSizeBlocks;
uint16_t cardCapacityMB;
uint32_t cardCapacityMB;

// cache for SD block
cache_t cache;
Expand Down Expand Up @@ -155,6 +164,16 @@ void clearCache(uint8_t addSig) {
// zero FAT and root dir area on SD
void clearFatDir(uint32_t bgn, uint32_t count) {
clearCache(false);
#if USE_SDIO
for (uint32_t i = 0; i < count; i++) {
if (!card.writeBlock(bgn + i, cache.data)) {
sdError("Clear FAT/DIR writeBlock failed");
}
if ((i & 0XFF) == 0) {
cout << '.';
}
}
#else // USE_SDIO
if (!card.writeStart(bgn, count)) {
sdError("Clear FAT/DIR writeStart failed");
}
Expand All @@ -169,6 +188,7 @@ void clearFatDir(uint32_t bgn, uint32_t count) {
if (!card.writeStop()) {
sdError("Clear FAT/DIR writeStop failed");
}
#endif // USE_SDIO
cout << endl;
}
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -496,14 +516,19 @@ void setup() {
cout << F("Quiting, invalid option entered.") << endl;
return;
}

#if USE_SDIO
if (!card.begin()) {
sdError("card.begin failed");
}
#else // USE_SDIO
if (!card.begin(chipSelect, SPI_SPEED)) {
cout << F(
"\nSD initialization failure!\n"
"Is the SD card inserted correctly?\n"
"Is chip select correct at the top of this program?\n");
sdError("card.begin failed");
}
#endif
cardSizeBlocks = card.cardSize();
if (cardSizeBlocks == 0) {
sdError("cardSize");
Expand Down
15 changes: 15 additions & 0 deletions SdFat/examples/SdInfo/SdInfo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
*/
#include <SPI.h>
#include "SdFat.h"

// Set USE_SDIO to zero for SPI card access.
#define USE_SDIO 0
/*
* SD chip select pin. Common values are:
*
Expand All @@ -18,7 +21,12 @@ const uint8_t SD_CHIP_SELECT = SS;
* to 10 to disable the Ethernet controller.
*/
const int8_t DISABLE_CHIP_SELECT = -1;

#if USE_SDIO
SdFatSdio sd;
#else // USE_SDIO
SdFat sd;
#endif // USE_SDIO

// serial output steam
ArduinoOutStream cout(Serial);
Expand Down Expand Up @@ -170,12 +178,19 @@ void loop() {
}

uint32_t t = millis();
#if USE_SDIO
if (!sd.cardBegin()) {
sdErrorMsg("\ncardBegin failed");
return;
}
#else // USE_SDIO
// Initialize at the highest speed supported by the board that is
// not over 50 MHz. Try a lower speed if SPI errors occur.
if (!sd.cardBegin(SD_CHIP_SELECT, SD_SCK_MHZ(50))) {
sdErrorMsg("\ncardBegin failed");
return;
}
#endif // USE_SDIO
t = millis() - t;

cardSize = sd.card()->cardSize();
Expand Down
110 changes: 110 additions & 0 deletions SdFat/examples/TeensySdioDemo/TeensySdioDemo.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Simple performance test for Teensy 3.5/3.6 SDHC.
// Demonstrates yield() efficiency.

#include "SdFat.h"

// 32 KiB buffer.
const size_t BUF_DIM = 32768;

// 8 MiB file.
const uint32_t FILE_SIZE = 256UL*BUF_DIM;

SdFatSdio sd;

File file;

uint8_t buf[BUF_DIM];

// buffer as uint32_t
uint32_t* buf32 = (uint32_t*)buf;

// Total usec in read/write calls.
uint32_t totalMicros = 0;
// Time in yield() function.
uint32_t yieldMicros = 0;
// Number of yield calls.
uint32_t yieldCalls = 0;
// Max busy time for single yield call.
uint32_t yieldMaxUsec = 0;
//-----------------------------------------------------------------------------
// Replace "weak" system yield() function.
void yield() {
// Only count cardBusy time.
if (!sd.card()->dmaBusy()) {
return;
}
uint32_t m = micros();
yieldCalls++;
while (sd.card()->dmaBusy()) {
// Do something here.
}
m = micros() - m;
if (m > yieldMaxUsec) {
yieldMaxUsec = m;
}
yieldMicros += m;
}
//-----------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
while (!Serial) {
}
Serial.println("Type any character to begin");
while (!Serial.available()) {
}
if (!sd.begin()) {
sd.initErrorHalt();
}
if (!file.open("TeensyDemo.bin", O_RDWR | O_CREAT)) {
sd.errorHalt("open failed");
}
Serial.println("\nsize,write,read");
Serial.println("bytes,KB/sec,KB/sec");
for (size_t nb = 512; nb <= BUF_DIM; nb *= 2) {
file.truncate(0);
uint32_t nRdWr = FILE_SIZE/nb;
Serial.print(nb);
Serial.print(',');
uint32_t t = micros();
for (uint32_t n = 0; n < nRdWr; n++) {
// Set start and end of buffer.
buf32[0] = n;
buf32[nb/4 - 1] = n;
if (nb != file.write(buf, nb)) {
sd.errorHalt("write failed");
}
}
t = micros() - t;
totalMicros += t;
Serial.print(1000.0*FILE_SIZE/t);
Serial.print(',');
file.rewind();
t = micros();

for (uint32_t n = 0; n < nRdWr; n++) {
if ((int)nb != file.read(buf, nb)) {
sd.errorHalt("read failed");
}
// crude check of data.
if (buf32[0] != n || buf32[nb/4 - 1] != n) {
sd.errorHalt("data check");
}
}
t = micros() - t;
totalMicros += t;
Serial.println(1000.0*FILE_SIZE/t);
}
file.close();
Serial.print("\ntotalMicros ");
Serial.println(totalMicros);
Serial.print("yieldMicros ");
Serial.println(yieldMicros);
Serial.print("yieldCalls ");
Serial.println(yieldCalls);
Serial.print("yieldMaxUsec ");
Serial.println(yieldMaxUsec);
Serial.println("Done");
}

void loop() {
}
14 changes: 13 additions & 1 deletion SdFat/examples/bench/bench.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include "SdFat.h"
#include "FreeStack.h"

// Set USE_SDIO to zero for SPI card access.
#define USE_SDIO 0

// SD chip select pin
const uint8_t chipSelect = SS;

Expand All @@ -28,7 +31,11 @@ const uint32_t FILE_SIZE = 1000000UL*FILE_SIZE_MB;
uint8_t buf[BUF_SIZE];

// file system
#if USE_SDIO
SdFatSdio sd;
#else // USE_SDIO
SdFat sd;
#endif // USE_SDIO

// Set ENABLE_EXTENDED_TRANSFER_CLASS to use extended SD I/O.
// Requires dedicated use of the SPI bus.
Expand Down Expand Up @@ -102,12 +109,17 @@ void loop() {

cout << F("FreeStack: ") << FreeStack() << endl;

#if USE_SDIO
if (!sd.begin()) {
sd.initErrorHalt();
}
#else // USE_SDIO
// Initialize at the highest speed supported by the board that is
// not over 50 MHz. Try a lower speed if SPI errors occur.
if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
sd.initErrorHalt();
}

#endif // USE_SDIO
cout << F("Type is FAT") << int(sd.vol()->fatType()) << endl;
cout << F("Card size: ") << sd.card()->cardSize()*512E-9;
cout << F(" GB (GB = 1E9 bytes)") << endl;
Expand Down
Loading

0 comments on commit 0a1f307

Please sign in to comment.