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
38{
39public:
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
76private:
77
78 /// Delay routine for bus timing.
79 void delayPeriod();
80
81private:
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
Base class for SPI interfaces.
Definition RHGenericSPI.h:31
DataMode
Defines constants for different SPI modes.
Definition RHGenericSPI.h:41
@ DataMode0
SPI Mode 0: CPOL = 0, CPHA = 0.
Definition RHGenericSPI.h:42
BitOrder
Defines constants for different SPI endianness.
Definition RHGenericSPI.h:71
@ BitOrderMSBFirst
SPI MSB first.
Definition RHGenericSPI.h:72
Frequency
Defines constants for different SPI bus frequencies.
Definition RHGenericSPI.h:56
@ Frequency1MHz
SPI bus frequency close to 1MHz.
Definition RHGenericSPI.h:57
Encapsulate a software SPI interface.
Definition RHSoftwareSPI.h:38
void end()
Definition RHSoftwareSPI.cpp:140
uint8_t transfer(uint8_t data)
Definition RHSoftwareSPI.cpp:18
void begin()
Initialise the SPI library.
Definition RHSoftwareSPI.cpp:90
void setPins(uint8_t miso=12, uint8_t mosi=11, uint8_t sck=13)
Definition RHSoftwareSPI.cpp:146