I may be interested in seeing the code you are using
@WMXZ: Sorry for a wrong impression - by "another way" I had meant to say "another way to Bill Greiman's SDFat". This is, of course, your very own "https://github.com/WMXZ-EU/uSDFS". The link I referenced in my post leads to the help given from Manitou which pointed me at your zip file.
However, perhaps it is still worth posting a code example for Noobs (like myself) to see just how EASY this is.
I claim no originality
... All I have done is to tease out the steps involved manually via the SERIAL MONITOR (taken from your own zip file example) so that I could (a) open or close a file for writing, (b) write any byte of my choosing, (c) vary the number of bytes written.
The example has no error handling, so anyone following it must take care to enter what the code is expecting from the SERIAL MONITOR (and in the correct sequence). Always close a file when done with writing. Format the SD card first on a PC as normal.
---
First, the uSDFS library needs to be "Included" in the Arduino IDE - as per WMXZ's instructions in the downloaded zip file.
Instructions for this example use:
========================
1) IMPORTANT - Ensure that the formatted microSD card is inserted into the Teensy 3.6 microSD socket first. Too easy to forget to do it
!!
Once the example code has compiled and downloaded, open the SERIAL MONITOR to use the following commands.
2) The file opened will always have the suffix ".DAT". You can name your file by typing "f" followed by the prefix name, e.g. "fTest001" gives file name = Test001.DAT.
3) Default data byte is character "A". You can change the data written to be any single ascii character using the "z" command, e.g. "zB" gives data byte = 0x42 (="B").
4) Default number of bytes to be written is "0080" (eighty decimal). You can change the number of bytes using the "d" command, e.g. "d0100" = one hundred decimal.
(always pad the number out with leading zeroes to be between "0000" and "9999").
Now you can write data to a file...
5) Type the command "o" to open the file that you named. Send.
6) Type the command "s" to store the data in the file. Send.
7) Type the command "c" to close the data in the file. Send.
When you first download the example code to the Teensy T3.6, nothing happens until the SERIAL MONITOR window is opened. At that point, you should see four blinks of the Teensy LED. This gives reassurance that the setup routine has executed and the main loop is running. You will also see four blinks if the reset button is pushed with the SERIAL MONITOR window open.
At each step 5), 6) or 7) you should see the LED blink once to indicate it has received the command. The time taken for writing the data is given in microSecs.
To inspect your file afterwards, use a hex editor (such as
http://www.hexedit.com/) to open it for reading and you can then see exactly what your file contains.
My thanks to WMXZ for his excellent work. You can read more on this topic at
http://elm-chan.org/fsw/ff/00index_e.html.
Code:
//TBill SD Data Logger 02 SEP 2016
//================================
//Version 1.00
//Data Logging from T3.6 to SD Card
//Thanks to WMXZ for uSDFS zip file (https://github.com/WMXZ-EU/uSDFS)
//-------------------------------------------------------------------
//Includes and Declarations
//-------------------------------------------------------------------
#include <stdint.h>
#include <ff.h>
#define LED_ON GPIOC_PSOR=(1<<5)
#define LED_OFF GPIOC_PCOR=(1<<5)
#define BUFFSIZE (16*4096)
uint8_t buffer[BUFFSIZE] __attribute__((aligned(16))); //8 bit data buffer = size of log file
UINT wr;
FRESULT rc; //Result code
FATFS fatfs; //File system object
FIL fil; //File object
char filename[80], FileDataChar;
int Byte1, Byte2, Byte3, Byte4, Byte5, NBytes1, NBytes2, DBytes=80, LoopCount;
unsigned long time1;
//-------------------------------------------------------------------
//Setup and Loop
//-------------------------------------------------------------------
//User setup routine
void setup() {
//initilise variables
PORTC_PCR5 = PORT_PCR_MUX(0x1); //LED PC5 pin 13, config GPIO alt = 1
GPIOC_PDDR = (1<<5); //make this an output pin
LED_OFF; //start with LED off
FileDataChar = 'A'; //default data character
Serial.begin(115200);
while (!Serial);
sprintf(filename,"AAAAAA.DAT"); //default filename
Serial.print("Filename = ");Serial.println(filename);
Serial.println("f=filename, o=open, s=store, c=close");
Serial.println("dXXXX=Dbytes, zX=data");
f_mount(&fatfs, (TCHAR *)L"/", 0); //mount a logical drive
//blink 4 times
Blink();
delay(400);
Blink();
delay(400);
Blink();
delay(400);
Blink();
delay(400);
} //end of User setup routine
//User loop routine
void loop() {
//read key input (if any)
KeyInput(); //read key input (if any)
LoopCount++; //keep tally of loop counts
} //end of User loop routine
//-------------------------------------------------------------------
//Subroutines
//-------------------------------------------------------------------
//Blink Routine
void Blink() {
//blink the LED
LED_ON;
delay(10);
LED_OFF;
delay(10);
} //end of Blink routine
//KeyInput Routine
void KeyInput() {
//blink the LED
if (Serial.available() > 0) {
//read the incoming byte
Byte1 = Serial.read();
if (Byte1>0x60) {
switch (Byte1) {
case 0x6F: //o - Open File
// OPEN new file
//==============
rc = f_open(&fil, filename, FA_WRITE | FA_CREATE_ALWAYS);
if (rc) {
Serial.print(filename); Serial.println(" was NOT opened"); Serial.flush();
break;
}
Serial.print(filename); Serial.println(" was opened"); Serial.flush();
Blink();
break;
case 0x73: //s - Store Data from buffer in file on SD card
// STORE data in file
//===================
for(int ii=0;ii<BUFFSIZE;ii++) {
buffer[ii]=FileDataChar;
}
//write data to file
time1 = micros();
Serial.print("Start Time = ");
Serial.println(time1);
rc = f_write(&fil, buffer, DBytes, &wr);
time1 = micros();
Serial.print("End Time = ");
Serial.println(time1);
if (rc) {
Serial.println("Data was NOT written"); Serial.flush();
break;
}
Serial.println("Data was written"); Serial.flush();
Blink();
break;
case 0x63: //c - Close File
//CLOSE file
//==========
rc = f_close(&fil);
if (rc) {
Serial.print(filename); Serial.println(" was NOT closed"); Serial.flush();
break;
}
Serial.print(filename); Serial.println(" was closed"); Serial.flush();
Blink();
break;
case 0x66: //f - Filename
//CHANGE FILE name
//================
if (Serial.available() < 3) {
Serial.print("Filename not specified - existing = "); Serial.println(filename); Serial.flush();
break;
}
Byte2 = Serial.available();
NBytes1 = 0; //total bytes available
NBytes2 = 0; //total less illegal characters
while (NBytes1 < Byte2) {
Byte3 = Serial.read();
if (Byte3 > 0x2F) { //test for illegal characters
filename[NBytes2] = Byte3;
NBytes2++;
}
NBytes1++;
}
filename[NBytes2] = '.'; NBytes2++;
filename[NBytes2] = 'D'; NBytes2++;
filename[NBytes2] = 'A'; NBytes2++;
filename[NBytes2] = 'T'; NBytes2++;
filename[NBytes2] = 0;
Serial.print("Filename is now "); Serial.println(filename); Serial.flush();
Blink();
break;
case 0x7A: //z - FileDataChar value
//CHANGE FILE DATA character
//==========================
if (Serial.available() > 2) {
Byte2 = Serial.read();
FileDataChar = Byte2;
}
Serial.print("Data = ");Serial.println(FileDataChar); Serial.flush();
Blink();
break;
case 0x64: //d - Number of Data Bytes
//change Number of Data Bytes
if (Serial.available() > 3) {
Byte2 = Serial.read();
Byte3 = Serial.read();
Byte4 = Serial.read();
Byte5 = Serial.read();
DBytes = ((Byte2-48)*1000) + ((Byte3-48)*100) + ((Byte4-48)*10)+(Byte5-48);
}
Serial.print("Num of Data Bytes = ");Serial.println(DBytes); Serial.flush();
Blink();
break;
case 0xFF: //h
//task goes here...
Blink();
break;
} //end of switch statement
} //end of Byte1 > 0x60
} //end of if Serial Available
} //end of KeyInput routine