SdioCard.h 8.5 KB

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