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:
Sun Jan 18 21:31:32 2015 +0000
Revision:
6:15b73dae124e
Parent:
2:9af05743d551
Added count argument for new USBDevice

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sissors 2:9af05743d551 1 /*---------------------------------------------------------------------------/
Sissors 2:9af05743d551 2 / FatFs - FAT file system module configuration file R0.09a (C)ChaN, 2012
Sissors 2:9af05743d551 3 /----------------------------------------------------------------------------/
Sissors 2:9af05743d551 4 /
Sissors 2:9af05743d551 5 / CAUTION! Do not forget to make clean the project after any changes to
Sissors 2:9af05743d551 6 / the configuration options.
Sissors 2:9af05743d551 7 /
Sissors 2:9af05743d551 8 /----------------------------------------------------------------------------*/
Sissors 2:9af05743d551 9 #ifndef _FFCONF
Sissors 2:9af05743d551 10 #define _FFCONF 4004 /* Revision ID */
Sissors 2:9af05743d551 11
Sissors 2:9af05743d551 12 #define FFS_DBG 0
Sissors 2:9af05743d551 13
Sissors 2:9af05743d551 14 /*---------------------------------------------------------------------------/
Sissors 2:9af05743d551 15 / Functions and Buffer Configurations
Sissors 2:9af05743d551 16 /----------------------------------------------------------------------------*/
Sissors 2:9af05743d551 17
Sissors 2:9af05743d551 18 #define _FS_TINY 1 /* 0:Normal or 1:Tiny */
Sissors 2:9af05743d551 19 /* When _FS_TINY is set to 1, FatFs uses the sector buffer in the file system
Sissors 2:9af05743d551 20 / object instead of the sector buffer in the individual file object for file
Sissors 2:9af05743d551 21 / data transfer. This reduces memory consumption 512 bytes each file object. */
Sissors 2:9af05743d551 22
Sissors 2:9af05743d551 23
Sissors 2:9af05743d551 24 #define _FS_READONLY 0 /* 0:Read/Write or 1:Read only */
Sissors 2:9af05743d551 25 /* Setting _FS_READONLY to 1 defines read only configuration. This removes
Sissors 2:9af05743d551 26 / writing functions, f_write, f_sync, f_unlink, f_mkdir, f_chmod, f_rename,
Sissors 2:9af05743d551 27 / f_truncate and useless f_getfree. */
Sissors 2:9af05743d551 28
Sissors 2:9af05743d551 29
Sissors 2:9af05743d551 30 #define _FS_MINIMIZE 0 /* 0 to 3 */
Sissors 2:9af05743d551 31 /* The _FS_MINIMIZE option defines minimization level to remove some functions.
Sissors 2:9af05743d551 32 /
Sissors 2:9af05743d551 33 / 0: Full function.
Sissors 2:9af05743d551 34 / 1: f_stat, f_getfree, f_unlink, f_mkdir, f_chmod, f_truncate and f_rename
Sissors 2:9af05743d551 35 / are removed.
Sissors 2:9af05743d551 36 / 2: f_opendir and f_readdir are removed in addition to 1.
Sissors 2:9af05743d551 37 / 3: f_lseek is removed in addition to 2. */
Sissors 2:9af05743d551 38
Sissors 2:9af05743d551 39
Sissors 2:9af05743d551 40 #define _USE_STRFUNC 0 /* 0:Disable or 1-2:Enable */
Sissors 2:9af05743d551 41 /* To enable string functions, set _USE_STRFUNC to 1 or 2. */
Sissors 2:9af05743d551 42
Sissors 2:9af05743d551 43
Sissors 2:9af05743d551 44 #define _USE_MKFS 1 /* 0:Disable or 1:Enable */
Sissors 2:9af05743d551 45 /* To enable f_mkfs function, set _USE_MKFS to 1 and set _FS_READONLY to 0 */
Sissors 2:9af05743d551 46
Sissors 2:9af05743d551 47
Sissors 2:9af05743d551 48 #define _USE_FORWARD 0 /* 0:Disable or 1:Enable */
Sissors 2:9af05743d551 49 /* To enable f_forward function, set _USE_FORWARD to 1 and set _FS_TINY to 1. */
Sissors 2:9af05743d551 50
Sissors 2:9af05743d551 51
Sissors 2:9af05743d551 52 #define _USE_FASTSEEK 0 /* 0:Disable or 1:Enable */
Sissors 2:9af05743d551 53 /* To enable fast seek feature, set _USE_FASTSEEK to 1. */
Sissors 2:9af05743d551 54
Sissors 2:9af05743d551 55
Sissors 2:9af05743d551 56
Sissors 2:9af05743d551 57 /*---------------------------------------------------------------------------/
Sissors 2:9af05743d551 58 / Locale and Namespace Configurations
Sissors 2:9af05743d551 59 /----------------------------------------------------------------------------*/
Sissors 2:9af05743d551 60
Sissors 2:9af05743d551 61 #define _CODE_PAGE 858
Sissors 2:9af05743d551 62 /* The _CODE_PAGE specifies the OEM code page to be used on the target system.
Sissors 2:9af05743d551 63 / Incorrect setting of the code page can cause a file open failure.
Sissors 2:9af05743d551 64 /
Sissors 2:9af05743d551 65 / 932 - Japanese Shift-JIS (DBCS, OEM, Windows)
Sissors 2:9af05743d551 66 / 936 - Simplified Chinese GBK (DBCS, OEM, Windows)
Sissors 2:9af05743d551 67 / 949 - Korean (DBCS, OEM, Windows)
Sissors 2:9af05743d551 68 / 950 - Traditional Chinese Big5 (DBCS, OEM, Windows)
Sissors 2:9af05743d551 69 / 1250 - Central Europe (Windows)
Sissors 2:9af05743d551 70 / 1251 - Cyrillic (Windows)
Sissors 2:9af05743d551 71 / 1252 - Latin 1 (Windows)
Sissors 2:9af05743d551 72 / 1253 - Greek (Windows)
Sissors 2:9af05743d551 73 / 1254 - Turkish (Windows)
Sissors 2:9af05743d551 74 / 1255 - Hebrew (Windows)
Sissors 2:9af05743d551 75 / 1256 - Arabic (Windows)
Sissors 2:9af05743d551 76 / 1257 - Baltic (Windows)
Sissors 2:9af05743d551 77 / 1258 - Vietnam (OEM, Windows)
Sissors 2:9af05743d551 78 / 437 - U.S. (OEM)
Sissors 2:9af05743d551 79 / 720 - Arabic (OEM)
Sissors 2:9af05743d551 80 / 737 - Greek (OEM)
Sissors 2:9af05743d551 81 / 775 - Baltic (OEM)
Sissors 2:9af05743d551 82 / 850 - Multilingual Latin 1 (OEM)
Sissors 2:9af05743d551 83 / 858 - Multilingual Latin 1 + Euro (OEM)
Sissors 2:9af05743d551 84 / 852 - Latin 2 (OEM)
Sissors 2:9af05743d551 85 / 855 - Cyrillic (OEM)
Sissors 2:9af05743d551 86 / 866 - Russian (OEM)
Sissors 2:9af05743d551 87 / 857 - Turkish (OEM)
Sissors 2:9af05743d551 88 / 862 - Hebrew (OEM)
Sissors 2:9af05743d551 89 / 874 - Thai (OEM, Windows)
Sissors 2:9af05743d551 90 / 1 - ASCII only (Valid for non LFN cfg.)
Sissors 2:9af05743d551 91 */
Sissors 2:9af05743d551 92
Sissors 2:9af05743d551 93
Sissors 2:9af05743d551 94 #define _USE_LFN 1 /* 0 to 3 */
Sissors 2:9af05743d551 95 #define _MAX_LFN 255 /* Maximum LFN length to handle (12 to 255) */
Sissors 2:9af05743d551 96 /* The _USE_LFN option switches the LFN support.
Sissors 2:9af05743d551 97 /
Sissors 2:9af05743d551 98 / 0: Disable LFN feature. _MAX_LFN and _LFN_UNICODE have no effect.
Sissors 2:9af05743d551 99 / 1: Enable LFN with static working buffer on the BSS. Always NOT reentrant.
Sissors 2:9af05743d551 100 / 2: Enable LFN with dynamic working buffer on the STACK.
Sissors 2:9af05743d551 101 / 3: Enable LFN with dynamic working buffer on the HEAP.
Sissors 2:9af05743d551 102 /
Sissors 2:9af05743d551 103 / The LFN working buffer occupies (_MAX_LFN + 1) * 2 bytes. To enable LFN,
Sissors 2:9af05743d551 104 / Unicode handling functions ff_convert() and ff_wtoupper() must be added
Sissors 2:9af05743d551 105 / to the project. When enable to use heap, memory control functions
Sissors 2:9af05743d551 106 / ff_memalloc() and ff_memfree() must be added to the project. */
Sissors 2:9af05743d551 107
Sissors 2:9af05743d551 108
Sissors 2:9af05743d551 109 #define _LFN_UNICODE 0 /* 0:ANSI/OEM or 1:Unicode */
Sissors 2:9af05743d551 110 /* To switch the character code set on FatFs API to Unicode,
Sissors 2:9af05743d551 111 / enable LFN feature and set _LFN_UNICODE to 1. */
Sissors 2:9af05743d551 112
Sissors 2:9af05743d551 113
Sissors 2:9af05743d551 114 #define _FS_RPATH 0 /* 0 to 2 */
Sissors 2:9af05743d551 115 /* The _FS_RPATH option configures relative path feature.
Sissors 2:9af05743d551 116 /
Sissors 2:9af05743d551 117 / 0: Disable relative path feature and remove related functions.
Sissors 2:9af05743d551 118 / 1: Enable relative path. f_chdrive() and f_chdir() are available.
Sissors 2:9af05743d551 119 / 2: f_getcwd() is available in addition to 1.
Sissors 2:9af05743d551 120 /
Sissors 2:9af05743d551 121 / Note that output of the f_readdir fnction is affected by this option. */
Sissors 2:9af05743d551 122
Sissors 2:9af05743d551 123
Sissors 2:9af05743d551 124
Sissors 2:9af05743d551 125 /*---------------------------------------------------------------------------/
Sissors 2:9af05743d551 126 / Physical Drive Configurations
Sissors 2:9af05743d551 127 /----------------------------------------------------------------------------*/
Sissors 2:9af05743d551 128
Sissors 2:9af05743d551 129 #define _VOLUMES 1
Sissors 2:9af05743d551 130 /* Number of volumes (logical drives) to be used. */
Sissors 2:9af05743d551 131
Sissors 6:15b73dae124e 132
Sissors 2:9af05743d551 133 #define _MAX_SS 512 /* 512, 1024, 2048 or 4096 */
Sissors 2:9af05743d551 134 /* Maximum sector size to be handled.
Sissors 2:9af05743d551 135 / Always set 512 for memory card and hard disk but a larger value may be
Sissors 2:9af05743d551 136 / required for on-board flash memory, floppy disk and optical disk.
Sissors 2:9af05743d551 137 / When _MAX_SS is larger than 512, it configures FatFs to variable sector size
Sissors 2:9af05743d551 138 / and GET_SECTOR_SIZE command must be implememted to the disk_ioctl function. */
Sissors 2:9af05743d551 139
Sissors 2:9af05743d551 140
Sissors 2:9af05743d551 141 #define _MULTI_PARTITION 0 /* 0:Single partition, 1/2:Enable multiple partition */
Sissors 2:9af05743d551 142 /* When set to 0, each volume is bound to the same physical drive number and
Sissors 2:9af05743d551 143 / it can mount only first primaly partition. When it is set to 1, each volume
Sissors 2:9af05743d551 144 / is tied to the partitions listed in VolToPart[]. */
Sissors 2:9af05743d551 145
Sissors 2:9af05743d551 146
Sissors 2:9af05743d551 147 #define _USE_ERASE 0 /* 0:Disable or 1:Enable */
Sissors 2:9af05743d551 148 /* To enable sector erase feature, set _USE_ERASE to 1. CTRL_ERASE_SECTOR command
Sissors 2:9af05743d551 149 / should be added to the disk_ioctl functio. */
Sissors 2:9af05743d551 150
Sissors 2:9af05743d551 151
Sissors 2:9af05743d551 152
Sissors 2:9af05743d551 153 /*---------------------------------------------------------------------------/
Sissors 2:9af05743d551 154 / System Configurations
Sissors 2:9af05743d551 155 /----------------------------------------------------------------------------*/
Sissors 2:9af05743d551 156
Sissors 2:9af05743d551 157 #define _WORD_ACCESS 0 /* 0 or 1 */
Sissors 2:9af05743d551 158 /* Set 0 first and it is always compatible with all platforms. The _WORD_ACCESS
Sissors 2:9af05743d551 159 / option defines which access method is used to the word data on the FAT volume.
Sissors 2:9af05743d551 160 /
Sissors 2:9af05743d551 161 / 0: Byte-by-byte access.
Sissors 2:9af05743d551 162 / 1: Word access. Do not choose this unless following condition is met.
Sissors 2:9af05743d551 163 /
Sissors 2:9af05743d551 164 / When the byte order on the memory is big-endian or address miss-aligned word
Sissors 2:9af05743d551 165 / access results incorrect behavior, the _WORD_ACCESS must be set to 0.
Sissors 2:9af05743d551 166 / If it is not the case, the value can also be set to 1 to improve the
Sissors 2:9af05743d551 167 / performance and code size.
Sissors 2:9af05743d551 168 */
Sissors 2:9af05743d551 169
Sissors 2:9af05743d551 170
Sissors 2:9af05743d551 171 /* A header file that defines sync object types on the O/S, such as
Sissors 2:9af05743d551 172 / windows.h, ucos_ii.h and semphr.h, must be included prior to ff.h. */
Sissors 2:9af05743d551 173
Sissors 2:9af05743d551 174 #define _FS_REENTRANT 0 /* 0:Disable or 1:Enable */
Sissors 2:9af05743d551 175 #define _FS_TIMEOUT 1000 /* Timeout period in unit of time ticks */
Sissors 2:9af05743d551 176 #define _SYNC_t HANDLE /* O/S dependent type of sync object. e.g. HANDLE, OS_EVENT*, ID and etc.. */
Sissors 2:9af05743d551 177
Sissors 2:9af05743d551 178 /* The _FS_REENTRANT option switches the reentrancy (thread safe) of the FatFs module.
Sissors 2:9af05743d551 179 /
Sissors 2:9af05743d551 180 / 0: Disable reentrancy. _SYNC_t and _FS_TIMEOUT have no effect.
Sissors 2:9af05743d551 181 / 1: Enable reentrancy. Also user provided synchronization handlers,
Sissors 2:9af05743d551 182 / ff_req_grant, ff_rel_grant, ff_del_syncobj and ff_cre_syncobj
Sissors 2:9af05743d551 183 / function must be added to the project. */
Sissors 2:9af05743d551 184
Sissors 2:9af05743d551 185
Sissors 2:9af05743d551 186 #define _FS_LOCK 0 /* 0:Disable or >=1:Enable */
Sissors 2:9af05743d551 187 /* To enable file lock control feature, set _FS_LOCK to 1 or greater.
Sissors 2:9af05743d551 188 The value defines how many files can be opened simultaneously. */
Sissors 2:9af05743d551 189
Sissors 6:15b73dae124e 190 #define FLUSH_ON_NEW_CLUSTER 0 /* Sync the file on every new cluster */
Sissors 6:15b73dae124e 191 #define FLUSH_ON_NEW_SECTOR 1 /* Sync the file on every new sector */
Sissors 6:15b73dae124e 192 /* Only one of these two defines needs to be set to 1. If both are set to 0
Sissors 6:15b73dae124e 193 the file is only sync when closed.
Sissors 6:15b73dae124e 194 Clusters are group of sectors (eg: 8 sectors). Flushing on new cluster means
Sissors 6:15b73dae124e 195 it would be less often than flushing on new sector. Sectors are generally
Sissors 6:15b73dae124e 196 512 Bytes long. */
Sissors 2:9af05743d551 197
Sissors 2:9af05743d551 198 #endif /* _FFCONFIG */