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.

144 line
4.8KB

  1. /**
  2. * Copyright (c) 2011-2019 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. /** SPISettings for SCK frequency in Hz. */
  47. #define SD_SCK_HZ(maxSpeed) (maxSpeed)
  48. /** SPISettings for SCK frequency in MHz. */
  49. #define SD_SCK_MHZ(maxMhz) (1000000UL*(maxMhz))
  50. // SPI divisor constants - obsolete.
  51. /** Set SCK to max rate. */
  52. #define SPI_FULL_SPEED SD_SCK_MHZ(50)
  53. /** Set SCK rate to 16 MHz for Due */
  54. #define SPI_DIV3_SPEED SD_SCK_MHZ(16)
  55. /** Set SCK rate to 4 MHz for AVR. */
  56. #define SPI_HALF_SPEED SD_SCK_MHZ(4)
  57. /** Set SCK rate to 8 MHz for Due */
  58. #define SPI_DIV6_SPEED SD_SCK_MHZ(8)
  59. /** Set SCK rate to 2 MHz for AVR. */
  60. #define SPI_QUARTER_SPEED SD_SCK_MHZ(2)
  61. /** Set SCK rate to 1 MHz for AVR. */
  62. #define SPI_EIGHTH_SPEED SD_SCK_MHZ(1)
  63. /** Set SCK rate to 500 kHz for AVR. */
  64. #define SPI_SIXTEENTH_SPEED SD_SCK_HZ(500000)
  65. //------------------------------------------------------------------------------
  66. /** The SD is the only device on the SPI bus. */
  67. #define DEDICATED_SPI 0X80
  68. /** SPI bus is share with other devices. */
  69. #define SHARED_SPI 0
  70. #if SPI_DRIVER_SELECT < 2
  71. #include "SPI.h"
  72. /** Port type for Arduino SPI hardware driver. */
  73. typedef SPIClass SpiPort_t;
  74. #elif SPI_DRIVER_SELECT == 2
  75. class SdSpiSoftDriver;
  76. /** Port type for software SPI driver. */
  77. typedef SdSpiSoftDriver SpiPort_t;
  78. #elif SPI_DRIVER_SELECT == 3
  79. class SdSpiBaseClass;
  80. /** Port type for extrernal SPI driver. */
  81. typedef SdSpiBaseClass SpiPort_t;
  82. #else // SPI_DRIVER_SELECT
  83. typedef void* SpiPort_t;
  84. #endif // SPI_DRIVER_SELECT
  85. //------------------------------------------------------------------------------
  86. /**
  87. * \class SdSpiConfig
  88. * \brief SPI card configuration.
  89. */
  90. class SdSpiConfig {
  91. public:
  92. /** SdSpiConfig constructor.
  93. *
  94. * \param[in] cs Chip select pin.
  95. * \param[in] opt Options.
  96. * \param[in] maxSpeed Maximum SCK frequency.
  97. * \param[in] port The SPI port to use.
  98. */
  99. SdSpiConfig(SdCsPin_t cs, uint8_t opt, uint32_t maxSpeed, SpiPort_t* port) :
  100. csPin(cs), options(opt), maxSck(maxSpeed), spiPort(port) {}
  101. /** SdSpiConfig constructor.
  102. *
  103. * \param[in] cs Chip select pin.
  104. * \param[in] opt Options.
  105. * \param[in] maxSpeed Maximum SCK frequency.
  106. */
  107. SdSpiConfig(SdCsPin_t cs, uint8_t opt, uint32_t maxSpeed) :
  108. csPin(cs), options(opt), maxSck(maxSpeed), spiPort(nullptr) {}
  109. /** SdSpiConfig constructor.
  110. *
  111. * \param[in] cs Chip select pin.
  112. * \param[in] opt Options.
  113. */
  114. SdSpiConfig(SdCsPin_t cs, uint8_t opt) :
  115. csPin(cs), options(opt), maxSck(SD_SCK_MHZ(50)), spiPort(nullptr) {}
  116. /** SdSpiConfig constructor.
  117. *
  118. * \param[in] cs Chip select pin.
  119. */
  120. explicit SdSpiConfig(SdCsPin_t cs) : csPin(cs), options(SHARED_SPI),
  121. maxSck(SD_SCK_MHZ(50)), spiPort(nullptr) {}
  122. /** Chip select pin. */
  123. const SdCsPin_t csPin;
  124. /** Options */
  125. const uint8_t options;
  126. /** Max SCK frequency */
  127. const uint32_t maxSck;
  128. /** SPI port */
  129. SpiPort_t* spiPort;
  130. };
  131. #if SPI_DRIVER_SELECT < 2
  132. #include "SdSpiArduinoDriver.h"
  133. #elif SPI_DRIVER_SELECT == 2
  134. #include "SdSpiSoftDriver.h"
  135. #elif SPI_DRIVER_SELECT == 3
  136. #include "SdSpiBaseClass.h"
  137. typedef SdSpiBaseClass SdSpiDriver;
  138. #else // SPI_DRIVER_SELECT
  139. #error Invalid SPI_DRIVER_SELECT
  140. #endif // SPI_DRIVER_SELECT
  141. #endif // SdSpiDriver_h