Sd2Card.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 "Marlin.h"
  21. #ifdef SDSUPPORT
  22. #ifndef Sd2Card_h
  23. #define Sd2Card_h
  24. /**
  25. * \file
  26. * \brief Sd2Card class for V2 SD/SDHC cards
  27. */
  28. #include "SdFatConfig.h"
  29. #include "Sd2PinMap.h"
  30. #include "SdInfo.h"
  31. //------------------------------------------------------------------------------
  32. // SPI speed is F_CPU/2^(1 + index), 0 <= index <= 6
  33. /** Set SCK to max rate of F_CPU/2. See Sd2Card::setSckRate(). */
  34. uint8_t const SPI_FULL_SPEED = 0;
  35. /** Set SCK rate to F_CPU/4. See Sd2Card::setSckRate(). */
  36. uint8_t const SPI_HALF_SPEED = 1;
  37. /** Set SCK rate to F_CPU/8. See Sd2Card::setSckRate(). */
  38. uint8_t const SPI_QUARTER_SPEED = 2;
  39. /** Set SCK rate to F_CPU/16. See Sd2Card::setSckRate(). */
  40. uint8_t const SPI_EIGHTH_SPEED = 3;
  41. /** Set SCK rate to F_CPU/32. See Sd2Card::setSckRate(). */
  42. uint8_t const SPI_SIXTEENTH_SPEED = 4;
  43. //------------------------------------------------------------------------------
  44. /** init timeout ms */
  45. uint16_t const SD_INIT_TIMEOUT = 2000;
  46. /** erase timeout ms */
  47. uint16_t const SD_ERASE_TIMEOUT = 10000;
  48. /** read timeout ms */
  49. uint16_t const SD_READ_TIMEOUT = 300;
  50. /** write time out ms */
  51. uint16_t const SD_WRITE_TIMEOUT = 600;
  52. //------------------------------------------------------------------------------
  53. // SD card errors
  54. /** timeout error for command CMD0 (initialize card in SPI mode) */
  55. uint8_t const SD_CARD_ERROR_CMD0 = 0X1;
  56. /** CMD8 was not accepted - not a valid SD card*/
  57. uint8_t const SD_CARD_ERROR_CMD8 = 0X2;
  58. /** card returned an error response for CMD12 (write stop) */
  59. uint8_t const SD_CARD_ERROR_CMD12 = 0X3;
  60. /** card returned an error response for CMD17 (read block) */
  61. uint8_t const SD_CARD_ERROR_CMD17 = 0X4;
  62. /** card returned an error response for CMD18 (read multiple block) */
  63. uint8_t const SD_CARD_ERROR_CMD18 = 0X5;
  64. /** card returned an error response for CMD24 (write block) */
  65. uint8_t const SD_CARD_ERROR_CMD24 = 0X6;
  66. /** WRITE_MULTIPLE_BLOCKS command failed */
  67. uint8_t const SD_CARD_ERROR_CMD25 = 0X7;
  68. /** card returned an error response for CMD58 (read OCR) */
  69. uint8_t const SD_CARD_ERROR_CMD58 = 0X8;
  70. /** SET_WR_BLK_ERASE_COUNT failed */
  71. uint8_t const SD_CARD_ERROR_ACMD23 = 0X9;
  72. /** ACMD41 initialization process timeout */
  73. uint8_t const SD_CARD_ERROR_ACMD41 = 0XA;
  74. /** card returned a bad CSR version field */
  75. uint8_t const SD_CARD_ERROR_BAD_CSD = 0XB;
  76. /** erase block group command failed */
  77. uint8_t const SD_CARD_ERROR_ERASE = 0XC;
  78. /** card not capable of single block erase */
  79. uint8_t const SD_CARD_ERROR_ERASE_SINGLE_BLOCK = 0XD;
  80. /** Erase sequence timed out */
  81. uint8_t const SD_CARD_ERROR_ERASE_TIMEOUT = 0XE;
  82. /** card returned an error token instead of read data */
  83. uint8_t const SD_CARD_ERROR_READ = 0XF;
  84. /** read CID or CSD failed */
  85. uint8_t const SD_CARD_ERROR_READ_REG = 0X10;
  86. /** timeout while waiting for start of read data */
  87. uint8_t const SD_CARD_ERROR_READ_TIMEOUT = 0X11;
  88. /** card did not accept STOP_TRAN_TOKEN */
  89. uint8_t const SD_CARD_ERROR_STOP_TRAN = 0X12;
  90. /** card returned an error token as a response to a write operation */
  91. uint8_t const SD_CARD_ERROR_WRITE = 0X13;
  92. /** attempt to write protected block zero */
  93. uint8_t const SD_CARD_ERROR_WRITE_BLOCK_ZERO = 0X14; // REMOVE - not used
  94. /** card did not go ready for a multiple block write */
  95. uint8_t const SD_CARD_ERROR_WRITE_MULTIPLE = 0X15;
  96. /** card returned an error to a CMD13 status check after a write */
  97. uint8_t const SD_CARD_ERROR_WRITE_PROGRAMMING = 0X16;
  98. /** timeout occurred during write programming */
  99. uint8_t const SD_CARD_ERROR_WRITE_TIMEOUT = 0X17;
  100. /** incorrect rate selected */
  101. uint8_t const SD_CARD_ERROR_SCK_RATE = 0X18;
  102. /** init() not called */
  103. uint8_t const SD_CARD_ERROR_INIT_NOT_CALLED = 0X19;
  104. /** crc check error */
  105. uint8_t const SD_CARD_ERROR_CRC = 0X20;
  106. /** no response to sent 0xFF */
  107. uint8_t const SD_CARD_ERROR_FF_TIMEOUT = 0X21;
  108. /** Toshiba FlashAir: iSDIO */
  109. uint8_t const SD_CARD_ERROR_CMD48 = 0x80;
  110. /** Toshiba FlashAir: iSDIO */
  111. uint8_t const SD_CARD_ERROR_CMD49 = 0x81;
  112. //------------------------------------------------------------------------------
  113. // card types
  114. /** Standard capacity V1 SD card */
  115. uint8_t const SD_CARD_TYPE_SD1 = 1;
  116. /** Standard capacity V2 SD card */
  117. uint8_t const SD_CARD_TYPE_SD2 = 2;
  118. /** High Capacity SD card */
  119. uint8_t const SD_CARD_TYPE_SDHC = 3;
  120. /**
  121. * define SOFTWARE_SPI to use bit-bang SPI
  122. */
  123. //------------------------------------------------------------------------------
  124. #if MEGA_SOFT_SPI && (defined(__AVR_ATmega1280__)||defined(__AVR_ATmega2560__))
  125. #define SOFTWARE_SPI
  126. #elif USE_SOFTWARE_SPI
  127. #define SOFTWARE_SPI
  128. #endif // MEGA_SOFT_SPI
  129. //------------------------------------------------------------------------------
  130. // SPI pin definitions - do not edit here - change in SdFatConfig.h
  131. //
  132. #ifndef SOFTWARE_SPI
  133. // hardware pin defs
  134. /** The default chip select pin for the SD card is SS. */
  135. uint8_t const SD_CHIP_SELECT_PIN = SS_PIN;
  136. // The following three pins must not be redefined for hardware SPI.
  137. /** SPI Master Out Slave In pin */
  138. uint8_t const SPI_MOSI_PIN = MOSI_PIN;
  139. /** SPI Master In Slave Out pin */
  140. uint8_t const SPI_MISO_PIN = MISO_PIN;
  141. /** SPI Clock pin */
  142. uint8_t const SPI_SCK_PIN = SCK_PIN;
  143. #else // SOFTWARE_SPI
  144. /** SPI chip select pin */
  145. uint8_t const SD_CHIP_SELECT_PIN = SOFT_SPI_CS_PIN;
  146. /** SPI Master Out Slave In pin */
  147. uint8_t const SPI_MOSI_PIN = SOFT_SPI_MOSI_PIN;
  148. /** SPI Master In Slave Out pin */
  149. uint8_t const SPI_MISO_PIN = SOFT_SPI_MISO_PIN;
  150. /** SPI Clock pin */
  151. uint8_t const SPI_SCK_PIN = SOFT_SPI_SCK_PIN;
  152. #endif // SOFTWARE_SPI
  153. //------------------------------------------------------------------------------
  154. /**
  155. * \class Sd2Card
  156. * \brief Raw access to SD and SDHC flash memory cards.
  157. */
  158. class Sd2Card {
  159. public:
  160. /** Construct an instance of Sd2Card. */
  161. Sd2Card() : errorCode_(SD_CARD_ERROR_INIT_NOT_CALLED), type_(0), flash_air_compatible_(false) {}
  162. uint32_t cardSize();
  163. bool erase(uint32_t firstBlock, uint32_t lastBlock);
  164. bool eraseSingleBlockEnable();
  165. /**
  166. * Set SD error code.
  167. * \param[in] code value for error code.
  168. */
  169. void error(uint8_t code) {errorCode_ = code;}
  170. /**
  171. * \return error code for last error. See Sd2Card.h for a list of error codes.
  172. */
  173. int errorCode() const {return errorCode_;}
  174. /** \return error data for last error. */
  175. int errorData() const {return status_;}
  176. /**
  177. * Initialize an SD flash memory card with default clock rate and chip
  178. * select pin. See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin).
  179. *
  180. * \return true for success or false for failure.
  181. */
  182. bool init(uint8_t sckRateID = SPI_FULL_SPEED,
  183. uint8_t chipSelectPin = SD_CHIP_SELECT_PIN);
  184. bool readBlock(uint32_t block, uint8_t* dst);
  185. /**
  186. * Read a card's CID register. The CID contains card identification
  187. * information such as Manufacturer ID, Product name, Product serial
  188. * number and Manufacturing date.
  189. *
  190. * \param[out] cid pointer to area for returned data.
  191. *
  192. * \return true for success or false for failure.
  193. */
  194. bool readCID(cid_t* cid) {
  195. return readRegister(CMD10, cid);
  196. }
  197. /**
  198. * Read a card's CSD register. The CSD contains Card-Specific Data that
  199. * provides information regarding access to the card's contents.
  200. *
  201. * \param[out] csd pointer to area for returned data.
  202. *
  203. * \return true for success or false for failure.
  204. */
  205. bool readCSD(csd_t* csd) {
  206. return readRegister(CMD9, csd);
  207. }
  208. bool readData(uint8_t *dst);
  209. bool readStart(uint32_t blockNumber);
  210. bool readStop();
  211. bool setSckRate(uint8_t sckRateID);
  212. /** Return the card type: SD V1, SD V2 or SDHC
  213. * \return 0 - SD V1, 1 - SD V2, or 3 - SDHC.
  214. */
  215. int type() const {return type_;}
  216. bool writeBlock(uint32_t blockNumber, const uint8_t* src);
  217. bool writeData(const uint8_t* src);
  218. bool writeStart(uint32_t blockNumber, uint32_t eraseCount);
  219. bool writeStop();
  220. // Toshiba FlashAir support
  221. uint8_t readExtMemory(uint8_t mio, uint8_t func, uint32_t addr, uint16_t count, uint8_t* dst);
  222. void setFlashAirCompatible(bool flashAirCompatible) { flash_air_compatible_ = flashAirCompatible; }
  223. bool getFlashAirCompatible() const { return flash_air_compatible_; }
  224. private:
  225. //----------------------------------------------------------------------------
  226. uint8_t chipSelectPin_;
  227. uint8_t errorCode_;
  228. uint8_t spiRate_;
  229. uint8_t status_;
  230. uint8_t type_;
  231. bool flash_air_compatible_;
  232. // private functions
  233. uint8_t cardAcmd(uint8_t cmd, uint32_t arg) {
  234. cardCommand(CMD55, 0);
  235. return cardCommand(cmd, arg);
  236. }
  237. uint8_t cardCommand(uint8_t cmd, uint32_t arg);
  238. bool readData(uint8_t* dst, uint16_t count);
  239. bool readRegister(uint8_t cmd, void* buf);
  240. void chipSelectHigh();
  241. void chipSelectLow();
  242. void type(uint8_t value) {type_ = value;}
  243. bool waitNotBusy(uint16_t timeoutMillis);
  244. bool writeData(uint8_t token, const uint8_t* src);
  245. // Toshiba FlashAir support
  246. uint8_t waitStartBlock(void);
  247. uint8_t readExt(uint32_t arg, uint8_t* dst, uint16_t count);
  248. };
  249. #endif // Sd2Card_h
  250. #endif