PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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 5.5KB

3 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* Arduino Sd2Card Library
  2. * Copyright (C) 2009 by William Greiman
  3. *
  4. * This file is part of the Arduino Sd2Card Library
  5. *
  6. * This Library is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This Library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Arduino Sd2Card Library. If not, see
  18. * <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef Sd2Card_h
  21. #define Sd2Card_h
  22. /**
  23. * \file
  24. * Sd2Card class
  25. */
  26. #include "Sd2PinMap.h"
  27. #include "SdInfo.h"
  28. /** Set SCK to max rate of F_CPU/2. See Sd2Card::setSckRate(). */
  29. uint8_t const SPI_FULL_SPEED = 0;
  30. /** Set SCK rate to F_CPU/4. See Sd2Card::setSckRate(). */
  31. uint8_t const SPI_HALF_SPEED = 1;
  32. /** Set SCK rate to F_CPU/8. Sd2Card::setSckRate(). */
  33. uint8_t const SPI_QUARTER_SPEED = 2;
  34. //------------------------------------------------------------------------------
  35. // SPI pin definitions
  36. //
  37. // hardware pin defs
  38. /**
  39. * SD Chip Select pin
  40. *
  41. * Warning if this pin is redefined the hardware SS will pin will be enabled
  42. * as an output by init(). An avr processor will not function as an SPI
  43. * master unless SS is set to output mode.
  44. */
  45. /** The default chip select pin for the SD card is SS. */
  46. uint8_t const SD_CHIP_SELECT_PIN = SS_PIN;
  47. // The following three pins must not be redefined for hardware SPI.
  48. /** SPI Master Out Slave In pin */
  49. uint8_t const SPI_MOSI_PIN = MOSI_PIN;
  50. /** SPI Master In Slave Out pin */
  51. uint8_t const SPI_MISO_PIN = MISO_PIN;
  52. /** SPI Clock pin */
  53. uint8_t const SPI_SCK_PIN = SCK_PIN;
  54. /** optimize loops for hardware SPI */
  55. #define OPTIMIZE_HARDWARE_SPI
  56. //------------------------------------------------------------------------------
  57. /** Protect block zero from write if nonzero */
  58. #define SD_PROTECT_BLOCK_ZERO 1
  59. /** init timeout ms */
  60. const unsigned int SD_INIT_TIMEOUT = 2000;
  61. /** erase timeout ms */
  62. const unsigned int SD_ERASE_TIMEOUT = 10000;
  63. /** read timeout ms */
  64. const unsigned int SD_READ_TIMEOUT = 300;
  65. /** write time out ms */
  66. const unsigned int SD_WRITE_TIMEOUT = 600;
  67. //------------------------------------------------------------------------------
  68. // card types
  69. /** Standard capacity V1 SD card */
  70. uint8_t const SD_CARD_TYPE_SD1 = 1;
  71. /** Standard capacity V2 SD card */
  72. uint8_t const SD_CARD_TYPE_SD2 = 2;
  73. /** High Capacity SD card */
  74. uint8_t const SD_CARD_TYPE_SDHC = 3;
  75. //------------------------------------------------------------------------------
  76. #if defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(__IMXRT1052__) || defined(__IMXRT1062__)
  77. #include "NXP_SDHC.h"
  78. #define BUILTIN_SDCARD 254
  79. #endif
  80. //------------------------------------------------------------------------------
  81. /**
  82. * \class Sd2Card
  83. * \brief Raw access to SD and SDHC flash memory cards.
  84. */
  85. class Sd2Card {
  86. public:
  87. /** Construct an instance of Sd2Card. */
  88. Sd2Card(void) : type_(0) {}
  89. /* Initialize an SD flash memory card with the selected SPI clock rate
  90. * and the SD chip select pin. */
  91. uint8_t init(uint8_t sckRateID, uint8_t chipSelectPin) {
  92. #if defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(__IMXRT1052__) || defined(__IMXRT1062__)
  93. if (chipSelectPin == BUILTIN_SDCARD) {
  94. chipSelectPin_ = BUILTIN_SDCARD;
  95. uint8_t ret = SDHC_CardInit();
  96. type_ = SDHC_CardGetType();
  97. return (ret == 0) ? true : false;
  98. }
  99. #endif
  100. return SD_init(sckRateID, chipSelectPin);
  101. }
  102. /* return the type of SD card detected during init() */
  103. uint8_t type(void) const {return type_;}
  104. /** Returns the current value, true or false, for partial block read. */
  105. uint8_t readBlock(uint32_t block, uint8_t* dst) {
  106. #if defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(__IMXRT1052__) || defined(__IMXRT1062__)
  107. if (chipSelectPin_ == BUILTIN_SDCARD) {
  108. return (SDHC_CardReadBlock(dst, block) == 0) ? true : false;
  109. }
  110. #endif
  111. return SD_readBlock(block, dst);
  112. }
  113. /** Return the card type: SD V1, SD V2 or SDHC */
  114. uint8_t writeBlock(uint32_t block, const uint8_t* src) {
  115. #if defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(__IMXRT1052__) || defined(__IMXRT1062__)
  116. if (chipSelectPin_ == BUILTIN_SDCARD) {
  117. return (SDHC_CardWriteBlock(src, block) == 0) ? true : false;
  118. }
  119. #endif
  120. return SD_writeBlock(block, src);
  121. }
  122. private:
  123. uint8_t chipSelectPin_;
  124. uint8_t status_;
  125. uint8_t type_;
  126. // private functions
  127. uint8_t SD_init(uint8_t sckRateID, uint8_t chipSelectPin);
  128. uint8_t SD_readBlock(uint32_t block, uint8_t* dst);
  129. uint8_t SD_writeBlock(uint32_t blockNumber, const uint8_t* src);
  130. uint8_t cardAcmd(uint8_t cmd, uint32_t arg) {
  131. cardCommand(CMD55, 0);
  132. return cardCommand(cmd, arg);
  133. }
  134. uint8_t cardCommand(uint8_t cmd, uint32_t arg);
  135. uint8_t sendWriteCommand(uint32_t blockNumber, uint32_t eraseCount);
  136. void chipSelectHigh(void);
  137. void chipSelectLow(void);
  138. uint8_t waitNotBusy(unsigned int timeoutMillis);
  139. uint8_t writeData(uint8_t token, const uint8_t* src);
  140. uint8_t waitStartBlock(void);
  141. uint8_t setSckRate(uint8_t sckRateID);
  142. };
  143. #endif // Sd2Card_h