Joris Aerts / SDFileSystem

Dependencies:   FATFileSystem

Fork of SDFileSystem by Neil Thiessen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SDFileSystem.h Source File

SDFileSystem.h

00001 /* SD/MMC File System Library
00002  * Copyright (c) 2014 Neil Thiessen
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef SD_FILE_SYSTEM_H
00018 #define SD_FILE_SYSTEM_H
00019 
00020 #include "mbed.h"
00021 #include "FATFileSystem.h"
00022 #include <stdint.h>
00023 
00024 /** SDFileSystem class.
00025  *  Used for creating a virtual file system for accessing SD/MMC cards via SPI.
00026  *
00027  * Example:
00028  * @code
00029  * #include "mbed.h"
00030  * #include "SDFileSystem.h"
00031  *
00032  * //Create an SDFileSystem object
00033  * SDFileSystem sd(p5, p6, p7, p20, "sd", p22, SDFileSystem::SWITCH_NEG_NO);
00034  *
00035  * int main()
00036  * {
00037  *     //Perform a write test
00038  *     printf("\nWriting to SD card...");
00039  *     FILE *fp = fopen("/sd/sdtest.txt", "w");
00040  *     if (fp != NULL) {
00041  *         fprintf(fp, "We're writing to an SD card!");
00042  *         fclose(fp);
00043  *         printf("success!\n");
00044  *     } else {
00045  *         printf("failed!\n");
00046  *     }
00047  *
00048  *     //Perform a read test
00049  *     printf("Reading from SD card...");
00050  *     fp = fopen("/sd/sdtest.txt", "r");
00051  *     if (fp != NULL) {
00052  *         char c = fgetc(fp);
00053  *         if (c == 'W')
00054  *             printf("success!\n");
00055  *         else
00056  *             printf("incorrect char (%c)!\n", c);
00057  *         fclose(fp);
00058  *     } else {
00059  *         printf("failed!\n");
00060  *     }
00061  * }
00062  * @endcode
00063  */
00064 class SDFileSystem : public FATFileSystem
00065 {
00066 public:
00067     /** Represents the different card detect switch types
00068      */
00069     enum SwitchType {
00070         SWITCH_NONE,    /**< Switch not used */
00071         SWITCH_POS_NO,  /**< Switch shorts to VDD when the socket is occupied (positive logic, normally open) */
00072         SWITCH_POS_NC,  /**< Switch shorts to VDD when the socket is empty (positive logic, normally closed) */
00073         SWITCH_NEG_NO,  /**< Switch shorts to GND when the socket is occupied (negative logic, normally open) */
00074         SWITCH_NEG_NC   /**< Switch shorts to GND when the socket is empty (negative logic, normally closed) */
00075     };
00076 
00077     /** Represents the different SD/MMC card types
00078      */
00079     enum CardType {
00080         CARD_NONE,      /**< No card is present */
00081         CARD_MMC,       /**< MMC card */
00082         CARD_SD,        /**< Standard capacity SD card */
00083         CARD_SDHC,      /**< High capacity SD card */
00084         CARD_UNKNOWN    /**< Unknown or unsupported card */
00085     };
00086 
00087     /** Create a virtual file system for accessing SD/MMC cards via SPI
00088      *
00089      * @param mosi The SPI data out pin.
00090      * @param miso The SPI data in pin.
00091      * @param sclk The SPI clock pin.
00092      * @param cs The SPI chip select pin.
00093      * @param name The name used to access the virtual filesystem.
00094      * @param cd The card detect pin.
00095      * @param cdtype The type of card detect switch.
00096      * @param hz The SPI bus frequency (defaults to 1MHz).
00097      */
00098     SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name, PinName cd, SwitchType cdtype, int hz = 1000000);
00099 
00100     /** Get the detected SD/MMC card type
00101      *
00102      * @returns The detected card type as a CardType enum.
00103      */
00104     SDFileSystem::CardType card_type();
00105 
00106     /** Get whether or not CRC is enabled for commands and data
00107      *
00108      * @returns
00109      *   'true' if CRC is enabled for commands and data,
00110      *   'false' if CRC is disabled for commands and data.
00111      */
00112     bool crc();
00113 
00114     /** Set whether or not CRC is enabled for commands and data
00115      *
00116      * @param enabled Whether or not to enable CRC for commands and data.
00117      */
00118     void crc(bool enabled);
00119 
00120     /** Get whether or not 16-bit frames are enabled for data read/write operations
00121      *
00122      * @returns
00123      *   'true' if 16-bit frames will be used during data read/write operations,
00124      *   'false' if 8-bit frames will be used during data read/write operations.
00125      */
00126     bool large_frames();
00127 
00128     /** Set whether or not 16-bit frames are enabled for data read/write operations
00129      *
00130      * @param enabled Whether or not 16-bit frames are enabled for data read/write operations.
00131      */
00132     void large_frames(bool enabled);
00133 
00134     /** Get whether or not write validation is enabled for data write operations
00135      *
00136      * @returns
00137      *   'true' if data writes will be verified using CMD13,
00138      *   'false' if data writes will not be verified.
00139      */
00140     bool write_validation();
00141 
00142     /** Set whether or not write validation is enabled for data write operations
00143      *
00144      * @param enabled Whether or not write validation is enabled for data write operations.
00145      */
00146     void write_validation(bool enabled);
00147 
00148     virtual int unmount();
00149     virtual int disk_initialize();
00150     virtual int disk_status();
00151     virtual int disk_read(uint8_t* buffer, uint64_t sector, uint8_t count);
00152     virtual int disk_write(const uint8_t* buffer, uint64_t sector, uint8_t count);
00153     virtual int disk_sync();
00154     virtual uint64_t disk_sectors();
00155 
00156 private:
00157     //Commands
00158     enum Command {
00159         CMD0 = (0x40 | 0),      /**< GO_IDLE_STATE */
00160         CMD1 = (0x40 | 1),      /**< SEND_OP_COND */
00161         CMD8 = (0x40 | 8),      /**< SEND_IF_COND */
00162         CMD9 = (0x40 | 9),      /**< SEND_CSD */
00163         CMD12 = (0x40 | 12),    /**< STOP_TRANSMISSION */
00164         CMD13 = (0x40 | 13),    /**< SEND_STATUS */
00165         CMD16 = (0x40 | 16),    /**< SET_BLOCKLEN */
00166         CMD17 = (0x40 | 17),    /**< READ_SINGLE_BLOCK */
00167         CMD18 = (0x40 | 18),    /**< READ_MULTIPLE_BLOCK */
00168         ACMD22 = (0x40 | 22),   /**< SEND_NUM_WR_BLOCKS */
00169         ACMD23 = (0x40 | 23),   /**< SET_WR_BLK_ERASE_COUNT */
00170         CMD24 = (0x40 | 24),    /**< WRITE_BLOCK */
00171         CMD25 = (0x40 | 25),    /**< WRITE_MULTIPLE_BLOCK */
00172         ACMD41 = (0x40 | 41),   /**< SD_SEND_OP_COND */
00173         ACMD42 = (0x40 | 42),   /**< SET_CLR_CARD_DETECT */
00174         CMD55 = (0x40 | 55),    /**< APP_CMD */
00175         CMD58 = (0x40 | 58),    /**< READ_OCR */
00176         CMD59 = (0x40 | 59)     /**< CRC_ON_OFF */
00177     };
00178 
00179     //Member variables
00180     Timer m_Timer;
00181     SPI m_Spi;
00182     DigitalOut m_Cs;
00183     InterruptIn m_Cd;
00184     int m_CdAssert;
00185     const int m_FREQ;
00186     SDFileSystem::CardType m_CardType;
00187     bool m_Crc;
00188     bool m_LargeFrames;
00189     bool m_WriteValidation;
00190     int m_Status;
00191 
00192     //Internal methods
00193     void onCardRemoval();
00194     void checkSocket();
00195     bool waitReady(int timeout);
00196     bool select();
00197     void deselect();
00198     char commandTransaction(char cmd, unsigned int arg, unsigned int* resp = NULL);
00199     char writeCommand(char cmd, unsigned int arg, unsigned int* resp = NULL);
00200     bool readData(char* buffer, int length);
00201     char writeData(const char* buffer, char token);
00202     bool readBlock(char* buffer, unsigned long long lba);
00203     bool readBlocks(char* buffer, unsigned long long lba, int count);
00204     bool writeBlock(const char* buffer, unsigned long long lba);
00205     bool writeBlocks(const char* buffer, unsigned long long lba, int count);
00206 };
00207 
00208 #endif