Class that contain only FATFileSystem

Fork of USBFileSystem by Erik -

Committer:
mkilivan
Date:
Fri Dec 19 14:41:08 2014 +0000
Revision:
6:eeeea7fbb0a2
Parent:
2:9af05743d551
Remove USB part

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sissors 0:dabe3383ef23 1 #include "USBFileSystem.h"
Sissors 0:dabe3383ef23 2
Sissors 0:dabe3383ef23 3 #define USBWRITE_TIMEOUT 100
Sissors 0:dabe3383ef23 4
Sissors 0:dabe3383ef23 5 USBFileSystem::USBFileSystem(const char* n) : FATFileSystem_ds(n) {
Sissors 0:dabe3383ef23 6 localFunction = NULL;
Sissors 0:dabe3383ef23 7 local_count = 0;
Sissors 0:dabe3383ef23 8 }
Sissors 0:dabe3383ef23 9
Sissors 0:dabe3383ef23 10
Sissors 0:dabe3383ef23 11 FileHandle* USBFileSystem::open(const char* name, int flags){
Sissors 0:dabe3383ef23 12 char n[64];
Sissors 0:dabe3383ef23 13 sprintf(n, "%d:/%s", _fsid, name);
Sissors 0:dabe3383ef23 14 bool write = true;
Sissors 0:dabe3383ef23 15
Sissors 0:dabe3383ef23 16 /* POSIX flags -> FatFS open mode */
Sissors 0:dabe3383ef23 17 BYTE openmode;
Sissors 0:dabe3383ef23 18 if (flags & O_RDWR) {
Sissors 0:dabe3383ef23 19 openmode = FA_READ|FA_WRITE;
Sissors 0:dabe3383ef23 20 localOpen(true);
Sissors 0:dabe3383ef23 21 } else if(flags & O_WRONLY) {
Sissors 0:dabe3383ef23 22 openmode = FA_WRITE;
Sissors 0:dabe3383ef23 23 localOpen(true);
Sissors 0:dabe3383ef23 24 } else {
Sissors 0:dabe3383ef23 25 openmode = FA_READ;
Sissors 0:dabe3383ef23 26 write = false;
Sissors 0:dabe3383ef23 27 }
Sissors 0:dabe3383ef23 28 if(flags & O_CREAT) {
Sissors 0:dabe3383ef23 29 if(flags & O_TRUNC) {
Sissors 0:dabe3383ef23 30 openmode |= FA_CREATE_ALWAYS;
Sissors 0:dabe3383ef23 31 } else {
Sissors 0:dabe3383ef23 32 openmode |= FA_OPEN_ALWAYS;
Sissors 0:dabe3383ef23 33 }
Sissors 0:dabe3383ef23 34 }
Sissors 0:dabe3383ef23 35
Sissors 0:dabe3383ef23 36 FIL fh;
Sissors 0:dabe3383ef23 37 FRESULT res = f_open(&fh, n, openmode);
Sissors 0:dabe3383ef23 38 if (res) {
Sissors 0:dabe3383ef23 39 if (write)
Sissors 0:dabe3383ef23 40 local_count--;
Sissors 0:dabe3383ef23 41 return NULL;
Sissors 0:dabe3383ef23 42 }
Sissors 0:dabe3383ef23 43 if (flags & O_APPEND) {
Sissors 0:dabe3383ef23 44 f_lseek(&fh, fh.fsize);
Sissors 0:dabe3383ef23 45 }
Sissors 0:dabe3383ef23 46 return new USBFileHandle(fh, this, write);
Sissors 0:dabe3383ef23 47 }
Sissors 0:dabe3383ef23 48
Sissors 0:dabe3383ef23 49 bool USBFileSystem::localSafe( void ){
Sissors 0:dabe3383ef23 50 return (local_count == 0);
Sissors 0:dabe3383ef23 51 }
Sissors 0:dabe3383ef23 52
Sissors 0:dabe3383ef23 53 int USBFileSystem::disk_status_fat() {
Sissors 0:dabe3383ef23 54 int retval = _disk_status();
Sissors 0:dabe3383ef23 55
mkilivan 6:eeeea7fbb0a2 56 if (retval == 0)
Sissors 0:dabe3383ef23 57 return 4;
Sissors 0:dabe3383ef23 58 else
Sissors 0:dabe3383ef23 59 return retval;
Sissors 0:dabe3383ef23 60 }
Sissors 0:dabe3383ef23 61
Sissors 0:dabe3383ef23 62 int USBFileSystem::disk_status_msd() {
Sissors 0:dabe3383ef23 63 int retval = _disk_status();
Sissors 0:dabe3383ef23 64
mkilivan 6:eeeea7fbb0a2 65 if (retval == 0)
Sissors 0:dabe3383ef23 66 return 4;
Sissors 0:dabe3383ef23 67 else
Sissors 0:dabe3383ef23 68 return retval;
Sissors 0:dabe3383ef23 69 }
Sissors 0:dabe3383ef23 70
Sissors 0:dabe3383ef23 71 int USBFileSystem::disk_write(const uint8_t * data, uint64_t block) {
mkilivan 6:eeeea7fbb0a2 72 int retval= _disk_write(data, block);
Sissors 0:dabe3383ef23 73
Sissors 0:dabe3383ef23 74 return retval;
Sissors 0:dabe3383ef23 75 }
Sissors 0:dabe3383ef23 76
Sissors 0:dabe3383ef23 77 void USBFileSystem::localOpen(bool open) {
Sissors 0:dabe3383ef23 78 if (open) {
Sissors 0:dabe3383ef23 79 local_count++;
Sissors 0:dabe3383ef23 80 } else {
Sissors 0:dabe3383ef23 81 local_count--;
Sissors 0:dabe3383ef23 82 }
Sissors 0:dabe3383ef23 83
Sissors 0:dabe3383ef23 84 //Pseudo-IRQ
Sissors 1:4ba08d11e36e 85 if (open && (local_count == 1)) {
Sissors 1:4ba08d11e36e 86 if (localFunction != NULL)
Sissors 0:dabe3383ef23 87 (*localFunction)(false);
Sissors 1:4ba08d11e36e 88 }
Sissors 1:4ba08d11e36e 89 if (!open && (local_count == 0)) {
Sissors 1:4ba08d11e36e 90 if (localFunction != NULL)
Sissors 0:dabe3383ef23 91 (*localFunction)(true);
Sissors 0:dabe3383ef23 92 }
Sissors 0:dabe3383ef23 93 }
Sissors 0:dabe3383ef23 94
Sissors 0:dabe3383ef23 95 void USBFileSystem::attachLocal(void (*function)(bool)) {
Sissors 0:dabe3383ef23 96 localFunction = function;
Sissors 0:dabe3383ef23 97 }