Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Sd2Card.h 7.3KB

10 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #ifndef SpiCard_h
  21. #define SpiCard_h
  22. /**
  23. * \file
  24. * \brief Sd2Card class for V2 SD/SDHC cards
  25. */
  26. #include <Arduino.h>
  27. #include <SdFatConfig.h>
  28. #include <SdInfo.h>
  29. #include <SdSpi.h>
  30. //------------------------------------------------------------------------------
  31. // SD card errors
  32. /** timeout error for command CMD0 (initialize card in SPI mode) */
  33. uint8_t const SD_CARD_ERROR_CMD0 = 0X1;
  34. /** CMD8 was not accepted - not a valid SD card*/
  35. uint8_t const SD_CARD_ERROR_CMD8 = 0X2;
  36. /** card returned an error response for CMD12 (stop multiblock read) */
  37. uint8_t const SD_CARD_ERROR_CMD12 = 0X3;
  38. /** card returned an error response for CMD17 (read block) */
  39. uint8_t const SD_CARD_ERROR_CMD17 = 0X4;
  40. /** card returned an error response for CMD18 (read multiple block) */
  41. uint8_t const SD_CARD_ERROR_CMD18 = 0X5;
  42. /** card returned an error response for CMD24 (write block) */
  43. uint8_t const SD_CARD_ERROR_CMD24 = 0X6;
  44. /** WRITE_MULTIPLE_BLOCKS command failed */
  45. uint8_t const SD_CARD_ERROR_CMD25 = 0X7;
  46. /** card returned an error response for CMD58 (read OCR) */
  47. uint8_t const SD_CARD_ERROR_CMD58 = 0X8;
  48. /** SET_WR_BLK_ERASE_COUNT failed */
  49. uint8_t const SD_CARD_ERROR_ACMD23 = 0X9;
  50. /** ACMD41 initialization process timeout */
  51. uint8_t const SD_CARD_ERROR_ACMD41 = 0XA;
  52. /** card returned a bad CSR version field */
  53. uint8_t const SD_CARD_ERROR_BAD_CSD = 0XB;
  54. /** erase block group command failed */
  55. uint8_t const SD_CARD_ERROR_ERASE = 0XC;
  56. /** card not capable of single block erase */
  57. uint8_t const SD_CARD_ERROR_ERASE_SINGLE_BLOCK = 0XD;
  58. /** Erase sequence timed out */
  59. uint8_t const SD_CARD_ERROR_ERASE_TIMEOUT = 0XE;
  60. /** card returned an error token instead of read data */
  61. uint8_t const SD_CARD_ERROR_READ = 0XF;
  62. /** read CID or CSD failed */
  63. uint8_t const SD_CARD_ERROR_READ_REG = 0X10;
  64. /** timeout while waiting for start of read data */
  65. uint8_t const SD_CARD_ERROR_READ_TIMEOUT = 0X11;
  66. /** card did not accept STOP_TRAN_TOKEN */
  67. uint8_t const SD_CARD_ERROR_STOP_TRAN = 0X12;
  68. /** card returned an error token as a response to a write operation */
  69. uint8_t const SD_CARD_ERROR_WRITE = 0X13;
  70. /** attempt to write protected block zero */
  71. uint8_t const SD_CARD_ERROR_WRITE_BLOCK_ZERO = 0X14; // REMOVE - not used
  72. /** card did not go ready for a multiple block write */
  73. uint8_t const SD_CARD_ERROR_WRITE_MULTIPLE = 0X15;
  74. /** card returned an error to a CMD13 status check after a write */
  75. uint8_t const SD_CARD_ERROR_WRITE_PROGRAMMING = 0X16;
  76. /** timeout occurred during write programming */
  77. uint8_t const SD_CARD_ERROR_WRITE_TIMEOUT = 0X17;
  78. /** incorrect rate selected */
  79. uint8_t const SD_CARD_ERROR_SCK_RATE = 0X18;
  80. /** init() not called */
  81. uint8_t const SD_CARD_ERROR_INIT_NOT_CALLED = 0X19;
  82. /** card returned an error for CMD59 (CRC_ON_OFF) */
  83. uint8_t const SD_CARD_ERROR_CMD59 = 0X1A;
  84. /** invalid read CRC */
  85. uint8_t const SD_CARD_ERROR_READ_CRC = 0X1B;
  86. /** SPI DMA error */
  87. uint8_t const SD_CARD_ERROR_SPI_DMA = 0X1C;
  88. //------------------------------------------------------------------------------
  89. // card types
  90. /** Standard capacity V1 SD card */
  91. uint8_t const SD_CARD_TYPE_SD1 = 1;
  92. /** Standard capacity V2 SD card */
  93. uint8_t const SD_CARD_TYPE_SD2 = 2;
  94. /** High Capacity SD card */
  95. uint8_t const SD_CARD_TYPE_SDHC = 3;
  96. //------------------------------------------------------------------------------
  97. /**
  98. * \class Sd2Card
  99. * \brief Raw access to SD and SDHC flash memory cards.
  100. */
  101. class Sd2Card {
  102. public:
  103. /** Construct an instance of Sd2Card. */
  104. Sd2Card() : m_errorCode(SD_CARD_ERROR_INIT_NOT_CALLED), m_type(0) {}
  105. bool begin(uint8_t chipSelectPin = SD_CHIP_SELECT_PIN,
  106. uint8_t sckDivisor = SPI_FULL_SPEED);
  107. uint32_t cardSize();
  108. bool erase(uint32_t firstBlock, uint32_t lastBlock);
  109. bool eraseSingleBlockEnable();
  110. /**
  111. * Set SD error code.
  112. * \param[in] code value for error code.
  113. */
  114. void error(uint8_t code) {m_errorCode = code;}
  115. /**
  116. * \return error code for last error. See Sd2Card.h for a list of error codes.
  117. */
  118. int errorCode() const {return m_errorCode;}
  119. /** \return error data for last error. */
  120. int errorData() const {return m_status;}
  121. /**
  122. * Initialize an SD flash memory card.
  123. *
  124. * \param[in] chipSelectPin SD chip select pin number.
  125. * \param[in] sckDivisor SPI SCK clock rate divisor.
  126. *
  127. * \return The value one, true, is returned for success and
  128. * the value zero, false, is returned for failure. The reason for failure
  129. * can be determined by calling errorCode() and errorData().
  130. */
  131. bool init(uint8_t sckDivisor = SPI_FULL_SPEED,
  132. uint8_t chipSelectPin = SD_CHIP_SELECT_PIN) {
  133. return begin(chipSelectPin, sckDivisor);
  134. }
  135. bool isBusy();
  136. bool readBlock(uint32_t block, uint8_t* dst);
  137. /**
  138. * Read a card's CID register. The CID contains card identification
  139. * information such as Manufacturer ID, Product name, Product serial
  140. * number and Manufacturing date.
  141. *
  142. * \param[out] cid pointer to area for returned data.
  143. *
  144. * \return true for success or false for failure.
  145. */
  146. bool readCID(cid_t* cid) {
  147. return readRegister(CMD10, cid);
  148. }
  149. /**
  150. * Read a card's CSD register. The CSD contains Card-Specific Data that
  151. * provides information regarding access to the card's contents.
  152. *
  153. * \param[out] csd pointer to area for returned data.
  154. *
  155. * \return true for success or false for failure.
  156. */
  157. bool readCSD(csd_t* csd) {
  158. return readRegister(CMD9, csd);
  159. }
  160. bool readData(uint8_t *dst);
  161. bool readOCR(uint32_t* ocr);
  162. bool readStart(uint32_t blockNumber);
  163. bool readStop();
  164. /** Return SCK divisor.
  165. *
  166. * \return Requested SCK divisor.
  167. */
  168. uint8_t sckDivisor() {return m_sckDivisor;}
  169. /** Return the card type: SD V1, SD V2 or SDHC
  170. * \return 0 - SD V1, 1 - SD V2, or 3 - SDHC.
  171. */
  172. int type() const {return m_type;}
  173. bool writeBlock(uint32_t blockNumber, const uint8_t* src);
  174. bool writeData(const uint8_t* src);
  175. bool writeStart(uint32_t blockNumber, uint32_t eraseCount);
  176. bool writeStop();
  177. private:
  178. //----------------------------------------------------------------------------
  179. // private functions
  180. uint8_t cardAcmd(uint8_t cmd, uint32_t arg) {
  181. cardCommand(CMD55, 0);
  182. return cardCommand(cmd, arg);
  183. }
  184. uint8_t cardCommand(uint8_t cmd, uint32_t arg);
  185. bool readData(uint8_t* dst, size_t count);
  186. bool readRegister(uint8_t cmd, void* buf);
  187. void chipSelectHigh();
  188. void chipSelectLow();
  189. void spiYield();
  190. void type(uint8_t value) {m_type = value;}
  191. bool waitNotBusy(uint16_t timeoutMillis);
  192. bool writeData(uint8_t token, const uint8_t* src);
  193. // private data
  194. static SdSpi m_spi;
  195. uint8_t m_chipSelectPin;
  196. uint8_t m_errorCode;
  197. uint8_t m_sckDivisor;
  198. uint8_t m_status;
  199. uint8_t m_type;
  200. };
  201. #endif // SpiCard_h