David Smart / Mbed 2 deprecated FileSys-MultiFileSystem

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 ///
00002 /// MultiFileSystem
00003 ///
00004 /// This supports both USB and SD File systems both being mounted at the same time.
00005 ///
00006 /// Untested:
00007 /// * live plug/unplug of either/both
00008 /// * Concurrent access (e.g. copy from one to the other).
00009 ///
00010 #include "mbed.h"
00011 #include "MSCFileSystem.h"
00012 #include "SDFileSystem.h"
00013 
00014 MSCFileSystem fs("fs");
00015 SDFileSystem sd(p5, p6, p7, p8, "sd", p11, SDFileSystem::SWITCH_NEG_NO, 15000000);
00016 
00017 Timer timer;
00018 
00019 char buffer[4096];          // someplace to stuff random data.
00020 #define TESTSIZE 1048576
00021 //#define TESTSIZE 100
00022 
00023 
00024 
00025 void SDWriteTest(const char * fname)
00026 {
00027     printf("\r\n");
00028     printf("SDWriteTest   : writing %d B using %d B buffer into %s...\r\n", TESTSIZE, sizeof(buffer), fname);
00029     FileHandle* file = sd.open(fname, O_WRONLY | O_CREAT | O_TRUNC);
00030     if (file != NULL) {
00031         timer.start();
00032         for (int i = 0; i < (TESTSIZE / sizeof(buffer)); i++) {
00033             if (file->write(buffer, sizeof(buffer)) != sizeof(buffer)) {
00034                 timer.stop();
00035                 printf("  write error!\r\n");
00036                 timer.reset();
00037                 return;
00038             }
00039         }
00040         timer.stop();
00041         if (file->close())
00042             printf("  failed to close file!\r\n");
00043         else
00044             printf("  Result: %.2f KB/s\r\n", 1024 / (timer.read_us() / 1000000.0));
00045         timer.reset();
00046     } else {
00047         printf("  failed to create file!\r\n");
00048     }
00049 }
00050 
00051 void SDReadTest(const char * fname)
00052 {
00053     printf("\r\n");
00054     printf("SDReadTest    : reading %d B using %d B buffer from %s...\r\n", TESTSIZE, sizeof(buffer), fname);
00055     FileHandle* file = sd.open(fname, O_RDONLY);
00056     if (file != NULL) {
00057         timer.start();
00058         int iterations = 0;
00059         while (file->read(buffer, sizeof(buffer)) == sizeof(buffer))
00060             iterations++;
00061         timer.stop();
00062         if (iterations != (TESTSIZE / sizeof(buffer)))
00063             printf("  read error!\r\n");
00064         else if (file->close())
00065             printf("  failed to close file!\r\n");
00066         else if (sd.remove(fname))
00067             printf("  failed to delete file!\r\n");
00068         else
00069             printf("  Result: %.2f KB/s\r\n", 1024 / (timer.read_us() / 1000000.0));
00070         timer.reset();
00071     } else {
00072         printf("  failed to open file!\n");
00073     }
00074 }
00075 
00076 // ====================================================
00077 
00078 void STDIOWriteTest(const char * fqfname)
00079 {
00080     printf("\r\n");
00081     printf("STDIOWriteTest: writing %d B using %d B buffer into %s...\r\n", TESTSIZE, sizeof(buffer), fqfname);
00082     FILE * file = fopen(fqfname, "w");
00083     if (file != NULL) {
00084         timer.start();
00085         for (int i = 0; i < (TESTSIZE / sizeof(buffer)); i++) {
00086             if (fwrite(buffer, 1, sizeof(buffer), file) != sizeof(buffer)) {
00087                 timer.stop();
00088                 printf("  write error!\r\n");
00089                 timer.reset();
00090                 return;
00091             }
00092         }
00093         timer.stop();
00094         if (fclose(file))
00095             printf("  failed to close file!\r\n");
00096         else
00097             printf("  Result: %.2f KB/s\r\n", 1024 / (timer.read_us() / 1000000.0));
00098         timer.reset();
00099     } else {
00100         printf("  failed to create file!\r\n");
00101     }
00102 }
00103 
00104 void STDIOReadTest(const char * fqfname)
00105 {
00106     printf("\r\n");
00107     printf("STDIOReadTest : reading %d B using %d B buffer from %s...\r\n", TESTSIZE, sizeof(buffer), fqfname);
00108     FILE * fh = fopen(fqfname, "r");
00109     if (fh) {
00110         timer.start();
00111         int iterations = 0;
00112         while (fread(buffer, 1, sizeof(buffer), fh) == sizeof(buffer))
00113             iterations++;
00114         timer.stop();
00115         if (iterations != (TESTSIZE / sizeof(buffer)))
00116             printf("  read error! on iteration %d\r\n", iterations);
00117         else if (fclose(fh))
00118             printf("  failed to close file!\r\n");
00119         else if (remove(fqfname))
00120             printf("  failed to delete file!\r\n");
00121         else
00122             printf("  Result: %.2f KB/s\r\n", 1024 / (timer.read_us() / 1000000.0));
00123         timer.reset();
00124     } else {
00125         printf("  failed to open file!\r\n");
00126     }
00127 }
00128 
00129 
00130 void ShowDir(char *dir, int depth = 0)
00131 {
00132     DIR *dp;
00133     struct dirent *dirp;
00134     
00135     dp = opendir(dir);
00136     if (dp) {
00137         printf("\r\nDirectory %s\r\n", dir);
00138         ++depth;
00139         while((dirp = readdir(dp)) != NULL) {
00140             printf("%*s  %s\r\n", (depth < 10) ? depth : 10, "", dirp->d_name);
00141             ShowDir(dirp->d_name, depth);
00142         }
00143         closedir(dp);
00144     }
00145 }
00146 
00147 
00148 
00149 // ====================================================
00150 
00151 
00152 int main()
00153 {
00154     printf("\r\n\r\n\r\n\r\n");
00155     printf("FileSys-Multi Test. Build " __DATE__ " " __TIME__ "\r\n");
00156 
00157     //Configure CRC, large frames, and write validation
00158     sd.crc(true);
00159     sd.large_frames(true);
00160     sd.write_validation(true);
00161 
00162     //Fill the buffer with random data for the write test
00163     srand(time(NULL));
00164     for (int i = 0; i < sizeof(buffer); i++)
00165         buffer[i] = rand();
00166 
00167     bool success = false;
00168     do {
00169         wait(0.5);
00170 
00171         if (!sd.card_present()) {
00172             printf("\r\nNo SD card present!\r\n");
00173             continue;
00174         }
00175 
00176         printf("\r\nMounting SD card...\r\n");
00177         if (sd.mount() != 0) {
00178             printf("failed to mount SD!\r\n");
00179             continue;
00180         }
00181         printf("  success!\r\n");
00182         printf("\tCard type: ");
00183         SDFileSystem::CardType cardType = sd.card_type();
00184         if (cardType == SDFileSystem::CARD_NONE)
00185             printf("None\r\n");
00186         else if (cardType == SDFileSystem::CARD_MMC)
00187             printf("MMC\r\n");
00188         else if (cardType == SDFileSystem::CARD_SD)
00189             printf("SD\r\n");
00190         else if (cardType == SDFileSystem::CARD_SDHC)
00191             printf("SDHC\r\n");
00192         else
00193             printf("Unknown\r\n");
00194 
00195         printf("\r\nMounting FS card...\r\n");
00196         if (fs.mount() != 0) {
00197             printf("failed to mount FS!\r\n");
00198             continue;
00199         }
00200         printf("  success!\r\n");
00201 
00202         //Display the card capacity
00203         printf("\tSectors: %u\r\n", sd.disk_sectors());
00204         printf("\tCapacity: %.1fMB\r\n", sd.disk_sectors() / 2048.0);
00205 
00206         STDIOWriteTest("/fs/fsfile1.bin");
00207         STDIOReadTest("/fs/fsfile1.bin");
00208 
00209         STDIOWriteTest("/sd/sdfile1.bin");
00210         STDIOReadTest("/sd/sdfile1.bin");
00211 
00212         SDWriteTest("sdfile2.bin");
00213         SDReadTest("sdfile2.bin");
00214 
00215         STDIOWriteTest("/fs/fsfile2.bin");
00216         STDIOReadTest("/fs/fsfile2.bin");
00217 
00218         STDIOWriteTest("/fs/fsfinal.txt");
00219         STDIOWriteTest("/sd/sdfinal.txt");
00220 
00221         /// * mkdir
00222         mkdir("/sd/sd_dir", 0777);       // @TODO check the permissions mask. ignored?
00223         mkdir("/fs/fs_dir", 0777);       // @TODO check the permissions mask. ignored?
00224         STDIOWriteTest("/sd/sd_dir/sdfile3.bin");
00225         STDIOWriteTest("/fs/fs_dir/fsfile3.bin");
00226 
00227         /// * opendir
00228         ShowDir("/sd");
00229         ShowDir("/fs");
00230         
00231         /// * rename starts with an unlink
00232         printf("\r\nrename:\r\n");
00233         remove("/sd/sdfinal.log");
00234         int rn = rename("/sd/sdfinal.txt", "/sd/sdfinal.log");
00235         printf("  rename(%s,%s) returned %d\r\n", "/sd/sdfinal.txt", "/sd/sdfinal.log", rn);
00236         remove("/fs/fsfinal.log");
00237             rn = rename("/fs/fsfinal.txt", "/fs/fsfinal.log");
00238         printf("  rename(%s,%s) returned %d\r\n", "/fs/fsfinal.txt", "/fs/fsfinal.log", rn);
00239         
00240         /// * live plug/unplug of either/both
00241         /// * Concurrent access (e.g. copy from one to the other).
00242 
00243         #if 0
00244         /// * fstat can be run against a specific object (e.g. sd.fstat)
00245         ///   but it will require deeper hooks into mbed library to get
00246         ///   the generalized form of fstat(file, &info);
00247         printf("fstat(%s):\r\n", "sdfinal.txt");
00248         FILINFO info;
00249         if (sd.fstat("sdfinal.txt", &info) == 0) {
00250             printf("  size: %d\r\n", info.fsize);
00251             printf("  date: %d\r\n", info.fdate);
00252             printf("  time: %d\r\n", info.ftime);
00253             printf("  attr: %X\r\n", info.fattrib);
00254             printf("  name: %s\r\n", info.fname);
00255         }
00256         // scraps that can have value...
00257         printf("Timestamp: %u/%02u/%02u, %02u:%02u\n",
00258                (info.fdate >> 9) + 1980, info.fdate >> 5 & 15, info.fdate & 31,
00259                info.ftime >> 11, info.ftime >> 5 & 63);
00260         printf("Attributes: %c%c%c%c%c\n",
00261                (info.fattrib & AM_DIR) ? 'D' : '-',
00262                (info.fattrib & AM_RDO) ? 'R' : '-',
00263                (info.fattrib & AM_HID) ? 'H' : '-',
00264                (info.fattrib & AM_SYS) ? 'S' : '-',
00265                (info.fattrib & AM_ARC) ? 'A' : '-');
00266         #endif
00267         
00268         //Unmount the SD card
00269         printf("\r\nunmounting all now...\r\n");
00270         sd.unmount();
00271         fs.unmount();
00272         success = true;
00273 
00274     } while (!success);
00275 }