SD library

Committer:
el14afma
Date:
Wed Apr 27 06:51:28 2016 +0000
Revision:
0:5ffb234d134c
-Save highscore and settings on a sd card

Who changed what in which revision?

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