SdSpiCard.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /**
  2. * Copyright (c) 20011-2017 Bill Greiman
  3. * This file is part of the SdFat library for SD memory cards.
  4. *
  5. * MIT License
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included
  15. * in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef SdSpiCard_h
  26. #define SdSpiCard_h
  27. /**
  28. * \file
  29. * \brief SdSpiCard class for V2 SD/SDHC cards
  30. */
  31. #include <stddef.h>
  32. #include "SysCall.h"
  33. #include "SdInfo.h"
  34. #include "../FatLib/BaseBlockDriver.h"
  35. #include "../SpiDriver/SdSpiDriver.h"
  36. //==============================================================================
  37. /**
  38. * \class SdSpiCard
  39. * \brief Raw access to SD and SDHC flash memory cards via SPI protocol.
  40. */
  41. #if ENABLE_EXTENDED_TRANSFER_CLASS || ENABLE_SDIO_CLASS
  42. class SdSpiCard : public BaseBlockDriver {
  43. #else // ENABLE_EXTENDED_TRANSFER_CLASS || ENABLE_SDIO_CLASS
  44. class SdSpiCard {
  45. #endif // ENABLE_EXTENDED_TRANSFER_CLASS || ENABLE_SDIO_CLASS
  46. public:
  47. /** Construct an instance of SdSpiCard. */
  48. SdSpiCard() : m_errorCode(SD_CARD_ERROR_INIT_NOT_CALLED), m_type(0) {}
  49. /** Initialize the SD card.
  50. * \param[in] spi SPI driver for card.
  51. * \param[in] csPin card chip select pin.
  52. * \param[in] spiSettings SPI speed, mode, and bit order.
  53. * \return true for success else false.
  54. */
  55. bool begin(SdSpiDriver* spi, uint8_t csPin, SPISettings spiSettings);
  56. /**
  57. * Determine the size of an SD flash memory card.
  58. *
  59. * \return The number of 512 byte data blocks in the card
  60. * or zero if an error occurs.
  61. */
  62. uint32_t cardSize();
  63. /** Erase a range of blocks.
  64. *
  65. * \param[in] firstBlock The address of the first block in the range.
  66. * \param[in] lastBlock The address of the last block in the range.
  67. *
  68. * \note This function requests the SD card to do a flash erase for a
  69. * range of blocks. The data on the card after an erase operation is
  70. * either 0 or 1, depends on the card vendor. The card must support
  71. * single block erase.
  72. *
  73. * \return The value true is returned for success and
  74. * the value false is returned for failure.
  75. */
  76. bool erase(uint32_t firstBlock, uint32_t lastBlock);
  77. /** Determine if card supports single block erase.
  78. *
  79. * \return true is returned if single block erase is supported.
  80. * false is returned if single block erase is not supported.
  81. */
  82. bool eraseSingleBlockEnable();
  83. /**
  84. * Set SD error code.
  85. * \param[in] code value for error code.
  86. */
  87. void error(uint8_t code) {
  88. m_errorCode = code;
  89. }
  90. /**
  91. * \return code for the last error. See SdInfo.h for a list of error codes.
  92. */
  93. int errorCode() const {
  94. return m_errorCode;
  95. }
  96. /** \return error data for last error. */
  97. int errorData() const {
  98. return m_status;
  99. }
  100. /**
  101. * Check for busy. MISO low indicates the card is busy.
  102. *
  103. * \return true if busy else false.
  104. */
  105. bool isBusy();
  106. /**
  107. * Read a 512 byte block from an SD card.
  108. *
  109. * \param[in] lba Logical block to be read.
  110. * \param[out] dst Pointer to the location that will receive the data.
  111. * \return The value true is returned for success and
  112. * the value false is returned for failure.
  113. */
  114. bool readBlock(uint32_t lba, uint8_t* dst);
  115. /**
  116. * Read multiple 512 byte blocks from an SD card.
  117. *
  118. * \param[in] lba Logical block to be read.
  119. * \param[in] nb Number of blocks to be read.
  120. * \param[out] dst Pointer to the location that will receive the data.
  121. * \return The value true is returned for success and
  122. * the value false is returned for failure.
  123. */
  124. bool readBlocks(uint32_t lba, uint8_t* dst, size_t nb);
  125. /**
  126. * Read a card's CID register. The CID contains card identification
  127. * information such as Manufacturer ID, Product name, Product serial
  128. * number and Manufacturing date.
  129. *
  130. * \param[out] cid pointer to area for returned data.
  131. *
  132. * \return true for success or false for failure.
  133. */
  134. bool readCID(cid_t* cid) {
  135. return readRegister(CMD10, cid);
  136. }
  137. /**
  138. * Read a card's CSD register. The CSD contains Card-Specific Data that
  139. * provides information regarding access to the card's contents.
  140. *
  141. * \param[out] csd pointer to area for returned data.
  142. *
  143. * \return true for success or false for failure.
  144. */
  145. bool readCSD(csd_t* csd) {
  146. return readRegister(CMD9, csd);
  147. }
  148. /** Read one data block in a multiple block read sequence
  149. *
  150. * \param[out] dst Pointer to the location for the data to be read.
  151. *
  152. * \return The value true is returned for success and
  153. * the value false is returned for failure.
  154. */
  155. bool readData(uint8_t *dst);
  156. /** Read OCR register.
  157. *
  158. * \param[out] ocr Value of OCR register.
  159. * \return true for success else false.
  160. */
  161. bool readOCR(uint32_t* ocr);
  162. /** Start a read multiple blocks sequence.
  163. *
  164. * \param[in] blockNumber Address of first block in sequence.
  165. *
  166. * \note This function is used with readData() and readStop() for optimized
  167. * multiple block reads. SPI chipSelect must be low for the entire sequence.
  168. *
  169. * \return The value true is returned for success and
  170. * the value false is returned for failure.
  171. */
  172. bool readStart(uint32_t blockNumber);
  173. /** Return the 64 byte card status
  174. * \param[out] status location for 64 status bytes.
  175. * \return The value true is returned for success and
  176. * the value false is returned for failure.
  177. */
  178. bool readStatus(uint8_t* status);
  179. /** End a read multiple blocks sequence.
  180. *
  181. * \return The value true is returned for success and
  182. * the value false is returned for failure.
  183. */
  184. bool readStop();
  185. /** \return success if sync successful. Not for user apps. */
  186. bool syncBlocks() {return true;}
  187. /** Return the card type: SD V1, SD V2 or SDHC
  188. * \return 0 - SD V1, 1 - SD V2, or 3 - SDHC.
  189. */
  190. int type() const {
  191. return m_type;
  192. }
  193. /**
  194. * Writes a 512 byte block to an SD card.
  195. *
  196. * \param[in] lba Logical block to be written.
  197. * \param[in] src Pointer to the location of the data to be written.
  198. * \return The value true is returned for success and
  199. * the value false is returned for failure.
  200. */
  201. bool writeBlock(uint32_t lba, const uint8_t* src);
  202. /**
  203. * Write multiple 512 byte blocks to an SD card.
  204. *
  205. * \param[in] lba Logical block to be written.
  206. * \param[in] nb Number of blocks to be written.
  207. * \param[in] src Pointer to the location of the data to be written.
  208. * \return The value true is returned for success and
  209. * the value false is returned for failure.
  210. */
  211. bool writeBlocks(uint32_t lba, const uint8_t* src, size_t nb);
  212. /** Write one data block in a multiple block write sequence.
  213. * \param[in] src Pointer to the location of the data to be written.
  214. * \return The value true is returned for success and
  215. * the value false is returned for failure.
  216. */
  217. bool writeData(const uint8_t* src);
  218. /** Start a write multiple blocks sequence.
  219. *
  220. * \param[in] blockNumber Address of first block in sequence.
  221. *
  222. * \note This function is used with writeData() and writeStop()
  223. * for optimized multiple block writes.
  224. *
  225. * \return The value true is returned for success and
  226. * the value false is returned for failure.
  227. */
  228. bool writeStart(uint32_t blockNumber);
  229. /** Start a write multiple blocks sequence with pre-erase.
  230. *
  231. * \param[in] blockNumber Address of first block in sequence.
  232. * \param[in] eraseCount The number of blocks to be pre-erased.
  233. *
  234. * \note This function is used with writeData() and writeStop()
  235. * for optimized multiple block writes.
  236. *
  237. * \return The value true is returned for success and
  238. * the value false is returned for failure.
  239. */
  240. bool writeStart(uint32_t blockNumber, uint32_t eraseCount);
  241. /** End a write multiple blocks sequence.
  242. *
  243. * \return The value true is returned for success and
  244. * the value false is returned for failure.
  245. */
  246. bool writeStop();
  247. /** Set CS low and activate the card. */
  248. void spiStart();
  249. /** Set CS high and deactivate the card. */
  250. void spiStop();
  251. private:
  252. // private functions
  253. uint8_t cardAcmd(uint8_t cmd, uint32_t arg) {
  254. cardCommand(CMD55, 0);
  255. return cardCommand(cmd, arg);
  256. }
  257. uint8_t cardCommand(uint8_t cmd, uint32_t arg);
  258. bool isTimedOut(uint16_t startMS, uint16_t timeoutMS);
  259. bool readData(uint8_t* dst, size_t count);
  260. bool readRegister(uint8_t cmd, void* buf);
  261. void type(uint8_t value) {
  262. m_type = value;
  263. }
  264. bool waitNotBusy(uint16_t timeoutMS);
  265. bool writeData(uint8_t token, const uint8_t* src);
  266. //---------------------------------------------------------------------------
  267. // functions defined in SdSpiDriver.h
  268. void spiActivate() {
  269. m_spiDriver->activate();
  270. }
  271. void spiDeactivate() {
  272. m_spiDriver->deactivate();
  273. }
  274. uint8_t spiReceive() {
  275. return m_spiDriver->receive();
  276. }
  277. uint8_t spiReceive(uint8_t* buf, size_t n) {
  278. return m_spiDriver->receive(buf, n);
  279. }
  280. void spiSend(uint8_t data) {
  281. m_spiDriver->send(data);
  282. }
  283. void spiSend(const uint8_t* buf, size_t n) {
  284. m_spiDriver->send(buf, n);
  285. }
  286. void spiSelect() {
  287. m_spiDriver->select();
  288. }
  289. void spiUnselect() {
  290. m_spiDriver->unselect();
  291. }
  292. uint8_t m_errorCode;
  293. SdSpiDriver *m_spiDriver;
  294. bool m_spiActive;
  295. uint8_t m_status;
  296. uint8_t m_type;
  297. };
  298. //==============================================================================
  299. /**
  300. * \class SdSpiCardEX
  301. * \brief Extended SD I/O block driver.
  302. */
  303. class SdSpiCardEX : public SdSpiCard {
  304. public:
  305. /** Initialize the SD card
  306. *
  307. * \param[in] spi SPI driver.
  308. * \param[in] csPin Card chip select pin number.
  309. * \param[in] spiSettings SPI speed, mode, and bit order.
  310. * \return The value true is returned for success and
  311. * the value false is returned for failure.
  312. */
  313. bool begin(SdSpiDriver* spi, uint8_t csPin, SPISettings spiSettings) {
  314. m_curState = IDLE_STATE;
  315. return SdSpiCard::begin(spi, csPin, spiSettings);
  316. }
  317. /**
  318. * Read a 512 byte block from an SD card.
  319. *
  320. * \param[in] block Logical block to be read.
  321. * \param[out] dst Pointer to the location that will receive the data.
  322. * \return The value true is returned for success and
  323. * the value false is returned for failure.
  324. */
  325. bool readBlock(uint32_t block, uint8_t* dst);
  326. /** End multi-block transfer and go to idle state.
  327. * \return The value true is returned for success and
  328. * the value false is returned for failure.
  329. */
  330. bool syncBlocks();
  331. /**
  332. * Writes a 512 byte block to an SD card.
  333. *
  334. * \param[in] block Logical block to be written.
  335. * \param[in] src Pointer to the location of the data to be written.
  336. * \return The value true is returned for success and
  337. * the value false is returned for failure.
  338. */
  339. bool writeBlock(uint32_t block, const uint8_t* src);
  340. /**
  341. * Read multiple 512 byte blocks from an SD card.
  342. *
  343. * \param[in] block Logical block to be read.
  344. * \param[in] nb Number of blocks to be read.
  345. * \param[out] dst Pointer to the location that will receive the data.
  346. * \return The value true is returned for success and
  347. * the value false is returned for failure.
  348. */
  349. bool readBlocks(uint32_t block, uint8_t* dst, size_t nb);
  350. /**
  351. * Write multiple 512 byte blocks to an SD card.
  352. *
  353. * \param[in] block Logical block to be written.
  354. * \param[in] nb Number of blocks to be written.
  355. * \param[in] src Pointer to the location of the data to be written.
  356. * \return The value true is returned for success and
  357. * the value false is returned for failure.
  358. */
  359. bool writeBlocks(uint32_t block, const uint8_t* src, size_t nb);
  360. private:
  361. static const uint32_t IDLE_STATE = 0;
  362. static const uint32_t READ_STATE = 1;
  363. static const uint32_t WRITE_STATE = 2;
  364. uint32_t m_curBlock;
  365. uint8_t m_curState;
  366. };
  367. #endif // SdSpiCard_h