PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

RH_NRF24.h 30KB

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