Hello,
I am using the sdFat library to manage an SD card with an ATmega1284p @ 8MHz internal clock. I have come across a strange issue in my system, and I created the smallest sketch that shows the problem: What I do is, that i call sd.begin() in the setup(), and then in Loop, I open a file and write the word "test". After that, I close the Serial and I turn off the SD card's power (TLV_EN signal). After a while I re-enable the power and restart Serial. The second time I try to open the file to write "test", it fails. If I, however, comment out the Serial.end() and Serial.begin() commands, everything works as expected.
Here is the code:
#include <Arduino.h>
//Power Management Libraries
#include <avr/power.h>
#include <avr/wdt.h>
#include <avr/sleep.h>
//SD Card
#include "SdFat.h"
#include "sdios.h"
#include "FreeStack.h"
#define SD_FAT_TYPE 1
const uint8_t SD_CS_PIN = 3; //SPI CS pin of SD card
#define SPI_CLOCK SD_SCK_MHZ(2)
// Try to select the best SD card configuration.
#define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI, SPI_CLOCK)
SdFat32 sd;
File32 file;
const char fileName[]="COMPMEAS.TXT"; //8.3 supported filenames: https://en.wikipedia.org/wiki/8.3_filename
int count=1;
//PSU Enables
const uint8_t TLV_EN=1;
// Serial output stream
ArduinoOutStream cout(Serial);
//------------------------------------------------------------------------------
// Store error strings in flash to save RAM.
#define error(s) sd.errorHalt(&Serial, F(s))
//------------------------------------------------------------------------------
void setup() {
pinMode(TLV_EN, OUTPUT); // sets the digital pin 13 as output
digitalWrite(TLV_EN,HIGH); //TLV high enable
delay(100);
Serial.begin(38400,SERIAL_8N1);
Serial.println();
Serial.println();
Serial.println(F("Hello!"));
Serial.println();
//SD Setup
Serial.print(F("- Initializing SD card..."));
while (!sd.begin(SD_CONFIG)) {
Serial.println();
Serial.print(F("SD initialization failed. Retrying in 2 seconds..."));
delay(2000);
}
Serial.println(F("done."));
Serial.println(F("- Setup Complete."));
Serial.println(F("=================================================="));
Serial.println();
delay(5000);
}
void loop(){
Serial.print("Loop: ");
Serial.println(count);
count++;
if (!file.open(fileName, O_RDWR | O_CREAT | O_APPEND )) {
Serial.println(F("- SD open failed!"));
}
else{
Serial.println(F("- SD open success!!!"));
if(file.write("test", strlen("test"))!=strlen("test")){
Serial.println(F("- SD write failed!"));
error("write failed");
}
else{
file.flush();
Serial.println(F("- SD write success!!!"));
}
file.close();
}
delay(1000);
Serial.end();
delay(1000);
digitalWrite(TLV_EN,LOW); //TLV high enable
delay(1000);
digitalWrite(TLV_EN,HIGH); //TLV high enable
delay(1000);
Serial.begin(38400,SERIAL_8N1);
while(!Serial){
}
delay(1000);
Serial.println("----------");
delay(5000);
}
Here is the result on console:
Hello!
- Initializing SD card...done.
- Setup Complete.
==================================================
Loop: 1
- SD open success!!!
- SD write success!!!
----------
Loop: 2
- SD open success!!!
- SD write failed!
error: write failed
SdError: 0X1D,0XFF