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.

247 line
8.3KB

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