| 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 | uint16_t t0 = (uint16_t)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 | if (((uint16_t)(millis() - t0)) > SD_INIT_TIMEOUT) { |
| 292 | error(SD_CARD_ERROR_CMD0); |
| 293 | goto fail; |
| 294 | } |
| 295 | } |
| 296 | // check SD version |
| 297 | if ((cardCommand(CMD8, 0x1AA) & R1_ILLEGAL_COMMAND)) { |
| 298 | type(SD_CARD_TYPE_SD1); |
| 299 | } else { |
| 300 | // only need last byte of r7 response |
| 301 | for (uint8_t i = 0; i < 4; i++) status_ = spiRec(); |
| 302 | if (status_ != 0XAA) { |
| 303 | error(SD_CARD_ERROR_CMD8); |
| 304 | goto fail; |
| 305 | } |
| 306 | type(SD_CARD_TYPE_SD2); |
| 307 | } |
| 308 | // initialize card and send host supports SDHC if SD2 |
| 309 | arg = type() == SD_CARD_TYPE_SD2 ? 0X40000000 : 0; |
| 310 | |
| 311 | while ((status_ = cardAcmd(ACMD41, arg)) != R1_READY_STATE) { |
| 312 | // check for timeout |
| 313 | if (((uint16_t)(millis() - t0)) > SD_INIT_TIMEOUT) { |
| 314 | error(SD_CARD_ERROR_ACMD41); |
| 315 | goto fail; |
| 316 | } |
| 317 | } |
| 318 | // if SD2 read OCR register to check for SDHC card |
| 319 | if (type() == SD_CARD_TYPE_SD2) { |
| 320 | if (cardCommand(CMD58, 0)) { |
| 321 | error(SD_CARD_ERROR_CMD58); |
| 322 | goto fail; |
| 323 | } |
| 324 | if ((spiRec() & 0XC0) == 0XC0) type(SD_CARD_TYPE_SDHC); |
| 325 | // discard rest of ocr - contains allowed voltage range |
| 326 | for (uint8_t i = 0; i < 3; i++) spiRec(); |
| 327 | } |
| 328 | chipSelectHigh(); |
| 329 | |
| 330 | #ifndef SOFTWARE_SPI |
| 331 | return setSckRate(sckRateID); |
| 332 | #else // SOFTWARE_SPI |
| 333 | return true; |
| 334 | #endif // SOFTWARE_SPI |
| 335 | |
| 336 | fail: |
| 337 | chipSelectHigh(); |
| 338 | return false; |
| 339 | } |
| 340 | //------------------------------------------------------------------------------ |
| 341 | /** |
| 342 | * Enable or disable partial block reads. |
| 343 | * |
| 344 | * Enabling partial block reads improves performance by allowing a block |
| 345 | * to be read over the SPI bus as several sub-blocks. Errors may occur |
| 346 | * if the time between reads is too long since the SD card may timeout. |
| 347 | * The SPI SS line will be held low until the entire block is read or |
| 348 | * readEnd() is called. |
| 349 | * |
| 350 | * Use this for applications like the Adafruit Wave Shield. |
| 351 | * |
| 352 | * \param[in] value The value TRUE (non-zero) or FALSE (zero).) |
| 353 | */ |
| 354 | void Sd2Card::partialBlockRead(uint8_t value) { |
| 355 | readEnd(); |
| 356 | partialBlockRead_ = value; |
| 357 | } |
| 358 | //------------------------------------------------------------------------------ |
| 359 | /** |
| 360 | * Read a 512 byte block from an SD card device. |
| 361 | * |
| 362 | * \param[in] block Logical block to be read. |
| 363 | * \param[out] dst Pointer to the location that will receive the data. |
| 364 | |
| 365 | * \return The value one, true, is returned for success and |
| 366 | * the value zero, false, is returned for failure. |
| 367 | */ |
| 368 | uint8_t Sd2Card::readBlock(uint32_t block, uint8_t* dst) { |
| 369 | return readData(block, 0, 512, dst); |
| 370 | } |
| 371 | //------------------------------------------------------------------------------ |
| 372 | /** |
| 373 | * Read part of a 512 byte block from an SD card. |
| 374 | * |
| 375 | * \param[in] block Logical block to be read. |
| 376 | * \param[in] offset Number of bytes to skip at start of block |
| 377 | * \param[out] dst Pointer to the location that will receive the data. |
| 378 | * \param[in] count Number of bytes to read |
| 379 | * \return The value one, true, is returned for success and |
| 380 | * the value zero, false, is returned for failure. |
| 381 | */ |
| 382 | uint8_t Sd2Card::readData(uint32_t block, |
| 383 | uint16_t offset, uint16_t count, uint8_t* dst) { |
| 384 | if (count == 0) return true; |
| 385 | if ((count + offset) > 512) { |
| 386 | goto fail; |
| 387 | } |
| 388 | if (!inBlock_ || block != block_ || offset < offset_) { |
| 389 | block_ = block; |
| 390 | // use address if not SDHC card |
| 391 | if (type()!= SD_CARD_TYPE_SDHC) block <<= 9; |
| 392 | if (cardCommand(CMD17, block)) { |
| 393 | error(SD_CARD_ERROR_CMD17); |
| 394 | goto fail; |
| 395 | } |
| 396 | if (!waitStartBlock()) { |
| 397 | goto fail; |
| 398 | } |
| 399 | offset_ = 0; |
| 400 | inBlock_ = 1; |
| 401 | } |
| 402 | |
| 403 | #ifdef OPTIMIZE_HARDWARE_SPI |
| 404 | // start first spi transfer |
| 405 | SPDR = 0XFF; |
| 406 | |
| 407 | // skip data before offset |
| 408 | for (;offset_ < offset; offset_++) { |
| 409 | while (!(SPSR & (1 << SPIF))) |
| 410 | ; |
| 411 | SPDR = 0XFF; |
| 412 | } |
| 413 | // transfer data |
| 414 | n = count - 1; |
| 415 | for (uint16_t i = 0; i < n; i++) { |
| 416 | while (!(SPSR & (1 << SPIF))) |
| 417 | ; |
| 418 | dst[i] = SPDR; |
| 419 | SPDR = 0XFF; |
| 420 | } |
| 421 | // wait for last byte |
| 422 | while (!(SPSR & (1 << SPIF))) |
| 423 | ; |
| 424 | dst[n] = SPDR; |
| 425 | |
| 426 | #else // OPTIMIZE_HARDWARE_SPI |
| 427 | |
| 428 | // skip data before offset |
| 429 | for (;offset_ < offset; offset_++) { |
| 430 | spiRec(); |
| 431 | } |
| 432 | // transfer data |
| 433 | for (uint16_t i = 0; i < count; i++) { |
| 434 | dst[i] = spiRec(); |
| 435 | } |
| 436 | #endif // OPTIMIZE_HARDWARE_SPI |
| 437 | |
| 438 | offset_ += count; |
| 439 | if (!partialBlockRead_ || offset_ >= 512) { |
| 440 | // read rest of data, checksum and set chip select high |
| 441 | readEnd(); |
| 442 | } |
| 443 | return true; |
| 444 | |
| 445 | fail: |
| 446 | chipSelectHigh(); |
| 447 | return false; |
| 448 | } |
| 449 | //------------------------------------------------------------------------------ |
| 450 | /** Skip remaining data in a block when in partial block read mode. */ |
| 451 | void Sd2Card::readEnd(void) { |
| 452 | if (inBlock_) { |
| 453 | // skip data and crc |
| 454 | #ifdef OPTIMIZE_HARDWARE_SPI |
| 455 | // optimize skip for hardware |
| 456 | SPDR = 0XFF; |
| 457 | while (offset_++ < 513) { |
| 458 | while (!(SPSR & (1 << SPIF))) |
| 459 | ; |
| 460 | SPDR = 0XFF; |
| 461 | } |
| 462 | // wait for last crc byte |
| 463 | while (!(SPSR & (1 << SPIF))) |
| 464 | ; |
| 465 | #else // OPTIMIZE_HARDWARE_SPI |
| 466 | while (offset_++ < 514) spiRec(); |
| 467 | #endif // OPTIMIZE_HARDWARE_SPI |
| 468 | chipSelectHigh(); |
| 469 | inBlock_ = 0; |
| 470 | } |
| 471 | } |
| 472 | //------------------------------------------------------------------------------ |
| 473 | /** read CID or CSR register */ |
| 474 | uint8_t Sd2Card::readRegister(uint8_t cmd, void* buf) { |
| 475 | uint8_t* dst = reinterpret_cast<uint8_t*>(buf); |
| 476 | if (cardCommand(cmd, 0)) { |
| 477 | error(SD_CARD_ERROR_READ_REG); |
| 478 | goto fail; |
| 479 | } |
| 480 | if (!waitStartBlock()) goto fail; |
| 481 | // transfer data |
| 482 | for (uint16_t i = 0; i < 16; i++) dst[i] = spiRec(); |
| 483 | spiRec(); // get first crc byte |
| 484 | spiRec(); // get second crc byte |
| 485 | chipSelectHigh(); |
| 486 | return true; |
| 487 | |
| 488 | fail: |
| 489 | chipSelectHigh(); |
| 490 | return false; |
| 491 | } |
| 492 | //------------------------------------------------------------------------------ |
| 493 | /** |
| 494 | * Set the SPI clock rate. |
| 495 | * |
| 496 | * \param[in] sckRateID A value in the range [0, 6]. |
| 497 | * |
| 498 | * The SPI clock will be set to F_CPU/pow(2, 1 + sckRateID). The maximum |
| 499 | * SPI rate is F_CPU/2 for \a sckRateID = 0 and the minimum rate is F_CPU/128 |
| 500 | * for \a scsRateID = 6. |
| 501 | * |
| 502 | * \return The value one, true, is returned for success and the value zero, |
| 503 | * false, is returned for an invalid value of \a sckRateID. |
| 504 | */ |
| 505 | uint8_t Sd2Card::setSckRate(uint8_t sckRateID) { |
| 506 | if (sckRateID > 6) { |
| 507 | error(SD_CARD_ERROR_SCK_RATE); |
| 508 | return false; |
| 509 | } |
| 510 | #ifndef USE_SPI_LIB |
| 511 | // see avr processor datasheet for SPI register bit definitions |
| 512 | if ((sckRateID & 1) || sckRateID == 6) { |
| 513 | SPSR &= ~(1 << SPI2X); |
| 514 | } else { |
| 515 | SPSR |= (1 << SPI2X); |
| 516 | } |
| 517 | SPCR &= ~((1 <<SPR1) | (1 << SPR0)); |
| 518 | SPCR |= (sckRateID & 4 ? (1 << SPR1) : 0) |
| 519 | | (sckRateID & 2 ? (1 << SPR0) : 0); |
| 520 | #else // USE_SPI_LIB |
| 521 | switch (sckRateID) { |
| 522 | case 0: settings = SPISettings(25000000, MSBFIRST, SPI_MODE0); break; |
| 523 | case 1: settings = SPISettings(4000000, MSBFIRST, SPI_MODE0); break; |
| 524 | case 2: settings = SPISettings(2000000, MSBFIRST, SPI_MODE0); break; |
| 525 | case 3: settings = SPISettings(1000000, MSBFIRST, SPI_MODE0); break; |
| 526 | case 4: settings = SPISettings(500000, MSBFIRST, SPI_MODE0); break; |
| 527 | case 5: settings = SPISettings(250000, MSBFIRST, SPI_MODE0); break; |
| 528 | default: settings = SPISettings(125000, MSBFIRST, SPI_MODE0); |
| 529 | } |
| 530 | #endif // USE_SPI_LIB |
| 531 | return true; |
| 532 | } |
| 533 | #ifdef USE_SPI_LIB |
| 534 | //------------------------------------------------------------------------------ |
| 535 | // set the SPI clock frequency |
| 536 | uint8_t Sd2Card::setSpiClock(uint32_t clock) |
| 537 | { |
| 538 | settings = SPISettings(clock, MSBFIRST, SPI_MODE0); |
| 539 | return true; |
| 540 | } |
| 541 | #endif |
| 542 | //------------------------------------------------------------------------------ |
| 543 | // wait for card to go not busy |
| 544 | uint8_t Sd2Card::waitNotBusy(uint16_t timeoutMillis) { |
| 545 | uint16_t t0 = millis(); |
| 546 | do { |
| 547 | if (spiRec() == 0XFF) return true; |
| 548 | } |
| 549 | while (((uint16_t)millis() - t0) < timeoutMillis); |
| 550 | return false; |
| 551 | } |
| 552 | //------------------------------------------------------------------------------ |
| 553 | /** Wait for start block token */ |
| 554 | uint8_t Sd2Card::waitStartBlock(void) { |
| 555 | uint16_t t0 = millis(); |
| 556 | while ((status_ = spiRec()) == 0XFF) { |
| 557 | if (((uint16_t)millis() - t0) > SD_READ_TIMEOUT) { |
| 558 | error(SD_CARD_ERROR_READ_TIMEOUT); |
| 559 | goto fail; |
| 560 | } |
| 561 | } |
| 562 | if (status_ != DATA_START_BLOCK) { |
| 563 | error(SD_CARD_ERROR_READ); |
| 564 | goto fail; |
| 565 | } |
| 566 | return true; |
| 567 | |
| 568 | fail: |
| 569 | chipSelectHigh(); |
| 570 | return false; |
| 571 | } |
| 572 | //------------------------------------------------------------------------------ |
| 573 | /** |
| 574 | * Writes a 512 byte block to an SD card. |
| 575 | * |
| 576 | * \param[in] blockNumber Logical block to be written. |
| 577 | * \param[in] src Pointer to the location of the data to be written. |
| 578 | * \return The value one, true, is returned for success and |
| 579 | * the value zero, false, is returned for failure. |
| 580 | */ |
| 581 | uint8_t Sd2Card::writeBlock(uint32_t blockNumber, const uint8_t* src) { |
| 582 | #if SD_PROTECT_BLOCK_ZERO |
| 583 | // don't allow write to first block |
| 584 | if (blockNumber == 0) { |
| 585 | error(SD_CARD_ERROR_WRITE_BLOCK_ZERO); |
| 586 | goto fail; |
| 587 | } |
| 588 | #endif // SD_PROTECT_BLOCK_ZERO |
| 589 | |
| 590 | // use address if not SDHC card |
| 591 | if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9; |
| 592 | if (cardCommand(CMD24, blockNumber)) { |
| 593 | error(SD_CARD_ERROR_CMD24); |
| 594 | goto fail; |
| 595 | } |
| 596 | if (!writeData(DATA_START_BLOCK, src)) goto fail; |
| 597 | |
| 598 | // wait for flash programming to complete |
| 599 | if (!waitNotBusy(SD_WRITE_TIMEOUT)) { |
| 600 | error(SD_CARD_ERROR_WRITE_TIMEOUT); |
| 601 | goto fail; |
| 602 | } |
| 603 | // response is r2 so get and check two bytes for nonzero |
| 604 | if (cardCommand(CMD13, 0) || spiRec()) { |
| 605 | error(SD_CARD_ERROR_WRITE_PROGRAMMING); |
| 606 | goto fail; |
| 607 | } |
| 608 | chipSelectHigh(); |
| 609 | return true; |
| 610 | |
| 611 | fail: |
| 612 | chipSelectHigh(); |
| 613 | return false; |
| 614 | } |
| 615 | //------------------------------------------------------------------------------ |
| 616 | /** Write one data block in a multiple block write sequence */ |
| 617 | uint8_t Sd2Card::writeData(const uint8_t* src) { |
| 618 | // wait for previous write to finish |
| 619 | if (!waitNotBusy(SD_WRITE_TIMEOUT)) { |
| 620 | error(SD_CARD_ERROR_WRITE_MULTIPLE); |
| 621 | chipSelectHigh(); |
| 622 | return false; |
| 623 | } |
| 624 | return writeData(WRITE_MULTIPLE_TOKEN, src); |
| 625 | } |
| 626 | //------------------------------------------------------------------------------ |
| 627 | // send one block of data for write block or write multiple blocks |
| 628 | uint8_t Sd2Card::writeData(uint8_t token, const uint8_t* src) { |
| 629 | #ifdef OPTIMIZE_HARDWARE_SPI |
| 630 | |
| 631 | // send data - optimized loop |
| 632 | SPDR = token; |
| 633 | |
| 634 | // send two byte per iteration |
| 635 | for (uint16_t i = 0; i < 512; i += 2) { |
| 636 | while (!(SPSR & (1 << SPIF))) |
| 637 | ; |
| 638 | SPDR = src[i]; |
| 639 | while (!(SPSR & (1 << SPIF))) |
| 640 | ; |
| 641 | SPDR = src[i+1]; |
| 642 | } |
| 643 | |
| 644 | // wait for last data byte |
| 645 | while (!(SPSR & (1 << SPIF))) |
| 646 | ; |
| 647 | |
| 648 | #else // OPTIMIZE_HARDWARE_SPI |
| 649 | spiSend(token); |
| 650 | for (uint16_t i = 0; i < 512; i++) { |
| 651 | spiSend(src[i]); |
| 652 | } |
| 653 | #endif // OPTIMIZE_HARDWARE_SPI |
| 654 | spiSend(0xff); // dummy crc |
| 655 | spiSend(0xff); // dummy crc |
| 656 | |
| 657 | status_ = spiRec(); |
| 658 | if ((status_ & DATA_RES_MASK) != DATA_RES_ACCEPTED) { |
| 659 | error(SD_CARD_ERROR_WRITE); |
| 660 | chipSelectHigh(); |
| 661 | return false; |
| 662 | } |
| 663 | return true; |
| 664 | } |
| 665 | //------------------------------------------------------------------------------ |
| 666 | /** Start a write multiple blocks sequence. |
| 667 | * |
| 668 | * \param[in] blockNumber Address of first block in sequence. |
| 669 | * \param[in] eraseCount The number of blocks to be pre-erased. |
| 670 | * |
| 671 | * \note This function is used with writeData() and writeStop() |
| 672 | * for optimized multiple block writes. |
| 673 | * |
| 674 | * \return The value one, true, is returned for success and |
| 675 | * the value zero, false, is returned for failure. |
| 676 | */ |
| 677 | uint8_t Sd2Card::writeStart(uint32_t blockNumber, uint32_t eraseCount) { |
| 678 | #if SD_PROTECT_BLOCK_ZERO |
| 679 | // don't allow write to first block |
| 680 | if (blockNumber == 0) { |
| 681 | error(SD_CARD_ERROR_WRITE_BLOCK_ZERO); |
| 682 | goto fail; |
| 683 | } |
| 684 | #endif // SD_PROTECT_BLOCK_ZERO |
| 685 | // send pre-erase count |
| 686 | if (cardAcmd(ACMD23, eraseCount)) { |
| 687 | error(SD_CARD_ERROR_ACMD23); |
| 688 | goto fail; |
| 689 | } |
| 690 | // use address if not SDHC card |
| 691 | if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9; |
| 692 | if (cardCommand(CMD25, blockNumber)) { |
| 693 | error(SD_CARD_ERROR_CMD25); |
| 694 | goto fail; |
| 695 | } |
| 696 | return true; |
| 697 | |
| 698 | fail: |
| 699 | chipSelectHigh(); |
| 700 | return false; |
| 701 | } |
| 702 | //------------------------------------------------------------------------------ |
| 703 | /** End a write multiple blocks sequence. |
| 704 | * |
| 705 | * \return The value one, true, is returned for success and |
| 706 | * the value zero, false, is returned for failure. |
| 707 | */ |
| 708 | uint8_t Sd2Card::writeStop(void) { |
| 709 | if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto fail; |
| 710 | spiSend(STOP_TRAN_TOKEN); |
| 711 | if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto fail; |
| 712 | chipSelectHigh(); |
| 713 | return true; |
| 714 | |
| 715 | fail: |
| 716 | error(SD_CARD_ERROR_STOP_TRAN); |
| 717 | chipSelectHigh(); |
| 718 | return false; |
| 719 | } |