updates

Dependencies:   FATFileSystem

Fork of SDFileSystem by Neil Thiessen

Committer:
neilt6
Date:
Wed Jul 30 17:45:02 2014 +0000
Revision:
3:7cf3d1835ef5
Parent:
2:eec1db773e7d
Child:
4:49b29888eca7
Changed the order of the constructor arguments to match the official SDFileSystem library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
neilt6 0:2a6d8a096edc 1 /* SD/MMC File System Library
neilt6 0:2a6d8a096edc 2 * Copyright (c) 2014 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 0:2a6d8a096edc 18 #include "diskio.h"
neilt6 0:2a6d8a096edc 19 #include "CRC7.h"
neilt6 0:2a6d8a096edc 20 #include "CRC16.h"
neilt6 0:2a6d8a096edc 21
neilt6 3:7cf3d1835ef5 22 SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name, PinName cd, SwitchType cdtype, int hz) : FATFileSystem(name), m_SPI(mosi, miso, sclk), m_CS(cs, 1), m_CD(cd), m_CD_ASSERT((int)cdtype)
neilt6 0:2a6d8a096edc 23 {
neilt6 0:2a6d8a096edc 24 //Initialize the member variables
neilt6 0:2a6d8a096edc 25 m_SpiFreq = hz;
neilt6 0:2a6d8a096edc 26 m_Status = STA_NOINIT;
neilt6 0:2a6d8a096edc 27 m_CardType = CARD_NONE;
neilt6 0:2a6d8a096edc 28
neilt6 0:2a6d8a096edc 29 //Configure the SPI bus
neilt6 0:2a6d8a096edc 30 m_SPI.format(8, 0);
neilt6 0:2a6d8a096edc 31
neilt6 0:2a6d8a096edc 32 //Configure the card detect pin
neilt6 0:2a6d8a096edc 33 m_CD.mode(PullUp);
neilt6 1:25f4ba436b81 34 if (cdtype == SWITCH_NO)
neilt6 1:25f4ba436b81 35 m_CD.rise(this, &SDFileSystem::checkSocket);
neilt6 1:25f4ba436b81 36 else
neilt6 1:25f4ba436b81 37 m_CD.fall(this, &SDFileSystem::checkSocket);
neilt6 0:2a6d8a096edc 38 }
neilt6 0:2a6d8a096edc 39
neilt6 0:2a6d8a096edc 40 SDFileSystem::CardType SDFileSystem::card_type()
neilt6 0:2a6d8a096edc 41 {
neilt6 0:2a6d8a096edc 42 //Check the card socket
neilt6 0:2a6d8a096edc 43 checkSocket();
neilt6 0:2a6d8a096edc 44
neilt6 0:2a6d8a096edc 45 //If a card is present but not initialized, initialize it
neilt6 0:2a6d8a096edc 46 if (!(m_Status & STA_NODISK) && (m_Status & STA_NOINIT))
neilt6 0:2a6d8a096edc 47 disk_initialize();
neilt6 0:2a6d8a096edc 48
neilt6 0:2a6d8a096edc 49 //Return the card type
neilt6 0:2a6d8a096edc 50 return m_CardType;
neilt6 0:2a6d8a096edc 51 }
neilt6 0:2a6d8a096edc 52
neilt6 0:2a6d8a096edc 53 int SDFileSystem::disk_initialize()
neilt6 0:2a6d8a096edc 54 {
neilt6 0:2a6d8a096edc 55 char resp;
neilt6 0:2a6d8a096edc 56
neilt6 0:2a6d8a096edc 57 //Make sure there's a card in the socket before proceeding
neilt6 0:2a6d8a096edc 58 checkSocket();
neilt6 0:2a6d8a096edc 59 if (m_Status & STA_NODISK)
neilt6 0:2a6d8a096edc 60 return m_Status;
neilt6 0:2a6d8a096edc 61
neilt6 0:2a6d8a096edc 62 //Make sure we're not already initialized before proceeding
neilt6 0:2a6d8a096edc 63 if (!(m_Status & STA_NOINIT))
neilt6 0:2a6d8a096edc 64 return m_Status;
neilt6 0:2a6d8a096edc 65
neilt6 0:2a6d8a096edc 66 //Set the SPI frequency to 100kHz for initialization
neilt6 0:2a6d8a096edc 67 m_SPI.frequency(100000);
neilt6 0:2a6d8a096edc 68
neilt6 0:2a6d8a096edc 69 //Send 80 dummy clocks with /CS and DI held high
neilt6 0:2a6d8a096edc 70 m_CS = 1;
neilt6 0:2a6d8a096edc 71 for (int i = 0; i < 10; i++)
neilt6 0:2a6d8a096edc 72 m_SPI.write(0xFF);
neilt6 0:2a6d8a096edc 73
neilt6 0:2a6d8a096edc 74 //Write CMD0(0), and check for a valid response
neilt6 0:2a6d8a096edc 75 resp = writeCommand(CMD0, 0);
neilt6 0:2a6d8a096edc 76 if (resp != 0x01) {
neilt6 0:2a6d8a096edc 77 //Initialization failed
neilt6 0:2a6d8a096edc 78 m_CardType = CARD_UNKNOWN;
neilt6 0:2a6d8a096edc 79 return m_Status;
neilt6 0:2a6d8a096edc 80 }
neilt6 0:2a6d8a096edc 81
neilt6 0:2a6d8a096edc 82 //Write CMD8(0x000001AA) to see if this is an SDCv2 card
neilt6 0:2a6d8a096edc 83 resp = writeCommand(CMD8, 0x000001AA);
neilt6 0:2a6d8a096edc 84 if (resp == 0x01) {
neilt6 0:2a6d8a096edc 85 //This is an SDCv2 card, get the 32-bit return value and verify the voltage range/check pattern
neilt6 0:2a6d8a096edc 86 if ((readReturn() & 0xFFF) != 0x1AA) {
neilt6 0:2a6d8a096edc 87 //Initialization failed
neilt6 0:2a6d8a096edc 88 m_CardType = CARD_UNKNOWN;
neilt6 0:2a6d8a096edc 89 return m_Status;
neilt6 0:2a6d8a096edc 90 }
neilt6 0:2a6d8a096edc 91
neilt6 0:2a6d8a096edc 92 //Send CMD58(0) to read the OCR, and verify that the card supports 3.2-3.3V
neilt6 0:2a6d8a096edc 93 resp = writeCommand(CMD58, 0);
neilt6 0:2a6d8a096edc 94 if (resp != 0x01 || !(readReturn() & (1 << 20))) {
neilt6 0:2a6d8a096edc 95 //Initialization failed
neilt6 0:2a6d8a096edc 96 m_CardType = CARD_UNKNOWN;
neilt6 0:2a6d8a096edc 97 return m_Status;
neilt6 0:2a6d8a096edc 98 }
neilt6 0:2a6d8a096edc 99
neilt6 0:2a6d8a096edc 100 //Send ACMD41(0x40100000) repeatedly for up to 1 second to initialize the card
neilt6 0:2a6d8a096edc 101 for (int i = 0; i < 1000; i++) {
neilt6 0:2a6d8a096edc 102 resp = writeCommand(ACMD41, 0x40100000);
neilt6 0:2a6d8a096edc 103 if (resp != 0x01)
neilt6 0:2a6d8a096edc 104 break;
neilt6 0:2a6d8a096edc 105 wait_ms(1);
neilt6 0:2a6d8a096edc 106 }
neilt6 0:2a6d8a096edc 107
neilt6 0:2a6d8a096edc 108 //Check if the card initialized
neilt6 0:2a6d8a096edc 109 if (resp != 0x00) {
neilt6 0:2a6d8a096edc 110 //Initialization failed
neilt6 0:2a6d8a096edc 111 m_CardType = CARD_UNKNOWN;
neilt6 0:2a6d8a096edc 112 return m_Status;
neilt6 0:2a6d8a096edc 113 }
neilt6 0:2a6d8a096edc 114
neilt6 0:2a6d8a096edc 115 //Send CMD58(0) to read the OCR
neilt6 0:2a6d8a096edc 116 resp = writeCommand(CMD58, 0);
neilt6 0:2a6d8a096edc 117 if (resp == 0x00) {
neilt6 0:2a6d8a096edc 118 //Check the CCS bit to determine if this is a high capacity card
neilt6 0:2a6d8a096edc 119 if (readReturn() & 0x40000000)
neilt6 0:2a6d8a096edc 120 m_CardType = CARD_SDHC;
neilt6 0:2a6d8a096edc 121 else
neilt6 0:2a6d8a096edc 122 m_CardType = CARD_SD;
neilt6 0:2a6d8a096edc 123 } else {
neilt6 0:2a6d8a096edc 124 //Initialization failed
neilt6 0:2a6d8a096edc 125 m_CardType = CARD_UNKNOWN;
neilt6 0:2a6d8a096edc 126 return m_Status;
neilt6 0:2a6d8a096edc 127 }
neilt6 0:2a6d8a096edc 128 } else {
neilt6 0:2a6d8a096edc 129 //Didn't respond or illegal command, this is either an SDCv1 or MMC card
neilt6 0:2a6d8a096edc 130 //Send CMD58(0) to read the OCR, and verify that the card supports 3.2-3.3V
neilt6 0:2a6d8a096edc 131 resp = writeCommand(CMD58, 0);
neilt6 0:2a6d8a096edc 132 if (resp != 0x01 || !(readReturn() & (1 << 20))) {
neilt6 0:2a6d8a096edc 133 //Initialization failed
neilt6 0:2a6d8a096edc 134 m_CardType = CARD_UNKNOWN;
neilt6 0:2a6d8a096edc 135 return m_Status;
neilt6 0:2a6d8a096edc 136 }
neilt6 0:2a6d8a096edc 137
neilt6 0:2a6d8a096edc 138 //Try to initialize the card using ACMD41(0x00100000) for 1 second
neilt6 0:2a6d8a096edc 139 for (int i = 0; i < 1000; i++) {
neilt6 0:2a6d8a096edc 140 resp = writeCommand(ACMD41, 0x00100000);
neilt6 0:2a6d8a096edc 141 if (resp != 0x01)
neilt6 0:2a6d8a096edc 142 break;
neilt6 0:2a6d8a096edc 143 wait_ms(1);
neilt6 0:2a6d8a096edc 144 }
neilt6 0:2a6d8a096edc 145
neilt6 0:2a6d8a096edc 146 //Check if the card initialized
neilt6 0:2a6d8a096edc 147 if (resp == 0x00) {
neilt6 0:2a6d8a096edc 148 //This is an SDCv1 standard capacity card
neilt6 0:2a6d8a096edc 149 m_CardType = CARD_SD;
neilt6 0:2a6d8a096edc 150 } else {
neilt6 0:2a6d8a096edc 151 //Try to initialize the card using CMD1(0x00100000) for 1 second
neilt6 0:2a6d8a096edc 152 for (int i = 0; i < 1000; i++) {
neilt6 0:2a6d8a096edc 153 resp = writeCommand(CMD1, 0x00100000);
neilt6 0:2a6d8a096edc 154 if (resp != 0x01)
neilt6 0:2a6d8a096edc 155 break;
neilt6 0:2a6d8a096edc 156 wait_ms(1);
neilt6 0:2a6d8a096edc 157 }
neilt6 0:2a6d8a096edc 158
neilt6 0:2a6d8a096edc 159 //Check if the card initialized
neilt6 0:2a6d8a096edc 160 if (resp == 0x00) {
neilt6 0:2a6d8a096edc 161 //This is an MMCv3 card
neilt6 0:2a6d8a096edc 162 m_CardType = CARD_MMC;
neilt6 0:2a6d8a096edc 163 } else {
neilt6 0:2a6d8a096edc 164 //Initialization failed
neilt6 0:2a6d8a096edc 165 m_CardType = CARD_UNKNOWN;
neilt6 0:2a6d8a096edc 166 return m_Status;
neilt6 0:2a6d8a096edc 167 }
neilt6 0:2a6d8a096edc 168 }
neilt6 0:2a6d8a096edc 169 }
neilt6 0:2a6d8a096edc 170
neilt6 0:2a6d8a096edc 171 //Send CMD59(0x00000001) to re-enable CRC
neilt6 0:2a6d8a096edc 172 resp = writeCommand(CMD59, 0x00000001);
neilt6 0:2a6d8a096edc 173 if (resp != 0x00) {
neilt6 0:2a6d8a096edc 174 //Initialization failed
neilt6 0:2a6d8a096edc 175 m_CardType = CARD_UNKNOWN;
neilt6 0:2a6d8a096edc 176 return m_Status;
neilt6 0:2a6d8a096edc 177 }
neilt6 0:2a6d8a096edc 178
neilt6 0:2a6d8a096edc 179 //Send CMD16(0x00000200) to force the block size to 512B if necessary
neilt6 0:2a6d8a096edc 180 if (m_CardType != CARD_SDHC) {
neilt6 0:2a6d8a096edc 181 resp = writeCommand(CMD16, 0x00000200);
neilt6 0:2a6d8a096edc 182 if (resp != 0x00) {
neilt6 0:2a6d8a096edc 183 //Initialization failed
neilt6 0:2a6d8a096edc 184 m_CardType = CARD_UNKNOWN;
neilt6 0:2a6d8a096edc 185 return m_Status;
neilt6 0:2a6d8a096edc 186 }
neilt6 0:2a6d8a096edc 187 }
neilt6 0:2a6d8a096edc 188
neilt6 0:2a6d8a096edc 189 //The card is now initialized
neilt6 0:2a6d8a096edc 190 m_Status &= ~STA_NOINIT;
neilt6 0:2a6d8a096edc 191
neilt6 0:2a6d8a096edc 192 //Increase the SPI frequency to full speed (limited to 20MHz for MMC, or 25MHz for SDC)
neilt6 0:2a6d8a096edc 193 if (m_CardType == CARD_MMC && m_SpiFreq > 20000000)
neilt6 0:2a6d8a096edc 194 m_SPI.frequency(20000000);
neilt6 0:2a6d8a096edc 195 else if (m_SpiFreq > 25000000)
neilt6 0:2a6d8a096edc 196 m_SPI.frequency(25000000);
neilt6 0:2a6d8a096edc 197 else
neilt6 0:2a6d8a096edc 198 m_SPI.frequency(m_SpiFreq);
neilt6 0:2a6d8a096edc 199
neilt6 0:2a6d8a096edc 200 //Return the device status
neilt6 0:2a6d8a096edc 201 return m_Status;
neilt6 0:2a6d8a096edc 202 }
neilt6 0:2a6d8a096edc 203
neilt6 0:2a6d8a096edc 204 int SDFileSystem::disk_status()
neilt6 0:2a6d8a096edc 205 {
neilt6 0:2a6d8a096edc 206 //Check if there's a card in the socket
neilt6 0:2a6d8a096edc 207 checkSocket();
neilt6 0:2a6d8a096edc 208
neilt6 0:2a6d8a096edc 209 //Return the device status
neilt6 0:2a6d8a096edc 210 return m_Status;
neilt6 0:2a6d8a096edc 211 }
neilt6 0:2a6d8a096edc 212
neilt6 0:2a6d8a096edc 213 int SDFileSystem::disk_read(uint8_t* buffer, uint64_t sector)
neilt6 0:2a6d8a096edc 214 {
neilt6 0:2a6d8a096edc 215 //Make sure the device is initialized before proceeding
neilt6 0:2a6d8a096edc 216 if (m_Status & STA_NOINIT)
neilt6 0:2a6d8a096edc 217 return RES_NOTRDY;
neilt6 0:2a6d8a096edc 218
neilt6 0:2a6d8a096edc 219 //Convert from LBA to a byte address for standard capacity cards
neilt6 0:2a6d8a096edc 220 if (m_CardType != CARD_SDHC)
neilt6 0:2a6d8a096edc 221 sector *= 512;
neilt6 0:2a6d8a096edc 222
neilt6 0:2a6d8a096edc 223 //Try to read the block up to 3 times
neilt6 0:2a6d8a096edc 224 for (int i = 0; i < 3; i++) {
neilt6 0:2a6d8a096edc 225 //Send CMD17(sector) to read a single block
neilt6 0:2a6d8a096edc 226 char resp = writeCommand(CMD17, sector);
neilt6 0:2a6d8a096edc 227 if (resp == 0x00) {
neilt6 0:2a6d8a096edc 228 //Try to read the sector, and return if successful
neilt6 0:2a6d8a096edc 229 if (readData((char*)buffer, 512))
neilt6 0:2a6d8a096edc 230 return RES_OK;
neilt6 0:2a6d8a096edc 231 } else {
neilt6 0:2a6d8a096edc 232 //The command failed
neilt6 0:2a6d8a096edc 233 return RES_ERROR;
neilt6 0:2a6d8a096edc 234 }
neilt6 0:2a6d8a096edc 235 }
neilt6 0:2a6d8a096edc 236
neilt6 0:2a6d8a096edc 237 //The read operation failed 3 times (CRC most likely)
neilt6 0:2a6d8a096edc 238 return RES_ERROR;
neilt6 0:2a6d8a096edc 239 }
neilt6 0:2a6d8a096edc 240
neilt6 0:2a6d8a096edc 241 int SDFileSystem::disk_write(const uint8_t* buffer, uint64_t sector)
neilt6 0:2a6d8a096edc 242 {
neilt6 0:2a6d8a096edc 243 //Make sure the device is initialized before proceeding
neilt6 0:2a6d8a096edc 244 if (m_Status & STA_NOINIT)
neilt6 0:2a6d8a096edc 245 return RES_NOTRDY;
neilt6 0:2a6d8a096edc 246
neilt6 0:2a6d8a096edc 247 //Make sure the device isn't write protected before proceeding
neilt6 0:2a6d8a096edc 248 if (m_Status & STA_PROTECT)
neilt6 0:2a6d8a096edc 249 return RES_WRPRT;
neilt6 0:2a6d8a096edc 250
neilt6 0:2a6d8a096edc 251 //Convert from LBA to a byte address for older cards
neilt6 0:2a6d8a096edc 252 if (m_CardType != CARD_SDHC)
neilt6 0:2a6d8a096edc 253 sector *= 512;
neilt6 0:2a6d8a096edc 254
neilt6 0:2a6d8a096edc 255 //Try to write the block up to 3 times
neilt6 0:2a6d8a096edc 256 for (int i = 0; i < 3; i++) {
neilt6 0:2a6d8a096edc 257 //Send CMD24(sector) to write a single block
neilt6 0:2a6d8a096edc 258 if (writeCommand(CMD24, sector) == 0x00) {
neilt6 0:2a6d8a096edc 259 //Wait for up to 500ms for the card to become ready
neilt6 0:2a6d8a096edc 260 if (!waitReady(500)) {
neilt6 2:eec1db773e7d 261 //We timed out, deselect and loop again
neilt6 0:2a6d8a096edc 262 deselect();
neilt6 0:2a6d8a096edc 263 continue;
neilt6 0:2a6d8a096edc 264 }
neilt6 0:2a6d8a096edc 265
neilt6 0:2a6d8a096edc 266 //Send the write data token
neilt6 0:2a6d8a096edc 267 m_SPI.write(0xFE);
neilt6 0:2a6d8a096edc 268
neilt6 0:2a6d8a096edc 269 //Write the data block from the buffer
neilt6 0:2a6d8a096edc 270 for (int b = 0; b < 512; b++)
neilt6 0:2a6d8a096edc 271 m_SPI.write(buffer[b]);
neilt6 0:2a6d8a096edc 272
neilt6 0:2a6d8a096edc 273 //Calculate the CRC16 checksum for the data block and send it
neilt6 0:2a6d8a096edc 274 unsigned short crc = CRC16((char*)buffer, 512);
neilt6 0:2a6d8a096edc 275 m_SPI.write(crc >> 8);
neilt6 0:2a6d8a096edc 276 m_SPI.write(crc);
neilt6 0:2a6d8a096edc 277
neilt6 0:2a6d8a096edc 278 //Receive the data response, and deselect the card
neilt6 0:2a6d8a096edc 279 char resp = m_SPI.write(0xFF) & 0x1F;
neilt6 0:2a6d8a096edc 280 deselect();
neilt6 0:2a6d8a096edc 281
neilt6 0:2a6d8a096edc 282 //Check the response
neilt6 0:2a6d8a096edc 283 if (resp == 0x05)
neilt6 0:2a6d8a096edc 284 return RES_OK;
neilt6 0:2a6d8a096edc 285 else if (resp == 0x0D)
neilt6 0:2a6d8a096edc 286 return RES_ERROR;
neilt6 0:2a6d8a096edc 287 } else {
neilt6 0:2a6d8a096edc 288 //The command failed
neilt6 0:2a6d8a096edc 289 return RES_ERROR;
neilt6 0:2a6d8a096edc 290 }
neilt6 0:2a6d8a096edc 291 }
neilt6 0:2a6d8a096edc 292
neilt6 0:2a6d8a096edc 293 //The operation either timed out 3 times, failed the CRC check 3 times, or experienced a write error
neilt6 0:2a6d8a096edc 294 return RES_ERROR;
neilt6 0:2a6d8a096edc 295 }
neilt6 0:2a6d8a096edc 296
neilt6 0:2a6d8a096edc 297 int SDFileSystem::disk_sync()
neilt6 0:2a6d8a096edc 298 {
neilt6 0:2a6d8a096edc 299 //Select the card so we're forced to wait for the end of any internal write processes
neilt6 0:2a6d8a096edc 300 bool ret = select();
neilt6 0:2a6d8a096edc 301 deselect();
neilt6 0:2a6d8a096edc 302
neilt6 0:2a6d8a096edc 303 //Return success/failure
neilt6 0:2a6d8a096edc 304 return (ret) ? RES_OK : RES_ERROR;
neilt6 0:2a6d8a096edc 305 }
neilt6 0:2a6d8a096edc 306
neilt6 0:2a6d8a096edc 307 uint64_t SDFileSystem::disk_sectors()
neilt6 0:2a6d8a096edc 308 {
neilt6 0:2a6d8a096edc 309 //Make sure the device is initialized before proceeding
neilt6 0:2a6d8a096edc 310 if (m_Status & STA_NOINIT)
neilt6 0:2a6d8a096edc 311 return 0;
neilt6 0:2a6d8a096edc 312
neilt6 0:2a6d8a096edc 313 //Try to read the CSD register up to 3 times
neilt6 0:2a6d8a096edc 314 for (int i = 0; i < 3; i++) {
neilt6 0:2a6d8a096edc 315 //Send CMD9(0) to read the CSD register
neilt6 0:2a6d8a096edc 316 if (writeCommand(CMD9, 0) == 0x00) {
neilt6 0:2a6d8a096edc 317 //Receive the 16B CSD data
neilt6 0:2a6d8a096edc 318 char csd[16];
neilt6 0:2a6d8a096edc 319 if (readData(csd, 16)) {
neilt6 0:2a6d8a096edc 320 //Calculate the sector count based on the card type
neilt6 0:2a6d8a096edc 321 if ((csd[0] >> 6) == 0x01) {
neilt6 0:2a6d8a096edc 322 //Calculate the sector count a high capacity card
neilt6 0:2a6d8a096edc 323 uint64_t sectors = (((csd[7] & 0x3F) << 16) | (csd[8] << 8) | csd[9]) + 1;
neilt6 0:2a6d8a096edc 324 return sectors << 10;
neilt6 0:2a6d8a096edc 325 } else {
neilt6 0:2a6d8a096edc 326 //Calculate the sector count standard capacity card
neilt6 0:2a6d8a096edc 327 uint64_t sectors = (((csd[6] & 0x03) << 10) | (csd[7] << 2) | ((csd[8] & 0xC0) >> 6)) + 1;
neilt6 0:2a6d8a096edc 328 sectors <<= ((((csd[9] & 0x03) << 1) | ((csd[10] & 0x80) >> 7)) + 2);
neilt6 0:2a6d8a096edc 329 sectors <<= (csd[5] & 0x0F);
neilt6 0:2a6d8a096edc 330 return sectors >> 9;
neilt6 0:2a6d8a096edc 331 }
neilt6 0:2a6d8a096edc 332 }
neilt6 0:2a6d8a096edc 333 } else {
neilt6 0:2a6d8a096edc 334 //The command failed
neilt6 0:2a6d8a096edc 335 return 0;
neilt6 0:2a6d8a096edc 336 }
neilt6 0:2a6d8a096edc 337 }
neilt6 0:2a6d8a096edc 338
neilt6 0:2a6d8a096edc 339 //The read operation failed 3 times (CRC most likely)
neilt6 0:2a6d8a096edc 340 return 0;
neilt6 0:2a6d8a096edc 341 }
neilt6 0:2a6d8a096edc 342
neilt6 0:2a6d8a096edc 343 void SDFileSystem::checkSocket()
neilt6 0:2a6d8a096edc 344 {
neilt6 0:2a6d8a096edc 345 //Check if a card is in the socket
neilt6 1:25f4ba436b81 346 if (m_CD == m_CD_ASSERT) {
neilt6 0:2a6d8a096edc 347 //The socket is occupied, clear the STA_NODISK flag
neilt6 0:2a6d8a096edc 348 m_Status &= ~STA_NODISK;
neilt6 0:2a6d8a096edc 349 } else {
neilt6 0:2a6d8a096edc 350 //The socket is empty
neilt6 0:2a6d8a096edc 351 m_Status |= (STA_NODISK | STA_NOINIT);
neilt6 0:2a6d8a096edc 352 m_CardType = CARD_NONE;
neilt6 0:2a6d8a096edc 353 }
neilt6 0:2a6d8a096edc 354 }
neilt6 0:2a6d8a096edc 355
neilt6 0:2a6d8a096edc 356 inline bool SDFileSystem::waitReady(int timeout)
neilt6 0:2a6d8a096edc 357 {
neilt6 0:2a6d8a096edc 358 //Wait for the specified timeout for the card to become ready
neilt6 0:2a6d8a096edc 359 for (int i = 0; i < timeout; i++) {
neilt6 0:2a6d8a096edc 360 if (m_SPI.write(0xFF) == 0xFF)
neilt6 0:2a6d8a096edc 361 return true;
neilt6 0:2a6d8a096edc 362 wait_ms(1);
neilt6 0:2a6d8a096edc 363 }
neilt6 0:2a6d8a096edc 364
neilt6 0:2a6d8a096edc 365 //We timed out
neilt6 0:2a6d8a096edc 366 return false;
neilt6 0:2a6d8a096edc 367 }
neilt6 0:2a6d8a096edc 368
neilt6 0:2a6d8a096edc 369 inline bool SDFileSystem::select()
neilt6 0:2a6d8a096edc 370 {
neilt6 0:2a6d8a096edc 371 //Pull /CS low
neilt6 0:2a6d8a096edc 372 m_CS = 0;
neilt6 0:2a6d8a096edc 373
neilt6 0:2a6d8a096edc 374 //Send a dummy clock to enable DO
neilt6 0:2a6d8a096edc 375 m_SPI.write(0xFF);
neilt6 0:2a6d8a096edc 376
neilt6 0:2a6d8a096edc 377 //Wait for up to 500ms for the card to become ready
neilt6 0:2a6d8a096edc 378 if (waitReady(500))
neilt6 0:2a6d8a096edc 379 return true;
neilt6 0:2a6d8a096edc 380
neilt6 0:2a6d8a096edc 381 //We timed out, deselect and return false
neilt6 0:2a6d8a096edc 382 deselect();
neilt6 0:2a6d8a096edc 383 return false;
neilt6 0:2a6d8a096edc 384 }
neilt6 0:2a6d8a096edc 385
neilt6 0:2a6d8a096edc 386 inline void SDFileSystem::deselect()
neilt6 0:2a6d8a096edc 387 {
neilt6 0:2a6d8a096edc 388 //Pull /CS high
neilt6 0:2a6d8a096edc 389 m_CS = 1;
neilt6 0:2a6d8a096edc 390
neilt6 0:2a6d8a096edc 391 //Send a dummy byte to release DO
neilt6 0:2a6d8a096edc 392 m_SPI.write(0xFF);
neilt6 0:2a6d8a096edc 393 }
neilt6 0:2a6d8a096edc 394
neilt6 0:2a6d8a096edc 395 char SDFileSystem::writeCommand(char cmd, unsigned int arg)
neilt6 0:2a6d8a096edc 396 {
neilt6 0:2a6d8a096edc 397 char resp;
neilt6 0:2a6d8a096edc 398
neilt6 0:2a6d8a096edc 399 //Try to send the command up to 3 times
neilt6 0:2a6d8a096edc 400 for (int i = 0; i < 3; i++) {
neilt6 2:eec1db773e7d 401 //Send CMD55 prior to an ACMD
neilt6 0:2a6d8a096edc 402 if (cmd == ACMD41) {
neilt6 0:2a6d8a096edc 403 resp = writeCommand(CMD55, 0);
neilt6 0:2a6d8a096edc 404 if (resp > 0x01)
neilt6 0:2a6d8a096edc 405 return resp;
neilt6 0:2a6d8a096edc 406 }
neilt6 0:2a6d8a096edc 407
neilt6 2:eec1db773e7d 408 //Select the card, and wait for ready
neilt6 0:2a6d8a096edc 409 if (!select())
neilt6 0:2a6d8a096edc 410 return 0xFF;
neilt6 0:2a6d8a096edc 411
neilt6 0:2a6d8a096edc 412 //Prepare the command packet
neilt6 0:2a6d8a096edc 413 char cmdPacket[6];
neilt6 0:2a6d8a096edc 414 cmdPacket[0] = 0x40 | cmd;
neilt6 0:2a6d8a096edc 415 cmdPacket[1] = arg >> 24;
neilt6 0:2a6d8a096edc 416 cmdPacket[2] = arg >> 16;
neilt6 0:2a6d8a096edc 417 cmdPacket[3] = arg >> 8;
neilt6 0:2a6d8a096edc 418 cmdPacket[4] = arg;
neilt6 0:2a6d8a096edc 419 cmdPacket[5] = (CRC7(cmdPacket, 5) << 1) | 0x01;
neilt6 0:2a6d8a096edc 420
neilt6 0:2a6d8a096edc 421 //Send the command packet
neilt6 0:2a6d8a096edc 422 for (int b = 0; b < 6; b++)
neilt6 0:2a6d8a096edc 423 m_SPI.write(cmdPacket[b]);
neilt6 0:2a6d8a096edc 424
neilt6 0:2a6d8a096edc 425 //Allow up to 10 bytes of delay for the command response
neilt6 0:2a6d8a096edc 426 for (int b = 0; b < 10; b++) {
neilt6 0:2a6d8a096edc 427 resp = m_SPI.write(0xFF);
neilt6 0:2a6d8a096edc 428 if (!(resp & 0x80))
neilt6 0:2a6d8a096edc 429 break;
neilt6 0:2a6d8a096edc 430 }
neilt6 0:2a6d8a096edc 431
neilt6 2:eec1db773e7d 432 //Deselect the card on errors, or if the transaction is finished
neilt6 2:eec1db773e7d 433 if (resp > 0x01 || !(cmd == CMD8 || cmd == CMD9 || cmd == CMD17 || cmd == CMD24 || cmd == CMD55 || cmd == CMD58))
neilt6 0:2a6d8a096edc 434 deselect();
neilt6 0:2a6d8a096edc 435
neilt6 2:eec1db773e7d 436 //Return the response unless there were CRC errors
neilt6 0:2a6d8a096edc 437 if (resp == 0xFF || !(resp & (1 << 3)))
neilt6 0:2a6d8a096edc 438 return resp;
neilt6 0:2a6d8a096edc 439 }
neilt6 0:2a6d8a096edc 440
neilt6 0:2a6d8a096edc 441 //The command failed 3 times due to CRC errors
neilt6 0:2a6d8a096edc 442 return 0xFF;
neilt6 0:2a6d8a096edc 443 }
neilt6 0:2a6d8a096edc 444
neilt6 0:2a6d8a096edc 445 unsigned int SDFileSystem::readReturn()
neilt6 0:2a6d8a096edc 446 {
neilt6 0:2a6d8a096edc 447 unsigned int ret;
neilt6 0:2a6d8a096edc 448
neilt6 0:2a6d8a096edc 449 //Read the 32-bit response value
neilt6 0:2a6d8a096edc 450 ret = (m_SPI.write(0xFF) << 24);
neilt6 0:2a6d8a096edc 451 ret |= (m_SPI.write(0xFF) << 16);
neilt6 0:2a6d8a096edc 452 ret |= (m_SPI.write(0xFF) << 8);
neilt6 0:2a6d8a096edc 453 ret |= m_SPI.write(0xFF);
neilt6 0:2a6d8a096edc 454
neilt6 0:2a6d8a096edc 455 //Deselect the card
neilt6 0:2a6d8a096edc 456 deselect();
neilt6 0:2a6d8a096edc 457
neilt6 0:2a6d8a096edc 458 //Return the response value
neilt6 0:2a6d8a096edc 459 return ret;
neilt6 0:2a6d8a096edc 460 }
neilt6 0:2a6d8a096edc 461
neilt6 0:2a6d8a096edc 462 bool SDFileSystem::readData(char* buffer, int length)
neilt6 0:2a6d8a096edc 463 {
neilt6 0:2a6d8a096edc 464 char token;
neilt6 0:2a6d8a096edc 465
neilt6 0:2a6d8a096edc 466 //Wait for up to 200ms for the DataStart token to arrive
neilt6 0:2a6d8a096edc 467 for (int i = 0; i < 200; i++) {
neilt6 0:2a6d8a096edc 468 token = m_SPI.write(0xFF);
neilt6 0:2a6d8a096edc 469 if (token != 0xFF)
neilt6 0:2a6d8a096edc 470 break;
neilt6 0:2a6d8a096edc 471 wait_ms(1);
neilt6 0:2a6d8a096edc 472 }
neilt6 0:2a6d8a096edc 473
neilt6 0:2a6d8a096edc 474 //Make sure the token is valid
neilt6 0:2a6d8a096edc 475 if (token != 0xFE)
neilt6 0:2a6d8a096edc 476 return false;
neilt6 0:2a6d8a096edc 477
neilt6 0:2a6d8a096edc 478 //Read the data into the buffer
neilt6 0:2a6d8a096edc 479 for (int i = 0; i < length; i++)
neilt6 0:2a6d8a096edc 480 buffer[i] = m_SPI.write(0xFF);
neilt6 0:2a6d8a096edc 481
neilt6 0:2a6d8a096edc 482 //Read the CRC16 checksum for the data block, and deselect the card
neilt6 0:2a6d8a096edc 483 unsigned short crc = (m_SPI.write(0xFF) << 8);
neilt6 0:2a6d8a096edc 484 crc |= m_SPI.write(0xFF);
neilt6 0:2a6d8a096edc 485 deselect();
neilt6 0:2a6d8a096edc 486
neilt6 0:2a6d8a096edc 487 //Indicate whether the CRC16 checksum was valid or not
neilt6 0:2a6d8a096edc 488 if (crc == CRC16(buffer, length))
neilt6 0:2a6d8a096edc 489 return true;
neilt6 0:2a6d8a096edc 490 else
neilt6 0:2a6d8a096edc 491 return false;
neilt6 0:2a6d8a096edc 492 }