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.

USBSDFileSystem.h

Committer:
karelv
Date:
2013-08-27
Revision:
1:88089dd56f0e
Parent:
0:c3d2e89ca30d

File content as of revision 1:88089dd56f0e:

#ifndef MBED_SDFILESYSTEM_H
#define MBED_SDFILESYSTEM_H

#include "mbed.h"
#include "USBFileSystem.h"
#include <stdint.h>

/** Access the filesystem on an SD Card using SPI
 *
 * @code
 * #include "mbed.h"
 * #include "SDFileSystem.h"
 *
 * USBSDFileSystem sd("sd", p5, p6, p7, p12); // mosi, miso, sclk, cs
 *  
 * int main() {
 *     FILE *fp = fopen("/sd/myfile.txt", "w");
 *     fprintf(fp, "Hello World!\n");
 *     fclose(fp);
 * }
 */
class USBSDFileSystem : public USBFileSystem {
public:

    /** Create the File System for accessing an SD Card using SPI
     *
     * @param mosi SPI mosi pin connected to SD Card
     * @param miso SPI miso pin conencted to SD Card
     * @param sclk SPI sclk pin connected to SD Card
     * @param cs   DigitalOut pin used as SD Card chip select
     * @param name The name used to access the virtual filesystem
     */
    USBSDFileSystem(const char *name, PinName mosi, PinName miso, PinName sclk, PinName cs);
    virtual int disk_initialize();
    virtual int _disk_status();
    virtual int disk_read(uint8_t * buffer, uint64_t block_number);
    virtual int _disk_write(const uint8_t * buffer, uint64_t block_number);
    virtual int disk_sync();
    virtual uint64_t disk_sectors();

protected:

    int _cmd(int cmd, int arg);
    int _cmdx(int cmd, int arg);
    int _cmd8();
    int _cmd58();
    int initialise_card();
    int initialise_card_v1();
    int initialise_card_v2();
    
    int _read(uint8_t * buffer, uint32_t length);
    int _write(const uint8_t *buffer, uint32_t length);
    uint64_t _sd_sectors();
    uint64_t _sectors;
    
    uint8_t _status;

    SPI _spi;
    DigitalOut _cs;
    int cdv;
};

#endif