SD_Test problems

SD_Test. I get “Card Mount Failed” many times.
So I changed the line 204 to: while(!SD.begin(SDCARD_SS_PIN, SDCARD_SPI, 16000000)){
… from Emoji_face. It works better.
I sometimes get “Card Mount Failed” and have to re-insert the card.

When I run it, Listing the root directory fails “Not a directory”.
When I make a empty directory and list it, I get a endless loop display of a file that is not there.

Code:
void setup(){
SERIAL.begin(115200);
pinMode(5, OUTPUT);
digitalWrite(5, HIGH);
while(!SERIAL){};
while(!SD.begin(SDCARD_SS_PIN, SDCARD_SPI, 16000000)){
SERIAL.println(“Card Mount Failed”);
return;
}

uint8_t cardType = SD.cardType();

if(cardType == CARD_NONE){
    SERIAL.println("No SD card attached");
    return;
}

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

SERIAL.print("SD Card Size: ");
SERIAL.print((uint32_t)cardSize);
SERIAL.println("MB");
listDir(SD, "/", 2); // Not a directory
createDir(SD, "/mydir");
//listDir(SD, "/mydir", 2); This loops endlessly.
listDir(SD, "/", 2); // Not a directory
writeFile(SD, "/mydir/mydir.txt", "Hi There");
listDir(SD, "/mydir", 2); // Works.
writeFile(SD, "/hello.txt", "Hello ");
appendFile(SD, "/hello.txt", "World!\n");
readFile(SD, "/hello.txt");
deleteFile(SD, "/foo.txt");
renameFile(SD, "/hello.txt", "/foo.txt");
readFile(SD, "/foo.txt");
removeDir(SD, "/mydir");
testFileIO(SD, "/test.txt");
SERIAL.print("Total space: ");
SERIAL.print((uint32_t)SD.totalBytes() / (1024 * 1024));
SERIAL.println("MB");
SERIAL.print("Used space: ");
SERIAL.print((uint32_t)SD.usedBytes() / (1024 * 1024));
SERIAL.println("MB");

}

Hi @mischko:
the demo regular work when I use the FAT type sd card to test it. I would say, It is very important for you to use the correct format type of SD.the The sd type is FAT32image
this is correct print when you update the example code to WIO terminal:
[18:55:46.998]收←◆SD Card Size: 15193MB
Listing directory: /
DIR : /System Volume Information
FILE: /test.txt SIZE: 0
FILE: /foo.txt SIZE: 13
Creating Dir: /mydir

[18:55:47.100]收←◆[SEEED_FS](C:\Users\59535\Documents\Arduino\libraries\Seeed_Arduino_FS\src\Seeed_FS.cpp:340) The status of mkdir : 0
[SEEED_FS](C:\Users\59535\Documents\Arduino\libraries\Seeed_Arduino_FS\src\Seeed_FS.cpp:341) more information about the status , you can view the FRESULT enum
Dir created
Listing directory: /
  DIR : /System Volume Information
  FILE: /test.txt  SIZE: 0
  FILE: /foo.txt  SIZE: 13
  DIR : /mydir
Removing Dir: /mydir
Dir removed
Listing directory: /
  DIR : /System Volume Information
Listing directory: /System Volume Information
  FILE: /System Volume Information/IndexerVolumeGuid  SIZE: 76
  FILE: /System Volume Information/WPSettings.dat  SIZE: 12
  FILE: /test.txt  SIZE: 0
  FILE: /foo.txt  SIZE: 13
Writing file: /hello.txt
[SEEED_FS](C:\Users\59535\Documents\Arduino\libraries\Seeed_Arduino_FS\src\Seeed_FS.cpp:84) The status of f_write : 0
[SEEED_FS](C:\Users\59535\Documents\Arduino\libraries\Seeed_Arduino_FS\src\Seeed_FS.cpp:85) more information about the status , you can view the FRESULT enum
File written
Appending to file: /hello.txt
[SEEED_FS](C:\Users\59535\Documents\Arduino\libraries\Seeed_Arduino_FS\src\Seeed_FS.cpp:84) The status of f_write : 0
[SEEED_FS](C:\Users\59535\Documents\Arduino\libraries\Seeed_Arduino_FS\src\Seeed_FS.cpp:85) more information about the status , you can view the FRESULT enum
Message appended
Reading Dir: /hello.txt
Read from file: Hello World!
Deleting file: /foo.txt
File deleted
Renaming file /hello.txt to /foo.txt
[SEEED_FS](C:\Users\59535\Documents\Arduino\libraries\Seeed_Arduino_FS\src\Seeed_FS.cpp:352) The status of rename : 0
[SEEED_FS](C:\Users\59535\Documents\Arduino\libraries\Seeed_Arduino_FS\src\Seeed_FS.cpp:353) more information about the status , you can view the FRESULT enum
File renamed
Reading Dir: /foo.txt
Read from file: Hello World!
0 bytes read for 0 ms
[SEEED_FS](C:\Users\59535\Documents\Arduino\libraries\Seeed_Arduino_FS\src\Seeed_FS.cpp:84) The status of f_write : 9
[SEEED_FS](C:\Users\59535\Documents\Arduino\libraries\Seeed_Arduino_FS\src\Seeed_FS.cpp:85) more information about the status , you can view the FRESULT enum
512 bytes write for 14 ms
Total space: 2897MB
Used space: 0MB

the more information you can access our wiki.

Please send me the code you used for the test.

Scott

Hi there:
this is my 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>
        #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 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 


            listDir(DEV, "/", 0);
            createDir(DEV, "/mydir");
            listDir(DEV, "/", 0);
            removeDir(DEV, "/mydir");
            listDir(DEV, "/", 2);
            writeFile(DEV, "/hello.txt", "Hello ");
            appendFile(DEV, "/hello.txt", "World!\n");
            readFile(DEV, "/hello.txt");
            deleteFile(DEV, "/foo.txt");
            renameFile(DEV, "/hello.txt", "/foo.txt");
            readFile(DEV, "/foo.txt");
            testFileIO(DEV, "/foo.txt");
            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() {
            
        }

there exist more code about this lib at our GitHub

1 Like

Thank you. This works.

Hi @Hansen, I have the same problem. I loaded your code and formatted the SD as both FAT32 and FAT but I always get the error “Card Mount Failed” I am using the latest Wio Terminal with the ATSAMD51 and compiling with Arduino IDE 1.8.12. Do you have any suggestions?

Is it an SD card compatibility issue? I haven’t met your problem in my test.

thanks @Baozhu, the SD was of the compatible type but you were right, changing SD was all right :smile:

Hi @hasen, forgive my little experience but I’m going crazy trying to read a json configuration file from SD and store it in the flash to use it on boot. I started from your TestSD example but I can’t manage reading from SD and writing to Flash at the same time. I regularly perform the operations one at a time but only by changing the #define USESPIFLASH header macros. Do you happen to have an example that can solve my problem?
I would be very grateful to you.
Thank you

@ Hansen
hello,
I used exactly your code example from post at 27 May.
And this is the only code working. None of the others (including the codes you mentioned from Github) works with mount fail message.
Card formatted as fat32
But your code works only partially. Below is the program output:

Card Size: 3819MB
Listing directory: /
Not a directory
Creating Dir: /mydir
mkdir failed
Listing directory: /
Not a directory
Removing Dir: /mydir
Dir removed
Listing directory: /
Not a directory
Writing file: /hello.txt
File written
0
Appending to file: /hello.txt
Message appended
0
Reading Dir: /hello.txt
Read from file: Hello World!
0
Deleting file: /foo.txt
File deleted
Renaming file /hello.txt to /foo.txt
File renamed
Reading Dir: /foo.txt
Read from file: Hello World!
0
13 bytes read for 1606 ns
0
Total space:3807MB
Used space: 0MB

What could be done?

I rewrite the example for more compact output and made it run in cycle.
It works with the following issues:

  1. listDir never works

  2. After approximately 30 cycles, the output stops for about a minute, then cycles
    continue but none of operation completed successfully. To restore normal operation power off/on needed.

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

    int cycles;

     void listDir(fs::FS &fs, const char *dirname, uint8_t levels) {
         Serial.printf("Listing directory: %s\n\r", 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.printf("  DIR : %s\n\r",file.name());
                 if (levels) {
                     listDir(fs, file.name(), levels - 1);
                 }
             } else {
                 Serial.printf("  FILE: %s  SIZE: %s\n\r",file.name(), file.size());
             }
             file = root.openNextFile();
         }
     }
    
     void createDir(fs::FS& fs, const char* path) {
         Serial.printf("Creating Dir: %s", path);
         if (fs.mkdir(path)) {
             Serial.println(" Dir created");
         } else {
             Serial.println(" mkdir failed");
         }
     }
    
     void removeDir(fs::FS& fs, const char* path) {
         Serial.printf("Removing Dir: %s", path);
         if (fs.rmdir(path)) {
             Serial.println(" Dir removed");
         } else {
             Serial.println(" rmdir failed");
         }
     }
    
     void readFile(fs::FS& fs, const char* path) {
         Serial.printf("Reading File: %s ", 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.printf("Writing file: %s", 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.printf("Appending to file: %s", 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.printf("Renaming file: %s to %s ", path1, path2);
         if (fs.rename(path1, path2)) {
             Serial.println(" File renamed");
         } else {
             Serial.println(" Rename failed");
         }
     }
    
     void deleteFile(fs::FS& fs, const char* path) {
         Serial.printf("Deleting file: %s ", 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.printf("%d bytes read for %d us\n\r", flen, end);
             file.close();
         } else {
             Serial.println("Failed to open file for reading");
         }
     }
    
     void setup() {
         Serial.begin(115200);
    

// pinMode(5, OUTPUT); // Do not know what for
// digitalWrite(5, HIGH);
while (!Serial) {};

        while (!SD.begin(SDCARD_SS_PIN, SDCARD_SPI, 4000000UL)) {
            Serial.println("Card Mount Failed");
			delay(1000);
        }
    }

    void loop() {
		test();
		cycles++;
		Serial.printf("Cycles %d\n\r", cycles);
		delay(1000);
    }
	
	void test() {

		uint8_t cardType = SD.cardType();
		Serial.printf("Card type: %d\n\r", cardType);
		if (cardType == CARD_NONE) {
			Serial.println("No SD card attached");
			return;
		}

		uint64_t cardSize = SD.cardSize() / (1024 * 1024);
		Serial.print("SD Card Size: ");
		Serial.print((uint32_t)cardSize);
		Serial.println("MB");

		listDir(SD, "/", 0);
		createDir(SD, "/mydir");	// Dir created		OK
		//listDir(SD, "/", 0);		// Not a directory
		removeDir(SD, "/mydir");
		//listDir(SD, "/", 2);
		writeFile(SD, "/hello.txt", "Hello ");
		appendFile(SD, "/hello.txt", "World!\n");
		readFile(SD, "/hello.txt");
		deleteFile(SD, "/foo.txt");
		renameFile(SD, "/hello.txt", "/foo.txt");
		readFile(SD, "/foo.txt");
		testFileIO(SD, "/foo.txt");
		uint32_t totalBytes = SD.totalBytes();
		Serial.printf("Total space: %d MB ", totalBytes / (1024 * 1024));
		uint32_t usedBytes = SD.usedBytes();
		Serial.printf("Used space: %d MB\n\r", usedBytes / (1024 * 1024));

}

Output:

Card type: 3
SD Card Size: 15200MB
Listing directory: /
Not a directory
Creating Dir: /mydir Dir created
Removing Dir: /mydir Dir removed
Writing file: /hello.txt File written
0
Appending to file: /hello.txt Message appended
0
Reading File: /hello.txt Read from file: Hello World!
0
Deleting file: /foo.txt File deleted
Renaming file: /hello.txt to /foo.txt File renamed
Reading File: /foo.txt Read from file: Hello World!
0
13 bytes read for 1383 us
0
Total space: 2898 MB Used space: 2 MB

Hi @Gianfry60:
there exist a demo that creates multi-partition by using flash and sd card in our github.

OK. It works with master release and I used 1.1.0

What does Pin 5 used for?

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