[ FORK ] USBFileSytem; fork from sissors/USBFileSystem
Dependents: USBMSD_CDC_11U35test
Fork of USBFileSystem by
FATFileSystem/MemFileSystem.h@9:98a97b7d56a8, 2015-04-21 (annotated)
- Committer:
- k4zuki
- Date:
- Tue Apr 21 09:14:07 2015 +0000
- Revision:
- 9:98a97b7d56a8
- Parent:
- 6:15b73dae124e
update USBDevice
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sissors | 6:15b73dae124e | 1 | /* mbed Microcontroller Library - MemFileSystem |
Sissors | 6:15b73dae124e | 2 | * Copyright (c) 2008, sford |
Sissors | 6:15b73dae124e | 3 | */ |
Sissors | 6:15b73dae124e | 4 | |
Sissors | 6:15b73dae124e | 5 | |
Sissors | 6:15b73dae124e | 6 | #ifndef MBED_MEMFILESYSTEM_H |
Sissors | 6:15b73dae124e | 7 | #define MBED_MEMFILESYSTEM_H |
Sissors | 6:15b73dae124e | 8 | |
Sissors | 6:15b73dae124e | 9 | #include "FATFileSystem.h" |
Sissors | 6:15b73dae124e | 10 | |
Sissors | 6:15b73dae124e | 11 | namespace mbed |
Sissors | 6:15b73dae124e | 12 | { |
Sissors | 6:15b73dae124e | 13 | |
Sissors | 6:15b73dae124e | 14 | class MemFileSystem : public FATFileSystem |
Sissors | 6:15b73dae124e | 15 | { |
Sissors | 6:15b73dae124e | 16 | public: |
Sissors | 6:15b73dae124e | 17 | |
Sissors | 6:15b73dae124e | 18 | // 2000 sectors, each 512 bytes (malloced as required) |
Sissors | 6:15b73dae124e | 19 | char *sectors[2000]; |
Sissors | 6:15b73dae124e | 20 | |
Sissors | 6:15b73dae124e | 21 | MemFileSystem(const char* name) : FATFileSystem(name) { |
Sissors | 6:15b73dae124e | 22 | memset(sectors, 0, sizeof(sectors)); |
Sissors | 6:15b73dae124e | 23 | } |
Sissors | 6:15b73dae124e | 24 | |
Sissors | 6:15b73dae124e | 25 | virtual ~MemFileSystem() { |
Sissors | 6:15b73dae124e | 26 | for(int i = 0; i < 2000; i++) { |
Sissors | 6:15b73dae124e | 27 | if(sectors[i]) { |
Sissors | 6:15b73dae124e | 28 | free(sectors[i]); |
Sissors | 6:15b73dae124e | 29 | } |
Sissors | 6:15b73dae124e | 30 | } |
Sissors | 6:15b73dae124e | 31 | } |
Sissors | 6:15b73dae124e | 32 | |
Sissors | 6:15b73dae124e | 33 | // read a sector in to the buffer, return 0 if ok |
Sissors | 6:15b73dae124e | 34 | virtual int disk_read(char *buffer, int sector) { |
Sissors | 6:15b73dae124e | 35 | if(sectors[sector] == 0) { |
Sissors | 6:15b73dae124e | 36 | // nothing allocated means sector is empty |
Sissors | 6:15b73dae124e | 37 | memset(buffer, 0, 512); |
Sissors | 6:15b73dae124e | 38 | } else { |
Sissors | 6:15b73dae124e | 39 | memcpy(buffer, sectors[sector], 512); |
Sissors | 6:15b73dae124e | 40 | } |
Sissors | 6:15b73dae124e | 41 | return 0; |
Sissors | 6:15b73dae124e | 42 | } |
Sissors | 6:15b73dae124e | 43 | |
Sissors | 6:15b73dae124e | 44 | // write a sector from the buffer, return 0 if ok |
Sissors | 6:15b73dae124e | 45 | virtual int disk_write(const char *buffer, int sector) { |
Sissors | 6:15b73dae124e | 46 | // if buffer is zero deallocate sector |
Sissors | 6:15b73dae124e | 47 | char zero[512]; |
Sissors | 6:15b73dae124e | 48 | memset(zero, 0, 512); |
Sissors | 6:15b73dae124e | 49 | if(memcmp(zero, buffer, 512)==0) { |
Sissors | 6:15b73dae124e | 50 | if(sectors[sector] != 0) { |
Sissors | 6:15b73dae124e | 51 | free(sectors[sector]); |
Sissors | 6:15b73dae124e | 52 | sectors[sector] = 0; |
Sissors | 6:15b73dae124e | 53 | } |
Sissors | 6:15b73dae124e | 54 | return 0; |
Sissors | 6:15b73dae124e | 55 | } |
Sissors | 6:15b73dae124e | 56 | // else allocate a sector if needed, and write |
Sissors | 6:15b73dae124e | 57 | if(sectors[sector] == 0) { |
Sissors | 6:15b73dae124e | 58 | char *sec = (char*)malloc(512); |
Sissors | 6:15b73dae124e | 59 | if(sec==0) { |
Sissors | 6:15b73dae124e | 60 | return 1; // out of memory |
Sissors | 6:15b73dae124e | 61 | } |
Sissors | 6:15b73dae124e | 62 | sectors[sector] = sec; |
Sissors | 6:15b73dae124e | 63 | } |
Sissors | 6:15b73dae124e | 64 | memcpy(sectors[sector], buffer, 512); |
Sissors | 6:15b73dae124e | 65 | return 0; |
Sissors | 6:15b73dae124e | 66 | } |
Sissors | 6:15b73dae124e | 67 | |
Sissors | 6:15b73dae124e | 68 | // return the number of sectors |
Sissors | 6:15b73dae124e | 69 | virtual int disk_sectors() { |
Sissors | 6:15b73dae124e | 70 | return sizeof(sectors)/sizeof(sectors[0]); |
Sissors | 6:15b73dae124e | 71 | } |
Sissors | 6:15b73dae124e | 72 | |
Sissors | 6:15b73dae124e | 73 | }; |
Sissors | 6:15b73dae124e | 74 | |
Sissors | 6:15b73dae124e | 75 | } |
Sissors | 6:15b73dae124e | 76 | |
Sissors | 6:15b73dae124e | 77 | #endif |