mFS file system library for EEPROM memory chips.

Committer:
HBP
Date:
Mon Feb 21 22:41:13 2011 +0000
Revision:
8:e67733ad4427
Parent:
7:5ac5121bb4e0
Child:
9:52c01cb100ac

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
HBP 5:a0fe74dce80d 1 /** @file mfs.h */
HBP 0:cbf45dde2b49 2 /*H****************************************************************************
HBP 7:5ac5121bb4e0 3 * FILENAME : mfs.h *
HBP 7:5ac5121bb4e0 4 * *
HBP 7:5ac5121bb4e0 5 * DESCRIPTION : *
HBP 7:5ac5121bb4e0 6 * mFS file system implementation for mBED with external I2C EEEPROM. *
HBP 8:e67733ad4427 7 * *
HBP 8:e67733ad4427 8 * ---------------------------------------------------------------------------*
HBP 8:e67733ad4427 9 * "THE BEER-WARE LICENSE" (Revision 42): *
HBP 8:e67733ad4427 10 * <olli.vanhoja@gmail.com> wrote this file. As long as you retain this notice*
HBP 8:e67733ad4427 11 * you can do whatever you want with this stuff. If we meet some day, and you *
HBP 8:e67733ad4427 12 * think this stuff is worth it, you can buy me a beer in return Olli Vanhoja *
HBP 8:e67733ad4427 13 * ---------------------------------------------------------------------------*
HBP 8:e67733ad4427 14 * *
HBP 8:e67733ad4427 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
HBP 8:e67733ad4427 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
HBP 8:e67733ad4427 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
HBP 8:e67733ad4427 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
HBP 8:e67733ad4427 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
HBP 8:e67733ad4427 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
HBP 8:e67733ad4427 21 * DEALINGS IN THE SOFTWARE. *
HBP 8:e67733ad4427 22 * *
HBP 8:e67733ad4427 23 * *
HBP 8:e67733ad4427 24 * Block Flags: *
HBP 8:e67733ad4427 25 * 7:FBOF Begining of file *
HBP 8:e67733ad4427 26 * 6:LBOF Last block of file *
HBP 8:e67733ad4427 27 * 5:RO Read only file (Used only with FBOF) *
HBP 8:e67733ad4427 28 * 4:HIDDEN Hidden file (Used only with FBOF) *
HBP 8:e67733ad4427 29 * 3:INUSE Block in use *
HBP 8:e67733ad4427 30 * 2:NBAD Bad block (INV) *
HBP 8:e67733ad4427 31 * 1:VOL Volume label (Used only with FBOF) *
HBP 8:e67733ad4427 32 * 0:LOCK Locked file (Used only with FBOF) *
HBP 8:e67733ad4427 33 * *
HBP 8:e67733ad4427 34 * AUTHOR : Olli Vanhoja START DATE : 2011-02-18 *
HBP 8:e67733ad4427 35 ******************************************************************************
HBP 7:5ac5121bb4e0 36 *
HBP 7:5ac5121bb4e0 37 * CHANGES :
HBP 7:5ac5121bb4e0 38 *
HBP 7:5ac5121bb4e0 39 * VERSION DATE WHO DETAIL
HBP 7:5ac5121bb4e0 40 * 0.1 2011-02-21 Olli Vanhoja Initial release version
HBP 7:5ac5121bb4e0 41 * 0.2 2011-02-21 Olli Vanhoja Documentational comments added
HBP 7:5ac5121bb4e0 42 * 0.3 2011-02-21 Olli Vanhoja *File::read issues fixed, rewind/forward
HBP 7:5ac5121bb4e0 43 * functions improved
HBP 7:5ac5121bb4e0 44 * *Added possibility change I2C speed
HBP 7:5ac5121bb4e0 45 * *I2C autoreset on failure
HBP 7:5ac5121bb4e0 46 * 0.4 2011-02-22 Olli Vanhoja *mfs::renameFile(char [20], char [20] function added
HBP 7:5ac5121bb4e0 47 *
HBP 7:5ac5121bb4e0 48 * TODO :
HBP 7:5ac5121bb4e0 49 * Directory support (VOL labeled blocks)
HBP 7:5ac5121bb4e0 50 *
HBP 7:5ac5121bb4e0 51 *H*/
HBP 0:cbf45dde2b49 52
HBP 0:cbf45dde2b49 53 #ifndef MFS_H
HBP 0:cbf45dde2b49 54 #define MFS_H
HBP 0:cbf45dde2b49 55
HBP 0:cbf45dde2b49 56 #include "i2c_eeprom.h"
HBP 0:cbf45dde2b49 57
HBP 6:dd3346914d42 58 const unsigned int VOL_SIZE=65536; /**< EEPROM chip size in bytes */
HBP 6:dd3346914d42 59 const unsigned int BS=1024; /**< How many bytes per block (default: 4096 bytes) */
HBP 0:cbf45dde2b49 60 const unsigned int BC=VOL_SIZE / BS; // 128 blocks
HBP 0:cbf45dde2b49 61 const char mEOF='\x01'; // End Of File/Section marked
HBP 6:dd3346914d42 62 const unsigned int BUF=400; /**< File buffer length */
HBP 0:cbf45dde2b49 63
HBP 0:cbf45dde2b49 64 typedef unsigned short int uint16;
HBP 0:cbf45dde2b49 65
HBP 3:1cbc15648de1 66 /** mFS File System class
HBP 3:1cbc15648de1 67 *
HBP 3:1cbc15648de1 68 * This class is used as a handle for the fs in use.
HBP 3:1cbc15648de1 69 */
HBP 0:cbf45dde2b49 70 class mfs {
HBP 0:cbf45dde2b49 71 private:
HBP 0:cbf45dde2b49 72 i2c_eeprom *mem; // Only 512 kB I2C EEPROM is supported ATM
HBP 0:cbf45dde2b49 73 public:
HBP 3:1cbc15648de1 74 /** Create a new file system object
HBP 3:1cbc15648de1 75 *
HBP 3:1cbc15648de1 76 * @param xi2c_address a Physical I2C address of the EEPROM chip
HBP 3:1cbc15648de1 77 */
HBP 0:cbf45dde2b49 78 mfs(int i2c_address);
HBP 5:a0fe74dce80d 79
HBP 3:1cbc15648de1 80 /** Reads data from specified fs block
HBP 3:1cbc15648de1 81 *
HBP 3:1cbc15648de1 82 * @param *data Pointer for readed data
HBP 3:1cbc15648de1 83 * @param block Block number.
HBP 3:1cbc15648de1 84 * @param byte Selected byte.
HBP 3:1cbc15648de1 85 * @param n Bytes to be read.
HBP 3:1cbc15648de1 86 * @returns Error code. 0 = OK, 1 = Incorrect input
HBP 3:1cbc15648de1 87 */
HBP 5:a0fe74dce80d 88 char read(char *data, char block, unsigned int byte, unsigned int n);
HBP 5:a0fe74dce80d 89
HBP 3:1cbc15648de1 90 /** Writes data to specified fs block
HBP 3:1cbc15648de1 91 *
HBP 3:1cbc15648de1 92 * @param *data Pointer for readed data
HBP 3:1cbc15648de1 93 * @param block Block number.
HBP 3:1cbc15648de1 94 * @param byte Selected byte.
HBP 3:1cbc15648de1 95 * @param n Bytes to be read.
HBP 3:1cbc15648de1 96 * @returns Error code. 0 = OK, 1 = Incorrect input
HBP 3:1cbc15648de1 97 */
HBP 5:a0fe74dce80d 98 char write(char *data, char block, unsigned int byte, unsigned int n);
HBP 5:a0fe74dce80d 99
HBP 3:1cbc15648de1 100 /** Locate next free block
HBP 3:1cbc15648de1 101 *
HBP 3:1cbc15648de1 102 * @param *blockOut Returns next free block from begining of the fs.
HBP 3:1cbc15648de1 103 * @returns Error code. 0 = OK, 1 = Out of space
HBP 3:1cbc15648de1 104 */
HBP 3:1cbc15648de1 105 char getNextFreeBlock(char *blockOut);
HBP 5:a0fe74dce80d 106
HBP 3:1cbc15648de1 107 /** Locates next starting file from parameter block
HBP 3:1cbc15648de1 108 *
HBP 3:1cbc15648de1 109 * @param block Start scanning from this block.
HBP 3:1cbc15648de1 110 * @param *filenameOut Return name of the file found.
HBP 3:1cbc15648de1 111 * @returns Block number of the file found or 0xFFFF to indicate empty file system.
HBP 3:1cbc15648de1 112 */
HBP 5:a0fe74dce80d 113 unsigned int findNextFile(unsigned int block, char *filenameOut);
HBP 5:a0fe74dce80d 114
HBP 3:1cbc15648de1 115 /** Get number of the first block of the given file. (FBOF flag)
HBP 3:1cbc15648de1 116 *
HBP 3:1cbc15648de1 117 * @param filename[20] Filename input.
HBP 3:1cbc15648de1 118 * @returns Block number of the file or 0xFFFF to indicate that file not found.
HBP 3:1cbc15648de1 119 */
HBP 0:cbf45dde2b49 120 uint16 getFirstBlockOfFile(char filename[20]);
HBP 5:a0fe74dce80d 121
HBP 4:c77812997c9c 122 /** Create a new empty file
HBP 3:1cbc15648de1 123 *
HBP 3:1cbc15648de1 124 * Reserves one block for the file created.
HBP 3:1cbc15648de1 125 *
HBP 3:1cbc15648de1 126 * @param filename[20] Filename input.
HBP 3:1cbc15648de1 127 * @returns Error code. 0 = OK, 1 = File exists already, 2 = Out of space
HBP 3:1cbc15648de1 128 */
HBP 0:cbf45dde2b49 129 char createFile(char filename[20]);
HBP 5:a0fe74dce80d 130
HBP 3:1cbc15648de1 131 /** Remove file from file system
HBP 3:1cbc15648de1 132 *
HBP 3:1cbc15648de1 133 * @param filename[20] Filename input.
HBP 3:1cbc15648de1 134 * @returns Error code. 0 = OK, 1 = File doesn't exists
HBP 3:1cbc15648de1 135 */
HBP 0:cbf45dde2b49 136 char removeFile(char filename[20]);
HBP 5:a0fe74dce80d 137
HBP 7:5ac5121bb4e0 138 /** Rename file
HBP 7:5ac5121bb4e0 139 *
HBP 7:5ac5121bb4e0 140 * @param filename[20] Filename input.
HBP 7:5ac5121bb4e0 141 * @returns Error code. 0 = OK, 1 = File doesn't exists
HBP 7:5ac5121bb4e0 142 */
HBP 7:5ac5121bb4e0 143 char renameFile(char oldFilename[20], char newFilename[20]);
HBP 7:5ac5121bb4e0 144
HBP 3:1cbc15648de1 145 /** Set user modifiable flags.
HBP 3:1cbc15648de1 146 *
HBP 3:1cbc15648de1 147 * desc RO|HIDDEN|LOCK
HBP 3:1cbc15648de1 148 * bit 3 2 1
HBP 3:1cbc15648de1 149 *
HBP 3:1cbc15648de1 150 * @param *flags Flag input
HBP 3:1cbc15648de1 151 * @param filename[20] Filename input.
HBP 3:1cbc15648de1 152 * @returns Error code. 0 = OK, 1 = File doesn't exists, 2 = File system is corrupted
HBP 3:1cbc15648de1 153 */
HBP 0:cbf45dde2b49 154 char setFileFlags(char *flags, char filename[20]);
HBP 5:a0fe74dce80d 155
HBP 3:1cbc15648de1 156 /** Read user modifiable flags.
HBP 3:1cbc15648de1 157 *
HBP 3:1cbc15648de1 158 * desc RO|HIDDEN|LOCK
HBP 3:1cbc15648de1 159 * bit 3 2 1
HBP 3:1cbc15648de1 160 *
HBP 3:1cbc15648de1 161 * @param *flags Flag output
HBP 3:1cbc15648de1 162 * @param filename[20] Filename input.
HBP 3:1cbc15648de1 163 * @returns Error code. 0 = OK, 1 = File doesn't exists
HBP 3:1cbc15648de1 164 */
HBP 0:cbf45dde2b49 165 char getFileFlags(char *flags, char filename[20]);
HBP 5:a0fe74dce80d 166
HBP 3:1cbc15648de1 167 /** Get number of free blocks
HBP 3:1cbc15648de1 168 *
HBP 3:1cbc15648de1 169 * @returns Number of free blocks.
HBP 3:1cbc15648de1 170 */
HBP 0:cbf45dde2b49 171 uint16 free();
HBP 5:a0fe74dce80d 172
HBP 3:1cbc15648de1 173 /** Format new file system
HBP 3:1cbc15648de1 174 *
HBP 3:1cbc15648de1 175 *
HBP 3:1cbc15648de1 176 * @param createLabel Create volume label at the begining of the file system. (there is no specified use for volume labels atm).
HBP 3:1cbc15648de1 177 * @returns Number of bad block headers. Keep in mind that only first byte is checked and if it's broken the who file system is useless.
HBP 3:1cbc15648de1 178 */
HBP 3:1cbc15648de1 179 char mkfs(char createLabel);
HBP 0:cbf45dde2b49 180 };
HBP 0:cbf45dde2b49 181
HBP 4:c77812997c9c 182 /** mFS File handle class
HBP 4:c77812997c9c 183 *
HBP 5:a0fe74dce80d 184 * This class is used as a handle for files stored in mFS.
HBP 4:c77812997c9c 185 */
HBP 0:cbf45dde2b49 186 class file {
HBP 0:cbf45dde2b49 187 private:
HBP 0:cbf45dde2b49 188 mfs *fs; // Reference to the file system in use
HBP 0:cbf45dde2b49 189 char attr; // RW = 1; RO = 0
HBP 0:cbf45dde2b49 190 char buffer[BUF]; // Write buffer
HBP 0:cbf45dde2b49 191 unsigned int bufPos; // "Cursor" position in buffer
HBP 0:cbf45dde2b49 192 char firstBlock; // First block of the file
HBP 0:cbf45dde2b49 193 char currBlock; // Current block in use
HBP 0:cbf45dde2b49 194 unsigned int blockPos; // "head" position on the current block
HBP 0:cbf45dde2b49 195 void needsFlush(); // check if flush is needed before read operation
HBP 0:cbf45dde2b49 196 public:
HBP 3:1cbc15648de1 197 /** Open new file handle
HBP 3:1cbc15648de1 198 *
HBP 3:1cbc15648de1 199 * File must be created before it can be opened!
HBP 3:1cbc15648de1 200 *
HBP 3:1cbc15648de1 201 * @param filename[20] Filename input.
HBP 3:1cbc15648de1 202 * @param operation 0 = read only, 1 = read and write. If read only file is opened in rw mode system will trip to error().
HBP 3:1cbc15648de1 203 */
HBP 3:1cbc15648de1 204 file(mfs *fs_ref, char filename[20], char operation);
HBP 5:a0fe74dce80d 205
HBP 3:1cbc15648de1 206 /** Close file handle
HBP 3:1cbc15648de1 207 *
HBP 3:1cbc15648de1 208 * Flushes the file and closes the handle.
HBP 3:1cbc15648de1 209 */
HBP 0:cbf45dde2b49 210 ~file(); // Close file handle and flush
HBP 5:a0fe74dce80d 211
HBP 3:1cbc15648de1 212 /** Rewind to the start postion of the file
HBP 5:a0fe74dce80d 213 *
HBP 3:1cbc15648de1 214 */
HBP 3:1cbc15648de1 215 void rewind();
HBP 5:a0fe74dce80d 216
HBP 5:a0fe74dce80d 217 /** Rewind n bytes back
HBP 5:a0fe74dce80d 218 *
HBP 5:a0fe74dce80d 219 * @param n Number of blocks to be rewinded.
HBP 5:a0fe74dce80d 220 * @returns Error code. 0 = OK, 1 = First block of file.
HBP 5:a0fe74dce80d 221 */
HBP 5:a0fe74dce80d 222 char rewind(uint16 n);
HBP 5:a0fe74dce80d 223
HBP 5:a0fe74dce80d 224 /** Forward one byte
HBP 5:a0fe74dce80d 225 *
HBP 5:a0fe74dce80d 226 * @returns Error code. 0 = OK, 1 = End of file.
HBP 3:1cbc15648de1 227 */
HBP 3:1cbc15648de1 228 char forward();
HBP 5:a0fe74dce80d 229
HBP 5:a0fe74dce80d 230 /** Forward n bytes
HBP 5:a0fe74dce80d 231 *
HBP 5:a0fe74dce80d 232 * @param n Number of blocks.
HBP 5:a0fe74dce80d 233 * @returns Error code. 0 = OK, 1 = End of file.
HBP 5:a0fe74dce80d 234 */
HBP 5:a0fe74dce80d 235 char forward(uint16 n);
HBP 5:a0fe74dce80d 236
HBP 3:1cbc15648de1 237 /** Reads a string of bytes
HBP 3:1cbc15648de1 238 *
HBP 3:1cbc15648de1 239 * Always places '\0' at the end of string.
HBP 3:1cbc15648de1 240 *
HBP 3:1cbc15648de1 241 * @param data Output buffer.
HBP 3:1cbc15648de1 242 * @param n Number of bytes to be read.
HBP 5:a0fe74dce80d 243 * @returns Error code. 0 = OK, 1 = Last block of the file
HBP 3:1cbc15648de1 244 */
HBP 0:cbf45dde2b49 245 void read(char *data, unsigned int n);
HBP 3:1cbc15648de1 246 /** Reads a binary array of bytes
HBP 3:1cbc15648de1 247 *
HBP 3:1cbc15648de1 248 * Doesn't add '\0' at the end of data array and doesn't respect mEOF byte.
HBP 3:1cbc15648de1 249 *
HBP 3:1cbc15648de1 250 * @param data Output buffer.
HBP 3:1cbc15648de1 251 * @param n Number of bytes to be read.
HBP 3:1cbc15648de1 252 */
HBP 5:a0fe74dce80d 253
HBP 0:cbf45dde2b49 254 void readBin(char *data, unsigned int n);
HBP 3:1cbc15648de1 255 /** Write byte array to a file (buffer)
HBP 3:1cbc15648de1 256 *
HBP 3:1cbc15648de1 257 * @param data Input data.
HBP 3:1cbc15648de1 258 * @param n Number of bytes to be read.
HBP 3:1cbc15648de1 259 * @returns Error code. 0 = OK, 1 = Flush failed.
HBP 3:1cbc15648de1 260 */
HBP 5:a0fe74dce80d 261
HBP 3:1cbc15648de1 262 char write(char *data, unsigned int n);
HBP 5:a0fe74dce80d 263
HBP 3:1cbc15648de1 264 /** Flush file buffer
HBP 3:1cbc15648de1 265 * Writes buffer to the EEPROM chip in use.
HBP 3:1cbc15648de1 266 */
HBP 0:cbf45dde2b49 267 char flush();
HBP 0:cbf45dde2b49 268 };
HBP 0:cbf45dde2b49 269
HBP 0:cbf45dde2b49 270 #endif