updates

Dependencies:   FATFileSystem

Fork of SDFileSystem by Neil Thiessen

Committer:
neilt6
Date:
Tue May 24 15:50:26 2016 +0000
Revision:
24:a52e682a1ce4
Parent:
23:6bb3c1987511
Child:
25:923df4ee70c4
Improved card initialization reliability

Who changed what in which revision?

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