Hi,
I have a problem with SD_Test.ino example from “SD/Seeed_SD.h” (version 2.0.2), if I use the function “lisDir()” inside the “Loop” after about 3 iterations the Wio terminal freezes, and my Arduino IDE also. (Windows 10, Arduino IDE 1.8.13, Seeed SAMD Boards 1.8.1, Seeed_Arduino_FS-master 2.0.2 )
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
#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);
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() {
listDir(DEV, "/", 0);
delay(1000);
}
Click to see the log
Writing file: /hello.txt
File written
Appending to file: /hello.txt
Message appended
Reading Dir: /hello.txt
Read from file: Hello World!
Deleting file: /foo.txt
File deleted
Renaming file /hello.txt to /foo.txt
File renamed
Reading Dir: /foo.txt
Read from file: Hello World!
13 bytes read for 1421 ns
Total space: 2898MB
Used space: 0MB
Listing directory: /
DIR : /System Volume Information
FILE: /2.bmp SIZE: 153604
FILE: /3.bmp SIZE: 153604
FILE: /4.bmp SIZE: 153604
FILE: /1.bmp SIZE: 153604
FILE: /Flight_1.json SIZE: 94
FILE: /foo.txt SIZE: 13
Listing directory: /
DIR : /System Volume Information
FILE: /2.bmp SIZE: 153604
FILE: /3.bmp SIZE: 153604
FILE: /4.bmp SIZE: 153604
FILE: /1.bmp SIZE: 153604
FILE: /Flight_1.json SIZE: 94
FILE: /foo.txt SIZE: 13
Listing directory: /
DIR : /System Volume Information
FILE: /2.bmp SIZE: 153604
FILE: /3.bmp SIZE: 153604
FILE: /4.bmp SIZE: 153604
FILE: /1.bmp SIZE: 153604
FILE: /Flight_1.json SIZE: 94
If I use SPIFLASH (line 12: #define USESPIFLASH true), it takes about 30s before freeze:
Click to see the log with SPI
21:44:07.805 -> flash Size: 4MB
21:44:07.805 -> Listing directory: /
21:44:07.805 -> FILE: /foo.txt SIZE: 13
21:44:07.805 -> Creating Dir: /mydir
21:44:08.012 -> Dir created
21:44:08.012 -> Listing directory: /
21:44:08.012 -> DIR : /mydir
21:44:08.012 -> FILE: /foo.txt SIZE: 13
21:44:08.012 -> Removing Dir: /mydir
21:44:08.171 -> Dir removed
21:44:08.171 -> Listing directory: /
21:44:08.171 -> FILE: /foo.txt SIZE: 13
21:44:08.171 -> Writing file: /hello.txt
21:44:08.218 -> File written
21:44:08.418 -> Appending to file: /hello.txt
21:44:08.418 -> Message appended
21:44:08.572 -> Reading Dir: /hello.txt
21:44:08.572 -> Read from file: Hello World!
21:44:08.572 -> Deleting file: /foo.txt
21:44:08.672 -> File deleted
21:44:08.672 -> Renaming file /hello.txt to /foo.txt
21:44:08.773 -> File renamed
21:44:08.773 -> Reading Dir: /foo.txt
21:44:08.773 -> Read from file: Hello World!
21:44:08.773 -> 13 bytes read for 691 ns
21:44:08.773 -> Total space: 3MB
21:44:08.773 -> Used space: 0MB
21:44:08.773 -> Listing directory: /
21:44:08.773 -> FILE: /foo.txt SIZE: 13
21:44:09.776 -> Listing directory: /
21:44:09.776 -> FILE: /foo.txt SIZE: 13
21:44:10.779 -> Listing directory: /
21:44:10.779 -> FILE: /foo.txt SIZE: 13
21:44:11.782 -> Listing directory: /
21:44:11.782 -> FILE: /foo.txt SIZE: 13
21:44:12.784 -> Listing directory: /
21:44:12.784 -> FILE: /foo.txt SIZE: 13
21:44:13.787 -> Listing directory: /
21:44:13.787 -> FILE: /foo.txt SIZE: 13
21:44:14.790 -> Listing directory: /
21:44:14.790 -> FILE: /foo.txt SIZE: 13
21:44:15.792 -> Listing directory: /
21:44:15.792 -> FILE: /foo.txt SIZE: 13
21:44:16.794 -> Listing directory: /
21:44:16.794 -> FILE: /foo.txt SIZE: 13
21:44:17.797 -> Listing directory: /
21:44:17.797 -> FILE: /foo.txt SIZE: 13
21:44:18.800 -> Listing directory: /
21:44:18.800 -> FILE: /foo.txt SIZE: 13
21:44:19.803 -> Listing directory: /
21:44:19.803 -> FILE: /foo.txt SIZE: 13
21:44:20.806 -> Listing directory: /
21:44:20.806 -> FILE: /foo.txt SIZE: 13
21:44:21.809 -> Listing directory: /
21:44:21.809 -> FILE: /foo.txt SIZE: 13
21:44:22.812 -> Listing directory: /
21:44:22.812 -> FILE: /foo.txt SIZE: 13
21:44:23.816 -> Listing directory: /
21:44:23.816 -> FILE: /foo.txt SIZE: 13
21:44:24.819 -> Listing directory: /
21:44:24.819 -> FILE: /foo.txt SIZE: 13
21:44:25.821 -> Listing directory: /
21:44:25.821 -> FILE: /foo.txt SIZE: 13
21:44:26.837 -> Listing directory: /
21:44:26.837 -> FILE: /foo.txt SIZE: 13
21:44:27.840 -> Listing directory: /
21:44:27.840 -> FILE: /foo.txt SIZE: 13
21:44:28.844 -> Listing directory: /
21:44:28.844 -> FILE: /foo.txt SIZE: 13
21:44:29.847 -> Listing directory: /
21:44:29.847 -> FILE: /foo.txt SIZE: 13
21:44:30.850 -> Listing directory: /
21:44:30.850 -> FILE: /foo.txt SIZE: 13
21:44:31.852 -> Listing directory: /
21:44:31.852 -> FILE: /foo.txt SIZE: 13
21:44:32.854 -> Listing directory: /
21:44:32.854 -> FILE: /foo.txt SIZE: 13
21:44:33.857 -> Listing directory: /
21:44:33.857 -> FILE: /foo.txt SIZE: 13
21:44:34.860 -> Listing directory: /
21:44:34.860 -> FILE: /foo.txt SIZE: 13
21:44:35.864 -> Listing directory: /
21:44:35.864 -> FILE: /foo.txt SIZE: 13
21:44:36.866 -> Listing directory: /
21:44:36.866 -> FILE: /foo.txt SIZE: 13
21:44:37.869 -> Listing directory: /
21:44:37.869 -> FILE: /foo.txt SIZE: 13
21:44:38.873 -> Listing directory: /
21:44:38.873 -> FILE: /foo.txt SIZE: 13
21:44:39.874 -> Listing directory: /
21:44:39.874 -> FILE: /foo.txt SIZE: 13
21:44:40.876 -> Listing directory: /