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