SDFileSystem library for FAT compatible
SDFileSystem.h@0:583b9f0b91e7, 2016-10-13 (annotated)
- Committer:
- sameera0824
- Date:
- Thu Oct 13 10:51:33 2016 +0000
- Revision:
- 0:583b9f0b91e7
adding SD
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sameera0824 | 0:583b9f0b91e7 | 1 | /* SD/MMC File System Library |
sameera0824 | 0:583b9f0b91e7 | 2 | * Copyright (c) 2016 Neil Thiessen |
sameera0824 | 0:583b9f0b91e7 | 3 | * |
sameera0824 | 0:583b9f0b91e7 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sameera0824 | 0:583b9f0b91e7 | 5 | * you may not use this file except in compliance with the License. |
sameera0824 | 0:583b9f0b91e7 | 6 | * You may obtain a copy of the License at |
sameera0824 | 0:583b9f0b91e7 | 7 | * |
sameera0824 | 0:583b9f0b91e7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
sameera0824 | 0:583b9f0b91e7 | 9 | * |
sameera0824 | 0:583b9f0b91e7 | 10 | * Unless required by applicable law or agreed to in writing, software |
sameera0824 | 0:583b9f0b91e7 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
sameera0824 | 0:583b9f0b91e7 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sameera0824 | 0:583b9f0b91e7 | 13 | * See the License for the specific language governing permissions and |
sameera0824 | 0:583b9f0b91e7 | 14 | * limitations under the License. |
sameera0824 | 0:583b9f0b91e7 | 15 | */ |
sameera0824 | 0:583b9f0b91e7 | 16 | |
sameera0824 | 0:583b9f0b91e7 | 17 | #ifndef SD_FILE_SYSTEM_H |
sameera0824 | 0:583b9f0b91e7 | 18 | #define SD_FILE_SYSTEM_H |
sameera0824 | 0:583b9f0b91e7 | 19 | |
sameera0824 | 0:583b9f0b91e7 | 20 | #include "mbed.h" |
sameera0824 | 0:583b9f0b91e7 | 21 | #include "FATFileSystem.h" |
sameera0824 | 0:583b9f0b91e7 | 22 | |
sameera0824 | 0:583b9f0b91e7 | 23 | /** SDFileSystem class. |
sameera0824 | 0:583b9f0b91e7 | 24 | * Used for creating a virtual file system for accessing SD/MMC cards via SPI. |
sameera0824 | 0:583b9f0b91e7 | 25 | * |
sameera0824 | 0:583b9f0b91e7 | 26 | * Example: |
sameera0824 | 0:583b9f0b91e7 | 27 | * @code |
sameera0824 | 0:583b9f0b91e7 | 28 | * #include "mbed.h" |
sameera0824 | 0:583b9f0b91e7 | 29 | * #include "SDFileSystem.h" |
sameera0824 | 0:583b9f0b91e7 | 30 | * |
sameera0824 | 0:583b9f0b91e7 | 31 | * //Create an SDFileSystem object |
sameera0824 | 0:583b9f0b91e7 | 32 | * SDFileSystem sd(p5, p6, p7, p20, "sd"); |
sameera0824 | 0:583b9f0b91e7 | 33 | * |
sameera0824 | 0:583b9f0b91e7 | 34 | * int main() |
sameera0824 | 0:583b9f0b91e7 | 35 | * { |
sameera0824 | 0:583b9f0b91e7 | 36 | * //Mount the filesystem |
sameera0824 | 0:583b9f0b91e7 | 37 | * sd.mount(); |
sameera0824 | 0:583b9f0b91e7 | 38 | * |
sameera0824 | 0:583b9f0b91e7 | 39 | * //Perform a write test |
sameera0824 | 0:583b9f0b91e7 | 40 | * printf("\nWriting to SD card..."); |
sameera0824 | 0:583b9f0b91e7 | 41 | * FILE *fp = fopen("/sd/sdtest.txt", "w"); |
sameera0824 | 0:583b9f0b91e7 | 42 | * if (fp != NULL) { |
sameera0824 | 0:583b9f0b91e7 | 43 | * fprintf(fp, "We're writing to an SD card!"); |
sameera0824 | 0:583b9f0b91e7 | 44 | * fclose(fp); |
sameera0824 | 0:583b9f0b91e7 | 45 | * printf("success!\n"); |
sameera0824 | 0:583b9f0b91e7 | 46 | * } else { |
sameera0824 | 0:583b9f0b91e7 | 47 | * printf("failed!\n"); |
sameera0824 | 0:583b9f0b91e7 | 48 | * } |
sameera0824 | 0:583b9f0b91e7 | 49 | * |
sameera0824 | 0:583b9f0b91e7 | 50 | * //Perform a read test |
sameera0824 | 0:583b9f0b91e7 | 51 | * printf("Reading from SD card..."); |
sameera0824 | 0:583b9f0b91e7 | 52 | * fp = fopen("/sd/sdtest.txt", "r"); |
sameera0824 | 0:583b9f0b91e7 | 53 | * if (fp != NULL) { |
sameera0824 | 0:583b9f0b91e7 | 54 | * char c = fgetc(fp); |
sameera0824 | 0:583b9f0b91e7 | 55 | * if (c == 'W') |
sameera0824 | 0:583b9f0b91e7 | 56 | * printf("success!\n"); |
sameera0824 | 0:583b9f0b91e7 | 57 | * else |
sameera0824 | 0:583b9f0b91e7 | 58 | * printf("incorrect char (%c)!\n", c); |
sameera0824 | 0:583b9f0b91e7 | 59 | * fclose(fp); |
sameera0824 | 0:583b9f0b91e7 | 60 | * } else { |
sameera0824 | 0:583b9f0b91e7 | 61 | * printf("failed!\n"); |
sameera0824 | 0:583b9f0b91e7 | 62 | * } |
sameera0824 | 0:583b9f0b91e7 | 63 | * |
sameera0824 | 0:583b9f0b91e7 | 64 | * //Unmount the filesystem |
sameera0824 | 0:583b9f0b91e7 | 65 | * sd.unmount(); |
sameera0824 | 0:583b9f0b91e7 | 66 | * } |
sameera0824 | 0:583b9f0b91e7 | 67 | * @endcode |
sameera0824 | 0:583b9f0b91e7 | 68 | */ |
sameera0824 | 0:583b9f0b91e7 | 69 | class SDFileSystem : public FATFileSystem |
sameera0824 | 0:583b9f0b91e7 | 70 | { |
sameera0824 | 0:583b9f0b91e7 | 71 | public: |
sameera0824 | 0:583b9f0b91e7 | 72 | /** Represents the different card detect switch types |
sameera0824 | 0:583b9f0b91e7 | 73 | */ |
sameera0824 | 0:583b9f0b91e7 | 74 | enum SwitchType { |
sameera0824 | 0:583b9f0b91e7 | 75 | SWITCH_NONE, /**< No card detect switch (assumes socket is always occupied) */ |
sameera0824 | 0:583b9f0b91e7 | 76 | SWITCH_POS_NO, /**< Switch shorts to VDD when the socket is occupied (positive logic, normally open) */ |
sameera0824 | 0:583b9f0b91e7 | 77 | SWITCH_POS_NC, /**< Switch shorts to VDD when the socket is empty (positive logic, normally closed) */ |
sameera0824 | 0:583b9f0b91e7 | 78 | SWITCH_NEG_NO, /**< Switch shorts to GND when the socket is occupied (negative logic, normally open) */ |
sameera0824 | 0:583b9f0b91e7 | 79 | SWITCH_NEG_NC /**< Switch shorts to GND when the socket is empty (negative logic, normally closed) */ |
sameera0824 | 0:583b9f0b91e7 | 80 | }; |
sameera0824 | 0:583b9f0b91e7 | 81 | |
sameera0824 | 0:583b9f0b91e7 | 82 | /** Represents the different SD/MMC card types |
sameera0824 | 0:583b9f0b91e7 | 83 | */ |
sameera0824 | 0:583b9f0b91e7 | 84 | enum CardType { |
sameera0824 | 0:583b9f0b91e7 | 85 | CARD_NONE, /**< No card is present */ |
sameera0824 | 0:583b9f0b91e7 | 86 | CARD_MMC, /**< MMC card */ |
sameera0824 | 0:583b9f0b91e7 | 87 | CARD_SD, /**< Standard capacity SD card */ |
sameera0824 | 0:583b9f0b91e7 | 88 | CARD_SDHC, /**< High capacity SD card */ |
sameera0824 | 0:583b9f0b91e7 | 89 | CARD_UNKNOWN /**< Unknown or unsupported card */ |
sameera0824 | 0:583b9f0b91e7 | 90 | }; |
sameera0824 | 0:583b9f0b91e7 | 91 | |
sameera0824 | 0:583b9f0b91e7 | 92 | /** Create a virtual file system for accessing SD/MMC cards via SPI |
sameera0824 | 0:583b9f0b91e7 | 93 | * |
sameera0824 | 0:583b9f0b91e7 | 94 | * @param mosi The SPI data out pin. |
sameera0824 | 0:583b9f0b91e7 | 95 | * @param miso The SPI data in pin. |
sameera0824 | 0:583b9f0b91e7 | 96 | * @param sclk The SPI clock pin. |
sameera0824 | 0:583b9f0b91e7 | 97 | * @param cs The SPI chip select pin. |
sameera0824 | 0:583b9f0b91e7 | 98 | * @param name The name used to access the virtual filesystem. |
sameera0824 | 0:583b9f0b91e7 | 99 | * @param cd The card detect pin. |
sameera0824 | 0:583b9f0b91e7 | 100 | * @param cdtype The type of card detect switch. |
sameera0824 | 0:583b9f0b91e7 | 101 | * @param hz The SPI bus frequency (defaults to 1MHz). |
sameera0824 | 0:583b9f0b91e7 | 102 | */ |
sameera0824 | 0:583b9f0b91e7 | 103 | SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name, PinName cd = NC, SwitchType cdtype = SWITCH_NONE, int hz = 1000000); |
sameera0824 | 0:583b9f0b91e7 | 104 | |
sameera0824 | 0:583b9f0b91e7 | 105 | /** Determine whether or not a card is present |
sameera0824 | 0:583b9f0b91e7 | 106 | * |
sameera0824 | 0:583b9f0b91e7 | 107 | * @returns |
sameera0824 | 0:583b9f0b91e7 | 108 | * 'true' if a card is present, |
sameera0824 | 0:583b9f0b91e7 | 109 | * 'false' if no card is present. |
sameera0824 | 0:583b9f0b91e7 | 110 | */ |
sameera0824 | 0:583b9f0b91e7 | 111 | bool card_present(); |
sameera0824 | 0:583b9f0b91e7 | 112 | |
sameera0824 | 0:583b9f0b91e7 | 113 | /** Get the detected SD/MMC card type |
sameera0824 | 0:583b9f0b91e7 | 114 | * |
sameera0824 | 0:583b9f0b91e7 | 115 | * @returns The detected card type as a CardType enum. |
sameera0824 | 0:583b9f0b91e7 | 116 | * |
sameera0824 | 0:583b9f0b91e7 | 117 | * @note Valid after the filesystem has been mounted. |
sameera0824 | 0:583b9f0b91e7 | 118 | */ |
sameera0824 | 0:583b9f0b91e7 | 119 | SDFileSystem::CardType card_type(); |
sameera0824 | 0:583b9f0b91e7 | 120 | |
sameera0824 | 0:583b9f0b91e7 | 121 | /** Get whether or not CRC is enabled for commands and data |
sameera0824 | 0:583b9f0b91e7 | 122 | * |
sameera0824 | 0:583b9f0b91e7 | 123 | * @returns |
sameera0824 | 0:583b9f0b91e7 | 124 | * 'true' if CRC is enabled for commands and data, |
sameera0824 | 0:583b9f0b91e7 | 125 | * 'false' if CRC is disabled for commands and data. |
sameera0824 | 0:583b9f0b91e7 | 126 | */ |
sameera0824 | 0:583b9f0b91e7 | 127 | bool crc(); |
sameera0824 | 0:583b9f0b91e7 | 128 | |
sameera0824 | 0:583b9f0b91e7 | 129 | /** Set whether or not CRC is enabled for commands and data |
sameera0824 | 0:583b9f0b91e7 | 130 | * |
sameera0824 | 0:583b9f0b91e7 | 131 | * @param enabled Whether or not to enable CRC for commands and data. |
sameera0824 | 0:583b9f0b91e7 | 132 | */ |
sameera0824 | 0:583b9f0b91e7 | 133 | void crc(bool enabled); |
sameera0824 | 0:583b9f0b91e7 | 134 | |
sameera0824 | 0:583b9f0b91e7 | 135 | /** Get whether or not 16-bit frames are enabled for data read/write operations |
sameera0824 | 0:583b9f0b91e7 | 136 | * |
sameera0824 | 0:583b9f0b91e7 | 137 | * @returns |
sameera0824 | 0:583b9f0b91e7 | 138 | * 'true' if 16-bit frames will be used during data read/write operations, |
sameera0824 | 0:583b9f0b91e7 | 139 | * 'false' if 8-bit frames will be used during data read/write operations. |
sameera0824 | 0:583b9f0b91e7 | 140 | */ |
sameera0824 | 0:583b9f0b91e7 | 141 | bool large_frames(); |
sameera0824 | 0:583b9f0b91e7 | 142 | |
sameera0824 | 0:583b9f0b91e7 | 143 | /** Set whether or not 16-bit frames are enabled for data read/write operations |
sameera0824 | 0:583b9f0b91e7 | 144 | * |
sameera0824 | 0:583b9f0b91e7 | 145 | * @param enabled Whether or not 16-bit frames are enabled for data read/write operations. |
sameera0824 | 0:583b9f0b91e7 | 146 | */ |
sameera0824 | 0:583b9f0b91e7 | 147 | void large_frames(bool enabled); |
sameera0824 | 0:583b9f0b91e7 | 148 | |
sameera0824 | 0:583b9f0b91e7 | 149 | /** Get whether or not write validation is enabled for data write operations |
sameera0824 | 0:583b9f0b91e7 | 150 | * |
sameera0824 | 0:583b9f0b91e7 | 151 | * @returns |
sameera0824 | 0:583b9f0b91e7 | 152 | * 'true' if data writes will be verified using CMD13, |
sameera0824 | 0:583b9f0b91e7 | 153 | * 'false' if data writes will not be verified. |
sameera0824 | 0:583b9f0b91e7 | 154 | */ |
sameera0824 | 0:583b9f0b91e7 | 155 | bool write_validation(); |
sameera0824 | 0:583b9f0b91e7 | 156 | |
sameera0824 | 0:583b9f0b91e7 | 157 | /** Set whether or not write validation is enabled for data write operations |
sameera0824 | 0:583b9f0b91e7 | 158 | * |
sameera0824 | 0:583b9f0b91e7 | 159 | * @param enabled Whether or not write validation is enabled for data write operations. |
sameera0824 | 0:583b9f0b91e7 | 160 | */ |
sameera0824 | 0:583b9f0b91e7 | 161 | void write_validation(bool enabled); |
sameera0824 | 0:583b9f0b91e7 | 162 | |
sameera0824 | 0:583b9f0b91e7 | 163 | virtual int unmount(); |
sameera0824 | 0:583b9f0b91e7 | 164 | virtual int disk_initialize(); |
sameera0824 | 0:583b9f0b91e7 | 165 | virtual int disk_status(); |
sameera0824 | 0:583b9f0b91e7 | 166 | virtual int disk_read(uint8_t* buffer, uint32_t sector, uint32_t count); |
sameera0824 | 0:583b9f0b91e7 | 167 | virtual int disk_write(const uint8_t* buffer, uint32_t sector, uint32_t count); |
sameera0824 | 0:583b9f0b91e7 | 168 | virtual int disk_sync(); |
sameera0824 | 0:583b9f0b91e7 | 169 | virtual uint32_t disk_sectors(); |
sameera0824 | 0:583b9f0b91e7 | 170 | |
sameera0824 | 0:583b9f0b91e7 | 171 | private: |
sameera0824 | 0:583b9f0b91e7 | 172 | //Commands |
sameera0824 | 0:583b9f0b91e7 | 173 | enum Command { |
sameera0824 | 0:583b9f0b91e7 | 174 | CMD0 = (0x40 | 0), /**< GO_IDLE_STATE */ |
sameera0824 | 0:583b9f0b91e7 | 175 | CMD1 = (0x40 | 1), /**< SEND_OP_COND */ |
sameera0824 | 0:583b9f0b91e7 | 176 | CMD6 = (0x40 | 6), /**< SWITCH_FUNC */ |
sameera0824 | 0:583b9f0b91e7 | 177 | CMD8 = (0x40 | 8), /**< SEND_IF_COND */ |
sameera0824 | 0:583b9f0b91e7 | 178 | CMD9 = (0x40 | 9), /**< SEND_CSD */ |
sameera0824 | 0:583b9f0b91e7 | 179 | CMD12 = (0x40 | 12), /**< STOP_TRANSMISSION */ |
sameera0824 | 0:583b9f0b91e7 | 180 | CMD13 = (0x40 | 13), /**< SEND_STATUS */ |
sameera0824 | 0:583b9f0b91e7 | 181 | CMD16 = (0x40 | 16), /**< SET_BLOCKLEN */ |
sameera0824 | 0:583b9f0b91e7 | 182 | CMD17 = (0x40 | 17), /**< READ_SINGLE_BLOCK */ |
sameera0824 | 0:583b9f0b91e7 | 183 | CMD18 = (0x40 | 18), /**< READ_MULTIPLE_BLOCK */ |
sameera0824 | 0:583b9f0b91e7 | 184 | ACMD22 = (0x40 | 22), /**< SEND_NUM_WR_BLOCKS */ |
sameera0824 | 0:583b9f0b91e7 | 185 | ACMD23 = (0x40 | 23), /**< SET_WR_BLK_ERASE_COUNT */ |
sameera0824 | 0:583b9f0b91e7 | 186 | CMD24 = (0x40 | 24), /**< WRITE_BLOCK */ |
sameera0824 | 0:583b9f0b91e7 | 187 | CMD25 = (0x40 | 25), /**< WRITE_MULTIPLE_BLOCK */ |
sameera0824 | 0:583b9f0b91e7 | 188 | ACMD41 = (0x40 | 41), /**< SD_SEND_OP_COND */ |
sameera0824 | 0:583b9f0b91e7 | 189 | ACMD42 = (0x40 | 42), /**< SET_CLR_CARD_DETECT */ |
sameera0824 | 0:583b9f0b91e7 | 190 | CMD55 = (0x40 | 55), /**< APP_CMD */ |
sameera0824 | 0:583b9f0b91e7 | 191 | CMD58 = (0x40 | 58), /**< READ_OCR */ |
sameera0824 | 0:583b9f0b91e7 | 192 | CMD59 = (0x40 | 59) /**< CRC_ON_OFF */ |
sameera0824 | 0:583b9f0b91e7 | 193 | }; |
sameera0824 | 0:583b9f0b91e7 | 194 | |
sameera0824 | 0:583b9f0b91e7 | 195 | //Member variables |
sameera0824 | 0:583b9f0b91e7 | 196 | Timer m_Timer; |
sameera0824 | 0:583b9f0b91e7 | 197 | SPI m_Spi; |
sameera0824 | 0:583b9f0b91e7 | 198 | DigitalOut m_Cs; |
sameera0824 | 0:583b9f0b91e7 | 199 | InterruptIn m_Cd; |
sameera0824 | 0:583b9f0b91e7 | 200 | int m_CdAssert; |
sameera0824 | 0:583b9f0b91e7 | 201 | const int m_FREQ; |
sameera0824 | 0:583b9f0b91e7 | 202 | SDFileSystem::CardType m_CardType; |
sameera0824 | 0:583b9f0b91e7 | 203 | bool m_Crc; |
sameera0824 | 0:583b9f0b91e7 | 204 | bool m_LargeFrames; |
sameera0824 | 0:583b9f0b91e7 | 205 | bool m_WriteValidation; |
sameera0824 | 0:583b9f0b91e7 | 206 | int m_Status; |
sameera0824 | 0:583b9f0b91e7 | 207 | |
sameera0824 | 0:583b9f0b91e7 | 208 | //Internal methods |
sameera0824 | 0:583b9f0b91e7 | 209 | void onCardRemoval(); |
sameera0824 | 0:583b9f0b91e7 | 210 | void checkSocket(); |
sameera0824 | 0:583b9f0b91e7 | 211 | bool waitReady(int timeout); |
sameera0824 | 0:583b9f0b91e7 | 212 | bool select(); |
sameera0824 | 0:583b9f0b91e7 | 213 | void deselect(); |
sameera0824 | 0:583b9f0b91e7 | 214 | char commandTransaction(char cmd, unsigned int arg, unsigned int* resp = NULL); |
sameera0824 | 0:583b9f0b91e7 | 215 | char writeCommand(char cmd, unsigned int arg, unsigned int* resp = NULL); |
sameera0824 | 0:583b9f0b91e7 | 216 | bool readData(char* buffer, int length); |
sameera0824 | 0:583b9f0b91e7 | 217 | char writeData(const char* buffer, char token); |
sameera0824 | 0:583b9f0b91e7 | 218 | bool readBlock(char* buffer, unsigned int lba); |
sameera0824 | 0:583b9f0b91e7 | 219 | bool readBlocks(char* buffer, unsigned int lba, unsigned int count); |
sameera0824 | 0:583b9f0b91e7 | 220 | bool writeBlock(const char* buffer, unsigned int lba); |
sameera0824 | 0:583b9f0b91e7 | 221 | bool writeBlocks(const char* buffer, unsigned int lba, unsigned int count); |
sameera0824 | 0:583b9f0b91e7 | 222 | bool enableHighSpeedMode(); |
sameera0824 | 0:583b9f0b91e7 | 223 | }; |
sameera0824 | 0:583b9f0b91e7 | 224 | |
sameera0824 | 0:583b9f0b91e7 | 225 | #endif |