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