megan gimple / SDSD
Committer:
mgimple
Date:
Wed Nov 17 00:13:23 2021 +0000
Revision:
0:12e8a23fbb0c
SD

Who changed what in which revision?

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