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.20 2017/07/25 05:26:50 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 /// Arduino (D10 for Diecimila, Uno etc and D53 for Mega)
352 ///
353 /// Caution: on some Arduinos such as the Mega 2560, if you set the slave select pin to be other than the usual SS
354 /// pin (D53 on Mega 2560), you may need to set the usual SS pin to be an output to force the Arduino into SPI
355 /// master mode.
356 ///
357 /// Caution: this module has not been proved to work with Leonardo, at least without level
358 /// shifters between the nRF24 and the Leonardo. Tests seem to indicate that such level shifters would be required
359 /// with Leonardo to make it work.
360 ///
361 /// It is possible to have 2 radios conected to one arduino, provided each radio has its own
362 /// CSN and CE line (SCK, SDI and SDO are common to both radios)
363 ///
364 /// \par SPI Interface
365 ///
366 /// You can interface to nRF24L01 with with hardware or software SPI. Use of software SPI with the RHSoftwareSPI
367 /// class depends on a fast enough processor and digitalOut() functions to achieve a high enough SPI bus frequency.
368 /// If you observe reliable behaviour with the default hardware SPI RHHardwareSPI, but unreliable behaviour
369 /// with Software SPI RHSoftwareSPI, it may be due to slow CPU performance.
370 ///
371 /// Initialisation example with hardware SPI
372 /// \code
373 /// #include <RH_NRF24.h>
374 /// RH_NRF24 driver;
375 /// RHReliableDatagram manager(driver, CLIENT_ADDRESS);
376 /// \endcode
377 ///
378 /// Initialisation example with software SPI
379 /// \code
380 /// #include <RH_NRF24.h>
381 /// #include <RHSoftwareSPI.h>
382 /// RHSoftwareSPI spi;
383 /// RH_NRF24 driver(8, 10, spi);
384 /// RHReliableDatagram manager(driver, CLIENT_ADDRESS);
385 /// \endcode
386 ///
387 /// \par Example programs
388 ///
389 /// Several example programs are provided.
390 ///
391 /// \par Radio Performance
392 ///
393 /// Frequency accuracy may be debatable. For nominal frequency of 2401.000 MHz (ie channel 1),
394 /// my Yaesu VR-5000 receiver indicated the center frequency for my test radios
395 /// was 2401.121 MHz. Its not clear to me if the Yaesu
396 /// is the source of the error, but I tend to believe it, which would make the nRF24l01 frequency out by 121kHz.
397 ///
398 /// The measured power output for a nRF24L01p with PA and LNA set to 0dBm output is about 18dBm.
399 ///
400 /// \par Radio operating strategy and defaults
401 ///
402 /// The radio is enabled all the time, and switched between TX and RX modes depending on
403 /// whether there is any data to send. Sending data sets the radio to TX mode.
404 /// After data is sent, the radio automatically returns to Standby II mode. Calling waitAvailable() or
405 /// waitAvailableTimeout() starts the radio in RX mode.
406 ///
407 /// The radio is configured by default to Channel 2, 2Mbps, 0dBm power, 5 bytes address, payload width 1, CRC enabled
408 /// 2 byte CRC, No Auto-Ack mode. Enhanced shockburst is used.
409 /// TX and P0 are set to the Network address. Node addresses and decoding are handled with the RH_NRF24 module.
410 ///
411 /// \par Memory
412 ///
413 /// Memory usage of this class is minimal. The compiled client and server sketches are about 6000 bytes on Arduino.
414 /// The reliable client and server sketches compile to about 8500 bytes on Arduino.
415 /// RAM requirements are minimal.
416 ///
417 class RH_NRF24 : public RHNRFSPIDriver
418 {
419 public:
420 
421  /// \brief Defines convenient values for setting data rates in setRF()
422  typedef enum
423  {
424  DataRate1Mbps = 0, ///< 1 Mbps
425  DataRate2Mbps, ///< 2 Mbps
426  DataRate250kbps ///< 250 kbps
427  } DataRate;
428 
429  /// \brief Convenient values for setting transmitter power in setRF()
430  /// These are designed to agree with the values for RF_PWR in RH_NRF24_REG_06_RF_SETUP
431  /// To be passed to setRF();
432  typedef enum
433  {
434  // Add 20dBm for nRF24L01p with PA and LNA modules
435  TransmitPowerm18dBm = 0, ///< On nRF24, -18 dBm
436  TransmitPowerm12dBm, ///< On nRF24, -12 dBm
437  TransmitPowerm6dBm, ///< On nRF24, -6 dBm
438  TransmitPower0dBm, ///< On nRF24, 0 dBm
439  // Sigh, different power levels for the same bit patterns on RFM73:
440  // On RFM73P-S, there is a Tx power amp, so expect higher power levels, up to 20dBm. Alas
441  // there is no clear documentation on the power for different settings :-(
442  RFM73TransmitPowerm10dBm = 0, ///< On RFM73, -10 dBm
443  RFM73TransmitPowerm5dBm, ///< On RFM73, -5 dBm
444  RFM73TransmitPowerm0dBm, ///< On RFM73, 0 dBm
445  RFM73TransmitPower5dBm ///< On RFM73, 5 dBm. 20dBm on RFM73P-S2 ?
446 
447  } TransmitPower;
448 
449  /// Constructor. You can have multiple instances, but each instance must have its own
450  /// chip enable and slave select pin.
451  /// After constructing, you must call init() to initialise the interface
452  /// and the radio module
453  /// \param[in] chipEnablePin the Arduino pin to use to enable the chip for transmit/receive
454  /// \param[in] slaveSelectPin the Arduino pin number of the output to use to select the NRF24 before
455  /// accessing it. Defaults to the normal SS pin for your Arduino (D10 for Diecimila, Uno etc, D53 for Mega,
456  /// D10 for Maple)
457  /// \param[in] spi Pointer to the SPI interface object to use.
458  /// Defaults to the standard Arduino hardware SPI interface
459  RH_NRF24(uint8_t chipEnablePin = 8, uint8_t slaveSelectPin = SS, RHGenericSPI& spi = hardware_spi);
460 
461  /// Initialises this instance and the radio module connected to it.
462  /// The following steps are taken:g
463  /// - Set the chip enable and chip select pins to output LOW, HIGH respectively.
464  /// - Initialise the SPI output pins
465  /// - Initialise the SPI interface library to 8MHz (Hint, if you want to lower
466  /// the SPI frequency (perhaps where you have other SPI shields, low voltages etc),
467  /// call SPI.setClockDivider() after init()).
468  /// -Flush the receiver and transmitter buffers
469  /// - Set the radio to receive with powerUpRx();
470  /// \return true if everything was successful
471  bool init();
472 
473  /// Reads a single register from the NRF24
474  /// \param[in] reg Register number, one of RH_NRF24_REG_*
475  /// \return The value of the register
476  uint8_t spiReadRegister(uint8_t reg);
477 
478  /// Writes a single byte to the NRF24, and at the same time reads the current STATUS register
479  /// \param[in] reg Register number, one of RH_NRF24_REG_*
480  /// \param[in] val The value to write
481  /// \return the current STATUS (read while the command is sent)
482  uint8_t spiWriteRegister(uint8_t reg, uint8_t val);
483 
484  /// Reads a number of consecutive registers from the NRF24 using burst read mode
485  /// \param[in] reg Register number of the first register, one of RH_NRF24_REG_*
486  /// \param[in] dest Array to write the register values to. Must be at least len bytes
487  /// \param[in] len Number of bytes to read
488  /// \return the current STATUS (read while the command is sent)
489  uint8_t spiBurstReadRegister(uint8_t reg, uint8_t* dest, uint8_t len);
490 
491  /// Write a number of consecutive registers using burst write mode
492  /// \param[in] reg Register number of the first register, one of RH_NRF24_REG_*
493  /// \param[in] src Array of new register values to write. Must be at least len bytes
494  /// \param[in] len Number of bytes to write
495  /// \return the current STATUS (read while the command is sent)
496  uint8_t spiBurstWriteRegister(uint8_t reg, uint8_t* src, uint8_t len);
497 
498  /// Reads and returns the device status register NRF24_REG_02_DEVICE_STATUS
499  /// \return The value of the device status register
500  uint8_t statusRead();
501 
502  /// Sets the transmit and receive channel number.
503  /// The frequency used is (2400 + channel) MHz
504  /// \return true on success
505  bool setChannel(uint8_t channel);
506 
507  /// Sets the chip configuration that will be used to set
508  /// the NRF24 NRF24_REG_00_CONFIG register when in Idle mode. This allows you to change some
509  /// chip configuration for compatibility with libraries other than this one.
510  /// You should not normally need to call this.
511  /// Defaults to NRF24_EN_CRC| RH_NRF24_CRCO, which is the standard configuration for this library
512  /// (2 byte CRC enabled).
513  /// \param[in] mode The chip configuration to be used whe in Idle mode.
514  /// \return true on success
515  bool setOpMode(uint8_t mode);
516 
517  /// Sets the Network address.
518  /// Only nodes with the same network address can communicate with each other. You
519  /// can set different network addresses in different sets of nodes to isolate them from each other.
520  /// Internally, this sets the nRF24 TX_ADDR and RX_ADDR_P0 to be the given network address.
521  /// The default network address is 0xE7E7E7E7E7
522  /// \param[in] address The new network address. Must match the network address of any receiving node(s).
523  /// \param[in] len Number of bytes of address to set (3 to 5).
524  /// \return true on success, false if len is not in the range 3-5 inclusive.
525  bool setNetworkAddress(uint8_t* address, uint8_t len);
526 
527  /// Sets the data rate and transmitter power to use. Note that the nRF24 and the RFM73 have different
528  /// available power levels, and for convenience, 2 different sets of values are available in the
529  /// RH_NRF24::TransmitPower enum. The ones with the RFM73 only have meaning on the RFM73 and compatible
530  /// devces. The others are for the nRF24.
531  /// \param [in] data_rate The data rate to use for all packets transmitted and received. One of RH_NRF24::DataRate.
532  /// \param [in] power Transmitter power. One of RH_NRF24::TransmitPower.
533  /// \return true on success
534  bool setRF(DataRate data_rate, TransmitPower power);
535 
536  /// Sets the radio in power down mode, with the configuration set to the
537  /// last value from setOpMode().
538  /// Sets chip enable to LOW.
539  void setModeIdle();
540 
541  /// Sets the radio in RX mode.
542  /// Sets chip enable to HIGH to enable the chip in RX mode.
543  void setModeRx();
544 
545  /// Sets the radio in TX mode.
546  /// Pulses the chip enable LOW then HIGH to enable the chip in TX mode.
547  void setModeTx();
548 
549  /// Sends data to the address set by setTransmitAddress()
550  /// Sets the radio to TX mode
551  /// \param [in] data Data bytes to send.
552  /// \param [in] len Number of data bytes to send
553  /// \return true on success (which does not necessarily mean the receiver got the message, only that the message was
554  /// successfully transmitted).
555  bool send(const uint8_t* data, uint8_t len);
556 
557  /// Blocks until the current message (if any)
558  /// has been transmitted
559  /// \return true on success, false if the chip is not in transmit mode or other transmit failure
560  virtual bool waitPacketSent();
561 
562  /// Indicates if the chip is in transmit mode and
563  /// there is a packet currently being transmitted
564  /// \return true if the chip is in transmit mode and there is a transmission in progress
565  bool isSending();
566 
567  /// Prints the value of all chip registers
568  /// to the Serial device if RH_HAVE_SERIAL is defined for the current platform
569  /// For debugging purposes only.
570  /// \return true on success
571  bool printRegisters();
572 
573  /// Checks whether a received message is available.
574  /// This can be called multiple times in a timeout loop
575  /// \return true if a complete, valid message has been received and is able to be retrieved by
576  /// recv()
577  bool available();
578 
579  /// Turns the receiver on if it not already on.
580  /// If there is a valid message available, copy it to buf and return true
581  /// else return false.
582  /// If a message is copied, *len is set to the length (Caution, 0 length messages are permitted).
583  /// You should be sure to call this function frequently enough to not miss any messages
584  /// It is recommended that you call it in your main loop.
585  /// \param[in] buf Location to copy the received message
586  /// \param[in,out] len Pointer to available space in buf. Set to the actual number of octets copied.
587  /// \return true if a valid message was copied to buf
588  bool recv(uint8_t* buf, uint8_t* len);
589 
590  /// The maximum message length supported by this driver
591  /// \return The maximum message length supported by this driver
592  uint8_t maxMessageLength();
593 
594  /// Sets the radio into Power Down mode.
595  /// If successful, the radio will stay in Power Down mode until woken by
596  /// changing mode it idle, transmit or receive (eg by calling send(), recv(), available() etc)
597  /// Caution: there is a time penalty as the radio takes a finite time to wake from sleep mode.
598  /// \return true if sleep mode was successfully entered.
599  virtual bool sleep();
600 
601 protected:
602  /// Flush the TX FIFOs
603  /// \return the value of the device status register
604  uint8_t flushTx();
605 
606  /// Flush the RX FIFOs
607  /// \return the value of the device status register
608  uint8_t flushRx();
609 
610  /// Examine the receive buffer to determine whether the message is for this node
611  void validateRxBuf();
612 
613  /// Clear our local receive buffer
614  void clearRxBuf();
615 
616 private:
617  /// This idle mode chip configuration
618  uint8_t _configuration;
619 
620  /// the number of the chip enable pin
621  uint8_t _chipEnablePin;
622 
623  /// Number of octets in the buffer
624  uint8_t _bufLen;
625 
626  /// The receiver/transmitter buffer
627  uint8_t _buf[RH_NRF24_MAX_PAYLOAD_LEN];
628 
629  /// True when there is a valid message in the buffer
630  bool _rxBufValid;
631 };
632 
633 /// @example nrf24_client.pde
634 /// @example nrf24_server.pde
635 /// @example nrf24_encrypted_client.pde
636 /// @example nrf24_encrypted_server.pde
637 /// @example nrf24_reliable_datagram_client.pde
638 /// @example nrf24_reliable_datagram_server.pde
639 /// @example RasPiRH.cpp
640 
641 #endif
RHGenericDriver::_rxGood
volatile uint16_t _rxGood
Count of the number of successfully transmitted messaged.
Definition: RHGenericDriver.h:292
RH_NRF24::TransmitPower0dBm
@ TransmitPower0dBm
On nRF24, 0 dBm.
Definition: RH_NRF24.h:438
RH_NRF24::setChannel
bool setChannel(uint8_t channel)
Definition: RH_NRF24.cpp:98
RH_NRF24::spiReadRegister
uint8_t spiReadRegister(uint8_t reg)
Definition: RH_NRF24.cpp:62
RH_NRF24::recv
bool recv(uint8_t *buf, uint8_t *len)
Definition: RH_NRF24.cpp:331
RHNRFSPIDriver::spiCommand
uint8_t spiCommand(uint8_t command)
Definition: RHNRFSPIDriver.cpp:31
RH_NRF24::flushRx
uint8_t flushRx()
Definition: RH_NRF24.cpp:93
RHGenericSPI
Base class for SPI interfaces.
Definition: RHGenericSPI.h:30
RH_NRF24::setOpMode
bool setOpMode(uint8_t mode)
Definition: RH_NRF24.cpp:104
RHNRFSPIDriver::spiRead
uint8_t spiRead(uint8_t reg)
Definition: RHNRFSPIDriver.cpp:48
RH_NRF24::RFM73TransmitPowerm10dBm
@ RFM73TransmitPowerm10dBm
On RFM73, -10 dBm.
Definition: RH_NRF24.h:442
RH_NRF24::setModeRx
void setModeRx()
Definition: RH_NRF24.cpp:163
RH_NRF24::RFM73TransmitPower5dBm
@ RFM73TransmitPower5dBm
On RFM73, 5 dBm. 20dBm on RFM73P-S2 ?
Definition: RH_NRF24.h:445
RH_NRF24::isSending
bool isSending()
Definition: RH_NRF24.cpp:236
RHGenericDriver::_rxHeaderFrom
volatile uint8_t _rxHeaderFrom
FROM header in the last received mesasge.
Definition: RHGenericDriver.h:265
RHGenericSPI::setFrequency
virtual void setFrequency(Frequency frequency)
Definition: RHGenericSPI.cpp:27
RH_NRF24::printRegisters
bool printRegisters()
Definition: RH_NRF24.cpp:242
RH_NRF24::send
bool send(const uint8_t *data, uint8_t len)
Definition: RH_NRF24.cpp:188
RH_NRF24::waitPacketSent
virtual bool waitPacketSent()
Definition: RH_NRF24.cpp:209
RH_NRF24::TransmitPowerm18dBm
@ TransmitPowerm18dBm
On nRF24, -18 dBm.
Definition: RH_NRF24.h:435
RHGenericDriver::_rxHeaderTo
volatile uint8_t _rxHeaderTo
TO header in the last received mesasge.
Definition: RHGenericDriver.h:262
RH_NRF24::TransmitPower
TransmitPower
Convenient values for setting transmitter power in setRF() These are designed to agree with the value...
Definition: RH_NRF24.h:432
RHGenericDriver::_txHeaderFrom
uint8_t _txHeaderFrom
FROM header to send in all messages.
Definition: RHGenericDriver.h:277
RH_NRF24::TransmitPowerm6dBm
@ TransmitPowerm6dBm
On nRF24, -6 dBm.
Definition: RH_NRF24.h:437
RH_NRF24::setNetworkAddress
bool setNetworkAddress(uint8_t *address, uint8_t len)
Definition: RH_NRF24.cpp:110
RHGenericDriver::_thisAddress
uint8_t _thisAddress
This node id.
Definition: RHGenericDriver.h:256
RHGenericDriver::_txGood
volatile uint16_t _txGood
Count of the number of bad messages (correct checksum etc) received.
Definition: RHGenericDriver.h:295
RH_NRF24
Send and receive unaddressed, unreliable datagrams by nRF24L01 and compatible transceivers.
Definition: RH_NRF24.h:417
RHNRFSPIDriver::init
bool init()
Definition: RHNRFSPIDriver.cpp:15
RHGenericDriver::_promiscuous
bool _promiscuous
Whether the transport is in promiscuous mode.
Definition: RHGenericDriver.h:259
RHNRFSPIDriver::spiBurstWrite
uint8_t spiBurstWrite(uint8_t reg, const uint8_t *src, uint8_t len)
Definition: RHNRFSPIDriver.cpp:109
RHGenericDriver::_rxHeaderFlags
volatile uint8_t _rxHeaderFlags
FLAGS header in the last received mesasge.
Definition: RHGenericDriver.h:271
RH_NRF24::statusRead
uint8_t statusRead()
Definition: RH_NRF24.cpp:82
RH_NRF24::DataRate250kbps
@ DataRate250kbps
250 kbps
Definition: RH_NRF24.h:426
RH_NRF24::init
bool init()
Definition: RH_NRF24.cpp:17
RHGenericDriver::RHModeSleep
@ RHModeSleep
Transport hardware is in low power sleep mode (if supported)
Definition: RHGenericDriver.h:51
RH_NRF24::TransmitPowerm12dBm
@ TransmitPowerm12dBm
On nRF24, -12 dBm.
Definition: RH_NRF24.h:436
RH_NRF24::validateRxBuf
void validateRxBuf()
Examine the receive buffer to determine whether the message is for this node.
Definition: RH_NRF24.cpp:276
RH_NRF24::clearRxBuf
void clearRxBuf()
Clear our local receive buffer.
Definition: RH_NRF24.cpp:325
RH_NRF24::maxMessageLength
uint8_t maxMessageLength()
Definition: RH_NRF24.cpp:346
RH_NRF24::RFM73TransmitPowerm5dBm
@ RFM73TransmitPowerm5dBm
On RFM73, -5 dBm.
Definition: RH_NRF24.h:443
RHGenericDriver::_txHeaderId
uint8_t _txHeaderId
ID header to send in all messages.
Definition: RHGenericDriver.h:280
RHGenericDriver::_txHeaderTo
uint8_t _txHeaderTo
TO header to send in all messages.
Definition: RHGenericDriver.h:274
RH_NRF24::setRF
bool setRF(DataRate data_rate, TransmitPower power)
Definition: RH_NRF24.cpp:122
RH_NRF24::spiBurstWriteRegister
uint8_t spiBurstWriteRegister(uint8_t reg, uint8_t *src, uint8_t len)
Definition: RH_NRF24.cpp:77
RH_NRF24::DataRate2Mbps
@ DataRate2Mbps
2 Mbps
Definition: RH_NRF24.h:425
RHGenericDriver::_txHeaderFlags
uint8_t _txHeaderFlags
FLAGS header to send in all messages.
Definition: RHGenericDriver.h:283
RH_NRF24::DataRate
DataRate
Defines convenient values for setting data rates in setRF()
Definition: RH_NRF24.h:422
RHNRFSPIDriver
Base class for RadioHead drivers that use the SPI bus to communicate with its NRF family transport ha...
Definition: RHNRFSPIDriver.h:33
RH_NRF24::spiBurstReadRegister
uint8_t spiBurstReadRegister(uint8_t reg, uint8_t *dest, uint8_t len)
Definition: RH_NRF24.cpp:72
RHNRFSPIDriver::spiWrite
uint8_t spiWrite(uint8_t reg, uint8_t val)
Definition: RHNRFSPIDriver.cpp:66
RH_NRF24::RFM73TransmitPowerm0dBm
@ RFM73TransmitPowerm0dBm
On RFM73, 0 dBm.
Definition: RH_NRF24.h:444
RHGenericDriver::_rxHeaderId
volatile uint8_t _rxHeaderId
ID header in the last received mesasge.
Definition: RHGenericDriver.h:268
RH_NRF24::setModeIdle
void setModeIdle()
Definition: RH_NRF24.cpp:141
RH_NRF24::flushTx
uint8_t flushTx()
Definition: RH_NRF24.cpp:88
RH_NRF24::spiWriteRegister
uint8_t spiWriteRegister(uint8_t reg, uint8_t val)
Definition: RH_NRF24.cpp:67
RHNRFSPIDriver::spiBurstRead
uint8_t spiBurstRead(uint8_t reg, uint8_t *dest, uint8_t len)
Definition: RHNRFSPIDriver.cpp:90
RHGenericDriver::RHModeIdle
@ RHModeIdle
Transport is idle.
Definition: RHGenericDriver.h:52
RHGenericDriver::_mode
volatile RHMode _mode
The current transport operating mode.
Definition: RHGenericDriver.h:253
RH_NRF24::RH_NRF24
RH_NRF24(uint8_t chipEnablePin=8, uint8_t slaveSelectPin=SS, RHGenericSPI &spi=hardware_spi)
Definition: RH_NRF24.cpp:8
RHNRFSPIDriver::_spi
RHGenericSPI & _spi
Reference to the RHGenericSPI instance to use to trasnfer data with teh SPI device.
Definition: RHNRFSPIDriver.h:95
RHGenericDriver::RHModeTx
@ RHModeTx
Transport is in the process of transmitting a message.
Definition: RHGenericDriver.h:53
RH_NRF24::available
bool available()
Definition: RH_NRF24.cpp:294
RHGenericDriver::mode
virtual RHMode mode()
Definition: RHGenericDriver.cpp:159
RH_NRF24::DataRate1Mbps
@ DataRate1Mbps
1 Mbps
Definition: RH_NRF24.h:424
RHGenericDriver::RHModeRx
@ RHModeRx
Transport is in the process of receiving a message.
Definition: RHGenericDriver.h:54
RHGenericSPI::Frequency1MHz
@ Frequency1MHz
SPI bus frequency close to 1MHz.
Definition: RHGenericSPI.h:57
RHGenericDriver::waitCAD
virtual bool waitCAD()
Definition: RHGenericDriver.cpp:72
RH_NRF24::sleep
virtual bool sleep()
Definition: RH_NRF24.cpp:151
RH_NRF24::setModeTx
void setModeTx()
Definition: RH_NRF24.cpp:173