1

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 SDcard'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
2
  • I tested your code almost verbatim using an SDCard shield that was designed to work with the UNO and so I used an UNO. The only change to the code was to SD_CS_PIN. This was using SdFat 2.0.4. I have run it for at least 20 iterations reporting success on both open and write. I have not investigated further yet. Thought I'd just contribute that information for now anyway. It seems unlikely that being a ATMega1284P will play into this, but perhaps. If I happen to find one I may try it later.
    – timemage
    Jan 8, 2021 at 17:09
  • Regarding your SdError: 0X1D,0XFF, this is apparently a read timeout. Not sure what to do with that information though. Maybe seeing how you've wired it would help.
    – timemage
    Jan 8, 2021 at 18:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.