Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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.
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:
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] frequency One of RHGenericSPI::Frequency to select the SPI bus frequency. The frequency is mapped to the closest available bus frequency on the platform. [in] bitOrder Select the SPI bus bit order, one of RHGenericSPI::BitOrderMSBFirst or RHGenericSPI::BitOrderLSBFirst. [in] dataMode Selects 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] bitOrder Bit 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] dataMode The 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] frequency The 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] data The octet to send
- Returns:
- The octet read from SPI while the data octet was sent
Implemented in RHHardwareSPI, and RHHardwareSPI.
Field Documentation
Bit order, one of RHGenericSPI::BitOrder.
Definition at line 132 of file RHGenericSPI.h.
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.
Generated on Tue Jul 12 2022 18:05:56 by
