Hello,
Is it possible that the SD I/O libraries for the Sakura have a bug for writing right now? It seems that we only get files filled with wrong characters and even corrupt files at the moment.
The codes for writing in our specific case have not been changed, but the output is changed dramatically. (as of 9-1-2018 for as far as we know)
-美月
Hello JB,
In my last message I told that I tried the demo program and that the data in it cant be read back on a PC (which is a requirement, by the way). I have since tried some more things, and at the moment I can write a file ..sometimes. When I try opening a file, it will not always do it. Generally, from the 2nd attempt on it will succesfully open a file for writing. Once, it worked the first time, but it produced a file of nearly 4GB, that was 0B when I tried to delete it.
I will post my piece of code below. Note that I changed variable names.
if (SD.exists("filename.txt") && !xOnlyDeleteOnce){ SD.remove("filename.txt"); xOnlyDeleteOnce = true; } fileVar = SD.open("filename.txt", FILE_WRITE); if (fileVar) {fileVar.print(intVar1); delay(5);fileVar.print(","); delay(5);fileVar.print(intVar2); delay(5);fileVar.print(","); delay(5);fileVar.print(intVar3); delay(5);fileVar.print(","); delay(5);fileVar.print(intVar4); delay(5);fileVar.print(","); delay(5);fileVar.print(intVar5); delay(5);fileVar.print(","); delay(5);fileVar.print(intVar6); delay(5);fileVar.print(","); delay(5);fileVar.print(floatVar1, 2); // print two decimal places delay(5);fileVar.print(","); delay(5);fileVar.print(floatVar2, 2); // print two decimal places delay(5);fileVar.println(","); // Ends line and inserts cr/lf delay(5); delay(300); //@TODO test this -> didn't workfileVar.close(); Serial.println("Done writing!"); } else { errorFlag = true; errorCode = 11; } // This is followed by if (!fileVar) { //code to return to main scenario }
if (SD.exists("filename.txt") && !xOnlyDeleteOnce){ SD.remove("filename.txt"); xOnlyDeleteOnce = true; }
fileVar = SD.open("filename.txt", FILE_WRITE);
if (fileVar) {fileVar.print(intVar1); delay(5);fileVar.print(","); delay(5);fileVar.print(intVar2); delay(5);fileVar.print(","); delay(5);fileVar.print(intVar3); delay(5);fileVar.print(","); delay(5);fileVar.print(intVar4); delay(5);fileVar.print(","); delay(5);fileVar.print(intVar5); delay(5);fileVar.print(","); delay(5);fileVar.print(intVar6); delay(5);fileVar.print(","); delay(5);fileVar.print(floatVar1, 2); // print two decimal places delay(5);fileVar.print(","); delay(5);fileVar.print(floatVar2, 2); // print two decimal places delay(5);fileVar.println(","); // Ends line and inserts cr/lf delay(5); delay(300); //@TODO test this -> didn't workfileVar.close(); Serial.println("Done writing!"); } else { errorFlag = true; errorCode = 11; }
// This is followed by
if (!fileVar) { //code to return to main scenario }
I hope that you, or anyone else, can help me fix this problem
Ok. There is no description in libraries. The version is only described template name or default sketch as below.
If you could, try the latest version V2.20 downloadable at GR-SAKURA web. http://gadget.renesas.com/en/product/sakura.html
Note that the projects can be used with e2studio V5.4, not for V6.x.
I've tried the below code on my e2studio 5.4 (GNURX 14.03).
/* GR-SAKURA Sketch Template V2.20 */#include <Arduino.h>#include <SD.h>static int intVar1, intVar2, intVar3, intVar4, intVar5, intVar6;static float floatVar1, floatVar2;static bool errorFlag = false;static bool xOnlyDeleteOnce = false;static uint8_t errorCode = 0;void setup() { Serial.begin(9600); if (!SD.begin()) { Serial.println("Card failed, or not present."); while (1) ; }}void loop() { Serial.println("Any key to start writing."); while (!Serial.available()) ; // wait to press key Serial.read(); //dummy if (SD.exists("filename.txt") && !xOnlyDeleteOnce) { SD.remove("filename.txt"); xOnlyDeleteOnce = true; } File fileVar = SD.open("filename.txt", FILE_WRITE); if (fileVar) { fileVar.print(intVar1); delay(5); fileVar.print(","); delay(5); fileVar.print(intVar2); delay(5); fileVar.print(","); delay(5); fileVar.print(intVar3); delay(5); fileVar.print(","); delay(5); fileVar.print(intVar4); delay(5); fileVar.print(","); delay(5); fileVar.print(intVar5); delay(5); fileVar.print(","); delay(5); fileVar.print(intVar6); delay(5); fileVar.print(","); delay(5); fileVar.print(floatVar1, 2); // print two decimal places delay(5); fileVar.print(","); delay(5); fileVar.print(floatVar2, 2); // print two decimal places delay(5); fileVar.println(","); // Ends line and inserts cr/lf delay(5); delay(300); //@TODO test this -> didn't work fileVar.close(); Serial.println("Done writing!"); } else { errorFlag = true; errorCode = 11; }}
The below image show that "FILENAME.TXT" was written, and show the contents in the file. It works fine.
Next, I modified the program as below, "delay" was removed.
/* GR-SAKURA Sketch Template V2.20 */#include <Arduino.h>#include <SD.h>static int intVar1, intVar2, intVar3, intVar4, intVar5, intVar6;static float floatVar1, floatVar2;static bool errorFlag = false;static bool xOnlyDeleteOnce = false;static uint8_t errorCode = 0;void setup() { Serial.begin(9600); if (!SD.begin()) { Serial.println("Card failed, or not present."); while (1) ; }}void loop() { Serial.println("Any key to start writing."); while (!Serial.available()) ; // wait to press key Serial.read(); //dummy if (SD.exists("NODELAY.txt") && !xOnlyDeleteOnce) { SD.remove("NODELAY.txt"); xOnlyDeleteOnce = true; } File fileVar = SD.open("NODELAY.txt", FILE_WRITE); if (fileVar) { fileVar.print(intVar1); fileVar.print(","); fileVar.print(intVar2); fileVar.print(","); fileVar.print(intVar3); fileVar.print(","); fileVar.print(intVar4); fileVar.print(","); fileVar.print(intVar5); fileVar.print(","); fileVar.print(intVar6); fileVar.print(","); fileVar.print(floatVar1, 2); // print two decimal places fileVar.print(","); fileVar.print(floatVar2, 2); // print two decimal places fileVar.println(","); // Ends line and inserts cr/lf fileVar.close(); Serial.println("Done writing!"); } else { errorFlag = true; errorCode = 11; }}
The below image shows that "NODELAY.TXT" was written, and show the contents in the file. It works fine.
Could you tell me the version of compiler ? I used GNURX v14.03, that is little bit old.
Anyway, __RX600__ is activated by -mcpu=rx600. We had integrated the library for GR-SAKURA and GR-CITRUS(RX631) so we use __RX600__ macro.
Dear Okamiya Yuuki-san,
The version of GNURX I use is 14.03, same as you.
Also, when I run the SD writing in its own program, all runs fine. It is only when I remove delays in the entire application that it starts doing strange things. I suspect the program is still busy writing registers or doing something else while the SD card is first being accessed. If I remove the delay after file.close();, random files that I have not accessed seem to disappear, almost as if the FAT table is not yet written properly.
I also experimented with delays and everything in other places. Right now, I only have a delay before opening a file, then one when I split up long strings (else the string gets cut off), and after closing the file:
delay(200); if (!SD.exists(fileName)) { // write a little header delay(200); file = SD.open(fileName, FILE_WRITE);// Below strings are kept at approximately equal length as real code file.print("day-month-year hrs:minutes\tfirstInt pos1\tsecondInt pos1"); delay(100); // 100 ms delay to write its buffer. file.print("\tthirdInt pos2\tfourthInt pos2\tfifthInt"); delay(100); // another 100ms to clear buffer file.println("\tnumeralsInt\tfinalInt"); file.close(); } delay(200); // 200ms delay getTime(); // RTCC function, retrieve internal RTCC time and cast to separate variables delay(100); file = SD.open(fileName, FILE_WRITE); // Write data if (file) { /* block for date and time, then * a space, a dash and then the * actual logging items. */ file.print(day); file.print("-"); file.print(month); file.print("-"); file.print(year); file.print(" "); if (hours < 10) { file.print("0"); } file.print(hours); file.print(":"); if (minutes < 10) { file.print("0"); } file.print(minutes); file.print("\t\t\t"); file.print(internalVar1/divisionFactor1); file.print("\t\t\t"); file.print(internalVar2/divisionFactor1); file.print("\t\t\t"); file.print(internalVar3/divisionFactor2); file.print("\t\t\t"); file.print(internalVar4/divisionFactor2); file.print("\t\t\t"); file.print(fifthInt); file.print("\t\t"); if (time_minutes< 10) { file.print("0"); } file.print(time_minutes); file.print(":"); if (time_seconds< 10) { file.print("0"); } file.print(time_seconds); file.print("\t\t"); file.println(finalInt); // if reading back is made, the \t should be filtered out. } //file.flush(); // Should not be needed, flushes data to SD card file.close(); delay(200); // Should not be needed, waits 200 ms to close the file. If this isn't done, this (or other) file might get corrupt
If I isolate this in its own program and run it without delays, everything works fine. If I then put it back in the complete program, things get screwed up. Also, it seems that if I remove most of the tabs in the header writing bit, it can go on for longer without needing the delay to (presumably) write the buffer.
Hope you can clear up some of my confusions with these new details.
Yours,
Dear Okamiya Yuuki-san, I have since replaced "FILE_WRITE" with "O_WRITE | O_CREAT | O_APPEND", as per the creator of sd2fat's suggestions ( forum.arduino.cc/index.php ). After that, the wriets go a lot faster, and I think that this is what has solved most of my problems. Now, it only really messes up files when it loses power while writing. (presumably the FAT table was being written to.) The O_WRITE | O_CREAT | O_APPEND parameters allow for writing multiple bytes between flushing and, thus, remove a lot of overhead. The FILE_WRITE flushes after every byte. Still, I would like to hear your opinion on whether writing to registers for I/O could have caused problems as well.
As an extra, there is apparently a much newer version of sdfat, you can find it at https://github.com/greiman/SdFat . Is there any possibility it will be included in the Sakura libraries in the near future? Apparently features are much more advanced, but the complexity has also grown a bit.
Yours, -美月