SDfat library - log file cannot be opened after a restart

Possible issue only noticed recently because product that logs events to SD file when operated without WiFi connection (WiFi credentials deleted) was storing events successfully to file, however client likes to turn off and on before entering WiFi credentials (hotspot) and this appeared to show missing events. Reproducing the problem it appears that the log file cannot be opened and thus is presumed missing and reinitialised. The question is why is this file unreadable on the device when the SDfat has been successfully initialised and other (read-only) files can be read before this file? Any ideas welcomed.

Hi, what DeviceOS are you using?
I got in trouble with SDfat on 5.8.0, and the issue is being looked at by the Particle team.

There might be unsynchronized data in the SdFat cache. Calling the card() method looks like it will synchronize the blocks to the physical card, which might help after writing.

Device OS 2.3.1 on Photon1.

I have hit an issue with SDfat on 5.8.0 P2 as well for new product development - good to know you have this covered!

@rickkas7 There is a battery (coin cell) backing up retained memory. The issue is therefore that the previous cache is not being refreshed with SD init - and sd object creation? Which call is required to resync this data?

Adding sd.card(); just after the setup/init has solved the problem. Thanks


Great to know - I've been trying to use SDfat on a P2 and Argon on DeviceOS 5.8 and 5.6 and have been having no luck getting the card to be detected.

yeah, I wish we had a "list of known issues" somehow or somewhere to avoid wasting time on trying things that are doomed from the start.
Anyhow, In improvement for the future, if possible.

Have you tried 5.5? SDfat was ok for my use on 5.7 but that version is gone now.

Working with @iitgrad on this and was able to get something working on an Argon running device OS 4.2.0 using one of the provided SDFat examples. Going to try moving up through device OS versions until one breaks it.

Example code working on 4.2.0
  SD card read/write
 This example shows how to read and write data to and from an SD card file
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 created   Nov 2010
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe
 This example code is in the public domain.
#include <SPI.h>
//#include <SD.h>
#include "SdFat.h"
SdFat SD;
#define SD_CS_PIN D2
File myFile;
void setup() {
  // Open serial communications and wait for port to open:
  while (!Serial.isConnected()) {
    ; // wait for serial port to connect. Needed for native USB port only
  Serial.print("Initializing SD card...");
  if (!SD.begin(SD_CS_PIN)) {
    Serial.println("initialization failed!");
  Serial.println("initialization done.");
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile ="test.txt", FILE_WRITE);
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  // re-open the file for reading:
  myFile ="test.txt");
  if (myFile) {
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
    // close the file:
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
void loop() {
  // nothing happens after setup

Seems like 5.6 also works on the P2 with the sample code I posted previously, so it's 5.8 with the P2 and Argon that has the issue and I just had an issue in my firmware before that was making 5.6 not work.

1 Like

It appears that 5.8 on Argon works. But definitely fails on 5.8 on P2.


@rickkas7 over 3 weeks - is the P2 / SdFat library an issue that's logged with the engineering team for 5.9.0? Is there any update please.

It is still being looked at.


Hey @rickkas7 Any progress on this. I have spent days trying to get the Photon 2 to read and write from my SDCard to no avail. I'm down to the point that I'm thinking it may be a tool chain issue, just not sure. My engineering associate @trylaarsdam is successful using DeviceOS5.6 on a Photon but that just doesn't work for me. He has even sent me his binary that works and I have flashed it to several of my Photon 2's with no avail.

Steps I have done to try to widdle down the culprit

  1. Purchased 2nd breakout boards from Adafruit both the 3V and 5V versions.
  2. 5V versions works without issue using the Arduino Uno, and 3V works with proper level shifters -- so I have confirmed that the breakout board and SDCard is fine.
  3. Have reflashed the OS5.6 and OS5.8 with new compiles on both the Photon and interestingly have done the same thing with my Argons and I still cannot get the SDCard to function properly.

Another interesting thing is I sent my binary to @trylaarsdam for him to try last night and he confirmed that it did not work for him and somehow borked his Photon 2 such that it would not longer work with his SDCard setup (which is the same as mine). He had to reflash the OS and then reload his good binary to get things back to working again.

I am really at a loss of what the issue is here. I have triple checked wiring which frankly is just 4 wires, SCK, MISO, MOSI and SS. I have rewired the whole thing on a different breakboard. I have used different wires just to eliminate all failure possibilities.

Any insights or help anyone can provide on this would be greatly appreciated.

Which version of Device OS are you using? I think it should work on 5.6.0, but is likely broken on RTL872x in 5.7.0 and 5.8.0. The problem is in the SPI implementation for RTL872x (P2, Photon 2, and M-SoM) in those versions.

I have been using 5.6 with no success.

Both @trylaarsdam and I have also noticed weird issues with downgrading to 5.6 from 5.8. (unrelated potentially to this issue). I dont see a way to have the let me flash another OS. Is there? We both have flashed 5.8 and then have to let Particle Workbench go through the steps to downgrade to 5.6 when flashing both application and OS. During this process many times it seems to get stuck about 1/2 way through.

It looks like the fix is in this PR and should be in 5.8.2. The fix merged into the develop branch.

Great, what is the proper way to get 5.8.2 onto my Photon 2.

5.8.2 is not released yet. There are problems with flashing over USB in 5.7.0 and 5.8.0 so it may work best to OTA upgrade to 5.8.2 once it is released if you are starting from an affected version.