Using the USBFileSystem class to provide SD-card with USBMSD and FATFileSystem support at same time.

Dependents:   SD_USB_FS_HelloWorld

See also helloworld program at http://mbed.org/users/karelv/code/SD_USB_FS_HelloWorld/

Created based on:

  1. comment from Erik Olieman. forum
  2. USBMSD_SD library for the spi-access to SD-card. (no dependency but spi-access code is copied) USBMSD_SD
  3. USBFileSystem libary USBFileSystem

Information

My win7 pc always disconnect/connect the drive and at disconnect it also closes the explorer which show the content of that drive. It does the same with the editor that has the file opened.

Quote:

(by Erik Olieman)

You are correct that that is what happens, got the same on windows 8. The problem is that windows (and probably every other OS) is quite heavy on caching of USB drives. So it reads the data ones, and after that it only writes and assumes its commands have been performed. For example if you have a drive with a bunch of files, and I enable write protection on the library, you can still happily delete the file and windows will tell you it is gone. You can make new files and windows will tell you they are created. Despite nothing actually being done. So the only way to force a refresh is disconnecting/reconnecting. Well not entirely true, I found out if you are writing custom programs you can disable this caching behavior after alot of digging. But in principle it is always on, and you have to reconnect the device. An alternative file browser might have other behavior, but dunno.

So the TL;DR: It is OS behavior that the program can't do anything about. Initially I intended to just use write protection to block the computer from writing, which is why it is still the default, but it doesn't actually work like you want it to work for 99% of the use cases. So reconnecting it is.

Committer:
karelv
Date:
Tue Aug 27 19:41:48 2013 +0000
Revision:
1:88089dd56f0e
Parent:
0:c3d2e89ca30d
update after recommendations of Erik Olieman

Who changed what in which revision?

UserRevisionLine numberNew contents of line
karelv 0:c3d2e89ca30d 1 #ifndef MBED_SDFILESYSTEM_H
karelv 0:c3d2e89ca30d 2 #define MBED_SDFILESYSTEM_H
karelv 0:c3d2e89ca30d 3
karelv 0:c3d2e89ca30d 4 #include "mbed.h"
karelv 0:c3d2e89ca30d 5 #include "USBFileSystem.h"
karelv 0:c3d2e89ca30d 6 #include <stdint.h>
karelv 0:c3d2e89ca30d 7
karelv 0:c3d2e89ca30d 8 /** Access the filesystem on an SD Card using SPI
karelv 0:c3d2e89ca30d 9 *
karelv 0:c3d2e89ca30d 10 * @code
karelv 0:c3d2e89ca30d 11 * #include "mbed.h"
karelv 0:c3d2e89ca30d 12 * #include "SDFileSystem.h"
karelv 0:c3d2e89ca30d 13 *
karelv 0:c3d2e89ca30d 14 * USBSDFileSystem sd("sd", p5, p6, p7, p12); // mosi, miso, sclk, cs
karelv 0:c3d2e89ca30d 15 *
karelv 0:c3d2e89ca30d 16 * int main() {
karelv 0:c3d2e89ca30d 17 * FILE *fp = fopen("/sd/myfile.txt", "w");
karelv 0:c3d2e89ca30d 18 * fprintf(fp, "Hello World!\n");
karelv 0:c3d2e89ca30d 19 * fclose(fp);
karelv 0:c3d2e89ca30d 20 * }
karelv 0:c3d2e89ca30d 21 */
karelv 0:c3d2e89ca30d 22 class USBSDFileSystem : public USBFileSystem {
karelv 0:c3d2e89ca30d 23 public:
karelv 0:c3d2e89ca30d 24
karelv 0:c3d2e89ca30d 25 /** Create the File System for accessing an SD Card using SPI
karelv 0:c3d2e89ca30d 26 *
karelv 0:c3d2e89ca30d 27 * @param mosi SPI mosi pin connected to SD Card
karelv 0:c3d2e89ca30d 28 * @param miso SPI miso pin conencted to SD Card
karelv 0:c3d2e89ca30d 29 * @param sclk SPI sclk pin connected to SD Card
karelv 0:c3d2e89ca30d 30 * @param cs DigitalOut pin used as SD Card chip select
karelv 0:c3d2e89ca30d 31 * @param name The name used to access the virtual filesystem
karelv 0:c3d2e89ca30d 32 */
karelv 0:c3d2e89ca30d 33 USBSDFileSystem(const char *name, PinName mosi, PinName miso, PinName sclk, PinName cs);
karelv 0:c3d2e89ca30d 34 virtual int disk_initialize();
karelv 0:c3d2e89ca30d 35 virtual int _disk_status();
karelv 0:c3d2e89ca30d 36 virtual int disk_read(uint8_t * buffer, uint64_t block_number);
karelv 0:c3d2e89ca30d 37 virtual int _disk_write(const uint8_t * buffer, uint64_t block_number);
karelv 0:c3d2e89ca30d 38 virtual int disk_sync();
karelv 0:c3d2e89ca30d 39 virtual uint64_t disk_sectors();
karelv 0:c3d2e89ca30d 40
karelv 0:c3d2e89ca30d 41 protected:
karelv 0:c3d2e89ca30d 42
karelv 0:c3d2e89ca30d 43 int _cmd(int cmd, int arg);
karelv 0:c3d2e89ca30d 44 int _cmdx(int cmd, int arg);
karelv 0:c3d2e89ca30d 45 int _cmd8();
karelv 0:c3d2e89ca30d 46 int _cmd58();
karelv 0:c3d2e89ca30d 47 int initialise_card();
karelv 0:c3d2e89ca30d 48 int initialise_card_v1();
karelv 0:c3d2e89ca30d 49 int initialise_card_v2();
karelv 0:c3d2e89ca30d 50
karelv 0:c3d2e89ca30d 51 int _read(uint8_t * buffer, uint32_t length);
karelv 0:c3d2e89ca30d 52 int _write(const uint8_t *buffer, uint32_t length);
karelv 0:c3d2e89ca30d 53 uint64_t _sd_sectors();
karelv 0:c3d2e89ca30d 54 uint64_t _sectors;
karelv 0:c3d2e89ca30d 55
karelv 0:c3d2e89ca30d 56 uint8_t _status;
karelv 0:c3d2e89ca30d 57
karelv 0:c3d2e89ca30d 58 SPI _spi;
karelv 0:c3d2e89ca30d 59 DigitalOut _cs;
karelv 0:c3d2e89ca30d 60 int cdv;
karelv 0:c3d2e89ca30d 61 };
karelv 0:c3d2e89ca30d 62
karelv 0:c3d2e89ca30d 63 #endif