sdFat and Serial conflict?

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

Perhaps you need to call sd.begin() again after turning the SD power off and on.

Thank you for the suggestion. I have tried that, and it does not work (sd.begin returns false, and the problem persists). From my experience so far, you don't have to do sd.begin() after power cycle, when using sdFatLib.

Not familiar with the processor. Any chance that the enable pin is shared with a serial pin?

 const uint8_t TLV_EN=1;
 pinMode(TLV_EN, OUTPUT);    // sets the digital pin 13 as output

Comment is confusing. Is TLV_EN on pin 1 or on pin13? Is this a custom board? Did you define pin mapping?

// Serial output stream

ArduinoOutStream cout(Serial);

Not sure what it does. get rid of this if you don’t use it.

Have you tried using default options instead of

#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;

J-M-L:
Comment is confusing. Is TLV_EN on pin 1 or on pin13? Is this a custom board? Did you define pin mapping?

Apologies for the comment, TLV_EN is on PIN 1. I am using a standalone ATmega1284P with MigthyCore, whith the pin mapping it provides (you can find it in the readme, under MightyCore DIP40 Standard pinout).

J-M-L:
Not sure what it does. get rid of this if you don’t use it.

I got rid of it, it was forgotten, did not fix anything though. It was used in the official examples of sdFat lib that I was testing.

J-M-L:
Have you tried using default options instead of

#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;

I just used the settings that seemed to match my case, by choosing from the settings recommended at the bench example of SdFatLib. I have seen that a recommended clock speed for SPI is one quarter of the MCU clock speed, my SD card is formatted in FAT32, and I intend to use other SPI devices as well. Still though, for the sake of testing, I copied the exact settings from the bench example (if those are considered the defaults), but the problem persists.

Do the default sketches work fine?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.