Revised to support ability to have both SD and USB drives mounted.

Dependents:   Multi-FileSystem Multi-FileSystem

Fork of FATFileSystem by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MemFileSystem.h Source File

MemFileSystem.h

00001 /* mbed Microcontroller Library - MemFileSystem
00002  * Copyright (c) 2008, sford
00003  */
00004 
00005 
00006 #ifndef MBED_MEMFILESYSTEM_H
00007 #define MBED_MEMFILESYSTEM_H
00008 
00009 #include "FATFileSystem.h"
00010 
00011 // 4 Bytes / Sector, each 512 Bytes - malloc'd as required
00012 // Value was 2000 (8K)
00013 #define NUM_SECTORS 100
00014 #define SECTOR_SIZE 512
00015 
00016 // moved the zeroed RAM buffer to flash, 
00017 // so it doesn't waste precious RAM
00018 static const char zero[SECTOR_SIZE] = {0};
00019 
00020 namespace mbed
00021 {
00022     class MemFileSystem : public FATFileSystem
00023     {
00024     public:
00025         // NUM_SECTORS sectors, each 512 bytes (malloced as required)
00026         char *sectors[NUM_SECTORS];
00027     
00028         MemFileSystem(const char* name) : FATFileSystem(name) {
00029             memset(sectors, 0, sizeof(sectors));
00030         }
00031 
00032         virtual ~MemFileSystem() {
00033             for(int i = 0; i < NUM_SECTORS; i++) {
00034                 if(sectors[i]) {
00035                     free(sectors[i]);
00036                 }
00037             }
00038         }
00039 
00040         // read a sector in to the buffer, return 0 if ok
00041         virtual int disk_read(char *buffer, int sector) {
00042             if(sectors[sector] == 0) {
00043                 // nothing allocated means sector is empty
00044                 memset(buffer, 0, SECTOR_SIZE);
00045             } else {
00046                 memcpy(buffer, sectors[sector], SECTOR_SIZE);
00047             }
00048             return 0;
00049         }
00050 
00051         // write a sector from the buffer, return 0 if ok
00052         virtual int disk_write(const char *buffer, int sector) {
00053             // if buffer is zero deallocate sector
00054             //char zero[SECTOR_SIZE] = {0};     // DS this used to be a ram buffer
00055             //memset(zero, 0, SECTOR_SIZE);
00056             if(memcmp(zero, buffer, SECTOR_SIZE)==0) {
00057                 if(sectors[sector] != 0) {
00058                     free(sectors[sector]);
00059                     sectors[sector] = 0;
00060                 }
00061                 return 0;
00062             }
00063             // else allocate a sector if needed, and write
00064             if(sectors[sector] == 0) {
00065                 char *sec = (char*)malloc(SECTOR_SIZE);
00066                 if(sec==0) {
00067                     return 1; // out of memory
00068                 }
00069                 sectors[sector] = sec;
00070             }
00071             memcpy(sectors[sector], buffer, SECTOR_SIZE);
00072             return 0;
00073         }
00074 
00075         // return the number of sectors
00076         virtual int disk_sectors() {
00077             return sizeof(sectors)/sizeof(sectors[0]);
00078         }
00079     };
00080 }
00081 
00082 #endif