My Marlin configs for Fabrikator Mini and CTC i3 Pro B
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.

Sd2Card.h 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. /**
  24. * \file
  25. * \brief Sd2Card class for V2 SD/SDHC cards
  26. */
  27. /**
  28. * Arduino Sd2Card Library
  29. * Copyright (c) 2009 by William Greiman
  30. *
  31. * This file is part of the Arduino Sd2Card Library
  32. */
  33. #include "SdFatConfig.h"
  34. #include "SdInfo.h"
  35. #include "disk_io_driver.h"
  36. #include <stdint.h>
  37. uint16_t const SD_INIT_TIMEOUT = 2000, // (ms) Init timeout
  38. SD_ERASE_TIMEOUT = 10000, // (ms) Erase timeout
  39. SD_READ_TIMEOUT = 300, // (ms) Read timeout
  40. SD_WRITE_TIMEOUT = 600; // (ms) Write timeout
  41. // SD card errors
  42. typedef enum : uint8_t {
  43. SD_CARD_ERROR_CMD0 = 0x01, // Timeout error for command CMD0 (initialize card in SPI mode)
  44. SD_CARD_ERROR_CMD8 = 0x02, // CMD8 was not accepted - not a valid SD card
  45. SD_CARD_ERROR_CMD12 = 0x03, // Card returned an error response for CMD12 (write stop)
  46. SD_CARD_ERROR_CMD17 = 0x04, // Card returned an error response for CMD17 (read block)
  47. SD_CARD_ERROR_CMD18 = 0x05, // Card returned an error response for CMD18 (read multiple block)
  48. SD_CARD_ERROR_CMD24 = 0x06, // Card returned an error response for CMD24 (write block)
  49. SD_CARD_ERROR_CMD25 = 0x07, // WRITE_MULTIPLE_BLOCKS command failed
  50. SD_CARD_ERROR_CMD58 = 0x08, // Card returned an error response for CMD58 (read OCR)
  51. SD_CARD_ERROR_ACMD23 = 0x09, // SET_WR_BLK_ERASE_COUNT failed
  52. SD_CARD_ERROR_ACMD41 = 0x0A, // ACMD41 initialization process timeout
  53. SD_CARD_ERROR_BAD_CSD = 0x0B, // Card returned a bad CSR version field
  54. SD_CARD_ERROR_ERASE = 0x0C, // Erase block group command failed
  55. SD_CARD_ERROR_ERASE_SINGLE_BLOCK = 0x0D, // Card not capable of single block erase
  56. SD_CARD_ERROR_ERASE_TIMEOUT = 0x0E, // Erase sequence timed out
  57. SD_CARD_ERROR_READ = 0x0F, // Card returned an error token instead of read data
  58. SD_CARD_ERROR_READ_REG = 0x10, // Read CID or CSD failed
  59. SD_CARD_ERROR_READ_TIMEOUT = 0x11, // Timeout while waiting for start of read data
  60. SD_CARD_ERROR_STOP_TRAN = 0x12, // Card did not accept STOP_TRAN_TOKEN
  61. SD_CARD_ERROR_WRITE = 0x13, // Card returned an error token as a response to a write operation
  62. SD_CARD_ERROR_WRITE_BLOCK_ZERO = 0x14, // REMOVE - not used ... attempt to write protected block zero
  63. SD_CARD_ERROR_WRITE_MULTIPLE = 0x15, // Card did not go ready for a multiple block write
  64. SD_CARD_ERROR_WRITE_PROGRAMMING = 0x16, // Card returned an error to a CMD13 status check after a write
  65. SD_CARD_ERROR_WRITE_TIMEOUT = 0x17, // Timeout occurred during write programming
  66. SD_CARD_ERROR_SCK_RATE = 0x18, // Incorrect rate selected
  67. SD_CARD_ERROR_INIT_NOT_CALLED = 0x19, // Init() not called
  68. // 0x1A is unused now, it was: card returned an error for CMD59 (CRC_ON_OFF)
  69. SD_CARD_ERROR_READ_CRC = 0x1B // Invalid read CRC
  70. } sd_error_code_t;
  71. // card types
  72. uint8_t const SD_CARD_TYPE_SD1 = 1, // Standard capacity V1 SD card
  73. SD_CARD_TYPE_SD2 = 2, // Standard capacity V2 SD card
  74. SD_CARD_TYPE_SDHC = 3; // High Capacity SD card
  75. /**
  76. * Define SOFTWARE_SPI to use bit-bang SPI
  77. */
  78. #if EITHER(MEGA_SOFT_SPI, USE_SOFTWARE_SPI)
  79. #define SOFTWARE_SPI
  80. #endif
  81. #if IS_TEENSY_35_36 || IS_TEENSY_40_41
  82. #include "NXP_SDHC.h"
  83. #define BUILTIN_SDCARD 254
  84. #endif
  85. /**
  86. * \class Sd2Card
  87. * \brief Raw access to SD and SDHC flash memory cards.
  88. */
  89. class DiskIODriver_SPI_SD : public DiskIODriver {
  90. public:
  91. DiskIODriver_SPI_SD() : errorCode_(SD_CARD_ERROR_INIT_NOT_CALLED), type_(0) {}
  92. bool erase(uint32_t firstBlock, uint32_t lastBlock);
  93. bool eraseSingleBlockEnable();
  94. /**
  95. * Set SD error code.
  96. * \param[in] code value for error code.
  97. */
  98. void error(const uint8_t code);
  99. /**
  100. * \return error code for last error. See Sd2Card.h for a list of error codes.
  101. */
  102. inline int errorCode() const { return errorCode_; }
  103. /** \return error data for last error. */
  104. inline int errorData() const { return status_; }
  105. /**
  106. * Initialize an SD flash memory card with default clock rate and chip
  107. * select pin. See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin).
  108. *
  109. * \return true for success or false for failure.
  110. */
  111. bool init(const uint8_t sckRateID, const pin_t chipSelectPin) override;
  112. bool setSckRate(const uint8_t sckRateID);
  113. /**
  114. * Return the card type: SD V1, SD V2 or SDHC
  115. * \return 0 - SD V1, 1 - SD V2, or 3 - SDHC.
  116. */
  117. int type() const { return type_; }
  118. /**
  119. * Read a card's CID register. The CID contains card identification
  120. * information such as Manufacturer ID, Product name, Product serial
  121. * number and Manufacturing date.
  122. *
  123. * \param[out] cid pointer to area for returned data.
  124. *
  125. * \return true for success or false for failure.
  126. */
  127. bool readCID(cid_t *cid) { return readRegister(CMD10, cid); }
  128. /**
  129. * Read a card's CSD register. The CSD contains Card-Specific Data that
  130. * provides information regarding access to the card's contents.
  131. *
  132. * \param[out] csd pointer to area for returned data.
  133. *
  134. * \return true for success or false for failure.
  135. */
  136. inline bool readCSD(csd_t *csd) override { return readRegister(CMD9, csd); }
  137. bool readData(uint8_t *dst) override;
  138. bool readStart(uint32_t blockNumber) override;
  139. bool readStop() override;
  140. bool writeData(const uint8_t *src) override;
  141. bool writeStart(const uint32_t blockNumber, const uint32_t eraseCount) override;
  142. bool writeStop() override;
  143. bool readBlock(uint32_t block, uint8_t *dst) override;
  144. bool writeBlock(uint32_t blockNumber, const uint8_t *src) override;
  145. uint32_t cardSize() override;
  146. bool isReady() override { return ready; };
  147. void idle() override {}
  148. private:
  149. bool ready = false;
  150. uint8_t chipSelectPin_,
  151. errorCode_,
  152. spiRate_,
  153. status_,
  154. type_;
  155. // private functions
  156. inline uint8_t cardAcmd(const uint8_t cmd, const uint32_t arg) {
  157. cardCommand(CMD55, 0);
  158. return cardCommand(cmd, arg);
  159. }
  160. uint8_t cardCommand(const uint8_t cmd, const uint32_t arg);
  161. bool readData(uint8_t *dst, const uint16_t count);
  162. bool readRegister(const uint8_t cmd, void *buf);
  163. void chipDeselect();
  164. void chipSelect();
  165. inline void type(const uint8_t value) { type_ = value; }
  166. bool waitNotBusy(const millis_t timeout_ms);
  167. bool writeData(const uint8_t token, const uint8_t *src);
  168. };