Paul's baseline branch
Fork of SDFileSystem by
SDFileSystem.cpp@24:6bb3c1987511, 2016-04-13 (annotated)
- Committer:
- neilt6
- Date:
- Wed Apr 13 16:51:25 2016 +0000
- Revision:
- 24:6bb3c1987511
- Parent:
- 22:3fa5eaf48e81
- Child:
- 25:a52e682a1ce4
Added high-speed mode (50MHz) support for v2.00 or later 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 |
neilt6 | 0:2a6d8a096edc | 3 | * |
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 | #include "SDFileSystem.h" |
neilt6 | 18:2286a4e7fa31 | 18 | #include "diskio.h" |
neilt6 | 15:c9e938f6934f | 19 | #include "pinmap.h" |
neilt6 | 18:2286a4e7fa31 | 20 | #include "SDCRC.h" |
neilt6 | 0:2a6d8a096edc | 21 | |
neilt6 | 22:3fa5eaf48e81 | 22 | SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name, PinName cd, SwitchType cdtype, int hz) |
neilt6 | 22:3fa5eaf48e81 | 23 | : FATFileSystem(name), |
neilt6 | 22:3fa5eaf48e81 | 24 | m_Spi(mosi, miso, sclk), |
neilt6 | 22:3fa5eaf48e81 | 25 | m_Cs(cs, 1), |
neilt6 | 22:3fa5eaf48e81 | 26 | m_Cd(cd), |
neilt6 | 22:3fa5eaf48e81 | 27 | m_FREQ(hz) |
neilt6 | 0:2a6d8a096edc | 28 | { |
neilt6 | 0:2a6d8a096edc | 29 | //Initialize the member variables |
neilt6 | 6:55a26a56046a | 30 | m_CardType = CARD_NONE; |
neilt6 | 7:61db99e52c0d | 31 | m_Crc = true; |
neilt6 | 6:55a26a56046a | 32 | m_LargeFrames = false; |
neilt6 | 13:635147efa748 | 33 | m_WriteValidation = true; |
neilt6 | 0:2a6d8a096edc | 34 | m_Status = STA_NOINIT; |
neilt6 | 0:2a6d8a096edc | 35 | |
neilt6 | 15:c9e938f6934f | 36 | //Enable the internal pull-up resistor on MISO |
neilt6 | 15:c9e938f6934f | 37 | pin_mode(miso, PullUp); |
neilt6 | 15:c9e938f6934f | 38 | |
neilt6 | 0:2a6d8a096edc | 39 | //Configure the SPI bus |
neilt6 | 7:61db99e52c0d | 40 | m_Spi.format(8, 0); |
neilt6 | 0:2a6d8a096edc | 41 | |
neilt6 | 0:2a6d8a096edc | 42 | //Configure the card detect pin |
neilt6 | 12:eebddab6eff2 | 43 | if (cdtype == SWITCH_POS_NO) { |
neilt6 | 12:eebddab6eff2 | 44 | m_Cd.mode(PullDown); |
neilt6 | 12:eebddab6eff2 | 45 | m_CdAssert = 1; |
neilt6 | 13:635147efa748 | 46 | m_Cd.fall(this, &SDFileSystem::onCardRemoval); |
neilt6 | 12:eebddab6eff2 | 47 | } else if (cdtype == SWITCH_POS_NC) { |
neilt6 | 12:eebddab6eff2 | 48 | m_Cd.mode(PullDown); |
neilt6 | 12:eebddab6eff2 | 49 | m_CdAssert = 0; |
neilt6 | 13:635147efa748 | 50 | m_Cd.rise(this, &SDFileSystem::onCardRemoval); |
neilt6 | 12:eebddab6eff2 | 51 | } else if (cdtype == SWITCH_NEG_NO) { |
neilt6 | 12:eebddab6eff2 | 52 | m_Cd.mode(PullUp); |
neilt6 | 12:eebddab6eff2 | 53 | m_CdAssert = 0; |
neilt6 | 13:635147efa748 | 54 | m_Cd.rise(this, &SDFileSystem::onCardRemoval); |
neilt6 | 16:c2c1f0b16380 | 55 | } else if (cdtype == SWITCH_NEG_NC) { |
neilt6 | 12:eebddab6eff2 | 56 | m_Cd.mode(PullUp); |
neilt6 | 12:eebddab6eff2 | 57 | m_CdAssert = 1; |
neilt6 | 13:635147efa748 | 58 | m_Cd.fall(this, &SDFileSystem::onCardRemoval); |
neilt6 | 16:c2c1f0b16380 | 59 | } else { |
neilt6 | 16:c2c1f0b16380 | 60 | m_CdAssert = -1; |
neilt6 | 12:eebddab6eff2 | 61 | } |
neilt6 | 0:2a6d8a096edc | 62 | } |
neilt6 | 0:2a6d8a096edc | 63 | |
neilt6 | 20:2c1e8d442f68 | 64 | bool SDFileSystem::card_present() |
neilt6 | 0:2a6d8a096edc | 65 | { |
neilt6 | 20:2c1e8d442f68 | 66 | //Check the card socket |
neilt6 | 0:2a6d8a096edc | 67 | checkSocket(); |
neilt6 | 0:2a6d8a096edc | 68 | |
neilt6 | 20:2c1e8d442f68 | 69 | //Return whether or not a card is present |
neilt6 | 20:2c1e8d442f68 | 70 | return !(m_Status & STA_NODISK); |
neilt6 | 20:2c1e8d442f68 | 71 | } |
neilt6 | 0:2a6d8a096edc | 72 | |
neilt6 | 20:2c1e8d442f68 | 73 | SDFileSystem::CardType SDFileSystem::card_type() |
neilt6 | 20:2c1e8d442f68 | 74 | { |
neilt6 | 20:2c1e8d442f68 | 75 | //Check the card socket |
neilt6 | 20:2c1e8d442f68 | 76 | checkSocket(); |
neilt6 | 18:2286a4e7fa31 | 77 | |
neilt6 | 0:2a6d8a096edc | 78 | //Return the card type |
neilt6 | 0:2a6d8a096edc | 79 | return m_CardType; |
neilt6 | 0:2a6d8a096edc | 80 | } |
neilt6 | 0:2a6d8a096edc | 81 | |
neilt6 | 7:61db99e52c0d | 82 | bool SDFileSystem::crc() |
neilt6 | 6:55a26a56046a | 83 | { |
neilt6 | 6:55a26a56046a | 84 | //Return whether or not CRC is enabled |
neilt6 | 7:61db99e52c0d | 85 | return m_Crc; |
neilt6 | 6:55a26a56046a | 86 | } |
neilt6 | 6:55a26a56046a | 87 | |
neilt6 | 7:61db99e52c0d | 88 | void SDFileSystem::crc(bool enabled) |
neilt6 | 6:55a26a56046a | 89 | { |
neilt6 | 20:2c1e8d442f68 | 90 | //Check the card socket |
neilt6 | 6:55a26a56046a | 91 | checkSocket(); |
neilt6 | 6:55a26a56046a | 92 | |
neilt6 | 6:55a26a56046a | 93 | //Just update the member variable if the card isn't initialized |
neilt6 | 6:55a26a56046a | 94 | if (m_Status & STA_NOINIT) { |
neilt6 | 7:61db99e52c0d | 95 | m_Crc = enabled; |
neilt6 | 6:55a26a56046a | 96 | return; |
neilt6 | 6:55a26a56046a | 97 | } |
neilt6 | 6:55a26a56046a | 98 | |
neilt6 | 6:55a26a56046a | 99 | //Enable or disable CRC |
neilt6 | 7:61db99e52c0d | 100 | if (enabled && !m_Crc) { |
neilt6 | 6:55a26a56046a | 101 | //Send CMD59(0x00000001) to enable CRC |
neilt6 | 7:61db99e52c0d | 102 | m_Crc = true; |
neilt6 | 11:67ddc53e3983 | 103 | commandTransaction(CMD59, 0x00000001); |
neilt6 | 7:61db99e52c0d | 104 | } else if (!enabled && m_Crc) { |
neilt6 | 6:55a26a56046a | 105 | //Send CMD59(0x00000000) to disable CRC |
neilt6 | 11:67ddc53e3983 | 106 | commandTransaction(CMD59, 0x00000000); |
neilt6 | 7:61db99e52c0d | 107 | m_Crc = false; |
neilt6 | 6:55a26a56046a | 108 | } |
neilt6 | 6:55a26a56046a | 109 | } |
neilt6 | 6:55a26a56046a | 110 | |
neilt6 | 6:55a26a56046a | 111 | bool SDFileSystem::large_frames() |
neilt6 | 6:55a26a56046a | 112 | { |
neilt6 | 6:55a26a56046a | 113 | //Return whether or not 16-bit frames are enabled |
neilt6 | 6:55a26a56046a | 114 | return m_LargeFrames; |
neilt6 | 6:55a26a56046a | 115 | } |
neilt6 | 6:55a26a56046a | 116 | |
neilt6 | 6:55a26a56046a | 117 | void SDFileSystem::large_frames(bool enabled) |
neilt6 | 6:55a26a56046a | 118 | { |
neilt6 | 6:55a26a56046a | 119 | //Set whether or not 16-bit frames are enabled |
neilt6 | 6:55a26a56046a | 120 | m_LargeFrames = enabled; |
neilt6 | 6:55a26a56046a | 121 | } |
neilt6 | 6:55a26a56046a | 122 | |
neilt6 | 13:635147efa748 | 123 | bool SDFileSystem::write_validation() |
neilt6 | 13:635147efa748 | 124 | { |
neilt6 | 13:635147efa748 | 125 | //Return whether or not write validation is enabled |
neilt6 | 13:635147efa748 | 126 | return m_WriteValidation; |
neilt6 | 13:635147efa748 | 127 | } |
neilt6 | 13:635147efa748 | 128 | |
neilt6 | 13:635147efa748 | 129 | void SDFileSystem::write_validation(bool enabled) |
neilt6 | 13:635147efa748 | 130 | { |
neilt6 | 13:635147efa748 | 131 | //Set whether or not write validation is enabled |
neilt6 | 13:635147efa748 | 132 | m_WriteValidation = enabled; |
neilt6 | 13:635147efa748 | 133 | } |
neilt6 | 13:635147efa748 | 134 | |
neilt6 | 11:67ddc53e3983 | 135 | int SDFileSystem::unmount() |
neilt6 | 11:67ddc53e3983 | 136 | { |
neilt6 | 11:67ddc53e3983 | 137 | //Unmount the filesystem |
neilt6 | 11:67ddc53e3983 | 138 | FATFileSystem::unmount(); |
neilt6 | 11:67ddc53e3983 | 139 | |
neilt6 | 20:2c1e8d442f68 | 140 | //Change the status to not initialized, and the card type to unknown |
neilt6 | 11:67ddc53e3983 | 141 | m_Status |= STA_NOINIT; |
neilt6 | 20:2c1e8d442f68 | 142 | m_CardType = CARD_UNKNOWN; |
neilt6 | 11:67ddc53e3983 | 143 | |
neilt6 | 11:67ddc53e3983 | 144 | //Always succeeds |
neilt6 | 11:67ddc53e3983 | 145 | return 0; |
neilt6 | 11:67ddc53e3983 | 146 | } |
neilt6 | 11:67ddc53e3983 | 147 | |
neilt6 | 0:2a6d8a096edc | 148 | int SDFileSystem::disk_initialize() |
neilt6 | 0:2a6d8a096edc | 149 | { |
neilt6 | 11:67ddc53e3983 | 150 | char token; |
neilt6 | 11:67ddc53e3983 | 151 | unsigned int resp; |
neilt6 | 0:2a6d8a096edc | 152 | |
neilt6 | 0:2a6d8a096edc | 153 | //Make sure there's a card in the socket before proceeding |
neilt6 | 0:2a6d8a096edc | 154 | checkSocket(); |
neilt6 | 0:2a6d8a096edc | 155 | if (m_Status & STA_NODISK) |
neilt6 | 0:2a6d8a096edc | 156 | return m_Status; |
neilt6 | 0:2a6d8a096edc | 157 | |
neilt6 | 0:2a6d8a096edc | 158 | //Make sure we're not already initialized before proceeding |
neilt6 | 0:2a6d8a096edc | 159 | if (!(m_Status & STA_NOINIT)) |
neilt6 | 0:2a6d8a096edc | 160 | return m_Status; |
neilt6 | 0:2a6d8a096edc | 161 | |
neilt6 | 9:1906befe7f30 | 162 | //Set the SPI frequency to 400kHz for initialization |
neilt6 | 9:1906befe7f30 | 163 | m_Spi.frequency(400000); |
neilt6 | 0:2a6d8a096edc | 164 | |
neilt6 | 10:395539a1481a | 165 | //Send 80 dummy clocks with /CS deasserted and DI held high |
neilt6 | 7:61db99e52c0d | 166 | m_Cs = 1; |
neilt6 | 0:2a6d8a096edc | 167 | for (int i = 0; i < 10; i++) |
neilt6 | 7:61db99e52c0d | 168 | m_Spi.write(0xFF); |
neilt6 | 0:2a6d8a096edc | 169 | |
neilt6 | 12:eebddab6eff2 | 170 | //Send CMD0(0x00000000) to reset the card |
neilt6 | 11:67ddc53e3983 | 171 | if (commandTransaction(CMD0, 0x00000000) != 0x01) { |
neilt6 | 0:2a6d8a096edc | 172 | //Initialization failed |
neilt6 | 0:2a6d8a096edc | 173 | m_CardType = CARD_UNKNOWN; |
neilt6 | 0:2a6d8a096edc | 174 | return m_Status; |
neilt6 | 0:2a6d8a096edc | 175 | } |
neilt6 | 0:2a6d8a096edc | 176 | |
neilt6 | 11:67ddc53e3983 | 177 | //Send CMD59(0x00000001) to enable CRC if necessary |
neilt6 | 11:67ddc53e3983 | 178 | if (m_Crc) { |
neilt6 | 11:67ddc53e3983 | 179 | if (commandTransaction(CMD59, 0x00000001) != 0x01) { |
neilt6 | 11:67ddc53e3983 | 180 | //Initialization failed |
neilt6 | 11:67ddc53e3983 | 181 | m_CardType = CARD_UNKNOWN; |
neilt6 | 11:67ddc53e3983 | 182 | return m_Status; |
neilt6 | 11:67ddc53e3983 | 183 | } |
neilt6 | 11:67ddc53e3983 | 184 | } |
neilt6 | 11:67ddc53e3983 | 185 | |
neilt6 | 12:eebddab6eff2 | 186 | //Send CMD8(0x000001AA) to see if this is an SDCv2 card |
neilt6 | 11:67ddc53e3983 | 187 | if (commandTransaction(CMD8, 0x000001AA, &resp) == 0x01) { |
neilt6 | 0:2a6d8a096edc | 188 | //This is an SDCv2 card, get the 32-bit return value and verify the voltage range/check pattern |
neilt6 | 11:67ddc53e3983 | 189 | if ((resp & 0xFFF) != 0x1AA) { |
neilt6 | 0:2a6d8a096edc | 190 | //Initialization failed |
neilt6 | 0:2a6d8a096edc | 191 | m_CardType = CARD_UNKNOWN; |
neilt6 | 0:2a6d8a096edc | 192 | return m_Status; |
neilt6 | 0:2a6d8a096edc | 193 | } |
neilt6 | 0:2a6d8a096edc | 194 | |
neilt6 | 5:6befff2300d0 | 195 | //Send CMD58(0x00000000) to read the OCR, and verify that the card supports 3.2-3.3V |
neilt6 | 11:67ddc53e3983 | 196 | if (commandTransaction(CMD58, 0x00000000, &resp) != 0x01 || !(resp & (1 << 20))) { |
neilt6 | 0:2a6d8a096edc | 197 | //Initialization failed |
neilt6 | 0:2a6d8a096edc | 198 | m_CardType = CARD_UNKNOWN; |
neilt6 | 0:2a6d8a096edc | 199 | return m_Status; |
neilt6 | 0:2a6d8a096edc | 200 | } |
neilt6 | 0:2a6d8a096edc | 201 | |
neilt6 | 22:3fa5eaf48e81 | 202 | //Try to initialize the card using ACMD41(0x00100000) |
neilt6 | 22:3fa5eaf48e81 | 203 | for (int i = 0; i < 1000; i++) { |
neilt6 | 11:67ddc53e3983 | 204 | token = commandTransaction(ACMD41, 0x40100000); |
neilt6 | 22:3fa5eaf48e81 | 205 | if (token != 0x01) { |
neilt6 | 22:3fa5eaf48e81 | 206 | break; |
neilt6 | 22:3fa5eaf48e81 | 207 | } |
neilt6 | 22:3fa5eaf48e81 | 208 | } |
neilt6 | 0:2a6d8a096edc | 209 | |
neilt6 | 0:2a6d8a096edc | 210 | //Check if the card initialized |
neilt6 | 11:67ddc53e3983 | 211 | if (token != 0x00) { |
neilt6 | 0:2a6d8a096edc | 212 | //Initialization failed |
neilt6 | 0:2a6d8a096edc | 213 | m_CardType = CARD_UNKNOWN; |
neilt6 | 0:2a6d8a096edc | 214 | return m_Status; |
neilt6 | 0:2a6d8a096edc | 215 | } |
neilt6 | 0:2a6d8a096edc | 216 | |
neilt6 | 5:6befff2300d0 | 217 | //Send CMD58(0x00000000) to read the OCR |
neilt6 | 11:67ddc53e3983 | 218 | if (commandTransaction(CMD58, 0x00000000, &resp) == 0x00) { |
neilt6 | 0:2a6d8a096edc | 219 | //Check the CCS bit to determine if this is a high capacity card |
neilt6 | 11:67ddc53e3983 | 220 | if (resp & (1 << 30)) |
neilt6 | 0:2a6d8a096edc | 221 | m_CardType = CARD_SDHC; |
neilt6 | 0:2a6d8a096edc | 222 | else |
neilt6 | 0:2a6d8a096edc | 223 | m_CardType = CARD_SD; |
neilt6 | 12:eebddab6eff2 | 224 | |
neilt6 | 24:6bb3c1987511 | 225 | //Increase the SPI frequency to full speed (up to 50MHz for SDCv2) |
neilt6 | 24:6bb3c1987511 | 226 | if (m_FREQ > 25000000) { |
neilt6 | 24:6bb3c1987511 | 227 | if (enableHighSpeedMode()) { |
neilt6 | 24:6bb3c1987511 | 228 | if (m_FREQ > 50000000) { |
neilt6 | 24:6bb3c1987511 | 229 | m_Spi.frequency(50000000); |
neilt6 | 24:6bb3c1987511 | 230 | } else { |
neilt6 | 24:6bb3c1987511 | 231 | m_Spi.frequency(m_FREQ); |
neilt6 | 24:6bb3c1987511 | 232 | } |
neilt6 | 24:6bb3c1987511 | 233 | } else { |
neilt6 | 24:6bb3c1987511 | 234 | m_Spi.frequency(25000000); |
neilt6 | 24:6bb3c1987511 | 235 | } |
neilt6 | 24:6bb3c1987511 | 236 | } else { |
neilt6 | 12:eebddab6eff2 | 237 | m_Spi.frequency(m_FREQ); |
neilt6 | 24:6bb3c1987511 | 238 | } |
neilt6 | 0:2a6d8a096edc | 239 | } else { |
neilt6 | 0:2a6d8a096edc | 240 | //Initialization failed |
neilt6 | 0:2a6d8a096edc | 241 | m_CardType = CARD_UNKNOWN; |
neilt6 | 0:2a6d8a096edc | 242 | return m_Status; |
neilt6 | 0:2a6d8a096edc | 243 | } |
neilt6 | 0:2a6d8a096edc | 244 | } else { |
neilt6 | 0:2a6d8a096edc | 245 | //Didn't respond or illegal command, this is either an SDCv1 or MMC card |
neilt6 | 5:6befff2300d0 | 246 | //Send CMD58(0x00000000) to read the OCR, and verify that the card supports 3.2-3.3V |
neilt6 | 11:67ddc53e3983 | 247 | if (commandTransaction(CMD58, 0x00000000, &resp) != 0x01 || !(resp & (1 << 20))) { |
neilt6 | 0:2a6d8a096edc | 248 | //Initialization failed |
neilt6 | 0:2a6d8a096edc | 249 | m_CardType = CARD_UNKNOWN; |
neilt6 | 0:2a6d8a096edc | 250 | return m_Status; |
neilt6 | 0:2a6d8a096edc | 251 | } |
neilt6 | 0:2a6d8a096edc | 252 | |
neilt6 | 22:3fa5eaf48e81 | 253 | //Try to initialize the card using ACMD41(0x00100000) |
neilt6 | 22:3fa5eaf48e81 | 254 | for (int i = 0; i < 1000; i++) { |
neilt6 | 22:3fa5eaf48e81 | 255 | token = commandTransaction(ACMD41, 0x40100000); |
neilt6 | 22:3fa5eaf48e81 | 256 | if (token != 0x01) { |
neilt6 | 22:3fa5eaf48e81 | 257 | break; |
neilt6 | 22:3fa5eaf48e81 | 258 | } |
neilt6 | 22:3fa5eaf48e81 | 259 | } |
neilt6 | 0:2a6d8a096edc | 260 | |
neilt6 | 0:2a6d8a096edc | 261 | //Check if the card initialized |
neilt6 | 11:67ddc53e3983 | 262 | if (token == 0x00) { |
neilt6 | 0:2a6d8a096edc | 263 | //This is an SDCv1 standard capacity card |
neilt6 | 0:2a6d8a096edc | 264 | m_CardType = CARD_SD; |
neilt6 | 12:eebddab6eff2 | 265 | |
neilt6 | 12:eebddab6eff2 | 266 | //Increase the SPI frequency to full speed (up to 25MHz for SDCv1) |
neilt6 | 12:eebddab6eff2 | 267 | if (m_FREQ > 25000000) |
neilt6 | 12:eebddab6eff2 | 268 | m_Spi.frequency(25000000); |
neilt6 | 12:eebddab6eff2 | 269 | else |
neilt6 | 12:eebddab6eff2 | 270 | m_Spi.frequency(m_FREQ); |
neilt6 | 0:2a6d8a096edc | 271 | } else { |
neilt6 | 22:3fa5eaf48e81 | 272 | //Try to initialize the card using CMD1(0x00100000) |
neilt6 | 22:3fa5eaf48e81 | 273 | for (int i = 0; i < 1000; i++) { |
neilt6 | 11:67ddc53e3983 | 274 | token = commandTransaction(CMD1, 0x00100000); |
neilt6 | 22:3fa5eaf48e81 | 275 | if (token != 0x01) { |
neilt6 | 22:3fa5eaf48e81 | 276 | break; |
neilt6 | 22:3fa5eaf48e81 | 277 | } |
neilt6 | 22:3fa5eaf48e81 | 278 | } |
neilt6 | 0:2a6d8a096edc | 279 | |
neilt6 | 0:2a6d8a096edc | 280 | //Check if the card initialized |
neilt6 | 11:67ddc53e3983 | 281 | if (token == 0x00) { |
neilt6 | 0:2a6d8a096edc | 282 | //This is an MMCv3 card |
neilt6 | 0:2a6d8a096edc | 283 | m_CardType = CARD_MMC; |
neilt6 | 12:eebddab6eff2 | 284 | |
neilt6 | 12:eebddab6eff2 | 285 | //Increase the SPI frequency to full speed (up to 20MHz for MMCv3) |
neilt6 | 12:eebddab6eff2 | 286 | if (m_FREQ > 20000000) |
neilt6 | 12:eebddab6eff2 | 287 | m_Spi.frequency(20000000); |
neilt6 | 12:eebddab6eff2 | 288 | else |
neilt6 | 12:eebddab6eff2 | 289 | m_Spi.frequency(m_FREQ); |
neilt6 | 0:2a6d8a096edc | 290 | } else { |
neilt6 | 0:2a6d8a096edc | 291 | //Initialization failed |
neilt6 | 0:2a6d8a096edc | 292 | m_CardType = CARD_UNKNOWN; |
neilt6 | 0:2a6d8a096edc | 293 | return m_Status; |
neilt6 | 0:2a6d8a096edc | 294 | } |
neilt6 | 0:2a6d8a096edc | 295 | } |
neilt6 | 0:2a6d8a096edc | 296 | } |
neilt6 | 0:2a6d8a096edc | 297 | |
neilt6 | 12:eebddab6eff2 | 298 | //Send ACMD42(0x00000000) to disconnect the internal pull-up resistor on pin 1 if necessary |
neilt6 | 12:eebddab6eff2 | 299 | if (m_CardType != CARD_MMC) { |
neilt6 | 12:eebddab6eff2 | 300 | if (commandTransaction(ACMD42, 0x00000000) != 0x00) { |
neilt6 | 12:eebddab6eff2 | 301 | //Initialization failed |
neilt6 | 12:eebddab6eff2 | 302 | m_CardType = CARD_UNKNOWN; |
neilt6 | 12:eebddab6eff2 | 303 | return m_Status; |
neilt6 | 12:eebddab6eff2 | 304 | } |
neilt6 | 12:eebddab6eff2 | 305 | } |
neilt6 | 12:eebddab6eff2 | 306 | |
neilt6 | 0:2a6d8a096edc | 307 | //Send CMD16(0x00000200) to force the block size to 512B if necessary |
neilt6 | 0:2a6d8a096edc | 308 | if (m_CardType != CARD_SDHC) { |
neilt6 | 11:67ddc53e3983 | 309 | if (commandTransaction(CMD16, 0x00000200) != 0x00) { |
neilt6 | 0:2a6d8a096edc | 310 | //Initialization failed |
neilt6 | 0:2a6d8a096edc | 311 | m_CardType = CARD_UNKNOWN; |
neilt6 | 0:2a6d8a096edc | 312 | return m_Status; |
neilt6 | 0:2a6d8a096edc | 313 | } |
neilt6 | 0:2a6d8a096edc | 314 | } |
neilt6 | 0:2a6d8a096edc | 315 | |
neilt6 | 0:2a6d8a096edc | 316 | //The card is now initialized |
neilt6 | 0:2a6d8a096edc | 317 | m_Status &= ~STA_NOINIT; |
neilt6 | 0:2a6d8a096edc | 318 | |
neilt6 | 9:1906befe7f30 | 319 | //Return the disk status |
neilt6 | 0:2a6d8a096edc | 320 | return m_Status; |
neilt6 | 0:2a6d8a096edc | 321 | } |
neilt6 | 0:2a6d8a096edc | 322 | |
neilt6 | 0:2a6d8a096edc | 323 | int SDFileSystem::disk_status() |
neilt6 | 0:2a6d8a096edc | 324 | { |
neilt6 | 20:2c1e8d442f68 | 325 | //Check the card socket |
neilt6 | 0:2a6d8a096edc | 326 | checkSocket(); |
neilt6 | 0:2a6d8a096edc | 327 | |
neilt6 | 9:1906befe7f30 | 328 | //Return the disk status |
neilt6 | 0:2a6d8a096edc | 329 | return m_Status; |
neilt6 | 0:2a6d8a096edc | 330 | } |
neilt6 | 0:2a6d8a096edc | 331 | |
neilt6 | 21:d10a519c0910 | 332 | int SDFileSystem::disk_read(uint8_t* buffer, uint32_t sector, uint32_t count) |
neilt6 | 0:2a6d8a096edc | 333 | { |
neilt6 | 9:1906befe7f30 | 334 | //Make sure the card is initialized before proceeding |
neilt6 | 0:2a6d8a096edc | 335 | if (m_Status & STA_NOINIT) |
neilt6 | 0:2a6d8a096edc | 336 | return RES_NOTRDY; |
neilt6 | 0:2a6d8a096edc | 337 | |
neilt6 | 11:67ddc53e3983 | 338 | //Read a single block, or multiple blocks |
neilt6 | 11:67ddc53e3983 | 339 | if (count > 1) { |
neilt6 | 13:635147efa748 | 340 | return readBlocks((char*)buffer, sector, count) ? RES_OK : RES_ERROR; |
neilt6 | 11:67ddc53e3983 | 341 | } else { |
neilt6 | 13:635147efa748 | 342 | return readBlock((char*)buffer, sector) ? RES_OK : RES_ERROR; |
neilt6 | 0:2a6d8a096edc | 343 | } |
neilt6 | 0:2a6d8a096edc | 344 | } |
neilt6 | 0:2a6d8a096edc | 345 | |
neilt6 | 21:d10a519c0910 | 346 | int SDFileSystem::disk_write(const uint8_t* buffer, uint32_t sector, uint32_t count) |
neilt6 | 0:2a6d8a096edc | 347 | { |
neilt6 | 9:1906befe7f30 | 348 | //Make sure the card is initialized before proceeding |
neilt6 | 0:2a6d8a096edc | 349 | if (m_Status & STA_NOINIT) |
neilt6 | 0:2a6d8a096edc | 350 | return RES_NOTRDY; |
neilt6 | 0:2a6d8a096edc | 351 | |
neilt6 | 9:1906befe7f30 | 352 | //Make sure the card isn't write protected before proceeding |
neilt6 | 0:2a6d8a096edc | 353 | if (m_Status & STA_PROTECT) |
neilt6 | 0:2a6d8a096edc | 354 | return RES_WRPRT; |
neilt6 | 0:2a6d8a096edc | 355 | |
neilt6 | 11:67ddc53e3983 | 356 | //Write a single block, or multiple blocks |
neilt6 | 11:67ddc53e3983 | 357 | if (count > 1) { |
neilt6 | 13:635147efa748 | 358 | return writeBlocks((const char*)buffer, sector, count) ? RES_OK : RES_ERROR; |
neilt6 | 11:67ddc53e3983 | 359 | } else { |
neilt6 | 13:635147efa748 | 360 | return writeBlock((const char*)buffer, sector) ? RES_OK : RES_ERROR; |
neilt6 | 0:2a6d8a096edc | 361 | } |
neilt6 | 0:2a6d8a096edc | 362 | } |
neilt6 | 0:2a6d8a096edc | 363 | |
neilt6 | 0:2a6d8a096edc | 364 | int SDFileSystem::disk_sync() |
neilt6 | 0:2a6d8a096edc | 365 | { |
neilt6 | 0:2a6d8a096edc | 366 | //Select the card so we're forced to wait for the end of any internal write processes |
neilt6 | 10:395539a1481a | 367 | if (select()) { |
neilt6 | 10:395539a1481a | 368 | deselect(); |
neilt6 | 10:395539a1481a | 369 | return RES_OK; |
neilt6 | 10:395539a1481a | 370 | } else { |
neilt6 | 10:395539a1481a | 371 | return RES_ERROR; |
neilt6 | 10:395539a1481a | 372 | } |
neilt6 | 0:2a6d8a096edc | 373 | } |
neilt6 | 0:2a6d8a096edc | 374 | |
neilt6 | 21:d10a519c0910 | 375 | uint32_t SDFileSystem::disk_sectors() |
neilt6 | 0:2a6d8a096edc | 376 | { |
neilt6 | 9:1906befe7f30 | 377 | //Make sure the card is initialized before proceeding |
neilt6 | 0:2a6d8a096edc | 378 | if (m_Status & STA_NOINIT) |
neilt6 | 0:2a6d8a096edc | 379 | return 0; |
neilt6 | 0:2a6d8a096edc | 380 | |
neilt6 | 0:2a6d8a096edc | 381 | //Try to read the CSD register up to 3 times |
neilt6 | 11:67ddc53e3983 | 382 | for (int f = 0; f < 3; f++) { |
neilt6 | 11:67ddc53e3983 | 383 | //Select the card, and wait for ready |
neilt6 | 13:635147efa748 | 384 | if(!select()) |
neilt6 | 11:67ddc53e3983 | 385 | break; |
neilt6 | 11:67ddc53e3983 | 386 | |
neilt6 | 5:6befff2300d0 | 387 | //Send CMD9(0x00000000) to read the CSD register |
neilt6 | 13:635147efa748 | 388 | if (writeCommand(CMD9, 0x00000000) == 0x00) { |
neilt6 | 11:67ddc53e3983 | 389 | //Read the 16B CSD data block |
neilt6 | 0:2a6d8a096edc | 390 | char csd[16]; |
neilt6 | 11:67ddc53e3983 | 391 | bool success = readData(csd, 16); |
neilt6 | 11:67ddc53e3983 | 392 | deselect(); |
neilt6 | 11:67ddc53e3983 | 393 | if (success) { |
neilt6 | 0:2a6d8a096edc | 394 | //Calculate the sector count based on the card type |
neilt6 | 0:2a6d8a096edc | 395 | if ((csd[0] >> 6) == 0x01) { |
neilt6 | 10:395539a1481a | 396 | //Calculate the sector count for a high capacity card |
neilt6 | 17:a47f74caa04e | 397 | unsigned int size = (((csd[7] & 0x3F) << 16) | (csd[8] << 8) | csd[9]) + 1; |
neilt6 | 10:395539a1481a | 398 | return size << 10; |
neilt6 | 0:2a6d8a096edc | 399 | } else { |
neilt6 | 10:395539a1481a | 400 | //Calculate the sector count for a standard capacity card |
neilt6 | 17:a47f74caa04e | 401 | unsigned int size = (((csd[6] & 0x03) << 10) | (csd[7] << 2) | ((csd[8] & 0xC0) >> 6)) + 1; |
neilt6 | 10:395539a1481a | 402 | size <<= ((((csd[9] & 0x03) << 1) | ((csd[10] & 0x80) >> 7)) + 2); |
neilt6 | 10:395539a1481a | 403 | size <<= (csd[5] & 0x0F); |
neilt6 | 10:395539a1481a | 404 | return size >> 9; |
neilt6 | 0:2a6d8a096edc | 405 | } |
neilt6 | 0:2a6d8a096edc | 406 | } |
neilt6 | 0:2a6d8a096edc | 407 | } else { |
neilt6 | 11:67ddc53e3983 | 408 | //The command failed, get out |
neilt6 | 11:67ddc53e3983 | 409 | break; |
neilt6 | 0:2a6d8a096edc | 410 | } |
neilt6 | 0:2a6d8a096edc | 411 | } |
neilt6 | 0:2a6d8a096edc | 412 | |
neilt6 | 9:1906befe7f30 | 413 | //The read operation failed 3 times |
neilt6 | 11:67ddc53e3983 | 414 | deselect(); |
neilt6 | 0:2a6d8a096edc | 415 | return 0; |
neilt6 | 0:2a6d8a096edc | 416 | } |
neilt6 | 0:2a6d8a096edc | 417 | |
neilt6 | 13:635147efa748 | 418 | void SDFileSystem::onCardRemoval() |
neilt6 | 13:635147efa748 | 419 | { |
neilt6 | 20:2c1e8d442f68 | 420 | //Check the card socket |
neilt6 | 13:635147efa748 | 421 | checkSocket(); |
neilt6 | 13:635147efa748 | 422 | } |
neilt6 | 13:635147efa748 | 423 | |
neilt6 | 13:635147efa748 | 424 | inline void SDFileSystem::checkSocket() |
neilt6 | 0:2a6d8a096edc | 425 | { |
neilt6 | 16:c2c1f0b16380 | 426 | //Use the card detect switch (if available) to determine if the socket is occupied |
neilt6 | 20:2c1e8d442f68 | 427 | if (m_CdAssert != -1) { |
neilt6 | 20:2c1e8d442f68 | 428 | if (m_Status & STA_NODISK) { |
neilt6 | 20:2c1e8d442f68 | 429 | if (m_Cd == m_CdAssert) { |
neilt6 | 20:2c1e8d442f68 | 430 | //The socket is now occupied |
neilt6 | 20:2c1e8d442f68 | 431 | m_Status &= ~STA_NODISK; |
neilt6 | 20:2c1e8d442f68 | 432 | m_CardType = CARD_UNKNOWN; |
neilt6 | 20:2c1e8d442f68 | 433 | } |
neilt6 | 20:2c1e8d442f68 | 434 | } else { |
neilt6 | 20:2c1e8d442f68 | 435 | if (m_Cd != m_CdAssert) { |
neilt6 | 20:2c1e8d442f68 | 436 | //The socket is now empty |
neilt6 | 20:2c1e8d442f68 | 437 | m_Status |= (STA_NODISK | STA_NOINIT); |
neilt6 | 20:2c1e8d442f68 | 438 | m_CardType = CARD_NONE; |
neilt6 | 20:2c1e8d442f68 | 439 | } |
neilt6 | 20:2c1e8d442f68 | 440 | } |
neilt6 | 0:2a6d8a096edc | 441 | } |
neilt6 | 0:2a6d8a096edc | 442 | } |
neilt6 | 0:2a6d8a096edc | 443 | |
neilt6 | 0:2a6d8a096edc | 444 | inline bool SDFileSystem::waitReady(int timeout) |
neilt6 | 0:2a6d8a096edc | 445 | { |
neilt6 | 13:635147efa748 | 446 | char resp; |
neilt6 | 0:2a6d8a096edc | 447 | |
neilt6 | 13:635147efa748 | 448 | //Keep sending dummy clocks with DI held high until the card releases the DO line |
neilt6 | 13:635147efa748 | 449 | m_Timer.start(); |
neilt6 | 13:635147efa748 | 450 | do { |
neilt6 | 13:635147efa748 | 451 | resp = m_Spi.write(0xFF); |
neilt6 | 13:635147efa748 | 452 | } while (resp == 0x00 && m_Timer.read_ms() < timeout); |
neilt6 | 13:635147efa748 | 453 | m_Timer.stop(); |
neilt6 | 13:635147efa748 | 454 | m_Timer.reset(); |
neilt6 | 13:635147efa748 | 455 | |
neilt6 | 13:635147efa748 | 456 | //Return success/failure |
neilt6 | 13:635147efa748 | 457 | return (resp > 0x00); |
neilt6 | 0:2a6d8a096edc | 458 | } |
neilt6 | 0:2a6d8a096edc | 459 | |
neilt6 | 0:2a6d8a096edc | 460 | inline bool SDFileSystem::select() |
neilt6 | 0:2a6d8a096edc | 461 | { |
neilt6 | 10:395539a1481a | 462 | //Assert /CS |
neilt6 | 7:61db99e52c0d | 463 | m_Cs = 0; |
neilt6 | 0:2a6d8a096edc | 464 | |
neilt6 | 9:1906befe7f30 | 465 | //Send 8 dummy clocks with DI held high to enable DO |
neilt6 | 7:61db99e52c0d | 466 | m_Spi.write(0xFF); |
neilt6 | 0:2a6d8a096edc | 467 | |
neilt6 | 0:2a6d8a096edc | 468 | //Wait for up to 500ms for the card to become ready |
neilt6 | 9:1906befe7f30 | 469 | if (waitReady(500)) { |
neilt6 | 0:2a6d8a096edc | 470 | return true; |
neilt6 | 9:1906befe7f30 | 471 | } else { |
neilt6 | 9:1906befe7f30 | 472 | //We timed out, deselect and return false |
neilt6 | 9:1906befe7f30 | 473 | deselect(); |
neilt6 | 9:1906befe7f30 | 474 | return false; |
neilt6 | 9:1906befe7f30 | 475 | } |
neilt6 | 0:2a6d8a096edc | 476 | } |
neilt6 | 0:2a6d8a096edc | 477 | |
neilt6 | 0:2a6d8a096edc | 478 | inline void SDFileSystem::deselect() |
neilt6 | 0:2a6d8a096edc | 479 | { |
neilt6 | 10:395539a1481a | 480 | //Deassert /CS |
neilt6 | 7:61db99e52c0d | 481 | m_Cs = 1; |
neilt6 | 0:2a6d8a096edc | 482 | |
neilt6 | 11:67ddc53e3983 | 483 | //Send 8 dummy clocks with DI held high to disable DO |
neilt6 | 7:61db99e52c0d | 484 | m_Spi.write(0xFF); |
neilt6 | 0:2a6d8a096edc | 485 | } |
neilt6 | 0:2a6d8a096edc | 486 | |
neilt6 | 11:67ddc53e3983 | 487 | inline char SDFileSystem::commandTransaction(char cmd, unsigned int arg, unsigned int* resp) |
neilt6 | 0:2a6d8a096edc | 488 | { |
neilt6 | 11:67ddc53e3983 | 489 | //Select the card, and wait for ready |
neilt6 | 13:635147efa748 | 490 | if(!select()) |
neilt6 | 11:67ddc53e3983 | 491 | return 0xFF; |
neilt6 | 11:67ddc53e3983 | 492 | |
neilt6 | 11:67ddc53e3983 | 493 | //Perform the command transaction |
neilt6 | 13:635147efa748 | 494 | char token = writeCommand(cmd, arg, resp); |
neilt6 | 11:67ddc53e3983 | 495 | |
neilt6 | 11:67ddc53e3983 | 496 | //Deselect the card, and return the R1 response token |
neilt6 | 11:67ddc53e3983 | 497 | deselect(); |
neilt6 | 11:67ddc53e3983 | 498 | return token; |
neilt6 | 11:67ddc53e3983 | 499 | } |
neilt6 | 11:67ddc53e3983 | 500 | |
neilt6 | 13:635147efa748 | 501 | char SDFileSystem::writeCommand(char cmd, unsigned int arg, unsigned int* resp) |
neilt6 | 11:67ddc53e3983 | 502 | { |
neilt6 | 11:67ddc53e3983 | 503 | char token; |
neilt6 | 0:2a6d8a096edc | 504 | |
neilt6 | 0:2a6d8a096edc | 505 | //Try to send the command up to 3 times |
neilt6 | 11:67ddc53e3983 | 506 | for (int f = 0; f < 3; f++) { |
neilt6 | 5:6befff2300d0 | 507 | //Send CMD55(0x00000000) prior to an application specific command |
neilt6 | 13:635147efa748 | 508 | if (cmd == ACMD22 || cmd == ACMD23 || cmd == ACMD41 || cmd == ACMD42) { |
neilt6 | 13:635147efa748 | 509 | token = writeCommand(CMD55, 0x00000000); |
neilt6 | 11:67ddc53e3983 | 510 | if (token > 0x01) |
neilt6 | 11:67ddc53e3983 | 511 | return token; |
neilt6 | 11:67ddc53e3983 | 512 | |
neilt6 | 13:635147efa748 | 513 | //Deselect and reselect the card between CMD55 and an ACMD |
neilt6 | 13:635147efa748 | 514 | deselect(); |
neilt6 | 13:635147efa748 | 515 | if(!select()) |
neilt6 | 13:635147efa748 | 516 | return 0xFF; |
neilt6 | 0:2a6d8a096edc | 517 | } |
neilt6 | 0:2a6d8a096edc | 518 | |
neilt6 | 0:2a6d8a096edc | 519 | //Prepare the command packet |
neilt6 | 0:2a6d8a096edc | 520 | char cmdPacket[6]; |
neilt6 | 4:49b29888eca7 | 521 | cmdPacket[0] = cmd; |
neilt6 | 0:2a6d8a096edc | 522 | cmdPacket[1] = arg >> 24; |
neilt6 | 0:2a6d8a096edc | 523 | cmdPacket[2] = arg >> 16; |
neilt6 | 0:2a6d8a096edc | 524 | cmdPacket[3] = arg >> 8; |
neilt6 | 0:2a6d8a096edc | 525 | cmdPacket[4] = arg; |
neilt6 | 7:61db99e52c0d | 526 | if (m_Crc || cmd == CMD0 || cmd == CMD8) |
neilt6 | 18:2286a4e7fa31 | 527 | cmdPacket[5] = (SDCRC::crc7(cmdPacket, 5) << 1) | 0x01; |
neilt6 | 6:55a26a56046a | 528 | else |
neilt6 | 6:55a26a56046a | 529 | cmdPacket[5] = 0x01; |
neilt6 | 0:2a6d8a096edc | 530 | |
neilt6 | 0:2a6d8a096edc | 531 | //Send the command packet |
neilt6 | 11:67ddc53e3983 | 532 | for (int i = 0; i < 6; i++) |
neilt6 | 11:67ddc53e3983 | 533 | m_Spi.write(cmdPacket[i]); |
neilt6 | 0:2a6d8a096edc | 534 | |
neilt6 | 11:67ddc53e3983 | 535 | //Discard the stuff byte immediately following CMD12 |
neilt6 | 11:67ddc53e3983 | 536 | if (cmd == CMD12) |
neilt6 | 11:67ddc53e3983 | 537 | m_Spi.write(0xFF); |
neilt6 | 11:67ddc53e3983 | 538 | |
neilt6 | 11:67ddc53e3983 | 539 | //Allow up to 8 bytes of delay for the R1 response token |
neilt6 | 11:67ddc53e3983 | 540 | for (int i = 0; i < 9; i++) { |
neilt6 | 11:67ddc53e3983 | 541 | token = m_Spi.write(0xFF); |
neilt6 | 11:67ddc53e3983 | 542 | if (!(token & 0x80)) |
neilt6 | 0:2a6d8a096edc | 543 | break; |
neilt6 | 0:2a6d8a096edc | 544 | } |
neilt6 | 0:2a6d8a096edc | 545 | |
neilt6 | 11:67ddc53e3983 | 546 | //Verify the R1 response token |
neilt6 | 11:67ddc53e3983 | 547 | if (token == 0xFF) { |
neilt6 | 11:67ddc53e3983 | 548 | //No data was received, get out early |
neilt6 | 11:67ddc53e3983 | 549 | break; |
neilt6 | 11:67ddc53e3983 | 550 | } else if (token & (1 << 3)) { |
neilt6 | 11:67ddc53e3983 | 551 | //There was a CRC error, try again |
neilt6 | 11:67ddc53e3983 | 552 | continue; |
neilt6 | 11:67ddc53e3983 | 553 | } else if (token > 0x01) { |
neilt6 | 11:67ddc53e3983 | 554 | //An error occured, get out early |
neilt6 | 11:67ddc53e3983 | 555 | break; |
neilt6 | 11:67ddc53e3983 | 556 | } |
neilt6 | 0:2a6d8a096edc | 557 | |
neilt6 | 11:67ddc53e3983 | 558 | //Handle R2 and R3/R7 response tokens |
neilt6 | 11:67ddc53e3983 | 559 | if (cmd == CMD13 && resp != NULL) { |
neilt6 | 11:67ddc53e3983 | 560 | //Read the R2 response value |
neilt6 | 11:67ddc53e3983 | 561 | *resp = m_Spi.write(0xFF); |
neilt6 | 11:67ddc53e3983 | 562 | } else if ((cmd == CMD8 || cmd == CMD58) && resp != NULL) { |
neilt6 | 11:67ddc53e3983 | 563 | //Read the R3/R7 response value |
neilt6 | 11:67ddc53e3983 | 564 | *resp = (m_Spi.write(0xFF) << 24); |
neilt6 | 11:67ddc53e3983 | 565 | *resp |= (m_Spi.write(0xFF) << 16); |
neilt6 | 11:67ddc53e3983 | 566 | *resp |= (m_Spi.write(0xFF) << 8); |
neilt6 | 11:67ddc53e3983 | 567 | *resp |= m_Spi.write(0xFF); |
neilt6 | 11:67ddc53e3983 | 568 | } |
neilt6 | 11:67ddc53e3983 | 569 | |
neilt6 | 11:67ddc53e3983 | 570 | //The command was successful |
neilt6 | 11:67ddc53e3983 | 571 | break; |
neilt6 | 0:2a6d8a096edc | 572 | } |
neilt6 | 0:2a6d8a096edc | 573 | |
neilt6 | 11:67ddc53e3983 | 574 | //Return the R1 response token |
neilt6 | 11:67ddc53e3983 | 575 | return token; |
neilt6 | 0:2a6d8a096edc | 576 | } |
neilt6 | 0:2a6d8a096edc | 577 | |
neilt6 | 0:2a6d8a096edc | 578 | bool SDFileSystem::readData(char* buffer, int length) |
neilt6 | 0:2a6d8a096edc | 579 | { |
neilt6 | 0:2a6d8a096edc | 580 | char token; |
neilt6 | 6:55a26a56046a | 581 | unsigned short crc; |
neilt6 | 0:2a6d8a096edc | 582 | |
neilt6 | 13:635147efa748 | 583 | //Wait for up to 500ms for a token to arrive |
neilt6 | 13:635147efa748 | 584 | m_Timer.start(); |
neilt6 | 13:635147efa748 | 585 | do { |
neilt6 | 7:61db99e52c0d | 586 | token = m_Spi.write(0xFF); |
neilt6 | 13:635147efa748 | 587 | } while (token == 0xFF && m_Timer.read_ms() < 500); |
neilt6 | 13:635147efa748 | 588 | m_Timer.stop(); |
neilt6 | 13:635147efa748 | 589 | m_Timer.reset(); |
neilt6 | 0:2a6d8a096edc | 590 | |
neilt6 | 13:635147efa748 | 591 | //Check if a valid start block token was received |
neilt6 | 11:67ddc53e3983 | 592 | if (token != 0xFE) |
neilt6 | 0:2a6d8a096edc | 593 | return false; |
neilt6 | 6:55a26a56046a | 594 | |
neilt6 | 6:55a26a56046a | 595 | //Check if large frames are enabled or not |
neilt6 | 6:55a26a56046a | 596 | if (m_LargeFrames) { |
neilt6 | 6:55a26a56046a | 597 | //Switch to 16-bit frames for better performance |
neilt6 | 7:61db99e52c0d | 598 | m_Spi.format(16, 0); |
neilt6 | 0:2a6d8a096edc | 599 | |
neilt6 | 9:1906befe7f30 | 600 | //Read the data block into the buffer |
neilt6 | 6:55a26a56046a | 601 | unsigned short dataWord; |
neilt6 | 6:55a26a56046a | 602 | for (int i = 0; i < length; i += 2) { |
neilt6 | 7:61db99e52c0d | 603 | dataWord = m_Spi.write(0xFFFF); |
neilt6 | 6:55a26a56046a | 604 | buffer[i] = dataWord >> 8; |
neilt6 | 6:55a26a56046a | 605 | buffer[i + 1] = dataWord; |
neilt6 | 6:55a26a56046a | 606 | } |
neilt6 | 6:55a26a56046a | 607 | |
neilt6 | 6:55a26a56046a | 608 | //Read the CRC16 checksum for the data block |
neilt6 | 7:61db99e52c0d | 609 | crc = m_Spi.write(0xFFFF); |
neilt6 | 0:2a6d8a096edc | 610 | |
neilt6 | 6:55a26a56046a | 611 | //Switch back to 8-bit frames |
neilt6 | 7:61db99e52c0d | 612 | m_Spi.format(8, 0); |
neilt6 | 6:55a26a56046a | 613 | } else { |
neilt6 | 6:55a26a56046a | 614 | //Read the data into the buffer |
neilt6 | 6:55a26a56046a | 615 | for (int i = 0; i < length; i++) |
neilt6 | 7:61db99e52c0d | 616 | buffer[i] = m_Spi.write(0xFF); |
neilt6 | 6:55a26a56046a | 617 | |
neilt6 | 6:55a26a56046a | 618 | //Read the CRC16 checksum for the data block |
neilt6 | 7:61db99e52c0d | 619 | crc = (m_Spi.write(0xFF) << 8); |
neilt6 | 7:61db99e52c0d | 620 | crc |= m_Spi.write(0xFF); |
neilt6 | 6:55a26a56046a | 621 | } |
neilt6 | 6:55a26a56046a | 622 | |
neilt6 | 9:1906befe7f30 | 623 | //Return the validity of the CRC16 checksum (if enabled) |
neilt6 | 18:2286a4e7fa31 | 624 | return (!m_Crc || crc == SDCRC::crc16(buffer, length)); |
neilt6 | 0:2a6d8a096edc | 625 | } |
neilt6 | 9:1906befe7f30 | 626 | |
neilt6 | 11:67ddc53e3983 | 627 | char SDFileSystem::writeData(const char* buffer, char token) |
neilt6 | 9:1906befe7f30 | 628 | { |
neilt6 | 9:1906befe7f30 | 629 | //Calculate the CRC16 checksum for the data block (if enabled) |
neilt6 | 18:2286a4e7fa31 | 630 | unsigned short crc = (m_Crc) ? SDCRC::crc16(buffer, 512) : 0xFFFF; |
neilt6 | 9:1906befe7f30 | 631 | |
neilt6 | 13:635147efa748 | 632 | //Wait for up to 500ms for the card to become ready |
neilt6 | 13:635147efa748 | 633 | if (!waitReady(500)) |
neilt6 | 13:635147efa748 | 634 | return false; |
neilt6 | 11:67ddc53e3983 | 635 | |
neilt6 | 11:67ddc53e3983 | 636 | //Send the start block token |
neilt6 | 11:67ddc53e3983 | 637 | m_Spi.write(token); |
neilt6 | 11:67ddc53e3983 | 638 | |
neilt6 | 9:1906befe7f30 | 639 | //Check if large frames are enabled or not |
neilt6 | 9:1906befe7f30 | 640 | if (m_LargeFrames) { |
neilt6 | 9:1906befe7f30 | 641 | //Switch to 16-bit frames for better performance |
neilt6 | 9:1906befe7f30 | 642 | m_Spi.format(16, 0); |
neilt6 | 9:1906befe7f30 | 643 | |
neilt6 | 9:1906befe7f30 | 644 | //Write the data block from the buffer |
neilt6 | 11:67ddc53e3983 | 645 | for (int i = 0; i < 512; i += 2) |
neilt6 | 11:67ddc53e3983 | 646 | m_Spi.write((buffer[i] << 8) | buffer[i + 1]); |
neilt6 | 9:1906befe7f30 | 647 | |
neilt6 | 9:1906befe7f30 | 648 | //Send the CRC16 checksum for the data block |
neilt6 | 9:1906befe7f30 | 649 | m_Spi.write(crc); |
neilt6 | 9:1906befe7f30 | 650 | |
neilt6 | 9:1906befe7f30 | 651 | //Switch back to 8-bit frames |
neilt6 | 9:1906befe7f30 | 652 | m_Spi.format(8, 0); |
neilt6 | 9:1906befe7f30 | 653 | } else { |
neilt6 | 9:1906befe7f30 | 654 | //Write the data block from the buffer |
neilt6 | 11:67ddc53e3983 | 655 | for (int i = 0; i < 512; i++) |
neilt6 | 11:67ddc53e3983 | 656 | m_Spi.write(buffer[i]); |
neilt6 | 9:1906befe7f30 | 657 | |
neilt6 | 9:1906befe7f30 | 658 | //Send the CRC16 checksum for the data block |
neilt6 | 9:1906befe7f30 | 659 | m_Spi.write(crc >> 8); |
neilt6 | 9:1906befe7f30 | 660 | m_Spi.write(crc); |
neilt6 | 9:1906befe7f30 | 661 | } |
neilt6 | 9:1906befe7f30 | 662 | |
neilt6 | 11:67ddc53e3983 | 663 | //Return the data response token |
neilt6 | 11:67ddc53e3983 | 664 | return (m_Spi.write(0xFF) & 0x1F); |
neilt6 | 11:67ddc53e3983 | 665 | } |
neilt6 | 11:67ddc53e3983 | 666 | |
neilt6 | 17:a47f74caa04e | 667 | inline bool SDFileSystem::readBlock(char* buffer, unsigned int lba) |
neilt6 | 11:67ddc53e3983 | 668 | { |
neilt6 | 11:67ddc53e3983 | 669 | //Try to read the block up to 3 times |
neilt6 | 11:67ddc53e3983 | 670 | for (int f = 0; f < 3; f++) { |
neilt6 | 11:67ddc53e3983 | 671 | //Select the card, and wait for ready |
neilt6 | 13:635147efa748 | 672 | if(!select()) |
neilt6 | 11:67ddc53e3983 | 673 | break; |
neilt6 | 11:67ddc53e3983 | 674 | |
neilt6 | 11:67ddc53e3983 | 675 | //Send CMD17(block) to read a single block |
neilt6 | 13:635147efa748 | 676 | if (writeCommand(CMD17, (m_CardType == CARD_SDHC) ? lba : lba << 9) == 0x00) { |
neilt6 | 11:67ddc53e3983 | 677 | //Try to read the block, and deselect the card |
neilt6 | 11:67ddc53e3983 | 678 | bool success = readData(buffer, 512); |
neilt6 | 11:67ddc53e3983 | 679 | deselect(); |
neilt6 | 11:67ddc53e3983 | 680 | |
neilt6 | 11:67ddc53e3983 | 681 | //Return if successful |
neilt6 | 11:67ddc53e3983 | 682 | if (success) |
neilt6 | 11:67ddc53e3983 | 683 | return true; |
neilt6 | 11:67ddc53e3983 | 684 | } else { |
neilt6 | 11:67ddc53e3983 | 685 | //The command failed, get out |
neilt6 | 11:67ddc53e3983 | 686 | break; |
neilt6 | 11:67ddc53e3983 | 687 | } |
neilt6 | 11:67ddc53e3983 | 688 | } |
neilt6 | 11:67ddc53e3983 | 689 | |
neilt6 | 11:67ddc53e3983 | 690 | //The single block read failed |
neilt6 | 11:67ddc53e3983 | 691 | deselect(); |
neilt6 | 11:67ddc53e3983 | 692 | return false; |
neilt6 | 11:67ddc53e3983 | 693 | } |
neilt6 | 11:67ddc53e3983 | 694 | |
neilt6 | 17:a47f74caa04e | 695 | inline bool SDFileSystem::readBlocks(char* buffer, unsigned int lba, unsigned int count) |
neilt6 | 11:67ddc53e3983 | 696 | { |
neilt6 | 11:67ddc53e3983 | 697 | //Try to read each block up to 3 times |
neilt6 | 11:67ddc53e3983 | 698 | for (int f = 0; f < 3;) { |
neilt6 | 11:67ddc53e3983 | 699 | //Select the card, and wait for ready |
neilt6 | 13:635147efa748 | 700 | if(!select()) |
neilt6 | 11:67ddc53e3983 | 701 | break; |
neilt6 | 9:1906befe7f30 | 702 | |
neilt6 | 11:67ddc53e3983 | 703 | //Send CMD18(block) to read multiple blocks |
neilt6 | 13:635147efa748 | 704 | if (writeCommand(CMD18, (m_CardType == CARD_SDHC) ? lba : lba << 9) == 0x00) { |
neilt6 | 11:67ddc53e3983 | 705 | //Try to read all of the data blocks |
neilt6 | 11:67ddc53e3983 | 706 | do { |
neilt6 | 13:635147efa748 | 707 | //Read the next block, and break on errors |
neilt6 | 11:67ddc53e3983 | 708 | if (!readData(buffer, 512)) { |
neilt6 | 11:67ddc53e3983 | 709 | f++; |
neilt6 | 11:67ddc53e3983 | 710 | break; |
neilt6 | 11:67ddc53e3983 | 711 | } |
neilt6 | 11:67ddc53e3983 | 712 | |
neilt6 | 11:67ddc53e3983 | 713 | //Update the variables |
neilt6 | 11:67ddc53e3983 | 714 | lba++; |
neilt6 | 11:67ddc53e3983 | 715 | buffer += 512; |
neilt6 | 11:67ddc53e3983 | 716 | f = 0; |
neilt6 | 11:67ddc53e3983 | 717 | } while (--count); |
neilt6 | 11:67ddc53e3983 | 718 | |
neilt6 | 11:67ddc53e3983 | 719 | //Send CMD12(0x00000000) to stop the transmission |
neilt6 | 13:635147efa748 | 720 | if (writeCommand(CMD12, 0x00000000) != 0x00) { |
neilt6 | 11:67ddc53e3983 | 721 | //The command failed, get out |
neilt6 | 11:67ddc53e3983 | 722 | break; |
neilt6 | 11:67ddc53e3983 | 723 | } |
neilt6 | 11:67ddc53e3983 | 724 | |
neilt6 | 13:635147efa748 | 725 | //Deselect the card, and return if successful |
neilt6 | 11:67ddc53e3983 | 726 | deselect(); |
neilt6 | 11:67ddc53e3983 | 727 | if (count == 0) |
neilt6 | 11:67ddc53e3983 | 728 | return true; |
neilt6 | 11:67ddc53e3983 | 729 | } else { |
neilt6 | 11:67ddc53e3983 | 730 | //The command failed, get out |
neilt6 | 11:67ddc53e3983 | 731 | break; |
neilt6 | 11:67ddc53e3983 | 732 | } |
neilt6 | 11:67ddc53e3983 | 733 | } |
neilt6 | 11:67ddc53e3983 | 734 | |
neilt6 | 11:67ddc53e3983 | 735 | //The multiple block read failed |
neilt6 | 9:1906befe7f30 | 736 | deselect(); |
neilt6 | 11:67ddc53e3983 | 737 | return false; |
neilt6 | 11:67ddc53e3983 | 738 | } |
neilt6 | 9:1906befe7f30 | 739 | |
neilt6 | 17:a47f74caa04e | 740 | inline bool SDFileSystem::writeBlock(const char* buffer, unsigned int lba) |
neilt6 | 11:67ddc53e3983 | 741 | { |
neilt6 | 11:67ddc53e3983 | 742 | //Try to write the block up to 3 times |
neilt6 | 11:67ddc53e3983 | 743 | for (int f = 0; f < 3; f++) { |
neilt6 | 11:67ddc53e3983 | 744 | //Select the card, and wait for ready |
neilt6 | 13:635147efa748 | 745 | if(!select()) |
neilt6 | 11:67ddc53e3983 | 746 | break; |
neilt6 | 11:67ddc53e3983 | 747 | |
neilt6 | 11:67ddc53e3983 | 748 | //Send CMD24(block) to write a single block |
neilt6 | 13:635147efa748 | 749 | if (writeCommand(CMD24, (m_CardType == CARD_SDHC) ? lba : lba << 9) == 0x00) { |
neilt6 | 11:67ddc53e3983 | 750 | //Try to write the block, and deselect the card |
neilt6 | 11:67ddc53e3983 | 751 | char token = writeData(buffer, 0xFE); |
neilt6 | 11:67ddc53e3983 | 752 | deselect(); |
neilt6 | 11:67ddc53e3983 | 753 | |
neilt6 | 11:67ddc53e3983 | 754 | //Check the data response token |
neilt6 | 11:67ddc53e3983 | 755 | if (token == 0x0A) { |
neilt6 | 11:67ddc53e3983 | 756 | //A CRC error occured, try again |
neilt6 | 11:67ddc53e3983 | 757 | continue; |
neilt6 | 11:67ddc53e3983 | 758 | } else if (token == 0x0C) { |
neilt6 | 11:67ddc53e3983 | 759 | //A write error occured, get out |
neilt6 | 11:67ddc53e3983 | 760 | break; |
neilt6 | 11:67ddc53e3983 | 761 | } |
neilt6 | 11:67ddc53e3983 | 762 | |
neilt6 | 13:635147efa748 | 763 | //Send CMD13(0x00000000) to verify that the programming was successful if enabled |
neilt6 | 13:635147efa748 | 764 | if (m_WriteValidation) { |
neilt6 | 13:635147efa748 | 765 | unsigned int resp; |
neilt6 | 13:635147efa748 | 766 | if (commandTransaction(CMD13, 0x00000000, &resp) != 0x00 || resp != 0x00) { |
neilt6 | 13:635147efa748 | 767 | //Some manner of unrecoverable write error occured during programming, get out |
neilt6 | 13:635147efa748 | 768 | break; |
neilt6 | 13:635147efa748 | 769 | } |
neilt6 | 11:67ddc53e3983 | 770 | } |
neilt6 | 11:67ddc53e3983 | 771 | |
neilt6 | 11:67ddc53e3983 | 772 | //The data was written successfully |
neilt6 | 11:67ddc53e3983 | 773 | return true; |
neilt6 | 11:67ddc53e3983 | 774 | } else { |
neilt6 | 11:67ddc53e3983 | 775 | //The command failed, get out |
neilt6 | 11:67ddc53e3983 | 776 | break; |
neilt6 | 11:67ddc53e3983 | 777 | } |
neilt6 | 11:67ddc53e3983 | 778 | } |
neilt6 | 11:67ddc53e3983 | 779 | |
neilt6 | 11:67ddc53e3983 | 780 | //The single block write failed |
neilt6 | 11:67ddc53e3983 | 781 | deselect(); |
neilt6 | 11:67ddc53e3983 | 782 | return false; |
neilt6 | 9:1906befe7f30 | 783 | } |
neilt6 | 11:67ddc53e3983 | 784 | |
neilt6 | 17:a47f74caa04e | 785 | inline bool SDFileSystem::writeBlocks(const char* buffer, unsigned int lba, unsigned int count) |
neilt6 | 11:67ddc53e3983 | 786 | { |
neilt6 | 11:67ddc53e3983 | 787 | char token; |
neilt6 | 11:67ddc53e3983 | 788 | const char* currentBuffer = buffer; |
neilt6 | 17:a47f74caa04e | 789 | unsigned int currentLba = lba; |
neilt6 | 11:67ddc53e3983 | 790 | int currentCount = count; |
neilt6 | 11:67ddc53e3983 | 791 | |
neilt6 | 11:67ddc53e3983 | 792 | //Try to write each block up to 3 times |
neilt6 | 11:67ddc53e3983 | 793 | for (int f = 0; f < 3;) { |
neilt6 | 11:67ddc53e3983 | 794 | //If this is an SD card, send ACMD23(count) to set the number of blocks to pre-erase |
neilt6 | 11:67ddc53e3983 | 795 | if (m_CardType != CARD_MMC) { |
neilt6 | 11:67ddc53e3983 | 796 | if (commandTransaction(ACMD23, currentCount) != 0x00) { |
neilt6 | 11:67ddc53e3983 | 797 | //The command failed, get out |
neilt6 | 11:67ddc53e3983 | 798 | break; |
neilt6 | 11:67ddc53e3983 | 799 | } |
neilt6 | 11:67ddc53e3983 | 800 | } |
neilt6 | 11:67ddc53e3983 | 801 | |
neilt6 | 11:67ddc53e3983 | 802 | //Select the card, and wait for ready |
neilt6 | 13:635147efa748 | 803 | if(!select()) |
neilt6 | 11:67ddc53e3983 | 804 | break; |
neilt6 | 11:67ddc53e3983 | 805 | |
neilt6 | 11:67ddc53e3983 | 806 | //Send CMD25(block) to write multiple blocks |
neilt6 | 13:635147efa748 | 807 | if (writeCommand(CMD25, (m_CardType == CARD_SDHC) ? currentLba : currentLba << 9) == 0x00) { |
neilt6 | 11:67ddc53e3983 | 808 | //Try to write all of the data blocks |
neilt6 | 11:67ddc53e3983 | 809 | do { |
neilt6 | 11:67ddc53e3983 | 810 | //Write the next block and break on errors |
neilt6 | 11:67ddc53e3983 | 811 | token = writeData(currentBuffer, 0xFC); |
neilt6 | 11:67ddc53e3983 | 812 | if (token != 0x05) { |
neilt6 | 11:67ddc53e3983 | 813 | f++; |
neilt6 | 11:67ddc53e3983 | 814 | break; |
neilt6 | 11:67ddc53e3983 | 815 | } |
neilt6 | 11:67ddc53e3983 | 816 | |
neilt6 | 11:67ddc53e3983 | 817 | //Update the variables |
neilt6 | 11:67ddc53e3983 | 818 | currentBuffer += 512; |
neilt6 | 11:67ddc53e3983 | 819 | f = 0; |
neilt6 | 11:67ddc53e3983 | 820 | } while (--currentCount); |
neilt6 | 11:67ddc53e3983 | 821 | |
neilt6 | 13:635147efa748 | 822 | //Wait for up to 500ms for the card to finish processing the last block |
neilt6 | 13:635147efa748 | 823 | if (!waitReady(500)) |
neilt6 | 13:635147efa748 | 824 | break; |
neilt6 | 11:67ddc53e3983 | 825 | |
neilt6 | 11:67ddc53e3983 | 826 | //Finalize the transmission |
neilt6 | 11:67ddc53e3983 | 827 | if (currentCount == 0) { |
neilt6 | 13:635147efa748 | 828 | //Send the stop tran token, and deselect the card |
neilt6 | 11:67ddc53e3983 | 829 | m_Spi.write(0xFD); |
neilt6 | 11:67ddc53e3983 | 830 | deselect(); |
neilt6 | 11:67ddc53e3983 | 831 | |
neilt6 | 13:635147efa748 | 832 | //Send CMD13(0x00000000) to verify that the programming was successful if enabled |
neilt6 | 13:635147efa748 | 833 | if (m_WriteValidation) { |
neilt6 | 13:635147efa748 | 834 | unsigned int resp; |
neilt6 | 13:635147efa748 | 835 | if (commandTransaction(CMD13, 0x00000000, &resp) != 0x00 || resp != 0x00) { |
neilt6 | 13:635147efa748 | 836 | //Some manner of unrecoverable write error occured during programming, get out |
neilt6 | 13:635147efa748 | 837 | break; |
neilt6 | 13:635147efa748 | 838 | } |
neilt6 | 11:67ddc53e3983 | 839 | } |
neilt6 | 11:67ddc53e3983 | 840 | |
neilt6 | 11:67ddc53e3983 | 841 | //The data was written successfully |
neilt6 | 11:67ddc53e3983 | 842 | return true; |
neilt6 | 11:67ddc53e3983 | 843 | } else { |
neilt6 | 11:67ddc53e3983 | 844 | //Send CMD12(0x00000000) to abort the transmission |
neilt6 | 13:635147efa748 | 845 | if (writeCommand(CMD12, 0x00000000) != 0x00) { |
neilt6 | 11:67ddc53e3983 | 846 | //The command failed, get out |
neilt6 | 11:67ddc53e3983 | 847 | break; |
neilt6 | 11:67ddc53e3983 | 848 | } |
neilt6 | 11:67ddc53e3983 | 849 | |
neilt6 | 13:635147efa748 | 850 | //Deselect the card |
neilt6 | 11:67ddc53e3983 | 851 | deselect(); |
neilt6 | 11:67ddc53e3983 | 852 | |
neilt6 | 11:67ddc53e3983 | 853 | //Check the error token |
neilt6 | 11:67ddc53e3983 | 854 | if (token == 0x0A) { |
neilt6 | 11:67ddc53e3983 | 855 | //Determine the number of well written blocks if possible |
neilt6 | 11:67ddc53e3983 | 856 | unsigned int writtenBlocks = 0; |
neilt6 | 13:635147efa748 | 857 | if (m_CardType != CARD_MMC && select()) { |
neilt6 | 11:67ddc53e3983 | 858 | //Send ACMD22(0x00000000) to get the number of well written blocks |
neilt6 | 13:635147efa748 | 859 | if (writeCommand(ACMD22, 0x00000000) == 0x00) { |
neilt6 | 11:67ddc53e3983 | 860 | //Read the data |
neilt6 | 11:67ddc53e3983 | 861 | char acmdData[4]; |
neilt6 | 11:67ddc53e3983 | 862 | if (readData(acmdData, 4)) { |
neilt6 | 11:67ddc53e3983 | 863 | //Extract the number of well written blocks |
neilt6 | 11:67ddc53e3983 | 864 | writtenBlocks = acmdData[0] << 24; |
neilt6 | 11:67ddc53e3983 | 865 | writtenBlocks |= acmdData[1] << 16; |
neilt6 | 11:67ddc53e3983 | 866 | writtenBlocks |= acmdData[2] << 8; |
neilt6 | 11:67ddc53e3983 | 867 | writtenBlocks |= acmdData[3]; |
neilt6 | 11:67ddc53e3983 | 868 | } |
neilt6 | 11:67ddc53e3983 | 869 | } |
neilt6 | 13:635147efa748 | 870 | deselect(); |
neilt6 | 11:67ddc53e3983 | 871 | } |
neilt6 | 11:67ddc53e3983 | 872 | |
neilt6 | 11:67ddc53e3983 | 873 | //Roll back the variables based on the number of well written blocks |
neilt6 | 13:635147efa748 | 874 | currentBuffer = buffer + (writtenBlocks << 9); |
neilt6 | 11:67ddc53e3983 | 875 | currentLba = lba + writtenBlocks; |
neilt6 | 11:67ddc53e3983 | 876 | currentCount = count - writtenBlocks; |
neilt6 | 11:67ddc53e3983 | 877 | |
neilt6 | 11:67ddc53e3983 | 878 | //Try again |
neilt6 | 11:67ddc53e3983 | 879 | continue; |
neilt6 | 11:67ddc53e3983 | 880 | } else { |
neilt6 | 11:67ddc53e3983 | 881 | //A write error occured, get out |
neilt6 | 11:67ddc53e3983 | 882 | break; |
neilt6 | 11:67ddc53e3983 | 883 | } |
neilt6 | 11:67ddc53e3983 | 884 | } |
neilt6 | 11:67ddc53e3983 | 885 | } else { |
neilt6 | 11:67ddc53e3983 | 886 | //The command failed, get out |
neilt6 | 11:67ddc53e3983 | 887 | break; |
neilt6 | 11:67ddc53e3983 | 888 | } |
neilt6 | 11:67ddc53e3983 | 889 | } |
neilt6 | 11:67ddc53e3983 | 890 | |
neilt6 | 11:67ddc53e3983 | 891 | //The multiple block write failed |
neilt6 | 11:67ddc53e3983 | 892 | deselect(); |
neilt6 | 11:67ddc53e3983 | 893 | return false; |
neilt6 | 11:67ddc53e3983 | 894 | } |
neilt6 | 24:6bb3c1987511 | 895 | |
neilt6 | 24:6bb3c1987511 | 896 | bool SDFileSystem::enableHighSpeedMode() |
neilt6 | 24:6bb3c1987511 | 897 | { |
neilt6 | 24:6bb3c1987511 | 898 | //Try to issue CMD6 up to 3 times |
neilt6 | 24:6bb3c1987511 | 899 | for (int f = 0; f < 3; f++) { |
neilt6 | 24:6bb3c1987511 | 900 | //Select the card, and wait for ready |
neilt6 | 24:6bb3c1987511 | 901 | if(!select()) |
neilt6 | 24:6bb3c1987511 | 902 | break; |
neilt6 | 24:6bb3c1987511 | 903 | |
neilt6 | 24:6bb3c1987511 | 904 | //Send CMD6(0x80FFFFF1) to change the access mode to high speed |
neilt6 | 24:6bb3c1987511 | 905 | if (writeCommand(CMD6, 0x80FFFFF1) == 0x00) { |
neilt6 | 24:6bb3c1987511 | 906 | //Read the 64B status data block |
neilt6 | 24:6bb3c1987511 | 907 | char status[64]; |
neilt6 | 24:6bb3c1987511 | 908 | bool success = readData(status, 64); |
neilt6 | 24:6bb3c1987511 | 909 | deselect(); |
neilt6 | 24:6bb3c1987511 | 910 | if (success) { |
neilt6 | 24:6bb3c1987511 | 911 | //Return whether or not the operation was successful |
neilt6 | 24:6bb3c1987511 | 912 | return ((status[16] & 0x0F) == 0x1); |
neilt6 | 24:6bb3c1987511 | 913 | } |
neilt6 | 24:6bb3c1987511 | 914 | } else { |
neilt6 | 24:6bb3c1987511 | 915 | //The command failed, get out |
neilt6 | 24:6bb3c1987511 | 916 | break; |
neilt6 | 24:6bb3c1987511 | 917 | } |
neilt6 | 24:6bb3c1987511 | 918 | } |
neilt6 | 24:6bb3c1987511 | 919 | |
neilt6 | 24:6bb3c1987511 | 920 | //The operation failed 3 times |
neilt6 | 24:6bb3c1987511 | 921 | deselect(); |
neilt6 | 24:6bb3c1987511 | 922 | return false; |
neilt6 | 24:6bb3c1987511 | 923 | } |