Hi,
I'm using a Teensy 4.1 board with its built in sd card drive. I'm using PlateformIO as the IDE. I'm having hard time to keep saved data after a power outage. To make a long story short, I'm constantly logging data to a file on the sd card. Periodically, I'm using file.flush() to be sure that at least last data written before the last file.flush() will be saved on the sd card. Unfortunately, it does not work. It looks like file.flush() do nothing at all. The only way to get it working is to open() and close() the file periodically instead of using flush().
After several unsuccessful search in forums, I decided to do a very small test program to try to isolate the problem and tried it with the Arduino IDE v1.8.19 instead of my PlateformIO IDE. For an unknown reason, it works just fine with the Arduino IDE. All data written before the last file.flush() are correctly saved on the sd card even after a power outage. I tried back this test program with PlateformIO without changing anything and it does not work with PlateformIO. Data are not saved to the file. I tried with sd cards exFat as well as Fat32 formats. See the test program below.
Code:
#include <SD.h>
File myFile;
void setup()
{
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect.
}
Serial.print("Initializing SD card...");
if (!SD.begin(BUILTIN_SDCARD)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
myFile = SD.open("test.txt", FILE_WRITE);
Serial.println(myFile.size());
if (myFile) {
Serial.print("Writing to test.txt...");
for (int i = 0; i < 4096; i++) {
myFile.print("X");
}
Serial.println(myFile.size());
myFile.flush();
//myFile.close();
Serial.println("Power off Teensy 4.1 board now to do the test.");
while (true);
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop()
{
// nothing happens after setup
}
So, did I miss something?
Thank you for your time.
G. Plante