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.
RHSPIDriver Class Reference
Base class for a RadioHead drivers that use the SPI bus to communicate with its transport hardware. More...
#include <RHSPIDriver.h>
Inherits RHGenericDriver.
Inherited by RH_RF22, RH_RF24, RH_RF69, and RH_RF95.
Public Types | |
enum | RHMode { RHModeInitialising = 0, RHModeSleep, RHModeIdle, RHModeTx, RHModeRx } |
Defines different operating modes for the transport hardware. More... | |
Public Member Functions | |
RHSPIDriver (PINS slaveSelectPin, RHGenericSPI &spi=hardware_spi) | |
Constructor. | |
bool | init () |
Initialise the Driver transport hardware and software. | |
uint8_t | spiRead (uint8_t reg) |
Reads a single register from the SPI device. | |
uint8_t | spiWrite (uint8_t reg, uint8_t val) |
Writes a single byte to the SPI device. | |
uint8_t | spiBurstRead (uint8_t reg, uint8_t *dest, uint8_t len) |
Reads a number of consecutive registers from the SPI device using burst read mode. | |
uint8_t | spiBurstWrite (uint8_t reg, const uint8_t *src, uint8_t len) |
Write a number of consecutive registers using burst write mode. | |
virtual bool | available ()=0 |
Tests whether a new message is available from the Driver. | |
virtual bool | recv (uint8_t *buf, uint8_t *len)=0 |
Turns the receiver on if it not already on. | |
virtual bool | send (const uint8_t *data, uint8_t len)=0 |
Waits until any previous transmit packet is finished being transmitted with waitPacketSent(). | |
virtual uint8_t | maxMessageLength ()=0 |
Returns the maximum message length available in this Driver. | |
virtual void | waitAvailable () |
Starts the receiver and blocks until a valid received message is available. | |
virtual bool | waitPacketSent () |
Blocks until the transmitter is no longer transmitting. | |
virtual bool | waitPacketSent (uint16_t timeout) |
Blocks until the transmitter is no longer transmitting. | |
virtual bool | waitAvailableTimeout (uint16_t timeout) |
Starts the receiver and blocks until a received message is available or a timeout. | |
virtual void | setThisAddress (uint8_t thisAddress) |
Sets the address of this node. | |
virtual void | setHeaderTo (uint8_t to) |
Sets the TO header to be sent in all subsequent messages. | |
virtual void | setHeaderFrom (uint8_t from) |
Sets the FROM header to be sent in all subsequent messages. | |
virtual void | setHeaderId (uint8_t id) |
Sets the ID header to be sent in all subsequent messages. | |
virtual void | setHeaderFlags (uint8_t set, uint8_t clear=RH_FLAGS_APPLICATION_SPECIFIC) |
Sets and clears bits in the FLAGS header to be sent in all subsequent messages First it clears he FLAGS according to the clear argument, then sets the flags according to the set argument. | |
virtual void | setPromiscuous (bool promiscuous) |
Tells the receiver to accept messages with any TO address, not just messages addressed to thisAddress or the broadcast address. | |
virtual uint8_t | headerTo () |
Returns the TO header of the last received message. | |
virtual uint8_t | headerFrom () |
Returns the FROM header of the last received message. | |
virtual uint8_t | headerId () |
Returns the ID header of the last received message. | |
virtual uint8_t | headerFlags () |
Returns the FLAGS header of the last received message. | |
int8_t | lastRssi () |
Returns the most recent RSSI (Receiver Signal Strength Indicator). | |
RHMode | mode () |
Returns the operating mode of the library. | |
void | setMode (RHMode mode) |
Sets the operating mode of the transport. | |
virtual bool | sleep () |
Sets the transport hardware into low-power sleep mode (if supported). | |
uint16_t | rxBad () |
Returns the count of the number of bad received packets (ie packets with bad lengths, checksum etc) which were rejected and not delivered to the application. | |
uint16_t | rxGood () |
Returns the count of the number of good received packets. | |
uint16_t | txGood () |
Returns the count of the number of packets successfully transmitted (though not necessarily received by the destination) | |
Static Public Member Functions | |
static void | printBuffer (const char *prompt, const uint8_t *buf, uint8_t len) |
Prints a data buffer in HEX. | |
Protected Attributes | |
RHGenericSPI & | _spi |
Reference to the RHGenericSPI instance to use to trasnfer data with teh SPI device. | |
DigitalOut | _slaveSelectPin |
The pin number of the Slave Selct pin that is used to select the desired device. | |
volatile RHMode | _mode |
The current transport operating mode. | |
uint8_t | _thisAddress |
This node id. | |
bool | _promiscuous |
Whether the transport is in promiscuous mode. | |
volatile uint8_t | _rxHeaderTo |
TO header in the last received mesasge. | |
volatile uint8_t | _rxHeaderFrom |
FROM header in the last received mesasge. | |
volatile uint8_t | _rxHeaderId |
ID header in the last received mesasge. | |
volatile uint8_t | _rxHeaderFlags |
FLAGS header in the last received mesasge. | |
uint8_t | _txHeaderTo |
TO header to send in all messages. | |
uint8_t | _txHeaderFrom |
FROM header to send in all messages. | |
uint8_t | _txHeaderId |
ID header to send in all messages. | |
uint8_t | _txHeaderFlags |
FLAGS header to send in all messages. | |
volatile int8_t | _lastRssi |
The value of the last received RSSI value, in some transport specific units. | |
volatile uint16_t | _rxBad |
Count of the number of bad messages (eg bad checksum etc) received. | |
volatile uint16_t | _rxGood |
Count of the number of successfully transmitted messaged. | |
volatile uint16_t | _txGood |
Count of the number of bad messages (correct checksum etc) received. |
Detailed Description
Base class for a RadioHead drivers that use the SPI bus to communicate with its transport hardware.
This class can be subclassed by Drivers that require to use the SPI bus. It can be configured to use either the RHHardwareSPI class (if there is one available on the platform) of the bitbanged RHSoftwareSPI class. The default behaviour is to use a pre-instantiated built-in RHHardwareSPI interface.
SPI bus access is protected by ATOMIC_BLOCK_START and ATOMIC_BLOCK_END, which will ensure interrupts are disabled during access.
The read and write routines implement commonly used SPI conventions: specifically that the MSB of the first byte transmitted indicates that it is a write and the remaining bits indicate the rehgister to access) This can be overriden in subclasses if necessaryor an alternative class, RHNRFSPIDriver can be used to access devices like Nordic NRF series radios, which have different requirements.
Application developers are not expected to instantiate this class directly: it is for the use of Driver developers.
Definition at line 38 of file RHSPIDriver.h.
Member Enumeration Documentation
enum RHMode [inherited] |
Defines different operating modes for the transport hardware.
These are the different values that can be adopted by the _mode variable and returned by the mode() member function,
- Enumerator:
RHModeInitialising Transport is initialising. Initial default value until init() is called..
RHModeSleep Transport hardware is in low power sleep mode (if supported)
RHModeIdle Transport is idle.
RHModeTx Transport is in the process of transmitting a message.
RHModeRx Transport is in the process of receiving a message.
Definition at line 45 of file RHGenericDriver.h.
Constructor & Destructor Documentation
RHSPIDriver | ( | PINS | slaveSelectPin, |
RHGenericSPI & | spi = hardware_spi |
||
) |
Constructor.
- Parameters:
-
[in] slaveSelectPin The controler pin to use to select the desired SPI device. This pin will be driven LOW during SPI communications with the SPI device that uis iused by this Driver. [in] spi Reference to the SPI interface to use. The default is to use a default built-in Hardware interface.
Definition at line 8 of file RHSPIDriver.cpp.
Member Function Documentation
virtual bool available | ( | ) | [pure virtual, inherited] |
Tests whether a new message is available from the Driver.
On most drivers, if there is an uncollected received message, and there is no message currently bing transmitted, this will also put the Driver into RHModeRx mode until a message is actually received by the transport, when it will be returned to RHModeIdle. This can be called multiple times in a timeout loop.
- Returns:
- true if a new, complete, error-free uncollected message is available to be retreived by recv().
Implemented in RH_NRF24, RH_NRF51, RH_NRF905, RH_RF22, RH_RF24, RH_RF69, RH_RF95, and RH_TCP.
uint8_t headerFlags | ( | ) | [virtual, inherited] |
Returns the FLAGS header of the last received message.
- Returns:
- The FLAGS header
Definition at line 119 of file RHGenericDriver.cpp.
uint8_t headerFrom | ( | ) | [virtual, inherited] |
Returns the FROM header of the last received message.
- Returns:
- The FROM header
Definition at line 109 of file RHGenericDriver.cpp.
uint8_t headerId | ( | ) | [virtual, inherited] |
Returns the ID header of the last received message.
- Returns:
- The ID header
Definition at line 114 of file RHGenericDriver.cpp.
uint8_t headerTo | ( | ) | [virtual, inherited] |
Returns the TO header of the last received message.
- Returns:
- The TO header
Definition at line 104 of file RHGenericDriver.cpp.
bool init | ( | ) | [virtual] |
Initialise the Driver transport hardware and software.
Make sure the Driver is properly configured before calling init().
- Returns:
- true if initialisation succeeded.
Reimplemented from RHGenericDriver.
Reimplemented in RH_RF22, RH_RF24, RH_RF69, and RH_RF95.
Definition at line 15 of file RHSPIDriver.cpp.
int8_t lastRssi | ( | ) | [inherited] |
Returns the most recent RSSI (Receiver Signal Strength Indicator).
Usually it is the RSSI of the last received message, which is measured when the preamble is received. If you called readRssi() more recently, it will return that more recent value.
- Returns:
- The most recent RSSI measurement in dBm.
Definition at line 124 of file RHGenericDriver.cpp.
virtual uint8_t maxMessageLength | ( | ) | [pure virtual, inherited] |
RHGenericDriver::RHMode mode | ( | ) | [inherited] |
Returns the operating mode of the library.
- Returns:
- the current mode, one of RF69_MODE_*
Definition at line 129 of file RHGenericDriver.cpp.
void printBuffer | ( | const char * | prompt, |
const uint8_t * | buf, | ||
uint8_t | len | ||
) | [static, inherited] |
Prints a data buffer in HEX.
For diagnostic use
- Parameters:
-
[in] prompt string to preface the print [in] buf Location of the buffer to print [in] len Length of the buffer in octets.
Definition at line 145 of file RHGenericDriver.cpp.
virtual bool recv | ( | uint8_t * | buf, |
uint8_t * | len | ||
) | [pure virtual, inherited] |
Turns the receiver on if it not already on.
If there is a valid message available, copy it to buf and return true else return false. If a message is copied, *len is set to the length (Caution, 0 length messages are permitted). You should be sure to call this function frequently enough to not miss any messages It is recommended that you call it in your main loop.
- Parameters:
-
[in] buf Location to copy the received message [in,out] len Pointer to available space in buf. Set to the actual number of octets copied.
- Returns:
- true if a valid message was copied to buf
Implemented in RH_NRF24, RH_NRF51, RH_NRF905, RH_RF22, RH_RF24, RH_RF69, RH_RF95, and RH_TCP.
uint16_t rxBad | ( | ) | [inherited] |
Returns the count of the number of bad received packets (ie packets with bad lengths, checksum etc) which were rejected and not delivered to the application.
Caution: not all drivers can correctly report this count. Some underlying hardware only report good packets.
- Returns:
- The number of bad packets received.
Definition at line 165 of file RHGenericDriver.cpp.
uint16_t rxGood | ( | ) | [inherited] |
Returns the count of the number of good received packets.
- Returns:
- The number of good packets received.
Definition at line 170 of file RHGenericDriver.cpp.
virtual bool send | ( | const uint8_t * | data, |
uint8_t | len | ||
) | [pure virtual, inherited] |
Waits until any previous transmit packet is finished being transmitted with waitPacketSent().
Then loads a message into the transmitter and starts the transmitter. Note that a message length of 0 is NOT permitted. If the message is too long for the underlying radio technology, send() will return false and will not send the message.
- Parameters:
-
[in] data Array of data to be sent [in] len Number of bytes of data to send (> 0)
- Returns:
- true if the message length was valid and it was correctly queued for transmit
Implemented in RH_NRF24, RH_NRF51, RH_NRF905, RH_RF22, RH_RF24, RH_RF69, RH_RF95, and RH_TCP.
void setHeaderFlags | ( | uint8_t | set, |
uint8_t | clear = RH_FLAGS_APPLICATION_SPECIFIC |
||
) | [virtual, inherited] |
Sets and clears bits in the FLAGS header to be sent in all subsequent messages First it clears he FLAGS according to the clear argument, then sets the flags according to the set argument.
The default for clear always clears the application specific flags.
- Parameters:
-
[in] set bitmask of bits to be set. Flags are cleared with the clear mask before being set. [in] clear bitmask of flags to clear. Defaults to RH_FLAGS_APPLICATION_SPECIFIC which clears the application specific flags, resultiung in new application specific flags identical to the set.
Definition at line 98 of file RHGenericDriver.cpp.
void setHeaderFrom | ( | uint8_t | from ) | [virtual, inherited] |
Sets the FROM header to be sent in all subsequent messages.
- Parameters:
-
[in] from The new FROM header value
Definition at line 88 of file RHGenericDriver.cpp.
void setHeaderId | ( | uint8_t | id ) | [virtual, inherited] |
Sets the ID header to be sent in all subsequent messages.
- Parameters:
-
[in] id The new ID header value
Definition at line 93 of file RHGenericDriver.cpp.
void setHeaderTo | ( | uint8_t | to ) | [virtual, inherited] |
Sets the TO header to be sent in all subsequent messages.
- Parameters:
-
[in] to The new TO header value
Definition at line 83 of file RHGenericDriver.cpp.
void setMode | ( | RHMode | mode ) | [inherited] |
Sets the operating mode of the transport.
Definition at line 134 of file RHGenericDriver.cpp.
void setPromiscuous | ( | bool | promiscuous ) | [virtual, inherited] |
Tells the receiver to accept messages with any TO address, not just messages addressed to thisAddress or the broadcast address.
- Parameters:
-
[in] promiscuous true if you wish to receive messages with any TO address
Reimplemented in RH_RF22.
Definition at line 73 of file RHGenericDriver.cpp.
void setThisAddress | ( | uint8_t | thisAddress ) | [virtual, inherited] |
Sets the address of this node.
Defaults to 0xFF. Subclasses or the user may want to change this. This will be used to test the adddress in incoming messages. In non-promiscuous mode, only messages with a TO header the same as thisAddress or the broadcast addess (0xFF) will be accepted. In promiscuous mode, all messages will be accepted regardless of the TO header. In a conventional multinode system, all nodes will have a unique address (which you could store in EEPROM). You would normally set the header FROM address to be the same as thisAddress (though you dont have to, allowing the possibilty of address spoofing).
- Parameters:
-
[in] thisAddress The address of this node.
Reimplemented in RH_RF22, and RH_TCP.
Definition at line 78 of file RHGenericDriver.cpp.
bool sleep | ( | ) | [virtual, inherited] |
Sets the transport hardware into low-power sleep mode (if supported).
May be overridden by specific drivers to initialte sleep mode. If successful, the transport will stay in sleep mode until woken by changing mode it idle, transmit or receive (eg by calling send(), recv(), available() etc)
- Returns:
- true if sleep mode is supported by transport hardware and the RadioHead driver, and if sleep mode was successfully entered. If sleep mode is not suported, return false.
Reimplemented in RH_NRF24, RH_RF22, RH_RF24, RH_RF69, and RH_RF95.
Definition at line 139 of file RHGenericDriver.cpp.
uint8_t spiBurstRead | ( | uint8_t | reg, |
uint8_t * | dest, | ||
uint8_t | len | ||
) |
Reads a number of consecutive registers from the SPI device using burst read mode.
- Parameters:
-
[in] reg Register number of the first register [in] dest Array to write the register values to. Must be at least len bytes [in] len Number of bytes to read
- Returns:
- Some devices return a status byte during the first data transfer. This byte is returned. it may or may not be meaningfule depending on the the type of device being accessed.
Definition at line 56 of file RHSPIDriver.cpp.
uint8_t spiBurstWrite | ( | uint8_t | reg, |
const uint8_t * | src, | ||
uint8_t | len | ||
) |
Write a number of consecutive registers using burst write mode.
- Parameters:
-
[in] reg Register number of the first register [in] src Array of new register values to write. Must be at least len bytes [in] len Number of bytes to write
- Returns:
- Some devices return a status byte during the first data transfer. This byte is returned. it may or may not be meaningfule depending on the the type of device being accessed.
Definition at line 69 of file RHSPIDriver.cpp.
uint8_t spiRead | ( | uint8_t | reg ) |
Reads a single register from the SPI device.
- Parameters:
-
[in] reg Register number
- Returns:
- The value of the register
Definition at line 32 of file RHSPIDriver.cpp.
uint8_t spiWrite | ( | uint8_t | reg, |
uint8_t | val | ||
) |
Writes a single byte to the SPI device.
- Parameters:
-
[in] reg Register number [in] val The value to write
- Returns:
- Some devices return a status byte during the first data transfer. This byte is returned. it may or may not be meaningfule depending on the the type of device being accessed.
Definition at line 44 of file RHSPIDriver.cpp.
uint16_t txGood | ( | ) | [inherited] |
Returns the count of the number of packets successfully transmitted (though not necessarily received by the destination)
- Returns:
- The number of packets successfully transmitted
Definition at line 175 of file RHGenericDriver.cpp.
void waitAvailable | ( | ) | [virtual, inherited] |
Starts the receiver and blocks until a valid received message is available.
Reimplemented in RH_TCP.
Definition at line 33 of file RHGenericDriver.cpp.
bool waitAvailableTimeout | ( | uint16_t | timeout ) | [virtual, inherited] |
Starts the receiver and blocks until a received message is available or a timeout.
- Parameters:
-
[in] timeout Maximum time to wait in milliseconds.
- Returns:
- true if a message is available
Reimplemented in RH_TCP.
Definition at line 42 of file RHGenericDriver.cpp.
bool waitPacketSent | ( | uint16_t | timeout ) | [virtual, inherited] |
Blocks until the transmitter is no longer transmitting.
or until the timeout occuers, whichever happens first
- Parameters:
-
[in] timeout Maximum time to wait in milliseconds.
- Returns:
- true if the RF22 completed transmission within the timeout period. False if it timed out.
Definition at line 61 of file RHGenericDriver.cpp.
bool waitPacketSent | ( | ) | [virtual, inherited] |
Blocks until the transmitter is no longer transmitting.
Reimplemented in RH_NRF24, RH_NRF51, and RH_NRF905.
Definition at line 54 of file RHGenericDriver.cpp.
Field Documentation
volatile int8_t _lastRssi [protected, inherited] |
The value of the last received RSSI value, in some transport specific units.
Definition at line 249 of file RHGenericDriver.h.
The current transport operating mode.
Definition at line 216 of file RHGenericDriver.h.
bool _promiscuous [protected, inherited] |
Whether the transport is in promiscuous mode.
Definition at line 222 of file RHGenericDriver.h.
volatile uint16_t _rxBad [protected, inherited] |
Count of the number of bad messages (eg bad checksum etc) received.
Definition at line 252 of file RHGenericDriver.h.
volatile uint16_t _rxGood [protected, inherited] |
Count of the number of successfully transmitted messaged.
Definition at line 255 of file RHGenericDriver.h.
volatile uint8_t _rxHeaderFlags [protected, inherited] |
FLAGS header in the last received mesasge.
Definition at line 234 of file RHGenericDriver.h.
volatile uint8_t _rxHeaderFrom [protected, inherited] |
FROM header in the last received mesasge.
Definition at line 228 of file RHGenericDriver.h.
volatile uint8_t _rxHeaderId [protected, inherited] |
ID header in the last received mesasge.
Definition at line 231 of file RHGenericDriver.h.
volatile uint8_t _rxHeaderTo [protected, inherited] |
TO header in the last received mesasge.
Definition at line 225 of file RHGenericDriver.h.
DigitalOut _slaveSelectPin [protected] |
The pin number of the Slave Selct pin that is used to select the desired device.
Definition at line 86 of file RHSPIDriver.h.
RHGenericSPI& _spi [protected] |
Reference to the RHGenericSPI instance to use to trasnfer data with teh SPI device.
Definition at line 82 of file RHSPIDriver.h.
uint8_t _thisAddress [protected, inherited] |
This node id.
Definition at line 219 of file RHGenericDriver.h.
volatile uint16_t _txGood [protected, inherited] |
Count of the number of bad messages (correct checksum etc) received.
Definition at line 258 of file RHGenericDriver.h.
uint8_t _txHeaderFlags [protected, inherited] |
FLAGS header to send in all messages.
Definition at line 246 of file RHGenericDriver.h.
uint8_t _txHeaderFrom [protected, inherited] |
FROM header to send in all messages.
Definition at line 240 of file RHGenericDriver.h.
uint8_t _txHeaderId [protected, inherited] |
ID header to send in all messages.
Definition at line 243 of file RHGenericDriver.h.
uint8_t _txHeaderTo [protected, inherited] |
TO header to send in all messages.
Definition at line 237 of file RHGenericDriver.h.
Generated on Tue Jul 12 2022 18:05:57 by
