My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Sd2Card.cpp 19KB

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