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