Fork of Smoothie to port to mbed non-LPC targets.

Dependencies:   mbed

Fork of Smoothie by Stéphane Cachat

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SDCard.h Source File

SDCard.h

00001 /* mbed SDFileSystem Library, for providing file access to SD cards
00002  * Copyright (c) 2008-2010, sford
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020  * THE SOFTWARE.
00021  *
00022  *
00023  * This version significantly altered by Michael Moon and is (c) 2012
00024  */
00025 
00026 #ifndef SDCARD_H
00027 #define SDCARD_H
00028 
00029 #include "spi.h"
00030 #include "gpio.h"
00031 
00032 #include "disk.h"
00033 
00034 // #include "DMA.h"
00035 
00036 /** Access the filesystem on an SD Card using SPI
00037  *
00038  * @code
00039  * #include "mbed.h"
00040  * #include "SDFileSystem.h"
00041  *
00042  * SDFileSystem sd(p5, p6, p7, p12, "sd"); // mosi, miso, sclk, cs
00043  *
00044  * int main() {
00045  *     FILE *fp = fopen("/sd/myfile.txt", "w");
00046  *     fprintf(fp, "Hello World!\n");
00047  *     fclose(fp);
00048  * }
00049  */
00050 class SDCard : public MSD_Disk {
00051 public:
00052 
00053     /** Create the File System for accessing an SD Card using SPI
00054      *
00055      * @param mosi SPI mosi pin connected to SD Card
00056      * @param miso SPI miso pin conencted to SD Card
00057      * @param sclk SPI sclk pin connected to SD Card
00058      * @param cs   DigitalOut pin used as SD Card chip select
00059      * @param name The name used to access the virtual filesystem
00060      */
00061     SDCard(PinName, PinName, PinName, PinName);
00062 
00063     typedef enum {
00064         SDCARD_FAIL,
00065         SDCARD_V1,
00066         SDCARD_V2,
00067         SDCARD_V2HC
00068     } CARD_TYPE;
00069 
00070     virtual int disk_initialize();
00071     virtual int disk_write(const char *buffer, uint32_t block_number);
00072     virtual int disk_read(char *buffer, uint32_t block_number);
00073     virtual int disk_status();
00074     virtual int disk_sync();
00075     virtual uint32_t disk_sectors();
00076     virtual uint64_t disk_size();
00077     virtual uint32_t disk_blocksize();
00078     virtual bool disk_canDMA(void);
00079 
00080     CARD_TYPE card_type(void);
00081 
00082     void on_main_loop(void);
00083 
00084     bool busy();
00085 
00086 protected:
00087 
00088     int _cmd(int cmd, uint32_t arg);
00089     int _cmdx(int cmd, uint32_t arg);
00090     int _cmd8();
00091     int _cmd58(uint32_t*);
00092     CARD_TYPE initialise_card();
00093     CARD_TYPE initialise_card_v1();
00094     CARD_TYPE initialise_card_v2();
00095 
00096     int _read(char *buffer, int length);
00097     int _write(const char *buffer, int length);
00098 
00099     uint32_t _sd_sectors();
00100     uint32_t _sectors;
00101 
00102     ::SPI _spi;
00103     GPIO _cs;
00104 
00105     volatile bool busyflag;
00106 
00107     CARD_TYPE cardtype;
00108 };
00109 
00110 #endif