Madeline Kistler / SDFileSystem2
Committer:
mkistler
Date:
Tue Nov 17 20:30:08 2020 +0000
Revision:
0:812e8b0b2832
Final draft.

Who changed what in which revision?

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