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.

671 satır
21KB

  1. /* Arduino Sd2Card Library
  2. * Copyright (C) 2012 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 <Sd2Card.h>
  21. #include <SdSpi.h>
  22. #if !USE_SOFTWARE_SPI && ENABLE_SPI_TRANSACTION
  23. #include <SPI.h>
  24. #endif // !USE_SOFTWARE_SPI && defined(SPI_HAS_TRANSACTION)
  25. // debug trace macro
  26. #define SD_TRACE(m, b)
  27. // #define SD_TRACE(m, b) Serial.print(m);Serial.println(b);
  28. //------------------------------------------------------------------------------
  29. SdSpi Sd2Card::m_spi;
  30. //==============================================================================
  31. #if USE_SD_CRC
  32. // CRC functions
  33. //------------------------------------------------------------------------------
  34. static uint8_t CRC7(const uint8_t* data, uint8_t n) {
  35. uint8_t crc = 0;
  36. for (uint8_t i = 0; i < n; i++) {
  37. uint8_t d = data[i];
  38. for (uint8_t j = 0; j < 8; j++) {
  39. crc <<= 1;
  40. if ((d & 0x80) ^ (crc & 0x80)) crc ^= 0x09;
  41. d <<= 1;
  42. }
  43. }
  44. return (crc << 1) | 1;
  45. }
  46. //------------------------------------------------------------------------------
  47. #if USE_SD_CRC == 1
  48. // slower CRC-CCITT
  49. // uses the x^16,x^12,x^5,x^1 polynomial.
  50. static uint16_t CRC_CCITT(const uint8_t *data, size_t n) {
  51. uint16_t crc = 0;
  52. for (size_t i = 0; i < n; i++) {
  53. crc = (uint8_t)(crc >> 8) | (crc << 8);
  54. crc ^= data[i];
  55. crc ^= (uint8_t)(crc & 0xff) >> 4;
  56. crc ^= crc << 12;
  57. crc ^= (crc & 0xff) << 5;
  58. }
  59. return crc;
  60. }
  61. #elif USE_SD_CRC > 1 // CRC_CCITT
  62. //------------------------------------------------------------------------------
  63. // faster CRC-CCITT
  64. // uses the x^16,x^12,x^5,x^1 polynomial.
  65. #ifdef __AVR__
  66. static const uint16_t crctab[] PROGMEM = {
  67. #else // __AVR__
  68. static const uint16_t crctab[] = {
  69. #endif // __AVR__
  70. 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
  71. 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
  72. 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
  73. 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
  74. 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
  75. 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
  76. 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
  77. 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
  78. 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
  79. 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
  80. 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
  81. 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
  82. 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
  83. 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
  84. 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
  85. 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
  86. 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
  87. 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
  88. 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
  89. 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
  90. 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
  91. 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  92. 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
  93. 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
  94. 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
  95. 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
  96. 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
  97. 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
  98. 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
  99. 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
  100. 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
  101. 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
  102. };
  103. static uint16_t CRC_CCITT(const uint8_t* data, size_t n) {
  104. uint16_t crc = 0;
  105. for (size_t i = 0; i < n; i++) {
  106. #ifdef __AVR__
  107. crc = pgm_read_word(&crctab[(crc >> 8 ^ data[i]) & 0XFF]) ^ (crc << 8);
  108. #else // __AVR__
  109. crc = crctab[(crc >> 8 ^ data[i]) & 0XFF] ^ (crc << 8);
  110. #endif // __AVR__
  111. }
  112. return crc;
  113. }
  114. #endif // CRC_CCITT
  115. #endif // USE_SD_CRC
  116. //==============================================================================
  117. // Sd2Card member functions
  118. //------------------------------------------------------------------------------
  119. /**
  120. * Initialize an SD flash memory card.
  121. *
  122. * \param[in] chipSelectPin SD chip select pin number.
  123. * \param[in] sckDivisor SPI SCK clock rate divisor.
  124. *
  125. * \return The value one, true, is returned for success and
  126. * the value zero, false, is returned for failure. The reason for failure
  127. * can be determined by calling errorCode() and errorData().
  128. */
  129. bool Sd2Card::begin(uint8_t chipSelectPin, uint8_t sckDivisor) {
  130. m_errorCode = m_type = 0;
  131. m_chipSelectPin = chipSelectPin;
  132. // 16-bit init start time allows over a minute
  133. uint16_t t0 = (uint16_t)millis();
  134. uint32_t arg;
  135. pinMode(m_chipSelectPin, OUTPUT);
  136. digitalWrite(m_chipSelectPin, HIGH);
  137. m_spi.begin();
  138. // set SCK rate for initialization commands
  139. m_sckDivisor = SPI_SCK_INIT_DIVISOR;
  140. m_spi.init(m_sckDivisor);
  141. // must supply min of 74 clock cycles with CS high.
  142. for (uint8_t i = 0; i < 10; i++) m_spi.send(0XFF);
  143. // command to go idle in SPI mode
  144. while (cardCommand(CMD0, 0) != R1_IDLE_STATE) {
  145. if (((uint16_t)millis() - t0) > SD_INIT_TIMEOUT) {
  146. error(SD_CARD_ERROR_CMD0);
  147. goto fail;
  148. }
  149. }
  150. #if USE_SD_CRC
  151. if (cardCommand(CMD59, 1) != R1_IDLE_STATE) {
  152. error(SD_CARD_ERROR_CMD59);
  153. goto fail;
  154. }
  155. #endif // USE_SD_CRC
  156. // check SD version
  157. while (1) {
  158. if (cardCommand(CMD8, 0x1AA) == (R1_ILLEGAL_COMMAND | R1_IDLE_STATE)) {
  159. type(SD_CARD_TYPE_SD1);
  160. break;
  161. }
  162. for (uint8_t i = 0; i < 4; i++) m_status = m_spi.receive();
  163. if (m_status == 0XAA) {
  164. type(SD_CARD_TYPE_SD2);
  165. break;
  166. }
  167. if (((uint16_t)millis() - t0) > SD_INIT_TIMEOUT) {
  168. error(SD_CARD_ERROR_CMD8);
  169. goto fail;
  170. }
  171. }
  172. // initialize card and send host supports SDHC if SD2
  173. arg = type() == SD_CARD_TYPE_SD2 ? 0X40000000 : 0;
  174. while (cardAcmd(ACMD41, arg) != R1_READY_STATE) {
  175. // check for timeout
  176. if (((uint16_t)millis() - t0) > SD_INIT_TIMEOUT) {
  177. error(SD_CARD_ERROR_ACMD41);
  178. goto fail;
  179. }
  180. }
  181. // if SD2 read OCR register to check for SDHC card
  182. if (type() == SD_CARD_TYPE_SD2) {
  183. if (cardCommand(CMD58, 0)) {
  184. error(SD_CARD_ERROR_CMD58);
  185. goto fail;
  186. }
  187. if ((m_spi.receive() & 0XC0) == 0XC0) type(SD_CARD_TYPE_SDHC);
  188. // Discard rest of ocr - contains allowed voltage range.
  189. for (uint8_t i = 0; i < 3; i++) m_spi.receive();
  190. }
  191. chipSelectHigh();
  192. m_sckDivisor = sckDivisor;
  193. return true;
  194. fail:
  195. chipSelectHigh();
  196. return false;
  197. }
  198. //------------------------------------------------------------------------------
  199. // send command and return error code. Return zero for OK
  200. uint8_t Sd2Card::cardCommand(uint8_t cmd, uint32_t arg) {
  201. // select card
  202. chipSelectLow();
  203. // wait if busy
  204. waitNotBusy(SD_WRITE_TIMEOUT);
  205. uint8_t *pa = reinterpret_cast<uint8_t *>(&arg);
  206. #if USE_SD_CRC
  207. // form message
  208. uint8_t d[6] = {cmd | 0X40, pa[3], pa[2], pa[1], pa[0]};
  209. // add crc
  210. d[5] = CRC7(d, 5);
  211. // send message
  212. for (uint8_t k = 0; k < 6; k++) m_spi.send(d[k]);
  213. #else // USE_SD_CRC
  214. // send command
  215. m_spi.send(cmd | 0x40);
  216. // send argument
  217. for (int8_t i = 3; i >= 0; i--) m_spi.send(pa[i]);
  218. // send CRC - correct for CMD0 with arg zero or CMD8 with arg 0X1AA
  219. m_spi.send(cmd == CMD0 ? 0X95 : 0X87);
  220. #endif // USE_SD_CRC
  221. // skip stuff byte for stop read
  222. if (cmd == CMD12) m_spi.receive();
  223. // wait for response
  224. for (uint8_t i = 0; ((m_status = m_spi.receive()) & 0X80) && i != 0XFF; i++) {
  225. }
  226. return m_status;
  227. }
  228. //------------------------------------------------------------------------------
  229. /**
  230. * Determine the size of an SD flash memory card.
  231. *
  232. * \return The number of 512 byte data blocks in the card
  233. * or zero if an error occurs.
  234. */
  235. uint32_t Sd2Card::cardSize() {
  236. csd_t csd;
  237. if (!readCSD(&csd)) return 0;
  238. if (csd.v1.csd_ver == 0) {
  239. uint8_t read_bl_len = csd.v1.read_bl_len;
  240. uint16_t c_size = (csd.v1.c_size_high << 10)
  241. | (csd.v1.c_size_mid << 2) | csd.v1.c_size_low;
  242. uint8_t c_size_mult = (csd.v1.c_size_mult_high << 1)
  243. | csd.v1.c_size_mult_low;
  244. return (uint32_t)(c_size + 1) << (c_size_mult + read_bl_len - 7);
  245. } else if (csd.v2.csd_ver == 1) {
  246. uint32_t c_size = 0X10000L * csd.v2.c_size_high + 0X100L
  247. * (uint32_t)csd.v2.c_size_mid + csd.v2.c_size_low;
  248. return (c_size + 1) << 10;
  249. } else {
  250. error(SD_CARD_ERROR_BAD_CSD);
  251. return 0;
  252. }
  253. }
  254. //------------------------------------------------------------------------------
  255. void Sd2Card::spiYield() {
  256. #if ENABLE_SPI_YIELD && !USE_SOFTWARE_SPI && defined(SPI_HAS_TRANSACTION)
  257. chipSelectHigh();
  258. chipSelectLow();
  259. #endif // ENABLE_SPI_YIELD && !USE_SOFTWARE_SPI && defined(SPI_HAS_TRANSACTION)
  260. }
  261. //------------------------------------------------------------------------------
  262. void Sd2Card::chipSelectHigh() {
  263. digitalWrite(m_chipSelectPin, HIGH);
  264. // insure MISO goes high impedance
  265. m_spi.send(0XFF);
  266. #if !USE_SOFTWARE_SPI && defined(SPI_HAS_TRANSACTION)
  267. SPI.endTransaction();
  268. #endif // !USE_SOFTWARE_SPI && defined(SPI_HAS_TRANSACTION)
  269. }
  270. //------------------------------------------------------------------------------
  271. void Sd2Card::chipSelectLow() {
  272. #if !USE_SOFTWARE_SPI && defined(SPI_HAS_TRANSACTION)
  273. SPI.beginTransaction(SPISettings());
  274. #endif // !USE_SOFTWARE_SPI && defined(SPI_HAS_TRANSACTION)
  275. m_spi.init(m_sckDivisor);
  276. digitalWrite(m_chipSelectPin, LOW);
  277. }
  278. //------------------------------------------------------------------------------
  279. /** Erase a range of blocks.
  280. *
  281. * \param[in] firstBlock The address of the first block in the range.
  282. * \param[in] lastBlock The address of the last block in the range.
  283. *
  284. * \note This function requests the SD card to do a flash erase for a
  285. * range of blocks. The data on the card after an erase operation is
  286. * either 0 or 1, depends on the card vendor. The card must support
  287. * single block erase.
  288. *
  289. * \return The value one, true, is returned for success and
  290. * the value zero, false, is returned for failure.
  291. */
  292. bool Sd2Card::erase(uint32_t firstBlock, uint32_t lastBlock) {
  293. csd_t csd;
  294. if (!readCSD(&csd)) goto fail;
  295. // check for single block erase
  296. if (!csd.v1.erase_blk_en) {
  297. // erase size mask
  298. uint8_t m = (csd.v1.sector_size_high << 1) | csd.v1.sector_size_low;
  299. if ((firstBlock & m) != 0 || ((lastBlock + 1) & m) != 0) {
  300. // error card can't erase specified area
  301. error(SD_CARD_ERROR_ERASE_SINGLE_BLOCK);
  302. goto fail;
  303. }
  304. }
  305. if (m_type != SD_CARD_TYPE_SDHC) {
  306. firstBlock <<= 9;
  307. lastBlock <<= 9;
  308. }
  309. if (cardCommand(CMD32, firstBlock)
  310. || cardCommand(CMD33, lastBlock)
  311. || cardCommand(CMD38, 0)) {
  312. error(SD_CARD_ERROR_ERASE);
  313. goto fail;
  314. }
  315. if (!waitNotBusy(SD_ERASE_TIMEOUT)) {
  316. error(SD_CARD_ERROR_ERASE_TIMEOUT);
  317. goto fail;
  318. }
  319. chipSelectHigh();
  320. return true;
  321. fail:
  322. chipSelectHigh();
  323. return false;
  324. }
  325. //------------------------------------------------------------------------------
  326. /** Determine if card supports single block erase.
  327. *
  328. * \return The value one, true, is returned if single block erase is supported.
  329. * The value zero, false, is returned if single block erase is not supported.
  330. */
  331. bool Sd2Card::eraseSingleBlockEnable() {
  332. csd_t csd;
  333. return readCSD(&csd) ? csd.v1.erase_blk_en : false;
  334. }
  335. //------------------------------------------------------------------------------
  336. /**
  337. * Check for busy. MISO low indicates the card is busy.
  338. *
  339. * \return true if busy else false.
  340. */
  341. bool Sd2Card::isBusy() {
  342. bool rtn;
  343. chipSelectLow();
  344. for (uint8_t i = 0; i < 8; i++) {
  345. rtn = m_spi.receive() != 0XFF;
  346. if (!rtn) break;
  347. }
  348. chipSelectHigh();
  349. return rtn;
  350. }
  351. //------------------------------------------------------------------------------
  352. /**
  353. * Read a 512 byte block from an SD card.
  354. *
  355. * \param[in] blockNumber Logical block to be read.
  356. * \param[out] dst Pointer to the location that will receive the data.
  357. * \return The value one, true, is returned for success and
  358. * the value zero, false, is returned for failure.
  359. */
  360. bool Sd2Card::readBlock(uint32_t blockNumber, uint8_t* dst) {
  361. SD_TRACE("RB", blockNumber);
  362. // use address if not SDHC card
  363. if (type()!= SD_CARD_TYPE_SDHC) blockNumber <<= 9;
  364. if (cardCommand(CMD17, blockNumber)) {
  365. error(SD_CARD_ERROR_CMD17);
  366. goto fail;
  367. }
  368. return readData(dst, 512);
  369. fail:
  370. chipSelectHigh();
  371. return false;
  372. }
  373. //------------------------------------------------------------------------------
  374. /** Read one data block in a multiple block read sequence
  375. *
  376. * \param[in] dst Pointer to the location for the data to be read.
  377. *
  378. * \return The value one, true, is returned for success and
  379. * the value zero, false, is returned for failure.
  380. */
  381. bool Sd2Card::readData(uint8_t *dst) {
  382. chipSelectLow();
  383. return readData(dst, 512);
  384. }
  385. //------------------------------------------------------------------------------
  386. bool Sd2Card::readData(uint8_t* dst, size_t count) {
  387. #if USE_SD_CRC
  388. uint16_t crc;
  389. #endif // USE_SD_CRC
  390. // wait for start block token
  391. uint16_t t0 = millis();
  392. while ((m_status = m_spi.receive()) == 0XFF) {
  393. if (((uint16_t)millis() - t0) > SD_READ_TIMEOUT) {
  394. error(SD_CARD_ERROR_READ_TIMEOUT);
  395. goto fail;
  396. }
  397. spiYield();
  398. }
  399. if (m_status != DATA_START_BLOCK) {
  400. error(SD_CARD_ERROR_READ);
  401. goto fail;
  402. }
  403. // transfer data
  404. if ((m_status = m_spi.receive(dst, count))) {
  405. error(SD_CARD_ERROR_SPI_DMA);
  406. goto fail;
  407. }
  408. #if USE_SD_CRC
  409. // get crc
  410. crc = (m_spi.receive() << 8) | m_spi.receive();
  411. if (crc != CRC_CCITT(dst, count)) {
  412. error(SD_CARD_ERROR_READ_CRC);
  413. goto fail;
  414. }
  415. #else
  416. // discard crc
  417. m_spi.receive();
  418. m_spi.receive();
  419. #endif // USE_SD_CRC
  420. chipSelectHigh();
  421. return true;
  422. fail:
  423. chipSelectHigh();
  424. return false;
  425. }
  426. //------------------------------------------------------------------------------
  427. /** Read OCR register.
  428. *
  429. * \param[out] ocr Value of OCR register.
  430. * \return true for success else false.
  431. */
  432. bool Sd2Card::readOCR(uint32_t* ocr) {
  433. uint8_t *p = reinterpret_cast<uint8_t*>(ocr);
  434. if (cardCommand(CMD58, 0)) {
  435. error(SD_CARD_ERROR_CMD58);
  436. goto fail;
  437. }
  438. for (uint8_t i = 0; i < 4; i++) p[3-i] = m_spi.receive();
  439. chipSelectHigh();
  440. return true;
  441. fail:
  442. chipSelectHigh();
  443. return false;
  444. }
  445. //------------------------------------------------------------------------------
  446. /** read CID or CSR register */
  447. bool Sd2Card::readRegister(uint8_t cmd, void* buf) {
  448. uint8_t* dst = reinterpret_cast<uint8_t*>(buf);
  449. if (cardCommand(cmd, 0)) {
  450. error(SD_CARD_ERROR_READ_REG);
  451. goto fail;
  452. }
  453. return readData(dst, 16);
  454. fail:
  455. chipSelectHigh();
  456. return false;
  457. }
  458. //------------------------------------------------------------------------------
  459. /** Start a read multiple blocks sequence.
  460. *
  461. * \param[in] blockNumber Address of first block in sequence.
  462. *
  463. * \note This function is used with readData() and readStop() for optimized
  464. * multiple block reads. SPI chipSelect must be low for the entire sequence.
  465. *
  466. * \return The value one, true, is returned for success and
  467. * the value zero, false, is returned for failure.
  468. */
  469. bool Sd2Card::readStart(uint32_t blockNumber) {
  470. SD_TRACE("RS", blockNumber);
  471. if (type()!= SD_CARD_TYPE_SDHC) blockNumber <<= 9;
  472. if (cardCommand(CMD18, blockNumber)) {
  473. error(SD_CARD_ERROR_CMD18);
  474. goto fail;
  475. }
  476. chipSelectHigh();
  477. return true;
  478. fail:
  479. chipSelectHigh();
  480. return false;
  481. }
  482. //------------------------------------------------------------------------------
  483. /** End a read multiple blocks sequence.
  484. *
  485. * \return The value one, true, is returned for success and
  486. * the value zero, false, is returned for failure.
  487. */
  488. bool Sd2Card::readStop() {
  489. if (cardCommand(CMD12, 0)) {
  490. error(SD_CARD_ERROR_CMD12);
  491. goto fail;
  492. }
  493. chipSelectHigh();
  494. return true;
  495. fail:
  496. chipSelectHigh();
  497. return false;
  498. }
  499. //------------------------------------------------------------------------------
  500. // wait for card to go not busy
  501. bool Sd2Card::waitNotBusy(uint16_t timeoutMillis) {
  502. uint16_t t0 = millis();
  503. while (m_spi.receive() != 0XFF) {
  504. if (((uint16_t)millis() - t0) >= timeoutMillis) goto fail;
  505. spiYield();
  506. }
  507. return true;
  508. fail:
  509. return false;
  510. }
  511. //------------------------------------------------------------------------------
  512. /**
  513. * Writes a 512 byte block to an SD card.
  514. *
  515. * \param[in] blockNumber Logical block to be written.
  516. * \param[in] src Pointer to the location of the data to be written.
  517. * \return The value one, true, is returned for success and
  518. * the value zero, false, is returned for failure.
  519. */
  520. bool Sd2Card::writeBlock(uint32_t blockNumber, const uint8_t* src) {
  521. SD_TRACE("WB", blockNumber);
  522. // use address if not SDHC card
  523. if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9;
  524. if (cardCommand(CMD24, blockNumber)) {
  525. error(SD_CARD_ERROR_CMD24);
  526. goto fail;
  527. }
  528. if (!writeData(DATA_START_BLOCK, src)) goto fail;
  529. #define CHECK_PROGRAMMING 0
  530. #if CHECK_PROGRAMMING
  531. // wait for flash programming to complete
  532. if (!waitNotBusy(SD_WRITE_TIMEOUT)) {
  533. error(SD_CARD_ERROR_WRITE_TIMEOUT);
  534. goto fail;
  535. }
  536. // response is r2 so get and check two bytes for nonzero
  537. if (cardCommand(CMD13, 0) || m_spi.receive()) {
  538. error(SD_CARD_ERROR_WRITE_PROGRAMMING);
  539. goto fail;
  540. }
  541. #endif // CHECK_PROGRAMMING
  542. chipSelectHigh();
  543. return true;
  544. fail:
  545. chipSelectHigh();
  546. return false;
  547. }
  548. //------------------------------------------------------------------------------
  549. /** Write one data block in a multiple block write sequence
  550. * \param[in] src Pointer to the location of the data to be written.
  551. * \return The value one, true, is returned for success and
  552. * the value zero, false, is returned for failure.
  553. */
  554. bool Sd2Card::writeData(const uint8_t* src) {
  555. chipSelectLow();
  556. // wait for previous write to finish
  557. if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto fail;
  558. if (!writeData(WRITE_MULTIPLE_TOKEN, src)) goto fail;
  559. chipSelectHigh();
  560. return true;
  561. fail:
  562. error(SD_CARD_ERROR_WRITE_MULTIPLE);
  563. chipSelectHigh();
  564. return false;
  565. }
  566. //------------------------------------------------------------------------------
  567. // send one block of data for write block or write multiple blocks
  568. bool Sd2Card::writeData(uint8_t token, const uint8_t* src) {
  569. #if USE_SD_CRC
  570. uint16_t crc = CRC_CCITT(src, 512);
  571. #else // USE_SD_CRC
  572. uint16_t crc = 0XFFFF;
  573. #endif // USE_SD_CRC
  574. m_spi.send(token);
  575. m_spi.send(src, 512);
  576. m_spi.send(crc >> 8);
  577. m_spi.send(crc & 0XFF);
  578. m_status = m_spi.receive();
  579. if ((m_status & DATA_RES_MASK) != DATA_RES_ACCEPTED) {
  580. error(SD_CARD_ERROR_WRITE);
  581. goto fail;
  582. }
  583. return true;
  584. fail:
  585. chipSelectHigh();
  586. return false;
  587. }
  588. //------------------------------------------------------------------------------
  589. /** Start a write multiple blocks sequence.
  590. *
  591. * \param[in] blockNumber Address of first block in sequence.
  592. * \param[in] eraseCount The number of blocks to be pre-erased.
  593. *
  594. * \note This function is used with writeData() and writeStop()
  595. * for optimized multiple block writes.
  596. *
  597. * \return The value one, true, is returned for success and
  598. * the value zero, false, is returned for failure.
  599. */
  600. bool Sd2Card::writeStart(uint32_t blockNumber, uint32_t eraseCount) {
  601. SD_TRACE("WS", blockNumber);
  602. // send pre-erase count
  603. if (cardAcmd(ACMD23, eraseCount)) {
  604. error(SD_CARD_ERROR_ACMD23);
  605. goto fail;
  606. }
  607. // use address if not SDHC card
  608. if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9;
  609. if (cardCommand(CMD25, blockNumber)) {
  610. error(SD_CARD_ERROR_CMD25);
  611. goto fail;
  612. }
  613. chipSelectHigh();
  614. return true;
  615. fail:
  616. chipSelectHigh();
  617. return false;
  618. }
  619. //------------------------------------------------------------------------------
  620. /** End a write multiple blocks sequence.
  621. *
  622. * \return The value one, true, is returned for success and
  623. * the value zero, false, is returned for failure.
  624. */
  625. bool Sd2Card::writeStop() {
  626. chipSelectLow();
  627. if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto fail;
  628. m_spi.send(STOP_TRAN_TOKEN);
  629. if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto fail;
  630. chipSelectHigh();
  631. return true;
  632. fail:
  633. error(SD_CARD_ERROR_STOP_TRAN);
  634. chipSelectHigh();
  635. return false;
  636. }