SdioCard.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /**
  2. * Copyright (c) 2011-2022 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 "../common/SysCall.h"
  28. #include "SdCardInterface.h"
  29. #define FIFO_SDIO 0
  30. #define DMA_SDIO 1
  31. /**
  32. * \class SdioConfig
  33. * \brief SDIO card configuration.
  34. */
  35. class SdioConfig {
  36. public:
  37. SdioConfig() {}
  38. /**
  39. * SdioConfig constructor.
  40. * \param[in] opt SDIO options.
  41. */
  42. explicit SdioConfig(uint8_t opt) : m_options(opt) {}
  43. /** \return SDIO card options. */
  44. uint8_t options() {return m_options;}
  45. /** \return true if DMA_SDIO. */
  46. bool useDma() {return m_options & DMA_SDIO;}
  47. private:
  48. uint8_t m_options = FIFO_SDIO;
  49. };
  50. //------------------------------------------------------------------------------
  51. /**
  52. * \class SdioCard
  53. * \brief Raw SDIO access to SD and SDHC flash memory cards.
  54. */
  55. class SdioCard : public SdCardInterface {
  56. public:
  57. /** Initialize the SD card.
  58. * \param[in] sdioConfig SDIO card configuration.
  59. * \return true for success or false for failure.
  60. */
  61. bool begin(SdioConfig sdioConfig);
  62. /** CMD6 Switch mode: Check Function Set Function.
  63. * \param[in] arg CMD6 argument.
  64. * \param[out] status return status data.
  65. *
  66. * \return true for success or false for failure.
  67. */
  68. bool cardCMD6(uint32_t arg, uint8_t* status);
  69. /** Disable an SDIO card.
  70. * not implemented.
  71. */
  72. void end() {}
  73. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  74. uint32_t __attribute__((error("use sectorCount()"))) cardSize();
  75. #endif // DOXYGEN_SHOULD_SKIP_THIS
  76. /** Erase a range of sectors.
  77. *
  78. * \param[in] firstSector The address of the first sector in the range.
  79. * \param[in] lastSector The address of the last sector in the range.
  80. *
  81. * \note This function requests the SD card to do a flash erase for a
  82. * range of sectors. The data on the card after an erase operation is
  83. * either 0 or 1, depends on the card vendor. The card must support
  84. * single sector erase.
  85. *
  86. * \return true for success or false for failure.
  87. */
  88. bool erase(uint32_t firstSector, uint32_t lastSector);
  89. /**
  90. * \return code for the last error. See SdCardInfo.h for a list of error codes.
  91. */
  92. uint8_t errorCode() const;
  93. /** \return error data for last error. */
  94. uint32_t errorData() const;
  95. /** \return error line for last error. Tmp function for debug. */
  96. uint32_t errorLine() const;
  97. /**
  98. * Check for busy with CMD13.
  99. *
  100. * \return true if busy else false.
  101. */
  102. bool isBusy();
  103. /** \return the SD clock frequency in kHz. */
  104. uint32_t kHzSdClk();
  105. /**
  106. * Read a 512 byte sector from an SD card.
  107. *
  108. * \param[in] sector Logical sector to be read.
  109. * \param[out] dst Pointer to the location that will receive the data.
  110. * \return true for success or false for failure.
  111. */
  112. bool readSector(uint32_t sector, uint8_t* dst);
  113. /**
  114. * Read multiple 512 byte sectors from an SD card.
  115. *
  116. * \param[in] sector Logical sector to be read.
  117. * \param[in] ns Number of sectors to be read.
  118. * \param[out] dst Pointer to the location that will receive the data.
  119. * \return true for success or false for failure.
  120. */
  121. bool readSectors(uint32_t sector, uint8_t* dst, size_t ns);
  122. /**
  123. * Read a card's CID register. The CID contains card identification
  124. * information such as Manufacturer ID, Product name, Product serial
  125. * number and Manufacturing date.
  126. *
  127. * \param[out] cid pointer to area for returned data.
  128. *
  129. * \return true for success or false for failure.
  130. */
  131. bool readCID(cid_t* cid);
  132. /**
  133. * Read a card's CSD register. The CSD contains Card-Specific Data that
  134. * provides information regarding access to the card's contents.
  135. *
  136. * \param[out] csd pointer to area for returned data.
  137. *
  138. * \return true for success or false for failure.
  139. */
  140. bool readCSD(csd_t* csd);
  141. /** Read one data sector in a multiple sector read sequence
  142. *
  143. * \param[out] dst Pointer to the location for the data to be read.
  144. *
  145. * \return true for success or false for failure.
  146. */
  147. bool readData(uint8_t* dst);
  148. /** Read OCR register.
  149. *
  150. * \param[out] ocr Value of OCR register.
  151. * \return true for success or false for failure.
  152. */
  153. bool readOCR(uint32_t* ocr);
  154. /** Read SCR register.
  155. *
  156. * \param[out] scr Value of SCR register.
  157. * \return true for success or false for failure.
  158. */
  159. bool readSCR(scr_t *scr);
  160. /** Start a read multiple sectors sequence.
  161. *
  162. * \param[in] sector Address of first sector in sequence.
  163. *
  164. * \note This function is used with readData() and readStop() for optimized
  165. * multiple sector reads. SPI chipSelect must be low for the entire sequence.
  166. *
  167. * \return true for success or false for failure.
  168. */
  169. bool readStart(uint32_t sector);
  170. /** Start a read multiple sectors sequence.
  171. *
  172. * \param[in] sector Address of first sector in sequence.
  173. * \param[in] count Maximum sector count.
  174. * \note This function is used with readData() and readStop() for optimized
  175. * multiple sector reads. SPI chipSelect must be low for the entire sequence.
  176. *
  177. * \return true for success or false for failure.
  178. */
  179. bool readStart(uint32_t sector, uint32_t count);
  180. /** End a read multiple sectors sequence.
  181. *
  182. * \return true for success or false for failure.
  183. */
  184. bool readStop();
  185. /** \return SDIO card status. */
  186. uint32_t status();
  187. /**
  188. * Determine the size of an SD flash memory card.
  189. *
  190. * \return The number of 512 byte data sectors in the card
  191. * or zero if an error occurs.
  192. */
  193. uint32_t sectorCount();
  194. /**
  195. * Send CMD12 to stop read or write.
  196. *
  197. * \param[in] blocking If true, wait for command complete.
  198. *
  199. * \return true for success or false for failure.
  200. */
  201. bool stopTransmission(bool blocking);
  202. /** \return success if sync successful. Not for user apps. */
  203. bool syncDevice();
  204. /** Return the card type: SD V1, SD V2 or SDHC
  205. * \return 0 - SD V1, 1 - SD V2, or 3 - SDHC.
  206. */
  207. uint8_t type() const;
  208. /**
  209. * Writes a 512 byte sector to an SD card.
  210. *
  211. * \param[in] sector Logical sector to be written.
  212. * \param[in] src Pointer to the location of the data to be written.
  213. * \return true for success or false for failure.
  214. */
  215. bool writeSector(uint32_t sector, const uint8_t* src);
  216. /**
  217. * Write multiple 512 byte sectors to an SD card.
  218. *
  219. * \param[in] sector Logical sector to be written.
  220. * \param[in] ns Number of sectors to be written.
  221. * \param[in] src Pointer to the location of the data to be written.
  222. * \return true for success or false for failure.
  223. */
  224. bool writeSectors(uint32_t sector, const uint8_t* src, size_t ns);
  225. /** Write one data sector in a multiple sector write sequence.
  226. * \param[in] src Pointer to the location of the data to be written.
  227. * \return true for success or false for failure.
  228. */
  229. bool writeData(const uint8_t* src);
  230. /** Start a write multiple sectors sequence.
  231. *
  232. * \param[in] sector Address of first sector in sequence.
  233. *
  234. * \note This function is used with writeData() and writeStop()
  235. * for optimized multiple sector writes.
  236. *
  237. * \return true for success or false for failure.
  238. */
  239. bool writeStart(uint32_t sector);
  240. /** Start a write multiple sectors sequence.
  241. *
  242. * \param[in] sector Address of first sector in sequence.
  243. * \param[in] count Maximum sector count.
  244. * \note This function is used with writeData() and writeStop()
  245. * for optimized multiple sector writes.
  246. *
  247. * \return true for success or false for failure.
  248. */
  249. bool writeStart(uint32_t sector, uint32_t count);
  250. /** End a write multiple sectors sequence.
  251. *
  252. * \return true for success or false for failure.
  253. */
  254. bool writeStop();
  255. private:
  256. static const uint8_t IDLE_STATE = 0;
  257. static const uint8_t READ_STATE = 1;
  258. static const uint8_t WRITE_STATE = 2;
  259. uint32_t m_curSector;
  260. SdioConfig m_sdioConfig;
  261. uint8_t m_curState = IDLE_STATE;
  262. };
  263. #endif // SdioCard_h