RainbowTeam / Mbed 2 deprecated ProjectTheseus

Dependencies:   mbed

Committer:
Alexander_Zuest
Date:
Wed May 23 11:50:58 2018 +0000
Revision:
14:0caa7b93af7a
Parent:
6:a1fd0f1374e6
50% Chance for working Routecalculation

Who changed what in which revision?

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