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

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