mFS file system library for EEPROM memory chips.
Diff: mfs.cpp
- Revision:
- 9:52c01cb100ac
- Parent:
- 7:5ac5121bb4e0
- Child:
- 10:211cb54339a0
--- a/mfs.cpp Mon Feb 21 22:41:13 2011 +0000 +++ b/mfs.cpp Tue Feb 22 18:57:37 2011 +0000 @@ -16,21 +16,12 @@ DigitalOut FlushLed(LED2); -//extern Serial pc(USBTX, USBRX); - -/* mFS ************************************************************************ - * | VOLinfo | fs | Data block 1 | etc.. * - * VOLinfo = | szVolname[256] | unsigned int nFreeBlocks | * - * fsH[n] = | RObit | LOCKbit | FEndBit | FBeingBit | (12-bit) next | * - * fsL[n] = | 4-bits unused | 12-bit address | * - *****************************************************************************/ - mfs::mfs(int i2c_address) { mem = new i2c_eeprom(i2c_address, 200000); } -char mfs::read(char *data, char block, unsigned int byte, unsigned int n) +char mfs::read(char *data, char block, uint32_t byte, uint32_t n) { // Faster reading without DEBUG mode #ifdef DEBUG @@ -42,7 +33,7 @@ return 0; } -char mfs::write(char *data, char block, unsigned int byte, unsigned int n) +char mfs::write(char *data, char block, uint32_t byte, uint32_t n) { // Faster writing without DEBUG mode #ifdef DEBUG @@ -70,9 +61,9 @@ return 0; } -unsigned int mfs::findNextFile(unsigned int block, char *filenameOut) +uint32_t mfs::findNextFile(char block, char *filenameOut) { - unsigned int i=block; + uint32_t i=block; char cFlags[1]; while (i < BC) @@ -96,9 +87,9 @@ return i; // Return block number } -uint16 mfs::getFirstBlockOfFile(char filename[20]) +uint32_t mfs::getFirstBlockOfFile(char filename[20]) { - uint16 block=0; + uint32_t block=0; char tmpFilename[20]=""; while (block < BC) @@ -119,7 +110,7 @@ char mfs::createFile(char filename[20]) { char tmpFilename[20]; - unsigned int n; + uint32_t n; char fb; for (n=0; n < BC; n++) @@ -153,7 +144,7 @@ char mfs::removeFile(char filename[20]) { - unsigned int block; + uint32_t block; char cData[3] = {'\0','\0','\0'}; // Check if file exists @@ -162,7 +153,7 @@ return 1; // File not found // Clear blocks reserver by the file - unsigned int i=0; + uint32_t i=0; char tmp_cData[2]; while(1) { @@ -188,7 +179,7 @@ char mfs::renameFile(char oldFilename[20], char newFilename[20]) { - unsigned int block; + uint32_t block; // Check if file exists block = getFirstBlockOfFile(oldFilename); @@ -205,7 +196,7 @@ /* RO|HIDDEN|LOCK * * H L */ - uint16 n; + uint32_t n; char cData[1] = {'\0'}; // Check if file exists @@ -225,7 +216,7 @@ /* RO|HIDDEN|LOCK * * H L */ - uint16 n; + uint32_t n; char cData[1] = {'\0'}; // Check if file exists @@ -240,10 +231,10 @@ } // Return number of free blocks -uint16 mfs::free() +char mfs::free() { - uint16 blocks=0; - uint16 r; + char blocks=0; + char r; char cFlags[1]; for (r=0; r < BC; r++) @@ -261,8 +252,8 @@ char mfs::mkfs(char createLabel) { unsigned int iAddr = 0; - uint16 i = 0; - uint16 bad = 0; // For counting bad block headers + char i = 0; + char bad = 0; // For counting bad block headers char cFlags[] = {'\0', '\0', '\0'}, a[1]; if (createLabel >= 1) @@ -290,13 +281,13 @@ file::file(mfs *fs_ref, char filename[20], char operation) { // Operations: - // 0 = Open RO - // 1 = Open RW + // 0 = Open in RO + // 1 = Open in RW attr = operation; fs = fs_ref; // Don't forget this :) - uint16 n; + uint32_t n; char cData[3] = {'\0','\0','\0'}; // Check if file exists @@ -329,21 +320,6 @@ flush(); } -void file::needsFlush() -{ - // Flush file if needed and return to current position - if (bufPos > 0) - { - char tmpBlock = currBlock; - unsigned int tmpPos = blockPos; - - if(flush() != 0) - error("Flush failed!"); - currBlock = tmpBlock; - blockPos = tmpPos; - } -} - void file::rewind() { flush(); @@ -351,12 +327,12 @@ blockPos = RB+20; // skip flags & pointers + filename } -char file::rewind(uint16 n) +char file::rewind(uint32_t n) { - uint16 i; + uint32_t i; char cData[3]; - needsFlush(); // Check if flush is needed + flush(); // Check if flush is needed for (i=0; i < n; i++) { @@ -389,12 +365,12 @@ return forward(1); } -char file::forward(uint16 n) +char file::forward(uint32_t n) { - uint16 i; + uint32_t i; char cData[2]; - needsFlush(); // Check if flush is needed + flush(); // Check if flush is needed for (i=0; i < n; i++) { @@ -423,12 +399,12 @@ } // Respects mEOF and automatically sets '\0' at the end of string -void file::read(char *data, unsigned int n) +void file::read(char *data, uint32_t n) { - unsigned int i; + uint32_t i; char cData[2]; - needsFlush(); + flush(); for (i=0; i < n; i++) { @@ -461,9 +437,9 @@ } // Ignores mEOF and doesn't set '\0' markings -void file::readBin(char *data, unsigned int n) +void file::readBin(char *data, uint32_t n) { - unsigned int i; + uint32_t i; char cData[2]; for (i=0; i < n; i++) @@ -490,11 +466,11 @@ } // Always binary -char file::write(char *data, unsigned int n) +char file::write(char *data, uint32_t n) { if (attr == 0) return 1; - for (unsigned int i=0; i < n; i++) + for (uint32_t i=0; i < n; i++) { // write to the buffer buffer[bufPos] = data[i]; @@ -515,7 +491,7 @@ { char cData[3], c[1]; char nextFree; - unsigned int i; + uint32_t i; if (bufPos == 0) return 0; // File up-to date if (attr == 0) return 1; @@ -534,6 +510,12 @@ if(fs->getNextFreeBlock(&nextFree) != 0) return 1; + // Take new block into use + cData[0] = 0x4C; // New flags + cData[1] = '\0'; + cData[2] = currBlock; // Prev Block + fs->write(cData, nextFree, 0, 3); // Update Block Data + // Link old block with new block fs->read(cData, currBlock, 0, 3); cData[0] &= ~0x40; // Clear LBOF flag if set @@ -541,13 +523,10 @@ cData[2] = '\0'; fs->write(cData, currBlock, 0, 3); // Update Block Data - // Take new block into use + // Update current block info + currBlock = nextFree; blockPos = RB; // Reset block position offset - cData[0] = 0x4C; // New flags - cData[1] = '\0'; - cData[2] = currBlock; // Prev Block - currBlock = nextFree; // Change current block to new one - fs->write(cData, currBlock, 0, 3); // Update Block Data + } if (blockPos < 3)