Updated get_fattime to use rtc and provide a date/time. This has been an annoying missing feature.
Dependents: IoTGateway_Basic y_XBeeTest_5_read CameraC1098_picture LifeCam ... more
MemFileSystem.h@0:93acdd9f65f4, 2012-03-30 (annotated)
- Committer:
- SomeRandomBloke
- Date:
- Fri Mar 30 18:41:26 2012 +0000
- Revision:
- 0:93acdd9f65f4
Updated get_fattime to use rtc and provide a date/time.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SomeRandomBloke | 0:93acdd9f65f4 | 1 | /* mbed Microcontroller Library - MemFileSystem |
SomeRandomBloke | 0:93acdd9f65f4 | 2 | * Copyright (c) 2008, sford |
SomeRandomBloke | 0:93acdd9f65f4 | 3 | */ |
SomeRandomBloke | 0:93acdd9f65f4 | 4 | |
SomeRandomBloke | 0:93acdd9f65f4 | 5 | |
SomeRandomBloke | 0:93acdd9f65f4 | 6 | #ifndef MBED_MEMFILESYSTEM_H |
SomeRandomBloke | 0:93acdd9f65f4 | 7 | #define MBED_MEMFILESYSTEM_H |
SomeRandomBloke | 0:93acdd9f65f4 | 8 | |
SomeRandomBloke | 0:93acdd9f65f4 | 9 | #include "FATFileSystem.h" |
SomeRandomBloke | 0:93acdd9f65f4 | 10 | |
SomeRandomBloke | 0:93acdd9f65f4 | 11 | namespace mbed { |
SomeRandomBloke | 0:93acdd9f65f4 | 12 | |
SomeRandomBloke | 0:93acdd9f65f4 | 13 | class MemFileSystem : public FATFileSystem { |
SomeRandomBloke | 0:93acdd9f65f4 | 14 | public: |
SomeRandomBloke | 0:93acdd9f65f4 | 15 | |
SomeRandomBloke | 0:93acdd9f65f4 | 16 | // 2000 sectors, each 512 bytes (malloced as required) |
SomeRandomBloke | 0:93acdd9f65f4 | 17 | char *sectors[2000]; |
SomeRandomBloke | 0:93acdd9f65f4 | 18 | |
SomeRandomBloke | 0:93acdd9f65f4 | 19 | MemFileSystem(const char* name) : FATFileSystem(name) { |
SomeRandomBloke | 0:93acdd9f65f4 | 20 | memset(sectors, 0, sizeof(sectors)); |
SomeRandomBloke | 0:93acdd9f65f4 | 21 | } |
SomeRandomBloke | 0:93acdd9f65f4 | 22 | |
SomeRandomBloke | 0:93acdd9f65f4 | 23 | virtual ~MemFileSystem() { |
SomeRandomBloke | 0:93acdd9f65f4 | 24 | for(int i = 0; i < 2000; i++) { |
SomeRandomBloke | 0:93acdd9f65f4 | 25 | if(sectors[i]) { |
SomeRandomBloke | 0:93acdd9f65f4 | 26 | free(sectors[i]); |
SomeRandomBloke | 0:93acdd9f65f4 | 27 | } |
SomeRandomBloke | 0:93acdd9f65f4 | 28 | } |
SomeRandomBloke | 0:93acdd9f65f4 | 29 | } |
SomeRandomBloke | 0:93acdd9f65f4 | 30 | |
SomeRandomBloke | 0:93acdd9f65f4 | 31 | // read a sector in to the buffer, return 0 if ok |
SomeRandomBloke | 0:93acdd9f65f4 | 32 | virtual int disk_read(char *buffer, int sector) { |
SomeRandomBloke | 0:93acdd9f65f4 | 33 | if(sectors[sector] == 0) { |
SomeRandomBloke | 0:93acdd9f65f4 | 34 | // nothing allocated means sector is empty |
SomeRandomBloke | 0:93acdd9f65f4 | 35 | memset(buffer, 0, 512); |
SomeRandomBloke | 0:93acdd9f65f4 | 36 | } else { |
SomeRandomBloke | 0:93acdd9f65f4 | 37 | memcpy(buffer, sectors[sector], 512); |
SomeRandomBloke | 0:93acdd9f65f4 | 38 | } |
SomeRandomBloke | 0:93acdd9f65f4 | 39 | return 0; |
SomeRandomBloke | 0:93acdd9f65f4 | 40 | } |
SomeRandomBloke | 0:93acdd9f65f4 | 41 | |
SomeRandomBloke | 0:93acdd9f65f4 | 42 | // write a sector from the buffer, return 0 if ok |
SomeRandomBloke | 0:93acdd9f65f4 | 43 | virtual int disk_write(const char *buffer, int sector) { |
SomeRandomBloke | 0:93acdd9f65f4 | 44 | // if buffer is zero deallocate sector |
SomeRandomBloke | 0:93acdd9f65f4 | 45 | char zero[512]; |
SomeRandomBloke | 0:93acdd9f65f4 | 46 | memset(zero, 0, 512); |
SomeRandomBloke | 0:93acdd9f65f4 | 47 | if(memcmp(zero, buffer, 512)==0) { |
SomeRandomBloke | 0:93acdd9f65f4 | 48 | if(sectors[sector] != 0) { |
SomeRandomBloke | 0:93acdd9f65f4 | 49 | free(sectors[sector]); |
SomeRandomBloke | 0:93acdd9f65f4 | 50 | sectors[sector] = 0; |
SomeRandomBloke | 0:93acdd9f65f4 | 51 | } |
SomeRandomBloke | 0:93acdd9f65f4 | 52 | return 0; |
SomeRandomBloke | 0:93acdd9f65f4 | 53 | } |
SomeRandomBloke | 0:93acdd9f65f4 | 54 | // else allocate a sector if needed, and write |
SomeRandomBloke | 0:93acdd9f65f4 | 55 | if(sectors[sector] == 0) { |
SomeRandomBloke | 0:93acdd9f65f4 | 56 | char *sec = (char*)malloc(512); |
SomeRandomBloke | 0:93acdd9f65f4 | 57 | if(sec==0) { |
SomeRandomBloke | 0:93acdd9f65f4 | 58 | return 1; // out of memory |
SomeRandomBloke | 0:93acdd9f65f4 | 59 | } |
SomeRandomBloke | 0:93acdd9f65f4 | 60 | sectors[sector] = sec; |
SomeRandomBloke | 0:93acdd9f65f4 | 61 | } |
SomeRandomBloke | 0:93acdd9f65f4 | 62 | memcpy(sectors[sector], buffer, 512); |
SomeRandomBloke | 0:93acdd9f65f4 | 63 | return 0; |
SomeRandomBloke | 0:93acdd9f65f4 | 64 | } |
SomeRandomBloke | 0:93acdd9f65f4 | 65 | |
SomeRandomBloke | 0:93acdd9f65f4 | 66 | // return the number of sectors |
SomeRandomBloke | 0:93acdd9f65f4 | 67 | virtual int disk_sectors() { |
SomeRandomBloke | 0:93acdd9f65f4 | 68 | return sizeof(sectors)/sizeof(sectors[0]); |
SomeRandomBloke | 0:93acdd9f65f4 | 69 | } |
SomeRandomBloke | 0:93acdd9f65f4 | 70 | |
SomeRandomBloke | 0:93acdd9f65f4 | 71 | }; |
SomeRandomBloke | 0:93acdd9f65f4 | 72 | |
SomeRandomBloke | 0:93acdd9f65f4 | 73 | } |
SomeRandomBloke | 0:93acdd9f65f4 | 74 | |
SomeRandomBloke | 0:93acdd9f65f4 | 75 | #endif |