RadioHead
RH_NRF24.h
1// RH_NRF24.h
2// Author: Mike McCauley
3// Copyright (C) 2012 Mike McCauley
4// $Id: RH_NRF24.h,v 1.21 2020/06/15 23:39:39 mikem Exp $
5//
6
7#ifndef RH_NRF24_h
8#define RH_NRF24_h
9
10#include <RHGenericSPI.h>
11#include <RHNRFSPIDriver.h>
12
13// This is the maximum number of bytes that can be carried by the nRF24.
14// We use some for headers, keeping fewer for RadioHead messages
15#define RH_NRF24_MAX_PAYLOAD_LEN 32
16
17// The length of the headers we add.
18// The headers are inside the nRF24 payload
19#define RH_NRF24_HEADER_LEN 4
20
21// This is the maximum RadioHead user message length that can be supported by this library. Limited by
22// the supported message lengths in the nRF24
23#define RH_NRF24_MAX_MESSAGE_LEN (RH_NRF24_MAX_PAYLOAD_LEN-RH_NRF24_HEADER_LEN)
24
25// SPI Command names
26#define RH_NRF24_COMMAND_R_REGISTER 0x00
27#define RH_NRF24_COMMAND_W_REGISTER 0x20
28#define RH_NRF24_COMMAND_ACTIVATE 0x50 // only on RFM73 ?
29#define RH_NRF24_COMMAND_R_RX_PAYLOAD 0x61
30#define RH_NRF24_COMMAND_W_TX_PAYLOAD 0xa0
31#define RH_NRF24_COMMAND_FLUSH_TX 0xe1
32#define RH_NRF24_COMMAND_FLUSH_RX 0xe2
33#define RH_NRF24_COMMAND_REUSE_TX_PL 0xe3
34#define RH_NRF24_COMMAND_R_RX_PL_WID 0x60
35#define RH_NRF24_COMMAND_W_ACK_PAYLOAD(pipe) (0xa8|(pipe&0x7))
36#define RH_NRF24_COMMAND_W_TX_PAYLOAD_NOACK 0xb0
37#define RH_NRF24_COMMAND_NOP 0xff
38
39// Register names
40#define RH_NRF24_REGISTER_MASK 0x1f
41#define RH_NRF24_REG_00_CONFIG 0x00
42#define RH_NRF24_REG_01_EN_AA 0x01
43#define RH_NRF24_REG_02_EN_RXADDR 0x02
44#define RH_NRF24_REG_03_SETUP_AW 0x03
45#define RH_NRF24_REG_04_SETUP_RETR 0x04
46#define RH_NRF24_REG_05_RF_CH 0x05
47#define RH_NRF24_REG_06_RF_SETUP 0x06
48#define RH_NRF24_REG_07_STATUS 0x07
49#define RH_NRF24_REG_08_OBSERVE_TX 0x08
50#define RH_NRF24_REG_09_RPD 0x09
51#define RH_NRF24_REG_0A_RX_ADDR_P0 0x0a
52#define RH_NRF24_REG_0B_RX_ADDR_P1 0x0b
53#define RH_NRF24_REG_0C_RX_ADDR_P2 0x0c
54#define RH_NRF24_REG_0D_RX_ADDR_P3 0x0d
55#define RH_NRF24_REG_0E_RX_ADDR_P4 0x0e
56#define RH_NRF24_REG_0F_RX_ADDR_P5 0x0f
57#define RH_NRF24_REG_10_TX_ADDR 0x10
58#define RH_NRF24_REG_11_RX_PW_P0 0x11
59#define RH_NRF24_REG_12_RX_PW_P1 0x12
60#define RH_NRF24_REG_13_RX_PW_P2 0x13
61#define RH_NRF24_REG_14_RX_PW_P3 0x14
62#define RH_NRF24_REG_15_RX_PW_P4 0x15
63#define RH_NRF24_REG_16_RX_PW_P5 0x16
64#define RH_NRF24_REG_17_FIFO_STATUS 0x17
65#define RH_NRF24_REG_1C_DYNPD 0x1c
66#define RH_NRF24_REG_1D_FEATURE 0x1d
67
68// These register masks etc are named wherever possible
69// corresponding to the bit and field names in the nRF24L01 Product Specification
70// #define RH_NRF24_REG_00_CONFIG 0x00
71#define RH_NRF24_MASK_RX_DR 0x40
72#define RH_NRF24_MASK_TX_DS 0x20
73#define RH_NRF24_MASK_MAX_RT 0x10
74#define RH_NRF24_EN_CRC 0x08
75#define RH_NRF24_CRCO 0x04
76#define RH_NRF24_PWR_UP 0x02
77#define RH_NRF24_PRIM_RX 0x01
78
79// #define RH_NRF24_REG_01_EN_AA 0x01
80#define RH_NRF24_ENAA_P5 0x20
81#define RH_NRF24_ENAA_P4 0x10
82#define RH_NRF24_ENAA_P3 0x08
83#define RH_NRF24_ENAA_P2 0x04
84#define RH_NRF24_ENAA_P1 0x02
85#define RH_NRF24_ENAA_P0 0x01
86
87// #define RH_NRF24_REG_02_EN_RXADDR 0x02
88#define RH_NRF24_ERX_P5 0x20
89#define RH_NRF24_ERX_P4 0x10
90#define RH_NRF24_ERX_P3 0x08
91#define RH_NRF24_ERX_P2 0x04
92#define RH_NRF24_ERX_P1 0x02
93#define RH_NRF24_ERX_P0 0x01
94
95// #define RH_NRF24_REG_03_SETUP_AW 0x03
96#define RH_NRF24_AW_3_BYTES 0x01
97#define RH_NRF24_AW_4_BYTES 0x02
98#define RH_NRF24_AW_5_BYTES 0x03
99
100// #define RH_NRF24_REG_04_SETUP_RETR 0x04
101#define RH_NRF24_ARD 0xf0
102#define RH_NRF24_ARC 0x0f
103
104// #define RH_NRF24_REG_05_RF_CH 0x05
105#define RH_NRF24_RF_CH 0x7f
106
107// #define RH_NRF24_REG_06_RF_SETUP 0x06
108#define RH_NRF24_CONT_WAVE 0x80
109#define RH_NRF24_RF_DR_LOW 0x20
110#define RH_NRF24_PLL_LOCK 0x10
111#define RH_NRF24_RF_DR_HIGH 0x08
112#define RH_NRF24_PWR 0x06
113#define RH_NRF24_PWR_m18dBm 0x00
114#define RH_NRF24_PWR_m12dBm 0x02
115#define RH_NRF24_PWR_m6dBm 0x04
116#define RH_NRF24_PWR_0dBm 0x06
117#define RH_NRF24_LNA_HCURR 0x01
118
119// #define RH_NRF24_REG_07_STATUS 0x07
120#define RH_NRF24_RX_DR 0x40
121#define RH_NRF24_TX_DS 0x20
122#define RH_NRF24_MAX_RT 0x10
123#define RH_NRF24_RX_P_NO 0x0e
124#define RH_NRF24_STATUS_TX_FULL 0x01
125
126// #define RH_NRF24_REG_08_OBSERVE_TX 0x08
127#define RH_NRF24_PLOS_CNT 0xf0
128#define RH_NRF24_ARC_CNT 0x0f
129
130// #define RH_NRF24_REG_09_RPD 0x09
131#define RH_NRF24_RPD 0x01
132
133// #define RH_NRF24_REG_17_FIFO_STATUS 0x17
134#define RH_NRF24_TX_REUSE 0x40
135#define RH_NRF24_TX_FULL 0x20
136#define RH_NRF24_TX_EMPTY 0x10
137#define RH_NRF24_RX_FULL 0x02
138#define RH_NRF24_RX_EMPTY 0x01
139
140// #define RH_NRF24_REG_1C_DYNPD 0x1c
141#define RH_NRF24_DPL_ALL 0x3f
142#define RH_NRF24_DPL_P5 0x20
143#define RH_NRF24_DPL_P4 0x10
144#define RH_NRF24_DPL_P3 0x08
145#define RH_NRF24_DPL_P2 0x04
146#define RH_NRF24_DPL_P1 0x02
147#define RH_NRF24_DPL_P0 0x01
148
149// #define RH_NRF24_REG_1D_FEATURE 0x1d
150#define RH_NRF24_EN_DPL 0x04
151#define RH_NRF24_EN_ACK_PAY 0x02
152#define RH_NRF24_EN_DYN_ACK 0x01
153
154
155/////////////////////////////////////////////////////////////////////
156/// \class RH_NRF24 RH_NRF24.h <RH_NRF24.h>
157/// \brief Send and receive unaddressed, unreliable datagrams by nRF24L01 and compatible transceivers.
158///
159/// Supported transceivers include:
160/// - Nordic nRF24 based 2.4GHz radio modules, such as nRF24L01 http://www.nordicsemi.com/eng/Products/2.4GHz-RF/nRF24L01
161/// and other compatible transceivers.
162/// - nRF24L01p with PA and LNA modules that produce a higher power output similar to this one:
163/// http://www.elecfreaks.com/wiki/index.php?title=2.4G_Wireless_nRF24L01p_with_PA_and_LNA
164/// - Sparkfun WRL-00691 module with nRF24L01 https://www.sparkfun.com/products/691
165/// or WRL-00705 https://www.sparkfun.com/products/705 etc.
166/// - Hope-RF RFM73 http://www.hoperf.com/rf/2.4g_module/RFM73.htm and
167/// http://www.anarduino.com/details.jsp?pid=121
168/// and compatible devices (such as BK2423). nRF24L01 and RFM73 can interoperate
169/// with each other.
170///
171/// This base class provides basic functions for sending and receiving unaddressed, unreliable datagrams
172/// of arbitrary length to 28 octets per packet. Use one of the Manager classes to get addressing and
173/// acknowledgement reliability, routing, meshes etc.
174///
175/// The nRF24L01 (http://www.sparkfun.com/datasheets/Wireless/Nordic/nRF24L01P_Product_Specification_1_0.pdf)
176/// is a low-cost 2.4GHz ISM transceiver module. It supports a number of channel frequencies in the 2.4GHz band
177/// and a range of data rates.
178///
179/// This library provides functions for sending and receiving messages of up to 28 octets on any
180/// frequency supported by the nRF24L01, at a selected data rate.
181///
182/// Several nRF24L01 modules can be connected to an Arduino, permitting the construction of translators
183/// and frequency changers, etc.
184///
185/// The nRF24 transceiver is configured to use Enhanced Shockburst with no acknowledgement and no retransmits.
186/// TX_ADDR and RX_ADDR_P0 are set to the network address. If you need the low level auto-acknowledgement
187/// feature supported by this chip, you can use our original NRF24 library
188/// at http://www.airspayce.com/mikem/arduino/NRF24
189///
190/// Naturally, for any 2 radios to communicate that must be configured to use the same frequency and
191/// data rate, and with identical network addresses.
192///
193/// Example Arduino programs are included to show the main modes of use.
194///
195/// \par Packet Format
196///
197/// All messages sent and received by this class conform to this packet format, as specified by
198/// the nRF24L01 product specification:
199///
200/// - 1 octets PREAMBLE
201/// - 3 to 5 octets NETWORK ADDRESS
202/// - 9 bits packet control field
203/// - 0 to 32 octets PAYLOAD, consisting of:
204/// - 1 octet TO header
205/// - 1 octet FROM header
206/// - 1 octet ID header
207/// - 1 octet FLAGS header
208/// - 0 to 28 octets of user message
209/// - 2 octets CRC
210///
211/// \par Connecting nRF24L01 to Arduino
212///
213/// The electrical connection between the nRF24L01 and the Arduino require 3.3V, the 3 x SPI pins (SCK, SDI, SDO),
214/// a Chip Enable pin and a Slave Select pin.
215/// If you are using the Sparkfun WRL-00691 module, it has a voltage regulator on board and
216/// can be should with 5V VCC if possible.
217/// The examples below assume the Sparkfun WRL-00691 module
218///
219/// Connect the nRF24L01 to most Arduino's like this (Caution, Arduino Mega has different pins for SPI,
220/// see below). Use these same connections for Teensy 3.1 (use 3.3V not 5V Vcc).
221/// \code
222/// Arduino Sparkfun WRL-00691
223/// 5V-----------VCC (3.3V to 7V in)
224/// pin D8-----------CE (chip enable in)
225/// SS pin D10----------CSN (chip select in)
226/// SCK pin D13----------SCK (SPI clock in)
227/// MOSI pin D11----------SDI (SPI Data in)
228/// MISO pin D12----------SDO (SPI data out)
229/// IRQ (Interrupt output, not connected)
230/// GND----------GND (ground in)
231/// \endcode
232///
233/// For an Arduino Leonardo (the SPI pins do not come out on the Digital pins as for normal Arduino, but only
234/// appear on the ICSP header)
235/// \code
236/// Leonardo Sparkfun WRL-00691
237/// 5V-----------VCC (3.3V to 7V in)
238/// pin D8-----------CE (chip enable in)
239/// SS pin D10----------CSN (chip select in)
240/// SCK ICSP pin 3----------SCK (SPI clock in)
241/// MOSI ICSP pin 4----------SDI (SPI Data in)
242/// MISO ICSP pin 1----------SDO (SPI data out)
243/// IRQ (Interrupt output, not connected)
244/// GND----------GND (ground in)
245/// \endcode
246/// and initialise the NRF24 object like this to explicitly set the SS pin
247/// NRF24 nrf24(8, 10);
248///
249/// For an Arduino Due (the SPI pins do not come out on the Digital pins as for normal Arduino, but only
250/// appear on the SPI header). Use the same connections for Yun with 5V or 3.3V.
251/// \code
252/// Due Sparkfun WRL-00691
253/// 3.3V-----------VCC (3.3V to 7V in)
254/// pin D8-----------CE (chip enable in)
255/// SS pin D10----------CSN (chip select in)
256/// SCK SPI pin 3----------SCK (SPI clock in)
257/// MOSI SPI pin 4----------SDI (SPI Data in)
258/// MISO SPI pin 1----------SDO (SPI data out)
259/// IRQ (Interrupt output, not connected)
260/// GND----------GND (ground in)
261/// \endcode
262/// and initialise the NRF24 object with the default constructor
263/// NRF24 nrf24;
264///
265/// For an Arduino Mega:
266/// \code
267/// Mega Sparkfun WRL-00691
268/// 5V-----------VCC (3.3V to 7V in)
269/// pin D8-----------CE (chip enable in)
270/// SS pin D53----------CSN (chip select in)
271/// SCK pin D52----------SCK (SPI clock in)
272/// MOSI pin D51----------SDI (SPI Data in)
273/// MISO pin D50----------SDO (SPI data out)
274/// IRQ (Interrupt output, not connected)
275/// GND----------GND (ground in)
276/// \endcode
277/// and you can then use the constructor RH_NRF24(8, 53).
278///
279/// For an Itead Studio IBoard Pro http://imall.iteadstudio.com/iboard-pro.html, connected by hardware SPI to the
280/// ITDB02 Parallel LCD Module Interface pins:
281/// \code
282/// IBoard Signal=ITDB02 pin Sparkfun WRL-00691
283/// 3.3V 37-----------VCC (3.3V to 7V in)
284/// D2 28-----------CE (chip enable in)
285/// D29 27----------CSN (chip select in)
286/// SCK D52 32----------SCK (SPI clock in)
287/// MOSI D51 34----------SDI (SPI Data in)
288/// MISO D50 30----------SDO (SPI data out)
289/// IRQ (Interrupt output, not connected)
290/// GND 39----------GND (ground in)
291/// \endcode
292/// And initialise like this:
293/// \code
294/// RH_NRF24 nrf24(2, 29);
295/// \endcode
296///
297/// For an Itead Studio IBoard Pro http://imall.iteadstudio.com/iboard-pro.html, connected by software SPI to the
298/// nRF24L01+ Module Interface pins. CAUTION: performance of software SPI is very slow and is not
299/// compatible with other modules running hardware SPI.
300/// \code
301/// IBoard Signal=Module pin Sparkfun WRL-00691
302/// 3.3V 2----------VCC (3.3V to 7V in)
303/// D12 3-----------CE (chip enable in)
304/// D29 4----------CSN (chip select in)
305/// D9 5----------SCK (SPI clock in)
306/// D8 6----------SDI (SPI Data in)
307/// D7 7----------SDO (SPI data out)
308/// IRQ (Interrupt output, not connected)
309/// GND 1----------GND (ground in)
310/// \endcode
311/// And initialise like this:
312/// \code
313/// #include <SPI.h>
314/// #include <RH_NRF24.h>
315/// #include <RHSoftwareSPI.h>
316/// Singleton instance of the radio driver
317/// RHSoftwareSPI spi;
318/// RH_NRF24 nrf24(12, 11, spi);
319/// void setup() {
320/// spi.setPins(7, 8, 9);
321/// ....
322/// \endcode
323///
324///
325/// For Raspberry Pi with Sparkfun WRL-00691
326/// \code
327/// Raspberry Pi P1 pin Sparkfun WRL-00691
328/// 5V 2-----------VCC (3.3V to 7V in)
329/// GPIO25 22-----------CE (chip enable in)
330/// GPIO8 24----------CSN (chip select in)
331/// GPIO11 23----------SCK (SPI clock in)
332/// GPIO10 19----------SDI (SPI Data in)
333/// GPIO9 21----------SDO (SPI data out)
334/// IRQ (Interrupt output, not connected)
335/// GND 6----------GND (ground in)
336/// \endcode
337/// and initialise like this:
338/// \code
339/// RH_NRF24 nrf24(RPI_V2_GPIO_P1_22, RPI_V2_GPIO_P1_24);
340/// \endcode
341/// See the example program and Makefile in examples/raspi. Requires bcm2835 library to be previously installed.
342/// \code
343/// cd examples/raspi
344/// make
345/// sudo ./RasPiRH
346/// \endcode
347/// \code
348///
349/// You can override the default settings for the CSN and CE pins
350/// in the NRF24() constructor if you wish to connect the slave select CSN to other than the normal one for your
351///
352/// Caution: on the Raspberry Pi Zero, the hardware SPI, is only connected to the
353/// ICSP-header. So in order to use the RF, one must either connect it to the SPI-pins
354/// of the ICSP-header or use the software SPI provided by RHSoftwareSPI.
355/// the mapping of the SPI-Pins for each board here:
356/// https://www.arduino.cc/en/Reference/SPI
357/// Arduino (D10 for Diecimila, Uno etc and D53 for Mega)
358///
359/// Caution: on some Arduinos such as the Mega 2560, if you set the slave select pin to be other than the usual SS
360/// pin (D53 on Mega 2560), you may need to set the usual SS pin to be an output to force the Arduino into SPI
361/// master mode.
362///
363/// Caution: this module has not been proved to work with Leonardo, at least without level
364/// shifters between the nRF24 and the Leonardo. Tests seem to indicate that such level shifters would be required
365/// with Leonardo to make it work.
366///
367/// It is possible to have 2 radios conected to one arduino, provided each radio has its own
368/// CSN and CE line (SCK, SDI and SDO are common to both radios)
369///
370/// \par SPI Interface
371///
372/// You can interface to nRF24L01 with with hardware or software SPI. Use of software SPI with the RHSoftwareSPI
373/// class depends on a fast enough processor and digitalOut() functions to achieve a high enough SPI bus frequency.
374/// If you observe reliable behaviour with the default hardware SPI RHHardwareSPI, but unreliable behaviour
375/// with Software SPI RHSoftwareSPI, it may be due to slow CPU performance.
376///
377/// Initialisation example with hardware SPI
378/// \code
379/// #include <RH_NRF24.h>
380/// RH_NRF24 driver;
381/// RHReliableDatagram manager(driver, CLIENT_ADDRESS);
382/// \endcode
383///
384/// Initialisation example with software SPI
385/// \code
386/// #include <RH_NRF24.h>
387/// #include <RHSoftwareSPI.h>
388/// RHSoftwareSPI spi;
389/// RH_NRF24 driver(8, 10, spi);
390/// RHReliableDatagram manager(driver, CLIENT_ADDRESS);
391/// \endcode
392///
393/// \par Example programs
394///
395/// Several example programs are provided.
396///
397/// \par Radio Performance
398///
399/// Frequency accuracy may be debatable. For nominal frequency of 2401.000 MHz (ie channel 1),
400/// my Yaesu VR-5000 receiver indicated the center frequency for my test radios
401/// was 2401.121 MHz. Its not clear to me if the Yaesu
402/// is the source of the error, but I tend to believe it, which would make the nRF24l01 frequency out by 121kHz.
403///
404/// The measured power output for a nRF24L01p with PA and LNA set to 0dBm output is about 18dBm.
405///
406/// \par Radio operating strategy and defaults
407///
408/// The radio is enabled all the time, and switched between TX and RX modes depending on
409/// whether there is any data to send. Sending data sets the radio to TX mode.
410/// After data is sent, the radio automatically returns to Standby II mode. Calling waitAvailable() or
411/// waitAvailableTimeout() starts the radio in RX mode.
412///
413/// The radio is configured by default to Channel 2, 2Mbps, 0dBm power, 5 bytes address, payload width 1, CRC enabled
414/// 2 byte CRC, No Auto-Ack mode. Enhanced shockburst is used.
415/// TX and P0 are set to the Network address. Node addresses and decoding are handled with the RH_NRF24 module.
416///
417/// \par Memory
418///
419/// Memory usage of this class is minimal. The compiled client and server sketches are about 6000 bytes on Arduino.
420/// The reliable client and server sketches compile to about 8500 bytes on Arduino.
421/// RAM requirements are minimal.
422///
424{
425public:
426
427 /// \brief Defines convenient values for setting data rates in setRF()
428 typedef enum
429 {
430 DataRate1Mbps = 0, ///< 1 Mbps
431 DataRate2Mbps, ///< 2 Mbps
432 DataRate250kbps ///< 250 kbps
434
435 /// \brief Convenient values for setting transmitter power in setRF()
436 /// These are designed to agree with the values for RF_PWR in RH_NRF24_REG_06_RF_SETUP
437 /// To be passed to setRF();
438 typedef enum
439 {
440 // Add 20dBm for nRF24L01p with PA and LNA modules
441 TransmitPowerm18dBm = 0, ///< On nRF24, -18 dBm
442 TransmitPowerm12dBm, ///< On nRF24, -12 dBm
443 TransmitPowerm6dBm, ///< On nRF24, -6 dBm
444 TransmitPower0dBm, ///< On nRF24, 0 dBm
445 // Sigh, different power levels for the same bit patterns on RFM73:
446 // On RFM73P-S, there is a Tx power amp, so expect higher power levels, up to 20dBm. Alas
447 // there is no clear documentation on the power for different settings :-(
448 RFM73TransmitPowerm10dBm = 0, ///< On RFM73, -10 dBm
449 RFM73TransmitPowerm5dBm, ///< On RFM73, -5 dBm
450 RFM73TransmitPowerm0dBm, ///< On RFM73, 0 dBm
451 RFM73TransmitPower5dBm ///< On RFM73, 5 dBm. 20dBm on RFM73P-S2 ?
452
454
455 /// Constructor. You can have multiple instances, but each instance must have its own
456 /// chip enable and slave select pin.
457 /// After constructing, you must call init() to initialise the interface
458 /// and the radio module
459 /// \param[in] chipEnablePin the Arduino pin to use to enable the chip for transmit/receive
460 /// \param[in] slaveSelectPin the Arduino pin number of the output to use to select the NRF24 before
461 /// accessing it. Defaults to the normal SS pin for your Arduino (D10 for Diecimila, Uno etc, D53 for Mega,
462 /// D10 for Maple)
463 /// \param[in] spi Pointer to the SPI interface object to use.
464 /// Defaults to the standard Arduino hardware SPI interface
465 RH_NRF24(uint8_t chipEnablePin = 8, uint8_t slaveSelectPin = SS, RHGenericSPI& spi = hardware_spi);
466
467 /// Initialises this instance and the radio module connected to it.
468 /// The following steps are taken:g
469 /// - Set the chip enable and chip select pins to output LOW, HIGH respectively.
470 /// - Initialise the SPI output pins
471 /// - Initialise the SPI interface library to 8MHz (Hint, if you want to lower
472 /// the SPI frequency (perhaps where you have other SPI shields, low voltages etc),
473 /// call SPI.setClockDivider() after init()).
474 /// -Flush the receiver and transmitter buffers
475 /// - Set the radio to receive with powerUpRx();
476 /// \return true if everything was successful
477 bool init();
478
479 /// Reads a single register from the NRF24
480 /// \param[in] reg Register number, one of RH_NRF24_REG_*
481 /// \return The value of the register
482 uint8_t spiReadRegister(uint8_t reg);
483
484 /// Writes a single byte to the NRF24, and at the same time reads the current STATUS register
485 /// \param[in] reg Register number, one of RH_NRF24_REG_*
486 /// \param[in] val The value to write
487 /// \return the current STATUS (read while the command is sent)
488 uint8_t spiWriteRegister(uint8_t reg, uint8_t val);
489
490 /// Reads a number of consecutive registers from the NRF24 using burst read mode
491 /// \param[in] reg Register number of the first register, one of RH_NRF24_REG_*
492 /// \param[in] dest Array to write the register values to. Must be at least len bytes
493 /// \param[in] len Number of bytes to read
494 /// \return the current STATUS (read while the command is sent)
495 uint8_t spiBurstReadRegister(uint8_t reg, uint8_t* dest, uint8_t len);
496
497 /// Write a number of consecutive registers using burst write mode
498 /// \param[in] reg Register number of the first register, one of RH_NRF24_REG_*
499 /// \param[in] src Array of new register values to write. Must be at least len bytes
500 /// \param[in] len Number of bytes to write
501 /// \return the current STATUS (read while the command is sent)
502 uint8_t spiBurstWriteRegister(uint8_t reg, uint8_t* src, uint8_t len);
503
504 /// Reads and returns the device status register NRF24_REG_02_DEVICE_STATUS
505 /// \return The value of the device status register
506 uint8_t statusRead();
507
508 /// Sets the transmit and receive channel number.
509 /// The frequency used is (2400 + channel) MHz
510 /// \return true on success
511 bool setChannel(uint8_t channel);
512
513 /// Sets the chip configuration that will be used to set
514 /// the NRF24 NRF24_REG_00_CONFIG register when in Idle mode. This allows you to change some
515 /// chip configuration for compatibility with libraries other than this one.
516 /// You should not normally need to call this.
517 /// Defaults to NRF24_EN_CRC| RH_NRF24_CRCO, which is the standard configuration for this library
518 /// (2 byte CRC enabled).
519 /// \param[in] mode The chip configuration to be used whe in Idle mode.
520 /// \return true on success
521 bool setOpMode(uint8_t mode);
522
523 /// Sets the Network address.
524 /// Only nodes with the same network address can communicate with each other. You
525 /// can set different network addresses in different sets of nodes to isolate them from each other.
526 /// Internally, this sets the nRF24 TX_ADDR and RX_ADDR_P0 to be the given network address.
527 /// The default network address is 0xE7E7E7E7E7
528 /// \param[in] address The new network address. Must match the network address of any receiving node(s).
529 /// \param[in] len Number of bytes of address to set (3 to 5).
530 /// \return true on success, false if len is not in the range 3-5 inclusive.
531 bool setNetworkAddress(uint8_t* address, uint8_t len);
532
533 /// Sets the data rate and transmitter power to use. Note that the nRF24 and the RFM73 have different
534 /// available power levels, and for convenience, 2 different sets of values are available in the
535 /// RH_NRF24::TransmitPower enum. The ones with the RFM73 only have meaning on the RFM73 and compatible
536 /// devces. The others are for the nRF24.
537 /// \param [in] data_rate The data rate to use for all packets transmitted and received. One of RH_NRF24::DataRate.
538 /// \param [in] power Transmitter power. One of RH_NRF24::TransmitPower.
539 /// \return true on success
540 bool setRF(DataRate data_rate, TransmitPower power);
541
542 /// Sets the radio in power down mode, with the configuration set to the
543 /// last value from setOpMode().
544 /// Sets chip enable to LOW.
545 void setModeIdle();
546
547 /// Sets the radio in RX mode.
548 /// Sets chip enable to HIGH to enable the chip in RX mode.
549 void setModeRx();
550
551 /// Sets the radio in TX mode.
552 /// Pulses the chip enable LOW then HIGH to enable the chip in TX mode.
553 void setModeTx();
554
555 /// Sends data to the address set by setTransmitAddress()
556 /// Sets the radio to TX mode
557 /// \param [in] data Data bytes to send.
558 /// \param [in] len Number of data bytes to send
559 /// \return true on success (which does not necessarily mean the receiver got the message, only that the message was
560 /// successfully transmitted).
561 bool send(const uint8_t* data, uint8_t len);
562
563 /// Blocks until the current message (if any)
564 /// has been transmitted
565 /// \return true on success, false if the chip is not in transmit mode or other transmit failure
566 virtual bool waitPacketSent();
567
568 /// Indicates if the chip is in transmit mode and
569 /// there is a packet currently being transmitted
570 /// \return true if the chip is in transmit mode and there is a transmission in progress
571 bool isSending();
572
573 /// Prints the value of all chip registers
574 /// to the Serial device if RH_HAVE_SERIAL is defined for the current platform
575 /// For debugging purposes only.
576 /// \return true on success
577 bool printRegisters();
578
579 /// Checks whether a received message is available.
580 /// This can be called multiple times in a timeout loop
581 /// \return true if a complete, valid message has been received and is able to be retrieved by
582 /// recv()
583 bool available();
584
585 /// Turns the receiver on if it not already on.
586 /// If there is a valid message available, copy it to buf and return true
587 /// else return false.
588 /// If a message is copied, *len is set to the length (Caution, 0 length messages are permitted).
589 /// You should be sure to call this function frequently enough to not miss any messages
590 /// It is recommended that you call it in your main loop.
591 /// \param[in] buf Location to copy the received message
592 /// \param[in,out] len Pointer to the number of octets available in buf. The number be reset to the actual number of octets copied.
593 /// \return true if a valid message was copied to buf
594 bool recv(uint8_t* buf, uint8_t* len);
595
596 /// The maximum message length supported by this driver
597 /// \return The maximum message length supported by this driver
598 uint8_t maxMessageLength();
599
600 /// Sets the radio into Power Down mode.
601 /// If successful, the radio will stay in Power Down mode until woken by
602 /// changing mode it idle, transmit or receive (eg by calling send(), recv(), available() etc)
603 /// Caution: there is a time penalty as the radio takes a finite time to wake from sleep mode.
604 /// \return true if sleep mode was successfully entered.
605 virtual bool sleep();
606
607protected:
608 /// Flush the TX FIFOs
609 /// \return the value of the device status register
610 uint8_t flushTx();
611
612 /// Flush the RX FIFOs
613 /// \return the value of the device status register
614 uint8_t flushRx();
615
616 /// Examine the receive buffer to determine whether the message is for this node
617 void validateRxBuf();
618
619 /// Clear our local receive buffer
620 void clearRxBuf();
621
622private:
623 /// This idle mode chip configuration
624 uint8_t _configuration;
625
626 /// the number of the chip enable pin
627 uint8_t _chipEnablePin;
628
629 /// Number of octets in the buffer
630 uint8_t _bufLen;
631
632 /// The receiver/transmitter buffer
633 uint8_t _buf[RH_NRF24_MAX_PAYLOAD_LEN];
634
635 /// True when there is a valid message in the buffer
636 bool _rxBufValid;
637};
638
639/// @example nrf24_client.ino
640/// @example nrf24_server.ino
641/// @example nrf24_encrypted_client.ino
642/// @example nrf24_encrypted_server.ino
643/// @example nrf24_reliable_datagram_client.ino
644/// @example nrf24_reliable_datagram_server.ino
645/// @example RasPiRH.cpp
646
647#endif
virtual RHMode mode()
Definition RHGenericDriver.cpp:165
Base class for SPI interfaces.
Definition RHGenericSPI.h:31
Base class for RadioHead drivers that use the SPI bus to communicate with its NRF family transport ha...
Definition RHNRFSPIDriver.h:34
Send and receive unaddressed, unreliable datagrams by nRF24L01 and compatible transceivers.
Definition RH_NRF24.h:424
uint8_t statusRead()
Definition RH_NRF24.cpp:82
bool setOpMode(uint8_t mode)
Definition RH_NRF24.cpp:104
bool setRF(DataRate data_rate, TransmitPower power)
Definition RH_NRF24.cpp:122
uint8_t spiBurstWriteRegister(uint8_t reg, uint8_t *src, uint8_t len)
Definition RH_NRF24.cpp:77
void clearRxBuf()
Clear our local receive buffer.
Definition RH_NRF24.cpp:325
uint8_t spiWriteRegister(uint8_t reg, uint8_t val)
Definition RH_NRF24.cpp:67
uint8_t spiBurstReadRegister(uint8_t reg, uint8_t *dest, uint8_t len)
Definition RH_NRF24.cpp:72
bool recv(uint8_t *buf, uint8_t *len)
Definition RH_NRF24.cpp:331
bool isSending()
Definition RH_NRF24.cpp:236
uint8_t spiReadRegister(uint8_t reg)
Definition RH_NRF24.cpp:62
virtual bool waitPacketSent()
Definition RH_NRF24.cpp:209
void validateRxBuf()
Examine the receive buffer to determine whether the message is for this node.
Definition RH_NRF24.cpp:276
void setModeTx()
Definition RH_NRF24.cpp:173
bool setChannel(uint8_t channel)
Definition RH_NRF24.cpp:98
bool printRegisters()
Definition RH_NRF24.cpp:242
bool send(const uint8_t *data, uint8_t len)
Definition RH_NRF24.cpp:188
void setModeIdle()
Definition RH_NRF24.cpp:141
uint8_t maxMessageLength()
Definition RH_NRF24.cpp:346
DataRate
Defines convenient values for setting data rates in setRF()
Definition RH_NRF24.h:429
@ DataRate1Mbps
1 Mbps
Definition RH_NRF24.h:430
@ DataRate2Mbps
2 Mbps
Definition RH_NRF24.h:431
@ DataRate250kbps
250 kbps
Definition RH_NRF24.h:432
bool setNetworkAddress(uint8_t *address, uint8_t len)
Definition RH_NRF24.cpp:110
bool available()
Definition RH_NRF24.cpp:294
TransmitPower
Convenient values for setting transmitter power in setRF() These are designed to agree with the value...
Definition RH_NRF24.h:439
@ TransmitPowerm18dBm
On nRF24, -18 dBm.
Definition RH_NRF24.h:441
@ TransmitPowerm12dBm
On nRF24, -12 dBm.
Definition RH_NRF24.h:442
@ RFM73TransmitPowerm0dBm
On RFM73, 0 dBm.
Definition RH_NRF24.h:450
@ RFM73TransmitPowerm5dBm
On RFM73, -5 dBm.
Definition RH_NRF24.h:449
@ RFM73TransmitPower5dBm
On RFM73, 5 dBm. 20dBm on RFM73P-S2 ?
Definition RH_NRF24.h:451
@ TransmitPowerm6dBm
On nRF24, -6 dBm.
Definition RH_NRF24.h:443
@ RFM73TransmitPowerm10dBm
On RFM73, -10 dBm.
Definition RH_NRF24.h:448
@ TransmitPower0dBm
On nRF24, 0 dBm.
Definition RH_NRF24.h:444
virtual bool sleep()
Definition RH_NRF24.cpp:151
uint8_t flushRx()
Definition RH_NRF24.cpp:93
void setModeRx()
Definition RH_NRF24.cpp:163
uint8_t flushTx()
Definition RH_NRF24.cpp:88
bool init()
Definition RH_NRF24.cpp:17