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