mFS file system library for EEPROM memory chips.

Revision:
5:a0fe74dce80d
Parent:
0:cbf45dde2b49
Child:
7:5ac5121bb4e0
--- a/mfs.cpp	Mon Feb 21 09:53:32 2011 +0000
+++ b/mfs.cpp	Mon Feb 21 18:26:27 2011 +0000
@@ -1,18 +1,11 @@
-/*H****************************************************************************
+/*CPP**************************************************************************
 * FILENAME :        mfs.cpp                                                   *
 *                                                                             *
 * DESCRIPTION :                                                               *
 *       mFS file system implementation for mBED with external I2C EEEPROM.    *
 *                                                                             *
 * AUTHOR :    Olli Vanhoja        START DATE :    2011-02-21                  *
-*******************************************************************************
-*
-* CHANGES :
-*
-* VERSION DATE       WHO             DETAIL
-* 0.1     2011-02-21 Olli Vanhoja    Initial release version
-*
-*H*/
+******************************************************************************/
 
 #include "mbed.h"
 #include "mfs.h"
@@ -34,7 +27,7 @@
 
 mfs::mfs(int i2c_address)
 {
-    mem = new i2c_eeprom(i2c_address);
+    mem = new i2c_eeprom(i2c_address, 200000);
 }
 
 char mfs::read(char *data, char block, unsigned int byte, unsigned int n)
@@ -45,7 +38,7 @@
         return 1;
     #endif
     mem->read(BS*block+byte, n, data);
-    //wait_us(100);
+    //wait_ms(1);
     return 0;
 }
 
@@ -343,19 +336,73 @@
     blockPos = RB+20; // skip flags & pointers + filename
 }
 
+char file::rewind(uint16 n)
+{
+    uint16 i;
+    char cData[3];
+    
+    needsFlush(); // Check if flush is needed
+    
+    for (i=0; i < n; i++)
+    {
+        blockPos--;
+    
+        // Change Block?
+        if (blockPos < 3)
+        {
+            // Fetch link to next block
+            fs->read(cData, currBlock, 0, 3);
+            if (!(cData[0] & 0x80))
+            {
+                currBlock = cData[2];
+                blockPos = BS-1; // Set block postion offset at end of the block
+            } else {
+                blockPos++;
+                return 1; // This is the last block
+            }
+        }
+        fs->read(cData, currBlock, blockPos, 1);
+        if (cData[0] == mEOF)
+            return 1;
+    }
+    
+    return 0; // OK
+}
+
 char file::forward()
 {
+    return forward(1);
+}
+
+char file::forward(uint16 n)
+{
+    uint16 i;
     char cData[2];
 
-    needsFlush();
+    needsFlush(); // Check if flush is needed
+    
+    for (i=0; i < n; i++)
+    {
+        blockPos++;
     
-    // Fetch link to next block
-    fs->read(cData, currBlock, 0, 2);
-    if (!(cData[0] & 0x40))
-    {
-        currBlock = cData[1];
-        blockPos = RB; // Reset block position offset
-    } else return 1; // Last block
+        // Change Block?
+        if (blockPos > BS-1)
+        {
+            // Fetch link to next block
+            fs->read(cData, currBlock, 0, 2);
+            if (!(cData[0] & 0x40))
+            {
+                currBlock = cData[1];
+                blockPos = RB; // Reset block position offset
+            } else {
+                blockPos--;
+                return 1; // This is the last block
+            }
+        }
+        fs->read(cData, currBlock, blockPos, 1);
+        if (cData[0] == mEOF)
+            return 1;
+    }
     
     return 0; // OK
 }
@@ -389,11 +436,13 @@
             stop:
             data[i]='\0';
             return;
-        } else data[i] = cData[0];
-        
-        blockPos++;
+        } else {
+            data[i] = cData[0];
+            blockPos++;
+        }
     }
-    //data[i-1] = '\0'; // Is this needed?
+    if (data[i] != '\0')
+        data[i] = '\0';
 }
 
 // Ignores mEOF and doesn't set '\0' markings
@@ -402,8 +451,6 @@
     unsigned int i;
     char cData[2];
     
-
-    
     for (i=0; i < n; i++)
     {
         // Change block?