PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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.

3 年之前
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // SoftwareSPI.h
  2. // Author: Chris Lapa (chris@lapa.com.au)
  3. // Copyright (C) 2014 Chris Lapa
  4. // Contributed by Chris Lapa
  5. #ifndef RHSoftwareSPI_h
  6. #define RHSoftwareSPI_h
  7. #include <RHGenericSPI.h>
  8. /////////////////////////////////////////////////////////////////////
  9. /// \class RHSoftwareSPI RHSoftwareSPI.h <RHSoftwareSPI.h>
  10. /// \brief Encapsulate a software SPI interface
  11. ///
  12. /// This concrete subclass of RHGenericSPI enapsulates a bit-banged software SPI interface.
  13. /// Caution: this software SPI interface will be much slower than hardware SPI on most
  14. /// platforms.
  15. ///
  16. /// \par Usage
  17. ///
  18. /// Usage varies slightly depending on what driver you are using.
  19. ///
  20. /// For RF22, for example:
  21. /// \code
  22. /// #include <RHSoftwareSPI.h>
  23. /// RHSoftwareSPI spi;
  24. /// RH_RF22 driver(SS, 2, spi);
  25. /// RHReliableDatagram(driver, CLIENT_ADDRESS);
  26. /// void setup()
  27. /// {
  28. /// spi.setPins(6, 5, 7); // Or whatever SPI pins you need
  29. /// ....
  30. /// }
  31. /// \endcode
  32. class RHSoftwareSPI : public RHGenericSPI
  33. {
  34. public:
  35. /// Constructor
  36. /// Creates an instance of a bit-banged software SPI interface.
  37. /// Sets the SPI pins to the defaults of
  38. /// MISO = 12, MOSI = 11, SCK = 13. If you need other assigments, call setPins() before
  39. /// calling manager.init() or driver.init().
  40. /// \param[in] frequency One of RHGenericSPI::Frequency to select the SPI bus frequency. The frequency
  41. /// is mapped to the closest available bus frequency on the platform. CAUTION: the achieved
  42. /// frequency will almost certainly be very much slower on most platforms. eg on Arduino Uno, the
  43. /// the clock rate is likely to be at best around 46kHz.
  44. /// \param[in] bitOrder Select the SPI bus bit order, one of RHGenericSPI::BitOrderMSBFirst or
  45. /// RHGenericSPI::BitOrderLSBFirst.
  46. /// \param[in] dataMode Selects the SPI bus data mode. One of RHGenericSPI::DataMode
  47. RHSoftwareSPI(Frequency frequency = Frequency1MHz, BitOrder bitOrder = BitOrderMSBFirst, DataMode dataMode = DataMode0);
  48. /// Transfer a single octet to and from the SPI interface
  49. /// \param[in] data The octet to send
  50. /// \return The octet read from SPI while the data octet was sent.
  51. uint8_t transfer(uint8_t data);
  52. /// Initialise the software SPI library
  53. /// Call this after configuring the SPI interface and before using it to transfer data.
  54. /// Initializes the SPI bus by setting SCK, MOSI, and SS to outputs, pulling SCK and MOSI low, and SS high.
  55. void begin();
  56. /// Disables the SPI bus usually, in this case
  57. /// there is no hardware controller to disable.
  58. void end();
  59. /// Sets the pins used by this SoftwareSPIClass instance.
  60. /// The defaults are: MISO = 12, MOSI = 11, SCK = 13.
  61. /// \param[in] miso master in slave out pin used
  62. /// \param[in] mosi master out slave in pin used
  63. /// \param[in] sck clock pin used
  64. void setPins(uint8_t miso = 12, uint8_t mosi = 11, uint8_t sck = 13);
  65. private:
  66. /// Delay routine for bus timing.
  67. void delayPeriod();
  68. private:
  69. uint8_t _miso;
  70. uint8_t _mosi;
  71. uint8_t _sck;
  72. uint8_t _bitOrder;
  73. uint8_t _delayCounts;
  74. uint8_t _clockPolarity;
  75. uint8_t _clockPhase;
  76. };
  77. #endif