My Marlin configs for Fabrikator Mini and CTC i3 Pro B
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.

Sd2Card.h 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * Arduino Sd2Card Library
  24. * Copyright (C) 2009 by William Greiman
  25. *
  26. * This file is part of the Arduino Sd2Card Library
  27. */
  28. #include "Marlin.h"
  29. #if ENABLED(SDSUPPORT)
  30. #ifndef Sd2Card_h
  31. #define Sd2Card_h
  32. /**
  33. * \file
  34. * \brief Sd2Card class for V2 SD/SDHC cards
  35. */
  36. #include "SdFatConfig.h"
  37. #include "SdInfo.h"
  38. //------------------------------------------------------------------------------
  39. /** init timeout ms */
  40. uint16_t const SD_INIT_TIMEOUT = 2000;
  41. /** erase timeout ms */
  42. uint16_t const SD_ERASE_TIMEOUT = 10000;
  43. /** read timeout ms */
  44. uint16_t const SD_READ_TIMEOUT = 300;
  45. /** write time out ms */
  46. uint16_t const SD_WRITE_TIMEOUT = 600;
  47. //------------------------------------------------------------------------------
  48. // SD card errors
  49. /** timeout error for command CMD0 (initialize card in SPI mode) */
  50. uint8_t const SD_CARD_ERROR_CMD0 = 0X1;
  51. /** CMD8 was not accepted - not a valid SD card*/
  52. uint8_t const SD_CARD_ERROR_CMD8 = 0X2;
  53. /** card returned an error response for CMD12 (write stop) */
  54. uint8_t const SD_CARD_ERROR_CMD12 = 0X3;
  55. /** card returned an error response for CMD17 (read block) */
  56. uint8_t const SD_CARD_ERROR_CMD17 = 0X4;
  57. /** card returned an error response for CMD18 (read multiple block) */
  58. uint8_t const SD_CARD_ERROR_CMD18 = 0X5;
  59. /** card returned an error response for CMD24 (write block) */
  60. uint8_t const SD_CARD_ERROR_CMD24 = 0X6;
  61. /** WRITE_MULTIPLE_BLOCKS command failed */
  62. uint8_t const SD_CARD_ERROR_CMD25 = 0X7;
  63. /** card returned an error response for CMD58 (read OCR) */
  64. uint8_t const SD_CARD_ERROR_CMD58 = 0X8;
  65. /** SET_WR_BLK_ERASE_COUNT failed */
  66. uint8_t const SD_CARD_ERROR_ACMD23 = 0X9;
  67. /** ACMD41 initialization process timeout */
  68. uint8_t const SD_CARD_ERROR_ACMD41 = 0XA;
  69. /** card returned a bad CSR version field */
  70. uint8_t const SD_CARD_ERROR_BAD_CSD = 0XB;
  71. /** erase block group command failed */
  72. uint8_t const SD_CARD_ERROR_ERASE = 0XC;
  73. /** card not capable of single block erase */
  74. uint8_t const SD_CARD_ERROR_ERASE_SINGLE_BLOCK = 0XD;
  75. /** Erase sequence timed out */
  76. uint8_t const SD_CARD_ERROR_ERASE_TIMEOUT = 0XE;
  77. /** card returned an error token instead of read data */
  78. uint8_t const SD_CARD_ERROR_READ = 0XF;
  79. /** read CID or CSD failed */
  80. uint8_t const SD_CARD_ERROR_READ_REG = 0x10;
  81. /** timeout while waiting for start of read data */
  82. uint8_t const SD_CARD_ERROR_READ_TIMEOUT = 0x11;
  83. /** card did not accept STOP_TRAN_TOKEN */
  84. uint8_t const SD_CARD_ERROR_STOP_TRAN = 0x12;
  85. /** card returned an error token as a response to a write operation */
  86. uint8_t const SD_CARD_ERROR_WRITE = 0x13;
  87. /** attempt to write protected block zero */
  88. uint8_t const SD_CARD_ERROR_WRITE_BLOCK_ZERO = 0x14; // REMOVE - not used
  89. /** card did not go ready for a multiple block write */
  90. uint8_t const SD_CARD_ERROR_WRITE_MULTIPLE = 0x15;
  91. /** card returned an error to a CMD13 status check after a write */
  92. uint8_t const SD_CARD_ERROR_WRITE_PROGRAMMING = 0x16;
  93. /** timeout occurred during write programming */
  94. uint8_t const SD_CARD_ERROR_WRITE_TIMEOUT = 0x17;
  95. /** incorrect rate selected */
  96. uint8_t const SD_CARD_ERROR_SCK_RATE = 0x18;
  97. /** init() not called */
  98. uint8_t const SD_CARD_ERROR_INIT_NOT_CALLED = 0x19;
  99. /** crc check error */
  100. uint8_t const SD_CARD_ERROR_CRC = 0x20;
  101. //------------------------------------------------------------------------------
  102. // card types
  103. /** Standard capacity V1 SD card */
  104. uint8_t const SD_CARD_TYPE_SD1 = 1;
  105. /** Standard capacity V2 SD card */
  106. uint8_t const SD_CARD_TYPE_SD2 = 2;
  107. /** High Capacity SD card */
  108. uint8_t const SD_CARD_TYPE_SDHC = 3;
  109. /**
  110. * define SOFTWARE_SPI to use bit-bang SPI
  111. */
  112. //------------------------------------------------------------------------------
  113. #if MEGA_SOFT_SPI
  114. #define SOFTWARE_SPI
  115. #elif USE_SOFTWARE_SPI
  116. #define SOFTWARE_SPI
  117. #endif // MEGA_SOFT_SPI
  118. //------------------------------------------------------------------------------
  119. // SPI pin definitions - do not edit here - change in SdFatConfig.h
  120. //
  121. #define SD_CHIP_SELECT_PIN SS_PIN
  122. #if 0
  123. #if DISABLED(SOFTWARE_SPI)
  124. // hardware pin defs
  125. /** The default chip select pin for the SD card is SS. */
  126. #define SD_CHIP_SELECT_PIN SS_PIN
  127. // The following three pins must not be redefined for hardware SPI.
  128. /** SPI Master Out Slave In pin */
  129. #define SPI_MOSI_PIN MOSI_PIN
  130. /** SPI Master In Slave Out pin */
  131. #define SPI_MISO_PIN MISO_PIN
  132. /** SPI Clock pin */
  133. #define SPI_SCK_PIN SCK_PIN
  134. #else // SOFTWARE_SPI
  135. /** SPI chip select pin */
  136. #define SD_CHIP_SELECT_PIN SOFT_SPI_CS_PIN
  137. /** SPI Master Out Slave In pin */
  138. #define SPI_MOSI_PIN SOFT_SPI_MOSI_PIN
  139. /** SPI Master In Slave Out pin */
  140. #define SPI_MISO_PIN SOFT_SPI_MISO_PIN
  141. /** SPI Clock pin */
  142. #define SPI_SCK_PIN SOFT_SPI_SCK_PIN
  143. #endif // SOFTWARE_SPI
  144. #endif
  145. //------------------------------------------------------------------------------
  146. /**
  147. * \class Sd2Card
  148. * \brief Raw access to SD and SDHC flash memory cards.
  149. */
  150. class Sd2Card {
  151. public:
  152. /** Construct an instance of Sd2Card. */
  153. Sd2Card() : errorCode_(SD_CARD_ERROR_INIT_NOT_CALLED), type_(0) {}
  154. uint32_t cardSize();
  155. bool erase(uint32_t firstBlock, uint32_t lastBlock);
  156. bool eraseSingleBlockEnable();
  157. /**
  158. * Set SD error code.
  159. * \param[in] code value for error code.
  160. */
  161. void error(uint8_t code) {errorCode_ = code;}
  162. /**
  163. * \return error code for last error. See Sd2Card.h for a list of error codes.
  164. */
  165. int errorCode() const {return errorCode_;}
  166. /** \return error data for last error. */
  167. int errorData() const {return status_;}
  168. /**
  169. * Initialize an SD flash memory card with default clock rate and chip
  170. * select pin. See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin).
  171. *
  172. * \return true for success or false for failure.
  173. */
  174. bool init(uint8_t sckRateID = SPI_FULL_SPEED,
  175. uint8_t chipSelectPin = SD_CHIP_SELECT_PIN);
  176. bool readBlock(uint32_t block, uint8_t* dst);
  177. /**
  178. * Read a card's CID register. The CID contains card identification
  179. * information such as Manufacturer ID, Product name, Product serial
  180. * number and Manufacturing date.
  181. *
  182. * \param[out] cid pointer to area for returned data.
  183. *
  184. * \return true for success or false for failure.
  185. */
  186. bool readCID(cid_t* cid) {
  187. return readRegister(CMD10, cid);
  188. }
  189. /**
  190. * Read a card's CSD register. The CSD contains Card-Specific Data that
  191. * provides information regarding access to the card's contents.
  192. *
  193. * \param[out] csd pointer to area for returned data.
  194. *
  195. * \return true for success or false for failure.
  196. */
  197. bool readCSD(csd_t* csd) {
  198. return readRegister(CMD9, csd);
  199. }
  200. bool readData(uint8_t* dst);
  201. bool readStart(uint32_t blockNumber);
  202. bool readStop();
  203. bool setSckRate(uint8_t sckRateID);
  204. /** Return the card type: SD V1, SD V2 or SDHC
  205. * \return 0 - SD V1, 1 - SD V2, or 3 - SDHC.
  206. */
  207. int type() const {return type_;}
  208. bool writeBlock(uint32_t blockNumber, const uint8_t* src);
  209. bool writeData(const uint8_t* src);
  210. bool writeStart(uint32_t blockNumber, uint32_t eraseCount);
  211. bool writeStop();
  212. private:
  213. //----------------------------------------------------------------------------
  214. uint8_t chipSelectPin_;
  215. uint8_t errorCode_;
  216. uint8_t spiRate_;
  217. uint8_t status_;
  218. uint8_t type_;
  219. // private functions
  220. uint8_t cardAcmd(uint8_t cmd, uint32_t arg) {
  221. cardCommand(CMD55, 0);
  222. return cardCommand(cmd, arg);
  223. }
  224. uint8_t cardCommand(uint8_t cmd, uint32_t arg);
  225. bool readData(uint8_t* dst, uint16_t count);
  226. bool readRegister(uint8_t cmd, void* buf);
  227. void chipSelectHigh();
  228. void chipSelectLow();
  229. void type(uint8_t value) {type_ = value;}
  230. bool waitNotBusy(uint16_t timeoutMillis);
  231. bool writeData(uint8_t token, const uint8_t* src);
  232. };
  233. #endif // Sd2Card_h
  234. #endif