SdFat library (v1.0.7) with Teensy 3.6 builtin uSD

Status
Not open for further replies.

XFer

Well-known member
Hello,
I'm trying the "RAWRITE" example from SdFat library (latest v1.0.7) with Teensy 3.6 (builtin card reader, 4 GB card preformatted with "SD Formatter" official utility).
https://github.com/greiman/SdFat/blob/master/examples/RawWrite/RawWrite.ino

I'm using Arduino IDE 1.8.5 and Teensyduino 1.44.

Since the example "as is" does not build with T3.6 (no SS pin for built-in card, and "BUILTIN_CARD" not defined), I've tried to adapt it to the SDIO objects which suit the T3.6.

So my source code is:

Code:
#include <SdFat.h>

#define SERIAL_SPEED 115200

// number of blocks in the contiguous file
const uint32_t BLOCK_COUNT = 10000UL;	// 5 MB

// time to produce a block of data
const uint32_t MICROS_PER_BLOCK = 10000UL;	// 10ms = 100 512byte_blocks/s = 51.2 KB/s

// file system
SdFatSdioEX sd;

// test file
SdFile file;

// file extent
uint32_t bgnBlock, endBlock;

// Serial output stream
ArduinoOutStream cout(Serial);
//------------------------------------------------------------------------------
// store error strings in flash to save RAM
#define error(s) sd.errorHalt(F(s))
//------------------------------------------------------------------------------
// log of first overruns
#define OVER_DIM 20
struct {
  uint32_t block;
  uint32_t micros;
} over[OVER_DIM];
//------------------------------------------------------------------------------


//////////
//
// SETUP
//
//////////


void setup(void) {
  Serial.begin(SERIAL_SPEED);
  while (!Serial) {}  // wait for Leonardo
}
//------------------------------------------------------------------------------


//////////
//
// LOOP
//
//////////


void loop(void) {
  while (Serial.read() >= 0) {}
  // pstr stores strings in flash to save RAM
  cout << F("Type any character to start\n");
  while (Serial.read() <= 0) {}
  delay(400);  // catch Due reset problem

  //cout << F("Free RAM: ") << FreeRam() << endl;

  if (!sd.begin()) 
  {
	  sd.initErrorHalt("SdFat begin() failed");
  }
  // make sdEx the current volume.
  sd.chvol();

  // delete possible existing file
  sd.remove("RawWrite.txt");

  // create a contiguous file
  if (!file.createContiguous(sd.vwd(), "RawWrite.txt", 512UL*BLOCK_COUNT)) {
    error("createContiguous failed");
  }
  // get the location of the file's blocks
  if (!file.contiguousRange(&bgnBlock, &endBlock)) {
    error("contiguousRange failed");
  }
  //*********************NOTE**************************************
  // NO SdFile calls are allowed while cache is used for raw writes
  //***************************************************************

  // clear the cache and use it as a 512 byte buffer
  uint8_t* pCache = (uint8_t*)sd.vol()->cacheClear();

  // fill cache with eight lines of 64 bytes each
  memset(pCache, ' ', 512);
  for (uint16_t i = 0; i < 512; i += 64) {
    // put line number at end of line then CR/LF
    pCache[i + 61] = '0' + (i/64);
    pCache[i + 62] = '\r';
    pCache[i + 63] = '\n';
  }

  cout << F("Start raw write of ") << file.fileSize() << F(" bytes at\n");
  cout << 512000000UL/MICROS_PER_BLOCK << F(" bytes per second\n");
  cout << F("Please wait ") << (BLOCK_COUNT*MICROS_PER_BLOCK)/1000000UL;
  cout << F(" seconds\n");

  // tell card to setup for multiple block write with pre-erase
  if (!sd.card()->writeStart(bgnBlock, BLOCK_COUNT)) {
    error("writeStart failed");
  }
  // init stats
  uint16_t overruns = 0;
  uint32_t maxWriteTime = 0;
  uint32_t t = micros();
  uint32_t tNext = t;

  // write data
  for (uint32_t b = 0; b < BLOCK_COUNT; b++) {
    // write must be done by this time
    tNext += MICROS_PER_BLOCK;

    // put block number at start of first line in block
    uint32_t n = b;
    for (int8_t d = 5; d >= 0; d--) {
      pCache[d] = n || d == 5 ? n % 10 + '0' : ' ';
      n /= 10;
    }
    // write a 512 byte block
    uint32_t tw = micros();
    if (!sd.card()->writeData(pCache)) {
      error("writeData failed");
    }
    tw = micros() - tw;

    // check for max write time
    if (tw > maxWriteTime) {
      maxWriteTime = tw;
    }
    // check for overrun
    if (micros() > tNext) {
      if (overruns < OVER_DIM) {
        over[overruns].block = b;
        over[overruns].micros = tw;
      }
      overruns++;
      // advance time to reflect overrun
      tNext = micros();
    } else {
      // wait for time to write next block
      while(micros() < tNext);
    }
  }
  // total write time
  t = micros() - t;

  // end multiple block write mode
  if (!sd.card()->writeStop()) {
    error("writeStop failed");
  }

  cout << F("Done\n");
  cout << F("Elapsed time: ") << setprecision(3)<< 1.e-6*t;
  cout << F(" seconds\n");
  cout << F("Max write time: ") << maxWriteTime << F(" micros\n");
  cout << F("Overruns: ") << overruns << endl;
  if (overruns) {
    uint8_t n = overruns > OVER_DIM ? OVER_DIM : overruns;
    cout << F("fileBlock,micros") << endl;
    for (uint8_t i = 0; i < n; i++) {
      cout << over[i].block << ',' << over[i].micros << endl;
    }
  }
  // close file for next pass of loop
  file.close();
  Serial.println();
}

This builds and starts, but after a while I get an error:


Type any character to start

Start raw write of 5120000 bytes at
51200 bytes per second
Please wait 100 seconds
error: writeStop failed
SD errorCode: 0X28,0X10001


The compiler seems to use the right libraries:

Code:
[...]
"D:\Arduino\hardware\teensy/../tools/teensy_post_compile" "-file=My_T35_RAWWRITE.ino" "-path=c:\temp\arduino_build_342837" "-tools=D:\Arduino\hardware\teensy/../tools/" "-board=TEENSY36"
Using library SdFat at version 1.0.7 in folder: D:\Arduino\hardware\teensy\avr\libraries\SdFat 
Using library SPI at version 1.0 in folder: D:\Arduino\hardware\teensy\avr\libraries\SPI 
Sketch uses 42476 bytes (4%) of program storage space. Maximum is 1048576 bytes.
Global variables use 5220 bytes (1%) of dynamic memory, leaving 256924 bytes for local variables. Maximum is 262144 bytes.

Any advice? :(

Thanks
 
Status
Not open for further replies.
Back
Top