Mark Schwarzer / SDFileSystem

Dependents:   Schwarzer_A7_1

Committer:
markschwarzer
Date:
Mon Nov 09 14:25:13 2020 +0000
Revision:
0:964d386ab059
logged data in SD card

Who changed what in which revision?

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