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()
{
}