error in the 375th call to the File::sync method

I’m running a test program on an Arduino Mega. I’m writing to an sd card through an sd card adapter from ali express. I’m using SdFat version 1.1.2. I’ve formatted the sd card with the SdFormat example program in SdFat.

The setup of the program

  • mounts the sd card (SdFat::begin),
  • list the contents of the root directory (SdFat::ls), and
  • opens an existing file for writing (File::open) and rewinds it (File::seekSet).
    The program’s loop writes a small string to the file (File::write) and then calls File::sync.

The 375th (I’ve also seen other numbers, but they’re all close to 375) call to the File::sync method fails. I tried with two different files, same result.

Below is the code and the output of the program. I would greatly appreciate any help you may provide. It would be interesting to know what other people get when they run the program.

Here’s the code. It’s simple, but a bit blown up because of all the error checking and logging.

#include <SdFat.h>

ArduinoOutStream cout (Serial);

bool good = false;
SdFat sdFat;
File * file = 0;

void mount ()
{
  cout << "mount" << endl;
  good = sdFat.begin (SS, SD_SCK_MHZ (1));

  if (not good) {
    sdFat.initErrorPrint ();
  }
}

void listDirectory ()
{
  if (good) {
    cout << "listDirectory" << endl;
    sdFat.ls (LS_SIZE);
  }
}

void openFile (char const * const fileName, oflag_t const openFlags)
{
  file = new File ();

  if (good) {
    cout << "trying to open file " << fileName << " ..." << endl;

    if (file->open (&sdFat, fileName, openFlags)) {
      cout << "... done" << endl;
    }
    else {
      cout << "... failed" << endl;
      delete file;
      file = 0;
      good = false;
    }
  }
  if (good) {
    if (not file->seekSet (0)) {
      cout << "error in rewind" << endl;
      good = false;
    }
  }
}

void writeAndSync (char const * const characters)
{
  if (good) {
    int const nWritten = file->write (characters);

    if (nWritten != strlen (characters)) {
      cout << strlen (characters) << " characters to be written, " << nWritten << " characters actually written" << endl;
    }
  }
  if (good) {
    if (not file->sync ()) {
      cout << "error in sync" << endl;
      good = false;
    }
  }
}

void setup ()
{
  Serial.begin (9600);

  while (not Serial) {
    SysCall::yield ();
  }
  cout << "Type any character to start" << endl;

  while (not Serial.available ()) {
    SysCall::yield ();
  }
  mount ();
  listDirectory ();
  openFile ("0003.TXT", FILE_WRITE);
}

int count = 0;

void loop ()
{
  if (good) {
    cout << "count " << count << endl;
    writeAndSync ("abc");
    ++count;
  }
}

And this is the output.

First time:

[11:49:28:445] mount
[11:49:28:855] RB0
[11:49:28:855] RB8192
[11:49:28:855] listDirectory
[11:49:28:855] RB16384
[11:49:28:888]       4000 0000.txt
[11:49:28:888]       4000 0001.txt
[11:49:28:921]       4000 0002.txt
[11:49:28:955]       4000 0003.txt
[11:49:28:955]          4 count.txt
[11:49:28:989] trying to open file 0003.TXT ...
[11:49:29:022] ... done
[11:49:29:022] count 0
[11:49:29:056] RB5477696
[11:49:29:056] WB5477696
[11:49:29:056] count 1
[11:49:29:089] WB5477696
[11:49:29:089] count 2
[11:49:29:089] WB5477696
[11:49:29:122] count 3
...
[11:49:37:565] count 373
[11:49:37:565] WB5477698
[11:49:37:599] count 374
[11:49:37:599] WB5477698
[11:49:37:599] count 375
[11:49:37:621] WB5477698
[11:49:37:892] 3
[11:49:37:892] .../sketch-book/libraries/SdFat/src/FatLib/FatVolume.cpp54
[11:49:37:959] .../sketch-book/libraries/SdFat/src/FatLib/FatFile.cpp1232
[11:49:38:059] error in sync

When I try again:

[11:52:44:366] mount
[11:52:44:552] Can't access SD card. Do not reformat.
[11:52:44:585] No card, wrong chip select pin, or SPI problem?
[11:52:44:619] SD errorCode: 0X20,0X0

I can no longer mount, I need to eject the sd card and re-insert.
Third time:

[11:52:52:750] mount
[11:52:53:251] RB0
[11:52:53:251] RB8192
[11:52:53:251] listDirectory
[11:52:53:251] RB16384
[11:52:53:284]       4000 0000.txt
[11:52:53:284]       4000 0001.txt
[11:52:53:318]       4000 0002.txt
[11:52:53:351]       4000 0003.txt
[11:52:53:351]          4 count.txt
[11:52:53:385] trying to open file 0003.TXT ...
[11:52:53:418] ... done
[11:52:53:418] count 0
[11:52:53:452] RB5477696
[11:52:53:452] WB5477696
[11:52:53:452] count 1
[11:52:53:475] WB5477696
[11:52:53:739] 3
[11:52:53:739] .../sketch-book/libraries/SdFat/src/FatLib/FatVolume.cpp54
[11:52:53:806] .../sketch-book/libraries/SdFat/src/FatLib/FatFile.cpp1232
[11:52:53:906] error in sync