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