Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

CREATE_ALWAYS flag missing from SDFAT Adafruit Fork?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

CREATE_ALWAYS flag missing from SDFAT Adafruit Fork?

by Professor K on Mon Feb 03, 2020 5:06 pm

Background:
I'm using an ItsyBitsy M0 Express for a project and writing/reading a file to/from the onboard SPI flash.

I'm actually porting this project from an older Arduino board (a Mega) that I had an SD card attached to. I was using the FA_CREATE_ALWAYS flag when opening the file for writing so that it would be truncated/overwritten with the new file. This worked, no issues.

Problem:
The SDFat_Adafruit_Fork version of SDFat that the board requires seems to have left out the CREATE_ALWAYS flag. I can't find any evidence that any flags besides FILE_READ and FILE_WRITE are even present. I tried doing my own define of the ALWAYS_CREATE flag and using it, but it's being ignored, new data is still being appended to the file. (Code snippet below)

Am I missing something? Did they really leave the other flags out of the fork? I'd rather not have to delete the file each time before writing to it. Everything else is working.

Code: Select all | TOGGLE FULL SIZE
#include "Arduino.h"
#include <SPI.h>
#include <SdFat.h>
#include <Adafruit_SPIFlash.h>

//Constants for SPI Flash
#if defined(__SAMD51__) || defined(NRF52840_XXAA)
  Adafruit_FlashTransport_QSPI flashTransport(PIN_QSPI_SCK, PIN_QSPI_CS, PIN_QSPI_IO0, PIN_QSPI_IO1, PIN_QSPI_IO2, PIN_QSPI_IO3);
#else
  #if (SPI_INTERFACES_COUNT == 1 || defined(ADAFRUIT_CIRCUITPLAYGROUND_M0))
    Adafruit_FlashTransport_SPI flashTransport(SS, &SPI);
  #else
    Adafruit_FlashTransport_SPI flashTransport(SS1, &SPI1);
  #endif
#endif

#define FA_CREATE_ALWAYS  0x08

//SPIFlash object
Adafruit_SPIFlash flash(&flashTransport);

// file system object from SdFat
FatFileSystem fatfs;


boolean saveConfig() {
  // Convert the config file name into a char array for the poorly written fatfs routines.
  char __fileName[_fileName.length()+1];
  _fileName.toCharArray(__fileName, sizeof(__fileName));
 
  File writeFile = fatfs.open(__fileName, FILE_WRITE | FA_CREATE_ALWAYS);
  if (!writeFile) {
    Serial.print("Error, failed to open "); Serial.print(__fileName); Serial.printLN(" for writing!");
    return false;
  }
 
  Serial.println("Opened file for writing...");

  writeFile.println(_Version);

  writeFile.println(_tempScale); // _tempScale; // 0 = C, 1 = F

  writeFile.println(_displayTimeout); // _dispTimeout; // 1, 3, 5, 10, 15, 30, 99

  writeFile.println(_autoDimBacklight); // _autoDimBacklight; // 0 = off, 1 = on

  writeFile.println(_lowBattShutoff); // _lowBattShutoff; // 0 = off, 1 = on
 
  Serial.println("Wrote Stuff, now going to close file.");

  writeFile.close();

} //saveConfig
Professor K
 
Posts: 54
Joined: Sun Mar 18, 2012 7:33 pm

Re: CREATE_ALWAYS flag missing from SDFAT Adafruit Fork?

by adafruit_support_mike on Tue Feb 04, 2020 5:52 am

Try using O_CREAT | O_TRUNC.

The first one creates a file if it doesn't already exist, and the second sets the length of a successfully opened file to 0.

adafruit_support_mike
 
Posts: 61443
Joined: Thu Feb 11, 2010 2:51 pm

Please be positive and constructive with your questions and comments.