Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
- Committer:
- WiredHome
- Date:
- 2016-03-13
- Revision:
- 4:be096a44f789
- Parent:
- 3:a72f30142261
- Child:
- 6:ea80c5a132b6
File content as of revision 4:be096a44f789:
///
/// MultiFileSystem
///
/// This supports both USB and SD File systems simultaneously.
///
///
#include "mbed.h"
#include "MSCFileSystem.h"
#include "SDFileSystem.h"
MSCFileSystem fs("fs");
SDFileSystem sd(p5, p6, p7, p8, "sd", p11, SDFileSystem::SWITCH_NEG_NO, 15000000);
DigitalOut led(LED1);
AnalogIn pot1(p19);
Timer timer;
char buffer[4096];
#define TESTSIZE 1048576
//#define TESTSIZE 100
void sdwriteTest(const char * fname)
{
//Test write performance by creating a 1MB file
printf("\r\nsdwriteTest %iB write performance on %s...\r\n", sizeof(buffer), fname);
FileHandle* file = sd.open(fname, O_WRONLY | O_CREAT | O_TRUNC);
if (file != NULL) {
timer.start();
for (int i = 0; i < (TESTSIZE / sizeof(buffer)); i++) {
if (file->write(buffer, sizeof(buffer)) != sizeof(buffer)) {
timer.stop();
printf(" write error!\r\n");
timer.reset();
return;
}
}
timer.stop();
if (file->close())
printf(" failed to close file!\r\n");
else
printf(" done!\r\n Result: %.2fKB/s\r\n", 1024 / (timer.read_us() / 1000000.0));
timer.reset();
} else {
printf(" failed to create file!\r\n");
}
}
void sdreadTest(const char * fname)
{
//Test read performance by reading the 1MB file created by writeTest()
printf("\r\nsdreadTest %iB read performance...\r\n", sizeof(buffer));
FileHandle* file = sd.open(fname, O_RDONLY);
if (file != NULL) {
timer.start();
int iterations = 0;
while (file->read(buffer, sizeof(buffer)) == sizeof(buffer))
iterations++;
timer.stop();
printf(" fname: %s\r\n", fname);
if (iterations != (TESTSIZE / sizeof(buffer)))
printf(" read error!\r\n");
else if (file->close())
printf(" failed to close file!\r\n");
else if (sd.remove(fname))
printf(" failed to delete file!\r\n");
else
printf(" done!\r\n Result: %.2fKB/s\r\n", 1024 / (timer.read_us() / 1000000.0));
timer.reset();
} else {
printf(" failed to open file!\n");
}
}
// ====================================================
void writeTest2(const char * fqfname)
{
//Test write performance by creating a 1MB file
printf("\r\nwriteTest2 %iB write performance on %s...\r\n", sizeof(buffer), fqfname);
FILE * file = fopen(fqfname, "w");
if (file != NULL) {
timer.start();
for (int i = 0; i < (TESTSIZE / sizeof(buffer)); i++) {
if (fwrite(buffer, 1, sizeof(buffer), file) != sizeof(buffer)) {
timer.stop();
printf(" write error!\r\n");
timer.reset();
return;
}
}
timer.stop();
if (fclose(file))
printf(" failed to close file!\r\n");
else
printf(" done!\r\n Result: %.2fKB/s\r\n", 1024 / (timer.read_us() / 1000000.0));
timer.reset();
} else {
printf(" failed to create file!\r\n");
}
}
void readTest2(const char * fqfname)
{
//Test read performance by reading the 1MB file created by writeTest()
printf("\r\nreadTest2 %i B read performance on %s...\r\n", sizeof(buffer), fqfname);
FILE * fh = fopen(fqfname, "r");
if (fh) {
timer.start();
int iterations = 0;
while (fread(buffer, 1, sizeof(buffer), fh) == sizeof(buffer))
iterations++;
timer.stop();
if (iterations != (TESTSIZE / sizeof(buffer)))
printf(" read error! on iteration %d\r\n", iterations);
else if (fclose(fh))
printf(" failed to close file!\r\n");
else if (remove(fqfname))
printf(" failed to delete file!\r\n");
else
printf(" done!\r\n Result: %.2fKB/s\r\n", 1024 / (timer.read_us() / 1000000.0));
timer.reset();
} else {
printf(" failed to open file!\r\n");
}
}
// ====================================================
int main()
{
printf("\r\n\r\n\r\n\r\n");
printf("FileSys-Multi Test. Build " __DATE__ " " __TIME__ "\r\n");
//Configure CRC, large frames, and write validation
sd.crc(true);
sd.large_frames(true);
sd.write_validation(true);
//Fill the buffer with random data for the write test
srand(time(NULL));
for (int i = 0; i < sizeof(buffer); i++)
buffer[i] = rand();
bool success = false;
do {
wait(0.5);
if (!sd.card_present()) {
printf("\r\nNo SD card present!\r\n");
continue;
}
printf("\r\nMounting SD card...\r\n");
if (sd.mount() != 0) {
printf("failed to mount SD!\r\n");
continue;
}
printf("success!\r\n");
printf("\tCard type: ");
SDFileSystem::CardType cardType = sd.card_type();
if (cardType == SDFileSystem::CARD_NONE)
printf("None\r\n");
else if (cardType == SDFileSystem::CARD_MMC)
printf("MMC\r\n");
else if (cardType == SDFileSystem::CARD_SD)
printf("SD\r\n");
else if (cardType == SDFileSystem::CARD_SDHC)
printf("SDHC\r\n");
else
printf("Unknown\r\n");
printf("\r\nMounting FS card...\r\n");
if (fs.mount() != 0) {
printf("failed to mount FS!\r\n");
continue;
}
printf("success!\r\n");
//Display the card capacity
printf("\tSectors: %u\r\n", sd.disk_sectors());
printf("\tCapacity: %.1fMB\r\n", sd.disk_sectors() / 2048.0);
writeTest2("/fs/fsfile1.bin");
readTest2("/fs/fsfile1.bin");
writeTest2("/sd/sdfile1.bin");
readTest2("/sd/sdfile1.bin");
sdwriteTest("sdfile2.bin");
sdreadTest("sdfile2.bin");
writeTest2("/fs/fsfile2.bin");
readTest2("/fs/fsfile2.bin");
writeTest2("/fs/fsfinal.txt");
writeTest2("/sd/sdfinal.txt");
//Unmount the SD card
printf("unmounting now...\r\n");
sd.unmount();
fs.unmount();
success = true;
} while (!success);
}
