Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

645 lines
19KB

  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 <Arduino.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. digitalWrite(SS_PIN, HIGH); // disable any SPI device using hardware SS pin
  213. // Enable SPI, Master, clock rate f_osc/128
  214. SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR1) | (1 << SPR0);
  215. // clear double speed
  216. SPSR &= ~(1 << SPI2X);
  217. #endif // SOFTWARE_SPI
  218. // must supply min of 74 clock cycles with CS high.
  219. for (uint8_t i = 0; i < 10; i++) spiSend(0XFF);
  220. chipSelectLow();
  221. // command to go idle in SPI mode
  222. while ((status_ = cardCommand(CMD0, 0)) != R1_IDLE_STATE) {
  223. if (((uint16_t)millis() - t0) > SD_INIT_TIMEOUT) {
  224. error(SD_CARD_ERROR_CMD0);
  225. goto fail;
  226. }
  227. }
  228. // check SD version
  229. if ((cardCommand(CMD8, 0x1AA) & R1_ILLEGAL_COMMAND)) {
  230. type(SD_CARD_TYPE_SD1);
  231. } else {
  232. // only need last byte of r7 response
  233. for (uint8_t i = 0; i < 4; i++) status_ = spiRec();
  234. if (status_ != 0XAA) {
  235. error(SD_CARD_ERROR_CMD8);
  236. goto fail;
  237. }
  238. type(SD_CARD_TYPE_SD2);
  239. }
  240. // initialize card and send host supports SDHC if SD2
  241. arg = type() == SD_CARD_TYPE_SD2 ? 0X40000000 : 0;
  242. while ((status_ = cardAcmd(ACMD41, arg)) != R1_READY_STATE) {
  243. // check for timeout
  244. if (((uint16_t)millis() - t0) > SD_INIT_TIMEOUT) {
  245. error(SD_CARD_ERROR_ACMD41);
  246. goto fail;
  247. }
  248. }
  249. // if SD2 read OCR register to check for SDHC card
  250. if (type() == SD_CARD_TYPE_SD2) {
  251. if (cardCommand(CMD58, 0)) {
  252. error(SD_CARD_ERROR_CMD58);
  253. goto fail;
  254. }
  255. if ((spiRec() & 0XC0) == 0XC0) type(SD_CARD_TYPE_SDHC);
  256. // discard rest of ocr - contains allowed voltage range
  257. for (uint8_t i = 0; i < 3; i++) spiRec();
  258. }
  259. chipSelectHigh();
  260. #ifndef SOFTWARE_SPI
  261. return setSckRate(sckRateID);
  262. #else // SOFTWARE_SPI
  263. return true;
  264. #endif // SOFTWARE_SPI
  265. fail:
  266. chipSelectHigh();
  267. return false;
  268. }
  269. //------------------------------------------------------------------------------
  270. /**
  271. * Enable or disable partial block reads.
  272. *
  273. * Enabling partial block reads improves performance by allowing a block
  274. * to be read over the SPI bus as several sub-blocks. Errors may occur
  275. * if the time between reads is too long since the SD card may timeout.
  276. * The SPI SS line will be held low until the entire block is read or
  277. * readEnd() is called.
  278. *
  279. * Use this for applications like the Adafruit Wave Shield.
  280. *
  281. * \param[in] value The value TRUE (non-zero) or FALSE (zero).)
  282. */
  283. void Sd2Card::partialBlockRead(uint8_t value) {
  284. readEnd();
  285. partialBlockRead_ = value;
  286. }
  287. //------------------------------------------------------------------------------
  288. /**
  289. * Read a 512 byte block from an SD card device.
  290. *
  291. * \param[in] block Logical block to be read.
  292. * \param[out] dst Pointer to the location that will receive the data.
  293. * \return The value one, true, is returned for success and
  294. * the value zero, false, is returned for failure.
  295. */
  296. uint8_t Sd2Card::readBlock(uint32_t block, uint8_t* dst) {
  297. return readData(block, 0, 512, dst);
  298. }
  299. //------------------------------------------------------------------------------
  300. /**
  301. * Read part of a 512 byte block from an SD card.
  302. *
  303. * \param[in] block Logical block to be read.
  304. * \param[in] offset Number of bytes to skip at start of block
  305. * \param[out] dst Pointer to the location that will receive the data.
  306. * \param[in] count Number of bytes to read
  307. * \return The value one, true, is returned for success and
  308. * the value zero, false, is returned for failure.
  309. */
  310. uint8_t Sd2Card::readData(uint32_t block,
  311. uint16_t offset, uint16_t count, uint8_t* dst) {
  312. uint16_t n;
  313. if (count == 0) return true;
  314. if ((count + offset) > 512) {
  315. goto fail;
  316. }
  317. if (!inBlock_ || block != block_ || offset < offset_) {
  318. block_ = block;
  319. // use address if not SDHC card
  320. if (type()!= SD_CARD_TYPE_SDHC) block <<= 9;
  321. if (cardCommand(CMD17, block)) {
  322. error(SD_CARD_ERROR_CMD17);
  323. goto fail;
  324. }
  325. if (!waitStartBlock()) {
  326. goto fail;
  327. }
  328. offset_ = 0;
  329. inBlock_ = 1;
  330. }
  331. #ifdef OPTIMIZE_HARDWARE_SPI
  332. // start first spi transfer
  333. SPDR = 0XFF;
  334. // skip data before offset
  335. for (;offset_ < offset; offset_++) {
  336. while (!(SPSR & (1 << SPIF)));
  337. SPDR = 0XFF;
  338. }
  339. // transfer data
  340. n = count - 1;
  341. for (uint16_t i = 0; i < n; i++) {
  342. while (!(SPSR & (1 << SPIF)));
  343. dst[i] = SPDR;
  344. SPDR = 0XFF;
  345. }
  346. // wait for last byte
  347. while (!(SPSR & (1 << SPIF)));
  348. dst[n] = SPDR;
  349. #else // OPTIMIZE_HARDWARE_SPI
  350. // skip data before offset
  351. for (;offset_ < offset; offset_++) {
  352. spiRec();
  353. }
  354. // transfer data
  355. for (uint16_t i = 0; i < count; i++) {
  356. dst[i] = spiRec();
  357. }
  358. #endif // OPTIMIZE_HARDWARE_SPI
  359. offset_ += count;
  360. if (!partialBlockRead_ || offset_ >= 512) {
  361. // read rest of data, checksum and set chip select high
  362. readEnd();
  363. }
  364. return true;
  365. fail:
  366. chipSelectHigh();
  367. return false;
  368. }
  369. //------------------------------------------------------------------------------
  370. /** Skip remaining data in a block when in partial block read mode. */
  371. void Sd2Card::readEnd(void) {
  372. if (inBlock_) {
  373. // skip data and crc
  374. #ifdef OPTIMIZE_HARDWARE_SPI
  375. // optimize skip for hardware
  376. SPDR = 0XFF;
  377. while (offset_++ < 513) {
  378. while (!(SPSR & (1 << SPIF)));
  379. SPDR = 0XFF;
  380. }
  381. // wait for last crc byte
  382. while (!(SPSR & (1 << SPIF)));
  383. #else // OPTIMIZE_HARDWARE_SPI
  384. while (offset_++ < 514) spiRec();
  385. #endif // OPTIMIZE_HARDWARE_SPI
  386. chipSelectHigh();
  387. inBlock_ = 0;
  388. }
  389. }
  390. //------------------------------------------------------------------------------
  391. /** read CID or CSR register */
  392. uint8_t Sd2Card::readRegister(uint8_t cmd, void* buf) {
  393. uint8_t* dst = reinterpret_cast<uint8_t*>(buf);
  394. if (cardCommand(cmd, 0)) {
  395. error(SD_CARD_ERROR_READ_REG);
  396. goto fail;
  397. }
  398. if (!waitStartBlock()) goto fail;
  399. // transfer data
  400. for (uint16_t i = 0; i < 16; i++) dst[i] = spiRec();
  401. spiRec(); // get first crc byte
  402. spiRec(); // get second crc byte
  403. chipSelectHigh();
  404. return true;
  405. fail:
  406. chipSelectHigh();
  407. return false;
  408. }
  409. //------------------------------------------------------------------------------
  410. /**
  411. * Set the SPI clock rate.
  412. *
  413. * \param[in] sckRateID A value in the range [0, 6].
  414. *
  415. * The SPI clock will be set to F_CPU/pow(2, 1 + sckRateID). The maximum
  416. * SPI rate is F_CPU/2 for \a sckRateID = 0 and the minimum rate is F_CPU/128
  417. * for \a scsRateID = 6.
  418. *
  419. * \return The value one, true, is returned for success and the value zero,
  420. * false, is returned for an invalid value of \a sckRateID.
  421. */
  422. uint8_t Sd2Card::setSckRate(uint8_t sckRateID) {
  423. if (sckRateID > 6) {
  424. error(SD_CARD_ERROR_SCK_RATE);
  425. return false;
  426. }
  427. // see avr processor datasheet for SPI register bit definitions
  428. if ((sckRateID & 1) || sckRateID == 6) {
  429. SPSR &= ~(1 << SPI2X);
  430. } else {
  431. SPSR |= (1 << SPI2X);
  432. }
  433. SPCR &= ~((1 <<SPR1) | (1 << SPR0));
  434. SPCR |= (sckRateID & 4 ? (1 << SPR1) : 0)
  435. | (sckRateID & 2 ? (1 << SPR0) : 0);
  436. return true;
  437. }
  438. //------------------------------------------------------------------------------
  439. // wait for card to go not busy
  440. uint8_t Sd2Card::waitNotBusy(uint16_t timeoutMillis) {
  441. uint16_t t0 = millis();
  442. do {
  443. if (spiRec() == 0XFF) return true;
  444. }
  445. while (((uint16_t)millis() - t0) < timeoutMillis);
  446. return false;
  447. }
  448. //------------------------------------------------------------------------------
  449. /** Wait for start block token */
  450. uint8_t Sd2Card::waitStartBlock(void) {
  451. uint16_t t0 = millis();
  452. while ((status_ = spiRec()) == 0XFF) {
  453. if (((uint16_t)millis() - t0) > SD_READ_TIMEOUT) {
  454. error(SD_CARD_ERROR_READ_TIMEOUT);
  455. goto fail;
  456. }
  457. }
  458. if (status_ != DATA_START_BLOCK) {
  459. error(SD_CARD_ERROR_READ);
  460. goto fail;
  461. }
  462. return true;
  463. fail:
  464. chipSelectHigh();
  465. return false;
  466. }
  467. //------------------------------------------------------------------------------
  468. /**
  469. * Writes a 512 byte block to an SD card.
  470. *
  471. * \param[in] blockNumber Logical block to be written.
  472. * \param[in] src Pointer to the location of the data to be written.
  473. * \return The value one, true, is returned for success and
  474. * the value zero, false, is returned for failure.
  475. */
  476. uint8_t Sd2Card::writeBlock(uint32_t blockNumber, const uint8_t* src) {
  477. #if SD_PROTECT_BLOCK_ZERO
  478. // don't allow write to first block
  479. if (blockNumber == 0) {
  480. error(SD_CARD_ERROR_WRITE_BLOCK_ZERO);
  481. goto fail;
  482. }
  483. #endif // SD_PROTECT_BLOCK_ZERO
  484. // use address if not SDHC card
  485. if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9;
  486. if (cardCommand(CMD24, blockNumber)) {
  487. error(SD_CARD_ERROR_CMD24);
  488. goto fail;
  489. }
  490. if (!writeData(DATA_START_BLOCK, src)) goto fail;
  491. // wait for flash programming to complete
  492. if (!waitNotBusy(SD_WRITE_TIMEOUT)) {
  493. error(SD_CARD_ERROR_WRITE_TIMEOUT);
  494. goto fail;
  495. }
  496. // response is r2 so get and check two bytes for nonzero
  497. if (cardCommand(CMD13, 0) || spiRec()) {
  498. error(SD_CARD_ERROR_WRITE_PROGRAMMING);
  499. goto fail;
  500. }
  501. chipSelectHigh();
  502. return true;
  503. fail:
  504. chipSelectHigh();
  505. return false;
  506. }
  507. //------------------------------------------------------------------------------
  508. /** Write one data block in a multiple block write sequence */
  509. uint8_t Sd2Card::writeData(const uint8_t* src) {
  510. // wait for previous write to finish
  511. if (!waitNotBusy(SD_WRITE_TIMEOUT)) {
  512. error(SD_CARD_ERROR_WRITE_MULTIPLE);
  513. chipSelectHigh();
  514. return false;
  515. }
  516. return writeData(WRITE_MULTIPLE_TOKEN, src);
  517. }
  518. //------------------------------------------------------------------------------
  519. // send one block of data for write block or write multiple blocks
  520. uint8_t Sd2Card::writeData(uint8_t token, const uint8_t* src) {
  521. #ifdef OPTIMIZE_HARDWARE_SPI
  522. // send data - optimized loop
  523. SPDR = token;
  524. // send two byte per iteration
  525. for (uint16_t i = 0; i < 512; i += 2) {
  526. while (!(SPSR & (1 << SPIF)));
  527. SPDR = src[i];
  528. while (!(SPSR & (1 << SPIF)));
  529. SPDR = src[i+1];
  530. }
  531. // wait for last data byte
  532. while (!(SPSR & (1 << SPIF)));
  533. #else // OPTIMIZE_HARDWARE_SPI
  534. spiSend(token);
  535. for (uint16_t i = 0; i < 512; i++) {
  536. spiSend(src[i]);
  537. }
  538. #endif // OPTIMIZE_HARDWARE_SPI
  539. spiSend(0xff); // dummy crc
  540. spiSend(0xff); // dummy crc
  541. status_ = spiRec();
  542. if ((status_ & DATA_RES_MASK) != DATA_RES_ACCEPTED) {
  543. error(SD_CARD_ERROR_WRITE);
  544. chipSelectHigh();
  545. return false;
  546. }
  547. return true;
  548. }
  549. //------------------------------------------------------------------------------
  550. /** Start a write multiple blocks sequence.
  551. *
  552. * \param[in] blockNumber Address of first block in sequence.
  553. * \param[in] eraseCount The number of blocks to be pre-erased.
  554. *
  555. * \note This function is used with writeData() and writeStop()
  556. * for optimized multiple block writes.
  557. *
  558. * \return The value one, true, is returned for success and
  559. * the value zero, false, is returned for failure.
  560. */
  561. uint8_t Sd2Card::writeStart(uint32_t blockNumber, uint32_t eraseCount) {
  562. #if SD_PROTECT_BLOCK_ZERO
  563. // don't allow write to first block
  564. if (blockNumber == 0) {
  565. error(SD_CARD_ERROR_WRITE_BLOCK_ZERO);
  566. goto fail;
  567. }
  568. #endif // SD_PROTECT_BLOCK_ZERO
  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. return true;
  581. fail:
  582. chipSelectHigh();
  583. return false;
  584. }
  585. //------------------------------------------------------------------------------
  586. /** End a write multiple blocks sequence.
  587. *
  588. * \return The value one, true, is returned for success and
  589. * the value zero, false, is returned for failure.
  590. */
  591. uint8_t Sd2Card::writeStop(void) {
  592. if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto fail;
  593. spiSend(STOP_TRAN_TOKEN);
  594. if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto fail;
  595. chipSelectHigh();
  596. return true;
  597. fail:
  598. error(SD_CARD_ERROR_STOP_TRAN);
  599. chipSelectHigh();
  600. return false;
  601. }