RadioHead
|
Send and receive unaddressed, unreliable datagrams by nRF24L01 and compatible transceivers. More...
#include <RH_NRF24.h>
Public Types | |
enum | DataRate { DataRate1Mbps = 0 , DataRate2Mbps , DataRate250kbps } |
Defines convenient values for setting data rates in setRF() More... | |
enum | TransmitPower { TransmitPowerm18dBm = 0 , TransmitPowerm12dBm , TransmitPowerm6dBm , TransmitPower0dBm , RFM73TransmitPowerm10dBm = 0 , RFM73TransmitPowerm5dBm , RFM73TransmitPowerm0dBm , RFM73TransmitPower5dBm } |
Convenient values for setting transmitter power in setRF() These are designed to agree with the values for RF_PWR in RH_NRF24_REG_06_RF_SETUP To be passed to setRF();. More... | |
Public Types inherited from RHGenericDriver | |
enum | RHMode { RHModeInitialising = 0 , RHModeSleep , RHModeIdle , RHModeTx , RHModeRx , RHModeCad } |
Defines different operating modes for the transport hardware. More... | |
Public Member Functions | |
RH_NRF24 (uint8_t chipEnablePin=8, uint8_t slaveSelectPin=SS, RHGenericSPI &spi=hardware_spi) | |
bool | init () |
uint8_t | spiReadRegister (uint8_t reg) |
uint8_t | spiWriteRegister (uint8_t reg, uint8_t val) |
uint8_t | spiBurstReadRegister (uint8_t reg, uint8_t *dest, uint8_t len) |
uint8_t | spiBurstWriteRegister (uint8_t reg, uint8_t *src, uint8_t len) |
uint8_t | statusRead () |
bool | setChannel (uint8_t channel) |
bool | setOpMode (uint8_t mode) |
bool | setNetworkAddress (uint8_t *address, uint8_t len) |
bool | setRF (DataRate data_rate, TransmitPower power) |
void | setModeIdle () |
void | setModeRx () |
void | setModeTx () |
bool | send (const uint8_t *data, uint8_t len) |
virtual bool | waitPacketSent () |
bool | isSending () |
bool | printRegisters () |
bool | available () |
bool | recv (uint8_t *buf, uint8_t *len) |
uint8_t | maxMessageLength () |
virtual bool | sleep () |
Public Member Functions inherited from RHNRFSPIDriver | |
RHNRFSPIDriver (uint8_t slaveSelectPin=SS, RHGenericSPI &spi=hardware_spi) | |
bool | init () |
uint8_t | spiCommand (uint8_t command) |
uint8_t | spiRead (uint8_t reg) |
uint8_t | spiWrite (uint8_t reg, uint8_t val) |
uint8_t | spiBurstRead (uint8_t reg, uint8_t *dest, uint8_t len) |
uint8_t | spiBurstWrite (uint8_t reg, const uint8_t *src, uint8_t len) |
void | setSlaveSelectPin (uint8_t slaveSelectPin) |
void | spiUsingInterrupt (uint8_t interruptNumber) |
Public Member Functions inherited from RHGenericDriver | |
RHGenericDriver () | |
Constructor. | |
virtual | ~RHGenericDriver () |
Generic destructor to prevent warnings when objects are dynamically allocated. | |
virtual void | waitAvailable (uint16_t polldelay=0) |
virtual bool | waitPacketSent (uint16_t timeout) |
virtual bool | waitAvailableTimeout (uint16_t timeout, uint16_t polldelay=0) |
virtual bool | waitCAD () |
void | setCADTimeout (unsigned long cad_timeout) |
virtual bool | isChannelActive () |
virtual void | setThisAddress (uint8_t thisAddress) |
virtual void | setHeaderTo (uint8_t to) |
virtual void | setHeaderFrom (uint8_t from) |
virtual void | setHeaderId (uint8_t id) |
virtual void | setHeaderFlags (uint8_t set, uint8_t clear=RH_FLAGS_APPLICATION_SPECIFIC) |
virtual void | setPromiscuous (bool promiscuous) |
virtual uint8_t | headerTo () |
virtual uint8_t | headerFrom () |
virtual uint8_t | headerId () |
virtual uint8_t | headerFlags () |
virtual int16_t | lastRssi () |
virtual RHMode | mode () |
virtual void | setMode (RHMode mode) |
Sets the operating mode of the transport. | |
virtual uint16_t | rxBad () |
virtual uint16_t | rxGood () |
virtual uint16_t | txGood () |
Protected Member Functions | |
uint8_t | flushTx () |
uint8_t | flushRx () |
void | validateRxBuf () |
Examine the receive buffer to determine whether the message is for this node. | |
void | clearRxBuf () |
Clear our local receive buffer. | |
Protected Member Functions inherited from RHNRFSPIDriver | |
virtual void | beginTransaction () |
virtual void | endTransaction () |
Signal the end of an SPI transaction. | |
Additional Inherited Members | |
Static Public Member Functions inherited from RHGenericDriver | |
static void | printBuffer (const char *prompt, const uint8_t *buf, uint8_t len) |
Protected Attributes inherited from RHNRFSPIDriver | |
RHGenericSPI & | _spi |
Reference to the RHGenericSPI instance to use to trasnfer data with teh SPI device. | |
uint8_t | _slaveSelectPin |
The pin number of the Slave Select pin that is used to select the desired device. | |
Protected Attributes inherited from RHGenericDriver | |
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 int16_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. | |
volatile bool | _cad |
Channel activity detected. | |
unsigned int | _cad_timeout |
Channel activity timeout in ms. | |
Send and receive unaddressed, unreliable datagrams by nRF24L01 and compatible transceivers.
Supported transceivers include:
This base class provides basic functions for sending and receiving unaddressed, unreliable datagrams of arbitrary length to 28 octets per packet. Use one of the Manager classes to get addressing and acknowledgement reliability, routing, meshes etc.
The nRF24L01 (http://www.sparkfun.com/datasheets/Wireless/Nordic/nRF24L01P_Product_Specification_1_0.pdf) is a low-cost 2.4GHz ISM transceiver module. It supports a number of channel frequencies in the 2.4GHz band and a range of data rates.
This library provides functions for sending and receiving messages of up to 28 octets on any frequency supported by the nRF24L01, at a selected data rate.
Several nRF24L01 modules can be connected to an Arduino, permitting the construction of translators and frequency changers, etc.
The nRF24 transceiver is configured to use Enhanced Shockburst with no acknowledgement and no retransmits. TX_ADDR and RX_ADDR_P0 are set to the network address. If you need the low level auto-acknowledgement feature supported by this chip, you can use our original NRF24 library at http://www.airspayce.com/mikem/arduino/NRF24
Naturally, for any 2 radios to communicate that must be configured to use the same frequency and data rate, and with identical network addresses.
Example Arduino programs are included to show the main modes of use.
All messages sent and received by this class conform to this packet format, as specified by the nRF24L01 product specification:
The electrical connection between the nRF24L01 and the Arduino require 3.3V, the 3 x SPI pins (SCK, SDI, SDO), a Chip Enable pin and a Slave Select pin. If you are using the Sparkfun WRL-00691 module, it has a voltage regulator on board and can be should with 5V VCC if possible. The examples below assume the Sparkfun WRL-00691 module
Connect the nRF24L01 to most Arduino's like this (Caution, Arduino Mega has different pins for SPI, see below). Use these same connections for Teensy 3.1 (use 3.3V not 5V Vcc).
For an Arduino Leonardo (the SPI pins do not come out on the Digital pins as for normal Arduino, but only appear on the ICSP header)
and initialise the NRF24 object like this to explicitly set the SS pin NRF24 nrf24(8, 10);
For an Arduino Due (the SPI pins do not come out on the Digital pins as for normal Arduino, but only appear on the SPI header). Use the same connections for Yun with 5V or 3.3V.
and initialise the NRF24 object with the default constructor NRF24 nrf24;
For an Arduino Mega:
and you can then use the constructor RH_NRF24(8, 53).
For an Itead Studio IBoard Pro http://imall.iteadstudio.com/iboard-pro.html, connected by hardware SPI to the ITDB02 Parallel LCD Module Interface pins:
And initialise like this:
For an Itead Studio IBoard Pro http://imall.iteadstudio.com/iboard-pro.html, connected by software SPI to the nRF24L01+ Module Interface pins. CAUTION: performance of software SPI is very slow and is not compatible with other modules running hardware SPI.
And initialise like this:
For Raspberry Pi with Sparkfun WRL-00691
and initialise like this:
See the example program and Makefile in examples/raspi. Requires bcm2835 library to be previously installed.
Initialisation example with software SPI
Several example programs are provided.
Frequency accuracy may be debatable. For nominal frequency of 2401.000 MHz (ie channel 1), my Yaesu VR-5000 receiver indicated the center frequency for my test radios was 2401.121 MHz. Its not clear to me if the Yaesu is the source of the error, but I tend to believe it, which would make the nRF24l01 frequency out by 121kHz.
The measured power output for a nRF24L01p with PA and LNA set to 0dBm output is about 18dBm.
The radio is enabled all the time, and switched between TX and RX modes depending on whether there is any data to send. Sending data sets the radio to TX mode. After data is sent, the radio automatically returns to Standby II mode. Calling waitAvailable() or waitAvailableTimeout() starts the radio in RX mode.
The radio is configured by default to Channel 2, 2Mbps, 0dBm power, 5 bytes address, payload width 1, CRC enabled 2 byte CRC, No Auto-Ack mode. Enhanced shockburst is used. TX and P0 are set to the Network address. Node addresses and decoding are handled with the RH_NRF24 module.
Memory usage of this class is minimal. The compiled client and server sketches are about 6000 bytes on Arduino. The reliable client and server sketches compile to about 8500 bytes on Arduino. RAM requirements are minimal.
enum RH_NRF24::DataRate |
Defines convenient values for setting data rates in setRF()
Enumerator | |
---|---|
DataRate1Mbps | 1 Mbps |
DataRate2Mbps | 2 Mbps |
DataRate250kbps | 250 kbps |
Convenient values for setting transmitter power in setRF() These are designed to agree with the values for RF_PWR in RH_NRF24_REG_06_RF_SETUP To be passed to setRF();.
RH_NRF24::RH_NRF24 | ( | uint8_t | chipEnablePin = 8 , |
uint8_t | slaveSelectPin = SS , |
||
RHGenericSPI & | spi = hardware_spi |
||
) |
Constructor. You can have multiple instances, but each instance must have its own chip enable and slave select pin. After constructing, you must call init() to initialise the interface and the radio module
[in] | chipEnablePin | the Arduino pin to use to enable the chip for transmit/receive |
[in] | slaveSelectPin | the Arduino pin number of the output to use to select the NRF24 before accessing it. Defaults to the normal SS pin for your Arduino (D10 for Diecimila, Uno etc, D53 for Mega, D10 for Maple) |
[in] | spi | Pointer to the SPI interface object to use. Defaults to the standard Arduino hardware SPI interface |
|
virtual |
Checks whether a received message is available. This can be called multiple times in a timeout loop
Implements RHGenericDriver.
References RHGenericDriver::_mode, clearRxBuf(), flushRx(), RHGenericDriver::RHModeTx, setModeIdle(), setModeRx(), RHNRFSPIDriver::spiBurstRead(), RHNRFSPIDriver::spiRead(), spiReadRegister(), spiWriteRegister(), and validateRxBuf().
Referenced by recv().
|
protected |
Flush the RX FIFOs
References RHNRFSPIDriver::spiCommand().
Referenced by available(), and init().
|
protected |
Flush the TX FIFOs
References RHNRFSPIDriver::spiCommand().
Referenced by init(), and waitPacketSent().
|
virtual |
Initialises this instance and the radio module connected to it. The following steps are taken:g
Reimplemented from RHGenericDriver.
References RHNRFSPIDriver::_spi, clearRxBuf(), DataRate2Mbps, flushRx(), flushTx(), RHGenericSPI::Frequency1MHz, RHNRFSPIDriver::init(), setChannel(), RHGenericSPI::setFrequency(), setModeIdle(), setRF(), spiReadRegister(), RHNRFSPIDriver::spiWrite(), spiWriteRegister(), and TransmitPower0dBm.
bool RH_NRF24::isSending | ( | ) |
Indicates if the chip is in transmit mode and there is a packet currently being transmitted
References spiReadRegister(), and statusRead().
|
virtual |
The maximum message length supported by this driver
Implements RHGenericDriver.
bool RH_NRF24::printRegisters | ( | ) |
Prints the value of all chip registers to the Serial device if RH_HAVE_SERIAL is defined for the current platform For debugging purposes only.
References spiBurstReadRegister().
|
virtual |
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.
[in] | buf | Location to copy the received message |
[in,out] | len | Pointer to the number of octets available in buf. The number be reset to the actual number of octets copied. |
Implements RHGenericDriver.
References available(), and clearRxBuf().
|
virtual |
Sends data to the address set by setTransmitAddress() Sets the radio to TX mode
[in] | data | Data bytes to send. |
[in] | len | Number of data bytes to send |
Implements RHGenericDriver.
References RHGenericDriver::_txGood, RHGenericDriver::_txHeaderFlags, RHGenericDriver::_txHeaderFrom, RHGenericDriver::_txHeaderId, RHGenericDriver::_txHeaderTo, setModeTx(), RHNRFSPIDriver::spiBurstWrite(), and RHGenericDriver::waitCAD().
bool RH_NRF24::setChannel | ( | uint8_t | channel | ) |
Sets the transmit and receive channel number. The frequency used is (2400 + channel) MHz
References spiWriteRegister().
Referenced by init().
void RH_NRF24::setModeIdle | ( | ) |
Sets the radio in power down mode, with the configuration set to the last value from setOpMode(). Sets chip enable to LOW.
References RHGenericDriver::_mode, RHGenericDriver::RHModeIdle, and spiWriteRegister().
Referenced by available(), init(), and waitPacketSent().
void RH_NRF24::setModeRx | ( | ) |
Sets the radio in RX mode. Sets chip enable to HIGH to enable the chip in RX mode.
References RHGenericDriver::_mode, RHGenericDriver::RHModeRx, and spiWriteRegister().
Referenced by available().
void RH_NRF24::setModeTx | ( | ) |
Sets the radio in TX mode. Pulses the chip enable LOW then HIGH to enable the chip in TX mode.
References RHGenericDriver::_mode, RHGenericDriver::RHModeTx, and spiWriteRegister().
Referenced by send().
bool RH_NRF24::setNetworkAddress | ( | uint8_t * | address, |
uint8_t | len | ||
) |
Sets the Network address. Only nodes with the same network address can communicate with each other. You can set different network addresses in different sets of nodes to isolate them from each other. Internally, this sets the nRF24 TX_ADDR and RX_ADDR_P0 to be the given network address. The default network address is 0xE7E7E7E7E7
[in] | address | The new network address. Must match the network address of any receiving node(s). |
[in] | len | Number of bytes of address to set (3 to 5). |
References spiBurstWriteRegister(), and spiWriteRegister().
bool RH_NRF24::setOpMode | ( | uint8_t | mode | ) |
Sets the chip configuration that will be used to set the NRF24 NRF24_REG_00_CONFIG register when in Idle mode. This allows you to change some chip configuration for compatibility with libraries other than this one. You should not normally need to call this. Defaults to NRF24_EN_CRC| RH_NRF24_CRCO, which is the standard configuration for this library (2 byte CRC enabled).
[in] | mode | The chip configuration to be used whe in Idle mode. |
References RHGenericDriver::mode().
bool RH_NRF24::setRF | ( | DataRate | data_rate, |
TransmitPower | power | ||
) |
Sets the data rate and transmitter power to use. Note that the nRF24 and the RFM73 have different available power levels, and for convenience, 2 different sets of values are available in the RH_NRF24::TransmitPower enum. The ones with the RFM73 only have meaning on the RFM73 and compatible devces. The others are for the nRF24.
[in] | data_rate | The data rate to use for all packets transmitted and received. One of RH_NRF24::DataRate. |
[in] | power | Transmitter power. One of RH_NRF24::TransmitPower. |
References DataRate250kbps, DataRate2Mbps, and spiWriteRegister().
Referenced by init().
|
virtual |
Sets the radio into Power Down mode. If successful, the radio will stay in Power Down mode until woken by changing mode it idle, transmit or receive (eg by calling send(), recv(), available() etc) Caution: there is a time penalty as the radio takes a finite time to wake from sleep mode.
Reimplemented from RHGenericDriver.
References RHGenericDriver::_mode, RHGenericDriver::RHModeSleep, and spiWriteRegister().
uint8_t RH_NRF24::spiBurstReadRegister | ( | uint8_t | reg, |
uint8_t * | dest, | ||
uint8_t | len | ||
) |
Reads a number of consecutive registers from the NRF24 using burst read mode
[in] | reg | Register number of the first register, one of RH_NRF24_REG_* |
[in] | dest | Array to write the register values to. Must be at least len bytes |
[in] | len | Number of bytes to read |
References RHNRFSPIDriver::spiBurstRead().
Referenced by printRegisters().
uint8_t RH_NRF24::spiBurstWriteRegister | ( | uint8_t | reg, |
uint8_t * | src, | ||
uint8_t | len | ||
) |
Write a number of consecutive registers using burst write mode
[in] | reg | Register number of the first register, one of RH_NRF24_REG_* |
[in] | src | Array of new register values to write. Must be at least len bytes |
[in] | len | Number of bytes to write |
References RHNRFSPIDriver::spiBurstWrite().
Referenced by setNetworkAddress().
uint8_t RH_NRF24::spiReadRegister | ( | uint8_t | reg | ) |
Reads a single register from the NRF24
[in] | reg | Register number, one of RH_NRF24_REG_* |
References RHNRFSPIDriver::spiRead().
Referenced by available(), init(), and isSending().
uint8_t RH_NRF24::spiWriteRegister | ( | uint8_t | reg, |
uint8_t | val | ||
) |
Writes a single byte to the NRF24, and at the same time reads the current STATUS register
[in] | reg | Register number, one of RH_NRF24_REG_* |
[in] | val | The value to write |
References RHNRFSPIDriver::spiWrite().
Referenced by available(), init(), setChannel(), setModeIdle(), setModeRx(), setModeTx(), setNetworkAddress(), setRF(), sleep(), and waitPacketSent().
uint8_t RH_NRF24::statusRead | ( | ) |
Reads and returns the device status register NRF24_REG_02_DEVICE_STATUS
References RHNRFSPIDriver::spiCommand().
Referenced by isSending(), and waitPacketSent().
|
virtual |
Blocks until the current message (if any) has been transmitted
Reimplemented from RHGenericDriver.
References RHGenericDriver::_mode, flushTx(), RHGenericDriver::RHModeTx, setModeIdle(), spiWriteRegister(), and statusRead().