Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

174 Zeilen
4.8KB

  1. /* Arduino SdSpi Library
  2. * Copyright (C) 2013 by William Greiman
  3. *
  4. * This file is part of the Arduino SdSpi 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 SdSpi Library. If not, see
  18. * <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file
  22. * \brief SdSpi class for V2 SD/SDHC cards
  23. */
  24. #ifndef SdSpi_h
  25. #define SdSpi_h
  26. #include <Arduino.h>
  27. #include <SdFatConfig.h>
  28. #if !USE_ARDUINO_SPI_LIBRARY
  29. // AVR Arduinos
  30. #ifdef __AVR__
  31. #if AVR_SOFT_SPI
  32. #define USE_SOFTWARE_SPI 1
  33. #elif LEONARDO_SOFT_SPI && defined(__AVR_ATmega32U4__) && !defined(CORE_TEENSY)
  34. #define USE_SOFTWARE_SPI 1
  35. #elif MEGA_SOFT_SPI && (defined(__AVR_ATmega1280__)\
  36. ||defined(__AVR_ATmega2560__))
  37. #define USE_SOFTWARE_SPI 1
  38. #else // USE_SOFTWARE_SPI
  39. #define USE_NATIVE_AVR_SPI 1
  40. #endif // USE_SOFTWARE_SPI
  41. #endif // __AVR__
  42. // Due
  43. #if defined(__arm__) && !defined(CORE_TEENSY)
  44. #if DUE_SOFT_SPI
  45. #define USE_SOFTWARE_SPI 1
  46. #else // DUE_SOFT_SPI
  47. /** Nonzero - use native SAM3X SPI */
  48. #define USE_NATIVE_SAM3X_SPI 1
  49. #endif // DUE_SOFT_SPI
  50. #endif // defined(__arm__) && !defined(CORE_TEENSY)
  51. // Teensy 3.0
  52. #if defined(__arm__) && defined(CORE_TEENSY)
  53. #if TEENSY3_SOFT_SPI
  54. #define USE_SOFTWARE_SPI 1
  55. #else // TEENSY3_SOFT_SPI
  56. /** Nonzero - use native MK20DX128 SPI */
  57. #define USE_NATIVE_TEENSY3_SPI 1
  58. #endif // TEENSY3_SOFT_SPI
  59. #endif // defined(__arm__) && defined(CORE_TEENSY)
  60. #endif // !USE_ARDUINO_SPI_LIBRARY
  61. #ifndef USE_SOFTWARE_SPI
  62. #define USE_SOFTWARE_SPI 0
  63. #endif // USE_SOFTWARE_SPI
  64. #ifndef USE_NATIVE_AVR_SPI
  65. #define USE_NATIVE_AVR_SPI 0
  66. #endif
  67. #ifndef USE_NATIVE_SAM3X_SPI
  68. #define USE_NATIVE_SAM3X_SPI 0
  69. #endif // USE_NATIVE_SAM3X_SPI
  70. #ifndef USE_NATIVE_TEENSY3_SPI
  71. #define USE_NATIVE_TEENSY3_SPI 0
  72. #endif // USE_NATIVE_TEENSY3_SPI
  73. //------------------------------------------------------------------------------
  74. // define default chip select pin
  75. //
  76. #if !USE_SOFTWARE_SPI
  77. /** The default chip select pin for the SD card is SS. */
  78. uint8_t const SD_CHIP_SELECT_PIN = SS;
  79. #else // USE_AVR_SOFTWARE_SPI
  80. /** SPI chip select pin for software SPI. */
  81. uint8_t const SD_CHIP_SELECT_PIN = SOFT_SPI_CS_PIN;
  82. #endif // USE_AVR_SOFTWARE_SPI
  83. //------------------------------------------------------------------------------
  84. /**
  85. * \class SdSpi
  86. * \brief SPI class for access to SD and SDHC flash memory cards.
  87. */
  88. class SdSpi {
  89. public:
  90. /** Initialize the SPI bus */
  91. void begin();
  92. /** Set SPI options for access to SD/SDHC cards.
  93. *
  94. * \param[in] spiDivisor SCK clock divider relative to the system clock.
  95. */
  96. void init(uint8_t spiDivisor);
  97. /** Receive a byte.
  98. *
  99. * \return The byte.
  100. */
  101. uint8_t receive();
  102. /** Receive multiple bytes.
  103. *
  104. * \param[out] buf Buffer to receive the data.
  105. * \param[in] n Number of bytes to receive.
  106. *
  107. * \return Zero for no error or nonzero error code.
  108. */
  109. uint8_t receive(uint8_t* buf, size_t n);
  110. /** Send a byte.
  111. *
  112. * \param[in] data Byte to send
  113. */
  114. void send(uint8_t data);
  115. /** Send multiple bytes.
  116. *
  117. * \param[in] buf Buffer for data to be sent.
  118. * \param[in] n Number of bytes to send.
  119. */
  120. void send(const uint8_t* buf, size_t n);
  121. };
  122. //------------------------------------------------------------------------------
  123. // Use of inline for AVR results in up to 10% better write performance.
  124. // Inline also save a little flash memory.
  125. /** inline avr native functions if nonzero. */
  126. #define USE_AVR_NATIVE_SPI_INLINE 1
  127. #if USE_NATIVE_AVR_SPI && USE_AVR_NATIVE_SPI_INLINE
  128. inline uint8_t SdSpi::receive() {
  129. SPDR = 0XFF;
  130. while (!(SPSR & (1 << SPIF))) {}
  131. return SPDR;
  132. }
  133. inline uint8_t SdSpi::receive(uint8_t* buf, size_t n) {
  134. if (n-- == 0) return 0;
  135. SPDR = 0XFF;
  136. for (size_t i = 0; i < n; i++) {
  137. while (!(SPSR & (1 << SPIF))) {}
  138. uint8_t b = SPDR;
  139. SPDR = 0XFF;
  140. buf[i] = b;
  141. }
  142. while (!(SPSR & (1 << SPIF))) {}
  143. buf[n] = SPDR;
  144. return 0;
  145. }
  146. inline void SdSpi::send(uint8_t data) {
  147. SPDR = data;
  148. while (!(SPSR & (1 << SPIF))) {}
  149. }
  150. inline void SdSpi::send(const uint8_t* buf , size_t n) {
  151. if (n == 0) return;
  152. SPDR = buf[0];
  153. if (n > 1) {
  154. uint8_t b = buf[1];
  155. size_t i = 2;
  156. while (1) {
  157. while (!(SPSR & (1 << SPIF))) {}
  158. SPDR = b;
  159. if (i == n) break;
  160. b = buf[i++];
  161. }
  162. }
  163. while (!(SPSR & (1 << SPIF))) {}
  164. }
  165. #endif // USE_NATIVE_AVR_SPI && USE_AVR_NATIVE_SPI_INLINE
  166. #endif // SdSpi_h