mFS file system library for EEPROM memory chips.

Revision:
5:a0fe74dce80d
Parent:
4:c77812997c9c
Child:
6:dd3346914d42
--- a/mfs.h	Mon Feb 21 09:53:32 2011 +0000
+++ b/mfs.h	Mon Feb 21 18:26:27 2011 +0000
@@ -1,3 +1,4 @@
+/** @file mfs.h */ 
 /*H****************************************************************************
 * FILENAME :        mfs.h                                                     *
 *                                                                             *
@@ -19,9 +20,13 @@
 *
 * CHANGES :
 *
-* VERSION DATE       WHO             DETAIL
-* 0.1     2011-02-21 Olli Vanhoja    Initial release version
-* 0.2     2011-02-21 Olli Vanhoja    Documentational comments added
+* VERSION DATE       WHO            DETAIL
+* 0.1     2011-02-21 Olli Vanhoja   Initial release version
+* 0.2     2011-02-21 Olli Vanhoja   Documentational comments added
+* 0.3     2011-02-21 Olli Vanhoja   *File::read issues fixed, rewind/forward
+*                                    functions improved
+*                                   *Added possibility change I2C speed
+*                                   *I2C autoreset on failure
 *
 *H*/
 
@@ -30,11 +35,11 @@
 
 #include "i2c_eeprom.h"
 
-const unsigned int VOL_SIZE=40960; // 40 kB EEPROM chip
-const unsigned int BS=4096; // 4096 bytes per block
+const unsigned int VOL_SIZE=65536; /** EEPROM chip size in bytes */
+const unsigned int BS=1024; /** How many bytes per block (default: 4096 bytes) */
 const unsigned int BC=VOL_SIZE / BS; // 128 blocks
 const char mEOF='\x01'; // End Of File/Section marked
-const unsigned int BUF=400; // file buffer length
+const unsigned int BUF=400; /** File buffer length */
 
 typedef unsigned short int uint16;
 
@@ -51,6 +56,7 @@
     * @param xi2c_address a Physical I2C address of the EEPROM chip  
     */
     mfs(int i2c_address);
+    
     /** Reads data from specified fs block
     *
     * @param *data Pointer for readed data
@@ -59,7 +65,8 @@
     * @param n Bytes to be read.
     * @returns Error code. 0 = OK, 1 = Incorrect input
     */
-    char read(char *data, char block, unsigned int byte, unsigned int n); // Read bytes from block
+    char read(char *data, char block, unsigned int byte, unsigned int n);
+    
     /** Writes data to specified fs block
     *
     * @param *data Pointer for readed data
@@ -68,26 +75,30 @@
     * @param n Bytes to be read.
     * @returns Error code. 0 = OK, 1 = Incorrect input
     */
-    char write(char *data, char block, unsigned int byte, unsigned int n); // write bytes to block
+    char write(char *data, char block, unsigned int byte, unsigned int n);
+    
     /** Locate next free block
     *
     * @param *blockOut Returns next free block from begining of the fs.
     * @returns Error code. 0 = OK, 1 = Out of space
     */
     char getNextFreeBlock(char *blockOut);
+    
     /** Locates next starting file from parameter block
     *
     * @param block Start scanning from this block.
     * @param *filenameOut Return name of the file found.
     * @returns Block number of the file found or 0xFFFF to indicate empty file system.
     */
-    unsigned int findNextFile(unsigned int block, char *filenameOut); // Returns block number
+    unsigned int findNextFile(unsigned int block, char *filenameOut);
+    
     /** Get number of the first block of the given file. (FBOF flag)
     *
     * @param filename[20] Filename input.
     * @returns Block number of the file or 0xFFFF to indicate that file not found.
     */
     uint16 getFirstBlockOfFile(char filename[20]);
+    
     /** Create a new empty file
     *
     * Reserves one block for the file created.
@@ -96,12 +107,14 @@
     * @returns Error code. 0 = OK, 1 = File exists already, 2 = Out of space
     */
     char createFile(char filename[20]);
+    
     /** Remove file from file system
     *
     * @param filename[20] Filename input.
     * @returns Error code. 0 = OK, 1 = File doesn't exists
     */
     char removeFile(char filename[20]);
+    
     /** Set user modifiable flags.
     *
     * desc RO|HIDDEN|LOCK
@@ -112,6 +125,7 @@
     * @returns Error code. 0 = OK, 1 = File doesn't exists, 2 = File system is corrupted
     */
     char setFileFlags(char *flags, char filename[20]);
+    
     /** Read user modifiable flags.
     *
     * desc RO|HIDDEN|LOCK
@@ -122,11 +136,13 @@
     * @returns Error code. 0 = OK, 1 = File doesn't exists
     */
     char getFileFlags(char *flags, char filename[20]);
+    
     /** Get number of free blocks
     *
     * @returns Number of free blocks.
     */
     uint16 free();
+    
     /** Format new file system
     *
     *
@@ -138,7 +154,7 @@
 
 /** mFS File handle class
  * 
- * This class is used as a handle for the files in mFS.
+ * This class is used as a handle for files stored in mFS.
  */
 class file {
 private:
@@ -159,25 +175,45 @@
     * @param operation 0 = read only, 1 = read and write. If read only file is opened in rw mode system will trip to error().
     */
     file(mfs *fs_ref, char filename[20], char operation);
+    
     /** Close file handle
     *
     * Flushes the file and closes the handle.
     */
     ~file(); // Close file handle and flush
+    
     /** Rewind to the start postion of the file
+    *
     */
     void rewind();
-    /** Forward one block
+    
+    /** Rewind n bytes back
+    *
+    * @param n Number of blocks to be rewinded.
+    * @returns Error code. 0 = OK, 1 = First block of file.
+    */
+    char rewind(uint16 n);
+    
+    /** Forward one byte
+    *
+    * @returns Error code. 0 = OK, 1 = End of file.
     */
     char forward();
-    //char seek(unsigned int byte); // seek to the byte
+    
+    /** Forward n bytes
+    *
+    * @param n Number of blocks.
+    * @returns Error code. 0 = OK, 1 = End of file.
+    */
+    char forward(uint16 n);
+    
     /** Reads a string of bytes
     *
     * Always places '\0' at the end of string.
     *
     * @param data Output buffer.
     * @param n Number of bytes to be read.
-    @returns Error code. 0 = OK, 1 = Last block of the file
+    * @returns Error code. 0 = OK, 1 = Last block of the file
     */
     void read(char *data, unsigned int n);
     /** Reads a binary array of bytes
@@ -187,6 +223,7 @@
     * @param data Output buffer.
     * @param n Number of bytes to be read.
     */
+    
     void readBin(char *data, unsigned int n);
     /** Write byte array to a file (buffer)
     *
@@ -194,7 +231,9 @@
     * @param n Number of bytes to be read.
     * @returns Error code. 0 = OK, 1 = Flush failed.
     */
+    
     char write(char *data, unsigned int n);
+    
     /** Flush file buffer
     * Writes buffer to the EEPROM chip in use.
     */