Class that combined FATFileSystem with a USBMSD device, similar to LocalFileSystem

Dependencies:   USBDevice

Dependents:   SD_USB_FS_HelloWorld S25FL216K_USBFileSystem USBFileSystem_RAMDISK_HelloWorld

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBFileSystem.cpp Source File

USBFileSystem.cpp

00001 #include "USBFileSystem.h"
00002 
00003 #define USBWRITE_TIMEOUT 100
00004 
00005 USBFileSystem::USBFileSystem(const char* n) : FATFileSystem_ds(n) {
00006   localFunction = NULL;
00007   usbFunction = NULL;
00008   usbfree = true;
00009   local_count = 0;
00010   usbMode(1);
00011 }
00012 
00013 
00014 FileHandle* USBFileSystem::open(const char* name, int flags){    
00015     char n[64];
00016     sprintf(n, "%d:/%s", _fsid, name);
00017     bool write = true;
00018     
00019     /* POSIX flags -> FatFS open mode */
00020     BYTE openmode;
00021     if (flags & O_RDWR) {
00022         openmode = FA_READ|FA_WRITE;
00023         localOpen(true);
00024     } else if(flags & O_WRONLY) {
00025         openmode = FA_WRITE;
00026         localOpen(true);
00027     } else {
00028         openmode = FA_READ;
00029         write = false;
00030     }
00031     if(flags & O_CREAT) {
00032         if(flags & O_TRUNC) {
00033             openmode |= FA_CREATE_ALWAYS;
00034         } else {
00035             openmode |= FA_OPEN_ALWAYS;
00036         }
00037     }
00038     
00039     FIL fh;
00040     FRESULT res = f_open(&fh, n, openmode);
00041     if (res) {
00042         if (write)
00043             local_count--;
00044         return NULL;
00045     }
00046     if (flags & O_APPEND) {
00047         f_lseek(&fh, fh.fsize);
00048     }
00049     return new USBFileHandle(fh, this, write);
00050 }
00051 
00052 bool USBFileSystem::localSafe( void ){
00053     return (local_count == 0);
00054 }
00055 
00056 bool USBFileSystem::usbSafe( void ){
00057     return usbfree;
00058 }
00059 
00060 int USBFileSystem::disk_status_fat() { 
00061     int retval = _disk_status();
00062     
00063     if ((retval == 0) && (!usbSafe()))
00064         return 4;
00065     else 
00066         return retval;
00067  }
00068  
00069  int USBFileSystem::disk_status_msd() { 
00070     int retval = _disk_status();
00071     
00072     if ((retval == 0) && (!localSafe()))
00073         return 4;
00074     else 
00075         return retval;
00076  }
00077  
00078 int USBFileSystem::disk_write(const uint8_t * data, uint64_t block, uint8_t count) {
00079     if (localSafe()) {
00080         if (usbfree && (usbFunction!=NULL) )
00081             usbFunction(false);
00082             
00083         usbfree = false;
00084         }
00085   
00086     int retval= _disk_write(data, block, count);
00087 
00088     if (localSafe())
00089         usb_write.attach_us(this, &USBFileSystem::usbFree, USBWRITE_TIMEOUT * 1000);
00090 
00091     return retval;
00092 }
00093 
00094 void USBFileSystem::localOpen(bool open) {
00095     if (open) {
00096         local_count++;
00097     } else {
00098         local_count--;
00099     }
00100     
00101     //Pseudo-IRQ
00102     if (open && (local_count == 1)) {
00103         if (usbmode == 1)
00104             disconnect();
00105         if (localFunction != NULL) 
00106             (*localFunction)(false);
00107     }
00108     if (!open && (local_count == 0)) {
00109         if (usbmode == 1)
00110             connect();
00111         if (localFunction != NULL) 
00112             (*localFunction)(true);
00113     }
00114 }
00115     
00116 void USBFileSystem::attachLocal(void (*function)(bool)) {
00117     localFunction = function;
00118 }   
00119 
00120 void USBFileSystem::attachUSB(void (*function)(bool)) {
00121     usbFunction = function;
00122 }   
00123 
00124 void USBFileSystem::usbFree( void ) {
00125     usbfree = true;
00126     if (usbFunction != NULL)
00127         usbFunction(true);
00128 }
00129 
00130 void USBFileSystem::usbMode(int mode) {
00131     usbmode = mode;
00132 }