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