SDFileSystem for STM32F746NG DISCOVERY with 4bit SDMMC interface on fixed pins
Dependents: DISCO-F746NG_SDFileSystem uzairkhan DISCO-F746NG_Scope_copy
Fork of SDFileSystem by
SDFileSystem.h@27:8d192c180436, 2016-04-16 (annotated)
- Committer:
- DieterGraef
- Date:
- Sat Apr 16 13:01:50 2016 +0000
- Revision:
- 27:8d192c180436
- Parent:
- 23:c03ef1abef0e
Changed hard locking mechanism to watchdog , solved issue with the sleep mode of SD cards
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
neilt6 | 0:2a6d8a096edc | 1 | /* SD/MMC File System Library |
neilt6 | 22:3fa5eaf48e81 | 2 | * Copyright (c) 2016 Neil Thiessen |
DieterGraef | 23:c03ef1abef0e | 3 | * Modified for the use with STM32F746 Discovery (C) 2016 Dieter Greaf |
neilt6 | 0:2a6d8a096edc | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
neilt6 | 0:2a6d8a096edc | 5 | * you may not use this file except in compliance with the License. |
neilt6 | 0:2a6d8a096edc | 6 | * You may obtain a copy of the License at |
neilt6 | 0:2a6d8a096edc | 7 | * |
neilt6 | 0:2a6d8a096edc | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
neilt6 | 0:2a6d8a096edc | 9 | * |
neilt6 | 0:2a6d8a096edc | 10 | * Unless required by applicable law or agreed to in writing, software |
neilt6 | 0:2a6d8a096edc | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
neilt6 | 0:2a6d8a096edc | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
neilt6 | 0:2a6d8a096edc | 13 | * See the License for the specific language governing permissions and |
neilt6 | 0:2a6d8a096edc | 14 | * limitations under the License. |
neilt6 | 0:2a6d8a096edc | 15 | */ |
neilt6 | 0:2a6d8a096edc | 16 | |
neilt6 | 0:2a6d8a096edc | 17 | #ifndef SD_FILE_SYSTEM_H |
neilt6 | 0:2a6d8a096edc | 18 | #define SD_FILE_SYSTEM_H |
neilt6 | 0:2a6d8a096edc | 19 | |
neilt6 | 0:2a6d8a096edc | 20 | #include "mbed.h" |
DieterGraef | 23:c03ef1abef0e | 21 | #include "stm32746g_discovery_sd.h" |
neilt6 | 0:2a6d8a096edc | 22 | #include "FATFileSystem.h" |
neilt6 | 0:2a6d8a096edc | 23 | |
neilt6 | 0:2a6d8a096edc | 24 | /** SDFileSystem class. |
neilt6 | 0:2a6d8a096edc | 25 | * Used for creating a virtual file system for accessing SD/MMC cards via SPI. |
neilt6 | 0:2a6d8a096edc | 26 | * |
neilt6 | 0:2a6d8a096edc | 27 | * Example: |
neilt6 | 0:2a6d8a096edc | 28 | * @code |
neilt6 | 0:2a6d8a096edc | 29 | * #include "mbed.h" |
neilt6 | 0:2a6d8a096edc | 30 | * #include "SDFileSystem.h" |
neilt6 | 0:2a6d8a096edc | 31 | * |
neilt6 | 0:2a6d8a096edc | 32 | * //Create an SDFileSystem object |
DieterGraef | 23:c03ef1abef0e | 33 | * SDFileSystem sd("sd"); |
neilt6 | 0:2a6d8a096edc | 34 | * |
neilt6 | 0:2a6d8a096edc | 35 | * int main() |
neilt6 | 0:2a6d8a096edc | 36 | * { |
neilt6 | 16:c2c1f0b16380 | 37 | * //Mount the filesystem |
neilt6 | 16:c2c1f0b16380 | 38 | * sd.mount(); |
neilt6 | 16:c2c1f0b16380 | 39 | * |
neilt6 | 0:2a6d8a096edc | 40 | * //Perform a write test |
neilt6 | 0:2a6d8a096edc | 41 | * printf("\nWriting to SD card..."); |
neilt6 | 0:2a6d8a096edc | 42 | * FILE *fp = fopen("/sd/sdtest.txt", "w"); |
neilt6 | 0:2a6d8a096edc | 43 | * if (fp != NULL) { |
neilt6 | 0:2a6d8a096edc | 44 | * fprintf(fp, "We're writing to an SD card!"); |
neilt6 | 0:2a6d8a096edc | 45 | * fclose(fp); |
neilt6 | 0:2a6d8a096edc | 46 | * printf("success!\n"); |
neilt6 | 0:2a6d8a096edc | 47 | * } else { |
neilt6 | 0:2a6d8a096edc | 48 | * printf("failed!\n"); |
neilt6 | 0:2a6d8a096edc | 49 | * } |
neilt6 | 0:2a6d8a096edc | 50 | * |
neilt6 | 0:2a6d8a096edc | 51 | * //Perform a read test |
neilt6 | 0:2a6d8a096edc | 52 | * printf("Reading from SD card..."); |
neilt6 | 0:2a6d8a096edc | 53 | * fp = fopen("/sd/sdtest.txt", "r"); |
neilt6 | 0:2a6d8a096edc | 54 | * if (fp != NULL) { |
neilt6 | 0:2a6d8a096edc | 55 | * char c = fgetc(fp); |
neilt6 | 0:2a6d8a096edc | 56 | * if (c == 'W') |
neilt6 | 0:2a6d8a096edc | 57 | * printf("success!\n"); |
neilt6 | 0:2a6d8a096edc | 58 | * else |
neilt6 | 0:2a6d8a096edc | 59 | * printf("incorrect char (%c)!\n", c); |
neilt6 | 0:2a6d8a096edc | 60 | * fclose(fp); |
neilt6 | 0:2a6d8a096edc | 61 | * } else { |
neilt6 | 0:2a6d8a096edc | 62 | * printf("failed!\n"); |
neilt6 | 0:2a6d8a096edc | 63 | * } |
neilt6 | 16:c2c1f0b16380 | 64 | * |
neilt6 | 16:c2c1f0b16380 | 65 | * //Unmount the filesystem |
neilt6 | 16:c2c1f0b16380 | 66 | * sd.unmount(); |
neilt6 | 0:2a6d8a096edc | 67 | * } |
neilt6 | 0:2a6d8a096edc | 68 | * @endcode |
DieterGraef | 23:c03ef1abef0e | 69 | |
neilt6 | 0:2a6d8a096edc | 70 | */ |
DieterGraef | 23:c03ef1abef0e | 71 | #define CMD59 (0x40 | 59) |
DieterGraef | 23:c03ef1abef0e | 72 | using namespace mbed; |
DieterGraef | 23:c03ef1abef0e | 73 | |
neilt6 | 0:2a6d8a096edc | 74 | class SDFileSystem : public FATFileSystem |
neilt6 | 0:2a6d8a096edc | 75 | { |
neilt6 | 0:2a6d8a096edc | 76 | public: |
neilt6 | 1:25f4ba436b81 | 77 | /** Represents the different card detect switch types |
neilt6 | 1:25f4ba436b81 | 78 | */ |
neilt6 | 1:25f4ba436b81 | 79 | enum SwitchType { |
neilt6 | 16:c2c1f0b16380 | 80 | SWITCH_NONE, /**< No card detect switch (assumes socket is always occupied) */ |
neilt6 | 12:eebddab6eff2 | 81 | SWITCH_POS_NO, /**< Switch shorts to VDD when the socket is occupied (positive logic, normally open) */ |
neilt6 | 12:eebddab6eff2 | 82 | SWITCH_POS_NC, /**< Switch shorts to VDD when the socket is empty (positive logic, normally closed) */ |
neilt6 | 12:eebddab6eff2 | 83 | SWITCH_NEG_NO, /**< Switch shorts to GND when the socket is occupied (negative logic, normally open) */ |
neilt6 | 12:eebddab6eff2 | 84 | SWITCH_NEG_NC /**< Switch shorts to GND when the socket is empty (negative logic, normally closed) */ |
neilt6 | 1:25f4ba436b81 | 85 | }; |
neilt6 | 1:25f4ba436b81 | 86 | |
neilt6 | 0:2a6d8a096edc | 87 | /** Represents the different SD/MMC card types |
neilt6 | 0:2a6d8a096edc | 88 | */ |
neilt6 | 0:2a6d8a096edc | 89 | enum CardType { |
neilt6 | 0:2a6d8a096edc | 90 | CARD_NONE, /**< No card is present */ |
neilt6 | 0:2a6d8a096edc | 91 | CARD_MMC, /**< MMC card */ |
neilt6 | 0:2a6d8a096edc | 92 | CARD_SD, /**< Standard capacity SD card */ |
neilt6 | 0:2a6d8a096edc | 93 | CARD_SDHC, /**< High capacity SD card */ |
neilt6 | 0:2a6d8a096edc | 94 | CARD_UNKNOWN /**< Unknown or unsupported card */ |
neilt6 | 0:2a6d8a096edc | 95 | }; |
neilt6 | 0:2a6d8a096edc | 96 | |
neilt6 | 0:2a6d8a096edc | 97 | /** Create a virtual file system for accessing SD/MMC cards via SPI |
neilt6 | 0:2a6d8a096edc | 98 | * |
neilt6 | 0:2a6d8a096edc | 99 | * @param mosi The SPI data out pin. |
neilt6 | 0:2a6d8a096edc | 100 | * @param miso The SPI data in pin. |
neilt6 | 0:2a6d8a096edc | 101 | * @param sclk The SPI clock pin. |
neilt6 | 0:2a6d8a096edc | 102 | * @param cs The SPI chip select pin. |
neilt6 | 3:7cf3d1835ef5 | 103 | * @param name The name used to access the virtual filesystem. |
neilt6 | 1:25f4ba436b81 | 104 | * @param cd The card detect pin. |
neilt6 | 12:eebddab6eff2 | 105 | * @param cdtype The type of card detect switch. |
neilt6 | 0:2a6d8a096edc | 106 | * @param hz The SPI bus frequency (defaults to 1MHz). |
neilt6 | 0:2a6d8a096edc | 107 | */ |
DieterGraef | 23:c03ef1abef0e | 108 | SDFileSystem( const char* name); |
neilt6 | 0:2a6d8a096edc | 109 | |
neilt6 | 20:2c1e8d442f68 | 110 | /** Determine whether or not a card is present |
neilt6 | 20:2c1e8d442f68 | 111 | * |
neilt6 | 20:2c1e8d442f68 | 112 | * @returns |
neilt6 | 20:2c1e8d442f68 | 113 | * 'true' if a card is present, |
neilt6 | 20:2c1e8d442f68 | 114 | * 'false' if no card is present. |
neilt6 | 20:2c1e8d442f68 | 115 | */ |
neilt6 | 20:2c1e8d442f68 | 116 | bool card_present(); |
neilt6 | 20:2c1e8d442f68 | 117 | |
neilt6 | 0:2a6d8a096edc | 118 | /** Get the detected SD/MMC card type |
neilt6 | 0:2a6d8a096edc | 119 | * |
neilt6 | 0:2a6d8a096edc | 120 | * @returns The detected card type as a CardType enum. |
neilt6 | 20:2c1e8d442f68 | 121 | * |
neilt6 | 20:2c1e8d442f68 | 122 | * @note Valid after the filesystem has been mounted. |
neilt6 | 0:2a6d8a096edc | 123 | */ |
neilt6 | 0:2a6d8a096edc | 124 | SDFileSystem::CardType card_type(); |
neilt6 | 0:2a6d8a096edc | 125 | |
neilt6 | 6:55a26a56046a | 126 | /** Get whether or not CRC is enabled for commands and data |
neilt6 | 6:55a26a56046a | 127 | * |
neilt6 | 6:55a26a56046a | 128 | * @returns |
neilt6 | 6:55a26a56046a | 129 | * 'true' if CRC is enabled for commands and data, |
neilt6 | 6:55a26a56046a | 130 | * 'false' if CRC is disabled for commands and data. |
neilt6 | 6:55a26a56046a | 131 | */ |
neilt6 | 7:61db99e52c0d | 132 | bool crc(); |
neilt6 | 6:55a26a56046a | 133 | |
neilt6 | 6:55a26a56046a | 134 | /** Set whether or not CRC is enabled for commands and data |
neilt6 | 6:55a26a56046a | 135 | * |
neilt6 | 6:55a26a56046a | 136 | * @param enabled Whether or not to enable CRC for commands and data. |
neilt6 | 6:55a26a56046a | 137 | */ |
neilt6 | 7:61db99e52c0d | 138 | void crc(bool enabled); |
neilt6 | 6:55a26a56046a | 139 | |
neilt6 | 6:55a26a56046a | 140 | /** Get whether or not 16-bit frames are enabled for data read/write operations |
neilt6 | 6:55a26a56046a | 141 | * |
neilt6 | 6:55a26a56046a | 142 | * @returns |
neilt6 | 6:55a26a56046a | 143 | * 'true' if 16-bit frames will be used during data read/write operations, |
neilt6 | 6:55a26a56046a | 144 | * 'false' if 8-bit frames will be used during data read/write operations. |
neilt6 | 6:55a26a56046a | 145 | */ |
neilt6 | 6:55a26a56046a | 146 | bool large_frames(); |
neilt6 | 6:55a26a56046a | 147 | |
neilt6 | 6:55a26a56046a | 148 | /** Set whether or not 16-bit frames are enabled for data read/write operations |
neilt6 | 6:55a26a56046a | 149 | * |
neilt6 | 6:55a26a56046a | 150 | * @param enabled Whether or not 16-bit frames are enabled for data read/write operations. |
neilt6 | 6:55a26a56046a | 151 | */ |
neilt6 | 6:55a26a56046a | 152 | void large_frames(bool enabled); |
neilt6 | 6:55a26a56046a | 153 | |
neilt6 | 13:635147efa748 | 154 | /** Get whether or not write validation is enabled for data write operations |
neilt6 | 13:635147efa748 | 155 | * |
neilt6 | 13:635147efa748 | 156 | * @returns |
neilt6 | 13:635147efa748 | 157 | * 'true' if data writes will be verified using CMD13, |
neilt6 | 13:635147efa748 | 158 | * 'false' if data writes will not be verified. |
neilt6 | 13:635147efa748 | 159 | */ |
neilt6 | 13:635147efa748 | 160 | bool write_validation(); |
neilt6 | 13:635147efa748 | 161 | |
neilt6 | 13:635147efa748 | 162 | /** Set whether or not write validation is enabled for data write operations |
neilt6 | 13:635147efa748 | 163 | * |
neilt6 | 13:635147efa748 | 164 | * @param enabled Whether or not write validation is enabled for data write operations. |
neilt6 | 13:635147efa748 | 165 | */ |
neilt6 | 13:635147efa748 | 166 | void write_validation(bool enabled); |
neilt6 | 13:635147efa748 | 167 | |
neilt6 | 11:67ddc53e3983 | 168 | virtual int unmount(); |
neilt6 | 0:2a6d8a096edc | 169 | virtual int disk_initialize(); |
neilt6 | 0:2a6d8a096edc | 170 | virtual int disk_status(); |
neilt6 | 21:d10a519c0910 | 171 | virtual int disk_read(uint8_t* buffer, uint32_t sector, uint32_t count); |
neilt6 | 21:d10a519c0910 | 172 | virtual int disk_write(const uint8_t* buffer, uint32_t sector, uint32_t count); |
neilt6 | 0:2a6d8a096edc | 173 | virtual int disk_sync(); |
neilt6 | 21:d10a519c0910 | 174 | virtual uint32_t disk_sectors(); |
neilt6 | 0:2a6d8a096edc | 175 | |
neilt6 | 0:2a6d8a096edc | 176 | private: |
neilt6 | 13:635147efa748 | 177 | Timer m_Timer; |
neilt6 | 7:61db99e52c0d | 178 | InterruptIn m_Cd; |
neilt6 | 12:eebddab6eff2 | 179 | int m_CdAssert; |
neilt6 | 6:55a26a56046a | 180 | SDFileSystem::CardType m_CardType; |
neilt6 | 7:61db99e52c0d | 181 | bool m_Crc; |
neilt6 | 6:55a26a56046a | 182 | bool m_LargeFrames; |
neilt6 | 13:635147efa748 | 183 | bool m_WriteValidation; |
neilt6 | 0:2a6d8a096edc | 184 | int m_Status; |
DieterGraef | 23:c03ef1abef0e | 185 | HAL_SD_CardInfoTypedef m_CardInfo; |
DieterGraef | 27:8d192c180436 | 186 | HAL_SD_TransferStateTypedef m_CardStatus; |
neilt6 | 0:2a6d8a096edc | 187 | //Internal methods |
neilt6 | 13:635147efa748 | 188 | void onCardRemoval(); |
neilt6 | 0:2a6d8a096edc | 189 | void checkSocket(); |
DieterGraef | 23:c03ef1abef0e | 190 | void DMA2_Stream3_IRQHandler(); |
DieterGraef | 23:c03ef1abef0e | 191 | void DMA2_Stream6_IRQHandler(); |
DieterGraef | 23:c03ef1abef0e | 192 | void SDMMC1_IRQHandler(); |
neilt6 | 0:2a6d8a096edc | 193 | }; |
neilt6 | 0:2a6d8a096edc | 194 | |
neilt6 | 0:2a6d8a096edc | 195 | #endif |