My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Sd2Card.cpp 20KB

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