SdioCard.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 SdioCard_h
  26. #define SdioCard_h
  27. #include "SysCall.h"
  28. #include "BlockDriver.h"
  29. /**
  30. * \class SdioCard
  31. * \brief Raw SDIO access to SD and SDHC flash memory cards.
  32. */
  33. class SdioCard : public BaseBlockDriver {
  34. public:
  35. /** Initialize the SD card.
  36. * \return true for success else false.
  37. */
  38. bool begin();
  39. /**
  40. * Determine the size of an SD flash memory card.
  41. *
  42. * \return The number of 512 byte data blocks in the card
  43. * or zero if an error occurs.
  44. */
  45. uint32_t cardSize();
  46. /** Erase a range of blocks.
  47. *
  48. * \param[in] firstBlock The address of the first block in the range.
  49. * \param[in] lastBlock The address of the last block in the range.
  50. *
  51. * \note This function requests the SD card to do a flash erase for a
  52. * range of blocks. The data on the card after an erase operation is
  53. * either 0 or 1, depends on the card vendor. The card must support
  54. * single block erase.
  55. *
  56. * \return The value true is returned for success and
  57. * the value false is returned for failure.
  58. */
  59. bool erase(uint32_t firstBlock, uint32_t lastBlock);
  60. /**
  61. * \return code for the last error. See SdInfo.h for a list of error codes.
  62. */
  63. uint8_t errorCode();
  64. /** \return error data for last error. */
  65. uint32_t errorData();
  66. /** \return error line for last error. Tmp function for debug. */
  67. uint32_t errorLine();
  68. /**
  69. * Check for busy with CMD13.
  70. *
  71. * \return true if busy else false.
  72. */
  73. bool isBusy();
  74. /** \return the SD clock frequency in kHz. */
  75. uint32_t kHzSdClk();
  76. /**
  77. * Read a 512 byte block from an SD card.
  78. *
  79. * \param[in] lba Logical block to be read.
  80. * \param[out] dst Pointer to the location that will receive the data.
  81. * \return The value true is returned for success and
  82. * the value false is returned for failure.
  83. */
  84. bool readBlock(uint32_t lba, uint8_t* dst);
  85. /**
  86. * Read multiple 512 byte blocks from an SD card.
  87. *
  88. * \param[in] lba Logical block to be read.
  89. * \param[in] nb Number of blocks to be read.
  90. * \param[out] dst Pointer to the location that will receive the data.
  91. * \return The value true is returned for success and
  92. * the value false is returned for failure.
  93. */
  94. bool readBlocks(uint32_t lba, uint8_t* dst, size_t nb);
  95. /**
  96. * Read a card's CID register. The CID contains card identification
  97. * information such as Manufacturer ID, Product name, Product serial
  98. * number and Manufacturing date.
  99. *
  100. * \param[out] cid pointer to area for returned data.
  101. *
  102. * \return true for success or false for failure.
  103. */
  104. bool readCID(void* cid);
  105. /**
  106. * Read a card's CSD register. The CSD contains Card-Specific Data that
  107. * provides information regarding access to the card's contents.
  108. *
  109. * \param[out] csd pointer to area for returned data.
  110. *
  111. * \return true for success or false for failure.
  112. */
  113. bool readCSD(void* csd);
  114. /** Read one data block in a multiple block read sequence
  115. *
  116. * \param[out] dst Pointer to the location for the data to be read.
  117. *
  118. * \return The value true is returned for success and
  119. * the value false is returned for failure.
  120. */
  121. bool readData(uint8_t *dst);
  122. /** Read OCR register.
  123. *
  124. * \param[out] ocr Value of OCR register.
  125. * \return true for success else false.
  126. */
  127. bool readOCR(uint32_t* ocr);
  128. /** Start a read multiple blocks sequence.
  129. *
  130. * \param[in] lba Address of first block in sequence.
  131. *
  132. * \note This function is used with readData() and readStop() for optimized
  133. * multiple block reads. SPI chipSelect must be low for the entire sequence.
  134. *
  135. * \return The value true is returned for success and
  136. * the value false is returned for failure.
  137. */
  138. bool readStart(uint32_t lba);
  139. /** Start a read multiple blocks sequence.
  140. *
  141. * \param[in] lba Address of first block in sequence.
  142. * \param[in] count Maximum block count.
  143. * \note This function is used with readData() and readStop() for optimized
  144. * multiple block reads. SPI chipSelect must be low for the entire sequence.
  145. *
  146. * \return The value true is returned for success and
  147. * the value false is returned for failure.
  148. */
  149. bool readStart(uint32_t lba, uint32_t count);
  150. /** End a read multiple blocks sequence.
  151. *
  152. * \return The value true is returned for success and
  153. * the value false is returned for failure.
  154. */
  155. bool readStop();
  156. /** \return success if sync successful. Not for user apps. */
  157. bool syncBlocks();
  158. /** Return the card type: SD V1, SD V2 or SDHC
  159. * \return 0 - SD V1, 1 - SD V2, or 3 - SDHC.
  160. */
  161. uint8_t type();
  162. /**
  163. * Writes a 512 byte block to an SD card.
  164. *
  165. * \param[in] lba Logical block to be written.
  166. * \param[in] src Pointer to the location of the data to be written.
  167. * \return The value true is returned for success and
  168. * the value false is returned for failure.
  169. */
  170. bool writeBlock(uint32_t lba, const uint8_t* src);
  171. /**
  172. * Write multiple 512 byte blocks to an SD card.
  173. *
  174. * \param[in] lba Logical block to be written.
  175. * \param[in] nb Number of blocks to be written.
  176. * \param[in] src Pointer to the location of the data to be written.
  177. * \return The value true is returned for success and
  178. * the value false is returned for failure.
  179. */
  180. bool writeBlocks(uint32_t lba, const uint8_t* src, size_t nb);
  181. /** Write one data block in a multiple block write sequence.
  182. * \param[in] src Pointer to the location of the data to be written.
  183. * \return The value true is returned for success and
  184. * the value false is returned for failure.
  185. */
  186. bool writeData(const uint8_t* src);
  187. /** Start a write multiple blocks sequence.
  188. *
  189. * \param[in] lba Address of first block in sequence.
  190. *
  191. * \note This function is used with writeData() and writeStop()
  192. * for optimized multiple block writes.
  193. *
  194. * \return The value true is returned for success and
  195. * the value false is returned for failure.
  196. */
  197. bool writeStart(uint32_t lba);
  198. /** Start a write multiple blocks sequence.
  199. *
  200. * \param[in] lba Address of first block in sequence.
  201. * \param[in] count Maximum block count.
  202. * \note This function is used with writeData() and writeStop()
  203. * for optimized multiple block writes.
  204. *
  205. * \return The value true is returned for success and
  206. * the value false is returned for failure.
  207. */
  208. bool writeStart(uint32_t lba, uint32_t count);
  209. /** End a write multiple blocks sequence.
  210. *
  211. * \return The value true is returned for success and
  212. * the value false is returned for failure.
  213. */
  214. bool writeStop();
  215. };
  216. //==============================================================================
  217. /**
  218. * \class SdioCardEX
  219. * \brief Extended SD I/O block driver.
  220. */
  221. class SdioCardEX : public SdioCard {
  222. public:
  223. /** Initialize the SD card
  224. *
  225. * \return The value true is returned for success and
  226. * the value false is returned for failure.
  227. */
  228. bool begin() {
  229. m_curState = IDLE_STATE;
  230. return SdioCard::begin();
  231. }
  232. /** Erase a range of blocks.
  233. *
  234. * \param[in] firstBlock The address of the first block in the range.
  235. * \param[in] lastBlock The address of the last block in the range.
  236. *
  237. * \note This function requests the SD card to do a flash erase for a
  238. * range of blocks. The data on the card after an erase operation is
  239. * either 0 or 1, depends on the card vendor. The card must support
  240. * single block erase.
  241. *
  242. * \return The value true is returned for success and
  243. * the value false is returned for failure.
  244. */
  245. bool erase(uint32_t firstBlock, uint32_t lastBlock) {
  246. return syncBlocks() && SdioCard::erase(firstBlock, lastBlock);
  247. }
  248. /**
  249. * Read a 512 byte block from an SD card.
  250. *
  251. * \param[in] block Logical block to be read.
  252. * \param[out] dst Pointer to the location that will receive the data.
  253. * \return The value true is returned for success and
  254. * the value false is returned for failure.
  255. */
  256. bool readBlock(uint32_t block, uint8_t* dst);
  257. /** End multi-block transfer and go to idle state.
  258. * \return The value true is returned for success and
  259. * the value false is returned for failure.
  260. */
  261. bool syncBlocks();
  262. /**
  263. * Writes a 512 byte block to an SD card.
  264. *
  265. * \param[in] block Logical block to be written.
  266. * \param[in] src Pointer to the location of the data to be written.
  267. * \return The value true is returned for success and
  268. * the value false is returned for failure.
  269. */
  270. bool writeBlock(uint32_t block, const uint8_t* src);
  271. /**
  272. * Read multiple 512 byte blocks from an SD card.
  273. *
  274. * \param[in] block Logical block to be read.
  275. * \param[in] nb Number of blocks to be read.
  276. * \param[out] dst Pointer to the location that will receive the data.
  277. * \return The value true is returned for success and
  278. * the value false is returned for failure.
  279. */
  280. bool readBlocks(uint32_t block, uint8_t* dst, size_t nb);
  281. /**
  282. * Write multiple 512 byte blocks to an SD card.
  283. *
  284. * \param[in] block Logical block to be written.
  285. * \param[in] nb Number of blocks to be written.
  286. * \param[in] src Pointer to the location of the data to be written.
  287. * \return The value true is returned for success and
  288. * the value false is returned for failure.
  289. */
  290. bool writeBlocks(uint32_t block, const uint8_t* src, size_t nb);
  291. private:
  292. static const uint32_t IDLE_STATE = 0;
  293. static const uint32_t READ_STATE = 1;
  294. static const uint32_t WRITE_STATE = 2;
  295. uint32_t m_curLba;
  296. uint32_t m_limitLba;
  297. uint8_t m_curState;
  298. };
  299. #endif // SdioCard_h