Demo to read and write to both SD and USB File System

Dependencies:   mbed

Example (more detailed example in the main.cpp file):

#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);


// CAUTION: Return values should ALWAYS be checked. Not shown here for brevity.

int main() {
...
    FileHandle * sdfile = sd.open("test.txt", O_WRONLY | O_CREAT | O_TRUNC);
    sdfile->write("hello", 6);
    sdfile->close();
...

    FILE * file = fopen("nextest.txt", "w");
    fwrite(buffer, 1, sizeof(buffer), file);
    fclose(file);
...
    mkdir("/sd/sd_dir", 0777);
    mkdir("/fs/fs_dir", 0777);

    remove("/sd/sdfinal.log");
    rename("/sd/sdfinal.txt", "/sd/sdfinal.log");
...
}

main.cpp

Committer:
WiredHome
Date:
2016-03-13
Revision:
3:a72f30142261
Parent:
2:c887cf1c3ed9
Child:
4:be096a44f789

File content as of revision 3:a72f30142261:

#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);
}