Example writing to SD card, sford (original) 21 June 2011: Enhanced by D. Wendelboe to test SD file and directories. Illustrates the following: * rename a file (via copy & delete old). * copy an existing file to a new file. * listing of SD directory names. TODO: Add method to list filenames in a directory. Add a method to return file length.

Dependencies:   mbed

Committer:
YouTahDoug
Date:
Wed Jun 22 16:52:20 2011 +0000
Revision:
1:4a0985d3a848
Parent:
0:d0eea54553ed

        

Who changed what in which revision?

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