Example SDFileSystem in which FATDirHandle exposes the file info struct of the current file/directory.

Committer:
uci1
Date:
Fri Nov 28 05:12:38 2014 +0000
Revision:
1:a7d8fc28a863
Parent:
0:687056ba3278
change SD init so it won't stall on failed initialization

Who changed what in which revision?

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