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