Olli Vanhoja / Mbed 2 deprecated mFS_sample

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "common.h"
00002 
00003 void ls(mfs *fs);
00004 void listReservedBlocks(mfs *fs);
00005 void printStatus(char status);
00006 
00007 int main() {
00008     char data[1];
00009     char buf[44];
00010     unsigned int n;
00011 
00012     mfs fs(0xA0);
00013     pc.printf("\n\r\n\r[mFS] Formatting... %u bad block headers.\n\r", fs.mkfs(true)); // Quick format EEPROM
00014 
00015     // Mark block 2 as bad
00016     fs.write((char[]){'\x00','\x00','\x00', '\x00', '\x00'}, 2, 0, 3);
00017     pc.printf("Block 2 marked as a bad block.\n\r");
00018 
00019     fs.read(data, 0, 0, 1);
00020     pc.printf("\n\rFirst byte: %X\n\r", data[0]);
00021 
00022     pc.printf("Create file testi.txt: "); printStatus(fs.createFile("testi.txt"));
00023     pc.printf("Create file testi.txt: "); printStatus(fs.createFile("testi.txt"));
00024     pc.printf("Create file koe.txt: "); printStatus(fs.createFile("koe.txt"));
00025     pc.printf("Rm file testi.txt: "); printStatus(fs.removeFile("testi.txt"));
00026     pc.printf("Create file new.txt: "); printStatus(fs.createFile("new.txt"));
00027     
00028     // Try to write multi block file new.txt 
00029     file *fp3 = new file(&fs, "new.txt", AWRITE);
00030     pc.printf("Trying to write multi-block file new.txt...\n\r");
00031     for (n=0; n < BS; n++)
00032         fp3->write("F", 1);
00033     
00034     pc.printf("Create file testi.txt: "); printStatus(fs.createFile("testi.txt"));
00035     file *fp = new file(&fs, "testi.txt", AWRITE);
00036     pc.printf("File new.txt opened successfully\n\r");
00037     fp->write("The quick brown fox jumps over the lazy dog", 44);
00038     pc.printf("File write OK\n\r");
00039     fp->flush(); 
00040     
00041     pc.printf("Continue to write new.txt...");
00042     for (n=0; n < 400; n++)
00043         fp3->write("S", 1);
00044     pc.printf(" End\n\r");
00045     
00046     fp3->rewind();
00047     fp3->read(buf, 20);
00048     pc.printf("Rewind test (new.txt): %s\n\r", buf);
00049     pc.printf("Forwarding new.txt: "); printStatus(fp3->forward(BS-25-22));
00050     fp3->read(buf, 43);
00051     pc.printf("Forward test (new.txt): %s\n\r", buf);
00052     delete fp3;
00053 
00054     pc.printf("Create file yykaa.txt: "); printStatus(fs.createFile("yykaa.txt"));
00055 
00056     fp->rewind();
00057     fp->read(buf, 44);
00058     pc.printf("\n\rRead file testi.txt: %s\n\r", buf);
00059     delete fp;
00060 
00061     // Open read-only file
00062     file *fp2 = new file(&fs, "koe.txt", RO); /* Keep in mind that error will  *
00063                                              * be thrown if RO file is       *
00064                                               * opened in RW mode. So it's    *
00065                                               * better to check flags before  *
00066                                               * creating a handle.            */
00067     // This should fail
00068     pc.printf("Trying to write RO file koe.txt: "); printStatus(fp2->write("aa", 3));
00069     delete fp2;
00070     
00071     pc.printf("Set file flags 0x07 for file new.txt\n\r");
00072     fs.setFileFlags((char[]){'\x07'}, "new.txt");
00073 
00074     pc.printf("Rename yykaa.xt => nameChanged.txt...\n\r");
00075     fs.renameFile("yykaa.txt", "nameChanged.txt");
00076 
00077     ls(&fs);
00078     listReservedBlocks(&fs);
00079     
00080     fs.setFileFlags(0, "new.txt");
00081     file *fp4 = new file(&fs, "new.txt", DWRITE);
00082     pc.printf("Trying destructive write with new.txt... ");
00083     for (n=0; n < 3*(BS-25); n++)
00084         fp3->write("*", 1);
00085     pc.printf("End\n\r");
00086     listReservedBlocks(&fs);
00087     
00088 }
00089 
00090 /** Lists files stored in file system
00091 *
00092 * @param *fs Gets handle to file system in use.
00093 */
00094 void ls(mfs *fs) {
00095     unsigned int n, fileCount=0;
00096     char name[20];
00097     char flags[1];
00098 
00099     pc.printf("\n\rls:\n\rName:\t\tBlock:\tFlags:\n\r");
00100     n=0;
00101     while (1)
00102     {
00103         if (fs->findNextFile(n, name, &n) == 0 )
00104         {
00105             fs->getFileFlags(flags, name);
00106             if ((flags[0] & 0x2) != 0)
00107                 goto next; // Skip hidden file
00108             
00109             pc.printf("%s", name);
00110             char len = strlen(name);
00111             for (char i=0; i < 20-len; i++)
00112                 pc.printf(" ");
00113             pc.printf("%X\t%X\n\r", n, flags[0]);
00114         } else break; // Reach end of fs
00115         fileCount++;
00116         next:
00117         n++;
00118     }
00119 
00120     // Get number of free blocks
00121     char iFreeBlocks = fs->free();
00122     // Calculate amount of space used
00123     unsigned int iSpaceUsed = VOL_SIZE - (iFreeBlocks*BS);
00124     // Summary
00125     pc.printf("%u files total\n\r%u/%u kB space used, %u blocks free\n\r\n\r", fileCount, iSpaceUsed/1024, VOL_SIZE/1024, iFreeBlocks);
00126 }
00127 
00128 /** Lists blocks marked as used
00129 *
00130 * @param *fs Gets handle to file system in use.
00131 */
00132 void listReservedBlocks(mfs *fs)
00133 {
00134     char data[5];
00135     
00136     pc.printf("\n\rReserved blocks and list of related links:\n\r");
00137     for (unsigned int n=0; n < BC; n++)
00138     {
00139         fs->read(data, n, 0, 5);
00140         if (data[0] != 4)
00141             pc.printf("b%XB0: %X, %X, %X\n\r", n, data[0], data[1]<<8|data[2], data[3]<<8|data[4]);
00142     }
00143 }
00144 
00145 void printStatus(char status)
00146 {
00147     if (status == 0)
00148         pc.printf("OK\n\r");
00149     else
00150         pc.printf("Failed: %u\n\r", status);
00151 }