SdSpiSoftDriver.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 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. /** deactivate SPI driver. */
  53. void end() {}
  54. /** Receive a byte.
  55. *
  56. * \return The byte.
  57. */
  58. virtual uint8_t receive() = 0;
  59. /** Receive multiple bytes.
  60. *
  61. * \param[out] buf Buffer to receive the data.
  62. * \param[in] count Number of bytes to receive.
  63. *
  64. * \return Zero for no error or nonzero error code.
  65. */
  66. uint8_t receive(uint8_t* buf, size_t count) {
  67. for (size_t i = 0; i < count; i++) {
  68. buf[i] = receive();
  69. }
  70. return 0;
  71. }
  72. /** Send a byte.
  73. *
  74. * \param[in] data Byte to send
  75. */
  76. virtual void send(uint8_t data) = 0;
  77. /** Send multiple bytes.
  78. *
  79. * \param[in] buf Buffer for data to be sent.
  80. * \param[in] count Number of bytes to send.
  81. */
  82. void send(const uint8_t* buf, size_t count) {
  83. for (size_t i = 0; i < count; i++) {
  84. send(buf[i]);
  85. }
  86. }
  87. /** Save high speed SPISettings after SD initialization.
  88. *
  89. * \param[in] maxSck Maximum SCK frequency.
  90. */
  91. void setSckSpeed(uint32_t maxSck) {
  92. (void)maxSck;
  93. }
  94. };
  95. //------------------------------------------------------------------------------
  96. /**
  97. * \class SoftSpiDriver
  98. * \brief Class for external soft SPI.
  99. */
  100. template<uint8_t MisoPin, uint8_t MosiPin, uint8_t SckPin>
  101. class SoftSpiDriver : public SdSpiSoftDriver {
  102. public:
  103. /** Initialize the SPI bus. */
  104. void begin() {m_spi.begin();}
  105. /** Receive a byte.
  106. *
  107. * \return The byte.
  108. */
  109. uint8_t receive() {return m_spi.receive();}
  110. /** Send a byte.
  111. *
  112. * \param[in] data Byte to send
  113. */
  114. void send(uint8_t data) {m_spi.send(data);}
  115. private:
  116. SoftSPI<MisoPin, MosiPin, SckPin, 0> m_spi;
  117. };
  118. /** Typedef for use of SdSoftSpiDriver */
  119. typedef SdSpiSoftDriver SdSpiDriver;
  120. #endif // SdSpiSoftDriver_h