SdSpiCard.h 15 KB

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