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