SdSpiDriver.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 SpiDriver classes
  28. */
  29. #ifndef SdSpiDriver_h
  30. #define SdSpiDriver_h
  31. #include "../common/SysCall.h"
  32. /**
  33. * Initialize SD chip select pin.
  34. *
  35. * \param[in] pin SD card chip select pin.
  36. */
  37. void sdCsInit(SdCsPin_t pin);
  38. /**
  39. * Initialize SD chip select pin.
  40. *
  41. * \param[in] pin SD card chip select pin.
  42. * \param[in] level SD card chip select level.
  43. */
  44. void sdCsWrite(SdCsPin_t pin, bool level);
  45. //------------------------------------------------------------------------------
  46. /** SPI bus is share with other devices. */
  47. const uint8_t SHARED_SPI = 0;
  48. #if ENABLE_DEDICATED_SPI
  49. /** The SD is the only device on the SPI bus. */
  50. const uint8_t DEDICATED_SPI = 1;
  51. /**
  52. * \param[in] opt option field of SdSpiConfig.
  53. * \return true for dedicated SPI.
  54. */
  55. inline bool spiOptionDedicated(uint8_t opt) {return opt & DEDICATED_SPI;}
  56. #else // ENABLE_DEDICATED_SPI
  57. /**
  58. * \param[in] opt option field of SdSpiConfig.
  59. * \return true for dedicated SPI.
  60. */
  61. inline bool spiOptionDedicated(uint8_t opt) {(void)opt; return false;}
  62. #endif // ENABLE_DEDICATED_SPI
  63. //------------------------------------------------------------------------------
  64. /** SPISettings for SCK frequency in Hz. */
  65. #define SD_SCK_HZ(maxSpeed) (maxSpeed)
  66. /** SPISettings for SCK frequency in MHz. */
  67. #define SD_SCK_MHZ(maxMhz) (1000000UL*(maxMhz))
  68. // SPI divisor constants - obsolete.
  69. /** Set SCK to max rate. */
  70. #define SPI_FULL_SPEED SD_SCK_MHZ(50)
  71. /** Set SCK rate to 16 MHz for Due */
  72. #define SPI_DIV3_SPEED SD_SCK_MHZ(16)
  73. /** Set SCK rate to 4 MHz for AVR. */
  74. #define SPI_HALF_SPEED SD_SCK_MHZ(4)
  75. /** Set SCK rate to 8 MHz for Due */
  76. #define SPI_DIV6_SPEED SD_SCK_MHZ(8)
  77. /** Set SCK rate to 2 MHz for AVR. */
  78. #define SPI_QUARTER_SPEED SD_SCK_MHZ(2)
  79. /** Set SCK rate to 1 MHz for AVR. */
  80. #define SPI_EIGHTH_SPEED SD_SCK_MHZ(1)
  81. /** Set SCK rate to 500 kHz for AVR. */
  82. #define SPI_SIXTEENTH_SPEED SD_SCK_HZ(500000)
  83. //------------------------------------------------------------------------------
  84. #if SPI_DRIVER_SELECT < 2
  85. #include "SPI.h"
  86. /** Port type for Arduino SPI hardware driver. */
  87. typedef SPIClass SpiPort_t;
  88. #elif SPI_DRIVER_SELECT == 2
  89. class SdSpiSoftDriver;
  90. /** Port type for software SPI driver. */
  91. typedef SdSpiSoftDriver SpiPort_t;
  92. #elif SPI_DRIVER_SELECT == 3
  93. class SdSpiBaseClass;
  94. /** Port type for extrernal SPI driver. */
  95. typedef SdSpiBaseClass SpiPort_t;
  96. #else // SPI_DRIVER_SELECT
  97. typedef void* SpiPort_t;
  98. #endif // SPI_DRIVER_SELECT
  99. //------------------------------------------------------------------------------
  100. /**
  101. * \class SdSpiConfig
  102. * \brief SPI card configuration.
  103. */
  104. class SdSpiConfig {
  105. public:
  106. /** SdSpiConfig constructor.
  107. *
  108. * \param[in] cs Chip select pin.
  109. * \param[in] opt Options.
  110. * \param[in] maxSpeed Maximum SCK frequency.
  111. * \param[in] port The SPI port to use.
  112. */
  113. SdSpiConfig(SdCsPin_t cs, uint8_t opt, uint32_t maxSpeed, SpiPort_t* port) :
  114. csPin(cs), options(opt), maxSck(maxSpeed), spiPort(port) {}
  115. /** SdSpiConfig constructor.
  116. *
  117. * \param[in] cs Chip select pin.
  118. * \param[in] opt Options.
  119. * \param[in] maxSpeed Maximum SCK frequency.
  120. */
  121. SdSpiConfig(SdCsPin_t cs, uint8_t opt, uint32_t maxSpeed) :
  122. csPin(cs), options(opt), maxSck(maxSpeed) {}
  123. /** SdSpiConfig constructor.
  124. *
  125. * \param[in] cs Chip select pin.
  126. * \param[in] opt Options.
  127. */
  128. SdSpiConfig(SdCsPin_t cs, uint8_t opt) : csPin(cs), options(opt) {}
  129. /** SdSpiConfig constructor.
  130. *
  131. * \param[in] cs Chip select pin.
  132. */
  133. explicit SdSpiConfig(SdCsPin_t cs) : csPin(cs) {}
  134. /** Chip select pin. */
  135. const SdCsPin_t csPin;
  136. /** Options */
  137. const uint8_t options = SHARED_SPI;
  138. /** Max SCK frequency */
  139. const uint32_t maxSck = SD_SCK_MHZ(50);
  140. /** SPI port */
  141. SpiPort_t* spiPort = nullptr;
  142. };
  143. #if SPI_DRIVER_SELECT < 2
  144. #include "SdSpiArduinoDriver.h"
  145. #elif SPI_DRIVER_SELECT == 2
  146. #include "SdSpiSoftDriver.h"
  147. #elif SPI_DRIVER_SELECT == 3
  148. #include "SdSpiBaseClass.h"
  149. typedef SdSpiBaseClass SdSpiDriver;
  150. #else // SPI_DRIVER_SELECT
  151. #error Invalid SPI_DRIVER_SELECT
  152. #endif // SPI_DRIVER_SELECT
  153. #endif // SdSpiDriver_h