選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

202 行
7.3KB

  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 readStart(uint32_t blockNumber);
  162. bool readStop();
  163. /** Return SCK divisor.
  164. *
  165. * \return Requested SCK divisor.
  166. */
  167. uint8_t sckDivisor() {return m_sckDivisor;}
  168. /** Return the card type: SD V1, SD V2 or SDHC
  169. * \return 0 - SD V1, 1 - SD V2, or 3 - SDHC.
  170. */
  171. int type() const {return m_type;}
  172. bool writeBlock(uint32_t blockNumber, const uint8_t* src);
  173. bool writeData(const uint8_t* src);
  174. bool writeStart(uint32_t blockNumber, uint32_t eraseCount);
  175. bool writeStop();
  176. private:
  177. //----------------------------------------------------------------------------
  178. // private functions
  179. uint8_t cardAcmd(uint8_t cmd, uint32_t arg) {
  180. cardCommand(CMD55, 0);
  181. return cardCommand(cmd, arg);
  182. }
  183. uint8_t cardCommand(uint8_t cmd, uint32_t arg);
  184. bool readData(uint8_t* dst, size_t count);
  185. bool readRegister(uint8_t cmd, void* buf);
  186. void chipSelectHigh();
  187. void chipSelectLow();
  188. void spiYield();
  189. void type(uint8_t value) {m_type = value;}
  190. bool waitNotBusy(uint16_t timeoutMillis);
  191. bool writeData(uint8_t token, const uint8_t* src);
  192. // private data
  193. static SdSpi m_spi;
  194. uint8_t m_chipSelectPin;
  195. uint8_t m_errorCode;
  196. uint8_t m_sckDivisor;
  197. uint8_t m_status;
  198. uint8_t m_type;
  199. };
  200. #endif // SpiCard_h