Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

196 lines
7.6KB

  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), type_(0) {}
  130. /**
  131. * \return error code for last error. See Sd2Card.h for a list of error codes.
  132. */
  133. uint8_t errorCode(void) const {return errorCode_;}
  134. /** \return error data for last error. */
  135. uint8_t errorData(void) const {return status_;}
  136. /**
  137. * Initialize an SD flash memory card with default clock rate and chip
  138. * select pin. See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin).
  139. */
  140. uint8_t init(void) {
  141. return init(SPI_FULL_SPEED, SD_CHIP_SELECT_PIN);
  142. }
  143. /**
  144. * Initialize an SD flash memory card with the selected SPI clock rate
  145. * and the default SD chip select pin.
  146. * See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin).
  147. */
  148. uint8_t init(uint8_t sckRateID) {
  149. return init(sckRateID, SD_CHIP_SELECT_PIN);
  150. }
  151. uint8_t init(uint8_t sckRateID, uint8_t chipSelectPin);
  152. /** Returns the current value, true or false, for partial block read. */
  153. uint8_t readBlock(uint32_t block, uint8_t* dst);
  154. /**
  155. * Read a cards CID register. The CID contains card identification
  156. * information such as Manufacturer ID, Product name, Product serial
  157. * number and Manufacturing date. */
  158. uint8_t readCID(cid_t* cid) {
  159. return readRegister(CMD10, cid);
  160. }
  161. /**
  162. * Read a cards CSD register. The CSD contains Card-Specific Data that
  163. * provides information regarding access to the card's contents. */
  164. uint8_t readCSD(csd_t* csd) {
  165. return readRegister(CMD9, csd);
  166. }
  167. uint8_t setSckRate(uint8_t sckRateID);
  168. /** Return the card type: SD V1, SD V2 or SDHC */
  169. uint8_t type(void) const {return type_;}
  170. uint8_t writeBlock(uint32_t blockNumber, const uint8_t* src);
  171. private:
  172. uint8_t chipSelectPin_;
  173. uint8_t errorCode_;
  174. uint8_t status_;
  175. uint8_t type_;
  176. // private functions
  177. uint8_t cardAcmd(uint8_t cmd, uint32_t arg) {
  178. cardCommand(CMD55, 0);
  179. return cardCommand(cmd, arg);
  180. }
  181. uint8_t cardCommand(uint8_t cmd, uint32_t arg);
  182. void error(uint8_t code) {errorCode_ = code;}
  183. uint8_t readRegister(uint8_t cmd, void* buf);
  184. uint8_t sendWriteCommand(uint32_t blockNumber, uint32_t eraseCount);
  185. void chipSelectHigh(void);
  186. void chipSelectLow(void);
  187. void type(uint8_t value) {type_ = value;}
  188. uint8_t waitNotBusy(uint16_t timeoutMillis);
  189. uint8_t writeData(uint8_t token, const uint8_t* src);
  190. uint8_t waitStartBlock(void);
  191. uint8_t readData(uint32_t block,
  192. uint16_t offset, uint16_t count, uint8_t* dst);
  193. };
  194. #endif // Sd2Card_h