USBMSD SD card Hello World for Mbed platforms
Dependencies: mbed USBMSD_SD USBDevice
ChaNFSSD/SDFileSystem.h@16:c753717bfd4d, 2011-12-11 (annotated)
- Committer:
- samux
- Date:
- Sun Dec 11 13:35:44 2011 +0000
- Revision:
- 16:c753717bfd4d
- Parent:
- 14:757226626acb
GOOD: works with M0 AND M3
Who changed what in which revision?
User | Revision | Line number | New 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 |
7:6494da2a5c60 | 30 | /** Access the filesystem on an SD Card using SPI |
samux |
7:6494da2a5c60 | 31 | * |
samux |
7:6494da2a5c60 | 32 | * @code |
samux |
7:6494da2a5c60 | 33 | * #include "mbed.h" |
samux |
7:6494da2a5c60 | 34 | * #include "SDFileSystem.h" |
samux |
7:6494da2a5c60 | 35 | * |
samux |
7:6494da2a5c60 | 36 | * SDFileSystem sd(p5, p6, p7, p12, "sd"); // mosi, miso, sclk, cs |
samux |
7:6494da2a5c60 | 37 | * |
samux |
7:6494da2a5c60 | 38 | * int main() { |
samux |
7:6494da2a5c60 | 39 | * FILE *fp = fopen("/sd/myfile.txt", "w"); |
samux |
7:6494da2a5c60 | 40 | * fprintf(fp, "Hello World!\n"); |
samux |
7:6494da2a5c60 | 41 | * fclose(fp); |
samux |
7:6494da2a5c60 | 42 | * } |
samux |
7:6494da2a5c60 | 43 | */ |
samux |
9:9c343b9ee6d8 | 44 | class SDFileSystem : public USBMSD, public FATFileSystem { |
samux |
7:6494da2a5c60 | 45 | public: |
samux |
7:6494da2a5c60 | 46 | |
samux |
7:6494da2a5c60 | 47 | /** Create the File System for accessing an SD Card using SPI |
samux |
7:6494da2a5c60 | 48 | * |
samux |
7:6494da2a5c60 | 49 | * @param mosi SPI mosi pin connected to SD Card |
samux |
7:6494da2a5c60 | 50 | * @param miso SPI miso pin conencted to SD Card |
samux |
7:6494da2a5c60 | 51 | * @param sclk SPI sclk pin connected to SD Card |
samux |
7:6494da2a5c60 | 52 | * @param cs DigitalOut pin used as SD Card chip select |
samux |
7:6494da2a5c60 | 53 | * @param name The name used to access the virtual filesystem |
samux |
7:6494da2a5c60 | 54 | */ |
samux |
7:6494da2a5c60 | 55 | SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name); |
samux |
7:6494da2a5c60 | 56 | virtual int disk_initialize(); |
samux |
7:6494da2a5c60 | 57 | virtual int disk_write(const char *buffer, int block_number); |
samux |
7:6494da2a5c60 | 58 | virtual int disk_read(char *buffer, int block_number); |
samux |
7:6494da2a5c60 | 59 | virtual int disk_status(); |
samux |
7:6494da2a5c60 | 60 | virtual int disk_sync(); |
samux |
7:6494da2a5c60 | 61 | virtual int disk_sectors(); |
samux |
13:32b8a767cf0e | 62 | virtual int disk_size(); |
samux |
7:6494da2a5c60 | 63 | |
samux |
7:6494da2a5c60 | 64 | protected: |
samux |
7:6494da2a5c60 | 65 | |
samux |
7:6494da2a5c60 | 66 | int _cmd(int cmd, int arg); |
samux |
7:6494da2a5c60 | 67 | int _cmdx(int cmd, int arg); |
samux |
7:6494da2a5c60 | 68 | int _cmd8(); |
samux |
7:6494da2a5c60 | 69 | int _cmd58(); |
samux |
7:6494da2a5c60 | 70 | int initialise_card(); |
samux |
7:6494da2a5c60 | 71 | int initialise_card_v1(); |
samux |
7:6494da2a5c60 | 72 | int initialise_card_v2(); |
samux |
7:6494da2a5c60 | 73 | |
samux |
7:6494da2a5c60 | 74 | int _read(char *buffer, int length); |
samux |
7:6494da2a5c60 | 75 | int _write(const char *buffer, int length); |
samux |
7:6494da2a5c60 | 76 | int _sd_sectors(); |
samux |
7:6494da2a5c60 | 77 | int _sectors; |
samux |
7:6494da2a5c60 | 78 | |
samux |
13:32b8a767cf0e | 79 | int capacity; |
samux |
13:32b8a767cf0e | 80 | |
samux |
7:6494da2a5c60 | 81 | SPI _spi; |
samux |
7:6494da2a5c60 | 82 | DigitalOut _cs; |
samux |
7:6494da2a5c60 | 83 | }; |
samux |
7:6494da2a5c60 | 84 | |
samux |
7:6494da2a5c60 | 85 | #endif |