Created
November 22, 2022 02:28
-
-
Save xeecos/60401953e7591219b7149437cb574ed3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <HWCDC.h> | |
#include "math.h" | |
#include "SPI.h" | |
HWCDC USBSerial; | |
SPIClass spi = SPIClass(FSPI); | |
#include "SdFat.h" | |
// SD_FAT_TYPE = 0 for SdFat/File as defined in SdFatConfig.h, | |
// 1 for FAT16/FAT32, 2 for exFAT, 3 for FAT16/FAT32 and exFAT. | |
#define SD_FAT_TYPE 0 | |
#define SD_CONFIG SdSpiConfig(SD_CS, DEDICATED_SPI, SD_SCK_MHZ(40), &spi) | |
SdFat sd; | |
File file; | |
void testFileIO(SdFat &fs, const char *path, uint32_t buffSize, uint32_t numMB) | |
{ | |
if(sd.exists(path)) | |
{ | |
sd.remove(path); | |
} | |
uint8_t *buff = (uint8_t *)malloc(buffSize); | |
for(int i=0; i<buffSize; i++) | |
{ | |
buff[i] = 33 + (i%10); | |
} | |
file.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.open(path,FILE_READ); | |
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(); | |
spi.begin(SD_CLK,SD_MISO,SD_MOSI,SD_CS); | |
// Wait for USB Serial | |
if (!sd.begin(SD_CONFIG)) { | |
USBSerial.println("Failed to Mount"); | |
sd.initErrorHalt(); | |
} | |
for (int i = 0; i <= 7; i++) | |
{ | |
testFileIO(sd, "/test.txt", 512 * pow(2, i), 4); | |
} | |
} | |
void loop() {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment