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

Dependencies:   USBDevice

Dependents:   SD_USB_FS_HelloWorld S25FL216K_USBFileSystem USBFileSystem_RAMDISK_HelloWorld

Introduction

USBFileSystem is a combination of FATFileSystem and USB-MSD (Mass-Storage Device). This way it allows you to create a filesystem that is both accessible on your PC with the USB cable plugged in, and on the device itself. This is very similar to LocalFileSystem, only you can use any Serial Flash, SD, etc as storage medium.

If your code works with either FATFileSystem or USBMSD it will with very little modification also work with USBFileSystem.

Basic functionality

Since both FATFileSystem and USBMSD write binary data to a the storage medium, if both are writing somewhere at the same time we have a problem. This library makes the medium read only for the local side, if USB is writing, and vice versa. Local is considered to be writing as long as you have opened a file for write. USB is considered writing as soon as a write command comes from USB, and this continues for a short time after the last write command. This is needed because I cannot know when the last sector is written by USB, so I have to wait a little while to see if more data is written.

Reading

You can still read when the other one is writing. This can result in issues. Using the functions given you can easily make sure that won't happen. However regardless if you do that or not, it is a good idea to make sure your program can handle unexpected situations. For example if you open a file locally, and you get a NULL pointer, do not immediatly go into an error condition, but just try it again.

USB MSD on your PC

When you write to / read from a USB drive Windows (and I expect other OS's too) will cache everything. The result is that once you read a file on your PC, it will never change, even if your mbed does change the data. And if you write/delete a file when the mbed is locally using the file system, it will be read only, however your PC will tell you data was succesfully written/removed.

If this is a problem for your device, you can disconnect the USB part when the mbed is writing to the storage medium locally. The library can do this automatically for you.

Required code

The virtual functions that need to be implemented by you are:

virtual int disk_initialize() { return 0; }
virtual int _disk_status() { return 0; }
virtual int disk_read(uint8_t * buffer, uint64_t sector) { return 0;}
virtual int _disk_write(const uint8_t * buffer, uint64_t sector) = 0;
virtual int disk_sync() { return 0; }
virtual uint64_t disk_sectors() = 0;

Some of those are optional, but disk_read, _disk_write and disk_sectors have to be defined in the child class.

Sector size

The sector size must be 512 bytes. In USBMSD this requirement isn't present, but FATFileSystem has this requirement!

Function name

Note the '_' at the beginning of _disk_status and _disk_write. You may not inherit it without the '_', that will break the library. Since the parent libraries made it virtual I cannot block it from happening, so just watch out.

Available functions for the user

The USBFileSystem library allows for some extra functions the user can use. The API documentation lists them, but summarized: You can attach functions which are called once either USB is writing to the storage medium, or when this is done locally. There are two functions which will tell you if currently USB/local is writing to the storage medium, and you can set the usbMode. Set it to mode 0 and USB is read only when the storage is used locally, set it to mode 1 and it is disconnected (default and recommended).

Besides that there are of course the standard USB functions (connect/disconnect for example), and you can use 'fopen', 'mkdir', etc similar to FATFileSystem.

Hello World

Currently available:

RAM-disk for KL25Z and LPC1768 (this one disappeared for some reason, I re-published it, should still work):

Import programUSBFileSystem_RAMDISK_HelloWorld

RAMDisk example for the USBFileSystem

Wi-Go serial flash:

Import programS25FL216K_HelloWorld

Helloworld program for the S25FL216K flash memory in combination with USBFileSystem

SD card (different from others):

Import programSD_USB_FS_HelloWorld

SD USB MSD helloworld

Note that this one is not mine, if you have problems with it check if there are updates for the library.

USBFileSystem.h

Committer:
Sissors
Date:
2015-01-18
Revision:
8:a9e1dffac4bd
Parent:
6:15b73dae124e

File content as of revision 8:a9e1dffac4bd:

#ifndef USBFILESYSTEM_H
#define USBFILESYSTEM_H

#include "FATFileSystem.h"
#include "USBMSD.h"
#include "FATFileHandle.h"

class FATFileSystem_ds : public FATFileSystem {    
    public:
    FATFileSystem_ds(const char* n) : FATFileSystem(n) {};
    
    protected:
    virtual int disk_status_fat( void ) = 0;
    virtual int disk_status( void ) {return disk_status_fat();}
};

class USBMSD_ds : public USBMSD {    
    public:
    USBMSD_ds() {};
    
    protected:
    virtual int disk_status_msd( void ) = 0;
    virtual int disk_status( void ) {return disk_status_msd();}
};

/** Class which combines FATFileSystem with USBMSD device
*
* The functions to be implemented by the child class are the same as
* those that have to be implemented when using FATFileSystem/USBMSD.
* However there are some caveats: _disk_status() and _disk_write MUST
* be named with the '_' in front of it. You are not allowed to have a
* function without the '_'. Those functions may not be inherited, but
* with this version of C++ I cannot prevent the child from doing so.
*
* Next the size of a disk sector must be 512 bytes. This is because
* FATFileSystem only accepts that. If disk_size is not implemented
* this is done automatically. 
*
* What the library does is block write access locally when USB writes,
* and USB write access is blocked when with fopen a file is opened for
* write locally. This way they can't both write at the same time. Read
* access is not blocked.
*
* The final issue to take into account is caching: FATFileSystem will not
* re-open a file if you read twice the same data from it. So if USBMSD
* changed it, it will not show up different. If you close the file and
* call fopen again it will work. The USBMSD part has the
* same issue, but worse. Windows at least will only load everything once!
* So if you open a file, and then change it manually, you will never find
* out until the device is disconnected. If the disk is write protected
* it will not tell you so. It will write/delete what you want, only when
* you reconnect the device will it show it didn't actually do anything!
*
* See the program's wiki for more documentation!
*/
class USBFileSystem : public FATFileSystem_ds, public USBMSD_ds {
public:
    /** 
    * Constructor for USBFileSystem
    *
    * @param n - Name for the file system
    */
    USBFileSystem(const char* n);
    
    /** Attach a function to be called when locally the filesystem is occupied/freed
    *
    * The function is called with 'true' as argument when it is freed, false when it is occupied
    *
    * @param function - Function to be called, must have void as return type, and a single boolean argument
    */
    void attachLocal(void (*function)(bool));
    
    /** Attach a function to be called when USB occupies/frees the filesystem 
    *
    * The function is called with 'true' as argument when it is freed, false when it is occupied
    *
    * @param function - Function to be called, must have void as return type, and a single boolean argument
    */
    void attachUSB(void (*function)(bool));
    
    /** Check if locally files are opened for write
    *
    * @return - true if none are opened for write, false otherwise
    */
    bool localSafe( void );
    
    /** Check if via USB files files are being written
    *
    * @return - true if none are being written, false otherwise
    */
    bool usbSafe( void );
    
    /** Sets the USB mode
    *
    * Argument = 0: USB is write protected when not available.
    * Argument = 1: USB is disconnected when not available (default).
    *
    * @param mode - USB safety mode
    */
    void usbMode( int mode );
    
    
protected:
    //Functions to be implemented by child:
    virtual int disk_initialize() { return 0; }
    virtual int _disk_status() { return 0; }
    virtual int disk_read(uint8_t * buffer, uint64_t sector, uint8_t count) { return 0;}
    virtual int _disk_write(const uint8_t * buffer, uint64_t sector, uint8_t count) = 0;
    virtual int disk_sync() { return 0; }
    virtual uint64_t disk_sectors() = 0;
    virtual uint64_t disk_size() {return 512 * disk_sectors();}
    
    //Not to be implemented by child:
    virtual FileHandle* open(const char* name, int flags);
    void localOpen( bool open );
    void usbFree( void );
    
private:
    virtual int disk_status_fat(void);
    virtual int disk_status_msd(void);
    virtual int disk_write(const uint8_t * buffer, uint64_t sector, uint8_t count);
    
    int local_count;
    int usbmode;
    Timeout usb_write;
    bool usbfree;
    
    void (*localFunction)(bool);
    void (*usbFunction)(bool);
    
    friend class USBFileHandle;

};


class USBFileHandle : public FATFileHandle {    
    public:
    USBFileHandle(FIL fh, USBFileSystem *p, bool write) : FATFileHandle(fh) {
     _p = p;
     _write = write;
    }
    
    protected:
    USBFileSystem *_p;
    bool _write;
    
    virtual int close() {
        int retval = FATFileHandle::close();
        if (_write)
            _p->localOpen(false);
        return retval;
        }
};





#endif