NRF24
NRF24.h
1 // NRF24.h
2 // Author: Mike McCauley
3 // Copyright (C) 2012 Mike McCauley
4 // $Id: NRF24.h,v 1.2 2014/05/20 06:00:55 mikem Exp mikem $
5 //
6 /// \mainpage NRF24 library for Arduino
7 ///
8 /// This NRF24 library has now been superceded by the RadioHead
9 /// library http://www.airspayce.com/mikem/arduino/RadioHead
10 /// RadioHead and its RH_NRF24 driver provides all the features supported by NRF24, and much more
11 /// besides, including Reliable Datagrams, Addressing, Routing and Meshes. All the platforms that
12 /// NRF24 supported are also supported by RadioHead.
13 ///
14 /// This library will no longer be maintained or updated, but we will continue to publish
15 /// it for the benefit of the the community, and you may continue to use it within the terms of
16 /// the license. Nevertheless we recommend upgrading to RadioHead where
17 /// possible.
18 ///
19 /// This is the Arduino NRF24 library.
20 /// It provides an object-oriented interface for sending and receiving data messages with Nordic nRF24L01
21 /// based radio modules, such as the sparkfun WRL-00691 http://www.sparkfun.com/products/691
22 ///
23 /// The nRF24L01 (http://www.sparkfun.com/datasheets/Wireless/Nordic/nRF24L01P_Product_Specification_1_0.pdf)
24 /// is a low-cost 2.4GHz ISM transceiver module. It supports a number of channel frequencies in the 2.4GHz band
25 /// and a range of data rates.
26 ///
27 /// This library provides functions for sending and receiving messages of up to 32 octets on any
28 /// frequency supported by the nRF24L01, at a selected data rate.
29 ///
30 /// Up to 2 nRF24L01 modules can be connected to an Arduino, permitting the construction of translators
31 /// and frequency changers, etc.
32 ///
33 /// This library provides classes for
34 /// - NRF24: addressed, reliable, retransmitted, acknowledged messages (using nrf24 Enhanced Shockburst)
35 ///
36 /// Example Arduino programs are included to show the main modes of use.
37 ///
38 /// The version of the package that this documentation refers to can be downloaded
39 /// from http://www.airspayce.com/mikem/arduino/NRF24/NRF24-1.14.zip
40 /// You can find the latest version at http://www.airspayce.com/mikem/arduino/NRF24
41 ///
42 /// You can also find online help and disussion at
43 /// http://groups.google.com/group/NRF24-arduino
44 /// Please use that group for all questions and discussions on this topic.
45 /// Do not contact the author directly, unless it is to discuss commercial licensing.
46 /// Before asking a question or reporting a bug, please read http://www.catb.org/esr/faqs/smart-questions.html
47 ///
48 /// Tested on Arduino Diecimila, Duemilanove and Mega with arduino 1.0
49 /// on OpenSuSE 12.1 with avr-libc-1.7.1-1.1, cross-avr-binutils-2.19.1-33.1
50 /// and cross-avr-gcc-4.3.3_20100125-28.1, and with arduino-0021 on the same platforms
51 ///
52 /// \par Packet Format
53 ///
54 /// All messages sent and received by this NRF24 library must conform to this packet format, as specified by
55 /// the nRF24L01 product specificaiton:
56 ///
57 /// - 1 octets PREAMBLE
58 /// - 4 octets ADDRESS
59 /// - 9 bits packet control field
60 /// - 0 to 32 octets PAYLOAD
61 /// - 2 octets CRC
62 ///
63 /// \par Connecting nRF24L01 to Arduino
64 ///
65 /// The physical connection between the nRF24L01 and the Arduino require 3.3V, the 3 x SPI pins (SCK, SDI, SDO),
66 /// a Chip Enable pin and a Slave Select pin.
67 /// If you are using the Sparkfun WRL-00691 module, it has a voltage regulator on board and
68 /// can be run with 5V VCC
69 /// The examples below assume the Sparkfun WRL-00691 module
70 ///
71 /// Connect the nRF24L01 to most Arduino's like this (Caution, Arduino Mega has different pins for SPI,
72 /// see below),
73 /// \code
74 /// Arduino Sparkfun WRL-00691
75 /// 3V3 or 5V----VCC (3.3V to 7V in)
76 /// pin D8-----------CE (chip enable in)
77 /// SS pin D10----------CSN (chip select in)
78 /// SCK pin D13----------SCK (SPI clock in)
79 /// MOSI pin D11----------SDI (SPI Data in)
80 /// MISO pin D12----------SDO (SPI data out)
81 /// IRQ (Interrupt output, not connected)
82 /// GND----------GND (ground in)
83 /// \endcode
84 ///
85 /// For an Arduino Leonardo (the SPI pins do not come out on the Digital pins as for normal Arduino, but only
86 /// appear on the ICSP header)
87 /// \code
88 /// Leonardo Sparkfun WRL-00691
89 /// 3V3 or 5V----VCC (3.3V to 7V in)
90 /// pin D8-----------CE (chip enable in)
91 /// SS pin D10----------CSN (chip select in)
92 /// SCK ICSP pin 3----------SCK (SPI clock in)
93 /// MOSI ICSP pin 4----------SDI (SPI Data in)
94 /// MISO ICSP pin 1----------SDO (SPI data out)
95 /// IRQ (Interrupt output, not connected)
96 /// GND----------GND (ground in)
97 /// \endcode
98 /// and initialise the NRF24 object like this to explicitly set the SS pin
99 /// NRF24 nrf24(8, 10);
100 ///
101 /// For an Arduino Mega:
102 /// \code
103 /// Mega Sparkfun WRL-00691
104 /// 3V3 or 5V----VCC (3.3V to 7V in)
105 /// pin D8-----------CE (chip enable in)
106 /// SS pin D53----------CSN (chip select in)
107 /// SCK pin D52----------SCK (SPI clock in)
108 /// MOSI pin D51----------SDI (SPI Data in)
109 /// MISO pin D50----------SDO (SPI data out)
110 /// IRQ (Interrupt output, not connected)
111 /// GND----------GND (ground in)
112 /// \endcode
113 /// and you can then use the default constructor NRF24().
114 /// You can override the default settings for the CSN and CE pins
115 /// in the NRF24() constructor if you wish to connect the slave select CSN to other than the normal one for your
116 /// Arduino (D10 for Diecimila, Uno etc and D53 for Mega)
117 ///
118 /// Caution: on some Arduinos such as the Mega 2560, if you set the slave select pin to be other than the usual SS
119 /// pin (D53 on Mega 2560), you may need to set the usual SS pin to be an output to force the Arduino into SPI
120 /// master mode.
121 ///
122 /// Caution: this module has not been proved to work with Leonardo, at least without level
123 /// shifters between the NRF24 and the Leonardo. Tests seem to indicate that such level shifters would be required
124 /// with Leonardo to make it work.
125 ///
126 /// It is possible to have 2 radios conected to one arduino, provided each radio has its own
127 /// CSN and CE line (SCK, SDI and SDO are common to both radios)
128 ///
129 /// \par Example programs
130 ///
131 /// The following example programs are provided:
132 /// -nrf24_ping_client, nrf24_pin_server. This is a matched pair. The client sends (acknowledged)
133 /// 4 byte timestamp to the server which replies (acknowledged).
134 /// The client measures the round-trip time and prints it. Typical RTT is 1-2 msec.
135 /// These are compatible radio wise with the ping_client and ping_server programs that come with the Mirf library
136 /// though the electrical connections are different
137 /// -nrf24_specan. Example sketch showing how to create a primitive spectrum analyser
138 /// with the NRF24 class. The nRF24L01 received power detector is only one bit, but
139 /// this will show which channels have more than -64dBm present.
140 /// -nrf24_audio_tx, nrf24_audio_rx. This is a matched pair. The clinet sends a stream of audio samples measured
141 /// from analog input 0 to the receiver, which reconstructs them on output D6. See comments in those files for
142 /// electrical requirements. The pair demonstrates the use of NRF24 in NOACK modefor improved performance
143 /// (but no reliability). Can achieve 6.4kHz sample rate. Dont expect good quality audio!
144 ///
145 /// \par Radio Performance
146 ///
147 /// The performance of this radio seems to be very good. I was able to build ping client/server
148 /// that was able to achieve over 800 round trips per second
149 /// (at 0dBm power, 2Mbps, channel 1, 4 byte payload each way, 1 checksum byte) when the radios were next to each other.
150 /// This rate could still be achieved at 15m distance, but the orientation of the radios and
151 /// obstructions became critical. The human body can easily block these signals.
152 /// Best response was when the chip antennas were broadside to each other.
153 ///
154 /// It is possible to get even better streaming performance using NOACK mode (see the nrf24_audio_tx sample)
155 /// at the cost of nop reliability.
156 /// In NOACK mode, at 2Mbps, 32 byte payload, can get about 1900 packets per second: 60800 bytes of payload per second
157 ///
158 /// Frequency accuracy may be debatable. For nominal frequency of 2401.000 MHz (ie channel 1),
159 /// my Yaesu VR-5000 receiver indicated the center frequency for my test radios
160 /// was 2401.121 MHz. Its not clear to me if the Yaesu
161 /// is the source of the error, but I tend to believe it, which would make the nRF24l01 frequency out by 121kHz.
162 ///
163 /// \par Radio operating strategy and defaults
164 ///
165 /// The radio is enabled all the time, and switched between TX and RX modes depending on
166 /// whether there is any data to send. Sending data sets the radio to TX mode.
167 /// After data is sent, the radion automatically returns to Standby II mode. Calling waitAvailable() or
168 /// waitAvailableTimeout() starts the radio in RX mode.
169 ///
170 /// The radio is configured by default to Channel 2, 2Mbps, 0dBm power, 5 bytes address, payload width 1, CRC enabled
171 /// 1 byte CRC, Auto-Ack mode. Enhanced shockburst is used.
172 /// P1 is the receive pipe. P0 is set to the transmit address to enable autoack.
173 ///
174 /// \par Memory
175 ///
176 /// Memory usage of this program is minimal. The compiled ping client and server programs are about 4000 bytes.
177 /// RAM requirements of the library are minimal.
178 ///
179 /// \par Installation
180 ///
181 /// Install in the usual way: unzip the distribution zip file to the libraries
182 /// sub-folder of your sketchbook.
183 ///
184 /// This software is Copyright (C) 2012 Mike McCauley. Use is subject to license
185 /// conditions. The main licensing options available are GPL V2 or Commercial:
186 ///
187 /// \par Donations
188 ///
189 /// This library is offered under a free GPL license for those who want to use it that way.
190 /// We try hard to keep it up to date, fix bugs
191 /// and to provide free support. If this library has helped you save time or money, please consider donating at
192 /// http://www.airspayce.com or here:
193 ///
194 /// \htmlonly <form action="https://www.paypal.com/cgi-bin/webscr" method="post"><input type="hidden" name="cmd" value="_donations" /> <input type="hidden" name="business" value="mikem@airspayce.com" /> <input type="hidden" name="lc" value="AU" /> <input type="hidden" name="item_name" value="Airspayce" /> <input type="hidden" name="item_number" value="NRF24" /> <input type="hidden" name="currency_code" value="USD" /> <input type="hidden" name="bn" value="PP-DonationsBF:btn_donateCC_LG.gif:NonHosted" /> <input type="image" alt="PayPal — The safer, easier way to pay online." name="submit" src="https://www.paypalobjects.com/en_AU/i/btn/btn_donateCC_LG.gif" /> <img alt="" src="https://www.paypalobjects.com/en_AU/i/scr/pixel.gif" width="1" height="1" border="0" /></form> \endhtmlonly
195 ///
196 /// \par Open Source Licensing GPL V2
197 ///
198 /// This is the appropriate option if you want to share the source code of your
199 /// application with everyone you distribute it to, and you also want to give them
200 /// the right to share who uses it. If you wish to use this software under Open
201 /// Source Licensing, you must contribute all your source code to the open source
202 /// community in accordance with the GPL Version 2 when your application is
203 /// distributed. See http://www.gnu.org/copyleft/gpl.html
204 ///
205 /// \par Commercial Licensing
206 ///
207 /// This is the appropriate option if you are creating proprietary applications
208 /// and you are not prepared to distribute and share the source code of your
209 /// application. Contact info@airspayce.com for details.
210 ///
211 /// \par Revision History
212 ///
213 /// \version 1.0 Initial release
214 ///
215 /// \version 1.1 Changed default value for slave slect pin in constructor to be SS, ie the normal one for
216 /// the compiled Arduino (D10 for Diecimila, Uno etc and D53 for Mega). This is because some Arduinos such as Mega 2560
217 /// reportedly use the type of the SS pin to determine whether to run in slave or master mode. Therfore it
218 /// is preferred that the slave select pin actually be the normal SS pin.
219 ///
220 /// \version 1.2 Updated documentation about what happens during init, and that SPI is initialised to 8MHz
221 /// (but can be set to other frequencies after calling init()
222 /// \version 1.3 Fixed error in title of main page
223 /// \version 1.4 Fixed typo in nrf24_test.pde, reported by TOM.
224 /// \version 1.6 Fixed an error NRF24::setRF in setting data rate to 250kbps. Reported by Shiu Kumar.
225 /// \version 1.7 Improvements to waitPacketSent() so if the chip is not in transmit mode, it wont wait forever.
226 /// Improvements to isSending() so it returns false if the chip is not in transmit mode.
227 /// \version 1.8 Fixed a conflict between 2 definitions of NRF24_TX_FULL. The one in 07 STATUS is changed to
228 /// NRF24_STATUS_TX_FULL. Reported by Charles-Henri Hallard.
229 /// Updated author and distribution location details to airspayce.com
230 /// \version 1.9 Improvements to waitAvailableTimeout to make it robust on millis() rollover.
231 /// \version 1.10 Testing with Leonardo and update documentation to reflect special electrical
232 /// connection needs on Leonardo.
233 /// \version 1.11 NRF24_COMMAND_W_ACK_PAYLOAD(c) is now a macro that takes a pipe number parameter.
234 /// Added new function setRetry(), a convenience for setting ARD and ARC retry parameter.
235 /// Can now customise the radio configuration byte with setConfiguration().
236 /// Added new examples crazyflie and crazyflie_client that use the NRF24 library to
237 /// communicate with Crazyflie quadcopters and transmitters.
238 /// \version 1.12 NRF24::init, powerUpRx, powerUpTx will now fail if no device is connected;
239 /// \version 1.13 Added End Of Life notice. This library will no longer be maintained
240 /// and updated: use RadioHead instead.
241 /// \version 1.14 Fixed problem that prevented 250kbps data rate working.
242 ///
243 /// \author Mike McCauley (mikem@airspayce.com) DO NOT CONTACT THE AUTHOR DIRECTLY: USE THE LISTS
244 
245 #ifndef NRF24_h
246 #define NRF24_h
247 
248 #if ARDUINO >= 100
249 #include <Arduino.h>
250 #else
251 #include <wiring.h>
252 #include <pins_arduino.h>
253 #endif
254 
255 // These defs cause trouble on some versions of Arduino
256 #undef round
257 #undef double
258 
259 // This is the bit in the SPI address that marks it as a write
260 #define NRF24_SPI_WRITE_MASK 0x80
261 
262 // This is the maximum message length that can be supported by this library. Limited by
263 // the suported message lengths oin the nRF24
264 // Can be pre-defined to a smaller size (to save SRAM) prior to including this header
265 #ifndef NRF24_MAX_MESSAGE_LEN
266 #define NRF24_MAX_MESSAGE_LEN 32
267 #endif
268 
269 // Keep track of the mode the NRF24 is in
270 #define NRF24_MODE_IDLE 0
271 #define NRF24_MODE_RX 1
272 #define NRF24_MODE_TX 2
273 
274 // These values we set for FIFO thresholds are actually the same as the POR values
275 #define NRF24_TXFFAEM_THRESHOLD 4
276 #define NRF24_RXFFAFULL_THRESHOLD 55
277 
278 // This is the default node address,
279 #define NRF24_DEFAULT_NODE_ADDRESS 0x00000000
280 
281 // This address in the TO addreess signifies a broadcast
282 #define NRF24_BROADCAST_ADDRESS 0xffffffffff
283 
284 // SPI Command names
285 #define NRF24_COMMAND_R_REGISTER 0x00
286 #define NRF24_COMMAND_W_REGISTER 0x20
287 #define NRF24_COMMAND_R_RX_PAYLOAD 0x61
288 #define NRF24_COMMAND_W_TX_PAYLOAD 0xa0
289 #define NRF24_COMMAND_FLUSH_TX 0xe1
290 #define NRF24_COMMAND_FLUSH_RX 0xe2
291 #define NRF24_COMMAND_REUSE_TX_PL 0xe3
292 #define NRF24_COMMAND_R_RX_PL_WID 0x60
293 #define NRF24_COMMAND_W_ACK_PAYLOAD(pipe) (0xa8|(pipe&0x7))
294 #define NRF24_COMMAND_W_TX_PAYLOAD_NOACK 0xb0
295 #define NRF24_COMMAND_NOP 0xff
296 
297 // Register names
298 #define NRF24_REGISTER_MASK 0x1f
299 #define NRF24_REG_00_CONFIG 0x00
300 #define NRF24_REG_01_EN_AA 0x01
301 #define NRF24_REG_02_EN_RXADDR 0x02
302 #define NRF24_REG_03_SETUP_AW 0x03
303 #define NRF24_REG_04_SETUP_RETR 0x04
304 #define NRF24_REG_05_RF_CH 0x05
305 #define NRF24_REG_06_RF_SETUP 0x06
306 #define NRF24_REG_07_STATUS 0x07
307 #define NRF24_REG_08_OBSERVE_TX 0x08
308 #define NRF24_REG_09_RPD 0x09
309 #define NRF24_REG_0A_RX_ADDR_P0 0x0a
310 #define NRF24_REG_0B_RX_ADDR_P1 0x0b
311 #define NRF24_REG_0C_RX_ADDR_P2 0x0c
312 #define NRF24_REG_0D_RX_ADDR_P3 0x0d
313 #define NRF24_REG_0E_RX_ADDR_P4 0x0e
314 #define NRF24_REG_0F_RX_ADDR_P5 0x0f
315 #define NRF24_REG_10_TX_ADDR 0x10
316 #define NRF24_REG_11_RX_PW_P0 0x11
317 #define NRF24_REG_12_RX_PW_P1 0x12
318 #define NRF24_REG_13_RX_PW_P2 0x13
319 #define NRF24_REG_14_RX_PW_P3 0x14
320 #define NRF24_REG_15_RX_PW_P4 0x15
321 #define NRF24_REG_16_RX_PW_P5 0x16
322 #define NRF24_REG_17_FIFO_STATUS 0x17
323 #define NRF24_REG_1C_DYNPD 0x1c
324 #define NRF24_REG_1D_FEATURE 0x1d
325 
326 // These register masks etc are named wherever possible
327 // corresponding to the bit and field names in the nRF24L01 Product Specification
328 // #define NRF24_REG_00_CONFIG 0x00
329 #define NRF24_MASK_RX_DR 0x40
330 #define NRF24_MASK_TX_DS 0x20
331 #define NRF24_MASK_MAX_RT 0x10
332 #define NRF24_EN_CRC 0x08
333 #define NRF24_CRCO 0x04
334 #define NRF24_PWR_UP 0x02
335 #define NRF24_PRIM_RX 0x01
336 
337 // #define NRF24_REG_01_EN_AA 0x01
338 #define NRF24_ENAA_P5 0x20
339 #define NRF24_ENAA_P4 0x10
340 #define NRF24_ENAA_P3 0x08
341 #define NRF24_ENAA_P2 0x04
342 #define NRF24_ENAA_P1 0x02
343 #define NRF24_ENAA_P0 0x01
344 
345 // #define NRF24_REG_02_EN_RXADDR 0x02
346 #define NRF24_ERX_P5 0x20
347 #define NRF24_ERX_P4 0x10
348 #define NRF24_ERX_P3 0x08
349 #define NRF24_ERX_P2 0x04
350 #define NRF24_ERX_P1 0x02
351 #define NRF24_ERX_P0 0x01
352 
353 // #define NRF24_REG_03_SETUP_AW 0x03
354 #define NRF24_AW_3_BYTES 0x01
355 #define NRF24_AW_4_BYTES 0x02
356 #define NRF24_AW_5_BYTES 0x03
357 
358 // #define NRF24_REG_04_SETUP_RETR 0x04
359 #define NRF24_ARD 0xf0
360 #define NRF24_ARC 0x0f
361 
362 // #define NRF24_REG_05_RF_CH 0x05
363 #define NRF24_RF_CH 0x7f
364 
365 // #define NRF24_REG_06_RF_SETUP 0x06
366 #define NRF24_CONT_WAVE 0x80
367 #define NRF24_RF_DR_LOW 0x20
368 #define NRF24_PLL_LOCK 0x10
369 #define NRF24_RF_DR_HIGH 0x08
370 #define NRF24_PWR 0x06
371 #define NRF24_PWR_m18dBm 0x00
372 #define NRF24_PWR_m12dBm 0x02
373 #define NRF24_PWR_m6dBm 0x04
374 #define NRF24_PWR_0dBm 0x06
375 
376 // #define NRF24_REG_07_STATUS 0x07
377 #define NRF24_RX_DR 0x40
378 #define NRF24_TX_DS 0x20
379 #define NRF24_MAX_RT 0x10
380 #define NRF24_RX_P_NO 0x0e
381 #define NRF24_STATUS_TX_FULL 0x01
382 
383 
384 // #define NRF24_REG_08_OBSERVE_TX 0x08
385 #define NRF24_PLOS_CNT 0xf0
386 #define NRF24_ARC_CNT 0x0f
387 
388 // #define NRF24_REG_09_RPD 0x09
389 #define NRF24_RPD 0x01
390 
391 // #define NRF24_REG_17_FIFO_STATUS 0x17
392 #define NRF24_TX_REUSE 0x40
393 #define NRF24_TX_FULL 0x20
394 #define NRF24_TX_EMPTY 0x10
395 #define NRF24_RX_FULL 0x02
396 #define NRF24_RX_EMPTY 0x01
397 
398 // #define NRF24_REG_1C_DYNPD 0x1c
399 #define NRF24_DPL_P5 0x20
400 #define NRF24_DPL_P4 0x10
401 #define NRF24_DPL_P3 0x08
402 #define NRF24_DPL_P2 0x04
403 #define NRF24_DPL_P1 0x02
404 #define NRF24_DPL_P0 0x01
405 
406 // #define NRF24_REG_1D_FEATURE 0x1d
407 #define NRF24_EN_DPL 0x04
408 #define NRF24_EN_ACK_PAY 0x02
409 #define NRF24_EN_DYN_ACK 0x01
410 
411 
412 /////////////////////////////////////////////////////////////////////
413 /// \class NRF24 NRF24.h <NRF24.h>
414 /// \brief Send and receive addressed, reliable, acknowledged datagrams by nRF24L01.
415 ///
416 /// This base class provides basic functions for sending and receiving addressed, reliable,
417 /// automatically acknowledged and retransmitted
418 /// datagrams via nRF24L01 of arbitrary length to 32 octets per packet.
419 /// Sender and receiver must each know the addreesses of the other, so arbitrary meshes and stars are
420 /// not possible at this level.
421 /// Directed replies (ie replies sent back to the original sender) are not possible
422 /// (the address of the sender is not carried in the message).
423 /// See subclasses for support for this.
424 ///
425 /// Subclasses may use this class to implement streams,
426 /// mesh routers, repeaters, translators etc.
427 ///
428 /// On transmission, the addresses of this node defaults to 0x0000000000, unless changed by a subclass.
429 ///
430 /// The radio is configured to use Enhanced Shockburst with retransmits.
431 /// TX_ADDR and RX_ADDR_P0 are set to the transmit address (ie the address of the destination for the next message
432 /// RX_ADDR_P1 is set to the address of this node
433 /// RX_ADDR_P2 is set to RX_ADDR_P1 with the LSbyte set to 0xff, for use as a broadcast address
434 ///
435 /// Naturally, for any 2 radios to communicate that must be configured to use the same frequency and
436 /// data rate, and with compatible addresses
437 class NRF24
438 {
439 public:
440 
441  /// \brief Defines convenient values for setting data rates in setRF()
442  typedef enum
443  {
444  NRF24DataRate1Mbps = 0, ///< 1 Mbps
445  NRF24DataRate2Mbps, ///< 2 Mbps
446  NRF24DataRate250kbps ///< 250 kbps
447  } NRF24DataRate;
448 
449  /// \brief Convenient values for setting transmitter power in setRF()
450  /// These are designed to agree with the values for RF_PWR
451  /// To be passed to setRF();
452  typedef enum
453  {
454  NRF24TransmitPowerm18dBm = 0, ///< -18 dBm
459 
460  /// Constructor. You can have multiple instances, but each instance must have its own
461  /// chip enable and slave select pin.
462  /// After constructing, you must call init() to initialise the interface
463  /// and the radio module
464  /// \param[in] chipEnablePin the Arduino pin to use to enable the chip for4 transmit/receive
465  /// \param[in] chipSelectPin the Arduino pin number of the output to use to select the NRF24 before
466  /// accessing it
467  NRF24(uint8_t chipEnablePin = 8, uint8_t chipSelectPin = SS);
468 
469  /// Initialises this instance and the radio module connected to it.
470  /// The following steps are taken:g
471  /// - Set the chip enable and chip select pins to output LOW, HIGH respectively.
472  /// - Initialise the SPI output pins
473  /// - Initialise the SPI interface library to 8MHz (Hint, if you want to lower
474  /// the SPI frequency (perhaps where you have other SPI shields, low voltages etc),
475  /// call SPI.setClockDivider() after init()).
476  /// -Flush the receiver and transmitter buffers
477  /// - Set the radio to receive with powerUpRx();
478  /// \return true if everything was successful
479  boolean init();
480 
481  /// Execute an SPI command that requires neither reading or writing
482  /// \param[in] command the SPI command to execute, one of NRF24_COMMAND_*
483  /// \return the value of the device status register
484  uint8_t spiCommand(uint8_t command);
485 
486  /// Reads a single command byte from the NRF24
487  /// \param[in] command Command number, one of NRF24_COMMAND_*
488  /// \return the single byte returned by the command
489  uint8_t spiRead(uint8_t command);
490 
491  /// Writes a single command byte to the NRF24
492  /// \param[in] command Command number, one of NRF24_COMMAND_*
493  /// \param[in] val The value to write
494  /// \return the value of the device status register
495  uint8_t spiWrite(uint8_t command, uint8_t val);
496 
497  /// Reads a number of consecutive bytes from a command using burst read mode
498  /// \param[in] command Command number of NRF24_COMMAND_*
499  /// \param[in] dest Array to write the bytes returned by the command to. Must be at least len bytes
500  /// \param[in] len Number of bytes to read
501  /// \return the value of the device status register
502  void spiBurstRead(uint8_t command, uint8_t* dest, uint8_t len);
503 
504  /// Write a number of consecutive bytes to a command using burst write mode
505  /// \param[in] command Command number of the first register, one of NRF24_COMMAND_*
506  /// \param[in] src Array of bytes to write. Must be at least len bytes
507  /// \param[in] len Number of bytes to write
508  /// \return the value of the device status register
509  uint8_t spiBurstWrite(uint8_t command, uint8_t* src, uint8_t len);
510 
511  /// Reads a single register from the NRF24
512  /// \param[in] reg Register number, one of NRF24_REG_*
513  /// \return The value of the register
514  uint8_t spiReadRegister(uint8_t reg);
515 
516  /// Writes a single byte to the NRF24, and at the ame time reads the current STATUS register
517  /// \param[in] reg Register number, one of NRF24_REG_*
518  /// \param[in] val The value to write
519  /// \return the current STATUS (read while the command is sent)
520  uint8_t spiWriteRegister(uint8_t reg, uint8_t val);
521 
522  /// Reads a number of consecutive registers from the NRF24 using burst read mode
523  /// \param[in] reg Register number of the first register, one of NRF24_REG_*
524  /// \param[in] dest Array to write the register values to. Must be at least len bytes
525  /// \param[in] len Number of bytes to read
526  /// \return the value of the device status register
527  void spiBurstReadRegister(uint8_t reg, uint8_t* dest, uint8_t len);
528 
529  /// Write a number of consecutive registers using burst write mode
530  /// \param[in] reg Register number of the first register, one of NRF24_REG_*
531  /// \param[in] src Array of new register values to write. Must be at least len bytes
532  /// \param[in] len Number of bytes to write
533  /// \return the value of the device status register
534  uint8_t spiBurstWriteRegister(uint8_t reg, uint8_t* src, uint8_t len);
535 
536  /// Reads and returns the device status register NRF24_REG_02_DEVICE_STATUS
537  /// \return The value of the device status register
538  uint8_t statusRead();
539 
540  /// Flush the TX FIFOs
541  /// \return the value of the device status register
542  uint8_t flushTx();
543 
544  /// Flush the RX FIFOs
545  /// \return the value of the device status register
546  uint8_t flushRx();
547 
548  /// Sets the transmit and receive channel number.
549  /// The frequency used is (2400 + channel) MHz
550  /// \return true on success
551  boolean setChannel(uint8_t channel);
552 
553  /// Sets the chip configuration that will be used to set
554  /// the NRF24 NRF24_REG_00_CONFIG register. This allows you to change some
555  /// chip configuration for compatibility with libraries other than this one.
556  /// You should not normally need to call this.
557  /// Defaults to NRF24_EN_CRC, which is the standard configuraiton for this library.
558  /// \param[in] configuration The chip configuration to be used.
559  /// \return true on success
560  boolean setConfiguration(uint8_t configuration);
561 
562  /// Sets the first len bytes of the address for the given NRF24 receiver pipe
563  /// This is an internal function and is not normally used by appications, but
564  /// can be used for specialised applications.
565  /// \param[in] pipe The index of the pipe to set, from 0 to 5
566  /// \param[in] address The new address for receiving. Must match the transmit address
567  /// of the transmitting node
568  /// \param[in] len Number of bytes of receive address to set.
569  /// \return true on success
570  boolean setPipeAddress(uint8_t pipe, uint8_t* address, uint8_t len);
571 
572  /// Set the Auto Retransmit Delay and Auto Retransmit Count
573  /// for Auto retransmission (ART). See section 7.4.2 in the NRF24 documentation
574  /// It may be very important to set an appropriate delay and count
575  /// for your application, especially with
576  /// 250kbps (i.e. slow) data rate. The defaults are OK for faster data rates. If
577  /// the delay is too short, the symptoms wil be unreliable transmission, or tranmsission failures
578  /// \param[in] delay The number of 250 microsecond intervals to wait for an ACK.
579  /// \param[in] count The number of retries to us.
580  /// \return true on success
581  boolean setRetry(uint8_t delay, uint8_t count = 3);
582 
583  /// Sets the first len bytes of the address of this node
584  /// Normally len is the same length as setAddressLength, but can be smaller in order to set the
585  /// least significant bytes of the address
586  /// \param[in] address The new address for receiving. Must match the setTransmitAddress of the transmitting node.
587  /// \param[in] len Number of bytes of receive address to set.
588  /// \return true on success
589  boolean setThisAddress(uint8_t* address, uint8_t len);
590 
591  /// Sets the next transmit address
592  /// \param[in] address The new address for transmitting. Must match the setThisAddress of the receiving node.
593  /// \param[in] len Number of bytes of receive address to set.
594  /// \return true on success
595  boolean setTransmitAddress(uint8_t* address, uint8_t len);
596 
597  /// Sets the number of bytes transmitted
598  /// in each payload
599  /// \param[in] size Size of the transmitted payload in bytes
600  /// \return true on success
601  boolean setPayloadSize(uint8_t size);
602 
603  /// Sets the data rate and tranmitter power to use
604  /// \param [in] data_rate The data rate to use for all packets transmitted and received. One of NRF24DataRate
605  /// \param [in] power Transmitter power. One of NRF24TransmitPower.
606  /// \return true on success
607  boolean setRF(uint8_t data_rate, uint8_t power);
608 
609  /// Sets the radio in power down mode.
610  /// Sets chip enable to LOW.
611  /// \return true on success
612  boolean powerDown();
613 
614  /// Sets the radio in RX mode.
615  /// Sets chip enable to HIGH to enable the chip in RX mode.
616  /// \return true on success
617  boolean powerUpRx();
618 
619  /// Sets the radio in TX mode.
620  /// Pulses the chip enable LOW then HIGH to enable the chip in TX mode.
621  /// \return true on success
622  boolean powerUpTx();
623 
624  /// Sends data to the address set by setTransmitAddress()
625  /// Sets the radio to TX mode
626  /// \param [in] data Data bytes to send.
627  /// \param [in] len Number of data bytes to set in teh TX buffer. The actual size of the
628  /// transmitted data payload is set by setPayloadSize
629  /// \param [in] noack Optional parameter if true sends the message NOACK mode. Requires that the NOACK feature be
630  /// enabled with spiWriteRegister(NRF24_REG_1D_FEATURE, NRF24_EN_DYN_ACK);
631  /// \return true on success
632  boolean send(uint8_t* data, uint8_t len, boolean noack = false);
633 
634  /// Blocks until the current message (if any)
635  /// has been transmitted
636  /// \return true on success, false if the Max retries were exceeded, or if the chip is not in transmit mode.
637  boolean waitPacketSent();
638 
639  /// Indicates if the chip is in transmit mode and
640  /// there is a packet currently being transmitted
641  /// \return true if the chip is in transmit mode and there is a transmission in progress
642  boolean isSending();
643 
644  /// Prints the value of all chip registers
645  /// for debugging purposes
646  /// \return true on success
647  boolean printRegisters();
648 
649  /// Checks whether a received message is available.
650  /// This can be called multiple times in a timeout loop
651  /// \return true if a complete, valid message has been received and is able to be retrieved by
652  /// recv()
653  boolean available();
654 
655  /// Starts the receiver and blocks until a valid received
656  /// message is available.
657  void waitAvailable();
658 
659  /// Starts the receiver and blocks until a received message is available or a timeout
660  /// \param[in] timeout Maximum time to wait in milliseconds.
661  /// \return true if a message is available
662  bool waitAvailableTimeout(uint16_t timeout);
663 
664  /// Turns the receiver on if it not already on.
665  /// If there is a valid message available, copy it to buf and return true
666  /// else return false.
667  /// If a message is copied, *len is set to the length (Caution, 0 length messages are permitted).
668  /// You should be sure to call this function frequently enough to not miss any messages
669  /// It is recommended that you call it in your main loop.
670  /// \param[in] buf Location to copy the received message
671  /// \param[in,out] len Pointer to available space in buf. Set to the actual number of octets copied.
672  /// \return true if a valid message was copied to buf
673  boolean recv(uint8_t* buf, uint8_t* len);
674 
675 protected:
676 
677 private:
678  uint8_t _configuration;
679  uint8_t _chipEnablePin;
680  uint8_t _chipSelectPin;
681 };
682 
683 /// @example nrf24_audio_rx.pde
684 /// Example sketch showing how to create an audio digital receiver
685 /// with the NRF24 class.
686 /// Works with the nrf24_audio_tx sample transmitter
687 /// Connect audio output to pin 6, through a low pass filter consisting of a 1k resistor in series followed by a
688 /// 0.0033 microfarad capacitor to ground (48kHz filter).
689 /// The audio quality is poor: dont expect hi-fi!
690 /// We have to change the PWM frequency to 62 kHz so we can get bandwidth reasonable
691 /// audio out through the low pass filter
692 /// Tested on UNO
693 
694 /// @example nrf24_audio_tx.pde
695 /// Example sketch showing how to create an audio digital transmitter
696 /// with the NRF24 class.
697 /// Connect a 1Vp-p audio sigal to analog input 0, connected through a 1uF capacitor
698 /// Works with the nrf24_audio_rx sample receiver
699 /// The audio quality is poor: dont expect hi-fi!
700 ///
701 /// This code sends about 250 messages per second, each with 32 8 bit samples from analog input 0
702 /// It uses the NRF4 in NOACK mode. The receiver never acknowledges or replies
703 /// Tested on UNO
704 
705 /// @example nrf24_ping_client.pde
706 /// Example sketch showing how to create a simple messageing client
707 /// with the NRF24 class.
708 /// It is designed to work with the example nrf24_ping_server
709 /// It also works with ping_server from the Mirf library
710 
711 /// @example nrf24_ping_server.pde
712 /// Example sketch showing how to create a simple messageing server
713 /// with the NRF24 class.
714 /// It is designed to work with the example nrf24_ping_client.
715 /// It also works with ping_client from the Mirf library
716 
717 /// @example nrf24_specan.pde
718 /// Example sketch showing how to create a primitive spectrum analyser
719 /// with the NRF24 class.
720 /// The nRF24L01 received power detector is only one bit, but
721 /// this will show which channels have more than -64dBm present
722 
723 /// @example nrf24_test.pde
724 /// Test suite for the NRF24 class.
725 
726 /// @example crazyflie.ino
727 /// This sketch act like a Crazyflie quadcopter http://www.bitcraze.se/
728 /// using the CRTP radiolink protocol:
729 /// http://wiki.bitcraze.se/projects:crazyflie:firmware:comm_protocol
730 
731 /// @example crazyflie_client.ino
732 /// This sketch act like a Crazyflie client (ie the transmitter) using the CRTP radiolink protocol:
733 /// http://wiki.bitcraze.se/projects:crazyflie:firmware:comm_protocol
734 /// to control a Crazyflie quadcopter http://www.bitcraze.se/
735 /// using a RC transmitter in trainer mode, such as the Spektrum DX6i and others.
736 #endif
NRF24(uint8_t chipEnablePin=8, uint8_t chipSelectPin=SS)
Definition: NRF24.cpp:9
void spiBurstReadRegister(uint8_t reg, uint8_t *dest, uint8_t len)
Definition: NRF24.cpp:112
250 kbps
Definition: NRF24.h:446
boolean setRetry(uint8_t delay, uint8_t count=3)
Definition: NRF24.cpp:154
boolean send(uint8_t *data, uint8_t len, boolean noack=false)
Definition: NRF24.cpp:225
-12 dBm
Definition: NRF24.h:455
uint8_t spiWriteRegister(uint8_t reg, uint8_t val)
Definition: NRF24.cpp:107
boolean isSending()
Definition: NRF24.cpp:253
uint8_t spiCommand(uint8_t command)
Definition: NRF24.cpp:54
boolean available()
Definition: NRF24.cpp:272
boolean recv(uint8_t *buf, uint8_t *len)
Definition: NRF24.cpp:305
-18 dBm
Definition: NRF24.h:454
uint8_t flushTx()
Definition: NRF24.cpp:128
uint8_t spiWrite(uint8_t command, uint8_t val)
Definition: NRF24.cpp:72
uint8_t flushRx()
Definition: NRF24.cpp:133
uint8_t spiReadRegister(uint8_t reg)
Definition: NRF24.cpp:102
0 dBm
Definition: NRF24.h:457
bool waitAvailableTimeout(uint16_t timeout)
Definition: NRF24.cpp:295
1 Mbps
Definition: NRF24.h:444
boolean setChannel(uint8_t channel)
Definition: NRF24.cpp:138
uint8_t spiBurstWriteRegister(uint8_t reg, uint8_t *src, uint8_t len)
Definition: NRF24.cpp:117
void waitAvailable()
Definition: NRF24.cpp:285
2 Mbps
Definition: NRF24.h:445
NRF24TransmitPower
Convenient values for setting transmitter power in setRF() These are designed to agree with the value...
Definition: NRF24.h:452
void spiBurstRead(uint8_t command, uint8_t *dest, uint8_t len)
Definition: NRF24.cpp:81
uint8_t spiRead(uint8_t command)
Definition: NRF24.cpp:63
uint8_t spiBurstWrite(uint8_t command, uint8_t *src, uint8_t len)
Definition: NRF24.cpp:91
boolean waitPacketSent()
Definition: NRF24.cpp:233
boolean powerDown()
Definition: NRF24.cpp:202
boolean setPipeAddress(uint8_t pipe, uint8_t *address, uint8_t len)
Definition: NRF24.cpp:148
boolean printRegisters()
Definition: NRF24.cpp:258
boolean setRF(uint8_t data_rate, uint8_t power)
Definition: NRF24.cpp:183
NRF24DataRate
Defines convenient values for setting data rates in setRF()
Definition: NRF24.h:442
Send and receive addressed, reliable, acknowledged datagrams by nRF24L01.
Definition: NRF24.h:437
boolean powerUpTx()
Definition: NRF24.cpp:216
boolean powerUpRx()
Definition: NRF24.cpp:209
uint8_t statusRead()
Definition: NRF24.cpp:122
boolean setPayloadSize(uint8_t size)
Definition: NRF24.cpp:176
boolean init()
Definition: NRF24.cpp:16
boolean setThisAddress(uint8_t *address, uint8_t len)
Definition: NRF24.cpp:160
boolean setConfiguration(uint8_t configuration)
Definition: NRF24.cpp:143
-6 dBm
Definition: NRF24.h:456
boolean setTransmitAddress(uint8_t *address, uint8_t len)
Definition: NRF24.cpp:168