Demo to read and write to both SD and USB File System
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"); ... }
Revision 6:ea80c5a132b6, committed 2016-03-13
- Comitter:
- WiredHome
- Date:
- Sun Mar 13 19:21:46 2016 +0000
- Parent:
- 5:2584287350fc
- Commit message:
- Example using Multi-FileSystem (which permits access to a USB Flash and an SD card from one application).
Changed in this revision
--- a/FileSystem/FATFileSystem.lib Sun Mar 13 01:02:58 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -https://developer.mbed.org/users/WiredHome/code/FATFileSystem/#4000fad3b21f
--- a/FileSystem/MSCFileSystem.lib Sun Mar 13 01:02:58 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -https://developer.mbed.org/users/WiredHome/code/MSCFileSystem/#c65b3b101292
--- a/FileSystem/SDFileSystem.lib Sun Mar 13 01:02:58 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -https://developer.mbed.org/users/WiredHome/code/SDFileSystem/#074a43f5ba6d
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Multi-FileSystem.lib Sun Mar 13 19:21:46 2016 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/WiredHome/code/Multi-FileSystem/#9f4cd0b2fa35
--- a/main.cpp Sun Mar 13 01:02:58 2016 +0000 +++ b/main.cpp Sun Mar 13 19:21:46 2016 +0000 @@ -1,29 +1,31 @@ /// /// MultiFileSystem /// -/// This supports both USB and SD File systems simultaneously. +/// This supports both USB and SD File systems both being mounted at the same time. /// +/// Untested: +/// * live plug/unplug of either/both +/// * Concurrent access (e.g. copy from one to the other). /// #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]; +char buffer[4096]; // someplace to stuff random data. #define TESTSIZE 1048576 //#define TESTSIZE 100 -void sdwriteTest(const char * fname) + + +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); + printf("\r\n"); + printf("SDWriteTest : writing %d B using %d B buffer into %s...\r\n", TESTSIZE, sizeof(buffer), fname); FileHandle* file = sd.open(fname, O_WRONLY | O_CREAT | O_TRUNC); if (file != NULL) { timer.start(); @@ -39,17 +41,17 @@ 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)); + printf(" Result: %.2f KB/s\r\n", 1024 / (timer.read_us() / 1000000.0)); timer.reset(); } else { printf(" failed to create file!\r\n"); } } -void sdreadTest(const char * fname) +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)); + printf("\r\n"); + printf("SDReadTest : reading %d B using %d B buffer from %s...\r\n", TESTSIZE, sizeof(buffer), fname); FileHandle* file = sd.open(fname, O_RDONLY); if (file != NULL) { timer.start(); @@ -57,7 +59,6 @@ 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()) @@ -65,7 +66,7 @@ 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)); + printf(" Result: %.2f KB/s\r\n", 1024 / (timer.read_us() / 1000000.0)); timer.reset(); } else { printf(" failed to open file!\n"); @@ -74,10 +75,10 @@ // ==================================================== -void writeTest2(const char * fqfname) +void STDIOWriteTest(const char * fqfname) { - //Test write performance by creating a 1MB file - printf("\r\nwriteTest2 %iB write performance on %s...\r\n", sizeof(buffer), fqfname); + printf("\r\n"); + printf("STDIOWriteTest: writing %d B using %d B buffer into %s...\r\n", TESTSIZE, sizeof(buffer), fqfname); FILE * file = fopen(fqfname, "w"); if (file != NULL) { timer.start(); @@ -93,17 +94,17 @@ 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)); + printf(" Result: %.2f KB/s\r\n", 1024 / (timer.read_us() / 1000000.0)); timer.reset(); } else { printf(" failed to create file!\r\n"); } } -void readTest2(const char * fqfname) +void STDIOReadTest(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); + printf("\r\n"); + printf("STDIOReadTest : reading %d B using %d B buffer from %s...\r\n", TESTSIZE, sizeof(buffer), fqfname); FILE * fh = fopen(fqfname, "r"); if (fh) { timer.start(); @@ -118,7 +119,7 @@ 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)); + printf(" Result: %.2f KB/s\r\n", 1024 / (timer.read_us() / 1000000.0)); timer.reset(); } else { printf(" failed to open file!\r\n"); @@ -126,6 +127,25 @@ } +void ShowDir(char *dir, int depth = 0) +{ + DIR *dp; + struct dirent *dirp; + + dp = opendir(dir); + if (dp) { + printf("\r\nDirectory %s\r\n", dir); + ++depth; + while((dirp = readdir(dp)) != NULL) { + printf("%*s %s\r\n", (depth < 10) ? depth : 10, "", dirp->d_name); + ShowDir(dirp->d_name, depth); + } + closedir(dp); + } +} + + + // ==================================================== @@ -158,7 +178,7 @@ printf("failed to mount SD!\r\n"); continue; } - printf("success!\r\n"); + printf(" success!\r\n"); printf("\tCard type: "); SDFileSystem::CardType cardType = sd.card_type(); if (cardType == SDFileSystem::CARD_NONE) @@ -177,29 +197,76 @@ printf("failed to mount FS!\r\n"); continue; } - printf("success!\r\n"); + 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"); + STDIOWriteTest("/fs/fsfile1.bin"); + STDIOReadTest("/fs/fsfile1.bin"); + + STDIOWriteTest("/sd/sdfile1.bin"); + STDIOReadTest("/sd/sdfile1.bin"); + + SDWriteTest("sdfile2.bin"); + SDReadTest("sdfile2.bin"); - writeTest2("/sd/sdfile1.bin"); - readTest2("/sd/sdfile1.bin"); + STDIOWriteTest("/fs/fsfile2.bin"); + STDIOReadTest("/fs/fsfile2.bin"); + + STDIOWriteTest("/fs/fsfinal.txt"); + STDIOWriteTest("/sd/sdfinal.txt"); - sdwriteTest("sdfile2.bin"); - sdreadTest("sdfile2.bin"); + /// * mkdir + mkdir("/sd/sd_dir", 0777); // @TODO check the permissions mask. ignored? + mkdir("/fs/fs_dir", 0777); // @TODO check the permissions mask. ignored? + STDIOWriteTest("/sd/sd_dir/sdfile3.bin"); + STDIOWriteTest("/fs/fs_dir/fsfile3.bin"); - writeTest2("/fs/fsfile2.bin"); - readTest2("/fs/fsfile2.bin"); + /// * opendir + ShowDir("/sd"); + ShowDir("/fs"); + + /// * rename starts with an unlink + printf("\r\nrename:\r\n"); + remove("/sd/sdfinal.log"); + int rn = rename("/sd/sdfinal.txt", "/sd/sdfinal.log"); + printf(" rename(%s,%s) returned %d\r\n", "/sd/sdfinal.txt", "/sd/sdfinal.log", rn); + remove("/fs/fsfinal.log"); + rn = rename("/fs/fsfinal.txt", "/fs/fsfinal.log"); + printf(" rename(%s,%s) returned %d\r\n", "/fs/fsfinal.txt", "/fs/fsfinal.log", rn); + + /// * live plug/unplug of either/both + /// * Concurrent access (e.g. copy from one to the other). - writeTest2("/fs/fsfinal.txt"); - writeTest2("/sd/sdfinal.txt"); - + #if 0 + /// * fstat can be run against a specific object (e.g. sd.fstat) + /// but it will require deeper hooks into mbed library to get + /// the generalized form of fstat(file, &info); + printf("fstat(%s):\r\n", "sdfinal.txt"); + FILINFO info; + if (sd.fstat("sdfinal.txt", &info) == 0) { + printf(" size: %d\r\n", info.fsize); + printf(" date: %d\r\n", info.fdate); + printf(" time: %d\r\n", info.ftime); + printf(" attr: %X\r\n", info.fattrib); + printf(" name: %s\r\n", info.fname); + } + // scraps that can have value... + printf("Timestamp: %u/%02u/%02u, %02u:%02u\n", + (info.fdate >> 9) + 1980, info.fdate >> 5 & 15, info.fdate & 31, + info.ftime >> 11, info.ftime >> 5 & 63); + printf("Attributes: %c%c%c%c%c\n", + (info.fattrib & AM_DIR) ? 'D' : '-', + (info.fattrib & AM_RDO) ? 'R' : '-', + (info.fattrib & AM_HID) ? 'H' : '-', + (info.fattrib & AM_SYS) ? 'S' : '-', + (info.fattrib & AM_ARC) ? 'A' : '-'); + #endif + //Unmount the SD card - printf("unmounting now...\r\n"); + printf("\r\nunmounting all now...\r\n"); sd.unmount(); fs.unmount(); success = true;