Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nexpaq 0:6c56fb4bc5f0 1 /* mbed Microcontroller Library - MemFileSystem
nexpaq 0:6c56fb4bc5f0 2 * Copyright (c) 2008, sford
nexpaq 0:6c56fb4bc5f0 3 */
nexpaq 0:6c56fb4bc5f0 4
nexpaq 0:6c56fb4bc5f0 5
nexpaq 0:6c56fb4bc5f0 6 #ifndef MBED_MEMFILESYSTEM_H
nexpaq 0:6c56fb4bc5f0 7 #define MBED_MEMFILESYSTEM_H
nexpaq 0:6c56fb4bc5f0 8
nexpaq 0:6c56fb4bc5f0 9 #include "FATFileSystem.h"
nexpaq 0:6c56fb4bc5f0 10
nexpaq 0:6c56fb4bc5f0 11 namespace mbed
nexpaq 0:6c56fb4bc5f0 12 {
nexpaq 0:6c56fb4bc5f0 13
nexpaq 0:6c56fb4bc5f0 14 class MemFileSystem : public FATFileSystem
nexpaq 0:6c56fb4bc5f0 15 {
nexpaq 0:6c56fb4bc5f0 16 public:
nexpaq 0:6c56fb4bc5f0 17
nexpaq 0:6c56fb4bc5f0 18 // 2000 sectors, each 512 bytes (malloced as required)
nexpaq 0:6c56fb4bc5f0 19 char *sectors[2000];
nexpaq 0:6c56fb4bc5f0 20
nexpaq 0:6c56fb4bc5f0 21 MemFileSystem(const char* name) : FATFileSystem(name) {
nexpaq 0:6c56fb4bc5f0 22 memset(sectors, 0, sizeof(sectors));
nexpaq 0:6c56fb4bc5f0 23 }
nexpaq 0:6c56fb4bc5f0 24
nexpaq 0:6c56fb4bc5f0 25 virtual ~MemFileSystem() {
nexpaq 0:6c56fb4bc5f0 26 for(int i = 0; i < 2000; i++) {
nexpaq 0:6c56fb4bc5f0 27 if(sectors[i]) {
nexpaq 0:6c56fb4bc5f0 28 free(sectors[i]);
nexpaq 0:6c56fb4bc5f0 29 }
nexpaq 0:6c56fb4bc5f0 30 }
nexpaq 0:6c56fb4bc5f0 31 }
nexpaq 0:6c56fb4bc5f0 32
nexpaq 0:6c56fb4bc5f0 33 // read a sector in to the buffer, return 0 if ok
nexpaq 0:6c56fb4bc5f0 34 virtual int disk_read(char *buffer, int sector) {
nexpaq 0:6c56fb4bc5f0 35 if(sectors[sector] == 0) {
nexpaq 0:6c56fb4bc5f0 36 // nothing allocated means sector is empty
nexpaq 0:6c56fb4bc5f0 37 memset(buffer, 0, 512);
nexpaq 0:6c56fb4bc5f0 38 } else {
nexpaq 0:6c56fb4bc5f0 39 memcpy(buffer, sectors[sector], 512);
nexpaq 0:6c56fb4bc5f0 40 }
nexpaq 0:6c56fb4bc5f0 41 return 0;
nexpaq 0:6c56fb4bc5f0 42 }
nexpaq 0:6c56fb4bc5f0 43
nexpaq 0:6c56fb4bc5f0 44 // write a sector from the buffer, return 0 if ok
nexpaq 0:6c56fb4bc5f0 45 virtual int disk_write(const char *buffer, int sector) {
nexpaq 0:6c56fb4bc5f0 46 // if buffer is zero deallocate sector
nexpaq 0:6c56fb4bc5f0 47 char zero[512];
nexpaq 0:6c56fb4bc5f0 48 memset(zero, 0, 512);
nexpaq 0:6c56fb4bc5f0 49 if(memcmp(zero, buffer, 512)==0) {
nexpaq 0:6c56fb4bc5f0 50 if(sectors[sector] != 0) {
nexpaq 0:6c56fb4bc5f0 51 free(sectors[sector]);
nexpaq 0:6c56fb4bc5f0 52 sectors[sector] = 0;
nexpaq 0:6c56fb4bc5f0 53 }
nexpaq 0:6c56fb4bc5f0 54 return 0;
nexpaq 0:6c56fb4bc5f0 55 }
nexpaq 0:6c56fb4bc5f0 56 // else allocate a sector if needed, and write
nexpaq 0:6c56fb4bc5f0 57 if(sectors[sector] == 0) {
nexpaq 0:6c56fb4bc5f0 58 char *sec = (char*)malloc(512);
nexpaq 0:6c56fb4bc5f0 59 if(sec==0) {
nexpaq 0:6c56fb4bc5f0 60 return 1; // out of memory
nexpaq 0:6c56fb4bc5f0 61 }
nexpaq 0:6c56fb4bc5f0 62 sectors[sector] = sec;
nexpaq 0:6c56fb4bc5f0 63 }
nexpaq 0:6c56fb4bc5f0 64 memcpy(sectors[sector], buffer, 512);
nexpaq 0:6c56fb4bc5f0 65 return 0;
nexpaq 0:6c56fb4bc5f0 66 }
nexpaq 0:6c56fb4bc5f0 67
nexpaq 0:6c56fb4bc5f0 68 // return the number of sectors
nexpaq 0:6c56fb4bc5f0 69 virtual int disk_sectors() {
nexpaq 0:6c56fb4bc5f0 70 return sizeof(sectors)/sizeof(sectors[0]);
nexpaq 0:6c56fb4bc5f0 71 }
nexpaq 0:6c56fb4bc5f0 72
nexpaq 0:6c56fb4bc5f0 73 };
nexpaq 0:6c56fb4bc5f0 74
nexpaq 0:6c56fb4bc5f0 75 }
nexpaq 0:6c56fb4bc5f0 76
nexpaq 0:6c56fb4bc5f0 77 #endif