SD_Test problems

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