Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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