Skip to content

Instantly share code, notes, and snippets.

@xeecos
Last active November 16, 2022 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xeecos/5491465c9df38960d645a5a5fd8940f8 to your computer and use it in GitHub Desktop.
Save xeecos/5491465c9df38960d645a5a5fd8940f8 to your computer and use it in GitHub Desktop.
#include "math.h"
#include "FS.h"
#include "SD_MMC.h"
void testFileIO(fs::FS &fs, const char *path, uint32_t buffSize, uint32_t numMB)
{
uint8_t *buff = (uint8_t *)ps_malloc(buffSize);
File file = fs.open(path, FILE_WRITE);
if (file)
{
size_t i;
uint32_t start = millis();
auto numToWrite = (numMB * 1024 * 1024) / buffSize;
for (i = 0; i < numToWrite; i++)
{
file.write(buff, buffSize);
yield();
}
uint32_t end = millis() - start;
float kbps = numMB * 1024 * 1024 / end;
USBSerial.printf("%u MB written using %d byte buffer for %u ms @ %.1f KBps\n", numMB, buffSize, end, kbps);
file.close();
}
else
{
USBSerial.println("Failed to open file for writing");
}
file = fs.open(path);
if (file)
{
uint32_t len = file.size();
size_t flen = len;
uint32_t start = millis();
while (len)
{
size_t toRead = len;
if (toRead > buffSize)
{
toRead = buffSize;
}
file.read(buff, toRead);
len -= toRead;
}
uint32_t end = millis() - start;
float kbps = numMB * 1024 * 1024 / end;
USBSerial.printf("%u MB read using %d byte buffer for %u ms @ %.1f KBps\n", flen / 1024 / 1024, buffSize, end, kbps);
file.close();
}
else
{
USBSerial.println("Failed to open file for reading");
}
delete[] buff;
}
void setup()
{
USBSerial.begin(115200);
SD_MMC.setPins(14, 13, 21, 41, 42, 12);
if (!SD_MMC.begin("/sd", false, false, 20000))
{
USBSerial.println("Card Mount Failed");
return;
}
for (int i = 0; i <= 7; i++)
{
testFileIO(SD_MMC, "/test.txt", 512 * pow(2, i), 8);
}
}
void loop()
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment