Examples for mFS.

Dependencies:   mbed

Committer:
HBP
Date:
Tue Feb 22 21:40:00 2011 +0000
Revision:
2:a608de004dcf
Parent:
1:1d0e27aaee62
Child:
3:fa4e0fea475b
Latest examples

Who changed what in which revision?

UserRevisionLine numberNew contents of line
HBP 0:6b88a0d4bab8 1 #include "common.h"
HBP 0:6b88a0d4bab8 2
HBP 0:6b88a0d4bab8 3 void ls(mfs *fs);
HBP 1:1d0e27aaee62 4 void listReservedBlocks(mfs *fs);
HBP 2:a608de004dcf 5 void printStatus(char status);
HBP 0:6b88a0d4bab8 6
HBP 0:6b88a0d4bab8 7 int main() {
HBP 0:6b88a0d4bab8 8 char data[1];
HBP 1:1d0e27aaee62 9 char buf[44];
HBP 0:6b88a0d4bab8 10 unsigned int n;
HBP 0:6b88a0d4bab8 11
HBP 0:6b88a0d4bab8 12 mfs fs(0xA0);
HBP 2:a608de004dcf 13 pc.printf("\n\r\n\r[mFS] Formatting... %u bad block headers.\n\r", fs.mkfs(true)); // Quick format EEPROM
HBP 0:6b88a0d4bab8 14
HBP 0:6b88a0d4bab8 15 // Mark block 2 as bad
HBP 2:a608de004dcf 16 fs.write((char[]){'\x00','\x00','\x00'}, 2, 0, 3);
HBP 0:6b88a0d4bab8 17 pc.printf("Block 2 marked as a bad block.\n\r");
HBP 0:6b88a0d4bab8 18
HBP 0:6b88a0d4bab8 19 fs.read(data, 0, 0, 1);
HBP 0:6b88a0d4bab8 20 pc.printf("\n\rFirst byte: %X\n\r", data[0]);
HBP 0:6b88a0d4bab8 21
HBP 2:a608de004dcf 22 pc.printf("Create file testi.txt: "); printStatus(fs.createFile("testi.txt"));
HBP 2:a608de004dcf 23 pc.printf("Create file testi.txt: "); printStatus(fs.createFile("testi.txt"));
HBP 2:a608de004dcf 24 pc.printf("Create file koe.txt: "); printStatus(fs.createFile("koe.txt"));
HBP 2:a608de004dcf 25 pc.printf("Rm file testi.txt: "); printStatus(fs.removeFile("testi.txt"));
HBP 2:a608de004dcf 26 pc.printf("Create file new.txt: "); printStatus(fs.createFile("new.txt"));
HBP 1:1d0e27aaee62 27
HBP 1:1d0e27aaee62 28 // Try to write multi block file new.txt
HBP 1:1d0e27aaee62 29 file *fp3 = new file(&fs, "new.txt", 1);
HBP 1:1d0e27aaee62 30 pc.printf("Trying to write multi block file new.txt...\n\r");
HBP 1:1d0e27aaee62 31 for (n=0; n < (BS-4)-20-1; n++)
HBP 1:1d0e27aaee62 32 fp3->write("b", 1);
HBP 1:1d0e27aaee62 33
HBP 2:a608de004dcf 34 pc.printf("Create file testi.txt: "); printStatus(fs.createFile("testi.txt"));
HBP 0:6b88a0d4bab8 35 file *fp = new file(&fs, "testi.txt", 1);
HBP 1:1d0e27aaee62 36 pc.printf("File new.txt opened successfully\n\r");
HBP 0:6b88a0d4bab8 37 fp->write("The quick brown fox jumps over the lazy dog", 44);
HBP 0:6b88a0d4bab8 38 pc.printf("File write OK\n\r");
HBP 1:1d0e27aaee62 39 fp->flush();
HBP 1:1d0e27aaee62 40
HBP 2:a608de004dcf 41 pc.printf("Continue to write new.txt...");
HBP 1:1d0e27aaee62 42 for (n=0; n < 40; n++)
HBP 1:1d0e27aaee62 43 fp3->write("u", 1);
HBP 1:1d0e27aaee62 44 pc.printf(" End\n\r");
HBP 1:1d0e27aaee62 45
HBP 1:1d0e27aaee62 46 fp3->rewind();
HBP 1:1d0e27aaee62 47 fp3->read(buf, 1);
HBP 2:a608de004dcf 48 pc.printf("Rewind test (new.txt): %s\n\r", buf);
HBP 1:1d0e27aaee62 49 fp3->forward(BS-20);
HBP 1:1d0e27aaee62 50 fp3->read(buf, 1);
HBP 2:a608de004dcf 51 pc.printf("Forward test (new.txt): %s\n\r", buf);
HBP 1:1d0e27aaee62 52 delete fp3;
HBP 0:6b88a0d4bab8 53
HBP 2:a608de004dcf 54 pc.printf("Create file ykaa.txt: "); printStatus(fs.createFile("yykaa.txt"));
HBP 0:6b88a0d4bab8 55
HBP 0:6b88a0d4bab8 56 fp->rewind();
HBP 0:6b88a0d4bab8 57 fp->read(buf, 44);
HBP 1:1d0e27aaee62 58 pc.printf("\n\rRead file testi.txt: %s\n\r", buf);
HBP 0:6b88a0d4bab8 59 delete fp;
HBP 0:6b88a0d4bab8 60
HBP 0:6b88a0d4bab8 61 // Open read-only file
HBP 2:a608de004dcf 62 file *fp2 = new file(&fs, "koe.txt", 0); /* Keep in mind that error will *
HBP 1:1d0e27aaee62 63 * be thrown if RO file is *
HBP 0:6b88a0d4bab8 64 * opened in RW mode. So it's *
HBP 0:6b88a0d4bab8 65 * better to check flags before *
HBP 0:6b88a0d4bab8 66 * creating a handle. */
HBP 0:6b88a0d4bab8 67 // This should fail
HBP 2:a608de004dcf 68 pc.printf("Trying to write RO file koe.txt: "); printStatus(fp2->write("aa", 3));
HBP 0:6b88a0d4bab8 69 delete fp2;
HBP 0:6b88a0d4bab8 70
HBP 1:1d0e27aaee62 71 pc.printf("Set file flags 0x07 for file new.txt\n\r");
HBP 1:1d0e27aaee62 72 fs.setFileFlags((char[]){'\x07'}, "new.txt");
HBP 1:1d0e27aaee62 73
HBP 2:a608de004dcf 74 pc.printf("Rename yykaa.xt => nameChanged.txt...\n\r");
HBP 2:a608de004dcf 75 fs.renameFile("yykaa.txt", "nameChanged.txt");
HBP 2:a608de004dcf 76
HBP 1:1d0e27aaee62 77 ls(&fs);
HBP 1:1d0e27aaee62 78 listReservedBlocks(&fs);
HBP 0:6b88a0d4bab8 79 }
HBP 0:6b88a0d4bab8 80
HBP 1:1d0e27aaee62 81 /** Lists files stored in file system
HBP 1:1d0e27aaee62 82 *
HBP 1:1d0e27aaee62 83 * @param *fs Gets handle to file system in use.
HBP 1:1d0e27aaee62 84 */
HBP 0:6b88a0d4bab8 85 void ls(mfs *fs) {
HBP 0:6b88a0d4bab8 86 unsigned int n, fileCount=0;
HBP 0:6b88a0d4bab8 87 char name[20];
HBP 1:1d0e27aaee62 88 char flags[1];
HBP 0:6b88a0d4bab8 89
HBP 2:a608de004dcf 90 pc.printf("\n\rls:\n\rName:\t\tBlock:\tFlags:\n\r");
HBP 0:6b88a0d4bab8 91 n=0;
HBP 0:6b88a0d4bab8 92 while (1)
HBP 0:6b88a0d4bab8 93 {
HBP 2:a608de004dcf 94 if (fs->findNextFile(n, name, &n) == 0 )
HBP 0:6b88a0d4bab8 95 {
HBP 1:1d0e27aaee62 96 fs->getFileFlags(flags, name);
HBP 1:1d0e27aaee62 97 if ((flags[0] & 0x2) != 0)
HBP 1:1d0e27aaee62 98 goto next; // Skip hidden file
HBP 1:1d0e27aaee62 99
HBP 0:6b88a0d4bab8 100 pc.printf("%s", name);
HBP 0:6b88a0d4bab8 101 char len = strlen(name);
HBP 2:a608de004dcf 102 for (char i=0; i < 20-len; i++)
HBP 2:a608de004dcf 103 pc.printf(" ");
HBP 0:6b88a0d4bab8 104 pc.printf("%X\t%X\n\r", n, flags[0]);
HBP 1:1d0e27aaee62 105 } else break; // Reach end of fs
HBP 1:1d0e27aaee62 106 next:
HBP 0:6b88a0d4bab8 107 n++;
HBP 0:6b88a0d4bab8 108 fileCount++;
HBP 0:6b88a0d4bab8 109 }
HBP 0:6b88a0d4bab8 110
HBP 0:6b88a0d4bab8 111 // Get number of free blocks
HBP 0:6b88a0d4bab8 112 char iFreeBlocks = fs->free();
HBP 0:6b88a0d4bab8 113 // Calculate amount of space used
HBP 0:6b88a0d4bab8 114 unsigned int iSpaceUsed = VOL_SIZE - (iFreeBlocks*BS);
HBP 0:6b88a0d4bab8 115 // Summary
HBP 2:a608de004dcf 116 pc.printf("%u files total\n\r%u/%u kB space used, %u blocks free\n\r\n\r", fileCount-1, iSpaceUsed/1024, VOL_SIZE/1024, iFreeBlocks);
HBP 0:6b88a0d4bab8 117 }
HBP 0:6b88a0d4bab8 118
HBP 1:1d0e27aaee62 119 /** Lists blocks marked as used
HBP 1:1d0e27aaee62 120 *
HBP 1:1d0e27aaee62 121 * @param *fs Gets handle to file system in use.
HBP 1:1d0e27aaee62 122 */
HBP 1:1d0e27aaee62 123 void listReservedBlocks(mfs *fs)
HBP 0:6b88a0d4bab8 124 {
HBP 0:6b88a0d4bab8 125 char data[3];
HBP 0:6b88a0d4bab8 126
HBP 1:1d0e27aaee62 127 pc.printf("\n\rReserved blocks:\n\r");
HBP 0:6b88a0d4bab8 128 for (unsigned int n=0; n < BC; n++)
HBP 0:6b88a0d4bab8 129 {
HBP 0:6b88a0d4bab8 130 fs->read(data, n, 0, 3);
HBP 0:6b88a0d4bab8 131 if (data[0] != 4)
HBP 0:6b88a0d4bab8 132 pc.printf("b%XB0: %X, %X, %X\n\r", n, data[0], data[1], data[2]);
HBP 0:6b88a0d4bab8 133 }
HBP 2:a608de004dcf 134 }
HBP 2:a608de004dcf 135
HBP 2:a608de004dcf 136 void printStatus(char status)
HBP 2:a608de004dcf 137 {
HBP 2:a608de004dcf 138 if (status == 0)
HBP 2:a608de004dcf 139 pc.printf("OK\n\r");
HBP 2:a608de004dcf 140 else
HBP 2:a608de004dcf 141 pc.printf("Failed: %u\n\r", status);
HBP 0:6b88a0d4bab8 142 }