SdSpiCard.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. /**
  26. * \file
  27. * \brief SdSpiCard class for V2 SD/SDHC cards
  28. */
  29. #ifndef SdSpiCard_h
  30. #define SdSpiCard_h
  31. #include <stddef.h>
  32. #include "../common/SysCall.h"
  33. #include "SdCardInfo.h"
  34. #include "SdCardInterface.h"
  35. #include "../SpiDriver/SdSpiDriver.h"
  36. /** Verify correct SPI active if non-zero. */
  37. #define CHECK_SPI_ACTIVE 0
  38. #if CHECK_SPI_ACTIVE
  39. /** Check SPI active. */
  40. #define SPI_ASSERT_ACTIVE {if (!m_spiActive) {\
  41. Serial.print(F("SPI_ASSERTACTIVE"));\
  42. Serial.println(__LINE__);}}
  43. #else // CHECK_SPI_ACTIVE
  44. /** Do not check SPI active. */
  45. #define SPI_ASSERT_ACTIVE
  46. #endif // CHECK_SPI_ACTIVE
  47. //==============================================================================
  48. /**
  49. * \class SharedSpiCard
  50. * \brief Raw access to SD and SDHC flash memory cards via shared SPI port.
  51. */
  52. #if HAS_SDIO_CLASS
  53. class SharedSpiCard : public SdCardInterface {
  54. #elif USE_BLOCK_DEVICE_INTERFACE
  55. class SharedSpiCard : public FsBlockDeviceInterface {
  56. #else // HAS_SDIO_CLASS
  57. class SharedSpiCard {
  58. #endif // HAS_SDIO_CLASS
  59. public:
  60. /** SD is in idle state */
  61. static const uint8_t IDLE_STATE = 0;
  62. /** SD is in multi-sector read state. */
  63. static const uint8_t READ_STATE = 1;
  64. /** SD is in multi-sector write state. */
  65. static const uint8_t WRITE_STATE = 2;
  66. /** Construct an instance of SharedSpiCard. */
  67. SharedSpiCard() {}
  68. /** Initialize the SD card.
  69. * \param[in] spiConfig SPI card configuration.
  70. * \return true for success or false for failure.
  71. */
  72. bool begin(SdSpiConfig spiConfig);
  73. /** CMD6 Switch mode: Check Function Set Function.
  74. * \param[in] arg CMD6 argument.
  75. * \param[out] status return status data.
  76. *
  77. * \return true for success or false for failure.
  78. */
  79. bool cardCMD6(uint32_t arg, uint8_t* status);
  80. /** End use of card */
  81. void end();
  82. /** Erase a range of sectors.
  83. *
  84. * \param[in] firstSector The address of the first sector in the range.
  85. * \param[in] lastSector The address of the last sector in the range.
  86. *
  87. * \note This function requests the SD card to do a flash erase for a
  88. * range of sectors. The data on the card after an erase operation is
  89. * either 0 or 1, depends on the card vendor. The card must support
  90. * single sector erase.
  91. *
  92. * \return true for success or false for failure.
  93. */
  94. bool erase(uint32_t firstSector, uint32_t lastSector);
  95. /** Determine if card supports single sector erase.
  96. *
  97. * \return true is returned if single sector erase is supported.
  98. * false is returned if single sector erase is not supported.
  99. */
  100. bool eraseSingleSectorEnable();
  101. /**
  102. * Set SD error code.
  103. * \param[in] code value for error code.
  104. */
  105. void error(uint8_t code) {
  106. // (void)code;
  107. m_errorCode = code;
  108. }
  109. /**
  110. * \return code for the last error. See SdCardInfo.h for a list of error codes.
  111. */
  112. uint8_t errorCode() const {
  113. return m_errorCode;
  114. }
  115. /** \return error data for last error. */
  116. uint32_t errorData() const {
  117. return m_status;
  118. }
  119. /** \return false for shared class. */
  120. bool hasDedicatedSpi() {return false;}
  121. /**
  122. * Check for busy. MISO low indicates the card is busy.
  123. *
  124. * \return true if busy else false.
  125. */
  126. bool isBusy();
  127. /** \return false, can't be in dedicated state. */
  128. bool isDedicatedSpi() {return false;}
  129. /**
  130. * Read a card's CID register. The CID contains card identification
  131. * information such as Manufacturer ID, Product name, Product serial
  132. * number and Manufacturing date.
  133. *
  134. * \param[out] cid pointer to area for returned data.
  135. *
  136. * \return true for success or false for failure.
  137. */
  138. bool readCID(cid_t* cid) {
  139. return readRegister(CMD10, cid);
  140. }
  141. /**
  142. * Read a card's CSD register. The CSD contains Card-Specific Data that
  143. * provides information regarding access to the card's contents.
  144. *
  145. * \param[out] csd pointer to area for returned data.
  146. *
  147. * \return true for success or false for failure.
  148. */
  149. bool readCSD(csd_t* csd) {
  150. return readRegister(CMD9, csd);
  151. }
  152. /** Read one data sector in a multiple sector read sequence
  153. *
  154. * \param[out] dst Pointer to the location for the data to be read.
  155. *
  156. * \return true for success or false for failure.
  157. */
  158. bool readData(uint8_t* dst);
  159. /** Read OCR register.
  160. *
  161. * \param[out] ocr Value of OCR register.
  162. * \return true for success or false for failure.
  163. */
  164. bool readOCR(uint32_t* ocr);
  165. /** Read SCR register.
  166. *
  167. * \param[out] scr Value of SCR register.
  168. * \return true for success or false for failure.
  169. */
  170. bool readSCR(scr_t* scr);
  171. /**
  172. * Read a 512 byte sector from an SD card.
  173. *
  174. * \param[in] sector Logical sector to be read.
  175. * \param[out] dst Pointer to the location that will receive the data.
  176. * \return true for success or false for failure.
  177. */
  178. bool readSector(uint32_t sector, uint8_t* dst);
  179. /**
  180. * Read multiple 512 byte sectors from an SD card.
  181. *
  182. * \param[in] sector Logical sector to be read.
  183. * \param[in] ns Number of sectors to be read.
  184. * \param[out] dst Pointer to the location that will receive the data.
  185. * \return true for success or false for failure.
  186. */
  187. bool readSectors(uint32_t sector, uint8_t* dst, size_t ns);
  188. /** Start a read multiple sector sequence.
  189. *
  190. * \param[in] sector Address of first sector in sequence.
  191. *
  192. * \note This function is used with readData() and readStop() for optimized
  193. * multiple sector reads. SPI chipSelect must be low for the entire sequence.
  194. *
  195. * \return true for success or false for failure.
  196. */
  197. bool readStart(uint32_t sector);
  198. /** Return the 64 byte card status
  199. * \param[out] status location for 64 status bytes.
  200. * \return true for success or false for failure.
  201. */
  202. bool readStatus(SdStatus* status);
  203. /** End a read multiple sectors sequence.
  204. *
  205. * \return true for success or false for failure.
  206. */
  207. bool readStop();
  208. /** \return SD multi-sector read/write state */
  209. uint8_t sdState() {return m_state;}
  210. /**
  211. * Determine the size of an SD flash memory card.
  212. *
  213. * \return The number of 512 byte data sectors in the card
  214. * or zero if an error occurs.
  215. */
  216. uint32_t sectorCount();
  217. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  218. // Use sectorCount(). cardSize() will be removed in the future.
  219. uint32_t __attribute__((error("use sectorCount()"))) cardSize();
  220. #endif // DOXYGEN_SHOULD_SKIP_THIS
  221. /** Set SPI sharing state
  222. * \param[in] value desired state.
  223. * \return false for shared card
  224. */
  225. bool setDedicatedSpi(bool value) {
  226. (void)value;
  227. return false;
  228. }
  229. /** end a mult-sector transfer.
  230. *
  231. * \return true for success or false for failure.
  232. */
  233. bool stopTransfer();
  234. /** \return success if sync successful. Not for user apps. */
  235. bool syncDevice();
  236. /** Return the card type: SD V1, SD V2 or SDHC/SDXC
  237. * \return 0 - SD V1, 1 - SD V2, or 3 - SDHC/SDXC.
  238. */
  239. uint8_t type() const {
  240. return m_type;
  241. }
  242. /**
  243. * Write a 512 byte sector to an SD card.
  244. *
  245. * \param[in] sector Logical sector to be written.
  246. * \param[in] src Pointer to the location of the data to be written.
  247. * \return true for success or false for failure.
  248. */
  249. bool writeSector(uint32_t sector, const uint8_t* src);
  250. /**
  251. * Write multiple 512 byte sectors to an SD card.
  252. *
  253. * \param[in] sector Logical sector to be written.
  254. * \param[in] ns Number of sectors to be written.
  255. * \param[in] src Pointer to the location of the data to be written.
  256. * \return true for success or false for failure.
  257. */
  258. bool writeSectors(uint32_t sector, const uint8_t* src, size_t ns);
  259. /** Write one data sector in a multiple sector write sequence.
  260. * \param[in] src Pointer to the location of the data to be written.
  261. * \return true for success or false for failure.
  262. */
  263. bool writeData(const uint8_t* src);
  264. /** Start a write multiple sectors sequence.
  265. *
  266. * \param[in] sector Address of first sector in sequence.
  267. *
  268. * \note This function is used with writeData() and writeStop()
  269. * for optimized multiple sector writes.
  270. *
  271. * \return true for success or false for failure.
  272. */
  273. bool writeStart(uint32_t sector);
  274. /** End a write multiple sectors sequence.
  275. *
  276. * \return true for success or false for failure.
  277. */
  278. bool writeStop();
  279. private:
  280. // private functions
  281. uint8_t cardAcmd(uint8_t cmd, uint32_t arg) {
  282. cardCommand(CMD55, 0);
  283. return cardCommand(cmd, arg);
  284. }
  285. uint8_t cardCommand(uint8_t cmd, uint32_t arg);
  286. bool readData(uint8_t* dst, size_t count);
  287. bool readRegister(uint8_t cmd, void* buf);
  288. void spiSelect() {
  289. sdCsWrite(m_csPin, false);
  290. }
  291. void spiStart();
  292. void spiStop();
  293. void spiUnselect() {
  294. sdCsWrite(m_csPin, true);
  295. }
  296. void type(uint8_t value) {
  297. m_type = value;
  298. }
  299. bool waitReady(uint16_t ms);
  300. bool writeData(uint8_t token, const uint8_t* src);
  301. #if SPI_DRIVER_SELECT < 2
  302. void spiActivate() {
  303. m_spiDriver.activate();
  304. }
  305. void spiBegin(SdSpiConfig spiConfig) {
  306. m_spiDriver.begin(spiConfig);
  307. }
  308. void spiDeactivate() {
  309. m_spiDriver.deactivate();
  310. }
  311. void spiEnd() {
  312. m_spiDriver.end();
  313. }
  314. uint8_t spiReceive() {
  315. SPI_ASSERT_ACTIVE;
  316. return m_spiDriver.receive();
  317. }
  318. uint8_t spiReceive(uint8_t* buf, size_t n) {
  319. SPI_ASSERT_ACTIVE;
  320. return m_spiDriver.receive(buf, n);
  321. }
  322. void spiSend(uint8_t data) {
  323. SPI_ASSERT_ACTIVE;
  324. m_spiDriver.send(data);
  325. }
  326. void spiSend(const uint8_t* buf, size_t n) {
  327. SPI_ASSERT_ACTIVE;
  328. m_spiDriver.send(buf, n);
  329. }
  330. void spiSetSckSpeed(uint32_t maxSck) {
  331. m_spiDriver.setSckSpeed(maxSck);
  332. }
  333. SdSpiDriver m_spiDriver;
  334. #else // SPI_DRIVER_SELECT < 2
  335. void spiActivate() {
  336. m_spiDriverPtr->activate();
  337. }
  338. void spiBegin(SdSpiConfig spiConfig) {
  339. m_spiDriverPtr->begin(spiConfig);
  340. }
  341. void spiDeactivate() {
  342. m_spiDriverPtr->deactivate();
  343. }
  344. void spiEnd() {
  345. m_spiDriverPtr->end();
  346. }
  347. uint8_t spiReceive() {
  348. SPI_ASSERT_ACTIVE;
  349. return m_spiDriverPtr->receive();
  350. }
  351. uint8_t spiReceive(uint8_t* buf, size_t n) {
  352. SPI_ASSERT_ACTIVE;
  353. return m_spiDriverPtr->receive(buf, n);
  354. }
  355. void spiSend(uint8_t data) {
  356. SPI_ASSERT_ACTIVE;
  357. m_spiDriverPtr->send(data);
  358. }
  359. void spiSend(const uint8_t* buf, size_t n) {
  360. SPI_ASSERT_ACTIVE;
  361. m_spiDriverPtr->send(buf, n);
  362. }
  363. void spiSetSckSpeed(uint32_t maxSck) {
  364. m_spiDriverPtr->setSckSpeed(maxSck);
  365. }
  366. SdSpiDriver* m_spiDriverPtr;
  367. #endif // SPI_DRIVER_SELECT < 2
  368. bool m_beginCalled = false;
  369. SdCsPin_t m_csPin;
  370. uint8_t m_errorCode = SD_CARD_ERROR_INIT_NOT_CALLED;
  371. bool m_spiActive;
  372. uint8_t m_state;
  373. uint8_t m_status;
  374. uint8_t m_type = 0;
  375. };
  376. //==============================================================================
  377. /**
  378. * \class DedicatedSpiCard
  379. * \brief Raw access to SD and SDHC flash memory cards via dedicate SPI port.
  380. */
  381. class DedicatedSpiCard : public SharedSpiCard {
  382. public:
  383. /** Construct an instance of DedicatedSpiCard. */
  384. DedicatedSpiCard() {}
  385. /** Initialize the SD card.
  386. * \param[in] spiConfig SPI card configuration.
  387. * \return true for success or false for failure.
  388. */
  389. bool begin(SdSpiConfig spiConfig);
  390. /** \return true, can be in dedicaded state. */
  391. bool hasDedicatedSpi() {return true;}
  392. /** \return true if in dedicated SPI state. */
  393. bool isDedicatedSpi() {return m_dedicatedSpi;}
  394. /**
  395. * Read a 512 byte sector from an SD card.
  396. *
  397. * \param[in] sector Logical sector to be read.
  398. * \param[out] dst Pointer to the location that will receive the data.
  399. * \return true for success or false for failure.
  400. */
  401. bool readSector(uint32_t sector, uint8_t* dst);
  402. /**
  403. * Read multiple 512 byte sectors from an SD card.
  404. *
  405. * \param[in] sector Logical sector to be read.
  406. * \param[in] ns Number of sectors to be read.
  407. * \param[out] dst Pointer to the location that will receive the data.
  408. * \return true for success or false for failure.
  409. */
  410. bool readSectors(uint32_t sector, uint8_t* dst, size_t ns);
  411. /** Set SPI sharing state
  412. * \param[in] value desired state.
  413. * \return true for success else false;
  414. */
  415. bool setDedicatedSpi(bool value);
  416. /**
  417. * Write a 512 byte sector to an SD card.
  418. *
  419. * \param[in] sector Logical sector to be written.
  420. * \param[in] src Pointer to the location of the data to be written.
  421. * \return true for success or false for failure.
  422. */
  423. bool writeSector(uint32_t sector, const uint8_t* src);
  424. /**
  425. * Write multiple 512 byte sectors to an SD card.
  426. *
  427. * \param[in] sector Logical sector to be written.
  428. * \param[in] ns Number of sectors to be written.
  429. * \param[in] src Pointer to the location of the data to be written.
  430. * \return true for success or false for failure.
  431. */
  432. bool writeSectors(uint32_t sector, const uint8_t* src, size_t ns);
  433. private:
  434. uint32_t m_curSector;
  435. bool m_dedicatedSpi = false;
  436. };
  437. //==============================================================================
  438. #if ENABLE_DEDICATED_SPI
  439. /** typedef for dedicated SPI. */
  440. typedef DedicatedSpiCard SdSpiCard;
  441. #else
  442. /** typedef for shared SPI. */
  443. typedef SharedSpiCard SdSpiCard;
  444. #endif
  445. #endif // SdSpiCard_h