USBMSD SD card Hello World for Mbed platforms

Dependencies:   mbed USBMSD_SD USBDevice

Committer:
samux
Date:
Sun Dec 11 15:07:59 2011 +0000
Revision:
17:364ef42e502d
OK: will write doc

Who changed what in which revision?

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