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.

120 lines
3.4KB

  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 Class for software SPI.
  28. */
  29. #ifndef SdSpiSoftDriver_h
  30. #define SdSpiSoftDriver_h
  31. #include "../DigitalIO/SoftSPI.h"
  32. /**
  33. * \class SdSpiSoftDriver
  34. * \brief Base class for external soft SPI.
  35. */
  36. class SdSpiSoftDriver {
  37. public:
  38. /** Activate SPI hardware. */
  39. void activate() {}
  40. /** Initialize the SPI bus. */
  41. virtual void begin() = 0;
  42. /** Initialize the SPI bus.
  43. *
  44. * \param[in] spiConfig SD card configuration.
  45. */
  46. void begin(SdSpiConfig spiConfig) {
  47. (void)spiConfig;
  48. begin();
  49. }
  50. /** Deactivate SPI hardware. */
  51. void deactivate() {}
  52. /** Receive a byte.
  53. *
  54. * \return The byte.
  55. */
  56. virtual uint8_t receive() = 0;
  57. /** Receive multiple bytes.
  58. *
  59. * \param[out] buf Buffer to receive the data.
  60. * \param[in] count Number of bytes to receive.
  61. *
  62. * \return Zero for no error or nonzero error code.
  63. */
  64. uint8_t receive(uint8_t* buf, size_t count) {
  65. for (size_t i = 0; i < count; i++) {
  66. buf[i] = receive();
  67. }
  68. return 0;
  69. }
  70. /** Send a byte.
  71. *
  72. * \param[in] data Byte to send
  73. */
  74. virtual void send(uint8_t data) = 0;
  75. /** Send multiple bytes.
  76. *
  77. * \param[in] buf Buffer for data to be sent.
  78. * \param[in] count Number of bytes to send.
  79. */
  80. void send(const uint8_t* buf, size_t count) {
  81. for (size_t i = 0; i < count; i++) {
  82. send(buf[i]);
  83. }
  84. }
  85. /** Save high speed SPISettings after SD initialization.
  86. *
  87. * \param[in] maxSck Maximum SCK frequency.
  88. */
  89. void setSckSpeed(uint32_t maxSck) {
  90. (void)maxSck;
  91. }
  92. };
  93. //------------------------------------------------------------------------------
  94. /**
  95. * \class SoftSpiDriver
  96. * \brief Class for external soft SPI.
  97. */
  98. template<uint8_t MisoPin, uint8_t MosiPin, uint8_t SckPin>
  99. class SoftSpiDriver : public SdSpiSoftDriver {
  100. public:
  101. /** Initialize the SPI bus. */
  102. void begin() {m_spi.begin();}
  103. /** Receive a byte.
  104. *
  105. * \return The byte.
  106. */
  107. uint8_t receive() {return m_spi.receive();}
  108. /** Send a byte.
  109. *
  110. * \param[in] data Byte to send
  111. */
  112. void send(uint8_t data) {m_spi.send(data);}
  113. private:
  114. SoftSPI<MisoPin, MosiPin, SckPin, 0> m_spi;
  115. };
  116. /** Typedef for use of SdSoftSpiDriver */
  117. typedef SdSpiSoftDriver SdSpiDriver;
  118. #endif // SdSpiSoftDriver_h