mFS file system library for EEPROM memory chips.

Committer:
HBP
Date:
Mon Feb 21 07:35:16 2011 +0000
Revision:
0:cbf45dde2b49
Child:
3:1cbc15648de1
Initial release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
HBP 0:cbf45dde2b49 1 /*H****************************************************************************
HBP 0:cbf45dde2b49 2 * FILENAME : mfs.h *
HBP 0:cbf45dde2b49 3 * *
HBP 0:cbf45dde2b49 4 * DESCRIPTION : *
HBP 0:cbf45dde2b49 5 * mFS file system implementation for mBED with external I2C EEEPROM. *
HBP 0:cbf45dde2b49 6 * *
HBP 0:cbf45dde2b49 7 * Block Flags: *
HBP 0:cbf45dde2b49 8 * 7:FBOF Begining of file *
HBP 0:cbf45dde2b49 9 * 6:LBOF Last block of file *
HBP 0:cbf45dde2b49 10 * 5:RO Read only file (Used only with FBOF) *
HBP 0:cbf45dde2b49 11 * 4:HIDDEN Hidden file (Used only with FBOF) *
HBP 0:cbf45dde2b49 12 * 3:INUSE Block in use *
HBP 0:cbf45dde2b49 13 * 2:NBAD Bad block (INV) *
HBP 0:cbf45dde2b49 14 * 1:VOL Volume label (Used only with FBOF) *
HBP 0:cbf45dde2b49 15 * 0:LOCK Locked file (Used only with FBOF) *
HBP 0:cbf45dde2b49 16 * *
HBP 0:cbf45dde2b49 17 * AUTHOR : Olli Vanhoja START DATE : 2011-02-18 *
HBP 0:cbf45dde2b49 18 *******************************************************************************
HBP 0:cbf45dde2b49 19 *
HBP 0:cbf45dde2b49 20 * CHANGES :
HBP 0:cbf45dde2b49 21 *
HBP 0:cbf45dde2b49 22 * VERSION DATE WHO DETAIL
HBP 0:cbf45dde2b49 23 * 0.1 2011-02-21 Olli Vanhoja Initial release version
HBP 0:cbf45dde2b49 24 *
HBP 0:cbf45dde2b49 25 *H*/
HBP 0:cbf45dde2b49 26
HBP 0:cbf45dde2b49 27 #ifndef MFS_H
HBP 0:cbf45dde2b49 28 #define MFS_H
HBP 0:cbf45dde2b49 29
HBP 0:cbf45dde2b49 30 #include "i2c_eeprom.h"
HBP 0:cbf45dde2b49 31
HBP 0:cbf45dde2b49 32 const unsigned int VOL_SIZE=40960; // 40 kB EEPROM chip
HBP 0:cbf45dde2b49 33 const unsigned int BS=4096; // 4096 bytes per block
HBP 0:cbf45dde2b49 34 const unsigned int BC=VOL_SIZE / BS; // 128 blocks
HBP 0:cbf45dde2b49 35 const char mEOF='\x01'; // End Of File/Section marked
HBP 0:cbf45dde2b49 36 const unsigned int BUF=400; // file buffer length
HBP 0:cbf45dde2b49 37
HBP 0:cbf45dde2b49 38 typedef unsigned short int uint16;
HBP 0:cbf45dde2b49 39
HBP 0:cbf45dde2b49 40 class mfs {
HBP 0:cbf45dde2b49 41 private:
HBP 0:cbf45dde2b49 42 i2c_eeprom *mem; // Only 512 kB I2C EEPROM is supported ATM
HBP 0:cbf45dde2b49 43 public:
HBP 0:cbf45dde2b49 44 mfs(int i2c_address);
HBP 0:cbf45dde2b49 45 char read(char *data, char block, unsigned int byteOffset, unsigned int n); // Read bytes from block
HBP 0:cbf45dde2b49 46 char write(char *data, char block, unsigned int byteOffset, unsigned int n); // write bytes to block
HBP 0:cbf45dde2b49 47 char getNextFreeBlock(char *blockOut); // Reserve next free block from begining
HBP 0:cbf45dde2b49 48 unsigned int findNextFile(unsigned int block, char *filenameOut); // Returns block number
HBP 0:cbf45dde2b49 49 uint16 getFirstBlockOfFile(char filename[20]);
HBP 0:cbf45dde2b49 50 char createFile(char filename[20]);
HBP 0:cbf45dde2b49 51 char removeFile(char filename[20]);
HBP 0:cbf45dde2b49 52 char setFileFlags(char *flags, char filename[20]);
HBP 0:cbf45dde2b49 53 char getFileFlags(char *flags, char filename[20]);
HBP 0:cbf45dde2b49 54 uint16 free();
HBP 0:cbf45dde2b49 55 char mkfs(char);
HBP 0:cbf45dde2b49 56 };
HBP 0:cbf45dde2b49 57
HBP 0:cbf45dde2b49 58 class file {
HBP 0:cbf45dde2b49 59 private:
HBP 0:cbf45dde2b49 60 mfs *fs; // Reference to the file system in use
HBP 0:cbf45dde2b49 61 char attr; // RW = 1; RO = 0
HBP 0:cbf45dde2b49 62 char buffer[BUF]; // Write buffer
HBP 0:cbf45dde2b49 63 unsigned int bufPos; // "Cursor" position in buffer
HBP 0:cbf45dde2b49 64 char firstBlock; // First block of the file
HBP 0:cbf45dde2b49 65 char currBlock; // Current block in use
HBP 0:cbf45dde2b49 66 unsigned int blockPos; // "head" position on the current block
HBP 0:cbf45dde2b49 67 void needsFlush(); // check if flush is needed before read operation
HBP 0:cbf45dde2b49 68 public:
HBP 0:cbf45dde2b49 69 file(mfs *fs_ref, char filename[20], char operation); // Open file handle
HBP 0:cbf45dde2b49 70 ~file(); // Close file handle and flush
HBP 0:cbf45dde2b49 71 void rewind(); // Rewind to start postion of the file
HBP 0:cbf45dde2b49 72 char forward(); // Forward one block
HBP 0:cbf45dde2b49 73 //char seek(unsigned int byte); // seek to the byte
HBP 0:cbf45dde2b49 74 void read(char *data, unsigned int n);
HBP 0:cbf45dde2b49 75 void readBin(char *data, unsigned int n);
HBP 0:cbf45dde2b49 76 char write(char *data, unsigned int n); // Write byte array
HBP 0:cbf45dde2b49 77 char flush();
HBP 0:cbf45dde2b49 78 };
HBP 0:cbf45dde2b49 79
HBP 0:cbf45dde2b49 80 #endif