Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: DISCO-F746NG_USB_Host
Fork of KL46Z-USBHost by
FATFileSystem/MemFileSystem.h@25:7d6d9fc471bf, 2016-06-17 (annotated)
- Committer:
- DieterGraef
- Date:
- Fri Jun 17 09:00:35 2016 +0000
- Revision:
- 25:7d6d9fc471bf
- Parent:
- 24:5396b6a93262
USB Host now works with both Interfaces even in parallel. Some changes in the USB MSD driver to make it operable
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DieterGraef | 24:5396b6a93262 | 1 | /* mbed Microcontroller Library - MemFileSystem |
DieterGraef | 24:5396b6a93262 | 2 | * Copyright (c) 2008, sford |
DieterGraef | 24:5396b6a93262 | 3 | */ |
DieterGraef | 24:5396b6a93262 | 4 | |
DieterGraef | 24:5396b6a93262 | 5 | |
DieterGraef | 24:5396b6a93262 | 6 | #ifndef MBED_MEMFILESYSTEM_H |
DieterGraef | 24:5396b6a93262 | 7 | #define MBED_MEMFILESYSTEM_H |
DieterGraef | 24:5396b6a93262 | 8 | |
DieterGraef | 24:5396b6a93262 | 9 | #include "FATFileSystem.h" |
DieterGraef | 24:5396b6a93262 | 10 | |
DieterGraef | 24:5396b6a93262 | 11 | namespace mbed |
DieterGraef | 24:5396b6a93262 | 12 | { |
DieterGraef | 24:5396b6a93262 | 13 | |
DieterGraef | 24:5396b6a93262 | 14 | class MemFileSystem : public FATFileSystem |
DieterGraef | 24:5396b6a93262 | 15 | { |
DieterGraef | 24:5396b6a93262 | 16 | public: |
DieterGraef | 24:5396b6a93262 | 17 | |
DieterGraef | 24:5396b6a93262 | 18 | // 2000 sectors, each 512 bytes (malloced as required) |
DieterGraef | 24:5396b6a93262 | 19 | char *sectors[2000]; |
DieterGraef | 24:5396b6a93262 | 20 | |
DieterGraef | 24:5396b6a93262 | 21 | MemFileSystem(const char* name) : FATFileSystem(name) { |
DieterGraef | 24:5396b6a93262 | 22 | memset(sectors, 0, sizeof(sectors)); |
DieterGraef | 24:5396b6a93262 | 23 | } |
DieterGraef | 24:5396b6a93262 | 24 | |
DieterGraef | 24:5396b6a93262 | 25 | virtual ~MemFileSystem() { |
DieterGraef | 24:5396b6a93262 | 26 | for(int i = 0; i < 2000; i++) { |
DieterGraef | 24:5396b6a93262 | 27 | if(sectors[i]) { |
DieterGraef | 24:5396b6a93262 | 28 | free(sectors[i]); |
DieterGraef | 24:5396b6a93262 | 29 | } |
DieterGraef | 24:5396b6a93262 | 30 | } |
DieterGraef | 24:5396b6a93262 | 31 | } |
DieterGraef | 24:5396b6a93262 | 32 | |
DieterGraef | 24:5396b6a93262 | 33 | // read a sector in to the buffer, return 0 if ok |
DieterGraef | 24:5396b6a93262 | 34 | virtual int disk_read(char *buffer, int sector) { |
DieterGraef | 24:5396b6a93262 | 35 | if(sectors[sector] == 0) { |
DieterGraef | 24:5396b6a93262 | 36 | // nothing allocated means sector is empty |
DieterGraef | 24:5396b6a93262 | 37 | memset(buffer, 0, 512); |
DieterGraef | 24:5396b6a93262 | 38 | } else { |
DieterGraef | 24:5396b6a93262 | 39 | memcpy(buffer, sectors[sector], 512); |
DieterGraef | 24:5396b6a93262 | 40 | } |
DieterGraef | 24:5396b6a93262 | 41 | return 0; |
DieterGraef | 24:5396b6a93262 | 42 | } |
DieterGraef | 24:5396b6a93262 | 43 | |
DieterGraef | 24:5396b6a93262 | 44 | // write a sector from the buffer, return 0 if ok |
DieterGraef | 24:5396b6a93262 | 45 | virtual int disk_write(const char *buffer, int sector) { |
DieterGraef | 24:5396b6a93262 | 46 | // if buffer is zero deallocate sector |
DieterGraef | 24:5396b6a93262 | 47 | char zero[512]; |
DieterGraef | 24:5396b6a93262 | 48 | memset(zero, 0, 512); |
DieterGraef | 24:5396b6a93262 | 49 | if(memcmp(zero, buffer, 512)==0) { |
DieterGraef | 24:5396b6a93262 | 50 | if(sectors[sector] != 0) { |
DieterGraef | 24:5396b6a93262 | 51 | free(sectors[sector]); |
DieterGraef | 24:5396b6a93262 | 52 | sectors[sector] = 0; |
DieterGraef | 24:5396b6a93262 | 53 | } |
DieterGraef | 24:5396b6a93262 | 54 | return 0; |
DieterGraef | 24:5396b6a93262 | 55 | } |
DieterGraef | 24:5396b6a93262 | 56 | // else allocate a sector if needed, and write |
DieterGraef | 24:5396b6a93262 | 57 | if(sectors[sector] == 0) { |
DieterGraef | 24:5396b6a93262 | 58 | char *sec = (char*)malloc(512); |
DieterGraef | 24:5396b6a93262 | 59 | if(sec==0) { |
DieterGraef | 24:5396b6a93262 | 60 | return 1; // out of memory |
DieterGraef | 24:5396b6a93262 | 61 | } |
DieterGraef | 24:5396b6a93262 | 62 | sectors[sector] = sec; |
DieterGraef | 24:5396b6a93262 | 63 | } |
DieterGraef | 24:5396b6a93262 | 64 | memcpy(sectors[sector], buffer, 512); |
DieterGraef | 24:5396b6a93262 | 65 | return 0; |
DieterGraef | 24:5396b6a93262 | 66 | } |
DieterGraef | 24:5396b6a93262 | 67 | |
DieterGraef | 24:5396b6a93262 | 68 | // return the number of sectors |
DieterGraef | 24:5396b6a93262 | 69 | virtual int disk_sectors() { |
DieterGraef | 24:5396b6a93262 | 70 | return sizeof(sectors)/sizeof(sectors[0]); |
DieterGraef | 24:5396b6a93262 | 71 | } |
DieterGraef | 24:5396b6a93262 | 72 | |
DieterGraef | 24:5396b6a93262 | 73 | }; |
DieterGraef | 24:5396b6a93262 | 74 | |
DieterGraef | 24:5396b6a93262 | 75 | } |
DieterGraef | 24:5396b6a93262 | 76 | |
DieterGraef | 24:5396b6a93262 | 77 | #endif |