My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Sd2Card.cpp 19KB

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