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.

Committer:
Sissors
Date:
Wed Oct 23 20:32:07 2013 +0000
Revision:
2:9af05743d551
FATFileSystem 'Tiny' option enabled -> Primary to solve a buffering issue when USB writes a file, also reduces RAM consumed
;
; USBDevice back to main branch
;
; USBMode 1 is default now

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sissors 2:9af05743d551 1 /* mbed Microcontroller Library
Sissors 2:9af05743d551 2 * Copyright (c) 2006-2012 ARM Limited
Sissors 2:9af05743d551 3 *
Sissors 2:9af05743d551 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
Sissors 2:9af05743d551 5 * of this software and associated documentation files (the "Software"), to deal
Sissors 2:9af05743d551 6 * in the Software without restriction, including without limitation the rights
Sissors 2:9af05743d551 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Sissors 2:9af05743d551 8 * copies of the Software, and to permit persons to whom the Software is
Sissors 2:9af05743d551 9 * furnished to do so, subject to the following conditions:
Sissors 2:9af05743d551 10 *
Sissors 2:9af05743d551 11 * The above copyright notice and this permission notice shall be included in
Sissors 2:9af05743d551 12 * all copies or substantial portions of the Software.
Sissors 2:9af05743d551 13 *
Sissors 2:9af05743d551 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Sissors 2:9af05743d551 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Sissors 2:9af05743d551 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Sissors 2:9af05743d551 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Sissors 2:9af05743d551 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Sissors 2:9af05743d551 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
Sissors 2:9af05743d551 20 * SOFTWARE.
Sissors 2:9af05743d551 21 */
Sissors 2:9af05743d551 22 #include "ff.h"
Sissors 2:9af05743d551 23 #include "ffconf.h"
Sissors 2:9af05743d551 24 #include "mbed_debug.h"
Sissors 2:9af05743d551 25
Sissors 2:9af05743d551 26 #include "FATFileHandle.h"
Sissors 2:9af05743d551 27
Sissors 2:9af05743d551 28 FATFileHandle::FATFileHandle(FIL fh) {
Sissors 2:9af05743d551 29 _fh = fh;
Sissors 2:9af05743d551 30 }
Sissors 2:9af05743d551 31
Sissors 2:9af05743d551 32 int FATFileHandle::close() {
Sissors 2:9af05743d551 33 int retval = f_close(&_fh);
Sissors 2:9af05743d551 34 delete this;
Sissors 2:9af05743d551 35 return retval;
Sissors 2:9af05743d551 36 }
Sissors 2:9af05743d551 37
Sissors 2:9af05743d551 38 ssize_t FATFileHandle::write(const void* buffer, size_t length) {
Sissors 2:9af05743d551 39 UINT n;
Sissors 2:9af05743d551 40 FRESULT res = f_write(&_fh, buffer, length, &n);
Sissors 2:9af05743d551 41 if (res) {
Sissors 2:9af05743d551 42 debug_if(FFS_DBG, "f_write() failed: %d", res);
Sissors 2:9af05743d551 43 return -1;
Sissors 2:9af05743d551 44 }
Sissors 2:9af05743d551 45 return n;
Sissors 2:9af05743d551 46 }
Sissors 2:9af05743d551 47
Sissors 2:9af05743d551 48 ssize_t FATFileHandle::read(void* buffer, size_t length) {
Sissors 2:9af05743d551 49 debug_if(FFS_DBG, "read(%d)\n", length);
Sissors 2:9af05743d551 50 UINT n;
Sissors 2:9af05743d551 51 FRESULT res = f_read(&_fh, buffer, length, &n);
Sissors 2:9af05743d551 52 if (res) {
Sissors 2:9af05743d551 53 debug_if(FFS_DBG, "f_read() failed: %d\n", res);
Sissors 2:9af05743d551 54 return -1;
Sissors 2:9af05743d551 55 }
Sissors 2:9af05743d551 56 return n;
Sissors 2:9af05743d551 57 }
Sissors 2:9af05743d551 58
Sissors 2:9af05743d551 59 int FATFileHandle::isatty() {
Sissors 2:9af05743d551 60 return 0;
Sissors 2:9af05743d551 61 }
Sissors 2:9af05743d551 62
Sissors 2:9af05743d551 63 off_t FATFileHandle::lseek(off_t position, int whence) {
Sissors 2:9af05743d551 64 if (whence == SEEK_END) {
Sissors 2:9af05743d551 65 position += _fh.fsize;
Sissors 2:9af05743d551 66 } else if(whence==SEEK_CUR) {
Sissors 2:9af05743d551 67 position += _fh.fptr;
Sissors 2:9af05743d551 68 }
Sissors 2:9af05743d551 69 FRESULT res = f_lseek(&_fh, position);
Sissors 2:9af05743d551 70 if (res) {
Sissors 2:9af05743d551 71 debug_if(FFS_DBG, "lseek failed: %d\n", res);
Sissors 2:9af05743d551 72 return -1;
Sissors 2:9af05743d551 73 } else {
Sissors 2:9af05743d551 74 debug_if(FFS_DBG, "lseek OK, returning %i\n", _fh.fptr);
Sissors 2:9af05743d551 75 return _fh.fptr;
Sissors 2:9af05743d551 76 }
Sissors 2:9af05743d551 77 }
Sissors 2:9af05743d551 78
Sissors 2:9af05743d551 79 int FATFileHandle::fsync() {
Sissors 2:9af05743d551 80 FRESULT res = f_sync(&_fh);
Sissors 2:9af05743d551 81 if (res) {
Sissors 2:9af05743d551 82 debug_if(FFS_DBG, "f_sync() failed: %d\n", res);
Sissors 2:9af05743d551 83 return -1;
Sissors 2:9af05743d551 84 }
Sissors 2:9af05743d551 85 return 0;
Sissors 2:9af05743d551 86 }
Sissors 2:9af05743d551 87
Sissors 2:9af05743d551 88 off_t FATFileHandle::flen() {
Sissors 2:9af05743d551 89 return _fh.fsize;
Sissors 2:9af05743d551 90 }