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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* Arduino SdSpiCard Library
  2. * Copyright (C) 2012 by William Greiman
  3. *
  4. * This file is part of the Arduino SdSpiCard 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 SdSpiCard Library. If not, see
  18. * <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef SpiCard_h
  21. #define SpiCard_h
  22. /**
  23. * \file
  24. * \brief SdSpiCard class for V2 SD/SDHC cards
  25. */
  26. #include <Arduino.h>
  27. #include <SdFatConfig.h>
  28. #include <SdInfo.h>
  29. #include <SdSpi.h>
  30. //==============================================================================
  31. /**
  32. * \class SdSpiCard
  33. * \brief Raw access to SD and SDHC flash memory cards via SPI protocol.
  34. */
  35. class SdSpiCard {
  36. public:
  37. /** typedef for SPI class. */
  38. #if USE_MULTIPLE_SPI_TYPES
  39. typedef SdSpiBase m_spi_t;
  40. #else // USE_MULTIPLE_SPI_TYPES
  41. typedef SpiDefault_t m_spi_t;
  42. #endif // USE_MULTIPLE_SPI_TYPES
  43. /** Construct an instance of SdSpiCard. */
  44. SdSpiCard() : m_errorCode(SD_CARD_ERROR_INIT_NOT_CALLED), m_type(0) {}
  45. /** Initialize the SD card.
  46. * \param[in] spi SPI object.
  47. * \param[in] chipSelectPin SD chip select pin.
  48. * \param[in] sckDivisor SPI clock divisor.
  49. * \return true for success else false.
  50. */
  51. bool begin(m_spi_t* spi, uint8_t chipSelectPin = SS,
  52. uint8_t sckDivisor = SPI_FULL_SPEED);
  53. /**
  54. * Determine the size of an SD flash memory card.
  55. *
  56. * \return The number of 512 byte data blocks in the card
  57. * or zero if an error occurs.
  58. */
  59. uint32_t cardSize();
  60. /** Erase a range of blocks.
  61. *
  62. * \param[in] firstBlock The address of the first block in the range.
  63. * \param[in] lastBlock The address of the last block in the range.
  64. *
  65. * \note This function requests the SD card to do a flash erase for a
  66. * range of blocks. The data on the card after an erase operation is
  67. * either 0 or 1, depends on the card vendor. The card must support
  68. * single block erase.
  69. *
  70. * \return The value one, true, is returned for success and
  71. * the value zero, false, is returned for failure.
  72. */
  73. bool erase(uint32_t firstBlock, uint32_t lastBlock);
  74. /** Determine if card supports single block erase.
  75. *
  76. * \return true is returned if single block erase is supported.
  77. * false is returned if single block erase is not supported.
  78. */
  79. bool eraseSingleBlockEnable();
  80. /**
  81. * Set SD error code.
  82. * \param[in] code value for error code.
  83. */
  84. void error(uint8_t code) {m_errorCode = code;}
  85. /**
  86. * \return code for the last error. See SdSpiCard.h for a list of error codes.
  87. */
  88. int errorCode() const {return m_errorCode;}
  89. /** \return error data for last error. */
  90. int errorData() const {return m_status;}
  91. /**
  92. * Check for busy. MISO low indicates the card is busy.
  93. *
  94. * \return true if busy else false.
  95. */
  96. bool isBusy();
  97. /**
  98. * Read a 512 byte block from an SD card.
  99. *
  100. * \param[in] block Logical block to be read.
  101. * \param[out] dst Pointer to the location that will receive the data.
  102. * \return The value one, true, is returned for success and
  103. * the value zero, false, is returned for failure.
  104. */
  105. bool readBlock(uint32_t block, uint8_t* dst);
  106. /**
  107. * Read multiple 512 byte blocks from an SD card.
  108. *
  109. * \param[in] block Logical block to be read.
  110. * \param[in] count Number of blocks to be read.
  111. * \param[out] dst Pointer to the location that will receive the data.
  112. * \return The value one, true, is returned for success and
  113. * the value zero, false, is returned for failure.
  114. */
  115. bool readBlocks(uint32_t block, uint8_t* dst, size_t count);
  116. /**
  117. * Read a card's CID register. The CID contains card identification
  118. * information such as Manufacturer ID, Product name, Product serial
  119. * number and Manufacturing date.
  120. *
  121. * \param[out] cid pointer to area for returned data.
  122. *
  123. * \return true for success or false for failure.
  124. */
  125. bool readCID(cid_t* cid) {
  126. return readRegister(CMD10, cid);
  127. }
  128. /**
  129. * Read a card's CSD register. The CSD contains Card-Specific Data that
  130. * provides information regarding access to the card's contents.
  131. *
  132. * \param[out] csd pointer to area for returned data.
  133. *
  134. * \return true for success or false for failure.
  135. */
  136. bool readCSD(csd_t* csd) {
  137. return readRegister(CMD9, csd);
  138. }
  139. /** Read one data block in a multiple block read sequence
  140. *
  141. * \param[out] dst Pointer to the location for the data to be read.
  142. *
  143. * \return The value one, true, is returned for success and
  144. * the value zero, false, is returned for failure.
  145. */
  146. bool readData(uint8_t *dst);
  147. /** Read OCR register.
  148. *
  149. * \param[out] ocr Value of OCR register.
  150. * \return true for success else false.
  151. */
  152. bool readOCR(uint32_t* ocr);
  153. /** Start a read multiple blocks sequence.
  154. *
  155. * \param[in] blockNumber Address of first block in sequence.
  156. *
  157. * \note This function is used with readData() and readStop() for optimized
  158. * multiple block reads. SPI chipSelect must be low for the entire sequence.
  159. *
  160. * \return The value one, true, is returned for success and
  161. * the value zero, false, is returned for failure.
  162. */
  163. bool readStart(uint32_t blockNumber);
  164. /** End a read multiple blocks sequence.
  165. *
  166. * \return The value one, true, is returned for success and
  167. * the value zero, false, is returned for failure.
  168. */
  169. bool readStop();
  170. /** Return SCK divisor.
  171. *
  172. * \return Requested SCK divisor.
  173. */
  174. uint8_t sckDivisor() {return m_sckDivisor;}
  175. /** Return the card type: SD V1, SD V2 or SDHC
  176. * \return 0 - SD V1, 1 - SD V2, or 3 - SDHC.
  177. */
  178. int type() const {return m_type;}
  179. /**
  180. * Writes a 512 byte block to an SD card.
  181. *
  182. * \param[in] blockNumber Logical block to be written.
  183. * \param[in] src Pointer to the location of the data to be written.
  184. * \return The value one, true, is returned for success and
  185. * the value zero, false, is returned for failure.
  186. */
  187. bool writeBlock(uint32_t blockNumber, const uint8_t* src);
  188. /**
  189. * Write multiple 512 byte blocks to an SD card.
  190. *
  191. * \param[in] block Logical block to be written.
  192. * \param[in] count Number of blocks to be written.
  193. * \param[in] src Pointer to the location of the data to be written.
  194. * \return The value one, true, is returned for success and
  195. * the value zero, false, is returned for failure.
  196. */
  197. bool writeBlocks(uint32_t block, const uint8_t* src, size_t count);
  198. /** Write one data block in a multiple block write sequence
  199. * \param[in] src Pointer to the location of the data to be written.
  200. * \return The value one, true, is returned for success and
  201. * the value zero, false, is returned for failure.
  202. */
  203. bool writeData(const uint8_t* src);
  204. /** Start a write multiple blocks sequence.
  205. *
  206. * \param[in] blockNumber Address of first block in sequence.
  207. * \param[in] eraseCount The number of blocks to be pre-erased.
  208. *
  209. * \note This function is used with writeData() and writeStop()
  210. * for optimized multiple block writes.
  211. *
  212. * \return The value one, true, is returned for success and
  213. * the value zero, false, is returned for failure.
  214. */
  215. bool writeStart(uint32_t blockNumber, uint32_t eraseCount);
  216. /** End a write multiple blocks sequence.
  217. *
  218. * \return The value one, true, is returned for success and
  219. * the value zero, false, is returned for failure.
  220. */
  221. bool writeStop();
  222. private:
  223. // private functions
  224. uint8_t cardAcmd(uint8_t cmd, uint32_t arg) {
  225. cardCommand(CMD55, 0);
  226. return cardCommand(cmd, arg);
  227. }
  228. uint8_t cardCommand(uint8_t cmd, uint32_t arg);
  229. bool readData(uint8_t* dst, size_t count);
  230. bool readRegister(uint8_t cmd, void* buf);
  231. void chipSelectHigh();
  232. void chipSelectLow();
  233. void spiYield();
  234. void type(uint8_t value) {m_type = value;}
  235. bool waitNotBusy(uint16_t timeoutMillis);
  236. bool writeData(uint8_t token, const uint8_t* src);
  237. void spiBegin() {m_spi->begin();}
  238. void spiInit(uint8_t spiDivisor) {m_spi->init(spiDivisor);}
  239. uint8_t spiReceive() {return m_spi->receive();}
  240. uint8_t spiReceive(uint8_t* buf, size_t n) {return m_spi->receive(buf, n);}
  241. void spiSend(uint8_t data) {m_spi->send(data);}
  242. void spiSend(const uint8_t* buf, size_t n) {m_spi->send(buf, n);}
  243. bool useSpiTransactions() {return m_spi->useSpiTransactions();}
  244. m_spi_t* m_spi;
  245. uint8_t m_chipSelectPin;
  246. uint8_t m_errorCode;
  247. uint8_t m_sckDivisor;
  248. uint8_t m_status;
  249. uint8_t m_type;
  250. };
  251. //==============================================================================
  252. /**
  253. * \class Sd2Card
  254. * \brief Raw access to SD and SDHC card using default SPI library.
  255. */
  256. class Sd2Card : public SdSpiCard {
  257. public:
  258. /** Initialize the SD card.
  259. * \param[in] chipSelectPin SD chip select pin.
  260. * \param[in] sckDivisor SPI clock divisor.
  261. * \return true for success else false.
  262. */
  263. bool begin(uint8_t chipSelectPin = SS, uint8_t sckDivisor = 2) {
  264. return SdSpiCard::begin(&m_spi, chipSelectPin, sckDivisor);
  265. }
  266. /** Initialize the SD card. Obsolete form.
  267. * \param[in] chipSelectPin SD chip select pin.
  268. * \param[in] sckDivisor SPI clock divisor.
  269. * \return true for success else false.
  270. */
  271. bool init(uint8_t sckDivisor = 2, uint8_t chipSelectPin = SS) {
  272. return begin(chipSelectPin, sckDivisor);
  273. }
  274. private:
  275. bool begin(m_spi_t* spi, uint8_t chipSelectPin = SS,
  276. uint8_t sckDivisor = SPI_FULL_SPEED) {return false;}
  277. SpiDefault_t m_spi;
  278. };
  279. #endif // SpiCard_h