My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Sd2Card.h 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. #ifndef Sd2Card_h
  21. #define Sd2Card_h
  22. /**
  23. * \file
  24. * Sd2Card class
  25. */
  26. #include "Sd2PinMap.h"
  27. #include "SdInfo.h"
  28. /** Set SCK to max rate of F_CPU/2. See Sd2Card::setSckRate(). */
  29. uint8_t const SPI_FULL_SPEED = 0;
  30. /** Set SCK rate to F_CPU/4. See Sd2Card::setSckRate(). */
  31. uint8_t const SPI_HALF_SPEED = 1;
  32. /** Set SCK rate to F_CPU/8. Sd2Card::setSckRate(). */
  33. uint8_t const SPI_QUARTER_SPEED = 2;
  34. /**
  35. * Define MEGA_SOFT_SPI non-zero to use software SPI on Mega Arduinos.
  36. * Pins used are SS 10, MOSI 11, MISO 12, and SCK 13.
  37. *
  38. * MEGA_SOFT_SPI allows an unmodified Adafruit GPS Shield to be used
  39. * on Mega Arduinos. Software SPI works well with GPS Shield V1.1
  40. * but many SD cards will fail with GPS Shield V1.0.
  41. */
  42. #define MEGA_SOFT_SPI 0
  43. //------------------------------------------------------------------------------
  44. #if MEGA_SOFT_SPI && (defined(__AVR_ATmega1280__)||defined(__AVR_ATmega2560__))
  45. #define SOFTWARE_SPI
  46. #endif // MEGA_SOFT_SPI
  47. //------------------------------------------------------------------------------
  48. // SPI pin definitions
  49. //
  50. #ifndef SOFTWARE_SPI
  51. // hardware pin defs
  52. /**
  53. * SD Chip Select pin
  54. *
  55. * Warning if this pin is redefined the hardware SS will pin will be enabled
  56. * as an output by init(). An avr processor will not function as an SPI
  57. * master unless SS is set to output mode.
  58. */
  59. /** The default chip select pin for the SD card is SS. */
  60. uint8_t const SD_CHIP_SELECT_PIN = SS_PIN;
  61. // The following three pins must not be redefined for hardware SPI.
  62. /** SPI Master Out Slave In pin */
  63. uint8_t const SPI_MOSI_PIN = MOSI_PIN;
  64. /** SPI Master In Slave Out pin */
  65. uint8_t const SPI_MISO_PIN = MISO_PIN;
  66. /** SPI Clock pin */
  67. uint8_t const SPI_SCK_PIN = SCK_PIN;
  68. /** optimize loops for hardware SPI */
  69. #define OPTIMIZE_HARDWARE_SPI
  70. #else // SOFTWARE_SPI
  71. // define software SPI pins so Mega can use unmodified GPS Shield
  72. /** SPI chip select pin */
  73. uint8_t const SD_CHIP_SELECT_PIN = 10;
  74. /** SPI Master Out Slave In pin */
  75. uint8_t const SPI_MOSI_PIN = 11;
  76. /** SPI Master In Slave Out pin */
  77. uint8_t const SPI_MISO_PIN = 12;
  78. /** SPI Clock pin */
  79. uint8_t const SPI_SCK_PIN = 13;
  80. #endif // SOFTWARE_SPI
  81. //------------------------------------------------------------------------------
  82. /** Protect block zero from write if nonzero */
  83. #define SD_PROTECT_BLOCK_ZERO 1
  84. /** init timeout ms */
  85. uint16_t const SD_INIT_TIMEOUT = 2000;
  86. /** erase timeout ms */
  87. uint16_t const SD_ERASE_TIMEOUT = 10000;
  88. /** read timeout ms */
  89. uint16_t const SD_READ_TIMEOUT = 300;
  90. /** write time out ms */
  91. uint16_t const SD_WRITE_TIMEOUT = 600;
  92. //------------------------------------------------------------------------------
  93. // SD card errors
  94. /** timeout error for command CMD0 */
  95. uint8_t const SD_CARD_ERROR_CMD0 = 0X1;
  96. /** CMD8 was not accepted - not a valid SD card*/
  97. uint8_t const SD_CARD_ERROR_CMD8 = 0X2;
  98. /** card returned an error response for CMD17 (read block) */
  99. uint8_t const SD_CARD_ERROR_CMD17 = 0X3;
  100. /** card returned an error response for CMD24 (write block) */
  101. uint8_t const SD_CARD_ERROR_CMD24 = 0X4;
  102. /** WRITE_MULTIPLE_BLOCKS command failed */
  103. uint8_t const SD_CARD_ERROR_CMD25 = 0X05;
  104. /** card returned an error response for CMD58 (read OCR) */
  105. uint8_t const SD_CARD_ERROR_CMD58 = 0X06;
  106. /** SET_WR_BLK_ERASE_COUNT failed */
  107. uint8_t const SD_CARD_ERROR_ACMD23 = 0X07;
  108. /** card's ACMD41 initialization process timeout */
  109. uint8_t const SD_CARD_ERROR_ACMD41 = 0X08;
  110. /** card returned a bad CSR version field */
  111. uint8_t const SD_CARD_ERROR_BAD_CSD = 0X09;
  112. /** erase block group command failed */
  113. uint8_t const SD_CARD_ERROR_ERASE = 0X0A;
  114. /** card not capable of single block erase */
  115. uint8_t const SD_CARD_ERROR_ERASE_SINGLE_BLOCK = 0X0B;
  116. /** Erase sequence timed out */
  117. uint8_t const SD_CARD_ERROR_ERASE_TIMEOUT = 0X0C;
  118. /** card returned an error token instead of read data */
  119. uint8_t const SD_CARD_ERROR_READ = 0X0D;
  120. /** read CID or CSD failed */
  121. uint8_t const SD_CARD_ERROR_READ_REG = 0X0E;
  122. /** timeout while waiting for start of read data */
  123. uint8_t const SD_CARD_ERROR_READ_TIMEOUT = 0X0F;
  124. /** card did not accept STOP_TRAN_TOKEN */
  125. uint8_t const SD_CARD_ERROR_STOP_TRAN = 0X10;
  126. /** card returned an error token as a response to a write operation */
  127. uint8_t const SD_CARD_ERROR_WRITE = 0X11;
  128. /** attempt to write protected block zero */
  129. uint8_t const SD_CARD_ERROR_WRITE_BLOCK_ZERO = 0X12;
  130. /** card did not go ready for a multiple block write */
  131. uint8_t const SD_CARD_ERROR_WRITE_MULTIPLE = 0X13;
  132. /** card returned an error to a CMD13 status check after a write */
  133. uint8_t const SD_CARD_ERROR_WRITE_PROGRAMMING = 0X14;
  134. /** timeout occurred during write programming */
  135. uint8_t const SD_CARD_ERROR_WRITE_TIMEOUT = 0X15;
  136. /** incorrect rate selected */
  137. uint8_t const SD_CARD_ERROR_SCK_RATE = 0X16;
  138. //------------------------------------------------------------------------------
  139. // card types
  140. /** Standard capacity V1 SD card */
  141. uint8_t const SD_CARD_TYPE_SD1 = 1;
  142. /** Standard capacity V2 SD card */
  143. uint8_t const SD_CARD_TYPE_SD2 = 2;
  144. /** High Capacity SD card */
  145. uint8_t const SD_CARD_TYPE_SDHC = 3;
  146. //------------------------------------------------------------------------------
  147. /**
  148. * \class Sd2Card
  149. * \brief Raw access to SD and SDHC flash memory cards.
  150. */
  151. class Sd2Card {
  152. public:
  153. /** Construct an instance of Sd2Card. */
  154. Sd2Card(void) : errorCode_(0), inBlock_(0), partialBlockRead_(0), type_(0) {}
  155. uint32_t cardSize(void);
  156. uint8_t erase(uint32_t firstBlock, uint32_t lastBlock);
  157. uint8_t eraseSingleBlockEnable(void);
  158. /**
  159. * \return error code for last error. See Sd2Card.h for a list of error codes.
  160. */
  161. uint8_t errorCode(void) const {return errorCode_;}
  162. /** \return error data for last error. */
  163. uint8_t errorData(void) const {return status_;}
  164. /**
  165. * Initialize an SD flash memory card with default clock rate and chip
  166. * select pin. See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin).
  167. */
  168. uint8_t init(void) {
  169. return init(SPI_FULL_SPEED, SD_CHIP_SELECT_PIN);
  170. }
  171. /**
  172. * Initialize an SD flash memory card with the selected SPI clock rate
  173. * and the default SD chip select pin.
  174. * See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin).
  175. */
  176. uint8_t init(uint8_t sckRateID) {
  177. return init(sckRateID, SD_CHIP_SELECT_PIN);
  178. }
  179. uint8_t init(uint8_t sckRateID, uint8_t chipSelectPin);
  180. void partialBlockRead(uint8_t value);
  181. /** Returns the current value, true or false, for partial block read. */
  182. uint8_t partialBlockRead(void) const {return partialBlockRead_;}
  183. uint8_t readBlock(uint32_t block, uint8_t* dst);
  184. uint8_t readData(uint32_t block,
  185. uint16_t offset, uint16_t count, uint8_t* dst);
  186. /**
  187. * Read a cards CID register. The CID contains card identification
  188. * information such as Manufacturer ID, Product name, Product serial
  189. * number and Manufacturing date. */
  190. uint8_t readCID(cid_t* cid) {
  191. return readRegister(CMD10, cid);
  192. }
  193. /**
  194. * Read a cards CSD register. The CSD contains Card-Specific Data that
  195. * provides information regarding access to the card's contents. */
  196. uint8_t readCSD(csd_t* csd) {
  197. return readRegister(CMD9, csd);
  198. }
  199. void readEnd(void);
  200. uint8_t setSckRate(uint8_t sckRateID);
  201. /** Return the card type: SD V1, SD V2 or SDHC */
  202. uint8_t type(void) const {return type_;}
  203. uint8_t writeBlock(uint32_t blockNumber, const uint8_t* src);
  204. uint8_t writeData(const uint8_t* src);
  205. uint8_t writeStart(uint32_t blockNumber, uint32_t eraseCount);
  206. uint8_t writeStop(void);
  207. private:
  208. uint32_t block_;
  209. uint8_t chipSelectPin_;
  210. uint8_t errorCode_;
  211. uint8_t inBlock_;
  212. uint16_t offset_;
  213. uint8_t partialBlockRead_;
  214. uint8_t status_;
  215. uint8_t type_;
  216. // private functions
  217. uint8_t cardAcmd(uint8_t cmd, uint32_t arg) {
  218. cardCommand(CMD55, 0);
  219. return cardCommand(cmd, arg);
  220. }
  221. uint8_t cardCommand(uint8_t cmd, uint32_t arg);
  222. void error(uint8_t code) {errorCode_ = code;}
  223. uint8_t readRegister(uint8_t cmd, void* buf);
  224. uint8_t sendWriteCommand(uint32_t blockNumber, uint32_t eraseCount);
  225. void chipSelectHigh(void);
  226. void chipSelectLow(void);
  227. void type(uint8_t value) {type_ = value;}
  228. uint8_t waitNotBusy(uint16_t timeoutMillis);
  229. uint8_t writeData(uint8_t token, const uint8_t* src);
  230. uint8_t waitStartBlock(void);
  231. };
  232. #endif // Sd2Card_h