Escritura SD USB

Dependencies:   mbed

Fork of FileSys-MultiFileSystem by David Smart

Revision:
6:ea80c5a132b6
Parent:
4:be096a44f789
Child:
7:a487f18bd99b
--- 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;