SD_Test problems

Hi @Hansen, I’m having problems writing two files consecutively on the Flash, If I run the sequences one at a time everything goes well, if I execute them consecutively the second writing corrupts the first file. The sequence consists of deleting the file and writing the new file. Do you have any idea where the problem might be? I’ll post the code part.
I will be grateful if you can help me

void deleteFile(fs::FS &fs, const char *pathD)
{
DPRINT("Deleting file: ");
DPRINTLN(pathD);
if (fs.remove(pathD))
{
DPRINTLN(“File deleted”);
}
else
{
DPRINTLN(“Delete failed”);
}
delay(500);
}

void writeFile(fs::FS &fs, const char *pathF, const char *message)
{
DPRINT("Writing file: ");
DPRINTLN(pathF);
File fileF = fs.open(pathF, FILE_WRITE);
if (!fileF)
{
DPRINTLN(“Failed to open file for writing”);
return;
}
const int capacity = 1024;
StaticJsonDocument docW;
// Set the values in the document
docW[“IdDevice”] = config.IdDevice;
docW[“DeviceName”] = config.DeviceName;
docW[“tftTimeout”] = config.tftTimeout;
docW[“ssidSD”] = config.ssidSD;
docW[“passSD”] = config.passSD;
docW[“local_IP”] = config.local_IP;
docW[“gateway”] = config.gateway;
docW[“subnet”] = config.subnet;
docW[“primaryDNS”] = config.primaryDNS;
docW[“secondaryDNS”] = config.secondaryDNS;
docW[“LowWiFiTreshold”] = config.LowWiFiTreshold;
docW[“SensorCalibrationTime”] = config.SensorCalibrationTime;
docW[“DBAddressParam”] = config.DBAddressParam;
docW[“DBPortParam”] = config.DBPortParam;
docW[“DBUpTimeParam”] = config.DBUpTimeParam;
docW[“DBUserParam”] = config.DBUserParam;
docW[“DBPswParam”] = config.DBPswParam;
docW[“HumAlaParam”] = config.HumAlaParam;
docW[“TempAlaParam”] = config.TempAlaParam;
docW[“DewAlaParam”] = config.DewAlaParam;
docW[“HumCalParam”] = config.HumCalParam;
docW[“TempCalParam”] = config.TempCalParam;
docW[“SyncDB”] = config.SyncDB;
// Serialize JSON to file
if (serializeJson(docW, fileF) == 0)
{
DPRINTLN(F(“Failed to write to file”));
}
fileF.close();
}

void WriteCfgNew()
{
DPRINTLN(“Delete file”);
deleteFile(SPIFLASH, FileCfgtest);
char bufcfgTime[30];
const char *fineriga = “\n”;
writeFilecfg(SPIFLASH, FileCfgtest, strcat(itoa(cfgTime, bufcfgTime, 10), fineriga));
readFile(SPIFLASH,FileCfgtest);
delay(500);
}

void writeFilecfg(fs::FS &fs, const char *pathcfg, const char *message)
{
DPRINT("Writing file cfg: ");
DPRINTLN(pathcfg);
File filecfg = fs.open(pathcfg, FILE_WRITE);
if (!filecfg)
{
DPRINTLN(“Failed to open file for writing”);
return;
}
if (filecfg.print(message))
{
DPRINTLN(“File written”);
}
else
{
DPRINTLN(“Write failed”);
}
filecfg.close();
}

Hi @Hansen, I tried to do a test based on the example file you indicated and I found the same problem sometimes the writing fails and one of the files gets corrupted. this is my code that i used:

/*
Connect the SD card to the following pins:
SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
*/

#include <Seeed_FS.h>
//#undef USESPIFLASH
#define USESPIFLASH
#ifdef USESPIFLASH
#define DEV SPIFLASH
#include “SFUD/Seeed_SFUD.h”
#else
#define DEV SD
#include “SD/Seeed_SD.h”
#endif

#define SERIAL Serial

#ifdef SAMD21
#define SDCARD_SS_PIN 1
#define SDCARD_SPI SPI
#endif
int c = 0;

void listDir(fs::FS &fs, const char *dirname, uint8_t levels)
{
SERIAL.print("Listing directory: ");
SERIAL.println(dirname);

File root = fs.open(dirname);
if (!root)
{
    SERIAL.println("Failed to open directory");
    return;
}
if (!root.isDirectory())
{
    SERIAL.println("Not a directory");
    return;
}

File file = root.openNextFile();
while (file)
{
    if (file.isDirectory())
    {
        SERIAL.print("  DIR : ");
        SERIAL.println(file.name());
        if (levels)
        {
            listDir(fs, file.name(), levels - 1);
        }
    }
    else
    {
        SERIAL.print("  FILE: ");
        SERIAL.print(file.name());
        SERIAL.print("  SIZE: ");
        SERIAL.println(file.size());
    }
    file = root.openNextFile();
}

}

void createDir(fs::FS &fs, const char *path)
{
SERIAL.print("Creating Dir: ");
SERIAL.println(path);
if (fs.mkdir(path))
{
SERIAL.println(“Dir created”);
}
else
{
SERIAL.println(“mkdir failed”);
}
}

void removeDir(fs::FS &fs, const char *path)
{
SERIAL.print("Removing Dir: ");
SERIAL.println(path);
if (fs.rmdir(path))
{
SERIAL.println(“Dir removed”);
}
else
{
SERIAL.println(“rmdir failed”);
}
}

void readFile(fs::FS &fs, const char *path)
{
SERIAL.print("Reading Dir: ");
SERIAL.println(path);
File file = fs.open(path);
if (!file)
{
SERIAL.println(“Failed to open file for reading”);
return;
}

SERIAL.println("Read from file: ");
while (file.available())
{
    SERIAL.write(file.read());
}
file.close();

}

void writeFile(fs::FS &fs, const char *path, const char *message)
{
SERIAL.print("Writing file: ");
SERIAL.println(path);
File file = fs.open(path, FILE_WRITE);
if (!file)
{
SERIAL.println(“Failed to open file for writing”);
return;
}

if (file.print(message))
{
    SERIAL.println("File written");
}
else
{
    SERIAL.println("Write failed");
}

file.close();

}

void appendFile(fs::FS &fs, const char *path, const char *message)
{
SERIAL.print("Appending to file: ");
SERIAL.println(path);

File file = fs.open(path, FILE_APPEND);
if (!file)
{
    SERIAL.println("Failed to open file for appending");
    return;
}
if (file.print(message))
{
    SERIAL.println("Message appended");
}
else
{
    SERIAL.println("Append failed");
}
file.close();

}

void renameFile(fs::FS &fs, const char *path1, const char *path2)
{
SERIAL.print("Renaming file “);
SERIAL.print(path1);
SERIAL.print(” to ");
SERIAL.println(path2);
if (fs.rename(path1, path2))
{
SERIAL.println(“File renamed”);
}
else
{
SERIAL.println(“Rename failed”);
}
}

void deleteFile(fs::FS &fs, const char *path)
{
SERIAL.print("Deleting file: ");
SERIAL.println(path);
if (fs.remove(path))
{
SERIAL.println(“File deleted”);
}
else
{
SERIAL.println(“Delete failed”);
}
}

void testFileIO(fs::FS &fs, const char *path)
{
File file = fs.open(path);
static uint8_t buf[512];
size_t len = 0;
uint32_t start = micros();
uint32_t end = start;
if (file)
{
len = file.size();
size_t flen = len;
start = micros();
while (len)
{
size_t toRead = len;
if (toRead > 512)
{
toRead = 512;
}
file.read(buf, toRead);
len -= toRead;
}
end = micros() - start;
SERIAL.print(flen);
SERIAL.print(" bytes read for “);
SERIAL.print(end);
SERIAL.println(” ns");
file.close();
}
else
{
SERIAL.println(“Failed to open file for reading”);
}
}

void setup()
{
SERIAL.begin(115200);
pinMode(5, OUTPUT);
digitalWrite(5, HIGH);
while (!SERIAL)
{
};
#ifdef SFUD_USING_QSPI
while (!DEV.begin(104000000UL))
{
SERIAL.println(“Card Mount Failed”);
return;
}
#else
while (!DEV.begin(SDCARD_SS_PIN, SDCARD_SPI, 4000000UL))
{
SERIAL.println(“Card Mount Failed”);
return;
}
#endif

#ifdef USESPIFLASH
uint8_t flashType = DEV.flashType();
if (flashType == FLASH_NONE)
{
SERIAL.println(“No SD card attached”);
return;
}
#else
uint8_t cardType = DEV.cardType();
if (cardType == CARD_NONE)
{
SERIAL.println(“No SD card attached”);
return;
}
#endif

#ifdef USESPIFLASH
uint8_t flashSize = DEV.flashSize() / (1024 * 1024);
SERIAL.print("SD Card Size: ");
SERIAL.print((uint32_t)flashSize);
SERIAL.println(“MB”);
#else
uint64_t cardSize = DEV.cardSize() / (1024 * 1024);
SERIAL.print("SD Card Size: ");
SERIAL.print((uint32_t)cardSize);
SERIAL.println(“MB”);
#endif
}

void loop()
{
c++;
delay(3000);
SERIAL.println©;
readFile(DEV, “/cfg2.json”);
readFile(DEV, “/cfg3.json”);
deleteFile(DEV, “/cfg2.json”);
writeFile(DEV, “/cfg2.json”, “file2!!!\n”);
readFile(DEV, “/cfg2.json”);
delay(1000);
deleteFile(DEV, “/cfg3.json”);
writeFile(DEV, “/cfg3.json”, “file3!!!\n”);
readFile(DEV, “/cfg3.json”);
}

Hi @Hansen, I placed the code again because I realized that the editor had changed it

#include <Seeed_FS.h>
//#undef USESPIFLASH
#define USESPIFLASH
#ifdef USESPIFLASH
#define DEV SPIFLASH
#include “SFUD/Seeed_SFUD.h”
#else
#define DEV SD
#include “SD/Seeed_SD.h”
#endif

#define SERIAL Serial

#ifdef SAMD21
#define SDCARD_SS_PIN 1
#define SDCARD_SPI SPI
#endif
int count = 0;

void listDir(fs::FS &fs, const char *dirname, uint8_t levels)
{
SERIAL.print("Listing directory: ");
SERIAL.println(dirname);

File root = fs.open(dirname);
if (!root)
{
    SERIAL.println("Failed to open directory");
    return;
}
if (!root.isDirectory())
{
    SERIAL.println("Not a directory");
    return;
}

File file = root.openNextFile();
while (file)
{
    if (file.isDirectory())
    {
        SERIAL.print("  DIR : ");
        SERIAL.println(file.name());
        if (levels)
        {
            listDir(fs, file.name(), levels - 1);
        }
    }
    else
    {
        SERIAL.print("  FILE: ");
        SERIAL.print(file.name());
        SERIAL.print("  SIZE: ");
        SERIAL.println(file.size());
    }
    file = root.openNextFile();
}

}

void createDir(fs::FS &fs, const char *path)
{
SERIAL.print("Creating Dir: ");
SERIAL.println(path);
if (fs.mkdir(path))
{
SERIAL.println(“Dir created”);
}
else
{
SERIAL.println(“mkdir failed”);
}
}

void removeDir(fs::FS &fs, const char *path)
{
SERIAL.print("Removing Dir: ");
SERIAL.println(path);
if (fs.rmdir(path))
{
SERIAL.println(“Dir removed”);
}
else
{
SERIAL.println(“rmdir failed”);
}
}

void readFile(fs::FS &fs, const char *path)
{
SERIAL.print("Reading Dir: ");
SERIAL.println(path);
File file = fs.open(path);
if (!file)
{
SERIAL.println(“Failed to open file for reading”);
return;
}

SERIAL.println("Read from file: ");
while (file.available())
{
    SERIAL.write(file.read());
}
file.close();

}

void writeFile(fs::FS &fs, const char *path, const char *message)
{
SERIAL.print("Writing file: ");
SERIAL.println(path);
File file = fs.open(path, FILE_WRITE);
if (!file)
{
SERIAL.println(“Failed to open file for writing”);
return;
}

if (file.print(message))
{
    SERIAL.println("File written");
}
else
{
    SERIAL.println("Write failed");
}

file.close();

}

void appendFile(fs::FS &fs, const char *path, const char *message)
{
SERIAL.print("Appending to file: ");
SERIAL.println(path);

File file = fs.open(path, FILE_APPEND);
if (!file)
{
    SERIAL.println("Failed to open file for appending");
    return;
}
if (file.print(message))
{
    SERIAL.println("Message appended");
}
else
{
    SERIAL.println("Append failed");
}
file.close();

}

void renameFile(fs::FS &fs, const char *path1, const char *path2)
{
SERIAL.print("Renaming file “);
SERIAL.print(path1);
SERIAL.print(” to ");
SERIAL.println(path2);
if (fs.rename(path1, path2))
{
SERIAL.println(“File renamed”);
}
else
{
SERIAL.println(“Rename failed”);
}
}
void deleteFile(fs::FS &fs, const char *path)
{
SERIAL.print("Deleting file: ");
SERIAL.println(path);
if (fs.remove(path))
{
SERIAL.println(“File deleted”);
}
else
{
SERIAL.println(“Delete failed”);
}
}

void testFileIO(fs::FS &fs, const char *path)
{
File file = fs.open(path);
static uint8_t buf[512];
size_t len = 0;
uint32_t start = micros();
uint32_t end = start;
if (file)
{
len = file.size();
size_t flen = len;
start = micros();
while (len)
{
size_t toRead = len;
if (toRead > 512)
{
toRead = 512;
}
file.read(buf, toRead);
len -= toRead;
}
end = micros() - start;
SERIAL.print(flen);
SERIAL.print(" bytes read for “);
SERIAL.print(end);
SERIAL.println(” ns");
file.close();
}
else
{
SERIAL.println(“Failed to open file for reading”);
}
}

void setup()
{
SERIAL.begin(115200);
pinMode(5, OUTPUT);
digitalWrite(5, HIGH);
while (!SERIAL)
{
};
#ifdef SFUD_USING_QSPI
while (!DEV.begin(104000000UL))
{
SERIAL.println(“Card Mount Failed”);
return;
}
#else
while (!DEV.begin(SDCARD_SS_PIN, SDCARD_SPI, 4000000UL))
{
SERIAL.println(“Card Mount Failed”);
return;
}
#endif

#ifdef USESPIFLASH
uint8_t flashType = DEV.flashType();
if (flashType == FLASH_NONE)
{
SERIAL.println(“No SD card attached”);
return;
}
#else
uint8_t cardType = DEV.cardType();
if (cardType == CARD_NONE)
{
SERIAL.println(“No SD card attached”);
return;
}
#endif

#ifdef USESPIFLASH
uint8_t flashSize = DEV.flashSize() / (1024 * 1024);
SERIAL.print("SD Card Size: ");
SERIAL.print((uint32_t)flashSize);
SERIAL.println(“MB”);
#else
uint64_t cardSize = DEV.cardSize() / (1024 * 1024);
SERIAL.print("SD Card Size: ");
SERIAL.print((uint32_t)cardSize);
SERIAL.println(“MB”);
#endif
}
void loop()
{
count++;
delay(3000);
SERIAL.println(count);
readFile(DEV, “/cfg2.json”);
readFile(DEV, “/cfg3.json”);
deleteFile(DEV, “/cfg2.json”);
writeFile(DEV, “/cfg2.json”, “file2!!!\n”);
readFile(DEV, “/cfg2.json”);
delay(1000);
deleteFile(DEV, “/cfg3.json”);
writeFile(DEV, “/cfg3.json”, “file3!!!\n”);
readFile(DEV, “/cfg3.json”);
}

Click to see the code
/*
Connect the SD card to the following pins:
SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
*/

#include <Seeed_FS.h>
//#undef USESPIFLASH
#define USESPIFLASH
#ifdef USESPIFLASH
#define DEV SPIFLASH
#include “SFUD/Seeed_SFUD.h”
#else
#define DEV SD
#include “SD/Seeed_SD.h”
#endif

#define SERIAL Serial

#ifdef SAMD21
#define SDCARD_SS_PIN 1
#define SDCARD_SPI SPI
#endif
int c = 0;

void listDir(fs::FS &fs, const char *dirname, uint8_t levels)
{
SERIAL.print("Listing directory: ");
SERIAL.println(dirname);

File root = fs.open(dirname);
if (!root)
{
    SERIAL.println("Failed to open directory");
    return;
}
if (!root.isDirectory())
{
    SERIAL.println("Not a directory");
    return;
}

File file = root.openNextFile();
while (file)
{
    if (file.isDirectory())
    {
        SERIAL.print("  DIR : ");
        SERIAL.println(file.name());
        if (levels)
        {
            listDir(fs, file.name(), levels - 1);
        }
    }
    else
    {
        SERIAL.print("  FILE: ");
        SERIAL.print(file.name());
        SERIAL.print("  SIZE: ");
        SERIAL.println(file.size());
    }
    file = root.openNextFile();
}

}

void createDir(fs::FS &fs, const char *path)
{
SERIAL.print("Creating Dir: ");
SERIAL.println(path);
if (fs.mkdir(path))
{
SERIAL.println(“Dir created”);
}
else
{
SERIAL.println(“mkdir failed”);
}
}

void removeDir(fs::FS &fs, const char *path)
{
SERIAL.print("Removing Dir: ");
SERIAL.println(path);
if (fs.rmdir(path))
{
SERIAL.println(“Dir removed”);
}
else
{
SERIAL.println(“rmdir failed”);
}
}

void readFile(fs::FS &fs, const char *path)
{
SERIAL.print("Reading Dir: ");
SERIAL.println(path);
File file = fs.open(path);
if (!file)
{
SERIAL.println(“Failed to open file for reading”);
return;
}

SERIAL.println("Read from file: ");
while (file.available())
{
    SERIAL.write(file.read());
}
file.close();

}

void writeFile(fs::FS &fs, const char *path, const char *message)
{
SERIAL.print("Writing file: ");
SERIAL.println(path);
File file = fs.open(path, FILE_WRITE);
if (!file)
{
SERIAL.println(“Failed to open file for writing”);
return;
}

if (file.print(message))
{
    SERIAL.println("File written");
}
else
{
    SERIAL.println("Write failed");
}

file.close();

}

void appendFile(fs::FS &fs, const char *path, const char *message)
{
SERIAL.print("Appending to file: ");
SERIAL.println(path);

File file = fs.open(path, FILE_APPEND);
if (!file)
{
    SERIAL.println("Failed to open file for appending");
    return;
}
if (file.print(message))
{
    SERIAL.println("Message appended");
}
else
{
    SERIAL.println("Append failed");
}
file.close();

}

void renameFile(fs::FS &fs, const char *path1, const char *path2)
{
SERIAL.print("Renaming file “);
SERIAL.print(path1);
SERIAL.print(” to ");
SERIAL.println(path2);
if (fs.rename(path1, path2))
{
SERIAL.println(“File renamed”);
}
else
{
SERIAL.println(“Rename failed”);
}
}

void deleteFile(fs::FS &fs, const char *path)
{
SERIAL.print("Deleting file: ");
SERIAL.println(path);
if (fs.remove(path))
{
SERIAL.println(“File deleted”);
}
else
{
SERIAL.println(“Delete failed”);
}
}

void testFileIO(fs::FS &fs, const char *path)
{
File file = fs.open(path);
static uint8_t buf[512];
size_t len = 0;
uint32_t start = micros();
uint32_t end = start;
if (file)
{
len = file.size();
size_t flen = len;
start = micros();
while (len)
{
size_t toRead = len;
if (toRead > 512)
{
toRead = 512;
}
file.read(buf, toRead);
len -= toRead;
}
end = micros() - start;
SERIAL.print(flen);
SERIAL.print(" bytes read for “);
SERIAL.print(end);
SERIAL.println(” ns");
file.close();
}
else
{
SERIAL.println(“Failed to open file for reading”);
}
}

void setup()
{
SERIAL.begin(115200);
pinMode(5, OUTPUT);
digitalWrite(5, HIGH);
while (!SERIAL)
{
};
#ifdef SFUD_USING_QSPI
while (!DEV.begin(104000000UL))
{
SERIAL.println(“Card Mount Failed”);
return;
}
#else
while (!DEV.begin(SDCARD_SS_PIN, SDCARD_SPI, 4000000UL))
{
SERIAL.println(“Card Mount Failed”);
return;
}
#endif

#ifdef USESPIFLASH
uint8_t flashType = DEV.flashType();
if (flashType == FLASH_NONE)
{
SERIAL.println(“No SD card attached”);
return;
}
#else
uint8_t cardType = DEV.cardType();
if (cardType == CARD_NONE)
{
SERIAL.println(“No SD card attached”);
return;
}
#endif

#ifdef USESPIFLASH
uint8_t flashSize = DEV.flashSize() / (1024 * 1024);
SERIAL.print("SD Card Size: ");
SERIAL.print((uint32_t)flashSize);
SERIAL.println(“MB”);
#else
uint64_t cardSize = DEV.cardSize() / (1024 * 1024);
SERIAL.print("SD Card Size: ");
SERIAL.print((uint32_t)cardSize);
SERIAL.println(“MB”);
#endif
}

void loop()
{
c++;
delay(3000);
SERIAL.println©;
readFile(DEV, “/cfg2.json”);
readFile(DEV, “/cfg3.json”);
deleteFile(DEV, “/cfg2.json”);
writeFile(DEV, “/cfg2.json”, “file2!!!\n”);
readFile(DEV, “/cfg2.json”);
delay(1000);
deleteFile(DEV, “/cfg3.json”);
writeFile(DEV, “/cfg3.json”, “file3!!!\n”);
readFile(DEV, “/cfg3.json”);
}

tip : If you have large code chunks (or logs) , you can collapse (and format) them to avoid a very long post like this :

[details=Click to see the code]
```cpp

code here

[/details]

thank you @BoRRoZ @Hansen,
I enter the code as you indicated below

Click to see the code

#include <Seeed_FS.h>
//#undef USESPIFLASH
#define USESPIFLASH
#ifdef USESPIFLASH
#define DEV SPIFLASH
#include "SFUD/Seeed_SFUD.h"
#else
#define DEV SD
#include "SD/Seeed_SD.h"
#endif

#define SERIAL Serial

#ifdef _SAMD21_
#define SDCARD_SS_PIN 1
#define SDCARD_SPI SPI
#endif
int count = 0;

void listDir(fs::FS &fs, const char *dirname, uint8_t levels)
{
    SERIAL.print("Listing directory: ");
    SERIAL.println(dirname);

    File root = fs.open(dirname);
    if (!root)
    {
        SERIAL.println("Failed to open directory");
        return;
    }
    if (!root.isDirectory())
    {
        SERIAL.println("Not a directory");
        return;
    }

    File file = root.openNextFile();
    while (file)
    {
        if (file.isDirectory())
        {
            SERIAL.print("  DIR : ");
            SERIAL.println(file.name());
            if (levels)
            {
                listDir(fs, file.name(), levels - 1);
            }
        }
        else
        {
            SERIAL.print("  FILE: ");
            SERIAL.print(file.name());
            SERIAL.print("  SIZE: ");
            SERIAL.println(file.size());
        }
        file = root.openNextFile();
    }
}

void createDir(fs::FS &fs, const char *path)
{
    SERIAL.print("Creating Dir: ");
    SERIAL.println(path);
    if (fs.mkdir(path))
    {
        SERIAL.println("Dir created");
    }
    else
    {
        SERIAL.println("mkdir failed");
    }
}

void removeDir(fs::FS &fs, const char *path)
{
    SERIAL.print("Removing Dir: ");
    SERIAL.println(path);
    if (fs.rmdir(path))
    {
        SERIAL.println("Dir removed");
    }
    else
    {
        SERIAL.println("rmdir failed");
    }
}

void readFile(fs::FS &fs, const char *path)
{
    SERIAL.print("Reading Dir: ");
    SERIAL.println(path);
    File file = fs.open(path);
    if (!file)
    {
        SERIAL.println("Failed to open file for reading");
        return;
    }

    SERIAL.println("Read from file: ");
    while (file.available())
    {
        SERIAL.write(file.read());
    }
    file.close();
}

void writeFile(fs::FS &fs, const char *path, const char *message)
{
    SERIAL.print("Writing file: ");
    SERIAL.println(path);
    File file = fs.open(path, FILE_WRITE);
    if (!file)
    {
        SERIAL.println("Failed to open file for writing");
        return;
    }

    if (file.print(message))
    {
        SERIAL.println("File written");
    }
    else
    {
        SERIAL.println("Write failed");
    }

    file.close();
}

void appendFile(fs::FS &fs, const char *path, const char *message)
{
    SERIAL.print("Appending to file: ");
    SERIAL.println(path);

    File file = fs.open(path, FILE_APPEND);
    if (!file)
    {
        SERIAL.println("Failed to open file for appending");
        return;
    }
    if (file.print(message))
    {
        SERIAL.println("Message appended");
    }
    else
    {
        SERIAL.println("Append failed");
    }
    file.close();
}

void renameFile(fs::FS &fs, const char *path1, const char *path2)
{
    SERIAL.print("Renaming file ");
    SERIAL.print(path1);
    SERIAL.print(" to ");
    SERIAL.println(path2);
    if (fs.rename(path1, path2))
    {
        SERIAL.println("File renamed");
    }
    else
    {
        SERIAL.println("Rename failed");
    }
}
void deleteFile(fs::FS &fs, const char *path)
{
    SERIAL.print("Deleting file: ");
    SERIAL.println(path);
    if (fs.remove(path))
    {
        SERIAL.println("File deleted");
    }
    else
    {
        SERIAL.println("Delete failed");
    }
}

void testFileIO(fs::FS &fs, const char *path)
{
    File file = fs.open(path);
    static uint8_t buf[512];
    size_t len = 0;
    uint32_t start = micros();
    uint32_t end = start;
    if (file)
    {
        len = file.size();
        size_t flen = len;
        start = micros();
        while (len)
        {
            size_t toRead = len;
            if (toRead > 512)
            {
                toRead = 512;
            }
            file.read(buf, toRead);
            len -= toRead;
        }
        end = micros() - start;
        SERIAL.print(flen);
        SERIAL.print(" bytes read for ");
        SERIAL.print(end);
        SERIAL.println(" ns");
        file.close();
    }
    else
    {
        SERIAL.println("Failed to open file for reading");
    }
}

void setup()
{
    SERIAL.begin(115200);
    pinMode(5, OUTPUT);
    digitalWrite(5, HIGH);
    while (!SERIAL)
    {
    };
#ifdef SFUD_USING_QSPI
    while (!DEV.begin(104000000UL))
    {
        SERIAL.println("Card Mount Failed");
        return;
    }
#else
    while (!DEV.begin(SDCARD_SS_PIN, SDCARD_SPI, 4000000UL))
    {
        SERIAL.println("Card Mount Failed");
        return;
    }
#endif

#ifdef USESPIFLASH
    uint8_t flashType = DEV.flashType();
    if (flashType == FLASH_NONE)
    {
        SERIAL.println("No SD card attached");
        return;
    }
#else
    uint8_t cardType = DEV.cardType();
    if (cardType == CARD_NONE)
    {
        SERIAL.println("No SD card attached");
        return;
    }
#endif

#ifdef USESPIFLASH
    uint8_t flashSize = DEV.flashSize() / (1024 * 1024);
    SERIAL.print("SD Card Size: ");
    SERIAL.print((uint32_t)flashSize);
    SERIAL.println("MB");
#else
    uint64_t cardSize = DEV.cardSize() / (1024 * 1024);
    SERIAL.print("SD Card Size: ");
    SERIAL.print((uint32_t)cardSize);
    SERIAL.println("MB");
#endif
}
void loop()
{
  count++;
  delay(3000);
  SERIAL.println(count);
  readFile(DEV, "/cfg2.json");
  readFile(DEV, "/cfg3.json");
  deleteFile(DEV, "/cfg2.json");
  writeFile(DEV, "/cfg2.json", "file2!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  readFile(DEV, "/cfg2.json");
  delay(1000);
  deleteFile(DEV, "/cfg3.json");
  writeFile(DEV, "/cfg3.json", "file3!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
  readFile(DEV, "/cfg3.json");
}

1 Like

Hi @Gianfry60
we have been updated the lib to solve this problem. we can get the newest lib by accessing https://github.com/Seeed-Studio/Seeed_Arduino_FS.git.

2 Likes

Hi @Hansen, I did some tests with the Multi_partition example file by inserting as a test the reading of the files. The files I layered with the SD_Test code on the SD and I checked both by reading them with the code and on the PC and they read perfectly. In the Multi_partition code, however, the reading of the files fails. is it a library error or am I doing something wrong?
I am attaching below the code I used for testing both the one to write “SD_TEST” and the one to read “Multi_partition”
If you can help me I will be grateful.
Thank you

CODE USED TO WRITE

Click to see the code
/* SD_Test code */

/*

    Connect the SD card to the following pins:

    SD card attached to SPI bus as follows:

 ** MOSI - pin 11

 ** MISO - pin 12

 ** CLK - pin 13

 ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)

*/

#include <Seeed_FS.h>

//#define USESPIFLASH

#undef USESPIFLASH

#ifdef USESPIFLASH

#define DEV SPIFLASH

#include "SFUD/Seeed_SFUD.h"

#else

#define DEV SD

#include "SD/Seeed_SD.h"

#endif

#define SERIAL Serial

#ifdef _SAMD21_

#define SDCARD_SS_PIN 1

#define SDCARD_SPI SPI

#endif

void listDir(fs::FS &fs, const char *dirname, uint8_t levels)

{

    SERIAL.print("Listing directory: ");

    SERIAL.println(dirname);

    File root = fs.open(dirname);

    if (!root)

    {

        SERIAL.println("Failed to open directory");

        return;

    }

    if (!root.isDirectory())

    {

        SERIAL.println("Not a directory");

        return;

    }

    File file = root.openNextFile();

    while (file)

    {

        if (file.isDirectory())

        {

            SERIAL.print("  DIR : ");

            SERIAL.println(file.name());

            if (levels)

            {

                listDir(fs, file.name(), levels - 1);

            }

        }

        else

        {

            SERIAL.print("  FILE: ");

            SERIAL.print(file.name());

            SERIAL.print("  SIZE: ");

            SERIAL.println(file.size());

        }

        file = root.openNextFile();

    }

}

void createDir(fs::FS &fs, const char *path)

{

    SERIAL.print("Creating Dir: ");

    SERIAL.println(path);

    if (fs.mkdir(path))

    {

        SERIAL.println("Dir created");

    }

    else

    {

        SERIAL.println("mkdir failed");

    }

}

void removeDir(fs::FS &fs, const char *path)

{

    SERIAL.print("Removing Dir: ");

    SERIAL.println(path);

    if (fs.rmdir(path))

    {

        SERIAL.println("Dir removed");

    }

    else

    {

        SERIAL.println("rmdir failed");

    }

}

void readFile(fs::FS &fs, const char *path)

{

    SERIAL.print("Reading Dir: ");

    SERIAL.println(path);

    File file = fs.open(path);

    if (!file)

    {

        SERIAL.println("Failed to open file for reading");

        return;

    }

    SERIAL.print("Read from file: ");

    while (file.available())

    {

        SERIAL.write(file.read());

    }

    file.close();

}

void writeFile(fs::FS &fs, const char *path, const char *message)

{

    SERIAL.print("Writing file: ");

    SERIAL.println(path);

    File file = fs.open(path, FILE_WRITE);

    if (!file)

    {

        SERIAL.println("Failed to open file for writing");

        return;

    }

    if (file.print(message))

    {

        SERIAL.println("File written");

    }

    else

    {

        SERIAL.println("Write failed");

    }

    file.close();

}

void appendFile(fs::FS &fs, const char *path, const char *message)

{

    SERIAL.print("Appending to file: ");

    SERIAL.println(path);

    File file = fs.open(path, FILE_APPEND);

    if (!file)

    {

        SERIAL.println("Failed to open file for appending");

        return;

    }

    if (file.print(message))

    {

        SERIAL.println("Message appended");

    }

    else

    {

        SERIAL.println("Append failed");

    }

    file.close();

}

void renameFile(fs::FS &fs, const char *path1, const char *path2)

{

    SERIAL.print("Renaming file ");

    SERIAL.print(path1);

    SERIAL.print(" to ");

    SERIAL.println(path2);

    if (fs.rename(path1, path2))

    {

        SERIAL.println("File renamed");

    }

    else

    {

        SERIAL.println("Rename failed");

    }

}

void deleteFile(fs::FS &fs, const char *path)

{

    SERIAL.print("Deleting file: ");

    SERIAL.println(path);

    if (fs.remove(path))

    {

        SERIAL.println("File deleted");

    }

    else

    {

        SERIAL.println("Delete failed");

    }

}

void testFileIO(fs::FS &fs, const char *path)

{

    File file = fs.open(path);

    static uint8_t buf[512];

    size_t len = 0;

    uint32_t start = micros();

    uint32_t end = start;

    if (file)

    {

        len = file.size();

        size_t flen = len;

        start = micros();

        while (len)

        {

            size_t toRead = len;

            if (toRead > 512)

            {

                toRead = 512;

            }

            file.read(buf, toRead);

            len -= toRead;

        }

        end = micros() - start;

        SERIAL.print(flen);

        SERIAL.print(" bytes read for ");

        SERIAL.print(end);

        SERIAL.println(" ns");

        file.close();

    }

    else

    {

        SERIAL.println("Failed to open file for reading");

    }

}

void setup()

{

    SERIAL.begin(115200);

    pinMode(5, OUTPUT);

    digitalWrite(5, HIGH);

    while (!SERIAL)

    {

    };

#ifdef SFUD_USING_QSPI

    while (!DEV.begin(104000000UL))

    {

        SERIAL.println("Card Mount Failed");

        return;

    }

#else

    while (!DEV.begin(SDCARD_SS_PIN, SDCARD_SPI, 4000000UL))

    {

        SERIAL.println("Card Mount Failed");

        return;

    }

#endif

#ifdef USESPIFLASH

    uint8_t flashType = DEV.flashType();

    if (flashType == FLASH_NONE)

    {

        SERIAL.println("No flash attached");

        return;

    }

#else

    uint8_t cardType = DEV.cardType();

    if (cardType == CARD_NONE)

    {

        SERIAL.println("No SD card attached");

        return;

    }

#endif

#ifdef USESPIFLASH

    uint32_t flashSize = DEV.flashSize() / (1024 * 1024);

    SERIAL.print("flash Size: ");

    SERIAL.print((uint32_t)flashSize);

    SERIAL.println("MB");

#else

    uint64_t cardSize = DEV.cardSize() / (1024 * 1024);

    SERIAL.print("SD Card Size: ");

    SERIAL.print((uint32_t)cardSize);

    SERIAL.println("MB");

#endif

    listDir(DEV, "/", 0);

    // createDir(DEV, "/mydir");

    // listDir(DEV, "/", 0);

    // removeDir(DEV, "/mydir");

    // listDir(DEV, "/", 2);

    deleteFile(DEV, "/foo.txt");

    writeFile(DEV, "/foo.txt", "Hello ");

    appendFile(DEV, "/foo.txt", "World!\n");

    readFile(DEV, "/foo.txt");

    // renameFile(DEV, "/hello.txt", "/foo.txt");

    deleteFile(DEV, "/foo2.txt");

    writeFile(DEV, "/foo2.txt", "Hello2 ");

    appendFile(DEV, "/foo2.txt", "World!2\n");

    readFile(DEV, "/foo2.txt");

    // testFileIO(DEV, "/foo.txt");

    listDir(DEV, "/", 0);

    uint32_t totalBytes = DEV.totalBytes();

    SERIAL.print("Total space: ");

    SERIAL.print(totalBytes / (1024 * 1024));

    SERIAL.println("MB");

    uint32_t usedBytes = DEV.usedBytes();

    SERIAL.print("Used space: ");

    SERIAL.print(usedBytes / (1024 * 1024));

    SERIAL.println("MB");

}

void loop()

{

}


CODE USED TO READ

Click to see the code
#include <Seeed_FS.h>
#include "SFUD/Seeed_SFUD.h"
#include "SD/Seeed_SD.h"
#define SERIAL Serial

#ifdef _SAMD21_
#define SDCARD_SS_PIN 2
#define SDCARD_SPI SPI
#endif

void readFile(fs::FS &fs, const char *path)
{
    SERIAL.print("Reading Dir: ");
    SERIAL.println(path);
    File file = fs.open(path);
    if (!file)
    {
        SERIAL.println("Failed to open file for reading");
        return;
    }

    SERIAL.print("Read from file: ");
    while (file.available())
    {
        SERIAL.write(file.read());
    }
    file.close();
}

void listDir(fs::FS &fs, const char *dirname, uint8_t levels)
{
    SERIAL.print("Listing directory: ");
    SERIAL.println(dirname);

    File root = fs.open(dirname);
    if (!root)
    {
        SERIAL.println("Failed to open directory");
        return;
    }
    if (!root.isDirectory())
    {
        SERIAL.println("Not a directory");
        return;
    }

    File file = root.openNextFile();
    while (file)
    {
        if (file.isDirectory())
        {
            SERIAL.print("  DIR : ");
            SERIAL.println(file.name());
            if (levels)
            {
                listDir(fs, file.name(), levels - 1);
            }
        }
        else
        {
            SERIAL.print("  FILE: ");
            SERIAL.print(file.name());
            SERIAL.print("  SIZE: ");
            SERIAL.println(file.size());
        }
        file = root.openNextFile();
    }
}
void setup()
{
    SERIAL.begin(115200);
    while (!SERIAL)
    {
    };
#ifdef SFUD_USING_QSPI
    while (!SPIFLASH.begin(104000000UL))
    {
        SERIAL.println("Flash Mount Failed");
        return;
    }
    while (!SD.begin(SDCARD_SS_PIN, SDCARD_SPI, 4000000UL))
    {
        SERIAL.println("SD Card Mount Failed");
        return;
    }
#else
    while (!SD.begin(SDCARD_SS_PIN, SDCARD_SPI, 4000000UL))
    {
        SERIAL.println("SD Card Mount Failed");
        return;
    }
    while (!SPIFLASH.begin(1, SPI, 4000000UL))
    {
        SERIAL.println(" Flash Mount Failed");
        return;
    }
#endif
    uint8_t flashType = SPIFLASH.flashType();
    if (flashType == FLASH_NONE)
    {
        SERIAL.println("No Flash attached");
        return;
    }
    uint8_t cardType = SD.cardType();
    if (cardType == CARD_NONE)
    {
        SERIAL.println("No SD card attached");
        return;
    }
    uint8_t flashSize = SPIFLASH.flashSize() / (1024 * 1024);
    SERIAL.print("Flash Size: ");
    SERIAL.print((uint32_t)flashSize);
    SERIAL.println("MB");
    uint64_t cardSize = SD.cardSize() / (1024 * 1024);
    SERIAL.print("SD Card Size: ");
    SERIAL.print((uint32_t)cardSize);
    SERIAL.println("MB");

    uint32_t FlashtotalBytes = SPIFLASH.totalBytes();
    SERIAL.print("Total space: ");
    SERIAL.print(FlashtotalBytes / (1024 * 1024));
    SERIAL.println("MB");
    uint32_t FlasusedBytes = SPIFLASH.usedBytes();
    SERIAL.print("Used space: ");
    SERIAL.print(FlasusedBytes / (1024 * 1024));
    SERIAL.println("MB");

    uint32_t SDtotalBytes = SD.totalBytes();
    SERIAL.print("Total space: ");
    SERIAL.print(SDtotalBytes / (1024 * 1024));
    SERIAL.println("MB");
    uint32_t SDusedBytes = SD.usedBytes();
    SERIAL.print("Used space: ");
    SERIAL.print(SDusedBytes / (1024 * 1024));
    SERIAL.println("MB");

    listDir(SD, "SD:/", 0);

    readFile(SD, "/foo2.txt");
    readFile(SD, "/foo.txt");
    //listDir(SPIFLASH, "Flash:/", 0);
    //readFile(SPIFLASH, "/foo.txt");
}

void loop()
{
}

1 Like

Hi @Hansen, I noticed that the problem arises when both #include “SD / Seeed_SD.h” and #include “SFUD / Seeed_SFUD.h” files are included. If I include both of them only FLASH reading works if I include only #include “SD / Seeed_SD.h” then SD works. How can you solve this problem to use both SD and FLASH at the same time?
I await your considerations.
Thank you

The updated library doesn’t appear on the Arduino Library Manager.

Do you plan to make an official release? Thank you!

Of course, we’re going to do that in Q4. @reivilo

Hi @Gianfry60:
there exist an example for doing this thing in our Github.

Hi @Hansen, the code you indicate is the same one I used if you see in the code I sent. It is the same code but if you try to insert a routine for reading the files you will see that it crashes unless you comment out all the Flash related code by removing the inclusion of “SFUD / Seeed_SFUD.h”. if you don’t do this the reading fails.

Hi @Gianfry60:
I would say, you must use a different filesystem’s variety if you want to use control two partitions at the same time.

    writeFile(SD, "/foo2.txt", "Hello2 ");
    writeFile(SPIFLASH, "/foo2.txt", "Hello2 ");

Sorry @Hansen but I don’t understand, I used the SD_Test code to write and read the “mickey.txt” and “mouse.txt” files and everything went ok. I changed the “#define USESPIFLASH” to #undef USESPIFLASH “and I wrote and read” foo.txt "and pluto.txt on SD and everything went ok. I tried to read SD files on PC and everything is OK. I load the Milti_partition software and if I only use the “listDir” everything is ok I insert the readFile routine copied from SD_Test and trying to read the files I created previously the program gives me a read error.
If I remove the inclusion of “SFUD / Seeed_SFUD.h” and the code related to the FLASH, the reading of the SD is successful.
I am attaching the result of the Serial Monitor.
I await your help
Thank you
Gianfranco

Click to see the code

Using the SD_Test code

I write and read on Flash by setting #define USESPIFLASH

09:30:46.444 -> flash Size: 4MB
09:30:46.444 -> Listing directory: /
09:30:46.444 -> Writing file: /mickey.txt
09:30:46.479 -> File written
09:30:46.618 -> Writing file: /mouse.txt
09:30:46.653 -> File written
09:30:46.793 -> Reading Dir: /mickey.txt
09:30:46.793 -> Read from file: Mickey  
09:30:46.793 -> Reading Dir: /mouse.txt
09:30:46.793 -> Read from file: mouse  
09:30:46.793 -> Listing directory: /
09:30:46.793 ->   FILE: /mickey.txt  SIZE: 7
09:30:46.793 ->   FILE: /mouse.txt  SIZE: 6
09:30:46.828 -> Total space: 3MB
09:30:46.828 -> Used space: 0MB

I write and read on SD by setting #undef USESPIFLASH

09:34:38.957 -> SD Card Size: 15079MB
09:34:38.957 -> Listing directory: /
09:34:38.957 ->   DIR : /System Volume Information
09:34:38.992 -> Writing file: /pippo.txt
09:34:39.040 -> File written
09:34:39.060 -> Writing file: /pluto.txt
09:34:39.095 -> File written
09:34:39.132 -> Reading Dir: /pippo.txt
09:34:39.132 -> Read from file: pippo  
09:34:39.132 -> Reading Dir: /pluto.txt
09:34:39.132 -> Read from file: pluto  
09:34:39.132 -> Listing directory: /
09:34:39.132 ->   DIR : /System Volume Information
09:34:39.132 ->   FILE: /pippo.txt  SIZE: 6
09:34:39.132 ->   FILE: /pluto.txt  SIZE: 6
09:34:39.165 -> Total space: 2774MB
09:34:39.165 -> Used space: 0MB

Using the Multi_partition code

original code only with listdir

09:35:55.242 -> Flash Size: 4MB
09:35:55.242 -> SD Card Size: 15079MB
09:35:55.242 -> Total space: 3MB
09:35:55.242 -> Used space: 0MB
09:35:55.242 -> Total space: 2774MB
09:35:55.242 -> Used space: 0MB
09:35:55.242 -> Listing directory: 1:/
09:35:55.242 ->   DIR : 1:/System Volume Information
09:35:55.242 ->   FILE: 1:/pippo.txt  SIZE: 6
09:35:55.277 ->   FILE: 1:/pluto.txt  SIZE: 6
09:35:55.277 -> Listing directory: 0:/
09:35:55.277 ->   FILE: 0:/mickey.txt  SIZE: 7
09:35:55.277 ->   FILE: 0:/mouse.txt  SIZE: 6

I insert a readFile routine copied from SD_Test

09:38:23.553 -> Flash Size: 4MB
09:38:23.553 -> SD Card Size: 15079MB
09:38:23.553 -> Total space: 3MB
09:38:23.553 -> Used space: 0MB
09:38:23.553 -> Total space: 2774MB
09:38:23.553 -> Used space: 0MB
09:38:23.553 -> Listing directory: 1:/
09:38:23.584 ->   DIR : 1:/System Volume Information
09:38:23.584 ->   FILE: 1:/pippo.txt  SIZE: 6
09:38:23.584 ->   FILE: 1:/pluto.txt  SIZE: 6
09:38:23.584 -> Listing directory: 0:/
09:38:23.584 ->   FILE: 0:/mickey.txt  SIZE: 7
09:38:23.584 ->   FILE: 0:/mouse.txt  SIZE: 6
09:38:23.584 -> Reading Dir: /pippo.txt
09:38:23.584 -> Failed to open file for reading
09:38:23.584 -> Reading Dir: /pluto.txt
09:38:23.584 -> Failed to open file for reading

Hi @Hansen, I found this solution but it’s a bit cumbersome, can you tell me if it’s the right way? Or can you give me some suggestions to simplify it?
I thank you for any help.
Thank you
Gianfranco

Click to see the code

#include <Seeed_FS.h>
#include "SD/Seeed_SD.h"
#include "SFUD/Seeed_SFUD.h"


#ifdef _SAMD21_
#define SDCARD_SS_PIN 1
#define SDCARD_SPI SPI
#endif

//#define NDEBUG

#ifndef NDEBUG
#define DSTART(speed) Serial.begin(speed)
#define DPRINT(...) Serial.print(__VA_ARGS__)
#define DPRINTLN(...) Serial.println(__VA_ARGS__)
#define DSWRITE(...) Serial.write(__VA_ARGS__)

#else
#define DSTART(...) \
    do              \
    {               \
    } while (0)
#define DPRINT(...) \
    do              \
    {               \
    } while (0)
#define DPRINTLN(...) \
    do                \
    {                 \
    } while (0)
#define DSWRITE(...)  \
    do                \
    {                 \
    } while (0)
#undef DSERIAL 
#endif

int startCount = 10100999;
uint8_t cardType = 0;
uint8_t cardSize = 0;
uint8_t flashType = 0;
uint8_t flashSize = 0;
int i = 1;

void listDir(fs::FS &fs, const char *dirname, uint8_t levels)
{
    DPRINT("Listing directory: ");
    DPRINTLN(dirname);

    File root = fs.open(dirname);
    if (!root)
    {
        DPRINTLN("Failed to open directory");
        return;
    }
    if (!root.isDirectory())
    {
        DPRINTLN("Not a directory");
        return;
    }

    File file = root.openNextFile();
    while (file)
    {
        if (file.isDirectory())
        {
            DPRINT("  DIR : ");
            DPRINTLN(file.name());
            if (levels)
            {
                listDir(fs, file.name(), levels - 1);
            }
        }
        else
        {
            DPRINT("  FILE: ");
            DPRINT(file.name());
            DPRINT("  SIZE: ");
            DPRINTLN(file.size());
        }
        file = root.openNextFile();
    }
}

void createDir(fs::FS &fs, const char *path)
{
    DPRINT("Creating Dir: ");
    DPRINTLN(path);
    if (fs.mkdir(path))
    {
        DPRINTLN("Dir created");
    }
    else
    {
        DPRINTLN("mkdir failed");
    }
}

void removeDir(fs::FS &fs, const char *path)
{
    DPRINT("Removing Dir: ");
    DPRINTLN(path);
    if (fs.rmdir(path))
    {
        DPRINTLN("Dir removed");
    }
    else
    {
        DPRINTLN("rmdir failed");
    }
}

void readFile(fs::FS &fs, const char *path)
{
    DPRINT("Reading Dir: ");
    DPRINTLN(path);
    File file = fs.open(path);
    if (!file)
    {
        DPRINTLN("Failed to open file for reading");
        return;
    }

    DPRINT("Read from file: ");
    DPRINTLN("");
    while (file.available())
    {
       DSWRITE(file.read());
    }
    DPRINTLN(" ");
    file.close();
}

void writeFile(fs::FS &fs, const char *path, const char *message)
{
    DPRINT("Writing file: ");
    DPRINTLN(path);
    File file = fs.open(path, FILE_WRITE);
    if (!file)
    {
        DPRINTLN("Failed to open file for writing");
        return;
    }

    if (file.print(message))
    {
        DPRINTLN("File written");
    }
    else
    {
        DPRINTLN("Write failed");
    }

    file.close();
}

void appendFile(fs::FS &fs, const char *path, const char *message)
{
    //DPRINT("Appending to file: ");
    //DPRINTLN(path);

    File file = fs.open(path, FILE_APPEND);
    if (!file)
    {
        DPRINTLN("Failed to open file for appending");
        return;
    }
    if (file.print(message))
    {
        //DPRINTLN("Message appended");
    }
    else
    {
        DPRINTLN("Append failed");
    }
    file.close();
}

void renameFile(fs::FS &fs, const char *path1, const char *path2)
{
    DPRINT("Renaming file ");
    DPRINT(path1);
    DPRINT(" to ");
    DPRINTLN(path2);
    if (fs.rename(path1, path2))
    {
        DPRINTLN("File renamed");
    }
    else
    {
        DPRINTLN("Rename failed");
    }
}

void deleteFile(fs::FS &fs, const char *path)
{
    DPRINT("Deleting file: ");
    DPRINTLN(path);
    if (fs.remove(path))
    {
        DPRINTLN("File deleted");
    }
    else
    {
        DPRINTLN("Delete failed");
    }
}

void testFileIO(fs::FS &fs, const char *path)
{
    File file = fs.open(path);
    static uint8_t buf[512];
    size_t len = 0;
    uint32_t start = micros();
    uint32_t end = start;
    if (file)
    {
        len = file.size();
        size_t flen = len;
        start = micros();
        while (len)
        {
            size_t toRead = len;
            if (toRead > 512)
            {
                toRead = 512;
            }
            file.read(buf, toRead);
            len -= toRead;
        }
        end = micros() - start;
        DPRINT(flen);
        DPRINT(" bytes read for ");
        DPRINT(end);
        DPRINTLN(" ns");
        file.close();
    }
    else
    {
        DPRINTLN("Failed to open file for reading");
    }
}

char *convertIntegerToChar(int N)
{

    // Count digits in number N
    int m = N;
    int digit = 0;
    while (m)
    {

        // Increment number of digits
        digit++;

        // Truncate the last
        // digit from the number
        m /= 10;
    }

    // Declare char array for result
    char *arr;

    // Declare duplicate char array
    char arr1[digit];

    // Memory allocaton of array
    arr = (char *)malloc(digit);

    // Separating integer into digits and
    // accomodate it to character array
    int index = 0;
    while (N)
    {

        // Separate last digit from
        // the number and add ASCII
        // value of character '0' is 48
        arr1[++index] = N % 10 + '0';

        // Truncate the last
        // digit from the number
        N /= 10;
    }

    // Reverse the array for result
    int i;
    for (i = 0; i < index; i++)
    {
        arr[i] = arr1[index - i];
    }

    // Char array truncate by null
    arr[i] = '\0';

    // Return char array
    return (char *)arr;
}

void writeSequence(fs::FS &fs, const char *path)
{
    deleteFile(fs, path);
    i = 1;
    for (int i = 1; i < 102; i++)
    {
        DPRINT(".");
        char *buf = convertIntegerToChar(startCount + i);
        appendFile(SD, path, buf);
        appendFile(SD, path, "\n");
    }
    DPRINTLN("");
    readFile(SD, path);
}
void setup()
{
    DSTART(115200);
#ifndef NDEBUG    
    while (!Serial)
    {
    };
#endif

#define DEV SD
#undef USESPIFLASH

    while (!SD.begin(SDCARD_SS_PIN, SDCARD_SPI, 4000000UL))
    {
        DPRINTLN("Card Mount Failed");
        delay(1500);
        // NVIC_SystemReset();
        return;
    }
    cardType = SD.cardType();
    if (cardType == CARD_NONE)
    {
        DPRINTLN("No SD card attached");
        delay(1500);
        //NVIC_SystemReset();
        return;
    }
    cardSize = SD.cardSize() / (1024 * 1024);
    DPRINT("\nSD Card Size: ");
    DPRINT((uint32_t)cardSize);
    DPRINTLN("MB");
    DPRINTLN(F("Loading configuration from SD..."));
    delay(100);
    listDir(DEV, "/", 0);
    DPRINTLN("Sequence generation");
    writeSequence(DEV, "/TabCA.txt");
    listDir(DEV, "/", 0);

#undef DEV SD
    SD.end();

#define USESPIFLASH
#define DEV SPIFLASH
    while (!SPIFLASH.begin(104000000UL))
    {
        DPRINTLN("Card Mount Failed");
        //NVIC_SystemReset();
        return;
    }
    flashType = SPIFLASH.flashType();
    if (flashType == FLASH_NONE)
    {
        DPRINTLN("No Flash attached");
        //NVIC_SystemReset();
        return;
    }
    flashSize = SPIFLASH.flashSize() / (1024 * 1024);
    DPRINT("\nFlash Size: ");
    DPRINT((uint32_t)flashSize);
    DPRINTLN("MB");

    DPRINTLN(F("Loading configuration from Flash..."));
    delay(100);
    listDir(DEV, "/", 0);
    DPRINTLN("Sequence generation");
    writeSequence(DEV, "/TabFSH.txt");
    listDir(DEV, "/", 0);

#define DEV SD
#undef USESPIFLASH
    SPIFLASH.end();
#define USESD

    while (!SD.begin(SDCARD_SS_PIN, SDCARD_SPI, 4000000UL))
    {
        DPRINTLN("Card Mount Failed");
        delay(1500);
        // NVIC_SystemReset();
        return;
    }
    cardType = SD.cardType();
    if (cardType == CARD_NONE)
    {
        DPRINTLN("No SD card attached");
        delay(1500);
        //NVIC_SystemReset();
        return;
    }
    cardSize = SD.cardSize() / (1024 * 1024);
    DPRINT("\nSD Card Size: ");
    DPRINT((uint32_t)cardSize);
    DPRINTLN("MB");
    DPRINT(F("Loading configuration from SD..."));
    delay(100);
    listDir(DEV, "/", 0);
    DPRINTLN("Sequence generation");
    writeSequence(DEV, "/TabCA2.txt");
    listDir(DEV, "/", 0);

#undef DEV
    SD.end();
#define USESPIFLASH
#define DEV SPIFLASH
    while (!SPIFLASH.begin(104000000UL))
    {
        DPRINTLN("Card Mount Failed");
        //NVIC_SystemReset();
        return;
    }
    flashType = SPIFLASH.flashType();
    if (flashType == FLASH_NONE)
    {
        DPRINTLN("No Flash attached");
        //NVIC_SystemReset();
        return;
    }
    flashSize = SPIFLASH.flashSize() / (1024 * 1024);
    DPRINT("\nFlash Size: ");
    DPRINT((uint32_t)flashSize);
    DPRINTLN("MB");

    DPRINTLN(F("Loading configuration from Flash..."));

    delay(100);
    listDir(DEV, "/", 0);
    DPRINTLN("Sequence generation");
    writeSequence(DEV, "/TabFSH2.txt");
    listDir(DEV, "/", 0);
}

void loop()
{
}

Hi @Hansen, I’m having a similar problem as reported in the discussion between @ansonhe97 and @blanee Using SD file access and WiFi in the same sketch on Wio Terminal, i.e. as soon as I include the AtWiFi library, access to the SD files is blocked. I think it’s the same problem, if so can you tell me about the resolution of this so that I can use the new FS library the previous one was giving me problems opening two subsequent files. So now I’m stuck if I put the previous one does not work if I put the recent one another does not work.
I look forward to the resolution

Hi, I removed the Seed_Arduino_FS-1.x.x folder from the libraries folder. I then went to the link from the Wiki to load the SD libraries, and chose the original 1.0.0 release. This seems to work better with the WiFi libraries. As I am new to this platform, I have used very small sketches to try to isolate issues.
I would try writing a sketch that includes the libraries, and then accesses a file, close it, and then access the second file. Once you have that working , you can integrate it into your larger project. I hope this helps, blane

hello @blanee, yes, as I wrote with the 1.0 library, the WIFI works together with the SD reading, but with this library I have another problem if you try to write two files to the FLASH concsecutively the second corrupts the first. For this @hansen had modified the library and this type of problem was solved but the wifi problem was introduced.

Hi @Hansen, I haven’t solved the multipartition problem yet. I also tried the new “Seeed_Arduino_FS” library now it is compatible with the “rpcWiFi.h” library. But I still can’t get multipartition to work.
I used the example code “Multi_partition.ino” but below I am attaching the serial monitor and as you can see, even if I put the reference to SD in the code, only the file is written to FLASH and I can’t even read an existing file or SD on the FLASH. Can you help me figure out if it’s a bug or if I’m doing something wrong?
Thanks
Gianfranco

Code:

Click to see the code

#include <Seeed_FS.h>
#include “SFUD/Seeed_SFUD.h”
#include “SD/Seeed_SD.h”
#define SERIAL Serial

#ifdef SAMD21
#define SDCARD_SS_PIN 2
#define SDCARD_SPI SPI
#endif

void listDir(fs::FS &fs, const char *dirname, uint8_t levels)
{
SERIAL.print("Listing directory: ");
SERIAL.println(dirname);

File root = fs.open(dirname);
if (!root)
{
    SERIAL.println("Failed to open directory");
    return;
}
if (!root.isDirectory())
{
    SERIAL.println("Not a directory");
    return;
}

File file = root.openNextFile();
while (file)
{
    if (file.isDirectory())
    {
        SERIAL.print("  DIR : ");
        SERIAL.println(file.name());
        if (levels)
        {
            listDir(fs, file.name(), levels - 1);
        }
    }
    else
    {
        SERIAL.print("  FILE: ");
        SERIAL.print(file.name());
        SERIAL.print("  SIZE: ");
        SERIAL.println(file.size());
    }
    file = root.openNextFile();
}

}
void deleteFile(fs::FS &fs, const char *path)
{
SERIAL.print("Deleting file: ");
SERIAL.println(path);
if (fs.remove(path))
{
SERIAL.println(“File deleted”);
}
else
{
SERIAL.println(“Delete failed”);
}
}
void writeFile(fs::FS &fs, const char *path, const char *message)
{
SERIAL.print("Writing file: ");
SERIAL.println(path);
File file = fs.open(path, FILE_WRITE);
if (!file)
{
SERIAL.println(“Failed to open file for writing”);
return;
}

if (file.print(message))
{
    SERIAL.println("File written");
}
else
{
    SERIAL.println("Write failed");
}

file.close();

}

void readFile(fs::FS &fs, const char *path)
{
SERIAL.print("Reading Dir: ");
SERIAL.println(path);
File file = fs.open(path);
if (!file)
{
SERIAL.println(“Failed to open file for reading”);
return;
}

SERIAL.print("Read from file: ");
SERIAL.println("");
while (file.available())
{
    SERIAL.write(file.read());
}
SERIAL.println(" ");
file.close();

}

void setup()
{
SERIAL.begin(115200);
while (!SERIAL)
{
};
#ifdef SFUD_USING_QSPI
while (!SPIFLASH.begin(104000000UL))
{
SERIAL.println(“Flash Mount Failed”);
return;
}
while (!SD.begin(SDCARD_SS_PIN, SDCARD_SPI, 4000000UL))
{
SERIAL.println(“SD Card Mount Failed”);
return;
}
#else
while (!SD.begin(SDCARD_SS_PIN, SDCARD_SPI, 4000000UL))
{
SERIAL.println(“SD Card Mount Failed”);
return;
}
while (!SPIFLASH.begin(1, SPI, 4000000UL))
{
SERIAL.println(" Flash Mount Failed");
return;
}
#endif
uint8_t flashType = SPIFLASH.flashType();
if (flashType == FLASH_NONE)
{
SERIAL.println(“No Flash attached”);
return;
}
uint8_t cardType = SD.cardType();
if (cardType == CARD_NONE)
{
SERIAL.println(“No SD card attached”);
return;
}
uint8_t flashSize = SPIFLASH.flashSize() / (1024 * 1024);
SERIAL.print("Flash Size: ");
SERIAL.print((uint32_t)flashSize);
SERIAL.println(“MB”);
uint64_t cardSize = SD.cardSize() / (1024 * 1024);
SERIAL.print("SD Card Size: ");
SERIAL.print((uint32_t)cardSize);
SERIAL.println(“MB”);

uint32_t FlashtotalBytes = SPIFLASH.totalBytes();
SERIAL.print("Total space: ");
SERIAL.print(FlashtotalBytes / (1024 * 1024));
SERIAL.println("MB");
uint32_t FlasusedBytes = SPIFLASH.usedBytes();
SERIAL.print("Used space: ");
SERIAL.print(FlasusedBytes / (1024 * 1024));
SERIAL.println("MB");

uint32_t SDtotalBytes = SD.totalBytes();
SERIAL.print("Total space: ");
SERIAL.print(SDtotalBytes / (1024 * 1024));
SERIAL.println("MB");
uint32_t SDusedBytes = SD.usedBytes();
SERIAL.print("Used space: ");
SERIAL.print(SDusedBytes / (1024 * 1024));
SERIAL.println("MB");
readFile(SD, "/config.json");
writeFile(SD, "/testSD.txt", "test1");
writeFile(SPIFLASH, "/testFL.txt", "test2");
listDir(SD, "1:/", 0);
listDir(SPIFLASH, "0:/", 0);
readFile(SPIFLASH, "testFL");

}

void loop()
{
}

Serial Monitor:

Click to see the code

16:40:42.844 -> Flash Size: 4MB
16:40:42.844 -> SD Card Size: 15193MB
16:40:42.844 -> Total space: 3MB
16:40:42.844 -> Used space: 0MB
16:40:42.844 -> Total space: 2869MB
16:40:42.844 -> Used space: 0MB
16:40:42.844 -> Reading Dir: /config.json
16:40:42.879 -> Failed to open file for reading
16:40:42.879 -> Writing file: /testSD.txt
16:40:42.982 -> File written
16:40:43.190 -> Writing file: /testFL.txt
16:40:43.293 -> File written
16:40:43.467 -> Listing directory: 1:/
16:40:43.502 -> DIR : 1:/System Volume Information
16:40:43.537 -> FILE: 1:/config.json SIZE: 787
16:40:43.537 -> Listing directory: 0:/
16:40:43.572 -> FILE: 0:/testSD.txt SIZE: 5
16:40:43.572 -> FILE: 0:/testFL.txt SIZE: 5
16:40:43.572 -> Reading Dir: testFL
16:40:43.572 -> Failed to open file for reading

Hi @Hansen, have you solved the problem of managing the multipartition incompatible with the new rpcWiFi library?