USBMSD SD card Hello World for Mbed platforms

Dependencies:   mbed USBMSD_SD USBDevice

Committer:
samux
Date:
Mon Nov 14 17:50:02 2011 +0000
Revision:
8:534fd41d8cc7
Parent:
7:6494da2a5c60
Child:
9:9c343b9ee6d8
doesn\'t work...

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 7:6494da2a5c60 1 /* mbed SDFileSystem Library, for providing file access to SD cards
samux 7:6494da2a5c60 2 * Copyright (c) 2008-2010, sford
samux 7:6494da2a5c60 3 *
samux 7:6494da2a5c60 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
samux 7:6494da2a5c60 5 * of this software and associated documentation files (the "Software"), to deal
samux 7:6494da2a5c60 6 * in the Software without restriction, including without limitation the rights
samux 7:6494da2a5c60 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
samux 7:6494da2a5c60 8 * copies of the Software, and to permit persons to whom the Software is
samux 7:6494da2a5c60 9 * furnished to do so, subject to the following conditions:
samux 7:6494da2a5c60 10 *
samux 7:6494da2a5c60 11 * The above copyright notice and this permission notice shall be included in
samux 7:6494da2a5c60 12 * all copies or substantial portions of the Software.
samux 7:6494da2a5c60 13 *
samux 7:6494da2a5c60 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
samux 7:6494da2a5c60 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
samux 7:6494da2a5c60 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
samux 7:6494da2a5c60 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
samux 7:6494da2a5c60 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
samux 7:6494da2a5c60 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
samux 7:6494da2a5c60 20 * THE SOFTWARE.
samux 7:6494da2a5c60 21 */
samux 7:6494da2a5c60 22
samux 7:6494da2a5c60 23 #ifndef MBED_SDFILESYSTEM_H
samux 7:6494da2a5c60 24 #define MBED_SDFILESYSTEM_H
samux 7:6494da2a5c60 25
samux 7:6494da2a5c60 26 #include "mbed.h"
samux 7:6494da2a5c60 27 #include "FATFileSystem.h"
samux 8:534fd41d8cc7 28 #include "USBMSD.h"
samux 8:534fd41d8cc7 29
samux 8:534fd41d8cc7 30 // MSC Bulk-only Stage
samux 8:534fd41d8cc7 31 enum STATUS {
samux 8:534fd41d8cc7 32 NOINIT = (1 << 0),
samux 8:534fd41d8cc7 33 NODISK = (1 << 1),
samux 8:534fd41d8cc7 34 PROTECT = (1 << 2),
samux 8:534fd41d8cc7 35 };
samux 7:6494da2a5c60 36
samux 7:6494da2a5c60 37 /** Access the filesystem on an SD Card using SPI
samux 7:6494da2a5c60 38 *
samux 7:6494da2a5c60 39 * @code
samux 7:6494da2a5c60 40 * #include "mbed.h"
samux 7:6494da2a5c60 41 * #include "SDFileSystem.h"
samux 7:6494da2a5c60 42 *
samux 7:6494da2a5c60 43 * SDFileSystem sd(p5, p6, p7, p12, "sd"); // mosi, miso, sclk, cs
samux 7:6494da2a5c60 44 *
samux 7:6494da2a5c60 45 * int main() {
samux 7:6494da2a5c60 46 * FILE *fp = fopen("/sd/myfile.txt", "w");
samux 7:6494da2a5c60 47 * fprintf(fp, "Hello World!\n");
samux 7:6494da2a5c60 48 * fclose(fp);
samux 7:6494da2a5c60 49 * }
samux 7:6494da2a5c60 50 */
samux 8:534fd41d8cc7 51 class SDFileSystem : public USBMSD, FATFileSystem {
samux 7:6494da2a5c60 52 public:
samux 7:6494da2a5c60 53
samux 7:6494da2a5c60 54 /** Create the File System for accessing an SD Card using SPI
samux 7:6494da2a5c60 55 *
samux 7:6494da2a5c60 56 * @param mosi SPI mosi pin connected to SD Card
samux 7:6494da2a5c60 57 * @param miso SPI miso pin conencted to SD Card
samux 7:6494da2a5c60 58 * @param sclk SPI sclk pin connected to SD Card
samux 7:6494da2a5c60 59 * @param cs DigitalOut pin used as SD Card chip select
samux 7:6494da2a5c60 60 * @param name The name used to access the virtual filesystem
samux 7:6494da2a5c60 61 */
samux 7:6494da2a5c60 62 SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name);
samux 7:6494da2a5c60 63 virtual int disk_initialize();
samux 7:6494da2a5c60 64 virtual int disk_write(const char *buffer, int block_number);
samux 7:6494da2a5c60 65 virtual int disk_read(char *buffer, int block_number);
samux 7:6494da2a5c60 66 virtual int disk_status();
samux 7:6494da2a5c60 67 virtual int disk_sync();
samux 7:6494da2a5c60 68 virtual int disk_sectors();
samux 8:534fd41d8cc7 69 virtual void set_status(int st);
samux 7:6494da2a5c60 70
samux 7:6494da2a5c60 71 protected:
samux 7:6494da2a5c60 72
samux 7:6494da2a5c60 73 int _cmd(int cmd, int arg);
samux 7:6494da2a5c60 74 int _cmdx(int cmd, int arg);
samux 7:6494da2a5c60 75 int _cmd8();
samux 7:6494da2a5c60 76 int _cmd58();
samux 7:6494da2a5c60 77 int initialise_card();
samux 7:6494da2a5c60 78 int initialise_card_v1();
samux 7:6494da2a5c60 79 int initialise_card_v2();
samux 7:6494da2a5c60 80
samux 7:6494da2a5c60 81 int _read(char *buffer, int length);
samux 7:6494da2a5c60 82 int _write(const char *buffer, int length);
samux 7:6494da2a5c60 83 int _sd_sectors();
samux 7:6494da2a5c60 84 int _sectors;
samux 7:6494da2a5c60 85
samux 8:534fd41d8cc7 86 int status;
samux 8:534fd41d8cc7 87
samux 7:6494da2a5c60 88 SPI _spi;
samux 7:6494da2a5c60 89 DigitalOut _cs;
samux 7:6494da2a5c60 90 };
samux 7:6494da2a5c60 91
samux 7:6494da2a5c60 92 #endif