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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. uint16_t const SD_INIT_TIMEOUT = 2000;
  61. /** erase timeout ms */
  62. uint16_t const SD_ERASE_TIMEOUT = 10000;
  63. /** read timeout ms */
  64. uint16_t const SD_READ_TIMEOUT = 300;
  65. /** write time out ms */
  66. uint16_t const 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__)
  77. extern "C" {
  78. uint8_t KinetisSDHC_InitCard(void);
  79. uint8_t KinetisSDHC_GetCardType(void);
  80. int KinetisSDHC_ReadBlock(void * buff, uint32_t sector);
  81. int KinetisSDHC_WriteBlock(const void * buff, uint32_t sector);
  82. }
  83. #endif
  84. #define BUILTIN_SDCARD 254
  85. //------------------------------------------------------------------------------
  86. /**
  87. * \class Sd2Card
  88. * \brief Raw access to SD and SDHC flash memory cards.
  89. */
  90. class Sd2Card {
  91. public:
  92. /** Construct an instance of Sd2Card. */
  93. Sd2Card(void) : type_(0) {}
  94. /* Initialize an SD flash memory card with the selected SPI clock rate
  95. * and the SD chip select pin. */
  96. uint8_t init(uint8_t sckRateID, uint8_t chipSelectPin) {
  97. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  98. if (chipSelectPin == BUILTIN_SDCARD) {
  99. chipSelectPin_ = BUILTIN_SDCARD;
  100. uint8_t ret = KinetisSDHC_InitCard();
  101. type_ = KinetisSDHC_GetCardType();
  102. return (ret == 0) ? true : false;
  103. }
  104. #endif
  105. return SD_init(sckRateID, chipSelectPin);
  106. }
  107. /* return the type of SD card detected during init() */
  108. uint8_t type(void) const {return type_;}
  109. /** Returns the current value, true or false, for partial block read. */
  110. uint8_t readBlock(uint32_t block, uint8_t* dst) {
  111. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  112. if (chipSelectPin_ == BUILTIN_SDCARD) {
  113. return (KinetisSDHC_ReadBlock(dst, block) == 0) ? true : false;
  114. }
  115. #endif
  116. return SD_readBlock(block, dst);
  117. }
  118. /** Return the card type: SD V1, SD V2 or SDHC */
  119. uint8_t writeBlock(uint32_t block, const uint8_t* src) {
  120. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  121. if (chipSelectPin_ == BUILTIN_SDCARD) {
  122. return (KinetisSDHC_WriteBlock(src, block) == 0) ? true : false;
  123. }
  124. #endif
  125. return SD_writeBlock(block, src);
  126. }
  127. private:
  128. uint8_t chipSelectPin_;
  129. uint8_t status_;
  130. uint8_t type_;
  131. // private functions
  132. uint8_t SD_init(uint8_t sckRateID, uint8_t chipSelectPin);
  133. uint8_t SD_readBlock(uint32_t block, uint8_t* dst);
  134. uint8_t SD_writeBlock(uint32_t blockNumber, const uint8_t* src);
  135. uint8_t cardAcmd(uint8_t cmd, uint32_t arg) {
  136. cardCommand(CMD55, 0);
  137. return cardCommand(cmd, arg);
  138. }
  139. uint8_t cardCommand(uint8_t cmd, uint32_t arg);
  140. uint8_t sendWriteCommand(uint32_t blockNumber, uint32_t eraseCount);
  141. void chipSelectHigh(void);
  142. void chipSelectLow(void);
  143. uint8_t waitNotBusy(uint16_t timeoutMillis);
  144. uint8_t writeData(uint8_t token, const uint8_t* src);
  145. uint8_t waitStartBlock(void);
  146. uint8_t setSckRate(uint8_t sckRateID);
  147. };
  148. #endif // Sd2Card_h