SD FileSystem to comunicate with a KL25Z

Dependencies:   FATFileSystem

Dependents:   KL25_Funciona_SD

Fork of SDFileSystem by Neil Thiessen

Committer:
neilt6
Date:
Wed Feb 24 17:46:31 2016 +0000
Revision:
22:3fa5eaf48e81
Parent:
21:d10a519c0910
Child:
23:6bb3c1987511
Fixed timer hang bug in disk_initialize()

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