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