FINAL VERSION

Dependencies:   mbed

Committer:
jamesheavey
Date:
Thu May 09 14:36:51 2019 +0000
Revision:
140:d8634e76ecce
Parent:
101:0df767523dbd
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

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