Olli Vanhoja
/
mFS_sample
Examples for mFS.
main.cpp@3:fa4e0fea475b, 2011-02-24 (annotated)
- Committer:
- HBP
- Date:
- Thu Feb 24 00:02:58 2011 +0000
- Revision:
- 3:fa4e0fea475b
- Parent:
- 2:a608de004dcf
-16bit block number version
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:fa4e0fea475b | 16 | fs.write((char[]){'\x00','\x00','\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 | 3:fa4e0fea475b | 29 | file *fp3 = new file(&fs, "new.txt", AWRITE); |
HBP | 3:fa4e0fea475b | 30 | pc.printf("Trying to write multi-block file new.txt...\n\r"); |
HBP | 3:fa4e0fea475b | 31 | for (n=0; n < BS; n++) |
HBP | 3:fa4e0fea475b | 32 | fp3->write("F", 1); |
HBP | 1:1d0e27aaee62 | 33 | |
HBP | 2:a608de004dcf | 34 | pc.printf("Create file testi.txt: "); printStatus(fs.createFile("testi.txt")); |
HBP | 3:fa4e0fea475b | 35 | file *fp = new file(&fs, "testi.txt", AWRITE); |
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 | 3:fa4e0fea475b | 42 | for (n=0; n < 400; n++) |
HBP | 3:fa4e0fea475b | 43 | fp3->write("S", 1); |
HBP | 1:1d0e27aaee62 | 44 | pc.printf(" End\n\r"); |
HBP | 1:1d0e27aaee62 | 45 | |
HBP | 1:1d0e27aaee62 | 46 | fp3->rewind(); |
HBP | 3:fa4e0fea475b | 47 | fp3->read(buf, 20); |
HBP | 2:a608de004dcf | 48 | pc.printf("Rewind test (new.txt): %s\n\r", buf); |
HBP | 3:fa4e0fea475b | 49 | pc.printf("Forwarding new.txt: "); printStatus(fp3->forward(BS-25-22)); |
HBP | 3:fa4e0fea475b | 50 | fp3->read(buf, 43); |
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 | 3:fa4e0fea475b | 54 | pc.printf("Create file yykaa.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 | 3:fa4e0fea475b | 62 | file *fp2 = new file(&fs, "koe.txt", RO); /* 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 | 3:fa4e0fea475b | 79 | |
HBP | 3:fa4e0fea475b | 80 | fs.setFileFlags(0, "new.txt"); |
HBP | 3:fa4e0fea475b | 81 | file *fp4 = new file(&fs, "new.txt", DWRITE); |
HBP | 3:fa4e0fea475b | 82 | pc.printf("Trying destructive write with new.txt... "); |
HBP | 3:fa4e0fea475b | 83 | for (n=0; n < 3*(BS-25); n++) |
HBP | 3:fa4e0fea475b | 84 | fp3->write("*", 1); |
HBP | 3:fa4e0fea475b | 85 | pc.printf("End\n\r"); |
HBP | 3:fa4e0fea475b | 86 | listReservedBlocks(&fs); |
HBP | 3:fa4e0fea475b | 87 | |
HBP | 0:6b88a0d4bab8 | 88 | } |
HBP | 0:6b88a0d4bab8 | 89 | |
HBP | 1:1d0e27aaee62 | 90 | /** Lists files stored in file system |
HBP | 1:1d0e27aaee62 | 91 | * |
HBP | 1:1d0e27aaee62 | 92 | * @param *fs Gets handle to file system in use. |
HBP | 1:1d0e27aaee62 | 93 | */ |
HBP | 0:6b88a0d4bab8 | 94 | void ls(mfs *fs) { |
HBP | 0:6b88a0d4bab8 | 95 | unsigned int n, fileCount=0; |
HBP | 0:6b88a0d4bab8 | 96 | char name[20]; |
HBP | 1:1d0e27aaee62 | 97 | char flags[1]; |
HBP | 0:6b88a0d4bab8 | 98 | |
HBP | 2:a608de004dcf | 99 | pc.printf("\n\rls:\n\rName:\t\tBlock:\tFlags:\n\r"); |
HBP | 0:6b88a0d4bab8 | 100 | n=0; |
HBP | 0:6b88a0d4bab8 | 101 | while (1) |
HBP | 0:6b88a0d4bab8 | 102 | { |
HBP | 2:a608de004dcf | 103 | if (fs->findNextFile(n, name, &n) == 0 ) |
HBP | 0:6b88a0d4bab8 | 104 | { |
HBP | 1:1d0e27aaee62 | 105 | fs->getFileFlags(flags, name); |
HBP | 1:1d0e27aaee62 | 106 | if ((flags[0] & 0x2) != 0) |
HBP | 1:1d0e27aaee62 | 107 | goto next; // Skip hidden file |
HBP | 1:1d0e27aaee62 | 108 | |
HBP | 0:6b88a0d4bab8 | 109 | pc.printf("%s", name); |
HBP | 0:6b88a0d4bab8 | 110 | char len = strlen(name); |
HBP | 2:a608de004dcf | 111 | for (char i=0; i < 20-len; i++) |
HBP | 2:a608de004dcf | 112 | pc.printf(" "); |
HBP | 0:6b88a0d4bab8 | 113 | pc.printf("%X\t%X\n\r", n, flags[0]); |
HBP | 1:1d0e27aaee62 | 114 | } else break; // Reach end of fs |
HBP | 3:fa4e0fea475b | 115 | fileCount++; |
HBP | 1:1d0e27aaee62 | 116 | next: |
HBP | 0:6b88a0d4bab8 | 117 | n++; |
HBP | 0:6b88a0d4bab8 | 118 | } |
HBP | 0:6b88a0d4bab8 | 119 | |
HBP | 0:6b88a0d4bab8 | 120 | // Get number of free blocks |
HBP | 0:6b88a0d4bab8 | 121 | char iFreeBlocks = fs->free(); |
HBP | 0:6b88a0d4bab8 | 122 | // Calculate amount of space used |
HBP | 0:6b88a0d4bab8 | 123 | unsigned int iSpaceUsed = VOL_SIZE - (iFreeBlocks*BS); |
HBP | 0:6b88a0d4bab8 | 124 | // Summary |
HBP | 3:fa4e0fea475b | 125 | 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); |
HBP | 0:6b88a0d4bab8 | 126 | } |
HBP | 0:6b88a0d4bab8 | 127 | |
HBP | 1:1d0e27aaee62 | 128 | /** Lists blocks marked as used |
HBP | 1:1d0e27aaee62 | 129 | * |
HBP | 1:1d0e27aaee62 | 130 | * @param *fs Gets handle to file system in use. |
HBP | 1:1d0e27aaee62 | 131 | */ |
HBP | 1:1d0e27aaee62 | 132 | void listReservedBlocks(mfs *fs) |
HBP | 0:6b88a0d4bab8 | 133 | { |
HBP | 3:fa4e0fea475b | 134 | char data[5]; |
HBP | 0:6b88a0d4bab8 | 135 | |
HBP | 3:fa4e0fea475b | 136 | pc.printf("\n\rReserved blocks and list of related links:\n\r"); |
HBP | 0:6b88a0d4bab8 | 137 | for (unsigned int n=0; n < BC; n++) |
HBP | 0:6b88a0d4bab8 | 138 | { |
HBP | 3:fa4e0fea475b | 139 | fs->read(data, n, 0, 5); |
HBP | 0:6b88a0d4bab8 | 140 | if (data[0] != 4) |
HBP | 3:fa4e0fea475b | 141 | pc.printf("b%XB0: %X, %X, %X\n\r", n, data[0], data[1]<<8|data[2], data[3]<<8|data[4]); |
HBP | 0:6b88a0d4bab8 | 142 | } |
HBP | 2:a608de004dcf | 143 | } |
HBP | 2:a608de004dcf | 144 | |
HBP | 2:a608de004dcf | 145 | void printStatus(char status) |
HBP | 2:a608de004dcf | 146 | { |
HBP | 2:a608de004dcf | 147 | if (status == 0) |
HBP | 2:a608de004dcf | 148 | pc.printf("OK\n\r"); |
HBP | 2:a608de004dcf | 149 | else |
HBP | 2:a608de004dcf | 150 | pc.printf("Failed: %u\n\r", status); |
HBP | 0:6b88a0d4bab8 | 151 | } |