| 1 | /* Arduino Sd2Card Library |
| 2 | * Copyright (C) 2009 by William Greiman |
| 3 | * |
| 4 | * This file is part of the Arduino Sd2Card Library |
| 5 | * |
| 6 | * This Library is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This Library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with the Arduino Sd2Card Library. If not, see |
| 18 | * <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | #define USE_SPI_LIB |
| 21 | #include <Arduino.h> |
| 22 | #include "Sd2Card.h" |
| 23 | //------------------------------------------------------------------------------ |
| 24 | #ifndef SOFTWARE_SPI |
| 25 | #ifdef USE_SPI_LIB |
| 26 | |
| 27 | #ifndef SDCARD_SPI |
| 28 | #define SDCARD_SPI SPI |
| 29 | #endif |
| 30 | |
| 31 | #include <SPI.h> |
| 32 | static SPISettings settings; |
| 33 | #endif |
| 34 | // functions for hardware SPI |
| 35 | /** Send a byte to the card */ |
| 36 | static void spiSend(uint8_t b) { |
| 37 | #ifndef USE_SPI_LIB |
| 38 | SPDR = b; |
| 39 | while (!(SPSR & (1 << SPIF))) |
| 40 | ; |
| 41 | #else |
| 42 | SDCARD_SPI.transfer(b); |
| 43 | #endif |
| 44 | } |
| 45 | /** Receive a byte from the card */ |
| 46 | static uint8_t spiRec(void) { |
| 47 | #ifndef USE_SPI_LIB |
| 48 | spiSend(0XFF); |
| 49 | return SPDR; |
| 50 | #else |
| 51 | return SDCARD_SPI.transfer(0xFF); |
| 52 | #endif |
| 53 | } |
| 54 | #else // SOFTWARE_SPI |
| 55 | //------------------------------------------------------------------------------ |
| 56 | /** nop to tune soft SPI timing */ |
| 57 | #define nop asm volatile ("nop\n\t") |
| 58 | //------------------------------------------------------------------------------ |
| 59 | /** Soft SPI receive */ |
| 60 | uint8_t spiRec(void) { |
| 61 | uint8_t data = 0; |
| 62 | // no interrupts during byte receive - about 8 us |
| 63 | cli(); |
| 64 | // output pin high - like sending 0XFF |
| 65 | fastDigitalWrite(SPI_MOSI_PIN, HIGH); |
| 66 | |
| 67 | for (uint8_t i = 0; i < 8; i++) { |
| 68 | fastDigitalWrite(SPI_SCK_PIN, HIGH); |
| 69 | |
| 70 | // adjust so SCK is nice |
| 71 | nop; |
| 72 | nop; |
| 73 | |
| 74 | data <<= 1; |
| 75 | |
| 76 | if (fastDigitalRead(SPI_MISO_PIN)) data |= 1; |
| 77 | |
| 78 | fastDigitalWrite(SPI_SCK_PIN, LOW); |
| 79 | } |
| 80 | // enable interrupts |
| 81 | sei(); |
| 82 | return data; |
| 83 | } |
| 84 | //------------------------------------------------------------------------------ |
| 85 | /** Soft SPI send */ |
| 86 | void spiSend(uint8_t data) { |
| 87 | // no interrupts during byte send - about 8 us |
| 88 | cli(); |
| 89 | for (uint8_t i = 0; i < 8; i++) { |
| 90 | fastDigitalWrite(SPI_SCK_PIN, LOW); |
| 91 | |
| 92 | fastDigitalWrite(SPI_MOSI_PIN, data & 0X80); |
| 93 | |
| 94 | data <<= 1; |
| 95 | |
| 96 | fastDigitalWrite(SPI_SCK_PIN, HIGH); |
| 97 | } |
| 98 | // hold SCK high for a few ns |
| 99 | nop; |
| 100 | nop; |
| 101 | nop; |
| 102 | nop; |
| 103 | |
| 104 | fastDigitalWrite(SPI_SCK_PIN, LOW); |
| 105 | // enable interrupts |
| 106 | sei(); |
| 107 | } |
| 108 | #endif // SOFTWARE_SPI |
| 109 | //------------------------------------------------------------------------------ |
| 110 | // send command and return error code. Return zero for OK |
| 111 | uint8_t Sd2Card::cardCommand(uint8_t cmd, uint32_t arg) { |
| 112 | // end read if in partialBlockRead mode |
| 113 | readEnd(); |
| 114 | |
| 115 | // select card |
| 116 | chipSelectLow(); |
| 117 | |
| 118 | // wait up to 300 ms if busy |
| 119 | waitNotBusy(300); |
| 120 | |
| 121 | // send command |
| 122 | spiSend(cmd | 0x40); |
| 123 | |
| 124 | // send argument |
| 125 | for (int8_t s = 24; s >= 0; s -= 8) spiSend(arg >> s); |
| 126 | |
| 127 | // send CRC |
| 128 | uint8_t crc = 0XFF; |
| 129 | if (cmd == CMD0) crc = 0X95; // correct crc for CMD0 with arg 0 |
| 130 | if (cmd == CMD8) crc = 0X87; // correct crc for CMD8 with arg 0X1AA |
| 131 | spiSend(crc); |
| 132 | |
| 133 | // wait for response |
| 134 | for (uint8_t i = 0; ((status_ = spiRec()) & 0X80) && i != 0XFF; i++) |
| 135 | ; |
| 136 | return status_; |
| 137 | } |
| 138 | //------------------------------------------------------------------------------ |
| 139 | /** |
| 140 | * Determine the size of an SD flash memory card. |
| 141 | * |
| 142 | * \return The number of 512 byte data blocks in the card |
| 143 | * or zero if an error occurs. |
| 144 | */ |
| 145 | uint32_t Sd2Card::cardSize(void) { |
| 146 | csd_t csd; |
| 147 | if (!readCSD(&csd)) return 0; |
| 148 | if (csd.v1.csd_ver == 0) { |
| 149 | uint8_t read_bl_len = csd.v1.read_bl_len; |
| 150 | uint16_t c_size = (csd.v1.c_size_high << 10) |
| 151 | | (csd.v1.c_size_mid << 2) | csd.v1.c_size_low; |
| 152 | uint8_t c_size_mult = (csd.v1.c_size_mult_high << 1) |
| 153 | | csd.v1.c_size_mult_low; |
| 154 | return (uint32_t)(c_size + 1) << (c_size_mult + read_bl_len - 7); |
| 155 | } else if (csd.v2.csd_ver == 1) { |
| 156 | uint32_t c_size = ((uint32_t)csd.v2.c_size_high << 16) |
| 157 | | (csd.v2.c_size_mid << 8) | csd.v2.c_size_low; |
| 158 | return (c_size + 1) << 10; |
| 159 | } else { |
| 160 | error(SD_CARD_ERROR_BAD_CSD); |
| 161 | return 0; |
| 162 | } |
| 163 | } |
| 164 | //------------------------------------------------------------------------------ |
| 165 | static uint8_t chip_select_asserted = 0; |
| 166 | |
| 167 | void Sd2Card::chipSelectHigh(void) { |
| 168 | digitalWrite(chipSelectPin_, HIGH); |
| 169 | #ifdef USE_SPI_LIB |
| 170 | if (chip_select_asserted) { |
| 171 | chip_select_asserted = 0; |
| 172 | SDCARD_SPI.endTransaction(); |
| 173 | } |
| 174 | #endif |
| 175 | } |
| 176 | //------------------------------------------------------------------------------ |
| 177 | void Sd2Card::chipSelectLow(void) { |
| 178 | #ifdef USE_SPI_LIB |
| 179 | if (!chip_select_asserted) { |
| 180 | chip_select_asserted = 1; |
| 181 | SDCARD_SPI.beginTransaction(settings); |
| 182 | } |
| 183 | #endif |
| 184 | digitalWrite(chipSelectPin_, LOW); |
| 185 | } |
| 186 | //------------------------------------------------------------------------------ |
| 187 | /** Erase a range of blocks. |
| 188 | * |
| 189 | * \param[in] firstBlock The address of the first block in the range. |
| 190 | * \param[in] lastBlock The address of the last block in the range. |
| 191 | * |
| 192 | * \note This function requests the SD card to do a flash erase for a |
| 193 | * range of blocks. The data on the card after an erase operation is |
| 194 | * either 0 or 1, depends on the card vendor. The card must support |
| 195 | * single block erase. |
| 196 | * |
| 197 | * \return The value one, true, is returned for success and |
| 198 | * the value zero, false, is returned for failure. |
| 199 | */ |
| 200 | uint8_t Sd2Card::erase(uint32_t firstBlock, uint32_t lastBlock) { |
| 201 | if (!eraseSingleBlockEnable()) { |
| 202 | error(SD_CARD_ERROR_ERASE_SINGLE_BLOCK); |
| 203 | goto fail; |
| 204 | } |
| 205 | if (type_ != SD_CARD_TYPE_SDHC) { |
| 206 | firstBlock <<= 9; |
| 207 | lastBlock <<= 9; |
| 208 | } |
| 209 | if (cardCommand(CMD32, firstBlock) |
| 210 | || cardCommand(CMD33, lastBlock) |
| 211 | || cardCommand(CMD38, 0)) { |
| 212 | error(SD_CARD_ERROR_ERASE); |
| 213 | goto fail; |
| 214 | } |
| 215 | if (!waitNotBusy(SD_ERASE_TIMEOUT)) { |
| 216 | error(SD_CARD_ERROR_ERASE_TIMEOUT); |
| 217 | goto fail; |
| 218 | } |
| 219 | chipSelectHigh(); |
| 220 | return true; |
| 221 | |
| 222 | fail: |
| 223 | chipSelectHigh(); |
| 224 | return false; |
| 225 | } |
| 226 | //------------------------------------------------------------------------------ |
| 227 | /** Determine if card supports single block erase. |
| 228 | * |
| 229 | * \return The value one, true, is returned if single block erase is supported. |
| 230 | * The value zero, false, is returned if single block erase is not supported. |
| 231 | */ |
| 232 | uint8_t Sd2Card::eraseSingleBlockEnable(void) { |
| 233 | csd_t csd; |
| 234 | return readCSD(&csd) ? csd.v1.erase_blk_en : 0; |
| 235 | } |
| 236 | //------------------------------------------------------------------------------ |
| 237 | /** |
| 238 | * Initialize an SD flash memory card. |
| 239 | * |
| 240 | * \param[in] sckRateID SPI clock rate selector. See setSckRate(). |
| 241 | * \param[in] chipSelectPin SD chip select pin number. |
| 242 | * |
| 243 | * \return The value one, true, is returned for success and |
| 244 | * the value zero, false, is returned for failure. The reason for failure |
| 245 | * can be determined by calling errorCode() and errorData(). |
| 246 | */ |
| 247 | uint8_t Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) { |
| 248 | errorCode_ = inBlock_ = partialBlockRead_ = type_ = 0; |
| 249 | chipSelectPin_ = chipSelectPin; |
| 250 | // 16-bit init start time allows over a minute |
| 251 | unsigned int t0 = millis(); |
| 252 | uint32_t arg; |
| 253 | |
| 254 | // set pin modes |
| 255 | pinMode(chipSelectPin_, OUTPUT); |
| 256 | digitalWrite(chipSelectPin_, HIGH); |
| 257 | #ifndef USE_SPI_LIB |
| 258 | pinMode(SPI_MISO_PIN, INPUT); |
| 259 | pinMode(SPI_MOSI_PIN, OUTPUT); |
| 260 | pinMode(SPI_SCK_PIN, OUTPUT); |
| 261 | #endif |
| 262 | |
| 263 | #ifndef SOFTWARE_SPI |
| 264 | #ifndef USE_SPI_LIB |
| 265 | // SS must be in output mode even it is not chip select |
| 266 | pinMode(SS_PIN, OUTPUT); |
| 267 | digitalWrite(SS_PIN, HIGH); // disable any SPI device using hardware SS pin |
| 268 | // Enable SPI, Master, clock rate f_osc/128 |
| 269 | SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR1) | (1 << SPR0); |
| 270 | // clear double speed |
| 271 | SPSR &= ~(1 << SPI2X); |
| 272 | #else // USE_SPI_LIB |
| 273 | SDCARD_SPI.begin(); |
| 274 | settings = SPISettings(250000, MSBFIRST, SPI_MODE0); |
| 275 | #endif // USE_SPI_LIB |
| 276 | #endif // SOFTWARE_SPI |
| 277 | |
| 278 | // must supply min of 74 clock cycles with CS high. |
| 279 | #ifdef USE_SPI_LIB |
| 280 | SDCARD_SPI.beginTransaction(settings); |
| 281 | #endif |
| 282 | for (uint8_t i = 0; i < 10; i++) spiSend(0XFF); |
| 283 | #ifdef USE_SPI_LIB |
| 284 | SDCARD_SPI.endTransaction(); |
| 285 | #endif |
| 286 | |
| 287 | chipSelectLow(); |
| 288 | |
| 289 | // command to go idle in SPI mode |
| 290 | while ((status_ = cardCommand(CMD0, 0)) != R1_IDLE_STATE) { |
| 291 | unsigned int d = millis() - t0; |
| 292 | if (d > SD_INIT_TIMEOUT) { |
| 293 | error(SD_CARD_ERROR_CMD0); |
| 294 | goto fail; |
| 295 | } |
| 296 | } |
| 297 | // check SD version |
| 298 | if ((cardCommand(CMD8, 0x1AA) & R1_ILLEGAL_COMMAND)) { |
| 299 | type(SD_CARD_TYPE_SD1); |
| 300 | } else { |
| 301 | // only need last byte of r7 response |
| 302 | for (uint8_t i = 0; i < 4; i++) status_ = spiRec(); |
| 303 | if (status_ != 0XAA) { |
| 304 | error(SD_CARD_ERROR_CMD8); |
| 305 | goto fail; |
| 306 | } |
| 307 | type(SD_CARD_TYPE_SD2); |
| 308 | } |
| 309 | // initialize card and send host supports SDHC if SD2 |
| 310 | arg = type() == SD_CARD_TYPE_SD2 ? 0X40000000 : 0; |
| 311 | |
| 312 | while ((status_ = cardAcmd(ACMD41, arg)) != R1_READY_STATE) { |
| 313 | // check for timeout |
| 314 | unsigned int d = millis() - t0; |
| 315 | if (d > SD_INIT_TIMEOUT) { |
| 316 | error(SD_CARD_ERROR_ACMD41); |
| 317 | goto fail; |
| 318 | } |
| 319 | } |
| 320 | // if SD2 read OCR register to check for SDHC card |
| 321 | if (type() == SD_CARD_TYPE_SD2) { |
| 322 | if (cardCommand(CMD58, 0)) { |
| 323 | error(SD_CARD_ERROR_CMD58); |
| 324 | goto fail; |
| 325 | } |
| 326 | if ((spiRec() & 0XC0) == 0XC0) type(SD_CARD_TYPE_SDHC); |
| 327 | // discard rest of ocr - contains allowed voltage range |
| 328 | for (uint8_t i = 0; i < 3; i++) spiRec(); |
| 329 | } |
| 330 | chipSelectHigh(); |
| 331 | |
| 332 | #ifndef SOFTWARE_SPI |
| 333 | return setSckRate(sckRateID); |
| 334 | #else // SOFTWARE_SPI |
| 335 | return true; |
| 336 | #endif // SOFTWARE_SPI |
| 337 | |
| 338 | fail: |
| 339 | chipSelectHigh(); |
| 340 | return false; |
| 341 | } |
| 342 | //------------------------------------------------------------------------------ |
| 343 | /** |
| 344 | * Enable or disable partial block reads. |
| 345 | * |
| 346 | * Enabling partial block reads improves performance by allowing a block |
| 347 | * to be read over the SPI bus as several sub-blocks. Errors may occur |
| 348 | * if the time between reads is too long since the SD card may timeout. |
| 349 | * The SPI SS line will be held low until the entire block is read or |
| 350 | * readEnd() is called. |
| 351 | * |
| 352 | * Use this for applications like the Adafruit Wave Shield. |
| 353 | * |
| 354 | * \param[in] value The value TRUE (non-zero) or FALSE (zero).) |
| 355 | */ |
| 356 | void Sd2Card::partialBlockRead(uint8_t value) { |
| 357 | readEnd(); |
| 358 | partialBlockRead_ = value; |
| 359 | } |
| 360 | //------------------------------------------------------------------------------ |
| 361 | /** |
| 362 | * Read a 512 byte block from an SD card device. |
| 363 | * |
| 364 | * \param[in] block Logical block to be read. |
| 365 | * \param[out] dst Pointer to the location that will receive the data. |
| 366 | |
| 367 | * \return The value one, true, is returned for success and |
| 368 | * the value zero, false, is returned for failure. |
| 369 | */ |
| 370 | uint8_t Sd2Card::readBlock(uint32_t block, uint8_t* dst) { |
| 371 | return readData(block, 0, 512, dst); |
| 372 | } |
| 373 | //------------------------------------------------------------------------------ |
| 374 | /** |
| 375 | * Read part of a 512 byte block from an SD card. |
| 376 | * |
| 377 | * \param[in] block Logical block to be read. |
| 378 | * \param[in] offset Number of bytes to skip at start of block |
| 379 | * \param[out] dst Pointer to the location that will receive the data. |
| 380 | * \param[in] count Number of bytes to read |
| 381 | * \return The value one, true, is returned for success and |
| 382 | * the value zero, false, is returned for failure. |
| 383 | */ |
| 384 | uint8_t Sd2Card::readData(uint32_t block, |
| 385 | uint16_t offset, uint16_t count, uint8_t* dst) { |
| 386 | if (count == 0) return true; |
| 387 | if ((count + offset) > 512) { |
| 388 | goto fail; |
| 389 | } |
| 390 | if (!inBlock_ || block != block_ || offset < offset_) { |
| 391 | block_ = block; |
| 392 | // use address if not SDHC card |
| 393 | if (type()!= SD_CARD_TYPE_SDHC) block <<= 9; |
| 394 | if (cardCommand(CMD17, block)) { |
| 395 | error(SD_CARD_ERROR_CMD17); |
| 396 | goto fail; |
| 397 | } |
| 398 | if (!waitStartBlock()) { |
| 399 | goto fail; |
| 400 | } |
| 401 | offset_ = 0; |
| 402 | inBlock_ = 1; |
| 403 | } |
| 404 | |
| 405 | #ifdef OPTIMIZE_HARDWARE_SPI |
| 406 | // start first spi transfer |
| 407 | SPDR = 0XFF; |
| 408 | |
| 409 | // skip data before offset |
| 410 | for (;offset_ < offset; offset_++) { |
| 411 | while (!(SPSR & (1 << SPIF))) |
| 412 | ; |
| 413 | SPDR = 0XFF; |
| 414 | } |
| 415 | // transfer data |
| 416 | n = count - 1; |
| 417 | for (uint16_t i = 0; i < n; i++) { |
| 418 | while (!(SPSR & (1 << SPIF))) |
| 419 | ; |
| 420 | dst[i] = SPDR; |
| 421 | SPDR = 0XFF; |
| 422 | } |
| 423 | // wait for last byte |
| 424 | while (!(SPSR & (1 << SPIF))) |
| 425 | ; |
| 426 | dst[n] = SPDR; |
| 427 | |
| 428 | #else // OPTIMIZE_HARDWARE_SPI |
| 429 | |
| 430 | // skip data before offset |
| 431 | for (;offset_ < offset; offset_++) { |
| 432 | spiRec(); |
| 433 | } |
| 434 | // transfer data |
| 435 | for (uint16_t i = 0; i < count; i++) { |
| 436 | dst[i] = spiRec(); |
| 437 | } |
| 438 | #endif // OPTIMIZE_HARDWARE_SPI |
| 439 | |
| 440 | offset_ += count; |
| 441 | if (!partialBlockRead_ || offset_ >= 512) { |
| 442 | // read rest of data, checksum and set chip select high |
| 443 | readEnd(); |
| 444 | } |
| 445 | return true; |
| 446 | |
| 447 | fail: |
| 448 | chipSelectHigh(); |
| 449 | return false; |
| 450 | } |
| 451 | //------------------------------------------------------------------------------ |
| 452 | /** Skip remaining data in a block when in partial block read mode. */ |
| 453 | void Sd2Card::readEnd(void) { |
| 454 | if (inBlock_) { |
| 455 | // skip data and crc |
| 456 | #ifdef OPTIMIZE_HARDWARE_SPI |
| 457 | // optimize skip for hardware |
| 458 | SPDR = 0XFF; |
| 459 | while (offset_++ < 513) { |
| 460 | while (!(SPSR & (1 << SPIF))) |
| 461 | ; |
| 462 | SPDR = 0XFF; |
| 463 | } |
| 464 | // wait for last crc byte |
| 465 | while (!(SPSR & (1 << SPIF))) |
| 466 | ; |
| 467 | #else // OPTIMIZE_HARDWARE_SPI |
| 468 | while (offset_++ < 514) spiRec(); |
| 469 | #endif // OPTIMIZE_HARDWARE_SPI |
| 470 | chipSelectHigh(); |
| 471 | inBlock_ = 0; |
| 472 | } |
| 473 | } |
| 474 | //------------------------------------------------------------------------------ |
| 475 | /** read CID or CSR register */ |
| 476 | uint8_t Sd2Card::readRegister(uint8_t cmd, void* buf) { |
| 477 | uint8_t* dst = reinterpret_cast<uint8_t*>(buf); |
| 478 | if (cardCommand(cmd, 0)) { |
| 479 | error(SD_CARD_ERROR_READ_REG); |
| 480 | goto fail; |
| 481 | } |
| 482 | if (!waitStartBlock()) goto fail; |
| 483 | // transfer data |
| 484 | for (uint16_t i = 0; i < 16; i++) dst[i] = spiRec(); |
| 485 | spiRec(); // get first crc byte |
| 486 | spiRec(); // get second crc byte |
| 487 | chipSelectHigh(); |
| 488 | return true; |
| 489 | |
| 490 | fail: |
| 491 | chipSelectHigh(); |
| 492 | return false; |
| 493 | } |
| 494 | //------------------------------------------------------------------------------ |
| 495 | /** |
| 496 | * Set the SPI clock rate. |
| 497 | * |
| 498 | * \param[in] sckRateID A value in the range [0, 6]. |
| 499 | * |
| 500 | * The SPI clock will be set to F_CPU/pow(2, 1 + sckRateID). The maximum |
| 501 | * SPI rate is F_CPU/2 for \a sckRateID = 0 and the minimum rate is F_CPU/128 |
| 502 | * for \a scsRateID = 6. |
| 503 | * |
| 504 | * \return The value one, true, is returned for success and the value zero, |
| 505 | * false, is returned for an invalid value of \a sckRateID. |
| 506 | */ |
| 507 | uint8_t Sd2Card::setSckRate(uint8_t sckRateID) { |
| 508 | if (sckRateID > 6) { |
| 509 | error(SD_CARD_ERROR_SCK_RATE); |
| 510 | return false; |
| 511 | } |
| 512 | #ifndef USE_SPI_LIB |
| 513 | // see avr processor datasheet for SPI register bit definitions |
| 514 | if ((sckRateID & 1) || sckRateID == 6) { |
| 515 | SPSR &= ~(1 << SPI2X); |
| 516 | } else { |
| 517 | SPSR |= (1 << SPI2X); |
| 518 | } |
| 519 | SPCR &= ~((1 <<SPR1) | (1 << SPR0)); |
| 520 | SPCR |= (sckRateID & 4 ? (1 << SPR1) : 0) |
| 521 | | (sckRateID & 2 ? (1 << SPR0) : 0); |
| 522 | #else // USE_SPI_LIB |
| 523 | switch (sckRateID) { |
| 524 | case 0: settings = SPISettings(25000000, MSBFIRST, SPI_MODE0); break; |
| 525 | case 1: settings = SPISettings(4000000, MSBFIRST, SPI_MODE0); break; |
| 526 | case 2: settings = SPISettings(2000000, MSBFIRST, SPI_MODE0); break; |
| 527 | case 3: settings = SPISettings(1000000, MSBFIRST, SPI_MODE0); break; |
| 528 | case 4: settings = SPISettings(500000, MSBFIRST, SPI_MODE0); break; |
| 529 | case 5: settings = SPISettings(250000, MSBFIRST, SPI_MODE0); break; |
| 530 | default: settings = SPISettings(125000, MSBFIRST, SPI_MODE0); |
| 531 | } |
| 532 | #endif // USE_SPI_LIB |
| 533 | return true; |
| 534 | } |
| 535 | #ifdef USE_SPI_LIB |
| 536 | //------------------------------------------------------------------------------ |
| 537 | // set the SPI clock frequency |
| 538 | uint8_t Sd2Card::setSpiClock(uint32_t clock) |
| 539 | { |
| 540 | settings = SPISettings(clock, MSBFIRST, SPI_MODE0); |
| 541 | return true; |
| 542 | } |
| 543 | #endif |
| 544 | //------------------------------------------------------------------------------ |
| 545 | // wait for card to go not busy |
| 546 | uint8_t Sd2Card::waitNotBusy(unsigned int timeoutMillis) { |
| 547 | unsigned int t0 = millis(); |
| 548 | unsigned int d; |
| 549 | do { |
| 550 | if (spiRec() == 0XFF) return true; |
| 551 | d = millis() - t0; |
| 552 | } |
| 553 | while (d < timeoutMillis); |
| 554 | return false; |
| 555 | } |
| 556 | //------------------------------------------------------------------------------ |
| 557 | /** Wait for start block token */ |
| 558 | uint8_t Sd2Card::waitStartBlock(void) { |
| 559 | unsigned int t0 = millis(); |
| 560 | while ((status_ = spiRec()) == 0XFF) { |
| 561 | unsigned int d = millis() - t0; |
| 562 | if (d > SD_READ_TIMEOUT) { |
| 563 | error(SD_CARD_ERROR_READ_TIMEOUT); |
| 564 | goto fail; |
| 565 | } |
| 566 | } |
| 567 | if (status_ != DATA_START_BLOCK) { |
| 568 | error(SD_CARD_ERROR_READ); |
| 569 | goto fail; |
| 570 | } |
| 571 | return true; |
| 572 | |
| 573 | fail: |
| 574 | chipSelectHigh(); |
| 575 | return false; |
| 576 | } |
| 577 | //------------------------------------------------------------------------------ |
| 578 | /** |
| 579 | * Writes a 512 byte block to an SD card. |
| 580 | * |
| 581 | * \param[in] blockNumber Logical block to be written. |
| 582 | * \param[in] src Pointer to the location of the data to be written. |
| 583 | * \return The value one, true, is returned for success and |
| 584 | * the value zero, false, is returned for failure. |
| 585 | */ |
| 586 | uint8_t Sd2Card::writeBlock(uint32_t blockNumber, const uint8_t* src) { |
| 587 | #if SD_PROTECT_BLOCK_ZERO |
| 588 | // don't allow write to first block |
| 589 | if (blockNumber == 0) { |
| 590 | error(SD_CARD_ERROR_WRITE_BLOCK_ZERO); |
| 591 | goto fail; |
| 592 | } |
| 593 | #endif // SD_PROTECT_BLOCK_ZERO |
| 594 | |
| 595 | // use address if not SDHC card |
| 596 | if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9; |
| 597 | if (cardCommand(CMD24, blockNumber)) { |
| 598 | error(SD_CARD_ERROR_CMD24); |
| 599 | goto fail; |
| 600 | } |
| 601 | if (!writeData(DATA_START_BLOCK, src)) goto fail; |
| 602 | |
| 603 | // wait for flash programming to complete |
| 604 | if (!waitNotBusy(SD_WRITE_TIMEOUT)) { |
| 605 | error(SD_CARD_ERROR_WRITE_TIMEOUT); |
| 606 | goto fail; |
| 607 | } |
| 608 | // response is r2 so get and check two bytes for nonzero |
| 609 | if (cardCommand(CMD13, 0) || spiRec()) { |
| 610 | error(SD_CARD_ERROR_WRITE_PROGRAMMING); |
| 611 | goto fail; |
| 612 | } |
| 613 | chipSelectHigh(); |
| 614 | return true; |
| 615 | |
| 616 | fail: |
| 617 | chipSelectHigh(); |
| 618 | return false; |
| 619 | } |
| 620 | //------------------------------------------------------------------------------ |
| 621 | /** Write one data block in a multiple block write sequence */ |
| 622 | uint8_t Sd2Card::writeData(const uint8_t* src) { |
| 623 | // wait for previous write to finish |
| 624 | if (!waitNotBusy(SD_WRITE_TIMEOUT)) { |
| 625 | error(SD_CARD_ERROR_WRITE_MULTIPLE); |
| 626 | chipSelectHigh(); |
| 627 | return false; |
| 628 | } |
| 629 | return writeData(WRITE_MULTIPLE_TOKEN, src); |
| 630 | } |
| 631 | //------------------------------------------------------------------------------ |
| 632 | // send one block of data for write block or write multiple blocks |
| 633 | uint8_t Sd2Card::writeData(uint8_t token, const uint8_t* src) { |
| 634 | #ifdef OPTIMIZE_HARDWARE_SPI |
| 635 | |
| 636 | // send data - optimized loop |
| 637 | SPDR = token; |
| 638 | |
| 639 | // send two byte per iteration |
| 640 | for (uint16_t i = 0; i < 512; i += 2) { |
| 641 | while (!(SPSR & (1 << SPIF))) |
| 642 | ; |
| 643 | SPDR = src[i]; |
| 644 | while (!(SPSR & (1 << SPIF))) |
| 645 | ; |
| 646 | SPDR = src[i+1]; |
| 647 | } |
| 648 | |
| 649 | // wait for last data byte |
| 650 | while (!(SPSR & (1 << SPIF))) |
| 651 | ; |
| 652 | |
| 653 | #else // OPTIMIZE_HARDWARE_SPI |
| 654 | spiSend(token); |
| 655 | for (uint16_t i = 0; i < 512; i++) { |
| 656 | spiSend(src[i]); |
| 657 | } |
| 658 | #endif // OPTIMIZE_HARDWARE_SPI |
| 659 | spiSend(0xff); // dummy crc |
| 660 | spiSend(0xff); // dummy crc |
| 661 | |
| 662 | status_ = spiRec(); |
| 663 | if ((status_ & DATA_RES_MASK) != DATA_RES_ACCEPTED) { |
| 664 | error(SD_CARD_ERROR_WRITE); |
| 665 | chipSelectHigh(); |
| 666 | return false; |
| 667 | } |
| 668 | return true; |
| 669 | } |
| 670 | //------------------------------------------------------------------------------ |
| 671 | /** Start a write multiple blocks sequence. |
| 672 | * |
| 673 | * \param[in] blockNumber Address of first block in sequence. |
| 674 | * \param[in] eraseCount The number of blocks to be pre-erased. |
| 675 | * |
| 676 | * \note This function is used with writeData() and writeStop() |
| 677 | * for optimized multiple block writes. |
| 678 | * |
| 679 | * \return The value one, true, is returned for success and |
| 680 | * the value zero, false, is returned for failure. |
| 681 | */ |
| 682 | uint8_t Sd2Card::writeStart(uint32_t blockNumber, uint32_t eraseCount) { |
| 683 | #if SD_PROTECT_BLOCK_ZERO |
| 684 | // don't allow write to first block |
| 685 | if (blockNumber == 0) { |
| 686 | error(SD_CARD_ERROR_WRITE_BLOCK_ZERO); |
| 687 | goto fail; |
| 688 | } |
| 689 | #endif // SD_PROTECT_BLOCK_ZERO |
| 690 | // send pre-erase count |
| 691 | if (cardAcmd(ACMD23, eraseCount)) { |
| 692 | error(SD_CARD_ERROR_ACMD23); |
| 693 | goto fail; |
| 694 | } |
| 695 | // use address if not SDHC card |
| 696 | if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9; |
| 697 | if (cardCommand(CMD25, blockNumber)) { |
| 698 | error(SD_CARD_ERROR_CMD25); |
| 699 | goto fail; |
| 700 | } |
| 701 | return true; |
| 702 | |
| 703 | fail: |
| 704 | chipSelectHigh(); |
| 705 | return false; |
| 706 | } |
| 707 | //------------------------------------------------------------------------------ |
| 708 | /** End a write multiple blocks sequence. |
| 709 | * |
| 710 | * \return The value one, true, is returned for success and |
| 711 | * the value zero, false, is returned for failure. |
| 712 | */ |
| 713 | uint8_t Sd2Card::writeStop(void) { |
| 714 | if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto fail; |
| 715 | spiSend(STOP_TRAN_TOKEN); |
| 716 | if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto fail; |
| 717 | chipSelectHigh(); |
| 718 | return true; |
| 719 | |
| 720 | fail: |
| 721 | error(SD_CARD_ERROR_STOP_TRAN); |
| 722 | chipSelectHigh(); |
| 723 | return false; |
| 724 | } |