David Rimer / RadioHead-148
Embed: (wiki syntax)

« Back to documentation index

RHGenericSPI Class Reference

RHGenericSPI Class Reference

Base class for SPI interfaces. More...

#include <RHGenericSPI.h>

Inherited by RHHardwareSPI.

Public Types

enum  DataMode { DataMode0 = 0, DataMode1, DataMode2, DataMode3 }
 

Defines constants for different SPI modes.

More...
enum  Frequency {
  Frequency1MHz = 0, Frequency2MHz, Frequency4MHz, Frequency8MHz,
  Frequency16MHz
}
 

Defines constants for different SPI bus frequencies.

More...
enum  BitOrder { BitOrderMSBFirst = 0, BitOrderLSBFirst }
 

Defines constants for different SPI endianness.

More...

Public Member Functions

 RHGenericSPI (Frequency frequency=Frequency1MHz, BitOrder bitOrder=BitOrderMSBFirst, DataMode dataMode=DataMode0)
 Constructor Creates an instance of an abstract SPI interface.
virtual uint8_t transfer (uint8_t data)=0
 Transfer a single octet to and from the SPI interface.
virtual void attachInterrupt ()
 SPI Configuration methods Enable SPI interrupts (if supported) This can be used in an SPI slave to indicate when an SPI message has been received.
virtual void detachInterrupt ()
 Disable SPI interrupts (if supported) This can be used to diable the SPI interrupt in slaves where that is supported.
virtual void begin ()=0
 Initialise the SPI library.
virtual void end ()=0
 Disables the SPI bus (leaving pin modes unchanged).
virtual void setBitOrder (BitOrder bitOrder)
 Sets the bit order the SPI interface will use Sets the order of the bits shifted out of and into the SPI bus, either LSBFIRST (least-significant bit first) or MSBFIRST (most-significant bit first).
virtual void setDataMode (DataMode dataMode)
 Sets the SPI data mode: that is, clock polarity and phase.
virtual void setFrequency (Frequency frequency)
 Sets the SPI clock divider relative to the system clock.

Protected Attributes

Frequency _frequency
 The configure SPI Bus frequency, one of RHGenericSPI::Frequency.
BitOrder _bitOrder
 Bit order, one of RHGenericSPI::BitOrder.
DataMode _dataMode
 SPI bus mode, one of RHGenericSPI::DataMode.

Detailed Description

Base class for SPI interfaces.

This generic abstract class is used to encapsulate hardware or software SPI interfaces for a variety of platforms. The intention is so that driver classes can be configured to use hardware or software SPI without changing the main code.

You must provide a subclass of this class to driver constructors that require SPI. A concrete subclass that encapsualates the standard Arduino hardware SPI and a bit-banged software implementation is included.

Do not directly use this class: it must be subclassed and the following abstract functions at least must be implmented:

Definition at line 30 of file RHGenericSPI.h.


Member Enumeration Documentation

enum BitOrder

Defines constants for different SPI endianness.

Defines constants for different SPI endianness that can be passed to setBitOrder() We need to define these in a device and platform independent way, because the SPI implementation is different on each platform.

Enumerator:
BitOrderMSBFirst 

SPI MSB first.

BitOrderLSBFirst 

SPI LSB first.

Definition at line 70 of file RHGenericSPI.h.

enum DataMode

Defines constants for different SPI modes.

Defines constants for different SPI modes that can be passed to the constructor or setMode() We need to define these in a device and platform independent way, because the SPI implementation is different on each platform.

Enumerator:
DataMode0 

SPI Mode 0: CPOL = 0, CPHA = 0.

DataMode1 

SPI Mode 1: CPOL = 0, CPHA = 1.

DataMode2 

SPI Mode 2: CPOL = 1, CPHA = 0.

DataMode3 

SPI Mode 3: CPOL = 1, CPHA = 1.

Definition at line 40 of file RHGenericSPI.h.

enum Frequency

Defines constants for different SPI bus frequencies.

Defines constants for different SPI bus frequencies that can be passed to setFrequency(). The frequency you get may not be exactly the one according to the name. We need to define these in a device and platform independent way, because the SPI implementation is different on each platform.

Enumerator:
Frequency1MHz 

SPI bus frequency close to 1MHz.

Frequency2MHz 

SPI bus frequency close to 2MHz.

Frequency4MHz 

SPI bus frequency close to 4MHz.

Frequency8MHz 

SPI bus frequency close to 8MHz.

Frequency16MHz 

SPI bus frequency close to 16MHz.

Definition at line 55 of file RHGenericSPI.h.


Constructor & Destructor Documentation

RHGenericSPI ( Frequency  frequency = Frequency1MHz,
BitOrder  bitOrder = BitOrderMSBFirst,
DataMode  dataMode = DataMode0 
)

Constructor Creates an instance of an abstract SPI interface.

Do not use this contructor directly: you must instead use on of the concrete subclasses provided such as RHHardwareSPI or RHSoftwareSPI

Parameters:
[in]frequencyOne of RHGenericSPI::Frequency to select the SPI bus frequency. The frequency is mapped to the closest available bus frequency on the platform.
[in]bitOrderSelect the SPI bus bit order, one of RHGenericSPI::BitOrderMSBFirst or RHGenericSPI::BitOrderLSBFirst.
[in]dataModeSelects the SPI bus data mode. One of RHGenericSPI::DataMode

Definition at line 9 of file RHGenericSPI.cpp.


Member Function Documentation

virtual void attachInterrupt (  ) [virtual]

SPI Configuration methods Enable SPI interrupts (if supported) This can be used in an SPI slave to indicate when an SPI message has been received.

Reimplemented in RHHardwareSPI.

Definition at line 95 of file RHGenericSPI.h.

virtual void begin (  ) [pure virtual]

Initialise the SPI library.

Call this after configuring and before using the SPI library

Implemented in RHHardwareSPI, and RHHardwareSPI.

virtual void detachInterrupt (  ) [virtual]

Disable SPI interrupts (if supported) This can be used to diable the SPI interrupt in slaves where that is supported.

Reimplemented in RHHardwareSPI.

Definition at line 99 of file RHGenericSPI.h.

virtual void end (  ) [pure virtual]

Disables the SPI bus (leaving pin modes unchanged).

Call this after you have finished using the SPI interface

Implemented in RHHardwareSPI, and RHHardwareSPI.

void setBitOrder ( BitOrder  bitOrder ) [virtual]

Sets the bit order the SPI interface will use Sets the order of the bits shifted out of and into the SPI bus, either LSBFIRST (least-significant bit first) or MSBFIRST (most-significant bit first).

Parameters:
[in]bitOrderBit order to be used: one of RHGenericSPI::BitOrder

Definition at line 17 of file RHGenericSPI.cpp.

void setDataMode ( DataMode  dataMode ) [virtual]

Sets the SPI data mode: that is, clock polarity and phase.

See the Wikipedia article on SPI for details.

Parameters:
[in]dataModeThe mode to use: one of RHGenericSPI::DataMode

Definition at line 22 of file RHGenericSPI.cpp.

void setFrequency ( Frequency  frequency ) [virtual]

Sets the SPI clock divider relative to the system clock.

On AVR based boards, the dividers available are 2, 4, 8, 16, 32, 64 or 128. The default setting is SPI_CLOCK_DIV4, which sets the SPI clock to one-quarter the frequency of the system clock (4 Mhz for the boards at 16 MHz).

Parameters:
[in]frequencyThe data rate to use: one of RHGenericSPI::Frequency

Definition at line 27 of file RHGenericSPI.cpp.

virtual uint8_t transfer ( uint8_t  data ) [pure virtual]

Transfer a single octet to and from the SPI interface.

Parameters:
[in]dataThe octet to send
Returns:
The octet read from SPI while the data octet was sent

Implemented in RHHardwareSPI, and RHHardwareSPI.


Field Documentation

BitOrder _bitOrder [protected]

Bit order, one of RHGenericSPI::BitOrder.

Definition at line 132 of file RHGenericSPI.h.

DataMode _dataMode [protected]

SPI bus mode, one of RHGenericSPI::DataMode.

Definition at line 135 of file RHGenericSPI.h.

Frequency _frequency [protected]

The configure SPI Bus frequency, one of RHGenericSPI::Frequency.

Definition at line 129 of file RHGenericSPI.h.